Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 11 | // |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Initialization.h" |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Designator.h" |
| 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/SemaInternal.h" |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APInt.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 28 | #include <map> |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 35 | /// \brief Check whether T is compatible with a wide character type (wchar_t, |
| 36 | /// char16_t or char32_t). |
| 37 | static bool IsWideCharCompatible(QualType T, ASTContext &Context) { |
| 38 | if (Context.typesAreCompatible(Context.getWideCharType(), T)) |
| 39 | return true; |
| 40 | if (Context.getLangOpts().CPlusPlus || Context.getLangOpts().C11) { |
| 41 | return Context.typesAreCompatible(Context.Char16Ty, T) || |
| 42 | Context.typesAreCompatible(Context.Char32Ty, T); |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | enum StringInitFailureKind { |
| 48 | SIF_None, |
| 49 | SIF_NarrowStringIntoWideChar, |
| 50 | SIF_WideStringIntoChar, |
| 51 | SIF_IncompatWideStringIntoWideChar, |
| 52 | SIF_Other |
| 53 | }; |
| 54 | |
| 55 | /// \brief Check whether the array of type AT can be initialized by the Init |
| 56 | /// expression by means of string initialization. Returns SIF_None if so, |
| 57 | /// otherwise returns a StringInitFailureKind that describes why the |
| 58 | /// initialization would not work. |
| 59 | static StringInitFailureKind IsStringInit(Expr *Init, const ArrayType *AT, |
| 60 | ASTContext &Context) { |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 61 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 62 | return SIF_Other; |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 63 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 64 | // See if this is a string literal or @encode. |
| 65 | Init = Init->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 67 | // Handle @encode, which is a narrow string. |
| 68 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 69 | return SIF_None; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 70 | |
| 71 | // Otherwise we can only handle string literals. |
| 72 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 73 | if (!SL) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 74 | return SIF_Other; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 75 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 76 | const QualType ElemTy = |
| 77 | Context.getCanonicalType(AT->getElementType()).getUnqualifiedType(); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 78 | |
| 79 | switch (SL->getKind()) { |
| 80 | case StringLiteral::Ascii: |
| 81 | case StringLiteral::UTF8: |
| 82 | // char array can be initialized with a narrow string. |
| 83 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 84 | if (ElemTy->isCharType()) |
| 85 | return SIF_None; |
| 86 | if (IsWideCharCompatible(ElemTy, Context)) |
| 87 | return SIF_NarrowStringIntoWideChar; |
| 88 | return SIF_Other; |
| 89 | // C99 6.7.8p15 (with correction from DR343), or C11 6.7.9p15: |
| 90 | // "An array with element type compatible with a qualified or unqualified |
| 91 | // version of wchar_t, char16_t, or char32_t may be initialized by a wide |
| 92 | // string literal with the corresponding encoding prefix (L, u, or U, |
| 93 | // respectively), optionally enclosed in braces. |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 94 | case StringLiteral::UTF16: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 95 | if (Context.typesAreCompatible(Context.Char16Ty, ElemTy)) |
| 96 | return SIF_None; |
| 97 | if (ElemTy->isCharType()) |
| 98 | return SIF_WideStringIntoChar; |
| 99 | if (IsWideCharCompatible(ElemTy, Context)) |
| 100 | return SIF_IncompatWideStringIntoWideChar; |
| 101 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 102 | case StringLiteral::UTF32: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 103 | if (Context.typesAreCompatible(Context.Char32Ty, ElemTy)) |
| 104 | return SIF_None; |
| 105 | if (ElemTy->isCharType()) |
| 106 | return SIF_WideStringIntoChar; |
| 107 | if (IsWideCharCompatible(ElemTy, Context)) |
| 108 | return SIF_IncompatWideStringIntoWideChar; |
| 109 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 110 | case StringLiteral::Wide: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 111 | if (Context.typesAreCompatible(Context.getWideCharType(), ElemTy)) |
| 112 | return SIF_None; |
| 113 | if (ElemTy->isCharType()) |
| 114 | return SIF_WideStringIntoChar; |
| 115 | if (IsWideCharCompatible(ElemTy, Context)) |
| 116 | return SIF_IncompatWideStringIntoWideChar; |
| 117 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 118 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 120 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 123 | static StringInitFailureKind IsStringInit(Expr *init, QualType declType, |
| 124 | ASTContext &Context) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 125 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 126 | if (!arrayType) |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 127 | return SIF_Other; |
| 128 | return IsStringInit(init, arrayType, Context); |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 131 | /// Update the type of a string literal, including any surrounding parentheses, |
| 132 | /// to match the type of the object which it is initializing. |
| 133 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 134 | while (true) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 135 | E->setType(Ty); |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 136 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) |
| 137 | break; |
| 138 | else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 139 | E = PE->getSubExpr(); |
| 140 | else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 141 | E = UO->getSubExpr(); |
| 142 | else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) |
| 143 | E = GSE->getResultExpr(); |
| 144 | else |
| 145 | llvm_unreachable("unexpected expr in string literal init"); |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 146 | } |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 149 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 150 | Sema &S) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 151 | // Get the length of the string as parsed. |
Ben Langmuir | 577b393 | 2015-01-26 19:04:10 +0000 | [diff] [blame] | 152 | auto *ConstantArrayTy = |
Ben Langmuir | 7b30f53 | 2015-01-26 20:01:17 +0000 | [diff] [blame] | 153 | cast<ConstantArrayType>(Str->getType()->getAsArrayTypeUnsafe()); |
Ben Langmuir | 577b393 | 2015-01-26 19:04:10 +0000 | [diff] [blame] | 154 | uint64_t StrLength = ConstantArrayTy->getSize().getZExtValue(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 156 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 158 | // being initialized to a string literal. |
Benjamin Kramer | e073177 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 159 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 160 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 161 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 162 | ConstVal, |
| 163 | ArrayType::Normal, 0); |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 164 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 165 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 166 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 168 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 170 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 171 | // the size may be smaller or larger than the string we are initializing. |
| 172 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 173 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 174 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | d162fb8 | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 175 | // For Pascal strings it's OK to strip off the terminating null character, |
| 176 | // so the example below is valid: |
| 177 | // |
| 178 | // unsigned char a[2] = "\pa"; |
| 179 | if (SL->isPascal()) |
| 180 | StrLength--; |
| 181 | } |
| 182 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 183 | // [dcl.init.string]p2 |
| 184 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 185 | S.Diag(Str->getLocStart(), |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 186 | diag::err_initializer_string_for_char_array_too_long) |
| 187 | << Str->getSourceRange(); |
| 188 | } else { |
| 189 | // C99 6.7.8p14. |
| 190 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 191 | S.Diag(Str->getLocStart(), |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 192 | diag::ext_initializer_string_for_char_array_too_long) |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 193 | << Str->getSourceRange(); |
| 194 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 196 | // Set the type to the actual size that we are initializing. If we have |
| 197 | // something like: |
| 198 | // char x[1] = "foo"; |
| 199 | // then this will set the string literal's type to char[1]. |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 200 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 203 | //===----------------------------------------------------------------------===// |
| 204 | // Semantic checking for initializer lists. |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 207 | /// @brief Semantic checking for initializer lists. |
| 208 | /// |
| 209 | /// The InitListChecker class contains a set of routines that each |
| 210 | /// handle the initialization of a certain kind of entity, e.g., |
| 211 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 212 | /// InitListChecker itself performs a recursive walk of the subobject |
| 213 | /// structure of the type to be initialized, while stepping through |
| 214 | /// the initializer list one element at a time. The IList and Index |
| 215 | /// parameters to each of the Check* routines contain the active |
| 216 | /// (syntactic) initializer list and the index into that initializer |
| 217 | /// list that represents the current initializer. Each routine is |
| 218 | /// responsible for moving that Index forward as it consumes elements. |
| 219 | /// |
| 220 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 221 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 222 | /// initializer list and the index into that initializer list where we |
| 223 | /// are copying initializers as we map them over to the semantic |
| 224 | /// list. Once we have completed our recursive walk of the subobject |
| 225 | /// structure, we will have constructed a full semantic initializer |
| 226 | /// list. |
| 227 | /// |
| 228 | /// C99 designators cause changes in the initializer list traversal, |
| 229 | /// because they make the initialization "jump" into a specific |
| 230 | /// subobject and then continue the initialization from that |
| 231 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 232 | /// designated subobject and manages backing out the recursion to |
| 233 | /// initialize the subobjects after the one designated. |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 234 | namespace { |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 235 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 236 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 237 | bool hadError; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 238 | bool VerifyOnly; // no diagnostics, no structure building |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 239 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 240 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 242 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 243 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 244 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 245 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 246 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 247 | InitListExpr *IList, QualType &T, |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 248 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 249 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 250 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 251 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 253 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 254 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 255 | unsigned &StructuredIndex, |
| 256 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 257 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 258 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 259 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 260 | InitListExpr *StructuredList, |
| 261 | unsigned &StructuredIndex); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 262 | void CheckComplexType(const InitializedEntity &Entity, |
| 263 | InitListExpr *IList, QualType DeclType, |
| 264 | unsigned &Index, |
| 265 | InitListExpr *StructuredList, |
| 266 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 267 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 268 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 269 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 270 | InitListExpr *StructuredList, |
| 271 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 272 | void CheckReferenceType(const InitializedEntity &Entity, |
| 273 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 274 | unsigned &Index, |
| 275 | InitListExpr *StructuredList, |
| 276 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 277 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 278 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 279 | InitListExpr *StructuredList, |
| 280 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 281 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 282 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 284 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 285 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 286 | unsigned &StructuredIndex, |
| 287 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 288 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 289 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 291 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 292 | InitListExpr *StructuredList, |
| 293 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 294 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 295 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 296 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 298 | RecordDecl::field_iterator *NextField, |
| 299 | llvm::APSInt *NextElementIndex, |
| 300 | unsigned &Index, |
| 301 | InitListExpr *StructuredList, |
| 302 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 303 | bool FinishSubobjectInit, |
| 304 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 305 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 306 | QualType CurrentObjectType, |
| 307 | InitListExpr *StructuredList, |
| 308 | unsigned StructuredIndex, |
| 309 | SourceRange InitRange); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 310 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 311 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 312 | Expr *expr); |
| 313 | int numArrayElements(QualType DeclType); |
| 314 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 315 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 316 | static ExprResult PerformEmptyInit(Sema &SemaRef, |
| 317 | SourceLocation Loc, |
| 318 | const InitializedEntity &Entity, |
| 319 | bool VerifyOnly); |
| 320 | void FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 321 | const InitializedEntity &ParentEntity, |
| 322 | InitListExpr *ILE, bool &RequiresSecondPass); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 323 | void FillInEmptyInitializations(const InitializedEntity &Entity, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 324 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 325 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 326 | Expr *InitExpr, FieldDecl *Field, |
| 327 | bool TopLevelObject); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 328 | void CheckEmptyInitializable(const InitializedEntity &Entity, |
| 329 | SourceLocation Loc); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 330 | |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 331 | public: |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 332 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 333 | InitListExpr *IL, QualType &T, bool VerifyOnly); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 334 | bool HadError() { return hadError; } |
| 335 | |
| 336 | // @brief Retrieves the fully-structured initializer list used for |
| 337 | // semantic analysis and code generation. |
| 338 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 339 | }; |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 340 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 341 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 342 | ExprResult InitListChecker::PerformEmptyInit(Sema &SemaRef, |
| 343 | SourceLocation Loc, |
| 344 | const InitializedEntity &Entity, |
| 345 | bool VerifyOnly) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 346 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 347 | true); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 348 | MultiExprArg SubInit; |
| 349 | Expr *InitExpr; |
| 350 | InitListExpr DummyInitList(SemaRef.Context, Loc, None, Loc); |
| 351 | |
| 352 | // C++ [dcl.init.aggr]p7: |
| 353 | // If there are fewer initializer-clauses in the list than there are |
| 354 | // members in the aggregate, then each member not explicitly initialized |
| 355 | // ... |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 356 | bool EmptyInitList = SemaRef.getLangOpts().CPlusPlus11 && |
| 357 | Entity.getType()->getBaseElementTypeUnsafe()->isRecordType(); |
| 358 | if (EmptyInitList) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 359 | // C++1y / DR1070: |
| 360 | // shall be initialized [...] from an empty initializer list. |
| 361 | // |
| 362 | // We apply the resolution of this DR to C++11 but not C++98, since C++98 |
| 363 | // does not have useful semantics for initialization from an init list. |
| 364 | // We treat this as copy-initialization, because aggregate initialization |
| 365 | // always performs copy-initialization on its elements. |
| 366 | // |
| 367 | // Only do this if we're initializing a class type, to avoid filling in |
| 368 | // the initializer list where possible. |
| 369 | InitExpr = VerifyOnly ? &DummyInitList : new (SemaRef.Context) |
| 370 | InitListExpr(SemaRef.Context, Loc, None, Loc); |
| 371 | InitExpr->setType(SemaRef.Context.VoidTy); |
| 372 | SubInit = InitExpr; |
| 373 | Kind = InitializationKind::CreateCopy(Loc, Loc); |
| 374 | } else { |
| 375 | // C++03: |
| 376 | // shall be value-initialized. |
| 377 | } |
| 378 | |
| 379 | InitializationSequence InitSeq(SemaRef, Entity, Kind, SubInit); |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 380 | // libstdc++4.6 marks the vector default constructor as explicit in |
| 381 | // _GLIBCXX_DEBUG mode, so recover using the C++03 logic in that case. |
| 382 | // stlport does so too. Look for std::__debug for libstdc++, and for |
| 383 | // std:: for stlport. This is effectively a compiler-side implementation of |
| 384 | // LWG2193. |
| 385 | if (!InitSeq && EmptyInitList && InitSeq.getFailureKind() == |
| 386 | InitializationSequence::FK_ExplicitConstructor) { |
| 387 | OverloadCandidateSet::iterator Best; |
| 388 | OverloadingResult O = |
| 389 | InitSeq.getFailedCandidateSet() |
| 390 | .BestViableFunction(SemaRef, Kind.getLocation(), Best); |
| 391 | (void)O; |
| 392 | assert(O == OR_Success && "Inconsistent overload resolution"); |
| 393 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 394 | CXXRecordDecl *R = CtorDecl->getParent(); |
| 395 | |
| 396 | if (CtorDecl->getMinRequiredArguments() == 0 && |
| 397 | CtorDecl->isExplicit() && R->getDeclName() && |
| 398 | SemaRef.SourceMgr.isInSystemHeader(CtorDecl->getLocation())) { |
| 399 | |
| 400 | |
| 401 | bool IsInStd = false; |
| 402 | for (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(R->getDeclContext()); |
Nico Weber | 5752ad0 | 2014-07-03 00:38:25 +0000 | [diff] [blame] | 403 | ND && !IsInStd; ND = dyn_cast<NamespaceDecl>(ND->getParent())) { |
Nico Weber | bcb70ee | 2014-07-02 23:51:09 +0000 | [diff] [blame] | 404 | if (SemaRef.getStdNamespace()->InEnclosingNamespaceSetOf(ND)) |
| 405 | IsInStd = true; |
| 406 | } |
| 407 | |
| 408 | if (IsInStd && llvm::StringSwitch<bool>(R->getName()) |
| 409 | .Cases("basic_string", "deque", "forward_list", true) |
| 410 | .Cases("list", "map", "multimap", "multiset", true) |
| 411 | .Cases("priority_queue", "queue", "set", "stack", true) |
| 412 | .Cases("unordered_map", "unordered_set", "vector", true) |
| 413 | .Default(false)) { |
| 414 | InitSeq.InitializeFrom( |
| 415 | SemaRef, Entity, |
| 416 | InitializationKind::CreateValue(Loc, Loc, Loc, true), |
| 417 | MultiExprArg(), /*TopLevelOfInitList=*/false); |
| 418 | // Emit a warning for this. System header warnings aren't shown |
| 419 | // by default, but people working on system headers should see it. |
| 420 | if (!VerifyOnly) { |
| 421 | SemaRef.Diag(CtorDecl->getLocation(), |
| 422 | diag::warn_invalid_initializer_from_system_header); |
| 423 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 424 | diag::note_used_in_initialization_here); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 429 | if (!InitSeq) { |
| 430 | if (!VerifyOnly) { |
| 431 | InitSeq.Diagnose(SemaRef, Entity, Kind, SubInit); |
| 432 | if (Entity.getKind() == InitializedEntity::EK_Member) |
| 433 | SemaRef.Diag(Entity.getDecl()->getLocation(), |
| 434 | diag::note_in_omitted_aggregate_initializer) |
| 435 | << /*field*/1 << Entity.getDecl(); |
| 436 | else if (Entity.getKind() == InitializedEntity::EK_ArrayElement) |
| 437 | SemaRef.Diag(Loc, diag::note_in_omitted_aggregate_initializer) |
| 438 | << /*array element*/0 << Entity.getElementIndex(); |
| 439 | } |
| 440 | return ExprError(); |
| 441 | } |
| 442 | |
| 443 | return VerifyOnly ? ExprResult(static_cast<Expr *>(nullptr)) |
| 444 | : InitSeq.Perform(SemaRef, Entity, Kind, SubInit); |
| 445 | } |
| 446 | |
| 447 | void InitListChecker::CheckEmptyInitializable(const InitializedEntity &Entity, |
| 448 | SourceLocation Loc) { |
| 449 | assert(VerifyOnly && |
| 450 | "CheckEmptyInitializable is only inteded for verification mode."); |
| 451 | if (PerformEmptyInit(SemaRef, Loc, Entity, /*VerifyOnly*/true).isInvalid()) |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 452 | hadError = true; |
| 453 | } |
| 454 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 455 | void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 456 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 457 | InitListExpr *ILE, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 458 | bool &RequiresSecondPass) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 459 | SourceLocation Loc = ILE->getLocEnd(); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 460 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 461 | InitializedEntity MemberEntity |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 462 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 463 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 464 | // C++1y [dcl.init.aggr]p7: |
| 465 | // If there are fewer initializer-clauses in the list than there are |
| 466 | // members in the aggregate, then each member not explicitly initialized |
| 467 | // shall be initialized from its brace-or-equal-initializer [...] |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 468 | if (Field->hasInClassInitializer()) { |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 469 | ExprResult DIE = SemaRef.BuildCXXDefaultInitExpr(Loc, Field); |
| 470 | if (DIE.isInvalid()) { |
| 471 | hadError = true; |
| 472 | return; |
| 473 | } |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 474 | if (Init < NumInits) |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 475 | ILE->setInit(Init, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 476 | else { |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 477 | ILE->updateInit(SemaRef.Context, Init, DIE.get()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 478 | RequiresSecondPass = true; |
| 479 | } |
| 480 | return; |
| 481 | } |
| 482 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 483 | if (Field->getType()->isReferenceType()) { |
| 484 | // C++ [dcl.init.aggr]p9: |
| 485 | // If an incomplete or empty initializer-list leaves a |
| 486 | // member of reference type uninitialized, the program is |
| 487 | // ill-formed. |
| 488 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 489 | << Field->getType() |
| 490 | << ILE->getSyntacticForm()->getSourceRange(); |
| 491 | SemaRef.Diag(Field->getLocation(), |
| 492 | diag::note_uninit_reference_member); |
| 493 | hadError = true; |
| 494 | return; |
| 495 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 496 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 497 | ExprResult MemberInit = PerformEmptyInit(SemaRef, Loc, MemberEntity, |
| 498 | /*VerifyOnly*/false); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 499 | if (MemberInit.isInvalid()) { |
| 500 | hadError = true; |
| 501 | return; |
| 502 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 504 | if (hadError) { |
| 505 | // Do nothing |
| 506 | } else if (Init < NumInits) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 507 | ILE->setInit(Init, MemberInit.getAs<Expr>()); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 508 | } else if (!isa<ImplicitValueInitExpr>(MemberInit.get())) { |
| 509 | // Empty initialization requires a constructor call, so |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 510 | // extend the initializer list to include the constructor |
| 511 | // call and make a note that we'll need to take another pass |
| 512 | // through the initializer list. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 513 | ILE->updateInit(SemaRef.Context, Init, MemberInit.getAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 514 | RequiresSecondPass = true; |
| 515 | } |
| 516 | } else if (InitListExpr *InnerILE |
| 517 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 518 | FillInEmptyInitializations(MemberEntity, InnerILE, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 519 | RequiresSecondPass); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 522 | /// Recursively replaces NULL values within the given initializer list |
| 523 | /// with expressions that perform value-initialization of the |
| 524 | /// appropriate type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 525 | void |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 526 | InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 527 | InitListExpr *ILE, |
| 528 | bool &RequiresSecondPass) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 530 | "Should not have void type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 532 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 533 | const RecordDecl *RDecl = RType->getDecl(); |
| 534 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 535 | FillInEmptyInitForField(0, ILE->getInitializedFieldInUnion(), |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 536 | Entity, ILE, RequiresSecondPass); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 537 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 538 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 539 | for (auto *Field : RDecl->fields()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 540 | if (Field->hasInClassInitializer()) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 541 | FillInEmptyInitForField(0, Field, Entity, ILE, RequiresSecondPass); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 542 | break; |
| 543 | } |
| 544 | } |
| 545 | } else { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 546 | unsigned Init = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 547 | for (auto *Field : RDecl->fields()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 548 | if (Field->isUnnamedBitfield()) |
| 549 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 550 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 551 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 552 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 553 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 554 | FillInEmptyInitForField(Init, Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 555 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 556 | return; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 557 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 558 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 560 | // Only look at the first initialization of a union. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 561 | if (RDecl->isUnion()) |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 562 | break; |
| 563 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 568 | |
| 569 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 571 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 572 | unsigned NumInits = ILE->getNumInits(); |
| 573 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 574 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 575 | ElementType = AType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 576 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 577 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 578 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 579 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 580 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 581 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 582 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 583 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 584 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 585 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 586 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 588 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 589 | if (hadError) |
| 590 | return; |
| 591 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 592 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 593 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 594 | ElementEntity.setElementIndex(Init); |
| 595 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 596 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : nullptr); |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 597 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 598 | ExprResult ElementInit = PerformEmptyInit(SemaRef, ILE->getLocEnd(), |
| 599 | ElementEntity, |
| 600 | /*VerifyOnly*/false); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 601 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 602 | hadError = true; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 603 | return; |
| 604 | } |
| 605 | |
| 606 | if (hadError) { |
| 607 | // Do nothing |
| 608 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 609 | // For arrays, just set the expression used for value-initialization |
| 610 | // of the "holes" in the array. |
| 611 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 612 | ILE->setArrayFiller(ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 613 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 614 | ILE->setInit(Init, ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 615 | } else { |
| 616 | // For arrays, just set the expression used for value-initialization |
| 617 | // of the rest of elements and exit. |
| 618 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 619 | ILE->setArrayFiller(ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 620 | return; |
| 621 | } |
| 622 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 623 | if (!isa<ImplicitValueInitExpr>(ElementInit.get())) { |
| 624 | // Empty initialization requires a constructor call, so |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 625 | // extend the initializer list to include the constructor |
| 626 | // call and make a note that we'll need to take another pass |
| 627 | // through the initializer list. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 628 | ILE->updateInit(SemaRef.Context, Init, ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 629 | RequiresSecondPass = true; |
| 630 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 631 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 632 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 633 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 634 | FillInEmptyInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 638 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 639 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 640 | InitListExpr *IL, QualType &T, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 641 | bool VerifyOnly) |
| 642 | : SemaRef(S), VerifyOnly(VerifyOnly) { |
Richard Smith | 520449d | 2015-02-05 06:15:50 +0000 | [diff] [blame] | 643 | // FIXME: Check that IL isn't already the semantic form of some other |
| 644 | // InitListExpr. If it is, we'd create a broken AST. |
| 645 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 646 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 647 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 648 | FullyStructuredList = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 649 | getStructuredSubobjectInit(IL, 0, T, nullptr, 0, IL->getSourceRange()); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 650 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 651 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 652 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 653 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 654 | bool RequiresSecondPass = false; |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 655 | FillInEmptyInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 656 | if (RequiresSecondPass && !hadError) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 657 | FillInEmptyInitializations(Entity, FullyStructuredList, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 658 | RequiresSecondPass); |
| 659 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 663 | // FIXME: use a proper constant |
| 664 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 665 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 666 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 667 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 668 | } |
| 669 | return maxElements; |
| 670 | } |
| 671 | |
| 672 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 673 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 674 | int InitializableMembers = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 675 | for (const auto *Field : structDecl->fields()) |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 676 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 677 | ++InitializableMembers; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 678 | |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 679 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 680 | return std::min(InitializableMembers, 1); |
| 681 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 684 | /// Check whether the range of the initializer \p ParentIList from element |
| 685 | /// \p Index onwards can be used to initialize an object of type \p T. Update |
| 686 | /// \p Index to indicate how many elements of the list were consumed. |
| 687 | /// |
| 688 | /// This also fills in \p StructuredList, from element \p StructuredIndex |
| 689 | /// onwards, with the fully-braced, desugared form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 690 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 691 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 692 | QualType T, unsigned &Index, |
| 693 | InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 694 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 695 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 697 | if (T->isArrayType()) |
| 698 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 699 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 700 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 701 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 702 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 703 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 704 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 705 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 706 | if (maxElements == 0) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 707 | if (!VerifyOnly) |
| 708 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 709 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 710 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 711 | hadError = true; |
| 712 | return; |
| 713 | } |
| 714 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 715 | // Build a structured initializer list corresponding to this subobject. |
| 716 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 718 | StructuredIndex, |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 719 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 720 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 721 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 722 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 723 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 724 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 725 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 726 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | StructuredSubobjectInitList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 728 | StructuredSubobjectInitIndex); |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 729 | |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 730 | if (!VerifyOnly) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 731 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 732 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 733 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 734 | // Update the structured sub-object initializer so that it's ending |
| 735 | // range corresponds with the end of the last initializer it used. |
| 736 | if (EndIndex < ParentIList->getNumInits()) { |
| 737 | SourceLocation EndLoc |
| 738 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 739 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 740 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 741 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 742 | // Complain about missing braces. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 743 | if (T->isArrayType() || T->isRecordType()) { |
| 744 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 745 | diag::warn_missing_braces) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 746 | << StructuredSubobjectInitList->getSourceRange() |
| 747 | << FixItHint::CreateInsertion( |
| 748 | StructuredSubobjectInitList->getLocStart(), "{") |
| 749 | << FixItHint::CreateInsertion( |
| 750 | SemaRef.getLocForEndOfToken( |
| 751 | StructuredSubobjectInitList->getLocEnd()), |
| 752 | "}"); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 753 | } |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 754 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 757 | /// Check whether the initializer \p IList (that was written with explicit |
| 758 | /// braces) can be used to initialize an object of type \p T. |
| 759 | /// |
| 760 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 761 | /// form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 762 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 763 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 764 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 765 | bool TopLevelObject) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 766 | if (!VerifyOnly) { |
| 767 | SyntacticToSemantic[IList] = StructuredList; |
| 768 | StructuredList->setSyntacticForm(IList); |
| 769 | } |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 770 | |
| 771 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 772 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 773 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 774 | if (!VerifyOnly) { |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 775 | QualType ExprTy = T; |
| 776 | if (!ExprTy->isArrayType()) |
| 777 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 778 | IList->setType(ExprTy); |
| 779 | StructuredList->setType(ExprTy); |
| 780 | } |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 781 | if (hadError) |
| 782 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 783 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 784 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 785 | // We have leftover initializers |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 786 | if (VerifyOnly) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 787 | if (SemaRef.getLangOpts().CPlusPlus || |
| 788 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 789 | IList->getType()->isVectorType())) { |
| 790 | hadError = true; |
| 791 | } |
| 792 | return; |
| 793 | } |
| 794 | |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 795 | if (StructuredIndex == 1 && |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 796 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 797 | SIF_None) { |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 798 | unsigned DK = diag::ext_excess_initializers_in_char_array_initializer; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 799 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 800 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 801 | hadError = true; |
| 802 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 803 | // Special-case |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 804 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 805 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 806 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 807 | // Don't complain for incomplete types, since we'll get an error |
| 808 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 809 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 811 | CurrentObjectType->isArrayType()? 0 : |
| 812 | CurrentObjectType->isVectorType()? 1 : |
| 813 | CurrentObjectType->isScalarType()? 2 : |
| 814 | CurrentObjectType->isUnionType()? 3 : |
| 815 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 816 | |
Richard Smith | 1b98ccc | 2014-07-19 01:39:17 +0000 | [diff] [blame] | 817 | unsigned DK = diag::ext_excess_initializers; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 818 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 819 | DK = diag::err_excess_initializers; |
| 820 | hadError = true; |
| 821 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 822 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 823 | DK = diag::err_excess_initializers; |
| 824 | hadError = true; |
| 825 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 826 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 827 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 828 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 829 | } |
| 830 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 831 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 832 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 833 | !TopLevelObject) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 834 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 835 | << IList->getSourceRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 836 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 837 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 840 | void InitListChecker::CheckListElementTypes(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 &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 843 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 844 | unsigned &Index, |
| 845 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 846 | unsigned &StructuredIndex, |
| 847 | bool TopLevelObject) { |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 848 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 849 | // Explicitly braced initializer for complex type can be real+imaginary |
| 850 | // parts. |
| 851 | CheckComplexType(Entity, IList, DeclType, Index, |
| 852 | StructuredList, StructuredIndex); |
| 853 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 854 | CheckScalarType(Entity, IList, DeclType, Index, |
| 855 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 856 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 857 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 858 | StructuredList, StructuredIndex); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 859 | } else if (DeclType->isRecordType()) { |
| 860 | assert(DeclType->isAggregateType() && |
| 861 | "non-aggregate records should be handed in CheckSubElementType"); |
| 862 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 863 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 864 | SubobjectIsDesignatorContext, Index, |
| 865 | StructuredList, StructuredIndex, |
| 866 | TopLevelObject); |
| 867 | } else if (DeclType->isArrayType()) { |
| 868 | llvm::APSInt Zero( |
| 869 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 870 | false); |
| 871 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 872 | SubobjectIsDesignatorContext, Index, |
| 873 | StructuredList, StructuredIndex); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 874 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 875 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 876 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 877 | if (!VerifyOnly) |
| 878 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 879 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 880 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 881 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 882 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 883 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 884 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 885 | if (!VerifyOnly) |
| 886 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 887 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 888 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 889 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 890 | if (!VerifyOnly) |
| 891 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 892 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 893 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 897 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 898 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 900 | unsigned &Index, |
| 901 | InitListExpr *StructuredList, |
| 902 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 903 | Expr *expr = IList->getInit(Index); |
Richard Smith | 72752e8 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 904 | |
| 905 | if (ElemType->isReferenceType()) |
| 906 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 907 | StructuredList, StructuredIndex); |
| 908 | |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 909 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 910 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 911 | InitListExpr *InnerStructuredList |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 912 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 913 | StructuredList, StructuredIndex, |
| 914 | SubInitList->getSourceRange()); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 915 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 916 | InnerStructuredList); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 917 | ++StructuredIndex; |
| 918 | ++Index; |
| 919 | return; |
| 920 | } |
| 921 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 922 | "non-aggregate records are only possible in C++"); |
| 923 | // C++ initialization is handled later. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 924 | } else if (isa<ImplicitValueInitExpr>(expr)) { |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 925 | // This happens during template instantiation when we see an InitListExpr |
| 926 | // that we've already checked once. |
Richard Smith | c4158e86 | 2014-07-18 04:47:25 +0000 | [diff] [blame] | 927 | assert(SemaRef.Context.hasSameType(expr->getType(), ElemType) && |
Richard Smith | 8aa561b | 2014-07-17 23:12:06 +0000 | [diff] [blame] | 928 | "found implicit initialization for the wrong type"); |
| 929 | if (!VerifyOnly) |
| 930 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 931 | ++Index; |
| 932 | return; |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Eli Friedman | 4628cf7 | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 935 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
| 936 | if (ElemType->isScalarType() || ElemType->isAtomicType()) |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 937 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 938 | StructuredList, StructuredIndex); |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 939 | |
Eli Friedman | 4628cf7 | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 940 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
| 941 | ElemType->isArrayType()) && "Unexpected type"); |
| 942 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 943 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 944 | // arrayType can be incomplete if we're initializing a flexible |
| 945 | // array member. There's nothing we can do with the completed |
| 946 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 947 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 948 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 949 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 950 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 951 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 952 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 953 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 954 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 955 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 956 | |
| 957 | // Fall through for subaggregate initialization. |
| 958 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 959 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 960 | // C++ [dcl.init.aggr]p12: |
| 961 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 962 | // initializing the aggregate member with an initializer from |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 963 | // an initializer-list. If the initializer can initialize a |
| 964 | // member, the member is initialized. [...] |
| 965 | |
| 966 | // FIXME: Better EqualLoc? |
| 967 | InitializationKind Kind = |
| 968 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 969 | InitializationSequence Seq(SemaRef, Entity, Kind, expr); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 970 | |
| 971 | if (Seq) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 972 | if (!VerifyOnly) { |
Richard Smith | 0f8ede1 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 973 | ExprResult Result = |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 974 | Seq.Perform(SemaRef, Entity, Kind, expr); |
Richard Smith | 0f8ede1 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 975 | if (Result.isInvalid()) |
| 976 | hadError = true; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 977 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 978 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 979 | Result.getAs<Expr>()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 980 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 981 | ++Index; |
| 982 | return; |
| 983 | } |
| 984 | |
| 985 | // Fall through for subaggregate initialization |
| 986 | } else { |
| 987 | // C99 6.7.8p13: |
| 988 | // |
| 989 | // The initializer for a structure or union object that has |
| 990 | // automatic storage duration shall be either an initializer |
| 991 | // list as described below, or a single expression that has |
| 992 | // compatible structure or union type. In the latter case, the |
| 993 | // initial value of the object, including unnamed members, is |
| 994 | // that of the expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 995 | ExprResult ExprRes = expr; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 996 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 997 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 998 | !VerifyOnly) |
Eli Friedman | b2a8d46 | 2013-09-17 04:07:04 +0000 | [diff] [blame] | 999 | != Sema::Incompatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1000 | if (ExprRes.isInvalid()) |
| 1001 | hadError = true; |
| 1002 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1003 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 1004 | if (ExprRes.isInvalid()) |
| 1005 | hadError = true; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1006 | } |
| 1007 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1008 | ExprRes.getAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1009 | ++Index; |
| 1010 | return; |
| 1011 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1012 | ExprRes.get(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1013 | // Fall through for subaggregate initialization |
| 1014 | } |
| 1015 | |
| 1016 | // C++ [dcl.init.aggr]p12: |
| 1017 | // |
| 1018 | // [...] Otherwise, if the member is itself a non-empty |
| 1019 | // subaggregate, brace elision is assumed and the initializer is |
| 1020 | // considered for the initialization of the first member of |
| 1021 | // the subaggregate. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1022 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 1023 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1024 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 1025 | StructuredIndex); |
| 1026 | ++StructuredIndex; |
| 1027 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1028 | if (!VerifyOnly) { |
| 1029 | // We cannot initialize this element, so let |
| 1030 | // PerformCopyInitialization produce the appropriate diagnostic. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1031 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1032 | /*TopLevelOfInitList=*/true); |
| 1033 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1034 | hadError = true; |
| 1035 | ++Index; |
| 1036 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1037 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1040 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 1041 | InitListExpr *IList, QualType DeclType, |
| 1042 | unsigned &Index, |
| 1043 | InitListExpr *StructuredList, |
| 1044 | unsigned &StructuredIndex) { |
| 1045 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 1046 | |
| 1047 | // As an extension, clang supports complex initializers, which initialize |
| 1048 | // a complex number component-wise. When an explicit initializer list for |
| 1049 | // a complex number contains two two initializers, this extension kicks in: |
| 1050 | // it exepcts the initializer list to contain two elements convertible to |
| 1051 | // the element type of the complex type. The first element initializes |
| 1052 | // the real part, and the second element intitializes the imaginary part. |
| 1053 | |
| 1054 | if (IList->getNumInits() != 2) |
| 1055 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1056 | StructuredIndex); |
| 1057 | |
| 1058 | // This is an extension in C. (The builtin _Complex type does not exist |
| 1059 | // in the C++ standard.) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1060 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1061 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 1062 | << IList->getSourceRange(); |
| 1063 | |
| 1064 | // Initialize the complex number. |
| 1065 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 1066 | InitializedEntity ElementEntity = |
| 1067 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1068 | |
| 1069 | for (unsigned i = 0; i < 2; ++i) { |
| 1070 | ElementEntity.setElementIndex(Index); |
| 1071 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1072 | StructuredList, StructuredIndex); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1077 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1078 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1079 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1080 | InitListExpr *StructuredList, |
| 1081 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1082 | if (Index >= IList->getNumInits()) { |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1083 | if (!VerifyOnly) |
| 1084 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1085 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1086 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 1087 | diag::err_empty_scalar_initializer) |
| 1088 | << IList->getSourceRange(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1089 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1090 | ++Index; |
| 1091 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1092 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1093 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1094 | |
| 1095 | Expr *expr = IList->getInit(Index); |
| 1096 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1097 | // FIXME: This is invalid, and accepting it causes overload resolution |
| 1098 | // to pick the wrong overload in some corner cases. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1099 | if (!VerifyOnly) |
| 1100 | SemaRef.Diag(SubIList->getLocStart(), |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1101 | diag::ext_many_braces_around_scalar_init) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1102 | << SubIList->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1103 | |
| 1104 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1105 | StructuredIndex); |
| 1106 | return; |
| 1107 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1108 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1109 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1110 | diag::err_designator_for_scalar_init) |
| 1111 | << DeclType << expr->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1112 | hadError = true; |
| 1113 | ++Index; |
| 1114 | ++StructuredIndex; |
| 1115 | return; |
| 1116 | } |
| 1117 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1118 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1119 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1120 | hadError = true; |
| 1121 | ++Index; |
| 1122 | return; |
| 1123 | } |
| 1124 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1125 | ExprResult Result = |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1126 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), expr, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1127 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1128 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1129 | Expr *ResultExpr = nullptr; |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1130 | |
| 1131 | if (Result.isInvalid()) |
| 1132 | hadError = true; // types weren't compatible. |
| 1133 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1134 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1135 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1136 | if (ResultExpr != expr) { |
| 1137 | // The type was promoted, update initializer list. |
| 1138 | IList->setInit(Index, ResultExpr); |
| 1139 | } |
| 1140 | } |
| 1141 | if (hadError) |
| 1142 | ++StructuredIndex; |
| 1143 | else |
| 1144 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1145 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1148 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1149 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1150 | unsigned &Index, |
| 1151 | InitListExpr *StructuredList, |
| 1152 | unsigned &StructuredIndex) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1153 | if (Index >= IList->getNumInits()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1154 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1155 | // general, it would be useful to pass location information down the stack, |
| 1156 | // so that we know the location (or decl) of the "current object" being |
| 1157 | // initialized. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1158 | if (!VerifyOnly) |
| 1159 | SemaRef.Diag(IList->getLocStart(), |
| 1160 | diag::err_init_reference_member_uninitialized) |
| 1161 | << DeclType |
| 1162 | << IList->getSourceRange(); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1163 | hadError = true; |
| 1164 | ++Index; |
| 1165 | ++StructuredIndex; |
| 1166 | return; |
| 1167 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1168 | |
| 1169 | Expr *expr = IList->getInit(Index); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1170 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1171 | if (!VerifyOnly) |
| 1172 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1173 | << DeclType << IList->getSourceRange(); |
| 1174 | hadError = true; |
| 1175 | ++Index; |
| 1176 | ++StructuredIndex; |
| 1177 | return; |
| 1178 | } |
| 1179 | |
| 1180 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1181 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1182 | hadError = true; |
| 1183 | ++Index; |
| 1184 | return; |
| 1185 | } |
| 1186 | |
| 1187 | ExprResult Result = |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1188 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), expr, |
| 1189 | /*TopLevelOfInitList=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1190 | |
| 1191 | if (Result.isInvalid()) |
| 1192 | hadError = true; |
| 1193 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1194 | expr = Result.getAs<Expr>(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1195 | IList->setInit(Index, expr); |
| 1196 | |
| 1197 | if (hadError) |
| 1198 | ++StructuredIndex; |
| 1199 | else |
| 1200 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1201 | ++Index; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1204 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1205 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1206 | unsigned &Index, |
| 1207 | InitListExpr *StructuredList, |
| 1208 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1209 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1210 | unsigned maxElements = VT->getNumElements(); |
| 1211 | unsigned numEltsInit = 0; |
| 1212 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1213 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1214 | if (Index >= IList->getNumInits()) { |
| 1215 | // Make sure the element type can be value-initialized. |
| 1216 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1217 | CheckEmptyInitializable( |
| 1218 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1219 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1220 | return; |
| 1221 | } |
| 1222 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1223 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1224 | // If the initializing element is a vector, try to copy-initialize |
| 1225 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1226 | Expr *Init = IList->getInit(Index); |
| 1227 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1228 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1229 | if (!SemaRef.CanPerformCopyInitialization(Entity, Init)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1230 | hadError = true; |
| 1231 | ++Index; |
| 1232 | return; |
| 1233 | } |
| 1234 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1235 | ExprResult Result = |
| 1236 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), Init, |
| 1237 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1238 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1239 | Expr *ResultExpr = nullptr; |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1240 | if (Result.isInvalid()) |
| 1241 | hadError = true; // types weren't compatible. |
| 1242 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1243 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1244 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1245 | if (ResultExpr != Init) { |
| 1246 | // The type was promoted, update initializer list. |
| 1247 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1248 | } |
| 1249 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1250 | if (hadError) |
| 1251 | ++StructuredIndex; |
| 1252 | else |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1253 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1254 | ResultExpr); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1255 | ++Index; |
| 1256 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1257 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1259 | InitializedEntity ElementEntity = |
| 1260 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1261 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1262 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1263 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1264 | if (Index >= IList->getNumInits()) { |
| 1265 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1266 | CheckEmptyInitializable(ElementEntity, IList->getLocEnd()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1267 | break; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1268 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1269 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1270 | ElementEntity.setElementIndex(Index); |
| 1271 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1272 | StructuredList, StructuredIndex); |
| 1273 | } |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1274 | |
| 1275 | if (VerifyOnly) |
| 1276 | return; |
| 1277 | |
| 1278 | bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian(); |
| 1279 | const VectorType *T = Entity.getType()->getAs<VectorType>(); |
| 1280 | if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector || |
| 1281 | T->getVectorKind() == VectorType::NeonPolyVector)) { |
| 1282 | // The ability to use vector initializer lists is a GNU vector extension |
| 1283 | // and is unrelated to the NEON intrinsics in arm_neon.h. On little |
| 1284 | // endian machines it works fine, however on big endian machines it |
| 1285 | // exhibits surprising behaviour: |
| 1286 | // |
| 1287 | // uint32x2_t x = {42, 64}; |
| 1288 | // return vget_lane_u32(x, 0); // Will return 64. |
| 1289 | // |
| 1290 | // Because of this, explicitly call out that it is non-portable. |
| 1291 | // |
| 1292 | SemaRef.Diag(IList->getLocStart(), |
| 1293 | diag::warn_neon_vector_initializer_non_portable); |
| 1294 | |
| 1295 | const char *typeCode; |
| 1296 | unsigned typeSize = SemaRef.Context.getTypeSize(elementType); |
| 1297 | |
| 1298 | if (elementType->isFloatingType()) |
| 1299 | typeCode = "f"; |
| 1300 | else if (elementType->isSignedIntegerType()) |
| 1301 | typeCode = "s"; |
| 1302 | else if (elementType->isUnsignedIntegerType()) |
| 1303 | typeCode = "u"; |
| 1304 | else |
| 1305 | llvm_unreachable("Invalid element type!"); |
| 1306 | |
| 1307 | SemaRef.Diag(IList->getLocStart(), |
| 1308 | SemaRef.Context.getTypeSize(VT) > 64 ? |
| 1309 | diag::note_neon_vector_initializer_non_portable_q : |
| 1310 | diag::note_neon_vector_initializer_non_portable) |
| 1311 | << typeCode << typeSize; |
| 1312 | } |
| 1313 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1314 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1315 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1316 | |
| 1317 | InitializedEntity ElementEntity = |
| 1318 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1319 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1320 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1321 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1322 | // Don't attempt to go past the end of the init list |
| 1323 | if (Index >= IList->getNumInits()) |
| 1324 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1325 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1326 | ElementEntity.setElementIndex(Index); |
| 1327 | |
| 1328 | QualType IType = IList->getInit(Index)->getType(); |
| 1329 | if (!IType->isVectorType()) { |
| 1330 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1331 | StructuredList, StructuredIndex); |
| 1332 | ++numEltsInit; |
| 1333 | } else { |
| 1334 | QualType VecType; |
| 1335 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1336 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1337 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1338 | if (IType->isExtVectorType()) |
| 1339 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1340 | else |
| 1341 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1342 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1343 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1344 | StructuredList, StructuredIndex); |
| 1345 | numEltsInit += numIElts; |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1350 | if (numEltsInit != maxElements) { |
| 1351 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1352 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1353 | diag::err_vector_incorrect_num_initializers) |
| 1354 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1355 | hadError = true; |
| 1356 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1359 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1360 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1361 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1363 | unsigned &Index, |
| 1364 | InitListExpr *StructuredList, |
| 1365 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1366 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1367 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1368 | // Check for the special-case of initializing an array with a string. |
| 1369 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1370 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1371 | SIF_None) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1372 | // We place the string literal directly into the resulting |
| 1373 | // initializer list. This is the only place where the structure |
| 1374 | // of the structured initializer list doesn't match exactly, |
| 1375 | // because doing so would involve allocating one character |
| 1376 | // constant for each string. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1377 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1378 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1379 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1380 | IList->getInit(Index)); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1381 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1382 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1383 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1384 | return; |
| 1385 | } |
| 1386 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1387 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1388 | // Check for VLAs; in standard C it would be possible to check this |
| 1389 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1390 | // them in all sorts of strange places). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1391 | if (!VerifyOnly) |
| 1392 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1393 | diag::err_variable_object_no_init) |
| 1394 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1395 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1396 | ++Index; |
| 1397 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1398 | return; |
| 1399 | } |
| 1400 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1401 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1402 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1403 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1404 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1405 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1406 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1407 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1408 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1409 | maxElementsKnown = true; |
| 1410 | } |
| 1411 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1412 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1413 | while (Index < IList->getNumInits()) { |
| 1414 | Expr *Init = IList->getInit(Index); |
| 1415 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1416 | // If we're not the subobject that matches up with the '{' for |
| 1417 | // the designator, we shouldn't be handling the |
| 1418 | // designator. Return immediately. |
| 1419 | if (!SubobjectIsDesignatorContext) |
| 1420 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1421 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1422 | // Handle this designated initializer. elementIndex will be |
| 1423 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1424 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1425 | DeclType, nullptr, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1426 | StructuredList, StructuredIndex, true, |
| 1427 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1428 | hadError = true; |
| 1429 | continue; |
| 1430 | } |
| 1431 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1432 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1433 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1434 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1435 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1436 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1437 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1438 | // If the array is of incomplete type, keep track of the number of |
| 1439 | // elements in the initializer. |
| 1440 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1441 | maxElements = elementIndex; |
| 1442 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1443 | continue; |
| 1444 | } |
| 1445 | |
| 1446 | // If we know the maximum number of elements, and we've already |
| 1447 | // hit it, stop consuming elements in the initializer list. |
| 1448 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1449 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1450 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1451 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1452 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1453 | Entity); |
| 1454 | // Check this element. |
| 1455 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1456 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1457 | ++elementIndex; |
| 1458 | |
| 1459 | // If the array is of incomplete type, keep track of the number of |
| 1460 | // elements in the initializer. |
| 1461 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1462 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1463 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1464 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1465 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1466 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1467 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1468 | if (maxElements == Zero) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1469 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1470 | // but is supported by GNU. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1471 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1472 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1473 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1474 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1476 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1477 | } |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1478 | if (!hadError && VerifyOnly) { |
| 1479 | // Check if there are any members of the array that get value-initialized. |
| 1480 | // If so, check if doing that is possible. |
| 1481 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1482 | if (maxElementsKnown && elementIndex < maxElements) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1483 | CheckEmptyInitializable(InitializedEntity::InitializeElement( |
| 1484 | SemaRef.Context, 0, Entity), |
| 1485 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1486 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1489 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1490 | Expr *InitExpr, |
| 1491 | FieldDecl *Field, |
| 1492 | bool TopLevelObject) { |
| 1493 | // Handle GNU flexible array initializers. |
| 1494 | unsigned FlexArrayDiag; |
| 1495 | if (isa<InitListExpr>(InitExpr) && |
| 1496 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1497 | // Empty flexible array init always allowed as an extension |
| 1498 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1499 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1500 | // Disallow flexible array init in C++; it is not required for gcc |
| 1501 | // compatibility, and it needs work to IRGen correctly in general. |
| 1502 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1503 | } else if (!TopLevelObject) { |
| 1504 | // Disallow flexible array init on non-top-level object |
| 1505 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1506 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1507 | // Disallow flexible array init on anything which is not a variable. |
| 1508 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1509 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1510 | // Disallow flexible array init on local variables. |
| 1511 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1512 | } else { |
| 1513 | // Allow other cases. |
| 1514 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1515 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1516 | |
| 1517 | if (!VerifyOnly) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1518 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1519 | FlexArrayDiag) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1520 | << InitExpr->getLocStart(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1521 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1522 | << Field; |
| 1523 | } |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1524 | |
| 1525 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1526 | } |
| 1527 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1528 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1529 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1530 | QualType DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1531 | RecordDecl::field_iterator Field, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1533 | unsigned &Index, |
| 1534 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1535 | unsigned &StructuredIndex, |
| 1536 | bool TopLevelObject) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1537 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1538 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1539 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1540 | // confusion, we forgo checking the intializer for the entire record. |
| 1541 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 845aa66 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1542 | // Assume it was supposed to consume a single initializer. |
| 1543 | ++Index; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1544 | hadError = true; |
| 1545 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1546 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1547 | |
| 1548 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1549 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1550 | |
| 1551 | // If there's a default initializer, use it. |
| 1552 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1553 | if (VerifyOnly) |
| 1554 | return; |
| 1555 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1556 | Field != FieldEnd; ++Field) { |
| 1557 | if (Field->hasInClassInitializer()) { |
| 1558 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1559 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1560 | return; |
| 1561 | } |
| 1562 | } |
| 1563 | } |
| 1564 | |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1565 | // Value-initialize the first member of the union that isn't an unnamed |
| 1566 | // bitfield. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1567 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1568 | Field != FieldEnd; ++Field) { |
Reid Kleckner | 6d829bd | 2014-11-12 21:30:23 +0000 | [diff] [blame] | 1569 | if (!Field->isUnnamedBitfield()) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1570 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1571 | CheckEmptyInitializable( |
| 1572 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 1573 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1574 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1575 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1576 | break; |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1577 | } |
| 1578 | } |
| 1579 | return; |
| 1580 | } |
| 1581 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1582 | // If structDecl is a forward declaration, this loop won't do |
| 1583 | // anything except look at designated initializers; That's okay, |
| 1584 | // because an error should get printed out elsewhere. It might be |
| 1585 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1586 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1587 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1588 | bool InitializedSomething = false; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1589 | bool CheckForMissingFields = true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1590 | while (Index < IList->getNumInits()) { |
| 1591 | Expr *Init = IList->getInit(Index); |
| 1592 | |
| 1593 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1594 | // If we're not the subobject that matches up with the '{' for |
| 1595 | // the designator, we shouldn't be handling the |
| 1596 | // designator. Return immediately. |
| 1597 | if (!SubobjectIsDesignatorContext) |
| 1598 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1599 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1600 | // Handle this designated initializer. Field will be updated to |
| 1601 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1602 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1603 | DeclType, &Field, nullptr, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1604 | StructuredList, StructuredIndex, |
| 1605 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1606 | hadError = true; |
| 1607 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1608 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1609 | |
| 1610 | // Disable check for missing fields when designators are used. |
| 1611 | // This matches gcc behaviour. |
| 1612 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1613 | continue; |
| 1614 | } |
| 1615 | |
| 1616 | if (Field == FieldEnd) { |
| 1617 | // We've run out of fields. We're done. |
| 1618 | break; |
| 1619 | } |
| 1620 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1621 | // We've already initialized a member of a union. We're done. |
| 1622 | if (InitializedSomething && DeclType->isUnionType()) |
| 1623 | break; |
| 1624 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1625 | // If we've hit the flexible array member at the end, we're done. |
| 1626 | if (Field->getType()->isIncompleteArrayType()) |
| 1627 | break; |
| 1628 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1629 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1630 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1631 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1632 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1633 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1634 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1635 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1636 | bool InvalidUse; |
| 1637 | if (VerifyOnly) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1638 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1639 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1640 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1641 | IList->getInit(Index)->getLocStart()); |
| 1642 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1643 | ++Index; |
| 1644 | ++Field; |
| 1645 | hadError = true; |
| 1646 | continue; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1647 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1648 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1649 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1650 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1651 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1652 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1653 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1654 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1655 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1656 | // Initialize the first field within the union. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1657 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1658 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1659 | |
| 1660 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1661 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1662 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1663 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1664 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1665 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1666 | !DeclType->isUnionType()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1667 | // It is possible we have one or more unnamed bitfields remaining. |
| 1668 | // Find first (if any) named field and emit warning. |
| 1669 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1670 | it != end; ++it) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1671 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1672 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
Aaron Ballman | b9bb201 | 2014-01-03 14:54:10 +0000 | [diff] [blame] | 1673 | diag::warn_missing_field_initializers) << *it; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1674 | break; |
| 1675 | } |
| 1676 | } |
| 1677 | } |
| 1678 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1679 | // Check that any remaining fields can be value-initialized. |
| 1680 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1681 | !Field->getType()->isIncompleteArrayType()) { |
| 1682 | // FIXME: Should check for holes left by designated initializers too. |
| 1683 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1684 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1685 | CheckEmptyInitializable( |
| 1686 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 1687 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1688 | } |
| 1689 | } |
| 1690 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1691 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1692 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1693 | return; |
| 1694 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1695 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1696 | TopLevelObject)) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1697 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1698 | ++Index; |
| 1699 | return; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1702 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1703 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1704 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1705 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1706 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1707 | StructuredList, StructuredIndex); |
| 1708 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1709 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1710 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1711 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1712 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1713 | /// \brief Expand a field designator that refers to a member of an |
| 1714 | /// anonymous struct or union into a series of field designators that |
| 1715 | /// refers to the field within the appropriate subobject. |
| 1716 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1717 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | DesignatedInitExpr *DIE, |
| 1719 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1720 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1721 | typedef DesignatedInitExpr::Designator Designator; |
| 1722 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1723 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1724 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1725 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1726 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1727 | if (PI + 1 == PE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1728 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1729 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1730 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1731 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1732 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 1733 | SourceLocation(), SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1734 | assert(isa<FieldDecl>(*PI)); |
| 1735 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | // Expand the current designator into the set of replacement |
| 1739 | // designators, so we have a full subobject path down to where the |
| 1740 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1741 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1742 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1743 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1745 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1746 | DesignatedInitExpr *DIE) { |
| 1747 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1748 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1749 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1750 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1751 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1752 | DIE->size(), IndexExprs, |
| 1753 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1754 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1755 | } |
| 1756 | |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1757 | namespace { |
| 1758 | |
| 1759 | // Callback to only accept typo corrections that are for field members of |
| 1760 | // the given struct or union. |
| 1761 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1762 | public: |
| 1763 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1764 | : Record(RD) {} |
| 1765 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1766 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1767 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1768 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1769 | } |
| 1770 | |
| 1771 | private: |
| 1772 | RecordDecl *Record; |
| 1773 | }; |
| 1774 | |
| 1775 | } |
| 1776 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1777 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1778 | /// |
| 1779 | /// Determines whether the designated initializer @p DIE, which |
| 1780 | /// resides at the given @p Index within the initializer list @p |
| 1781 | /// IList, is well-formed for a current object of type @p DeclType |
| 1782 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1784 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1785 | /// |
| 1786 | /// @param IList The initializer list in which this designated |
| 1787 | /// initializer occurs. |
| 1788 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1789 | /// @param DIE The designated initializer expression. |
| 1790 | /// |
| 1791 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1792 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1793 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1794 | /// into which the designation in @p DIE should refer. |
| 1795 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1796 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1797 | /// a field, this will be set to the field declaration corresponding |
| 1798 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1799 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1800 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1801 | /// DIE is an array designator or GNU array-range designator, this |
| 1802 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1803 | /// |
| 1804 | /// @param Index Index into @p IList where the designated initializer |
| 1805 | /// @p DIE occurs. |
| 1806 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1807 | /// @param StructuredList The initializer list expression that |
| 1808 | /// describes all of the subobject initializers in the order they'll |
| 1809 | /// actually be initialized. |
| 1810 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1811 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1812 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1813 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1814 | InitListExpr *IList, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1815 | DesignatedInitExpr *DIE, |
| 1816 | unsigned DesigIdx, |
| 1817 | QualType &CurrentObjectType, |
| 1818 | RecordDecl::field_iterator *NextField, |
| 1819 | llvm::APSInt *NextElementIndex, |
| 1820 | unsigned &Index, |
| 1821 | InitListExpr *StructuredList, |
| 1822 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1823 | bool FinishSubobjectInit, |
| 1824 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1825 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1826 | // Check the actual initialization for the designated object type. |
| 1827 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1828 | |
| 1829 | // Temporarily remove the designator expression from the |
| 1830 | // initializer list that the child calls see, so that we don't try |
| 1831 | // to re-process the designator. |
| 1832 | unsigned OldIndex = Index; |
| 1833 | IList->setInit(OldIndex, DIE->getInit()); |
| 1834 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1835 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1836 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1837 | |
| 1838 | // Restore the designated initializer expression in the syntactic |
| 1839 | // form of the initializer list. |
| 1840 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1841 | DIE->setInit(IList->getInit(OldIndex)); |
| 1842 | IList->setInit(OldIndex, DIE); |
| 1843 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1844 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1847 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1848 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1849 | if (!VerifyOnly) { |
| 1850 | assert((IsFirstDesignator || StructuredList) && |
| 1851 | "Need a non-designated initializer list to start from"); |
| 1852 | |
| 1853 | // Determine the structural initializer list that corresponds to the |
| 1854 | // current subobject. |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1855 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1856 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1857 | StructuredList, StructuredIndex, |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1858 | SourceRange(D->getLocStart(), |
| 1859 | DIE->getLocEnd())); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1860 | assert(StructuredList && "Expected a structured initializer list"); |
| 1861 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1862 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1863 | if (D->isFieldDesignator()) { |
| 1864 | // C99 6.7.8p7: |
| 1865 | // |
| 1866 | // If a designator has the form |
| 1867 | // |
| 1868 | // . identifier |
| 1869 | // |
| 1870 | // then the current object (defined below) shall have |
| 1871 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1872 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1873 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1874 | if (!RT) { |
| 1875 | SourceLocation Loc = D->getDotLoc(); |
| 1876 | if (Loc.isInvalid()) |
| 1877 | Loc = D->getFieldLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1878 | if (!VerifyOnly) |
| 1879 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1880 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1881 | ++Index; |
| 1882 | return true; |
| 1883 | } |
| 1884 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1885 | FieldDecl *KnownField = D->getField(); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1886 | if (!KnownField) { |
| 1887 | IdentifierInfo *FieldName = D->getFieldName(); |
| 1888 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
| 1889 | for (NamedDecl *ND : Lookup) { |
| 1890 | if (auto *FD = dyn_cast<FieldDecl>(ND)) { |
| 1891 | KnownField = FD; |
| 1892 | break; |
| 1893 | } |
| 1894 | if (auto *IFD = dyn_cast<IndirectFieldDecl>(ND)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1895 | // In verify mode, don't modify the original. |
| 1896 | if (VerifyOnly) |
| 1897 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1898 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IFD); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1899 | D = DIE->getDesignator(DesigIdx); |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1900 | KnownField = cast<FieldDecl>(*IFD->chain_begin()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1901 | break; |
| 1902 | } |
| 1903 | } |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1904 | if (!KnownField) { |
| 1905 | if (VerifyOnly) { |
| 1906 | ++Index; |
| 1907 | return true; // No typo correction when just trying this out. |
| 1908 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1909 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1910 | // Name lookup found something, but it wasn't a field. |
| 1911 | if (!Lookup.empty()) { |
| 1912 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
| 1913 | << FieldName; |
| 1914 | SemaRef.Diag(Lookup.front()->getLocation(), |
| 1915 | diag::note_field_designator_found); |
| 1916 | ++Index; |
| 1917 | return true; |
| 1918 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1919 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1920 | // Name lookup didn't find anything. |
| 1921 | // Determine whether this was a typo for another field name. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1922 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1923 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1924 | Sema::LookupMemberName, /*Scope=*/nullptr, /*SS=*/nullptr, |
Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 1925 | llvm::make_unique<FieldInitializerValidatorCCC>(RT->getDecl()), |
| 1926 | Sema::CTK_ErrorRecovery, RT->getDecl())) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1927 | SemaRef.diagnoseTypo( |
| 1928 | Corrected, |
| 1929 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1930 | << FieldName << CurrentObjectType); |
| 1931 | KnownField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1932 | hadError = true; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1933 | } else { |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1934 | // Typo correction didn't find anything. |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1935 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1936 | << FieldName << CurrentObjectType; |
| 1937 | ++Index; |
| 1938 | return true; |
| 1939 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1940 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1941 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1942 | |
David Majnemer | 58e4ea9 | 2014-08-23 01:48:50 +0000 | [diff] [blame] | 1943 | unsigned FieldIndex = 0; |
| 1944 | for (auto *FI : RT->getDecl()->fields()) { |
| 1945 | if (FI->isUnnamedBitfield()) |
| 1946 | continue; |
| 1947 | if (KnownField == FI) |
| 1948 | break; |
| 1949 | ++FieldIndex; |
| 1950 | } |
| 1951 | |
David Majnemer | 36ef898 | 2014-08-11 18:33:59 +0000 | [diff] [blame] | 1952 | RecordDecl::field_iterator Field = |
| 1953 | RecordDecl::field_iterator(DeclContext::decl_iterator(KnownField)); |
| 1954 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1955 | // All of the fields of a union are located at the same place in |
| 1956 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1957 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1958 | FieldIndex = 0; |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1959 | if (!VerifyOnly) { |
| 1960 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
| 1961 | if (CurrentField && CurrentField != *Field) { |
| 1962 | assert(StructuredList->getNumInits() == 1 |
| 1963 | && "A union should never have more than one initializer!"); |
| 1964 | |
| 1965 | // we're about to throw away an initializer, emit warning |
| 1966 | SemaRef.Diag(D->getFieldLoc(), |
| 1967 | diag::warn_initializer_overrides) |
| 1968 | << D->getSourceRange(); |
| 1969 | Expr *ExistingInit = StructuredList->getInit(0); |
| 1970 | SemaRef.Diag(ExistingInit->getLocStart(), |
| 1971 | diag::note_previous_initializer) |
| 1972 | << /*FIXME:has side effects=*/0 |
| 1973 | << ExistingInit->getSourceRange(); |
| 1974 | |
| 1975 | // remove existing initializer |
| 1976 | StructuredList->resizeInits(SemaRef.Context, 0); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1977 | StructuredList->setInitializedFieldInUnion(nullptr); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1980 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1981 | } |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1982 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1983 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1984 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1985 | bool InvalidUse; |
| 1986 | if (VerifyOnly) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1987 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1988 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1989 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1990 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1991 | ++Index; |
| 1992 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1993 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1994 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1995 | if (!VerifyOnly) { |
| 1996 | // Update the designator with the field declaration. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1997 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1999 | // Make sure that our non-designated initializer list has space |
| 2000 | // for a subobject corresponding to this field. |
| 2001 | if (FieldIndex >= StructuredList->getNumInits()) |
| 2002 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 2003 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2004 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2005 | // This designator names a flexible array member. |
| 2006 | if (Field->getType()->isIncompleteArrayType()) { |
| 2007 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2008 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2009 | // We can't designate an object within the flexible array |
| 2010 | // member (because GCC doesn't allow it). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2011 | if (!VerifyOnly) { |
| 2012 | DesignatedInitExpr::Designator *NextD |
| 2013 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 2014 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2015 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 2016 | << SourceRange(NextD->getLocStart(), |
| 2017 | DIE->getLocEnd()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2018 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2019 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2020 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2021 | Invalid = true; |
| 2022 | } |
| 2023 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 2024 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 2025 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2026 | // The initializer is not an initializer list. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2027 | if (!VerifyOnly) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2028 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2029 | diag::err_flexible_array_init_needs_braces) |
| 2030 | << DIE->getInit()->getSourceRange(); |
| 2031 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2032 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2033 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2034 | Invalid = true; |
| 2035 | } |
| 2036 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2037 | // Check GNU flexible array initializer. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2038 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2039 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2040 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2041 | |
| 2042 | if (Invalid) { |
| 2043 | ++Index; |
| 2044 | return true; |
| 2045 | } |
| 2046 | |
| 2047 | // Initialize the array. |
| 2048 | bool prevHadError = hadError; |
| 2049 | unsigned newStructuredIndex = FieldIndex; |
| 2050 | unsigned OldIndex = Index; |
| 2051 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2052 | |
| 2053 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2054 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2055 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2056 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2057 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2058 | IList->setInit(OldIndex, DIE); |
| 2059 | if (hadError && !prevHadError) { |
| 2060 | ++Field; |
| 2061 | ++FieldIndex; |
| 2062 | if (NextField) |
| 2063 | *NextField = Field; |
| 2064 | StructuredIndex = FieldIndex; |
| 2065 | return true; |
| 2066 | } |
| 2067 | } else { |
| 2068 | // Recurse to check later designated subobjects. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2069 | QualType FieldType = Field->getType(); |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2070 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2071 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2072 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2073 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2074 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2075 | FieldType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2076 | StructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2077 | true, false)) |
| 2078 | return true; |
| 2079 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2080 | |
| 2081 | // Find the position of the next field to be initialized in this |
| 2082 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2083 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2084 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2085 | |
| 2086 | // If this the first designator, our caller will continue checking |
| 2087 | // the rest of this struct/class/union subobject. |
| 2088 | if (IsFirstDesignator) { |
| 2089 | if (NextField) |
| 2090 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2091 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2092 | return false; |
| 2093 | } |
| 2094 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2095 | if (!FinishSubobjectInit) |
| 2096 | return false; |
| 2097 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2098 | // We've already initialized something in the union; we're done. |
| 2099 | if (RT->getDecl()->isUnion()) |
| 2100 | return hadError; |
| 2101 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2102 | // Check the remaining fields within this class/struct/union subobject. |
| 2103 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2104 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2105 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2106 | StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2107 | return hadError && !prevHadError; |
| 2108 | } |
| 2109 | |
| 2110 | // C99 6.7.8p6: |
| 2111 | // |
| 2112 | // If a designator has the form |
| 2113 | // |
| 2114 | // [ constant-expression ] |
| 2115 | // |
| 2116 | // then the current object (defined below) shall have array |
| 2117 | // type and the expression shall be an integer constant |
| 2118 | // expression. If the array is of unknown size, any |
| 2119 | // nonnegative value is valid. |
| 2120 | // |
| 2121 | // Additionally, cope with the GNU extension that permits |
| 2122 | // designators of the form |
| 2123 | // |
| 2124 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2125 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2126 | if (!AT) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2127 | if (!VerifyOnly) |
| 2128 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2129 | << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2130 | ++Index; |
| 2131 | return true; |
| 2132 | } |
| 2133 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2134 | Expr *IndexExpr = nullptr; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2135 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2136 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2137 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2138 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2139 | DesignatedEndIndex = DesignatedStartIndex; |
| 2140 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2141 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2142 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | DesignatedStartIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2144 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | DesignatedEndIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2146 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2147 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2148 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2149 | // Codegen can't handle evaluating array range designators that have side |
| 2150 | // effects, because we replicate the AST value for each initialized element. |
| 2151 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2152 | // elements with something that has a side effect, so codegen can emit an |
| 2153 | // "error unsupported" error instead of miscompiling the app. |
| 2154 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2155 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2156 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2159 | if (isa<ConstantArrayType>(AT)) { |
| 2160 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2161 | DesignatedStartIndex |
| 2162 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2163 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2164 | DesignatedEndIndex |
| 2165 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2166 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2167 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | ed0f916 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2168 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2169 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2170 | diag::err_array_designator_too_large) |
| 2171 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2172 | << IndexExpr->getSourceRange(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2173 | ++Index; |
| 2174 | return true; |
| 2175 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2176 | } else { |
| 2177 | // Make sure the bit-widths and signedness match. |
| 2178 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2179 | DesignatedEndIndex |
| 2180 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2181 | else if (DesignatedStartIndex.getBitWidth() < |
| 2182 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2183 | DesignatedStartIndex |
| 2184 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2185 | DesignatedStartIndex.setIsUnsigned(true); |
| 2186 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2187 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2188 | |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2189 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2190 | // We're modifying a string literal init; we have to decompose the string |
| 2191 | // so we can modify the individual characters. |
| 2192 | ASTContext &Context = SemaRef.Context; |
| 2193 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2194 | |
| 2195 | // Compute the character type |
| 2196 | QualType CharTy = AT->getElementType(); |
| 2197 | |
| 2198 | // Compute the type of the integer literals. |
| 2199 | QualType PromotedCharTy = CharTy; |
| 2200 | if (CharTy->isPromotableIntegerType()) |
| 2201 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2202 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2203 | |
| 2204 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2205 | // Get the length of the string. |
| 2206 | uint64_t StrLen = SL->getLength(); |
| 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, SL->getCodeUnit(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 | } else { |
| 2223 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2224 | std::string Str; |
| 2225 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2226 | |
| 2227 | // Get the length of the string. |
| 2228 | uint64_t StrLen = Str.size(); |
| 2229 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2230 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2231 | StructuredList->resizeInits(Context, StrLen); |
| 2232 | |
| 2233 | // Build a literal for each character in the string, and put them into |
| 2234 | // the init list. |
| 2235 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2236 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2237 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2238 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2239 | if (CharTy != PromotedCharTy) |
| 2240 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2241 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2242 | StructuredList->updateInit(Context, i, Init); |
| 2243 | } |
| 2244 | } |
| 2245 | } |
| 2246 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2247 | // Make sure that our non-designated initializer list has space |
| 2248 | // for a subobject corresponding to this array element. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2249 | if (!VerifyOnly && |
| 2250 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2252 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2253 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2254 | // Repeatedly perform subobject initializations in the range |
| 2255 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2256 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2257 | // Move to the next designator |
| 2258 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2259 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2260 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2261 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2262 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2263 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2264 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2265 | // Recurse to check later designated subobjects. |
| 2266 | QualType ElementType = AT->getElementType(); |
| 2267 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2268 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2269 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2270 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2271 | ElementType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2272 | StructuredList, ElementIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2273 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2274 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2275 | return true; |
| 2276 | |
| 2277 | // Move to the next index in the array that we'll be initializing. |
| 2278 | ++DesignatedStartIndex; |
| 2279 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2280 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2281 | |
| 2282 | // If this the first designator, our caller will continue checking |
| 2283 | // the rest of this array subobject. |
| 2284 | if (IsFirstDesignator) { |
| 2285 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2286 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2287 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2288 | return false; |
| 2289 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2290 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2291 | if (!FinishSubobjectInit) |
| 2292 | return false; |
| 2293 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2294 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2295 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2296 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2297 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2298 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2299 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2302 | // Get the structured initializer list for a subobject of type |
| 2303 | // @p CurrentObjectType. |
| 2304 | InitListExpr * |
| 2305 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2306 | QualType CurrentObjectType, |
| 2307 | InitListExpr *StructuredList, |
| 2308 | unsigned StructuredIndex, |
| 2309 | SourceRange InitRange) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2310 | if (VerifyOnly) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2311 | return nullptr; // No structured list in verification-only mode. |
| 2312 | Expr *ExistingInit = nullptr; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2313 | if (!StructuredList) |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2314 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2315 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2316 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2317 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2318 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2319 | return Result; |
| 2320 | |
| 2321 | if (ExistingInit) { |
| 2322 | // We are creating an initializer list that initializes the |
| 2323 | // subobjects of the current object, but there was already an |
| 2324 | // initialization that completely initialized the current |
| 2325 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2326 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2327 | // struct X { int a, b; }; |
| 2328 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2330 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2331 | // designated initializer re-initializes the whole |
| 2332 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2333 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2334 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2335 | << InitRange; |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2336 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2337 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2338 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2339 | << ExistingInit->getSourceRange(); |
| 2340 | } |
| 2341 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2342 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2343 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2344 | InitRange.getBegin(), None, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2345 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2346 | |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2347 | QualType ResultType = CurrentObjectType; |
| 2348 | if (!ResultType->isArrayType()) |
| 2349 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2350 | Result->setType(ResultType); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2351 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2352 | // Pre-allocate storage for the structured initializer list. |
| 2353 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2354 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2355 | bool GotNumInits = false; |
| 2356 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2357 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2358 | GotNumInits = true; |
| 2359 | } else if (Index < IList->getNumInits()) { |
| 2360 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2361 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2362 | GotNumInits = true; |
| 2363 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2366 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2367 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2368 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2369 | NumElements = CAType->getSize().getZExtValue(); |
| 2370 | // Simple heuristic so that we don't allocate a very large |
| 2371 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2372 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2373 | NumElements = 0; |
| 2374 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2375 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2376 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2377 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2378 | RecordDecl *RDecl = RType->getDecl(); |
| 2379 | if (RDecl->isUnion()) |
| 2380 | NumElements = 1; |
| 2381 | else |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 2382 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2385 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2386 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2387 | // Link this new initializer list into the structured initializer |
| 2388 | // lists. |
| 2389 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2390 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2391 | else { |
| 2392 | Result->setSyntacticForm(IList); |
| 2393 | SyntacticToSemantic[IList] = Result; |
| 2394 | } |
| 2395 | |
| 2396 | return Result; |
| 2397 | } |
| 2398 | |
| 2399 | /// Update the initializer at index @p StructuredIndex within the |
| 2400 | /// structured initializer list to the value @p expr. |
| 2401 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2402 | unsigned &StructuredIndex, |
| 2403 | Expr *expr) { |
| 2404 | // No structured initializer list to update |
| 2405 | if (!StructuredList) |
| 2406 | return; |
| 2407 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2408 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2409 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2410 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2411 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2412 | diag::warn_initializer_overrides) |
| 2413 | << expr->getSourceRange(); |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2414 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2415 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2416 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2417 | << PrevInit->getSourceRange(); |
| 2418 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2419 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2420 | ++StructuredIndex; |
| 2421 | } |
| 2422 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2423 | /// Check that the given Index expression is a valid array designator |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2424 | /// value. This is essentially just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2425 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2426 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2427 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2428 | /// added, on success. If everything went okay, Value will receive the |
| 2429 | /// value of the constant expression. |
| 2430 | static ExprResult |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2431 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2432 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2433 | |
| 2434 | // Make sure this is an integer constant expression. |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2435 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2436 | if (Result.isInvalid()) |
| 2437 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2438 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2439 | if (Value.isSigned() && Value.isNegative()) |
| 2440 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2441 | << Value.toString(10) << Index->getSourceRange(); |
| 2442 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2443 | Value.setIsUnsigned(true); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2444 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2447 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2448 | SourceLocation Loc, |
| 2449 | bool GNUSyntax, |
| 2450 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2451 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2452 | |
| 2453 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2454 | SmallVector<ASTDesignator, 32> Designators; |
| 2455 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2456 | |
| 2457 | // Build designators and check array designator expressions. |
| 2458 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2459 | const Designator &D = Desig.getDesignator(Idx); |
| 2460 | switch (D.getKind()) { |
| 2461 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2463 | D.getFieldLoc())); |
| 2464 | break; |
| 2465 | |
| 2466 | case Designator::ArrayDesignator: { |
| 2467 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2468 | llvm::APSInt IndexValue; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2469 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2470 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2471 | if (!Index) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2472 | Invalid = true; |
| 2473 | else { |
| 2474 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2476 | D.getRBracketLoc())); |
| 2477 | InitExpressions.push_back(Index); |
| 2478 | } |
| 2479 | break; |
| 2480 | } |
| 2481 | |
| 2482 | case Designator::ArrayRangeDesignator: { |
| 2483 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2484 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2485 | llvm::APSInt StartValue; |
| 2486 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2487 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2488 | StartIndex->isValueDependent(); |
| 2489 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2490 | EndIndex->isValueDependent(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2491 | if (!StartDependent) |
| 2492 | StartIndex = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2493 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2494 | if (!EndDependent) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2495 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2496 | |
| 2497 | if (!StartIndex || !EndIndex) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2498 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2499 | else { |
| 2500 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2501 | if (StartDependent || EndDependent) { |
| 2502 | // Nothing to compute. |
| 2503 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2504 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2505 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2506 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2507 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2508 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2509 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2510 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2511 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2512 | Invalid = true; |
| 2513 | } else { |
| 2514 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2515 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2516 | D.getEllipsisLoc(), |
| 2517 | D.getRBracketLoc())); |
| 2518 | InitExpressions.push_back(StartIndex); |
| 2519 | InitExpressions.push_back(EndIndex); |
| 2520 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2521 | } |
| 2522 | break; |
| 2523 | } |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | if (Invalid || Init.isInvalid()) |
| 2528 | return ExprError(); |
| 2529 | |
| 2530 | // Clear out the expressions within the designation. |
| 2531 | Desig.ClearExprs(*this); |
| 2532 | |
| 2533 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2534 | = DesignatedInitExpr::Create(Context, |
| 2535 | Designators.data(), Designators.size(), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2536 | InitExpressions, Loc, GNUSyntax, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2537 | Init.getAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2538 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2539 | if (!getLangOpts().C99) |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2540 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2541 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2542 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2543 | return DIE; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2544 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2545 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2546 | //===----------------------------------------------------------------------===// |
| 2547 | // Initialization entity |
| 2548 | //===----------------------------------------------------------------------===// |
| 2549 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2550 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2551 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2552 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2553 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2554 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2555 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2556 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2557 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2558 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2559 | Type = VT->getElementType(); |
| 2560 | } else { |
| 2561 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2562 | assert(CT && "Unexpected type"); |
| 2563 | Kind = EK_ComplexElement; |
| 2564 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2565 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2568 | InitializedEntity |
| 2569 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 2570 | const CXXBaseSpecifier *Base, |
| 2571 | bool IsInheritedVirtualBase) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2572 | InitializedEntity Result; |
| 2573 | Result.Kind = EK_Base; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2574 | Result.Parent = nullptr; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2575 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2576 | if (IsInheritedVirtualBase) |
| 2577 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2578 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2579 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2580 | return Result; |
| 2581 | } |
| 2582 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2583 | DeclarationName InitializedEntity::getName() const { |
| 2584 | switch (getKind()) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2585 | case EK_Parameter: |
| 2586 | case EK_Parameter_CF_Audited: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2587 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2588 | return (D ? D->getDeclName() : DeclarationName()); |
| 2589 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2590 | |
| 2591 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2592 | case EK_Member: |
| 2593 | return VariableOrMember->getDeclName(); |
| 2594 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2595 | case EK_LambdaCapture: |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 2596 | return DeclarationName(Capture.VarID); |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2597 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2598 | case EK_Result: |
| 2599 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2600 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2601 | case EK_Temporary: |
| 2602 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2603 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2604 | case EK_ArrayElement: |
| 2605 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2606 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2607 | case EK_BlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2608 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2609 | case EK_RelatedResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2610 | return DeclarationName(); |
| 2611 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2612 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2613 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2616 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2617 | switch (getKind()) { |
| 2618 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2619 | case EK_Member: |
| 2620 | return VariableOrMember; |
| 2621 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2622 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2623 | case EK_Parameter_CF_Audited: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2624 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2625 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2626 | case EK_Result: |
| 2627 | case EK_Exception: |
| 2628 | case EK_New: |
| 2629 | case EK_Temporary: |
| 2630 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2631 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2632 | case EK_ArrayElement: |
| 2633 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2634 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2635 | case EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2636 | case EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2637 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2638 | case EK_RelatedResult: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2639 | return nullptr; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2640 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2641 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2642 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2645 | bool InitializedEntity::allowsNRVO() const { |
| 2646 | switch (getKind()) { |
| 2647 | case EK_Result: |
| 2648 | case EK_Exception: |
| 2649 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2650 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2651 | case EK_Variable: |
| 2652 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2653 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2654 | case EK_Member: |
| 2655 | case EK_New: |
| 2656 | case EK_Temporary: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2657 | case EK_CompoundLiteralInit: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2658 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2659 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2660 | case EK_ArrayElement: |
| 2661 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2662 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2663 | case EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2664 | case EK_LambdaCapture: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2665 | case EK_RelatedResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2666 | break; |
| 2667 | } |
| 2668 | |
| 2669 | return false; |
| 2670 | } |
| 2671 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2672 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2673 | assert(getParent() != this); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2674 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 2675 | for (unsigned I = 0; I != Depth; ++I) |
| 2676 | OS << "`-"; |
| 2677 | |
| 2678 | switch (getKind()) { |
| 2679 | case EK_Variable: OS << "Variable"; break; |
| 2680 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2681 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 2682 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2683 | case EK_Result: OS << "Result"; break; |
| 2684 | case EK_Exception: OS << "Exception"; break; |
| 2685 | case EK_Member: OS << "Member"; break; |
| 2686 | case EK_New: OS << "New"; break; |
| 2687 | case EK_Temporary: OS << "Temporary"; break; |
| 2688 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2689 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2690 | case EK_Base: OS << "Base"; break; |
| 2691 | case EK_Delegating: OS << "Delegating"; break; |
| 2692 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 2693 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 2694 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 2695 | case EK_BlockElement: OS << "Block"; break; |
| 2696 | case EK_LambdaCapture: |
| 2697 | OS << "LambdaCapture "; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 2698 | OS << DeclarationName(Capture.VarID); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2699 | break; |
| 2700 | } |
| 2701 | |
| 2702 | if (Decl *D = getDecl()) { |
| 2703 | OS << " "; |
| 2704 | cast<NamedDecl>(D)->printQualifiedName(OS); |
| 2705 | } |
| 2706 | |
| 2707 | OS << " '" << getType().getAsString() << "'\n"; |
| 2708 | |
| 2709 | return Depth + 1; |
| 2710 | } |
| 2711 | |
| 2712 | void InitializedEntity::dump() const { |
| 2713 | dumpImpl(llvm::errs()); |
| 2714 | } |
| 2715 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2716 | //===----------------------------------------------------------------------===// |
| 2717 | // Initialization sequence |
| 2718 | //===----------------------------------------------------------------------===// |
| 2719 | |
| 2720 | void InitializationSequence::Step::Destroy() { |
| 2721 | switch (Kind) { |
| 2722 | case SK_ResolveAddressOfOverloadedFunction: |
| 2723 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2724 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2725 | case SK_CastDerivedToBaseLValue: |
| 2726 | case SK_BindReference: |
| 2727 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2728 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2729 | case SK_UserConversion: |
| 2730 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2731 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2732 | case SK_QualificationConversionLValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 2733 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2734 | case SK_LValueToRValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2735 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2736 | case SK_UnwrapInitList: |
| 2737 | case SK_RewrapInitList: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2738 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 2739 | case SK_ConstructorInitializationFromList: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2740 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2741 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2742 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2743 | case SK_ObjCObjectConversion: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2744 | case SK_ArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2745 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2746 | case SK_PassByIndirectCopyRestore: |
| 2747 | case SK_PassByIndirectRestore: |
| 2748 | case SK_ProduceObjCObject: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2749 | case SK_StdInitializerList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 2750 | case SK_StdInitializerListConstructorCall: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2751 | case SK_OCLSamplerInit: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2752 | case SK_OCLZeroEvent: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2753 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2754 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2755 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2756 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2757 | delete ICS; |
| 2758 | } |
| 2759 | } |
| 2760 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2761 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2762 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2766 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2767 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2768 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2769 | switch (getFailureKind()) { |
| 2770 | case FK_TooManyInitsForReference: |
| 2771 | case FK_ArrayNeedsInitList: |
| 2772 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 2773 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 2774 | case FK_NarrowStringIntoWideCharArray: |
| 2775 | case FK_WideStringIntoCharArray: |
| 2776 | case FK_IncompatWideStringIntoWideChar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2777 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2778 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2779 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2780 | case FK_RValueReferenceBindingToLValue: |
| 2781 | case FK_ReferenceInitDropsQualifiers: |
| 2782 | case FK_ReferenceInitFailed: |
| 2783 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2784 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2785 | case FK_TooManyInitsForScalar: |
| 2786 | case FK_ReferenceBindingToInitList: |
| 2787 | case FK_InitListBadDestinationType: |
| 2788 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2789 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2790 | case FK_ArrayTypeMismatch: |
| 2791 | case FK_NonConstantArrayInit: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2792 | case FK_ListInitializationFailed: |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2793 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2794 | case FK_PlaceholderType: |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2795 | case FK_ExplicitConstructor: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2796 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2797 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2798 | case FK_ReferenceInitOverloadFailed: |
| 2799 | case FK_UserConversionOverloadFailed: |
| 2800 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2801 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2802 | return FailedOverloadResult == OR_Ambiguous; |
| 2803 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2804 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2805 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2808 | bool InitializationSequence::isConstructorInitialization() const { |
| 2809 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2810 | } |
| 2811 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2812 | void |
| 2813 | InitializationSequence |
| 2814 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2815 | DeclAccessPair Found, |
| 2816 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2817 | Step S; |
| 2818 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2819 | S.Type = Function->getType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2820 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2821 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2822 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2823 | Steps.push_back(S); |
| 2824 | } |
| 2825 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2826 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2827 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2828 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2829 | switch (VK) { |
| 2830 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2831 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2832 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2833 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2834 | S.Type = BaseType; |
| 2835 | Steps.push_back(S); |
| 2836 | } |
| 2837 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2838 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2839 | bool BindingTemporary) { |
| 2840 | Step S; |
| 2841 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2842 | S.Type = T; |
| 2843 | Steps.push_back(S); |
| 2844 | } |
| 2845 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2846 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2847 | Step S; |
| 2848 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2849 | S.Type = T; |
| 2850 | Steps.push_back(S); |
| 2851 | } |
| 2852 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2853 | void |
| 2854 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2855 | DeclAccessPair FoundDecl, |
| 2856 | QualType T, |
| 2857 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2858 | Step S; |
| 2859 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2860 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2861 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2862 | S.Function.Function = Function; |
| 2863 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2864 | Steps.push_back(S); |
| 2865 | } |
| 2866 | |
| 2867 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2868 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2869 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2870 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2871 | switch (VK) { |
| 2872 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2873 | S.Kind = SK_QualificationConversionRValue; |
| 2874 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2875 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2876 | S.Kind = SK_QualificationConversionXValue; |
| 2877 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2878 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2879 | S.Kind = SK_QualificationConversionLValue; |
| 2880 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2881 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2882 | S.Type = Ty; |
| 2883 | Steps.push_back(S); |
| 2884 | } |
| 2885 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 2886 | void InitializationSequence::AddAtomicConversionStep(QualType Ty) { |
| 2887 | Step S; |
| 2888 | S.Kind = SK_AtomicConversion; |
| 2889 | S.Type = Ty; |
| 2890 | Steps.push_back(S); |
| 2891 | } |
| 2892 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2893 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 2894 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 2895 | |
| 2896 | Step S; |
| 2897 | S.Kind = SK_LValueToRValue; |
| 2898 | S.Type = Ty; |
| 2899 | Steps.push_back(S); |
| 2900 | } |
| 2901 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2902 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2903 | const ImplicitConversionSequence &ICS, QualType T, |
| 2904 | bool TopLevelOfInitList) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2905 | Step S; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2906 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 2907 | : SK_ConversionSequence; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2908 | S.Type = T; |
| 2909 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2910 | Steps.push_back(S); |
| 2911 | } |
| 2912 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2913 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2914 | Step S; |
| 2915 | S.Kind = SK_ListInitialization; |
| 2916 | S.Type = T; |
| 2917 | Steps.push_back(S); |
| 2918 | } |
| 2919 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2920 | void |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2921 | InitializationSequence |
| 2922 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2923 | AccessSpecifier Access, |
| 2924 | QualType T, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2925 | bool HadMultipleCandidates, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2926 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2927 | Step S; |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 2928 | S.Kind = FromInitList ? AsInitList ? SK_StdInitializerListConstructorCall |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 2929 | : SK_ConstructorInitializationFromList |
| 2930 | : SK_ConstructorInitialization; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2931 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2932 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2933 | S.Function.Function = Constructor; |
| 2934 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2935 | Steps.push_back(S); |
| 2936 | } |
| 2937 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2938 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2939 | Step S; |
| 2940 | S.Kind = SK_ZeroInitialization; |
| 2941 | S.Type = T; |
| 2942 | Steps.push_back(S); |
| 2943 | } |
| 2944 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2945 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2946 | Step S; |
| 2947 | S.Kind = SK_CAssignment; |
| 2948 | S.Type = T; |
| 2949 | Steps.push_back(S); |
| 2950 | } |
| 2951 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2952 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2953 | Step S; |
| 2954 | S.Kind = SK_StringInit; |
| 2955 | S.Type = T; |
| 2956 | Steps.push_back(S); |
| 2957 | } |
| 2958 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2959 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2960 | Step S; |
| 2961 | S.Kind = SK_ObjCObjectConversion; |
| 2962 | S.Type = T; |
| 2963 | Steps.push_back(S); |
| 2964 | } |
| 2965 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2966 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2967 | Step S; |
| 2968 | S.Kind = SK_ArrayInit; |
| 2969 | S.Type = T; |
| 2970 | Steps.push_back(S); |
| 2971 | } |
| 2972 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2973 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2974 | Step S; |
| 2975 | S.Kind = SK_ParenthesizedArrayInit; |
| 2976 | S.Type = T; |
| 2977 | Steps.push_back(S); |
| 2978 | } |
| 2979 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2980 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2981 | bool shouldCopy) { |
| 2982 | Step s; |
| 2983 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2984 | : SK_PassByIndirectRestore); |
| 2985 | s.Type = type; |
| 2986 | Steps.push_back(s); |
| 2987 | } |
| 2988 | |
| 2989 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2990 | Step S; |
| 2991 | S.Kind = SK_ProduceObjCObject; |
| 2992 | S.Type = T; |
| 2993 | Steps.push_back(S); |
| 2994 | } |
| 2995 | |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2996 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2997 | Step S; |
| 2998 | S.Kind = SK_StdInitializerList; |
| 2999 | S.Type = T; |
| 3000 | Steps.push_back(S); |
| 3001 | } |
| 3002 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3003 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 3004 | Step S; |
| 3005 | S.Kind = SK_OCLSamplerInit; |
| 3006 | S.Type = T; |
| 3007 | Steps.push_back(S); |
| 3008 | } |
| 3009 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3010 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 3011 | Step S; |
| 3012 | S.Kind = SK_OCLZeroEvent; |
| 3013 | S.Type = T; |
| 3014 | Steps.push_back(S); |
| 3015 | } |
| 3016 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3017 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 3018 | InitListExpr *Syntactic) { |
| 3019 | assert(Syntactic->getNumInits() == 1 && |
| 3020 | "Can only rewrap trivial init lists."); |
| 3021 | Step S; |
| 3022 | S.Kind = SK_UnwrapInitList; |
| 3023 | S.Type = Syntactic->getInit(0)->getType(); |
| 3024 | Steps.insert(Steps.begin(), S); |
| 3025 | |
| 3026 | S.Kind = SK_RewrapInitList; |
| 3027 | S.Type = T; |
| 3028 | S.WrappingSyntacticList = Syntactic; |
| 3029 | Steps.push_back(S); |
| 3030 | } |
| 3031 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3032 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3033 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3034 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3035 | this->Failure = Failure; |
| 3036 | this->FailedOverloadResult = Result; |
| 3037 | } |
| 3038 | |
| 3039 | //===----------------------------------------------------------------------===// |
| 3040 | // Attempt initialization |
| 3041 | //===----------------------------------------------------------------------===// |
| 3042 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3043 | static void MaybeProduceObjCObject(Sema &S, |
| 3044 | InitializationSequence &Sequence, |
| 3045 | const InitializedEntity &Entity) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3046 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3047 | |
| 3048 | /// When initializing a parameter, produce the value if it's marked |
| 3049 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3050 | if (Entity.isParameterKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3051 | if (!Entity.isParameterConsumed()) |
| 3052 | return; |
| 3053 | |
| 3054 | assert(Entity.getType()->isObjCRetainableType() && |
| 3055 | "consuming an object of unretainable type?"); |
| 3056 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3057 | |
| 3058 | /// When initializing a return value, if the return type is a |
| 3059 | /// retainable type, then returns need to immediately retain the |
| 3060 | /// object. If an autorelease is required, it will be done at the |
| 3061 | /// last instant. |
| 3062 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 3063 | if (!Entity.getType()->isObjCRetainableType()) |
| 3064 | return; |
| 3065 | |
| 3066 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3067 | } |
| 3068 | } |
| 3069 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3070 | static void TryListInitialization(Sema &S, |
| 3071 | const InitializedEntity &Entity, |
| 3072 | const InitializationKind &Kind, |
| 3073 | InitListExpr *InitList, |
| 3074 | InitializationSequence &Sequence); |
| 3075 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3076 | /// \brief When initializing from init list via constructor, handle |
| 3077 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3078 | /// |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3079 | /// \return true if we have handled initialization of an object of type |
| 3080 | /// std::initializer_list<T>, false otherwise. |
| 3081 | static bool TryInitializerListConstruction(Sema &S, |
| 3082 | InitListExpr *List, |
| 3083 | QualType DestType, |
| 3084 | InitializationSequence &Sequence) { |
| 3085 | QualType E; |
| 3086 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3087 | return false; |
| 3088 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3089 | if (S.RequireCompleteType(List->getExprLoc(), E, 0)) { |
| 3090 | Sequence.setIncompleteTypeFailure(E); |
| 3091 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3092 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3093 | |
| 3094 | // Try initializing a temporary array from the init list. |
| 3095 | QualType ArrayType = S.Context.getConstantArrayType( |
| 3096 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 3097 | List->getNumInits()), |
| 3098 | clang::ArrayType::Normal, 0); |
| 3099 | InitializedEntity HiddenArray = |
| 3100 | InitializedEntity::InitializeTemporary(ArrayType); |
| 3101 | InitializationKind Kind = |
| 3102 | InitializationKind::CreateDirectList(List->getExprLoc()); |
| 3103 | TryListInitialization(S, HiddenArray, Kind, List, Sequence); |
| 3104 | if (Sequence) |
| 3105 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3106 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3109 | static OverloadingResult |
| 3110 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3111 | MultiExprArg Args, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3112 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3113 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3114 | OverloadCandidateSet::iterator &Best, |
| 3115 | bool CopyInitializing, bool AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame^] | 3116 | bool OnlyListConstructors, bool IsListInit) { |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3117 | CandidateSet.clear(); |
| 3118 | |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3119 | for (ArrayRef<NamedDecl *>::iterator |
| 3120 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3121 | NamedDecl *D = *Con; |
| 3122 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3123 | bool SuppressUserConversions = false; |
| 3124 | |
| 3125 | // Find the constructor (which may be a template). |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3126 | CXXConstructorDecl *Constructor = nullptr; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3127 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 3128 | if (ConstructorTmpl) |
| 3129 | Constructor = cast<CXXConstructorDecl>( |
| 3130 | ConstructorTmpl->getTemplatedDecl()); |
| 3131 | else { |
| 3132 | Constructor = cast<CXXConstructorDecl>(D); |
| 3133 | |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3134 | // C++11 [over.best.ics]p4: |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3135 | // ... and the constructor or user-defined conversion function is a |
| 3136 | // candidate by |
| 3137 | // — 13.3.1.3, when the argument is the temporary in the second step |
| 3138 | // of a class copy-initialization, or |
| 3139 | // — 13.3.1.4, 13.3.1.5, or 13.3.1.6 (in all cases), |
| 3140 | // user-defined conversion sequences are not considered. |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame^] | 3141 | // FIXME: This breaks backward compatibility, e.g. PR12117. As a |
| 3142 | // temporary fix, let's re-instate the third bullet above until |
| 3143 | // there is a resolution in the standard, i.e., |
| 3144 | // - 13.3.1.7 when the initializer list has exactly one element that is |
| 3145 | // itself an initializer list and a conversion to some class X or |
| 3146 | // reference to (possibly cv-qualified) X is considered for the first |
| 3147 | // parameter of a constructor of X. |
| 3148 | if ((CopyInitializing || |
| 3149 | (IsListInit && Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
| 3150 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3151 | SuppressUserConversions = true; |
| 3152 | } |
| 3153 | |
| 3154 | if (!Constructor->isInvalidDecl() && |
| 3155 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3156 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3157 | if (ConstructorTmpl) |
| 3158 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3159 | /*ExplicitArgs*/ nullptr, Args, |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3160 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3161 | else { |
| 3162 | // C++ [over.match.copy]p1: |
| 3163 | // - When initializing a temporary to be bound to the first parameter |
| 3164 | // of a constructor that takes a reference to possibly cv-qualified |
| 3165 | // T as its first argument, called with a single argument in the |
| 3166 | // context of direct-initialization, explicit conversion functions |
| 3167 | // are also considered. |
| 3168 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3169 | Args.size() == 1 && |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3170 | Constructor->isCopyOrMoveConstructor(); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3171 | S.AddOverloadCandidate(Constructor, FoundDecl, Args, CandidateSet, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3172 | SuppressUserConversions, |
| 3173 | /*PartialOverloading=*/false, |
| 3174 | /*AllowExplicit=*/AllowExplicitConv); |
| 3175 | } |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | // Perform overload resolution and return the result. |
| 3180 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3181 | } |
| 3182 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3183 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3184 | /// enumerates the constructors of the initialized entity and performs overload |
| 3185 | /// resolution to select the best. |
NAKAMURA Takumi | ffcc98a | 2015-02-05 23:12:13 +0000 | [diff] [blame] | 3186 | /// \param IsListInit Is this list-initialization? |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3187 | /// \param IsInitListCopy Is this non-list-initialization resulting from a |
| 3188 | /// list-initialization from {x} where x is the same |
| 3189 | /// type as the entity? |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3190 | static void TryConstructorInitialization(Sema &S, |
| 3191 | const InitializedEntity &Entity, |
| 3192 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3193 | MultiExprArg Args, QualType DestType, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3194 | InitializationSequence &Sequence, |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3195 | bool IsListInit = false, |
| 3196 | bool IsInitListCopy = false) { |
| 3197 | assert((!IsListInit || (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
| 3198 | "IsListInit must come with a single initializer list argument."); |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3199 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3200 | // The type we're constructing needs to be complete. |
| 3201 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3202 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3203 | return; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3207 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3208 | CXXRecordDecl *DestRecordDecl |
| 3209 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3210 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3211 | // Build the candidate set directly in the initialization sequence |
| 3212 | // structure, so that it will persist if we fail. |
| 3213 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3214 | |
| 3215 | // Determine whether we are allowed to call explicit constructors or |
| 3216 | // explicit conversion operators. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3217 | bool AllowExplicit = Kind.AllowExplicit() || IsListInit; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3218 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3219 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3220 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3221 | // applicable constructors are enumerated, and the best one is chosen |
| 3222 | // through overload resolution. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3223 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3224 | // The container holding the constructors can under certain conditions |
| 3225 | // be changed while iterating (e.g. because of deserialization). |
| 3226 | // To be safe we copy the lookup results to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3227 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3228 | |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3229 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3230 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3231 | bool AsInitializerList = false; |
| 3232 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3233 | // C++11 [over.match.list]p1, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3234 | // When objects of non-aggregate type T are list-initialized, such that |
| 3235 | // 8.5.4 [dcl.init.list] specifies that overload resolution is performed |
| 3236 | // according to the rules in this section, overload resolution selects |
| 3237 | // the constructor in two phases: |
| 3238 | // |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3239 | // - Initially, the candidate functions are the initializer-list |
| 3240 | // constructors of the class T and the argument list consists of the |
| 3241 | // initializer list as a single argument. |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3242 | if (IsListInit) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3243 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3244 | AsInitializerList = true; |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3245 | |
| 3246 | // If the initializer list has no elements and T has a default constructor, |
| 3247 | // the first phase is omitted. |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3248 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3249 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3250 | CandidateSet, Ctors, Best, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3251 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame^] | 3252 | /*OnlyListConstructor=*/true, |
| 3253 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3254 | |
| 3255 | // Time to unwrap the init list. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3256 | Args = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | // C++11 [over.match.list]p1: |
| 3260 | // - If no viable initializer-list constructor is found, overload resolution |
| 3261 | // is performed again, where the candidate functions are all the |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3262 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3263 | // elements of the initializer list. |
| 3264 | if (Result == OR_No_Viable_Function) { |
| 3265 | AsInitializerList = false; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3266 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3267 | CandidateSet, Ctors, Best, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3268 | CopyInitialization, AllowExplicit, |
Larisse Voufo | bcf327a | 2015-02-10 02:20:14 +0000 | [diff] [blame^] | 3269 | /*OnlyListConstructors=*/false, |
| 3270 | IsListInit); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3271 | } |
| 3272 | if (Result) { |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3273 | Sequence.SetOverloadFailure(IsListInit ? |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3274 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3275 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3276 | Result); |
| 3277 | return; |
| 3278 | } |
| 3279 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3280 | // C++11 [dcl.init]p6: |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3281 | // If a program calls for the default initialization of an object |
| 3282 | // of a const-qualified type T, T shall be a class type with a |
| 3283 | // user-provided default constructor. |
| 3284 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3285 | Entity.getType().isConstQualified() && |
Aaron Ballman | 899b9c6 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 3286 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3287 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3288 | return; |
| 3289 | } |
| 3290 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3291 | // C++11 [over.match.list]p1: |
| 3292 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3293 | // initializer is ill-formed. |
| 3294 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3295 | if (IsListInit && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3296 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3297 | return; |
| 3298 | } |
| 3299 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3300 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3301 | // subsumed by the initialization. |
| 3302 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3303 | Sequence.AddConstructorInitializationStep( |
| 3304 | CtorDecl, Best->FoundDecl.getAccess(), DestType, HadMultipleCandidates, |
| 3305 | IsListInit | IsInitListCopy, AsInitializerList); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3308 | static bool |
| 3309 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3310 | Expr *Initializer, |
| 3311 | QualType &SourceType, |
| 3312 | QualType &UnqualifiedSourceType, |
| 3313 | QualType UnqualifiedTargetType, |
| 3314 | InitializationSequence &Sequence) { |
| 3315 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3316 | S.Context.OverloadTy) { |
| 3317 | DeclAccessPair Found; |
| 3318 | bool HadMultipleCandidates = false; |
| 3319 | if (FunctionDecl *Fn |
| 3320 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3321 | UnqualifiedTargetType, |
| 3322 | false, Found, |
| 3323 | &HadMultipleCandidates)) { |
| 3324 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3325 | HadMultipleCandidates); |
| 3326 | SourceType = Fn->getType(); |
| 3327 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3328 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3329 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3330 | return true; |
| 3331 | } |
| 3332 | } |
| 3333 | return false; |
| 3334 | } |
| 3335 | |
| 3336 | static void TryReferenceInitializationCore(Sema &S, |
| 3337 | const InitializedEntity &Entity, |
| 3338 | const InitializationKind &Kind, |
| 3339 | Expr *Initializer, |
| 3340 | QualType cv1T1, QualType T1, |
| 3341 | Qualifiers T1Quals, |
| 3342 | QualType cv2T2, QualType T2, |
| 3343 | Qualifiers T2Quals, |
| 3344 | InitializationSequence &Sequence); |
| 3345 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3346 | static void TryValueInitialization(Sema &S, |
| 3347 | const InitializedEntity &Entity, |
| 3348 | const InitializationKind &Kind, |
| 3349 | InitializationSequence &Sequence, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3350 | InitListExpr *InitList = nullptr); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3351 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3352 | /// \brief Attempt list initialization of a reference. |
| 3353 | static void TryReferenceListInitialization(Sema &S, |
| 3354 | const InitializedEntity &Entity, |
| 3355 | const InitializationKind &Kind, |
| 3356 | InitListExpr *InitList, |
Richard Smith | faadef7 | 2013-06-08 00:02:08 +0000 | [diff] [blame] | 3357 | InitializationSequence &Sequence) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3358 | // First, catch C++03 where this isn't possible. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3359 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3360 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3361 | return; |
| 3362 | } |
| 3363 | |
| 3364 | QualType DestType = Entity.getType(); |
| 3365 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3366 | Qualifiers T1Quals; |
| 3367 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3368 | |
| 3369 | // Reference initialization via an initializer list works thus: |
| 3370 | // If the initializer list consists of a single element that is |
| 3371 | // reference-related to the referenced type, bind directly to that element |
| 3372 | // (possibly creating temporaries). |
| 3373 | // Otherwise, initialize a temporary with the initializer list and |
| 3374 | // bind to that. |
| 3375 | if (InitList->getNumInits() == 1) { |
| 3376 | Expr *Initializer = InitList->getInit(0); |
| 3377 | QualType cv2T2 = Initializer->getType(); |
| 3378 | Qualifiers T2Quals; |
| 3379 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3380 | |
| 3381 | // If this fails, creating a temporary wouldn't work either. |
| 3382 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3383 | T1, Sequence)) |
| 3384 | return; |
| 3385 | |
| 3386 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3387 | bool dummy1, dummy2, dummy3; |
| 3388 | Sema::ReferenceCompareResult RefRelationship |
| 3389 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3390 | dummy2, dummy3); |
| 3391 | if (RefRelationship >= Sema::Ref_Related) { |
| 3392 | // Try to bind the reference here. |
| 3393 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3394 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3395 | if (Sequence) |
| 3396 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3397 | return; |
| 3398 | } |
Richard Smith | 03d9393 | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3399 | |
| 3400 | // Update the initializer if we've resolved an overloaded function. |
| 3401 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3402 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | // Not reference-related. Create a temporary and bind to that. |
| 3406 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3407 | |
| 3408 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3409 | if (Sequence) { |
| 3410 | if (DestType->isRValueReferenceType() || |
| 3411 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3412 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3413 | else |
| 3414 | Sequence.SetFailed( |
| 3415 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3416 | } |
| 3417 | } |
| 3418 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3419 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3420 | static void TryListInitialization(Sema &S, |
| 3421 | const InitializedEntity &Entity, |
| 3422 | const InitializationKind &Kind, |
| 3423 | InitListExpr *InitList, |
| 3424 | InitializationSequence &Sequence) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3425 | QualType DestType = Entity.getType(); |
| 3426 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3427 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3428 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3429 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3430 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3431 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3432 | return; |
| 3433 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3434 | if (DestType->isReferenceType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3435 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3436 | return; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3437 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3438 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3439 | if (DestType->isRecordType() && |
| 3440 | S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
| 3441 | Sequence.setIncompleteTypeFailure(DestType); |
| 3442 | return; |
| 3443 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3444 | |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3445 | // C++11 [dcl.init.list]p3, per DR1467: |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3446 | // - If T is a class type and the initializer list has a single element of |
| 3447 | // type cv U, where U is T or a class derived from T, the object is |
| 3448 | // initialized from that element (by copy-initialization for |
| 3449 | // copy-list-initialization, or by direct-initialization for |
| 3450 | // direct-list-initialization). |
| 3451 | // - Otherwise, if T is a character array and the initializer list has a |
| 3452 | // single element that is an appropriately-typed string literal |
| 3453 | // (8.5.2 [dcl.init.string]), initialization is performed as described |
| 3454 | // in that section. |
Larisse Voufo | 19d0867 | 2015-01-27 18:47:05 +0000 | [diff] [blame] | 3455 | // - Otherwise, if T is an aggregate, [...] (continue below). |
| 3456 | if (S.getLangOpts().CPlusPlus11 && InitList->getNumInits() == 1) { |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3457 | if (DestType->isRecordType()) { |
| 3458 | QualType InitType = InitList->getInit(0)->getType(); |
| 3459 | if (S.Context.hasSameUnqualifiedType(InitType, DestType) || |
| 3460 | S.IsDerivedFrom(InitType, DestType)) { |
Richard Smith | ed83ebd | 2015-02-05 07:02:11 +0000 | [diff] [blame] | 3461 | Expr *InitAsExpr = InitList->getInit(0); |
| 3462 | TryConstructorInitialization(S, Entity, Kind, InitAsExpr, DestType, |
| 3463 | Sequence, /*InitListSyntax*/ false, |
| 3464 | /*IsInitListCopy*/ true); |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3465 | return; |
| 3466 | } |
| 3467 | } |
| 3468 | if (const ArrayType *DestAT = S.Context.getAsArrayType(DestType)) { |
| 3469 | Expr *SubInit[1] = {InitList->getInit(0)}; |
| 3470 | if (!isa<VariableArrayType>(DestAT) && |
| 3471 | IsStringInit(SubInit[0], DestAT, S.Context) == SIF_None) { |
| 3472 | InitializationKind SubKind = |
| 3473 | Kind.getKind() == InitializationKind::IK_DirectList |
| 3474 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 3475 | InitList->getLBraceLoc(), |
| 3476 | InitList->getRBraceLoc()) |
| 3477 | : Kind; |
| 3478 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 3479 | /*TopLevelOfInitList*/ true); |
| 3480 | |
| 3481 | // TryStringLiteralInitialization() (in InitializeFrom()) will fail if |
| 3482 | // the element is not an appropriately-typed string literal, in which |
| 3483 | // case we should proceed as in C++11 (below). |
| 3484 | if (Sequence) { |
| 3485 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 3486 | return; |
| 3487 | } |
| 3488 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3489 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3490 | } |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3491 | |
| 3492 | // C++11 [dcl.init.list]p3: |
| 3493 | // - If T is an aggregate, aggregate initialization is performed. |
| 3494 | if (DestType->isRecordType() && !DestType->isAggregateType()) { |
| 3495 | if (S.getLangOpts().CPlusPlus11) { |
| 3496 | // - Otherwise, if the initializer list has no elements and T is a |
| 3497 | // class type with a default constructor, the object is |
| 3498 | // value-initialized. |
| 3499 | if (InitList->getNumInits() == 0) { |
| 3500 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
| 3501 | if (RD->hasDefaultConstructor()) { |
| 3502 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3503 | return; |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3508 | // an initializer_list object constructed [...] |
| 3509 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3510 | return; |
| 3511 | |
| 3512 | // - Otherwise, if T is a class type, constructors are considered. |
| 3513 | Expr *InitListAsExpr = InitList; |
| 3514 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
| 3515 | Sequence, /*InitListSyntax*/ true); |
| 3516 | } else |
| 3517 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 3518 | return; |
| 3519 | } |
| 3520 | |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 3521 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
| 3522 | InitList->getNumInits() == 1 && |
| 3523 | InitList->getInit(0)->getType()->isRecordType()) { |
| 3524 | // - Otherwise, if the initializer list has a single element of type E |
| 3525 | // [...references are handled above...], the object or reference is |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 3526 | // initialized from that element (by copy-initialization for |
| 3527 | // copy-list-initialization, or by direct-initialization for |
| 3528 | // direct-list-initialization); if a narrowing conversion is required |
| 3529 | // to convert the element to T, the program is ill-formed. |
| 3530 | // |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 3531 | // Per core-24034, this is direct-initialization if we were performing |
| 3532 | // direct-list-initialization and copy-initialization otherwise. |
| 3533 | // We can't use InitListChecker for this, because it always performs |
| 3534 | // copy-initialization. This only matters if we might use an 'explicit' |
| 3535 | // conversion operator, so we only need to handle the cases where the source |
| 3536 | // is of record type. |
| 3537 | InitializationKind SubKind = |
| 3538 | Kind.getKind() == InitializationKind::IK_DirectList |
| 3539 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 3540 | InitList->getLBraceLoc(), |
| 3541 | InitList->getRBraceLoc()) |
| 3542 | : Kind; |
| 3543 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 3544 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 3545 | /*TopLevelOfInitList*/true); |
| 3546 | if (Sequence) |
| 3547 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 3548 | return; |
| 3549 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3550 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3551 | InitListChecker CheckInitList(S, Entity, InitList, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 3552 | DestType, /*VerifyOnly=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3553 | if (CheckInitList.HadError()) { |
| 3554 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3555 | return; |
| 3556 | } |
| 3557 | |
| 3558 | // Add the list initialization step with the built init list. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3559 | Sequence.AddListInitializationStep(DestType); |
| 3560 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3561 | |
| 3562 | /// \brief Try a reference initialization that involves calling a conversion |
| 3563 | /// function. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3564 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3565 | const InitializedEntity &Entity, |
| 3566 | const InitializationKind &Kind, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3567 | Expr *Initializer, |
| 3568 | bool AllowRValues, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3569 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3570 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3571 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3572 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3573 | QualType cv2T2 = Initializer->getType(); |
| 3574 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3575 | |
| 3576 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3577 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3578 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3579 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3580 | T1, T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3581 | ObjCConversion, |
| 3582 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3583 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3584 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3585 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3586 | (void)ObjCLifetimeConversion; |
| 3587 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3588 | // Build the candidate set directly in the initialization sequence |
| 3589 | // structure, so that it will persist if we fail. |
| 3590 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3591 | CandidateSet.clear(); |
| 3592 | |
| 3593 | // Determine whether we are allowed to call explicit constructors or |
| 3594 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3595 | bool AllowExplicit = Kind.AllowExplicit(); |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3596 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 3597 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3598 | const RecordType *T1RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3599 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3600 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3601 | // The type we're converting to is a class type. Enumerate its constructors |
| 3602 | // to see if there is a suitable conversion. |
| 3603 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3604 | |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3605 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3606 | // The container holding the constructors can under certain conditions |
| 3607 | // be changed while iterating (e.g. because of deserialization). |
| 3608 | // To be safe we copy the lookup results to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3609 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3610 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3611 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3612 | NamedDecl *D = *CI; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3613 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3614 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3615 | // Find the constructor (which may be a template). |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3616 | CXXConstructorDecl *Constructor = nullptr; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3617 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3618 | if (ConstructorTmpl) |
| 3619 | Constructor = cast<CXXConstructorDecl>( |
| 3620 | ConstructorTmpl->getTemplatedDecl()); |
| 3621 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3622 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3623 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3624 | if (!Constructor->isInvalidDecl() && |
| 3625 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3626 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3627 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3628 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3629 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3630 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3631 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3632 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3633 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3634 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3635 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3636 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3637 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3638 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3639 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3640 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3641 | const RecordType *T2RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3642 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3643 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3644 | // The type we're converting from is a class type, enumerate its conversion |
| 3645 | // functions. |
| 3646 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3647 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 3648 | const auto &Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3649 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3650 | NamedDecl *D = *I; |
| 3651 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3652 | if (isa<UsingShadowDecl>(D)) |
| 3653 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3654 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3655 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3656 | CXXConversionDecl *Conv; |
| 3657 | if (ConvTemplate) |
| 3658 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3659 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3660 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3661 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3662 | // If the conversion function doesn't return a reference type, |
| 3663 | // it can't be considered for this conversion unless we're allowed to |
| 3664 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3665 | // FIXME: Do we need to make sure that we only consider conversion |
| 3666 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3667 | // break recursion. |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3668 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3669 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3670 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3671 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3672 | ActingDC, Initializer, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 3673 | DestType, CandidateSet, |
| 3674 | /*AllowObjCConversionOnExplicit=*/ |
| 3675 | false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3676 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3677 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 3678 | Initializer, DestType, CandidateSet, |
| 3679 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3680 | } |
| 3681 | } |
| 3682 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3683 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3684 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3685 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3686 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3687 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3688 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3689 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3690 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3691 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3692 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3693 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3694 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3695 | // This is the overload that will be used for this initialization step if we |
| 3696 | // use this initialization. Mark it as referenced. |
| 3697 | Function->setReferenced(); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3698 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3699 | // Compute the returned type of the conversion. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3700 | if (isa<CXXConversionDecl>(Function)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3701 | T2 = Function->getReturnType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3702 | else |
| 3703 | T2 = cv1T1; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3704 | |
| 3705 | // Add the user-defined conversion step. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3706 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3707 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3708 | T2.getNonLValueExprType(S.Context), |
| 3709 | HadMultipleCandidates); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3710 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3711 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3712 | // cv-qualification adjustments. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3713 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3714 | if (T2->isLValueReferenceType()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3715 | VK = VK_LValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3716 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3717 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3718 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3719 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3720 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3721 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3722 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3723 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3724 | T2.getNonLValueExprType(S.Context), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3725 | NewDerivedToBase, NewObjCConversion, |
| 3726 | NewObjCLifetimeConversion); |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3727 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3728 | // If the type we've converted to is not reference-related to the |
| 3729 | // type we're looking for, then there is another conversion step |
| 3730 | // we need to perform to produce a temporary of the right type |
| 3731 | // that we'll be binding to. |
| 3732 | ImplicitConversionSequence ICS; |
| 3733 | ICS.setStandard(); |
| 3734 | ICS.Standard = Best->FinalConversion; |
| 3735 | T2 = ICS.Standard.getToType(2); |
| 3736 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3737 | } else if (NewDerivedToBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3738 | Sequence.AddDerivedToBaseCastStep( |
| 3739 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3740 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3741 | VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3742 | else if (NewObjCConversion) |
| 3743 | Sequence.AddObjCObjectConversionStep( |
| 3744 | S.Context.getQualifiedType(T1, |
| 3745 | T2.getNonReferenceType().getQualifiers())); |
| 3746 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3747 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3748 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3749 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3750 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3751 | return OR_Success; |
| 3752 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3753 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3754 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3755 | const InitializedEntity &Entity, |
| 3756 | Expr *CurInitExpr); |
| 3757 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3758 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3759 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3760 | const InitializedEntity &Entity, |
| 3761 | const InitializationKind &Kind, |
| 3762 | Expr *Initializer, |
| 3763 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3764 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3765 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3766 | Qualifiers T1Quals; |
| 3767 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3768 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3769 | Qualifiers T2Quals; |
| 3770 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3771 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3772 | // If the initializer is the address of an overloaded function, try |
| 3773 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3774 | // type of the resulting function. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3775 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3776 | T1, Sequence)) |
| 3777 | return; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3778 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3779 | // Delegate everything else to a subfunction. |
| 3780 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3781 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3782 | } |
| 3783 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3784 | /// Converts the target of reference initialization so that it has the |
| 3785 | /// appropriate qualifiers and value kind. |
| 3786 | /// |
| 3787 | /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'. |
| 3788 | /// \code |
| 3789 | /// int x; |
| 3790 | /// const int &r = x; |
| 3791 | /// \endcode |
| 3792 | /// |
| 3793 | /// In this case the reference is binding to a bitfield lvalue, which isn't |
| 3794 | /// valid. Perform a load to create a lifetime-extended temporary instead. |
| 3795 | /// \code |
| 3796 | /// const int &r = someStruct.bitfield; |
| 3797 | /// \endcode |
| 3798 | static ExprValueKind |
| 3799 | convertQualifiersAndValueKindIfNecessary(Sema &S, |
| 3800 | InitializationSequence &Sequence, |
| 3801 | Expr *Initializer, |
| 3802 | QualType cv1T1, |
| 3803 | Qualifiers T1Quals, |
| 3804 | Qualifiers T2Quals, |
| 3805 | bool IsLValueRef) { |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3806 | bool IsNonAddressableType = Initializer->refersToBitField() || |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3807 | Initializer->refersToVectorElement(); |
| 3808 | |
| 3809 | if (IsNonAddressableType) { |
| 3810 | // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an |
| 3811 | // lvalue reference to a non-volatile const type, or the reference shall be |
| 3812 | // an rvalue reference. |
| 3813 | // |
| 3814 | // If not, we can't make a temporary and bind to that. Give up and allow the |
| 3815 | // error to be diagnosed later. |
| 3816 | if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) { |
| 3817 | assert(Initializer->isGLValue()); |
| 3818 | return Initializer->getValueKind(); |
| 3819 | } |
| 3820 | |
| 3821 | // Force a load so we can materialize a temporary. |
| 3822 | Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType()); |
| 3823 | return VK_RValue; |
| 3824 | } |
| 3825 | |
| 3826 | if (T1Quals != T2Quals) { |
| 3827 | Sequence.AddQualificationConversionStep(cv1T1, |
| 3828 | Initializer->getValueKind()); |
| 3829 | } |
| 3830 | |
| 3831 | return Initializer->getValueKind(); |
| 3832 | } |
| 3833 | |
| 3834 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3835 | /// \brief Reference initialization without resolving overloaded functions. |
| 3836 | static void TryReferenceInitializationCore(Sema &S, |
| 3837 | const InitializedEntity &Entity, |
| 3838 | const InitializationKind &Kind, |
| 3839 | Expr *Initializer, |
| 3840 | QualType cv1T1, QualType T1, |
| 3841 | Qualifiers T1Quals, |
| 3842 | QualType cv2T2, QualType T2, |
| 3843 | Qualifiers T2Quals, |
| 3844 | InitializationSequence &Sequence) { |
| 3845 | QualType DestType = Entity.getType(); |
| 3846 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3847 | // Compute some basic properties of the types and the initializer. |
| 3848 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3849 | bool isRValueRef = !isLValueRef; |
| 3850 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3851 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3852 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3853 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3854 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3855 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3856 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3857 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3858 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3859 | // 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] | 3860 | // "cv2 T2" as follows: |
| 3861 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3862 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3863 | // expression |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3864 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3865 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3866 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3867 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3868 | bool T1Function = T1->isFunctionType(); |
| 3869 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3870 | if (InitCategory.isLValue() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3871 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3872 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3873 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3874 | // - 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] | 3875 | // reference-compatible with "cv2 T2," or |
| 3876 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3877 | // 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] | 3878 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3879 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3880 | // to decide whether we're actually binding to a temporary created from |
| 3881 | // the bit-field. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3882 | if (DerivedToBase) |
| 3883 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3884 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3885 | VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3886 | else if (ObjCConversion) |
| 3887 | Sequence.AddObjCObjectConversionStep( |
| 3888 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3889 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3890 | ExprValueKind ValueKind = |
| 3891 | convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer, |
| 3892 | cv1T1, T1Quals, T2Quals, |
| 3893 | isLValueRef); |
| 3894 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3895 | return; |
| 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 |
| 3900 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3901 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3902 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3903 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3904 | // 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] | 3905 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3906 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3907 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3908 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3909 | S, Entity, Kind, Initializer, /*AllowRValues*/isRValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3910 | if (ConvOvlResult == OR_Success) |
| 3911 | return; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3912 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3913 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3914 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3915 | ConvOvlResult); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3916 | } |
| 3917 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3918 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3919 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3920 | // 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] | 3921 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3922 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3923 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3924 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3925 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3926 | Sequence.SetOverloadFailure( |
| 3927 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3928 | ConvOvlResult); |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3929 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3930 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3931 | ? (RefRelationship == Sema::Ref_Related |
| 3932 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3933 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3934 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3935 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3936 | return; |
| 3937 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3938 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3939 | // - If the initializer expression |
| 3940 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3941 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3942 | // Note: functions are handled below. |
| 3943 | if (!T1Function && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3944 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3945 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3946 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3947 | (InitCategory.isXValue() || |
| 3948 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3949 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3950 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3951 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3952 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3953 | // compiler the freedom to perform a copy here or bind to the |
| 3954 | // object, while C++0x requires that we bind directly to the |
| 3955 | // object. Hence, we always bind to the object without making an |
| 3956 | // extra copy. However, in C++03 requires that we check for the |
| 3957 | // presence of a suitable copy constructor: |
| 3958 | // |
| 3959 | // The constructor that would be used to make the copy shall |
| 3960 | // be callable whether or not the copy is actually done. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3961 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3962 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3963 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3964 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3965 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3966 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3967 | if (DerivedToBase) |
| 3968 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3969 | ValueKind); |
| 3970 | else if (ObjCConversion) |
| 3971 | Sequence.AddObjCObjectConversionStep( |
| 3972 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3973 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3974 | ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence, |
| 3975 | Initializer, cv1T1, |
| 3976 | T1Quals, T2Quals, |
| 3977 | isLValueRef); |
| 3978 | |
| 3979 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3980 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3981 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3982 | |
| 3983 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3984 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3985 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3986 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3987 | // |
| 3988 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3989 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3990 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3991 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3992 | S, Entity, Kind, Initializer, /*AllowRValues*/true, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3993 | if (ConvOvlResult) |
| 3994 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3995 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3996 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3997 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3998 | return; |
| 3999 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4000 | |
Douglas Gregor | 6fa6ab0 | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 4001 | if ((RefRelationship == Sema::Ref_Compatible || |
| 4002 | RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) && |
| 4003 | isRValueRef && InitCategory.isLValue()) { |
| 4004 | Sequence.SetFailed( |
| 4005 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4006 | return; |
| 4007 | } |
| 4008 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4009 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4010 | return; |
| 4011 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4012 | |
| 4013 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4014 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4015 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4016 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4017 | |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4018 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 4019 | |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4020 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 4021 | // copy-initialization? |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4022 | ImplicitConversionSequence ICS |
| 4023 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 4024 | /*SuppressUserConversions=*/false, |
| 4025 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4026 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4027 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4028 | /*AllowObjCWritebackConversion=*/false); |
| 4029 | |
| 4030 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4031 | // FIXME: Use the conversion function set stored in ICS to turn |
| 4032 | // this into an overloading ambiguity diagnostic. However, we need |
| 4033 | // to keep that set as an OverloadCandidateSet rather than as some |
| 4034 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4035 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 4036 | Sequence.SetOverloadFailure( |
| 4037 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4038 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4039 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4040 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4041 | else |
| 4042 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4043 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4044 | } else { |
| 4045 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4046 | } |
| 4047 | |
| 4048 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 4049 | // same cv-qualification as, or greater cv-qualification |
| 4050 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4051 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 4052 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4053 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4054 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4055 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4056 | return; |
| 4057 | } |
| 4058 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4059 | // [...] 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] | 4060 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4061 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4062 | InitCategory.isLValue()) { |
| 4063 | Sequence.SetFailed( |
| 4064 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4065 | return; |
| 4066 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4067 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4068 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 4069 | return; |
| 4070 | } |
| 4071 | |
| 4072 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4073 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 4074 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4075 | const InitializedEntity &Entity, |
| 4076 | const InitializationKind &Kind, |
| 4077 | Expr *Initializer, |
| 4078 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4079 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4082 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4083 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4084 | const InitializedEntity &Entity, |
| 4085 | const InitializationKind &Kind, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4086 | InitializationSequence &Sequence, |
| 4087 | InitListExpr *InitList) { |
| 4088 | assert((!InitList || InitList->getNumInits() == 0) && |
| 4089 | "Shouldn't use value-init for non-empty init lists"); |
| 4090 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4091 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4092 | // |
| 4093 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4094 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4095 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4096 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4097 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4098 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4099 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 4100 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4101 | bool NeedZeroInitialization = true; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4102 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4103 | // C++98: |
| 4104 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 4105 | // (12.1), then the default constructor for T is called (and the |
| 4106 | // initialization is ill-formed if T has no accessible default |
| 4107 | // constructor); |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4108 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4109 | NeedZeroInitialization = false; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4110 | } else { |
| 4111 | // C++11: |
| 4112 | // -- if T is a class type (clause 9) with either no default constructor |
| 4113 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 4114 | // or deleted, then the object is default-initialized; |
| 4115 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 4116 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4117 | NeedZeroInitialization = false; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4118 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4119 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4120 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 4121 | // user-provided or deleted default constructor, then the object is |
| 4122 | // zero-initialized and, if T has a non-trivial default constructor, |
| 4123 | // default-initialized; |
Richard Smith | fb26652 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 4124 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 4125 | // constructor' part was removed by DR1507. |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4126 | if (NeedZeroInitialization) |
| 4127 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4128 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4129 | // C++03: |
| 4130 | // -- if T is a non-union class type without a user-declared constructor, |
| 4131 | // then every non-static data member and base class component of T is |
| 4132 | // value-initialized; |
| 4133 | // [...] A program that calls for [...] value-initialization of an |
| 4134 | // entity of reference type is ill-formed. |
| 4135 | // |
| 4136 | // C++11 doesn't need this handling, because value-initialization does not |
| 4137 | // occur recursively there, and the implicit default constructor is |
| 4138 | // defined as deleted in the problematic cases. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4139 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4140 | ClassDecl->hasUninitializedReferenceMember()) { |
| 4141 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 4142 | return; |
| 4143 | } |
| 4144 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4145 | // If this is list-value-initialization, pass the empty init list on when |
| 4146 | // building the constructor call. This affects the semantics of a few |
| 4147 | // things (such as whether an explicit default constructor can be called). |
| 4148 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4149 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4150 | bool InitListSyntax = InitList; |
| 4151 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4152 | return TryConstructorInitialization(S, Entity, Kind, Args, T, Sequence, |
| 4153 | InitListSyntax); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4154 | } |
| 4155 | } |
| 4156 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4157 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4158 | } |
| 4159 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4160 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 4161 | static void TryDefaultInitialization(Sema &S, |
| 4162 | const InitializedEntity &Entity, |
| 4163 | const InitializationKind &Kind, |
| 4164 | InitializationSequence &Sequence) { |
| 4165 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4166 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4167 | // C++ [dcl.init]p6: |
| 4168 | // To default-initialize an object of type T means: |
| 4169 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4170 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 4171 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4172 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 4173 | // constructor for T is called (and the initialization is ill-formed if |
| 4174 | // T has no accessible default constructor); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4175 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 4176 | TryConstructorInitialization(S, Entity, Kind, None, DestType, Sequence); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4177 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4178 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4179 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4180 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4181 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4182 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4183 | // 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] | 4184 | // default constructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4185 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4186 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4187 | return; |
| 4188 | } |
| 4189 | |
| 4190 | // If the destination type has a lifetime property, zero-initialize it. |
| 4191 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4192 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4193 | return; |
| 4194 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4197 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 4198 | /// which enumerates all conversion functions and performs overload resolution |
| 4199 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4200 | static void TryUserDefinedConversion(Sema &S, |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4201 | QualType DestType, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4202 | const InitializationKind &Kind, |
| 4203 | Expr *Initializer, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4204 | InitializationSequence &Sequence, |
| 4205 | bool TopLevelOfInitList) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4206 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4207 | QualType SourceType = Initializer->getType(); |
| 4208 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4209 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4210 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4211 | // Build the candidate set directly in the initialization sequence |
| 4212 | // structure, so that it will persist if we fail. |
| 4213 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 4214 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4215 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4216 | // Determine whether we are allowed to call explicit constructors or |
| 4217 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4218 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4219 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4220 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4221 | // The type we're converting to is a class type. Enumerate its constructors |
| 4222 | // to see if there is a suitable conversion. |
| 4223 | CXXRecordDecl *DestRecordDecl |
| 4224 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4225 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4226 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4227 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4228 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 12be639 | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4229 | // The container holding the constructors can under certain conditions |
| 4230 | // be changed while iterating. To be safe we copy the lookup results |
| 4231 | // to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4232 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4233 | for (SmallVectorImpl<NamedDecl *>::iterator |
David Blaikie | 12be639 | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4234 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4235 | Con != ConEnd; ++Con) { |
| 4236 | NamedDecl *D = *Con; |
| 4237 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4238 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4239 | // Find the constructor (which may be a template). |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4240 | CXXConstructorDecl *Constructor = nullptr; |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4241 | FunctionTemplateDecl *ConstructorTmpl |
| 4242 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4243 | if (ConstructorTmpl) |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4244 | Constructor = cast<CXXConstructorDecl>( |
| 4245 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4246 | else |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4247 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4248 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4249 | if (!Constructor->isInvalidDecl() && |
| 4250 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4251 | if (ConstructorTmpl) |
| 4252 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4253 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4254 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4255 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4256 | else |
| 4257 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4258 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4259 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4260 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4261 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4262 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4263 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4264 | |
| 4265 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 4266 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4267 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4268 | // The type we're converting from is a class type, enumerate its conversion |
| 4269 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4270 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4271 | // We can only enumerate the conversion functions for a complete type; if |
| 4272 | // the type isn't complete, simply skip this step. |
| 4273 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 4274 | CXXRecordDecl *SourceRecordDecl |
| 4275 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4276 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 4277 | const auto &Conversions = |
| 4278 | SourceRecordDecl->getVisibleConversionFunctions(); |
| 4279 | for (auto I = Conversions.begin(), E = Conversions.end(); I != E; ++I) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4280 | NamedDecl *D = *I; |
| 4281 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4282 | if (isa<UsingShadowDecl>(D)) |
| 4283 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4284 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4285 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4286 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4287 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4288 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4289 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4290 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4291 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4292 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4293 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4294 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4295 | ActingDC, Initializer, DestType, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4296 | CandidateSet, AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4297 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4298 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4299 | Initializer, DestType, CandidateSet, |
| 4300 | AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4301 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4302 | } |
| 4303 | } |
| 4304 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4305 | |
| 4306 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4307 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4308 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4309 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4310 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4311 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4312 | Result); |
| 4313 | return; |
| 4314 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4315 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4316 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4317 | Function->setReferenced(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4318 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4319 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4320 | if (isa<CXXConstructorDecl>(Function)) { |
| 4321 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4322 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 4323 | // cv-unqualified type of the destination. |
| 4324 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 4325 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4326 | HadMultipleCandidates); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4327 | return; |
| 4328 | } |
| 4329 | |
| 4330 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4331 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4332 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4333 | // 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] | 4334 | // the resulting temporary object (possible to create an object of |
| 4335 | // a base class type). That copy is not a separate conversion, so |
| 4336 | // we just make a note of the actual destination type (possibly a |
| 4337 | // base class of the type returned by the conversion function) and |
| 4338 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4339 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 4340 | HadMultipleCandidates); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4341 | return; |
| 4342 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4343 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4344 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 4345 | HadMultipleCandidates); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4346 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4347 | // If the conversion following the call to the conversion function |
| 4348 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4349 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 4350 | Best->FinalConversion.Third) { |
| 4351 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4352 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4353 | ICS.Standard = Best->FinalConversion; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4354 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4355 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4358 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 4359 | /// a function with a pointer return type contains a 'return false;' statement. |
| 4360 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 4361 | /// code using that header. |
| 4362 | /// |
| 4363 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 4364 | /// if it's used in a pointer-returning function in a system header. |
| 4365 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 4366 | const InitializedEntity &Entity, |
| 4367 | const Expr *Init) { |
| 4368 | return S.getLangOpts().CPlusPlus11 && |
| 4369 | Entity.getKind() == InitializedEntity::EK_Result && |
| 4370 | Entity.getType()->isPointerType() && |
| 4371 | isa<CXXBoolLiteralExpr>(Init) && |
| 4372 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 4373 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 4374 | } |
| 4375 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4376 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 4377 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 4378 | |
| 4379 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4380 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4381 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4382 | // Skip parens. |
| 4383 | e = e->IgnoreParens(); |
| 4384 | |
| 4385 | // Skip address-of nodes. |
| 4386 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 4387 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4388 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 4389 | isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4390 | |
| 4391 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4392 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 4393 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4394 | case CK_Dependent: |
| 4395 | case CK_BitCast: |
| 4396 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4397 | case CK_NoOp: |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4398 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4399 | |
| 4400 | case CK_ArrayToPointerDecay: |
| 4401 | return IIK_nonscalar; |
| 4402 | |
| 4403 | case CK_NullToPointer: |
| 4404 | return IIK_okay; |
| 4405 | |
| 4406 | default: |
| 4407 | break; |
| 4408 | } |
| 4409 | |
| 4410 | // 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] | 4411 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4412 | // set isWeakAccess to true, to mean that there will be an implicit |
| 4413 | // load which requires a cleanup. |
| 4414 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4415 | isWeakAccess = true; |
| 4416 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4417 | if (!isAddressOf) return IIK_nonlocal; |
| 4418 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4419 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 4420 | if (!var) return IIK_nonlocal; |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4421 | |
| 4422 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4423 | |
| 4424 | // If we have a conditional operator, check both sides. |
| 4425 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4426 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 4427 | isWeakAccess)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4428 | return iik; |
| 4429 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4430 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4431 | |
| 4432 | // These are never scalar. |
| 4433 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 4434 | return IIK_nonscalar; |
| 4435 | |
| 4436 | // Otherwise, it needs to be a null pointer constant. |
| 4437 | } else { |
| 4438 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 4439 | ? IIK_okay : IIK_nonlocal); |
| 4440 | } |
| 4441 | |
| 4442 | return IIK_nonlocal; |
| 4443 | } |
| 4444 | |
| 4445 | /// Check whether the given expression is a valid operand for an |
| 4446 | /// indirect copy/restore. |
| 4447 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 4448 | assert(src->isRValue()); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4449 | bool isWeakAccess = false; |
| 4450 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 4451 | // If isWeakAccess to true, there will be an implicit |
| 4452 | // load which requires a cleanup. |
| 4453 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 4454 | S.ExprNeedsCleanups = true; |
| 4455 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4456 | if (iik == IIK_okay) return; |
| 4457 | |
| 4458 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 4459 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 4460 | << src->getSourceRange(); |
| 4461 | } |
| 4462 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4463 | /// \brief Determine whether we have compatible array types for the |
| 4464 | /// purposes of GNU by-copy array initialization. |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4465 | static bool hasCompatibleArrayTypes(ASTContext &Context, const ArrayType *Dest, |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4466 | const ArrayType *Source) { |
| 4467 | // If the source and destination array types are equivalent, we're |
| 4468 | // done. |
| 4469 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 4470 | return true; |
| 4471 | |
| 4472 | // Make sure that the element types are the same. |
| 4473 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 4474 | return false; |
| 4475 | |
| 4476 | // The only mismatch we allow is when the destination is an |
| 4477 | // incomplete array type and the source is a constant array type. |
| 4478 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 4479 | } |
| 4480 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4481 | static bool tryObjCWritebackConversion(Sema &S, |
| 4482 | InitializationSequence &Sequence, |
| 4483 | const InitializedEntity &Entity, |
| 4484 | Expr *Initializer) { |
| 4485 | bool ArrayDecay = false; |
| 4486 | QualType ArgType = Initializer->getType(); |
| 4487 | QualType ArgPointee; |
| 4488 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 4489 | ArrayDecay = true; |
| 4490 | ArgPointee = ArgArrayType->getElementType(); |
| 4491 | ArgType = S.Context.getPointerType(ArgPointee); |
| 4492 | } |
| 4493 | |
| 4494 | // Handle write-back conversion. |
| 4495 | QualType ConvertedArgType; |
| 4496 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 4497 | ConvertedArgType)) |
| 4498 | return false; |
| 4499 | |
| 4500 | // We should copy unless we're passing to an argument explicitly |
| 4501 | // marked 'out'. |
| 4502 | bool ShouldCopy = true; |
| 4503 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4504 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4505 | |
| 4506 | // Do we need an lvalue conversion? |
| 4507 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4508 | ImplicitConversionSequence ICS; |
| 4509 | ICS.setStandard(); |
| 4510 | ICS.Standard.setAsIdentityConversion(); |
| 4511 | |
| 4512 | QualType ResultType; |
| 4513 | if (ArrayDecay) { |
| 4514 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4515 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4516 | } else { |
| 4517 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4518 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4519 | } |
| 4520 | |
| 4521 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4522 | } |
| 4523 | |
| 4524 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4525 | return true; |
| 4526 | } |
| 4527 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4528 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4529 | InitializationSequence &Sequence, |
| 4530 | QualType DestType, |
| 4531 | Expr *Initializer) { |
| 4532 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4533 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4534 | return false; |
| 4535 | |
| 4536 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4537 | return true; |
| 4538 | } |
| 4539 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4540 | // |
| 4541 | // OpenCL 1.2 spec, s6.12.10 |
| 4542 | // |
| 4543 | // The event argument can also be used to associate the |
| 4544 | // async_work_group_copy with a previous async copy allowing |
| 4545 | // an event to be shared by multiple async copies; otherwise |
| 4546 | // event should be zero. |
| 4547 | // |
| 4548 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4549 | InitializationSequence &Sequence, |
| 4550 | QualType DestType, |
| 4551 | Expr *Initializer) { |
| 4552 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4553 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4554 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4555 | return false; |
| 4556 | |
| 4557 | Sequence.AddOCLZeroEventStep(DestType); |
| 4558 | return true; |
| 4559 | } |
| 4560 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4561 | InitializationSequence::InitializationSequence(Sema &S, |
| 4562 | const InitializedEntity &Entity, |
| 4563 | const InitializationKind &Kind, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4564 | MultiExprArg Args, |
| 4565 | bool TopLevelOfInitList) |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 4566 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4567 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList); |
| 4568 | } |
| 4569 | |
| 4570 | void InitializationSequence::InitializeFrom(Sema &S, |
| 4571 | const InitializedEntity &Entity, |
| 4572 | const InitializationKind &Kind, |
| 4573 | MultiExprArg Args, |
| 4574 | bool TopLevelOfInitList) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4575 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4576 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4577 | // Eliminate non-overload placeholder types in the arguments. We |
| 4578 | // need to do this before checking whether types are dependent |
| 4579 | // because lowering a pseudo-object expression might well give us |
| 4580 | // something of dependent type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4581 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4582 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4583 | // FIXME: should we be doing this here? |
| 4584 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4585 | if (result.isInvalid()) { |
| 4586 | SetFailed(FK_PlaceholderType); |
| 4587 | return; |
| 4588 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4589 | Args[I] = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4592 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4593 | // The semantics of initializers are as follows. The destination type is |
| 4594 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4595 | // 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] | 4596 | // 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] | 4597 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4598 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4599 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4600 | if (DestType->isDependentType() || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4601 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4602 | SequenceKind = DependentSequence; |
| 4603 | return; |
| 4604 | } |
| 4605 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4606 | // Almost everything is a normal sequence. |
| 4607 | setSequenceKind(NormalSequence); |
| 4608 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4609 | QualType SourceType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4610 | Expr *Initializer = nullptr; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4611 | if (Args.size() == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4612 | Initializer = Args[0]; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 4613 | if (S.getLangOpts().ObjC1) { |
| 4614 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getLocStart(), |
| 4615 | DestType, Initializer->getType(), |
| 4616 | Initializer) || |
| 4617 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 4618 | Args[0] = Initializer; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 4619 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4620 | if (!isa<InitListExpr>(Initializer)) |
| 4621 | SourceType = Initializer->getType(); |
| 4622 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4623 | |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4624 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4625 | // object is list-initialized (8.5.4). |
| 4626 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4627 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4628 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4629 | return; |
| 4630 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4631 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4632 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4633 | // - If the destination type is a reference type, see 8.5.3. |
| 4634 | if (DestType->isReferenceType()) { |
| 4635 | // C++0x [dcl.init.ref]p1: |
| 4636 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4637 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4638 | // by an object that can be converted into a T. |
| 4639 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4640 | if (Args.size() != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4641 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4642 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4643 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4644 | return; |
| 4645 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4646 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4647 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4648 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4649 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4650 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4651 | return; |
| 4652 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4653 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4654 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4655 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4656 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4657 | return; |
| 4658 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4659 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4660 | // - If the destination type is an array of characters, an array of |
| 4661 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4662 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4663 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4664 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4665 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4666 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4667 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4668 | return; |
| 4669 | } |
| 4670 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4671 | if (Initializer) { |
| 4672 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 4673 | case SIF_None: |
| 4674 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 4675 | return; |
| 4676 | case SIF_NarrowStringIntoWideChar: |
| 4677 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 4678 | return; |
| 4679 | case SIF_WideStringIntoChar: |
| 4680 | SetFailed(FK_WideStringIntoCharArray); |
| 4681 | return; |
| 4682 | case SIF_IncompatWideStringIntoWideChar: |
| 4683 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 4684 | return; |
| 4685 | case SIF_Other: |
| 4686 | break; |
| 4687 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4690 | // Note: as an GNU C extension, we allow initialization of an |
| 4691 | // array from a compound literal that creates an array of the same |
| 4692 | // type, so long as the initializer has no side effects. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4693 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4694 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4695 | Initializer->getType()->isArrayType()) { |
| 4696 | const ArrayType *SourceAT |
| 4697 | = Context.getAsArrayType(Initializer->getType()); |
| 4698 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4699 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4700 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4701 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4702 | else { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4703 | AddArrayInitStep(DestType); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4704 | } |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4705 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4706 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4707 | // class member of array type from a parenthesized initializer list. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4708 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4709 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4710 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4711 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4712 | *this); |
| 4713 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4714 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4715 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4716 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 4717 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4718 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4719 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4720 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4721 | return; |
| 4722 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4723 | |
Larisse Voufo | d201099 | 2015-01-24 23:09:54 +0000 | [diff] [blame] | 4724 | // Determine whether we should consider writeback conversions for |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4725 | // Objective-C ARC. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4726 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4727 | Entity.isParameterKind(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4728 | |
| 4729 | // We're at the end of the line for C: it's either a write-back conversion |
| 4730 | // 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] | 4731 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4732 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4733 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4734 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4735 | return; |
| 4736 | } |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4737 | |
| 4738 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4739 | return; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4740 | |
| 4741 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4742 | return; |
| 4743 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4744 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4745 | AddCAssignmentStep(DestType); |
| 4746 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4747 | return; |
| 4748 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4749 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4750 | assert(S.getLangOpts().CPlusPlus); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4751 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4752 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4753 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4754 | // - If the initialization is direct-initialization, or if it is |
| 4755 | // copy-initialization where the cv-unqualified version of the |
| 4756 | // 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] | 4757 | // class of the destination, constructors are considered. [...] |
| 4758 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4759 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4760 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4761 | S.IsDerivedFrom(SourceType, DestType)))) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4762 | TryConstructorInitialization(S, Entity, Kind, Args, |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4763 | DestType, *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4764 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4765 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4766 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4767 | // used) to a derived class thereof are enumerated as described in |
| 4768 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4769 | // (13.3). |
| 4770 | else |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4771 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4772 | TopLevelOfInitList); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4773 | return; |
| 4774 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4775 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4776 | if (Args.size() > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4777 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4778 | return; |
| 4779 | } |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4780 | assert(Args.size() == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4781 | |
| 4782 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4783 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4784 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4785 | // For a conversion to _Atomic(T) from either T or a class type derived |
| 4786 | // from T, initialize the T object then convert to _Atomic type. |
| 4787 | bool NeedAtomicConversion = false; |
| 4788 | if (const AtomicType *Atomic = DestType->getAs<AtomicType>()) { |
| 4789 | if (Context.hasSameUnqualifiedType(SourceType, Atomic->getValueType()) || |
| 4790 | S.IsDerivedFrom(SourceType, Atomic->getValueType())) { |
| 4791 | DestType = Atomic->getValueType(); |
| 4792 | NeedAtomicConversion = true; |
| 4793 | } |
| 4794 | } |
| 4795 | |
| 4796 | TryUserDefinedConversion(S, DestType, Kind, Initializer, *this, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4797 | TopLevelOfInitList); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4798 | MaybeProduceObjCObject(S, *this, Entity); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4799 | if (!Failed() && NeedAtomicConversion) |
| 4800 | AddAtomicConversionStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4801 | return; |
| 4802 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4803 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4804 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4805 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4806 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4807 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4808 | // destination type; no user-defined conversions are considered. |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4809 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4810 | ImplicitConversionSequence ICS |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4811 | = S.TryImplicitConversion(Initializer, DestType, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4812 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4813 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4814 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4815 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4816 | allowObjCWritebackConversion); |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4817 | |
| 4818 | if (ICS.isStandard() && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4819 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4820 | // Objective-C ARC writeback conversion. |
| 4821 | |
| 4822 | // We should copy unless we're passing to an argument explicitly |
| 4823 | // marked 'out'. |
| 4824 | bool ShouldCopy = true; |
| 4825 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4826 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4827 | |
| 4828 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4829 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4830 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4831 | ImplicitConversionSequence LvalueICS; |
| 4832 | LvalueICS.setStandard(); |
| 4833 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4834 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4835 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4836 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4837 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4838 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4839 | AddPassByIndirectCopyRestoreStep(DestType, ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4840 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4841 | DeclAccessPair dap; |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4842 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 4843 | AddZeroInitializationStep(Entity.getType()); |
| 4844 | } else if (Initializer->getType() == Context.OverloadTy && |
| 4845 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 4846 | false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4847 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4848 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4849 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4850 | } else { |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 4851 | AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4852 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4853 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4854 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4855 | } |
| 4856 | |
| 4857 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4858 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4859 | StepEnd = Steps.end(); |
| 4860 | Step != StepEnd; ++Step) |
| 4861 | Step->Destroy(); |
| 4862 | } |
| 4863 | |
| 4864 | //===----------------------------------------------------------------------===// |
| 4865 | // Perform initialization |
| 4866 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4867 | static Sema::AssignmentAction |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4868 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4869 | switch(Entity.getKind()) { |
| 4870 | case InitializedEntity::EK_Variable: |
| 4871 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4872 | case InitializedEntity::EK_Exception: |
| 4873 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4874 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4875 | return Sema::AA_Initializing; |
| 4876 | |
| 4877 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4878 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4879 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4880 | return Sema::AA_Sending; |
| 4881 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4882 | return Sema::AA_Passing; |
| 4883 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4884 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 4885 | if (Entity.getDecl() && |
| 4886 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4887 | return Sema::AA_Sending; |
| 4888 | |
| 4889 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
| 4890 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4891 | case InitializedEntity::EK_Result: |
| 4892 | return Sema::AA_Returning; |
| 4893 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4894 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | 14e9541 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 4895 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4896 | // FIXME: Can we tell apart casting vs. converting? |
| 4897 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4898 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4899 | case InitializedEntity::EK_Member: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4900 | case InitializedEntity::EK_ArrayElement: |
| 4901 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4902 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4903 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4904 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4905 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4906 | return Sema::AA_Initializing; |
| 4907 | } |
| 4908 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4909 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4912 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4913 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4914 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4915 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4916 | case InitializedEntity::EK_ArrayElement: |
| 4917 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4918 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4919 | case InitializedEntity::EK_New: |
| 4920 | case InitializedEntity::EK_Variable: |
| 4921 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4922 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4923 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4924 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4925 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4926 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4927 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4928 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4929 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4930 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4931 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4932 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4933 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4934 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4935 | return true; |
| 4936 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4937 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4938 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4939 | } |
| 4940 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4941 | /// \brief Whether the given entity, when initialized with an object |
| 4942 | /// created for that initialization, requires destruction. |
| 4943 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4944 | switch (Entity.getKind()) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4945 | case InitializedEntity::EK_Result: |
| 4946 | case InitializedEntity::EK_New: |
| 4947 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4948 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4949 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4950 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4951 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4952 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4953 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4954 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4955 | case InitializedEntity::EK_Member: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4956 | case InitializedEntity::EK_Variable: |
| 4957 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4958 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4959 | case InitializedEntity::EK_Temporary: |
| 4960 | case InitializedEntity::EK_ArrayElement: |
| 4961 | case InitializedEntity::EK_Exception: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4962 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4963 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4964 | return true; |
| 4965 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4966 | |
| 4967 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4970 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4971 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4972 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4973 | OverloadCandidateSet &CandidateSet, |
| 4974 | CXXRecordDecl *Class, |
| 4975 | Expr *CurInitExpr) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4976 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4977 | // The container holding the constructors can under certain conditions |
| 4978 | // be changed while iterating (e.g. because of deserialization). |
| 4979 | // To be safe we copy the lookup results to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4980 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4981 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4982 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4983 | NamedDecl *D = *CI; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4984 | CXXConstructorDecl *Constructor = nullptr; |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4985 | |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4986 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4987 | // Handle copy/moveconstructors, only. |
| 4988 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4989 | !Constructor->isCopyOrMoveConstructor() || |
| 4990 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4991 | continue; |
| 4992 | |
| 4993 | DeclAccessPair FoundDecl |
| 4994 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4995 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4996 | CurInitExpr, CandidateSet); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4997 | continue; |
| 4998 | } |
| 4999 | |
| 5000 | // Handle constructor templates. |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 5001 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5002 | if (ConstructorTmpl->isInvalidDecl()) |
| 5003 | continue; |
| 5004 | |
| 5005 | Constructor = cast<CXXConstructorDecl>( |
| 5006 | ConstructorTmpl->getTemplatedDecl()); |
| 5007 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 5008 | continue; |
| 5009 | |
| 5010 | // FIXME: Do we need to limit this to copy-constructor-like |
| 5011 | // candidates? |
| 5012 | DeclAccessPair FoundDecl |
| 5013 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5014 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5015 | CurInitExpr, CandidateSet, true); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5016 | } |
| 5017 | } |
| 5018 | |
| 5019 | /// \brief Get the location at which initialization diagnostics should appear. |
| 5020 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 5021 | Expr *Initializer) { |
| 5022 | switch (Entity.getKind()) { |
| 5023 | case InitializedEntity::EK_Result: |
| 5024 | return Entity.getReturnLoc(); |
| 5025 | |
| 5026 | case InitializedEntity::EK_Exception: |
| 5027 | return Entity.getThrowLoc(); |
| 5028 | |
| 5029 | case InitializedEntity::EK_Variable: |
| 5030 | return Entity.getDecl()->getLocation(); |
| 5031 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 5032 | case InitializedEntity::EK_LambdaCapture: |
| 5033 | return Entity.getCaptureLoc(); |
| 5034 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5035 | case InitializedEntity::EK_ArrayElement: |
| 5036 | case InitializedEntity::EK_Member: |
| 5037 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5038 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5039 | case InitializedEntity::EK_Temporary: |
| 5040 | case InitializedEntity::EK_New: |
| 5041 | case InitializedEntity::EK_Base: |
| 5042 | case InitializedEntity::EK_Delegating: |
| 5043 | case InitializedEntity::EK_VectorElement: |
| 5044 | case InitializedEntity::EK_ComplexElement: |
| 5045 | case InitializedEntity::EK_BlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5046 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5047 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5048 | return Initializer->getLocStart(); |
| 5049 | } |
| 5050 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5051 | } |
| 5052 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5053 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 5054 | /// provided by the given initializer by calling the appropriate copy |
| 5055 | /// constructor. |
| 5056 | /// |
| 5057 | /// \param S The Sema object used for type-checking. |
| 5058 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 5059 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5060 | /// the type of the initializer expression or a superclass thereof. |
| 5061 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5062 | /// \param Entity The entity being initialized. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5063 | /// |
| 5064 | /// \param CurInit The initializer expression. |
| 5065 | /// |
| 5066 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 5067 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 5068 | /// an rvalue. |
| 5069 | /// |
| 5070 | /// \returns An expression that copies the initializer expression into |
| 5071 | /// a temporary object, or an error expression if a copy could not be |
| 5072 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5073 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5074 | QualType T, |
| 5075 | const InitializedEntity &Entity, |
| 5076 | ExprResult CurInit, |
| 5077 | bool IsExtraneousCopy) { |
Fariborz Jahanian | 36f7f13 | 2015-01-28 22:08:10 +0000 | [diff] [blame] | 5078 | if (CurInit.isInvalid()) |
| 5079 | return CurInit; |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5080 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5081 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5082 | CXXRecordDecl *Class = nullptr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5083 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5084 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 5085 | if (!Class) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5086 | return CurInit; |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5087 | |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 5088 | // C++0x [class.copy]p32: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5089 | // When certain criteria are met, an implementation is allowed to |
| 5090 | // omit the copy/move construction of a class object, even if the |
| 5091 | // copy/move constructor and/or destructor for the object have |
| 5092 | // side effects. [...] |
| 5093 | // - when a temporary class object that has not been bound to a |
| 5094 | // reference (12.2) would be copied/moved to a class object |
| 5095 | // with the same cv-unqualified type, the copy/move operation |
| 5096 | // can be omitted by constructing the temporary object |
| 5097 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5098 | // |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5099 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 5100 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5101 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 5102 | // is handled by the run-time. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5103 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5104 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5105 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5106 | // Make sure that the type we are copying is complete. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5107 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5108 | return CurInit; |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5109 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 5110 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5111 | // Only consider constructors and constructor templates. Per |
| 5112 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 5113 | // is direct-initialization. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5114 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5115 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5116 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5117 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 5118 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5119 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 5120 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5121 | case OR_Success: |
| 5122 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5123 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5124 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5125 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 5126 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 5127 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5128 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5129 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5130 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5131 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5132 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5133 | return CurInit; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5134 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5135 | case OR_Ambiguous: |
| 5136 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5137 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5138 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5139 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5140 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5141 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5142 | case OR_Deleted: |
| 5143 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5144 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5145 | << CurInitExpr->getSourceRange(); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5146 | S.NoteDeletedFunction(Best->Function); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5147 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5150 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5151 | SmallVector<Expr*, 8> ConstructorArgs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5152 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5153 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5154 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5155 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5156 | |
| 5157 | if (IsExtraneousCopy) { |
| 5158 | // If this is a totally extraneous copy for C++03 reference |
| 5159 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5160 | // expression. We don't generate an (elided) copy operation here |
| 5161 | // because doing so would require us to pass down a flag to avoid |
| 5162 | // infinite recursion, where each step adds another extraneous, |
| 5163 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5164 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5165 | // Instantiate the default arguments of any extra parameters in |
| 5166 | // the selected copy constructor, as if we were going to create a |
| 5167 | // proper call to the copy constructor. |
| 5168 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 5169 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 5170 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5171 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5172 | break; |
| 5173 | |
| 5174 | // Build the default argument expression; we don't actually care |
| 5175 | // if this succeeds or not, because this routine will complain |
| 5176 | // if there was a problem. |
| 5177 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 5178 | } |
| 5179 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5180 | return CurInitExpr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5181 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5182 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5183 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5184 | // constructor call (we might have derived-to-base conversions, or |
| 5185 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5186 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5187 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5188 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5189 | // Actually perform the constructor call. |
| 5190 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5191 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5192 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5193 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5194 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5195 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5196 | CXXConstructExpr::CK_Complete, |
| 5197 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5198 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5199 | // If we're supposed to bind temporaries, do so. |
| 5200 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5201 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5202 | return CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5203 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5204 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5205 | /// \brief Check whether elidable copy construction for binding a reference to |
| 5206 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 5207 | /// -Wc++98-compat. |
| 5208 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 5209 | const InitializedEntity &Entity, |
| 5210 | Expr *CurInitExpr) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5211 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5212 | |
| 5213 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 5214 | if (!Record) |
| 5215 | return; |
| 5216 | |
| 5217 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 5218 | if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc)) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5219 | return; |
| 5220 | |
| 5221 | // Find constructors which would have been considered. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5222 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5223 | LookupCopyAndMoveConstructors( |
| 5224 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 5225 | |
| 5226 | // Perform overload resolution. |
| 5227 | OverloadCandidateSet::iterator Best; |
| 5228 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 5229 | |
| 5230 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 5231 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 5232 | << CurInitExpr->getSourceRange(); |
| 5233 | |
| 5234 | switch (OR) { |
| 5235 | case OR_Success: |
| 5236 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 5237 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5238 | // FIXME: Check default arguments as far as that's possible. |
| 5239 | break; |
| 5240 | |
| 5241 | case OR_No_Viable_Function: |
| 5242 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5243 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5244 | break; |
| 5245 | |
| 5246 | case OR_Ambiguous: |
| 5247 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5248 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5249 | break; |
| 5250 | |
| 5251 | case OR_Deleted: |
| 5252 | S.Diag(Loc, Diag); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5253 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5254 | break; |
| 5255 | } |
| 5256 | } |
| 5257 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5258 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 5259 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5260 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5261 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 5262 | return; |
| 5263 | |
| 5264 | if (Entity.getDecl()->getDeclName()) |
| 5265 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 5266 | << Entity.getDecl()->getDeclName(); |
| 5267 | else |
| 5268 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 5269 | } |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5270 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 5271 | Entity.getMethodDecl()) |
| 5272 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 5273 | diag::note_method_return_type_change) |
| 5274 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5275 | } |
| 5276 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5277 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 5278 | return s.Kind == InitializationSequence::SK_BindReference || |
| 5279 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 5280 | } |
| 5281 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5282 | /// Returns true if the parameters describe a constructor initialization of |
| 5283 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 5284 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 5285 | const InitializationKind &Kind, |
| 5286 | unsigned NumArgs) { |
| 5287 | switch (Entity.getKind()) { |
| 5288 | case InitializedEntity::EK_Temporary: |
| 5289 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5290 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5291 | break; |
| 5292 | default: |
| 5293 | return false; |
| 5294 | } |
| 5295 | |
| 5296 | switch (Kind.getKind()) { |
| 5297 | case InitializationKind::IK_DirectList: |
| 5298 | return true; |
| 5299 | // FIXME: Hack to work around cast weirdness. |
| 5300 | case InitializationKind::IK_Direct: |
| 5301 | case InitializationKind::IK_Value: |
| 5302 | return NumArgs != 1; |
| 5303 | default: |
| 5304 | return false; |
| 5305 | } |
| 5306 | } |
| 5307 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5308 | static ExprResult |
| 5309 | PerformConstructorInitialization(Sema &S, |
| 5310 | const InitializedEntity &Entity, |
| 5311 | const InitializationKind &Kind, |
| 5312 | MultiExprArg Args, |
| 5313 | const InitializationSequence::Step& Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5314 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5315 | bool IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5316 | bool IsStdInitListInitialization, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5317 | SourceLocation LBraceLoc, |
| 5318 | SourceLocation RBraceLoc) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5319 | unsigned NumArgs = Args.size(); |
| 5320 | CXXConstructorDecl *Constructor |
| 5321 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 5322 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 5323 | |
| 5324 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5325 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5326 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 5327 | ? Kind.getEqualLoc() |
| 5328 | : Kind.getLocation(); |
| 5329 | |
| 5330 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 5331 | // Force even a trivial, implicit default constructor to be |
| 5332 | // semantically checked. We do this explicitly because we don't build |
| 5333 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 47ff122 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 5334 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5335 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | f704ade | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 5336 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5337 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 5338 | } |
| 5339 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5340 | ExprResult CurInit((Expr *)nullptr); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5341 | |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5342 | // C++ [over.match.copy]p1: |
| 5343 | // - When initializing a temporary to be bound to the first parameter |
| 5344 | // of a constructor that takes a reference to possibly cv-qualified |
| 5345 | // T as its first argument, called with a single argument in the |
| 5346 | // context of direct-initialization, explicit conversion functions |
| 5347 | // are also considered. |
| 5348 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 5349 | Args.size() == 1 && |
| 5350 | Constructor->isCopyOrMoveConstructor(); |
| 5351 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5352 | // Determine the arguments required to actually perform the constructor |
| 5353 | // call. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5354 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5355 | Loc, ConstructorArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5356 | AllowExplicitConv, |
| 5357 | IsListInitialization)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5358 | return ExprError(); |
| 5359 | |
| 5360 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5361 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5362 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5363 | S.MarkFunctionReferenced(Loc, Constructor); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5364 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 5365 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5366 | |
| 5367 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5368 | if (!TSInfo) |
| 5369 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5370 | SourceRange ParenOrBraceRange = |
| 5371 | (Kind.getKind() == InitializationKind::IK_DirectList) |
| 5372 | ? SourceRange(LBraceLoc, RBraceLoc) |
| 5373 | : Kind.getParenRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5374 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5375 | CurInit = new (S.Context) CXXTemporaryObjectExpr( |
| 5376 | S.Context, Constructor, TSInfo, ConstructorArgs, ParenOrBraceRange, |
| 5377 | HadMultipleCandidates, IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5378 | IsStdInitListInitialization, ConstructorInitRequiresZeroInit); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5379 | } else { |
| 5380 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 5381 | CXXConstructExpr::CK_Complete; |
| 5382 | |
| 5383 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5384 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 5385 | CXXConstructExpr::CK_VirtualBase : |
| 5386 | CXXConstructExpr::CK_NonVirtualBase; |
| 5387 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 5388 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 5389 | } |
| 5390 | |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 5391 | // Only get the parenthesis or brace range if it is a list initialization or |
| 5392 | // direct construction. |
| 5393 | SourceRange ParenOrBraceRange; |
| 5394 | if (IsListInitialization) |
| 5395 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 5396 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
| 5397 | ParenOrBraceRange = Kind.getParenRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5398 | |
| 5399 | // If the entity allows NRVO, mark the construction as elidable |
| 5400 | // unconditionally. |
| 5401 | if (Entity.allowsNRVO()) |
| 5402 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5403 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5404 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5405 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5406 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5407 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5408 | ConstructorInitRequiresZeroInit, |
| 5409 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 5410 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5411 | else |
| 5412 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5413 | Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5414 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5415 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5416 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5417 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5418 | ConstructorInitRequiresZeroInit, |
| 5419 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 5420 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5421 | } |
| 5422 | if (CurInit.isInvalid()) |
| 5423 | return ExprError(); |
| 5424 | |
| 5425 | // Only check access if all of that succeeded. |
| 5426 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 5427 | Step.Function.FoundDecl.getAccess()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5428 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 5429 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5430 | |
| 5431 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5432 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5433 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5434 | return CurInit; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5435 | } |
| 5436 | |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5437 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 5438 | /// longer than the current full-expression. Conservatively returns false if |
| 5439 | /// it's unclear. |
| 5440 | static bool |
| 5441 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 5442 | const InitializedEntity *Top = &Entity; |
| 5443 | while (Top->getParent()) |
| 5444 | Top = Top->getParent(); |
| 5445 | |
| 5446 | switch (Top->getKind()) { |
| 5447 | case InitializedEntity::EK_Variable: |
| 5448 | case InitializedEntity::EK_Result: |
| 5449 | case InitializedEntity::EK_Exception: |
| 5450 | case InitializedEntity::EK_Member: |
| 5451 | case InitializedEntity::EK_New: |
| 5452 | case InitializedEntity::EK_Base: |
| 5453 | case InitializedEntity::EK_Delegating: |
| 5454 | return true; |
| 5455 | |
| 5456 | case InitializedEntity::EK_ArrayElement: |
| 5457 | case InitializedEntity::EK_VectorElement: |
| 5458 | case InitializedEntity::EK_BlockElement: |
| 5459 | case InitializedEntity::EK_ComplexElement: |
| 5460 | // Could not determine what the full initialization is. Assume it might not |
| 5461 | // outlive the full-expression. |
| 5462 | return false; |
| 5463 | |
| 5464 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5465 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5466 | case InitializedEntity::EK_Temporary: |
| 5467 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5468 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5469 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5470 | // The entity being initialized might not outlive the full-expression. |
| 5471 | return false; |
| 5472 | } |
| 5473 | |
| 5474 | llvm_unreachable("unknown entity kind"); |
| 5475 | } |
| 5476 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5477 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 5478 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 5479 | /// the initialization of \p Entity. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5480 | static const InitializedEntity *getEntityForTemporaryLifetimeExtension( |
| 5481 | const InitializedEntity *Entity, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5482 | const InitializedEntity *FallbackDecl = nullptr) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5483 | // C++11 [class.temporary]p5: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5484 | switch (Entity->getKind()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5485 | case InitializedEntity::EK_Variable: |
| 5486 | // The temporary [...] persists for the lifetime of the reference |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5487 | return Entity; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5488 | |
| 5489 | case InitializedEntity::EK_Member: |
| 5490 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5491 | if (Entity->getParent()) |
| 5492 | return getEntityForTemporaryLifetimeExtension(Entity->getParent(), |
| 5493 | Entity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5494 | |
| 5495 | // except: |
| 5496 | // -- A temporary bound to a reference member in a constructor's |
| 5497 | // ctor-initializer persists until the constructor exits. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5498 | return Entity; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5499 | |
| 5500 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5501 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5502 | // -- A temporary bound to a reference parameter in a function call |
| 5503 | // persists until the completion of the full-expression containing |
| 5504 | // the call. |
| 5505 | case InitializedEntity::EK_Result: |
| 5506 | // -- The lifetime of a temporary bound to the returned value in a |
| 5507 | // function return statement is not extended; the temporary is |
| 5508 | // destroyed at the end of the full-expression in the return statement. |
| 5509 | case InitializedEntity::EK_New: |
| 5510 | // -- A temporary bound to a reference in a new-initializer persists |
| 5511 | // until the completion of the full-expression containing the |
| 5512 | // new-initializer. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5513 | return nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5514 | |
| 5515 | case InitializedEntity::EK_Temporary: |
| 5516 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5517 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5518 | // We don't yet know the storage duration of the surrounding temporary. |
| 5519 | // Assume it's got full-expression duration for now, it will patch up our |
| 5520 | // storage duration if that's not correct. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5521 | return nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5522 | |
| 5523 | case InitializedEntity::EK_ArrayElement: |
| 5524 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5525 | return getEntityForTemporaryLifetimeExtension(Entity->getParent(), |
| 5526 | FallbackDecl); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5527 | |
| 5528 | case InitializedEntity::EK_Base: |
| 5529 | case InitializedEntity::EK_Delegating: |
| 5530 | // We can reach this case for aggregate initialization in a constructor: |
| 5531 | // struct A { int &&r; }; |
| 5532 | // struct B : A { B() : A{0} {} }; |
| 5533 | // In this case, use the innermost field decl as the context. |
| 5534 | return FallbackDecl; |
| 5535 | |
| 5536 | case InitializedEntity::EK_BlockElement: |
| 5537 | case InitializedEntity::EK_LambdaCapture: |
| 5538 | case InitializedEntity::EK_Exception: |
| 5539 | case InitializedEntity::EK_VectorElement: |
| 5540 | case InitializedEntity::EK_ComplexElement: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5541 | return nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5542 | } |
Benjamin Kramer | cabc882 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 5543 | llvm_unreachable("unknown entity kind"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5544 | } |
| 5545 | |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5546 | static void performLifetimeExtension(Expr *Init, |
| 5547 | const InitializedEntity *ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5548 | |
| 5549 | /// Update a glvalue expression that is used as the initializer of a reference |
| 5550 | /// to note that its lifetime is extended. |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5551 | /// \return \c true if any temporary had its lifetime extended. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5552 | static bool |
| 5553 | performReferenceExtension(Expr *Init, |
| 5554 | const InitializedEntity *ExtendingEntity) { |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5555 | // Walk past any constructs which we can lifetime-extend across. |
| 5556 | Expr *Old; |
| 5557 | do { |
| 5558 | Old = Init; |
| 5559 | |
Richard Smith | dbc8249 | 2015-01-10 01:28:13 +0000 | [diff] [blame] | 5560 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 5561 | if (ILE->getNumInits() == 1 && ILE->isGLValue()) { |
| 5562 | // This is just redundant braces around an initializer. Step over it. |
| 5563 | Init = ILE->getInit(0); |
| 5564 | } |
| 5565 | } |
| 5566 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5567 | // Step over any subobject adjustments; we may have a materialized |
| 5568 | // temporary inside them. |
| 5569 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5570 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5571 | Init = const_cast<Expr *>( |
| 5572 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5573 | |
| 5574 | // Per current approach for DR1376, look through casts to reference type |
| 5575 | // when performing lifetime extension. |
| 5576 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 5577 | if (CE->getSubExpr()->isGLValue()) |
| 5578 | Init = CE->getSubExpr(); |
| 5579 | |
| 5580 | // FIXME: Per DR1213, subscripting on an array temporary produces an xvalue. |
| 5581 | // It's unclear if binding a reference to that xvalue extends the array |
| 5582 | // temporary. |
| 5583 | } while (Init != Old); |
| 5584 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5585 | if (MaterializeTemporaryExpr *ME = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
| 5586 | // Update the storage duration of the materialized temporary. |
| 5587 | // FIXME: Rebuild the expression instead of mutating it. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5588 | ME->setExtendingDecl(ExtendingEntity->getDecl(), |
| 5589 | ExtendingEntity->allocateManglingNumber()); |
| 5590 | performLifetimeExtension(ME->GetTemporaryExpr(), ExtendingEntity); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5591 | return true; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5592 | } |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5593 | |
| 5594 | return false; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5595 | } |
| 5596 | |
| 5597 | /// Update a prvalue expression that is going to be materialized as a |
| 5598 | /// lifetime-extended temporary. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5599 | static void performLifetimeExtension(Expr *Init, |
| 5600 | const InitializedEntity *ExtendingEntity) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5601 | // Dig out the expression which constructs the extended temporary. |
| 5602 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5603 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5604 | Init = const_cast<Expr *>( |
| 5605 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5606 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5607 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 5608 | Init = BTE->getSubExpr(); |
| 5609 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5610 | if (CXXStdInitializerListExpr *ILE = |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5611 | dyn_cast<CXXStdInitializerListExpr>(Init)) { |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5612 | performReferenceExtension(ILE->getSubExpr(), ExtendingEntity); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5613 | return; |
| 5614 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5615 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5616 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5617 | if (ILE->getType()->isArrayType()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5618 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5619 | performLifetimeExtension(ILE->getInit(I), ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5620 | return; |
| 5621 | } |
| 5622 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5623 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5624 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 5625 | |
| 5626 | // If we lifetime-extend a braced initializer which is initializing an |
| 5627 | // aggregate, and that aggregate contains reference members which are |
| 5628 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 5629 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 5630 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5631 | performReferenceExtension(ILE->getInit(0), ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5632 | else { |
| 5633 | unsigned Index = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5634 | for (const auto *I : RD->fields()) { |
Richard Smith | 0bca59d | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 5635 | if (Index >= ILE->getNumInits()) |
| 5636 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5637 | if (I->isUnnamedBitfield()) |
| 5638 | continue; |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5639 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5640 | if (I->getType()->isReferenceType()) |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5641 | performReferenceExtension(SubInit, ExtendingEntity); |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5642 | else if (isa<InitListExpr>(SubInit) || |
| 5643 | isa<CXXStdInitializerListExpr>(SubInit)) |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5644 | // This may be either aggregate-initialization of a member or |
| 5645 | // initialization of a std::initializer_list object. Either way, |
| 5646 | // we should recursively lifetime-extend that initializer. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5647 | performLifetimeExtension(SubInit, ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5648 | ++Index; |
| 5649 | } |
| 5650 | } |
| 5651 | } |
| 5652 | } |
| 5653 | } |
| 5654 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5655 | static void warnOnLifetimeExtension(Sema &S, const InitializedEntity &Entity, |
| 5656 | const Expr *Init, bool IsInitializerList, |
| 5657 | const ValueDecl *ExtendingDecl) { |
| 5658 | // Warn if a field lifetime-extends a temporary. |
| 5659 | if (isa<FieldDecl>(ExtendingDecl)) { |
| 5660 | if (IsInitializerList) { |
| 5661 | S.Diag(Init->getExprLoc(), diag::warn_dangling_std_initializer_list) |
| 5662 | << /*at end of constructor*/true; |
| 5663 | return; |
| 5664 | } |
| 5665 | |
| 5666 | bool IsSubobjectMember = false; |
| 5667 | for (const InitializedEntity *Ent = Entity.getParent(); Ent; |
| 5668 | Ent = Ent->getParent()) { |
| 5669 | if (Ent->getKind() != InitializedEntity::EK_Base) { |
| 5670 | IsSubobjectMember = true; |
| 5671 | break; |
| 5672 | } |
| 5673 | } |
| 5674 | S.Diag(Init->getExprLoc(), |
| 5675 | diag::warn_bind_ref_member_to_temporary) |
| 5676 | << ExtendingDecl << Init->getSourceRange() |
| 5677 | << IsSubobjectMember << IsInitializerList; |
| 5678 | if (IsSubobjectMember) |
| 5679 | S.Diag(ExtendingDecl->getLocation(), |
| 5680 | diag::note_ref_subobject_of_member_declared_here); |
| 5681 | else |
| 5682 | S.Diag(ExtendingDecl->getLocation(), |
| 5683 | diag::note_ref_or_ptr_member_declared_here) |
| 5684 | << /*is pointer*/false; |
| 5685 | } |
| 5686 | } |
| 5687 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5688 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 5689 | const ImplicitConversionSequence &ICS, |
| 5690 | QualType PreNarrowingType, |
| 5691 | QualType EntityType, |
| 5692 | const Expr *PostInit); |
| 5693 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5694 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5695 | InitializationSequence::Perform(Sema &S, |
| 5696 | const InitializedEntity &Entity, |
| 5697 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5698 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5699 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5700 | if (Failed()) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5701 | Diagnose(S, Entity, Kind, Args); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5702 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5703 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5704 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5705 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5706 | // If the declaration is a non-dependent, incomplete array type |
| 5707 | // that has an initializer, then its type will be completed once |
| 5708 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5709 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5710 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5711 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5712 | if (const IncompleteArrayType *ArrayT |
| 5713 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 5714 | // FIXME: We don't currently have the ability to accurately |
| 5715 | // compute the length of an initializer list without |
| 5716 | // performing full type-checking of the initializer list |
| 5717 | // (since we have to determine where braces are implicitly |
| 5718 | // introduced and such). So, we fall back to making the array |
| 5719 | // type a dependently-sized array type with no specified |
| 5720 | // bound. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5721 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5722 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5723 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5724 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5725 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 5726 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 5727 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5728 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 5729 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 5730 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5731 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5732 | } |
| 5733 | |
| 5734 | *ResultType |
| 5735 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5736 | /*NumElts=*/nullptr, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5737 | ArrayT->getSizeModifier(), |
| 5738 | ArrayT->getIndexTypeCVRQualifiers(), |
| 5739 | Brackets); |
| 5740 | } |
| 5741 | |
| 5742 | } |
| 5743 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5744 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 5745 | !Kind.isExplicitCast()) { |
| 5746 | // Rebuild the ParenListExpr. |
| 5747 | SourceRange ParenRange = Kind.getParenRange(); |
| 5748 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5749 | Args); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5750 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5751 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | bf13895 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 5752 | Kind.isExplicitCast() || |
| 5753 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5754 | return ExprResult(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5755 | } |
| 5756 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5757 | // No steps means no initialization. |
| 5758 | if (Steps.empty()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5759 | return ExprResult((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5760 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5761 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5762 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5763 | !Entity.isParameterKind()) { |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5764 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 5765 | // from an initializer list. For parameters, we produce a better warning |
| 5766 | // elsewhere. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5767 | Expr *Init = Args[0]; |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5768 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 5769 | << Init->getSourceRange(); |
| 5770 | } |
| 5771 | |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5772 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 5773 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5774 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5775 | Entity.getType()->isPointerType() && |
| 5776 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5777 | Expr *Init = Args[0]; |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5778 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 5779 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 5780 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 5781 | << Init->getSourceRange(); |
| 5782 | } |
| 5783 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5784 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 5785 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5786 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 5787 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5788 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5789 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5790 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5791 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5792 | ExprResult CurInit((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5793 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5794 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5795 | // grab the only argument out the Args and place it into the "current" |
| 5796 | // initializer. |
| 5797 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5798 | case SK_ResolveAddressOfOverloadedFunction: |
| 5799 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5800 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5801 | case SK_CastDerivedToBaseLValue: |
| 5802 | case SK_BindReference: |
| 5803 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5804 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5805 | case SK_UserConversion: |
| 5806 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5807 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5808 | case SK_QualificationConversionRValue: |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 5809 | case SK_AtomicConversion: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5810 | case SK_LValueToRValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5811 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5812 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5813 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5814 | case SK_UnwrapInitList: |
| 5815 | case SK_RewrapInitList: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5816 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5817 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5818 | case SK_ObjCObjectConversion: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5819 | case SK_ArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5820 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5821 | case SK_PassByIndirectCopyRestore: |
| 5822 | case SK_PassByIndirectRestore: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5823 | case SK_ProduceObjCObject: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5824 | case SK_StdInitializerList: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5825 | case SK_OCLSamplerInit: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5826 | case SK_OCLZeroEvent: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5827 | assert(Args.size() == 1); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5828 | CurInit = Args[0]; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5829 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5830 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5831 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5832 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5833 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 5834 | case SK_ConstructorInitializationFromList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 5835 | case SK_StdInitializerListConstructorCall: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5836 | case SK_ZeroInitialization: |
| 5837 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5838 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5839 | |
| 5840 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5841 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5842 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5843 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5844 | Step != StepEnd; ++Step) { |
| 5845 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5846 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5847 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5848 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5849 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5850 | switch (Step->Kind) { |
| 5851 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5852 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5853 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5854 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5855 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 5856 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5857 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5858 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5859 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5860 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5861 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5862 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5863 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5864 | case SK_CastDerivedToBaseLValue: { |
| 5865 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5866 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5867 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5868 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5869 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5870 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5871 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5872 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5873 | CurInit.get()->getLocStart(), |
| 5874 | CurInit.get()->getSourceRange(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5875 | &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5876 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5877 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5878 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5879 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5880 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5881 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5882 | VK_XValue : |
| 5883 | VK_RValue); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5884 | CurInit = |
| 5885 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase, |
| 5886 | CurInit.get(), &BasePath, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5887 | break; |
| 5888 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5889 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5890 | case SK_BindReference: |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5891 | // References cannot bind to bit-fields (C++ [dcl.init.ref]p5). |
| 5892 | if (CurInit.get()->refersToBitField()) { |
| 5893 | // We don't necessarily have an unambiguous source bit-field. |
| 5894 | FieldDecl *BitField = CurInit.get()->getSourceBitField(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5895 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5896 | << Entity.getType().isVolatileQualified() |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5897 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5898 | << (BitField != nullptr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5899 | << CurInit.get()->getSourceRange(); |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5900 | if (BitField) |
| 5901 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 5902 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5903 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5904 | } |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5905 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5906 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | c17ae44 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5907 | // References cannot bind to vector elements. |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5908 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5909 | << Entity.getType().isVolatileQualified() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5910 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5911 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5912 | return ExprError(); |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5913 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5914 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5915 | // Reference binding does not have any corresponding ASTs. |
| 5916 | |
| 5917 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5918 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5919 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5920 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5921 | // Even though we didn't materialize a temporary, the binding may still |
| 5922 | // extend the lifetime of a temporary. This happens if we bind a reference |
| 5923 | // to the result of a cast to reference type. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5924 | if (const InitializedEntity *ExtendingEntity = |
| 5925 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 5926 | if (performReferenceExtension(CurInit.get(), ExtendingEntity)) |
| 5927 | warnOnLifetimeExtension(S, Entity, CurInit.get(), |
| 5928 | /*IsInitializerList=*/false, |
| 5929 | ExtendingEntity->getDecl()); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5930 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5931 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5932 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5933 | case SK_BindReferenceToTemporary: { |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5934 | // Make sure the "temporary" is actually an rvalue. |
| 5935 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 5936 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5937 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5938 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5939 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5940 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5941 | // Materialize the temporary into memory. |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5942 | MaterializeTemporaryExpr *MTE = new (S.Context) MaterializeTemporaryExpr( |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5943 | Entity.getType().getNonReferenceType(), CurInit.get(), |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5944 | Entity.getType()->isLValueReferenceType()); |
| 5945 | |
| 5946 | // Maybe lifetime-extend the temporary's subobjects to match the |
| 5947 | // entity's lifetime. |
| 5948 | if (const InitializedEntity *ExtendingEntity = |
| 5949 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 5950 | if (performReferenceExtension(MTE, ExtendingEntity)) |
| 5951 | warnOnLifetimeExtension(S, Entity, CurInit.get(), /*IsInitializerList=*/false, |
| 5952 | ExtendingEntity->getDecl()); |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5953 | |
| 5954 | // 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] | 5955 | // need cleanups. Likewise if we're extending this temporary to automatic |
| 5956 | // storage duration -- we need to register its cleanup during the |
| 5957 | // full-expression's cleanups. |
| 5958 | if ((S.getLangOpts().ObjCAutoRefCount && |
| 5959 | MTE->getType()->isObjCLifetimeType()) || |
| 5960 | (MTE->getStorageDuration() == SD_Automatic && |
| 5961 | MTE->getType().isDestructedType())) |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5962 | S.ExprNeedsCleanups = true; |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5963 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5964 | CurInit = MTE; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5965 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5966 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5967 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5968 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5969 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5970 | /*IsExtraneousCopy=*/true); |
| 5971 | break; |
| 5972 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5973 | case SK_UserConversion: { |
| 5974 | // We have a user-defined conversion that invokes either a constructor |
| 5975 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5976 | CastKind CastKind; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5977 | bool IsCopy = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5978 | FunctionDecl *Fn = Step->Function.Function; |
| 5979 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5980 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5981 | bool CreatedObject = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5982 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5983 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5984 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5985 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5986 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5987 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5988 | // Determine the arguments required to actually perform the constructor |
| 5989 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5990 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5991 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5992 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5993 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5994 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5995 | |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5996 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5997 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5998 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5999 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6000 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6001 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 6002 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 6003 | CXXConstructExpr::CK_Complete, |
| 6004 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6005 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6006 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 6007 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 6008 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6009 | FoundFn.getAccess()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6010 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 6011 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6012 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6013 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6014 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 6015 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 6016 | S.IsDerivedFrom(SourceType, Class)) |
| 6017 | IsCopy = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6018 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6019 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6020 | } else { |
| 6021 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 6022 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6023 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 6024 | FoundFn); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6025 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 6026 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6027 | |
| 6028 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6029 | // derived-to-base conversion? I believe the answer is "no", because |
| 6030 | // 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] | 6031 | ExprResult CurInitExprRes = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6032 | S.PerformObjectArgumentInitialization(CurInit.get(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6033 | /*Qualifier=*/nullptr, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6034 | FoundFn, Conversion); |
| 6035 | if(CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6036 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6037 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6038 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6039 | // Build the actual call to the conversion function. |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6040 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 6041 | HadMultipleCandidates); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6042 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6043 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6044 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6045 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6046 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6047 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6048 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6049 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 6050 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 6051 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 6052 | |
| 6053 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6054 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6055 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6056 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 6057 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6058 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6059 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 6060 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6061 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart())) |
| 6062 | return ExprError(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6063 | } |
| 6064 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6065 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6066 | CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(), |
| 6067 | CastKind, CurInit.get(), nullptr, |
| 6068 | CurInit.get()->getValueKind()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 6069 | if (MaybeBindToTemp) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6070 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 6071 | if (RequiresCopy) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6072 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6073 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6074 | break; |
| 6075 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6076 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6077 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6078 | case SK_QualificationConversionXValue: |
| 6079 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6080 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6081 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6082 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6083 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6084 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6085 | VK_XValue : |
| 6086 | VK_RValue); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6087 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6088 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6089 | } |
| 6090 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 6091 | case SK_AtomicConversion: { |
| 6092 | assert(CurInit.get()->isRValue() && "cannot convert glvalue to atomic"); |
| 6093 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
| 6094 | CK_NonAtomicToAtomic, VK_RValue); |
| 6095 | break; |
| 6096 | } |
| 6097 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6098 | case SK_LValueToRValue: { |
| 6099 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6100 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 6101 | CK_LValueToRValue, CurInit.get(), |
| 6102 | /*BasePath=*/nullptr, VK_RValue); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6103 | break; |
| 6104 | } |
| 6105 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6106 | case SK_ConversionSequence: |
| 6107 | case SK_ConversionSequenceNoNarrowing: { |
| 6108 | Sema::CheckedConversionKind CCK |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6109 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 6110 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 6111 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6112 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6113 | ExprResult CurInitExprRes = |
| 6114 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6115 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6116 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6117 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6118 | CurInit = CurInitExprRes; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6119 | |
| 6120 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
| 6121 | S.getLangOpts().CPlusPlus && !CurInit.get()->isValueDependent()) |
| 6122 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 6123 | CurInit.get()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6124 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 6125 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6126 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6127 | case SK_ListInitialization: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6128 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6129 | // If we're not initializing the top-level entity, we need to create an |
| 6130 | // InitializeTemporary entity for our target type. |
| 6131 | QualType Ty = Step->Type; |
| 6132 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6133 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 6134 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 6135 | InitListChecker PerformInitList(S, InitEntity, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 6136 | InitList, Ty, /*VerifyOnly=*/false); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6137 | if (PerformInitList.HadError()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6138 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6139 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6140 | // Hack: We must update *ResultType if available in order to set the |
| 6141 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 6142 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 6143 | if (ResultType && |
| 6144 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6145 | if ((*ResultType)->isRValueReferenceType()) |
| 6146 | Ty = S.Context.getRValueReferenceType(Ty); |
| 6147 | else if ((*ResultType)->isLValueReferenceType()) |
| 6148 | Ty = S.Context.getLValueReferenceType(Ty, |
| 6149 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 6150 | *ResultType = Ty; |
| 6151 | } |
| 6152 | |
| 6153 | InitListExpr *StructuredInitList = |
| 6154 | PerformInitList.getFullyStructuredList(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6155 | CurInit.get(); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 6156 | CurInit = shouldBindAsTemporary(InitEntity) |
| 6157 | ? S.MaybeBindToTemporary(StructuredInitList) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6158 | : StructuredInitList; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6159 | break; |
| 6160 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6161 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6162 | case SK_ConstructorInitializationFromList: { |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6163 | // When an initializer list is passed for a parameter of type "reference |
| 6164 | // to object", we don't get an EK_Temporary entity, but instead an |
| 6165 | // EK_Parameter entity with reference type. |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6166 | // FIXME: This is a hack. What we really should do is create a user |
| 6167 | // conversion step for this case, but this makes it considerably more |
| 6168 | // complicated. For now, this will do. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6169 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6170 | Entity.getType().getNonReferenceType()); |
| 6171 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6172 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6173 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 6174 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 6175 | << InitList->getSourceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6176 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6177 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 6178 | Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6179 | Kind, Arg, *Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6180 | ConstructorInitRequiresZeroInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6181 | /*IsListInitialization*/true, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6182 | /*IsStdInitListInit*/false, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6183 | InitList->getLBraceLoc(), |
| 6184 | InitList->getRBraceLoc()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6185 | break; |
| 6186 | } |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6187 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6188 | case SK_UnwrapInitList: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6189 | CurInit = cast<InitListExpr>(CurInit.get())->getInit(0); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6190 | break; |
| 6191 | |
| 6192 | case SK_RewrapInitList: { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6193 | Expr *E = CurInit.get(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6194 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 6195 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 6196 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6197 | ILE->setSyntacticForm(Syntactic); |
| 6198 | ILE->setType(E->getType()); |
| 6199 | ILE->setValueKind(E->getValueKind()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6200 | CurInit = ILE; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6201 | break; |
| 6202 | } |
| 6203 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6204 | case SK_ConstructorInitialization: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6205 | case SK_StdInitializerListConstructorCall: { |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6206 | // When an initializer list is passed for a parameter of type "reference |
| 6207 | // to object", we don't get an EK_Temporary entity, but instead an |
| 6208 | // EK_Parameter entity with reference type. |
| 6209 | // FIXME: This is a hack. What we really should do is create a user |
| 6210 | // conversion step for this case, but this makes it considerably more |
| 6211 | // complicated. For now, this will do. |
| 6212 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6213 | Entity.getType().getNonReferenceType()); |
| 6214 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6215 | bool IsStdInitListInit = |
| 6216 | Step->Kind == SK_StdInitializerListConstructorCall; |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6217 | CurInit = PerformConstructorInitialization( |
| 6218 | S, UseTemporary ? TempEntity : Entity, Kind, Args, *Step, |
| 6219 | ConstructorInitRequiresZeroInit, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 6220 | /*IsListInitialization*/IsStdInitListInit, |
| 6221 | /*IsStdInitListInitialization*/IsStdInitListInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6222 | /*LBraceLoc*/SourceLocation(), |
| 6223 | /*RBraceLoc*/SourceLocation()); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6224 | break; |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6225 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6226 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6227 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6228 | step_iterator NextStep = Step; |
| 6229 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6230 | if (NextStep != StepEnd && |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6231 | (NextStep->Kind == SK_ConstructorInitialization || |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6232 | NextStep->Kind == SK_ConstructorInitializationFromList)) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6233 | // The need for zero-initialization is recorded directly into |
| 6234 | // the call to the object's constructor within the next step. |
| 6235 | ConstructorInitRequiresZeroInit = true; |
| 6236 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6237 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6238 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6239 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6240 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6241 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6242 | Kind.getRange().getBegin()); |
| 6243 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6244 | CurInit = new (S.Context) CXXScalarValueInitExpr( |
| 6245 | TSInfo->getType().getNonLValueExprType(S.Context), TSInfo, |
| 6246 | Kind.getRange().getEnd()); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6247 | } else { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6248 | CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6249 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6250 | break; |
| 6251 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6252 | |
| 6253 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6254 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6255 | ExprResult Result = CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6256 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 6257 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 6258 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6259 | if (Result.isInvalid()) |
| 6260 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6261 | CurInit = Result; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6262 | |
| 6263 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6264 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6265 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6266 | Entity.isParameterKind() && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6267 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6268 | == Sema::Compatible) |
| 6269 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6270 | if (CurInitExprRes.isInvalid()) |
| 6271 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6272 | CurInit = CurInitExprRes; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6273 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6274 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6275 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 6276 | Step->Type, SourceType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6277 | CurInit.get(), |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 6278 | getAssignmentAction(Entity, true), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6279 | &Complained)) { |
| 6280 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6281 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6282 | } else if (Complained) |
| 6283 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6284 | break; |
| 6285 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6286 | |
| 6287 | case SK_StringInit: { |
| 6288 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6289 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 6290 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6291 | break; |
| 6292 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6293 | |
| 6294 | case SK_ObjCObjectConversion: |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6295 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6296 | CK_ObjCObjectLValueCast, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 6297 | CurInit.get()->getValueKind()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6298 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6299 | |
| 6300 | case SK_ArrayInit: |
| 6301 | // Okay: we checked everything before creating this step. Note that |
| 6302 | // this is a GNU extension. |
| 6303 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6304 | << Step->Type << CurInit.get()->getType() |
| 6305 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6306 | |
| 6307 | // If the destination type is an incomplete array type, update the |
| 6308 | // type accordingly. |
| 6309 | if (ResultType) { |
| 6310 | if (const IncompleteArrayType *IncompleteDest |
| 6311 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 6312 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6313 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6314 | *ResultType = S.Context.getConstantArrayType( |
| 6315 | IncompleteDest->getElementType(), |
| 6316 | ConstantSource->getSize(), |
| 6317 | ArrayType::Normal, 0); |
| 6318 | } |
| 6319 | } |
| 6320 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6321 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6322 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6323 | case SK_ParenthesizedArrayInit: |
| 6324 | // Okay: we checked everything before creating this step. Note that |
| 6325 | // this is a GNU extension. |
| 6326 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 6327 | << CurInit.get()->getSourceRange(); |
| 6328 | break; |
| 6329 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6330 | case SK_PassByIndirectCopyRestore: |
| 6331 | case SK_PassByIndirectRestore: |
| 6332 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6333 | CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr( |
| 6334 | CurInit.get(), Step->Type, |
| 6335 | Step->Kind == SK_PassByIndirectCopyRestore); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6336 | break; |
| 6337 | |
| 6338 | case SK_ProduceObjCObject: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6339 | CurInit = |
| 6340 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject, |
| 6341 | CurInit.get(), nullptr, VK_RValue); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6342 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6343 | |
| 6344 | case SK_StdInitializerList: { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6345 | S.Diag(CurInit.get()->getExprLoc(), |
| 6346 | diag::warn_cxx98_compat_initializer_list_init) |
| 6347 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 249dee5 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 6348 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6349 | // Materialize the temporary into memory. |
| 6350 | MaterializeTemporaryExpr *MTE = new (S.Context) |
| 6351 | MaterializeTemporaryExpr(CurInit.get()->getType(), CurInit.get(), |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6352 | /*BoundToLvalueReference=*/false); |
| 6353 | |
| 6354 | // Maybe lifetime-extend the array temporary's subobjects to match the |
| 6355 | // entity's lifetime. |
| 6356 | if (const InitializedEntity *ExtendingEntity = |
| 6357 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 6358 | if (performReferenceExtension(MTE, ExtendingEntity)) |
| 6359 | warnOnLifetimeExtension(S, Entity, CurInit.get(), |
| 6360 | /*IsInitializerList=*/true, |
| 6361 | ExtendingEntity->getDecl()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6362 | |
| 6363 | // Wrap it in a construction of a std::initializer_list<T>. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6364 | CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6365 | |
| 6366 | // Bind the result, in case the library has given initializer_list a |
| 6367 | // non-trivial destructor. |
| 6368 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6369 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6370 | break; |
| 6371 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6372 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6373 | case SK_OCLSamplerInit: { |
| 6374 | assert(Step->Type->isSamplerT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 6375 | "Sampler initialization on non-sampler type."); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6376 | |
| 6377 | QualType SourceType = CurInit.get()->getType(); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6378 | |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6379 | if (Entity.isParameterKind()) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6380 | if (!SourceType->isSamplerT()) |
| 6381 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 6382 | << SourceType; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6383 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6384 | llvm_unreachable("Invalid EntityKind!"); |
| 6385 | } |
| 6386 | |
| 6387 | break; |
| 6388 | } |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6389 | case SK_OCLZeroEvent: { |
| 6390 | assert(Step->Type->isEventT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 6391 | "Event initialization on non-event type."); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6392 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6393 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6394 | CK_ZeroToOCLEvent, |
| 6395 | CurInit.get()->getValueKind()); |
| 6396 | break; |
| 6397 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6398 | } |
| 6399 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6400 | |
| 6401 | // Diagnose non-fatal problems with the completed initialization. |
| 6402 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6403 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 6404 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 6405 | cast<FieldDecl>(Entity.getDecl()), |
| 6406 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6407 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6408 | return CurInit; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6409 | } |
| 6410 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6411 | /// Somewhere within T there is an uninitialized reference subobject. |
| 6412 | /// Dig it out and diagnose it. |
Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 6413 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 6414 | QualType T) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6415 | if (T->isReferenceType()) { |
| 6416 | S.Diag(Loc, diag::err_reference_without_init) |
| 6417 | << T.getNonReferenceType(); |
| 6418 | return true; |
| 6419 | } |
| 6420 | |
| 6421 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6422 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 6423 | return false; |
| 6424 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6425 | for (const auto *FI : RD->fields()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6426 | if (FI->isUnnamedBitfield()) |
| 6427 | continue; |
| 6428 | |
| 6429 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 6430 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6431 | return true; |
| 6432 | } |
| 6433 | } |
| 6434 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6435 | for (const auto &BI : RD->bases()) { |
| 6436 | if (DiagnoseUninitializedReference(S, BI.getLocStart(), BI.getType())) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6437 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6438 | return true; |
| 6439 | } |
| 6440 | } |
| 6441 | |
| 6442 | return false; |
| 6443 | } |
| 6444 | |
| 6445 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6446 | //===----------------------------------------------------------------------===// |
| 6447 | // Diagnose initialization failures |
| 6448 | //===----------------------------------------------------------------------===// |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6449 | |
| 6450 | /// Emit notes associated with an initialization that failed due to a |
| 6451 | /// "simple" conversion failure. |
| 6452 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 6453 | Expr *op) { |
| 6454 | QualType destType = entity.getType(); |
| 6455 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 6456 | op->getType()->isObjCObjectPointerType()) { |
| 6457 | |
| 6458 | // Emit a possible note about the conversion failing because the |
| 6459 | // operand is a message send with a related result type. |
| 6460 | S.EmitRelatedResultTypeNote(op); |
| 6461 | |
| 6462 | // Emit a possible note about a return failing because we're |
| 6463 | // expecting a related result type. |
| 6464 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 6465 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 6466 | } |
| 6467 | } |
| 6468 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6469 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 6470 | InitListExpr *InitList) { |
| 6471 | QualType DestType = Entity.getType(); |
| 6472 | |
| 6473 | QualType E; |
| 6474 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 6475 | QualType ArrayType = S.Context.getConstantArrayType( |
| 6476 | E.withConst(), |
| 6477 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 6478 | InitList->getNumInits()), |
| 6479 | clang::ArrayType::Normal, 0); |
| 6480 | InitializedEntity HiddenArray = |
| 6481 | InitializedEntity::InitializeTemporary(ArrayType); |
| 6482 | return diagnoseListInit(S, HiddenArray, InitList); |
| 6483 | } |
| 6484 | |
Richard Smith | 8d082d1 | 2014-09-04 22:13:39 +0000 | [diff] [blame] | 6485 | if (DestType->isReferenceType()) { |
| 6486 | // A list-initialization failure for a reference means that we tried to |
| 6487 | // create a temporary of the inner type (per [dcl.init.list]p3.6) and the |
| 6488 | // inner initialization failed. |
| 6489 | QualType T = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 6490 | diagnoseListInit(S, InitializedEntity::InitializeTemporary(T), InitList); |
| 6491 | SourceLocation Loc = InitList->getLocStart(); |
| 6492 | if (auto *D = Entity.getDecl()) |
| 6493 | Loc = D->getLocation(); |
| 6494 | S.Diag(Loc, diag::note_in_reference_temporary_list_initializer) << T; |
| 6495 | return; |
| 6496 | } |
| 6497 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6498 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
| 6499 | /*VerifyOnly=*/false); |
| 6500 | assert(DiagnoseInitList.HadError() && |
| 6501 | "Inconsistent init list check result."); |
| 6502 | } |
| 6503 | |
Nico Weber | 9386c82 | 2014-07-23 05:16:10 +0000 | [diff] [blame] | 6504 | /// Prints a fixit for adding a null initializer for |Entity|. Call this only |
| 6505 | /// right after emitting a diagnostic. |
| 6506 | static void maybeEmitZeroInitializationFixit(Sema &S, |
| 6507 | InitializationSequence &Sequence, |
| 6508 | const InitializedEntity &Entity) { |
| 6509 | if (Entity.getKind() != InitializedEntity::EK_Variable) |
| 6510 | return; |
| 6511 | |
| 6512 | VarDecl *VD = cast<VarDecl>(Entity.getDecl()); |
| 6513 | if (VD->getInit() || VD->getLocEnd().isMacroID()) |
| 6514 | return; |
| 6515 | |
| 6516 | QualType VariableTy = VD->getType().getCanonicalType(); |
| 6517 | SourceLocation Loc = S.getLocForEndOfToken(VD->getLocEnd()); |
| 6518 | std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc); |
| 6519 | |
| 6520 | S.Diag(Loc, diag::note_add_initializer) |
| 6521 | << VD << FixItHint::CreateInsertion(Loc, Init); |
| 6522 | } |
| 6523 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6524 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6525 | const InitializedEntity &Entity, |
| 6526 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6527 | ArrayRef<Expr *> Args) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 6528 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6529 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6530 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 6531 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6532 | switch (Failure) { |
| 6533 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6534 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6535 | if (Args.empty()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6536 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 6537 | // If this is value-initialization, this could be nested some way within |
| 6538 | // the target type. |
| 6539 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 6540 | DestType->isReferenceType()); |
| 6541 | bool Diagnosed = |
| 6542 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 6543 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 6544 | (void)Diagnosed; |
| 6545 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6546 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6547 | << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd()); |
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 | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6550 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6551 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6552 | break; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6553 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6554 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 6555 | break; |
| 6556 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6557 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 6558 | break; |
| 6559 | case FK_NarrowStringIntoWideCharArray: |
| 6560 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 6561 | break; |
| 6562 | case FK_WideStringIntoCharArray: |
| 6563 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 6564 | break; |
| 6565 | case FK_IncompatWideStringIntoWideChar: |
| 6566 | S.Diag(Kind.getLocation(), |
| 6567 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 6568 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6569 | case FK_ArrayTypeMismatch: |
| 6570 | case FK_NonConstantArrayInit: |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6571 | S.Diag(Kind.getLocation(), |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6572 | (Failure == FK_ArrayTypeMismatch |
| 6573 | ? diag::err_array_init_different_type |
| 6574 | : diag::err_array_init_non_constant_array)) |
| 6575 | << DestType.getNonReferenceType() |
| 6576 | << Args[0]->getType() |
| 6577 | << Args[0]->getSourceRange(); |
| 6578 | break; |
| 6579 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6580 | case FK_VariableLengthArrayHasInitializer: |
| 6581 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 6582 | << Args[0]->getSourceRange(); |
| 6583 | break; |
| 6584 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6585 | case FK_AddressOfOverloadFailed: { |
| 6586 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6587 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6588 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6589 | true, |
| 6590 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6591 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6592 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6593 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6594 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 6595 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6596 | switch (FailedOverloadResult) { |
| 6597 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6598 | if (Failure == FK_UserConversionOverloadFailed) |
| 6599 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 6600 | << Args[0]->getType() << DestType |
| 6601 | << Args[0]->getSourceRange(); |
| 6602 | else |
| 6603 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 6604 | << DestType << Args[0]->getType() |
| 6605 | << Args[0]->getSourceRange(); |
| 6606 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6607 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6608 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6609 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6610 | case OR_No_Viable_Function: |
Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 6611 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 6612 | DestType.getNonReferenceType(), |
| 6613 | diag::err_typecheck_nonviable_condition_incomplete, |
| 6614 | Args[0]->getType(), Args[0]->getSourceRange())) |
| 6615 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 6616 | << Args[0]->getType() << Args[0]->getSourceRange() |
| 6617 | << DestType.getNonReferenceType(); |
| 6618 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6619 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6620 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6621 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6622 | case OR_Deleted: { |
| 6623 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 6624 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 6625 | << Args[0]->getSourceRange(); |
| 6626 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6627 | OverloadingResult Ovl |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6628 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 6629 | true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6630 | if (Ovl == OR_Deleted) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6631 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6632 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6633 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6634 | } |
| 6635 | break; |
| 6636 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6637 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6638 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6639 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6640 | } |
| 6641 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6642 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6643 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6644 | if (isa<InitListExpr>(Args[0])) { |
| 6645 | S.Diag(Kind.getLocation(), |
| 6646 | diag::err_lvalue_reference_bind_to_initlist) |
| 6647 | << DestType.getNonReferenceType().isVolatileQualified() |
| 6648 | << DestType.getNonReferenceType() |
| 6649 | << Args[0]->getSourceRange(); |
| 6650 | break; |
| 6651 | } |
| 6652 | // Intentional fallthrough |
| 6653 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6654 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6655 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6656 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 6657 | ? diag::err_lvalue_reference_bind_to_temporary |
| 6658 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 6659 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6660 | << DestType.getNonReferenceType() |
| 6661 | << Args[0]->getType() |
| 6662 | << Args[0]->getSourceRange(); |
| 6663 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6664 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6665 | case FK_RValueReferenceBindingToLValue: |
| 6666 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | bed28f7 | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 6667 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6668 | << Args[0]->getSourceRange(); |
| 6669 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6670 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6671 | case FK_ReferenceInitDropsQualifiers: |
| 6672 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 6673 | << DestType.getNonReferenceType() |
| 6674 | << Args[0]->getType() |
| 6675 | << Args[0]->getSourceRange(); |
| 6676 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6677 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6678 | case FK_ReferenceInitFailed: |
| 6679 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 6680 | << DestType.getNonReferenceType() |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6681 | << Args[0]->isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6682 | << Args[0]->getType() |
| 6683 | << Args[0]->getSourceRange(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6684 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6685 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6686 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6687 | case FK_ConversionFailed: { |
| 6688 | QualType FromType = Args[0]->getType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6689 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6690 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6691 | << DestType |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6692 | << Args[0]->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6693 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6694 | << Args[0]->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6695 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 6696 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6697 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6698 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6699 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6700 | |
| 6701 | case FK_ConversionFromPropertyFailed: |
| 6702 | // No-op. This error has already been reported. |
| 6703 | break; |
| 6704 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6705 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6706 | SourceRange R; |
| 6707 | |
| 6708 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6709 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6710 | InitList->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6711 | else |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6712 | R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6713 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 6714 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6715 | if (Kind.isCStyleOrFunctionalCast()) |
| 6716 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 6717 | << R; |
| 6718 | else |
| 6719 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 6720 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6721 | break; |
| 6722 | } |
| 6723 | |
| 6724 | case FK_ReferenceBindingToInitList: |
| 6725 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 6726 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 6727 | break; |
| 6728 | |
| 6729 | case FK_InitListBadDestinationType: |
| 6730 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 6731 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 6732 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6733 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6734 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6735 | case FK_ConstructorOverloadFailed: { |
| 6736 | SourceRange ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6737 | if (Args.size()) |
| 6738 | ArgsRange = SourceRange(Args.front()->getLocStart(), |
| 6739 | Args.back()->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6740 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6741 | if (Failure == FK_ListConstructorOverloadFailed) { |
Nico Weber | 9709ebf | 2014-07-08 23:54:25 +0000 | [diff] [blame] | 6742 | assert(Args.size() == 1 && |
| 6743 | "List construction from other than 1 argument."); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6744 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6745 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6746 | } |
| 6747 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6748 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 6749 | // bad. |
| 6750 | switch (FailedOverloadResult) { |
| 6751 | case OR_Ambiguous: |
| 6752 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 6753 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6754 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6755 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6756 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6757 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6758 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 6759 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 6760 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 6761 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6762 | // This is implicit default initialization of a member or |
| 6763 | // base within a constructor. If no viable function was |
| 6764 | // found, notify the user that she needs to explicitly |
| 6765 | // initialize this base/member. |
| 6766 | CXXConstructorDecl *Constructor |
| 6767 | = cast<CXXConstructorDecl>(S.CurContext); |
| 6768 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6769 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6770 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6771 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6772 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6773 | << /*base=*/0 |
| 6774 | << Entity.getType(); |
| 6775 | |
| 6776 | RecordDecl *BaseDecl |
| 6777 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 6778 | ->getDecl(); |
| 6779 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 6780 | << S.Context.getTagDeclType(BaseDecl); |
| 6781 | } else { |
| 6782 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6783 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6784 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6785 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6786 | << /*member=*/1 |
| 6787 | << Entity.getName(); |
Alp Toker | 2afa878 | 2014-05-28 12:20:14 +0000 | [diff] [blame] | 6788 | S.Diag(Entity.getDecl()->getLocation(), |
| 6789 | diag::note_member_declared_at); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6790 | |
| 6791 | if (const RecordType *Record |
| 6792 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6793 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6794 | diag::note_previous_decl) |
| 6795 | << S.Context.getTagDeclType(Record->getDecl()); |
| 6796 | } |
| 6797 | break; |
| 6798 | } |
| 6799 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6800 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 6801 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6802 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6803 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6804 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6805 | case OR_Deleted: { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6806 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6807 | OverloadingResult Ovl |
| 6808 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6809 | if (Ovl != OR_Deleted) { |
| 6810 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6811 | << true << DestType << ArgsRange; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6812 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6813 | break; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6814 | } |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6815 | |
| 6816 | // If this is a defaulted or implicitly-declared function, then |
| 6817 | // it was implicitly deleted. Make it clear that the deletion was |
| 6818 | // implicit. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6819 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6820 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6821 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6822 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6823 | else |
| 6824 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6825 | << true << DestType << ArgsRange; |
| 6826 | |
| 6827 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6828 | break; |
| 6829 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6830 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6831 | case OR_Success: |
| 6832 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6833 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6834 | } |
David Blaikie | 60deeee | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 6835 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6836 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6837 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6838 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6839 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6840 | // This is implicit default-initialization of a const member in |
| 6841 | // a constructor. Complain that it needs to be explicitly |
| 6842 | // initialized. |
| 6843 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 6844 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6845 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6846 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6847 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6848 | << /*const=*/1 |
| 6849 | << Entity.getName(); |
| 6850 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 6851 | << Entity.getName(); |
| 6852 | } else { |
| 6853 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
Nico Weber | 9386c82 | 2014-07-23 05:16:10 +0000 | [diff] [blame] | 6854 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 6855 | maybeEmitZeroInitializationFixit(S, *this, Entity); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6856 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6857 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6858 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6859 | case FK_Incomplete: |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 6860 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6861 | diag::err_init_incomplete_type); |
| 6862 | break; |
| 6863 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6864 | case FK_ListInitializationFailed: { |
| 6865 | // Run the init list checker again to emit diagnostics. |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6866 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 6867 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6868 | break; |
| 6869 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6870 | |
| 6871 | case FK_PlaceholderType: { |
| 6872 | // FIXME: Already diagnosed! |
| 6873 | break; |
| 6874 | } |
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 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 6878 | << Args[0]->getSourceRange(); |
| 6879 | OverloadCandidateSet::iterator Best; |
| 6880 | OverloadingResult Ovl |
| 6881 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | 5dcce09 | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 6882 | (void)Ovl; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6883 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 6884 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 6885 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 6886 | break; |
| 6887 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6888 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6889 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6890 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6891 | return true; |
| 6892 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6893 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6894 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6895 | switch (SequenceKind) { |
| 6896 | case FailedSequence: { |
| 6897 | OS << "Failed sequence: "; |
| 6898 | switch (Failure) { |
| 6899 | case FK_TooManyInitsForReference: |
| 6900 | OS << "too many initializers for reference"; |
| 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 FK_ArrayNeedsInitList: |
| 6904 | OS << "array requires initializer list"; |
| 6905 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6906 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6907 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6908 | OS << "array requires initializer list or string literal"; |
| 6909 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6910 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6911 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6912 | OS << "array requires initializer list or wide string literal"; |
| 6913 | break; |
| 6914 | |
| 6915 | case FK_NarrowStringIntoWideCharArray: |
| 6916 | OS << "narrow string into wide char array"; |
| 6917 | break; |
| 6918 | |
| 6919 | case FK_WideStringIntoCharArray: |
| 6920 | OS << "wide string into char array"; |
| 6921 | break; |
| 6922 | |
| 6923 | case FK_IncompatWideStringIntoWideChar: |
| 6924 | OS << "incompatible wide string into wide char array"; |
| 6925 | break; |
| 6926 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6927 | case FK_ArrayTypeMismatch: |
| 6928 | OS << "array type mismatch"; |
| 6929 | break; |
| 6930 | |
| 6931 | case FK_NonConstantArrayInit: |
| 6932 | OS << "non-constant array initializer"; |
| 6933 | break; |
| 6934 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6935 | case FK_AddressOfOverloadFailed: |
| 6936 | OS << "address of overloaded function failed"; |
| 6937 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6938 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6939 | case FK_ReferenceInitOverloadFailed: |
| 6940 | OS << "overload resolution for reference initialization failed"; |
| 6941 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6942 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6943 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6944 | OS << "non-const lvalue reference bound to temporary"; |
| 6945 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6946 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6947 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6948 | OS << "non-const lvalue reference bound to unrelated type"; |
| 6949 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6950 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6951 | case FK_RValueReferenceBindingToLValue: |
| 6952 | OS << "rvalue reference bound to an lvalue"; |
| 6953 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6954 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6955 | case FK_ReferenceInitDropsQualifiers: |
| 6956 | OS << "reference initialization drops qualifiers"; |
| 6957 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6958 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6959 | case FK_ReferenceInitFailed: |
| 6960 | OS << "reference initialization failed"; |
| 6961 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6962 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6963 | case FK_ConversionFailed: |
| 6964 | OS << "conversion failed"; |
| 6965 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6966 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6967 | case FK_ConversionFromPropertyFailed: |
| 6968 | OS << "conversion from property failed"; |
| 6969 | break; |
| 6970 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6971 | case FK_TooManyInitsForScalar: |
| 6972 | OS << "too many initializers for scalar"; |
| 6973 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6974 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6975 | case FK_ReferenceBindingToInitList: |
| 6976 | OS << "referencing binding to initializer list"; |
| 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 FK_InitListBadDestinationType: |
| 6980 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 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 FK_UserConversionOverloadFailed: |
| 6984 | OS << "overloading failed for user-defined conversion"; |
| 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 FK_ConstructorOverloadFailed: |
| 6988 | OS << "constructor overloading failed"; |
| 6989 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6990 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6991 | case FK_DefaultInitOfConst: |
| 6992 | OS << "default initialization of a const variable"; |
| 6993 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6994 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 6995 | case FK_Incomplete: |
| 6996 | OS << "initialization of incomplete type"; |
| 6997 | break; |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6998 | |
| 6999 | case FK_ListInitializationFailed: |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 7000 | OS << "list initialization checker failure"; |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 7001 | break; |
| 7002 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 7003 | case FK_VariableLengthArrayHasInitializer: |
| 7004 | OS << "variable length array has an initializer"; |
| 7005 | break; |
| 7006 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 7007 | case FK_PlaceholderType: |
| 7008 | OS << "initializer expression isn't contextually valid"; |
| 7009 | break; |
Nick Lewycky | 097f47c | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 7010 | |
| 7011 | case FK_ListConstructorOverloadFailed: |
| 7012 | OS << "list constructor overloading failed"; |
| 7013 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7014 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 7015 | case FK_ExplicitConstructor: |
| 7016 | OS << "list copy initialization chose explicit constructor"; |
| 7017 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7018 | } |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7019 | OS << '\n'; |
| 7020 | return; |
| 7021 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7022 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7023 | case DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7024 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7025 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7026 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 7027 | case NormalSequence: |
| 7028 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7029 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7030 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7031 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7032 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 7033 | if (S != step_begin()) { |
| 7034 | OS << " -> "; |
| 7035 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7036 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7037 | switch (S->Kind) { |
| 7038 | case SK_ResolveAddressOfOverloadedFunction: |
| 7039 | OS << "resolve address of overloaded function"; |
| 7040 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7041 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7042 | case SK_CastDerivedToBaseRValue: |
| 7043 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 7044 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7045 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7046 | case SK_CastDerivedToBaseXValue: |
| 7047 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 7048 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7049 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7050 | case SK_CastDerivedToBaseLValue: |
| 7051 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 7052 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7053 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7054 | case SK_BindReference: |
| 7055 | OS << "bind reference to lvalue"; |
| 7056 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7057 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7058 | case SK_BindReferenceToTemporary: |
| 7059 | OS << "bind reference to a temporary"; |
| 7060 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7061 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 7062 | case SK_ExtraneousCopyToTemporary: |
| 7063 | OS << "extraneous C++03 copy to temporary"; |
| 7064 | break; |
| 7065 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7066 | case SK_UserConversion: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 7067 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7068 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7069 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7070 | case SK_QualificationConversionRValue: |
| 7071 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7072 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7073 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7074 | case SK_QualificationConversionXValue: |
| 7075 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7076 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7077 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7078 | case SK_QualificationConversionLValue: |
| 7079 | OS << "qualification conversion (lvalue)"; |
| 7080 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7081 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 7082 | case SK_AtomicConversion: |
| 7083 | OS << "non-atomic-to-atomic conversion"; |
| 7084 | break; |
| 7085 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7086 | case SK_LValueToRValue: |
| 7087 | OS << "load (lvalue to rvalue)"; |
| 7088 | break; |
| 7089 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7090 | case SK_ConversionSequence: |
| 7091 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 7092 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7093 | OS << ")"; |
| 7094 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7095 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7096 | case SK_ConversionSequenceNoNarrowing: |
| 7097 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 7098 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7099 | OS << ")"; |
| 7100 | break; |
| 7101 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7102 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 7103 | OS << "list aggregate initialization"; |
| 7104 | break; |
| 7105 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7106 | case SK_UnwrapInitList: |
| 7107 | OS << "unwrap reference initializer list"; |
| 7108 | break; |
| 7109 | |
| 7110 | case SK_RewrapInitList: |
| 7111 | OS << "rewrap reference initializer list"; |
| 7112 | break; |
| 7113 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7114 | case SK_ConstructorInitialization: |
| 7115 | OS << "constructor initialization"; |
| 7116 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7117 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7118 | case SK_ConstructorInitializationFromList: |
| 7119 | OS << "list initialization via constructor"; |
| 7120 | break; |
| 7121 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7122 | case SK_ZeroInitialization: |
| 7123 | OS << "zero initialization"; |
| 7124 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7125 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7126 | case SK_CAssignment: |
| 7127 | OS << "C assignment"; |
| 7128 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7129 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7130 | case SK_StringInit: |
| 7131 | OS << "string initialization"; |
| 7132 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7133 | |
| 7134 | case SK_ObjCObjectConversion: |
| 7135 | OS << "Objective-C object conversion"; |
| 7136 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7137 | |
| 7138 | case SK_ArrayInit: |
| 7139 | OS << "array initialization"; |
| 7140 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7141 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7142 | case SK_ParenthesizedArrayInit: |
| 7143 | OS << "parenthesized array initialization"; |
| 7144 | break; |
| 7145 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7146 | case SK_PassByIndirectCopyRestore: |
| 7147 | OS << "pass by indirect copy and restore"; |
| 7148 | break; |
| 7149 | |
| 7150 | case SK_PassByIndirectRestore: |
| 7151 | OS << "pass by indirect restore"; |
| 7152 | break; |
| 7153 | |
| 7154 | case SK_ProduceObjCObject: |
| 7155 | OS << "Objective-C object retension"; |
| 7156 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7157 | |
| 7158 | case SK_StdInitializerList: |
| 7159 | OS << "std::initializer_list from initializer list"; |
| 7160 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7161 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame] | 7162 | case SK_StdInitializerListConstructorCall: |
| 7163 | OS << "list initialization from std::initializer_list"; |
| 7164 | break; |
| 7165 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7166 | case SK_OCLSamplerInit: |
| 7167 | OS << "OpenCL sampler_t from integer constant"; |
| 7168 | break; |
| 7169 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7170 | case SK_OCLZeroEvent: |
| 7171 | OS << "OpenCL event_t from zero"; |
| 7172 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7173 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 7174 | |
| 7175 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7176 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 7177 | |
| 7178 | OS << '\n'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7179 | } |
| 7180 | |
| 7181 | void InitializationSequence::dump() const { |
| 7182 | dump(llvm::errs()); |
| 7183 | } |
| 7184 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7185 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 7186 | const ImplicitConversionSequence &ICS, |
| 7187 | QualType PreNarrowingType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7188 | QualType EntityType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7189 | const Expr *PostInit) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7190 | const StandardConversionSequence *SCS = nullptr; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7191 | switch (ICS.getKind()) { |
| 7192 | case ImplicitConversionSequence::StandardConversion: |
| 7193 | SCS = &ICS.Standard; |
| 7194 | break; |
| 7195 | case ImplicitConversionSequence::UserDefinedConversion: |
| 7196 | SCS = &ICS.UserDefined.After; |
| 7197 | break; |
| 7198 | case ImplicitConversionSequence::AmbiguousConversion: |
| 7199 | case ImplicitConversionSequence::EllipsisConversion: |
| 7200 | case ImplicitConversionSequence::BadConversion: |
| 7201 | return; |
| 7202 | } |
| 7203 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7204 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 7205 | APValue ConstantValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 7206 | QualType ConstantType; |
| 7207 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 7208 | ConstantType)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7209 | case NK_Not_Narrowing: |
| 7210 | // No narrowing occurred. |
| 7211 | return; |
| 7212 | |
| 7213 | case NK_Type_Narrowing: |
| 7214 | // This was a floating-to-integer conversion, which is always considered a |
| 7215 | // narrowing conversion even if the value is a constant and can be |
| 7216 | // represented exactly as an integer. |
| 7217 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7218 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7219 | ? diag::warn_init_list_type_narrowing |
| 7220 | : diag::ext_init_list_type_narrowing) |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7221 | << PostInit->getSourceRange() |
| 7222 | << PreNarrowingType.getLocalUnqualifiedType() |
| 7223 | << EntityType.getLocalUnqualifiedType(); |
| 7224 | break; |
| 7225 | |
| 7226 | case NK_Constant_Narrowing: |
| 7227 | // A constant value was narrowed. |
| 7228 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7229 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7230 | ? diag::warn_init_list_constant_narrowing |
| 7231 | : diag::ext_init_list_constant_narrowing) |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7232 | << PostInit->getSourceRange() |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 7233 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 7423138 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 7234 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7235 | break; |
| 7236 | |
| 7237 | case NK_Variable_Narrowing: |
| 7238 | // A variable's value may have been narrowed. |
| 7239 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7240 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7241 | ? diag::warn_init_list_variable_narrowing |
| 7242 | : diag::ext_init_list_variable_narrowing) |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7243 | << PostInit->getSourceRange() |
| 7244 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 7423138 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 7245 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7246 | break; |
| 7247 | } |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7248 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 7249 | SmallString<128> StaticCast; |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7250 | llvm::raw_svector_ostream OS(StaticCast); |
| 7251 | OS << "static_cast<"; |
| 7252 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 7253 | // It's important to use the typedef's name if there is one so that the |
| 7254 | // fixit doesn't break code using types like int64_t. |
| 7255 | // |
| 7256 | // FIXME: This will break if the typedef requires qualification. But |
| 7257 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 7258 | OS << *TT->getDecl(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7259 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7260 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7261 | else { |
| 7262 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 7263 | // with a broken cast. |
| 7264 | return; |
| 7265 | } |
| 7266 | OS << ">("; |
Alp Toker | b086903 | 2014-05-17 01:13:18 +0000 | [diff] [blame] | 7267 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_silence) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 7268 | << PostInit->getSourceRange() |
| 7269 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
| 7270 | << FixItHint::CreateInsertion( |
| 7271 | S.getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7272 | } |
| 7273 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7274 | //===----------------------------------------------------------------------===// |
| 7275 | // Initialization helper functions |
| 7276 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7277 | bool |
| 7278 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 7279 | ExprResult Init) { |
| 7280 | if (Init.isInvalid()) |
| 7281 | return false; |
| 7282 | |
| 7283 | Expr *InitE = Init.get(); |
| 7284 | assert(InitE && "No initialization expression"); |
| 7285 | |
Douglas Gregor | f4cc61d | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 7286 | InitializationKind Kind |
| 7287 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7288 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 7289 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7290 | } |
| 7291 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7292 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7293 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 7294 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7295 | ExprResult Init, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7296 | bool TopLevelOfInitList, |
| 7297 | bool AllowExplicit) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7298 | if (Init.isInvalid()) |
| 7299 | return ExprError(); |
| 7300 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7301 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7302 | assert(InitE && "No initialization expression?"); |
| 7303 | |
| 7304 | if (EqualLoc.isInvalid()) |
| 7305 | EqualLoc = InitE->getLocStart(); |
| 7306 | |
| 7307 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7308 | EqualLoc, |
| 7309 | AllowExplicit); |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7310 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7311 | Init.get(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7312 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7313 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7314 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7315 | return Result; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7316 | } |