Steve Naroff | 0cca749 | 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 | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 11 | // |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Designator.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
| 16 | #include "clang/Sema/Lookup.h" |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaInternal.h" |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeLoc.h" |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APInt.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 28 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 35 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 36 | ASTContext &Context) { |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 37 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 38 | return 0; |
| 39 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 40 | // See if this is a string literal or @encode. |
| 41 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 43 | // Handle @encode, which is a narrow string. |
| 44 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 45 | return Init; |
| 46 | |
| 47 | // Otherwise we can only handle string literals. |
| 48 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 220b636 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 49 | if (SL == 0) return 0; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 50 | |
| 51 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 52 | |
| 53 | switch (SL->getKind()) { |
| 54 | case StringLiteral::Ascii: |
| 55 | case StringLiteral::UTF8: |
| 56 | // char array can be initialized with a narrow string. |
| 57 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 58 | return ElemTy->isCharType() ? Init : 0; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 59 | case StringLiteral::UTF16: |
| 60 | return ElemTy->isChar16Type() ? Init : 0; |
| 61 | case StringLiteral::UTF32: |
| 62 | return ElemTy->isChar32Type() ? Init : 0; |
| 63 | case StringLiteral::Wide: |
| 64 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 65 | // correction from DR343): "An array with element type compatible with a |
| 66 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 67 | // string literal, optionally enclosed in braces." |
| 68 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 69 | ElemTy.getUnqualifiedType())) |
| 70 | return Init; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 71 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 72 | return 0; |
| 73 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 75 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 76 | } |
| 77 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 78 | static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { |
| 79 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 80 | if (!arrayType) return 0; |
| 81 | |
| 82 | return IsStringInit(init, arrayType, Context); |
| 83 | } |
| 84 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 85 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 86 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 87 | // Get the length of the string as parsed. |
| 88 | uint64_t StrLength = |
| 89 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 90 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 92 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 94 | // being initialized to a string literal. |
| 95 | llvm::APSInt ConstVal(32); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 96 | ConstVal = StrLength; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 97 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 98 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 99 | ConstVal, |
| 100 | ArrayType::Normal, 0); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 101 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 102 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 104 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 106 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 107 | // the size may be smaller or larger than the string we are initializing. |
| 108 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 109 | if (S.getLangOpts().CPlusPlus) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 110 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 111 | // For Pascal strings it's OK to strip off the terminating null character, |
| 112 | // so the example below is valid: |
| 113 | // |
| 114 | // unsigned char a[2] = "\pa"; |
| 115 | if (SL->isPascal()) |
| 116 | StrLength--; |
| 117 | } |
| 118 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 119 | // [dcl.init.string]p2 |
| 120 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 121 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 122 | diag::err_initializer_string_for_char_array_too_long) |
| 123 | << Str->getSourceRange(); |
| 124 | } else { |
| 125 | // C99 6.7.8p14. |
| 126 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 127 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 128 | diag::warn_initializer_string_for_char_array_too_long) |
| 129 | << Str->getSourceRange(); |
| 130 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 132 | // Set the type to the actual size that we are initializing. If we have |
| 133 | // something like: |
| 134 | // char x[1] = "foo"; |
| 135 | // then this will set the string literal's type to char[1]. |
| 136 | Str->setType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 139 | //===----------------------------------------------------------------------===// |
| 140 | // Semantic checking for initializer lists. |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 143 | /// @brief Semantic checking for initializer lists. |
| 144 | /// |
| 145 | /// The InitListChecker class contains a set of routines that each |
| 146 | /// handle the initialization of a certain kind of entity, e.g., |
| 147 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 148 | /// InitListChecker itself performs a recursive walk of the subobject |
| 149 | /// structure of the type to be initialized, while stepping through |
| 150 | /// the initializer list one element at a time. The IList and Index |
| 151 | /// parameters to each of the Check* routines contain the active |
| 152 | /// (syntactic) initializer list and the index into that initializer |
| 153 | /// list that represents the current initializer. Each routine is |
| 154 | /// responsible for moving that Index forward as it consumes elements. |
| 155 | /// |
| 156 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 157 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 158 | /// initializer list and the index into that initializer list where we |
| 159 | /// are copying initializers as we map them over to the semantic |
| 160 | /// list. Once we have completed our recursive walk of the subobject |
| 161 | /// structure, we will have constructed a full semantic initializer |
| 162 | /// list. |
| 163 | /// |
| 164 | /// C99 designators cause changes in the initializer list traversal, |
| 165 | /// because they make the initialization "jump" into a specific |
| 166 | /// subobject and then continue the initialization from that |
| 167 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 168 | /// designated subobject and manages backing out the recursion to |
| 169 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 170 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 171 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 172 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 173 | bool hadError; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 174 | bool VerifyOnly; // no diagnostics, no structure building |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 175 | bool AllowBraceElision; |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 176 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 177 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 179 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 180 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 181 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 182 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 183 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 184 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 185 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 186 | unsigned &StructuredIndex, |
| 187 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 188 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 189 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 191 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 192 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 193 | unsigned &StructuredIndex, |
| 194 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 195 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 196 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 197 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 198 | InitListExpr *StructuredList, |
| 199 | unsigned &StructuredIndex); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 200 | void CheckComplexType(const InitializedEntity &Entity, |
| 201 | InitListExpr *IList, QualType DeclType, |
| 202 | unsigned &Index, |
| 203 | InitListExpr *StructuredList, |
| 204 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 205 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 206 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 207 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 208 | InitListExpr *StructuredList, |
| 209 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 210 | void CheckReferenceType(const InitializedEntity &Entity, |
| 211 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 212 | unsigned &Index, |
| 213 | InitListExpr *StructuredList, |
| 214 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 215 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 216 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 217 | InitListExpr *StructuredList, |
| 218 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 219 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 220 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 222 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 223 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 224 | unsigned &StructuredIndex, |
| 225 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 226 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 227 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 229 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 230 | InitListExpr *StructuredList, |
| 231 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 232 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 233 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 234 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 236 | RecordDecl::field_iterator *NextField, |
| 237 | llvm::APSInt *NextElementIndex, |
| 238 | unsigned &Index, |
| 239 | InitListExpr *StructuredList, |
| 240 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 241 | bool FinishSubobjectInit, |
| 242 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 243 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 244 | QualType CurrentObjectType, |
| 245 | InitListExpr *StructuredList, |
| 246 | unsigned StructuredIndex, |
| 247 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 248 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 249 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 250 | Expr *expr); |
| 251 | int numArrayElements(QualType DeclType); |
| 252 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 253 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 254 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 255 | const InitializedEntity &ParentEntity, |
| 256 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 257 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 258 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 259 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 260 | Expr *InitExpr, FieldDecl *Field, |
| 261 | bool TopLevelObject); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 262 | void CheckValueInitializable(const InitializedEntity &Entity); |
| 263 | |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 264 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 265 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 266 | InitListExpr *IL, QualType &T, bool VerifyOnly, |
| 267 | bool AllowBraceElision); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 268 | bool HadError() { return hadError; } |
| 269 | |
| 270 | // @brief Retrieves the fully-structured initializer list used for |
| 271 | // semantic analysis and code generation. |
| 272 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 273 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 274 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 275 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 276 | void InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) { |
| 277 | assert(VerifyOnly && |
| 278 | "CheckValueInitializable is only inteded for verification mode."); |
| 279 | |
| 280 | SourceLocation Loc; |
| 281 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 282 | true); |
| 283 | InitializationSequence InitSeq(SemaRef, Entity, Kind, 0, 0); |
| 284 | if (InitSeq.Failed()) |
| 285 | hadError = true; |
| 286 | } |
| 287 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 288 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 289 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 290 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 291 | bool &RequiresSecondPass) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 292 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 293 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 294 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 295 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 296 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 297 | // FIXME: We probably don't need to handle references |
| 298 | // specially here, since value-initialization of references is |
| 299 | // handled in InitializationSequence. |
| 300 | if (Field->getType()->isReferenceType()) { |
| 301 | // C++ [dcl.init.aggr]p9: |
| 302 | // If an incomplete or empty initializer-list leaves a |
| 303 | // member of reference type uninitialized, the program is |
| 304 | // ill-formed. |
| 305 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 306 | << Field->getType() |
| 307 | << ILE->getSyntacticForm()->getSourceRange(); |
| 308 | SemaRef.Diag(Field->getLocation(), |
| 309 | diag::note_uninit_reference_member); |
| 310 | hadError = true; |
| 311 | return; |
| 312 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 313 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 314 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 315 | true); |
| 316 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 317 | if (!InitSeq) { |
| 318 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 319 | hadError = true; |
| 320 | return; |
| 321 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 322 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 323 | ExprResult MemberInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 324 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 325 | if (MemberInit.isInvalid()) { |
| 326 | hadError = true; |
| 327 | return; |
| 328 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 329 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 330 | if (hadError) { |
| 331 | // Do nothing |
| 332 | } else if (Init < NumInits) { |
| 333 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 334 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 335 | // Value-initialization requires a constructor call, so |
| 336 | // extend the initializer list to include the constructor |
| 337 | // call and make a note that we'll need to take another pass |
| 338 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 339 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 340 | RequiresSecondPass = true; |
| 341 | } |
| 342 | } else if (InitListExpr *InnerILE |
| 343 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 344 | FillInValueInitializations(MemberEntity, InnerILE, |
| 345 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 348 | /// Recursively replaces NULL values within the given initializer list |
| 349 | /// with expressions that perform value-initialization of the |
| 350 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 351 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 352 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 353 | InitListExpr *ILE, |
| 354 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 356 | "Should not have void type"); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 357 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 358 | if (ILE->getSyntacticForm()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 359 | Loc = ILE->getSyntacticForm()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 361 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 362 | if (RType->getDecl()->isUnion() && |
| 363 | ILE->getInitializedFieldInUnion()) |
| 364 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 365 | Entity, ILE, RequiresSecondPass); |
| 366 | else { |
| 367 | unsigned Init = 0; |
| 368 | for (RecordDecl::field_iterator |
| 369 | Field = RType->getDecl()->field_begin(), |
| 370 | FieldEnd = RType->getDecl()->field_end(); |
| 371 | Field != FieldEnd; ++Field) { |
| 372 | if (Field->isUnnamedBitfield()) |
| 373 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 374 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 375 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 376 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 377 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 378 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 379 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 380 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 381 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 382 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 383 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 384 | // Only look at the first initialization of a union. |
| 385 | if (RType->getDecl()->isUnion()) |
| 386 | break; |
| 387 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 392 | |
| 393 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 395 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 396 | unsigned NumInits = ILE->getNumInits(); |
| 397 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 398 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 399 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 400 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 401 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 402 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 403 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 404 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 405 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 406 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 407 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 408 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 410 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 412 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 413 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 414 | if (hadError) |
| 415 | return; |
| 416 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 417 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 418 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 419 | ElementEntity.setElementIndex(Init); |
| 420 | |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 421 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : 0); |
| 422 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 423 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 424 | true); |
| 425 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 426 | if (!InitSeq) { |
| 427 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 428 | hadError = true; |
| 429 | return; |
| 430 | } |
| 431 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 432 | ExprResult ElementInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 433 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 434 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 435 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 436 | return; |
| 437 | } |
| 438 | |
| 439 | if (hadError) { |
| 440 | // Do nothing |
| 441 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 442 | // For arrays, just set the expression used for value-initialization |
| 443 | // of the "holes" in the array. |
| 444 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 445 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 446 | else |
| 447 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 448 | } else { |
| 449 | // For arrays, just set the expression used for value-initialization |
| 450 | // of the rest of elements and exit. |
| 451 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 452 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 453 | return; |
| 454 | } |
| 455 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 456 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 457 | // Value-initialization requires a constructor call, so |
| 458 | // extend the initializer list to include the constructor |
| 459 | // call and make a note that we'll need to take another pass |
| 460 | // through the initializer list. |
| 461 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 462 | RequiresSecondPass = true; |
| 463 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 464 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 465 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 466 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 467 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 471 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 472 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 473 | InitListExpr *IL, QualType &T, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 474 | bool VerifyOnly, bool AllowBraceElision) |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 475 | : SemaRef(S), VerifyOnly(VerifyOnly), AllowBraceElision(AllowBraceElision) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 476 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 477 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 478 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 479 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 481 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 482 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 483 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 484 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 485 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 486 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 487 | bool RequiresSecondPass = false; |
| 488 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 489 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 490 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 491 | RequiresSecondPass); |
| 492 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 496 | // FIXME: use a proper constant |
| 497 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 498 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 499 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 500 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 501 | } |
| 502 | return maxElements; |
| 503 | } |
| 504 | |
| 505 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 506 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 507 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 509 | Field = structDecl->field_begin(), |
| 510 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 511 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 512 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 513 | ++InitializableMembers; |
| 514 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 515 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 516 | return std::min(InitializableMembers, 1); |
| 517 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 520 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 521 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 522 | QualType T, unsigned &Index, |
| 523 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 524 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 525 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 527 | if (T->isArrayType()) |
| 528 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 529 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 530 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 531 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 532 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 533 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 534 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 535 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 536 | if (maxElements == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 537 | if (!VerifyOnly) |
| 538 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 539 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 540 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 541 | hadError = true; |
| 542 | return; |
| 543 | } |
| 544 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 545 | // Build a structured initializer list corresponding to this subobject. |
| 546 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 548 | StructuredIndex, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 549 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 550 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 551 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 553 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 554 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 555 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 556 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 558 | StructuredSubobjectInitIndex); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 559 | |
| 560 | if (VerifyOnly) { |
| 561 | if (!AllowBraceElision && (T->isArrayType() || T->isRecordType())) |
| 562 | hadError = true; |
| 563 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 564 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 565 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 566 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 567 | // Update the structured sub-object initializer so that it's ending |
| 568 | // range corresponds with the end of the last initializer it used. |
| 569 | if (EndIndex < ParentIList->getNumInits()) { |
| 570 | SourceLocation EndLoc |
| 571 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 572 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 573 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 574 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 575 | // Complain about missing braces. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 576 | if (T->isArrayType() || T->isRecordType()) { |
| 577 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 578 | AllowBraceElision ? diag::warn_missing_braces : |
| 579 | diag::err_missing_braces) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 580 | << StructuredSubobjectInitList->getSourceRange() |
| 581 | << FixItHint::CreateInsertion( |
| 582 | StructuredSubobjectInitList->getLocStart(), "{") |
| 583 | << FixItHint::CreateInsertion( |
| 584 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 585 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 586 | "}"); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 587 | if (!AllowBraceElision) |
| 588 | hadError = true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 589 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 590 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 593 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 594 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 595 | unsigned &Index, |
| 596 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 597 | unsigned &StructuredIndex, |
| 598 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 599 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 600 | if (!VerifyOnly) { |
| 601 | SyntacticToSemantic[IList] = StructuredList; |
| 602 | StructuredList->setSyntacticForm(IList); |
| 603 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 604 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 605 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 606 | if (!VerifyOnly) { |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 607 | QualType ExprTy = T; |
| 608 | if (!ExprTy->isArrayType()) |
| 609 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 610 | IList->setType(ExprTy); |
| 611 | StructuredList->setType(ExprTy); |
| 612 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 613 | if (hadError) |
| 614 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 615 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 616 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 617 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 618 | if (VerifyOnly) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 619 | if (SemaRef.getLangOpts().CPlusPlus || |
| 620 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 621 | IList->getType()->isVectorType())) { |
| 622 | hadError = true; |
| 623 | } |
| 624 | return; |
| 625 | } |
| 626 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 627 | if (StructuredIndex == 1 && |
| 628 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 629 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 630 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 631 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 632 | hadError = true; |
| 633 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 634 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 635 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 636 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 637 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 638 | // Don't complain for incomplete types, since we'll get an error |
| 639 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 640 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 642 | CurrentObjectType->isArrayType()? 0 : |
| 643 | CurrentObjectType->isVectorType()? 1 : |
| 644 | CurrentObjectType->isScalarType()? 2 : |
| 645 | CurrentObjectType->isUnionType()? 3 : |
| 646 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 647 | |
| 648 | unsigned DK = diag::warn_excess_initializers; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 649 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 650 | DK = diag::err_excess_initializers; |
| 651 | hadError = true; |
| 652 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 653 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 654 | DK = diag::err_excess_initializers; |
| 655 | hadError = true; |
| 656 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 658 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 659 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 662 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 663 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 664 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 665 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 666 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 667 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 668 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 671 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 672 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 674 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 675 | unsigned &Index, |
| 676 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 677 | unsigned &StructuredIndex, |
| 678 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 679 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 680 | // Explicitly braced initializer for complex type can be real+imaginary |
| 681 | // parts. |
| 682 | CheckComplexType(Entity, IList, DeclType, Index, |
| 683 | StructuredList, StructuredIndex); |
| 684 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 685 | CheckScalarType(Entity, IList, DeclType, Index, |
| 686 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 687 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 688 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 689 | StructuredList, StructuredIndex); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 690 | } else if (DeclType->isRecordType()) { |
| 691 | assert(DeclType->isAggregateType() && |
| 692 | "non-aggregate records should be handed in CheckSubElementType"); |
| 693 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 694 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 695 | SubobjectIsDesignatorContext, Index, |
| 696 | StructuredList, StructuredIndex, |
| 697 | TopLevelObject); |
| 698 | } else if (DeclType->isArrayType()) { |
| 699 | llvm::APSInt Zero( |
| 700 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 701 | false); |
| 702 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 703 | SubobjectIsDesignatorContext, Index, |
| 704 | StructuredList, StructuredIndex); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 705 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 706 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 707 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 708 | if (!VerifyOnly) |
| 709 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 710 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 711 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 712 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 713 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 714 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 715 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 716 | if (!VerifyOnly) |
| 717 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 718 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 719 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 720 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 721 | if (!VerifyOnly) |
| 722 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 723 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 724 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 728 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 729 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 731 | unsigned &Index, |
| 732 | InitListExpr *StructuredList, |
| 733 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 734 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 735 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 736 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
| 737 | unsigned newIndex = 0; |
| 738 | unsigned newStructuredIndex = 0; |
| 739 | InitListExpr *newStructuredList |
| 740 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 741 | StructuredList, StructuredIndex, |
| 742 | SubInitList->getSourceRange()); |
| 743 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
| 744 | newStructuredList, newStructuredIndex); |
| 745 | ++StructuredIndex; |
| 746 | ++Index; |
| 747 | return; |
| 748 | } |
| 749 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 750 | "non-aggregate records are only possible in C++"); |
| 751 | // C++ initialization is handled later. |
| 752 | } |
| 753 | |
| 754 | if (ElemType->isScalarType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 755 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 756 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 757 | } else if (ElemType->isReferenceType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 758 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 759 | StructuredList, StructuredIndex); |
| 760 | } |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 761 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 762 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 763 | // arrayType can be incomplete if we're initializing a flexible |
| 764 | // array member. There's nothing we can do with the completed |
| 765 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 766 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 767 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 768 | if (!VerifyOnly) { |
| 769 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 770 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 771 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 772 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 773 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 774 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 775 | |
| 776 | // Fall through for subaggregate initialization. |
| 777 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 778 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 779 | // C++ [dcl.init.aggr]p12: |
| 780 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 781 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 782 | // an initializer-list. If the initializer can initialize a |
| 783 | // member, the member is initialized. [...] |
| 784 | |
| 785 | // FIXME: Better EqualLoc? |
| 786 | InitializationKind Kind = |
| 787 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 788 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 789 | |
| 790 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 791 | if (!VerifyOnly) { |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 792 | ExprResult Result = |
| 793 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 794 | if (Result.isInvalid()) |
| 795 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 796 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 797 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 798 | Result.takeAs<Expr>()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 799 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 800 | ++Index; |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | // Fall through for subaggregate initialization |
| 805 | } else { |
| 806 | // C99 6.7.8p13: |
| 807 | // |
| 808 | // The initializer for a structure or union object that has |
| 809 | // automatic storage duration shall be either an initializer |
| 810 | // list as described below, or a single expression that has |
| 811 | // compatible structure or union type. In the latter case, the |
| 812 | // initial value of the object, including unnamed members, is |
| 813 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 814 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 815 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 816 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 817 | !VerifyOnly) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 818 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 819 | if (ExprRes.isInvalid()) |
| 820 | hadError = true; |
| 821 | else { |
| 822 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 823 | if (ExprRes.isInvalid()) |
| 824 | hadError = true; |
| 825 | } |
| 826 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 827 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 828 | ++Index; |
| 829 | return; |
| 830 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 831 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 832 | // Fall through for subaggregate initialization |
| 833 | } |
| 834 | |
| 835 | // C++ [dcl.init.aggr]p12: |
| 836 | // |
| 837 | // [...] Otherwise, if the member is itself a non-empty |
| 838 | // subaggregate, brace elision is assumed and the initializer is |
| 839 | // considered for the initialization of the first member of |
| 840 | // the subaggregate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 841 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 842 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 843 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 844 | StructuredIndex); |
| 845 | ++StructuredIndex; |
| 846 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 847 | if (!VerifyOnly) { |
| 848 | // We cannot initialize this element, so let |
| 849 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 850 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 851 | SemaRef.Owned(expr), |
| 852 | /*TopLevelOfInitList=*/true); |
| 853 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 854 | hadError = true; |
| 855 | ++Index; |
| 856 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 857 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 860 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 861 | InitListExpr *IList, QualType DeclType, |
| 862 | unsigned &Index, |
| 863 | InitListExpr *StructuredList, |
| 864 | unsigned &StructuredIndex) { |
| 865 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 866 | |
| 867 | // As an extension, clang supports complex initializers, which initialize |
| 868 | // a complex number component-wise. When an explicit initializer list for |
| 869 | // a complex number contains two two initializers, this extension kicks in: |
| 870 | // it exepcts the initializer list to contain two elements convertible to |
| 871 | // the element type of the complex type. The first element initializes |
| 872 | // the real part, and the second element intitializes the imaginary part. |
| 873 | |
| 874 | if (IList->getNumInits() != 2) |
| 875 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 876 | StructuredIndex); |
| 877 | |
| 878 | // This is an extension in C. (The builtin _Complex type does not exist |
| 879 | // in the C++ standard.) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 880 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 881 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 882 | << IList->getSourceRange(); |
| 883 | |
| 884 | // Initialize the complex number. |
| 885 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 886 | InitializedEntity ElementEntity = |
| 887 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 888 | |
| 889 | for (unsigned i = 0; i < 2; ++i) { |
| 890 | ElementEntity.setElementIndex(Index); |
| 891 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 892 | StructuredList, StructuredIndex); |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 897 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 898 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 899 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 900 | InitListExpr *StructuredList, |
| 901 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 902 | if (Index >= IList->getNumInits()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 903 | if (!VerifyOnly) |
| 904 | SemaRef.Diag(IList->getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 905 | SemaRef.getLangOpts().CPlusPlus0x ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 906 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 907 | diag::err_empty_scalar_initializer) |
| 908 | << IList->getSourceRange(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 909 | hadError = !SemaRef.getLangOpts().CPlusPlus0x; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 910 | ++Index; |
| 911 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 912 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 913 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 914 | |
| 915 | Expr *expr = IList->getInit(Index); |
| 916 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 917 | if (!VerifyOnly) |
| 918 | SemaRef.Diag(SubIList->getLocStart(), |
| 919 | diag::warn_many_braces_around_scalar_init) |
| 920 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 921 | |
| 922 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 923 | StructuredIndex); |
| 924 | return; |
| 925 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 926 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 927 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 928 | diag::err_designator_for_scalar_init) |
| 929 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 930 | hadError = true; |
| 931 | ++Index; |
| 932 | ++StructuredIndex; |
| 933 | return; |
| 934 | } |
| 935 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 936 | if (VerifyOnly) { |
| 937 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 938 | hadError = true; |
| 939 | ++Index; |
| 940 | return; |
| 941 | } |
| 942 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 943 | ExprResult Result = |
| 944 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 945 | SemaRef.Owned(expr), |
| 946 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 947 | |
| 948 | Expr *ResultExpr = 0; |
| 949 | |
| 950 | if (Result.isInvalid()) |
| 951 | hadError = true; // types weren't compatible. |
| 952 | else { |
| 953 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 954 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 955 | if (ResultExpr != expr) { |
| 956 | // The type was promoted, update initializer list. |
| 957 | IList->setInit(Index, ResultExpr); |
| 958 | } |
| 959 | } |
| 960 | if (hadError) |
| 961 | ++StructuredIndex; |
| 962 | else |
| 963 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 964 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 967 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 968 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 969 | unsigned &Index, |
| 970 | InitListExpr *StructuredList, |
| 971 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 972 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 973 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 974 | // general, it would be useful to pass location information down the stack, |
| 975 | // so that we know the location (or decl) of the "current object" being |
| 976 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 977 | if (!VerifyOnly) |
| 978 | SemaRef.Diag(IList->getLocStart(), |
| 979 | diag::err_init_reference_member_uninitialized) |
| 980 | << DeclType |
| 981 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 982 | hadError = true; |
| 983 | ++Index; |
| 984 | ++StructuredIndex; |
| 985 | return; |
| 986 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 987 | |
| 988 | Expr *expr = IList->getInit(Index); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 989 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus0x) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 990 | if (!VerifyOnly) |
| 991 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 992 | << DeclType << IList->getSourceRange(); |
| 993 | hadError = true; |
| 994 | ++Index; |
| 995 | ++StructuredIndex; |
| 996 | return; |
| 997 | } |
| 998 | |
| 999 | if (VerifyOnly) { |
| 1000 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1001 | hadError = true; |
| 1002 | ++Index; |
| 1003 | return; |
| 1004 | } |
| 1005 | |
| 1006 | ExprResult Result = |
| 1007 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1008 | SemaRef.Owned(expr), |
| 1009 | /*TopLevelOfInitList=*/true); |
| 1010 | |
| 1011 | if (Result.isInvalid()) |
| 1012 | hadError = true; |
| 1013 | |
| 1014 | expr = Result.takeAs<Expr>(); |
| 1015 | IList->setInit(Index, expr); |
| 1016 | |
| 1017 | if (hadError) |
| 1018 | ++StructuredIndex; |
| 1019 | else |
| 1020 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1021 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1024 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1025 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1026 | unsigned &Index, |
| 1027 | InitListExpr *StructuredList, |
| 1028 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1029 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1030 | unsigned maxElements = VT->getNumElements(); |
| 1031 | unsigned numEltsInit = 0; |
| 1032 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1033 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1034 | if (Index >= IList->getNumInits()) { |
| 1035 | // Make sure the element type can be value-initialized. |
| 1036 | if (VerifyOnly) |
| 1037 | CheckValueInitializable( |
| 1038 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1039 | return; |
| 1040 | } |
| 1041 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1042 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1043 | // If the initializing element is a vector, try to copy-initialize |
| 1044 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1045 | Expr *Init = IList->getInit(Index); |
| 1046 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1047 | if (VerifyOnly) { |
| 1048 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1049 | hadError = true; |
| 1050 | ++Index; |
| 1051 | return; |
| 1052 | } |
| 1053 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1054 | ExprResult Result = |
| 1055 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1056 | SemaRef.Owned(Init), |
| 1057 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1058 | |
| 1059 | Expr *ResultExpr = 0; |
| 1060 | if (Result.isInvalid()) |
| 1061 | hadError = true; // types weren't compatible. |
| 1062 | else { |
| 1063 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1064 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1065 | if (ResultExpr != Init) { |
| 1066 | // The type was promoted, update initializer list. |
| 1067 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1068 | } |
| 1069 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1070 | if (hadError) |
| 1071 | ++StructuredIndex; |
| 1072 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1073 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1074 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1075 | ++Index; |
| 1076 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1077 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1079 | InitializedEntity ElementEntity = |
| 1080 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1081 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1082 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1083 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1084 | if (Index >= IList->getNumInits()) { |
| 1085 | if (VerifyOnly) |
| 1086 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1087 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1088 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1089 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1090 | ElementEntity.setElementIndex(Index); |
| 1091 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1092 | StructuredList, StructuredIndex); |
| 1093 | } |
| 1094 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1095 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1096 | |
| 1097 | InitializedEntity ElementEntity = |
| 1098 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1099 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1100 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1101 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1102 | // Don't attempt to go past the end of the init list |
| 1103 | if (Index >= IList->getNumInits()) |
| 1104 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1105 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1106 | ElementEntity.setElementIndex(Index); |
| 1107 | |
| 1108 | QualType IType = IList->getInit(Index)->getType(); |
| 1109 | if (!IType->isVectorType()) { |
| 1110 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1111 | StructuredList, StructuredIndex); |
| 1112 | ++numEltsInit; |
| 1113 | } else { |
| 1114 | QualType VecType; |
| 1115 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1116 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1117 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1118 | if (IType->isExtVectorType()) |
| 1119 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1120 | else |
| 1121 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1122 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1123 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1124 | StructuredList, StructuredIndex); |
| 1125 | numEltsInit += numIElts; |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1130 | if (numEltsInit != maxElements) { |
| 1131 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1132 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1133 | diag::err_vector_incorrect_num_initializers) |
| 1134 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1135 | hadError = true; |
| 1136 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1139 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1140 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1141 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1143 | unsigned &Index, |
| 1144 | InitListExpr *StructuredList, |
| 1145 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1146 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1147 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1148 | // Check for the special-case of initializing an array with a string. |
| 1149 | if (Index < IList->getNumInits()) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1150 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1151 | SemaRef.Context)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1152 | // We place the string literal directly into the resulting |
| 1153 | // initializer list. This is the only place where the structure |
| 1154 | // of the structured initializer list doesn't match exactly, |
| 1155 | // because doing so would involve allocating one character |
| 1156 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1157 | if (!VerifyOnly) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1158 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1159 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 1160 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1161 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1162 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1163 | return; |
| 1164 | } |
| 1165 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1166 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1167 | // Check for VLAs; in standard C it would be possible to check this |
| 1168 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1169 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1170 | if (!VerifyOnly) |
| 1171 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1172 | diag::err_variable_object_no_init) |
| 1173 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1174 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1175 | ++Index; |
| 1176 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1177 | return; |
| 1178 | } |
| 1179 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1180 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1181 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1182 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1183 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1184 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1185 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1186 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1187 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1188 | maxElementsKnown = true; |
| 1189 | } |
| 1190 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1191 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1192 | while (Index < IList->getNumInits()) { |
| 1193 | Expr *Init = IList->getInit(Index); |
| 1194 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1195 | // If we're not the subobject that matches up with the '{' for |
| 1196 | // the designator, we shouldn't be handling the |
| 1197 | // designator. Return immediately. |
| 1198 | if (!SubobjectIsDesignatorContext) |
| 1199 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1200 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1201 | // Handle this designated initializer. elementIndex will be |
| 1202 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1203 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1204 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1205 | StructuredList, StructuredIndex, true, |
| 1206 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1207 | hadError = true; |
| 1208 | continue; |
| 1209 | } |
| 1210 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1211 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1212 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1213 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1214 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1215 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1216 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1217 | // If the array is of incomplete type, keep track of the number of |
| 1218 | // elements in the initializer. |
| 1219 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1220 | maxElements = elementIndex; |
| 1221 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1222 | continue; |
| 1223 | } |
| 1224 | |
| 1225 | // If we know the maximum number of elements, and we've already |
| 1226 | // hit it, stop consuming elements in the initializer list. |
| 1227 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1228 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1229 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1230 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1231 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1232 | Entity); |
| 1233 | // Check this element. |
| 1234 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1235 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1236 | ++elementIndex; |
| 1237 | |
| 1238 | // If the array is of incomplete type, keep track of the number of |
| 1239 | // elements in the initializer. |
| 1240 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1241 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1242 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1243 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1244 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1245 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1246 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1247 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1248 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1249 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1250 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1251 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1252 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1253 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1255 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1256 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1257 | if (!hadError && VerifyOnly) { |
| 1258 | // Check if there are any members of the array that get value-initialized. |
| 1259 | // If so, check if doing that is possible. |
| 1260 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1261 | if (maxElementsKnown && elementIndex < maxElements) |
| 1262 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1263 | SemaRef.Context, 0, Entity)); |
| 1264 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1267 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1268 | Expr *InitExpr, |
| 1269 | FieldDecl *Field, |
| 1270 | bool TopLevelObject) { |
| 1271 | // Handle GNU flexible array initializers. |
| 1272 | unsigned FlexArrayDiag; |
| 1273 | if (isa<InitListExpr>(InitExpr) && |
| 1274 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1275 | // Empty flexible array init always allowed as an extension |
| 1276 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1277 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1278 | // Disallow flexible array init in C++; it is not required for gcc |
| 1279 | // compatibility, and it needs work to IRGen correctly in general. |
| 1280 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1281 | } else if (!TopLevelObject) { |
| 1282 | // Disallow flexible array init on non-top-level object |
| 1283 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1284 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1285 | // Disallow flexible array init on anything which is not a variable. |
| 1286 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1287 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1288 | // Disallow flexible array init on local variables. |
| 1289 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1290 | } else { |
| 1291 | // Allow other cases. |
| 1292 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1293 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1294 | |
| 1295 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1296 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1297 | FlexArrayDiag) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1298 | << InitExpr->getLocStart(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1299 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1300 | << Field; |
| 1301 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1302 | |
| 1303 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1304 | } |
| 1305 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1306 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1307 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1309 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1311 | unsigned &Index, |
| 1312 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1313 | unsigned &StructuredIndex, |
| 1314 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1315 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1317 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1318 | // confusion, we forgo checking the intializer for the entire record. |
| 1319 | if (structDecl->isInvalidDecl()) { |
| 1320 | hadError = true; |
| 1321 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1323 | |
| 1324 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1325 | // Value-initialize the first named member of the union. |
| 1326 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 1327 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1328 | Field != FieldEnd; ++Field) { |
| 1329 | if (Field->getDeclName()) { |
| 1330 | if (VerifyOnly) |
| 1331 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1332 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1333 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1334 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1335 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | return; |
| 1339 | } |
| 1340 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1341 | // If structDecl is a forward declaration, this loop won't do |
| 1342 | // anything except look at designated initializers; That's okay, |
| 1343 | // because an error should get printed out elsewhere. It might be |
| 1344 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1345 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1346 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1347 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1348 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1349 | while (Index < IList->getNumInits()) { |
| 1350 | Expr *Init = IList->getInit(Index); |
| 1351 | |
| 1352 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1353 | // If we're not the subobject that matches up with the '{' for |
| 1354 | // the designator, we shouldn't be handling the |
| 1355 | // designator. Return immediately. |
| 1356 | if (!SubobjectIsDesignatorContext) |
| 1357 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1358 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1359 | // Handle this designated initializer. Field will be updated to |
| 1360 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1361 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1362 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1363 | StructuredList, StructuredIndex, |
| 1364 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1365 | hadError = true; |
| 1366 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1367 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1368 | |
| 1369 | // Disable check for missing fields when designators are used. |
| 1370 | // This matches gcc behaviour. |
| 1371 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1372 | continue; |
| 1373 | } |
| 1374 | |
| 1375 | if (Field == FieldEnd) { |
| 1376 | // We've run out of fields. We're done. |
| 1377 | break; |
| 1378 | } |
| 1379 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1380 | // We've already initialized a member of a union. We're done. |
| 1381 | if (InitializedSomething && DeclType->isUnionType()) |
| 1382 | break; |
| 1383 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1384 | // If we've hit the flexible array member at the end, we're done. |
| 1385 | if (Field->getType()->isIncompleteArrayType()) |
| 1386 | break; |
| 1387 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1388 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1389 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1390 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1391 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1392 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1393 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1394 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1395 | bool InvalidUse; |
| 1396 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1397 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1398 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1399 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1400 | IList->getInit(Index)->getLocStart()); |
| 1401 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1402 | ++Index; |
| 1403 | ++Field; |
| 1404 | hadError = true; |
| 1405 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1406 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1407 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1408 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1409 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1410 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1411 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1412 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1413 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1414 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1415 | // Initialize the first field within the union. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1416 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1417 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1418 | |
| 1419 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1420 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1421 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1422 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1423 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1424 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1425 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1426 | // It is possible we have one or more unnamed bitfields remaining. |
| 1427 | // Find first (if any) named field and emit warning. |
| 1428 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1429 | it != end; ++it) { |
| 1430 | if (!it->isUnnamedBitfield()) { |
| 1431 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1432 | diag::warn_missing_field_initializers) << it->getName(); |
| 1433 | break; |
| 1434 | } |
| 1435 | } |
| 1436 | } |
| 1437 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1438 | // Check that any remaining fields can be value-initialized. |
| 1439 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1440 | !Field->getType()->isIncompleteArrayType()) { |
| 1441 | // FIXME: Should check for holes left by designated initializers too. |
| 1442 | for (; Field != FieldEnd && !hadError; ++Field) { |
| 1443 | if (!Field->isUnnamedBitfield()) |
| 1444 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1445 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1450 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1451 | return; |
| 1452 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1453 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1454 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1455 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1456 | ++Index; |
| 1457 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1460 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1461 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1462 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1463 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1464 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1465 | StructuredList, StructuredIndex); |
| 1466 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1467 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1468 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1469 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1470 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1471 | /// \brief Expand a field designator that refers to a member of an |
| 1472 | /// anonymous struct or union into a series of field designators that |
| 1473 | /// refers to the field within the appropriate subobject. |
| 1474 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1475 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | DesignatedInitExpr *DIE, |
| 1477 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1478 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1479 | typedef DesignatedInitExpr::Designator Designator; |
| 1480 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1481 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1482 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1483 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1484 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1485 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1487 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1488 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1489 | else |
| 1490 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1491 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1492 | assert(isa<FieldDecl>(*PI)); |
| 1493 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | // Expand the current designator into the set of replacement |
| 1497 | // designators, so we have a full subobject path down to where the |
| 1498 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1499 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1500 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1501 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1503 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1504 | /// corresponds to FieldName. |
| 1505 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1506 | IdentifierInfo *FieldName) { |
| 1507 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1508 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 3e78b19 | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1509 | while (IndirectFieldDecl *IF = |
| 1510 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1511 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1512 | return IF; |
| 1513 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1514 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1515 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1518 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1519 | DesignatedInitExpr *DIE) { |
| 1520 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1521 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1522 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1523 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1524 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
| 1525 | DIE->size(), IndexExprs.data(), |
| 1526 | NumIndexExprs, DIE->getEqualOrColonLoc(), |
| 1527 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1528 | } |
| 1529 | |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1530 | namespace { |
| 1531 | |
| 1532 | // Callback to only accept typo corrections that are for field members of |
| 1533 | // the given struct or union. |
| 1534 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1535 | public: |
| 1536 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1537 | : Record(RD) {} |
| 1538 | |
| 1539 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 1540 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1541 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1542 | } |
| 1543 | |
| 1544 | private: |
| 1545 | RecordDecl *Record; |
| 1546 | }; |
| 1547 | |
| 1548 | } |
| 1549 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1550 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1551 | /// |
| 1552 | /// Determines whether the designated initializer @p DIE, which |
| 1553 | /// resides at the given @p Index within the initializer list @p |
| 1554 | /// IList, is well-formed for a current object of type @p DeclType |
| 1555 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1557 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1558 | /// |
| 1559 | /// @param IList The initializer list in which this designated |
| 1560 | /// initializer occurs. |
| 1561 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1562 | /// @param DIE The designated initializer expression. |
| 1563 | /// |
| 1564 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1565 | /// |
| 1566 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1567 | /// into which the designation in @p DIE should refer. |
| 1568 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1569 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1570 | /// a field, this will be set to the field declaration corresponding |
| 1571 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1572 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1573 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1574 | /// DIE is an array designator or GNU array-range designator, this |
| 1575 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1576 | /// |
| 1577 | /// @param Index Index into @p IList where the designated initializer |
| 1578 | /// @p DIE occurs. |
| 1579 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1580 | /// @param StructuredList The initializer list expression that |
| 1581 | /// describes all of the subobject initializers in the order they'll |
| 1582 | /// actually be initialized. |
| 1583 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1584 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1586 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1587 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1588 | DesignatedInitExpr *DIE, |
| 1589 | unsigned DesigIdx, |
| 1590 | QualType &CurrentObjectType, |
| 1591 | RecordDecl::field_iterator *NextField, |
| 1592 | llvm::APSInt *NextElementIndex, |
| 1593 | unsigned &Index, |
| 1594 | InitListExpr *StructuredList, |
| 1595 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1596 | bool FinishSubobjectInit, |
| 1597 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1598 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1599 | // Check the actual initialization for the designated object type. |
| 1600 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1601 | |
| 1602 | // Temporarily remove the designator expression from the |
| 1603 | // initializer list that the child calls see, so that we don't try |
| 1604 | // to re-process the designator. |
| 1605 | unsigned OldIndex = Index; |
| 1606 | IList->setInit(OldIndex, DIE->getInit()); |
| 1607 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1608 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1609 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1610 | |
| 1611 | // Restore the designated initializer expression in the syntactic |
| 1612 | // form of the initializer list. |
| 1613 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1614 | DIE->setInit(IList->getInit(OldIndex)); |
| 1615 | IList->setInit(OldIndex, DIE); |
| 1616 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1617 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1620 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1621 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1622 | if (!VerifyOnly) { |
| 1623 | assert((IsFirstDesignator || StructuredList) && |
| 1624 | "Need a non-designated initializer list to start from"); |
| 1625 | |
| 1626 | // Determine the structural initializer list that corresponds to the |
| 1627 | // current subobject. |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1628 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1629 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1630 | StructuredList, StructuredIndex, |
| 1631 | SourceRange(D->getStartLocation(), |
| 1632 | DIE->getSourceRange().getEnd())); |
| 1633 | assert(StructuredList && "Expected a structured initializer list"); |
| 1634 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1635 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1636 | if (D->isFieldDesignator()) { |
| 1637 | // C99 6.7.8p7: |
| 1638 | // |
| 1639 | // If a designator has the form |
| 1640 | // |
| 1641 | // . identifier |
| 1642 | // |
| 1643 | // then the current object (defined below) shall have |
| 1644 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1646 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1647 | if (!RT) { |
| 1648 | SourceLocation Loc = D->getDotLoc(); |
| 1649 | if (Loc.isInvalid()) |
| 1650 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1651 | if (!VerifyOnly) |
| 1652 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1653 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1654 | ++Index; |
| 1655 | return true; |
| 1656 | } |
| 1657 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1658 | // Note: we perform a linear search of the fields here, despite |
| 1659 | // the fact that we have a faster lookup method, because we always |
| 1660 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1661 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1662 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1663 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1664 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1665 | Field = RT->getDecl()->field_begin(), |
| 1666 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1667 | for (; Field != FieldEnd; ++Field) { |
| 1668 | if (Field->isUnnamedBitfield()) |
| 1669 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1670 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1671 | // If we find a field representing an anonymous field, look in the |
| 1672 | // IndirectFieldDecl that follow for the designated initializer. |
| 1673 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1674 | if (IndirectFieldDecl *IF = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1675 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1676 | // In verify mode, don't modify the original. |
| 1677 | if (VerifyOnly) |
| 1678 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1679 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1680 | D = DIE->getDesignator(DesigIdx); |
| 1681 | break; |
| 1682 | } |
| 1683 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1684 | if (KnownField && KnownField == *Field) |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1685 | break; |
| 1686 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1687 | break; |
| 1688 | |
| 1689 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1692 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1693 | if (VerifyOnly) { |
| 1694 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1695 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1696 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1697 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1698 | // There was no normal field in the struct with the designated |
| 1699 | // name. Perform another lookup for this name, which may find |
| 1700 | // something that we can't designate (e.g., a member function), |
| 1701 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1702 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1703 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1704 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1705 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1706 | // Name lookup didn't find anything. Determine whether this |
| 1707 | // was a typo for another field name. |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1708 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1709 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1710 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1711 | Sema::LookupMemberName, /*Scope=*/0, /*SS=*/0, Validator, |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1712 | RT->getDecl()); |
| 1713 | if (Corrected) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1714 | std::string CorrectedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1715 | Corrected.getAsString(SemaRef.getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1716 | std::string CorrectedQuotedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1717 | Corrected.getQuoted(SemaRef.getLangOpts())); |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1718 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1719 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1720 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1721 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1722 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1723 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1724 | diag::note_previous_decl) << CorrectedQuotedStr; |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1725 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1726 | } else { |
| 1727 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1728 | << FieldName << CurrentObjectType; |
| 1729 | ++Index; |
| 1730 | return true; |
| 1731 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1732 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1733 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1734 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1735 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1736 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1737 | << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1739 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1740 | ++Index; |
| 1741 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1742 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1743 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1744 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1745 | // The replacement field comes from typo correction; find it |
| 1746 | // in the list of fields. |
| 1747 | FieldIndex = 0; |
| 1748 | Field = RT->getDecl()->field_begin(); |
| 1749 | for (; Field != FieldEnd; ++Field) { |
| 1750 | if (Field->isUnnamedBitfield()) |
| 1751 | continue; |
| 1752 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1753 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1754 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1755 | break; |
| 1756 | |
| 1757 | ++FieldIndex; |
| 1758 | } |
| 1759 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1760 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1761 | |
| 1762 | // All of the fields of a union are located at the same place in |
| 1763 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1764 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1765 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1766 | if (!VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1767 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1768 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1769 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1770 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1771 | bool InvalidUse; |
| 1772 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1773 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1774 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1775 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1776 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1777 | ++Index; |
| 1778 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1779 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1780 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1781 | if (!VerifyOnly) { |
| 1782 | // Update the designator with the field declaration. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1783 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1785 | // Make sure that our non-designated initializer list has space |
| 1786 | // for a subobject corresponding to this field. |
| 1787 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1788 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1789 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1790 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1791 | // This designator names a flexible array member. |
| 1792 | if (Field->getType()->isIncompleteArrayType()) { |
| 1793 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1794 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1795 | // We can't designate an object within the flexible array |
| 1796 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1797 | if (!VerifyOnly) { |
| 1798 | DesignatedInitExpr::Designator *NextD |
| 1799 | = DIE->getDesignator(DesigIdx + 1); |
| 1800 | SemaRef.Diag(NextD->getStartLocation(), |
| 1801 | diag::err_designator_into_flexible_array_member) |
| 1802 | << SourceRange(NextD->getStartLocation(), |
| 1803 | DIE->getSourceRange().getEnd()); |
| 1804 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1805 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1806 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1807 | Invalid = true; |
| 1808 | } |
| 1809 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1810 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1811 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1812 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1813 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1814 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1815 | diag::err_flexible_array_init_needs_braces) |
| 1816 | << DIE->getInit()->getSourceRange(); |
| 1817 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1818 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1819 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1820 | Invalid = true; |
| 1821 | } |
| 1822 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1823 | // Check GNU flexible array initializer. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1824 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1825 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1826 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1827 | |
| 1828 | if (Invalid) { |
| 1829 | ++Index; |
| 1830 | return true; |
| 1831 | } |
| 1832 | |
| 1833 | // Initialize the array. |
| 1834 | bool prevHadError = hadError; |
| 1835 | unsigned newStructuredIndex = FieldIndex; |
| 1836 | unsigned OldIndex = Index; |
| 1837 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1838 | |
| 1839 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1840 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1841 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1842 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1843 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1844 | IList->setInit(OldIndex, DIE); |
| 1845 | if (hadError && !prevHadError) { |
| 1846 | ++Field; |
| 1847 | ++FieldIndex; |
| 1848 | if (NextField) |
| 1849 | *NextField = Field; |
| 1850 | StructuredIndex = FieldIndex; |
| 1851 | return true; |
| 1852 | } |
| 1853 | } else { |
| 1854 | // Recurse to check later designated subobjects. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1855 | QualType FieldType = Field->getType(); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1856 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1857 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1858 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1859 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1860 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1861 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1862 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1863 | true, false)) |
| 1864 | return true; |
| 1865 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1866 | |
| 1867 | // Find the position of the next field to be initialized in this |
| 1868 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1869 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1870 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1871 | |
| 1872 | // If this the first designator, our caller will continue checking |
| 1873 | // the rest of this struct/class/union subobject. |
| 1874 | if (IsFirstDesignator) { |
| 1875 | if (NextField) |
| 1876 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1877 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1878 | return false; |
| 1879 | } |
| 1880 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1881 | if (!FinishSubobjectInit) |
| 1882 | return false; |
| 1883 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1884 | // We've already initialized something in the union; we're done. |
| 1885 | if (RT->getDecl()->isUnion()) |
| 1886 | return hadError; |
| 1887 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1888 | // Check the remaining fields within this class/struct/union subobject. |
| 1889 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1890 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1891 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1892 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1893 | return hadError && !prevHadError; |
| 1894 | } |
| 1895 | |
| 1896 | // C99 6.7.8p6: |
| 1897 | // |
| 1898 | // If a designator has the form |
| 1899 | // |
| 1900 | // [ constant-expression ] |
| 1901 | // |
| 1902 | // then the current object (defined below) shall have array |
| 1903 | // type and the expression shall be an integer constant |
| 1904 | // expression. If the array is of unknown size, any |
| 1905 | // nonnegative value is valid. |
| 1906 | // |
| 1907 | // Additionally, cope with the GNU extension that permits |
| 1908 | // designators of the form |
| 1909 | // |
| 1910 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1911 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1912 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1913 | if (!VerifyOnly) |
| 1914 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 1915 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1916 | ++Index; |
| 1917 | return true; |
| 1918 | } |
| 1919 | |
| 1920 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1921 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1922 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1923 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1924 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1925 | DesignatedEndIndex = DesignatedStartIndex; |
| 1926 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1927 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1928 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1930 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1932 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1933 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1934 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1935 | // Codegen can't handle evaluating array range designators that have side |
| 1936 | // effects, because we replicate the AST value for each initialized element. |
| 1937 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1938 | // elements with something that has a side effect, so codegen can emit an |
| 1939 | // "error unsupported" error instead of miscompiling the app. |
| 1940 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1941 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1942 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1945 | if (isa<ConstantArrayType>(AT)) { |
| 1946 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1947 | DesignatedStartIndex |
| 1948 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1949 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1950 | DesignatedEndIndex |
| 1951 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1952 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1953 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 1954 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1955 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1956 | diag::err_array_designator_too_large) |
| 1957 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 1958 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1959 | ++Index; |
| 1960 | return true; |
| 1961 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1962 | } else { |
| 1963 | // Make sure the bit-widths and signedness match. |
| 1964 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1965 | DesignatedEndIndex |
| 1966 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1967 | else if (DesignatedStartIndex.getBitWidth() < |
| 1968 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1969 | DesignatedStartIndex |
| 1970 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1971 | DesignatedStartIndex.setIsUnsigned(true); |
| 1972 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1973 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1975 | // Make sure that our non-designated initializer list has space |
| 1976 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1977 | if (!VerifyOnly && |
| 1978 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1980 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1981 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1982 | // Repeatedly perform subobject initializations in the range |
| 1983 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1984 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1985 | // Move to the next designator |
| 1986 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1987 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1988 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1989 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1990 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1991 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1992 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1993 | // Recurse to check later designated subobjects. |
| 1994 | QualType ElementType = AT->getElementType(); |
| 1995 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1996 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1997 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1998 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1999 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2000 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2001 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2002 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2003 | return true; |
| 2004 | |
| 2005 | // Move to the next index in the array that we'll be initializing. |
| 2006 | ++DesignatedStartIndex; |
| 2007 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2008 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2009 | |
| 2010 | // If this the first designator, our caller will continue checking |
| 2011 | // the rest of this array subobject. |
| 2012 | if (IsFirstDesignator) { |
| 2013 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2014 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2015 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2016 | return false; |
| 2017 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2019 | if (!FinishSubobjectInit) |
| 2020 | return false; |
| 2021 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2022 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2023 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2024 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2025 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2026 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2030 | // Get the structured initializer list for a subobject of type |
| 2031 | // @p CurrentObjectType. |
| 2032 | InitListExpr * |
| 2033 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2034 | QualType CurrentObjectType, |
| 2035 | InitListExpr *StructuredList, |
| 2036 | unsigned StructuredIndex, |
| 2037 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2038 | if (VerifyOnly) |
| 2039 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2040 | Expr *ExistingInit = 0; |
| 2041 | if (!StructuredList) |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2042 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2043 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2044 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2045 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2046 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2047 | return Result; |
| 2048 | |
| 2049 | if (ExistingInit) { |
| 2050 | // We are creating an initializer list that initializes the |
| 2051 | // subobjects of the current object, but there was already an |
| 2052 | // initialization that completely initialized the current |
| 2053 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2055 | // struct X { int a, b; }; |
| 2056 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2057 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2058 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2059 | // designated initializer re-initializes the whole |
| 2060 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2062 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2063 | << InitRange; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2064 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2065 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2066 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2067 | << ExistingInit->getSourceRange(); |
| 2068 | } |
| 2069 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2071 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 2072 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2073 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2074 | |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2075 | QualType ResultType = CurrentObjectType; |
| 2076 | if (!ResultType->isArrayType()) |
| 2077 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2078 | Result->setType(ResultType); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2079 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2080 | // Pre-allocate storage for the structured initializer list. |
| 2081 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2082 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2083 | bool GotNumInits = false; |
| 2084 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2085 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2086 | GotNumInits = true; |
| 2087 | } else if (Index < IList->getNumInits()) { |
| 2088 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2089 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2090 | GotNumInits = true; |
| 2091 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2095 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2096 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2097 | NumElements = CAType->getSize().getZExtValue(); |
| 2098 | // Simple heuristic so that we don't allocate a very large |
| 2099 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2100 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2101 | NumElements = 0; |
| 2102 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2103 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2104 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2105 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2106 | RecordDecl *RDecl = RType->getDecl(); |
| 2107 | if (RDecl->isUnion()) |
| 2108 | NumElements = 1; |
| 2109 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2110 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2111 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2114 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2115 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2116 | // Link this new initializer list into the structured initializer |
| 2117 | // lists. |
| 2118 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2119 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2120 | else { |
| 2121 | Result->setSyntacticForm(IList); |
| 2122 | SyntacticToSemantic[IList] = Result; |
| 2123 | } |
| 2124 | |
| 2125 | return Result; |
| 2126 | } |
| 2127 | |
| 2128 | /// Update the initializer at index @p StructuredIndex within the |
| 2129 | /// structured initializer list to the value @p expr. |
| 2130 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2131 | unsigned &StructuredIndex, |
| 2132 | Expr *expr) { |
| 2133 | // No structured initializer list to update |
| 2134 | if (!StructuredList) |
| 2135 | return; |
| 2136 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2137 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2138 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2139 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2140 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2141 | diag::warn_initializer_overrides) |
| 2142 | << expr->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2143 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2144 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2145 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2146 | << PrevInit->getSourceRange(); |
| 2147 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2148 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2149 | ++StructuredIndex; |
| 2150 | } |
| 2151 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2152 | /// Check that the given Index expression is a valid array designator |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2153 | /// value. This is essentially just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2154 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2155 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2156 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2157 | /// added, on success. If everything went okay, Value will receive the |
| 2158 | /// value of the constant expression. |
| 2159 | static ExprResult |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2160 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2161 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2162 | |
| 2163 | // Make sure this is an integer constant expression. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2164 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2165 | if (Result.isInvalid()) |
| 2166 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2167 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2168 | if (Value.isSigned() && Value.isNegative()) |
| 2169 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2170 | << Value.toString(10) << Index->getSourceRange(); |
| 2171 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2172 | Value.setIsUnsigned(true); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2173 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2176 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2177 | SourceLocation Loc, |
| 2178 | bool GNUSyntax, |
| 2179 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2180 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2181 | |
| 2182 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2183 | SmallVector<ASTDesignator, 32> Designators; |
| 2184 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2185 | |
| 2186 | // Build designators and check array designator expressions. |
| 2187 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2188 | const Designator &D = Desig.getDesignator(Idx); |
| 2189 | switch (D.getKind()) { |
| 2190 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2192 | D.getFieldLoc())); |
| 2193 | break; |
| 2194 | |
| 2195 | case Designator::ArrayDesignator: { |
| 2196 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2197 | llvm::APSInt IndexValue; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2198 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
| 2199 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).take(); |
| 2200 | if (!Index) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2201 | Invalid = true; |
| 2202 | else { |
| 2203 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2204 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2205 | D.getRBracketLoc())); |
| 2206 | InitExpressions.push_back(Index); |
| 2207 | } |
| 2208 | break; |
| 2209 | } |
| 2210 | |
| 2211 | case Designator::ArrayRangeDesignator: { |
| 2212 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2213 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2214 | llvm::APSInt StartValue; |
| 2215 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2216 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2217 | StartIndex->isValueDependent(); |
| 2218 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2219 | EndIndex->isValueDependent(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2220 | if (!StartDependent) |
| 2221 | StartIndex = |
| 2222 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).take(); |
| 2223 | if (!EndDependent) |
| 2224 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).take(); |
| 2225 | |
| 2226 | if (!StartIndex || !EndIndex) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2227 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2228 | else { |
| 2229 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2230 | if (StartDependent || EndDependent) { |
| 2231 | // Nothing to compute. |
| 2232 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2233 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2234 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2235 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2236 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2237 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2238 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2239 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2240 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2241 | Invalid = true; |
| 2242 | } else { |
| 2243 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2244 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2245 | D.getEllipsisLoc(), |
| 2246 | D.getRBracketLoc())); |
| 2247 | InitExpressions.push_back(StartIndex); |
| 2248 | InitExpressions.push_back(EndIndex); |
| 2249 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2250 | } |
| 2251 | break; |
| 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | if (Invalid || Init.isInvalid()) |
| 2257 | return ExprError(); |
| 2258 | |
| 2259 | // Clear out the expressions within the designation. |
| 2260 | Desig.ClearExprs(*this); |
| 2261 | |
| 2262 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2263 | = DesignatedInitExpr::Create(Context, |
| 2264 | Designators.data(), Designators.size(), |
| 2265 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 2266 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2267 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2268 | if (!getLangOpts().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2269 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2270 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2271 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2272 | return Owned(DIE); |
| 2273 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2274 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2275 | //===----------------------------------------------------------------------===// |
| 2276 | // Initialization entity |
| 2277 | //===----------------------------------------------------------------------===// |
| 2278 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2279 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2280 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2281 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2282 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2283 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2284 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2285 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2286 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2287 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2288 | Type = VT->getElementType(); |
| 2289 | } else { |
| 2290 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2291 | assert(CT && "Unexpected type"); |
| 2292 | Kind = EK_ComplexElement; |
| 2293 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2294 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2297 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2298 | CXXBaseSpecifier *Base, |
| 2299 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2300 | { |
| 2301 | InitializedEntity Result; |
| 2302 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2303 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2304 | if (IsInheritedVirtualBase) |
| 2305 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2306 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2307 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2308 | return Result; |
| 2309 | } |
| 2310 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2311 | DeclarationName InitializedEntity::getName() const { |
| 2312 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2313 | case EK_Parameter: { |
| 2314 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2315 | return (D ? D->getDeclName() : DeclarationName()); |
| 2316 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2317 | |
| 2318 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2319 | case EK_Member: |
| 2320 | return VariableOrMember->getDeclName(); |
| 2321 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2322 | case EK_LambdaCapture: |
| 2323 | return Capture.Var->getDeclName(); |
| 2324 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2325 | case EK_Result: |
| 2326 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2327 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2328 | case EK_Temporary: |
| 2329 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2330 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2331 | case EK_ArrayElement: |
| 2332 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2333 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2334 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2335 | return DeclarationName(); |
| 2336 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2337 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2338 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2341 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2342 | switch (getKind()) { |
| 2343 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2344 | case EK_Member: |
| 2345 | return VariableOrMember; |
| 2346 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2347 | case EK_Parameter: |
| 2348 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2349 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2350 | case EK_Result: |
| 2351 | case EK_Exception: |
| 2352 | case EK_New: |
| 2353 | case EK_Temporary: |
| 2354 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2355 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2356 | case EK_ArrayElement: |
| 2357 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2358 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2359 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2360 | case EK_LambdaCapture: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2361 | return 0; |
| 2362 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2363 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2364 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2367 | bool InitializedEntity::allowsNRVO() const { |
| 2368 | switch (getKind()) { |
| 2369 | case EK_Result: |
| 2370 | case EK_Exception: |
| 2371 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2372 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2373 | case EK_Variable: |
| 2374 | case EK_Parameter: |
| 2375 | case EK_Member: |
| 2376 | case EK_New: |
| 2377 | case EK_Temporary: |
| 2378 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2379 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2380 | case EK_ArrayElement: |
| 2381 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2382 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2383 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2384 | case EK_LambdaCapture: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2385 | break; |
| 2386 | } |
| 2387 | |
| 2388 | return false; |
| 2389 | } |
| 2390 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2391 | //===----------------------------------------------------------------------===// |
| 2392 | // Initialization sequence |
| 2393 | //===----------------------------------------------------------------------===// |
| 2394 | |
| 2395 | void InitializationSequence::Step::Destroy() { |
| 2396 | switch (Kind) { |
| 2397 | case SK_ResolveAddressOfOverloadedFunction: |
| 2398 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2399 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2400 | case SK_CastDerivedToBaseLValue: |
| 2401 | case SK_BindReference: |
| 2402 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2403 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2404 | case SK_UserConversion: |
| 2405 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2406 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2407 | case SK_QualificationConversionLValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2408 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2409 | case SK_ListConstructorCall: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2410 | case SK_UnwrapInitList: |
| 2411 | case SK_RewrapInitList: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2412 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2413 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2414 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2415 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2416 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2417 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2418 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2419 | case SK_PassByIndirectCopyRestore: |
| 2420 | case SK_PassByIndirectRestore: |
| 2421 | case SK_ProduceObjCObject: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2422 | case SK_StdInitializerList: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2423 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2424 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2425 | case SK_ConversionSequence: |
| 2426 | delete ICS; |
| 2427 | } |
| 2428 | } |
| 2429 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2430 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2431 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2435 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2436 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2437 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2438 | switch (getFailureKind()) { |
| 2439 | case FK_TooManyInitsForReference: |
| 2440 | case FK_ArrayNeedsInitList: |
| 2441 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2442 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2443 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2444 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2445 | case FK_RValueReferenceBindingToLValue: |
| 2446 | case FK_ReferenceInitDropsQualifiers: |
| 2447 | case FK_ReferenceInitFailed: |
| 2448 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2449 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2450 | case FK_TooManyInitsForScalar: |
| 2451 | case FK_ReferenceBindingToInitList: |
| 2452 | case FK_InitListBadDestinationType: |
| 2453 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2454 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2455 | case FK_ArrayTypeMismatch: |
| 2456 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2457 | case FK_ListInitializationFailed: |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2458 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2459 | case FK_PlaceholderType: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2460 | case FK_InitListElementCopyFailure: |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2461 | case FK_ExplicitConstructor: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2462 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2463 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2464 | case FK_ReferenceInitOverloadFailed: |
| 2465 | case FK_UserConversionOverloadFailed: |
| 2466 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2467 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2468 | return FailedOverloadResult == OR_Ambiguous; |
| 2469 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2470 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2471 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2474 | bool InitializationSequence::isConstructorInitialization() const { |
| 2475 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2476 | } |
| 2477 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2478 | void |
| 2479 | InitializationSequence |
| 2480 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2481 | DeclAccessPair Found, |
| 2482 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2483 | Step S; |
| 2484 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2485 | S.Type = Function->getType(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2486 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2487 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2488 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2489 | Steps.push_back(S); |
| 2490 | } |
| 2491 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2492 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2493 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2494 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2495 | switch (VK) { |
| 2496 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2497 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2498 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2499 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2500 | S.Type = BaseType; |
| 2501 | Steps.push_back(S); |
| 2502 | } |
| 2503 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2504 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2505 | bool BindingTemporary) { |
| 2506 | Step S; |
| 2507 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2508 | S.Type = T; |
| 2509 | Steps.push_back(S); |
| 2510 | } |
| 2511 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2512 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2513 | Step S; |
| 2514 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2515 | S.Type = T; |
| 2516 | Steps.push_back(S); |
| 2517 | } |
| 2518 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2519 | void |
| 2520 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2521 | DeclAccessPair FoundDecl, |
| 2522 | QualType T, |
| 2523 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2524 | Step S; |
| 2525 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2526 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2527 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2528 | S.Function.Function = Function; |
| 2529 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2530 | Steps.push_back(S); |
| 2531 | } |
| 2532 | |
| 2533 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2534 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2535 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2536 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2537 | switch (VK) { |
| 2538 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2539 | S.Kind = SK_QualificationConversionRValue; |
| 2540 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2541 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2542 | S.Kind = SK_QualificationConversionXValue; |
| 2543 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2544 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2545 | S.Kind = SK_QualificationConversionLValue; |
| 2546 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2547 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2548 | S.Type = Ty; |
| 2549 | Steps.push_back(S); |
| 2550 | } |
| 2551 | |
| 2552 | void InitializationSequence::AddConversionSequenceStep( |
| 2553 | const ImplicitConversionSequence &ICS, |
| 2554 | QualType T) { |
| 2555 | Step S; |
| 2556 | S.Kind = SK_ConversionSequence; |
| 2557 | S.Type = T; |
| 2558 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2559 | Steps.push_back(S); |
| 2560 | } |
| 2561 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2562 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2563 | Step S; |
| 2564 | S.Kind = SK_ListInitialization; |
| 2565 | S.Type = T; |
| 2566 | Steps.push_back(S); |
| 2567 | } |
| 2568 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2569 | void |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2570 | InitializationSequence |
| 2571 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2572 | AccessSpecifier Access, |
| 2573 | QualType T, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2574 | bool HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2575 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2576 | Step S; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2577 | S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall |
| 2578 | : SK_ConstructorInitialization; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2579 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2580 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2581 | S.Function.Function = Constructor; |
| 2582 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2583 | Steps.push_back(S); |
| 2584 | } |
| 2585 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2586 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2587 | Step S; |
| 2588 | S.Kind = SK_ZeroInitialization; |
| 2589 | S.Type = T; |
| 2590 | Steps.push_back(S); |
| 2591 | } |
| 2592 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2593 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2594 | Step S; |
| 2595 | S.Kind = SK_CAssignment; |
| 2596 | S.Type = T; |
| 2597 | Steps.push_back(S); |
| 2598 | } |
| 2599 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2600 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2601 | Step S; |
| 2602 | S.Kind = SK_StringInit; |
| 2603 | S.Type = T; |
| 2604 | Steps.push_back(S); |
| 2605 | } |
| 2606 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2607 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2608 | Step S; |
| 2609 | S.Kind = SK_ObjCObjectConversion; |
| 2610 | S.Type = T; |
| 2611 | Steps.push_back(S); |
| 2612 | } |
| 2613 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2614 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2615 | Step S; |
| 2616 | S.Kind = SK_ArrayInit; |
| 2617 | S.Type = T; |
| 2618 | Steps.push_back(S); |
| 2619 | } |
| 2620 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2621 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2622 | Step S; |
| 2623 | S.Kind = SK_ParenthesizedArrayInit; |
| 2624 | S.Type = T; |
| 2625 | Steps.push_back(S); |
| 2626 | } |
| 2627 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2628 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2629 | bool shouldCopy) { |
| 2630 | Step s; |
| 2631 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2632 | : SK_PassByIndirectRestore); |
| 2633 | s.Type = type; |
| 2634 | Steps.push_back(s); |
| 2635 | } |
| 2636 | |
| 2637 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2638 | Step S; |
| 2639 | S.Kind = SK_ProduceObjCObject; |
| 2640 | S.Type = T; |
| 2641 | Steps.push_back(S); |
| 2642 | } |
| 2643 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2644 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2645 | Step S; |
| 2646 | S.Kind = SK_StdInitializerList; |
| 2647 | S.Type = T; |
| 2648 | Steps.push_back(S); |
| 2649 | } |
| 2650 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2651 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2652 | InitListExpr *Syntactic) { |
| 2653 | assert(Syntactic->getNumInits() == 1 && |
| 2654 | "Can only rewrap trivial init lists."); |
| 2655 | Step S; |
| 2656 | S.Kind = SK_UnwrapInitList; |
| 2657 | S.Type = Syntactic->getInit(0)->getType(); |
| 2658 | Steps.insert(Steps.begin(), S); |
| 2659 | |
| 2660 | S.Kind = SK_RewrapInitList; |
| 2661 | S.Type = T; |
| 2662 | S.WrappingSyntacticList = Syntactic; |
| 2663 | Steps.push_back(S); |
| 2664 | } |
| 2665 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2666 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2667 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2668 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2669 | this->Failure = Failure; |
| 2670 | this->FailedOverloadResult = Result; |
| 2671 | } |
| 2672 | |
| 2673 | //===----------------------------------------------------------------------===// |
| 2674 | // Attempt initialization |
| 2675 | //===----------------------------------------------------------------------===// |
| 2676 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2677 | static void MaybeProduceObjCObject(Sema &S, |
| 2678 | InitializationSequence &Sequence, |
| 2679 | const InitializedEntity &Entity) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2680 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2681 | |
| 2682 | /// When initializing a parameter, produce the value if it's marked |
| 2683 | /// __attribute__((ns_consumed)). |
| 2684 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2685 | if (!Entity.isParameterConsumed()) |
| 2686 | return; |
| 2687 | |
| 2688 | assert(Entity.getType()->isObjCRetainableType() && |
| 2689 | "consuming an object of unretainable type?"); |
| 2690 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2691 | |
| 2692 | /// When initializing a return value, if the return type is a |
| 2693 | /// retainable type, then returns need to immediately retain the |
| 2694 | /// object. If an autorelease is required, it will be done at the |
| 2695 | /// last instant. |
| 2696 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2697 | if (!Entity.getType()->isObjCRetainableType()) |
| 2698 | return; |
| 2699 | |
| 2700 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2701 | } |
| 2702 | } |
| 2703 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2704 | /// \brief When initializing from init list via constructor, handle |
| 2705 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2706 | /// |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2707 | /// \return true if we have handled initialization of an object of type |
| 2708 | /// std::initializer_list<T>, false otherwise. |
| 2709 | static bool TryInitializerListConstruction(Sema &S, |
| 2710 | InitListExpr *List, |
| 2711 | QualType DestType, |
| 2712 | InitializationSequence &Sequence) { |
| 2713 | QualType E; |
| 2714 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 2715 | return false; |
| 2716 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2717 | // Check that each individual element can be copy-constructed. But since we |
| 2718 | // have no place to store further information, we'll recalculate everything |
| 2719 | // later. |
| 2720 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 2721 | S.Context.getConstantArrayType(E, |
| 2722 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 2723 | List->getNumInits()), |
| 2724 | ArrayType::Normal, 0)); |
| 2725 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 2726 | 0, HiddenArray); |
| 2727 | for (unsigned i = 0, n = List->getNumInits(); i < n; ++i) { |
| 2728 | Element.setElementIndex(i); |
| 2729 | if (!S.CanPerformCopyInitialization(Element, List->getInit(i))) { |
| 2730 | Sequence.SetFailed( |
| 2731 | InitializationSequence::FK_InitListElementCopyFailure); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2732 | return true; |
| 2733 | } |
| 2734 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2735 | Sequence.AddStdInitializerListConstructionStep(DestType); |
| 2736 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2739 | static OverloadingResult |
| 2740 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
| 2741 | Expr **Args, unsigned NumArgs, |
| 2742 | OverloadCandidateSet &CandidateSet, |
| 2743 | DeclContext::lookup_iterator Con, |
| 2744 | DeclContext::lookup_iterator ConEnd, |
| 2745 | OverloadCandidateSet::iterator &Best, |
| 2746 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2747 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2748 | CandidateSet.clear(); |
| 2749 | |
| 2750 | for (; Con != ConEnd; ++Con) { |
| 2751 | NamedDecl *D = *Con; |
| 2752 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2753 | bool SuppressUserConversions = false; |
| 2754 | |
| 2755 | // Find the constructor (which may be a template). |
| 2756 | CXXConstructorDecl *Constructor = 0; |
| 2757 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 2758 | if (ConstructorTmpl) |
| 2759 | Constructor = cast<CXXConstructorDecl>( |
| 2760 | ConstructorTmpl->getTemplatedDecl()); |
| 2761 | else { |
| 2762 | Constructor = cast<CXXConstructorDecl>(D); |
| 2763 | |
| 2764 | // If we're performing copy initialization using a copy constructor, we |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2765 | // suppress user-defined conversions on the arguments. We do the same for |
| 2766 | // move constructors. |
| 2767 | if ((CopyInitializing || (InitListSyntax && NumArgs == 1)) && |
| 2768 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2769 | SuppressUserConversions = true; |
| 2770 | } |
| 2771 | |
| 2772 | if (!Constructor->isInvalidDecl() && |
| 2773 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2774 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2775 | if (ConstructorTmpl) |
| 2776 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2777 | /*ExplicitArgs*/ 0, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2778 | llvm::makeArrayRef(Args, NumArgs), |
| 2779 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2780 | else { |
| 2781 | // C++ [over.match.copy]p1: |
| 2782 | // - When initializing a temporary to be bound to the first parameter |
| 2783 | // of a constructor that takes a reference to possibly cv-qualified |
| 2784 | // T as its first argument, called with a single argument in the |
| 2785 | // context of direct-initialization, explicit conversion functions |
| 2786 | // are also considered. |
| 2787 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
| 2788 | NumArgs == 1 && |
| 2789 | Constructor->isCopyOrMoveConstructor(); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2790 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2791 | llvm::makeArrayRef(Args, NumArgs), CandidateSet, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2792 | SuppressUserConversions, |
| 2793 | /*PartialOverloading=*/false, |
| 2794 | /*AllowExplicit=*/AllowExplicitConv); |
| 2795 | } |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | // Perform overload resolution and return the result. |
| 2800 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 2801 | } |
| 2802 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2803 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2804 | /// enumerates the constructors of the initialized entity and performs overload |
| 2805 | /// resolution to select the best. |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2806 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2807 | /// class type. |
| 2808 | static void TryConstructorInitialization(Sema &S, |
| 2809 | const InitializedEntity &Entity, |
| 2810 | const InitializationKind &Kind, |
| 2811 | Expr **Args, unsigned NumArgs, |
| 2812 | QualType DestType, |
| 2813 | InitializationSequence &Sequence, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2814 | bool InitListSyntax = false) { |
| 2815 | assert((!InitListSyntax || (NumArgs == 1 && isa<InitListExpr>(Args[0]))) && |
| 2816 | "InitListSyntax must come with a single initializer list argument."); |
| 2817 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2818 | // Check constructor arguments for self reference. |
| 2819 | if (DeclaratorDecl *DD = Entity.getDecl()) |
| 2820 | // Parameters arguments are occassionially constructed with itself, |
| 2821 | // for instance, in recursive functions. Skip them. |
| 2822 | if (!isa<ParmVarDecl>(DD)) |
| 2823 | for (unsigned i = 0; i < NumArgs; ++i) |
| 2824 | S.CheckSelfReference(DD, Args[i]); |
| 2825 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2826 | // The type we're constructing needs to be complete. |
| 2827 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 2828 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2829 | return; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
| 2832 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 2833 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 2834 | CXXRecordDecl *DestRecordDecl |
| 2835 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 2836 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2837 | // Build the candidate set directly in the initialization sequence |
| 2838 | // structure, so that it will persist if we fail. |
| 2839 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2840 | |
| 2841 | // Determine whether we are allowed to call explicit constructors or |
| 2842 | // explicit conversion operators. |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2843 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2844 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2845 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2846 | // - Otherwise, if T is a class type, constructors are considered. The |
| 2847 | // applicable constructors are enumerated, and the best one is chosen |
| 2848 | // through overload resolution. |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2849 | DeclContext::lookup_iterator ConStart, ConEnd; |
| 2850 | llvm::tie(ConStart, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2851 | |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2852 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2853 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2854 | bool AsInitializerList = false; |
| 2855 | |
| 2856 | // C++11 [over.match.list]p1: |
| 2857 | // When objects of non-aggregate type T are list-initialized, overload |
| 2858 | // resolution selects the constructor in two phases: |
| 2859 | // - Initially, the candidate functions are the initializer-list |
| 2860 | // constructors of the class T and the argument list consists of the |
| 2861 | // initializer list as a single argument. |
| 2862 | if (InitListSyntax) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2863 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2864 | AsInitializerList = true; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2865 | |
| 2866 | // If the initializer list has no elements and T has a default constructor, |
| 2867 | // the first phase is omitted. |
| 2868 | if (ILE->getNumInits() != 0 || |
| 2869 | (!DestRecordDecl->hasDeclaredDefaultConstructor() && |
| 2870 | !DestRecordDecl->needsImplicitDefaultConstructor())) |
| 2871 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, NumArgs, |
| 2872 | CandidateSet, ConStart, ConEnd, Best, |
| 2873 | CopyInitialization, AllowExplicit, |
| 2874 | /*OnlyListConstructor=*/true, |
| 2875 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2876 | |
| 2877 | // Time to unwrap the init list. |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2878 | Args = ILE->getInits(); |
| 2879 | NumArgs = ILE->getNumInits(); |
| 2880 | } |
| 2881 | |
| 2882 | // C++11 [over.match.list]p1: |
| 2883 | // - If no viable initializer-list constructor is found, overload resolution |
| 2884 | // is performed again, where the candidate functions are all the |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2885 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2886 | // elements of the initializer list. |
| 2887 | if (Result == OR_No_Viable_Function) { |
| 2888 | AsInitializerList = false; |
| 2889 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, NumArgs, |
| 2890 | CandidateSet, ConStart, ConEnd, Best, |
| 2891 | CopyInitialization, AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2892 | /*OnlyListConstructors=*/false, |
| 2893 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2894 | } |
| 2895 | if (Result) { |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2896 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2897 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 2898 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2899 | Result); |
| 2900 | return; |
| 2901 | } |
| 2902 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2903 | // C++11 [dcl.init]p6: |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2904 | // If a program calls for the default initialization of an object |
| 2905 | // of a const-qualified type T, T shall be a class type with a |
| 2906 | // user-provided default constructor. |
| 2907 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 2908 | Entity.getType().isConstQualified() && |
| 2909 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 2910 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2911 | return; |
| 2912 | } |
| 2913 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2914 | // C++11 [over.match.list]p1: |
| 2915 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 2916 | // initializer is ill-formed. |
| 2917 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 2918 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 2919 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 2920 | return; |
| 2921 | } |
| 2922 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2923 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 2924 | // subsumed by the initialization. |
| 2925 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2926 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 2927 | Best->FoundDecl.getAccess(), |
| 2928 | DestType, HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2929 | InitListSyntax, AsInitializerList); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2932 | static bool |
| 2933 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 2934 | Expr *Initializer, |
| 2935 | QualType &SourceType, |
| 2936 | QualType &UnqualifiedSourceType, |
| 2937 | QualType UnqualifiedTargetType, |
| 2938 | InitializationSequence &Sequence) { |
| 2939 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 2940 | S.Context.OverloadTy) { |
| 2941 | DeclAccessPair Found; |
| 2942 | bool HadMultipleCandidates = false; |
| 2943 | if (FunctionDecl *Fn |
| 2944 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 2945 | UnqualifiedTargetType, |
| 2946 | false, Found, |
| 2947 | &HadMultipleCandidates)) { |
| 2948 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 2949 | HadMultipleCandidates); |
| 2950 | SourceType = Fn->getType(); |
| 2951 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 2952 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 2953 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2954 | return true; |
| 2955 | } |
| 2956 | } |
| 2957 | return false; |
| 2958 | } |
| 2959 | |
| 2960 | static void TryReferenceInitializationCore(Sema &S, |
| 2961 | const InitializedEntity &Entity, |
| 2962 | const InitializationKind &Kind, |
| 2963 | Expr *Initializer, |
| 2964 | QualType cv1T1, QualType T1, |
| 2965 | Qualifiers T1Quals, |
| 2966 | QualType cv2T2, QualType T2, |
| 2967 | Qualifiers T2Quals, |
| 2968 | InitializationSequence &Sequence); |
| 2969 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2970 | static void TryValueInitialization(Sema &S, |
| 2971 | const InitializedEntity &Entity, |
| 2972 | const InitializationKind &Kind, |
| 2973 | InitializationSequence &Sequence, |
| 2974 | InitListExpr *InitList = 0); |
| 2975 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2976 | static void TryListInitialization(Sema &S, |
| 2977 | const InitializedEntity &Entity, |
| 2978 | const InitializationKind &Kind, |
| 2979 | InitListExpr *InitList, |
| 2980 | InitializationSequence &Sequence); |
| 2981 | |
| 2982 | /// \brief Attempt list initialization of a reference. |
| 2983 | static void TryReferenceListInitialization(Sema &S, |
| 2984 | const InitializedEntity &Entity, |
| 2985 | const InitializationKind &Kind, |
| 2986 | InitListExpr *InitList, |
| 2987 | InitializationSequence &Sequence) |
| 2988 | { |
| 2989 | // First, catch C++03 where this isn't possible. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2990 | if (!S.getLangOpts().CPlusPlus0x) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2991 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2992 | return; |
| 2993 | } |
| 2994 | |
| 2995 | QualType DestType = Entity.getType(); |
| 2996 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2997 | Qualifiers T1Quals; |
| 2998 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 2999 | |
| 3000 | // Reference initialization via an initializer list works thus: |
| 3001 | // If the initializer list consists of a single element that is |
| 3002 | // reference-related to the referenced type, bind directly to that element |
| 3003 | // (possibly creating temporaries). |
| 3004 | // Otherwise, initialize a temporary with the initializer list and |
| 3005 | // bind to that. |
| 3006 | if (InitList->getNumInits() == 1) { |
| 3007 | Expr *Initializer = InitList->getInit(0); |
| 3008 | QualType cv2T2 = Initializer->getType(); |
| 3009 | Qualifiers T2Quals; |
| 3010 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3011 | |
| 3012 | // If this fails, creating a temporary wouldn't work either. |
| 3013 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3014 | T1, Sequence)) |
| 3015 | return; |
| 3016 | |
| 3017 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3018 | bool dummy1, dummy2, dummy3; |
| 3019 | Sema::ReferenceCompareResult RefRelationship |
| 3020 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3021 | dummy2, dummy3); |
| 3022 | if (RefRelationship >= Sema::Ref_Related) { |
| 3023 | // Try to bind the reference here. |
| 3024 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3025 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3026 | if (Sequence) |
| 3027 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3028 | return; |
| 3029 | } |
| 3030 | } |
| 3031 | |
| 3032 | // Not reference-related. Create a temporary and bind to that. |
| 3033 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3034 | |
| 3035 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3036 | if (Sequence) { |
| 3037 | if (DestType->isRValueReferenceType() || |
| 3038 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3039 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3040 | else |
| 3041 | Sequence.SetFailed( |
| 3042 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3043 | } |
| 3044 | } |
| 3045 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3046 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3047 | static void TryListInitialization(Sema &S, |
| 3048 | const InitializedEntity &Entity, |
| 3049 | const InitializationKind &Kind, |
| 3050 | InitListExpr *InitList, |
| 3051 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3052 | QualType DestType = Entity.getType(); |
| 3053 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3054 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3055 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3056 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3057 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3058 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3059 | return; |
| 3060 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3061 | if (DestType->isReferenceType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3062 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3063 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3064 | } |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3065 | if (DestType->isRecordType()) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3066 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3067 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3068 | return; |
| 3069 | } |
| 3070 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3071 | // C++11 [dcl.init.list]p3: |
| 3072 | // - If T is an aggregate, aggregate initialization is performed. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3073 | if (!DestType->isAggregateType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3074 | if (S.getLangOpts().CPlusPlus0x) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3075 | // - Otherwise, if the initializer list has no elements and T is a |
| 3076 | // class type with a default constructor, the object is |
| 3077 | // value-initialized. |
| 3078 | if (InitList->getNumInits() == 0) { |
| 3079 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
| 3080 | if (RD->hasDeclaredDefaultConstructor() || |
| 3081 | RD->needsImplicitDefaultConstructor()) { |
| 3082 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3083 | return; |
| 3084 | } |
| 3085 | } |
| 3086 | |
| 3087 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3088 | // an initializer_list object constructed [...] |
| 3089 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3090 | return; |
| 3091 | |
| 3092 | // - Otherwise, if T is a class type, constructors are considered. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3093 | Expr *Arg = InitList; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3094 | TryConstructorInitialization(S, Entity, Kind, &Arg, 1, DestType, |
| 3095 | Sequence, /*InitListSyntax*/true); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3096 | } else |
| 3097 | Sequence.SetFailed( |
| 3098 | InitializationSequence::FK_InitListBadDestinationType); |
| 3099 | return; |
| 3100 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3103 | InitListChecker CheckInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 3104 | DestType, /*VerifyOnly=*/true, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3105 | Kind.getKind() != InitializationKind::IK_DirectList || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3106 | !S.getLangOpts().CPlusPlus0x); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3107 | if (CheckInitList.HadError()) { |
| 3108 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3109 | return; |
| 3110 | } |
| 3111 | |
| 3112 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3113 | Sequence.AddListInitializationStep(DestType); |
| 3114 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3115 | |
| 3116 | /// \brief Try a reference initialization that involves calling a conversion |
| 3117 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3118 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3119 | const InitializedEntity &Entity, |
| 3120 | const InitializationKind &Kind, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3121 | Expr *Initializer, |
| 3122 | bool AllowRValues, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3123 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3124 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3125 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3126 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3127 | QualType cv2T2 = Initializer->getType(); |
| 3128 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3129 | |
| 3130 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3131 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3132 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3133 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3134 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3135 | ObjCConversion, |
| 3136 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3137 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3138 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3139 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3140 | (void)ObjCLifetimeConversion; |
| 3141 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3142 | // Build the candidate set directly in the initialization sequence |
| 3143 | // structure, so that it will persist if we fail. |
| 3144 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3145 | CandidateSet.clear(); |
| 3146 | |
| 3147 | // Determine whether we are allowed to call explicit constructors or |
| 3148 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3149 | bool AllowExplicit = Kind.AllowExplicit(); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3150 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctions(); |
| 3151 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3152 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3153 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3154 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3155 | // The type we're converting to is a class type. Enumerate its constructors |
| 3156 | // to see if there is a suitable conversion. |
| 3157 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3158 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3159 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3160 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3161 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3162 | NamedDecl *D = *Con; |
| 3163 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3164 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3165 | // Find the constructor (which may be a template). |
| 3166 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3167 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3168 | if (ConstructorTmpl) |
| 3169 | Constructor = cast<CXXConstructorDecl>( |
| 3170 | ConstructorTmpl->getTemplatedDecl()); |
| 3171 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3172 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3173 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3174 | if (!Constructor->isInvalidDecl() && |
| 3175 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3176 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3177 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3178 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3179 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3180 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3181 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3182 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3183 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3184 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3185 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3186 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3187 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3188 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3189 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3190 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3191 | const RecordType *T2RecordType = 0; |
| 3192 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3193 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3194 | // The type we're converting from is a class type, enumerate its conversion |
| 3195 | // functions. |
| 3196 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3197 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3198 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3199 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3200 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 3201 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3202 | NamedDecl *D = *I; |
| 3203 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3204 | if (isa<UsingShadowDecl>(D)) |
| 3205 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3206 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3207 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3208 | CXXConversionDecl *Conv; |
| 3209 | if (ConvTemplate) |
| 3210 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3211 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3212 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3213 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3214 | // If the conversion function doesn't return a reference type, |
| 3215 | // it can't be considered for this conversion unless we're allowed to |
| 3216 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3217 | // FIXME: Do we need to make sure that we only consider conversion |
| 3218 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3219 | // break recursion. |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3220 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3221 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3222 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3223 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3224 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3225 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3226 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3227 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3228 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3229 | } |
| 3230 | } |
| 3231 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3232 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3233 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3234 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3235 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3236 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3237 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3238 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3239 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3240 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3241 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3242 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3243 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3244 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3245 | // This is the overload that will actually be used for the initialization, so |
| 3246 | // mark it as used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3247 | S.MarkFunctionReferenced(DeclLoc, Function); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3248 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3249 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3250 | if (isa<CXXConversionDecl>(Function)) |
| 3251 | T2 = Function->getResultType(); |
| 3252 | else |
| 3253 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3254 | |
| 3255 | // Add the user-defined conversion step. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3256 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3257 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3258 | T2.getNonLValueExprType(S.Context), |
| 3259 | HadMultipleCandidates); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3260 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3261 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3262 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3263 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3264 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3265 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3266 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3267 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3268 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3269 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3270 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3271 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3272 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3273 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3274 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3275 | NewDerivedToBase, NewObjCConversion, |
| 3276 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3277 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3278 | // If the type we've converted to is not reference-related to the |
| 3279 | // type we're looking for, then there is another conversion step |
| 3280 | // we need to perform to produce a temporary of the right type |
| 3281 | // that we'll be binding to. |
| 3282 | ImplicitConversionSequence ICS; |
| 3283 | ICS.setStandard(); |
| 3284 | ICS.Standard = Best->FinalConversion; |
| 3285 | T2 = ICS.Standard.getToType(2); |
| 3286 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3287 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3288 | Sequence.AddDerivedToBaseCastStep( |
| 3289 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3290 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3291 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3292 | else if (NewObjCConversion) |
| 3293 | Sequence.AddObjCObjectConversionStep( |
| 3294 | S.Context.getQualifiedType(T1, |
| 3295 | T2.getNonReferenceType().getQualifiers())); |
| 3296 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3297 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3298 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3299 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3300 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3301 | return OR_Success; |
| 3302 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3303 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3304 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3305 | const InitializedEntity &Entity, |
| 3306 | Expr *CurInitExpr); |
| 3307 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3308 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3309 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3310 | const InitializedEntity &Entity, |
| 3311 | const InitializationKind &Kind, |
| 3312 | Expr *Initializer, |
| 3313 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3314 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3315 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3316 | Qualifiers T1Quals; |
| 3317 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3318 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3319 | Qualifiers T2Quals; |
| 3320 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3321 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3322 | // If the initializer is the address of an overloaded function, try |
| 3323 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3324 | // type of the resulting function. |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3325 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3326 | T1, Sequence)) |
| 3327 | return; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3328 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3329 | // Delegate everything else to a subfunction. |
| 3330 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3331 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3332 | } |
| 3333 | |
| 3334 | /// \brief Reference initialization without resolving overloaded functions. |
| 3335 | static void TryReferenceInitializationCore(Sema &S, |
| 3336 | const InitializedEntity &Entity, |
| 3337 | const InitializationKind &Kind, |
| 3338 | Expr *Initializer, |
| 3339 | QualType cv1T1, QualType T1, |
| 3340 | Qualifiers T1Quals, |
| 3341 | QualType cv2T2, QualType T2, |
| 3342 | Qualifiers T2Quals, |
| 3343 | InitializationSequence &Sequence) { |
| 3344 | QualType DestType = Entity.getType(); |
| 3345 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3346 | // Compute some basic properties of the types and the initializer. |
| 3347 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3348 | bool isRValueRef = !isLValueRef; |
| 3349 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3350 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3351 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3352 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3353 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3354 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3355 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3356 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3357 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3358 | // A reference to type "cv1 T1" is initialized by an expression of type |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3359 | // "cv2 T2" as follows: |
| 3360 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3361 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3362 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3363 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3364 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3365 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3366 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3367 | bool T1Function = T1->isFunctionType(); |
| 3368 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3369 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3370 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3371 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3372 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3373 | // - is an lvalue (but is not a bit-field), and "cv1 T1" is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3374 | // reference-compatible with "cv2 T2," or |
| 3375 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3376 | // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3377 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3378 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3379 | // to decide whether we're actually binding to a temporary created from |
| 3380 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3381 | if (DerivedToBase) |
| 3382 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3383 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3384 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3385 | else if (ObjCConversion) |
| 3386 | Sequence.AddObjCObjectConversionStep( |
| 3387 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3388 | |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3389 | if (T1Quals != T2Quals) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3390 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3391 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3392 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3393 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3394 | return; |
| 3395 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3396 | |
| 3397 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3398 | // reference-related to T2, and can be implicitly converted to an |
| 3399 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3400 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3401 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3402 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3403 | // If we have an rvalue ref to function type here, the rhs must be |
| 3404 | // an rvalue. |
| 3405 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3406 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3407 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3408 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3409 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3410 | Sequence); |
| 3411 | if (ConvOvlResult == OR_Success) |
| 3412 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3413 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3414 | Sequence.SetOverloadFailure( |
| 3415 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3416 | ConvOvlResult); |
| 3417 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3418 | } |
| 3419 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3420 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3421 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3422 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 3423 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3424 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3425 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3426 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3427 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3428 | Sequence.SetOverloadFailure( |
| 3429 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3430 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3431 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3432 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3433 | ? (RefRelationship == Sema::Ref_Related |
| 3434 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3435 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3436 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3437 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3438 | return; |
| 3439 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3440 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3441 | // - If the initializer expression |
| 3442 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3443 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3444 | // Note: functions are handled below. |
| 3445 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3446 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3447 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3448 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3449 | (InitCategory.isXValue() || |
| 3450 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3451 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3452 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3453 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3454 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3455 | // compiler the freedom to perform a copy here or bind to the |
| 3456 | // object, while C++0x requires that we bind directly to the |
| 3457 | // object. Hence, we always bind to the object without making an |
| 3458 | // extra copy. However, in C++03 requires that we check for the |
| 3459 | // presence of a suitable copy constructor: |
| 3460 | // |
| 3461 | // The constructor that would be used to make the copy shall |
| 3462 | // be callable whether or not the copy is actually done. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3463 | if (!S.getLangOpts().CPlusPlus0x && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3464 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3465 | else if (S.getLangOpts().CPlusPlus0x) |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3466 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3467 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3468 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3469 | if (DerivedToBase) |
| 3470 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3471 | ValueKind); |
| 3472 | else if (ObjCConversion) |
| 3473 | Sequence.AddObjCObjectConversionStep( |
| 3474 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3475 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3476 | if (T1Quals != T2Quals) |
| 3477 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3478 | Sequence.AddReferenceBindingStep(cv1T1, |
Peter Collingbourne | 65bfd68 | 2011-11-13 00:51:30 +0000 | [diff] [blame] | 3479 | /*bindingTemporary=*/InitCategory.isPRValue()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3480 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3481 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3482 | |
| 3483 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3484 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3485 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3486 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3487 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3488 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3489 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3490 | Kind, Initializer, |
| 3491 | /*AllowRValues=*/true, |
| 3492 | Sequence); |
| 3493 | if (ConvOvlResult) |
| 3494 | Sequence.SetOverloadFailure( |
| 3495 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3496 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3497 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3498 | return; |
| 3499 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3500 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3501 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3502 | return; |
| 3503 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3504 | |
| 3505 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3506 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3507 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3508 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3509 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3510 | // Determine whether we are allowed to call explicit constructors or |
| 3511 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3512 | bool AllowExplicit = Kind.AllowExplicit(); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3513 | |
| 3514 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3515 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3516 | ImplicitConversionSequence ICS |
| 3517 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3518 | /*SuppressUserConversions*/ false, |
| 3519 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3520 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3521 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3522 | /*AllowObjCWritebackConversion=*/false); |
| 3523 | |
| 3524 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3525 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3526 | // this into an overloading ambiguity diagnostic. However, we need |
| 3527 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3528 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3529 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3530 | Sequence.SetOverloadFailure( |
| 3531 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3532 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3533 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3534 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3535 | else |
| 3536 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3537 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3538 | } else { |
| 3539 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3543 | // same cv-qualification as, or greater cv-qualification |
| 3544 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3545 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3546 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3547 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3548 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3549 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3550 | return; |
| 3551 | } |
| 3552 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3553 | // [...] If T1 is reference-related to T2 and the reference is an rvalue |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3554 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3555 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3556 | InitCategory.isLValue()) { |
| 3557 | Sequence.SetFailed( |
| 3558 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3559 | return; |
| 3560 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3561 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3562 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3563 | return; |
| 3564 | } |
| 3565 | |
| 3566 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3567 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3568 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3569 | const InitializedEntity &Entity, |
| 3570 | const InitializationKind &Kind, |
| 3571 | Expr *Initializer, |
| 3572 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3573 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3576 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3577 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3578 | const InitializedEntity &Entity, |
| 3579 | const InitializationKind &Kind, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3580 | InitializationSequence &Sequence, |
| 3581 | InitListExpr *InitList) { |
| 3582 | assert((!InitList || InitList->getNumInits() == 0) && |
| 3583 | "Shouldn't use value-init for non-empty init lists"); |
| 3584 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3585 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3586 | // |
| 3587 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3588 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3589 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3590 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3591 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3592 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3593 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3594 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3595 | bool NeedZeroInitialization = true; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3596 | if (!S.getLangOpts().CPlusPlus0x) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3597 | // C++98: |
| 3598 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 3599 | // (12.1), then the default constructor for T is called (and the |
| 3600 | // initialization is ill-formed if T has no accessible default |
| 3601 | // constructor); |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3602 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3603 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3604 | } else { |
| 3605 | // C++11: |
| 3606 | // -- if T is a class type (clause 9) with either no default constructor |
| 3607 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 3608 | // or deleted, then the object is default-initialized; |
| 3609 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 3610 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3611 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3612 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3613 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3614 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 3615 | // user-provided or deleted default constructor, then the object is |
| 3616 | // zero-initialized and, if T has a non-trivial default constructor, |
| 3617 | // default-initialized; |
Richard Smith | d079abf | 2012-05-07 01:07:30 +0000 | [diff] [blame] | 3618 | // FIXME: The 'non-union' here is a defect (not yet assigned an issue |
| 3619 | // number). Update the quotation when the defect is resolved. |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3620 | if (NeedZeroInitialization) |
| 3621 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3622 | |
| 3623 | // If this is list-value-initialization, pass the empty init list on when |
| 3624 | // building the constructor call. This affects the semantics of a few |
| 3625 | // things (such as whether an explicit default constructor can be called). |
| 3626 | Expr *InitListAsExpr = InitList; |
| 3627 | Expr **Args = InitList ? &InitListAsExpr : 0; |
| 3628 | unsigned NumArgs = InitList ? 1 : 0; |
| 3629 | bool InitListSyntax = InitList; |
| 3630 | |
| 3631 | return TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, T, |
| 3632 | Sequence, InitListSyntax); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3633 | } |
| 3634 | } |
| 3635 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3636 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3639 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3640 | static void TryDefaultInitialization(Sema &S, |
| 3641 | const InitializedEntity &Entity, |
| 3642 | const InitializationKind &Kind, |
| 3643 | InitializationSequence &Sequence) { |
| 3644 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3645 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3646 | // C++ [dcl.init]p6: |
| 3647 | // To default-initialize an object of type T means: |
| 3648 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3649 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3650 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3651 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3652 | // constructor for T is called (and the initialization is ill-formed if |
| 3653 | // T has no accessible default constructor); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3654 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3655 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3656 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3657 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3658 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3659 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3660 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3661 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3662 | // a const-qualified type T, T shall be a class type with a user-provided |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3663 | // default constructor. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3664 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3665 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3666 | return; |
| 3667 | } |
| 3668 | |
| 3669 | // If the destination type has a lifetime property, zero-initialize it. |
| 3670 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3671 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3672 | return; |
| 3673 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3676 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3677 | /// which enumerates all conversion functions and performs overload resolution |
| 3678 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3679 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3680 | const InitializedEntity &Entity, |
| 3681 | const InitializationKind &Kind, |
| 3682 | Expr *Initializer, |
| 3683 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3684 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3685 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3686 | QualType SourceType = Initializer->getType(); |
| 3687 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3688 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3689 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3690 | // Build the candidate set directly in the initialization sequence |
| 3691 | // structure, so that it will persist if we fail. |
| 3692 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3693 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3694 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3695 | // Determine whether we are allowed to call explicit constructors or |
| 3696 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3697 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3698 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3699 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3700 | // The type we're converting to is a class type. Enumerate its constructors |
| 3701 | // to see if there is a suitable conversion. |
| 3702 | CXXRecordDecl *DestRecordDecl |
| 3703 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3704 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3705 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3706 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3707 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3708 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3709 | Con != ConEnd; ++Con) { |
| 3710 | NamedDecl *D = *Con; |
| 3711 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3712 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3713 | // Find the constructor (which may be a template). |
| 3714 | CXXConstructorDecl *Constructor = 0; |
| 3715 | FunctionTemplateDecl *ConstructorTmpl |
| 3716 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3717 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3718 | Constructor = cast<CXXConstructorDecl>( |
| 3719 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3720 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3721 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3722 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3723 | if (!Constructor->isInvalidDecl() && |
| 3724 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3725 | if (ConstructorTmpl) |
| 3726 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3727 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3728 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3729 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3730 | else |
| 3731 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3732 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3733 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3734 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3735 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3736 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3737 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3738 | |
| 3739 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3740 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3741 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3742 | // The type we're converting from is a class type, enumerate its conversion |
| 3743 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3744 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3745 | // We can only enumerate the conversion functions for a complete type; if |
| 3746 | // the type isn't complete, simply skip this step. |
| 3747 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3748 | CXXRecordDecl *SourceRecordDecl |
| 3749 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3750 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3751 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3752 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3753 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3754 | E = Conversions->end(); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3755 | I != E; ++I) { |
| 3756 | NamedDecl *D = *I; |
| 3757 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3758 | if (isa<UsingShadowDecl>(D)) |
| 3759 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3760 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3761 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3762 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3763 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3764 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3765 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3766 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3767 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3768 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3769 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3770 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3771 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3772 | CandidateSet); |
| 3773 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3774 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3775 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3776 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3777 | } |
| 3778 | } |
| 3779 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3780 | |
| 3781 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3782 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3783 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3784 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3785 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3786 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3787 | Result); |
| 3788 | return; |
| 3789 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3790 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3791 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3792 | S.MarkFunctionReferenced(DeclLoc, Function); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3793 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3794 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3795 | if (isa<CXXConstructorDecl>(Function)) { |
| 3796 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 3797 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 3798 | // cv-unqualified type of the destination. |
| 3799 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 3800 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3801 | HadMultipleCandidates); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3802 | return; |
| 3803 | } |
| 3804 | |
| 3805 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3806 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3807 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 3808 | // If we're converting to a class type, there may be an copy of |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3809 | // the resulting temporary object (possible to create an object of |
| 3810 | // a base class type). That copy is not a separate conversion, so |
| 3811 | // we just make a note of the actual destination type (possibly a |
| 3812 | // base class of the type returned by the conversion function) and |
| 3813 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3814 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 3815 | HadMultipleCandidates); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3816 | return; |
| 3817 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3818 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3819 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 3820 | HadMultipleCandidates); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3821 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3822 | // If the conversion following the call to the conversion function |
| 3823 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3824 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3825 | Best->FinalConversion.Third) { |
| 3826 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3827 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3828 | ICS.Standard = Best->FinalConversion; |
| 3829 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3830 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3833 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3834 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3835 | |
| 3836 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3837 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3838 | bool isAddressOf) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3839 | // Skip parens. |
| 3840 | e = e->IgnoreParens(); |
| 3841 | |
| 3842 | // Skip address-of nodes. |
| 3843 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3844 | if (op->getOpcode() == UO_AddrOf) |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3845 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3846 | |
| 3847 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3848 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3849 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3850 | case CK_Dependent: |
| 3851 | case CK_BitCast: |
| 3852 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3853 | case CK_NoOp: |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3854 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3855 | |
| 3856 | case CK_ArrayToPointerDecay: |
| 3857 | return IIK_nonscalar; |
| 3858 | |
| 3859 | case CK_NullToPointer: |
| 3860 | return IIK_okay; |
| 3861 | |
| 3862 | default: |
| 3863 | break; |
| 3864 | } |
| 3865 | |
| 3866 | // If we have a declaration reference, it had better be a local variable. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3867 | } else if (isa<DeclRefExpr>(e)) { |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3868 | if (!isAddressOf) return IIK_nonlocal; |
| 3869 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3870 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3871 | if (!var) return IIK_nonlocal; |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3872 | |
| 3873 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3874 | |
| 3875 | // If we have a conditional operator, check both sides. |
| 3876 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3877 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3878 | return iik; |
| 3879 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3880 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3881 | |
| 3882 | // These are never scalar. |
| 3883 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3884 | return IIK_nonscalar; |
| 3885 | |
| 3886 | // Otherwise, it needs to be a null pointer constant. |
| 3887 | } else { |
| 3888 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3889 | ? IIK_okay : IIK_nonlocal); |
| 3890 | } |
| 3891 | |
| 3892 | return IIK_nonlocal; |
| 3893 | } |
| 3894 | |
| 3895 | /// Check whether the given expression is a valid operand for an |
| 3896 | /// indirect copy/restore. |
| 3897 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3898 | assert(src->isRValue()); |
| 3899 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3900 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3901 | if (iik == IIK_okay) return; |
| 3902 | |
| 3903 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3904 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3905 | << src->getSourceRange(); |
| 3906 | } |
| 3907 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3908 | /// \brief Determine whether we have compatible array types for the |
| 3909 | /// purposes of GNU by-copy array initialization. |
| 3910 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3911 | const ArrayType *Dest, |
| 3912 | const ArrayType *Source) { |
| 3913 | // If the source and destination array types are equivalent, we're |
| 3914 | // done. |
| 3915 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3916 | return true; |
| 3917 | |
| 3918 | // Make sure that the element types are the same. |
| 3919 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3920 | return false; |
| 3921 | |
| 3922 | // The only mismatch we allow is when the destination is an |
| 3923 | // incomplete array type and the source is a constant array type. |
| 3924 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3925 | } |
| 3926 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3927 | static bool tryObjCWritebackConversion(Sema &S, |
| 3928 | InitializationSequence &Sequence, |
| 3929 | const InitializedEntity &Entity, |
| 3930 | Expr *Initializer) { |
| 3931 | bool ArrayDecay = false; |
| 3932 | QualType ArgType = Initializer->getType(); |
| 3933 | QualType ArgPointee; |
| 3934 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3935 | ArrayDecay = true; |
| 3936 | ArgPointee = ArgArrayType->getElementType(); |
| 3937 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3938 | } |
| 3939 | |
| 3940 | // Handle write-back conversion. |
| 3941 | QualType ConvertedArgType; |
| 3942 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3943 | ConvertedArgType)) |
| 3944 | return false; |
| 3945 | |
| 3946 | // We should copy unless we're passing to an argument explicitly |
| 3947 | // marked 'out'. |
| 3948 | bool ShouldCopy = true; |
| 3949 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3950 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3951 | |
| 3952 | // Do we need an lvalue conversion? |
| 3953 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3954 | ImplicitConversionSequence ICS; |
| 3955 | ICS.setStandard(); |
| 3956 | ICS.Standard.setAsIdentityConversion(); |
| 3957 | |
| 3958 | QualType ResultType; |
| 3959 | if (ArrayDecay) { |
| 3960 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3961 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3962 | } else { |
| 3963 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3964 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3965 | } |
| 3966 | |
| 3967 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3968 | } |
| 3969 | |
| 3970 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3971 | return true; |
| 3972 | } |
| 3973 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3974 | InitializationSequence::InitializationSequence(Sema &S, |
| 3975 | const InitializedEntity &Entity, |
| 3976 | const InitializationKind &Kind, |
| 3977 | Expr **Args, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3978 | unsigned NumArgs) |
| 3979 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3980 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3981 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3982 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3983 | // The semantics of initializers are as follows. The destination type is |
| 3984 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3985 | // type is the type of the initializer expression. The source type is not |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3986 | // defined when the initializer is a braced-init-list or when it is a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3987 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3988 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3989 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3990 | if (DestType->isDependentType() || |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3991 | Expr::hasAnyTypeDependentArguments(llvm::makeArrayRef(Args, NumArgs))) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3992 | SequenceKind = DependentSequence; |
| 3993 | return; |
| 3994 | } |
| 3995 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3996 | // Almost everything is a normal sequence. |
| 3997 | setSequenceKind(NormalSequence); |
| 3998 | |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3999 | for (unsigned I = 0; I != NumArgs; ++I) |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4000 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4001 | // FIXME: should we be doing this here? |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4002 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4003 | if (result.isInvalid()) { |
| 4004 | SetFailed(FK_PlaceholderType); |
| 4005 | return; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4006 | } |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4007 | Args[I] = result.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4008 | } |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 4009 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4010 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4011 | QualType SourceType; |
| 4012 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4013 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4014 | Initializer = Args[0]; |
| 4015 | if (!isa<InitListExpr>(Initializer)) |
| 4016 | SourceType = Initializer->getType(); |
| 4017 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4018 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4019 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4020 | // object is list-initialized (8.5.4). |
| 4021 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4022 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4023 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4024 | return; |
| 4025 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4026 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4027 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4028 | // - If the destination type is a reference type, see 8.5.3. |
| 4029 | if (DestType->isReferenceType()) { |
| 4030 | // C++0x [dcl.init.ref]p1: |
| 4031 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4032 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4033 | // by an object that can be converted into a T. |
| 4034 | // (Therefore, multiple arguments are not permitted.) |
| 4035 | if (NumArgs != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4036 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4037 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4038 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4039 | return; |
| 4040 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4041 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4042 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4043 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 4044 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4045 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4046 | return; |
| 4047 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4048 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4049 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4050 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4051 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4052 | return; |
| 4053 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4054 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4055 | // - If the destination type is an array of characters, an array of |
| 4056 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4057 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4058 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4059 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4060 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4061 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4062 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4063 | return; |
| 4064 | } |
| 4065 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4066 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4067 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4068 | return; |
| 4069 | } |
| 4070 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4071 | // Note: as an GNU C extension, we allow initialization of an |
| 4072 | // array from a compound literal that creates an array of the same |
| 4073 | // type, so long as the initializer has no side effects. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4074 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4075 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4076 | Initializer->getType()->isArrayType()) { |
| 4077 | const ArrayType *SourceAT |
| 4078 | = Context.getAsArrayType(Initializer->getType()); |
| 4079 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4080 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4081 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4082 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4083 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4084 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4085 | } |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4086 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4087 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4088 | // class member of array type from a parenthesized initializer list. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4089 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4090 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4091 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4092 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4093 | *this); |
| 4094 | AddParenthesizedArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4095 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4096 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4097 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4098 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4099 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4100 | return; |
| 4101 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4102 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4103 | // Determine whether we should consider writeback conversions for |
| 4104 | // Objective-C ARC. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4105 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4106 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 4107 | |
| 4108 | // We're at the end of the line for C: it's either a write-back conversion |
| 4109 | // or it's a C assignment. There's no need to check anything else. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4110 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4111 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4112 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4113 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4114 | return; |
| 4115 | } |
| 4116 | |
| 4117 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4118 | AddCAssignmentStep(DestType); |
| 4119 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4120 | return; |
| 4121 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4122 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4123 | assert(S.getLangOpts().CPlusPlus); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4124 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4125 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4126 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4127 | // - If the initialization is direct-initialization, or if it is |
| 4128 | // copy-initialization where the cv-unqualified version of the |
| 4129 | // source type is the same class as, or a derived class of, the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4130 | // class of the destination, constructors are considered. [...] |
| 4131 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4132 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4133 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4134 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4135 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4136 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4137 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4138 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4139 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4140 | // used) to a derived class thereof are enumerated as described in |
| 4141 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4142 | // (13.3). |
| 4143 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4144 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4145 | return; |
| 4146 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4147 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4148 | if (NumArgs > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4149 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4150 | return; |
| 4151 | } |
| 4152 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4153 | |
| 4154 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4155 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4156 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4157 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 4158 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4159 | return; |
| 4160 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4161 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4162 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4163 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4164 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4165 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4166 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4167 | |
| 4168 | ImplicitConversionSequence ICS |
| 4169 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4170 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4171 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4172 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4173 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4174 | allowObjCWritebackConversion); |
| 4175 | |
| 4176 | if (ICS.isStandard() && |
| 4177 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4178 | // Objective-C ARC writeback conversion. |
| 4179 | |
| 4180 | // We should copy unless we're passing to an argument explicitly |
| 4181 | // marked 'out'. |
| 4182 | bool ShouldCopy = true; |
| 4183 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4184 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4185 | |
| 4186 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4187 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4188 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4189 | ImplicitConversionSequence LvalueICS; |
| 4190 | LvalueICS.setStandard(); |
| 4191 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4192 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4193 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4194 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4195 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4196 | |
| 4197 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4198 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4199 | DeclAccessPair dap; |
| 4200 | if (Initializer->getType() == Context.OverloadTy && |
| 4201 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 4202 | , DestType, false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4203 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4204 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4205 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4206 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4207 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4208 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4209 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4210 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4214 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4215 | StepEnd = Steps.end(); |
| 4216 | Step != StepEnd; ++Step) |
| 4217 | Step->Destroy(); |
| 4218 | } |
| 4219 | |
| 4220 | //===----------------------------------------------------------------------===// |
| 4221 | // Perform initialization |
| 4222 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4223 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4224 | getAssignmentAction(const InitializedEntity &Entity) { |
| 4225 | switch(Entity.getKind()) { |
| 4226 | case InitializedEntity::EK_Variable: |
| 4227 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4228 | case InitializedEntity::EK_Exception: |
| 4229 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4230 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4231 | return Sema::AA_Initializing; |
| 4232 | |
| 4233 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4234 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4235 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4236 | return Sema::AA_Sending; |
| 4237 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4238 | return Sema::AA_Passing; |
| 4239 | |
| 4240 | case InitializedEntity::EK_Result: |
| 4241 | return Sema::AA_Returning; |
| 4242 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4243 | case InitializedEntity::EK_Temporary: |
| 4244 | // FIXME: Can we tell apart casting vs. converting? |
| 4245 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4246 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4247 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4248 | case InitializedEntity::EK_ArrayElement: |
| 4249 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4250 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4251 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4252 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4253 | return Sema::AA_Initializing; |
| 4254 | } |
| 4255 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4256 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4259 | /// \brief Whether we should binding a created object as a temporary when |
| 4260 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4261 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4262 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4263 | case InitializedEntity::EK_ArrayElement: |
| 4264 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4265 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4266 | case InitializedEntity::EK_New: |
| 4267 | case InitializedEntity::EK_Variable: |
| 4268 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4269 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4270 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4271 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4272 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4273 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4274 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4275 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4276 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4277 | case InitializedEntity::EK_Parameter: |
| 4278 | case InitializedEntity::EK_Temporary: |
| 4279 | return true; |
| 4280 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4282 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4283 | } |
| 4284 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4285 | /// \brief Whether the given entity, when initialized with an object |
| 4286 | /// created for that initialization, requires destruction. |
| 4287 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4288 | switch (Entity.getKind()) { |
| 4289 | case InitializedEntity::EK_Member: |
| 4290 | case InitializedEntity::EK_Result: |
| 4291 | case InitializedEntity::EK_New: |
| 4292 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4293 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4294 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4295 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4296 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4297 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4298 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4299 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4300 | case InitializedEntity::EK_Variable: |
| 4301 | case InitializedEntity::EK_Parameter: |
| 4302 | case InitializedEntity::EK_Temporary: |
| 4303 | case InitializedEntity::EK_ArrayElement: |
| 4304 | case InitializedEntity::EK_Exception: |
| 4305 | return true; |
| 4306 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4307 | |
| 4308 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4311 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4312 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4313 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4314 | OverloadCandidateSet &CandidateSet, |
| 4315 | CXXRecordDecl *Class, |
| 4316 | Expr *CurInitExpr) { |
| 4317 | DeclContext::lookup_iterator Con, ConEnd; |
| 4318 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
| 4319 | Con != ConEnd; ++Con) { |
| 4320 | CXXConstructorDecl *Constructor = 0; |
| 4321 | |
| 4322 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
| 4323 | // Handle copy/moveconstructors, only. |
| 4324 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4325 | !Constructor->isCopyOrMoveConstructor() || |
| 4326 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4327 | continue; |
| 4328 | |
| 4329 | DeclAccessPair FoundDecl |
| 4330 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4331 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4332 | CurInitExpr, CandidateSet); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4333 | continue; |
| 4334 | } |
| 4335 | |
| 4336 | // Handle constructor templates. |
| 4337 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 4338 | if (ConstructorTmpl->isInvalidDecl()) |
| 4339 | continue; |
| 4340 | |
| 4341 | Constructor = cast<CXXConstructorDecl>( |
| 4342 | ConstructorTmpl->getTemplatedDecl()); |
| 4343 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4344 | continue; |
| 4345 | |
| 4346 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4347 | // candidates? |
| 4348 | DeclAccessPair FoundDecl |
| 4349 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4350 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4351 | CurInitExpr, CandidateSet, true); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4352 | } |
| 4353 | } |
| 4354 | |
| 4355 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4356 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4357 | Expr *Initializer) { |
| 4358 | switch (Entity.getKind()) { |
| 4359 | case InitializedEntity::EK_Result: |
| 4360 | return Entity.getReturnLoc(); |
| 4361 | |
| 4362 | case InitializedEntity::EK_Exception: |
| 4363 | return Entity.getThrowLoc(); |
| 4364 | |
| 4365 | case InitializedEntity::EK_Variable: |
| 4366 | return Entity.getDecl()->getLocation(); |
| 4367 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4368 | case InitializedEntity::EK_LambdaCapture: |
| 4369 | return Entity.getCaptureLoc(); |
| 4370 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4371 | case InitializedEntity::EK_ArrayElement: |
| 4372 | case InitializedEntity::EK_Member: |
| 4373 | case InitializedEntity::EK_Parameter: |
| 4374 | case InitializedEntity::EK_Temporary: |
| 4375 | case InitializedEntity::EK_New: |
| 4376 | case InitializedEntity::EK_Base: |
| 4377 | case InitializedEntity::EK_Delegating: |
| 4378 | case InitializedEntity::EK_VectorElement: |
| 4379 | case InitializedEntity::EK_ComplexElement: |
| 4380 | case InitializedEntity::EK_BlockElement: |
| 4381 | return Initializer->getLocStart(); |
| 4382 | } |
| 4383 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4384 | } |
| 4385 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4386 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4387 | /// provided by the given initializer by calling the appropriate copy |
| 4388 | /// constructor. |
| 4389 | /// |
| 4390 | /// \param S The Sema object used for type-checking. |
| 4391 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4392 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4393 | /// the type of the initializer expression or a superclass thereof. |
| 4394 | /// |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4395 | /// \param Entity The entity being initialized. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4396 | /// |
| 4397 | /// \param CurInit The initializer expression. |
| 4398 | /// |
| 4399 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4400 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4401 | /// an rvalue. |
| 4402 | /// |
| 4403 | /// \returns An expression that copies the initializer expression into |
| 4404 | /// a temporary object, or an error expression if a copy could not be |
| 4405 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4406 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4407 | QualType T, |
| 4408 | const InitializedEntity &Entity, |
| 4409 | ExprResult CurInit, |
| 4410 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4411 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4412 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4413 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4414 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4415 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4416 | if (!Class) |
| 4417 | return move(CurInit); |
| 4418 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4419 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4420 | // When certain criteria are met, an implementation is allowed to |
| 4421 | // omit the copy/move construction of a class object, even if the |
| 4422 | // copy/move constructor and/or destructor for the object have |
| 4423 | // side effects. [...] |
| 4424 | // - when a temporary class object that has not been bound to a |
| 4425 | // reference (12.2) would be copied/moved to a class object |
| 4426 | // with the same cv-unqualified type, the copy/move operation |
| 4427 | // can be omitted by constructing the temporary object |
| 4428 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4429 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4430 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4431 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4432 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4433 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4434 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4435 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4436 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4437 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4438 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4439 | return move(CurInit); |
| 4440 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4441 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4442 | // Only consider constructors and constructor templates. Per |
| 4443 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4444 | // is direct-initialization. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4445 | OverloadCandidateSet CandidateSet(Loc); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4446 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4447 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4448 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4449 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4450 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4451 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4452 | case OR_Success: |
| 4453 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4454 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4455 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4456 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4457 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4458 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4459 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4460 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4461 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4462 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4463 | return ExprError(); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4464 | return move(CurInit); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4465 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4466 | case OR_Ambiguous: |
| 4467 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4468 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4469 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4470 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4471 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4472 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4473 | case OR_Deleted: |
| 4474 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4475 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4476 | << CurInitExpr->getSourceRange(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4477 | S.NoteDeletedFunction(Best->Function); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4478 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4481 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4482 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4483 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4484 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4485 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4486 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4487 | |
| 4488 | if (IsExtraneousCopy) { |
| 4489 | // If this is a totally extraneous copy for C++03 reference |
| 4490 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4491 | // expression. We don't generate an (elided) copy operation here |
| 4492 | // because doing so would require us to pass down a flag to avoid |
| 4493 | // infinite recursion, where each step adds another extraneous, |
| 4494 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4495 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4496 | // Instantiate the default arguments of any extra parameters in |
| 4497 | // the selected copy constructor, as if we were going to create a |
| 4498 | // proper call to the copy constructor. |
| 4499 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4500 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4501 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4502 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4503 | break; |
| 4504 | |
| 4505 | // Build the default argument expression; we don't actually care |
| 4506 | // if this succeeds or not, because this routine will complain |
| 4507 | // if there was a problem. |
| 4508 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4509 | } |
| 4510 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4511 | return S.Owned(CurInitExpr); |
| 4512 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4513 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 4514 | S.MarkFunctionReferenced(Loc, Constructor); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4515 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4516 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4517 | // constructor call (we might have derived-to-base conversions, or |
| 4518 | // the copy constructor may have default arguments). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4519 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4520 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4521 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4522 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4523 | // Actually perform the constructor call. |
| 4524 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4525 | move_arg(ConstructorArgs), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4526 | HadMultipleCandidates, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4527 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4528 | CXXConstructExpr::CK_Complete, |
| 4529 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4530 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4531 | // If we're supposed to bind temporaries, do so. |
| 4532 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4533 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4534 | return move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4535 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4536 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4537 | /// \brief Check whether elidable copy construction for binding a reference to |
| 4538 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 4539 | /// -Wc++98-compat. |
| 4540 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4541 | const InitializedEntity &Entity, |
| 4542 | Expr *CurInitExpr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4543 | assert(S.getLangOpts().CPlusPlus0x); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4544 | |
| 4545 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 4546 | if (!Record) |
| 4547 | return; |
| 4548 | |
| 4549 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 4550 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 4551 | == DiagnosticsEngine::Ignored) |
| 4552 | return; |
| 4553 | |
| 4554 | // Find constructors which would have been considered. |
| 4555 | OverloadCandidateSet CandidateSet(Loc); |
| 4556 | LookupCopyAndMoveConstructors( |
| 4557 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 4558 | |
| 4559 | // Perform overload resolution. |
| 4560 | OverloadCandidateSet::iterator Best; |
| 4561 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 4562 | |
| 4563 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 4564 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 4565 | << CurInitExpr->getSourceRange(); |
| 4566 | |
| 4567 | switch (OR) { |
| 4568 | case OR_Success: |
| 4569 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 4570 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4571 | // FIXME: Check default arguments as far as that's possible. |
| 4572 | break; |
| 4573 | |
| 4574 | case OR_No_Viable_Function: |
| 4575 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4576 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4577 | break; |
| 4578 | |
| 4579 | case OR_Ambiguous: |
| 4580 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4581 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4582 | break; |
| 4583 | |
| 4584 | case OR_Deleted: |
| 4585 | S.Diag(Loc, Diag); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4586 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4587 | break; |
| 4588 | } |
| 4589 | } |
| 4590 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4591 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4592 | const InitializedEntity &Entity) { |
| 4593 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4594 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4595 | return; |
| 4596 | |
| 4597 | if (Entity.getDecl()->getDeclName()) |
| 4598 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4599 | << Entity.getDecl()->getDeclName(); |
| 4600 | else |
| 4601 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4602 | } |
| 4603 | } |
| 4604 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4605 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4606 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4607 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4608 | } |
| 4609 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4610 | static ExprResult |
| 4611 | PerformConstructorInitialization(Sema &S, |
| 4612 | const InitializedEntity &Entity, |
| 4613 | const InitializationKind &Kind, |
| 4614 | MultiExprArg Args, |
| 4615 | const InitializationSequence::Step& Step, |
| 4616 | bool &ConstructorInitRequiresZeroInit) { |
| 4617 | unsigned NumArgs = Args.size(); |
| 4618 | CXXConstructorDecl *Constructor |
| 4619 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 4620 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 4621 | |
| 4622 | // Build a call to the selected constructor. |
| 4623 | ASTOwningVector<Expr*> ConstructorArgs(S); |
| 4624 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4625 | ? Kind.getEqualLoc() |
| 4626 | : Kind.getLocation(); |
| 4627 | |
| 4628 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4629 | // Force even a trivial, implicit default constructor to be |
| 4630 | // semantically checked. We do this explicitly because we don't build |
| 4631 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 28e4702 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 4632 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4633 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | 5d86f61 | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 4634 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4635 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4636 | } |
| 4637 | |
| 4638 | ExprResult CurInit = S.Owned((Expr *)0); |
| 4639 | |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4640 | // C++ [over.match.copy]p1: |
| 4641 | // - When initializing a temporary to be bound to the first parameter |
| 4642 | // of a constructor that takes a reference to possibly cv-qualified |
| 4643 | // T as its first argument, called with a single argument in the |
| 4644 | // context of direct-initialization, explicit conversion functions |
| 4645 | // are also considered. |
| 4646 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 4647 | Args.size() == 1 && |
| 4648 | Constructor->isCopyOrMoveConstructor(); |
| 4649 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4650 | // Determine the arguments required to actually perform the constructor |
| 4651 | // call. |
| 4652 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4653 | Loc, ConstructorArgs, |
| 4654 | AllowExplicitConv)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4655 | return ExprError(); |
| 4656 | |
| 4657 | |
| 4658 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4659 | (Kind.getKind() == InitializationKind::IK_DirectList || |
| 4660 | (NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
| 4661 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4662 | Kind.getKind() == InitializationKind::IK_Value)))) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4663 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4664 | unsigned NumExprs = ConstructorArgs.size(); |
| 4665 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 4666 | S.MarkFunctionReferenced(Loc, Constructor); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4667 | S.DiagnoseUseOfDecl(Constructor, Loc); |
| 4668 | |
| 4669 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4670 | if (!TSInfo) |
| 4671 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4672 | SourceRange ParenRange; |
| 4673 | if (Kind.getKind() != InitializationKind::IK_DirectList) |
| 4674 | ParenRange = Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4675 | |
| 4676 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4677 | Constructor, |
| 4678 | TSInfo, |
| 4679 | Exprs, |
| 4680 | NumExprs, |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4681 | ParenRange, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4682 | HadMultipleCandidates, |
| 4683 | ConstructorInitRequiresZeroInit)); |
| 4684 | } else { |
| 4685 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4686 | CXXConstructExpr::CK_Complete; |
| 4687 | |
| 4688 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4689 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 4690 | CXXConstructExpr::CK_VirtualBase : |
| 4691 | CXXConstructExpr::CK_NonVirtualBase; |
| 4692 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 4693 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4694 | } |
| 4695 | |
| 4696 | // Only get the parenthesis range if it is a direct construction. |
| 4697 | SourceRange parenRange = |
| 4698 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4699 | Kind.getParenRange() : SourceRange(); |
| 4700 | |
| 4701 | // If the entity allows NRVO, mark the construction as elidable |
| 4702 | // unconditionally. |
| 4703 | if (Entity.allowsNRVO()) |
| 4704 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4705 | Constructor, /*Elidable=*/true, |
| 4706 | move_arg(ConstructorArgs), |
| 4707 | HadMultipleCandidates, |
| 4708 | ConstructorInitRequiresZeroInit, |
| 4709 | ConstructKind, |
| 4710 | parenRange); |
| 4711 | else |
| 4712 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4713 | Constructor, |
| 4714 | move_arg(ConstructorArgs), |
| 4715 | HadMultipleCandidates, |
| 4716 | ConstructorInitRequiresZeroInit, |
| 4717 | ConstructKind, |
| 4718 | parenRange); |
| 4719 | } |
| 4720 | if (CurInit.isInvalid()) |
| 4721 | return ExprError(); |
| 4722 | |
| 4723 | // Only check access if all of that succeeded. |
| 4724 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 4725 | Step.Function.FoundDecl.getAccess()); |
| 4726 | S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc); |
| 4727 | |
| 4728 | if (shouldBindAsTemporary(Entity)) |
| 4729 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4730 | |
| 4731 | return move(CurInit); |
| 4732 | } |
| 4733 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 4734 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 4735 | /// longer than the current full-expression. Conservatively returns false if |
| 4736 | /// it's unclear. |
| 4737 | static bool |
| 4738 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 4739 | const InitializedEntity *Top = &Entity; |
| 4740 | while (Top->getParent()) |
| 4741 | Top = Top->getParent(); |
| 4742 | |
| 4743 | switch (Top->getKind()) { |
| 4744 | case InitializedEntity::EK_Variable: |
| 4745 | case InitializedEntity::EK_Result: |
| 4746 | case InitializedEntity::EK_Exception: |
| 4747 | case InitializedEntity::EK_Member: |
| 4748 | case InitializedEntity::EK_New: |
| 4749 | case InitializedEntity::EK_Base: |
| 4750 | case InitializedEntity::EK_Delegating: |
| 4751 | return true; |
| 4752 | |
| 4753 | case InitializedEntity::EK_ArrayElement: |
| 4754 | case InitializedEntity::EK_VectorElement: |
| 4755 | case InitializedEntity::EK_BlockElement: |
| 4756 | case InitializedEntity::EK_ComplexElement: |
| 4757 | // Could not determine what the full initialization is. Assume it might not |
| 4758 | // outlive the full-expression. |
| 4759 | return false; |
| 4760 | |
| 4761 | case InitializedEntity::EK_Parameter: |
| 4762 | case InitializedEntity::EK_Temporary: |
| 4763 | case InitializedEntity::EK_LambdaCapture: |
| 4764 | // The entity being initialized might not outlive the full-expression. |
| 4765 | return false; |
| 4766 | } |
| 4767 | |
| 4768 | llvm_unreachable("unknown entity kind"); |
| 4769 | } |
| 4770 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4771 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4772 | InitializationSequence::Perform(Sema &S, |
| 4773 | const InitializedEntity &Entity, |
| 4774 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4775 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4776 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4777 | if (Failed()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4778 | unsigned NumArgs = Args.size(); |
| 4779 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4780 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4781 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4782 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4783 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4784 | // If the declaration is a non-dependent, incomplete array type |
| 4785 | // that has an initializer, then its type will be completed once |
| 4786 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4787 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4788 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4789 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4790 | if (const IncompleteArrayType *ArrayT |
| 4791 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4792 | // FIXME: We don't currently have the ability to accurately |
| 4793 | // compute the length of an initializer list without |
| 4794 | // performing full type-checking of the initializer list |
| 4795 | // (since we have to determine where braces are implicitly |
| 4796 | // introduced and such). So, we fall back to making the array |
| 4797 | // type a dependently-sized array type with no specified |
| 4798 | // bound. |
| 4799 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 4800 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4801 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4802 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4803 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4804 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4805 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4806 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4807 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4808 | Brackets = ArrayLoc->getBracketsRange(); |
| 4809 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | *ResultType |
| 4813 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4814 | /*NumElts=*/0, |
| 4815 | ArrayT->getSizeModifier(), |
| 4816 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4817 | Brackets); |
| 4818 | } |
| 4819 | |
| 4820 | } |
| 4821 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4822 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 4823 | !Kind.isExplicitCast()) { |
| 4824 | // Rebuild the ParenListExpr. |
| 4825 | SourceRange ParenRange = Kind.getParenRange(); |
| 4826 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
| 4827 | move(Args)); |
| 4828 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4829 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | a9b55a4 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 4830 | Kind.isExplicitCast() || |
| 4831 | Kind.getKind() == InitializationKind::IK_DirectList); |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4832 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4835 | // No steps means no initialization. |
| 4836 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4837 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4838 | |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 4839 | if (S.getLangOpts().CPlusPlus0x && Entity.getType()->isReferenceType() && |
| 4840 | Args.size() == 1 && isa<InitListExpr>(Args.get()[0]) && |
| 4841 | Entity.getKind() != InitializedEntity::EK_Parameter) { |
| 4842 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 4843 | // from an initializer list. For parameters, we produce a better warning |
| 4844 | // elsewhere. |
| 4845 | Expr *Init = Args.get()[0]; |
| 4846 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 4847 | << Init->getSourceRange(); |
| 4848 | } |
| 4849 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 4850 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 4851 | // pointer obviously outlives the temporary. |
| 4852 | if (Args.size() == 1 && Args.get()[0]->getType()->isArrayType() && |
| 4853 | Entity.getType()->isPointerType() && |
| 4854 | InitializedEntityOutlivesFullExpression(Entity)) { |
| 4855 | Expr *Init = Args.get()[0]; |
| 4856 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 4857 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 4858 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 4859 | << Init->getSourceRange(); |
| 4860 | } |
| 4861 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4862 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4863 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4864 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4865 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4866 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4867 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4868 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4869 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4870 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4871 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4872 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4873 | // grab the only argument out the Args and place it into the "current" |
| 4874 | // initializer. |
| 4875 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4876 | case SK_ResolveAddressOfOverloadedFunction: |
| 4877 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4878 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4879 | case SK_CastDerivedToBaseLValue: |
| 4880 | case SK_BindReference: |
| 4881 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4882 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4883 | case SK_UserConversion: |
| 4884 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4885 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4886 | case SK_QualificationConversionRValue: |
| 4887 | case SK_ConversionSequence: |
| 4888 | case SK_ListInitialization: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4889 | case SK_UnwrapInitList: |
| 4890 | case SK_RewrapInitList: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4891 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4892 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4893 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4894 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4895 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4896 | case SK_PassByIndirectCopyRestore: |
| 4897 | case SK_PassByIndirectRestore: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 4898 | case SK_ProduceObjCObject: |
| 4899 | case SK_StdInitializerList: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4900 | assert(Args.size() == 1); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4901 | CurInit = Args.get()[0]; |
| 4902 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4903 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4904 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4905 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4906 | case SK_ConstructorInitialization: |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4907 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4908 | case SK_ZeroInitialization: |
| 4909 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4910 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4911 | |
| 4912 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4913 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4914 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4915 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4916 | Step != StepEnd; ++Step) { |
| 4917 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4918 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4919 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4920 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4921 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4922 | switch (Step->Kind) { |
| 4923 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4924 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4925 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4926 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4927 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4928 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4929 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4930 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4931 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4932 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4933 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4934 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4935 | case SK_CastDerivedToBaseLValue: { |
| 4936 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4937 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4938 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4939 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4940 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4941 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4942 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4943 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4944 | CurInit.get()->getLocStart(), |
| 4945 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4946 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4947 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4948 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4949 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4950 | QualType T = SourceType; |
| 4951 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4952 | T = Pointer->getPointeeType(); |
| 4953 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4954 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4955 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4956 | } |
| 4957 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4958 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4959 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4960 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4961 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4962 | VK_XValue : |
| 4963 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4964 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4965 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4966 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4967 | CurInit.get(), |
| 4968 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4969 | break; |
| 4970 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4971 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4972 | case SK_BindReference: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4973 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4974 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4975 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4976 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4977 | << BitField->getDeclName() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4978 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4979 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4980 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4981 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4982 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4983 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 4984 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4985 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 4986 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4987 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4988 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4989 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4990 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4991 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4992 | // Reference binding does not have any corresponding ASTs. |
| 4993 | |
| 4994 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4995 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4996 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4997 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4998 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4999 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5000 | case SK_BindReferenceToTemporary: |
| 5001 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5002 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5003 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5004 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5005 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 5006 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 5007 | Entity.getType().getNonReferenceType(), |
| 5008 | CurInit.get(), |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5009 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5010 | |
| 5011 | // If we're binding to an Objective-C object that has lifetime, we |
| 5012 | // need cleanups. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5013 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5014 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 5015 | S.ExprNeedsCleanups = true; |
| 5016 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5017 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5018 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5019 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5020 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5021 | /*IsExtraneousCopy=*/true); |
| 5022 | break; |
| 5023 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5024 | case SK_UserConversion: { |
| 5025 | // We have a user-defined conversion that invokes either a constructor |
| 5026 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5027 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5028 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5029 | FunctionDecl *Fn = Step->Function.Function; |
| 5030 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5031 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5032 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5033 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5034 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5035 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5036 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5037 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5038 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5039 | // Determine the arguments required to actually perform the constructor |
| 5040 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5041 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5042 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5043 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5044 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5045 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5046 | |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5047 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5048 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5049 | move_arg(ConstructorArgs), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5050 | HadMultipleCandidates, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5051 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5052 | CXXConstructExpr::CK_Complete, |
| 5053 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5054 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5055 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5056 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5057 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5058 | FoundFn.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5059 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5060 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5061 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5062 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5063 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5064 | S.IsDerivedFrom(SourceType, Class)) |
| 5065 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5066 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5067 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5068 | } else { |
| 5069 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5070 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5071 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5072 | FoundFn); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5073 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5074 | |
| 5075 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5076 | // derived-to-base conversion? I believe the answer is "no", because |
| 5077 | // we don't want to turn off access control here for c-style casts. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5078 | ExprResult CurInitExprRes = |
| 5079 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 5080 | FoundFn, Conversion); |
| 5081 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5082 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5083 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5084 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5085 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5086 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 5087 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5088 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5089 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5090 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5091 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5092 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5093 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5094 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5095 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5096 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5097 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 5098 | |
| 5099 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5100 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5101 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5102 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5103 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5104 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5105 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5106 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5107 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5108 | } |
| 5109 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5110 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5111 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5112 | CurInit.get()->getType(), |
| 5113 | CastKind, CurInit.get(), 0, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 5114 | CurInit.get()->getValueKind())); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5115 | if (MaybeBindToTemp) |
| 5116 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5117 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5118 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 5119 | move(CurInit), /*IsExtraneousCopy=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5120 | break; |
| 5121 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5122 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5123 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5124 | case SK_QualificationConversionXValue: |
| 5125 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5126 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5127 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5128 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5129 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5130 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5131 | VK_XValue : |
| 5132 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5133 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5134 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5135 | } |
| 5136 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5137 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5138 | Sema::CheckedConversionKind CCK |
| 5139 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 5140 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 5141 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5142 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5143 | ExprResult CurInitExprRes = |
| 5144 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5145 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5146 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5147 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5148 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5149 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5150 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5151 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5152 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5153 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5154 | // Hack: We must pass *ResultType if available in order to set the type |
| 5155 | // of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 5156 | // But in 'const X &x = {1, 2, 3};' we're supposed to initialize a |
| 5157 | // temporary, not a reference, so we should pass Ty. |
| 5158 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 5159 | // Since this step is never used for a reference directly, we explicitly |
| 5160 | // unwrap references here and rewrap them afterwards. |
| 5161 | // We also need to create a InitializeTemporary entity for this. |
| 5162 | QualType Ty = ResultType ? ResultType->getNonReferenceType() : Step->Type; |
Sebastian Redl | cbf8209 | 2012-03-07 16:10:45 +0000 | [diff] [blame] | 5163 | bool IsTemporary = Entity.getType()->isReferenceType(); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5164 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
| 5165 | InitListChecker PerformInitList(S, IsTemporary ? TempEntity : Entity, |
| 5166 | InitList, Ty, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5167 | Kind.getKind() != InitializationKind::IK_DirectList || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5168 | !S.getLangOpts().CPlusPlus0x); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5169 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5170 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5171 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5172 | if (ResultType) { |
| 5173 | if ((*ResultType)->isRValueReferenceType()) |
| 5174 | Ty = S.Context.getRValueReferenceType(Ty); |
| 5175 | else if ((*ResultType)->isLValueReferenceType()) |
| 5176 | Ty = S.Context.getLValueReferenceType(Ty, |
| 5177 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 5178 | *ResultType = Ty; |
| 5179 | } |
| 5180 | |
| 5181 | InitListExpr *StructuredInitList = |
| 5182 | PerformInitList.getFullyStructuredList(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5183 | CurInit.release(); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5184 | CurInit = S.Owned(StructuredInitList); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5185 | break; |
| 5186 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5187 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5188 | case SK_ListConstructorCall: { |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5189 | // When an initializer list is passed for a parameter of type "reference |
| 5190 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5191 | // EK_Parameter entity with reference type. |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5192 | // FIXME: This is a hack. What we really should do is create a user |
| 5193 | // conversion step for this case, but this makes it considerably more |
| 5194 | // complicated. For now, this will do. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5195 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5196 | Entity.getType().getNonReferenceType()); |
| 5197 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5198 | assert(Args.size() == 1 && "expected a single argument for list init"); |
| 5199 | InitListExpr *InitList = cast<InitListExpr>(Args.get()[0]); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5200 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 5201 | << InitList->getSourceRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5202 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5203 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 5204 | Entity, |
| 5205 | Kind, move(Arg), *Step, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5206 | ConstructorInitRequiresZeroInit); |
| 5207 | break; |
| 5208 | } |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5209 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5210 | case SK_UnwrapInitList: |
| 5211 | CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0)); |
| 5212 | break; |
| 5213 | |
| 5214 | case SK_RewrapInitList: { |
| 5215 | Expr *E = CurInit.take(); |
| 5216 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 5217 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
| 5218 | Syntactic->getLBraceLoc(), &E, 1, Syntactic->getRBraceLoc()); |
| 5219 | ILE->setSyntacticForm(Syntactic); |
| 5220 | ILE->setType(E->getType()); |
| 5221 | ILE->setValueKind(E->getValueKind()); |
| 5222 | CurInit = S.Owned(ILE); |
| 5223 | break; |
| 5224 | } |
| 5225 | |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5226 | case SK_ConstructorInitialization: { |
| 5227 | // When an initializer list is passed for a parameter of type "reference |
| 5228 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5229 | // EK_Parameter entity with reference type. |
| 5230 | // FIXME: This is a hack. What we really should do is create a user |
| 5231 | // conversion step for this case, but this makes it considerably more |
| 5232 | // complicated. For now, this will do. |
| 5233 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5234 | Entity.getType().getNonReferenceType()); |
| 5235 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 5236 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity |
| 5237 | : Entity, |
| 5238 | Kind, move(Args), *Step, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5239 | ConstructorInitRequiresZeroInit); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5240 | break; |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5241 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5242 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5243 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5244 | step_iterator NextStep = Step; |
| 5245 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5246 | if (NextStep != StepEnd && |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5247 | (NextStep->Kind == SK_ConstructorInitialization || |
| 5248 | NextStep->Kind == SK_ListConstructorCall)) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5249 | // The need for zero-initialization is recorded directly into |
| 5250 | // the call to the object's constructor within the next step. |
| 5251 | ConstructorInitRequiresZeroInit = true; |
| 5252 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5253 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5254 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5255 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5256 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5257 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5258 | Kind.getRange().getBegin()); |
| 5259 | |
| 5260 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 5261 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 5262 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5263 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5264 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5265 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5266 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5267 | break; |
| 5268 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5269 | |
| 5270 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5271 | QualType SourceType = CurInit.get()->getType(); |
| 5272 | ExprResult Result = move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5273 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5274 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 5275 | if (Result.isInvalid()) |
| 5276 | return ExprError(); |
| 5277 | CurInit = move(Result); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5278 | |
| 5279 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5280 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5281 | if (ConvTy != Sema::Compatible && |
| 5282 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5283 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5284 | == Sema::Compatible) |
| 5285 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5286 | if (CurInitExprRes.isInvalid()) |
| 5287 | return ExprError(); |
| 5288 | CurInit = move(CurInitExprRes); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5289 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5290 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5291 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 5292 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5293 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5294 | getAssignmentAction(Entity), |
| 5295 | &Complained)) { |
| 5296 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5297 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5298 | } else if (Complained) |
| 5299 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5300 | break; |
| 5301 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5302 | |
| 5303 | case SK_StringInit: { |
| 5304 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5305 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 5306 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5307 | break; |
| 5308 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5309 | |
| 5310 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5311 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5312 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 5313 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5314 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5315 | |
| 5316 | case SK_ArrayInit: |
| 5317 | // Okay: we checked everything before creating this step. Note that |
| 5318 | // this is a GNU extension. |
| 5319 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5320 | << Step->Type << CurInit.get()->getType() |
| 5321 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5322 | |
| 5323 | // If the destination type is an incomplete array type, update the |
| 5324 | // type accordingly. |
| 5325 | if (ResultType) { |
| 5326 | if (const IncompleteArrayType *IncompleteDest |
| 5327 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 5328 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5329 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5330 | *ResultType = S.Context.getConstantArrayType( |
| 5331 | IncompleteDest->getElementType(), |
| 5332 | ConstantSource->getSize(), |
| 5333 | ArrayType::Normal, 0); |
| 5334 | } |
| 5335 | } |
| 5336 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5337 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5338 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5339 | case SK_ParenthesizedArrayInit: |
| 5340 | // Okay: we checked everything before creating this step. Note that |
| 5341 | // this is a GNU extension. |
| 5342 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 5343 | << CurInit.get()->getSourceRange(); |
| 5344 | break; |
| 5345 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5346 | case SK_PassByIndirectCopyRestore: |
| 5347 | case SK_PassByIndirectRestore: |
| 5348 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 5349 | CurInit = S.Owned(new (S.Context) |
| 5350 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 5351 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 5352 | break; |
| 5353 | |
| 5354 | case SK_ProduceObjCObject: |
| 5355 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5356 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5357 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5358 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5359 | |
| 5360 | case SK_StdInitializerList: { |
| 5361 | QualType Dest = Step->Type; |
| 5362 | QualType E; |
| 5363 | bool Success = S.isStdInitializerList(Dest, &E); |
| 5364 | (void)Success; |
| 5365 | assert(Success && "Destination type changed?"); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 5366 | |
| 5367 | // If the element type has a destructor, check it. |
| 5368 | if (CXXRecordDecl *RD = E->getAsCXXRecordDecl()) { |
| 5369 | if (!RD->hasIrrelevantDestructor()) { |
| 5370 | if (CXXDestructorDecl *Destructor = S.LookupDestructor(RD)) { |
| 5371 | S.MarkFunctionReferenced(Kind.getLocation(), Destructor); |
| 5372 | S.CheckDestructorAccess(Kind.getLocation(), Destructor, |
| 5373 | S.PDiag(diag::err_access_dtor_temp) << E); |
| 5374 | S.DiagnoseUseOfDecl(Destructor, Kind.getLocation()); |
| 5375 | } |
| 5376 | } |
| 5377 | } |
| 5378 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5379 | InitListExpr *ILE = cast<InitListExpr>(CurInit.take()); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5380 | S.Diag(ILE->getExprLoc(), diag::warn_cxx98_compat_initializer_list_init) |
| 5381 | << ILE->getSourceRange(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5382 | unsigned NumInits = ILE->getNumInits(); |
| 5383 | SmallVector<Expr*, 16> Converted(NumInits); |
| 5384 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 5385 | S.Context.getConstantArrayType(E, |
| 5386 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 5387 | NumInits), |
| 5388 | ArrayType::Normal, 0)); |
| 5389 | InitializedEntity Element =InitializedEntity::InitializeElement(S.Context, |
| 5390 | 0, HiddenArray); |
| 5391 | for (unsigned i = 0; i < NumInits; ++i) { |
| 5392 | Element.setElementIndex(i); |
| 5393 | ExprResult Init = S.Owned(ILE->getInit(i)); |
| 5394 | ExprResult Res = S.PerformCopyInitialization(Element, |
| 5395 | Init.get()->getExprLoc(), |
| 5396 | Init); |
| 5397 | assert(!Res.isInvalid() && "Result changed since try phase."); |
| 5398 | Converted[i] = Res.take(); |
| 5399 | } |
| 5400 | InitListExpr *Semantic = new (S.Context) |
| 5401 | InitListExpr(S.Context, ILE->getLBraceLoc(), |
| 5402 | Converted.data(), NumInits, ILE->getRBraceLoc()); |
| 5403 | Semantic->setSyntacticForm(ILE); |
| 5404 | Semantic->setType(Dest); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 5405 | Semantic->setInitializesStdInitializerList(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5406 | CurInit = S.Owned(Semantic); |
| 5407 | break; |
| 5408 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5409 | } |
| 5410 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5411 | |
| 5412 | // Diagnose non-fatal problems with the completed initialization. |
| 5413 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5414 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 5415 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 5416 | cast<FieldDecl>(Entity.getDecl()), |
| 5417 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5418 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5419 | return move(CurInit); |
| 5420 | } |
| 5421 | |
| 5422 | //===----------------------------------------------------------------------===// |
| 5423 | // Diagnose initialization failures |
| 5424 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5425 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5426 | const InitializedEntity &Entity, |
| 5427 | const InitializationKind &Kind, |
| 5428 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5429 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5430 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5431 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5432 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5433 | switch (Failure) { |
| 5434 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5435 | // FIXME: Customize for the initialized entity? |
| 5436 | if (NumArgs == 0) |
| 5437 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 5438 | << DestType.getNonReferenceType(); |
| 5439 | else // FIXME: diagnostic below could be better! |
| 5440 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 5441 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5442 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5443 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5444 | case FK_ArrayNeedsInitList: |
| 5445 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5446 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 5447 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 5448 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5449 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5450 | case FK_ArrayTypeMismatch: |
| 5451 | case FK_NonConstantArrayInit: |
| 5452 | S.Diag(Kind.getLocation(), |
| 5453 | (Failure == FK_ArrayTypeMismatch |
| 5454 | ? diag::err_array_init_different_type |
| 5455 | : diag::err_array_init_non_constant_array)) |
| 5456 | << DestType.getNonReferenceType() |
| 5457 | << Args[0]->getType() |
| 5458 | << Args[0]->getSourceRange(); |
| 5459 | break; |
| 5460 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5461 | case FK_VariableLengthArrayHasInitializer: |
| 5462 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 5463 | << Args[0]->getSourceRange(); |
| 5464 | break; |
| 5465 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5466 | case FK_AddressOfOverloadFailed: { |
| 5467 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5468 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5469 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5470 | true, |
| 5471 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5472 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5473 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5475 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5476 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5477 | switch (FailedOverloadResult) { |
| 5478 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5479 | if (Failure == FK_UserConversionOverloadFailed) |
| 5480 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 5481 | << Args[0]->getType() << DestType |
| 5482 | << Args[0]->getSourceRange(); |
| 5483 | else |
| 5484 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 5485 | << DestType << Args[0]->getType() |
| 5486 | << Args[0]->getSourceRange(); |
| 5487 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5488 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 5489 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5490 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5491 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5492 | case OR_No_Viable_Function: |
| 5493 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 5494 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5495 | << Args[0]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5496 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, |
| 5497 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5498 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5499 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5500 | case OR_Deleted: { |
| 5501 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 5502 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5503 | << Args[0]->getSourceRange(); |
| 5504 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5505 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5506 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 5507 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5508 | if (Ovl == OR_Deleted) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5509 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5510 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5511 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5512 | } |
| 5513 | break; |
| 5514 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5515 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5516 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5517 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5518 | } |
| 5519 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5520 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5521 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5522 | if (isa<InitListExpr>(Args[0])) { |
| 5523 | S.Diag(Kind.getLocation(), |
| 5524 | diag::err_lvalue_reference_bind_to_initlist) |
| 5525 | << DestType.getNonReferenceType().isVolatileQualified() |
| 5526 | << DestType.getNonReferenceType() |
| 5527 | << Args[0]->getSourceRange(); |
| 5528 | break; |
| 5529 | } |
| 5530 | // Intentional fallthrough |
| 5531 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5532 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5533 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5534 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 5535 | ? diag::err_lvalue_reference_bind_to_temporary |
| 5536 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 5537 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5538 | << DestType.getNonReferenceType() |
| 5539 | << Args[0]->getType() |
| 5540 | << Args[0]->getSourceRange(); |
| 5541 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5542 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5543 | case FK_RValueReferenceBindingToLValue: |
| 5544 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 5545 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5546 | << Args[0]->getSourceRange(); |
| 5547 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5548 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5549 | case FK_ReferenceInitDropsQualifiers: |
| 5550 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 5551 | << DestType.getNonReferenceType() |
| 5552 | << Args[0]->getType() |
| 5553 | << Args[0]->getSourceRange(); |
| 5554 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5555 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5556 | case FK_ReferenceInitFailed: |
| 5557 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 5558 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5559 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5560 | << Args[0]->getType() |
| 5561 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 5562 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5563 | Args[0]->getType()->isObjCObjectPointerType()) |
| 5564 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5565 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5566 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5567 | case FK_ConversionFailed: { |
| 5568 | QualType FromType = Args[0]->getType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5569 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5570 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5571 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5572 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5573 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5574 | << Args[0]->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5575 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 5576 | S.Diag(Kind.getLocation(), PDiag); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 5577 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5578 | Args[0]->getType()->isObjCObjectPointerType()) |
| 5579 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5580 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5581 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5582 | |
| 5583 | case FK_ConversionFromPropertyFailed: |
| 5584 | // No-op. This error has already been reported. |
| 5585 | break; |
| 5586 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5587 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5588 | SourceRange R; |
| 5589 | |
| 5590 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5591 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5592 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5593 | else |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5594 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5595 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5596 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 5597 | if (Kind.isCStyleOrFunctionalCast()) |
| 5598 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 5599 | << R; |
| 5600 | else |
| 5601 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 5602 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5603 | break; |
| 5604 | } |
| 5605 | |
| 5606 | case FK_ReferenceBindingToInitList: |
| 5607 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 5608 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 5609 | break; |
| 5610 | |
| 5611 | case FK_InitListBadDestinationType: |
| 5612 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 5613 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 5614 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5615 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5616 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5617 | case FK_ConstructorOverloadFailed: { |
| 5618 | SourceRange ArgsRange; |
| 5619 | if (NumArgs) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5620 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5621 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5622 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5623 | if (Failure == FK_ListConstructorOverloadFailed) { |
| 5624 | assert(NumArgs == 1 && "List construction from other than 1 argument."); |
| 5625 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 5626 | Args = InitList->getInits(); |
| 5627 | NumArgs = InitList->getNumInits(); |
| 5628 | } |
| 5629 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5630 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 5631 | // bad. |
| 5632 | switch (FailedOverloadResult) { |
| 5633 | case OR_Ambiguous: |
| 5634 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 5635 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5636 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5637 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5638 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5639 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5640 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5641 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 5642 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 5643 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 5644 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5645 | // This is implicit default initialization of a member or |
| 5646 | // base within a constructor. If no viable function was |
| 5647 | // found, notify the user that she needs to explicitly |
| 5648 | // initialize this base/member. |
| 5649 | CXXConstructorDecl *Constructor |
| 5650 | = cast<CXXConstructorDecl>(S.CurContext); |
| 5651 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5652 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5653 | << Constructor->isImplicit() |
| 5654 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5655 | << /*base=*/0 |
| 5656 | << Entity.getType(); |
| 5657 | |
| 5658 | RecordDecl *BaseDecl |
| 5659 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 5660 | ->getDecl(); |
| 5661 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 5662 | << S.Context.getTagDeclType(BaseDecl); |
| 5663 | } else { |
| 5664 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5665 | << Constructor->isImplicit() |
| 5666 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5667 | << /*member=*/1 |
| 5668 | << Entity.getName(); |
| 5669 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 5670 | |
| 5671 | if (const RecordType *Record |
| 5672 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5673 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5674 | diag::note_previous_decl) |
| 5675 | << S.Context.getTagDeclType(Record->getDecl()); |
| 5676 | } |
| 5677 | break; |
| 5678 | } |
| 5679 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5680 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 5681 | << DestType << ArgsRange; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5682 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, |
| 5683 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5684 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5685 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5686 | case OR_Deleted: { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5687 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5688 | OverloadingResult Ovl |
| 5689 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5690 | if (Ovl != OR_Deleted) { |
| 5691 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 5692 | << true << DestType << ArgsRange; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5693 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5694 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5695 | } |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5696 | |
| 5697 | // If this is a defaulted or implicitly-declared function, then |
| 5698 | // it was implicitly deleted. Make it clear that the deletion was |
| 5699 | // implicit. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5700 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5701 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5702 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5703 | << DestType << ArgsRange; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5704 | else |
| 5705 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 5706 | << true << DestType << ArgsRange; |
| 5707 | |
| 5708 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5709 | break; |
| 5710 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5711 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5712 | case OR_Success: |
| 5713 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5714 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5715 | } |
David Blaikie | 9fdefb3 | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 5716 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5717 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5718 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5719 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5720 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5721 | // This is implicit default-initialization of a const member in |
| 5722 | // a constructor. Complain that it needs to be explicitly |
| 5723 | // initialized. |
| 5724 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 5725 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 5726 | << Constructor->isImplicit() |
| 5727 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5728 | << /*const=*/1 |
| 5729 | << Entity.getName(); |
| 5730 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 5731 | << Entity.getName(); |
| 5732 | } else { |
| 5733 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 5734 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 5735 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5736 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5737 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5738 | case FK_Incomplete: |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 5739 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5740 | diag::err_init_incomplete_type); |
| 5741 | break; |
| 5742 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5743 | case FK_ListInitializationFailed: { |
| 5744 | // Run the init list checker again to emit diagnostics. |
| 5745 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 5746 | QualType DestType = Entity.getType(); |
| 5747 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 5748 | DestType, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5749 | Kind.getKind() != InitializationKind::IK_DirectList || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5750 | !S.getLangOpts().CPlusPlus0x); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5751 | assert(DiagnoseInitList.HadError() && |
| 5752 | "Inconsistent init list check result."); |
| 5753 | break; |
| 5754 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 5755 | |
| 5756 | case FK_PlaceholderType: { |
| 5757 | // FIXME: Already diagnosed! |
| 5758 | break; |
| 5759 | } |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5760 | |
| 5761 | case FK_InitListElementCopyFailure: { |
| 5762 | // Try to perform all copies again. |
| 5763 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 5764 | unsigned NumInits = InitList->getNumInits(); |
| 5765 | QualType DestType = Entity.getType(); |
| 5766 | QualType E; |
| 5767 | bool Success = S.isStdInitializerList(DestType, &E); |
| 5768 | (void)Success; |
| 5769 | assert(Success && "Where did the std::initializer_list go?"); |
| 5770 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 5771 | S.Context.getConstantArrayType(E, |
| 5772 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 5773 | NumInits), |
| 5774 | ArrayType::Normal, 0)); |
| 5775 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 5776 | 0, HiddenArray); |
| 5777 | // Show at most 3 errors. Otherwise, you'd get a lot of errors for errors |
| 5778 | // where the init list type is wrong, e.g. |
| 5779 | // std::initializer_list<void*> list = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 5780 | // FIXME: Emit a note if we hit the limit? |
| 5781 | int ErrorCount = 0; |
| 5782 | for (unsigned i = 0; i < NumInits && ErrorCount < 3; ++i) { |
| 5783 | Element.setElementIndex(i); |
| 5784 | ExprResult Init = S.Owned(InitList->getInit(i)); |
| 5785 | if (S.PerformCopyInitialization(Element, Init.get()->getExprLoc(), Init) |
| 5786 | .isInvalid()) |
| 5787 | ++ErrorCount; |
| 5788 | } |
| 5789 | break; |
| 5790 | } |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 5791 | |
| 5792 | case FK_ExplicitConstructor: { |
| 5793 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 5794 | << Args[0]->getSourceRange(); |
| 5795 | OverloadCandidateSet::iterator Best; |
| 5796 | OverloadingResult Ovl |
| 5797 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | e7d0bbf | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 5798 | (void)Ovl; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 5799 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 5800 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 5801 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 5802 | break; |
| 5803 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5804 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5805 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5806 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5807 | return true; |
| 5808 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5809 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5810 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5811 | switch (SequenceKind) { |
| 5812 | case FailedSequence: { |
| 5813 | OS << "Failed sequence: "; |
| 5814 | switch (Failure) { |
| 5815 | case FK_TooManyInitsForReference: |
| 5816 | OS << "too many initializers for reference"; |
| 5817 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5818 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5819 | case FK_ArrayNeedsInitList: |
| 5820 | OS << "array requires initializer list"; |
| 5821 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5822 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5823 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5824 | OS << "array requires initializer list or string literal"; |
| 5825 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5826 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5827 | case FK_ArrayTypeMismatch: |
| 5828 | OS << "array type mismatch"; |
| 5829 | break; |
| 5830 | |
| 5831 | case FK_NonConstantArrayInit: |
| 5832 | OS << "non-constant array initializer"; |
| 5833 | break; |
| 5834 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5835 | case FK_AddressOfOverloadFailed: |
| 5836 | OS << "address of overloaded function failed"; |
| 5837 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5838 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5839 | case FK_ReferenceInitOverloadFailed: |
| 5840 | OS << "overload resolution for reference initialization failed"; |
| 5841 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5842 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5843 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 5844 | OS << "non-const lvalue reference bound to temporary"; |
| 5845 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5846 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5847 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 5848 | OS << "non-const lvalue reference bound to unrelated type"; |
| 5849 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5850 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5851 | case FK_RValueReferenceBindingToLValue: |
| 5852 | OS << "rvalue reference bound to an lvalue"; |
| 5853 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5854 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5855 | case FK_ReferenceInitDropsQualifiers: |
| 5856 | OS << "reference initialization drops qualifiers"; |
| 5857 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5858 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5859 | case FK_ReferenceInitFailed: |
| 5860 | OS << "reference initialization failed"; |
| 5861 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5862 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5863 | case FK_ConversionFailed: |
| 5864 | OS << "conversion failed"; |
| 5865 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5866 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5867 | case FK_ConversionFromPropertyFailed: |
| 5868 | OS << "conversion from property failed"; |
| 5869 | break; |
| 5870 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5871 | case FK_TooManyInitsForScalar: |
| 5872 | OS << "too many initializers for scalar"; |
| 5873 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5874 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5875 | case FK_ReferenceBindingToInitList: |
| 5876 | OS << "referencing binding to initializer list"; |
| 5877 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5878 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5879 | case FK_InitListBadDestinationType: |
| 5880 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 5881 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5882 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5883 | case FK_UserConversionOverloadFailed: |
| 5884 | OS << "overloading failed for user-defined conversion"; |
| 5885 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5886 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5887 | case FK_ConstructorOverloadFailed: |
| 5888 | OS << "constructor overloading failed"; |
| 5889 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5890 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5891 | case FK_DefaultInitOfConst: |
| 5892 | OS << "default initialization of a const variable"; |
| 5893 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5894 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5895 | case FK_Incomplete: |
| 5896 | OS << "initialization of incomplete type"; |
| 5897 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5898 | |
| 5899 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5900 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 5901 | break; |
| 5902 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5903 | case FK_VariableLengthArrayHasInitializer: |
| 5904 | OS << "variable length array has an initializer"; |
| 5905 | break; |
| 5906 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 5907 | case FK_PlaceholderType: |
| 5908 | OS << "initializer expression isn't contextually valid"; |
| 5909 | break; |
Nick Lewycky | b0c6c33 | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 5910 | |
| 5911 | case FK_ListConstructorOverloadFailed: |
| 5912 | OS << "list constructor overloading failed"; |
| 5913 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5914 | |
| 5915 | case FK_InitListElementCopyFailure: |
| 5916 | OS << "copy construction of initializer list element failed"; |
| 5917 | break; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 5918 | |
| 5919 | case FK_ExplicitConstructor: |
| 5920 | OS << "list copy initialization chose explicit constructor"; |
| 5921 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5922 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5923 | OS << '\n'; |
| 5924 | return; |
| 5925 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5926 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5927 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5928 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5929 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5930 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5931 | case NormalSequence: |
| 5932 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5933 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5934 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5935 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5936 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 5937 | if (S != step_begin()) { |
| 5938 | OS << " -> "; |
| 5939 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5940 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5941 | switch (S->Kind) { |
| 5942 | case SK_ResolveAddressOfOverloadedFunction: |
| 5943 | OS << "resolve address of overloaded function"; |
| 5944 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5945 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5946 | case SK_CastDerivedToBaseRValue: |
| 5947 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 5948 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5949 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5950 | case SK_CastDerivedToBaseXValue: |
| 5951 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 5952 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5953 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5954 | case SK_CastDerivedToBaseLValue: |
| 5955 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 5956 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5957 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5958 | case SK_BindReference: |
| 5959 | OS << "bind reference to lvalue"; |
| 5960 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5961 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5962 | case SK_BindReferenceToTemporary: |
| 5963 | OS << "bind reference to a temporary"; |
| 5964 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5965 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5966 | case SK_ExtraneousCopyToTemporary: |
| 5967 | OS << "extraneous C++03 copy to temporary"; |
| 5968 | break; |
| 5969 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5970 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 5971 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5972 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5973 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5974 | case SK_QualificationConversionRValue: |
| 5975 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5976 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5977 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5978 | case SK_QualificationConversionXValue: |
| 5979 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5980 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5981 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5982 | case SK_QualificationConversionLValue: |
| 5983 | OS << "qualification conversion (lvalue)"; |
| 5984 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5985 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5986 | case SK_ConversionSequence: |
| 5987 | OS << "implicit conversion sequence ("; |
| 5988 | S->ICS->DebugPrint(); // FIXME: use OS |
| 5989 | OS << ")"; |
| 5990 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5991 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5992 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5993 | OS << "list aggregate initialization"; |
| 5994 | break; |
| 5995 | |
| 5996 | case SK_ListConstructorCall: |
| 5997 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5998 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5999 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6000 | case SK_UnwrapInitList: |
| 6001 | OS << "unwrap reference initializer list"; |
| 6002 | break; |
| 6003 | |
| 6004 | case SK_RewrapInitList: |
| 6005 | OS << "rewrap reference initializer list"; |
| 6006 | break; |
| 6007 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6008 | case SK_ConstructorInitialization: |
| 6009 | OS << "constructor initialization"; |
| 6010 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6011 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6012 | case SK_ZeroInitialization: |
| 6013 | OS << "zero initialization"; |
| 6014 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6015 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6016 | case SK_CAssignment: |
| 6017 | OS << "C assignment"; |
| 6018 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6019 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6020 | case SK_StringInit: |
| 6021 | OS << "string initialization"; |
| 6022 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6023 | |
| 6024 | case SK_ObjCObjectConversion: |
| 6025 | OS << "Objective-C object conversion"; |
| 6026 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6027 | |
| 6028 | case SK_ArrayInit: |
| 6029 | OS << "array initialization"; |
| 6030 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6031 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6032 | case SK_ParenthesizedArrayInit: |
| 6033 | OS << "parenthesized array initialization"; |
| 6034 | break; |
| 6035 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6036 | case SK_PassByIndirectCopyRestore: |
| 6037 | OS << "pass by indirect copy and restore"; |
| 6038 | break; |
| 6039 | |
| 6040 | case SK_PassByIndirectRestore: |
| 6041 | OS << "pass by indirect restore"; |
| 6042 | break; |
| 6043 | |
| 6044 | case SK_ProduceObjCObject: |
| 6045 | OS << "Objective-C object retension"; |
| 6046 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6047 | |
| 6048 | case SK_StdInitializerList: |
| 6049 | OS << "std::initializer_list from initializer list"; |
| 6050 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6051 | } |
| 6052 | } |
| 6053 | } |
| 6054 | |
| 6055 | void InitializationSequence::dump() const { |
| 6056 | dump(llvm::errs()); |
| 6057 | } |
| 6058 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6059 | static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq, |
| 6060 | QualType EntityType, |
| 6061 | const Expr *PreInit, |
| 6062 | const Expr *PostInit) { |
| 6063 | if (Seq.step_begin() == Seq.step_end() || PreInit->isValueDependent()) |
| 6064 | return; |
| 6065 | |
| 6066 | // A narrowing conversion can only appear as the final implicit conversion in |
| 6067 | // an initialization sequence. |
| 6068 | const InitializationSequence::Step &LastStep = Seq.step_end()[-1]; |
| 6069 | if (LastStep.Kind != InitializationSequence::SK_ConversionSequence) |
| 6070 | return; |
| 6071 | |
| 6072 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 6073 | const StandardConversionSequence *SCS = 0; |
| 6074 | switch (ICS.getKind()) { |
| 6075 | case ImplicitConversionSequence::StandardConversion: |
| 6076 | SCS = &ICS.Standard; |
| 6077 | break; |
| 6078 | case ImplicitConversionSequence::UserDefinedConversion: |
| 6079 | SCS = &ICS.UserDefined.After; |
| 6080 | break; |
| 6081 | case ImplicitConversionSequence::AmbiguousConversion: |
| 6082 | case ImplicitConversionSequence::EllipsisConversion: |
| 6083 | case ImplicitConversionSequence::BadConversion: |
| 6084 | return; |
| 6085 | } |
| 6086 | |
| 6087 | // Determine the type prior to the narrowing conversion. If a conversion |
| 6088 | // operator was used, this may be different from both the type of the entity |
| 6089 | // and of the pre-initialization expression. |
| 6090 | QualType PreNarrowingType = PreInit->getType(); |
| 6091 | if (Seq.step_begin() + 1 != Seq.step_end()) |
| 6092 | PreNarrowingType = Seq.step_end()[-2].Type; |
| 6093 | |
| 6094 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 6095 | APValue ConstantValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6096 | QualType ConstantType; |
| 6097 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 6098 | ConstantType)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6099 | case NK_Not_Narrowing: |
| 6100 | // No narrowing occurred. |
| 6101 | return; |
| 6102 | |
| 6103 | case NK_Type_Narrowing: |
| 6104 | // This was a floating-to-integer conversion, which is always considered a |
| 6105 | // narrowing conversion even if the value is a constant and can be |
| 6106 | // represented exactly as an integer. |
| 6107 | S.Diag(PostInit->getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6108 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus0x? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6109 | diag::warn_init_list_type_narrowing |
| 6110 | : S.isSFINAEContext()? |
| 6111 | diag::err_init_list_type_narrowing_sfinae |
| 6112 | : diag::err_init_list_type_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6113 | << PostInit->getSourceRange() |
| 6114 | << PreNarrowingType.getLocalUnqualifiedType() |
| 6115 | << EntityType.getLocalUnqualifiedType(); |
| 6116 | break; |
| 6117 | |
| 6118 | case NK_Constant_Narrowing: |
| 6119 | // A constant value was narrowed. |
| 6120 | S.Diag(PostInit->getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6121 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus0x? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6122 | diag::warn_init_list_constant_narrowing |
| 6123 | : S.isSFINAEContext()? |
| 6124 | diag::err_init_list_constant_narrowing_sfinae |
| 6125 | : diag::err_init_list_constant_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6126 | << PostInit->getSourceRange() |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6127 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6128 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6129 | break; |
| 6130 | |
| 6131 | case NK_Variable_Narrowing: |
| 6132 | // A variable's value may have been narrowed. |
| 6133 | S.Diag(PostInit->getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6134 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus0x? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6135 | diag::warn_init_list_variable_narrowing |
| 6136 | : S.isSFINAEContext()? |
| 6137 | diag::err_init_list_variable_narrowing_sfinae |
| 6138 | : diag::err_init_list_variable_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6139 | << PostInit->getSourceRange() |
| 6140 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6141 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6142 | break; |
| 6143 | } |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6144 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6145 | SmallString<128> StaticCast; |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6146 | llvm::raw_svector_ostream OS(StaticCast); |
| 6147 | OS << "static_cast<"; |
| 6148 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 6149 | // It's important to use the typedef's name if there is one so that the |
| 6150 | // fixit doesn't break code using types like int64_t. |
| 6151 | // |
| 6152 | // FIXME: This will break if the typedef requires qualification. But |
| 6153 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6154 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6155 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6156 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6157 | else { |
| 6158 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 6159 | // with a broken cast. |
| 6160 | return; |
| 6161 | } |
| 6162 | OS << ">("; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6163 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_override) |
| 6164 | << PostInit->getSourceRange() |
| 6165 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6166 | << FixItHint::CreateInsertion( |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6167 | S.getPreprocessor().getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6168 | } |
| 6169 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6170 | //===----------------------------------------------------------------------===// |
| 6171 | // Initialization helper functions |
| 6172 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6173 | bool |
| 6174 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 6175 | ExprResult Init) { |
| 6176 | if (Init.isInvalid()) |
| 6177 | return false; |
| 6178 | |
| 6179 | Expr *InitE = Init.get(); |
| 6180 | assert(InitE && "No initialization expression"); |
| 6181 | |
| 6182 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 6183 | SourceLocation()); |
| 6184 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 6185 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6186 | } |
| 6187 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6188 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6189 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 6190 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6191 | ExprResult Init, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6192 | bool TopLevelOfInitList, |
| 6193 | bool AllowExplicit) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6194 | if (Init.isInvalid()) |
| 6195 | return ExprError(); |
| 6196 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6197 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6198 | assert(InitE && "No initialization expression?"); |
| 6199 | |
| 6200 | if (EqualLoc.isInvalid()) |
| 6201 | EqualLoc = InitE->getLocStart(); |
| 6202 | |
| 6203 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6204 | EqualLoc, |
| 6205 | AllowExplicit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6206 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 6207 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6208 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6209 | ExprResult Result = Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
| 6210 | |
| 6211 | if (!Result.isInvalid() && TopLevelOfInitList) |
| 6212 | DiagnoseNarrowingInInitList(*this, Seq, Entity.getType(), |
| 6213 | InitE, Result.get()); |
| 6214 | |
| 6215 | return Result; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6216 | } |