Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 11 | // |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Initialization.h" |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Designator.h" |
| 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/SemaInternal.h" |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APInt.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 28 | #include <map> |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 35 | /// \brief Check whether T is compatible with a wide character type (wchar_t, |
| 36 | /// char16_t or char32_t). |
| 37 | static bool IsWideCharCompatible(QualType T, ASTContext &Context) { |
| 38 | if (Context.typesAreCompatible(Context.getWideCharType(), T)) |
| 39 | return true; |
| 40 | if (Context.getLangOpts().CPlusPlus || Context.getLangOpts().C11) { |
| 41 | return Context.typesAreCompatible(Context.Char16Ty, T) || |
| 42 | Context.typesAreCompatible(Context.Char32Ty, T); |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | enum StringInitFailureKind { |
| 48 | SIF_None, |
| 49 | SIF_NarrowStringIntoWideChar, |
| 50 | SIF_WideStringIntoChar, |
| 51 | SIF_IncompatWideStringIntoWideChar, |
| 52 | SIF_Other |
| 53 | }; |
| 54 | |
| 55 | /// \brief Check whether the array of type AT can be initialized by the Init |
| 56 | /// expression by means of string initialization. Returns SIF_None if so, |
| 57 | /// otherwise returns a StringInitFailureKind that describes why the |
| 58 | /// initialization would not work. |
| 59 | static StringInitFailureKind IsStringInit(Expr *Init, const ArrayType *AT, |
| 60 | ASTContext &Context) { |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 61 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 62 | return SIF_Other; |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 63 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 64 | // See if this is a string literal or @encode. |
| 65 | Init = Init->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 67 | // Handle @encode, which is a narrow string. |
| 68 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 69 | return SIF_None; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 70 | |
| 71 | // Otherwise we can only handle string literals. |
| 72 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 73 | if (!SL) |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 74 | return SIF_Other; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 75 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 76 | const QualType ElemTy = |
| 77 | Context.getCanonicalType(AT->getElementType()).getUnqualifiedType(); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 78 | |
| 79 | switch (SL->getKind()) { |
| 80 | case StringLiteral::Ascii: |
| 81 | case StringLiteral::UTF8: |
| 82 | // char array can be initialized with a narrow string. |
| 83 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 84 | if (ElemTy->isCharType()) |
| 85 | return SIF_None; |
| 86 | if (IsWideCharCompatible(ElemTy, Context)) |
| 87 | return SIF_NarrowStringIntoWideChar; |
| 88 | return SIF_Other; |
| 89 | // C99 6.7.8p15 (with correction from DR343), or C11 6.7.9p15: |
| 90 | // "An array with element type compatible with a qualified or unqualified |
| 91 | // version of wchar_t, char16_t, or char32_t may be initialized by a wide |
| 92 | // string literal with the corresponding encoding prefix (L, u, or U, |
| 93 | // respectively), optionally enclosed in braces. |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 94 | case StringLiteral::UTF16: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 95 | if (Context.typesAreCompatible(Context.Char16Ty, ElemTy)) |
| 96 | return SIF_None; |
| 97 | if (ElemTy->isCharType()) |
| 98 | return SIF_WideStringIntoChar; |
| 99 | if (IsWideCharCompatible(ElemTy, Context)) |
| 100 | return SIF_IncompatWideStringIntoWideChar; |
| 101 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 102 | case StringLiteral::UTF32: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 103 | if (Context.typesAreCompatible(Context.Char32Ty, ElemTy)) |
| 104 | return SIF_None; |
| 105 | if (ElemTy->isCharType()) |
| 106 | return SIF_WideStringIntoChar; |
| 107 | if (IsWideCharCompatible(ElemTy, Context)) |
| 108 | return SIF_IncompatWideStringIntoWideChar; |
| 109 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 110 | case StringLiteral::Wide: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 111 | if (Context.typesAreCompatible(Context.getWideCharType(), ElemTy)) |
| 112 | return SIF_None; |
| 113 | if (ElemTy->isCharType()) |
| 114 | return SIF_WideStringIntoChar; |
| 115 | if (IsWideCharCompatible(ElemTy, Context)) |
| 116 | return SIF_IncompatWideStringIntoWideChar; |
| 117 | return SIF_Other; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 118 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 120 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 123 | static StringInitFailureKind IsStringInit(Expr *init, QualType declType, |
| 124 | ASTContext &Context) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 125 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 126 | if (!arrayType) |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 127 | return SIF_Other; |
| 128 | return IsStringInit(init, arrayType, Context); |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 131 | /// Update the type of a string literal, including any surrounding parentheses, |
| 132 | /// to match the type of the object which it is initializing. |
| 133 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 134 | while (true) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 135 | E->setType(Ty); |
Richard Smith | d74b1606 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 136 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) |
| 137 | break; |
| 138 | else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 139 | E = PE->getSubExpr(); |
| 140 | else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 141 | E = UO->getSubExpr(); |
| 142 | else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) |
| 143 | E = GSE->getResultExpr(); |
| 144 | else |
| 145 | llvm_unreachable("unexpected expr in string literal init"); |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 146 | } |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 149 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 150 | Sema &S) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 151 | // Get the length of the string as parsed. |
| 152 | uint64_t StrLength = |
| 153 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 154 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 156 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 158 | // being initialized to a string literal. |
Benjamin Kramer | e073177 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 159 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 160 | // Return a new array type (C99 6.7.8p22). |
John McCall | c5b8225 | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 161 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 162 | ConstVal, |
| 163 | ArrayType::Normal, 0); |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 164 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 165 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 166 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 168 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 170 | // We have an array of character type with known size. However, |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 171 | // the size may be smaller or larger than the string we are initializing. |
| 172 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 173 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 174 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | d162fb8 | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 175 | // For Pascal strings it's OK to strip off the terminating null character, |
| 176 | // so the example below is valid: |
| 177 | // |
| 178 | // unsigned char a[2] = "\pa"; |
| 179 | if (SL->isPascal()) |
| 180 | StrLength--; |
| 181 | } |
| 182 | |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 183 | // [dcl.init.string]p2 |
| 184 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 185 | S.Diag(Str->getLocStart(), |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 186 | diag::err_initializer_string_for_char_array_too_long) |
| 187 | << Str->getSourceRange(); |
| 188 | } else { |
| 189 | // C99 6.7.8p14. |
| 190 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 191 | S.Diag(Str->getLocStart(), |
Eli Friedman | 554eba9 | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 192 | diag::warn_initializer_string_for_char_array_too_long) |
| 193 | << Str->getSourceRange(); |
| 194 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 196 | // Set the type to the actual size that we are initializing. If we have |
| 197 | // something like: |
| 198 | // char x[1] = "foo"; |
| 199 | // then this will set the string literal's type to char[1]. |
Richard Smith | 430c23b | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 200 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 203 | //===----------------------------------------------------------------------===// |
| 204 | // Semantic checking for initializer lists. |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 207 | /// @brief Semantic checking for initializer lists. |
| 208 | /// |
| 209 | /// The InitListChecker class contains a set of routines that each |
| 210 | /// handle the initialization of a certain kind of entity, e.g., |
| 211 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 212 | /// InitListChecker itself performs a recursive walk of the subobject |
| 213 | /// structure of the type to be initialized, while stepping through |
| 214 | /// the initializer list one element at a time. The IList and Index |
| 215 | /// parameters to each of the Check* routines contain the active |
| 216 | /// (syntactic) initializer list and the index into that initializer |
| 217 | /// list that represents the current initializer. Each routine is |
| 218 | /// responsible for moving that Index forward as it consumes elements. |
| 219 | /// |
| 220 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 221 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 222 | /// initializer list and the index into that initializer list where we |
| 223 | /// are copying initializers as we map them over to the semantic |
| 224 | /// list. Once we have completed our recursive walk of the subobject |
| 225 | /// structure, we will have constructed a full semantic initializer |
| 226 | /// list. |
| 227 | /// |
| 228 | /// C99 designators cause changes in the initializer list traversal, |
| 229 | /// because they make the initialization "jump" into a specific |
| 230 | /// subobject and then continue the initialization from that |
| 231 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 232 | /// designated subobject and manages backing out the recursion to |
| 233 | /// initialize the subobjects after the one designated. |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 234 | namespace { |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 235 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 236 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 237 | bool hadError; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 238 | bool VerifyOnly; // no diagnostics, no structure building |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 239 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 240 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 242 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 243 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 244 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 245 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 246 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 247 | InitListExpr *IList, QualType &T, |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 248 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 249 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 250 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 251 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 253 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 254 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 255 | unsigned &StructuredIndex, |
| 256 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 257 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 258 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 259 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 260 | InitListExpr *StructuredList, |
| 261 | unsigned &StructuredIndex); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 262 | void CheckComplexType(const InitializedEntity &Entity, |
| 263 | InitListExpr *IList, QualType DeclType, |
| 264 | unsigned &Index, |
| 265 | InitListExpr *StructuredList, |
| 266 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 267 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 268 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 269 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 270 | InitListExpr *StructuredList, |
| 271 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 272 | void CheckReferenceType(const InitializedEntity &Entity, |
| 273 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 274 | unsigned &Index, |
| 275 | InitListExpr *StructuredList, |
| 276 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 277 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 278 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 279 | InitListExpr *StructuredList, |
| 280 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 281 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 282 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 284 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 285 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 286 | unsigned &StructuredIndex, |
| 287 | bool TopLevelObject = false); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 288 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 289 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 291 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 292 | InitListExpr *StructuredList, |
| 293 | unsigned &StructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 294 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 295 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 296 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 298 | RecordDecl::field_iterator *NextField, |
| 299 | llvm::APSInt *NextElementIndex, |
| 300 | unsigned &Index, |
| 301 | InitListExpr *StructuredList, |
| 302 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 303 | bool FinishSubobjectInit, |
| 304 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 305 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 306 | QualType CurrentObjectType, |
| 307 | InitListExpr *StructuredList, |
| 308 | unsigned StructuredIndex, |
| 309 | SourceRange InitRange); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 310 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 311 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 312 | Expr *expr); |
| 313 | int numArrayElements(QualType DeclType); |
| 314 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 315 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 316 | static ExprResult PerformEmptyInit(Sema &SemaRef, |
| 317 | SourceLocation Loc, |
| 318 | const InitializedEntity &Entity, |
| 319 | bool VerifyOnly); |
| 320 | void FillInEmptyInitForField(unsigned Init, FieldDecl *Field, |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 321 | const InitializedEntity &ParentEntity, |
| 322 | InitListExpr *ILE, bool &RequiresSecondPass); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 323 | void FillInEmptyInitializations(const InitializedEntity &Entity, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 324 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 325 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 326 | Expr *InitExpr, FieldDecl *Field, |
| 327 | bool TopLevelObject); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 328 | void CheckEmptyInitializable(const InitializedEntity &Entity, |
| 329 | SourceLocation Loc); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 330 | |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 331 | public: |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 332 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 333 | InitListExpr *IL, QualType &T, bool VerifyOnly); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 334 | bool HadError() { return hadError; } |
| 335 | |
| 336 | // @brief Retrieves the fully-structured initializer list used for |
| 337 | // semantic analysis and code generation. |
| 338 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 339 | }; |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 340 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 341 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 342 | ExprResult InitListChecker::PerformEmptyInit(Sema &SemaRef, |
| 343 | SourceLocation Loc, |
| 344 | const InitializedEntity &Entity, |
| 345 | bool VerifyOnly) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 346 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 347 | true); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 348 | MultiExprArg SubInit; |
| 349 | Expr *InitExpr; |
| 350 | InitListExpr DummyInitList(SemaRef.Context, Loc, None, Loc); |
| 351 | |
| 352 | // C++ [dcl.init.aggr]p7: |
| 353 | // If there are fewer initializer-clauses in the list than there are |
| 354 | // members in the aggregate, then each member not explicitly initialized |
| 355 | // ... |
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()) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 469 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, Loc, Field); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 470 | if (Init < NumInits) |
| 471 | ILE->setInit(Init, DIE); |
| 472 | else { |
| 473 | ILE->updateInit(SemaRef.Context, Init, DIE); |
| 474 | RequiresSecondPass = true; |
| 475 | } |
| 476 | return; |
| 477 | } |
| 478 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 479 | if (Field->getType()->isReferenceType()) { |
| 480 | // C++ [dcl.init.aggr]p9: |
| 481 | // If an incomplete or empty initializer-list leaves a |
| 482 | // member of reference type uninitialized, the program is |
| 483 | // ill-formed. |
| 484 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 485 | << Field->getType() |
| 486 | << ILE->getSyntacticForm()->getSourceRange(); |
| 487 | SemaRef.Diag(Field->getLocation(), |
| 488 | diag::note_uninit_reference_member); |
| 489 | hadError = true; |
| 490 | return; |
| 491 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 492 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 493 | ExprResult MemberInit = PerformEmptyInit(SemaRef, Loc, MemberEntity, |
| 494 | /*VerifyOnly*/false); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 495 | if (MemberInit.isInvalid()) { |
| 496 | hadError = true; |
| 497 | return; |
| 498 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 499 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 500 | if (hadError) { |
| 501 | // Do nothing |
| 502 | } else if (Init < NumInits) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 503 | ILE->setInit(Init, MemberInit.getAs<Expr>()); |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 504 | } else if (!isa<ImplicitValueInitExpr>(MemberInit.get())) { |
| 505 | // Empty initialization requires a constructor call, so |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 506 | // extend the initializer list to include the constructor |
| 507 | // call and make a note that we'll need to take another pass |
| 508 | // through the initializer list. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 509 | ILE->updateInit(SemaRef.Context, Init, MemberInit.getAs<Expr>()); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 510 | RequiresSecondPass = true; |
| 511 | } |
| 512 | } else if (InitListExpr *InnerILE |
| 513 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 514 | FillInEmptyInitializations(MemberEntity, InnerILE, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 515 | RequiresSecondPass); |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 518 | /// Recursively replaces NULL values within the given initializer list |
| 519 | /// with expressions that perform value-initialization of the |
| 520 | /// appropriate type. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 521 | void |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 522 | InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 523 | InitListExpr *ILE, |
| 524 | bool &RequiresSecondPass) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 526 | "Should not have void type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 528 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 529 | const RecordDecl *RDecl = RType->getDecl(); |
| 530 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 531 | FillInEmptyInitForField(0, ILE->getInitializedFieldInUnion(), |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 532 | Entity, ILE, RequiresSecondPass); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 533 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 534 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 535 | for (auto *Field : RDecl->fields()) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 536 | if (Field->hasInClassInitializer()) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 537 | FillInEmptyInitForField(0, Field, Entity, ILE, RequiresSecondPass); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 538 | break; |
| 539 | } |
| 540 | } |
| 541 | } else { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 542 | unsigned Init = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 543 | for (auto *Field : RDecl->fields()) { |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 544 | if (Field->isUnnamedBitfield()) |
| 545 | continue; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 546 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 547 | if (hadError) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 548 | return; |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 549 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 550 | FillInEmptyInitForField(Init, Field, Entity, ILE, RequiresSecondPass); |
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 | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 553 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 554 | ++Init; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 556 | // Only look at the first initialization of a union. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 557 | if (RDecl->isUnion()) |
Douglas Gregor | 2bb0765 | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 558 | break; |
| 559 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 564 | |
| 565 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 567 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 568 | unsigned NumInits = ILE->getNumInits(); |
| 569 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 570 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 571 | ElementType = AType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 572 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 573 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 574 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 575 | 0, Entity); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 576 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 577 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 578 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 579 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 580 | 0, Entity); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 582 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 584 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 585 | if (hadError) |
| 586 | return; |
| 587 | |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 588 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 589 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 590 | ElementEntity.setElementIndex(Init); |
| 591 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 592 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : nullptr); |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 593 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 594 | ExprResult ElementInit = PerformEmptyInit(SemaRef, ILE->getLocEnd(), |
| 595 | ElementEntity, |
| 596 | /*VerifyOnly*/false); |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 597 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 598 | hadError = true; |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 599 | return; |
| 600 | } |
| 601 | |
| 602 | if (hadError) { |
| 603 | // Do nothing |
| 604 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 605 | // For arrays, just set the expression used for value-initialization |
| 606 | // of the "holes" in the array. |
| 607 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 608 | ILE->setArrayFiller(ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 609 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 610 | ILE->setInit(Init, ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 611 | } else { |
| 612 | // For arrays, just set the expression used for value-initialization |
| 613 | // of the rest of elements and exit. |
| 614 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 615 | ILE->setArrayFiller(ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 616 | return; |
| 617 | } |
| 618 | |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 619 | if (!isa<ImplicitValueInitExpr>(ElementInit.get())) { |
| 620 | // Empty initialization requires a constructor call, so |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 621 | // extend the initializer list to include the constructor |
| 622 | // call and make a note that we'll need to take another pass |
| 623 | // through the initializer list. |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 624 | ILE->updateInit(SemaRef.Context, Init, ElementInit.getAs<Expr>()); |
Argyrios Kyrtzidis | b2ed28e | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 625 | RequiresSecondPass = true; |
| 626 | } |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 627 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 628 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 629 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 630 | FillInEmptyInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 634 | |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 635 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 636 | InitListExpr *IL, QualType &T, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 637 | bool VerifyOnly) |
| 638 | : SemaRef(S), VerifyOnly(VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 639 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 640 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 641 | FullyStructuredList = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 642 | getStructuredSubobjectInit(IL, 0, T, nullptr, 0, IL->getSourceRange()); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 643 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 644 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 645 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 646 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 647 | bool RequiresSecondPass = false; |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 648 | FillInEmptyInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 649 | if (RequiresSecondPass && !hadError) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 650 | FillInEmptyInitializations(Entity, FullyStructuredList, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 651 | RequiresSecondPass); |
| 652 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 656 | // FIXME: use a proper constant |
| 657 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 658 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 659 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 660 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 661 | } |
| 662 | return maxElements; |
| 663 | } |
| 664 | |
| 665 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 666 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 667 | int InitializableMembers = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 668 | for (const auto *Field : structDecl->fields()) |
Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 669 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 670 | ++InitializableMembers; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 671 | |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 672 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 673 | return std::min(InitializableMembers, 1); |
| 674 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 677 | /// Check whether the range of the initializer \p ParentIList from element |
| 678 | /// \p Index onwards can be used to initialize an object of type \p T. Update |
| 679 | /// \p Index to indicate how many elements of the list were consumed. |
| 680 | /// |
| 681 | /// This also fills in \p StructuredList, from element \p StructuredIndex |
| 682 | /// onwards, with the fully-braced, desugared form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 683 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 684 | InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 685 | QualType T, unsigned &Index, |
| 686 | InitListExpr *StructuredList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 687 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 688 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 690 | if (T->isArrayType()) |
| 691 | maxElements = numArrayElements(T); |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 692 | else if (T->isRecordType()) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 693 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 694 | else if (T->isVectorType()) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 695 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 696 | else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 697 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 698 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 699 | if (maxElements == 0) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 700 | if (!VerifyOnly) |
| 701 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 702 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 703 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 704 | hadError = true; |
| 705 | return; |
| 706 | } |
| 707 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 708 | // Build a structured initializer list corresponding to this subobject. |
| 709 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 711 | StructuredIndex, |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 712 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 713 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 714 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 715 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 716 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 717 | unsigned StartIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 718 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 719 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | StructuredSubobjectInitList, |
Eli Friedman | c616c5f | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 721 | StructuredSubobjectInitIndex); |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 722 | |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 723 | if (!VerifyOnly) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 724 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 725 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 726 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 727 | // Update the structured sub-object initializer so that it's ending |
| 728 | // range corresponds with the end of the last initializer it used. |
| 729 | if (EndIndex < ParentIList->getNumInits()) { |
| 730 | SourceLocation EndLoc |
| 731 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 732 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 733 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 734 | |
Sebastian Redl | 8b6412a | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 735 | // Complain about missing braces. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 736 | if (T->isArrayType() || T->isRecordType()) { |
| 737 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 738 | diag::warn_missing_braces) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 739 | << StructuredSubobjectInitList->getSourceRange() |
| 740 | << FixItHint::CreateInsertion( |
| 741 | StructuredSubobjectInitList->getLocStart(), "{") |
| 742 | << FixItHint::CreateInsertion( |
| 743 | SemaRef.getLocForEndOfToken( |
| 744 | StructuredSubobjectInitList->getLocEnd()), |
| 745 | "}"); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 746 | } |
Tanya Lattner | 5029d56 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 747 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 750 | /// Check whether the initializer \p IList (that was written with explicit |
| 751 | /// braces) can be used to initialize an object of type \p T. |
| 752 | /// |
| 753 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 754 | /// form of the initialization. |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 755 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 756 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 757 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 758 | bool TopLevelObject) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 759 | if (!VerifyOnly) { |
| 760 | SyntacticToSemantic[IList] = StructuredList; |
| 761 | StructuredList->setSyntacticForm(IList); |
| 762 | } |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 763 | |
| 764 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 765 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 766 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 767 | if (!VerifyOnly) { |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 768 | QualType ExprTy = T; |
| 769 | if (!ExprTy->isArrayType()) |
| 770 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 771 | IList->setType(ExprTy); |
| 772 | StructuredList->setType(ExprTy); |
| 773 | } |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 774 | if (hadError) |
| 775 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 776 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 777 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 778 | // We have leftover initializers |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 779 | if (VerifyOnly) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 780 | if (SemaRef.getLangOpts().CPlusPlus || |
| 781 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 782 | IList->getType()->isVectorType())) { |
| 783 | hadError = true; |
| 784 | } |
| 785 | return; |
| 786 | } |
| 787 | |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 788 | if (StructuredIndex == 1 && |
Hans Wennborg | 950f318 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 789 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 790 | SIF_None) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 791 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 792 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 793 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 794 | hadError = true; |
| 795 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 796 | // Special-case |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 797 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 798 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 799 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 800 | // Don't complain for incomplete types, since we'll get an error |
| 801 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 802 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 803 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 804 | CurrentObjectType->isArrayType()? 0 : |
| 805 | CurrentObjectType->isVectorType()? 1 : |
| 806 | CurrentObjectType->isScalarType()? 2 : |
| 807 | CurrentObjectType->isUnionType()? 3 : |
| 808 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 809 | |
| 810 | unsigned DK = diag::warn_excess_initializers; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 811 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 812 | DK = diag::err_excess_initializers; |
| 813 | hadError = true; |
| 814 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 815 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 816 | DK = diag::err_excess_initializers; |
| 817 | hadError = true; |
| 818 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 819 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 820 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 821 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 824 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 825 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 826 | !TopLevelObject) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 827 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 828 | << IList->getSourceRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 829 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 830 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 833 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 834 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 836 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 837 | unsigned &Index, |
| 838 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 839 | unsigned &StructuredIndex, |
| 840 | bool TopLevelObject) { |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 841 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 842 | // Explicitly braced initializer for complex type can be real+imaginary |
| 843 | // parts. |
| 844 | CheckComplexType(Entity, IList, DeclType, Index, |
| 845 | StructuredList, StructuredIndex); |
| 846 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 847 | CheckScalarType(Entity, IList, DeclType, Index, |
| 848 | StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 849 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 850 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 851 | StructuredList, StructuredIndex); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 852 | } else if (DeclType->isRecordType()) { |
| 853 | assert(DeclType->isAggregateType() && |
| 854 | "non-aggregate records should be handed in CheckSubElementType"); |
| 855 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 856 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 857 | SubobjectIsDesignatorContext, Index, |
| 858 | StructuredList, StructuredIndex, |
| 859 | TopLevelObject); |
| 860 | } else if (DeclType->isArrayType()) { |
| 861 | llvm::APSInt Zero( |
| 862 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 863 | false); |
| 864 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 865 | SubobjectIsDesignatorContext, Index, |
| 866 | StructuredList, StructuredIndex); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 867 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 868 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 869 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 870 | if (!VerifyOnly) |
| 871 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 872 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 873 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 874 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 875 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 876 | StructuredList, StructuredIndex); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 877 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 878 | if (!VerifyOnly) |
| 879 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 880 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 881 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 882 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 883 | if (!VerifyOnly) |
| 884 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 885 | << DeclType; |
Douglas Gregor | 50ec46d | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 886 | hadError = true; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 890 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 891 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 893 | unsigned &Index, |
| 894 | InitListExpr *StructuredList, |
| 895 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 896 | Expr *expr = IList->getInit(Index); |
Richard Smith | 72752e8 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 897 | |
| 898 | if (ElemType->isReferenceType()) |
| 899 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 900 | StructuredList, StructuredIndex); |
| 901 | |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 902 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 903 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 904 | InitListExpr *InnerStructuredList |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 905 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 906 | StructuredList, StructuredIndex, |
| 907 | SubInitList->getSourceRange()); |
Richard Smith | 4e0d2e4 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 908 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 909 | InnerStructuredList); |
Richard Smith | e20c83d | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 910 | ++StructuredIndex; |
| 911 | ++Index; |
| 912 | return; |
| 913 | } |
| 914 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 915 | "non-aggregate records are only possible in C++"); |
| 916 | // C++ initialization is handled later. |
| 917 | } |
| 918 | |
Eli Friedman | 4628cf7 | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 919 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
| 920 | if (ElemType->isScalarType() || ElemType->isAtomicType()) |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 921 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 922 | StructuredList, StructuredIndex); |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 923 | |
Eli Friedman | 4628cf7 | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 924 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
| 925 | ElemType->isArrayType()) && "Unexpected type"); |
| 926 | |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 927 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 928 | // arrayType can be incomplete if we're initializing a flexible |
| 929 | // array member. There's nothing we can do with the completed |
| 930 | // type here, though. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 931 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 932 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 933 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 934 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 935 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | d8d7a37 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 936 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 937 | ++Index; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 938 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 939 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 940 | |
| 941 | // Fall through for subaggregate initialization. |
| 942 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 943 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 944 | // C++ [dcl.init.aggr]p12: |
| 945 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 26bcc94 | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 946 | // initializing the aggregate member with an initializer from |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 947 | // an initializer-list. If the initializer can initialize a |
| 948 | // member, the member is initialized. [...] |
| 949 | |
| 950 | // FIXME: Better EqualLoc? |
| 951 | InitializationKind Kind = |
| 952 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 953 | InitializationSequence Seq(SemaRef, Entity, Kind, expr); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 954 | |
| 955 | if (Seq) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 956 | if (!VerifyOnly) { |
Richard Smith | 0f8ede1 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 957 | ExprResult Result = |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 958 | Seq.Perform(SemaRef, Entity, Kind, expr); |
Richard Smith | 0f8ede1 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 959 | if (Result.isInvalid()) |
| 960 | hadError = true; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 961 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 962 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 963 | Result.getAs<Expr>()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 964 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 965 | ++Index; |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | // Fall through for subaggregate initialization |
| 970 | } else { |
| 971 | // C99 6.7.8p13: |
| 972 | // |
| 973 | // The initializer for a structure or union object that has |
| 974 | // automatic storage duration shall be either an initializer |
| 975 | // list as described below, or a single expression that has |
| 976 | // compatible structure or union type. In the latter case, the |
| 977 | // initial value of the object, including unnamed members, is |
| 978 | // that of the expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 979 | ExprResult ExprRes = expr; |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 980 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 981 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 982 | !VerifyOnly) |
Eli Friedman | b2a8d46 | 2013-09-17 04:07:04 +0000 | [diff] [blame] | 983 | != Sema::Incompatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 984 | if (ExprRes.isInvalid()) |
| 985 | hadError = true; |
| 986 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 987 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 988 | if (ExprRes.isInvalid()) |
| 989 | hadError = true; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 990 | } |
| 991 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 992 | ExprRes.getAs<Expr>()); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 993 | ++Index; |
| 994 | return; |
| 995 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 996 | ExprRes.get(); |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 997 | // Fall through for subaggregate initialization |
| 998 | } |
| 999 | |
| 1000 | // C++ [dcl.init.aggr]p12: |
| 1001 | // |
| 1002 | // [...] Otherwise, if the member is itself a non-empty |
| 1003 | // subaggregate, brace elision is assumed and the initializer is |
| 1004 | // considered for the initialization of the first member of |
| 1005 | // the subaggregate. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1006 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 1007 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1008 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 1009 | StructuredIndex); |
| 1010 | ++StructuredIndex; |
| 1011 | } else { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1012 | if (!VerifyOnly) { |
| 1013 | // We cannot initialize this element, so let |
| 1014 | // PerformCopyInitialization produce the appropriate diagnostic. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1015 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1016 | /*TopLevelOfInitList=*/true); |
| 1017 | } |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 1018 | hadError = true; |
| 1019 | ++Index; |
| 1020 | ++StructuredIndex; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1021 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1024 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 1025 | InitListExpr *IList, QualType DeclType, |
| 1026 | unsigned &Index, |
| 1027 | InitListExpr *StructuredList, |
| 1028 | unsigned &StructuredIndex) { |
| 1029 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 1030 | |
| 1031 | // As an extension, clang supports complex initializers, which initialize |
| 1032 | // a complex number component-wise. When an explicit initializer list for |
| 1033 | // a complex number contains two two initializers, this extension kicks in: |
| 1034 | // it exepcts the initializer list to contain two elements convertible to |
| 1035 | // the element type of the complex type. The first element initializes |
| 1036 | // the real part, and the second element intitializes the imaginary part. |
| 1037 | |
| 1038 | if (IList->getNumInits() != 2) |
| 1039 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 1040 | StructuredIndex); |
| 1041 | |
| 1042 | // This is an extension in C. (The builtin _Complex type does not exist |
| 1043 | // in the C++ standard.) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1044 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 1045 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 1046 | << IList->getSourceRange(); |
| 1047 | |
| 1048 | // Initialize the complex number. |
| 1049 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 1050 | InitializedEntity ElementEntity = |
| 1051 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 1052 | |
| 1053 | for (unsigned i = 0; i < 2; ++i) { |
| 1054 | ElementEntity.setElementIndex(Index); |
| 1055 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1056 | StructuredList, StructuredIndex); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1061 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1062 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1063 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1064 | InitListExpr *StructuredList, |
| 1065 | unsigned &StructuredIndex) { |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1066 | if (Index >= IList->getNumInits()) { |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1067 | if (!VerifyOnly) |
| 1068 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1069 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | c823973 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 1070 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 1071 | diag::err_empty_scalar_initializer) |
| 1072 | << IList->getSourceRange(); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1073 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1074 | ++Index; |
| 1075 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1076 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1077 | } |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1078 | |
| 1079 | Expr *expr = IList->getInit(Index); |
| 1080 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1081 | // FIXME: This is invalid, and accepting it causes overload resolution |
| 1082 | // to pick the wrong overload in some corner cases. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1083 | if (!VerifyOnly) |
| 1084 | SemaRef.Diag(SubIList->getLocStart(), |
Richard Smith | fe9d2c0 | 2013-11-19 03:41:32 +0000 | [diff] [blame] | 1085 | diag::ext_many_braces_around_scalar_init) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1086 | << SubIList->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1087 | |
| 1088 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1089 | StructuredIndex); |
| 1090 | return; |
| 1091 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1092 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1093 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1094 | diag::err_designator_for_scalar_init) |
| 1095 | << DeclType << expr->getSourceRange(); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1096 | hadError = true; |
| 1097 | ++Index; |
| 1098 | ++StructuredIndex; |
| 1099 | return; |
| 1100 | } |
| 1101 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1102 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1103 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1104 | hadError = true; |
| 1105 | ++Index; |
| 1106 | return; |
| 1107 | } |
| 1108 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1109 | ExprResult Result = |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1110 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), expr, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1111 | /*TopLevelOfInitList=*/true); |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1112 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1113 | Expr *ResultExpr = nullptr; |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1114 | |
| 1115 | if (Result.isInvalid()) |
| 1116 | hadError = true; // types weren't compatible. |
| 1117 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1118 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1119 | |
John McCall | 643169b | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1120 | if (ResultExpr != expr) { |
| 1121 | // The type was promoted, update initializer list. |
| 1122 | IList->setInit(Index, ResultExpr); |
| 1123 | } |
| 1124 | } |
| 1125 | if (hadError) |
| 1126 | ++StructuredIndex; |
| 1127 | else |
| 1128 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1129 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1132 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1133 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1134 | unsigned &Index, |
| 1135 | InitListExpr *StructuredList, |
| 1136 | unsigned &StructuredIndex) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1137 | if (Index >= IList->getNumInits()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1138 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1139 | // general, it would be useful to pass location information down the stack, |
| 1140 | // so that we know the location (or decl) of the "current object" being |
| 1141 | // initialized. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1142 | if (!VerifyOnly) |
| 1143 | SemaRef.Diag(IList->getLocStart(), |
| 1144 | diag::err_init_reference_member_uninitialized) |
| 1145 | << DeclType |
| 1146 | << IList->getSourceRange(); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1147 | hadError = true; |
| 1148 | ++Index; |
| 1149 | ++StructuredIndex; |
| 1150 | return; |
| 1151 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1152 | |
| 1153 | Expr *expr = IList->getInit(Index); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1154 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1155 | if (!VerifyOnly) |
| 1156 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1157 | << DeclType << IList->getSourceRange(); |
| 1158 | hadError = true; |
| 1159 | ++Index; |
| 1160 | ++StructuredIndex; |
| 1161 | return; |
| 1162 | } |
| 1163 | |
| 1164 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1165 | if (!SemaRef.CanPerformCopyInitialization(Entity,expr)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1166 | hadError = true; |
| 1167 | ++Index; |
| 1168 | return; |
| 1169 | } |
| 1170 | |
| 1171 | ExprResult Result = |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1172 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), expr, |
| 1173 | /*TopLevelOfInitList=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1174 | |
| 1175 | if (Result.isInvalid()) |
| 1176 | hadError = true; |
| 1177 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1178 | expr = Result.getAs<Expr>(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1179 | IList->setInit(Index, expr); |
| 1180 | |
| 1181 | if (hadError) |
| 1182 | ++StructuredIndex; |
| 1183 | else |
| 1184 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1185 | ++Index; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1188 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1189 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1190 | unsigned &Index, |
| 1191 | InitListExpr *StructuredList, |
| 1192 | unsigned &StructuredIndex) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1193 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1194 | unsigned maxElements = VT->getNumElements(); |
| 1195 | unsigned numEltsInit = 0; |
| 1196 | QualType elementType = VT->getElementType(); |
Anders Carlsson | d084925 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1197 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1198 | if (Index >= IList->getNumInits()) { |
| 1199 | // Make sure the element type can be value-initialized. |
| 1200 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1201 | CheckEmptyInitializable( |
| 1202 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity), |
| 1203 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1204 | return; |
| 1205 | } |
| 1206 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1207 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1208 | // If the initializing element is a vector, try to copy-initialize |
| 1209 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1210 | Expr *Init = IList->getInit(Index); |
| 1211 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1212 | if (VerifyOnly) { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1213 | if (!SemaRef.CanPerformCopyInitialization(Entity, Init)) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1214 | hadError = true; |
| 1215 | ++Index; |
| 1216 | return; |
| 1217 | } |
| 1218 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1219 | ExprResult Result = |
| 1220 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), Init, |
| 1221 | /*TopLevelOfInitList=*/true); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1222 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1223 | Expr *ResultExpr = nullptr; |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1224 | if (Result.isInvalid()) |
| 1225 | hadError = true; // types weren't compatible. |
| 1226 | else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1227 | ResultExpr = Result.getAs<Expr>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1228 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1229 | if (ResultExpr != Init) { |
| 1230 | // The type was promoted, update initializer list. |
| 1231 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1232 | } |
| 1233 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1234 | if (hadError) |
| 1235 | ++StructuredIndex; |
| 1236 | else |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1237 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1238 | ResultExpr); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1239 | ++Index; |
| 1240 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1241 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1243 | InitializedEntity ElementEntity = |
| 1244 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1245 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1246 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1247 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1248 | if (Index >= IList->getNumInits()) { |
| 1249 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1250 | CheckEmptyInitializable(ElementEntity, IList->getLocEnd()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1251 | break; |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1252 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1253 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1254 | ElementEntity.setElementIndex(Index); |
| 1255 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1256 | StructuredList, StructuredIndex); |
| 1257 | } |
James Molloy | 9eef265 | 2014-06-20 14:35:13 +0000 | [diff] [blame] | 1258 | |
| 1259 | if (VerifyOnly) |
| 1260 | return; |
| 1261 | |
| 1262 | bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian(); |
| 1263 | const VectorType *T = Entity.getType()->getAs<VectorType>(); |
| 1264 | if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector || |
| 1265 | T->getVectorKind() == VectorType::NeonPolyVector)) { |
| 1266 | // The ability to use vector initializer lists is a GNU vector extension |
| 1267 | // and is unrelated to the NEON intrinsics in arm_neon.h. On little |
| 1268 | // endian machines it works fine, however on big endian machines it |
| 1269 | // exhibits surprising behaviour: |
| 1270 | // |
| 1271 | // uint32x2_t x = {42, 64}; |
| 1272 | // return vget_lane_u32(x, 0); // Will return 64. |
| 1273 | // |
| 1274 | // Because of this, explicitly call out that it is non-portable. |
| 1275 | // |
| 1276 | SemaRef.Diag(IList->getLocStart(), |
| 1277 | diag::warn_neon_vector_initializer_non_portable); |
| 1278 | |
| 1279 | const char *typeCode; |
| 1280 | unsigned typeSize = SemaRef.Context.getTypeSize(elementType); |
| 1281 | |
| 1282 | if (elementType->isFloatingType()) |
| 1283 | typeCode = "f"; |
| 1284 | else if (elementType->isSignedIntegerType()) |
| 1285 | typeCode = "s"; |
| 1286 | else if (elementType->isUnsignedIntegerType()) |
| 1287 | typeCode = "u"; |
| 1288 | else |
| 1289 | llvm_unreachable("Invalid element type!"); |
| 1290 | |
| 1291 | SemaRef.Diag(IList->getLocStart(), |
| 1292 | SemaRef.Context.getTypeSize(VT) > 64 ? |
| 1293 | diag::note_neon_vector_initializer_non_portable_q : |
| 1294 | diag::note_neon_vector_initializer_non_portable) |
| 1295 | << typeCode << typeSize; |
| 1296 | } |
| 1297 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1298 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1299 | } |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1300 | |
| 1301 | InitializedEntity ElementEntity = |
| 1302 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1303 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1304 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1305 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1306 | // Don't attempt to go past the end of the init list |
| 1307 | if (Index >= IList->getNumInits()) |
| 1308 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1309 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1310 | ElementEntity.setElementIndex(Index); |
| 1311 | |
| 1312 | QualType IType = IList->getInit(Index)->getType(); |
| 1313 | if (!IType->isVectorType()) { |
| 1314 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1315 | StructuredList, StructuredIndex); |
| 1316 | ++numEltsInit; |
| 1317 | } else { |
| 1318 | QualType VecType; |
| 1319 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1320 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1321 | |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1322 | if (IType->isExtVectorType()) |
| 1323 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1324 | else |
| 1325 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1326 | IVT->getVectorKind()); |
John McCall | 6a16b2f | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1327 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1328 | StructuredList, StructuredIndex); |
| 1329 | numEltsInit += numIElts; |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1334 | if (numEltsInit != maxElements) { |
| 1335 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1336 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1337 | diag::err_vector_incorrect_num_initializers) |
| 1338 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1339 | hadError = true; |
| 1340 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1343 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1344 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1345 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1347 | unsigned &Index, |
| 1348 | InitListExpr *StructuredList, |
| 1349 | unsigned &StructuredIndex) { |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1350 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1351 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1352 | // Check for the special-case of initializing an array with a string. |
| 1353 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1354 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1355 | SIF_None) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1356 | // We place the string literal directly into the resulting |
| 1357 | // initializer list. This is the only place where the structure |
| 1358 | // of the structured initializer list doesn't match exactly, |
| 1359 | // because doing so would involve allocating one character |
| 1360 | // constant for each string. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1361 | if (!VerifyOnly) { |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1362 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1363 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1364 | IList->getInit(Index)); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1365 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1366 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1367 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1368 | return; |
| 1369 | } |
| 1370 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1371 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1372 | // Check for VLAs; in standard C it would be possible to check this |
| 1373 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1374 | // them in all sorts of strange places). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1375 | if (!VerifyOnly) |
| 1376 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1377 | diag::err_variable_object_no_init) |
| 1378 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1379 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1380 | ++Index; |
| 1381 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1382 | return; |
| 1383 | } |
| 1384 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1385 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1386 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1387 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1388 | bool maxElementsKnown = false; |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1389 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1390 | maxElements = CAT->getSize(); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1391 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1392 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1393 | maxElementsKnown = true; |
| 1394 | } |
| 1395 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1396 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1397 | while (Index < IList->getNumInits()) { |
| 1398 | Expr *Init = IList->getInit(Index); |
| 1399 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1400 | // If we're not the subobject that matches up with the '{' for |
| 1401 | // the designator, we shouldn't be handling the |
| 1402 | // designator. Return immediately. |
| 1403 | if (!SubobjectIsDesignatorContext) |
| 1404 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1405 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1406 | // Handle this designated initializer. elementIndex will be |
| 1407 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1408 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1409 | DeclType, nullptr, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1410 | StructuredList, StructuredIndex, true, |
| 1411 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1412 | hadError = true; |
| 1413 | continue; |
| 1414 | } |
| 1415 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1416 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1417 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1418 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1419 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1420 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1421 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1422 | // If the array is of incomplete type, keep track of the number of |
| 1423 | // elements in the initializer. |
| 1424 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1425 | maxElements = elementIndex; |
| 1426 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1427 | continue; |
| 1428 | } |
| 1429 | |
| 1430 | // If we know the maximum number of elements, and we've already |
| 1431 | // hit it, stop consuming elements in the initializer list. |
| 1432 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1433 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1434 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1435 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1436 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1437 | Entity); |
| 1438 | // Check this element. |
| 1439 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1440 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1441 | ++elementIndex; |
| 1442 | |
| 1443 | // If the array is of incomplete type, keep track of the number of |
| 1444 | // elements in the initializer. |
| 1445 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1446 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1447 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1448 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1449 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1450 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1451 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1452 | if (maxElements == Zero) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1453 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1454 | // but is supported by GNU. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1455 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1456 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1457 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1458 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1460 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1461 | } |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1462 | if (!hadError && VerifyOnly) { |
| 1463 | // Check if there are any members of the array that get value-initialized. |
| 1464 | // If so, check if doing that is possible. |
| 1465 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1466 | if (maxElementsKnown && elementIndex < maxElements) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1467 | CheckEmptyInitializable(InitializedEntity::InitializeElement( |
| 1468 | SemaRef.Context, 0, Entity), |
| 1469 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1470 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1473 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1474 | Expr *InitExpr, |
| 1475 | FieldDecl *Field, |
| 1476 | bool TopLevelObject) { |
| 1477 | // Handle GNU flexible array initializers. |
| 1478 | unsigned FlexArrayDiag; |
| 1479 | if (isa<InitListExpr>(InitExpr) && |
| 1480 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1481 | // Empty flexible array init always allowed as an extension |
| 1482 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1483 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1484 | // Disallow flexible array init in C++; it is not required for gcc |
| 1485 | // compatibility, and it needs work to IRGen correctly in general. |
| 1486 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1487 | } else if (!TopLevelObject) { |
| 1488 | // Disallow flexible array init on non-top-level object |
| 1489 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1490 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1491 | // Disallow flexible array init on anything which is not a variable. |
| 1492 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1493 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1494 | // Disallow flexible array init on local variables. |
| 1495 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1496 | } else { |
| 1497 | // Allow other cases. |
| 1498 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1499 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1500 | |
| 1501 | if (!VerifyOnly) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1502 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1503 | FlexArrayDiag) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1504 | << InitExpr->getLocStart(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1505 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1506 | << Field; |
| 1507 | } |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1508 | |
| 1509 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1510 | } |
| 1511 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1512 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 73eb7cd | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1513 | InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | QualType DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1515 | RecordDecl::field_iterator Field, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1517 | unsigned &Index, |
| 1518 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1519 | unsigned &StructuredIndex, |
| 1520 | bool TopLevelObject) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1521 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1523 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1524 | // confusion, we forgo checking the intializer for the entire record. |
| 1525 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 845aa66 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1526 | // Assume it was supposed to consume a single initializer. |
| 1527 | ++Index; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1528 | hadError = true; |
| 1529 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1530 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1531 | |
| 1532 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1533 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1534 | |
| 1535 | // If there's a default initializer, use it. |
| 1536 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1537 | if (VerifyOnly) |
| 1538 | return; |
| 1539 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1540 | Field != FieldEnd; ++Field) { |
| 1541 | if (Field->hasInClassInitializer()) { |
| 1542 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1543 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1544 | return; |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | // Value-initialize the first named member of the union. |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1550 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1551 | Field != FieldEnd; ++Field) { |
| 1552 | if (Field->getDeclName()) { |
| 1553 | if (VerifyOnly) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1554 | CheckEmptyInitializable( |
| 1555 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 1556 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1557 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1558 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1559 | break; |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | return; |
| 1563 | } |
| 1564 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1565 | // If structDecl is a forward declaration, this loop won't do |
| 1566 | // anything except look at designated initializers; That's okay, |
| 1567 | // because an error should get printed out elsewhere. It might be |
| 1568 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1569 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1570 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1571 | bool InitializedSomething = false; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1572 | bool CheckForMissingFields = true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1573 | while (Index < IList->getNumInits()) { |
| 1574 | Expr *Init = IList->getInit(Index); |
| 1575 | |
| 1576 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1577 | // If we're not the subobject that matches up with the '{' for |
| 1578 | // the designator, we shouldn't be handling the |
| 1579 | // designator. Return immediately. |
| 1580 | if (!SubobjectIsDesignatorContext) |
| 1581 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1582 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1583 | // Handle this designated initializer. Field will be updated to |
| 1584 | // the next field that we'll be initializing. |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1585 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1586 | DeclType, &Field, nullptr, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1587 | StructuredList, StructuredIndex, |
| 1588 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1589 | hadError = true; |
| 1590 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1591 | InitializedSomething = true; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1592 | |
| 1593 | // Disable check for missing fields when designators are used. |
| 1594 | // This matches gcc behaviour. |
| 1595 | CheckForMissingFields = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1596 | continue; |
| 1597 | } |
| 1598 | |
| 1599 | if (Field == FieldEnd) { |
| 1600 | // We've run out of fields. We're done. |
| 1601 | break; |
| 1602 | } |
| 1603 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1604 | // We've already initialized a member of a union. We're done. |
| 1605 | if (InitializedSomething && DeclType->isUnionType()) |
| 1606 | break; |
| 1607 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1608 | // If we've hit the flexible array member at the end, we're done. |
| 1609 | if (Field->getType()->isIncompleteArrayType()) |
| 1610 | break; |
| 1611 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1612 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1613 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1614 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1615 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1616 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1617 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1618 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1619 | bool InvalidUse; |
| 1620 | if (VerifyOnly) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1621 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1622 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1623 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1624 | IList->getInit(Index)->getLocStart()); |
| 1625 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1626 | ++Index; |
| 1627 | ++Field; |
| 1628 | hadError = true; |
| 1629 | continue; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1630 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1631 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1632 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1633 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1634 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1635 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1636 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1637 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1638 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1639 | // Initialize the first field within the union. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1640 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1641 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1642 | |
| 1643 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1644 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1645 | |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1646 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1647 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1648 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1649 | !DeclType->isUnionType()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1650 | // It is possible we have one or more unnamed bitfields remaining. |
| 1651 | // Find first (if any) named field and emit warning. |
| 1652 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1653 | it != end; ++it) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1654 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1655 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
Aaron Ballman | b9bb201 | 2014-01-03 14:54:10 +0000 | [diff] [blame] | 1656 | diag::warn_missing_field_initializers) << *it; |
John McCall | e40b58e | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1657 | break; |
| 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1662 | // Check that any remaining fields can be value-initialized. |
| 1663 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1664 | !Field->getType()->isIncompleteArrayType()) { |
| 1665 | // FIXME: Should check for holes left by designated initializers too. |
| 1666 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1667 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Richard Smith | 454a7cd | 2014-06-03 08:26:00 +0000 | [diff] [blame] | 1668 | CheckEmptyInitializable( |
| 1669 | InitializedEntity::InitializeMember(*Field, &Entity), |
| 1670 | IList->getLocEnd()); |
Sebastian Redl | 2b47b7a | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1671 | } |
| 1672 | } |
| 1673 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1675 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1676 | return; |
| 1677 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1678 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1679 | TopLevelObject)) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1680 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1681 | ++Index; |
| 1682 | return; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1685 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1686 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1687 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1688 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1689 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1690 | StructuredList, StructuredIndex); |
| 1691 | else |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1692 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | dbb25a3 | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1693 | StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1694 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1695 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1696 | /// \brief Expand a field designator that refers to a member of an |
| 1697 | /// anonymous struct or union into a series of field designators that |
| 1698 | /// refers to the field within the appropriate subobject. |
| 1699 | /// |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1700 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | DesignatedInitExpr *DIE, |
| 1702 | unsigned DesigIdx, |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1703 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1704 | typedef DesignatedInitExpr::Designator Designator; |
| 1705 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1706 | // Build the replacement designators. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1707 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1708 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1709 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1710 | if (PI + 1 == PE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1711 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1712 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1713 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1714 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1715 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 1716 | SourceLocation(), SourceLocation())); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1717 | assert(isa<FieldDecl>(*PI)); |
| 1718 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | // Expand the current designator into the set of replacement |
| 1722 | // designators, so we have a full subobject path down to where the |
| 1723 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1724 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1725 | &Replacements[0] + Replacements.size()); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1726 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1727 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1728 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1729 | /// corresponds to FieldName. |
| 1730 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1731 | IdentifierInfo *FieldName) { |
Argyrios Kyrtzidis | 54b1029 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1732 | if (!FieldName) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1733 | return nullptr; |
Argyrios Kyrtzidis | 54b1029 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1734 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1735 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1736 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 6d1bebb | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1737 | while (IndirectFieldDecl *IF = |
| 1738 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Argyrios Kyrtzidis | 54b1029 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1739 | if (FieldName == IF->getAnonField()->getIdentifier()) |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1740 | return IF; |
| 1741 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1742 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1743 | return nullptr; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1746 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1747 | DesignatedInitExpr *DIE) { |
| 1748 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1749 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1750 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1751 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1752 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1753 | DIE->size(), IndexExprs, |
| 1754 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1755 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1756 | } |
| 1757 | |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1758 | namespace { |
| 1759 | |
| 1760 | // Callback to only accept typo corrections that are for field members of |
| 1761 | // the given struct or union. |
| 1762 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1763 | public: |
| 1764 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1765 | : Record(RD) {} |
| 1766 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1767 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1768 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1769 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1770 | } |
| 1771 | |
| 1772 | private: |
| 1773 | RecordDecl *Record; |
| 1774 | }; |
| 1775 | |
| 1776 | } |
| 1777 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1778 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1779 | /// |
| 1780 | /// Determines whether the designated initializer @p DIE, which |
| 1781 | /// resides at the given @p Index within the initializer list @p |
| 1782 | /// IList, is well-formed for a current object of type @p DeclType |
| 1783 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1785 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1786 | /// |
| 1787 | /// @param IList The initializer list in which this designated |
| 1788 | /// initializer occurs. |
| 1789 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1790 | /// @param DIE The designated initializer expression. |
| 1791 | /// |
| 1792 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1793 | /// |
Dmitri Gribenko | adba9be | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1794 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1795 | /// into which the designation in @p DIE should refer. |
| 1796 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1797 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1798 | /// a field, this will be set to the field declaration corresponding |
| 1799 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1800 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1801 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1802 | /// DIE is an array designator or GNU array-range designator, this |
| 1803 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1804 | /// |
| 1805 | /// @param Index Index into @p IList where the designated initializer |
| 1806 | /// @p DIE occurs. |
| 1807 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1808 | /// @param StructuredList The initializer list expression that |
| 1809 | /// describes all of the subobject initializers in the order they'll |
| 1810 | /// actually be initialized. |
| 1811 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1812 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | bool |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1814 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1815 | InitListExpr *IList, |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1816 | DesignatedInitExpr *DIE, |
| 1817 | unsigned DesigIdx, |
| 1818 | QualType &CurrentObjectType, |
| 1819 | RecordDecl::field_iterator *NextField, |
| 1820 | llvm::APSInt *NextElementIndex, |
| 1821 | unsigned &Index, |
| 1822 | InitListExpr *StructuredList, |
| 1823 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1824 | bool FinishSubobjectInit, |
| 1825 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1826 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1827 | // Check the actual initialization for the designated object type. |
| 1828 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1829 | |
| 1830 | // Temporarily remove the designator expression from the |
| 1831 | // initializer list that the child calls see, so that we don't try |
| 1832 | // to re-process the designator. |
| 1833 | unsigned OldIndex = Index; |
| 1834 | IList->setInit(OldIndex, DIE->getInit()); |
| 1835 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1836 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1837 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1838 | |
| 1839 | // Restore the designated initializer expression in the syntactic |
| 1840 | // form of the initializer list. |
| 1841 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1842 | DIE->setInit(IList->getInit(OldIndex)); |
| 1843 | IList->setInit(OldIndex, DIE); |
| 1844 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1845 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1848 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1849 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1850 | if (!VerifyOnly) { |
| 1851 | assert((IsFirstDesignator || StructuredList) && |
| 1852 | "Need a non-designated initializer list to start from"); |
| 1853 | |
| 1854 | // Determine the structural initializer list that corresponds to the |
| 1855 | // current subobject. |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1856 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1857 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1858 | StructuredList, StructuredIndex, |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1859 | SourceRange(D->getLocStart(), |
| 1860 | DIE->getLocEnd())); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1861 | assert(StructuredList && "Expected a structured initializer list"); |
| 1862 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1863 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1864 | if (D->isFieldDesignator()) { |
| 1865 | // C99 6.7.8p7: |
| 1866 | // |
| 1867 | // If a designator has the form |
| 1868 | // |
| 1869 | // . identifier |
| 1870 | // |
| 1871 | // then the current object (defined below) shall have |
| 1872 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1874 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1875 | if (!RT) { |
| 1876 | SourceLocation Loc = D->getDotLoc(); |
| 1877 | if (Loc.isInvalid()) |
| 1878 | Loc = D->getFieldLoc(); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1879 | if (!VerifyOnly) |
| 1880 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1881 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1882 | ++Index; |
| 1883 | return true; |
| 1884 | } |
| 1885 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1886 | // Note: we perform a linear search of the fields here, despite |
| 1887 | // the fact that we have a faster lookup method, because we always |
| 1888 | // need to compute the field's index. |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1889 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1890 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1891 | unsigned FieldIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1893 | Field = RT->getDecl()->field_begin(), |
| 1894 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1895 | for (; Field != FieldEnd; ++Field) { |
| 1896 | if (Field->isUnnamedBitfield()) |
| 1897 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1898 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1899 | // If we find a field representing an anonymous field, look in the |
| 1900 | // IndirectFieldDecl that follow for the designated initializer. |
| 1901 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1902 | if (IndirectFieldDecl *IF = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1903 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1904 | // In verify mode, don't modify the original. |
| 1905 | if (VerifyOnly) |
| 1906 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1907 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1908 | D = DIE->getDesignator(DesigIdx); |
| 1909 | break; |
| 1910 | } |
| 1911 | } |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1912 | if (KnownField && KnownField == *Field) |
Douglas Gregor | 559c9fb | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1913 | break; |
| 1914 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1915 | break; |
| 1916 | |
| 1917 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1920 | if (Field == FieldEnd) { |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1921 | if (VerifyOnly) { |
| 1922 | ++Index; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1923 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1924 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1925 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1926 | // There was no normal field in the struct with the designated |
| 1927 | // name. Perform another lookup for this name, which may find |
| 1928 | // something that we can't designate (e.g., a member function), |
| 1929 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | // struct/union. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1931 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1932 | FieldDecl *ReplacementField = nullptr; |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1933 | if (Lookup.empty()) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1934 | // Name lookup didn't find anything. Determine whether this |
| 1935 | // was a typo for another field name. |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1936 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1937 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1938 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1939 | Sema::LookupMemberName, /*Scope=*/ nullptr, /*SS=*/ nullptr, |
| 1940 | Validator, Sema::CTK_ErrorRecovery, RT->getDecl())) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1941 | SemaRef.diagnoseTypo( |
| 1942 | Corrected, |
| 1943 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
| 1944 | << FieldName << CurrentObjectType); |
Kaelyn Uhrain | b02c5e9 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1945 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | afe718f | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1946 | hadError = true; |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1947 | } else { |
| 1948 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1949 | << FieldName << CurrentObjectType; |
| 1950 | ++Index; |
| 1951 | return true; |
| 1952 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1953 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1954 | |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1955 | if (!ReplacementField) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1956 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1957 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1958 | << FieldName; |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1959 | SemaRef.Diag(Lookup.front()->getLocation(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1960 | diag::note_field_designator_found); |
Eli Friedman | 8d25b09 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1961 | ++Index; |
| 1962 | return true; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1963 | } |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1964 | |
Francois Pichet | f3e5b4e | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1965 | if (!KnownField) { |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1966 | // The replacement field comes from typo correction; find it |
| 1967 | // in the list of fields. |
| 1968 | FieldIndex = 0; |
| 1969 | Field = RT->getDecl()->field_begin(); |
| 1970 | for (; Field != FieldEnd; ++Field) { |
| 1971 | if (Field->isUnnamedBitfield()) |
| 1972 | continue; |
| 1973 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1974 | if (ReplacementField == *Field || |
Douglas Gregor | 4e0299b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1975 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1976 | break; |
| 1977 | |
| 1978 | ++FieldIndex; |
| 1979 | } |
| 1980 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1981 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1982 | |
| 1983 | // All of the fields of a union are located at the same place in |
| 1984 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1985 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1986 | FieldIndex = 0; |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1987 | if (!VerifyOnly) { |
| 1988 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
| 1989 | if (CurrentField && CurrentField != *Field) { |
| 1990 | assert(StructuredList->getNumInits() == 1 |
| 1991 | && "A union should never have more than one initializer!"); |
| 1992 | |
| 1993 | // we're about to throw away an initializer, emit warning |
| 1994 | SemaRef.Diag(D->getFieldLoc(), |
| 1995 | diag::warn_initializer_overrides) |
| 1996 | << D->getSourceRange(); |
| 1997 | Expr *ExistingInit = StructuredList->getInit(0); |
| 1998 | SemaRef.Diag(ExistingInit->getLocStart(), |
| 1999 | diag::note_previous_initializer) |
| 2000 | << /*FIXME:has side effects=*/0 |
| 2001 | << ExistingInit->getSourceRange(); |
| 2002 | |
| 2003 | // remove existing initializer |
| 2004 | StructuredList->resizeInits(SemaRef.Context, 0); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2005 | StructuredList->setInitializedFieldInUnion(nullptr); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2008 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 274a9cc | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 2009 | } |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 2010 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2011 | |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2012 | // Make sure we can use this declaration. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2013 | bool InvalidUse; |
| 2014 | if (VerifyOnly) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2015 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2016 | else |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2017 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2018 | if (InvalidUse) { |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2019 | ++Index; |
| 2020 | return true; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2021 | } |
Douglas Gregor | a82064c | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 2022 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2023 | if (!VerifyOnly) { |
| 2024 | // Update the designator with the field declaration. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2025 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2026 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2027 | // Make sure that our non-designated initializer list has space |
| 2028 | // for a subobject corresponding to this field. |
| 2029 | if (FieldIndex >= StructuredList->getNumInits()) |
| 2030 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 2031 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2032 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2033 | // This designator names a flexible array member. |
| 2034 | if (Field->getType()->isIncompleteArrayType()) { |
| 2035 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 2036 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2037 | // We can't designate an object within the flexible array |
| 2038 | // member (because GCC doesn't allow it). |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2039 | if (!VerifyOnly) { |
| 2040 | DesignatedInitExpr::Designator *NextD |
| 2041 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 2042 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2043 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 2044 | << SourceRange(NextD->getLocStart(), |
| 2045 | DIE->getLocEnd()); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2046 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2047 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2048 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2049 | Invalid = true; |
| 2050 | } |
| 2051 | |
Chris Lattner | 001b29c | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 2052 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 2053 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2054 | // The initializer is not an initializer list. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2055 | if (!VerifyOnly) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2056 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2057 | diag::err_flexible_array_init_needs_braces) |
| 2058 | << DIE->getInit()->getSourceRange(); |
| 2059 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2060 | << *Field; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2061 | } |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2062 | Invalid = true; |
| 2063 | } |
| 2064 | |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2065 | // Check GNU flexible array initializer. |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2066 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | 3fa64df | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 2067 | TopLevelObject)) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2068 | Invalid = true; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2069 | |
| 2070 | if (Invalid) { |
| 2071 | ++Index; |
| 2072 | return true; |
| 2073 | } |
| 2074 | |
| 2075 | // Initialize the array. |
| 2076 | bool prevHadError = hadError; |
| 2077 | unsigned newStructuredIndex = FieldIndex; |
| 2078 | unsigned OldIndex = Index; |
| 2079 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2080 | |
| 2081 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2082 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2083 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2084 | StructuredList, newStructuredIndex); |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2085 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2086 | IList->setInit(OldIndex, DIE); |
| 2087 | if (hadError && !prevHadError) { |
| 2088 | ++Field; |
| 2089 | ++FieldIndex; |
| 2090 | if (NextField) |
| 2091 | *NextField = Field; |
| 2092 | StructuredIndex = FieldIndex; |
| 2093 | return true; |
| 2094 | } |
| 2095 | } else { |
| 2096 | // Recurse to check later designated subobjects. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2097 | QualType FieldType = Field->getType(); |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2098 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2099 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2100 | InitializedEntity MemberEntity = |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2101 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2102 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2103 | FieldType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2104 | StructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2105 | true, false)) |
| 2106 | return true; |
| 2107 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2108 | |
| 2109 | // Find the position of the next field to be initialized in this |
| 2110 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2111 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2112 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2113 | |
| 2114 | // If this the first designator, our caller will continue checking |
| 2115 | // the rest of this struct/class/union subobject. |
| 2116 | if (IsFirstDesignator) { |
| 2117 | if (NextField) |
| 2118 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2119 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2120 | return false; |
| 2121 | } |
| 2122 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2123 | if (!FinishSubobjectInit) |
| 2124 | return false; |
| 2125 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2126 | // We've already initialized something in the union; we're done. |
| 2127 | if (RT->getDecl()->isUnion()) |
| 2128 | return hadError; |
| 2129 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2130 | // Check the remaining fields within this class/struct/union subobject. |
| 2131 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2132 | |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2133 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2134 | StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2135 | return hadError && !prevHadError; |
| 2136 | } |
| 2137 | |
| 2138 | // C99 6.7.8p6: |
| 2139 | // |
| 2140 | // If a designator has the form |
| 2141 | // |
| 2142 | // [ constant-expression ] |
| 2143 | // |
| 2144 | // then the current object (defined below) shall have array |
| 2145 | // type and the expression shall be an integer constant |
| 2146 | // expression. If the array is of unknown size, any |
| 2147 | // nonnegative value is valid. |
| 2148 | // |
| 2149 | // Additionally, cope with the GNU extension that permits |
| 2150 | // designators of the form |
| 2151 | // |
| 2152 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2153 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2154 | if (!AT) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2155 | if (!VerifyOnly) |
| 2156 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2157 | << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2158 | ++Index; |
| 2159 | return true; |
| 2160 | } |
| 2161 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2162 | Expr *IndexExpr = nullptr; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2163 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2164 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2165 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2166 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2167 | DesignatedEndIndex = DesignatedStartIndex; |
| 2168 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2169 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2170 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | DesignatedStartIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2172 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2173 | DesignatedEndIndex = |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2174 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2175 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2176 | |
Chris Lattner | b0ed51d | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2177 | // Codegen can't handle evaluating array range designators that have side |
| 2178 | // effects, because we replicate the AST value for each initialized element. |
| 2179 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2180 | // elements with something that has a side effect, so codegen can emit an |
| 2181 | // "error unsupported" error instead of miscompiling the app. |
| 2182 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2183 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2184 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2187 | if (isa<ConstantArrayType>(AT)) { |
| 2188 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2189 | DesignatedStartIndex |
| 2190 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2191 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2192 | DesignatedEndIndex |
| 2193 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2194 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2195 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | ed0f916 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2196 | if (!VerifyOnly) |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2197 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2198 | diag::err_array_designator_too_large) |
| 2199 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2200 | << IndexExpr->getSourceRange(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2201 | ++Index; |
| 2202 | return true; |
| 2203 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2204 | } else { |
| 2205 | // Make sure the bit-widths and signedness match. |
| 2206 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2207 | DesignatedEndIndex |
| 2208 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2209 | else if (DesignatedStartIndex.getBitWidth() < |
| 2210 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2211 | DesignatedStartIndex |
| 2212 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2213 | DesignatedStartIndex.setIsUnsigned(true); |
| 2214 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2215 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2216 | |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2217 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2218 | // We're modifying a string literal init; we have to decompose the string |
| 2219 | // so we can modify the individual characters. |
| 2220 | ASTContext &Context = SemaRef.Context; |
| 2221 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2222 | |
| 2223 | // Compute the character type |
| 2224 | QualType CharTy = AT->getElementType(); |
| 2225 | |
| 2226 | // Compute the type of the integer literals. |
| 2227 | QualType PromotedCharTy = CharTy; |
| 2228 | if (CharTy->isPromotableIntegerType()) |
| 2229 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2230 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2231 | |
| 2232 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2233 | // Get the length of the string. |
| 2234 | uint64_t StrLen = SL->getLength(); |
| 2235 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2236 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2237 | StructuredList->resizeInits(Context, StrLen); |
| 2238 | |
| 2239 | // Build a literal for each character in the string, and put them into |
| 2240 | // the init list. |
| 2241 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2242 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2243 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2244 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2245 | if (CharTy != PromotedCharTy) |
| 2246 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2247 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2248 | StructuredList->updateInit(Context, i, Init); |
| 2249 | } |
| 2250 | } else { |
| 2251 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2252 | std::string Str; |
| 2253 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2254 | |
| 2255 | // Get the length of the string. |
| 2256 | uint64_t StrLen = Str.size(); |
| 2257 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2258 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2259 | StructuredList->resizeInits(Context, StrLen); |
| 2260 | |
| 2261 | // Build a literal for each character in the string, and put them into |
| 2262 | // the init list. |
| 2263 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2264 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2265 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 6cc05f7 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2266 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2267 | if (CharTy != PromotedCharTy) |
| 2268 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2269 | Init, nullptr, VK_RValue); |
Eli Friedman | 1f16b74 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2270 | StructuredList->updateInit(Context, i, Init); |
| 2271 | } |
| 2272 | } |
| 2273 | } |
| 2274 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2275 | // Make sure that our non-designated initializer list has space |
| 2276 | // for a subobject corresponding to this array element. |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2277 | if (!VerifyOnly && |
| 2278 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2279 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2280 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2281 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2282 | // Repeatedly perform subobject initializations in the range |
| 2283 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2284 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2285 | // Move to the next designator |
| 2286 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2287 | unsigned OldIndex = Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2288 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2289 | InitializedEntity ElementEntity = |
Anders Carlsson | 6cabf31 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2290 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2291 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2292 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2293 | // Recurse to check later designated subobjects. |
| 2294 | QualType ElementType = AT->getElementType(); |
| 2295 | Index = OldIndex; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2296 | |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2297 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2298 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2299 | ElementType, nullptr, nullptr, Index, |
Anders Carlsson | 3fa93b7 | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2300 | StructuredList, ElementIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2301 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2302 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2303 | return true; |
| 2304 | |
| 2305 | // Move to the next index in the array that we'll be initializing. |
| 2306 | ++DesignatedStartIndex; |
| 2307 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2308 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2309 | |
| 2310 | // If this the first designator, our caller will continue checking |
| 2311 | // the rest of this array subobject. |
| 2312 | if (IsFirstDesignator) { |
| 2313 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2314 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2315 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2316 | return false; |
| 2317 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2318 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2319 | if (!FinishSubobjectInit) |
| 2320 | return false; |
| 2321 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2322 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2323 | bool prevHadError = hadError; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2324 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 0cf999b | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2325 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2326 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2327 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2330 | // Get the structured initializer list for a subobject of type |
| 2331 | // @p CurrentObjectType. |
| 2332 | InitListExpr * |
| 2333 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2334 | QualType CurrentObjectType, |
| 2335 | InitListExpr *StructuredList, |
| 2336 | unsigned StructuredIndex, |
| 2337 | SourceRange InitRange) { |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2338 | if (VerifyOnly) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2339 | return nullptr; // No structured list in verification-only mode. |
| 2340 | Expr *ExistingInit = nullptr; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2341 | if (!StructuredList) |
Benjamin Kramer | 6b441d6 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2342 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2343 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2344 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2345 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2346 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2347 | return Result; |
| 2348 | |
| 2349 | if (ExistingInit) { |
| 2350 | // We are creating an initializer list that initializes the |
| 2351 | // subobjects of the current object, but there was already an |
| 2352 | // initialization that completely initialized the current |
| 2353 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2354 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2355 | // struct X { int a, b; }; |
| 2356 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2357 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2358 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2359 | // designated initializer re-initializes the whole |
| 2360 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2361 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2362 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2363 | << InitRange; |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2364 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2365 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2366 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2367 | << ExistingInit->getSourceRange(); |
| 2368 | } |
| 2369 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2370 | InitListExpr *Result |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2371 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2372 | InitRange.getBegin(), None, |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2373 | InitRange.getEnd()); |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2374 | |
Eli Friedman | 91f5ae5 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2375 | QualType ResultType = CurrentObjectType; |
| 2376 | if (!ResultType->isArrayType()) |
| 2377 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2378 | Result->setType(ResultType); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2379 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2380 | // Pre-allocate storage for the structured initializer list. |
| 2381 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2382 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2383 | bool GotNumInits = false; |
| 2384 | if (!StructuredList) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2385 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2386 | GotNumInits = true; |
| 2387 | } else if (Index < IList->getNumInits()) { |
| 2388 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2389 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2390 | GotNumInits = true; |
| 2391 | } |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2394 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2395 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2396 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2397 | NumElements = CAType->getSize().getZExtValue(); |
| 2398 | // Simple heuristic so that we don't allocate a very large |
| 2399 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | fddbcfb | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2400 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2401 | NumElements = 0; |
| 2402 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2403 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2404 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2405 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2406 | RecordDecl *RDecl = RType->getDecl(); |
| 2407 | if (RDecl->isUnion()) |
| 2408 | NumElements = 1; |
| 2409 | else |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 2410 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2413 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2414 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2415 | // Link this new initializer list into the structured initializer |
| 2416 | // lists. |
| 2417 | if (StructuredList) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2418 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2419 | else { |
| 2420 | Result->setSyntacticForm(IList); |
| 2421 | SyntacticToSemantic[IList] = Result; |
| 2422 | } |
| 2423 | |
| 2424 | return Result; |
| 2425 | } |
| 2426 | |
| 2427 | /// Update the initializer at index @p StructuredIndex within the |
| 2428 | /// structured initializer list to the value @p expr. |
| 2429 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2430 | unsigned &StructuredIndex, |
| 2431 | Expr *expr) { |
| 2432 | // No structured initializer list to update |
| 2433 | if (!StructuredList) |
| 2434 | return; |
| 2435 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2436 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2437 | StructuredIndex, expr)) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2438 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2439 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2440 | diag::warn_initializer_overrides) |
| 2441 | << expr->getSourceRange(); |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2442 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2443 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2444 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2445 | << PrevInit->getSourceRange(); |
| 2446 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2448 | ++StructuredIndex; |
| 2449 | } |
| 2450 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2451 | /// Check that the given Index expression is a valid array designator |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2452 | /// value. This is essentially just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2453 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2454 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2455 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2456 | /// added, on success. If everything went okay, Value will receive the |
| 2457 | /// value of the constant expression. |
| 2458 | static ExprResult |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2459 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 62ee641 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2460 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2461 | |
| 2462 | // Make sure this is an integer constant expression. |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2463 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2464 | if (Result.isInvalid()) |
| 2465 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2466 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2467 | if (Value.isSigned() && Value.isNegative()) |
| 2468 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2469 | << Value.toString(10) << Index->getSourceRange(); |
| 2470 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2471 | Value.setIsUnsigned(true); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2472 | return Result; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2475 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2476 | SourceLocation Loc, |
| 2477 | bool GNUSyntax, |
| 2478 | ExprResult Init) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2479 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2480 | |
| 2481 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2482 | SmallVector<ASTDesignator, 32> Designators; |
| 2483 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2484 | |
| 2485 | // Build designators and check array designator expressions. |
| 2486 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2487 | const Designator &D = Desig.getDesignator(Idx); |
| 2488 | switch (D.getKind()) { |
| 2489 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2490 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2491 | D.getFieldLoc())); |
| 2492 | break; |
| 2493 | |
| 2494 | case Designator::ArrayDesignator: { |
| 2495 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2496 | llvm::APSInt IndexValue; |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2497 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2498 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2499 | if (!Index) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2500 | Invalid = true; |
| 2501 | else { |
| 2502 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2503 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2504 | D.getRBracketLoc())); |
| 2505 | InitExpressions.push_back(Index); |
| 2506 | } |
| 2507 | break; |
| 2508 | } |
| 2509 | |
| 2510 | case Designator::ArrayRangeDesignator: { |
| 2511 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2512 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2513 | llvm::APSInt StartValue; |
| 2514 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2515 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2516 | StartIndex->isValueDependent(); |
| 2517 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2518 | EndIndex->isValueDependent(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2519 | if (!StartDependent) |
| 2520 | StartIndex = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2521 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2522 | if (!EndDependent) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2523 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get(); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2524 | |
| 2525 | if (!StartIndex || !EndIndex) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2526 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2527 | else { |
| 2528 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2529 | if (StartDependent || EndDependent) { |
| 2530 | // Nothing to compute. |
| 2531 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2532 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2533 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2534 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2535 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2536 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2537 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2538 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2539 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2540 | Invalid = true; |
| 2541 | } else { |
| 2542 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2543 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2544 | D.getEllipsisLoc(), |
| 2545 | D.getRBracketLoc())); |
| 2546 | InitExpressions.push_back(StartIndex); |
| 2547 | InitExpressions.push_back(EndIndex); |
| 2548 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2549 | } |
| 2550 | break; |
| 2551 | } |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | if (Invalid || Init.isInvalid()) |
| 2556 | return ExprError(); |
| 2557 | |
| 2558 | // Clear out the expressions within the designation. |
| 2559 | Desig.ClearExprs(*this); |
| 2560 | |
| 2561 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2562 | = DesignatedInitExpr::Create(Context, |
| 2563 | Designators.data(), Designators.size(), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2564 | InitExpressions, Loc, GNUSyntax, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2565 | Init.getAs<Expr>()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2566 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2567 | if (!getLangOpts().C99) |
Douglas Gregor | c124e59 | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2568 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2569 | << DIE->getSourceRange(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2570 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2571 | return DIE; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2572 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2573 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2574 | //===----------------------------------------------------------------------===// |
| 2575 | // Initialization entity |
| 2576 | //===----------------------------------------------------------------------===// |
| 2577 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2578 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2579 | const InitializedEntity &Parent) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2580 | : Parent(&Parent), Index(Index) |
Douglas Gregor | 723796a | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2581 | { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2582 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2583 | Kind = EK_ArrayElement; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2584 | Type = AT->getElementType(); |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2585 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2586 | Kind = EK_VectorElement; |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2587 | Type = VT->getElementType(); |
| 2588 | } else { |
| 2589 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2590 | assert(CT && "Unexpected type"); |
| 2591 | Kind = EK_ComplexElement; |
| 2592 | Type = CT->getElementType(); |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2593 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Benjamin Kramer | 8bf4435 | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2596 | InitializedEntity |
| 2597 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 2598 | const CXXBaseSpecifier *Base, |
| 2599 | bool IsInheritedVirtualBase) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2600 | InitializedEntity Result; |
| 2601 | Result.Kind = EK_Base; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2602 | Result.Parent = nullptr; |
Anders Carlsson | 43c64af | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2603 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2604 | if (IsInheritedVirtualBase) |
| 2605 | Result.Base |= 0x01; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2606 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2607 | Result.Type = Base->getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2608 | return Result; |
| 2609 | } |
| 2610 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2611 | DeclarationName InitializedEntity::getName() const { |
| 2612 | switch (getKind()) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2613 | case EK_Parameter: |
| 2614 | case EK_Parameter_CF_Audited: { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2615 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2616 | return (D ? D->getDeclName() : DeclarationName()); |
| 2617 | } |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2618 | |
| 2619 | case EK_Variable: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2620 | case EK_Member: |
| 2621 | return VariableOrMember->getDeclName(); |
| 2622 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2623 | case EK_LambdaCapture: |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 2624 | return DeclarationName(Capture.VarID); |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2625 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2626 | case EK_Result: |
| 2627 | case EK_Exception: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2628 | case EK_New: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 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: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2636 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2637 | case EK_RelatedResult: |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2638 | return DeclarationName(); |
| 2639 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2640 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2641 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2644 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2645 | switch (getKind()) { |
| 2646 | case EK_Variable: |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2647 | case EK_Member: |
| 2648 | return VariableOrMember; |
| 2649 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2650 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2651 | case EK_Parameter_CF_Audited: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2652 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2653 | |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2654 | case EK_Result: |
| 2655 | case EK_Exception: |
| 2656 | case EK_New: |
| 2657 | case EK_Temporary: |
| 2658 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2659 | case EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +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: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2665 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2666 | case EK_RelatedResult: |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2667 | return nullptr; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2668 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2669 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2670 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2673 | bool InitializedEntity::allowsNRVO() const { |
| 2674 | switch (getKind()) { |
| 2675 | case EK_Result: |
| 2676 | case EK_Exception: |
| 2677 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2678 | |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2679 | case EK_Variable: |
| 2680 | case EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2681 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2682 | case EK_Member: |
| 2683 | case EK_New: |
| 2684 | case EK_Temporary: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2685 | case EK_CompoundLiteralInit: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2686 | case EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2687 | case EK_Delegating: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2688 | case EK_ArrayElement: |
| 2689 | case EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2690 | case EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2691 | case EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2692 | case EK_LambdaCapture: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2693 | case EK_RelatedResult: |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2694 | break; |
| 2695 | } |
| 2696 | |
| 2697 | return false; |
| 2698 | } |
| 2699 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2700 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | e3b28bc | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2701 | assert(getParent() != this); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2702 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 2703 | for (unsigned I = 0; I != Depth; ++I) |
| 2704 | OS << "`-"; |
| 2705 | |
| 2706 | switch (getKind()) { |
| 2707 | case EK_Variable: OS << "Variable"; break; |
| 2708 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2709 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 2710 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2711 | case EK_Result: OS << "Result"; break; |
| 2712 | case EK_Exception: OS << "Exception"; break; |
| 2713 | case EK_Member: OS << "Member"; break; |
| 2714 | case EK_New: OS << "New"; break; |
| 2715 | case EK_Temporary: OS << "Temporary"; break; |
| 2716 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2717 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2718 | case EK_Base: OS << "Base"; break; |
| 2719 | case EK_Delegating: OS << "Delegating"; break; |
| 2720 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 2721 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 2722 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 2723 | case EK_BlockElement: OS << "Block"; break; |
| 2724 | case EK_LambdaCapture: |
| 2725 | OS << "LambdaCapture "; |
Faisal Vali | 5fb7c3c | 2013-12-05 01:40:41 +0000 | [diff] [blame] | 2726 | OS << DeclarationName(Capture.VarID); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2727 | break; |
| 2728 | } |
| 2729 | |
| 2730 | if (Decl *D = getDecl()) { |
| 2731 | OS << " "; |
| 2732 | cast<NamedDecl>(D)->printQualifiedName(OS); |
| 2733 | } |
| 2734 | |
| 2735 | OS << " '" << getType().getAsString() << "'\n"; |
| 2736 | |
| 2737 | return Depth + 1; |
| 2738 | } |
| 2739 | |
| 2740 | void InitializedEntity::dump() const { |
| 2741 | dumpImpl(llvm::errs()); |
| 2742 | } |
| 2743 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2744 | //===----------------------------------------------------------------------===// |
| 2745 | // Initialization sequence |
| 2746 | //===----------------------------------------------------------------------===// |
| 2747 | |
| 2748 | void InitializationSequence::Step::Destroy() { |
| 2749 | switch (Kind) { |
| 2750 | case SK_ResolveAddressOfOverloadedFunction: |
| 2751 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2752 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2753 | case SK_CastDerivedToBaseLValue: |
| 2754 | case SK_BindReference: |
| 2755 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2756 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2757 | case SK_UserConversion: |
| 2758 | case SK_QualificationConversionRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2759 | case SK_QualificationConversionXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2760 | case SK_QualificationConversionLValue: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2761 | case SK_LValueToRValue: |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2762 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2763 | case SK_UnwrapInitList: |
| 2764 | case SK_RewrapInitList: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2765 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 2766 | case SK_ConstructorInitializationFromList: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2767 | case SK_ZeroInitialization: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2768 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2769 | case SK_StringInit: |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2770 | case SK_ObjCObjectConversion: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2771 | case SK_ArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2772 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2773 | case SK_PassByIndirectCopyRestore: |
| 2774 | case SK_PassByIndirectRestore: |
| 2775 | case SK_ProduceObjCObject: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2776 | case SK_StdInitializerList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 2777 | case SK_StdInitializerListConstructorCall: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2778 | case SK_OCLSamplerInit: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2779 | case SK_OCLZeroEvent: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2780 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2782 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2783 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2784 | delete ICS; |
| 2785 | } |
| 2786 | } |
| 2787 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2788 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2789 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2793 | if (!Failed()) |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2794 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2795 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2796 | switch (getFailureKind()) { |
| 2797 | case FK_TooManyInitsForReference: |
| 2798 | case FK_ArrayNeedsInitList: |
| 2799 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 2800 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 2801 | case FK_NarrowStringIntoWideCharArray: |
| 2802 | case FK_WideStringIntoCharArray: |
| 2803 | case FK_IncompatWideStringIntoWideChar: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2804 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2805 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2806 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2807 | case FK_RValueReferenceBindingToLValue: |
| 2808 | case FK_ReferenceInitDropsQualifiers: |
| 2809 | case FK_ReferenceInitFailed: |
| 2810 | case FK_ConversionFailed: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2811 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2812 | case FK_TooManyInitsForScalar: |
| 2813 | case FK_ReferenceBindingToInitList: |
| 2814 | case FK_InitListBadDestinationType: |
| 2815 | case FK_DefaultInitOfConst: |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2816 | case FK_Incomplete: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2817 | case FK_ArrayTypeMismatch: |
| 2818 | case FK_NonConstantArrayInit: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2819 | case FK_ListInitializationFailed: |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2820 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2821 | case FK_PlaceholderType: |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2822 | case FK_ExplicitConstructor: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2823 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2824 | |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2825 | case FK_ReferenceInitOverloadFailed: |
| 2826 | case FK_UserConversionOverloadFailed: |
| 2827 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2828 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2829 | return FailedOverloadResult == OR_Ambiguous; |
| 2830 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2831 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2832 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 838fcc3 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Douglas Gregor | b33eed0 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2835 | bool InitializationSequence::isConstructorInitialization() const { |
| 2836 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2837 | } |
| 2838 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2839 | void |
| 2840 | InitializationSequence |
| 2841 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2842 | DeclAccessPair Found, |
| 2843 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2844 | Step S; |
| 2845 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2846 | S.Type = Function->getType(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2847 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2848 | S.Function.Function = Function; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2849 | S.Function.FoundDecl = Found; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2850 | Steps.push_back(S); |
| 2851 | } |
| 2852 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2853 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2854 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2855 | Step S; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2856 | switch (VK) { |
| 2857 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2858 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2859 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2860 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2861 | S.Type = BaseType; |
| 2862 | Steps.push_back(S); |
| 2863 | } |
| 2864 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2865 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2866 | bool BindingTemporary) { |
| 2867 | Step S; |
| 2868 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2869 | S.Type = T; |
| 2870 | Steps.push_back(S); |
| 2871 | } |
| 2872 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2873 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2874 | Step S; |
| 2875 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2876 | S.Type = T; |
| 2877 | Steps.push_back(S); |
| 2878 | } |
| 2879 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2880 | void |
| 2881 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2882 | DeclAccessPair FoundDecl, |
| 2883 | QualType T, |
| 2884 | bool HadMultipleCandidates) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2885 | Step S; |
| 2886 | S.Kind = SK_UserConversion; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2887 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2888 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2889 | S.Function.Function = Function; |
| 2890 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2891 | Steps.push_back(S); |
| 2892 | } |
| 2893 | |
| 2894 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2895 | ExprValueKind VK) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2896 | Step S; |
John McCall | 7a1da89 | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2897 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2898 | switch (VK) { |
| 2899 | case VK_RValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2900 | S.Kind = SK_QualificationConversionRValue; |
| 2901 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2902 | case VK_XValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2903 | S.Kind = SK_QualificationConversionXValue; |
| 2904 | break; |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2905 | case VK_LValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2906 | S.Kind = SK_QualificationConversionLValue; |
| 2907 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2908 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2909 | S.Type = Ty; |
| 2910 | Steps.push_back(S); |
| 2911 | } |
| 2912 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2913 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 2914 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 2915 | |
| 2916 | Step S; |
| 2917 | S.Kind = SK_LValueToRValue; |
| 2918 | S.Type = Ty; |
| 2919 | Steps.push_back(S); |
| 2920 | } |
| 2921 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2922 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2923 | const ImplicitConversionSequence &ICS, QualType T, |
| 2924 | bool TopLevelOfInitList) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2925 | Step S; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2926 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 2927 | : SK_ConversionSequence; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2928 | S.Type = T; |
| 2929 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2930 | Steps.push_back(S); |
| 2931 | } |
| 2932 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2933 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2934 | Step S; |
| 2935 | S.Kind = SK_ListInitialization; |
| 2936 | S.Type = T; |
| 2937 | Steps.push_back(S); |
| 2938 | } |
| 2939 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2940 | void |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2941 | InitializationSequence |
| 2942 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2943 | AccessSpecifier Access, |
| 2944 | QualType T, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2945 | bool HadMultipleCandidates, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2946 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2947 | Step S; |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 2948 | S.Kind = FromInitList ? AsInitList ? SK_StdInitializerListConstructorCall |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 2949 | : SK_ConstructorInitializationFromList |
| 2950 | : SK_ConstructorInitialization; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2951 | S.Type = T; |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2952 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2953 | S.Function.Function = Constructor; |
| 2954 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2955 | Steps.push_back(S); |
| 2956 | } |
| 2957 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2958 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2959 | Step S; |
| 2960 | S.Kind = SK_ZeroInitialization; |
| 2961 | S.Type = T; |
| 2962 | Steps.push_back(S); |
| 2963 | } |
| 2964 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2965 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2966 | Step S; |
| 2967 | S.Kind = SK_CAssignment; |
| 2968 | S.Type = T; |
| 2969 | Steps.push_back(S); |
| 2970 | } |
| 2971 | |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2972 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2973 | Step S; |
| 2974 | S.Kind = SK_StringInit; |
| 2975 | S.Type = T; |
| 2976 | Steps.push_back(S); |
| 2977 | } |
| 2978 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2979 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2980 | Step S; |
| 2981 | S.Kind = SK_ObjCObjectConversion; |
| 2982 | S.Type = T; |
| 2983 | Steps.push_back(S); |
| 2984 | } |
| 2985 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2986 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2987 | Step S; |
| 2988 | S.Kind = SK_ArrayInit; |
| 2989 | S.Type = T; |
| 2990 | Steps.push_back(S); |
| 2991 | } |
| 2992 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2993 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2994 | Step S; |
| 2995 | S.Kind = SK_ParenthesizedArrayInit; |
| 2996 | S.Type = T; |
| 2997 | Steps.push_back(S); |
| 2998 | } |
| 2999 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3000 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 3001 | bool shouldCopy) { |
| 3002 | Step s; |
| 3003 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 3004 | : SK_PassByIndirectRestore); |
| 3005 | s.Type = type; |
| 3006 | Steps.push_back(s); |
| 3007 | } |
| 3008 | |
| 3009 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 3010 | Step S; |
| 3011 | S.Kind = SK_ProduceObjCObject; |
| 3012 | S.Type = T; |
| 3013 | Steps.push_back(S); |
| 3014 | } |
| 3015 | |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 3016 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 3017 | Step S; |
| 3018 | S.Kind = SK_StdInitializerList; |
| 3019 | S.Type = T; |
| 3020 | Steps.push_back(S); |
| 3021 | } |
| 3022 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3023 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 3024 | Step S; |
| 3025 | S.Kind = SK_OCLSamplerInit; |
| 3026 | S.Type = T; |
| 3027 | Steps.push_back(S); |
| 3028 | } |
| 3029 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3030 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 3031 | Step S; |
| 3032 | S.Kind = SK_OCLZeroEvent; |
| 3033 | S.Type = T; |
| 3034 | Steps.push_back(S); |
| 3035 | } |
| 3036 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3037 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 3038 | InitListExpr *Syntactic) { |
| 3039 | assert(Syntactic->getNumInits() == 1 && |
| 3040 | "Can only rewrap trivial init lists."); |
| 3041 | Step S; |
| 3042 | S.Kind = SK_UnwrapInitList; |
| 3043 | S.Type = Syntactic->getInit(0)->getType(); |
| 3044 | Steps.insert(Steps.begin(), S); |
| 3045 | |
| 3046 | S.Kind = SK_RewrapInitList; |
| 3047 | S.Type = T; |
| 3048 | S.WrappingSyntacticList = Syntactic; |
| 3049 | Steps.push_back(S); |
| 3050 | } |
| 3051 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3052 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3053 | OverloadingResult Result) { |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3054 | setSequenceKind(FailedSequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3055 | this->Failure = Failure; |
| 3056 | this->FailedOverloadResult = Result; |
| 3057 | } |
| 3058 | |
| 3059 | //===----------------------------------------------------------------------===// |
| 3060 | // Attempt initialization |
| 3061 | //===----------------------------------------------------------------------===// |
| 3062 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3063 | static void MaybeProduceObjCObject(Sema &S, |
| 3064 | InitializationSequence &Sequence, |
| 3065 | const InitializedEntity &Entity) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3066 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3067 | |
| 3068 | /// When initializing a parameter, produce the value if it's marked |
| 3069 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 3070 | if (Entity.isParameterKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3071 | if (!Entity.isParameterConsumed()) |
| 3072 | return; |
| 3073 | |
| 3074 | assert(Entity.getType()->isObjCRetainableType() && |
| 3075 | "consuming an object of unretainable type?"); |
| 3076 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3077 | |
| 3078 | /// When initializing a return value, if the return type is a |
| 3079 | /// retainable type, then returns need to immediately retain the |
| 3080 | /// object. If an autorelease is required, it will be done at the |
| 3081 | /// last instant. |
| 3082 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 3083 | if (!Entity.getType()->isObjCRetainableType()) |
| 3084 | return; |
| 3085 | |
| 3086 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 3087 | } |
| 3088 | } |
| 3089 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3090 | static void TryListInitialization(Sema &S, |
| 3091 | const InitializedEntity &Entity, |
| 3092 | const InitializationKind &Kind, |
| 3093 | InitListExpr *InitList, |
| 3094 | InitializationSequence &Sequence); |
| 3095 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3096 | /// \brief When initializing from init list via constructor, handle |
| 3097 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3098 | /// |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3099 | /// \return true if we have handled initialization of an object of type |
| 3100 | /// std::initializer_list<T>, false otherwise. |
| 3101 | static bool TryInitializerListConstruction(Sema &S, |
| 3102 | InitListExpr *List, |
| 3103 | QualType DestType, |
| 3104 | InitializationSequence &Sequence) { |
| 3105 | QualType E; |
| 3106 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3107 | return false; |
| 3108 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3109 | if (S.RequireCompleteType(List->getExprLoc(), E, 0)) { |
| 3110 | Sequence.setIncompleteTypeFailure(E); |
| 3111 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3112 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 3113 | |
| 3114 | // Try initializing a temporary array from the init list. |
| 3115 | QualType ArrayType = S.Context.getConstantArrayType( |
| 3116 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 3117 | List->getNumInits()), |
| 3118 | clang::ArrayType::Normal, 0); |
| 3119 | InitializedEntity HiddenArray = |
| 3120 | InitializedEntity::InitializeTemporary(ArrayType); |
| 3121 | InitializationKind Kind = |
| 3122 | InitializationKind::CreateDirectList(List->getExprLoc()); |
| 3123 | TryListInitialization(S, HiddenArray, Kind, List, Sequence); |
| 3124 | if (Sequence) |
| 3125 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3126 | return true; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3129 | static OverloadingResult |
| 3130 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3131 | MultiExprArg Args, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3132 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3133 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3134 | OverloadCandidateSet::iterator &Best, |
| 3135 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3136 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3137 | CandidateSet.clear(); |
| 3138 | |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3139 | for (ArrayRef<NamedDecl *>::iterator |
| 3140 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3141 | NamedDecl *D = *Con; |
| 3142 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3143 | bool SuppressUserConversions = false; |
| 3144 | |
| 3145 | // Find the constructor (which may be a template). |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3146 | CXXConstructorDecl *Constructor = nullptr; |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3147 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 3148 | if (ConstructorTmpl) |
| 3149 | Constructor = cast<CXXConstructorDecl>( |
| 3150 | ConstructorTmpl->getTemplatedDecl()); |
| 3151 | else { |
| 3152 | Constructor = cast<CXXConstructorDecl>(D); |
| 3153 | |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3154 | // C++11 [over.best.ics]p4: |
| 3155 | // However, when considering the argument of a constructor or |
| 3156 | // user-defined conversion function that is a candidate: |
| 3157 | // -- by 13.3.1.3 when invoked for the copying/moving of a temporary |
| 3158 | // in the second step of a class copy-initialization, |
| 3159 | // -- by 13.3.1.7 when passing the initializer list as a single |
| 3160 | // argument or when the initializer list has exactly one elementand |
| 3161 | // a conversion to some class X or reference to (possibly |
| 3162 | // cv-qualified) X is considered for the first parameter of a |
| 3163 | // constructor of X, or |
| 3164 | // -- by 13.3.1.4, 13.3.1.5, or 13.3.1.6 in all cases, |
| 3165 | // only standard conversion sequences and ellipsis conversion sequences |
| 3166 | // are considered. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3167 | if ((CopyInitializing || (InitListSyntax && Args.size() == 1)) && |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3168 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3169 | SuppressUserConversions = true; |
| 3170 | } |
| 3171 | |
| 3172 | if (!Constructor->isInvalidDecl() && |
| 3173 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3174 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3175 | if (ConstructorTmpl) |
| 3176 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3177 | /*ExplicitArgs*/ nullptr, Args, |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3178 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3179 | else { |
| 3180 | // C++ [over.match.copy]p1: |
| 3181 | // - When initializing a temporary to be bound to the first parameter |
| 3182 | // of a constructor that takes a reference to possibly cv-qualified |
| 3183 | // T as its first argument, called with a single argument in the |
| 3184 | // context of direct-initialization, explicit conversion functions |
| 3185 | // are also considered. |
| 3186 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3187 | Args.size() == 1 && |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3188 | Constructor->isCopyOrMoveConstructor(); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3189 | S.AddOverloadCandidate(Constructor, FoundDecl, Args, CandidateSet, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3190 | SuppressUserConversions, |
| 3191 | /*PartialOverloading=*/false, |
| 3192 | /*AllowExplicit=*/AllowExplicitConv); |
| 3193 | } |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | // Perform overload resolution and return the result. |
| 3198 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3199 | } |
| 3200 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3201 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3202 | /// enumerates the constructors of the initialized entity and performs overload |
| 3203 | /// resolution to select the best. |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3204 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3205 | /// class type. |
| 3206 | static void TryConstructorInitialization(Sema &S, |
| 3207 | const InitializedEntity &Entity, |
| 3208 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3209 | MultiExprArg Args, QualType DestType, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3210 | InitializationSequence &Sequence, |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3211 | bool InitListSyntax = false) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3212 | assert((!InitListSyntax || (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3213 | "InitListSyntax must come with a single initializer list argument."); |
| 3214 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3215 | // The type we're constructing needs to be complete. |
| 3216 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3217 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3218 | return; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3222 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3223 | CXXRecordDecl *DestRecordDecl |
| 3224 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3225 | |
Sebastian Redl | ab3f7a4 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3226 | // Build the candidate set directly in the initialization sequence |
| 3227 | // structure, so that it will persist if we fail. |
| 3228 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3229 | |
| 3230 | // Determine whether we are allowed to call explicit constructors or |
| 3231 | // explicit conversion operators. |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3232 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3233 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3234 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3235 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3236 | // applicable constructors are enumerated, and the best one is chosen |
| 3237 | // through overload resolution. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3238 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3239 | // The container holding the constructors can under certain conditions |
| 3240 | // be changed while iterating (e.g. because of deserialization). |
| 3241 | // To be safe we copy the lookup results to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3242 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3243 | |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3244 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3245 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3246 | bool AsInitializerList = false; |
| 3247 | |
| 3248 | // C++11 [over.match.list]p1: |
| 3249 | // When objects of non-aggregate type T are list-initialized, overload |
| 3250 | // resolution selects the constructor in two phases: |
| 3251 | // - Initially, the candidate functions are the initializer-list |
| 3252 | // constructors of the class T and the argument list consists of the |
| 3253 | // initializer list as a single argument. |
| 3254 | if (InitListSyntax) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3255 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3256 | AsInitializerList = true; |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3257 | |
| 3258 | // If the initializer list has no elements and T has a default constructor, |
| 3259 | // the first phase is omitted. |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3260 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3261 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3262 | CandidateSet, Ctors, Best, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3263 | CopyInitialization, AllowExplicit, |
| 3264 | /*OnlyListConstructor=*/true, |
| 3265 | InitListSyntax); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3266 | |
| 3267 | // Time to unwrap the init list. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3268 | Args = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | // C++11 [over.match.list]p1: |
| 3272 | // - If no viable initializer-list constructor is found, overload resolution |
| 3273 | // is performed again, where the candidate functions are all the |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3274 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3275 | // elements of the initializer list. |
| 3276 | if (Result == OR_No_Viable_Function) { |
| 3277 | AsInitializerList = false; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3278 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3279 | CandidateSet, Ctors, Best, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3280 | CopyInitialization, AllowExplicit, |
Sebastian Redl | c7b718e | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3281 | /*OnlyListConstructors=*/false, |
| 3282 | InitListSyntax); |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3283 | } |
| 3284 | if (Result) { |
Sebastian Redl | 88e4d49 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3285 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3286 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3287 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3288 | Result); |
| 3289 | return; |
| 3290 | } |
| 3291 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3292 | // C++11 [dcl.init]p6: |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3293 | // If a program calls for the default initialization of an object |
| 3294 | // of a const-qualified type T, T shall be a class type with a |
| 3295 | // user-provided default constructor. |
| 3296 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3297 | Entity.getType().isConstQualified() && |
Aaron Ballman | 899b9c6 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 3298 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3299 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3300 | return; |
| 3301 | } |
| 3302 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3303 | // C++11 [over.match.list]p1: |
| 3304 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3305 | // initializer is ill-formed. |
| 3306 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 3307 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 3308 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3309 | return; |
| 3310 | } |
| 3311 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3312 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3313 | // subsumed by the initialization. |
| 3314 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3315 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 3316 | Best->FoundDecl.getAccess(), |
| 3317 | DestType, HadMultipleCandidates, |
Sebastian Redl | 860eb7c | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3318 | InitListSyntax, AsInitializerList); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3321 | static bool |
| 3322 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3323 | Expr *Initializer, |
| 3324 | QualType &SourceType, |
| 3325 | QualType &UnqualifiedSourceType, |
| 3326 | QualType UnqualifiedTargetType, |
| 3327 | InitializationSequence &Sequence) { |
| 3328 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3329 | S.Context.OverloadTy) { |
| 3330 | DeclAccessPair Found; |
| 3331 | bool HadMultipleCandidates = false; |
| 3332 | if (FunctionDecl *Fn |
| 3333 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3334 | UnqualifiedTargetType, |
| 3335 | false, Found, |
| 3336 | &HadMultipleCandidates)) { |
| 3337 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3338 | HadMultipleCandidates); |
| 3339 | SourceType = Fn->getType(); |
| 3340 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3341 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3342 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3343 | return true; |
| 3344 | } |
| 3345 | } |
| 3346 | return false; |
| 3347 | } |
| 3348 | |
| 3349 | static void TryReferenceInitializationCore(Sema &S, |
| 3350 | const InitializedEntity &Entity, |
| 3351 | const InitializationKind &Kind, |
| 3352 | Expr *Initializer, |
| 3353 | QualType cv1T1, QualType T1, |
| 3354 | Qualifiers T1Quals, |
| 3355 | QualType cv2T2, QualType T2, |
| 3356 | Qualifiers T2Quals, |
| 3357 | InitializationSequence &Sequence); |
| 3358 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3359 | static void TryValueInitialization(Sema &S, |
| 3360 | const InitializedEntity &Entity, |
| 3361 | const InitializationKind &Kind, |
| 3362 | InitializationSequence &Sequence, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3363 | InitListExpr *InitList = nullptr); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3364 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3365 | /// \brief Attempt list initialization of a reference. |
| 3366 | static void TryReferenceListInitialization(Sema &S, |
| 3367 | const InitializedEntity &Entity, |
| 3368 | const InitializationKind &Kind, |
| 3369 | InitListExpr *InitList, |
Richard Smith | faadef7 | 2013-06-08 00:02:08 +0000 | [diff] [blame] | 3370 | InitializationSequence &Sequence) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3371 | // First, catch C++03 where this isn't possible. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3372 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3373 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3374 | return; |
| 3375 | } |
| 3376 | |
| 3377 | QualType DestType = Entity.getType(); |
| 3378 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3379 | Qualifiers T1Quals; |
| 3380 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3381 | |
| 3382 | // Reference initialization via an initializer list works thus: |
| 3383 | // If the initializer list consists of a single element that is |
| 3384 | // reference-related to the referenced type, bind directly to that element |
| 3385 | // (possibly creating temporaries). |
| 3386 | // Otherwise, initialize a temporary with the initializer list and |
| 3387 | // bind to that. |
| 3388 | if (InitList->getNumInits() == 1) { |
| 3389 | Expr *Initializer = InitList->getInit(0); |
| 3390 | QualType cv2T2 = Initializer->getType(); |
| 3391 | Qualifiers T2Quals; |
| 3392 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3393 | |
| 3394 | // If this fails, creating a temporary wouldn't work either. |
| 3395 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3396 | T1, Sequence)) |
| 3397 | return; |
| 3398 | |
| 3399 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3400 | bool dummy1, dummy2, dummy3; |
| 3401 | Sema::ReferenceCompareResult RefRelationship |
| 3402 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3403 | dummy2, dummy3); |
| 3404 | if (RefRelationship >= Sema::Ref_Related) { |
| 3405 | // Try to bind the reference here. |
| 3406 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3407 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3408 | if (Sequence) |
| 3409 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3410 | return; |
| 3411 | } |
Richard Smith | 03d9393 | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3412 | |
| 3413 | // Update the initializer if we've resolved an overloaded function. |
| 3414 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3415 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | // Not reference-related. Create a temporary and bind to that. |
| 3419 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3420 | |
| 3421 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3422 | if (Sequence) { |
| 3423 | if (DestType->isRValueReferenceType() || |
| 3424 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3425 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3426 | else |
| 3427 | Sequence.SetFailed( |
| 3428 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3429 | } |
| 3430 | } |
| 3431 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3432 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3433 | static void TryListInitialization(Sema &S, |
| 3434 | const InitializedEntity &Entity, |
| 3435 | const InitializationKind &Kind, |
| 3436 | InitListExpr *InitList, |
| 3437 | InitializationSequence &Sequence) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3438 | QualType DestType = Entity.getType(); |
| 3439 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3440 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3441 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3442 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3443 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3444 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3445 | return; |
| 3446 | } |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3447 | if (DestType->isReferenceType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3448 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3449 | return; |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3450 | } |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3451 | if (DestType->isRecordType()) { |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3452 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3453 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3454 | return; |
| 3455 | } |
| 3456 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3457 | // C++11 [dcl.init.list]p3: |
| 3458 | // - If T is an aggregate, aggregate initialization is performed. |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3459 | if (!DestType->isAggregateType()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3460 | if (S.getLangOpts().CPlusPlus11) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3461 | // - Otherwise, if the initializer list has no elements and T is a |
| 3462 | // class type with a default constructor, the object is |
| 3463 | // value-initialized. |
| 3464 | if (InitList->getNumInits() == 0) { |
| 3465 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
Richard Smith | 2be35f5 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3466 | if (RD->hasDefaultConstructor()) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3467 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3468 | return; |
| 3469 | } |
| 3470 | } |
| 3471 | |
| 3472 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3473 | // an initializer_list object constructed [...] |
| 3474 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3475 | return; |
| 3476 | |
| 3477 | // - Otherwise, if T is a class type, constructors are considered. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3478 | Expr *InitListAsExpr = InitList; |
| 3479 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3480 | Sequence, /*InitListSyntax*/true); |
Sebastian Redl | 4f28b58 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3481 | } else |
| 3482 | Sequence.SetFailed( |
| 3483 | InitializationSequence::FK_InitListBadDestinationType); |
| 3484 | return; |
| 3485 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3486 | } |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 3487 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
| 3488 | InitList->getNumInits() == 1 && |
| 3489 | InitList->getInit(0)->getType()->isRecordType()) { |
| 3490 | // - Otherwise, if the initializer list has a single element of type E |
| 3491 | // [...references are handled above...], the object or reference is |
| 3492 | // initialized from that element; if a narrowing conversion is required |
| 3493 | // to convert the element to T, the program is ill-formed. |
| 3494 | // |
| 3495 | // Per core-24034, this is direct-initialization if we were performing |
| 3496 | // direct-list-initialization and copy-initialization otherwise. |
| 3497 | // We can't use InitListChecker for this, because it always performs |
| 3498 | // copy-initialization. This only matters if we might use an 'explicit' |
| 3499 | // conversion operator, so we only need to handle the cases where the source |
| 3500 | // is of record type. |
| 3501 | InitializationKind SubKind = |
| 3502 | Kind.getKind() == InitializationKind::IK_DirectList |
| 3503 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 3504 | InitList->getLBraceLoc(), |
| 3505 | InitList->getRBraceLoc()) |
| 3506 | : Kind; |
| 3507 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 3508 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 3509 | /*TopLevelOfInitList*/true); |
| 3510 | if (Sequence) |
| 3511 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 3512 | return; |
| 3513 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3514 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3515 | InitListChecker CheckInitList(S, Entity, InitList, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 3516 | DestType, /*VerifyOnly=*/true); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3517 | if (CheckInitList.HadError()) { |
| 3518 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3519 | return; |
| 3520 | } |
| 3521 | |
| 3522 | // Add the list initialization step with the built init list. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3523 | Sequence.AddListInitializationStep(DestType); |
| 3524 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3525 | |
| 3526 | /// \brief Try a reference initialization that involves calling a conversion |
| 3527 | /// function. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3528 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3529 | const InitializedEntity &Entity, |
| 3530 | const InitializationKind &Kind, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3531 | Expr *Initializer, |
| 3532 | bool AllowRValues, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3533 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3534 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3535 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3536 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3537 | QualType cv2T2 = Initializer->getType(); |
| 3538 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3539 | |
| 3540 | bool DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3541 | bool ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3542 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3543 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3544 | T1, T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3545 | ObjCConversion, |
| 3546 | ObjCLifetimeConversion) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3547 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 8abbc65 | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3548 | (void)DerivedToBase; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3549 | (void)ObjCConversion; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3550 | (void)ObjCLifetimeConversion; |
| 3551 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3552 | // Build the candidate set directly in the initialization sequence |
| 3553 | // structure, so that it will persist if we fail. |
| 3554 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3555 | CandidateSet.clear(); |
| 3556 | |
| 3557 | // Determine whether we are allowed to call explicit constructors or |
| 3558 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3559 | bool AllowExplicit = Kind.AllowExplicit(); |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3560 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 3561 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3562 | const RecordType *T1RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3563 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3564 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3565 | // The type we're converting to is a class type. Enumerate its constructors |
| 3566 | // to see if there is a suitable conversion. |
| 3567 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3568 | |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3569 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3570 | // The container holding the constructors can under certain conditions |
| 3571 | // be changed while iterating (e.g. because of deserialization). |
| 3572 | // To be safe we copy the lookup results to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3573 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3574 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3575 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3576 | NamedDecl *D = *CI; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3577 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3578 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3579 | // Find the constructor (which may be a template). |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3580 | CXXConstructorDecl *Constructor = nullptr; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3581 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3582 | if (ConstructorTmpl) |
| 3583 | Constructor = cast<CXXConstructorDecl>( |
| 3584 | ConstructorTmpl->getTemplatedDecl()); |
| 3585 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3586 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3587 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3588 | if (!Constructor->isInvalidDecl() && |
| 3589 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3590 | if (ConstructorTmpl) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3591 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3592 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3593 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3594 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3595 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3596 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3597 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | dfbdfbb | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3598 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3599 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3600 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3601 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3602 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3603 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3604 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3605 | const RecordType *T2RecordType = nullptr; |
Douglas Gregor | 496e8b34 | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3606 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3607 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3608 | // The type we're converting from is a class type, enumerate its conversion |
| 3609 | // functions. |
| 3610 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3611 | |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3612 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3613 | CXXRecordDecl::conversion_iterator> |
| 3614 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3615 | for (CXXRecordDecl::conversion_iterator |
| 3616 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3617 | NamedDecl *D = *I; |
| 3618 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3619 | if (isa<UsingShadowDecl>(D)) |
| 3620 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3621 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3622 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3623 | CXXConversionDecl *Conv; |
| 3624 | if (ConvTemplate) |
| 3625 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3626 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3627 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3628 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3629 | // If the conversion function doesn't return a reference type, |
| 3630 | // it can't be considered for this conversion unless we're allowed to |
| 3631 | // consider rvalues. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3632 | // FIXME: Do we need to make sure that we only consider conversion |
| 3633 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3634 | // break recursion. |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3635 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3636 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3637 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3638 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3639 | ActingDC, Initializer, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 3640 | DestType, CandidateSet, |
| 3641 | /*AllowObjCConversionOnExplicit=*/ |
| 3642 | false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3643 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3644 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 3645 | Initializer, DestType, CandidateSet, |
| 3646 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3647 | } |
| 3648 | } |
| 3649 | } |
John McCall | 3696dcb | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3650 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3651 | return OR_No_Viable_Function; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3652 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3653 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3654 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3655 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3656 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3657 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3658 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3659 | return Result; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3660 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3661 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3662 | // This is the overload that will be used for this initialization step if we |
| 3663 | // use this initialization. Mark it as referenced. |
| 3664 | Function->setReferenced(); |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3665 | |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3666 | // Compute the returned type of the conversion. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3667 | if (isa<CXXConversionDecl>(Function)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3668 | T2 = Function->getReturnType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3669 | else |
| 3670 | T2 = cv1T1; |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3671 | |
| 3672 | // Add the user-defined conversion step. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3673 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3674 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3675 | T2.getNonLValueExprType(S.Context), |
| 3676 | HadMultipleCandidates); |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3677 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3678 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | ad6c2e5 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3679 | // cv-qualification adjustments. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3680 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3681 | if (T2->isLValueReferenceType()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3682 | VK = VK_LValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3683 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3684 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3685 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3686 | bool NewDerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3687 | bool NewObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3688 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3689 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3690 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3691 | T2.getNonLValueExprType(S.Context), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3692 | NewDerivedToBase, NewObjCConversion, |
| 3693 | NewObjCLifetimeConversion); |
Douglas Gregor | 1ce52ca | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3694 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3695 | // If the type we've converted to is not reference-related to the |
| 3696 | // type we're looking for, then there is another conversion step |
| 3697 | // we need to perform to produce a temporary of the right type |
| 3698 | // that we'll be binding to. |
| 3699 | ImplicitConversionSequence ICS; |
| 3700 | ICS.setStandard(); |
| 3701 | ICS.Standard = Best->FinalConversion; |
| 3702 | T2 = ICS.Standard.getToType(2); |
| 3703 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3704 | } else if (NewDerivedToBase) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3705 | Sequence.AddDerivedToBaseCastStep( |
| 3706 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3707 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3708 | VK); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3709 | else if (NewObjCConversion) |
| 3710 | Sequence.AddObjCObjectConversionStep( |
| 3711 | S.Context.getQualifiedType(T1, |
| 3712 | T2.getNonReferenceType().getQualifiers())); |
| 3713 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3714 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3715 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3716 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3717 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3718 | return OR_Success; |
| 3719 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3720 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3721 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3722 | const InitializedEntity &Entity, |
| 3723 | Expr *CurInitExpr); |
| 3724 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3725 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3726 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3727 | const InitializedEntity &Entity, |
| 3728 | const InitializationKind &Kind, |
| 3729 | Expr *Initializer, |
| 3730 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3731 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3732 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3733 | Qualifiers T1Quals; |
| 3734 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3735 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3736 | Qualifiers T2Quals; |
| 3737 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3738 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3739 | // If the initializer is the address of an overloaded function, try |
| 3740 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3741 | // type of the resulting function. |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3742 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3743 | T1, Sequence)) |
| 3744 | return; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3745 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3746 | // Delegate everything else to a subfunction. |
| 3747 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3748 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3749 | } |
| 3750 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3751 | /// Converts the target of reference initialization so that it has the |
| 3752 | /// appropriate qualifiers and value kind. |
| 3753 | /// |
| 3754 | /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'. |
| 3755 | /// \code |
| 3756 | /// int x; |
| 3757 | /// const int &r = x; |
| 3758 | /// \endcode |
| 3759 | /// |
| 3760 | /// In this case the reference is binding to a bitfield lvalue, which isn't |
| 3761 | /// valid. Perform a load to create a lifetime-extended temporary instead. |
| 3762 | /// \code |
| 3763 | /// const int &r = someStruct.bitfield; |
| 3764 | /// \endcode |
| 3765 | static ExprValueKind |
| 3766 | convertQualifiersAndValueKindIfNecessary(Sema &S, |
| 3767 | InitializationSequence &Sequence, |
| 3768 | Expr *Initializer, |
| 3769 | QualType cv1T1, |
| 3770 | Qualifiers T1Quals, |
| 3771 | Qualifiers T2Quals, |
| 3772 | bool IsLValueRef) { |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3773 | bool IsNonAddressableType = Initializer->refersToBitField() || |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3774 | Initializer->refersToVectorElement(); |
| 3775 | |
| 3776 | if (IsNonAddressableType) { |
| 3777 | // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an |
| 3778 | // lvalue reference to a non-volatile const type, or the reference shall be |
| 3779 | // an rvalue reference. |
| 3780 | // |
| 3781 | // If not, we can't make a temporary and bind to that. Give up and allow the |
| 3782 | // error to be diagnosed later. |
| 3783 | if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) { |
| 3784 | assert(Initializer->isGLValue()); |
| 3785 | return Initializer->getValueKind(); |
| 3786 | } |
| 3787 | |
| 3788 | // Force a load so we can materialize a temporary. |
| 3789 | Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType()); |
| 3790 | return VK_RValue; |
| 3791 | } |
| 3792 | |
| 3793 | if (T1Quals != T2Quals) { |
| 3794 | Sequence.AddQualificationConversionStep(cv1T1, |
| 3795 | Initializer->getValueKind()); |
| 3796 | } |
| 3797 | |
| 3798 | return Initializer->getValueKind(); |
| 3799 | } |
| 3800 | |
| 3801 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3802 | /// \brief Reference initialization without resolving overloaded functions. |
| 3803 | static void TryReferenceInitializationCore(Sema &S, |
| 3804 | const InitializedEntity &Entity, |
| 3805 | const InitializationKind &Kind, |
| 3806 | Expr *Initializer, |
| 3807 | QualType cv1T1, QualType T1, |
| 3808 | Qualifiers T1Quals, |
| 3809 | QualType cv2T2, QualType T2, |
| 3810 | Qualifiers T2Quals, |
| 3811 | InitializationSequence &Sequence) { |
| 3812 | QualType DestType = Entity.getType(); |
| 3813 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3814 | // Compute some basic properties of the types and the initializer. |
| 3815 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3816 | bool isRValueRef = !isLValueRef; |
| 3817 | bool DerivedToBase = false; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3818 | bool ObjCConversion = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3819 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3820 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3821 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3822 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3823 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3824 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3825 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3826 | // 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] | 3827 | // "cv2 T2" as follows: |
| 3828 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3829 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3830 | // expression |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3831 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3832 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3833 | // like lvalue refs. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3834 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3835 | bool T1Function = T1->isFunctionType(); |
| 3836 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3837 | if (InitCategory.isLValue() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3838 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3839 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3840 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3841 | // - 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] | 3842 | // reference-compatible with "cv2 T2," or |
| 3843 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3844 | // 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] | 3845 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3846 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3847 | // to decide whether we're actually binding to a temporary created from |
| 3848 | // the bit-field. |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3849 | if (DerivedToBase) |
| 3850 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3851 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3852 | VK_LValue); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3853 | else if (ObjCConversion) |
| 3854 | Sequence.AddObjCObjectConversionStep( |
| 3855 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3856 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3857 | ExprValueKind ValueKind = |
| 3858 | convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer, |
| 3859 | cv1T1, T1Quals, T2Quals, |
| 3860 | isLValueRef); |
| 3861 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3862 | return; |
| 3863 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3864 | |
| 3865 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3866 | // reference-related to T2, and can be implicitly converted to an |
| 3867 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3868 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3869 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3870 | // one through overload resolution (13.3)), |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3871 | // 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] | 3872 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3873 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3874 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3875 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3876 | S, Entity, Kind, Initializer, /*AllowRValues*/isRValueRef, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3877 | if (ConvOvlResult == OR_Success) |
| 3878 | return; |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3879 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3880 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3881 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3882 | ConvOvlResult); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3883 | } |
| 3884 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3885 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3886 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3887 | // 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] | 3888 | // shall be an rvalue reference. |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3889 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3890 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3891 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3892 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3893 | Sequence.SetOverloadFailure( |
| 3894 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3895 | ConvOvlResult); |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3896 | else |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3897 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3898 | ? (RefRelationship == Sema::Ref_Related |
| 3899 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3900 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3901 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3902 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3903 | return; |
| 3904 | } |
Sebastian Redl | d92badf | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3905 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3906 | // - If the initializer expression |
| 3907 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3908 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3909 | // Note: functions are handled below. |
| 3910 | if (!T1Function && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3911 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3912 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3913 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3914 | (InitCategory.isXValue() || |
| 3915 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3916 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3917 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3918 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3919 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3920 | // compiler the freedom to perform a copy here or bind to the |
| 3921 | // object, while C++0x requires that we bind directly to the |
| 3922 | // object. Hence, we always bind to the object without making an |
| 3923 | // extra copy. However, in C++03 requires that we check for the |
| 3924 | // presence of a suitable copy constructor: |
| 3925 | // |
| 3926 | // The constructor that would be used to make the copy shall |
| 3927 | // be callable whether or not the copy is actually done. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3928 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3929 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3930 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3931 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3932 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3933 | |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3934 | if (DerivedToBase) |
| 3935 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3936 | ValueKind); |
| 3937 | else if (ObjCConversion) |
| 3938 | Sequence.AddObjCObjectConversionStep( |
| 3939 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3940 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3941 | ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence, |
| 3942 | Initializer, cv1T1, |
| 3943 | T1Quals, T2Quals, |
| 3944 | isLValueRef); |
| 3945 | |
| 3946 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3947 | return; |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3948 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3949 | |
| 3950 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3951 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3952 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3953 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3954 | // |
| 3955 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | 92e460e | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3956 | if (T2->isRecordType()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3957 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3958 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3959 | S, Entity, Kind, Initializer, /*AllowRValues*/true, Sequence); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3960 | if (ConvOvlResult) |
| 3961 | Sequence.SetOverloadFailure( |
Richard Smith | 6c6ddab | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3962 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3963 | ConvOvlResult); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3964 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3965 | return; |
| 3966 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3967 | |
Douglas Gregor | 6fa6ab0 | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 3968 | if ((RefRelationship == Sema::Ref_Compatible || |
| 3969 | RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) && |
| 3970 | isRValueRef && InitCategory.isLValue()) { |
| 3971 | Sequence.SetFailed( |
| 3972 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3973 | return; |
| 3974 | } |
| 3975 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3976 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3977 | return; |
| 3978 | } |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3979 | |
| 3980 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3981 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3982 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3983 | // temporary. [...] |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3984 | |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3985 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3986 | |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3987 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 3988 | // copy-initialization? |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3989 | ImplicitConversionSequence ICS |
| 3990 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 2eabf78 | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3991 | /*SuppressUserConversions=*/false, |
| 3992 | /*AllowExplicit=*/false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3993 | /*FIXME:InOverloadResolution=*/false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3994 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3995 | /*AllowObjCWritebackConversion=*/false); |
| 3996 | |
| 3997 | if (ICS.isBad()) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3998 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3999 | // this into an overloading ambiguity diagnostic. However, we need |
| 4000 | // to keep that set as an OverloadCandidateSet rather than as some |
| 4001 | // other kind of set. |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4002 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 4003 | Sequence.SetOverloadFailure( |
| 4004 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 4005 | ConvOvlResult); |
Douglas Gregor | bcd6253 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 4006 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 4007 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4008 | else |
| 4009 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4010 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4011 | } else { |
| 4012 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 4016 | // same cv-qualification as, or greater cv-qualification |
| 4017 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4018 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 4019 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4020 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 04bdce6 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 4021 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4022 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 4023 | return; |
| 4024 | } |
| 4025 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4026 | // [...] 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] | 4027 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4028 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | 24f2e8e | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 4029 | InitCategory.isLValue()) { |
| 4030 | Sequence.SetFailed( |
| 4031 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 4032 | return; |
| 4033 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4034 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4035 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 4036 | return; |
| 4037 | } |
| 4038 | |
| 4039 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4040 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 4041 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4042 | const InitializedEntity &Entity, |
| 4043 | const InitializationKind &Kind, |
| 4044 | Expr *Initializer, |
| 4045 | InitializationSequence &Sequence) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4046 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4049 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4050 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4051 | const InitializedEntity &Entity, |
| 4052 | const InitializationKind &Kind, |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4053 | InitializationSequence &Sequence, |
| 4054 | InitListExpr *InitList) { |
| 4055 | assert((!InitList || InitList->getNumInits() == 0) && |
| 4056 | "Shouldn't use value-init for non-empty init lists"); |
| 4057 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4058 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4059 | // |
| 4060 | // To value-initialize an object of type T means: |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4061 | QualType T = Entity.getType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4062 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4063 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4064 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4065 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4066 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 4067 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4068 | bool NeedZeroInitialization = true; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4069 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4070 | // C++98: |
| 4071 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 4072 | // (12.1), then the default constructor for T is called (and the |
| 4073 | // initialization is ill-formed if T has no accessible default |
| 4074 | // constructor); |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4075 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4076 | NeedZeroInitialization = false; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4077 | } else { |
| 4078 | // C++11: |
| 4079 | // -- if T is a class type (clause 9) with either no default constructor |
| 4080 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 4081 | // or deleted, then the object is default-initialized; |
| 4082 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 4083 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4084 | NeedZeroInitialization = false; |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4085 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4086 | |
Richard Smith | 1bfe068 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 4087 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 4088 | // user-provided or deleted default constructor, then the object is |
| 4089 | // zero-initialized and, if T has a non-trivial default constructor, |
| 4090 | // default-initialized; |
Richard Smith | fb26652 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 4091 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 4092 | // constructor' part was removed by DR1507. |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4093 | if (NeedZeroInitialization) |
| 4094 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4095 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4096 | // C++03: |
| 4097 | // -- if T is a non-union class type without a user-declared constructor, |
| 4098 | // then every non-static data member and base class component of T is |
| 4099 | // value-initialized; |
| 4100 | // [...] A program that calls for [...] value-initialization of an |
| 4101 | // entity of reference type is ill-formed. |
| 4102 | // |
| 4103 | // C++11 doesn't need this handling, because value-initialization does not |
| 4104 | // occur recursively there, and the implicit default constructor is |
| 4105 | // defined as deleted in the problematic cases. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4106 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 4107 | ClassDecl->hasUninitializedReferenceMember()) { |
| 4108 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 4109 | return; |
| 4110 | } |
| 4111 | |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4112 | // If this is list-value-initialization, pass the empty init list on when |
| 4113 | // building the constructor call. This affects the semantics of a few |
| 4114 | // things (such as whether an explicit default constructor can be called). |
| 4115 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4116 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4117 | bool InitListSyntax = InitList; |
| 4118 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4119 | return TryConstructorInitialization(S, Entity, Kind, Args, T, Sequence, |
| 4120 | InitListSyntax); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4121 | } |
| 4122 | } |
| 4123 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4124 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4127 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 4128 | static void TryDefaultInitialization(Sema &S, |
| 4129 | const InitializedEntity &Entity, |
| 4130 | const InitializationKind &Kind, |
| 4131 | InitializationSequence &Sequence) { |
| 4132 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4133 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4134 | // C++ [dcl.init]p6: |
| 4135 | // To default-initialize an object of type T means: |
| 4136 | // - if T is an array type, each element is default-initialized; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4137 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 4138 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4139 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 4140 | // constructor for T is called (and the initialization is ill-formed if |
| 4141 | // T has no accessible default constructor); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4142 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Dmitri Gribenko | 78852e9 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 4143 | TryConstructorInitialization(S, Entity, Kind, None, DestType, Sequence); |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4144 | return; |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4145 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4146 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4147 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4148 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4149 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4150 | // 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] | 4151 | // default constructor. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4152 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4153 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4154 | return; |
| 4155 | } |
| 4156 | |
| 4157 | // If the destination type has a lifetime property, zero-initialize it. |
| 4158 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4159 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4160 | return; |
| 4161 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4164 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 4165 | /// which enumerates all conversion functions and performs overload resolution |
| 4166 | /// to select the best. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4167 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4168 | const InitializedEntity &Entity, |
| 4169 | const InitializationKind &Kind, |
| 4170 | Expr *Initializer, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4171 | InitializationSequence &Sequence, |
| 4172 | bool TopLevelOfInitList) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4173 | QualType DestType = Entity.getType(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4174 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4175 | QualType SourceType = Initializer->getType(); |
| 4176 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4177 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4178 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4179 | // Build the candidate set directly in the initialization sequence |
| 4180 | // structure, so that it will persist if we fail. |
| 4181 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 4182 | CandidateSet.clear(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4183 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4184 | // Determine whether we are allowed to call explicit constructors or |
| 4185 | // explicit conversion operators. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4186 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4187 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4188 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4189 | // The type we're converting to is a class type. Enumerate its constructors |
| 4190 | // to see if there is a suitable conversion. |
| 4191 | CXXRecordDecl *DestRecordDecl |
| 4192 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4193 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4194 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4195 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4196 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 12be639 | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4197 | // The container holding the constructors can under certain conditions |
| 4198 | // be changed while iterating. To be safe we copy the lookup results |
| 4199 | // to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4200 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4201 | for (SmallVectorImpl<NamedDecl *>::iterator |
David Blaikie | 12be639 | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4202 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4203 | Con != ConEnd; ++Con) { |
| 4204 | NamedDecl *D = *Con; |
| 4205 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4206 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4207 | // Find the constructor (which may be a template). |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4208 | CXXConstructorDecl *Constructor = nullptr; |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4209 | FunctionTemplateDecl *ConstructorTmpl |
| 4210 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4211 | if (ConstructorTmpl) |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4212 | Constructor = cast<CXXConstructorDecl>( |
| 4213 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4214 | else |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4215 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4216 | |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4217 | if (!Constructor->isInvalidDecl() && |
| 4218 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4219 | if (ConstructorTmpl) |
| 4220 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4221 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4222 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4223 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4224 | else |
| 4225 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4226 | Initializer, CandidateSet, |
Douglas Gregor | 7c42659 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4227 | /*SuppressUserConversions=*/true); |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4228 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4229 | } |
Douglas Gregor | d984815 | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4230 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4231 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4232 | |
| 4233 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 4234 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4235 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4236 | // The type we're converting from is a class type, enumerate its conversion |
| 4237 | // functions. |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4238 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4239 | // We can only enumerate the conversion functions for a complete type; if |
| 4240 | // the type isn't complete, simply skip this step. |
| 4241 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 4242 | CXXRecordDecl *SourceRecordDecl |
| 4243 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4244 | |
Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4245 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4246 | CXXRecordDecl::conversion_iterator> |
| 4247 | Conversions = SourceRecordDecl->getVisibleConversionFunctions(); |
| 4248 | for (CXXRecordDecl::conversion_iterator |
| 4249 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4250 | NamedDecl *D = *I; |
| 4251 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4252 | if (isa<UsingShadowDecl>(D)) |
| 4253 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4254 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4255 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4256 | CXXConversionDecl *Conv; |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4257 | if (ConvTemplate) |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4258 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4259 | else |
John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4260 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4261 | |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4262 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4263 | if (ConvTemplate) |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4264 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | b89836b | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4265 | ActingDC, Initializer, DestType, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4266 | CandidateSet, AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4267 | else |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4268 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 6878214 | 2013-12-18 21:46:16 +0000 | [diff] [blame] | 4269 | Initializer, DestType, CandidateSet, |
| 4270 | AllowExplicit); |
Eli Friedman | 4afe9a3 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4271 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4272 | } |
| 4273 | } |
| 4274 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4275 | |
| 4276 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4277 | OverloadCandidateSet::iterator Best; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4278 | if (OverloadingResult Result |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4279 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4280 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4282 | Result); |
| 4283 | return; |
| 4284 | } |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4285 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4286 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4287 | Function->setReferenced(); |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4288 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4289 | |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4290 | if (isa<CXXConstructorDecl>(Function)) { |
| 4291 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4292 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 4293 | // cv-unqualified type of the destination. |
| 4294 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 4295 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4296 | HadMultipleCandidates); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4297 | return; |
| 4298 | } |
| 4299 | |
| 4300 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4301 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4302 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4303 | // 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] | 4304 | // the resulting temporary object (possible to create an object of |
| 4305 | // a base class type). That copy is not a separate conversion, so |
| 4306 | // we just make a note of the actual destination type (possibly a |
| 4307 | // base class of the type returned by the conversion function) and |
| 4308 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4309 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 4310 | HadMultipleCandidates); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4311 | return; |
| 4312 | } |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4313 | |
Abramo Bagnara | 5001caa | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4314 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 4315 | HadMultipleCandidates); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4317 | // If the conversion following the call to the conversion function |
| 4318 | // is interesting, add it as a separate step. |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4319 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 4320 | Best->FinalConversion.Third) { |
| 4321 | ImplicitConversionSequence ICS; |
John McCall | 0d1da22 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4322 | ICS.setStandard(); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4323 | ICS.Standard = Best->FinalConversion; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4324 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4325 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4326 | } |
| 4327 | |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4328 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 4329 | /// a function with a pointer return type contains a 'return false;' statement. |
| 4330 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 4331 | /// code using that header. |
| 4332 | /// |
| 4333 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 4334 | /// if it's used in a pointer-returning function in a system header. |
| 4335 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 4336 | const InitializedEntity &Entity, |
| 4337 | const Expr *Init) { |
| 4338 | return S.getLangOpts().CPlusPlus11 && |
| 4339 | Entity.getKind() == InitializedEntity::EK_Result && |
| 4340 | Entity.getType()->isPointerType() && |
| 4341 | isa<CXXBoolLiteralExpr>(Init) && |
| 4342 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 4343 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 4344 | } |
| 4345 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4346 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 4347 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 4348 | |
| 4349 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4350 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4351 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4352 | // Skip parens. |
| 4353 | e = e->IgnoreParens(); |
| 4354 | |
| 4355 | // Skip address-of nodes. |
| 4356 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 4357 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4358 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 4359 | isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4360 | |
| 4361 | // Skip certain casts. |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4362 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 4363 | switch (ce->getCastKind()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4364 | case CK_Dependent: |
| 4365 | case CK_BitCast: |
| 4366 | case CK_LValueBitCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4367 | case CK_NoOp: |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4368 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4369 | |
| 4370 | case CK_ArrayToPointerDecay: |
| 4371 | return IIK_nonscalar; |
| 4372 | |
| 4373 | case CK_NullToPointer: |
| 4374 | return IIK_okay; |
| 4375 | |
| 4376 | default: |
| 4377 | break; |
| 4378 | } |
| 4379 | |
| 4380 | // 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] | 4381 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4382 | // set isWeakAccess to true, to mean that there will be an implicit |
| 4383 | // load which requires a cleanup. |
| 4384 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4385 | isWeakAccess = true; |
| 4386 | |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4387 | if (!isAddressOf) return IIK_nonlocal; |
| 4388 | |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4389 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 4390 | if (!var) return IIK_nonlocal; |
John McCall | 63f8444 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4391 | |
| 4392 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4393 | |
| 4394 | // If we have a conditional operator, check both sides. |
| 4395 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4396 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 4397 | isWeakAccess)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4398 | return iik; |
| 4399 | |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4400 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4401 | |
| 4402 | // These are never scalar. |
| 4403 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 4404 | return IIK_nonscalar; |
| 4405 | |
| 4406 | // Otherwise, it needs to be a null pointer constant. |
| 4407 | } else { |
| 4408 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 4409 | ? IIK_okay : IIK_nonlocal); |
| 4410 | } |
| 4411 | |
| 4412 | return IIK_nonlocal; |
| 4413 | } |
| 4414 | |
| 4415 | /// Check whether the given expression is a valid operand for an |
| 4416 | /// indirect copy/restore. |
| 4417 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 4418 | assert(src->isRValue()); |
Fariborz Jahanian | fbd1974 | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4419 | bool isWeakAccess = false; |
| 4420 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 4421 | // If isWeakAccess to true, there will be an implicit |
| 4422 | // load which requires a cleanup. |
| 4423 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 4424 | S.ExprNeedsCleanups = true; |
| 4425 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4426 | if (iik == IIK_okay) return; |
| 4427 | |
| 4428 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 4429 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 4430 | << src->getSourceRange(); |
| 4431 | } |
| 4432 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4433 | /// \brief Determine whether we have compatible array types for the |
| 4434 | /// purposes of GNU by-copy array initialization. |
| 4435 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 4436 | const ArrayType *Dest, |
| 4437 | const ArrayType *Source) { |
| 4438 | // If the source and destination array types are equivalent, we're |
| 4439 | // done. |
| 4440 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 4441 | return true; |
| 4442 | |
| 4443 | // Make sure that the element types are the same. |
| 4444 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 4445 | return false; |
| 4446 | |
| 4447 | // The only mismatch we allow is when the destination is an |
| 4448 | // incomplete array type and the source is a constant array type. |
| 4449 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 4450 | } |
| 4451 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4452 | static bool tryObjCWritebackConversion(Sema &S, |
| 4453 | InitializationSequence &Sequence, |
| 4454 | const InitializedEntity &Entity, |
| 4455 | Expr *Initializer) { |
| 4456 | bool ArrayDecay = false; |
| 4457 | QualType ArgType = Initializer->getType(); |
| 4458 | QualType ArgPointee; |
| 4459 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 4460 | ArrayDecay = true; |
| 4461 | ArgPointee = ArgArrayType->getElementType(); |
| 4462 | ArgType = S.Context.getPointerType(ArgPointee); |
| 4463 | } |
| 4464 | |
| 4465 | // Handle write-back conversion. |
| 4466 | QualType ConvertedArgType; |
| 4467 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 4468 | ConvertedArgType)) |
| 4469 | return false; |
| 4470 | |
| 4471 | // We should copy unless we're passing to an argument explicitly |
| 4472 | // marked 'out'. |
| 4473 | bool ShouldCopy = true; |
| 4474 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4475 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4476 | |
| 4477 | // Do we need an lvalue conversion? |
| 4478 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4479 | ImplicitConversionSequence ICS; |
| 4480 | ICS.setStandard(); |
| 4481 | ICS.Standard.setAsIdentityConversion(); |
| 4482 | |
| 4483 | QualType ResultType; |
| 4484 | if (ArrayDecay) { |
| 4485 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4486 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4487 | } else { |
| 4488 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4489 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4490 | } |
| 4491 | |
| 4492 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4493 | } |
| 4494 | |
| 4495 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4496 | return true; |
| 4497 | } |
| 4498 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4499 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4500 | InitializationSequence &Sequence, |
| 4501 | QualType DestType, |
| 4502 | Expr *Initializer) { |
| 4503 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4504 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4505 | return false; |
| 4506 | |
| 4507 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4508 | return true; |
| 4509 | } |
| 4510 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4511 | // |
| 4512 | // OpenCL 1.2 spec, s6.12.10 |
| 4513 | // |
| 4514 | // The event argument can also be used to associate the |
| 4515 | // async_work_group_copy with a previous async copy allowing |
| 4516 | // an event to be shared by multiple async copies; otherwise |
| 4517 | // event should be zero. |
| 4518 | // |
| 4519 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4520 | InitializationSequence &Sequence, |
| 4521 | QualType DestType, |
| 4522 | Expr *Initializer) { |
| 4523 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4524 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4525 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4526 | return false; |
| 4527 | |
| 4528 | Sequence.AddOCLZeroEventStep(DestType); |
| 4529 | return true; |
| 4530 | } |
| 4531 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4532 | InitializationSequence::InitializationSequence(Sema &S, |
| 4533 | const InitializedEntity &Entity, |
| 4534 | const InitializationKind &Kind, |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4535 | MultiExprArg Args, |
| 4536 | bool TopLevelOfInitList) |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 4537 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
Richard Smith | 089c316 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4538 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList); |
| 4539 | } |
| 4540 | |
| 4541 | void InitializationSequence::InitializeFrom(Sema &S, |
| 4542 | const InitializedEntity &Entity, |
| 4543 | const InitializationKind &Kind, |
| 4544 | MultiExprArg Args, |
| 4545 | bool TopLevelOfInitList) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4546 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4547 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4548 | // Eliminate non-overload placeholder types in the arguments. We |
| 4549 | // need to do this before checking whether types are dependent |
| 4550 | // because lowering a pseudo-object expression might well give us |
| 4551 | // something of dependent type. |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4552 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4553 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4554 | // FIXME: should we be doing this here? |
| 4555 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4556 | if (result.isInvalid()) { |
| 4557 | SetFailed(FK_PlaceholderType); |
| 4558 | return; |
| 4559 | } |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4560 | Args[I] = result.get(); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4563 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4564 | // The semantics of initializers are as follows. The destination type is |
| 4565 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4566 | // 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] | 4567 | // 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] | 4568 | // parenthesized list of expressions. |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4569 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4570 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4571 | if (DestType->isDependentType() || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4572 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4573 | SequenceKind = DependentSequence; |
| 4574 | return; |
| 4575 | } |
| 4576 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4577 | // Almost everything is a normal sequence. |
| 4578 | setSequenceKind(NormalSequence); |
| 4579 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4580 | QualType SourceType; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4581 | Expr *Initializer = nullptr; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4582 | if (Args.size() == 1) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4583 | Initializer = Args[0]; |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 4584 | if (S.getLangOpts().ObjC1) { |
| 4585 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getLocStart(), |
| 4586 | DestType, Initializer->getType(), |
| 4587 | Initializer) || |
| 4588 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 4589 | Args[0] = Initializer; |
| 4590 | |
| 4591 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4592 | if (!isa<InitListExpr>(Initializer)) |
| 4593 | SourceType = Initializer->getType(); |
| 4594 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4595 | |
Sebastian Redl | 0501c63 | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4596 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4597 | // object is list-initialized (8.5.4). |
| 4598 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4599 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4600 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4601 | return; |
| 4602 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4603 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4604 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4605 | // - If the destination type is a reference type, see 8.5.3. |
| 4606 | if (DestType->isReferenceType()) { |
| 4607 | // C++0x [dcl.init.ref]p1: |
| 4608 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4609 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4610 | // by an object that can be converted into a T. |
| 4611 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4612 | if (Args.size() != 1) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4613 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4614 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4615 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4616 | return; |
| 4617 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4618 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4619 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4620 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4621 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4622 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4623 | return; |
| 4624 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4625 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4626 | // Handle default initialization. |
Nick Lewycky | 9331ed8 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4627 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4628 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4629 | return; |
| 4630 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4631 | |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4632 | // - If the destination type is an array of characters, an array of |
| 4633 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4634 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4635 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4636 | // ill-formed. |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4637 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4638 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4639 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4640 | return; |
| 4641 | } |
| 4642 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4643 | if (Initializer) { |
| 4644 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 4645 | case SIF_None: |
| 4646 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 4647 | return; |
| 4648 | case SIF_NarrowStringIntoWideChar: |
| 4649 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 4650 | return; |
| 4651 | case SIF_WideStringIntoChar: |
| 4652 | SetFailed(FK_WideStringIntoCharArray); |
| 4653 | return; |
| 4654 | case SIF_IncompatWideStringIntoWideChar: |
| 4655 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 4656 | return; |
| 4657 | case SIF_Other: |
| 4658 | break; |
| 4659 | } |
John McCall | 66884dd | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4660 | } |
| 4661 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4662 | // Note: as an GNU C extension, we allow initialization of an |
| 4663 | // array from a compound literal that creates an array of the same |
| 4664 | // type, so long as the initializer has no side effects. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4665 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4666 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4667 | Initializer->getType()->isArrayType()) { |
| 4668 | const ArrayType *SourceAT |
| 4669 | = Context.getAsArrayType(Initializer->getType()); |
| 4670 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4671 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4672 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4673 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4674 | else { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4675 | AddArrayInitStep(DestType); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4676 | } |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4677 | } |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4678 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4679 | // class member of array type from a parenthesized initializer list. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4680 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4681 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4682 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4683 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4684 | *this); |
| 4685 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4686 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4687 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4688 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 4689 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4690 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4691 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4692 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4693 | return; |
| 4694 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4695 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4696 | // Determine whether we should consider writeback conversions for |
| 4697 | // Objective-C ARC. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4698 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4699 | Entity.isParameterKind(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4700 | |
| 4701 | // We're at the end of the line for C: it's either a write-back conversion |
| 4702 | // 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] | 4703 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4704 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4705 | if (allowObjCWritebackConversion && |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4706 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4707 | return; |
| 4708 | } |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4709 | |
| 4710 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4711 | return; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4712 | |
| 4713 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4714 | return; |
| 4715 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4716 | // Handle initialization in C |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4717 | AddCAssignmentStep(DestType); |
| 4718 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4719 | return; |
| 4720 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4721 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4722 | assert(S.getLangOpts().CPlusPlus); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4723 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4724 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4725 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4726 | // - If the initialization is direct-initialization, or if it is |
| 4727 | // copy-initialization where the cv-unqualified version of the |
| 4728 | // 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] | 4729 | // class of the destination, constructors are considered. [...] |
| 4730 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4731 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4732 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4733 | S.IsDerivedFrom(SourceType, DestType)))) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4734 | TryConstructorInitialization(S, Entity, Kind, Args, |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4735 | Entity.getType(), *this); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4736 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4737 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4738 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4739 | // used) to a derived class thereof are enumerated as described in |
| 4740 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4741 | // (13.3). |
| 4742 | else |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4743 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this, |
| 4744 | TopLevelOfInitList); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4745 | return; |
| 4746 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4747 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4748 | if (Args.size() > 1) { |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4749 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4750 | return; |
| 4751 | } |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4752 | assert(Args.size() == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4753 | |
| 4754 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4755 | // type, conversion functions are considered. |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4756 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4757 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this, |
| 4758 | TopLevelOfInitList); |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4759 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4760 | return; |
| 4761 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4762 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4763 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4764 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4765 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4766 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4767 | // destination type; no user-defined conversions are considered. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4768 | |
| 4769 | ImplicitConversionSequence ICS |
| 4770 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4771 | /*SuppressUserConversions*/true, |
John McCall | ec6f4e9 | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4772 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 5828135 | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4773 | /*InOverloadResolution*/ false, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4774 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4775 | allowObjCWritebackConversion); |
| 4776 | |
| 4777 | if (ICS.isStandard() && |
| 4778 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4779 | // Objective-C ARC writeback conversion. |
| 4780 | |
| 4781 | // We should copy unless we're passing to an argument explicitly |
| 4782 | // marked 'out'. |
| 4783 | bool ShouldCopy = true; |
| 4784 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4785 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4786 | |
| 4787 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4788 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4789 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4790 | ImplicitConversionSequence LvalueICS; |
| 4791 | LvalueICS.setStandard(); |
| 4792 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4793 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4794 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4795 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4796 | } |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4797 | |
| 4798 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4799 | } else if (ICS.isBad()) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4800 | DeclAccessPair dap; |
Richard Smith | f032001b | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4801 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 4802 | AddZeroInitializationStep(Entity.getType()); |
| 4803 | } else if (Initializer->getType() == Context.OverloadTy && |
| 4804 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 4805 | false, dap)) |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4806 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4807 | else |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4808 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4809 | } else { |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4810 | AddConversionSequenceStep(ICS, Entity.getType(), TopLevelOfInitList); |
John McCall | fa27234 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4811 | |
Rafael Espindola | 699fc4d | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4812 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | e81f58e | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4813 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4817 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4818 | StepEnd = Steps.end(); |
| 4819 | Step != StepEnd; ++Step) |
| 4820 | Step->Destroy(); |
| 4821 | } |
| 4822 | |
| 4823 | //===----------------------------------------------------------------------===// |
| 4824 | // Perform initialization |
| 4825 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4826 | static Sema::AssignmentAction |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4827 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4828 | switch(Entity.getKind()) { |
| 4829 | case InitializedEntity::EK_Variable: |
| 4830 | case InitializedEntity::EK_New: |
Douglas Gregor | 6dd3a6a | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4831 | case InitializedEntity::EK_Exception: |
| 4832 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4833 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4834 | return Sema::AA_Initializing; |
| 4835 | |
| 4836 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4837 | if (Entity.getDecl() && |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4838 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4839 | return Sema::AA_Sending; |
| 4840 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4841 | return Sema::AA_Passing; |
| 4842 | |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4843 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 4844 | if (Entity.getDecl() && |
| 4845 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4846 | return Sema::AA_Sending; |
| 4847 | |
| 4848 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
| 4849 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4850 | case InitializedEntity::EK_Result: |
| 4851 | return Sema::AA_Returning; |
| 4852 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4853 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | 14e9541 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 4854 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4855 | // FIXME: Can we tell apart casting vs. converting? |
| 4856 | return Sema::AA_Casting; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4857 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4858 | case InitializedEntity::EK_Member: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4859 | case InitializedEntity::EK_ArrayElement: |
| 4860 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4861 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4862 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4863 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4864 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4865 | return Sema::AA_Initializing; |
| 4866 | } |
| 4867 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4868 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4869 | } |
| 4870 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4871 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4872 | /// initializing the given entity. |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4873 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4874 | switch (Entity.getKind()) { |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4875 | case InitializedEntity::EK_ArrayElement: |
| 4876 | case InitializedEntity::EK_Member: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4877 | case InitializedEntity::EK_Result: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4878 | case InitializedEntity::EK_New: |
| 4879 | case InitializedEntity::EK_Variable: |
| 4880 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4881 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | ed8d80d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4882 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4883 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | fcd764a | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4884 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4885 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4886 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4887 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4888 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4889 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4890 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4891 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4892 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4893 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4894 | return true; |
| 4895 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4896 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4897 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4898 | } |
| 4899 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4900 | /// \brief Whether the given entity, when initialized with an object |
| 4901 | /// created for that initialization, requires destruction. |
| 4902 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4903 | switch (Entity.getKind()) { |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4904 | case InitializedEntity::EK_Result: |
| 4905 | case InitializedEntity::EK_New: |
| 4906 | case InitializedEntity::EK_Base: |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4907 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4908 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 6b9c41e | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4909 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4910 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4911 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4912 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4913 | |
Richard Smith | 27874d6 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4914 | case InitializedEntity::EK_Member: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4915 | case InitializedEntity::EK_Variable: |
| 4916 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4917 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4918 | case InitializedEntity::EK_Temporary: |
| 4919 | case InitializedEntity::EK_ArrayElement: |
| 4920 | case InitializedEntity::EK_Exception: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4921 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4922 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4923 | return true; |
| 4924 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4925 | |
| 4926 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4929 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4930 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4931 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4932 | OverloadCandidateSet &CandidateSet, |
| 4933 | CXXRecordDecl *Class, |
| 4934 | Expr *CurInitExpr) { |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4935 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4936 | // The container holding the constructors can under certain conditions |
| 4937 | // be changed while iterating (e.g. because of deserialization). |
| 4938 | // To be safe we copy the lookup results to a new container. |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4939 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 2341c0d | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4940 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4941 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4942 | NamedDecl *D = *CI; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4943 | CXXConstructorDecl *Constructor = nullptr; |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4944 | |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4945 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4946 | // Handle copy/moveconstructors, only. |
| 4947 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4948 | !Constructor->isCopyOrMoveConstructor() || |
| 4949 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4950 | continue; |
| 4951 | |
| 4952 | DeclAccessPair FoundDecl |
| 4953 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4954 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4955 | CurInitExpr, CandidateSet); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4956 | continue; |
| 4957 | } |
| 4958 | |
| 4959 | // Handle constructor templates. |
Argyrios Kyrtzidis | 243d8234 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4960 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4961 | if (ConstructorTmpl->isInvalidDecl()) |
| 4962 | continue; |
| 4963 | |
| 4964 | Constructor = cast<CXXConstructorDecl>( |
| 4965 | ConstructorTmpl->getTemplatedDecl()); |
| 4966 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4967 | continue; |
| 4968 | |
| 4969 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4970 | // candidates? |
| 4971 | DeclAccessPair FoundDecl |
| 4972 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4973 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, nullptr, |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4974 | CurInitExpr, CandidateSet, true); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4975 | } |
| 4976 | } |
| 4977 | |
| 4978 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4979 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4980 | Expr *Initializer) { |
| 4981 | switch (Entity.getKind()) { |
| 4982 | case InitializedEntity::EK_Result: |
| 4983 | return Entity.getReturnLoc(); |
| 4984 | |
| 4985 | case InitializedEntity::EK_Exception: |
| 4986 | return Entity.getThrowLoc(); |
| 4987 | |
| 4988 | case InitializedEntity::EK_Variable: |
| 4989 | return Entity.getDecl()->getLocation(); |
| 4990 | |
Douglas Gregor | 19666fb | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4991 | case InitializedEntity::EK_LambdaCapture: |
| 4992 | return Entity.getCaptureLoc(); |
| 4993 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4994 | case InitializedEntity::EK_ArrayElement: |
| 4995 | case InitializedEntity::EK_Member: |
| 4996 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4997 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4998 | case InitializedEntity::EK_Temporary: |
| 4999 | case InitializedEntity::EK_New: |
| 5000 | case InitializedEntity::EK_Base: |
| 5001 | case InitializedEntity::EK_Delegating: |
| 5002 | case InitializedEntity::EK_VectorElement: |
| 5003 | case InitializedEntity::EK_ComplexElement: |
| 5004 | case InitializedEntity::EK_BlockElement: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5005 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5006 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5007 | return Initializer->getLocStart(); |
| 5008 | } |
| 5009 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 5010 | } |
| 5011 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5012 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 5013 | /// provided by the given initializer by calling the appropriate copy |
| 5014 | /// constructor. |
| 5015 | /// |
| 5016 | /// \param S The Sema object used for type-checking. |
| 5017 | /// |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 5018 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5019 | /// the type of the initializer expression or a superclass thereof. |
| 5020 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5021 | /// \param Entity The entity being initialized. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5022 | /// |
| 5023 | /// \param CurInit The initializer expression. |
| 5024 | /// |
| 5025 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 5026 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 5027 | /// an rvalue. |
| 5028 | /// |
| 5029 | /// \returns An expression that copies the initializer expression into |
| 5030 | /// a temporary object, or an error expression if a copy could not be |
| 5031 | /// created. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5032 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5033 | QualType T, |
| 5034 | const InitializedEntity &Entity, |
| 5035 | ExprResult CurInit, |
| 5036 | bool IsExtraneousCopy) { |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5037 | // Determine which class type we're copying to. |
Anders Carlsson | 0bd5240 | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 5038 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5039 | CXXRecordDecl *Class = nullptr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5040 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5041 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 5042 | if (!Class) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5043 | return CurInit; |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5044 | |
Douglas Gregor | 5d36900 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 5045 | // C++0x [class.copy]p32: |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5046 | // When certain criteria are met, an implementation is allowed to |
| 5047 | // omit the copy/move construction of a class object, even if the |
| 5048 | // copy/move constructor and/or destructor for the object have |
| 5049 | // side effects. [...] |
| 5050 | // - when a temporary class object that has not been bound to a |
| 5051 | // reference (12.2) would be copied/moved to a class object |
| 5052 | // with the same cv-unqualified type, the copy/move operation |
| 5053 | // can be omitted by constructing the temporary object |
| 5054 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5055 | // |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5056 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 5057 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5058 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 222cf0e | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 5059 | // is handled by the run-time. |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 5060 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5061 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5062 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5063 | // Make sure that the type we are copying is complete. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5064 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5065 | return CurInit; |
Douglas Gregor | d5c231e | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 5066 | |
Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 5067 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5068 | // Only consider constructors and constructor templates. Per |
| 5069 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 5070 | // is direct-initialization. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5071 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5072 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5073 | |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5074 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 5075 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5076 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 3014163 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 5077 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5078 | case OR_Success: |
| 5079 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5080 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5081 | case OR_No_Viable_Function: |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5082 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 5083 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 5084 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5085 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5086 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5087 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5088 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5089 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5090 | return CurInit; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5091 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5092 | case OR_Ambiguous: |
| 5093 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5094 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5095 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5096 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5097 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5098 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5099 | case OR_Deleted: |
| 5100 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 5101 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5102 | << CurInitExpr->getSourceRange(); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5103 | S.NoteDeletedFunction(Best->Function); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5104 | return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5107 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5108 | SmallVector<Expr*, 8> ConstructorArgs; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5109 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5110 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5111 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | caa710d | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 5112 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5113 | |
| 5114 | if (IsExtraneousCopy) { |
| 5115 | // If this is a totally extraneous copy for C++03 reference |
| 5116 | // binding purposes, just return the original initialization |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5117 | // expression. We don't generate an (elided) copy operation here |
| 5118 | // because doing so would require us to pass down a flag to avoid |
| 5119 | // infinite recursion, where each step adds another extraneous, |
| 5120 | // elidable copy. |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5121 | |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5122 | // Instantiate the default arguments of any extra parameters in |
| 5123 | // the selected copy constructor, as if we were going to create a |
| 5124 | // proper call to the copy constructor. |
| 5125 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 5126 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 5127 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5128 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 30b5277 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5129 | break; |
| 5130 | |
| 5131 | // Build the default argument expression; we don't actually care |
| 5132 | // if this succeeds or not, because this routine will complain |
| 5133 | // if there was a problem. |
| 5134 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 5135 | } |
| 5136 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5137 | return CurInitExpr; |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5138 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5139 | |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5140 | // Determine the arguments required to actually perform the |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5141 | // constructor call (we might have derived-to-base conversions, or |
| 5142 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5143 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5144 | return ExprError(); |
Douglas Gregor | 5ab1165 | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5145 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5146 | // Actually perform the constructor call. |
| 5147 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5148 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5149 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5150 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 5151 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5152 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5153 | CXXConstructExpr::CK_Complete, |
| 5154 | SourceRange()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5155 | |
Douglas Gregor | d0ace02 | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5156 | // If we're supposed to bind temporaries, do so. |
| 5157 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5158 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5159 | return CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5160 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5161 | |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5162 | /// \brief Check whether elidable copy construction for binding a reference to |
| 5163 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 5164 | /// -Wc++98-compat. |
| 5165 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 5166 | const InitializedEntity &Entity, |
| 5167 | Expr *CurInitExpr) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5168 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5169 | |
| 5170 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 5171 | if (!Record) |
| 5172 | return; |
| 5173 | |
| 5174 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 5175 | if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc)) |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5176 | return; |
| 5177 | |
| 5178 | // Find constructors which would have been considered. |
Richard Smith | 100b24a | 2014-04-17 01:52:14 +0000 | [diff] [blame] | 5179 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5180 | LookupCopyAndMoveConstructors( |
| 5181 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 5182 | |
| 5183 | // Perform overload resolution. |
| 5184 | OverloadCandidateSet::iterator Best; |
| 5185 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 5186 | |
| 5187 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 5188 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 5189 | << CurInitExpr->getSourceRange(); |
| 5190 | |
| 5191 | switch (OR) { |
| 5192 | case OR_Success: |
| 5193 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | 5dadb65 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 5194 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5195 | // FIXME: Check default arguments as far as that's possible. |
| 5196 | break; |
| 5197 | |
| 5198 | case OR_No_Viable_Function: |
| 5199 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5200 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5201 | break; |
| 5202 | |
| 5203 | case OR_Ambiguous: |
| 5204 | S.Diag(Loc, Diag); |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5205 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5206 | break; |
| 5207 | |
| 5208 | case OR_Deleted: |
| 5209 | S.Diag(Loc, Diag); |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5210 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | c620f55 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5211 | break; |
| 5212 | } |
| 5213 | } |
| 5214 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5215 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 5216 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5217 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5218 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 5219 | return; |
| 5220 | |
| 5221 | if (Entity.getDecl()->getDeclName()) |
| 5222 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 5223 | << Entity.getDecl()->getDeclName(); |
| 5224 | else |
| 5225 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 5226 | } |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5227 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 5228 | Entity.getMethodDecl()) |
| 5229 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 5230 | diag::note_method_return_type_change) |
| 5231 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5232 | } |
| 5233 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5234 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 5235 | return s.Kind == InitializationSequence::SK_BindReference || |
| 5236 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 5237 | } |
| 5238 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5239 | /// Returns true if the parameters describe a constructor initialization of |
| 5240 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 5241 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 5242 | const InitializationKind &Kind, |
| 5243 | unsigned NumArgs) { |
| 5244 | switch (Entity.getKind()) { |
| 5245 | case InitializedEntity::EK_Temporary: |
| 5246 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5247 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5248 | break; |
| 5249 | default: |
| 5250 | return false; |
| 5251 | } |
| 5252 | |
| 5253 | switch (Kind.getKind()) { |
| 5254 | case InitializationKind::IK_DirectList: |
| 5255 | return true; |
| 5256 | // FIXME: Hack to work around cast weirdness. |
| 5257 | case InitializationKind::IK_Direct: |
| 5258 | case InitializationKind::IK_Value: |
| 5259 | return NumArgs != 1; |
| 5260 | default: |
| 5261 | return false; |
| 5262 | } |
| 5263 | } |
| 5264 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5265 | static ExprResult |
| 5266 | PerformConstructorInitialization(Sema &S, |
| 5267 | const InitializedEntity &Entity, |
| 5268 | const InitializationKind &Kind, |
| 5269 | MultiExprArg Args, |
| 5270 | const InitializationSequence::Step& Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5271 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5272 | bool IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 5273 | bool IsStdInitListInitialization, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5274 | SourceLocation LBraceLoc, |
| 5275 | SourceLocation RBraceLoc) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5276 | unsigned NumArgs = Args.size(); |
| 5277 | CXXConstructorDecl *Constructor |
| 5278 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 5279 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 5280 | |
| 5281 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5282 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5283 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 5284 | ? Kind.getEqualLoc() |
| 5285 | : Kind.getLocation(); |
| 5286 | |
| 5287 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 5288 | // Force even a trivial, implicit default constructor to be |
| 5289 | // semantically checked. We do this explicitly because we don't build |
| 5290 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 47ff122 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 5291 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5292 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | f704ade | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 5293 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5294 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 5295 | } |
| 5296 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5297 | ExprResult CurInit((Expr *)nullptr); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5298 | |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5299 | // C++ [over.match.copy]p1: |
| 5300 | // - When initializing a temporary to be bound to the first parameter |
| 5301 | // of a constructor that takes a reference to possibly cv-qualified |
| 5302 | // T as its first argument, called with a single argument in the |
| 5303 | // context of direct-initialization, explicit conversion functions |
| 5304 | // are also considered. |
| 5305 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 5306 | Args.size() == 1 && |
| 5307 | Constructor->isCopyOrMoveConstructor(); |
| 5308 | |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5309 | // Determine the arguments required to actually perform the constructor |
| 5310 | // call. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5311 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5312 | Loc, ConstructorArgs, |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5313 | AllowExplicitConv, |
| 5314 | IsListInitialization)) |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5315 | return ExprError(); |
| 5316 | |
| 5317 | |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5318 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5319 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5320 | S.MarkFunctionReferenced(Loc, Constructor); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5321 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 5322 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5323 | |
| 5324 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5325 | if (!TSInfo) |
| 5326 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5327 | SourceRange ParenOrBraceRange = |
| 5328 | (Kind.getKind() == InitializationKind::IK_DirectList) |
| 5329 | ? SourceRange(LBraceLoc, RBraceLoc) |
| 5330 | : Kind.getParenRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5331 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5332 | CurInit = new (S.Context) CXXTemporaryObjectExpr( |
| 5333 | S.Context, Constructor, TSInfo, ConstructorArgs, ParenOrBraceRange, |
| 5334 | HadMultipleCandidates, IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 5335 | IsStdInitListInitialization, ConstructorInitRequiresZeroInit); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5336 | } else { |
| 5337 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 5338 | CXXConstructExpr::CK_Complete; |
| 5339 | |
| 5340 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5341 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 5342 | CXXConstructExpr::CK_VirtualBase : |
| 5343 | CXXConstructExpr::CK_NonVirtualBase; |
| 5344 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 5345 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 5346 | } |
| 5347 | |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 5348 | // Only get the parenthesis or brace range if it is a list initialization or |
| 5349 | // direct construction. |
| 5350 | SourceRange ParenOrBraceRange; |
| 5351 | if (IsListInitialization) |
| 5352 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 5353 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
| 5354 | ParenOrBraceRange = Kind.getParenRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5355 | |
| 5356 | // If the entity allows NRVO, mark the construction as elidable |
| 5357 | // unconditionally. |
| 5358 | if (Entity.allowsNRVO()) |
| 5359 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5360 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5361 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5362 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5363 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 5364 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5365 | ConstructorInitRequiresZeroInit, |
| 5366 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 5367 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5368 | else |
| 5369 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5370 | Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5371 | ConstructorArgs, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5372 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5373 | IsListInitialization, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 5374 | IsStdInitListInitialization, |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5375 | ConstructorInitRequiresZeroInit, |
| 5376 | ConstructKind, |
Peter Collingbourne | b8d17e7 | 2014-02-22 02:59:41 +0000 | [diff] [blame] | 5377 | ParenOrBraceRange); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5378 | } |
| 5379 | if (CurInit.isInvalid()) |
| 5380 | return ExprError(); |
| 5381 | |
| 5382 | // Only check access if all of that succeeded. |
| 5383 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 5384 | Step.Function.FoundDecl.getAccess()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5385 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 5386 | return ExprError(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5387 | |
| 5388 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5389 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5390 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5391 | return CurInit; |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5394 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 5395 | /// longer than the current full-expression. Conservatively returns false if |
| 5396 | /// it's unclear. |
| 5397 | static bool |
| 5398 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 5399 | const InitializedEntity *Top = &Entity; |
| 5400 | while (Top->getParent()) |
| 5401 | Top = Top->getParent(); |
| 5402 | |
| 5403 | switch (Top->getKind()) { |
| 5404 | case InitializedEntity::EK_Variable: |
| 5405 | case InitializedEntity::EK_Result: |
| 5406 | case InitializedEntity::EK_Exception: |
| 5407 | case InitializedEntity::EK_Member: |
| 5408 | case InitializedEntity::EK_New: |
| 5409 | case InitializedEntity::EK_Base: |
| 5410 | case InitializedEntity::EK_Delegating: |
| 5411 | return true; |
| 5412 | |
| 5413 | case InitializedEntity::EK_ArrayElement: |
| 5414 | case InitializedEntity::EK_VectorElement: |
| 5415 | case InitializedEntity::EK_BlockElement: |
| 5416 | case InitializedEntity::EK_ComplexElement: |
| 5417 | // Could not determine what the full initialization is. Assume it might not |
| 5418 | // outlive the full-expression. |
| 5419 | return false; |
| 5420 | |
| 5421 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5422 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5423 | case InitializedEntity::EK_Temporary: |
| 5424 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 6c0505e | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5425 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5426 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5427 | // The entity being initialized might not outlive the full-expression. |
| 5428 | return false; |
| 5429 | } |
| 5430 | |
| 5431 | llvm_unreachable("unknown entity kind"); |
| 5432 | } |
| 5433 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5434 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 5435 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 5436 | /// the initialization of \p Entity. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5437 | static const InitializedEntity *getEntityForTemporaryLifetimeExtension( |
| 5438 | const InitializedEntity *Entity, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5439 | const InitializedEntity *FallbackDecl = nullptr) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5440 | // C++11 [class.temporary]p5: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5441 | switch (Entity->getKind()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5442 | case InitializedEntity::EK_Variable: |
| 5443 | // The temporary [...] persists for the lifetime of the reference |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5444 | return Entity; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5445 | |
| 5446 | case InitializedEntity::EK_Member: |
| 5447 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5448 | if (Entity->getParent()) |
| 5449 | return getEntityForTemporaryLifetimeExtension(Entity->getParent(), |
| 5450 | Entity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5451 | |
| 5452 | // except: |
| 5453 | // -- A temporary bound to a reference member in a constructor's |
| 5454 | // ctor-initializer persists until the constructor exits. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5455 | return Entity; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5456 | |
| 5457 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5458 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5459 | // -- A temporary bound to a reference parameter in a function call |
| 5460 | // persists until the completion of the full-expression containing |
| 5461 | // the call. |
| 5462 | case InitializedEntity::EK_Result: |
| 5463 | // -- The lifetime of a temporary bound to the returned value in a |
| 5464 | // function return statement is not extended; the temporary is |
| 5465 | // destroyed at the end of the full-expression in the return statement. |
| 5466 | case InitializedEntity::EK_New: |
| 5467 | // -- A temporary bound to a reference in a new-initializer persists |
| 5468 | // until the completion of the full-expression containing the |
| 5469 | // new-initializer. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5470 | return nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5471 | |
| 5472 | case InitializedEntity::EK_Temporary: |
| 5473 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | b248ca5 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5474 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5475 | // We don't yet know the storage duration of the surrounding temporary. |
| 5476 | // Assume it's got full-expression duration for now, it will patch up our |
| 5477 | // storage duration if that's not correct. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5478 | return nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5479 | |
| 5480 | case InitializedEntity::EK_ArrayElement: |
| 5481 | // For subobjects, we look at the complete object. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5482 | return getEntityForTemporaryLifetimeExtension(Entity->getParent(), |
| 5483 | FallbackDecl); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5484 | |
| 5485 | case InitializedEntity::EK_Base: |
| 5486 | case InitializedEntity::EK_Delegating: |
| 5487 | // We can reach this case for aggregate initialization in a constructor: |
| 5488 | // struct A { int &&r; }; |
| 5489 | // struct B : A { B() : A{0} {} }; |
| 5490 | // In this case, use the innermost field decl as the context. |
| 5491 | return FallbackDecl; |
| 5492 | |
| 5493 | case InitializedEntity::EK_BlockElement: |
| 5494 | case InitializedEntity::EK_LambdaCapture: |
| 5495 | case InitializedEntity::EK_Exception: |
| 5496 | case InitializedEntity::EK_VectorElement: |
| 5497 | case InitializedEntity::EK_ComplexElement: |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5498 | return nullptr; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5499 | } |
Benjamin Kramer | cabc882 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 5500 | llvm_unreachable("unknown entity kind"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5501 | } |
| 5502 | |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5503 | static void performLifetimeExtension(Expr *Init, |
| 5504 | const InitializedEntity *ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5505 | |
| 5506 | /// Update a glvalue expression that is used as the initializer of a reference |
| 5507 | /// to note that its lifetime is extended. |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5508 | /// \return \c true if any temporary had its lifetime extended. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5509 | static bool |
| 5510 | performReferenceExtension(Expr *Init, |
| 5511 | const InitializedEntity *ExtendingEntity) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5512 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 5513 | if (ILE->getNumInits() == 1 && ILE->isGLValue()) { |
| 5514 | // This is just redundant braces around an initializer. Step over it. |
| 5515 | Init = ILE->getInit(0); |
| 5516 | } |
| 5517 | } |
| 5518 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5519 | // Walk past any constructs which we can lifetime-extend across. |
| 5520 | Expr *Old; |
| 5521 | do { |
| 5522 | Old = Init; |
| 5523 | |
| 5524 | // Step over any subobject adjustments; we may have a materialized |
| 5525 | // temporary inside them. |
| 5526 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5527 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5528 | Init = const_cast<Expr *>( |
| 5529 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5530 | |
| 5531 | // Per current approach for DR1376, look through casts to reference type |
| 5532 | // when performing lifetime extension. |
| 5533 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 5534 | if (CE->getSubExpr()->isGLValue()) |
| 5535 | Init = CE->getSubExpr(); |
| 5536 | |
| 5537 | // FIXME: Per DR1213, subscripting on an array temporary produces an xvalue. |
| 5538 | // It's unclear if binding a reference to that xvalue extends the array |
| 5539 | // temporary. |
| 5540 | } while (Init != Old); |
| 5541 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5542 | if (MaterializeTemporaryExpr *ME = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
| 5543 | // Update the storage duration of the materialized temporary. |
| 5544 | // FIXME: Rebuild the expression instead of mutating it. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5545 | ME->setExtendingDecl(ExtendingEntity->getDecl(), |
| 5546 | ExtendingEntity->allocateManglingNumber()); |
| 5547 | performLifetimeExtension(ME->GetTemporaryExpr(), ExtendingEntity); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5548 | return true; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5549 | } |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5550 | |
| 5551 | return false; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5552 | } |
| 5553 | |
| 5554 | /// Update a prvalue expression that is going to be materialized as a |
| 5555 | /// lifetime-extended temporary. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5556 | static void performLifetimeExtension(Expr *Init, |
| 5557 | const InitializedEntity *ExtendingEntity) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5558 | // Dig out the expression which constructs the extended temporary. |
| 5559 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5560 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5561 | Init = const_cast<Expr *>( |
| 5562 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5563 | |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5564 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 5565 | Init = BTE->getSubExpr(); |
| 5566 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5567 | if (CXXStdInitializerListExpr *ILE = |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5568 | dyn_cast<CXXStdInitializerListExpr>(Init)) { |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5569 | performReferenceExtension(ILE->getSubExpr(), ExtendingEntity); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5570 | return; |
| 5571 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5572 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5573 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5574 | if (ILE->getType()->isArrayType()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5575 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5576 | performLifetimeExtension(ILE->getInit(I), ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5577 | return; |
| 5578 | } |
| 5579 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5580 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5581 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 5582 | |
| 5583 | // If we lifetime-extend a braced initializer which is initializing an |
| 5584 | // aggregate, and that aggregate contains reference members which are |
| 5585 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 5586 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 5587 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5588 | performReferenceExtension(ILE->getInit(0), ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5589 | else { |
| 5590 | unsigned Index = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5591 | for (const auto *I : RD->fields()) { |
Richard Smith | 0bca59d | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 5592 | if (Index >= ILE->getNumInits()) |
| 5593 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5594 | if (I->isUnnamedBitfield()) |
| 5595 | continue; |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5596 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5597 | if (I->getType()->isReferenceType()) |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5598 | performReferenceExtension(SubInit, ExtendingEntity); |
Richard Smith | 8d7f11d | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5599 | else if (isa<InitListExpr>(SubInit) || |
| 5600 | isa<CXXStdInitializerListExpr>(SubInit)) |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5601 | // This may be either aggregate-initialization of a member or |
| 5602 | // initialization of a std::initializer_list object. Either way, |
| 5603 | // we should recursively lifetime-extend that initializer. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5604 | performLifetimeExtension(SubInit, ExtendingEntity); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5605 | ++Index; |
| 5606 | } |
| 5607 | } |
| 5608 | } |
| 5609 | } |
| 5610 | } |
| 5611 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5612 | static void warnOnLifetimeExtension(Sema &S, const InitializedEntity &Entity, |
| 5613 | const Expr *Init, bool IsInitializerList, |
| 5614 | const ValueDecl *ExtendingDecl) { |
| 5615 | // Warn if a field lifetime-extends a temporary. |
| 5616 | if (isa<FieldDecl>(ExtendingDecl)) { |
| 5617 | if (IsInitializerList) { |
| 5618 | S.Diag(Init->getExprLoc(), diag::warn_dangling_std_initializer_list) |
| 5619 | << /*at end of constructor*/true; |
| 5620 | return; |
| 5621 | } |
| 5622 | |
| 5623 | bool IsSubobjectMember = false; |
| 5624 | for (const InitializedEntity *Ent = Entity.getParent(); Ent; |
| 5625 | Ent = Ent->getParent()) { |
| 5626 | if (Ent->getKind() != InitializedEntity::EK_Base) { |
| 5627 | IsSubobjectMember = true; |
| 5628 | break; |
| 5629 | } |
| 5630 | } |
| 5631 | S.Diag(Init->getExprLoc(), |
| 5632 | diag::warn_bind_ref_member_to_temporary) |
| 5633 | << ExtendingDecl << Init->getSourceRange() |
| 5634 | << IsSubobjectMember << IsInitializerList; |
| 5635 | if (IsSubobjectMember) |
| 5636 | S.Diag(ExtendingDecl->getLocation(), |
| 5637 | diag::note_ref_subobject_of_member_declared_here); |
| 5638 | else |
| 5639 | S.Diag(ExtendingDecl->getLocation(), |
| 5640 | diag::note_ref_or_ptr_member_declared_here) |
| 5641 | << /*is pointer*/false; |
| 5642 | } |
| 5643 | } |
| 5644 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5645 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 5646 | const ImplicitConversionSequence &ICS, |
| 5647 | QualType PreNarrowingType, |
| 5648 | QualType EntityType, |
| 5649 | const Expr *PostInit); |
| 5650 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5651 | ExprResult |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5652 | InitializationSequence::Perform(Sema &S, |
| 5653 | const InitializedEntity &Entity, |
| 5654 | const InitializationKind &Kind, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5655 | MultiExprArg Args, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5656 | QualType *ResultType) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5657 | if (Failed()) { |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5658 | Diagnose(S, Entity, Kind, Args); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5659 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5660 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5661 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5662 | if (getKind() == DependentSequence) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5663 | // If the declaration is a non-dependent, incomplete array type |
| 5664 | // that has an initializer, then its type will be completed once |
| 5665 | // the initializer is instantiated. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5666 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5667 | Args.size() == 1) { |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5668 | QualType DeclType = Entity.getType(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5669 | if (const IncompleteArrayType *ArrayT |
| 5670 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 5671 | // FIXME: We don't currently have the ability to accurately |
| 5672 | // compute the length of an initializer list without |
| 5673 | // performing full type-checking of the initializer list |
| 5674 | // (since we have to determine where braces are implicitly |
| 5675 | // introduced and such). So, we fall back to making the array |
| 5676 | // type a dependently-sized array type with no specified |
| 5677 | // bound. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5678 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5679 | SourceRange Brackets; |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5680 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5681 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5682 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 5683 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 5684 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5685 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 5686 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 5687 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5688 | } |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5689 | } |
| 5690 | |
| 5691 | *ResultType |
| 5692 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5693 | /*NumElts=*/nullptr, |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5694 | ArrayT->getSizeModifier(), |
| 5695 | ArrayT->getIndexTypeCVRQualifiers(), |
| 5696 | Brackets); |
| 5697 | } |
| 5698 | |
| 5699 | } |
| 5700 | } |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5701 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 5702 | !Kind.isExplicitCast()) { |
| 5703 | // Rebuild the ParenListExpr. |
| 5704 | SourceRange ParenRange = Kind.getParenRange(); |
| 5705 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5706 | Args); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5707 | } |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5708 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | bf13895 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 5709 | Kind.isExplicitCast() || |
| 5710 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5711 | return ExprResult(Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5712 | } |
| 5713 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5714 | // No steps means no initialization. |
| 5715 | if (Steps.empty()) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5716 | return ExprResult((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5717 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5718 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5719 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5720 | !Entity.isParameterKind()) { |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5721 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 5722 | // from an initializer list. For parameters, we produce a better warning |
| 5723 | // elsewhere. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5724 | Expr *Init = Args[0]; |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5725 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 5726 | << Init->getSourceRange(); |
| 5727 | } |
| 5728 | |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5729 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 5730 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5731 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5732 | Entity.getType()->isPointerType() && |
| 5733 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5734 | Expr *Init = Args[0]; |
Richard Smith | eb3cad5 | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5735 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 5736 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 5737 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 5738 | << Init->getSourceRange(); |
| 5739 | } |
| 5740 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5741 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 5742 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5743 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 5744 | // and we want latter when it makes sense. |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5745 | if (ResultType) |
Eli Friedman | 463e523 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5746 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5747 | Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5748 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5749 | ExprResult CurInit((Expr *)nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5750 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5751 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5752 | // grab the only argument out the Args and place it into the "current" |
| 5753 | // initializer. |
| 5754 | switch (Steps.front().Kind) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5755 | case SK_ResolveAddressOfOverloadedFunction: |
| 5756 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5757 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5758 | case SK_CastDerivedToBaseLValue: |
| 5759 | case SK_BindReference: |
| 5760 | case SK_BindReferenceToTemporary: |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5761 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5762 | case SK_UserConversion: |
| 5763 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5764 | case SK_QualificationConversionXValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5765 | case SK_QualificationConversionRValue: |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5766 | case SK_LValueToRValue: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5767 | case SK_ConversionSequence: |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5768 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5769 | case SK_ListInitialization: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5770 | case SK_UnwrapInitList: |
| 5771 | case SK_RewrapInitList: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5772 | case SK_CAssignment: |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5773 | case SK_StringInit: |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5774 | case SK_ObjCObjectConversion: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5775 | case SK_ArrayInit: |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5776 | case SK_ParenthesizedArrayInit: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5777 | case SK_PassByIndirectCopyRestore: |
| 5778 | case SK_PassByIndirectRestore: |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5779 | case SK_ProduceObjCObject: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5780 | case SK_StdInitializerList: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5781 | case SK_OCLSamplerInit: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5782 | case SK_OCLZeroEvent: { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5783 | assert(Args.size() == 1); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5784 | CurInit = Args[0]; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5785 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5786 | break; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5787 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5788 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5789 | case SK_ConstructorInitialization: |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 5790 | case SK_ConstructorInitializationFromList: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 5791 | case SK_StdInitializerListConstructorCall: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5792 | case SK_ZeroInitialization: |
| 5793 | break; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5794 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5795 | |
| 5796 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5797 | // performing the specified conversions along the way. |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5798 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5799 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5800 | Step != StepEnd; ++Step) { |
| 5801 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5802 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5803 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5804 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5805 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5806 | switch (Step->Kind) { |
| 5807 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5808 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5809 | // initializer to reflect that choice. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5810 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5811 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 5812 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5813 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5814 | Step->Function.FoundDecl, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5815 | Step->Function.Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5816 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5817 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5818 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5819 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5820 | case SK_CastDerivedToBaseLValue: { |
| 5821 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5822 | // lvalue. Perform that cast. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5823 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5824 | CXXCastPath BasePath; |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5825 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5826 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5827 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5828 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5829 | CurInit.get()->getLocStart(), |
| 5830 | CurInit.get()->getSourceRange(), |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5831 | &BasePath, IgnoreBaseAccess)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5832 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5833 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5834 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 5835 | QualType T = SourceType; |
| 5836 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 5837 | T = Pointer->getPointeeType(); |
| 5838 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5839 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5840 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 5841 | } |
| 5842 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5843 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5844 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5845 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5846 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5847 | VK_XValue : |
| 5848 | VK_RValue); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5849 | CurInit = |
| 5850 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase, |
| 5851 | CurInit.get(), &BasePath, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5852 | break; |
| 5853 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5854 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5855 | case SK_BindReference: |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5856 | // References cannot bind to bit-fields (C++ [dcl.init.ref]p5). |
| 5857 | if (CurInit.get()->refersToBitField()) { |
| 5858 | // We don't necessarily have an unambiguous source bit-field. |
| 5859 | FieldDecl *BitField = CurInit.get()->getSourceBitField(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5860 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5861 | << Entity.getType().isVolatileQualified() |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5862 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5863 | << (BitField != nullptr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5864 | << CurInit.get()->getSourceRange(); |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5865 | if (BitField) |
| 5866 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 5867 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5868 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5869 | } |
Anders Carlsson | a91be64 | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5870 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5871 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | c17ae44 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5872 | // References cannot bind to vector elements. |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5873 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5874 | << Entity.getType().isVolatileQualified() |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5875 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5876 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5877 | return ExprError(); |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5878 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5879 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5880 | // Reference binding does not have any corresponding ASTs. |
| 5881 | |
| 5882 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5883 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5884 | return ExprError(); |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5885 | |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5886 | // Even though we didn't materialize a temporary, the binding may still |
| 5887 | // extend the lifetime of a temporary. This happens if we bind a reference |
| 5888 | // to the result of a cast to reference type. |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5889 | if (const InitializedEntity *ExtendingEntity = |
| 5890 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 5891 | if (performReferenceExtension(CurInit.get(), ExtendingEntity)) |
| 5892 | warnOnLifetimeExtension(S, Entity, CurInit.get(), |
| 5893 | /*IsInitializerList=*/false, |
| 5894 | ExtendingEntity->getDecl()); |
Richard Smith | 6b6f8aa | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5895 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5896 | break; |
Anders Carlsson | ab0ddb5 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5897 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5898 | case SK_BindReferenceToTemporary: { |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5899 | // Make sure the "temporary" is actually an rvalue. |
| 5900 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 5901 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5902 | // Check exception specifications |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5903 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5904 | return ExprError(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5905 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5906 | // Materialize the temporary into memory. |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5907 | MaterializeTemporaryExpr *MTE = new (S.Context) MaterializeTemporaryExpr( |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5908 | Entity.getType().getNonReferenceType(), CurInit.get(), |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 5909 | Entity.getType()->isLValueReferenceType()); |
| 5910 | |
| 5911 | // Maybe lifetime-extend the temporary's subobjects to match the |
| 5912 | // entity's lifetime. |
| 5913 | if (const InitializedEntity *ExtendingEntity = |
| 5914 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 5915 | if (performReferenceExtension(MTE, ExtendingEntity)) |
| 5916 | warnOnLifetimeExtension(S, Entity, CurInit.get(), /*IsInitializerList=*/false, |
| 5917 | ExtendingEntity->getDecl()); |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5918 | |
| 5919 | // 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] | 5920 | // need cleanups. Likewise if we're extending this temporary to automatic |
| 5921 | // storage duration -- we need to register its cleanup during the |
| 5922 | // full-expression's cleanups. |
| 5923 | if ((S.getLangOpts().ObjCAutoRefCount && |
| 5924 | MTE->getType()->isObjCLifetimeType()) || |
| 5925 | (MTE->getStorageDuration() == SD_Automatic && |
| 5926 | MTE->getType().isDestructedType())) |
Douglas Gregor | 58df509 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5927 | S.ExprNeedsCleanups = true; |
Richard Smith | 736a947 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5928 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 5929 | CurInit = MTE; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5930 | break; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5931 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5932 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5933 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5934 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5935 | /*IsExtraneousCopy=*/true); |
| 5936 | break; |
| 5937 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5938 | case SK_UserConversion: { |
| 5939 | // We have a user-defined conversion that invokes either a constructor |
| 5940 | // or a conversion function. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5941 | CastKind CastKind; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5942 | bool IsCopy = false; |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5943 | FunctionDecl *Fn = Step->Function.Function; |
| 5944 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5945 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5946 | bool CreatedObject = false; |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5947 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5948 | // Build a call to the selected constructor. |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5949 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5950 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5951 | CurInit.get(); // Ownership transferred into MultiExprArg, below. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5952 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5953 | // Determine the arguments required to actually perform the constructor |
| 5954 | // call. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5955 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5956 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5957 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5958 | Loc, ConstructorArgs)) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5959 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5960 | |
Richard Smith | b24f067 | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5961 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5962 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5963 | ConstructorArgs, |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5964 | HadMultipleCandidates, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5965 | /*ListInit*/ false, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 5966 | /*StdInitListInit*/ false, |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5967 | /*ZeroInit*/ false, |
Chandler Carruth | 0171815 | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5968 | CXXConstructExpr::CK_Complete, |
| 5969 | SourceRange()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5970 | if (CurInit.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5971 | return ExprError(); |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5972 | |
Anders Carlsson | a01874b | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5973 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5974 | FoundFn.getAccess()); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5975 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5976 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5977 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5978 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5979 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5980 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5981 | S.IsDerivedFrom(SourceType, Class)) |
| 5982 | IsCopy = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5983 | |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5984 | CreatedObject = true; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5985 | } else { |
| 5986 | // Build a call to the conversion function. |
John McCall | 760af17 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5987 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5988 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
John McCall | a0296f7 | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5989 | FoundFn); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5990 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5991 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5992 | |
| 5993 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5994 | // derived-to-base conversion? I believe the answer is "no", because |
| 5995 | // 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] | 5996 | ExprResult CurInitExprRes = |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 5997 | S.PerformObjectArgumentInitialization(CurInit.get(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5998 | /*Qualifier=*/nullptr, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5999 | FoundFn, Conversion); |
| 6000 | if(CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6001 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6002 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6003 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6004 | // Build the actual call to the conversion function. |
Abramo Bagnara | 635ed24e | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 6005 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 6006 | HadMultipleCandidates); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6007 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6008 | return ExprError(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6009 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6010 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6011 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6012 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6013 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6014 | |
Sebastian Redl | 112aa82 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 6015 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 6016 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 6017 | |
| 6018 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6019 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6020 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6021 | CXXDestructorDecl *Destructor |
Douglas Gregor | e71edda | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 6022 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6023 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6024 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 6025 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
Richard Smith | 22262ab | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 6026 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart())) |
| 6027 | return ExprError(); |
Douglas Gregor | 9556257 | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 6028 | } |
| 6029 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6030 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6031 | CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(), |
| 6032 | CastKind, CurInit.get(), nullptr, |
| 6033 | CurInit.get()->getValueKind()); |
Abramo Bagnara | b0cf297 | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 6034 | if (MaybeBindToTemp) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6035 | CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>()); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 6036 | if (RequiresCopy) |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6037 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6038 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6039 | break; |
| 6040 | } |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6041 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6042 | case SK_QualificationConversionLValue: |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6043 | case SK_QualificationConversionXValue: |
| 6044 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6045 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6046 | ExprValueKind VK = |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6047 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6048 | VK_LValue : |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6049 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 6050 | VK_XValue : |
| 6051 | VK_RValue); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6052 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6053 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6054 | } |
| 6055 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6056 | case SK_LValueToRValue: { |
| 6057 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6058 | CurInit = ImplicitCastExpr::Create(S.Context, Step->Type, |
| 6059 | CK_LValueToRValue, CurInit.get(), |
| 6060 | /*BasePath=*/nullptr, VK_RValue); |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6061 | break; |
| 6062 | } |
| 6063 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6064 | case SK_ConversionSequence: |
| 6065 | case SK_ConversionSequenceNoNarrowing: { |
| 6066 | Sema::CheckedConversionKind CCK |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6067 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 6068 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | 507840d | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 6069 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6070 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6071 | ExprResult CurInitExprRes = |
| 6072 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6073 | getAssignmentAction(Entity), CCK); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6074 | if (CurInitExprRes.isInvalid()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6075 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6076 | CurInit = CurInitExprRes; |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6077 | |
| 6078 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
| 6079 | S.getLangOpts().CPlusPlus && !CurInit.get()->isValueDependent()) |
| 6080 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 6081 | CurInit.get()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6082 | break; |
Douglas Gregor | 5c8ffab | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 6083 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6084 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6085 | case SK_ListInitialization: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6086 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6087 | // If we're not initializing the top-level entity, we need to create an |
| 6088 | // InitializeTemporary entity for our target type. |
| 6089 | QualType Ty = Step->Type; |
| 6090 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6091 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 6092 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 6093 | InitListChecker PerformInitList(S, InitEntity, |
Richard Smith | de22923 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 6094 | InitList, Ty, /*VerifyOnly=*/false); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6095 | if (PerformInitList.HadError()) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6096 | return ExprError(); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6097 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6098 | // Hack: We must update *ResultType if available in order to set the |
| 6099 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 6100 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 6101 | if (ResultType && |
| 6102 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6103 | if ((*ResultType)->isRValueReferenceType()) |
| 6104 | Ty = S.Context.getRValueReferenceType(Ty); |
| 6105 | else if ((*ResultType)->isLValueReferenceType()) |
| 6106 | Ty = S.Context.getLValueReferenceType(Ty, |
| 6107 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 6108 | *ResultType = Ty; |
| 6109 | } |
| 6110 | |
| 6111 | InitListExpr *StructuredInitList = |
| 6112 | PerformInitList.getFullyStructuredList(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6113 | CurInit.get(); |
Richard Smith | d712d0d | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 6114 | CurInit = shouldBindAsTemporary(InitEntity) |
| 6115 | ? S.MaybeBindToTemporary(StructuredInitList) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6116 | : StructuredInitList; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6117 | break; |
| 6118 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6119 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6120 | case SK_ConstructorInitializationFromList: { |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6121 | // When an initializer list is passed for a parameter of type "reference |
| 6122 | // to object", we don't get an EK_Temporary entity, but instead an |
| 6123 | // EK_Parameter entity with reference type. |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6124 | // FIXME: This is a hack. What we really should do is create a user |
| 6125 | // conversion step for this case, but this makes it considerably more |
| 6126 | // complicated. For now, this will do. |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6127 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6128 | Entity.getType().getNonReferenceType()); |
| 6129 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6130 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6131 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 2b349ae | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 6132 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 6133 | << InitList->getSourceRange(); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6134 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 5a41f68 | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6135 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 6136 | Entity, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6137 | Kind, Arg, *Step, |
Richard Smith | d59b832 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6138 | ConstructorInitRequiresZeroInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6139 | /*IsListInitialization*/true, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 6140 | /*IsStdInitListInit*/false, |
Enea Zaffanella | 76e98fe | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6141 | InitList->getLBraceLoc(), |
| 6142 | InitList->getRBraceLoc()); |
Sebastian Redl | ed2e532 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6143 | break; |
| 6144 | } |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6145 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6146 | case SK_UnwrapInitList: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6147 | CurInit = cast<InitListExpr>(CurInit.get())->getInit(0); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6148 | break; |
| 6149 | |
| 6150 | case SK_RewrapInitList: { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6151 | Expr *E = CurInit.get(); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6152 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 6153 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 6154 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6155 | ILE->setSyntacticForm(Syntactic); |
| 6156 | ILE->setType(E->getType()); |
| 6157 | ILE->setValueKind(E->getValueKind()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6158 | CurInit = ILE; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6159 | break; |
| 6160 | } |
| 6161 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6162 | case SK_ConstructorInitialization: |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 6163 | case SK_StdInitializerListConstructorCall: { |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6164 | // When an initializer list is passed for a parameter of type "reference |
| 6165 | // to object", we don't get an EK_Temporary entity, but instead an |
| 6166 | // EK_Parameter entity with reference type. |
| 6167 | // FIXME: This is a hack. What we really should do is create a user |
| 6168 | // conversion step for this case, but this makes it considerably more |
| 6169 | // complicated. For now, this will do. |
| 6170 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6171 | Entity.getType().getNonReferenceType()); |
| 6172 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 6173 | bool IsStdInitListInit = |
| 6174 | Step->Kind == SK_StdInitializerListConstructorCall; |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6175 | CurInit = PerformConstructorInitialization( |
| 6176 | S, UseTemporary ? TempEntity : Entity, Kind, Args, *Step, |
| 6177 | ConstructorInitRequiresZeroInit, |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 6178 | /*IsListInitialization*/IsStdInitListInit, |
| 6179 | /*IsStdInitListInitialization*/IsStdInitListInit, |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6180 | /*LBraceLoc*/SourceLocation(), |
| 6181 | /*RBraceLoc*/SourceLocation()); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6182 | break; |
Sebastian Redl | 99f6616 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6183 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6184 | |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6185 | case SK_ZeroInitialization: { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6186 | step_iterator NextStep = Step; |
| 6187 | ++NextStep; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6188 | if (NextStep != StepEnd && |
Richard Smith | d86812d | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6189 | (NextStep->Kind == SK_ConstructorInitialization || |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 6190 | NextStep->Kind == SK_ConstructorInitializationFromList)) { |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6191 | // The need for zero-initialization is recorded directly into |
| 6192 | // the call to the object's constructor within the next step. |
| 6193 | ConstructorInitRequiresZeroInit = true; |
| 6194 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6195 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6196 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6197 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6198 | if (!TSInfo) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6199 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | 2b88c11 | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6200 | Kind.getRange().getBegin()); |
| 6201 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6202 | CurInit = new (S.Context) CXXScalarValueInitExpr( |
| 6203 | TSInfo->getType().getNonLValueExprType(S.Context), TSInfo, |
| 6204 | Kind.getRange().getEnd()); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6205 | } else { |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6206 | CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type); |
Douglas Gregor | 4f4b186 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6207 | } |
Douglas Gregor | 7dc42e5 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6208 | break; |
| 6209 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6210 | |
| 6211 | case SK_CAssignment: { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6212 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6213 | ExprResult Result = CurInit; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6214 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 6215 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 6216 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6217 | if (Result.isInvalid()) |
| 6218 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6219 | CurInit = Result; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6220 | |
| 6221 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6222 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6223 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6224 | Entity.isParameterKind() && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6225 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6226 | == Sema::Compatible) |
| 6227 | ConvTy = Sema::Compatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6228 | if (CurInitExprRes.isInvalid()) |
| 6229 | return ExprError(); |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6230 | CurInit = CurInitExprRes; |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6231 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6232 | bool Complained; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6233 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 6234 | Step->Type, SourceType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6235 | CurInit.get(), |
Fariborz Jahanian | 3a25d0d | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 6236 | getAssignmentAction(Entity, true), |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6237 | &Complained)) { |
| 6238 | PrintInitLocationNote(S, Entity); |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6239 | return ExprError(); |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6240 | } else if (Complained) |
| 6241 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6242 | break; |
| 6243 | } |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6244 | |
| 6245 | case SK_StringInit: { |
| 6246 | QualType Ty = Step->Type; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6247 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | 5decec9 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 6248 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | 7827520 | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6249 | break; |
| 6250 | } |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6251 | |
| 6252 | case SK_ObjCObjectConversion: |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6253 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6254 | CK_ObjCObjectLValueCast, |
Eli Friedman | be4b363 | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 6255 | CurInit.get()->getValueKind()); |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6256 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6257 | |
| 6258 | case SK_ArrayInit: |
| 6259 | // Okay: we checked everything before creating this step. Note that |
| 6260 | // this is a GNU extension. |
| 6261 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6262 | << Step->Type << CurInit.get()->getType() |
| 6263 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6264 | |
| 6265 | // If the destination type is an incomplete array type, update the |
| 6266 | // type accordingly. |
| 6267 | if (ResultType) { |
| 6268 | if (const IncompleteArrayType *IncompleteDest |
| 6269 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 6270 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6271 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6272 | *ResultType = S.Context.getConstantArrayType( |
| 6273 | IncompleteDest->getElementType(), |
| 6274 | ConstantSource->getSize(), |
| 6275 | ArrayType::Normal, 0); |
| 6276 | } |
| 6277 | } |
| 6278 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6279 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6280 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6281 | case SK_ParenthesizedArrayInit: |
| 6282 | // Okay: we checked everything before creating this step. Note that |
| 6283 | // this is a GNU extension. |
| 6284 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 6285 | << CurInit.get()->getSourceRange(); |
| 6286 | break; |
| 6287 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6288 | case SK_PassByIndirectCopyRestore: |
| 6289 | case SK_PassByIndirectRestore: |
| 6290 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6291 | CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr( |
| 6292 | CurInit.get(), Step->Type, |
| 6293 | Step->Kind == SK_PassByIndirectCopyRestore); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6294 | break; |
| 6295 | |
| 6296 | case SK_ProduceObjCObject: |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6297 | CurInit = |
| 6298 | ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject, |
| 6299 | CurInit.get(), nullptr, VK_RValue); |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6300 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6301 | |
| 6302 | case SK_StdInitializerList: { |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6303 | S.Diag(CurInit.get()->getExprLoc(), |
| 6304 | diag::warn_cxx98_compat_initializer_list_init) |
| 6305 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 249dee5 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 6306 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6307 | // Materialize the temporary into memory. |
| 6308 | MaterializeTemporaryExpr *MTE = new (S.Context) |
| 6309 | MaterializeTemporaryExpr(CurInit.get()->getType(), CurInit.get(), |
David Majnemer | daff370 | 2014-05-01 17:50:17 +0000 | [diff] [blame] | 6310 | /*BoundToLvalueReference=*/false); |
| 6311 | |
| 6312 | // Maybe lifetime-extend the array temporary's subobjects to match the |
| 6313 | // entity's lifetime. |
| 6314 | if (const InitializedEntity *ExtendingEntity = |
| 6315 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 6316 | if (performReferenceExtension(MTE, ExtendingEntity)) |
| 6317 | warnOnLifetimeExtension(S, Entity, CurInit.get(), |
| 6318 | /*IsInitializerList=*/true, |
| 6319 | ExtendingEntity->getDecl()); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6320 | |
| 6321 | // Wrap it in a construction of a std::initializer_list<T>. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 6322 | CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6323 | |
| 6324 | // Bind the result, in case the library has given initializer_list a |
| 6325 | // non-trivial destructor. |
| 6326 | if (shouldBindAsTemporary(Entity)) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6327 | CurInit = S.MaybeBindToTemporary(CurInit.get()); |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6328 | break; |
| 6329 | } |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6330 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6331 | case SK_OCLSamplerInit: { |
| 6332 | assert(Step->Type->isSamplerT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 6333 | "Sampler initialization on non-sampler type."); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6334 | |
| 6335 | QualType SourceType = CurInit.get()->getType(); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6336 | |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6337 | if (Entity.isParameterKind()) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6338 | if (!SourceType->isSamplerT()) |
| 6339 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 6340 | << SourceType; |
Fariborz Jahanian | 131996b | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6341 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6342 | llvm_unreachable("Invalid EntityKind!"); |
| 6343 | } |
| 6344 | |
| 6345 | break; |
| 6346 | } |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6347 | case SK_OCLZeroEvent: { |
| 6348 | assert(Step->Type->isEventT() && |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 6349 | "Event initialization on non-event type."); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6350 | |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 6351 | CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6352 | CK_ZeroToOCLEvent, |
| 6353 | CurInit.get()->getValueKind()); |
| 6354 | break; |
| 6355 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6356 | } |
| 6357 | } |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6358 | |
| 6359 | // Diagnose non-fatal problems with the completed initialization. |
| 6360 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6361 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 6362 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 6363 | cast<FieldDecl>(Entity.getDecl()), |
| 6364 | CurInit.get()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6365 | |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6366 | return CurInit; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6369 | /// Somewhere within T there is an uninitialized reference subobject. |
| 6370 | /// Dig it out and diagnose it. |
Benjamin Kramer | 3e35026 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 6371 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 6372 | QualType T) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6373 | if (T->isReferenceType()) { |
| 6374 | S.Diag(Loc, diag::err_reference_without_init) |
| 6375 | << T.getNonReferenceType(); |
| 6376 | return true; |
| 6377 | } |
| 6378 | |
| 6379 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6380 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 6381 | return false; |
| 6382 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6383 | for (const auto *FI : RD->fields()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6384 | if (FI->isUnnamedBitfield()) |
| 6385 | continue; |
| 6386 | |
| 6387 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 6388 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6389 | return true; |
| 6390 | } |
| 6391 | } |
| 6392 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6393 | for (const auto &BI : RD->bases()) { |
| 6394 | if (DiagnoseUninitializedReference(S, BI.getLocStart(), BI.getType())) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6395 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6396 | return true; |
| 6397 | } |
| 6398 | } |
| 6399 | |
| 6400 | return false; |
| 6401 | } |
| 6402 | |
| 6403 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6404 | //===----------------------------------------------------------------------===// |
| 6405 | // Diagnose initialization failures |
| 6406 | //===----------------------------------------------------------------------===// |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6407 | |
| 6408 | /// Emit notes associated with an initialization that failed due to a |
| 6409 | /// "simple" conversion failure. |
| 6410 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 6411 | Expr *op) { |
| 6412 | QualType destType = entity.getType(); |
| 6413 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 6414 | op->getType()->isObjCObjectPointerType()) { |
| 6415 | |
| 6416 | // Emit a possible note about the conversion failing because the |
| 6417 | // operand is a message send with a related result type. |
| 6418 | S.EmitRelatedResultTypeNote(op); |
| 6419 | |
| 6420 | // Emit a possible note about a return failing because we're |
| 6421 | // expecting a related result type. |
| 6422 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 6423 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 6424 | } |
| 6425 | } |
| 6426 | |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6427 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 6428 | InitListExpr *InitList) { |
| 6429 | QualType DestType = Entity.getType(); |
| 6430 | |
| 6431 | QualType E; |
| 6432 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 6433 | QualType ArrayType = S.Context.getConstantArrayType( |
| 6434 | E.withConst(), |
| 6435 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 6436 | InitList->getNumInits()), |
| 6437 | clang::ArrayType::Normal, 0); |
| 6438 | InitializedEntity HiddenArray = |
| 6439 | InitializedEntity::InitializeTemporary(ArrayType); |
| 6440 | return diagnoseListInit(S, HiddenArray, InitList); |
| 6441 | } |
| 6442 | |
| 6443 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
| 6444 | /*VerifyOnly=*/false); |
| 6445 | assert(DiagnoseInitList.HadError() && |
| 6446 | "Inconsistent init list check result."); |
| 6447 | } |
| 6448 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6449 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6450 | const InitializedEntity &Entity, |
| 6451 | const InitializationKind &Kind, |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6452 | ArrayRef<Expr *> Args) { |
Sebastian Redl | 724bfe1 | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 6453 | if (!Failed()) |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6454 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6455 | |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 6456 | QualType DestType = Entity.getType(); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6457 | switch (Failure) { |
| 6458 | case FK_TooManyInitsForReference: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6459 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6460 | if (Args.empty()) { |
Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6461 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 6462 | // If this is value-initialization, this could be nested some way within |
| 6463 | // the target type. |
| 6464 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 6465 | DestType->isReferenceType()); |
| 6466 | bool Diagnosed = |
| 6467 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 6468 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 6469 | (void)Diagnosed; |
| 6470 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6471 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6472 | << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd()); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6473 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6474 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6475 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6476 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6477 | break; |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6478 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6479 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 6480 | break; |
| 6481 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6482 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 6483 | break; |
| 6484 | case FK_NarrowStringIntoWideCharArray: |
| 6485 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 6486 | break; |
| 6487 | case FK_WideStringIntoCharArray: |
| 6488 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 6489 | break; |
| 6490 | case FK_IncompatWideStringIntoWideChar: |
| 6491 | S.Diag(Kind.getLocation(), |
| 6492 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 6493 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6494 | case FK_ArrayTypeMismatch: |
| 6495 | case FK_NonConstantArrayInit: |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6496 | S.Diag(Kind.getLocation(), |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6497 | (Failure == FK_ArrayTypeMismatch |
| 6498 | ? diag::err_array_init_different_type |
| 6499 | : diag::err_array_init_non_constant_array)) |
| 6500 | << DestType.getNonReferenceType() |
| 6501 | << Args[0]->getType() |
| 6502 | << Args[0]->getSourceRange(); |
| 6503 | break; |
| 6504 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6505 | case FK_VariableLengthArrayHasInitializer: |
| 6506 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 6507 | << Args[0]->getSourceRange(); |
| 6508 | break; |
| 6509 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6510 | case FK_AddressOfOverloadFailed: { |
| 6511 | DeclAccessPair Found; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6512 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6513 | DestType.getNonReferenceType(), |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6514 | true, |
| 6515 | Found); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6516 | break; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6517 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6518 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6519 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 540c3b0 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 6520 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6521 | switch (FailedOverloadResult) { |
| 6522 | case OR_Ambiguous: |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6523 | if (Failure == FK_UserConversionOverloadFailed) |
| 6524 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 6525 | << Args[0]->getType() << DestType |
| 6526 | << Args[0]->getSourceRange(); |
| 6527 | else |
| 6528 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 6529 | << DestType << Args[0]->getType() |
| 6530 | << Args[0]->getSourceRange(); |
| 6531 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6532 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6533 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6534 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6535 | case OR_No_Viable_Function: |
Larisse Voufo | 70bb43a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 6536 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 64cf3ef | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 6537 | DestType.getNonReferenceType(), |
| 6538 | diag::err_typecheck_nonviable_condition_incomplete, |
| 6539 | Args[0]->getType(), Args[0]->getSourceRange())) |
| 6540 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 6541 | << Args[0]->getType() << Args[0]->getSourceRange() |
| 6542 | << DestType.getNonReferenceType(); |
| 6543 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6544 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6545 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6546 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6547 | case OR_Deleted: { |
| 6548 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 6549 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 6550 | << Args[0]->getSourceRange(); |
| 6551 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6552 | OverloadingResult Ovl |
Douglas Gregor | d5b730c9 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6553 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 6554 | true); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6555 | if (Ovl == OR_Deleted) { |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6556 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6557 | } else { |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6558 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6559 | } |
| 6560 | break; |
| 6561 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6562 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6563 | case OR_Success: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6564 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6565 | } |
| 6566 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6567 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6568 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6569 | if (isa<InitListExpr>(Args[0])) { |
| 6570 | S.Diag(Kind.getLocation(), |
| 6571 | diag::err_lvalue_reference_bind_to_initlist) |
| 6572 | << DestType.getNonReferenceType().isVolatileQualified() |
| 6573 | << DestType.getNonReferenceType() |
| 6574 | << Args[0]->getSourceRange(); |
| 6575 | break; |
| 6576 | } |
| 6577 | // Intentional fallthrough |
| 6578 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6579 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6580 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6581 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 6582 | ? diag::err_lvalue_reference_bind_to_temporary |
| 6583 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | d1e0864 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 6584 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6585 | << DestType.getNonReferenceType() |
| 6586 | << Args[0]->getType() |
| 6587 | << Args[0]->getSourceRange(); |
| 6588 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6589 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6590 | case FK_RValueReferenceBindingToLValue: |
| 6591 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | bed28f7 | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 6592 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6593 | << Args[0]->getSourceRange(); |
| 6594 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6595 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6596 | case FK_ReferenceInitDropsQualifiers: |
| 6597 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 6598 | << DestType.getNonReferenceType() |
| 6599 | << Args[0]->getType() |
| 6600 | << Args[0]->getSourceRange(); |
| 6601 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6602 | |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6603 | case FK_ReferenceInitFailed: |
| 6604 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 6605 | << DestType.getNonReferenceType() |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6606 | << Args[0]->isLValue() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6607 | << Args[0]->getType() |
| 6608 | << Args[0]->getSourceRange(); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6609 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6610 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6611 | |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6612 | case FK_ConversionFailed: { |
| 6613 | QualType FromType = Args[0]->getType(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6614 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6615 | << (int)Entity.getKind() |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6616 | << DestType |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6617 | << Args[0]->isLValue() |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6618 | << FromType |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6619 | << Args[0]->getSourceRange(); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6620 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 6621 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6622 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6623 | break; |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6624 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6625 | |
| 6626 | case FK_ConversionFromPropertyFailed: |
| 6627 | // No-op. This error has already been reported. |
| 6628 | break; |
| 6629 | |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6630 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6631 | SourceRange R; |
| 6632 | |
| 6633 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6634 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6635 | InitList->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6636 | else |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6637 | R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6638 | |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 6639 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
Douglas Gregor | 8ec5173 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6640 | if (Kind.isCStyleOrFunctionalCast()) |
| 6641 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 6642 | << R; |
| 6643 | else |
| 6644 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 6645 | << /*scalar=*/2 << R; |
Douglas Gregor | 51e77d5 | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6646 | break; |
| 6647 | } |
| 6648 | |
| 6649 | case FK_ReferenceBindingToInitList: |
| 6650 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 6651 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 6652 | break; |
| 6653 | |
| 6654 | case FK_InitListBadDestinationType: |
| 6655 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 6656 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 6657 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6658 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6659 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6660 | case FK_ConstructorOverloadFailed: { |
| 6661 | SourceRange ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6662 | if (Args.size()) |
| 6663 | ArgsRange = SourceRange(Args.front()->getLocStart(), |
| 6664 | Args.back()->getLocEnd()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6665 | |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6666 | if (Failure == FK_ListConstructorOverloadFailed) { |
Nico Weber | 9709ebf | 2014-07-08 23:54:25 +0000 | [diff] [blame] | 6667 | assert(Args.size() == 1 && |
| 6668 | "List construction from other than 1 argument."); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6669 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6670 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 6901c0d | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6671 | } |
| 6672 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6673 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 6674 | // bad. |
| 6675 | switch (FailedOverloadResult) { |
| 6676 | case OR_Ambiguous: |
| 6677 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 6678 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6679 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6680 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6681 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6682 | case OR_No_Viable_Function: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6683 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 6684 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 6685 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 6686 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6687 | // This is implicit default initialization of a member or |
| 6688 | // base within a constructor. If no viable function was |
| 6689 | // found, notify the user that she needs to explicitly |
| 6690 | // initialize this base/member. |
| 6691 | CXXConstructorDecl *Constructor |
| 6692 | = cast<CXXConstructorDecl>(S.CurContext); |
| 6693 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6694 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6695 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6696 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6697 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6698 | << /*base=*/0 |
| 6699 | << Entity.getType(); |
| 6700 | |
| 6701 | RecordDecl *BaseDecl |
| 6702 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 6703 | ->getDecl(); |
| 6704 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 6705 | << S.Context.getTagDeclType(BaseDecl); |
| 6706 | } else { |
| 6707 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | c2bc61b | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6708 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6709 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6710 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6711 | << /*member=*/1 |
| 6712 | << Entity.getName(); |
Alp Toker | 2afa878 | 2014-05-28 12:20:14 +0000 | [diff] [blame] | 6713 | S.Diag(Entity.getDecl()->getLocation(), |
| 6714 | diag::note_member_declared_at); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6715 | |
| 6716 | if (const RecordType *Record |
| 6717 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6718 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6719 | diag::note_previous_decl) |
| 6720 | << S.Context.getTagDeclType(Record->getDecl()); |
| 6721 | } |
| 6722 | break; |
| 6723 | } |
| 6724 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6725 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 6726 | << DestType << ArgsRange; |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6727 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6728 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6729 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6730 | case OR_Deleted: { |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6731 | OverloadCandidateSet::iterator Best; |
John McCall | 5c32be0 | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6732 | OverloadingResult Ovl |
| 6733 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6734 | if (Ovl != OR_Deleted) { |
| 6735 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6736 | << true << DestType << ArgsRange; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6737 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6738 | break; |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6739 | } |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6740 | |
| 6741 | // If this is a defaulted or implicitly-declared function, then |
| 6742 | // it was implicitly deleted. Make it clear that the deletion was |
| 6743 | // implicit. |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6744 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6745 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6746 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | 74f7d50 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6747 | << DestType << ArgsRange; |
Richard Smith | 852265f | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6748 | else |
| 6749 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6750 | << true << DestType << ArgsRange; |
| 6751 | |
| 6752 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6753 | break; |
| 6754 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6755 | |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6756 | case OR_Success: |
| 6757 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6758 | } |
Douglas Gregor | 1e7ffa7 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6759 | } |
David Blaikie | 60deeee | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 6760 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6761 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6762 | case FK_DefaultInitOfConst: |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6763 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6764 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6765 | // This is implicit default-initialization of a const member in |
| 6766 | // a constructor. Complain that it needs to be explicitly |
| 6767 | // initialized. |
| 6768 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 6769 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_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 | << /*const=*/1 |
| 6774 | << Entity.getName(); |
| 6775 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 6776 | << Entity.getName(); |
| 6777 | } else { |
| 6778 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 6779 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 6780 | } |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6781 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6782 | |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6783 | case FK_Incomplete: |
Douglas Gregor | 85f3423 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 6784 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6785 | diag::err_init_incomplete_type); |
| 6786 | break; |
| 6787 | |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6788 | case FK_ListInitializationFailed: { |
| 6789 | // Run the init list checker again to emit diagnostics. |
Richard Smith | 0449aaf | 2013-11-21 23:30:57 +0000 | [diff] [blame] | 6790 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 6791 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6792 | break; |
| 6793 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6794 | |
| 6795 | case FK_PlaceholderType: { |
| 6796 | // FIXME: Already diagnosed! |
| 6797 | break; |
| 6798 | } |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6799 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6800 | case FK_ExplicitConstructor: { |
| 6801 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 6802 | << Args[0]->getSourceRange(); |
| 6803 | OverloadCandidateSet::iterator Best; |
| 6804 | OverloadingResult Ovl |
| 6805 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | 5dcce09 | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 6806 | (void)Ovl; |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6807 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 6808 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 6809 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 6810 | break; |
| 6811 | } |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6812 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6813 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6814 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 3e1e527 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6815 | return true; |
| 6816 | } |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6817 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6818 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6819 | switch (SequenceKind) { |
| 6820 | case FailedSequence: { |
| 6821 | OS << "Failed sequence: "; |
| 6822 | switch (Failure) { |
| 6823 | case FK_TooManyInitsForReference: |
| 6824 | OS << "too many initializers for reference"; |
| 6825 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6826 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6827 | case FK_ArrayNeedsInitList: |
| 6828 | OS << "array requires initializer list"; |
| 6829 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6830 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6831 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6832 | OS << "array requires initializer list or string literal"; |
| 6833 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6834 | |
Hans Wennborg | 8f62c5c | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6835 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6836 | OS << "array requires initializer list or wide string literal"; |
| 6837 | break; |
| 6838 | |
| 6839 | case FK_NarrowStringIntoWideCharArray: |
| 6840 | OS << "narrow string into wide char array"; |
| 6841 | break; |
| 6842 | |
| 6843 | case FK_WideStringIntoCharArray: |
| 6844 | OS << "wide string into char array"; |
| 6845 | break; |
| 6846 | |
| 6847 | case FK_IncompatWideStringIntoWideChar: |
| 6848 | OS << "incompatible wide string into wide char array"; |
| 6849 | break; |
| 6850 | |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6851 | case FK_ArrayTypeMismatch: |
| 6852 | OS << "array type mismatch"; |
| 6853 | break; |
| 6854 | |
| 6855 | case FK_NonConstantArrayInit: |
| 6856 | OS << "non-constant array initializer"; |
| 6857 | break; |
| 6858 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6859 | case FK_AddressOfOverloadFailed: |
| 6860 | OS << "address of overloaded function failed"; |
| 6861 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6862 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6863 | case FK_ReferenceInitOverloadFailed: |
| 6864 | OS << "overload resolution for reference initialization failed"; |
| 6865 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6866 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6867 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6868 | OS << "non-const lvalue reference bound to temporary"; |
| 6869 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6870 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6871 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6872 | OS << "non-const lvalue reference bound to unrelated type"; |
| 6873 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6874 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6875 | case FK_RValueReferenceBindingToLValue: |
| 6876 | OS << "rvalue reference bound to an lvalue"; |
| 6877 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6878 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6879 | case FK_ReferenceInitDropsQualifiers: |
| 6880 | OS << "reference initialization drops qualifiers"; |
| 6881 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6882 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6883 | case FK_ReferenceInitFailed: |
| 6884 | OS << "reference initialization failed"; |
| 6885 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6886 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6887 | case FK_ConversionFailed: |
| 6888 | OS << "conversion failed"; |
| 6889 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6890 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6891 | case FK_ConversionFromPropertyFailed: |
| 6892 | OS << "conversion from property failed"; |
| 6893 | break; |
| 6894 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6895 | case FK_TooManyInitsForScalar: |
| 6896 | OS << "too many initializers for scalar"; |
| 6897 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6898 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6899 | case FK_ReferenceBindingToInitList: |
| 6900 | OS << "referencing binding to initializer list"; |
| 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_InitListBadDestinationType: |
| 6904 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 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_UserConversionOverloadFailed: |
| 6908 | OS << "overloading failed for user-defined conversion"; |
| 6909 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6910 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6911 | case FK_ConstructorOverloadFailed: |
| 6912 | OS << "constructor overloading failed"; |
| 6913 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6914 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6915 | case FK_DefaultInitOfConst: |
| 6916 | OS << "default initialization of a const variable"; |
| 6917 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6918 | |
Douglas Gregor | 3f4f03a | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 6919 | case FK_Incomplete: |
| 6920 | OS << "initialization of incomplete type"; |
| 6921 | break; |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6922 | |
| 6923 | case FK_ListInitializationFailed: |
Sebastian Redl | b49c46c | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6924 | OS << "list initialization checker failure"; |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6925 | break; |
| 6926 | |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6927 | case FK_VariableLengthArrayHasInitializer: |
| 6928 | OS << "variable length array has an initializer"; |
| 6929 | break; |
| 6930 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6931 | case FK_PlaceholderType: |
| 6932 | OS << "initializer expression isn't contextually valid"; |
| 6933 | break; |
Nick Lewycky | 097f47c | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 6934 | |
| 6935 | case FK_ListConstructorOverloadFailed: |
| 6936 | OS << "list constructor overloading failed"; |
| 6937 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6938 | |
Sebastian Redl | 048a6d7 | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6939 | case FK_ExplicitConstructor: |
| 6940 | OS << "list copy initialization chose explicit constructor"; |
| 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 | OS << '\n'; |
| 6944 | return; |
| 6945 | } |
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 DependentSequence: |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6948 | OS << "Dependent sequence\n"; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6949 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6950 | |
Sebastian Redl | d201edf | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6951 | case NormalSequence: |
| 6952 | OS << "Normal sequence: "; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6953 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6954 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6955 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6956 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 6957 | if (S != step_begin()) { |
| 6958 | OS << " -> "; |
| 6959 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6960 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6961 | switch (S->Kind) { |
| 6962 | case SK_ResolveAddressOfOverloadedFunction: |
| 6963 | OS << "resolve address of overloaded function"; |
| 6964 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6965 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6966 | case SK_CastDerivedToBaseRValue: |
| 6967 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 6968 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6969 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6970 | case SK_CastDerivedToBaseXValue: |
| 6971 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 6972 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6973 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6974 | case SK_CastDerivedToBaseLValue: |
| 6975 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 6976 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6977 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6978 | case SK_BindReference: |
| 6979 | OS << "bind reference to lvalue"; |
| 6980 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6981 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6982 | case SK_BindReferenceToTemporary: |
| 6983 | OS << "bind reference to a temporary"; |
| 6984 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6985 | |
Douglas Gregor | c9cd64e | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6986 | case SK_ExtraneousCopyToTemporary: |
| 6987 | OS << "extraneous C++03 copy to temporary"; |
| 6988 | break; |
| 6989 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6990 | case SK_UserConversion: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6991 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6992 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6993 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6994 | case SK_QualificationConversionRValue: |
| 6995 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6996 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6997 | |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6998 | case SK_QualificationConversionXValue: |
| 6999 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7000 | break; |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 7001 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7002 | case SK_QualificationConversionLValue: |
| 7003 | OS << "qualification conversion (lvalue)"; |
| 7004 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7005 | |
Jordan Rose | b1312a5 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 7006 | case SK_LValueToRValue: |
| 7007 | OS << "load (lvalue to rvalue)"; |
| 7008 | break; |
| 7009 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7010 | case SK_ConversionSequence: |
| 7011 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 7012 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7013 | OS << ")"; |
| 7014 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7015 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7016 | case SK_ConversionSequenceNoNarrowing: |
| 7017 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 9f2ed47 | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 7018 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7019 | OS << ")"; |
| 7020 | break; |
| 7021 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7022 | case SK_ListInitialization: |
Sebastian Redl | 7de1fb4 | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 7023 | OS << "list aggregate initialization"; |
| 7024 | break; |
| 7025 | |
Sebastian Redl | 29526f0 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 7026 | case SK_UnwrapInitList: |
| 7027 | OS << "unwrap reference initializer list"; |
| 7028 | break; |
| 7029 | |
| 7030 | case SK_RewrapInitList: |
| 7031 | OS << "rewrap reference initializer list"; |
| 7032 | break; |
| 7033 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7034 | case SK_ConstructorInitialization: |
| 7035 | OS << "constructor initialization"; |
| 7036 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7037 | |
Richard Smith | 5332411 | 2014-07-16 21:33:43 +0000 | [diff] [blame] | 7038 | case SK_ConstructorInitializationFromList: |
| 7039 | OS << "list initialization via constructor"; |
| 7040 | break; |
| 7041 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7042 | case SK_ZeroInitialization: |
| 7043 | OS << "zero initialization"; |
| 7044 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7045 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7046 | case SK_CAssignment: |
| 7047 | OS << "C assignment"; |
| 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_StringInit: |
| 7051 | OS << "string initialization"; |
| 7052 | break; |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7053 | |
| 7054 | case SK_ObjCObjectConversion: |
| 7055 | OS << "Objective-C object conversion"; |
| 7056 | break; |
Douglas Gregor | e2f943b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 7057 | |
| 7058 | case SK_ArrayInit: |
| 7059 | OS << "array initialization"; |
| 7060 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7061 | |
Richard Smith | ebeed41 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 7062 | case SK_ParenthesizedArrayInit: |
| 7063 | OS << "parenthesized array initialization"; |
| 7064 | break; |
| 7065 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7066 | case SK_PassByIndirectCopyRestore: |
| 7067 | OS << "pass by indirect copy and restore"; |
| 7068 | break; |
| 7069 | |
| 7070 | case SK_PassByIndirectRestore: |
| 7071 | OS << "pass by indirect restore"; |
| 7072 | break; |
| 7073 | |
| 7074 | case SK_ProduceObjCObject: |
| 7075 | OS << "Objective-C object retension"; |
| 7076 | break; |
Sebastian Redl | c1839b1 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 7077 | |
| 7078 | case SK_StdInitializerList: |
| 7079 | OS << "std::initializer_list from initializer list"; |
| 7080 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7081 | |
Richard Smith | f8adcdc | 2014-07-17 05:12:35 +0000 | [diff] [blame^] | 7082 | case SK_StdInitializerListConstructorCall: |
| 7083 | OS << "list initialization from std::initializer_list"; |
| 7084 | break; |
| 7085 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 7086 | case SK_OCLSamplerInit: |
| 7087 | OS << "OpenCL sampler_t from integer constant"; |
| 7088 | break; |
| 7089 | |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7090 | case SK_OCLZeroEvent: |
| 7091 | OS << "OpenCL event_t from zero"; |
| 7092 | break; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7093 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 7094 | |
| 7095 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7096 | } |
Richard Smith | 6b21696 | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 7097 | |
| 7098 | OS << '\n'; |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 7099 | } |
| 7100 | |
| 7101 | void InitializationSequence::dump() const { |
| 7102 | dump(llvm::errs()); |
| 7103 | } |
| 7104 | |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7105 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 7106 | const ImplicitConversionSequence &ICS, |
| 7107 | QualType PreNarrowingType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7108 | QualType EntityType, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7109 | const Expr *PostInit) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 7110 | const StandardConversionSequence *SCS = nullptr; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7111 | switch (ICS.getKind()) { |
| 7112 | case ImplicitConversionSequence::StandardConversion: |
| 7113 | SCS = &ICS.Standard; |
| 7114 | break; |
| 7115 | case ImplicitConversionSequence::UserDefinedConversion: |
| 7116 | SCS = &ICS.UserDefined.After; |
| 7117 | break; |
| 7118 | case ImplicitConversionSequence::AmbiguousConversion: |
| 7119 | case ImplicitConversionSequence::EllipsisConversion: |
| 7120 | case ImplicitConversionSequence::BadConversion: |
| 7121 | return; |
| 7122 | } |
| 7123 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7124 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 7125 | APValue ConstantValue; |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 7126 | QualType ConstantType; |
| 7127 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 7128 | ConstantType)) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7129 | case NK_Not_Narrowing: |
| 7130 | // No narrowing occurred. |
| 7131 | return; |
| 7132 | |
| 7133 | case NK_Type_Narrowing: |
| 7134 | // This was a floating-to-integer conversion, which is always considered a |
| 7135 | // narrowing conversion even if the value is a constant and can be |
| 7136 | // represented exactly as an integer. |
| 7137 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7138 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7139 | ? diag::warn_init_list_type_narrowing |
| 7140 | : diag::ext_init_list_type_narrowing) |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7141 | << PostInit->getSourceRange() |
| 7142 | << PreNarrowingType.getLocalUnqualifiedType() |
| 7143 | << EntityType.getLocalUnqualifiedType(); |
| 7144 | break; |
| 7145 | |
| 7146 | case NK_Constant_Narrowing: |
| 7147 | // A constant value was narrowed. |
| 7148 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7149 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7150 | ? diag::warn_init_list_constant_narrowing |
| 7151 | : diag::ext_init_list_constant_narrowing) |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7152 | << PostInit->getSourceRange() |
Richard Smith | 5614ca7 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 7153 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 7423138 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 7154 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7155 | break; |
| 7156 | |
| 7157 | case NK_Variable_Narrowing: |
| 7158 | // A variable's value may have been narrowed. |
| 7159 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 16e1b07 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7160 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7161 | ? diag::warn_init_list_variable_narrowing |
| 7162 | : diag::ext_init_list_variable_narrowing) |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7163 | << PostInit->getSourceRange() |
| 7164 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 7423138 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 7165 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7166 | break; |
| 7167 | } |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7168 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 7169 | SmallString<128> StaticCast; |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7170 | llvm::raw_svector_ostream OS(StaticCast); |
| 7171 | OS << "static_cast<"; |
| 7172 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 7173 | // It's important to use the typedef's name if there is one so that the |
| 7174 | // fixit doesn't break code using types like int64_t. |
| 7175 | // |
| 7176 | // FIXME: This will break if the typedef requires qualification. But |
| 7177 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 7178 | OS << *TT->getDecl(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7179 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7180 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7181 | else { |
| 7182 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 7183 | // with a broken cast. |
| 7184 | return; |
| 7185 | } |
| 7186 | OS << ">("; |
Alp Toker | b086903 | 2014-05-17 01:13:18 +0000 | [diff] [blame] | 7187 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_silence) |
Alp Toker | b6cc592 | 2014-05-03 03:45:55 +0000 | [diff] [blame] | 7188 | << PostInit->getSourceRange() |
| 7189 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
| 7190 | << FixItHint::CreateInsertion( |
| 7191 | S.getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7192 | } |
| 7193 | |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7194 | //===----------------------------------------------------------------------===// |
| 7195 | // Initialization helper functions |
| 7196 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7197 | bool |
| 7198 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 7199 | ExprResult Init) { |
| 7200 | if (Init.isInvalid()) |
| 7201 | return false; |
| 7202 | |
| 7203 | Expr *InitE = Init.get(); |
| 7204 | assert(InitE && "No initialization expression"); |
| 7205 | |
Douglas Gregor | f4cc61d | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 7206 | InitializationKind Kind |
| 7207 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7208 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | c7ca587 | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 7209 | return !Seq.Failed(); |
Alexis Hunt | 1f69a02 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7210 | } |
| 7211 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7212 | ExprResult |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7213 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 7214 | SourceLocation EqualLoc, |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7215 | ExprResult Init, |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7216 | bool TopLevelOfInitList, |
| 7217 | bool AllowExplicit) { |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7218 | if (Init.isInvalid()) |
| 7219 | return ExprError(); |
| 7220 | |
John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7221 | Expr *InitE = Init.get(); |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7222 | assert(InitE && "No initialization expression?"); |
| 7223 | |
| 7224 | if (EqualLoc.isInvalid()) |
| 7225 | EqualLoc = InitE->getLocStart(); |
| 7226 | |
| 7227 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | 6073dca | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7228 | EqualLoc, |
| 7229 | AllowExplicit); |
Richard Smith | aaa0ec4 | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7230 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 7231 | Init.get(); |
Jeffrey Yasskin | a666781 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7232 | |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7233 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7234 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7235 | return Result; |
Douglas Gregor | e1314a6 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7236 | } |