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. |
| 152 | uint64_t StrLength = |
| 153 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 154 | |
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(), |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 192 | diag::warn_initializer_string_for_char_array_too_long) |
| 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 | // ... |
| 356 | if (SemaRef.getLangOpts().CPlusPlus11 && |
| 357 | Entity.getType()->getBaseElementTypeUnsafe()->isRecordType()) { |
| 358 | // C++1y / DR1070: |
| 359 | // shall be initialized [...] from an empty initializer list. |
| 360 | // |
| 361 | // We apply the resolution of this DR to C++11 but not C++98, since C++98 |
| 362 | // does not have useful semantics for initialization from an init list. |
| 363 | // We treat this as copy-initialization, because aggregate initialization |
| 364 | // always performs copy-initialization on its elements. |
| 365 | // |
| 366 | // Only do this if we're initializing a class type, to avoid filling in |
| 367 | // the initializer list where possible. |
| 368 | InitExpr = VerifyOnly ? &DummyInitList : new (SemaRef.Context) |
| 369 | InitListExpr(SemaRef.Context, Loc, None, Loc); |
| 370 | InitExpr->setType(SemaRef.Context.VoidTy); |
| 371 | SubInit = InitExpr; |
| 372 | Kind = InitializationKind::CreateCopy(Loc, Loc); |
| 373 | } else { |
| 374 | // C++03: |
| 375 | // shall be value-initialized. |
| 376 | } |
| 377 | |
| 378 | InitializationSequence InitSeq(SemaRef, Entity, Kind, SubInit); |
| 379 | if (!InitSeq) { |
| 380 | if (!VerifyOnly) { |
| 381 | InitSeq.Diagnose(SemaRef, Entity, Kind, SubInit); |
| 382 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 383 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 384 | diag::note_in_omitted_aggregate_initializer) |
| 385 | << /*field*/1 << Entity.getDecl(); |
| 386 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) |
| 387 | SemaRef.Diag(Loc, diag::note_in_omitted_aggregate_initializer) |
| 388 | << /*array element*/0 << Entity.getElementIndex(); |
| 389 | } |
| 390 | return ExprError(); |
| 391 | } |
| 392 | |
| 393 | return VerifyOnly ? ExprResult(static_cast<Expr *>(nullptr)) |
| 394 | : InitSeq.Perform(SemaRef, Entity, Kind, SubInit); |
| 395 | } |
| 396 | |
| 397 | void InitListChecker::CheckEmptyInitializable(const InitializedEntity &Entity, |
| 398 | SourceLocation Loc) { |
| 399 | assert(VerifyOnly && |
| 400 | "CheckEmptyInitializable is only inteded for verification mode."); |
| 401 | if (PerformEmptyInit(SemaRef, Loc, Entity, /*VerifyOnly*/true).isInvalid()) |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 402 | hadError = true; |
| 403 | } |
| 404 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 405 | void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 406 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 407 | InitListExpr *ILE, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 408 | bool &RequiresSecondPass) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 409 | SourceLocation Loc = ILE->getLocEnd(); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 410 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 411 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 412 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 413 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 414 | // C++1y [dcl.init.aggr]p7: |
| 415 | // If there are fewer initializer-clauses in the list than there are |
| 416 | // members in the aggregate, then each member not explicitly initialized |
| 417 | // shall be initialized from its brace-or-equal-initializer [...] |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 418 | if (Field->hasInClassInitializer()) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 419 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, Loc, Field); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 420 | if (Init < NumInits) |
| 421 | ILE->setInit(Init, DIE); |
| 422 | else { |
| 423 | ILE->updateInit(SemaRef.Context, Init, DIE); |
| 424 | RequiresSecondPass = true; |
| 425 | } |
| 426 | return; |
| 427 | } |
| 428 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 429 | if (Field->getType()->isReferenceType()) { |
| 430 | // C++ [dcl.init.aggr]p9: |
| 431 | // If an incomplete or empty initializer-list leaves a |
| 432 | // member of reference type uninitialized, the program is |
| 433 | // ill-formed. |
| 434 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 435 | << Field->getType() |
| 436 | << ILE->getSyntacticForm()->getSourceRange(); |
| 437 | SemaRef.Diag(Field->getLocation(), |
| 438 | diag::note_uninit_reference_member); |
| 439 | hadError = true; |
| 440 | return; |
| 441 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 442 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 443 | ExprResult MemberInit = PerformEmptyInit(SemaRef, Loc, MemberEntity, |
| 444 | /*VerifyOnly*/false); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 445 | if (MemberInit.isInvalid()) { |
| 446 | hadError = true; |
| 447 | return; |
| 448 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 449 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 450 | if (hadError) { |
| 451 | // Do nothing |
| 452 | } else if (Init < NumInits) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 453 | ILE->setInit(Init, MemberInit.getAs<Expr>()); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 454 | } else if (!isa<ImplicitValueInitExpr>(MemberInit.get())) { |
| 455 | // Empty initialization requires a constructor call, so |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 456 | // extend the initializer list to include the constructor |
| 457 | // call and make a note that we'll need to take another pass |
| 458 | // through the initializer list. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 459 | ILE->updateInit(SemaRef.Context, Init, MemberInit.getAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 460 | RequiresSecondPass = true; |
| 461 | } |
| 462 | } else if (InitListExpr *InnerILE |
| 463 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 464 | FillInEmptyInitializations(MemberEntity, InnerILE, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 465 | RequiresSecondPass); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 468 | /// Recursively replaces NULL values within the given initializer list |
| 469 | /// with expressions that perform value-initialization of the |
| 470 | /// appropriate type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 471 | void |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 472 | InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 473 | InitListExpr *ILE, |
| 474 | bool &RequiresSecondPass) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 476 | "Should not have void type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 478 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 479 | const RecordDecl *RDecl = RType->getDecl(); |
| 480 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 481 | FillInEmptyInitForField(0, ILE->getInitializedFieldInUnion(), |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 482 | Entity, ILE, RequiresSecondPass); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 483 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 484 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 485 | for (auto *Field : RDecl->fields()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 486 | if (Field->hasInClassInitializer()) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 487 | FillInEmptyInitForField(0, Field, Entity, ILE, RequiresSecondPass); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 488 | break; |
| 489 | } |
| 490 | } |
| 491 | } else { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 492 | unsigned Init = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 493 | for (auto *Field : RDecl->fields()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 494 | if (Field->isUnnamedBitfield()) |
| 495 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 496 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 497 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 498 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 499 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 500 | FillInEmptyInitForField(Init, Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 501 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 502 | return; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 504 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 505 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 506 | // Only look at the first initialization of a union. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 507 | if (RDecl->isUnion()) |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 508 | break; |
| 509 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 514 | |
| 515 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 517 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 518 | unsigned NumInits = ILE->getNumInits(); |
| 519 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 520 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 521 | ElementType = AType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 522 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 523 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 524 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 525 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 526 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 527 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 528 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 529 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 530 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 532 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 534 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 535 | if (hadError) |
| 536 | return; |
| 537 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 538 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 539 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 540 | ElementEntity.setElementIndex(Init); |
| 541 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 542 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : nullptr); |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 543 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 544 | ExprResult ElementInit = PerformEmptyInit(SemaRef, ILE->getLocEnd(), |
| 545 | ElementEntity, |
| 546 | /*VerifyOnly*/false); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 547 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 548 | hadError = true; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 549 | return; |
| 550 | } |
| 551 | |
| 552 | if (hadError) { |
| 553 | // Do nothing |
| 554 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 555 | // For arrays, just set the expression used for value-initialization |
| 556 | // of the "holes" in the array. |
| 557 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 558 | ILE->setArrayFiller(ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 559 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 560 | ILE->setInit(Init, ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 561 | } else { |
| 562 | // For arrays, just set the expression used for value-initialization |
| 563 | // of the rest of elements and exit. |
| 564 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 565 | ILE->setArrayFiller(ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 566 | return; |
| 567 | } |
| 568 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 569 | if (!isa<ImplicitValueInitExpr>(ElementInit.get())) { |
| 570 | // Empty initialization requires a constructor call, so |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 571 | // extend the initializer list to include the constructor |
| 572 | // call and make a note that we'll need to take another pass |
| 573 | // through the initializer list. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 574 | ILE->updateInit(SemaRef.Context, Init, ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 575 | RequiresSecondPass = true; |
| 576 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 577 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 578 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 579 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 580 | FillInEmptyInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 584 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 585 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 586 | InitListExpr *IL, QualType &T, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 587 | bool VerifyOnly) |
| 588 | : SemaRef(S), VerifyOnly(VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 589 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 590 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 591 | FullyStructuredList = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 592 | getStructuredSubobjectInit(IL, 0, T, nullptr, 0, IL->getSourceRange()); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 593 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 594 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 595 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 596 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 597 | bool RequiresSecondPass = false; |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 598 | FillInEmptyInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 599 | if (RequiresSecondPass && !hadError) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 600 | FillInEmptyInitializations(Entity, FullyStructuredList, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 601 | RequiresSecondPass); |
| 602 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 606 | // FIXME: use a proper constant |
| 607 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 608 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 609 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 610 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 611 | } |
| 612 | return maxElements; |
| 613 | } |
| 614 | |
| 615 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 616 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 617 | int InitializableMembers = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 618 | for (const auto *Field : structDecl->fields()) |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 619 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 620 | ++InitializableMembers; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 621 | |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 622 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 623 | return std::min(InitializableMembers, 1); |
| 624 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 627 | /// Check whether the range of the initializer \p ParentIList from element |
| 628 | /// \p Index onwards can be used to initialize an object of type \p T. Update |
| 629 | /// \p Index to indicate how many elements of the list were consumed. |
| 630 | /// |
| 631 | /// This also fills in \p StructuredList, from element \p StructuredIndex |
| 632 | /// onwards, with the fully-braced, desugared form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 633 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 634 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 635 | QualType T, unsigned &Index, |
| 636 | InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 637 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 638 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 640 | if (T->isArrayType()) |
| 641 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 642 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 643 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 644 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 645 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 646 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 647 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 648 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 649 | if (maxElements == 0) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 650 | if (!VerifyOnly) |
| 651 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 652 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 653 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 654 | hadError = true; |
| 655 | return; |
| 656 | } |
| 657 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 658 | // Build a structured initializer list corresponding to this subobject. |
| 659 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 661 | StructuredIndex, |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 662 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 663 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 664 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 665 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 666 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 667 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 668 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 669 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | StructuredSubobjectInitList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 671 | StructuredSubobjectInitIndex); |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 672 | |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 673 | if (!VerifyOnly) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 674 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 675 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 676 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 677 | // Update the structured sub-object initializer so that it's ending |
| 678 | // range corresponds with the end of the last initializer it used. |
| 679 | if (EndIndex < ParentIList->getNumInits()) { |
| 680 | SourceLocation EndLoc |
| 681 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 682 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 683 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 684 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 685 | // Complain about missing braces. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 686 | if (T->isArrayType() || T->isRecordType()) { |
| 687 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 688 | diag::warn_missing_braces) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 689 | << StructuredSubobjectInitList->getSourceRange() |
| 690 | << FixItHint::CreateInsertion( |
| 691 | StructuredSubobjectInitList->getLocStart(), "{") |
| 692 | << FixItHint::CreateInsertion( |
| 693 | SemaRef.getLocForEndOfToken( |
| 694 | StructuredSubobjectInitList->getLocEnd()), |
| 695 | "}"); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 696 | } |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 697 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 700 | /// Check whether the initializer \p IList (that was written with explicit |
| 701 | /// braces) can be used to initialize an object of type \p T. |
| 702 | /// |
| 703 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 704 | /// form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 705 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 706 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 707 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 708 | bool TopLevelObject) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 709 | if (!VerifyOnly) { |
| 710 | SyntacticToSemantic[IList] = StructuredList; |
| 711 | StructuredList->setSyntacticForm(IList); |
| 712 | } |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 713 | |
| 714 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 715 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 716 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 717 | if (!VerifyOnly) { |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 718 | QualType ExprTy = T; |
| 719 | if (!ExprTy->isArrayType()) |
| 720 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 721 | IList->setType(ExprTy); |
| 722 | StructuredList->setType(ExprTy); |
| 723 | } |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 724 | if (hadError) |
| 725 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 726 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 727 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 728 | // We have leftover initializers |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 729 | if (VerifyOnly) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 730 | if (SemaRef.getLangOpts().CPlusPlus || |
| 731 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 732 | IList->getType()->isVectorType())) { |
| 733 | hadError = true; |
| 734 | } |
| 735 | return; |
| 736 | } |
| 737 | |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 738 | if (StructuredIndex == 1 && |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 739 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 740 | SIF_None) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 741 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 742 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 743 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 744 | hadError = true; |
| 745 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 746 | // Special-case |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 747 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 748 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 749 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 750 | // Don't complain for incomplete types, since we'll get an error |
| 751 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 752 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 754 | CurrentObjectType->isArrayType()? 0 : |
| 755 | CurrentObjectType->isVectorType()? 1 : |
| 756 | CurrentObjectType->isScalarType()? 2 : |
| 757 | CurrentObjectType->isUnionType()? 3 : |
| 758 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 759 | |
| 760 | unsigned DK = diag::warn_excess_initializers; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 761 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 762 | DK = diag::err_excess_initializers; |
| 763 | hadError = true; |
| 764 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 765 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 766 | DK = diag::err_excess_initializers; |
| 767 | hadError = true; |
| 768 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 769 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 770 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 771 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 772 | } |
| 773 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 774 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 775 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 776 | !TopLevelObject) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 777 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 778 | << IList->getSourceRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 779 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 780 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 783 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 784 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 786 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 787 | unsigned &Index, |
| 788 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 789 | unsigned &StructuredIndex, |
| 790 | bool TopLevelObject) { |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 791 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 792 | // Explicitly braced initializer for complex type can be real+imaginary |
| 793 | // parts. |
| 794 | CheckComplexType(Entity, IList, DeclType, Index, |
| 795 | StructuredList, StructuredIndex); |
| 796 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 797 | CheckScalarType(Entity, IList, DeclType, Index, |
| 798 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 799 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 800 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 801 | StructuredList, StructuredIndex); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 802 | } else if (DeclType->isRecordType()) { |
| 803 | assert(DeclType->isAggregateType() && |
| 804 | "non-aggregate records should be handed in CheckSubElementType"); |
| 805 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 806 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 807 | SubobjectIsDesignatorContext, Index, |
| 808 | StructuredList, StructuredIndex, |
| 809 | TopLevelObject); |
| 810 | } else if (DeclType->isArrayType()) { |
| 811 | llvm::APSInt Zero( |
| 812 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 813 | false); |
| 814 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 815 | SubobjectIsDesignatorContext, Index, |
| 816 | StructuredList, StructuredIndex); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 817 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 818 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 819 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 820 | if (!VerifyOnly) |
| 821 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 822 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 823 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 824 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 825 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 826 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 827 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 828 | if (!VerifyOnly) |
| 829 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 830 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 831 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 832 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 833 | if (!VerifyOnly) |
| 834 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 835 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 836 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 840 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 841 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 843 | unsigned &Index, |
| 844 | InitListExpr *StructuredList, |
| 845 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 846 | Expr *expr = IList->getInit(Index); |
Richard Smith | 72752e8 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 847 | |
| 848 | if (ElemType->isReferenceType()) |
| 849 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 850 | StructuredList, StructuredIndex); |
| 851 | |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 852 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 853 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 854 | InitListExpr *InnerStructuredList |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 855 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 856 | StructuredList, StructuredIndex, |
| 857 | SubInitList->getSourceRange()); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 858 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 859 | InnerStructuredList); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 860 | ++StructuredIndex; |
| 861 | ++Index; |
| 862 | return; |
| 863 | } |
| 864 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 865 | "non-aggregate records are only possible in C++"); |
| 866 | // C++ initialization is handled later. |
| 867 | } |
| 868 | |
Eli Friedman | 4628cf7 | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 869 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
| 870 | if (ElemType->isScalarType() || ElemType->isAtomicType()) |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 871 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 872 | StructuredList, StructuredIndex); |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 873 | |
Eli Friedman | 4628cf7 | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 874 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
| 875 | ElemType->isArrayType()) && "Unexpected type"); |
| 876 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 877 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 878 | // arrayType can be incomplete if we're initializing a flexible |
| 879 | // array member. There's nothing we can do with the completed |
| 880 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 881 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 882 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 883 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 884 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 885 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 886 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 887 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 888 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 889 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 890 | |
| 891 | // Fall through for subaggregate initialization. |
| 892 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 893 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 894 | // C++ [dcl.init.aggr]p12: |
| 895 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 896 | // initializing the aggregate member with an initializer from |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 897 | // an initializer-list. If the initializer can initialize a |
| 898 | // member, the member is initialized. [...] |
| 899 | |
| 900 | // FIXME: Better EqualLoc? |
| 901 | InitializationKind Kind = |
| 902 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 903 | InitializationSequence Seq(SemaRef, Entity, Kind, expr); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 904 | |
| 905 | if (Seq) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 906 | if (!VerifyOnly) { |
Richard Smith | 0f8ede1 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 907 | ExprResult Result = |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 908 | Seq.Perform(SemaRef, Entity, Kind, expr); |
Richard Smith | 0f8ede1 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 909 | if (Result.isInvalid()) |
| 910 | hadError = true; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 911 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 912 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 913 | Result.getAs<Expr>()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 914 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 915 | ++Index; |
| 916 | return; |
| 917 | } |
| 918 | |
| 919 | // Fall through for subaggregate initialization |
| 920 | } else { |
| 921 | // C99 6.7.8p13: |
| 922 | // |
| 923 | // The initializer for a structure or union object that has |
| 924 | // automatic storage duration shall be either an initializer |
| 925 | // list as described below, or a single expression that has |
| 926 | // compatible structure or union type. In the latter case, the |
| 927 | // initial value of the object, including unnamed members, is |
| 928 | // that of the expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 929 | ExprResult ExprRes = expr; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 930 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 931 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 932 | !VerifyOnly) |
Eli Friedman | b2a8d46 | 2013-09-17 04:07:04 +0000 | [diff] [blame] | 933 | != Sema::Incompatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 934 | if (ExprRes.isInvalid()) |
| 935 | hadError = true; |
| 936 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 937 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 938 | if (ExprRes.isInvalid()) |
| 939 | hadError = true; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 940 | } |
| 941 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 942 | ExprRes.getAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 943 | ++Index; |
| 944 | return; |
| 945 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 946 | ExprRes.get(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 947 | // Fall through for subaggregate initialization |
| 948 | } |
| 949 | |
| 950 | // C++ [dcl.init.aggr]p12: |
| 951 | // |
| 952 | // [...] Otherwise, if the member is itself a non-empty |
| 953 | // subaggregate, brace elision is assumed and the initializer is |
| 954 | // considered for the initialization of the first member of |
| 955 | // the subaggregate. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 956 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 957 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 958 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 959 | StructuredIndex); |
| 960 | ++StructuredIndex; |
| 961 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 962 | if (!VerifyOnly) { |
| 963 | // We cannot initialize this element, so let |
| 964 | // PerformCopyInitialization produce the appropriate diagnostic. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 965 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 966 | /*TopLevelOfInitList=*/true); |
| 967 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 968 | hadError = true; |
| 969 | ++Index; |
| 970 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 971 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 974 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 975 | InitListExpr *IList, QualType DeclType, |
| 976 | unsigned &Index, |
| 977 | InitListExpr *StructuredList, |
| 978 | unsigned &StructuredIndex) { |
| 979 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 980 | |
| 981 | // As an extension, clang supports complex initializers, which initialize |
| 982 | // a complex number component-wise. When an explicit initializer list for |
| 983 | // a complex number contains two two initializers, this extension kicks in: |
| 984 | // it exepcts the initializer list to contain two elements convertible to |
| 985 | // the element type of the complex type. The first element initializes |
| 986 | // the real part, and the second element intitializes the imaginary part. |
| 987 | |
| 988 | if (IList->getNumInits() != 2) |
| 989 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 990 | StructuredIndex); |
| 991 | |
| 992 | // This is an extension in C. (The builtin _Complex type does not exist |
| 993 | // in the C++ standard.) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 994 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 995 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 996 | << IList->getSourceRange(); |
| 997 | |
| 998 | // Initialize the complex number. |
| 999 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 1000 | InitializedEntity ElementEntity = |
| 1001 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1002 | |
| 1003 | for (unsigned i = 0; i < 2; ++i) { |
| 1004 | ElementEntity.setElementIndex(Index); |
| 1005 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1006 | StructuredList, StructuredIndex); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1011 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1012 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1013 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1014 | InitListExpr *StructuredList, |
| 1015 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1016 | if (Index >= IList->getNumInits()) { |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1017 | if (!VerifyOnly) |
| 1018 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1019 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1020 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 1021 | diag::err_empty_scalar_initializer) |
| 1022 | << IList->getSourceRange(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1023 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1024 | ++Index; |
| 1025 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1026 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1027 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1028 | |
| 1029 | Expr *expr = IList->getInit(Index); |
| 1030 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1031 | // FIXME: This is invalid, and accepting it causes overload resolution |
| 1032 | // to pick the wrong overload in some corner cases. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1033 | if (!VerifyOnly) |
| 1034 | SemaRef.Diag(SubIList->getLocStart(), |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1035 | diag::ext_many_braces_around_scalar_init) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1036 | << SubIList->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1037 | |
| 1038 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1039 | StructuredIndex); |
| 1040 | return; |
| 1041 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1042 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1043 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1044 | diag::err_designator_for_scalar_init) |
| 1045 | << DeclType << expr->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1046 | hadError = true; |
| 1047 | ++Index; |
| 1048 | ++StructuredIndex; |
| 1049 | return; |
| 1050 | } |
| 1051 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1052 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1053 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1054 | hadError = true; |
| 1055 | ++Index; |
| 1056 | return; |
| 1057 | } |
| 1058 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1059 | ExprResult Result = |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1060 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), expr, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1061 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1062 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1063 | Expr *ResultExpr = nullptr; |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1064 | |
| 1065 | if (Result.isInvalid()) |
| 1066 | hadError = true; // types weren't compatible. |
| 1067 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1068 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1069 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1070 | if (ResultExpr != expr) { |
| 1071 | // The type was promoted, update initializer list. |
| 1072 | IList->setInit(Index, ResultExpr); |
| 1073 | } |
| 1074 | } |
| 1075 | if (hadError) |
| 1076 | ++StructuredIndex; |
| 1077 | else |
| 1078 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1079 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1082 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1083 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1084 | unsigned &Index, |
| 1085 | InitListExpr *StructuredList, |
| 1086 | unsigned &StructuredIndex) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1087 | if (Index >= IList->getNumInits()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1088 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1089 | // general, it would be useful to pass location information down the stack, |
| 1090 | // so that we know the location (or decl) of the "current object" being |
| 1091 | // initialized. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1092 | if (!VerifyOnly) |
| 1093 | SemaRef.Diag(IList->getLocStart(), |
| 1094 | diag::err_init_reference_member_uninitialized) |
| 1095 | << DeclType |
| 1096 | << IList->getSourceRange(); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1097 | hadError = true; |
| 1098 | ++Index; |
| 1099 | ++StructuredIndex; |
| 1100 | return; |
| 1101 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1102 | |
| 1103 | Expr *expr = IList->getInit(Index); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1104 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1105 | if (!VerifyOnly) |
| 1106 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1107 | << DeclType << IList->getSourceRange(); |
| 1108 | hadError = true; |
| 1109 | ++Index; |
| 1110 | ++StructuredIndex; |
| 1111 | return; |
| 1112 | } |
| 1113 | |
| 1114 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1115 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1116 | hadError = true; |
| 1117 | ++Index; |
| 1118 | return; |
| 1119 | } |
| 1120 | |
| 1121 | ExprResult Result = |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1122 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), expr, |
| 1123 | /*TopLevelOfInitList=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1124 | |
| 1125 | if (Result.isInvalid()) |
| 1126 | hadError = true; |
| 1127 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1128 | expr = Result.getAs<Expr>(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1129 | IList->setInit(Index, expr); |
| 1130 | |
| 1131 | if (hadError) |
| 1132 | ++StructuredIndex; |
| 1133 | else |
| 1134 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1135 | ++Index; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1138 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1139 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1140 | unsigned &Index, |
| 1141 | InitListExpr *StructuredList, |
| 1142 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1143 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1144 | unsigned maxElements = VT->getNumElements(); |
| 1145 | unsigned numEltsInit = 0; |
| 1146 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1147 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1148 | if (Index >= IList->getNumInits()) { |
| 1149 | // Make sure the element type can be value-initialized. |
| 1150 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1151 | CheckEmptyInitializable( |
| 1152 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1153 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1154 | return; |
| 1155 | } |
| 1156 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1157 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1158 | // If the initializing element is a vector, try to copy-initialize |
| 1159 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1160 | Expr *Init = IList->getInit(Index); |
| 1161 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1162 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1163 | if (!SemaRef.CanPerformCopyInitialization(Entity, Init)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1164 | hadError = true; |
| 1165 | ++Index; |
| 1166 | return; |
| 1167 | } |
| 1168 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1169 | ExprResult Result = |
| 1170 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), Init, |
| 1171 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1172 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1173 | Expr *ResultExpr = nullptr; |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1174 | if (Result.isInvalid()) |
| 1175 | hadError = true; // types weren't compatible. |
| 1176 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1177 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1178 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1179 | if (ResultExpr != Init) { |
| 1180 | // The type was promoted, update initializer list. |
| 1181 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1182 | } |
| 1183 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1184 | if (hadError) |
| 1185 | ++StructuredIndex; |
| 1186 | else |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1187 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1188 | ResultExpr); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1189 | ++Index; |
| 1190 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1191 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1192 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1193 | InitializedEntity ElementEntity = |
| 1194 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1195 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1196 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1197 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1198 | if (Index >= IList->getNumInits()) { |
| 1199 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1200 | CheckEmptyInitializable(ElementEntity, IList->getLocEnd()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1201 | break; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1202 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1203 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1204 | ElementEntity.setElementIndex(Index); |
| 1205 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1206 | StructuredList, StructuredIndex); |
| 1207 | } |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame^] | 1208 | |
| 1209 | if (VerifyOnly) |
| 1210 | return; |
| 1211 | |
| 1212 | bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian(); |
| 1213 | const VectorType *T = Entity.getType()->getAs<VectorType>(); |
| 1214 | if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector || |
| 1215 | T->getVectorKind() == VectorType::NeonPolyVector)) { |
| 1216 | // The ability to use vector initializer lists is a GNU vector extension |
| 1217 | // and is unrelated to the NEON intrinsics in arm_neon.h. On little |
| 1218 | // endian machines it works fine, however on big endian machines it |
| 1219 | // exhibits surprising behaviour: |
| 1220 | // |
| 1221 | // uint32x2_t x = {42, 64}; |
| 1222 | // return vget_lane_u32(x, 0); // Will return 64. |
| 1223 | // |
| 1224 | // Because of this, explicitly call out that it is non-portable. |
| 1225 | // |
| 1226 | SemaRef.Diag(IList->getLocStart(), |
| 1227 | diag::warn_neon_vector_initializer_non_portable); |
| 1228 | |
| 1229 | const char *typeCode; |
| 1230 | unsigned typeSize = SemaRef.Context.getTypeSize(elementType); |
| 1231 | |
| 1232 | if (elementType->isFloatingType()) |
| 1233 | typeCode = "f"; |
| 1234 | else if (elementType->isSignedIntegerType()) |
| 1235 | typeCode = "s"; |
| 1236 | else if (elementType->isUnsignedIntegerType()) |
| 1237 | typeCode = "u"; |
| 1238 | else |
| 1239 | llvm_unreachable("Invalid element type!"); |
| 1240 | |
| 1241 | SemaRef.Diag(IList->getLocStart(), |
| 1242 | SemaRef.Context.getTypeSize(VT) > 64 ? |
| 1243 | diag::note_neon_vector_initializer_non_portable_q : |
| 1244 | diag::note_neon_vector_initializer_non_portable) |
| 1245 | << typeCode << typeSize; |
| 1246 | } |
| 1247 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1248 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1249 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1250 | |
| 1251 | InitializedEntity ElementEntity = |
| 1252 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1253 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1254 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1255 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1256 | // Don't attempt to go past the end of the init list |
| 1257 | if (Index >= IList->getNumInits()) |
| 1258 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1259 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1260 | ElementEntity.setElementIndex(Index); |
| 1261 | |
| 1262 | QualType IType = IList->getInit(Index)->getType(); |
| 1263 | if (!IType->isVectorType()) { |
| 1264 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1265 | StructuredList, StructuredIndex); |
| 1266 | ++numEltsInit; |
| 1267 | } else { |
| 1268 | QualType VecType; |
| 1269 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1270 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1271 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1272 | if (IType->isExtVectorType()) |
| 1273 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1274 | else |
| 1275 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1276 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1277 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1278 | StructuredList, StructuredIndex); |
| 1279 | numEltsInit += numIElts; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1284 | if (numEltsInit != maxElements) { |
| 1285 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1286 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1287 | diag::err_vector_incorrect_num_initializers) |
| 1288 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1289 | hadError = true; |
| 1290 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1293 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1294 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1295 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1296 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1297 | unsigned &Index, |
| 1298 | InitListExpr *StructuredList, |
| 1299 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1300 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1301 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1302 | // Check for the special-case of initializing an array with a string. |
| 1303 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1304 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1305 | SIF_None) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1306 | // We place the string literal directly into the resulting |
| 1307 | // initializer list. This is the only place where the structure |
| 1308 | // of the structured initializer list doesn't match exactly, |
| 1309 | // because doing so would involve allocating one character |
| 1310 | // constant for each string. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1311 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1312 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1313 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1314 | IList->getInit(Index)); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1315 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1316 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1317 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1318 | return; |
| 1319 | } |
| 1320 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1321 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1322 | // Check for VLAs; in standard C it would be possible to check this |
| 1323 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1324 | // them in all sorts of strange places). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1325 | if (!VerifyOnly) |
| 1326 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1327 | diag::err_variable_object_no_init) |
| 1328 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1329 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1330 | ++Index; |
| 1331 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1332 | return; |
| 1333 | } |
| 1334 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1335 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1336 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1337 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1338 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1339 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1340 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1341 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1342 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1343 | maxElementsKnown = true; |
| 1344 | } |
| 1345 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1346 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1347 | while (Index < IList->getNumInits()) { |
| 1348 | Expr *Init = IList->getInit(Index); |
| 1349 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1350 | // If we're not the subobject that matches up with the '{' for |
| 1351 | // the designator, we shouldn't be handling the |
| 1352 | // designator. Return immediately. |
| 1353 | if (!SubobjectIsDesignatorContext) |
| 1354 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1355 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1356 | // Handle this designated initializer. elementIndex will be |
| 1357 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1358 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1359 | DeclType, nullptr, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1360 | StructuredList, StructuredIndex, true, |
| 1361 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1362 | hadError = true; |
| 1363 | continue; |
| 1364 | } |
| 1365 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1366 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1367 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1368 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1369 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1370 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1371 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1372 | // If the array is of incomplete type, keep track of the number of |
| 1373 | // elements in the initializer. |
| 1374 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1375 | maxElements = elementIndex; |
| 1376 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1377 | continue; |
| 1378 | } |
| 1379 | |
| 1380 | // If we know the maximum number of elements, and we've already |
| 1381 | // hit it, stop consuming elements in the initializer list. |
| 1382 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1383 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1384 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1385 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1386 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1387 | Entity); |
| 1388 | // Check this element. |
| 1389 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1390 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1391 | ++elementIndex; |
| 1392 | |
| 1393 | // If the array is of incomplete type, keep track of the number of |
| 1394 | // elements in the initializer. |
| 1395 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1396 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1397 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1398 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1399 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1400 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1401 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1402 | if (maxElements == Zero) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1403 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1404 | // but is supported by GNU. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1405 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1406 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1407 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1408 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1409 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1410 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1411 | } |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1412 | if (!hadError && VerifyOnly) { |
| 1413 | // Check if there are any members of the array that get value-initialized. |
| 1414 | // If so, check if doing that is possible. |
| 1415 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1416 | if (maxElementsKnown && elementIndex < maxElements) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1417 | CheckEmptyInitializable(InitializedEntity::InitializeElement( |
| 1418 | SemaRef.Context, 0, Entity), |
| 1419 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1420 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1423 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1424 | Expr *InitExpr, |
| 1425 | FieldDecl *Field, |
| 1426 | bool TopLevelObject) { |
| 1427 | // Handle GNU flexible array initializers. |
| 1428 | unsigned FlexArrayDiag; |
| 1429 | if (isa<InitListExpr>(InitExpr) && |
| 1430 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1431 | // Empty flexible array init always allowed as an extension |
| 1432 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1433 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1434 | // Disallow flexible array init in C++; it is not required for gcc |
| 1435 | // compatibility, and it needs work to IRGen correctly in general. |
| 1436 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1437 | } else if (!TopLevelObject) { |
| 1438 | // Disallow flexible array init on non-top-level object |
| 1439 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1440 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1441 | // Disallow flexible array init on anything which is not a variable. |
| 1442 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1443 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1444 | // Disallow flexible array init on local variables. |
| 1445 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1446 | } else { |
| 1447 | // Allow other cases. |
| 1448 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1449 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1450 | |
| 1451 | if (!VerifyOnly) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1452 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1453 | FlexArrayDiag) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1454 | << InitExpr->getLocStart(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1455 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1456 | << Field; |
| 1457 | } |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1458 | |
| 1459 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1460 | } |
| 1461 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1462 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1463 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | QualType DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1465 | RecordDecl::field_iterator Field, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1467 | unsigned &Index, |
| 1468 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1469 | unsigned &StructuredIndex, |
| 1470 | bool TopLevelObject) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1471 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1473 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1474 | // confusion, we forgo checking the intializer for the entire record. |
| 1475 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 845aa66 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1476 | // Assume it was supposed to consume a single initializer. |
| 1477 | ++Index; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1478 | hadError = true; |
| 1479 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1481 | |
| 1482 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1483 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1484 | |
| 1485 | // If there's a default initializer, use it. |
| 1486 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1487 | if (VerifyOnly) |
| 1488 | return; |
| 1489 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1490 | Field != FieldEnd; ++Field) { |
| 1491 | if (Field->hasInClassInitializer()) { |
| 1492 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1493 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1494 | return; |
| 1495 | } |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | // Value-initialize the first named member of the union. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1500 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1501 | Field != FieldEnd; ++Field) { |
| 1502 | if (Field->getDeclName()) { |
| 1503 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1504 | CheckEmptyInitializable( |
| 1505 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 1506 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1507 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1508 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1509 | break; |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | return; |
| 1513 | } |
| 1514 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1515 | // If structDecl is a forward declaration, this loop won't do |
| 1516 | // anything except look at designated initializers; That's okay, |
| 1517 | // because an error should get printed out elsewhere. It might be |
| 1518 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1519 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1520 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1521 | bool InitializedSomething = false; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1522 | bool CheckForMissingFields = true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1523 | while (Index < IList->getNumInits()) { |
| 1524 | Expr *Init = IList->getInit(Index); |
| 1525 | |
| 1526 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1527 | // If we're not the subobject that matches up with the '{' for |
| 1528 | // the designator, we shouldn't be handling the |
| 1529 | // designator. Return immediately. |
| 1530 | if (!SubobjectIsDesignatorContext) |
| 1531 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1532 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1533 | // Handle this designated initializer. Field will be updated to |
| 1534 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1535 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1536 | DeclType, &Field, nullptr, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1537 | StructuredList, StructuredIndex, |
| 1538 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1539 | hadError = true; |
| 1540 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1541 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1542 | |
| 1543 | // Disable check for missing fields when designators are used. |
| 1544 | // This matches gcc behaviour. |
| 1545 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1546 | continue; |
| 1547 | } |
| 1548 | |
| 1549 | if (Field == FieldEnd) { |
| 1550 | // We've run out of fields. We're done. |
| 1551 | break; |
| 1552 | } |
| 1553 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1554 | // We've already initialized a member of a union. We're done. |
| 1555 | if (InitializedSomething && DeclType->isUnionType()) |
| 1556 | break; |
| 1557 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1558 | // If we've hit the flexible array member at the end, we're done. |
| 1559 | if (Field->getType()->isIncompleteArrayType()) |
| 1560 | break; |
| 1561 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1562 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1563 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1564 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1565 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1566 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1567 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1568 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1569 | bool InvalidUse; |
| 1570 | if (VerifyOnly) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1571 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1572 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1573 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1574 | IList->getInit(Index)->getLocStart()); |
| 1575 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1576 | ++Index; |
| 1577 | ++Field; |
| 1578 | hadError = true; |
| 1579 | continue; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1580 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1581 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1582 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1583 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1584 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1585 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1586 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1587 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1588 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1589 | // Initialize the first field within the union. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1590 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1591 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1592 | |
| 1593 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1594 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1595 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1596 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1597 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1598 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1599 | !DeclType->isUnionType()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1600 | // It is possible we have one or more unnamed bitfields remaining. |
| 1601 | // Find first (if any) named field and emit warning. |
| 1602 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1603 | it != end; ++it) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1604 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1605 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
Aaron Ballman | b9bb201 | 2014-01-03 14:54:10 +0000 | [diff] [blame] | 1606 | diag::warn_missing_field_initializers) << *it; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1607 | break; |
| 1608 | } |
| 1609 | } |
| 1610 | } |
| 1611 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1612 | // Check that any remaining fields can be value-initialized. |
| 1613 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1614 | !Field->getType()->isIncompleteArrayType()) { |
| 1615 | // FIXME: Should check for holes left by designated initializers too. |
| 1616 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1617 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1618 | CheckEmptyInitializable( |
| 1619 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 1620 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1625 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1626 | return; |
| 1627 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1628 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1629 | TopLevelObject)) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1630 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1631 | ++Index; |
| 1632 | return; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1635 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1636 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1637 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1638 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1639 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1640 | StructuredList, StructuredIndex); |
| 1641 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1642 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1643 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1644 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1645 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1646 | /// \brief Expand a field designator that refers to a member of an |
| 1647 | /// anonymous struct or union into a series of field designators that |
| 1648 | /// refers to the field within the appropriate subobject. |
| 1649 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1650 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | DesignatedInitExpr *DIE, |
| 1652 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1653 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1654 | typedef DesignatedInitExpr::Designator Designator; |
| 1655 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1656 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1657 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1658 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1659 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1660 | if (PI + 1 == PE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1661 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1662 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1663 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1664 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1665 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 1666 | SourceLocation(), SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1667 | assert(isa<FieldDecl>(*PI)); |
| 1668 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | // Expand the current designator into the set of replacement |
| 1672 | // designators, so we have a full subobject path down to where the |
| 1673 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1674 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1675 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1676 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1678 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1679 | /// corresponds to FieldName. |
| 1680 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1681 | IdentifierInfo *FieldName) { |
Argyrios Kyrtzidis | 54b1029 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1682 | if (!FieldName) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1683 | return nullptr; |
Argyrios Kyrtzidis | 54b1029 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1684 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1685 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1686 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 6d1bebb | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1687 | while (IndirectFieldDecl *IF = |
| 1688 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Argyrios Kyrtzidis | 54b1029 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1689 | if (FieldName == IF->getAnonField()->getIdentifier()) |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1690 | return IF; |
| 1691 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1692 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1693 | return nullptr; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1696 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1697 | DesignatedInitExpr *DIE) { |
| 1698 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1699 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1700 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1701 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1702 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1703 | DIE->size(), IndexExprs, |
| 1704 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1705 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1706 | } |
| 1707 | |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1708 | namespace { |
| 1709 | |
| 1710 | // Callback to only accept typo corrections that are for field members of |
| 1711 | // the given struct or union. |
| 1712 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1713 | public: |
| 1714 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1715 | : Record(RD) {} |
| 1716 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1717 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1718 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1719 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1720 | } |
| 1721 | |
| 1722 | private: |
| 1723 | RecordDecl *Record; |
| 1724 | }; |
| 1725 | |
| 1726 | } |
| 1727 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1728 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1729 | /// |
| 1730 | /// Determines whether the designated initializer @p DIE, which |
| 1731 | /// resides at the given @p Index within the initializer list @p |
| 1732 | /// IList, is well-formed for a current object of type @p DeclType |
| 1733 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1735 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1736 | /// |
| 1737 | /// @param IList The initializer list in which this designated |
| 1738 | /// initializer occurs. |
| 1739 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1740 | /// @param DIE The designated initializer expression. |
| 1741 | /// |
| 1742 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1743 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1744 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1745 | /// into which the designation in @p DIE should refer. |
| 1746 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1747 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1748 | /// a field, this will be set to the field declaration corresponding |
| 1749 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1750 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1751 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1752 | /// DIE is an array designator or GNU array-range designator, this |
| 1753 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1754 | /// |
| 1755 | /// @param Index Index into @p IList where the designated initializer |
| 1756 | /// @p DIE occurs. |
| 1757 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1758 | /// @param StructuredList The initializer list expression that |
| 1759 | /// describes all of the subobject initializers in the order they'll |
| 1760 | /// actually be initialized. |
| 1761 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1762 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1763 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1764 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1765 | InitListExpr *IList, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1766 | DesignatedInitExpr *DIE, |
| 1767 | unsigned DesigIdx, |
| 1768 | QualType &CurrentObjectType, |
| 1769 | RecordDecl::field_iterator *NextField, |
| 1770 | llvm::APSInt *NextElementIndex, |
| 1771 | unsigned &Index, |
| 1772 | InitListExpr *StructuredList, |
| 1773 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1774 | bool FinishSubobjectInit, |
| 1775 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1776 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1777 | // Check the actual initialization for the designated object type. |
| 1778 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1779 | |
| 1780 | // Temporarily remove the designator expression from the |
| 1781 | // initializer list that the child calls see, so that we don't try |
| 1782 | // to re-process the designator. |
| 1783 | unsigned OldIndex = Index; |
| 1784 | IList->setInit(OldIndex, DIE->getInit()); |
| 1785 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1786 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1787 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1788 | |
| 1789 | // Restore the designated initializer expression in the syntactic |
| 1790 | // form of the initializer list. |
| 1791 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1792 | DIE->setInit(IList->getInit(OldIndex)); |
| 1793 | IList->setInit(OldIndex, DIE); |
| 1794 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1795 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1798 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1799 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1800 | if (!VerifyOnly) { |
| 1801 | assert((IsFirstDesignator || StructuredList) && |
| 1802 | "Need a non-designated initializer list to start from"); |
| 1803 | |
| 1804 | // Determine the structural initializer list that corresponds to the |
| 1805 | // current subobject. |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1806 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1807 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1808 | StructuredList, StructuredIndex, |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1809 | SourceRange(D->getLocStart(), |
| 1810 | DIE->getLocEnd())); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1811 | assert(StructuredList && "Expected a structured initializer list"); |
| 1812 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1813 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1814 | if (D->isFieldDesignator()) { |
| 1815 | // C99 6.7.8p7: |
| 1816 | // |
| 1817 | // If a designator has the form |
| 1818 | // |
| 1819 | // . identifier |
| 1820 | // |
| 1821 | // then the current object (defined below) shall have |
| 1822 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1824 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1825 | if (!RT) { |
| 1826 | SourceLocation Loc = D->getDotLoc(); |
| 1827 | if (Loc.isInvalid()) |
| 1828 | Loc = D->getFieldLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1829 | if (!VerifyOnly) |
| 1830 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1831 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1832 | ++Index; |
| 1833 | return true; |
| 1834 | } |
| 1835 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1836 | // Note: we perform a linear search of the fields here, despite |
| 1837 | // the fact that we have a faster lookup method, because we always |
| 1838 | // need to compute the field's index. |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1839 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1840 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1841 | unsigned FieldIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1842 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1843 | Field = RT->getDecl()->field_begin(), |
| 1844 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1845 | for (; Field != FieldEnd; ++Field) { |
| 1846 | if (Field->isUnnamedBitfield()) |
| 1847 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1848 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1849 | // If we find a field representing an anonymous field, look in the |
| 1850 | // IndirectFieldDecl that follow for the designated initializer. |
| 1851 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1852 | if (IndirectFieldDecl *IF = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1853 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1854 | // In verify mode, don't modify the original. |
| 1855 | if (VerifyOnly) |
| 1856 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1857 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1858 | D = DIE->getDesignator(DesigIdx); |
| 1859 | break; |
| 1860 | } |
| 1861 | } |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1862 | if (KnownField && KnownField == *Field) |
Douglas Gregor | 559c9fb | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1863 | break; |
| 1864 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1865 | break; |
| 1866 | |
| 1867 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1870 | if (Field == FieldEnd) { |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1871 | if (VerifyOnly) { |
| 1872 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1873 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1874 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1875 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1876 | // There was no normal field in the struct with the designated |
| 1877 | // name. Perform another lookup for this name, which may find |
| 1878 | // something that we can't designate (e.g., a member function), |
| 1879 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1880 | // struct/union. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1881 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1882 | FieldDecl *ReplacementField = nullptr; |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1883 | if (Lookup.empty()) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1884 | // Name lookup didn't find anything. Determine whether this |
| 1885 | // was a typo for another field name. |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1886 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1887 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1888 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1889 | Sema::LookupMemberName, /*Scope=*/ nullptr, /*SS=*/ nullptr, |
| 1890 | Validator, Sema::CTK_ErrorRecovery, RT->getDecl())) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1891 | SemaRef.diagnoseTypo( |
| 1892 | Corrected, |
| 1893 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
| 1894 | << FieldName << CurrentObjectType); |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1895 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1896 | hadError = true; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1897 | } else { |
| 1898 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1899 | << FieldName << CurrentObjectType; |
| 1900 | ++Index; |
| 1901 | return true; |
| 1902 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1903 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1904 | |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1905 | if (!ReplacementField) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1906 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1907 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1908 | << FieldName; |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1909 | SemaRef.Diag(Lookup.front()->getLocation(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1910 | diag::note_field_designator_found); |
Eli Friedman | 8d25b09 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1911 | ++Index; |
| 1912 | return true; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1913 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1914 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1915 | if (!KnownField) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1916 | // The replacement field comes from typo correction; find it |
| 1917 | // in the list of fields. |
| 1918 | FieldIndex = 0; |
| 1919 | Field = RT->getDecl()->field_begin(); |
| 1920 | for (; Field != FieldEnd; ++Field) { |
| 1921 | if (Field->isUnnamedBitfield()) |
| 1922 | continue; |
| 1923 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1924 | if (ReplacementField == *Field || |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1925 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1926 | break; |
| 1927 | |
| 1928 | ++FieldIndex; |
| 1929 | } |
| 1930 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1931 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1932 | |
| 1933 | // All of the fields of a union are located at the same place in |
| 1934 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1935 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1936 | FieldIndex = 0; |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1937 | if (!VerifyOnly) { |
| 1938 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
| 1939 | if (CurrentField && CurrentField != *Field) { |
| 1940 | assert(StructuredList->getNumInits() == 1 |
| 1941 | && "A union should never have more than one initializer!"); |
| 1942 | |
| 1943 | // we're about to throw away an initializer, emit warning |
| 1944 | SemaRef.Diag(D->getFieldLoc(), |
| 1945 | diag::warn_initializer_overrides) |
| 1946 | << D->getSourceRange(); |
| 1947 | Expr *ExistingInit = StructuredList->getInit(0); |
| 1948 | SemaRef.Diag(ExistingInit->getLocStart(), |
| 1949 | diag::note_previous_initializer) |
| 1950 | << /*FIXME:has side effects=*/0 |
| 1951 | << ExistingInit->getSourceRange(); |
| 1952 | |
| 1953 | // remove existing initializer |
| 1954 | StructuredList->resizeInits(SemaRef.Context, 0); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1955 | StructuredList->setInitializedFieldInUnion(nullptr); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1958 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1959 | } |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1960 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1961 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1962 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1963 | bool InvalidUse; |
| 1964 | if (VerifyOnly) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1965 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1966 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1967 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1968 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1969 | ++Index; |
| 1970 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1971 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1972 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1973 | if (!VerifyOnly) { |
| 1974 | // Update the designator with the field declaration. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1975 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1977 | // Make sure that our non-designated initializer list has space |
| 1978 | // for a subobject corresponding to this field. |
| 1979 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1980 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1981 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1982 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1983 | // This designator names a flexible array member. |
| 1984 | if (Field->getType()->isIncompleteArrayType()) { |
| 1985 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1986 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1987 | // We can't designate an object within the flexible array |
| 1988 | // member (because GCC doesn't allow it). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1989 | if (!VerifyOnly) { |
| 1990 | DesignatedInitExpr::Designator *NextD |
| 1991 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1992 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1993 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1994 | << SourceRange(NextD->getLocStart(), |
| 1995 | DIE->getLocEnd()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1996 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1997 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1998 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1999 | Invalid = true; |
| 2000 | } |
| 2001 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 2002 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 2003 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2004 | // The initializer is not an initializer list. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2005 | if (!VerifyOnly) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2006 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2007 | diag::err_flexible_array_init_needs_braces) |
| 2008 | << DIE->getInit()->getSourceRange(); |
| 2009 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2010 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2011 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2012 | Invalid = true; |
| 2013 | } |
| 2014 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2015 | // Check GNU flexible array initializer. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2016 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2017 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2018 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2019 | |
| 2020 | if (Invalid) { |
| 2021 | ++Index; |
| 2022 | return true; |
| 2023 | } |
| 2024 | |
| 2025 | // Initialize the array. |
| 2026 | bool prevHadError = hadError; |
| 2027 | unsigned newStructuredIndex = FieldIndex; |
| 2028 | unsigned OldIndex = Index; |
| 2029 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2030 | |
| 2031 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2032 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2033 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2034 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2035 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2036 | IList->setInit(OldIndex, DIE); |
| 2037 | if (hadError && !prevHadError) { |
| 2038 | ++Field; |
| 2039 | ++FieldIndex; |
| 2040 | if (NextField) |
| 2041 | *NextField = Field; |
| 2042 | StructuredIndex = FieldIndex; |
| 2043 | return true; |
| 2044 | } |
| 2045 | } else { |
| 2046 | // Recurse to check later designated subobjects. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2047 | QualType FieldType = Field->getType(); |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2048 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2049 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2050 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2051 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2052 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2053 | FieldType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2054 | StructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2055 | true, false)) |
| 2056 | return true; |
| 2057 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2058 | |
| 2059 | // Find the position of the next field to be initialized in this |
| 2060 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2061 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2062 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2063 | |
| 2064 | // If this the first designator, our caller will continue checking |
| 2065 | // the rest of this struct/class/union subobject. |
| 2066 | if (IsFirstDesignator) { |
| 2067 | if (NextField) |
| 2068 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2069 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2070 | return false; |
| 2071 | } |
| 2072 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2073 | if (!FinishSubobjectInit) |
| 2074 | return false; |
| 2075 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2076 | // We've already initialized something in the union; we're done. |
| 2077 | if (RT->getDecl()->isUnion()) |
| 2078 | return hadError; |
| 2079 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2080 | // Check the remaining fields within this class/struct/union subobject. |
| 2081 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2082 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2083 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2084 | StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2085 | return hadError && !prevHadError; |
| 2086 | } |
| 2087 | |
| 2088 | // C99 6.7.8p6: |
| 2089 | // |
| 2090 | // If a designator has the form |
| 2091 | // |
| 2092 | // [ constant-expression ] |
| 2093 | // |
| 2094 | // then the current object (defined below) shall have array |
| 2095 | // type and the expression shall be an integer constant |
| 2096 | // expression. If the array is of unknown size, any |
| 2097 | // nonnegative value is valid. |
| 2098 | // |
| 2099 | // Additionally, cope with the GNU extension that permits |
| 2100 | // designators of the form |
| 2101 | // |
| 2102 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2103 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2104 | if (!AT) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2105 | if (!VerifyOnly) |
| 2106 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2107 | << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2108 | ++Index; |
| 2109 | return true; |
| 2110 | } |
| 2111 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2112 | Expr *IndexExpr = nullptr; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2113 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2114 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2115 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2116 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2117 | DesignatedEndIndex = DesignatedStartIndex; |
| 2118 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2119 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2120 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2121 | DesignatedStartIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2122 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | DesignatedEndIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2124 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2125 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2126 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2127 | // Codegen can't handle evaluating array range designators that have side |
| 2128 | // effects, because we replicate the AST value for each initialized element. |
| 2129 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2130 | // elements with something that has a side effect, so codegen can emit an |
| 2131 | // "error unsupported" error instead of miscompiling the app. |
| 2132 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2133 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2134 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2137 | if (isa<ConstantArrayType>(AT)) { |
| 2138 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2139 | DesignatedStartIndex |
| 2140 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2141 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2142 | DesignatedEndIndex |
| 2143 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2144 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2145 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | ed0f916 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2146 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2147 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2148 | diag::err_array_designator_too_large) |
| 2149 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2150 | << IndexExpr->getSourceRange(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2151 | ++Index; |
| 2152 | return true; |
| 2153 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2154 | } else { |
| 2155 | // Make sure the bit-widths and signedness match. |
| 2156 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2157 | DesignatedEndIndex |
| 2158 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2159 | else if (DesignatedStartIndex.getBitWidth() < |
| 2160 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2161 | DesignatedStartIndex |
| 2162 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2163 | DesignatedStartIndex.setIsUnsigned(true); |
| 2164 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2165 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2166 | |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2167 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2168 | // We're modifying a string literal init; we have to decompose the string |
| 2169 | // so we can modify the individual characters. |
| 2170 | ASTContext &Context = SemaRef.Context; |
| 2171 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2172 | |
| 2173 | // Compute the character type |
| 2174 | QualType CharTy = AT->getElementType(); |
| 2175 | |
| 2176 | // Compute the type of the integer literals. |
| 2177 | QualType PromotedCharTy = CharTy; |
| 2178 | if (CharTy->isPromotableIntegerType()) |
| 2179 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2180 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2181 | |
| 2182 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2183 | // Get the length of the string. |
| 2184 | uint64_t StrLen = SL->getLength(); |
| 2185 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2186 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2187 | StructuredList->resizeInits(Context, StrLen); |
| 2188 | |
| 2189 | // Build a literal for each character in the string, and put them into |
| 2190 | // the init list. |
| 2191 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2192 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2193 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2194 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2195 | if (CharTy != PromotedCharTy) |
| 2196 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2197 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2198 | StructuredList->updateInit(Context, i, Init); |
| 2199 | } |
| 2200 | } else { |
| 2201 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2202 | std::string Str; |
| 2203 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2204 | |
| 2205 | // Get the length of the string. |
| 2206 | uint64_t StrLen = Str.size(); |
| 2207 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2208 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2209 | StructuredList->resizeInits(Context, StrLen); |
| 2210 | |
| 2211 | // Build a literal for each character in the string, and put them into |
| 2212 | // the init list. |
| 2213 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2214 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2215 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2216 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2217 | if (CharTy != PromotedCharTy) |
| 2218 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2219 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2220 | StructuredList->updateInit(Context, i, Init); |
| 2221 | } |
| 2222 | } |
| 2223 | } |
| 2224 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2225 | // Make sure that our non-designated initializer list has space |
| 2226 | // for a subobject corresponding to this array element. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2227 | if (!VerifyOnly && |
| 2228 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2229 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2230 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2231 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2232 | // Repeatedly perform subobject initializations in the range |
| 2233 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2234 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2235 | // Move to the next designator |
| 2236 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2237 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2238 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2239 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2240 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2241 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2242 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2243 | // Recurse to check later designated subobjects. |
| 2244 | QualType ElementType = AT->getElementType(); |
| 2245 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2246 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2247 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2248 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2249 | ElementType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2250 | StructuredList, ElementIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2251 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2252 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2253 | return true; |
| 2254 | |
| 2255 | // Move to the next index in the array that we'll be initializing. |
| 2256 | ++DesignatedStartIndex; |
| 2257 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2258 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2259 | |
| 2260 | // If this the first designator, our caller will continue checking |
| 2261 | // the rest of this array subobject. |
| 2262 | if (IsFirstDesignator) { |
| 2263 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2264 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2265 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2266 | return false; |
| 2267 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2268 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2269 | if (!FinishSubobjectInit) |
| 2270 | return false; |
| 2271 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2272 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2273 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2274 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2275 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2276 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2277 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2280 | // Get the structured initializer list for a subobject of type |
| 2281 | // @p CurrentObjectType. |
| 2282 | InitListExpr * |
| 2283 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2284 | QualType CurrentObjectType, |
| 2285 | InitListExpr *StructuredList, |
| 2286 | unsigned StructuredIndex, |
| 2287 | SourceRange InitRange) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2288 | if (VerifyOnly) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2289 | return nullptr; // No structured list in verification-only mode. |
| 2290 | Expr *ExistingInit = nullptr; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2291 | if (!StructuredList) |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2292 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2293 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2294 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2295 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2296 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2297 | return Result; |
| 2298 | |
| 2299 | if (ExistingInit) { |
| 2300 | // We are creating an initializer list that initializes the |
| 2301 | // subobjects of the current object, but there was already an |
| 2302 | // initialization that completely initialized the current |
| 2303 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2304 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2305 | // struct X { int a, b; }; |
| 2306 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2308 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2309 | // designated initializer re-initializes the whole |
| 2310 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2311 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2312 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2313 | << InitRange; |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2314 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2315 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2316 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2317 | << ExistingInit->getSourceRange(); |
| 2318 | } |
| 2319 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2321 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2322 | InitRange.getBegin(), None, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2323 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2324 | |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2325 | QualType ResultType = CurrentObjectType; |
| 2326 | if (!ResultType->isArrayType()) |
| 2327 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2328 | Result->setType(ResultType); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2329 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2330 | // Pre-allocate storage for the structured initializer list. |
| 2331 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2332 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2333 | bool GotNumInits = false; |
| 2334 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2335 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2336 | GotNumInits = true; |
| 2337 | } else if (Index < IList->getNumInits()) { |
| 2338 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2339 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2340 | GotNumInits = true; |
| 2341 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2344 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2345 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2346 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2347 | NumElements = CAType->getSize().getZExtValue(); |
| 2348 | // Simple heuristic so that we don't allocate a very large |
| 2349 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2350 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2351 | NumElements = 0; |
| 2352 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2353 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2354 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2355 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2356 | RecordDecl *RDecl = RType->getDecl(); |
| 2357 | if (RDecl->isUnion()) |
| 2358 | NumElements = 1; |
| 2359 | else |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 2360 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2363 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2364 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2365 | // Link this new initializer list into the structured initializer |
| 2366 | // lists. |
| 2367 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2368 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2369 | else { |
| 2370 | Result->setSyntacticForm(IList); |
| 2371 | SyntacticToSemantic[IList] = Result; |
| 2372 | } |
| 2373 | |
| 2374 | return Result; |
| 2375 | } |
| 2376 | |
| 2377 | /// Update the initializer at index @p StructuredIndex within the |
| 2378 | /// structured initializer list to the value @p expr. |
| 2379 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2380 | unsigned &StructuredIndex, |
| 2381 | Expr *expr) { |
| 2382 | // No structured initializer list to update |
| 2383 | if (!StructuredList) |
| 2384 | return; |
| 2385 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2386 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2387 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2388 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2389 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2390 | diag::warn_initializer_overrides) |
| 2391 | << expr->getSourceRange(); |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2392 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2393 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2394 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2395 | << PrevInit->getSourceRange(); |
| 2396 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2397 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2398 | ++StructuredIndex; |
| 2399 | } |
| 2400 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2401 | /// Check that the given Index expression is a valid array designator |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2402 | /// value. This is essentially just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2403 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2404 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2405 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2406 | /// added, on success. If everything went okay, Value will receive the |
| 2407 | /// value of the constant expression. |
| 2408 | static ExprResult |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2409 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2410 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2411 | |
| 2412 | // Make sure this is an integer constant expression. |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2413 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2414 | if (Result.isInvalid()) |
| 2415 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2416 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2417 | if (Value.isSigned() && Value.isNegative()) |
| 2418 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2419 | << Value.toString(10) << Index->getSourceRange(); |
| 2420 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2421 | Value.setIsUnsigned(true); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2422 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2425 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2426 | SourceLocation Loc, |
| 2427 | bool GNUSyntax, |
| 2428 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2429 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2430 | |
| 2431 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2432 | SmallVector<ASTDesignator, 32> Designators; |
| 2433 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2434 | |
| 2435 | // Build designators and check array designator expressions. |
| 2436 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2437 | const Designator &D = Desig.getDesignator(Idx); |
| 2438 | switch (D.getKind()) { |
| 2439 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2440 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2441 | D.getFieldLoc())); |
| 2442 | break; |
| 2443 | |
| 2444 | case Designator::ArrayDesignator: { |
| 2445 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2446 | llvm::APSInt IndexValue; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2447 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2448 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2449 | if (!Index) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2450 | Invalid = true; |
| 2451 | else { |
| 2452 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2453 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2454 | D.getRBracketLoc())); |
| 2455 | InitExpressions.push_back(Index); |
| 2456 | } |
| 2457 | break; |
| 2458 | } |
| 2459 | |
| 2460 | case Designator::ArrayRangeDesignator: { |
| 2461 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2462 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2463 | llvm::APSInt StartValue; |
| 2464 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2465 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2466 | StartIndex->isValueDependent(); |
| 2467 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2468 | EndIndex->isValueDependent(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2469 | if (!StartDependent) |
| 2470 | StartIndex = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2471 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2472 | if (!EndDependent) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2473 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2474 | |
| 2475 | if (!StartIndex || !EndIndex) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2476 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2477 | else { |
| 2478 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2479 | if (StartDependent || EndDependent) { |
| 2480 | // Nothing to compute. |
| 2481 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2482 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2483 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2484 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2485 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2486 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2487 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2488 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2489 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2490 | Invalid = true; |
| 2491 | } else { |
| 2492 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2494 | D.getEllipsisLoc(), |
| 2495 | D.getRBracketLoc())); |
| 2496 | InitExpressions.push_back(StartIndex); |
| 2497 | InitExpressions.push_back(EndIndex); |
| 2498 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2499 | } |
| 2500 | break; |
| 2501 | } |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | if (Invalid || Init.isInvalid()) |
| 2506 | return ExprError(); |
| 2507 | |
| 2508 | // Clear out the expressions within the designation. |
| 2509 | Desig.ClearExprs(*this); |
| 2510 | |
| 2511 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2512 | = DesignatedInitExpr::Create(Context, |
| 2513 | Designators.data(), Designators.size(), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2514 | InitExpressions, Loc, GNUSyntax, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2515 | Init.getAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2516 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2517 | if (!getLangOpts().C99) |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2518 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2519 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2520 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2521 | return DIE; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2522 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2523 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2524 | //===----------------------------------------------------------------------===// |
| 2525 | // Initialization entity |
| 2526 | //===----------------------------------------------------------------------===// |
| 2527 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2528 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2529 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2530 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2531 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2532 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2533 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2534 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2535 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2536 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2537 | Type = VT->getElementType(); |
| 2538 | } else { |
| 2539 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2540 | assert(CT && "Unexpected type"); |
| 2541 | Kind = EK_ComplexElement; |
| 2542 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2543 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2546 | InitializedEntity |
| 2547 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 2548 | const CXXBaseSpecifier *Base, |
| 2549 | bool IsInheritedVirtualBase) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2550 | InitializedEntity Result; |
| 2551 | Result.Kind = EK_Base; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2552 | Result.Parent = nullptr; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2553 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2554 | if (IsInheritedVirtualBase) |
| 2555 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2556 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2557 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2558 | return Result; |
| 2559 | } |
| 2560 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2561 | DeclarationName InitializedEntity::getName() const { |
| 2562 | switch (getKind()) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2563 | case EK_Parameter: |
| 2564 | case EK_Parameter_CF_Audited: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2565 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2566 | return (D ? D->getDeclName() : DeclarationName()); |
| 2567 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2568 | |
| 2569 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2570 | case EK_Member: |
| 2571 | return VariableOrMember->getDeclName(); |
| 2572 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2573 | case EK_LambdaCapture: |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 2574 | return DeclarationName(Capture.VarID); |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2575 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2576 | case EK_Result: |
| 2577 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2578 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2579 | case EK_Temporary: |
| 2580 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2581 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2582 | case EK_ArrayElement: |
| 2583 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2584 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2585 | case EK_BlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2586 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2587 | case EK_RelatedResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2588 | return DeclarationName(); |
| 2589 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2590 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2591 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2594 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2595 | switch (getKind()) { |
| 2596 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2597 | case EK_Member: |
| 2598 | return VariableOrMember; |
| 2599 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2600 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2601 | case EK_Parameter_CF_Audited: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2602 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2603 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2604 | case EK_Result: |
| 2605 | case EK_Exception: |
| 2606 | case EK_New: |
| 2607 | case EK_Temporary: |
| 2608 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2609 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2610 | case EK_ArrayElement: |
| 2611 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2612 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2613 | case EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2614 | case EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2615 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2616 | case EK_RelatedResult: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2617 | return nullptr; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2618 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2619 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2620 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2623 | bool InitializedEntity::allowsNRVO() const { |
| 2624 | switch (getKind()) { |
| 2625 | case EK_Result: |
| 2626 | case EK_Exception: |
| 2627 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2628 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2629 | case EK_Variable: |
| 2630 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2631 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2632 | case EK_Member: |
| 2633 | case EK_New: |
| 2634 | case EK_Temporary: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2635 | case EK_CompoundLiteralInit: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2636 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2637 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2638 | case EK_ArrayElement: |
| 2639 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2640 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2641 | case EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2642 | case EK_LambdaCapture: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2643 | case EK_RelatedResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2644 | break; |
| 2645 | } |
| 2646 | |
| 2647 | return false; |
| 2648 | } |
| 2649 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2650 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2651 | assert(getParent() != this); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2652 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 2653 | for (unsigned I = 0; I != Depth; ++I) |
| 2654 | OS << "`-"; |
| 2655 | |
| 2656 | switch (getKind()) { |
| 2657 | case EK_Variable: OS << "Variable"; break; |
| 2658 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2659 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 2660 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2661 | case EK_Result: OS << "Result"; break; |
| 2662 | case EK_Exception: OS << "Exception"; break; |
| 2663 | case EK_Member: OS << "Member"; break; |
| 2664 | case EK_New: OS << "New"; break; |
| 2665 | case EK_Temporary: OS << "Temporary"; break; |
| 2666 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2667 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2668 | case EK_Base: OS << "Base"; break; |
| 2669 | case EK_Delegating: OS << "Delegating"; break; |
| 2670 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 2671 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 2672 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 2673 | case EK_BlockElement: OS << "Block"; break; |
| 2674 | case EK_LambdaCapture: |
| 2675 | OS << "LambdaCapture "; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 2676 | OS << DeclarationName(Capture.VarID); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2677 | break; |
| 2678 | } |
| 2679 | |
| 2680 | if (Decl *D = getDecl()) { |
| 2681 | OS << " "; |
| 2682 | cast<NamedDecl>(D)->printQualifiedName(OS); |
| 2683 | } |
| 2684 | |
| 2685 | OS << " '" << getType().getAsString() << "'\n"; |
| 2686 | |
| 2687 | return Depth + 1; |
| 2688 | } |
| 2689 | |
| 2690 | void InitializedEntity::dump() const { |
| 2691 | dumpImpl(llvm::errs()); |
| 2692 | } |
| 2693 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2694 | //===----------------------------------------------------------------------===// |
| 2695 | // Initialization sequence |
| 2696 | //===----------------------------------------------------------------------===// |
| 2697 | |
| 2698 | void InitializationSequence::Step::Destroy() { |
| 2699 | switch (Kind) { |
| 2700 | case SK_ResolveAddressOfOverloadedFunction: |
| 2701 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2702 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2703 | case SK_CastDerivedToBaseLValue: |
| 2704 | case SK_BindReference: |
| 2705 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2706 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2707 | case SK_UserConversion: |
| 2708 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2709 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2710 | case SK_QualificationConversionLValue: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2711 | case SK_LValueToRValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2712 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2713 | case SK_ListConstructorCall: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2714 | case SK_UnwrapInitList: |
| 2715 | case SK_RewrapInitList: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2716 | case SK_ConstructorInitialization: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2717 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2718 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2719 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2720 | case SK_ObjCObjectConversion: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2721 | case SK_ArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2722 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2723 | case SK_PassByIndirectCopyRestore: |
| 2724 | case SK_PassByIndirectRestore: |
| 2725 | case SK_ProduceObjCObject: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2726 | case SK_StdInitializerList: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2727 | case SK_OCLSamplerInit: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2728 | case SK_OCLZeroEvent: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2729 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2730 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2731 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2732 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2733 | delete ICS; |
| 2734 | } |
| 2735 | } |
| 2736 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2737 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2738 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2742 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2743 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2744 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2745 | switch (getFailureKind()) { |
| 2746 | case FK_TooManyInitsForReference: |
| 2747 | case FK_ArrayNeedsInitList: |
| 2748 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 2749 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 2750 | case FK_NarrowStringIntoWideCharArray: |
| 2751 | case FK_WideStringIntoCharArray: |
| 2752 | case FK_IncompatWideStringIntoWideChar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2753 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2754 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2755 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2756 | case FK_RValueReferenceBindingToLValue: |
| 2757 | case FK_ReferenceInitDropsQualifiers: |
| 2758 | case FK_ReferenceInitFailed: |
| 2759 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2760 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2761 | case FK_TooManyInitsForScalar: |
| 2762 | case FK_ReferenceBindingToInitList: |
| 2763 | case FK_InitListBadDestinationType: |
| 2764 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2765 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2766 | case FK_ArrayTypeMismatch: |
| 2767 | case FK_NonConstantArrayInit: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2768 | case FK_ListInitializationFailed: |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2769 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2770 | case FK_PlaceholderType: |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2771 | case FK_ExplicitConstructor: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2772 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2773 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2774 | case FK_ReferenceInitOverloadFailed: |
| 2775 | case FK_UserConversionOverloadFailed: |
| 2776 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2777 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2778 | return FailedOverloadResult == OR_Ambiguous; |
| 2779 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2780 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2781 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2784 | bool InitializationSequence::isConstructorInitialization() const { |
| 2785 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2786 | } |
| 2787 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2788 | void |
| 2789 | InitializationSequence |
| 2790 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2791 | DeclAccessPair Found, |
| 2792 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2793 | Step S; |
| 2794 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2795 | S.Type = Function->getType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2796 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2797 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2798 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2799 | Steps.push_back(S); |
| 2800 | } |
| 2801 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2802 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2803 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2804 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2805 | switch (VK) { |
| 2806 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2807 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2808 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2809 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2810 | S.Type = BaseType; |
| 2811 | Steps.push_back(S); |
| 2812 | } |
| 2813 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2814 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2815 | bool BindingTemporary) { |
| 2816 | Step S; |
| 2817 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2818 | S.Type = T; |
| 2819 | Steps.push_back(S); |
| 2820 | } |
| 2821 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2822 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2823 | Step S; |
| 2824 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2825 | S.Type = T; |
| 2826 | Steps.push_back(S); |
| 2827 | } |
| 2828 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2829 | void |
| 2830 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2831 | DeclAccessPair FoundDecl, |
| 2832 | QualType T, |
| 2833 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2834 | Step S; |
| 2835 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2836 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2837 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2838 | S.Function.Function = Function; |
| 2839 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2840 | Steps.push_back(S); |
| 2841 | } |
| 2842 | |
| 2843 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2844 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2845 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2846 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2847 | switch (VK) { |
| 2848 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2849 | S.Kind = SK_QualificationConversionRValue; |
| 2850 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2851 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2852 | S.Kind = SK_QualificationConversionXValue; |
| 2853 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2854 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2855 | S.Kind = SK_QualificationConversionLValue; |
| 2856 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2857 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2858 | S.Type = Ty; |
| 2859 | Steps.push_back(S); |
| 2860 | } |
| 2861 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2862 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 2863 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 2864 | |
| 2865 | Step S; |
| 2866 | S.Kind = SK_LValueToRValue; |
| 2867 | S.Type = Ty; |
| 2868 | Steps.push_back(S); |
| 2869 | } |
| 2870 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2871 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2872 | const ImplicitConversionSequence &ICS, QualType T, |
| 2873 | bool TopLevelOfInitList) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2874 | Step S; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2875 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 2876 | : SK_ConversionSequence; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2877 | S.Type = T; |
| 2878 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2879 | Steps.push_back(S); |
| 2880 | } |
| 2881 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2882 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2883 | Step S; |
| 2884 | S.Kind = SK_ListInitialization; |
| 2885 | S.Type = T; |
| 2886 | Steps.push_back(S); |
| 2887 | } |
| 2888 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2889 | void |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2890 | InitializationSequence |
| 2891 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2892 | AccessSpecifier Access, |
| 2893 | QualType T, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2894 | bool HadMultipleCandidates, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2895 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2896 | Step S; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2897 | S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall |
| 2898 | : SK_ConstructorInitialization; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2899 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2900 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2901 | S.Function.Function = Constructor; |
| 2902 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2903 | Steps.push_back(S); |
| 2904 | } |
| 2905 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2906 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2907 | Step S; |
| 2908 | S.Kind = SK_ZeroInitialization; |
| 2909 | S.Type = T; |
| 2910 | Steps.push_back(S); |
| 2911 | } |
| 2912 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2913 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2914 | Step S; |
| 2915 | S.Kind = SK_CAssignment; |
| 2916 | S.Type = T; |
| 2917 | Steps.push_back(S); |
| 2918 | } |
| 2919 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2920 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2921 | Step S; |
| 2922 | S.Kind = SK_StringInit; |
| 2923 | S.Type = T; |
| 2924 | Steps.push_back(S); |
| 2925 | } |
| 2926 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2927 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2928 | Step S; |
| 2929 | S.Kind = SK_ObjCObjectConversion; |
| 2930 | S.Type = T; |
| 2931 | Steps.push_back(S); |
| 2932 | } |
| 2933 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2934 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2935 | Step S; |
| 2936 | S.Kind = SK_ArrayInit; |
| 2937 | S.Type = T; |
| 2938 | Steps.push_back(S); |
| 2939 | } |
| 2940 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2941 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2942 | Step S; |
| 2943 | S.Kind = SK_ParenthesizedArrayInit; |
| 2944 | S.Type = T; |
| 2945 | Steps.push_back(S); |
| 2946 | } |
| 2947 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2948 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2949 | bool shouldCopy) { |
| 2950 | Step s; |
| 2951 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2952 | : SK_PassByIndirectRestore); |
| 2953 | s.Type = type; |
| 2954 | Steps.push_back(s); |
| 2955 | } |
| 2956 | |
| 2957 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2958 | Step S; |
| 2959 | S.Kind = SK_ProduceObjCObject; |
| 2960 | S.Type = T; |
| 2961 | Steps.push_back(S); |
| 2962 | } |
| 2963 | |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2964 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2965 | Step S; |
| 2966 | S.Kind = SK_StdInitializerList; |
| 2967 | S.Type = T; |
| 2968 | Steps.push_back(S); |
| 2969 | } |
| 2970 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2971 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 2972 | Step S; |
| 2973 | S.Kind = SK_OCLSamplerInit; |
| 2974 | S.Type = T; |
| 2975 | Steps.push_back(S); |
| 2976 | } |
| 2977 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2978 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 2979 | Step S; |
| 2980 | S.Kind = SK_OCLZeroEvent; |
| 2981 | S.Type = T; |
| 2982 | Steps.push_back(S); |
| 2983 | } |
| 2984 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2985 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2986 | InitListExpr *Syntactic) { |
| 2987 | assert(Syntactic->getNumInits() == 1 && |
| 2988 | "Can only rewrap trivial init lists."); |
| 2989 | Step S; |
| 2990 | S.Kind = SK_UnwrapInitList; |
| 2991 | S.Type = Syntactic->getInit(0)->getType(); |
| 2992 | Steps.insert(Steps.begin(), S); |
| 2993 | |
| 2994 | S.Kind = SK_RewrapInitList; |
| 2995 | S.Type = T; |
| 2996 | S.WrappingSyntacticList = Syntactic; |
| 2997 | Steps.push_back(S); |
| 2998 | } |
| 2999 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3000 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3001 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3002 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3003 | this->Failure = Failure; |
| 3004 | this->FailedOverloadResult = Result; |
| 3005 | } |
| 3006 | |
| 3007 | //===----------------------------------------------------------------------===// |
| 3008 | // Attempt initialization |
| 3009 | //===----------------------------------------------------------------------===// |
| 3010 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3011 | static void MaybeProduceObjCObject(Sema &S, |
| 3012 | InitializationSequence &Sequence, |
| 3013 | const InitializedEntity &Entity) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3014 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3015 | |
| 3016 | /// When initializing a parameter, produce the value if it's marked |
| 3017 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3018 | if (Entity.isParameterKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3019 | if (!Entity.isParameterConsumed()) |
| 3020 | return; |
| 3021 | |
| 3022 | assert(Entity.getType()->isObjCRetainableType() && |
| 3023 | "consuming an object of unretainable type?"); |
| 3024 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3025 | |
| 3026 | /// When initializing a return value, if the return type is a |
| 3027 | /// retainable type, then returns need to immediately retain the |
| 3028 | /// object. If an autorelease is required, it will be done at the |
| 3029 | /// last instant. |
| 3030 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 3031 | if (!Entity.getType()->isObjCRetainableType()) |
| 3032 | return; |
| 3033 | |
| 3034 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3035 | } |
| 3036 | } |
| 3037 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3038 | static void TryListInitialization(Sema &S, |
| 3039 | const InitializedEntity &Entity, |
| 3040 | const InitializationKind &Kind, |
| 3041 | InitListExpr *InitList, |
| 3042 | InitializationSequence &Sequence); |
| 3043 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3044 | /// \brief When initializing from init list via constructor, handle |
| 3045 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3046 | /// |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3047 | /// \return true if we have handled initialization of an object of type |
| 3048 | /// std::initializer_list<T>, false otherwise. |
| 3049 | static bool TryInitializerListConstruction(Sema &S, |
| 3050 | InitListExpr *List, |
| 3051 | QualType DestType, |
| 3052 | InitializationSequence &Sequence) { |
| 3053 | QualType E; |
| 3054 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3055 | return false; |
| 3056 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3057 | if (S.RequireCompleteType(List->getExprLoc(), E, 0)) { |
| 3058 | Sequence.setIncompleteTypeFailure(E); |
| 3059 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3060 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3061 | |
| 3062 | // Try initializing a temporary array from the init list. |
| 3063 | QualType ArrayType = S.Context.getConstantArrayType( |
| 3064 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 3065 | List->getNumInits()), |
| 3066 | clang::ArrayType::Normal, 0); |
| 3067 | InitializedEntity HiddenArray = |
| 3068 | InitializedEntity::InitializeTemporary(ArrayType); |
| 3069 | InitializationKind Kind = |
| 3070 | InitializationKind::CreateDirectList(List->getExprLoc()); |
| 3071 | TryListInitialization(S, HiddenArray, Kind, List, Sequence); |
| 3072 | if (Sequence) |
| 3073 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3074 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3077 | static OverloadingResult |
| 3078 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3079 | MultiExprArg Args, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3080 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3081 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3082 | OverloadCandidateSet::iterator &Best, |
| 3083 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3084 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3085 | CandidateSet.clear(); |
| 3086 | |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3087 | for (ArrayRef<NamedDecl *>::iterator |
| 3088 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3089 | NamedDecl *D = *Con; |
| 3090 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3091 | bool SuppressUserConversions = false; |
| 3092 | |
| 3093 | // Find the constructor (which may be a template). |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3094 | CXXConstructorDecl *Constructor = nullptr; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3095 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 3096 | if (ConstructorTmpl) |
| 3097 | Constructor = cast<CXXConstructorDecl>( |
| 3098 | ConstructorTmpl->getTemplatedDecl()); |
| 3099 | else { |
| 3100 | Constructor = cast<CXXConstructorDecl>(D); |
| 3101 | |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3102 | // C++11 [over.best.ics]p4: |
| 3103 | // However, when considering the argument of a constructor or |
| 3104 | // user-defined conversion function that is a candidate: |
| 3105 | // -- by 13.3.1.3 when invoked for the copying/moving of a temporary |
| 3106 | // in the second step of a class copy-initialization, |
| 3107 | // -- by 13.3.1.7 when passing the initializer list as a single |
| 3108 | // argument or when the initializer list has exactly one elementand |
| 3109 | // a conversion to some class X or reference to (possibly |
| 3110 | // cv-qualified) X is considered for the first parameter of a |
| 3111 | // constructor of X, or |
| 3112 | // -- by 13.3.1.4, 13.3.1.5, or 13.3.1.6 in all cases, |
| 3113 | // only standard conversion sequences and ellipsis conversion sequences |
| 3114 | // are considered. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3115 | if ((CopyInitializing || (InitListSyntax && Args.size() == 1)) && |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3116 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3117 | SuppressUserConversions = true; |
| 3118 | } |
| 3119 | |
| 3120 | if (!Constructor->isInvalidDecl() && |
| 3121 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3122 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3123 | if (ConstructorTmpl) |
| 3124 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3125 | /*ExplicitArgs*/ nullptr, Args, |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3126 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3127 | else { |
| 3128 | // C++ [over.match.copy]p1: |
| 3129 | // - When initializing a temporary to be bound to the first parameter |
| 3130 | // of a constructor that takes a reference to possibly cv-qualified |
| 3131 | // T as its first argument, called with a single argument in the |
| 3132 | // context of direct-initialization, explicit conversion functions |
| 3133 | // are also considered. |
| 3134 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3135 | Args.size() == 1 && |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3136 | Constructor->isCopyOrMoveConstructor(); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3137 | S.AddOverloadCandidate(Constructor, FoundDecl, Args, CandidateSet, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3138 | SuppressUserConversions, |
| 3139 | /*PartialOverloading=*/false, |
| 3140 | /*AllowExplicit=*/AllowExplicitConv); |
| 3141 | } |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3142 | } |
| 3143 | } |
| 3144 | |
| 3145 | // Perform overload resolution and return the result. |
| 3146 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3147 | } |
| 3148 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3149 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3150 | /// enumerates the constructors of the initialized entity and performs overload |
| 3151 | /// resolution to select the best. |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3152 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3153 | /// class type. |
| 3154 | static void TryConstructorInitialization(Sema &S, |
| 3155 | const InitializedEntity &Entity, |
| 3156 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3157 | MultiExprArg Args, QualType DestType, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3158 | InitializationSequence &Sequence, |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3159 | bool InitListSyntax = false) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3160 | assert((!InitListSyntax || (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3161 | "InitListSyntax must come with a single initializer list argument."); |
| 3162 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3163 | // The type we're constructing needs to be complete. |
| 3164 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3165 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3166 | return; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3170 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3171 | CXXRecordDecl *DestRecordDecl |
| 3172 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3173 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3174 | // Build the candidate set directly in the initialization sequence |
| 3175 | // structure, so that it will persist if we fail. |
| 3176 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3177 | |
| 3178 | // Determine whether we are allowed to call explicit constructors or |
| 3179 | // explicit conversion operators. |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3180 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3181 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3182 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3183 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3184 | // applicable constructors are enumerated, and the best one is chosen |
| 3185 | // through overload resolution. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3186 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3187 | // The container holding the constructors can under certain conditions |
| 3188 | // be changed while iterating (e.g. because of deserialization). |
| 3189 | // To be safe we copy the lookup results to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3190 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3191 | |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3192 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3193 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3194 | bool AsInitializerList = false; |
| 3195 | |
| 3196 | // C++11 [over.match.list]p1: |
| 3197 | // When objects of non-aggregate type T are list-initialized, overload |
| 3198 | // resolution selects the constructor in two phases: |
| 3199 | // - Initially, the candidate functions are the initializer-list |
| 3200 | // constructors of the class T and the argument list consists of the |
| 3201 | // initializer list as a single argument. |
| 3202 | if (InitListSyntax) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3203 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3204 | AsInitializerList = true; |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3205 | |
| 3206 | // If the initializer list has no elements and T has a default constructor, |
| 3207 | // the first phase is omitted. |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3208 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3209 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3210 | CandidateSet, Ctors, Best, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3211 | CopyInitialization, AllowExplicit, |
| 3212 | /*OnlyListConstructor=*/true, |
| 3213 | InitListSyntax); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3214 | |
| 3215 | // Time to unwrap the init list. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3216 | Args = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | // C++11 [over.match.list]p1: |
| 3220 | // - If no viable initializer-list constructor is found, overload resolution |
| 3221 | // is performed again, where the candidate functions are all the |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3222 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3223 | // elements of the initializer list. |
| 3224 | if (Result == OR_No_Viable_Function) { |
| 3225 | AsInitializerList = false; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3226 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3227 | CandidateSet, Ctors, Best, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3228 | CopyInitialization, AllowExplicit, |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3229 | /*OnlyListConstructors=*/false, |
| 3230 | InitListSyntax); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3231 | } |
| 3232 | if (Result) { |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3233 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3234 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3235 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3236 | Result); |
| 3237 | return; |
| 3238 | } |
| 3239 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3240 | // C++11 [dcl.init]p6: |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3241 | // If a program calls for the default initialization of an object |
| 3242 | // of a const-qualified type T, T shall be a class type with a |
| 3243 | // user-provided default constructor. |
| 3244 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3245 | Entity.getType().isConstQualified() && |
Aaron Ballman | 899b9c6 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 3246 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3247 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3248 | return; |
| 3249 | } |
| 3250 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3251 | // C++11 [over.match.list]p1: |
| 3252 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3253 | // initializer is ill-formed. |
| 3254 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 3255 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 3256 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3257 | return; |
| 3258 | } |
| 3259 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3260 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3261 | // subsumed by the initialization. |
| 3262 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3263 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 3264 | Best->FoundDecl.getAccess(), |
| 3265 | DestType, HadMultipleCandidates, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3266 | InitListSyntax, AsInitializerList); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3267 | } |
| 3268 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3269 | static bool |
| 3270 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3271 | Expr *Initializer, |
| 3272 | QualType &SourceType, |
| 3273 | QualType &UnqualifiedSourceType, |
| 3274 | QualType UnqualifiedTargetType, |
| 3275 | InitializationSequence &Sequence) { |
| 3276 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3277 | S.Context.OverloadTy) { |
| 3278 | DeclAccessPair Found; |
| 3279 | bool HadMultipleCandidates = false; |
| 3280 | if (FunctionDecl *Fn |
| 3281 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3282 | UnqualifiedTargetType, |
| 3283 | false, Found, |
| 3284 | &HadMultipleCandidates)) { |
| 3285 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3286 | HadMultipleCandidates); |
| 3287 | SourceType = Fn->getType(); |
| 3288 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3289 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3290 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3291 | return true; |
| 3292 | } |
| 3293 | } |
| 3294 | return false; |
| 3295 | } |
| 3296 | |
| 3297 | static void TryReferenceInitializationCore(Sema &S, |
| 3298 | const InitializedEntity &Entity, |
| 3299 | const InitializationKind &Kind, |
| 3300 | Expr *Initializer, |
| 3301 | QualType cv1T1, QualType T1, |
| 3302 | Qualifiers T1Quals, |
| 3303 | QualType cv2T2, QualType T2, |
| 3304 | Qualifiers T2Quals, |
| 3305 | InitializationSequence &Sequence); |
| 3306 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3307 | static void TryValueInitialization(Sema &S, |
| 3308 | const InitializedEntity &Entity, |
| 3309 | const InitializationKind &Kind, |
| 3310 | InitializationSequence &Sequence, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3311 | InitListExpr *InitList = nullptr); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3312 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3313 | /// \brief Attempt list initialization of a reference. |
| 3314 | static void TryReferenceListInitialization(Sema &S, |
| 3315 | const InitializedEntity &Entity, |
| 3316 | const InitializationKind &Kind, |
| 3317 | InitListExpr *InitList, |
Richard Smith | faadef7 | 2013-06-08 00:02:08 +0000 | [diff] [blame] | 3318 | InitializationSequence &Sequence) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3319 | // First, catch C++03 where this isn't possible. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3320 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3321 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3322 | return; |
| 3323 | } |
| 3324 | |
| 3325 | QualType DestType = Entity.getType(); |
| 3326 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3327 | Qualifiers T1Quals; |
| 3328 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3329 | |
| 3330 | // Reference initialization via an initializer list works thus: |
| 3331 | // If the initializer list consists of a single element that is |
| 3332 | // reference-related to the referenced type, bind directly to that element |
| 3333 | // (possibly creating temporaries). |
| 3334 | // Otherwise, initialize a temporary with the initializer list and |
| 3335 | // bind to that. |
| 3336 | if (InitList->getNumInits() == 1) { |
| 3337 | Expr *Initializer = InitList->getInit(0); |
| 3338 | QualType cv2T2 = Initializer->getType(); |
| 3339 | Qualifiers T2Quals; |
| 3340 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3341 | |
| 3342 | // If this fails, creating a temporary wouldn't work either. |
| 3343 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3344 | T1, Sequence)) |
| 3345 | return; |
| 3346 | |
| 3347 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3348 | bool dummy1, dummy2, dummy3; |
| 3349 | Sema::ReferenceCompareResult RefRelationship |
| 3350 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3351 | dummy2, dummy3); |
| 3352 | if (RefRelationship >= Sema::Ref_Related) { |
| 3353 | // Try to bind the reference here. |
| 3354 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3355 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3356 | if (Sequence) |
| 3357 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3358 | return; |
| 3359 | } |
Richard Smith | 03d9393 | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3360 | |
| 3361 | // Update the initializer if we've resolved an overloaded function. |
| 3362 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3363 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
| 3366 | // Not reference-related. Create a temporary and bind to that. |
| 3367 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3368 | |
| 3369 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3370 | if (Sequence) { |
| 3371 | if (DestType->isRValueReferenceType() || |
| 3372 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3373 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3374 | else |
| 3375 | Sequence.SetFailed( |
| 3376 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3377 | } |
| 3378 | } |
| 3379 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3380 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3381 | static void TryListInitialization(Sema &S, |
| 3382 | const InitializedEntity &Entity, |
| 3383 | const InitializationKind &Kind, |
| 3384 | InitListExpr *InitList, |
| 3385 | InitializationSequence &Sequence) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3386 | QualType DestType = Entity.getType(); |
| 3387 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3388 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3389 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3390 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3391 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3392 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3393 | return; |
| 3394 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3395 | if (DestType->isReferenceType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3396 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3397 | return; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3398 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3399 | if (DestType->isRecordType()) { |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3400 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3401 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3402 | return; |
| 3403 | } |
| 3404 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3405 | // C++11 [dcl.init.list]p3: |
| 3406 | // - If T is an aggregate, aggregate initialization is performed. |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3407 | if (!DestType->isAggregateType()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3408 | if (S.getLangOpts().CPlusPlus11) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3409 | // - Otherwise, if the initializer list has no elements and T is a |
| 3410 | // class type with a default constructor, the object is |
| 3411 | // value-initialized. |
| 3412 | if (InitList->getNumInits() == 0) { |
| 3413 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3414 | if (RD->hasDefaultConstructor()) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3415 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3416 | return; |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3421 | // an initializer_list object constructed [...] |
| 3422 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3423 | return; |
| 3424 | |
| 3425 | // - Otherwise, if T is a class type, constructors are considered. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3426 | Expr *InitListAsExpr = InitList; |
| 3427 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3428 | Sequence, /*InitListSyntax*/true); |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3429 | } else |
| 3430 | Sequence.SetFailed( |
| 3431 | InitializationSequence::FK_InitListBadDestinationType); |
| 3432 | return; |
| 3433 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3434 | } |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 3435 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
| 3436 | InitList->getNumInits() == 1 && |
| 3437 | InitList->getInit(0)->getType()->isRecordType()) { |
| 3438 | // - Otherwise, if the initializer list has a single element of type E |
| 3439 | // [...references are handled above...], the object or reference is |
| 3440 | // initialized from that element; if a narrowing conversion is required |
| 3441 | // to convert the element to T, the program is ill-formed. |
| 3442 | // |
| 3443 | // Per core-24034, this is direct-initialization if we were performing |
| 3444 | // direct-list-initialization and copy-initialization otherwise. |
| 3445 | // We can't use InitListChecker for this, because it always performs |
| 3446 | // copy-initialization. This only matters if we might use an 'explicit' |
| 3447 | // conversion operator, so we only need to handle the cases where the source |
| 3448 | // is of record type. |
| 3449 | InitializationKind SubKind = |
| 3450 | Kind.getKind() == InitializationKind::IK_DirectList |
| 3451 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 3452 | InitList->getLBraceLoc(), |
| 3453 | InitList->getRBraceLoc()) |
| 3454 | : Kind; |
| 3455 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 3456 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 3457 | /*TopLevelOfInitList*/true); |
| 3458 | if (Sequence) |
| 3459 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 3460 | return; |
| 3461 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3462 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3463 | InitListChecker CheckInitList(S, Entity, InitList, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 3464 | DestType, /*VerifyOnly=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3465 | if (CheckInitList.HadError()) { |
| 3466 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3467 | return; |
| 3468 | } |
| 3469 | |
| 3470 | // Add the list initialization step with the built init list. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3471 | Sequence.AddListInitializationStep(DestType); |
| 3472 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3473 | |
| 3474 | /// \brief Try a reference initialization that involves calling a conversion |
| 3475 | /// function. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3476 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3477 | const InitializedEntity &Entity, |
| 3478 | const InitializationKind &Kind, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3479 | Expr *Initializer, |
| 3480 | bool AllowRValues, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3481 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3482 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3483 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3484 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3485 | QualType cv2T2 = Initializer->getType(); |
| 3486 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3487 | |
| 3488 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3489 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3490 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3491 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3492 | T1, T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3493 | ObjCConversion, |
| 3494 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3495 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3496 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3497 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3498 | (void)ObjCLifetimeConversion; |
| 3499 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3500 | // Build the candidate set directly in the initialization sequence |
| 3501 | // structure, so that it will persist if we fail. |
| 3502 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3503 | CandidateSet.clear(); |
| 3504 | |
| 3505 | // Determine whether we are allowed to call explicit constructors or |
| 3506 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3507 | bool AllowExplicit = Kind.AllowExplicit(); |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3508 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 3509 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3510 | const RecordType *T1RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3511 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3512 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3513 | // The type we're converting to is a class type. Enumerate its constructors |
| 3514 | // to see if there is a suitable conversion. |
| 3515 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3516 | |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3517 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3518 | // The container holding the constructors can under certain conditions |
| 3519 | // be changed while iterating (e.g. because of deserialization). |
| 3520 | // To be safe we copy the lookup results to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3521 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3522 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3523 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3524 | NamedDecl *D = *CI; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3525 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3526 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3527 | // Find the constructor (which may be a template). |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3528 | CXXConstructorDecl *Constructor = nullptr; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3529 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3530 | if (ConstructorTmpl) |
| 3531 | Constructor = cast<CXXConstructorDecl>( |
| 3532 | ConstructorTmpl->getTemplatedDecl()); |
| 3533 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3534 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3535 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3536 | if (!Constructor->isInvalidDecl() && |
| 3537 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3538 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3539 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3540 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3541 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3542 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3543 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3544 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3545 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3546 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3547 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3548 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3549 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3550 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3551 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3552 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3553 | const RecordType *T2RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3554 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3555 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3556 | // The type we're converting from is a class type, enumerate its conversion |
| 3557 | // functions. |
| 3558 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3559 | |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3560 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3561 | CXXRecordDecl::conversion_iterator> |
| 3562 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3563 | for (CXXRecordDecl::conversion_iterator |
| 3564 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3565 | NamedDecl *D = *I; |
| 3566 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3567 | if (isa<UsingShadowDecl>(D)) |
| 3568 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3569 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3570 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3571 | CXXConversionDecl *Conv; |
| 3572 | if (ConvTemplate) |
| 3573 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3574 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3575 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3576 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3577 | // If the conversion function doesn't return a reference type, |
| 3578 | // it can't be considered for this conversion unless we're allowed to |
| 3579 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3580 | // FIXME: Do we need to make sure that we only consider conversion |
| 3581 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3582 | // break recursion. |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3583 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3584 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3585 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3586 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3587 | ActingDC, Initializer, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 3588 | DestType, CandidateSet, |
| 3589 | /*AllowObjCConversionOnExplicit=*/ |
| 3590 | false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3591 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3592 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 3593 | Initializer, DestType, CandidateSet, |
| 3594 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3595 | } |
| 3596 | } |
| 3597 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3598 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3599 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3600 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3601 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3602 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3603 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3604 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3605 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3606 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3607 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3608 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3609 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3610 | // This is the overload that will be used for this initialization step if we |
| 3611 | // use this initialization. Mark it as referenced. |
| 3612 | Function->setReferenced(); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3613 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3614 | // Compute the returned type of the conversion. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3615 | if (isa<CXXConversionDecl>(Function)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3616 | T2 = Function->getReturnType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3617 | else |
| 3618 | T2 = cv1T1; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3619 | |
| 3620 | // Add the user-defined conversion step. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3621 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3622 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3623 | T2.getNonLValueExprType(S.Context), |
| 3624 | HadMultipleCandidates); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3625 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3626 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3627 | // cv-qualification adjustments. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3628 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3629 | if (T2->isLValueReferenceType()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3630 | VK = VK_LValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3631 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3632 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3633 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3634 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3635 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3636 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3637 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3638 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3639 | T2.getNonLValueExprType(S.Context), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3640 | NewDerivedToBase, NewObjCConversion, |
| 3641 | NewObjCLifetimeConversion); |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3642 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3643 | // If the type we've converted to is not reference-related to the |
| 3644 | // type we're looking for, then there is another conversion step |
| 3645 | // we need to perform to produce a temporary of the right type |
| 3646 | // that we'll be binding to. |
| 3647 | ImplicitConversionSequence ICS; |
| 3648 | ICS.setStandard(); |
| 3649 | ICS.Standard = Best->FinalConversion; |
| 3650 | T2 = ICS.Standard.getToType(2); |
| 3651 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3652 | } else if (NewDerivedToBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3653 | Sequence.AddDerivedToBaseCastStep( |
| 3654 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3655 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3656 | VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3657 | else if (NewObjCConversion) |
| 3658 | Sequence.AddObjCObjectConversionStep( |
| 3659 | S.Context.getQualifiedType(T1, |
| 3660 | T2.getNonReferenceType().getQualifiers())); |
| 3661 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3662 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3663 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3664 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3665 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3666 | return OR_Success; |
| 3667 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3668 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3669 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3670 | const InitializedEntity &Entity, |
| 3671 | Expr *CurInitExpr); |
| 3672 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3673 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3674 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3675 | const InitializedEntity &Entity, |
| 3676 | const InitializationKind &Kind, |
| 3677 | Expr *Initializer, |
| 3678 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3679 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3680 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3681 | Qualifiers T1Quals; |
| 3682 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3683 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3684 | Qualifiers T2Quals; |
| 3685 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3686 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3687 | // If the initializer is the address of an overloaded function, try |
| 3688 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3689 | // type of the resulting function. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3690 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3691 | T1, Sequence)) |
| 3692 | return; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3693 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3694 | // Delegate everything else to a subfunction. |
| 3695 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3696 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3697 | } |
| 3698 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3699 | /// Converts the target of reference initialization so that it has the |
| 3700 | /// appropriate qualifiers and value kind. |
| 3701 | /// |
| 3702 | /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'. |
| 3703 | /// \code |
| 3704 | /// int x; |
| 3705 | /// const int &r = x; |
| 3706 | /// \endcode |
| 3707 | /// |
| 3708 | /// In this case the reference is binding to a bitfield lvalue, which isn't |
| 3709 | /// valid. Perform a load to create a lifetime-extended temporary instead. |
| 3710 | /// \code |
| 3711 | /// const int &r = someStruct.bitfield; |
| 3712 | /// \endcode |
| 3713 | static ExprValueKind |
| 3714 | convertQualifiersAndValueKindIfNecessary(Sema &S, |
| 3715 | InitializationSequence &Sequence, |
| 3716 | Expr *Initializer, |
| 3717 | QualType cv1T1, |
| 3718 | Qualifiers T1Quals, |
| 3719 | Qualifiers T2Quals, |
| 3720 | bool IsLValueRef) { |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3721 | bool IsNonAddressableType = Initializer->refersToBitField() || |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3722 | Initializer->refersToVectorElement(); |
| 3723 | |
| 3724 | if (IsNonAddressableType) { |
| 3725 | // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an |
| 3726 | // lvalue reference to a non-volatile const type, or the reference shall be |
| 3727 | // an rvalue reference. |
| 3728 | // |
| 3729 | // If not, we can't make a temporary and bind to that. Give up and allow the |
| 3730 | // error to be diagnosed later. |
| 3731 | if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) { |
| 3732 | assert(Initializer->isGLValue()); |
| 3733 | return Initializer->getValueKind(); |
| 3734 | } |
| 3735 | |
| 3736 | // Force a load so we can materialize a temporary. |
| 3737 | Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType()); |
| 3738 | return VK_RValue; |
| 3739 | } |
| 3740 | |
| 3741 | if (T1Quals != T2Quals) { |
| 3742 | Sequence.AddQualificationConversionStep(cv1T1, |
| 3743 | Initializer->getValueKind()); |
| 3744 | } |
| 3745 | |
| 3746 | return Initializer->getValueKind(); |
| 3747 | } |
| 3748 | |
| 3749 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3750 | /// \brief Reference initialization without resolving overloaded functions. |
| 3751 | static void TryReferenceInitializationCore(Sema &S, |
| 3752 | const InitializedEntity &Entity, |
| 3753 | const InitializationKind &Kind, |
| 3754 | Expr *Initializer, |
| 3755 | QualType cv1T1, QualType T1, |
| 3756 | Qualifiers T1Quals, |
| 3757 | QualType cv2T2, QualType T2, |
| 3758 | Qualifiers T2Quals, |
| 3759 | InitializationSequence &Sequence) { |
| 3760 | QualType DestType = Entity.getType(); |
| 3761 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3762 | // Compute some basic properties of the types and the initializer. |
| 3763 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3764 | bool isRValueRef = !isLValueRef; |
| 3765 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3766 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3767 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3768 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3769 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3770 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3771 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3772 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3773 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3774 | // 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] | 3775 | // "cv2 T2" as follows: |
| 3776 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3777 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3778 | // expression |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3779 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3780 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3781 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3782 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3783 | bool T1Function = T1->isFunctionType(); |
| 3784 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3785 | if (InitCategory.isLValue() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3786 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3787 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3788 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3789 | // - 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] | 3790 | // reference-compatible with "cv2 T2," or |
| 3791 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3792 | // 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] | 3793 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3794 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3795 | // to decide whether we're actually binding to a temporary created from |
| 3796 | // the bit-field. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3797 | if (DerivedToBase) |
| 3798 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3799 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3800 | VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3801 | else if (ObjCConversion) |
| 3802 | Sequence.AddObjCObjectConversionStep( |
| 3803 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3804 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3805 | ExprValueKind ValueKind = |
| 3806 | convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer, |
| 3807 | cv1T1, T1Quals, T2Quals, |
| 3808 | isLValueRef); |
| 3809 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3810 | return; |
| 3811 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3812 | |
| 3813 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3814 | // reference-related to T2, and can be implicitly converted to an |
| 3815 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3816 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3817 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3818 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3819 | // 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] | 3820 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3821 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3822 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3823 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3824 | S, Entity, Kind, Initializer, /*AllowRValues*/isRValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3825 | if (ConvOvlResult == OR_Success) |
| 3826 | return; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3827 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3828 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3829 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3830 | ConvOvlResult); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3831 | } |
| 3832 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3833 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3834 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3835 | // 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] | 3836 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3837 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3838 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3839 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3840 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3841 | Sequence.SetOverloadFailure( |
| 3842 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3843 | ConvOvlResult); |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3844 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3845 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3846 | ? (RefRelationship == Sema::Ref_Related |
| 3847 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3848 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3849 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3850 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3851 | return; |
| 3852 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3853 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3854 | // - If the initializer expression |
| 3855 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3856 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3857 | // Note: functions are handled below. |
| 3858 | if (!T1Function && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3859 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3860 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3861 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3862 | (InitCategory.isXValue() || |
| 3863 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3864 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3865 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3866 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3867 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3868 | // compiler the freedom to perform a copy here or bind to the |
| 3869 | // object, while C++0x requires that we bind directly to the |
| 3870 | // object. Hence, we always bind to the object without making an |
| 3871 | // extra copy. However, in C++03 requires that we check for the |
| 3872 | // presence of a suitable copy constructor: |
| 3873 | // |
| 3874 | // The constructor that would be used to make the copy shall |
| 3875 | // be callable whether or not the copy is actually done. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3876 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3877 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3878 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3879 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3880 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3881 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3882 | if (DerivedToBase) |
| 3883 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3884 | ValueKind); |
| 3885 | else if (ObjCConversion) |
| 3886 | Sequence.AddObjCObjectConversionStep( |
| 3887 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3888 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3889 | ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence, |
| 3890 | Initializer, cv1T1, |
| 3891 | T1Quals, T2Quals, |
| 3892 | isLValueRef); |
| 3893 | |
| 3894 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3895 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3896 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3897 | |
| 3898 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3899 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3900 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3901 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3902 | // |
| 3903 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3904 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3905 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3906 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3907 | S, Entity, Kind, Initializer, /*AllowRValues*/true, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3908 | if (ConvOvlResult) |
| 3909 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3910 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3911 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3912 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3913 | return; |
| 3914 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3915 | |
Douglas Gregor | 6fa6ab0 | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 3916 | if ((RefRelationship == Sema::Ref_Compatible || |
| 3917 | RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) && |
| 3918 | isRValueRef && InitCategory.isLValue()) { |
| 3919 | Sequence.SetFailed( |
| 3920 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3921 | return; |
| 3922 | } |
| 3923 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3924 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3925 | return; |
| 3926 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3927 | |
| 3928 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3929 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3930 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3931 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3932 | |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3933 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3934 | |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3935 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 3936 | // copy-initialization? |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3937 | ImplicitConversionSequence ICS |
| 3938 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3939 | /*SuppressUserConversions=*/false, |
| 3940 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3941 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3942 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3943 | /*AllowObjCWritebackConversion=*/false); |
| 3944 | |
| 3945 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3946 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3947 | // this into an overloading ambiguity diagnostic. However, we need |
| 3948 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3949 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3950 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3951 | Sequence.SetOverloadFailure( |
| 3952 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3953 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3954 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3955 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3956 | else |
| 3957 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3958 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3959 | } else { |
| 3960 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3964 | // same cv-qualification as, or greater cv-qualification |
| 3965 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3966 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3967 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3968 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3969 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3970 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3971 | return; |
| 3972 | } |
| 3973 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3974 | // [...] 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] | 3975 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3976 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3977 | InitCategory.isLValue()) { |
| 3978 | Sequence.SetFailed( |
| 3979 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3980 | return; |
| 3981 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3982 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3983 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3984 | return; |
| 3985 | } |
| 3986 | |
| 3987 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3988 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3989 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3990 | const InitializedEntity &Entity, |
| 3991 | const InitializationKind &Kind, |
| 3992 | Expr *Initializer, |
| 3993 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3994 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3997 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3998 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3999 | const InitializedEntity &Entity, |
| 4000 | const InitializationKind &Kind, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4001 | InitializationSequence &Sequence, |
| 4002 | InitListExpr *InitList) { |
| 4003 | assert((!InitList || InitList->getNumInits() == 0) && |
| 4004 | "Shouldn't use value-init for non-empty init lists"); |
| 4005 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4006 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4007 | // |
| 4008 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4009 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4010 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4011 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4012 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4013 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4014 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 4015 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4016 | bool NeedZeroInitialization = true; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4017 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4018 | // C++98: |
| 4019 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 4020 | // (12.1), then the default constructor for T is called (and the |
| 4021 | // initialization is ill-formed if T has no accessible default |
| 4022 | // constructor); |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4023 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4024 | NeedZeroInitialization = false; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4025 | } else { |
| 4026 | // C++11: |
| 4027 | // -- if T is a class type (clause 9) with either no default constructor |
| 4028 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 4029 | // or deleted, then the object is default-initialized; |
| 4030 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 4031 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4032 | NeedZeroInitialization = false; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4033 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4034 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4035 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 4036 | // user-provided or deleted default constructor, then the object is |
| 4037 | // zero-initialized and, if T has a non-trivial default constructor, |
| 4038 | // default-initialized; |
Richard Smith | fb26652 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 4039 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 4040 | // constructor' part was removed by DR1507. |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4041 | if (NeedZeroInitialization) |
| 4042 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4043 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4044 | // C++03: |
| 4045 | // -- if T is a non-union class type without a user-declared constructor, |
| 4046 | // then every non-static data member and base class component of T is |
| 4047 | // value-initialized; |
| 4048 | // [...] A program that calls for [...] value-initialization of an |
| 4049 | // entity of reference type is ill-formed. |
| 4050 | // |
| 4051 | // C++11 doesn't need this handling, because value-initialization does not |
| 4052 | // occur recursively there, and the implicit default constructor is |
| 4053 | // defined as deleted in the problematic cases. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4054 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4055 | ClassDecl->hasUninitializedReferenceMember()) { |
| 4056 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 4057 | return; |
| 4058 | } |
| 4059 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4060 | // If this is list-value-initialization, pass the empty init list on when |
| 4061 | // building the constructor call. This affects the semantics of a few |
| 4062 | // things (such as whether an explicit default constructor can be called). |
| 4063 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4064 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4065 | bool InitListSyntax = InitList; |
| 4066 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4067 | return TryConstructorInitialization(S, Entity, Kind, Args, T, Sequence, |
| 4068 | InitListSyntax); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4069 | } |
| 4070 | } |
| 4071 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4072 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4075 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 4076 | static void TryDefaultInitialization(Sema &S, |
| 4077 | const InitializedEntity &Entity, |
| 4078 | const InitializationKind &Kind, |
| 4079 | InitializationSequence &Sequence) { |
| 4080 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4081 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4082 | // C++ [dcl.init]p6: |
| 4083 | // To default-initialize an object of type T means: |
| 4084 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4085 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 4086 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4087 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 4088 | // constructor for T is called (and the initialization is ill-formed if |
| 4089 | // T has no accessible default constructor); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4090 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 4091 | TryConstructorInitialization(S, Entity, Kind, None, DestType, Sequence); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4092 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4093 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4094 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4095 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4096 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4097 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4098 | // 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] | 4099 | // default constructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4100 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4101 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4102 | return; |
| 4103 | } |
| 4104 | |
| 4105 | // If the destination type has a lifetime property, zero-initialize it. |
| 4106 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4107 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4108 | return; |
| 4109 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4112 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 4113 | /// which enumerates all conversion functions and performs overload resolution |
| 4114 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4115 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4116 | const InitializedEntity &Entity, |
| 4117 | const InitializationKind &Kind, |
| 4118 | Expr *Initializer, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4119 | InitializationSequence &Sequence, |
| 4120 | bool TopLevelOfInitList) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4121 | QualType DestType = Entity.getType(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4122 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4123 | QualType SourceType = Initializer->getType(); |
| 4124 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4125 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4126 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4127 | // Build the candidate set directly in the initialization sequence |
| 4128 | // structure, so that it will persist if we fail. |
| 4129 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 4130 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4131 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4132 | // Determine whether we are allowed to call explicit constructors or |
| 4133 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4134 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4135 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4136 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4137 | // The type we're converting to is a class type. Enumerate its constructors |
| 4138 | // to see if there is a suitable conversion. |
| 4139 | CXXRecordDecl *DestRecordDecl |
| 4140 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4141 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4142 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4143 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4144 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 12be639 | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4145 | // The container holding the constructors can under certain conditions |
| 4146 | // be changed while iterating. To be safe we copy the lookup results |
| 4147 | // to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4148 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4149 | for (SmallVectorImpl<NamedDecl *>::iterator |
David Blaikie | 12be639 | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4150 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4151 | Con != ConEnd; ++Con) { |
| 4152 | NamedDecl *D = *Con; |
| 4153 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4154 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4155 | // Find the constructor (which may be a template). |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4156 | CXXConstructorDecl *Constructor = nullptr; |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4157 | FunctionTemplateDecl *ConstructorTmpl |
| 4158 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4159 | if (ConstructorTmpl) |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4160 | Constructor = cast<CXXConstructorDecl>( |
| 4161 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4162 | else |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4163 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4164 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4165 | if (!Constructor->isInvalidDecl() && |
| 4166 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4167 | if (ConstructorTmpl) |
| 4168 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4169 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4170 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4171 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4172 | else |
| 4173 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4174 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4175 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4176 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4177 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4178 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4179 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4180 | |
| 4181 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 4182 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4183 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4184 | // The type we're converting from is a class type, enumerate its conversion |
| 4185 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4186 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4187 | // We can only enumerate the conversion functions for a complete type; if |
| 4188 | // the type isn't complete, simply skip this step. |
| 4189 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 4190 | CXXRecordDecl *SourceRecordDecl |
| 4191 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4192 | |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4193 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4194 | CXXRecordDecl::conversion_iterator> |
| 4195 | Conversions = SourceRecordDecl->getVisibleConversionFunctions(); |
| 4196 | for (CXXRecordDecl::conversion_iterator |
| 4197 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4198 | NamedDecl *D = *I; |
| 4199 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4200 | if (isa<UsingShadowDecl>(D)) |
| 4201 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4202 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4203 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4204 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4205 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4206 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4207 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4208 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4209 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4210 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4211 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4212 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4213 | ActingDC, Initializer, DestType, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4214 | CandidateSet, AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4215 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4216 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4217 | Initializer, DestType, CandidateSet, |
| 4218 | AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4219 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4220 | } |
| 4221 | } |
| 4222 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4223 | |
| 4224 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4225 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4226 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4227 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4228 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4229 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4230 | Result); |
| 4231 | return; |
| 4232 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4233 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4234 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4235 | Function->setReferenced(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4236 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4237 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4238 | if (isa<CXXConstructorDecl>(Function)) { |
| 4239 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4240 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 4241 | // cv-unqualified type of the destination. |
| 4242 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 4243 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4244 | HadMultipleCandidates); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4245 | return; |
| 4246 | } |
| 4247 | |
| 4248 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4249 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4250 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4251 | // 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] | 4252 | // the resulting temporary object (possible to create an object of |
| 4253 | // a base class type). That copy is not a separate conversion, so |
| 4254 | // we just make a note of the actual destination type (possibly a |
| 4255 | // base class of the type returned by the conversion function) and |
| 4256 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4257 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 4258 | HadMultipleCandidates); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4259 | return; |
| 4260 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4261 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4262 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 4263 | HadMultipleCandidates); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4264 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4265 | // If the conversion following the call to the conversion function |
| 4266 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4267 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 4268 | Best->FinalConversion.Third) { |
| 4269 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4270 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4271 | ICS.Standard = Best->FinalConversion; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4272 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4273 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4276 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 4277 | /// a function with a pointer return type contains a 'return false;' statement. |
| 4278 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 4279 | /// code using that header. |
| 4280 | /// |
| 4281 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 4282 | /// if it's used in a pointer-returning function in a system header. |
| 4283 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 4284 | const InitializedEntity &Entity, |
| 4285 | const Expr *Init) { |
| 4286 | return S.getLangOpts().CPlusPlus11 && |
| 4287 | Entity.getKind() == InitializedEntity::EK_Result && |
| 4288 | Entity.getType()->isPointerType() && |
| 4289 | isa<CXXBoolLiteralExpr>(Init) && |
| 4290 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 4291 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 4292 | } |
| 4293 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4294 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 4295 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 4296 | |
| 4297 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4298 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4299 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4300 | // Skip parens. |
| 4301 | e = e->IgnoreParens(); |
| 4302 | |
| 4303 | // Skip address-of nodes. |
| 4304 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 4305 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4306 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 4307 | isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4308 | |
| 4309 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4310 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 4311 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4312 | case CK_Dependent: |
| 4313 | case CK_BitCast: |
| 4314 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4315 | case CK_NoOp: |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4316 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4317 | |
| 4318 | case CK_ArrayToPointerDecay: |
| 4319 | return IIK_nonscalar; |
| 4320 | |
| 4321 | case CK_NullToPointer: |
| 4322 | return IIK_okay; |
| 4323 | |
| 4324 | default: |
| 4325 | break; |
| 4326 | } |
| 4327 | |
| 4328 | // 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] | 4329 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4330 | // set isWeakAccess to true, to mean that there will be an implicit |
| 4331 | // load which requires a cleanup. |
| 4332 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4333 | isWeakAccess = true; |
| 4334 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4335 | if (!isAddressOf) return IIK_nonlocal; |
| 4336 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4337 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 4338 | if (!var) return IIK_nonlocal; |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4339 | |
| 4340 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4341 | |
| 4342 | // If we have a conditional operator, check both sides. |
| 4343 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4344 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 4345 | isWeakAccess)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4346 | return iik; |
| 4347 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4348 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4349 | |
| 4350 | // These are never scalar. |
| 4351 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 4352 | return IIK_nonscalar; |
| 4353 | |
| 4354 | // Otherwise, it needs to be a null pointer constant. |
| 4355 | } else { |
| 4356 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 4357 | ? IIK_okay : IIK_nonlocal); |
| 4358 | } |
| 4359 | |
| 4360 | return IIK_nonlocal; |
| 4361 | } |
| 4362 | |
| 4363 | /// Check whether the given expression is a valid operand for an |
| 4364 | /// indirect copy/restore. |
| 4365 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 4366 | assert(src->isRValue()); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4367 | bool isWeakAccess = false; |
| 4368 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 4369 | // If isWeakAccess to true, there will be an implicit |
| 4370 | // load which requires a cleanup. |
| 4371 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 4372 | S.ExprNeedsCleanups = true; |
| 4373 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4374 | if (iik == IIK_okay) return; |
| 4375 | |
| 4376 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 4377 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 4378 | << src->getSourceRange(); |
| 4379 | } |
| 4380 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4381 | /// \brief Determine whether we have compatible array types for the |
| 4382 | /// purposes of GNU by-copy array initialization. |
| 4383 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 4384 | const ArrayType *Dest, |
| 4385 | const ArrayType *Source) { |
| 4386 | // If the source and destination array types are equivalent, we're |
| 4387 | // done. |
| 4388 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 4389 | return true; |
| 4390 | |
| 4391 | // Make sure that the element types are the same. |
| 4392 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 4393 | return false; |
| 4394 | |
| 4395 | // The only mismatch we allow is when the destination is an |
| 4396 | // incomplete array type and the source is a constant array type. |
| 4397 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 4398 | } |
| 4399 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4400 | static bool tryObjCWritebackConversion(Sema &S, |
| 4401 | InitializationSequence &Sequence, |
| 4402 | const InitializedEntity &Entity, |
| 4403 | Expr *Initializer) { |
| 4404 | bool ArrayDecay = false; |
| 4405 | QualType ArgType = Initializer->getType(); |
| 4406 | QualType ArgPointee; |
| 4407 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 4408 | ArrayDecay = true; |
| 4409 | ArgPointee = ArgArrayType->getElementType(); |
| 4410 | ArgType = S.Context.getPointerType(ArgPointee); |
| 4411 | } |
| 4412 | |
| 4413 | // Handle write-back conversion. |
| 4414 | QualType ConvertedArgType; |
| 4415 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 4416 | ConvertedArgType)) |
| 4417 | return false; |
| 4418 | |
| 4419 | // We should copy unless we're passing to an argument explicitly |
| 4420 | // marked 'out'. |
| 4421 | bool ShouldCopy = true; |
| 4422 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4423 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4424 | |
| 4425 | // Do we need an lvalue conversion? |
| 4426 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4427 | ImplicitConversionSequence ICS; |
| 4428 | ICS.setStandard(); |
| 4429 | ICS.Standard.setAsIdentityConversion(); |
| 4430 | |
| 4431 | QualType ResultType; |
| 4432 | if (ArrayDecay) { |
| 4433 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4434 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4435 | } else { |
| 4436 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4437 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4438 | } |
| 4439 | |
| 4440 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4441 | } |
| 4442 | |
| 4443 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4444 | return true; |
| 4445 | } |
| 4446 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4447 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4448 | InitializationSequence &Sequence, |
| 4449 | QualType DestType, |
| 4450 | Expr *Initializer) { |
| 4451 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4452 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4453 | return false; |
| 4454 | |
| 4455 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4456 | return true; |
| 4457 | } |
| 4458 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4459 | // |
| 4460 | // OpenCL 1.2 spec, s6.12.10 |
| 4461 | // |
| 4462 | // The event argument can also be used to associate the |
| 4463 | // async_work_group_copy with a previous async copy allowing |
| 4464 | // an event to be shared by multiple async copies; otherwise |
| 4465 | // event should be zero. |
| 4466 | // |
| 4467 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4468 | InitializationSequence &Sequence, |
| 4469 | QualType DestType, |
| 4470 | Expr *Initializer) { |
| 4471 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4472 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4473 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4474 | return false; |
| 4475 | |
| 4476 | Sequence.AddOCLZeroEventStep(DestType); |
| 4477 | return true; |
| 4478 | } |
| 4479 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4480 | InitializationSequence::InitializationSequence(Sema &S, |
| 4481 | const InitializedEntity &Entity, |
| 4482 | const InitializationKind &Kind, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4483 | MultiExprArg Args, |
| 4484 | bool TopLevelOfInitList) |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 4485 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4486 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList); |
| 4487 | } |
| 4488 | |
| 4489 | void InitializationSequence::InitializeFrom(Sema &S, |
| 4490 | const InitializedEntity &Entity, |
| 4491 | const InitializationKind &Kind, |
| 4492 | MultiExprArg Args, |
| 4493 | bool TopLevelOfInitList) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4494 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4495 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4496 | // Eliminate non-overload placeholder types in the arguments. We |
| 4497 | // need to do this before checking whether types are dependent |
| 4498 | // because lowering a pseudo-object expression might well give us |
| 4499 | // something of dependent type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4500 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4501 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4502 | // FIXME: should we be doing this here? |
| 4503 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4504 | if (result.isInvalid()) { |
| 4505 | SetFailed(FK_PlaceholderType); |
| 4506 | return; |
| 4507 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4508 | Args[I] = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4511 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4512 | // The semantics of initializers are as follows. The destination type is |
| 4513 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4514 | // 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] | 4515 | // 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] | 4516 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4517 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4518 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4519 | if (DestType->isDependentType() || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4520 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4521 | SequenceKind = DependentSequence; |
| 4522 | return; |
| 4523 | } |
| 4524 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4525 | // Almost everything is a normal sequence. |
| 4526 | setSequenceKind(NormalSequence); |
| 4527 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4528 | QualType SourceType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4529 | Expr *Initializer = nullptr; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4530 | if (Args.size() == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4531 | Initializer = Args[0]; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 4532 | if (S.getLangOpts().ObjC1) { |
| 4533 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getLocStart(), |
| 4534 | DestType, Initializer->getType(), |
| 4535 | Initializer) || |
| 4536 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 4537 | Args[0] = Initializer; |
| 4538 | |
| 4539 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4540 | if (!isa<InitListExpr>(Initializer)) |
| 4541 | SourceType = Initializer->getType(); |
| 4542 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4543 | |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4544 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4545 | // object is list-initialized (8.5.4). |
| 4546 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4547 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4548 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4549 | return; |
| 4550 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4551 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4552 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4553 | // - If the destination type is a reference type, see 8.5.3. |
| 4554 | if (DestType->isReferenceType()) { |
| 4555 | // C++0x [dcl.init.ref]p1: |
| 4556 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4557 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4558 | // by an object that can be converted into a T. |
| 4559 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4560 | if (Args.size() != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4561 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4562 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4563 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4564 | return; |
| 4565 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4566 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4567 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4568 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4569 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4570 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4571 | return; |
| 4572 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4573 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4574 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4575 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4576 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4577 | return; |
| 4578 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4579 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4580 | // - If the destination type is an array of characters, an array of |
| 4581 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4582 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4583 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4584 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4585 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4586 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4587 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4588 | return; |
| 4589 | } |
| 4590 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4591 | if (Initializer) { |
| 4592 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 4593 | case SIF_None: |
| 4594 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 4595 | return; |
| 4596 | case SIF_NarrowStringIntoWideChar: |
| 4597 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 4598 | return; |
| 4599 | case SIF_WideStringIntoChar: |
| 4600 | SetFailed(FK_WideStringIntoCharArray); |
| 4601 | return; |
| 4602 | case SIF_IncompatWideStringIntoWideChar: |
| 4603 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 4604 | return; |
| 4605 | case SIF_Other: |
| 4606 | break; |
| 4607 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4610 | // Note: as an GNU C extension, we allow initialization of an |
| 4611 | // array from a compound literal that creates an array of the same |
| 4612 | // type, so long as the initializer has no side effects. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4613 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4614 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4615 | Initializer->getType()->isArrayType()) { |
| 4616 | const ArrayType *SourceAT |
| 4617 | = Context.getAsArrayType(Initializer->getType()); |
| 4618 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4619 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4620 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4621 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4622 | else { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4623 | AddArrayInitStep(DestType); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4624 | } |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4625 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4626 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4627 | // class member of array type from a parenthesized initializer list. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4628 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4629 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4630 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4631 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4632 | *this); |
| 4633 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4634 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4635 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4636 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 4637 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4638 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4639 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4640 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4641 | return; |
| 4642 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4643 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4644 | // Determine whether we should consider writeback conversions for |
| 4645 | // Objective-C ARC. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4646 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4647 | Entity.isParameterKind(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4648 | |
| 4649 | // We're at the end of the line for C: it's either a write-back conversion |
| 4650 | // 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] | 4651 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4652 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4653 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4654 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4655 | return; |
| 4656 | } |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4657 | |
| 4658 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4659 | return; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4660 | |
| 4661 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4662 | return; |
| 4663 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4664 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4665 | AddCAssignmentStep(DestType); |
| 4666 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4667 | return; |
| 4668 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4669 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4670 | assert(S.getLangOpts().CPlusPlus); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4671 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4672 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4673 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4674 | // - If the initialization is direct-initialization, or if it is |
| 4675 | // copy-initialization where the cv-unqualified version of the |
| 4676 | // 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] | 4677 | // class of the destination, constructors are considered. [...] |
| 4678 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4679 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4680 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4681 | S.IsDerivedFrom(SourceType, DestType)))) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4682 | TryConstructorInitialization(S, Entity, Kind, Args, |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4683 | Entity.getType(), *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4684 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4685 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4686 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4687 | // used) to a derived class thereof are enumerated as described in |
| 4688 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4689 | // (13.3). |
| 4690 | else |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4691 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this, |
| 4692 | TopLevelOfInitList); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4693 | return; |
| 4694 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4695 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4696 | if (Args.size() > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4697 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4698 | return; |
| 4699 | } |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4700 | assert(Args.size() == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4701 | |
| 4702 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4703 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4704 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4705 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this, |
| 4706 | TopLevelOfInitList); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4707 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4708 | return; |
| 4709 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4710 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4711 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4712 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4713 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4715 | // destination type; no user-defined conversions are considered. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4716 | |
| 4717 | ImplicitConversionSequence ICS |
| 4718 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4719 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4720 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4721 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4722 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4723 | allowObjCWritebackConversion); |
| 4724 | |
| 4725 | if (ICS.isStandard() && |
| 4726 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4727 | // Objective-C ARC writeback conversion. |
| 4728 | |
| 4729 | // We should copy unless we're passing to an argument explicitly |
| 4730 | // marked 'out'. |
| 4731 | bool ShouldCopy = true; |
| 4732 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4733 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4734 | |
| 4735 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4736 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4737 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4738 | ImplicitConversionSequence LvalueICS; |
| 4739 | LvalueICS.setStandard(); |
| 4740 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4741 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4742 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4743 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4744 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4745 | |
| 4746 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4747 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4748 | DeclAccessPair dap; |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4749 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 4750 | AddZeroInitializationStep(Entity.getType()); |
| 4751 | } else if (Initializer->getType() == Context.OverloadTy && |
| 4752 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 4753 | false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4754 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4755 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4756 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4757 | } else { |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4758 | AddConversionSequenceStep(ICS, Entity.getType(), TopLevelOfInitList); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4759 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4760 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4761 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
| 4764 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4765 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4766 | StepEnd = Steps.end(); |
| 4767 | Step != StepEnd; ++Step) |
| 4768 | Step->Destroy(); |
| 4769 | } |
| 4770 | |
| 4771 | //===----------------------------------------------------------------------===// |
| 4772 | // Perform initialization |
| 4773 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4774 | static Sema::AssignmentAction |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4775 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4776 | switch(Entity.getKind()) { |
| 4777 | case InitializedEntity::EK_Variable: |
| 4778 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4779 | case InitializedEntity::EK_Exception: |
| 4780 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4781 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4782 | return Sema::AA_Initializing; |
| 4783 | |
| 4784 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4785 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4786 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4787 | return Sema::AA_Sending; |
| 4788 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4789 | return Sema::AA_Passing; |
| 4790 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4791 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 4792 | if (Entity.getDecl() && |
| 4793 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4794 | return Sema::AA_Sending; |
| 4795 | |
| 4796 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
| 4797 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4798 | case InitializedEntity::EK_Result: |
| 4799 | return Sema::AA_Returning; |
| 4800 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4801 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | 14e9541 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 4802 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4803 | // FIXME: Can we tell apart casting vs. converting? |
| 4804 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4805 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4806 | case InitializedEntity::EK_Member: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4807 | case InitializedEntity::EK_ArrayElement: |
| 4808 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4809 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4810 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4811 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4812 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4813 | return Sema::AA_Initializing; |
| 4814 | } |
| 4815 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4816 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4819 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4820 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4821 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4822 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4823 | case InitializedEntity::EK_ArrayElement: |
| 4824 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4825 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4826 | case InitializedEntity::EK_New: |
| 4827 | case InitializedEntity::EK_Variable: |
| 4828 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4829 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4830 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4831 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4832 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4833 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4834 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4835 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4836 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4837 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4838 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4839 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4840 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4841 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4842 | return true; |
| 4843 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4844 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4845 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4846 | } |
| 4847 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4848 | /// \brief Whether the given entity, when initialized with an object |
| 4849 | /// created for that initialization, requires destruction. |
| 4850 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4851 | switch (Entity.getKind()) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4852 | case InitializedEntity::EK_Result: |
| 4853 | case InitializedEntity::EK_New: |
| 4854 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4855 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4856 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4857 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4858 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4859 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4860 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4861 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4862 | case InitializedEntity::EK_Member: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4863 | case InitializedEntity::EK_Variable: |
| 4864 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4865 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4866 | case InitializedEntity::EK_Temporary: |
| 4867 | case InitializedEntity::EK_ArrayElement: |
| 4868 | case InitializedEntity::EK_Exception: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4869 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4870 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4871 | return true; |
| 4872 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4873 | |
| 4874 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4877 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4878 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4879 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4880 | OverloadCandidateSet &CandidateSet, |
| 4881 | CXXRecordDecl *Class, |
| 4882 | Expr *CurInitExpr) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4883 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4884 | // The container holding the constructors can under certain conditions |
| 4885 | // be changed while iterating (e.g. because of deserialization). |
| 4886 | // To be safe we copy the lookup results to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4887 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4888 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4889 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4890 | NamedDecl *D = *CI; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4891 | CXXConstructorDecl *Constructor = nullptr; |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4892 | |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4893 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4894 | // Handle copy/moveconstructors, only. |
| 4895 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4896 | !Constructor->isCopyOrMoveConstructor() || |
| 4897 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4898 | continue; |
| 4899 | |
| 4900 | DeclAccessPair FoundDecl |
| 4901 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4902 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4903 | CurInitExpr, CandidateSet); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4904 | continue; |
| 4905 | } |
| 4906 | |
| 4907 | // Handle constructor templates. |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4908 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4909 | if (ConstructorTmpl->isInvalidDecl()) |
| 4910 | continue; |
| 4911 | |
| 4912 | Constructor = cast<CXXConstructorDecl>( |
| 4913 | ConstructorTmpl->getTemplatedDecl()); |
| 4914 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4915 | continue; |
| 4916 | |
| 4917 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4918 | // candidates? |
| 4919 | DeclAccessPair FoundDecl |
| 4920 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4921 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4922 | CurInitExpr, CandidateSet, true); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4923 | } |
| 4924 | } |
| 4925 | |
| 4926 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4927 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4928 | Expr *Initializer) { |
| 4929 | switch (Entity.getKind()) { |
| 4930 | case InitializedEntity::EK_Result: |
| 4931 | return Entity.getReturnLoc(); |
| 4932 | |
| 4933 | case InitializedEntity::EK_Exception: |
| 4934 | return Entity.getThrowLoc(); |
| 4935 | |
| 4936 | case InitializedEntity::EK_Variable: |
| 4937 | return Entity.getDecl()->getLocation(); |
| 4938 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4939 | case InitializedEntity::EK_LambdaCapture: |
| 4940 | return Entity.getCaptureLoc(); |
| 4941 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4942 | case InitializedEntity::EK_ArrayElement: |
| 4943 | case InitializedEntity::EK_Member: |
| 4944 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4945 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4946 | case InitializedEntity::EK_Temporary: |
| 4947 | case InitializedEntity::EK_New: |
| 4948 | case InitializedEntity::EK_Base: |
| 4949 | case InitializedEntity::EK_Delegating: |
| 4950 | case InitializedEntity::EK_VectorElement: |
| 4951 | case InitializedEntity::EK_ComplexElement: |
| 4952 | case InitializedEntity::EK_BlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4953 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4954 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4955 | return Initializer->getLocStart(); |
| 4956 | } |
| 4957 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4958 | } |
| 4959 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4960 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4961 | /// provided by the given initializer by calling the appropriate copy |
| 4962 | /// constructor. |
| 4963 | /// |
| 4964 | /// \param S The Sema object used for type-checking. |
| 4965 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4966 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4967 | /// the type of the initializer expression or a superclass thereof. |
| 4968 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4969 | /// \param Entity The entity being initialized. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4970 | /// |
| 4971 | /// \param CurInit The initializer expression. |
| 4972 | /// |
| 4973 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4974 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4975 | /// an rvalue. |
| 4976 | /// |
| 4977 | /// \returns An expression that copies the initializer expression into |
| 4978 | /// a temporary object, or an error expression if a copy could not be |
| 4979 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4980 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4981 | QualType T, |
| 4982 | const InitializedEntity &Entity, |
| 4983 | ExprResult CurInit, |
| 4984 | bool IsExtraneousCopy) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4985 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4986 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4987 | CXXRecordDecl *Class = nullptr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4988 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4989 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4990 | if (!Class) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4991 | return CurInit; |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4992 | |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4993 | // C++0x [class.copy]p32: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4994 | // When certain criteria are met, an implementation is allowed to |
| 4995 | // omit the copy/move construction of a class object, even if the |
| 4996 | // copy/move constructor and/or destructor for the object have |
| 4997 | // side effects. [...] |
| 4998 | // - when a temporary class object that has not been bound to a |
| 4999 | // reference (12.2) would be copied/moved to a class object |
| 5000 | // with the same cv-unqualified type, the copy/move operation |
| 5001 | // can be omitted by constructing the temporary object |
| 5002 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5003 | // |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5004 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 5005 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5006 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 5007 | // is handled by the run-time. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5008 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5009 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5010 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5011 | // Make sure that the type we are copying is complete. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5012 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5013 | return CurInit; |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5014 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 5015 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5016 | // Only consider constructors and constructor templates. Per |
| 5017 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 5018 | // is direct-initialization. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5019 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5020 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5021 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5022 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 5023 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5024 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 5025 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5026 | case OR_Success: |
| 5027 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5028 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5029 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5030 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 5031 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 5032 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5033 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5034 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5035 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5036 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5037 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5038 | return CurInit; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5039 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5040 | case OR_Ambiguous: |
| 5041 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5042 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5043 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5044 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5045 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5046 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5047 | case OR_Deleted: |
| 5048 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5049 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5050 | << CurInitExpr->getSourceRange(); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5051 | S.NoteDeletedFunction(Best->Function); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5052 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5053 | } |
| 5054 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5055 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5056 | SmallVector<Expr*, 8> ConstructorArgs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5057 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5058 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5059 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5060 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5061 | |
| 5062 | if (IsExtraneousCopy) { |
| 5063 | // If this is a totally extraneous copy for C++03 reference |
| 5064 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5065 | // expression. We don't generate an (elided) copy operation here |
| 5066 | // because doing so would require us to pass down a flag to avoid |
| 5067 | // infinite recursion, where each step adds another extraneous, |
| 5068 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5069 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5070 | // Instantiate the default arguments of any extra parameters in |
| 5071 | // the selected copy constructor, as if we were going to create a |
| 5072 | // proper call to the copy constructor. |
| 5073 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 5074 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 5075 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5076 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5077 | break; |
| 5078 | |
| 5079 | // Build the default argument expression; we don't actually care |
| 5080 | // if this succeeds or not, because this routine will complain |
| 5081 | // if there was a problem. |
| 5082 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 5083 | } |
| 5084 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5085 | return CurInitExpr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5086 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5087 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5088 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5089 | // constructor call (we might have derived-to-base conversions, or |
| 5090 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5091 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5092 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5093 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5094 | // Actually perform the constructor call. |
| 5095 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5096 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5097 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5098 | /*ListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5099 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5100 | CXXConstructExpr::CK_Complete, |
| 5101 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5102 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5103 | // If we're supposed to bind temporaries, do so. |
| 5104 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5105 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5106 | return CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5107 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5108 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5109 | /// \brief Check whether elidable copy construction for binding a reference to |
| 5110 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 5111 | /// -Wc++98-compat. |
| 5112 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 5113 | const InitializedEntity &Entity, |
| 5114 | Expr *CurInitExpr) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5115 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5116 | |
| 5117 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 5118 | if (!Record) |
| 5119 | return; |
| 5120 | |
| 5121 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 5122 | if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc)) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5123 | return; |
| 5124 | |
| 5125 | // Find constructors which would have been considered. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5126 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5127 | LookupCopyAndMoveConstructors( |
| 5128 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 5129 | |
| 5130 | // Perform overload resolution. |
| 5131 | OverloadCandidateSet::iterator Best; |
| 5132 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 5133 | |
| 5134 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 5135 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 5136 | << CurInitExpr->getSourceRange(); |
| 5137 | |
| 5138 | switch (OR) { |
| 5139 | case OR_Success: |
| 5140 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 5141 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5142 | // FIXME: Check default arguments as far as that's possible. |
| 5143 | break; |
| 5144 | |
| 5145 | case OR_No_Viable_Function: |
| 5146 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5147 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5148 | break; |
| 5149 | |
| 5150 | case OR_Ambiguous: |
| 5151 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5152 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5153 | break; |
| 5154 | |
| 5155 | case OR_Deleted: |
| 5156 | S.Diag(Loc, Diag); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5157 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5158 | break; |
| 5159 | } |
| 5160 | } |
| 5161 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5162 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 5163 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5164 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5165 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 5166 | return; |
| 5167 | |
| 5168 | if (Entity.getDecl()->getDeclName()) |
| 5169 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 5170 | << Entity.getDecl()->getDeclName(); |
| 5171 | else |
| 5172 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 5173 | } |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5174 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 5175 | Entity.getMethodDecl()) |
| 5176 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 5177 | diag::note_method_return_type_change) |
| 5178 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5181 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 5182 | return s.Kind == InitializationSequence::SK_BindReference || |
| 5183 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 5184 | } |
| 5185 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5186 | /// Returns true if the parameters describe a constructor initialization of |
| 5187 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 5188 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 5189 | const InitializationKind &Kind, |
| 5190 | unsigned NumArgs) { |
| 5191 | switch (Entity.getKind()) { |
| 5192 | case InitializedEntity::EK_Temporary: |
| 5193 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5194 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5195 | break; |
| 5196 | default: |
| 5197 | return false; |
| 5198 | } |
| 5199 | |
| 5200 | switch (Kind.getKind()) { |
| 5201 | case InitializationKind::IK_DirectList: |
| 5202 | return true; |
| 5203 | // FIXME: Hack to work around cast weirdness. |
| 5204 | case InitializationKind::IK_Direct: |
| 5205 | case InitializationKind::IK_Value: |
| 5206 | return NumArgs != 1; |
| 5207 | default: |
| 5208 | return false; |
| 5209 | } |
| 5210 | } |
| 5211 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5212 | static ExprResult |
| 5213 | PerformConstructorInitialization(Sema &S, |
| 5214 | const InitializedEntity &Entity, |
| 5215 | const InitializationKind &Kind, |
| 5216 | MultiExprArg Args, |
| 5217 | const InitializationSequence::Step& Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5218 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5219 | bool IsListInitialization, |
| 5220 | SourceLocation LBraceLoc, |
| 5221 | SourceLocation RBraceLoc) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5222 | unsigned NumArgs = Args.size(); |
| 5223 | CXXConstructorDecl *Constructor |
| 5224 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 5225 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 5226 | |
| 5227 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5228 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5229 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 5230 | ? Kind.getEqualLoc() |
| 5231 | : Kind.getLocation(); |
| 5232 | |
| 5233 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 5234 | // Force even a trivial, implicit default constructor to be |
| 5235 | // semantically checked. We do this explicitly because we don't build |
| 5236 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 47ff122 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 5237 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5238 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | f704ade | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 5239 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5240 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 5241 | } |
| 5242 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5243 | ExprResult CurInit((Expr *)nullptr); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5244 | |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5245 | // C++ [over.match.copy]p1: |
| 5246 | // - When initializing a temporary to be bound to the first parameter |
| 5247 | // of a constructor that takes a reference to possibly cv-qualified |
| 5248 | // T as its first argument, called with a single argument in the |
| 5249 | // context of direct-initialization, explicit conversion functions |
| 5250 | // are also considered. |
| 5251 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 5252 | Args.size() == 1 && |
| 5253 | Constructor->isCopyOrMoveConstructor(); |
| 5254 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5255 | // Determine the arguments required to actually perform the constructor |
| 5256 | // call. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5257 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5258 | Loc, ConstructorArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5259 | AllowExplicitConv, |
| 5260 | IsListInitialization)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5261 | return ExprError(); |
| 5262 | |
| 5263 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5264 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5265 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5266 | S.MarkFunctionReferenced(Loc, Constructor); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5267 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 5268 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5269 | |
| 5270 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5271 | if (!TSInfo) |
| 5272 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5273 | SourceRange ParenOrBraceRange = |
| 5274 | (Kind.getKind() == InitializationKind::IK_DirectList) |
| 5275 | ? SourceRange(LBraceLoc, RBraceLoc) |
| 5276 | : Kind.getParenRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5277 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5278 | CurInit = new (S.Context) CXXTemporaryObjectExpr( |
| 5279 | S.Context, Constructor, TSInfo, ConstructorArgs, ParenOrBraceRange, |
| 5280 | HadMultipleCandidates, IsListInitialization, |
| 5281 | ConstructorInitRequiresZeroInit); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5282 | } else { |
| 5283 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 5284 | CXXConstructExpr::CK_Complete; |
| 5285 | |
| 5286 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5287 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 5288 | CXXConstructExpr::CK_VirtualBase : |
| 5289 | CXXConstructExpr::CK_NonVirtualBase; |
| 5290 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 5291 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 5292 | } |
| 5293 | |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 5294 | // Only get the parenthesis or brace range if it is a list initialization or |
| 5295 | // direct construction. |
| 5296 | SourceRange ParenOrBraceRange; |
| 5297 | if (IsListInitialization) |
| 5298 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 5299 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
| 5300 | ParenOrBraceRange = Kind.getParenRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5301 | |
| 5302 | // If the entity allows NRVO, mark the construction as elidable |
| 5303 | // unconditionally. |
| 5304 | if (Entity.allowsNRVO()) |
| 5305 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5306 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5307 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5308 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5309 | IsListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5310 | ConstructorInitRequiresZeroInit, |
| 5311 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 5312 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5313 | else |
| 5314 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5315 | Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5316 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5317 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5318 | IsListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5319 | ConstructorInitRequiresZeroInit, |
| 5320 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 5321 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5322 | } |
| 5323 | if (CurInit.isInvalid()) |
| 5324 | return ExprError(); |
| 5325 | |
| 5326 | // Only check access if all of that succeeded. |
| 5327 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 5328 | Step.Function.FoundDecl.getAccess()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5329 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 5330 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5331 | |
| 5332 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5333 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5334 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5335 | return CurInit; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5336 | } |
| 5337 | |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5338 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 5339 | /// longer than the current full-expression. Conservatively returns false if |
| 5340 | /// it's unclear. |
| 5341 | static bool |
| 5342 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 5343 | const InitializedEntity *Top = &Entity; |
| 5344 | while (Top->getParent()) |
| 5345 | Top = Top->getParent(); |
| 5346 | |
| 5347 | switch (Top->getKind()) { |
| 5348 | case InitializedEntity::EK_Variable: |
| 5349 | case InitializedEntity::EK_Result: |
| 5350 | case InitializedEntity::EK_Exception: |
| 5351 | case InitializedEntity::EK_Member: |
| 5352 | case InitializedEntity::EK_New: |
| 5353 | case InitializedEntity::EK_Base: |
| 5354 | case InitializedEntity::EK_Delegating: |
| 5355 | return true; |
| 5356 | |
| 5357 | case InitializedEntity::EK_ArrayElement: |
| 5358 | case InitializedEntity::EK_VectorElement: |
| 5359 | case InitializedEntity::EK_BlockElement: |
| 5360 | case InitializedEntity::EK_ComplexElement: |
| 5361 | // Could not determine what the full initialization is. Assume it might not |
| 5362 | // outlive the full-expression. |
| 5363 | return false; |
| 5364 | |
| 5365 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5366 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5367 | case InitializedEntity::EK_Temporary: |
| 5368 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5369 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5370 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5371 | // The entity being initialized might not outlive the full-expression. |
| 5372 | return false; |
| 5373 | } |
| 5374 | |
| 5375 | llvm_unreachable("unknown entity kind"); |
| 5376 | } |
| 5377 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5378 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 5379 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 5380 | /// the initialization of \p Entity. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5381 | static const InitializedEntity *getEntityForTemporaryLifetimeExtension( |
| 5382 | const InitializedEntity *Entity, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5383 | const InitializedEntity *FallbackDecl = nullptr) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5384 | // C++11 [class.temporary]p5: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5385 | switch (Entity->getKind()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5386 | case InitializedEntity::EK_Variable: |
| 5387 | // The temporary [...] persists for the lifetime of the reference |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5388 | return Entity; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5389 | |
| 5390 | case InitializedEntity::EK_Member: |
| 5391 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5392 | if (Entity->getParent()) |
| 5393 | return getEntityForTemporaryLifetimeExtension(Entity->getParent(), |
| 5394 | Entity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5395 | |
| 5396 | // except: |
| 5397 | // -- A temporary bound to a reference member in a constructor's |
| 5398 | // ctor-initializer persists until the constructor exits. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5399 | return Entity; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5400 | |
| 5401 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5402 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5403 | // -- A temporary bound to a reference parameter in a function call |
| 5404 | // persists until the completion of the full-expression containing |
| 5405 | // the call. |
| 5406 | case InitializedEntity::EK_Result: |
| 5407 | // -- The lifetime of a temporary bound to the returned value in a |
| 5408 | // function return statement is not extended; the temporary is |
| 5409 | // destroyed at the end of the full-expression in the return statement. |
| 5410 | case InitializedEntity::EK_New: |
| 5411 | // -- A temporary bound to a reference in a new-initializer persists |
| 5412 | // until the completion of the full-expression containing the |
| 5413 | // new-initializer. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5414 | return nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5415 | |
| 5416 | case InitializedEntity::EK_Temporary: |
| 5417 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5418 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5419 | // We don't yet know the storage duration of the surrounding temporary. |
| 5420 | // Assume it's got full-expression duration for now, it will patch up our |
| 5421 | // storage duration if that's not correct. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5422 | return nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5423 | |
| 5424 | case InitializedEntity::EK_ArrayElement: |
| 5425 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5426 | return getEntityForTemporaryLifetimeExtension(Entity->getParent(), |
| 5427 | FallbackDecl); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5428 | |
| 5429 | case InitializedEntity::EK_Base: |
| 5430 | case InitializedEntity::EK_Delegating: |
| 5431 | // We can reach this case for aggregate initialization in a constructor: |
| 5432 | // struct A { int &&r; }; |
| 5433 | // struct B : A { B() : A{0} {} }; |
| 5434 | // In this case, use the innermost field decl as the context. |
| 5435 | return FallbackDecl; |
| 5436 | |
| 5437 | case InitializedEntity::EK_BlockElement: |
| 5438 | case InitializedEntity::EK_LambdaCapture: |
| 5439 | case InitializedEntity::EK_Exception: |
| 5440 | case InitializedEntity::EK_VectorElement: |
| 5441 | case InitializedEntity::EK_ComplexElement: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5442 | return nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5443 | } |
Benjamin Kramer | cabc882 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 5444 | llvm_unreachable("unknown entity kind"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5445 | } |
| 5446 | |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5447 | static void performLifetimeExtension(Expr *Init, |
| 5448 | const InitializedEntity *ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5449 | |
| 5450 | /// Update a glvalue expression that is used as the initializer of a reference |
| 5451 | /// to note that its lifetime is extended. |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5452 | /// \return \c true if any temporary had its lifetime extended. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5453 | static bool |
| 5454 | performReferenceExtension(Expr *Init, |
| 5455 | const InitializedEntity *ExtendingEntity) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5456 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 5457 | if (ILE->getNumInits() == 1 && ILE->isGLValue()) { |
| 5458 | // This is just redundant braces around an initializer. Step over it. |
| 5459 | Init = ILE->getInit(0); |
| 5460 | } |
| 5461 | } |
| 5462 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5463 | // Walk past any constructs which we can lifetime-extend across. |
| 5464 | Expr *Old; |
| 5465 | do { |
| 5466 | Old = Init; |
| 5467 | |
| 5468 | // Step over any subobject adjustments; we may have a materialized |
| 5469 | // temporary inside them. |
| 5470 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5471 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5472 | Init = const_cast<Expr *>( |
| 5473 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5474 | |
| 5475 | // Per current approach for DR1376, look through casts to reference type |
| 5476 | // when performing lifetime extension. |
| 5477 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 5478 | if (CE->getSubExpr()->isGLValue()) |
| 5479 | Init = CE->getSubExpr(); |
| 5480 | |
| 5481 | // FIXME: Per DR1213, subscripting on an array temporary produces an xvalue. |
| 5482 | // It's unclear if binding a reference to that xvalue extends the array |
| 5483 | // temporary. |
| 5484 | } while (Init != Old); |
| 5485 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5486 | if (MaterializeTemporaryExpr *ME = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
| 5487 | // Update the storage duration of the materialized temporary. |
| 5488 | // FIXME: Rebuild the expression instead of mutating it. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5489 | ME->setExtendingDecl(ExtendingEntity->getDecl(), |
| 5490 | ExtendingEntity->allocateManglingNumber()); |
| 5491 | performLifetimeExtension(ME->GetTemporaryExpr(), ExtendingEntity); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5492 | return true; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5493 | } |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5494 | |
| 5495 | return false; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5496 | } |
| 5497 | |
| 5498 | /// Update a prvalue expression that is going to be materialized as a |
| 5499 | /// lifetime-extended temporary. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5500 | static void performLifetimeExtension(Expr *Init, |
| 5501 | const InitializedEntity *ExtendingEntity) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5502 | // Dig out the expression which constructs the extended temporary. |
| 5503 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5504 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5505 | Init = const_cast<Expr *>( |
| 5506 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5507 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5508 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 5509 | Init = BTE->getSubExpr(); |
| 5510 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5511 | if (CXXStdInitializerListExpr *ILE = |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5512 | dyn_cast<CXXStdInitializerListExpr>(Init)) { |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5513 | performReferenceExtension(ILE->getSubExpr(), ExtendingEntity); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5514 | return; |
| 5515 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5516 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5517 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5518 | if (ILE->getType()->isArrayType()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5519 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5520 | performLifetimeExtension(ILE->getInit(I), ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5521 | return; |
| 5522 | } |
| 5523 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5524 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5525 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 5526 | |
| 5527 | // If we lifetime-extend a braced initializer which is initializing an |
| 5528 | // aggregate, and that aggregate contains reference members which are |
| 5529 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 5530 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 5531 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5532 | performReferenceExtension(ILE->getInit(0), ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5533 | else { |
| 5534 | unsigned Index = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5535 | for (const auto *I : RD->fields()) { |
Richard Smith | 0bca59d | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 5536 | if (Index >= ILE->getNumInits()) |
| 5537 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5538 | if (I->isUnnamedBitfield()) |
| 5539 | continue; |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5540 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5541 | if (I->getType()->isReferenceType()) |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5542 | performReferenceExtension(SubInit, ExtendingEntity); |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5543 | else if (isa<InitListExpr>(SubInit) || |
| 5544 | isa<CXXStdInitializerListExpr>(SubInit)) |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5545 | // This may be either aggregate-initialization of a member or |
| 5546 | // initialization of a std::initializer_list object. Either way, |
| 5547 | // we should recursively lifetime-extend that initializer. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5548 | performLifetimeExtension(SubInit, ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5549 | ++Index; |
| 5550 | } |
| 5551 | } |
| 5552 | } |
| 5553 | } |
| 5554 | } |
| 5555 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5556 | static void warnOnLifetimeExtension(Sema &S, const InitializedEntity &Entity, |
| 5557 | const Expr *Init, bool IsInitializerList, |
| 5558 | const ValueDecl *ExtendingDecl) { |
| 5559 | // Warn if a field lifetime-extends a temporary. |
| 5560 | if (isa<FieldDecl>(ExtendingDecl)) { |
| 5561 | if (IsInitializerList) { |
| 5562 | S.Diag(Init->getExprLoc(), diag::warn_dangling_std_initializer_list) |
| 5563 | << /*at end of constructor*/true; |
| 5564 | return; |
| 5565 | } |
| 5566 | |
| 5567 | bool IsSubobjectMember = false; |
| 5568 | for (const InitializedEntity *Ent = Entity.getParent(); Ent; |
| 5569 | Ent = Ent->getParent()) { |
| 5570 | if (Ent->getKind() != InitializedEntity::EK_Base) { |
| 5571 | IsSubobjectMember = true; |
| 5572 | break; |
| 5573 | } |
| 5574 | } |
| 5575 | S.Diag(Init->getExprLoc(), |
| 5576 | diag::warn_bind_ref_member_to_temporary) |
| 5577 | << ExtendingDecl << Init->getSourceRange() |
| 5578 | << IsSubobjectMember << IsInitializerList; |
| 5579 | if (IsSubobjectMember) |
| 5580 | S.Diag(ExtendingDecl->getLocation(), |
| 5581 | diag::note_ref_subobject_of_member_declared_here); |
| 5582 | else |
| 5583 | S.Diag(ExtendingDecl->getLocation(), |
| 5584 | diag::note_ref_or_ptr_member_declared_here) |
| 5585 | << /*is pointer*/false; |
| 5586 | } |
| 5587 | } |
| 5588 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5589 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 5590 | const ImplicitConversionSequence &ICS, |
| 5591 | QualType PreNarrowingType, |
| 5592 | QualType EntityType, |
| 5593 | const Expr *PostInit); |
| 5594 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5595 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5596 | InitializationSequence::Perform(Sema &S, |
| 5597 | const InitializedEntity &Entity, |
| 5598 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5599 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5600 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5601 | if (Failed()) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5602 | Diagnose(S, Entity, Kind, Args); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5603 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5604 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5605 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5606 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5607 | // If the declaration is a non-dependent, incomplete array type |
| 5608 | // that has an initializer, then its type will be completed once |
| 5609 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5610 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5611 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5612 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5613 | if (const IncompleteArrayType *ArrayT |
| 5614 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 5615 | // FIXME: We don't currently have the ability to accurately |
| 5616 | // compute the length of an initializer list without |
| 5617 | // performing full type-checking of the initializer list |
| 5618 | // (since we have to determine where braces are implicitly |
| 5619 | // introduced and such). So, we fall back to making the array |
| 5620 | // type a dependently-sized array type with no specified |
| 5621 | // bound. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5622 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5623 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5624 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5625 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5626 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 5627 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 5628 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5629 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 5630 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 5631 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5632 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | *ResultType |
| 5636 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5637 | /*NumElts=*/nullptr, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5638 | ArrayT->getSizeModifier(), |
| 5639 | ArrayT->getIndexTypeCVRQualifiers(), |
| 5640 | Brackets); |
| 5641 | } |
| 5642 | |
| 5643 | } |
| 5644 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5645 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 5646 | !Kind.isExplicitCast()) { |
| 5647 | // Rebuild the ParenListExpr. |
| 5648 | SourceRange ParenRange = Kind.getParenRange(); |
| 5649 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5650 | Args); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5651 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5652 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | bf13895 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 5653 | Kind.isExplicitCast() || |
| 5654 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5655 | return ExprResult(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5656 | } |
| 5657 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5658 | // No steps means no initialization. |
| 5659 | if (Steps.empty()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5660 | return ExprResult((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5661 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5662 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5663 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5664 | !Entity.isParameterKind()) { |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5665 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 5666 | // from an initializer list. For parameters, we produce a better warning |
| 5667 | // elsewhere. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5668 | Expr *Init = Args[0]; |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5669 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 5670 | << Init->getSourceRange(); |
| 5671 | } |
| 5672 | |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5673 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 5674 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5675 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5676 | Entity.getType()->isPointerType() && |
| 5677 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5678 | Expr *Init = Args[0]; |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5679 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 5680 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 5681 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 5682 | << Init->getSourceRange(); |
| 5683 | } |
| 5684 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5685 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 5686 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5687 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 5688 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5689 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5690 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5691 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5692 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5693 | ExprResult CurInit((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5694 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5695 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5696 | // grab the only argument out the Args and place it into the "current" |
| 5697 | // initializer. |
| 5698 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5699 | case SK_ResolveAddressOfOverloadedFunction: |
| 5700 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5701 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5702 | case SK_CastDerivedToBaseLValue: |
| 5703 | case SK_BindReference: |
| 5704 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5705 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5706 | case SK_UserConversion: |
| 5707 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5708 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5709 | case SK_QualificationConversionRValue: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5710 | case SK_LValueToRValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5711 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5712 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5713 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5714 | case SK_UnwrapInitList: |
| 5715 | case SK_RewrapInitList: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5716 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5717 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5718 | case SK_ObjCObjectConversion: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5719 | case SK_ArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5720 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5721 | case SK_PassByIndirectCopyRestore: |
| 5722 | case SK_PassByIndirectRestore: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5723 | case SK_ProduceObjCObject: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5724 | case SK_StdInitializerList: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5725 | case SK_OCLSamplerInit: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5726 | case SK_OCLZeroEvent: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5727 | assert(Args.size() == 1); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5728 | CurInit = Args[0]; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5729 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5730 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5731 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5732 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5733 | case SK_ConstructorInitialization: |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5734 | case SK_ListConstructorCall: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5735 | case SK_ZeroInitialization: |
| 5736 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5737 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5738 | |
| 5739 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5740 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5741 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5742 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5743 | Step != StepEnd; ++Step) { |
| 5744 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5745 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5746 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5747 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5748 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5749 | switch (Step->Kind) { |
| 5750 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5751 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5752 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5753 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5754 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 5755 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5756 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5757 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5758 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5759 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5760 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5761 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5762 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5763 | case SK_CastDerivedToBaseLValue: { |
| 5764 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5765 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5766 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5767 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5768 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5769 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5770 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5771 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5772 | CurInit.get()->getLocStart(), |
| 5773 | CurInit.get()->getSourceRange(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5774 | &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5775 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5776 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5777 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 5778 | QualType T = SourceType; |
| 5779 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 5780 | T = Pointer->getPointeeType(); |
| 5781 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5782 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5783 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 5784 | } |
| 5785 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5786 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5787 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5788 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5789 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5790 | VK_XValue : |
| 5791 | VK_RValue); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5792 | CurInit = |
| 5793 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase, |
| 5794 | CurInit.get(), &BasePath, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5795 | break; |
| 5796 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5797 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5798 | case SK_BindReference: |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5799 | // References cannot bind to bit-fields (C++ [dcl.init.ref]p5). |
| 5800 | if (CurInit.get()->refersToBitField()) { |
| 5801 | // We don't necessarily have an unambiguous source bit-field. |
| 5802 | FieldDecl *BitField = CurInit.get()->getSourceBitField(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5803 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5804 | << Entity.getType().isVolatileQualified() |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5805 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5806 | << (BitField != nullptr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5807 | << CurInit.get()->getSourceRange(); |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5808 | if (BitField) |
| 5809 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 5810 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5811 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5812 | } |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5813 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5814 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | c17ae44 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5815 | // References cannot bind to vector elements. |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5816 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5817 | << Entity.getType().isVolatileQualified() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5818 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5819 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5820 | return ExprError(); |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5821 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5822 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5823 | // Reference binding does not have any corresponding ASTs. |
| 5824 | |
| 5825 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5826 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5827 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5828 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5829 | // Even though we didn't materialize a temporary, the binding may still |
| 5830 | // extend the lifetime of a temporary. This happens if we bind a reference |
| 5831 | // to the result of a cast to reference type. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5832 | if (const InitializedEntity *ExtendingEntity = |
| 5833 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 5834 | if (performReferenceExtension(CurInit.get(), ExtendingEntity)) |
| 5835 | warnOnLifetimeExtension(S, Entity, CurInit.get(), |
| 5836 | /*IsInitializerList=*/false, |
| 5837 | ExtendingEntity->getDecl()); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5838 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5839 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5840 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5841 | case SK_BindReferenceToTemporary: { |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5842 | // Make sure the "temporary" is actually an rvalue. |
| 5843 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 5844 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5845 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5846 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5847 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5848 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5849 | // Materialize the temporary into memory. |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5850 | MaterializeTemporaryExpr *MTE = new (S.Context) MaterializeTemporaryExpr( |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5851 | Entity.getType().getNonReferenceType(), CurInit.get(), |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5852 | Entity.getType()->isLValueReferenceType()); |
| 5853 | |
| 5854 | // Maybe lifetime-extend the temporary's subobjects to match the |
| 5855 | // entity's lifetime. |
| 5856 | if (const InitializedEntity *ExtendingEntity = |
| 5857 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 5858 | if (performReferenceExtension(MTE, ExtendingEntity)) |
| 5859 | warnOnLifetimeExtension(S, Entity, CurInit.get(), /*IsInitializerList=*/false, |
| 5860 | ExtendingEntity->getDecl()); |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5861 | |
| 5862 | // 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] | 5863 | // need cleanups. Likewise if we're extending this temporary to automatic |
| 5864 | // storage duration -- we need to register its cleanup during the |
| 5865 | // full-expression's cleanups. |
| 5866 | if ((S.getLangOpts().ObjCAutoRefCount && |
| 5867 | MTE->getType()->isObjCLifetimeType()) || |
| 5868 | (MTE->getStorageDuration() == SD_Automatic && |
| 5869 | MTE->getType().isDestructedType())) |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5870 | S.ExprNeedsCleanups = true; |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5871 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5872 | CurInit = MTE; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5873 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5874 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5875 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5876 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5877 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5878 | /*IsExtraneousCopy=*/true); |
| 5879 | break; |
| 5880 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5881 | case SK_UserConversion: { |
| 5882 | // We have a user-defined conversion that invokes either a constructor |
| 5883 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5884 | CastKind CastKind; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5885 | bool IsCopy = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5886 | FunctionDecl *Fn = Step->Function.Function; |
| 5887 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5888 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5889 | bool CreatedObject = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5890 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5891 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5892 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5893 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5894 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5895 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5896 | // Determine the arguments required to actually perform the constructor |
| 5897 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5898 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5899 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5900 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5901 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5902 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5903 | |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5904 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5905 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5906 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5907 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5908 | /*ListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5909 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5910 | CXXConstructExpr::CK_Complete, |
| 5911 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5912 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5913 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5914 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5915 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5916 | FoundFn.getAccess()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5917 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5918 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5919 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5920 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5921 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5922 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5923 | S.IsDerivedFrom(SourceType, Class)) |
| 5924 | IsCopy = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5925 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5926 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5927 | } else { |
| 5928 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5929 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5930 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5931 | FoundFn); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5932 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5933 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5934 | |
| 5935 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5936 | // derived-to-base conversion? I believe the answer is "no", because |
| 5937 | // 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] | 5938 | ExprResult CurInitExprRes = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5939 | S.PerformObjectArgumentInitialization(CurInit.get(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5940 | /*Qualifier=*/nullptr, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5941 | FoundFn, Conversion); |
| 5942 | if(CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5943 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5944 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5945 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5946 | // Build the actual call to the conversion function. |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5947 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 5948 | HadMultipleCandidates); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5949 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5950 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5951 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5952 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5953 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5954 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5955 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5956 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5957 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5958 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 5959 | |
| 5960 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5961 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5962 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5963 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5964 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5965 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5966 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5967 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5968 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart())) |
| 5969 | return ExprError(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5970 | } |
| 5971 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5972 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5973 | CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(), |
| 5974 | CastKind, CurInit.get(), nullptr, |
| 5975 | CurInit.get()->getValueKind()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5976 | if (MaybeBindToTemp) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5977 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5978 | if (RequiresCopy) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5979 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5980 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5981 | break; |
| 5982 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5983 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5984 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5985 | case SK_QualificationConversionXValue: |
| 5986 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5987 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5988 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5989 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5990 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5991 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5992 | VK_XValue : |
| 5993 | VK_RValue); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5994 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5995 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5996 | } |
| 5997 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5998 | case SK_LValueToRValue: { |
| 5999 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6000 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 6001 | CK_LValueToRValue, CurInit.get(), |
| 6002 | /*BasePath=*/nullptr, VK_RValue); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6003 | break; |
| 6004 | } |
| 6005 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6006 | case SK_ConversionSequence: |
| 6007 | case SK_ConversionSequenceNoNarrowing: { |
| 6008 | Sema::CheckedConversionKind CCK |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6009 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 6010 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 6011 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6012 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6013 | ExprResult CurInitExprRes = |
| 6014 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6015 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6016 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6017 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6018 | CurInit = CurInitExprRes; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6019 | |
| 6020 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
| 6021 | S.getLangOpts().CPlusPlus && !CurInit.get()->isValueDependent()) |
| 6022 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 6023 | CurInit.get()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6024 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 6025 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6026 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6027 | case SK_ListInitialization: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6028 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6029 | // If we're not initializing the top-level entity, we need to create an |
| 6030 | // InitializeTemporary entity for our target type. |
| 6031 | QualType Ty = Step->Type; |
| 6032 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6033 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 6034 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 6035 | InitListChecker PerformInitList(S, InitEntity, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 6036 | InitList, Ty, /*VerifyOnly=*/false); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6037 | if (PerformInitList.HadError()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6038 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6039 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6040 | // Hack: We must update *ResultType if available in order to set the |
| 6041 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 6042 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 6043 | if (ResultType && |
| 6044 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6045 | if ((*ResultType)->isRValueReferenceType()) |
| 6046 | Ty = S.Context.getRValueReferenceType(Ty); |
| 6047 | else if ((*ResultType)->isLValueReferenceType()) |
| 6048 | Ty = S.Context.getLValueReferenceType(Ty, |
| 6049 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 6050 | *ResultType = Ty; |
| 6051 | } |
| 6052 | |
| 6053 | InitListExpr *StructuredInitList = |
| 6054 | PerformInitList.getFullyStructuredList(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6055 | CurInit.get(); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 6056 | CurInit = shouldBindAsTemporary(InitEntity) |
| 6057 | ? S.MaybeBindToTemporary(StructuredInitList) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6058 | : StructuredInitList; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6059 | break; |
| 6060 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6061 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6062 | case SK_ListConstructorCall: { |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6063 | // When an initializer list is passed for a parameter of type "reference |
| 6064 | // to object", we don't get an EK_Temporary entity, but instead an |
| 6065 | // EK_Parameter entity with reference type. |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6066 | // FIXME: This is a hack. What we really should do is create a user |
| 6067 | // conversion step for this case, but this makes it considerably more |
| 6068 | // complicated. For now, this will do. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6069 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6070 | Entity.getType().getNonReferenceType()); |
| 6071 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6072 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6073 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 6074 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 6075 | << InitList->getSourceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6076 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6077 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 6078 | Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6079 | Kind, Arg, *Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6080 | ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6081 | /*IsListInitialization*/ true, |
| 6082 | InitList->getLBraceLoc(), |
| 6083 | InitList->getRBraceLoc()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6084 | break; |
| 6085 | } |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6086 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6087 | case SK_UnwrapInitList: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6088 | CurInit = cast<InitListExpr>(CurInit.get())->getInit(0); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6089 | break; |
| 6090 | |
| 6091 | case SK_RewrapInitList: { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6092 | Expr *E = CurInit.get(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6093 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 6094 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 6095 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6096 | ILE->setSyntacticForm(Syntactic); |
| 6097 | ILE->setType(E->getType()); |
| 6098 | ILE->setValueKind(E->getValueKind()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6099 | CurInit = ILE; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6100 | break; |
| 6101 | } |
| 6102 | |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6103 | case SK_ConstructorInitialization: { |
| 6104 | // When an initializer list is passed for a parameter of type "reference |
| 6105 | // to object", we don't get an EK_Temporary entity, but instead an |
| 6106 | // EK_Parameter entity with reference type. |
| 6107 | // FIXME: This is a hack. What we really should do is create a user |
| 6108 | // conversion step for this case, but this makes it considerably more |
| 6109 | // complicated. For now, this will do. |
| 6110 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6111 | Entity.getType().getNonReferenceType()); |
| 6112 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 6113 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity |
| 6114 | : Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6115 | Kind, Args, *Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6116 | ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6117 | /*IsListInitialization*/ false, |
| 6118 | /*LBraceLoc*/ SourceLocation(), |
| 6119 | /*RBraceLoc*/ SourceLocation()); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6120 | break; |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6121 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6122 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6123 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6124 | step_iterator NextStep = Step; |
| 6125 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6126 | if (NextStep != StepEnd && |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6127 | (NextStep->Kind == SK_ConstructorInitialization || |
| 6128 | NextStep->Kind == SK_ListConstructorCall)) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6129 | // The need for zero-initialization is recorded directly into |
| 6130 | // the call to the object's constructor within the next step. |
| 6131 | ConstructorInitRequiresZeroInit = true; |
| 6132 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6133 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6134 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6135 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6136 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6137 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6138 | Kind.getRange().getBegin()); |
| 6139 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6140 | CurInit = new (S.Context) CXXScalarValueInitExpr( |
| 6141 | TSInfo->getType().getNonLValueExprType(S.Context), TSInfo, |
| 6142 | Kind.getRange().getEnd()); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6143 | } else { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6144 | CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6145 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6146 | break; |
| 6147 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6148 | |
| 6149 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6150 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6151 | ExprResult Result = CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6152 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 6153 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 6154 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6155 | if (Result.isInvalid()) |
| 6156 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6157 | CurInit = Result; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6158 | |
| 6159 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6160 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6161 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6162 | Entity.isParameterKind() && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6163 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6164 | == Sema::Compatible) |
| 6165 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6166 | if (CurInitExprRes.isInvalid()) |
| 6167 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6168 | CurInit = CurInitExprRes; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6169 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6170 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6171 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 6172 | Step->Type, SourceType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6173 | CurInit.get(), |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 6174 | getAssignmentAction(Entity, true), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6175 | &Complained)) { |
| 6176 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6177 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6178 | } else if (Complained) |
| 6179 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6180 | break; |
| 6181 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6182 | |
| 6183 | case SK_StringInit: { |
| 6184 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6185 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 6186 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6187 | break; |
| 6188 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6189 | |
| 6190 | case SK_ObjCObjectConversion: |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6191 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6192 | CK_ObjCObjectLValueCast, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 6193 | CurInit.get()->getValueKind()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6194 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6195 | |
| 6196 | case SK_ArrayInit: |
| 6197 | // Okay: we checked everything before creating this step. Note that |
| 6198 | // this is a GNU extension. |
| 6199 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6200 | << Step->Type << CurInit.get()->getType() |
| 6201 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6202 | |
| 6203 | // If the destination type is an incomplete array type, update the |
| 6204 | // type accordingly. |
| 6205 | if (ResultType) { |
| 6206 | if (const IncompleteArrayType *IncompleteDest |
| 6207 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 6208 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6209 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6210 | *ResultType = S.Context.getConstantArrayType( |
| 6211 | IncompleteDest->getElementType(), |
| 6212 | ConstantSource->getSize(), |
| 6213 | ArrayType::Normal, 0); |
| 6214 | } |
| 6215 | } |
| 6216 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6217 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6218 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6219 | case SK_ParenthesizedArrayInit: |
| 6220 | // Okay: we checked everything before creating this step. Note that |
| 6221 | // this is a GNU extension. |
| 6222 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 6223 | << CurInit.get()->getSourceRange(); |
| 6224 | break; |
| 6225 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6226 | case SK_PassByIndirectCopyRestore: |
| 6227 | case SK_PassByIndirectRestore: |
| 6228 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6229 | CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr( |
| 6230 | CurInit.get(), Step->Type, |
| 6231 | Step->Kind == SK_PassByIndirectCopyRestore); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6232 | break; |
| 6233 | |
| 6234 | case SK_ProduceObjCObject: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6235 | CurInit = |
| 6236 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject, |
| 6237 | CurInit.get(), nullptr, VK_RValue); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6238 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6239 | |
| 6240 | case SK_StdInitializerList: { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6241 | S.Diag(CurInit.get()->getExprLoc(), |
| 6242 | diag::warn_cxx98_compat_initializer_list_init) |
| 6243 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 249dee5 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 6244 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6245 | // Materialize the temporary into memory. |
| 6246 | MaterializeTemporaryExpr *MTE = new (S.Context) |
| 6247 | MaterializeTemporaryExpr(CurInit.get()->getType(), CurInit.get(), |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6248 | /*BoundToLvalueReference=*/false); |
| 6249 | |
| 6250 | // Maybe lifetime-extend the array temporary's subobjects to match the |
| 6251 | // entity's lifetime. |
| 6252 | if (const InitializedEntity *ExtendingEntity = |
| 6253 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 6254 | if (performReferenceExtension(MTE, ExtendingEntity)) |
| 6255 | warnOnLifetimeExtension(S, Entity, CurInit.get(), |
| 6256 | /*IsInitializerList=*/true, |
| 6257 | ExtendingEntity->getDecl()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6258 | |
| 6259 | // Wrap it in a construction of a std::initializer_list<T>. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6260 | CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6261 | |
| 6262 | // Bind the result, in case the library has given initializer_list a |
| 6263 | // non-trivial destructor. |
| 6264 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6265 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6266 | break; |
| 6267 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6268 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6269 | case SK_OCLSamplerInit: { |
| 6270 | assert(Step->Type->isSamplerT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 6271 | "Sampler initialization on non-sampler type."); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6272 | |
| 6273 | QualType SourceType = CurInit.get()->getType(); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6274 | |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6275 | if (Entity.isParameterKind()) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6276 | if (!SourceType->isSamplerT()) |
| 6277 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 6278 | << SourceType; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6279 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6280 | llvm_unreachable("Invalid EntityKind!"); |
| 6281 | } |
| 6282 | |
| 6283 | break; |
| 6284 | } |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6285 | case SK_OCLZeroEvent: { |
| 6286 | assert(Step->Type->isEventT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 6287 | "Event initialization on non-event type."); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6288 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6289 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6290 | CK_ZeroToOCLEvent, |
| 6291 | CurInit.get()->getValueKind()); |
| 6292 | break; |
| 6293 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6294 | } |
| 6295 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6296 | |
| 6297 | // Diagnose non-fatal problems with the completed initialization. |
| 6298 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6299 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 6300 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 6301 | cast<FieldDecl>(Entity.getDecl()), |
| 6302 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6303 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6304 | return CurInit; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6305 | } |
| 6306 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6307 | /// Somewhere within T there is an uninitialized reference subobject. |
| 6308 | /// Dig it out and diagnose it. |
Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 6309 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 6310 | QualType T) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6311 | if (T->isReferenceType()) { |
| 6312 | S.Diag(Loc, diag::err_reference_without_init) |
| 6313 | << T.getNonReferenceType(); |
| 6314 | return true; |
| 6315 | } |
| 6316 | |
| 6317 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6318 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 6319 | return false; |
| 6320 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6321 | for (const auto *FI : RD->fields()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6322 | if (FI->isUnnamedBitfield()) |
| 6323 | continue; |
| 6324 | |
| 6325 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 6326 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6327 | return true; |
| 6328 | } |
| 6329 | } |
| 6330 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6331 | for (const auto &BI : RD->bases()) { |
| 6332 | if (DiagnoseUninitializedReference(S, BI.getLocStart(), BI.getType())) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6333 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6334 | return true; |
| 6335 | } |
| 6336 | } |
| 6337 | |
| 6338 | return false; |
| 6339 | } |
| 6340 | |
| 6341 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6342 | //===----------------------------------------------------------------------===// |
| 6343 | // Diagnose initialization failures |
| 6344 | //===----------------------------------------------------------------------===// |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6345 | |
| 6346 | /// Emit notes associated with an initialization that failed due to a |
| 6347 | /// "simple" conversion failure. |
| 6348 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 6349 | Expr *op) { |
| 6350 | QualType destType = entity.getType(); |
| 6351 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 6352 | op->getType()->isObjCObjectPointerType()) { |
| 6353 | |
| 6354 | // Emit a possible note about the conversion failing because the |
| 6355 | // operand is a message send with a related result type. |
| 6356 | S.EmitRelatedResultTypeNote(op); |
| 6357 | |
| 6358 | // Emit a possible note about a return failing because we're |
| 6359 | // expecting a related result type. |
| 6360 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 6361 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 6362 | } |
| 6363 | } |
| 6364 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6365 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 6366 | InitListExpr *InitList) { |
| 6367 | QualType DestType = Entity.getType(); |
| 6368 | |
| 6369 | QualType E; |
| 6370 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 6371 | QualType ArrayType = S.Context.getConstantArrayType( |
| 6372 | E.withConst(), |
| 6373 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 6374 | InitList->getNumInits()), |
| 6375 | clang::ArrayType::Normal, 0); |
| 6376 | InitializedEntity HiddenArray = |
| 6377 | InitializedEntity::InitializeTemporary(ArrayType); |
| 6378 | return diagnoseListInit(S, HiddenArray, InitList); |
| 6379 | } |
| 6380 | |
| 6381 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
| 6382 | /*VerifyOnly=*/false); |
| 6383 | assert(DiagnoseInitList.HadError() && |
| 6384 | "Inconsistent init list check result."); |
| 6385 | } |
| 6386 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6387 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6388 | const InitializedEntity &Entity, |
| 6389 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6390 | ArrayRef<Expr *> Args) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 6391 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6392 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6393 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 6394 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6395 | switch (Failure) { |
| 6396 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6397 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6398 | if (Args.empty()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6399 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 6400 | // If this is value-initialization, this could be nested some way within |
| 6401 | // the target type. |
| 6402 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 6403 | DestType->isReferenceType()); |
| 6404 | bool Diagnosed = |
| 6405 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 6406 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 6407 | (void)Diagnosed; |
| 6408 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6409 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6410 | << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6411 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6412 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6413 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6414 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6415 | break; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6416 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6417 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 6418 | break; |
| 6419 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6420 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 6421 | break; |
| 6422 | case FK_NarrowStringIntoWideCharArray: |
| 6423 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 6424 | break; |
| 6425 | case FK_WideStringIntoCharArray: |
| 6426 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 6427 | break; |
| 6428 | case FK_IncompatWideStringIntoWideChar: |
| 6429 | S.Diag(Kind.getLocation(), |
| 6430 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 6431 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6432 | case FK_ArrayTypeMismatch: |
| 6433 | case FK_NonConstantArrayInit: |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6434 | S.Diag(Kind.getLocation(), |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6435 | (Failure == FK_ArrayTypeMismatch |
| 6436 | ? diag::err_array_init_different_type |
| 6437 | : diag::err_array_init_non_constant_array)) |
| 6438 | << DestType.getNonReferenceType() |
| 6439 | << Args[0]->getType() |
| 6440 | << Args[0]->getSourceRange(); |
| 6441 | break; |
| 6442 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6443 | case FK_VariableLengthArrayHasInitializer: |
| 6444 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 6445 | << Args[0]->getSourceRange(); |
| 6446 | break; |
| 6447 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6448 | case FK_AddressOfOverloadFailed: { |
| 6449 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6450 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6451 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6452 | true, |
| 6453 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6454 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6455 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6456 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6457 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 6458 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6459 | switch (FailedOverloadResult) { |
| 6460 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6461 | if (Failure == FK_UserConversionOverloadFailed) |
| 6462 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 6463 | << Args[0]->getType() << DestType |
| 6464 | << Args[0]->getSourceRange(); |
| 6465 | else |
| 6466 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 6467 | << DestType << Args[0]->getType() |
| 6468 | << Args[0]->getSourceRange(); |
| 6469 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6470 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6471 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6472 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6473 | case OR_No_Viable_Function: |
Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 6474 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 6475 | DestType.getNonReferenceType(), |
| 6476 | diag::err_typecheck_nonviable_condition_incomplete, |
| 6477 | Args[0]->getType(), Args[0]->getSourceRange())) |
| 6478 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 6479 | << Args[0]->getType() << Args[0]->getSourceRange() |
| 6480 | << DestType.getNonReferenceType(); |
| 6481 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6482 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6483 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6484 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6485 | case OR_Deleted: { |
| 6486 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 6487 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 6488 | << Args[0]->getSourceRange(); |
| 6489 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6490 | OverloadingResult Ovl |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6491 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 6492 | true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6493 | if (Ovl == OR_Deleted) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6494 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6495 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6496 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6497 | } |
| 6498 | break; |
| 6499 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6500 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6501 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6502 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6503 | } |
| 6504 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6505 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6506 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6507 | if (isa<InitListExpr>(Args[0])) { |
| 6508 | S.Diag(Kind.getLocation(), |
| 6509 | diag::err_lvalue_reference_bind_to_initlist) |
| 6510 | << DestType.getNonReferenceType().isVolatileQualified() |
| 6511 | << DestType.getNonReferenceType() |
| 6512 | << Args[0]->getSourceRange(); |
| 6513 | break; |
| 6514 | } |
| 6515 | // Intentional fallthrough |
| 6516 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6517 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6518 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6519 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 6520 | ? diag::err_lvalue_reference_bind_to_temporary |
| 6521 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 6522 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6523 | << DestType.getNonReferenceType() |
| 6524 | << Args[0]->getType() |
| 6525 | << Args[0]->getSourceRange(); |
| 6526 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6527 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6528 | case FK_RValueReferenceBindingToLValue: |
| 6529 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | bed28f7 | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 6530 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6531 | << Args[0]->getSourceRange(); |
| 6532 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6533 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6534 | case FK_ReferenceInitDropsQualifiers: |
| 6535 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 6536 | << DestType.getNonReferenceType() |
| 6537 | << Args[0]->getType() |
| 6538 | << Args[0]->getSourceRange(); |
| 6539 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6540 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6541 | case FK_ReferenceInitFailed: |
| 6542 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 6543 | << DestType.getNonReferenceType() |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6544 | << Args[0]->isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6545 | << Args[0]->getType() |
| 6546 | << Args[0]->getSourceRange(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6547 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6548 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6549 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6550 | case FK_ConversionFailed: { |
| 6551 | QualType FromType = Args[0]->getType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6552 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6553 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6554 | << DestType |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6555 | << Args[0]->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6556 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6557 | << Args[0]->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6558 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 6559 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6560 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6561 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6562 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6563 | |
| 6564 | case FK_ConversionFromPropertyFailed: |
| 6565 | // No-op. This error has already been reported. |
| 6566 | break; |
| 6567 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6568 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6569 | SourceRange R; |
| 6570 | |
| 6571 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6572 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6573 | InitList->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6574 | else |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6575 | R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6576 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 6577 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6578 | if (Kind.isCStyleOrFunctionalCast()) |
| 6579 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 6580 | << R; |
| 6581 | else |
| 6582 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 6583 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6584 | break; |
| 6585 | } |
| 6586 | |
| 6587 | case FK_ReferenceBindingToInitList: |
| 6588 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 6589 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 6590 | break; |
| 6591 | |
| 6592 | case FK_InitListBadDestinationType: |
| 6593 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 6594 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 6595 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6596 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6597 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6598 | case FK_ConstructorOverloadFailed: { |
| 6599 | SourceRange ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6600 | if (Args.size()) |
| 6601 | ArgsRange = SourceRange(Args.front()->getLocStart(), |
| 6602 | Args.back()->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6603 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6604 | if (Failure == FK_ListConstructorOverloadFailed) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6605 | assert(Args.size() == 1 && "List construction from other than 1 argument."); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6606 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6607 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6608 | } |
| 6609 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6610 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 6611 | // bad. |
| 6612 | switch (FailedOverloadResult) { |
| 6613 | case OR_Ambiguous: |
| 6614 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 6615 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6616 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6617 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6618 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6619 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6620 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 6621 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 6622 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 6623 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6624 | // This is implicit default initialization of a member or |
| 6625 | // base within a constructor. If no viable function was |
| 6626 | // found, notify the user that she needs to explicitly |
| 6627 | // initialize this base/member. |
| 6628 | CXXConstructorDecl *Constructor |
| 6629 | = cast<CXXConstructorDecl>(S.CurContext); |
| 6630 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6631 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6632 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6633 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6634 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6635 | << /*base=*/0 |
| 6636 | << Entity.getType(); |
| 6637 | |
| 6638 | RecordDecl *BaseDecl |
| 6639 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 6640 | ->getDecl(); |
| 6641 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 6642 | << S.Context.getTagDeclType(BaseDecl); |
| 6643 | } else { |
| 6644 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6645 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6646 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6647 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6648 | << /*member=*/1 |
| 6649 | << Entity.getName(); |
Alp Toker | 2afa878 | 2014-05-28 12:20:14 +0000 | [diff] [blame] | 6650 | S.Diag(Entity.getDecl()->getLocation(), |
| 6651 | diag::note_member_declared_at); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6652 | |
| 6653 | if (const RecordType *Record |
| 6654 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6655 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6656 | diag::note_previous_decl) |
| 6657 | << S.Context.getTagDeclType(Record->getDecl()); |
| 6658 | } |
| 6659 | break; |
| 6660 | } |
| 6661 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6662 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 6663 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6664 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6665 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6666 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6667 | case OR_Deleted: { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6668 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6669 | OverloadingResult Ovl |
| 6670 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6671 | if (Ovl != OR_Deleted) { |
| 6672 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6673 | << true << DestType << ArgsRange; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6674 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6675 | break; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6676 | } |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6677 | |
| 6678 | // If this is a defaulted or implicitly-declared function, then |
| 6679 | // it was implicitly deleted. Make it clear that the deletion was |
| 6680 | // implicit. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6681 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6682 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6683 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6684 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6685 | else |
| 6686 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6687 | << true << DestType << ArgsRange; |
| 6688 | |
| 6689 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6690 | break; |
| 6691 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6692 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6693 | case OR_Success: |
| 6694 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6695 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6696 | } |
David Blaikie | 60deeee | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 6697 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6698 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6699 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6700 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6701 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6702 | // This is implicit default-initialization of a const member in |
| 6703 | // a constructor. Complain that it needs to be explicitly |
| 6704 | // initialized. |
| 6705 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 6706 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6707 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6708 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6709 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6710 | << /*const=*/1 |
| 6711 | << Entity.getName(); |
| 6712 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 6713 | << Entity.getName(); |
| 6714 | } else { |
| 6715 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 6716 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 6717 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6718 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6719 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6720 | case FK_Incomplete: |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 6721 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6722 | diag::err_init_incomplete_type); |
| 6723 | break; |
| 6724 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6725 | case FK_ListInitializationFailed: { |
| 6726 | // Run the init list checker again to emit diagnostics. |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6727 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 6728 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6729 | break; |
| 6730 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6731 | |
| 6732 | case FK_PlaceholderType: { |
| 6733 | // FIXME: Already diagnosed! |
| 6734 | break; |
| 6735 | } |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6736 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6737 | case FK_ExplicitConstructor: { |
| 6738 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 6739 | << Args[0]->getSourceRange(); |
| 6740 | OverloadCandidateSet::iterator Best; |
| 6741 | OverloadingResult Ovl |
| 6742 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | 5dcce09 | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 6743 | (void)Ovl; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6744 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 6745 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 6746 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 6747 | break; |
| 6748 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6749 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6750 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6751 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6752 | return true; |
| 6753 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6754 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6755 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6756 | switch (SequenceKind) { |
| 6757 | case FailedSequence: { |
| 6758 | OS << "Failed sequence: "; |
| 6759 | switch (Failure) { |
| 6760 | case FK_TooManyInitsForReference: |
| 6761 | OS << "too many initializers for reference"; |
| 6762 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6763 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6764 | case FK_ArrayNeedsInitList: |
| 6765 | OS << "array requires initializer list"; |
| 6766 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6767 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6768 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6769 | OS << "array requires initializer list or string literal"; |
| 6770 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6771 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6772 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6773 | OS << "array requires initializer list or wide string literal"; |
| 6774 | break; |
| 6775 | |
| 6776 | case FK_NarrowStringIntoWideCharArray: |
| 6777 | OS << "narrow string into wide char array"; |
| 6778 | break; |
| 6779 | |
| 6780 | case FK_WideStringIntoCharArray: |
| 6781 | OS << "wide string into char array"; |
| 6782 | break; |
| 6783 | |
| 6784 | case FK_IncompatWideStringIntoWideChar: |
| 6785 | OS << "incompatible wide string into wide char array"; |
| 6786 | break; |
| 6787 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6788 | case FK_ArrayTypeMismatch: |
| 6789 | OS << "array type mismatch"; |
| 6790 | break; |
| 6791 | |
| 6792 | case FK_NonConstantArrayInit: |
| 6793 | OS << "non-constant array initializer"; |
| 6794 | break; |
| 6795 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6796 | case FK_AddressOfOverloadFailed: |
| 6797 | OS << "address of overloaded function failed"; |
| 6798 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6799 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6800 | case FK_ReferenceInitOverloadFailed: |
| 6801 | OS << "overload resolution for reference initialization failed"; |
| 6802 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6803 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6804 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6805 | OS << "non-const lvalue reference bound to temporary"; |
| 6806 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6807 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6808 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6809 | OS << "non-const lvalue reference bound to unrelated type"; |
| 6810 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6811 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6812 | case FK_RValueReferenceBindingToLValue: |
| 6813 | OS << "rvalue reference bound to an lvalue"; |
| 6814 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6815 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6816 | case FK_ReferenceInitDropsQualifiers: |
| 6817 | OS << "reference initialization drops qualifiers"; |
| 6818 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6819 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6820 | case FK_ReferenceInitFailed: |
| 6821 | OS << "reference initialization failed"; |
| 6822 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6823 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6824 | case FK_ConversionFailed: |
| 6825 | OS << "conversion failed"; |
| 6826 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6827 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6828 | case FK_ConversionFromPropertyFailed: |
| 6829 | OS << "conversion from property failed"; |
| 6830 | break; |
| 6831 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6832 | case FK_TooManyInitsForScalar: |
| 6833 | OS << "too many initializers for scalar"; |
| 6834 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6835 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6836 | case FK_ReferenceBindingToInitList: |
| 6837 | OS << "referencing binding to initializer list"; |
| 6838 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6839 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6840 | case FK_InitListBadDestinationType: |
| 6841 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 6842 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6843 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6844 | case FK_UserConversionOverloadFailed: |
| 6845 | OS << "overloading failed for user-defined conversion"; |
| 6846 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6847 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6848 | case FK_ConstructorOverloadFailed: |
| 6849 | OS << "constructor overloading failed"; |
| 6850 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6851 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6852 | case FK_DefaultInitOfConst: |
| 6853 | OS << "default initialization of a const variable"; |
| 6854 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6855 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 6856 | case FK_Incomplete: |
| 6857 | OS << "initialization of incomplete type"; |
| 6858 | break; |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6859 | |
| 6860 | case FK_ListInitializationFailed: |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6861 | OS << "list initialization checker failure"; |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6862 | break; |
| 6863 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6864 | case FK_VariableLengthArrayHasInitializer: |
| 6865 | OS << "variable length array has an initializer"; |
| 6866 | break; |
| 6867 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6868 | case FK_PlaceholderType: |
| 6869 | OS << "initializer expression isn't contextually valid"; |
| 6870 | break; |
Nick Lewycky | 097f47c | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 6871 | |
| 6872 | case FK_ListConstructorOverloadFailed: |
| 6873 | OS << "list constructor overloading failed"; |
| 6874 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6875 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6876 | case FK_ExplicitConstructor: |
| 6877 | OS << "list copy initialization chose explicit constructor"; |
| 6878 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6879 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6880 | OS << '\n'; |
| 6881 | return; |
| 6882 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6883 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6884 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6885 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6886 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6887 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6888 | case NormalSequence: |
| 6889 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6890 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6891 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6892 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6893 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 6894 | if (S != step_begin()) { |
| 6895 | OS << " -> "; |
| 6896 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6897 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6898 | switch (S->Kind) { |
| 6899 | case SK_ResolveAddressOfOverloadedFunction: |
| 6900 | OS << "resolve address of overloaded function"; |
| 6901 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6902 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6903 | case SK_CastDerivedToBaseRValue: |
| 6904 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 6905 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6906 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6907 | case SK_CastDerivedToBaseXValue: |
| 6908 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 6909 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6910 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6911 | case SK_CastDerivedToBaseLValue: |
| 6912 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 6913 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6914 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6915 | case SK_BindReference: |
| 6916 | OS << "bind reference to lvalue"; |
| 6917 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6918 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6919 | case SK_BindReferenceToTemporary: |
| 6920 | OS << "bind reference to a temporary"; |
| 6921 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6922 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6923 | case SK_ExtraneousCopyToTemporary: |
| 6924 | OS << "extraneous C++03 copy to temporary"; |
| 6925 | break; |
| 6926 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6927 | case SK_UserConversion: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6928 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6929 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6930 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6931 | case SK_QualificationConversionRValue: |
| 6932 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6933 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6934 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6935 | case SK_QualificationConversionXValue: |
| 6936 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6937 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6938 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6939 | case SK_QualificationConversionLValue: |
| 6940 | OS << "qualification conversion (lvalue)"; |
| 6941 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6942 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6943 | case SK_LValueToRValue: |
| 6944 | OS << "load (lvalue to rvalue)"; |
| 6945 | break; |
| 6946 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6947 | case SK_ConversionSequence: |
| 6948 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 6949 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6950 | OS << ")"; |
| 6951 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6952 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6953 | case SK_ConversionSequenceNoNarrowing: |
| 6954 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 6955 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6956 | OS << ")"; |
| 6957 | break; |
| 6958 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6959 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6960 | OS << "list aggregate initialization"; |
| 6961 | break; |
| 6962 | |
| 6963 | case SK_ListConstructorCall: |
| 6964 | OS << "list initialization via constructor"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6965 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6966 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6967 | case SK_UnwrapInitList: |
| 6968 | OS << "unwrap reference initializer list"; |
| 6969 | break; |
| 6970 | |
| 6971 | case SK_RewrapInitList: |
| 6972 | OS << "rewrap reference initializer list"; |
| 6973 | break; |
| 6974 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6975 | case SK_ConstructorInitialization: |
| 6976 | OS << "constructor initialization"; |
| 6977 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6978 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6979 | case SK_ZeroInitialization: |
| 6980 | OS << "zero initialization"; |
| 6981 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6982 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6983 | case SK_CAssignment: |
| 6984 | OS << "C assignment"; |
| 6985 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6986 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6987 | case SK_StringInit: |
| 6988 | OS << "string initialization"; |
| 6989 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6990 | |
| 6991 | case SK_ObjCObjectConversion: |
| 6992 | OS << "Objective-C object conversion"; |
| 6993 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6994 | |
| 6995 | case SK_ArrayInit: |
| 6996 | OS << "array initialization"; |
| 6997 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6998 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6999 | case SK_ParenthesizedArrayInit: |
| 7000 | OS << "parenthesized array initialization"; |
| 7001 | break; |
| 7002 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7003 | case SK_PassByIndirectCopyRestore: |
| 7004 | OS << "pass by indirect copy and restore"; |
| 7005 | break; |
| 7006 | |
| 7007 | case SK_PassByIndirectRestore: |
| 7008 | OS << "pass by indirect restore"; |
| 7009 | break; |
| 7010 | |
| 7011 | case SK_ProduceObjCObject: |
| 7012 | OS << "Objective-C object retension"; |
| 7013 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7014 | |
| 7015 | case SK_StdInitializerList: |
| 7016 | OS << "std::initializer_list from initializer list"; |
| 7017 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7018 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7019 | case SK_OCLSamplerInit: |
| 7020 | OS << "OpenCL sampler_t from integer constant"; |
| 7021 | break; |
| 7022 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7023 | case SK_OCLZeroEvent: |
| 7024 | OS << "OpenCL event_t from zero"; |
| 7025 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7026 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 7027 | |
| 7028 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7029 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 7030 | |
| 7031 | OS << '\n'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7032 | } |
| 7033 | |
| 7034 | void InitializationSequence::dump() const { |
| 7035 | dump(llvm::errs()); |
| 7036 | } |
| 7037 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7038 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 7039 | const ImplicitConversionSequence &ICS, |
| 7040 | QualType PreNarrowingType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7041 | QualType EntityType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7042 | const Expr *PostInit) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7043 | const StandardConversionSequence *SCS = nullptr; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7044 | switch (ICS.getKind()) { |
| 7045 | case ImplicitConversionSequence::StandardConversion: |
| 7046 | SCS = &ICS.Standard; |
| 7047 | break; |
| 7048 | case ImplicitConversionSequence::UserDefinedConversion: |
| 7049 | SCS = &ICS.UserDefined.After; |
| 7050 | break; |
| 7051 | case ImplicitConversionSequence::AmbiguousConversion: |
| 7052 | case ImplicitConversionSequence::EllipsisConversion: |
| 7053 | case ImplicitConversionSequence::BadConversion: |
| 7054 | return; |
| 7055 | } |
| 7056 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7057 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 7058 | APValue ConstantValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 7059 | QualType ConstantType; |
| 7060 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 7061 | ConstantType)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7062 | case NK_Not_Narrowing: |
| 7063 | // No narrowing occurred. |
| 7064 | return; |
| 7065 | |
| 7066 | case NK_Type_Narrowing: |
| 7067 | // This was a floating-to-integer conversion, which is always considered a |
| 7068 | // narrowing conversion even if the value is a constant and can be |
| 7069 | // represented exactly as an integer. |
| 7070 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7071 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7072 | ? diag::warn_init_list_type_narrowing |
| 7073 | : diag::ext_init_list_type_narrowing) |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7074 | << PostInit->getSourceRange() |
| 7075 | << PreNarrowingType.getLocalUnqualifiedType() |
| 7076 | << EntityType.getLocalUnqualifiedType(); |
| 7077 | break; |
| 7078 | |
| 7079 | case NK_Constant_Narrowing: |
| 7080 | // A constant value was narrowed. |
| 7081 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7082 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7083 | ? diag::warn_init_list_constant_narrowing |
| 7084 | : diag::ext_init_list_constant_narrowing) |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7085 | << PostInit->getSourceRange() |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 7086 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 7423138 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 7087 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7088 | break; |
| 7089 | |
| 7090 | case NK_Variable_Narrowing: |
| 7091 | // A variable's value may have been narrowed. |
| 7092 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7093 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7094 | ? diag::warn_init_list_variable_narrowing |
| 7095 | : diag::ext_init_list_variable_narrowing) |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7096 | << PostInit->getSourceRange() |
| 7097 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 7423138 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 7098 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7099 | break; |
| 7100 | } |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7101 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 7102 | SmallString<128> StaticCast; |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7103 | llvm::raw_svector_ostream OS(StaticCast); |
| 7104 | OS << "static_cast<"; |
| 7105 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 7106 | // It's important to use the typedef's name if there is one so that the |
| 7107 | // fixit doesn't break code using types like int64_t. |
| 7108 | // |
| 7109 | // FIXME: This will break if the typedef requires qualification. But |
| 7110 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 7111 | OS << *TT->getDecl(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7112 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7113 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7114 | else { |
| 7115 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 7116 | // with a broken cast. |
| 7117 | return; |
| 7118 | } |
| 7119 | OS << ">("; |
Alp Toker | b086903 | 2014-05-17 01:13:18 +0000 | [diff] [blame] | 7120 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_silence) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 7121 | << PostInit->getSourceRange() |
| 7122 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
| 7123 | << FixItHint::CreateInsertion( |
| 7124 | S.getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7125 | } |
| 7126 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7127 | //===----------------------------------------------------------------------===// |
| 7128 | // Initialization helper functions |
| 7129 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7130 | bool |
| 7131 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 7132 | ExprResult Init) { |
| 7133 | if (Init.isInvalid()) |
| 7134 | return false; |
| 7135 | |
| 7136 | Expr *InitE = Init.get(); |
| 7137 | assert(InitE && "No initialization expression"); |
| 7138 | |
Douglas Gregor | f4cc61d | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 7139 | InitializationKind Kind |
| 7140 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7141 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 7142 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7143 | } |
| 7144 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7145 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7146 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 7147 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7148 | ExprResult Init, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7149 | bool TopLevelOfInitList, |
| 7150 | bool AllowExplicit) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7151 | if (Init.isInvalid()) |
| 7152 | return ExprError(); |
| 7153 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7154 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7155 | assert(InitE && "No initialization expression?"); |
| 7156 | |
| 7157 | if (EqualLoc.isInvalid()) |
| 7158 | EqualLoc = InitE->getLocStart(); |
| 7159 | |
| 7160 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7161 | EqualLoc, |
| 7162 | AllowExplicit); |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7163 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7164 | Init.get(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7165 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7166 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7167 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7168 | return Result; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7169 | } |