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 | 262bc18 | 2012-04-30 02:36:29 +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); |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 690 | } else if (DeclType->isAggregateType()) { |
| 691 | if (DeclType->isRecordType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 692 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 693 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 694 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 695 | StructuredList, StructuredIndex, |
| 696 | TopLevelObject); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 697 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 698 | llvm::APSInt Zero( |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 699 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 700 | false); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 701 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 702 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 703 | StructuredList, StructuredIndex); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 704 | } else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 705 | llvm_unreachable("Aggregate that isn't a structure or array?!"); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 706 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 707 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 708 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 709 | if (!VerifyOnly) |
| 710 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 711 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 712 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 713 | } else if (DeclType->isRecordType()) { |
| 714 | // C++ [dcl.init]p14: |
| 715 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 716 | // is a brace-enclosed list, see 8.5.1. |
| 717 | // |
| 718 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 719 | // we have an initializer list and a destination type that is not |
| 720 | // an aggregate. |
| 721 | // FIXME: In C++0x, this is yet another form of initialization. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 722 | if (!VerifyOnly) |
| 723 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 724 | << DeclType << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 725 | hadError = true; |
| 726 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 727 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 728 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 729 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 730 | if (!VerifyOnly) |
| 731 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 732 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 733 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 734 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 735 | if (!VerifyOnly) |
| 736 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 737 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 738 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 742 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 743 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 745 | unsigned &Index, |
| 746 | InitListExpr *StructuredList, |
| 747 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 748 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 749 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 750 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 751 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 752 | InitListExpr *newStructuredList |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 753 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 754 | StructuredList, StructuredIndex, |
| 755 | SubInitList->getSourceRange()); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 756 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 757 | newStructuredList, newStructuredIndex); |
| 758 | ++StructuredIndex; |
| 759 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 760 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 761 | } else if (ElemType->isScalarType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 762 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 763 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 764 | } else if (ElemType->isReferenceType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 765 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 766 | StructuredList, StructuredIndex); |
| 767 | } |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 768 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 769 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 770 | // arrayType can be incomplete if we're initializing a flexible |
| 771 | // array member. There's nothing we can do with the completed |
| 772 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 773 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 774 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 775 | if (!VerifyOnly) { |
| 776 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 777 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 778 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 779 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 780 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 781 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 782 | |
| 783 | // Fall through for subaggregate initialization. |
| 784 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 785 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 786 | // C++ [dcl.init.aggr]p12: |
| 787 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 788 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 789 | // an initializer-list. If the initializer can initialize a |
| 790 | // member, the member is initialized. [...] |
| 791 | |
| 792 | // FIXME: Better EqualLoc? |
| 793 | InitializationKind Kind = |
| 794 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 795 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 796 | |
| 797 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 798 | if (!VerifyOnly) { |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 799 | ExprResult Result = |
| 800 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 801 | if (Result.isInvalid()) |
| 802 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 803 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 804 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 805 | Result.takeAs<Expr>()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 806 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 807 | ++Index; |
| 808 | return; |
| 809 | } |
| 810 | |
| 811 | // Fall through for subaggregate initialization |
| 812 | } else { |
| 813 | // C99 6.7.8p13: |
| 814 | // |
| 815 | // The initializer for a structure or union object that has |
| 816 | // automatic storage duration shall be either an initializer |
| 817 | // list as described below, or a single expression that has |
| 818 | // compatible structure or union type. In the latter case, the |
| 819 | // initial value of the object, including unnamed members, is |
| 820 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 821 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 822 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 823 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 824 | !VerifyOnly) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 825 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 826 | if (ExprRes.isInvalid()) |
| 827 | hadError = true; |
| 828 | else { |
| 829 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 830 | if (ExprRes.isInvalid()) |
| 831 | hadError = true; |
| 832 | } |
| 833 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 834 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 835 | ++Index; |
| 836 | return; |
| 837 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 838 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 839 | // Fall through for subaggregate initialization |
| 840 | } |
| 841 | |
| 842 | // C++ [dcl.init.aggr]p12: |
| 843 | // |
| 844 | // [...] Otherwise, if the member is itself a non-empty |
| 845 | // subaggregate, brace elision is assumed and the initializer is |
| 846 | // considered for the initialization of the first member of |
| 847 | // the subaggregate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 848 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 849 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 850 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 851 | StructuredIndex); |
| 852 | ++StructuredIndex; |
| 853 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 854 | if (!VerifyOnly) { |
| 855 | // We cannot initialize this element, so let |
| 856 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 857 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 858 | SemaRef.Owned(expr), |
| 859 | /*TopLevelOfInitList=*/true); |
| 860 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 861 | hadError = true; |
| 862 | ++Index; |
| 863 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 864 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 867 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 868 | InitListExpr *IList, QualType DeclType, |
| 869 | unsigned &Index, |
| 870 | InitListExpr *StructuredList, |
| 871 | unsigned &StructuredIndex) { |
| 872 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 873 | |
| 874 | // As an extension, clang supports complex initializers, which initialize |
| 875 | // a complex number component-wise. When an explicit initializer list for |
| 876 | // a complex number contains two two initializers, this extension kicks in: |
| 877 | // it exepcts the initializer list to contain two elements convertible to |
| 878 | // the element type of the complex type. The first element initializes |
| 879 | // the real part, and the second element intitializes the imaginary part. |
| 880 | |
| 881 | if (IList->getNumInits() != 2) |
| 882 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 883 | StructuredIndex); |
| 884 | |
| 885 | // This is an extension in C. (The builtin _Complex type does not exist |
| 886 | // in the C++ standard.) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 887 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 888 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 889 | << IList->getSourceRange(); |
| 890 | |
| 891 | // Initialize the complex number. |
| 892 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 893 | InitializedEntity ElementEntity = |
| 894 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 895 | |
| 896 | for (unsigned i = 0; i < 2; ++i) { |
| 897 | ElementEntity.setElementIndex(Index); |
| 898 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 899 | StructuredList, StructuredIndex); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 904 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 905 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 906 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 907 | InitListExpr *StructuredList, |
| 908 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 909 | if (Index >= IList->getNumInits()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 910 | if (!VerifyOnly) |
| 911 | SemaRef.Diag(IList->getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 912 | SemaRef.getLangOpts().CPlusPlus0x ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 913 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 914 | diag::err_empty_scalar_initializer) |
| 915 | << IList->getSourceRange(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 916 | hadError = !SemaRef.getLangOpts().CPlusPlus0x; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 917 | ++Index; |
| 918 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 919 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 920 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 921 | |
| 922 | Expr *expr = IList->getInit(Index); |
| 923 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 924 | if (!VerifyOnly) |
| 925 | SemaRef.Diag(SubIList->getLocStart(), |
| 926 | diag::warn_many_braces_around_scalar_init) |
| 927 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 928 | |
| 929 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 930 | StructuredIndex); |
| 931 | return; |
| 932 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 933 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 934 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 935 | diag::err_designator_for_scalar_init) |
| 936 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 937 | hadError = true; |
| 938 | ++Index; |
| 939 | ++StructuredIndex; |
| 940 | return; |
| 941 | } |
| 942 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 943 | if (VerifyOnly) { |
| 944 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 945 | hadError = true; |
| 946 | ++Index; |
| 947 | return; |
| 948 | } |
| 949 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 950 | ExprResult Result = |
| 951 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 952 | SemaRef.Owned(expr), |
| 953 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 954 | |
| 955 | Expr *ResultExpr = 0; |
| 956 | |
| 957 | if (Result.isInvalid()) |
| 958 | hadError = true; // types weren't compatible. |
| 959 | else { |
| 960 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 961 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 962 | if (ResultExpr != expr) { |
| 963 | // The type was promoted, update initializer list. |
| 964 | IList->setInit(Index, ResultExpr); |
| 965 | } |
| 966 | } |
| 967 | if (hadError) |
| 968 | ++StructuredIndex; |
| 969 | else |
| 970 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 971 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 974 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 975 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 976 | unsigned &Index, |
| 977 | InitListExpr *StructuredList, |
| 978 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 979 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 980 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 981 | // general, it would be useful to pass location information down the stack, |
| 982 | // so that we know the location (or decl) of the "current object" being |
| 983 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 984 | if (!VerifyOnly) |
| 985 | SemaRef.Diag(IList->getLocStart(), |
| 986 | diag::err_init_reference_member_uninitialized) |
| 987 | << DeclType |
| 988 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 989 | hadError = true; |
| 990 | ++Index; |
| 991 | ++StructuredIndex; |
| 992 | return; |
| 993 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 994 | |
| 995 | Expr *expr = IList->getInit(Index); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 996 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus0x) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 997 | if (!VerifyOnly) |
| 998 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 999 | << DeclType << IList->getSourceRange(); |
| 1000 | hadError = true; |
| 1001 | ++Index; |
| 1002 | ++StructuredIndex; |
| 1003 | return; |
| 1004 | } |
| 1005 | |
| 1006 | if (VerifyOnly) { |
| 1007 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1008 | hadError = true; |
| 1009 | ++Index; |
| 1010 | return; |
| 1011 | } |
| 1012 | |
| 1013 | ExprResult Result = |
| 1014 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1015 | SemaRef.Owned(expr), |
| 1016 | /*TopLevelOfInitList=*/true); |
| 1017 | |
| 1018 | if (Result.isInvalid()) |
| 1019 | hadError = true; |
| 1020 | |
| 1021 | expr = Result.takeAs<Expr>(); |
| 1022 | IList->setInit(Index, expr); |
| 1023 | |
| 1024 | if (hadError) |
| 1025 | ++StructuredIndex; |
| 1026 | else |
| 1027 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1028 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1031 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1032 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1033 | unsigned &Index, |
| 1034 | InitListExpr *StructuredList, |
| 1035 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1036 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1037 | unsigned maxElements = VT->getNumElements(); |
| 1038 | unsigned numEltsInit = 0; |
| 1039 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1040 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1041 | if (Index >= IList->getNumInits()) { |
| 1042 | // Make sure the element type can be value-initialized. |
| 1043 | if (VerifyOnly) |
| 1044 | CheckValueInitializable( |
| 1045 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1046 | return; |
| 1047 | } |
| 1048 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1049 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1050 | // If the initializing element is a vector, try to copy-initialize |
| 1051 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1052 | Expr *Init = IList->getInit(Index); |
| 1053 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1054 | if (VerifyOnly) { |
| 1055 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1056 | hadError = true; |
| 1057 | ++Index; |
| 1058 | return; |
| 1059 | } |
| 1060 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1061 | ExprResult Result = |
| 1062 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1063 | SemaRef.Owned(Init), |
| 1064 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1065 | |
| 1066 | Expr *ResultExpr = 0; |
| 1067 | if (Result.isInvalid()) |
| 1068 | hadError = true; // types weren't compatible. |
| 1069 | else { |
| 1070 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1071 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1072 | if (ResultExpr != Init) { |
| 1073 | // The type was promoted, update initializer list. |
| 1074 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1075 | } |
| 1076 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1077 | if (hadError) |
| 1078 | ++StructuredIndex; |
| 1079 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1080 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1081 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1082 | ++Index; |
| 1083 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1084 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1086 | InitializedEntity ElementEntity = |
| 1087 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1088 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1089 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1090 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1091 | if (Index >= IList->getNumInits()) { |
| 1092 | if (VerifyOnly) |
| 1093 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1094 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1095 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1096 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1097 | ElementEntity.setElementIndex(Index); |
| 1098 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1099 | StructuredList, StructuredIndex); |
| 1100 | } |
| 1101 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1102 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1103 | |
| 1104 | InitializedEntity ElementEntity = |
| 1105 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1106 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1107 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1108 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1109 | // Don't attempt to go past the end of the init list |
| 1110 | if (Index >= IList->getNumInits()) |
| 1111 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1112 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1113 | ElementEntity.setElementIndex(Index); |
| 1114 | |
| 1115 | QualType IType = IList->getInit(Index)->getType(); |
| 1116 | if (!IType->isVectorType()) { |
| 1117 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1118 | StructuredList, StructuredIndex); |
| 1119 | ++numEltsInit; |
| 1120 | } else { |
| 1121 | QualType VecType; |
| 1122 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1123 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1124 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1125 | if (IType->isExtVectorType()) |
| 1126 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1127 | else |
| 1128 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1129 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1130 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1131 | StructuredList, StructuredIndex); |
| 1132 | numEltsInit += numIElts; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1137 | if (numEltsInit != maxElements) { |
| 1138 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1139 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1140 | diag::err_vector_incorrect_num_initializers) |
| 1141 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1142 | hadError = true; |
| 1143 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1146 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1147 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1148 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1150 | unsigned &Index, |
| 1151 | InitListExpr *StructuredList, |
| 1152 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1153 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1154 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1155 | // Check for the special-case of initializing an array with a string. |
| 1156 | if (Index < IList->getNumInits()) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1157 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1158 | SemaRef.Context)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1159 | // We place the string literal directly into the resulting |
| 1160 | // initializer list. This is the only place where the structure |
| 1161 | // of the structured initializer list doesn't match exactly, |
| 1162 | // because doing so would involve allocating one character |
| 1163 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1164 | if (!VerifyOnly) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1165 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1166 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 1167 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1168 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1169 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1170 | return; |
| 1171 | } |
| 1172 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1173 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1174 | // Check for VLAs; in standard C it would be possible to check this |
| 1175 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1176 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1177 | if (!VerifyOnly) |
| 1178 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1179 | diag::err_variable_object_no_init) |
| 1180 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1181 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1182 | ++Index; |
| 1183 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1184 | return; |
| 1185 | } |
| 1186 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1187 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1188 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1189 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1190 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1191 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1192 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1193 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1194 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1195 | maxElementsKnown = true; |
| 1196 | } |
| 1197 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1198 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1199 | while (Index < IList->getNumInits()) { |
| 1200 | Expr *Init = IList->getInit(Index); |
| 1201 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1202 | // If we're not the subobject that matches up with the '{' for |
| 1203 | // the designator, we shouldn't be handling the |
| 1204 | // designator. Return immediately. |
| 1205 | if (!SubobjectIsDesignatorContext) |
| 1206 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1207 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1208 | // Handle this designated initializer. elementIndex will be |
| 1209 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1210 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1211 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1212 | StructuredList, StructuredIndex, true, |
| 1213 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1214 | hadError = true; |
| 1215 | continue; |
| 1216 | } |
| 1217 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1218 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1219 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1220 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1221 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1222 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1223 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1224 | // If the array is of incomplete type, keep track of the number of |
| 1225 | // elements in the initializer. |
| 1226 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1227 | maxElements = elementIndex; |
| 1228 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1229 | continue; |
| 1230 | } |
| 1231 | |
| 1232 | // If we know the maximum number of elements, and we've already |
| 1233 | // hit it, stop consuming elements in the initializer list. |
| 1234 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1235 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1236 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1237 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1238 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1239 | Entity); |
| 1240 | // Check this element. |
| 1241 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1242 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1243 | ++elementIndex; |
| 1244 | |
| 1245 | // If the array is of incomplete type, keep track of the number of |
| 1246 | // elements in the initializer. |
| 1247 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1248 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1249 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1250 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1251 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1252 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1253 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1254 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1255 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1256 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1257 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1258 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1259 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1260 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1262 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1263 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1264 | if (!hadError && VerifyOnly) { |
| 1265 | // Check if there are any members of the array that get value-initialized. |
| 1266 | // If so, check if doing that is possible. |
| 1267 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1268 | if (maxElementsKnown && elementIndex < maxElements) |
| 1269 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1270 | SemaRef.Context, 0, Entity)); |
| 1271 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1274 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1275 | Expr *InitExpr, |
| 1276 | FieldDecl *Field, |
| 1277 | bool TopLevelObject) { |
| 1278 | // Handle GNU flexible array initializers. |
| 1279 | unsigned FlexArrayDiag; |
| 1280 | if (isa<InitListExpr>(InitExpr) && |
| 1281 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1282 | // Empty flexible array init always allowed as an extension |
| 1283 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1284 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1285 | // Disallow flexible array init in C++; it is not required for gcc |
| 1286 | // compatibility, and it needs work to IRGen correctly in general. |
| 1287 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1288 | } else if (!TopLevelObject) { |
| 1289 | // Disallow flexible array init on non-top-level object |
| 1290 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1291 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1292 | // Disallow flexible array init on anything which is not a variable. |
| 1293 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1294 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1295 | // Disallow flexible array init on local variables. |
| 1296 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1297 | } else { |
| 1298 | // Allow other cases. |
| 1299 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1300 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1301 | |
| 1302 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1303 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1304 | FlexArrayDiag) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1305 | << InitExpr->getLocStart(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1306 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1307 | << Field; |
| 1308 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1309 | |
| 1310 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1311 | } |
| 1312 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1313 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1314 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1316 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1318 | unsigned &Index, |
| 1319 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1320 | unsigned &StructuredIndex, |
| 1321 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1322 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1324 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1325 | // confusion, we forgo checking the intializer for the entire record. |
| 1326 | if (structDecl->isInvalidDecl()) { |
| 1327 | hadError = true; |
| 1328 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1330 | |
| 1331 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1332 | // Value-initialize the first named member of the union. |
| 1333 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 1334 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1335 | Field != FieldEnd; ++Field) { |
| 1336 | if (Field->getDeclName()) { |
| 1337 | if (VerifyOnly) |
| 1338 | CheckValueInitializable( |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1339 | InitializedEntity::InitializeMember(&*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1340 | else |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1341 | StructuredList->setInitializedFieldInUnion(&*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1342 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | return; |
| 1346 | } |
| 1347 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1348 | // If structDecl is a forward declaration, this loop won't do |
| 1349 | // anything except look at designated initializers; That's okay, |
| 1350 | // because an error should get printed out elsewhere. It might be |
| 1351 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1352 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1353 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1354 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1355 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1356 | while (Index < IList->getNumInits()) { |
| 1357 | Expr *Init = IList->getInit(Index); |
| 1358 | |
| 1359 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1360 | // If we're not the subobject that matches up with the '{' for |
| 1361 | // the designator, we shouldn't be handling the |
| 1362 | // designator. Return immediately. |
| 1363 | if (!SubobjectIsDesignatorContext) |
| 1364 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1365 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1366 | // Handle this designated initializer. Field will be updated to |
| 1367 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1368 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1369 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1370 | StructuredList, StructuredIndex, |
| 1371 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1372 | hadError = true; |
| 1373 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1374 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1375 | |
| 1376 | // Disable check for missing fields when designators are used. |
| 1377 | // This matches gcc behaviour. |
| 1378 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1379 | continue; |
| 1380 | } |
| 1381 | |
| 1382 | if (Field == FieldEnd) { |
| 1383 | // We've run out of fields. We're done. |
| 1384 | break; |
| 1385 | } |
| 1386 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1387 | // We've already initialized a member of a union. We're done. |
| 1388 | if (InitializedSomething && DeclType->isUnionType()) |
| 1389 | break; |
| 1390 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1391 | // If we've hit the flexible array member at the end, we're done. |
| 1392 | if (Field->getType()->isIncompleteArrayType()) |
| 1393 | break; |
| 1394 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1395 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1396 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1397 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1398 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1399 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1400 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1401 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1402 | bool InvalidUse; |
| 1403 | if (VerifyOnly) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1404 | InvalidUse = !SemaRef.CanUseDecl(&*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1405 | else |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1406 | InvalidUse = SemaRef.DiagnoseUseOfDecl(&*Field, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1407 | IList->getInit(Index)->getLocStart()); |
| 1408 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1409 | ++Index; |
| 1410 | ++Field; |
| 1411 | hadError = true; |
| 1412 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1413 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1414 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1415 | InitializedEntity MemberEntity = |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1416 | InitializedEntity::InitializeMember(&*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1417 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1418 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1419 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1420 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1421 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1422 | // Initialize the first field within the union. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1423 | StructuredList->setInitializedFieldInUnion(&*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1424 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1425 | |
| 1426 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1427 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1428 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1429 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1430 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1431 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1432 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1433 | // It is possible we have one or more unnamed bitfields remaining. |
| 1434 | // Find first (if any) named field and emit warning. |
| 1435 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1436 | it != end; ++it) { |
| 1437 | if (!it->isUnnamedBitfield()) { |
| 1438 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1439 | diag::warn_missing_field_initializers) << it->getName(); |
| 1440 | break; |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1445 | // Check that any remaining fields can be value-initialized. |
| 1446 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1447 | !Field->getType()->isIncompleteArrayType()) { |
| 1448 | // FIXME: Should check for holes left by designated initializers too. |
| 1449 | for (; Field != FieldEnd && !hadError; ++Field) { |
| 1450 | if (!Field->isUnnamedBitfield()) |
| 1451 | CheckValueInitializable( |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1452 | InitializedEntity::InitializeMember(&*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1457 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1458 | return; |
| 1459 | |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1460 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), &*Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1461 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1462 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1463 | ++Index; |
| 1464 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1467 | InitializedEntity MemberEntity = |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1468 | InitializedEntity::InitializeMember(&*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1469 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1470 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1471 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1472 | StructuredList, StructuredIndex); |
| 1473 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1474 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1475 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1476 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1477 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1478 | /// \brief Expand a field designator that refers to a member of an |
| 1479 | /// anonymous struct or union into a series of field designators that |
| 1480 | /// refers to the field within the appropriate subobject. |
| 1481 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1482 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | DesignatedInitExpr *DIE, |
| 1484 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1485 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1486 | typedef DesignatedInitExpr::Designator Designator; |
| 1487 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1488 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1489 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1490 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1491 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1492 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1494 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1495 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1496 | else |
| 1497 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1498 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1499 | assert(isa<FieldDecl>(*PI)); |
| 1500 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | // Expand the current designator into the set of replacement |
| 1504 | // designators, so we have a full subobject path down to where the |
| 1505 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1506 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1507 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1508 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1510 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1511 | /// corresponds to FieldName. |
| 1512 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1513 | IdentifierInfo *FieldName) { |
| 1514 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1515 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 3e78b19 | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1516 | while (IndirectFieldDecl *IF = |
| 1517 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1518 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1519 | return IF; |
| 1520 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1521 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1522 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1525 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1526 | DesignatedInitExpr *DIE) { |
| 1527 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1528 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1529 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1530 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1531 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
| 1532 | DIE->size(), IndexExprs.data(), |
| 1533 | NumIndexExprs, DIE->getEqualOrColonLoc(), |
| 1534 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1535 | } |
| 1536 | |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1537 | namespace { |
| 1538 | |
| 1539 | // Callback to only accept typo corrections that are for field members of |
| 1540 | // the given struct or union. |
| 1541 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1542 | public: |
| 1543 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1544 | : Record(RD) {} |
| 1545 | |
| 1546 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 1547 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1548 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1549 | } |
| 1550 | |
| 1551 | private: |
| 1552 | RecordDecl *Record; |
| 1553 | }; |
| 1554 | |
| 1555 | } |
| 1556 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1557 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1558 | /// |
| 1559 | /// Determines whether the designated initializer @p DIE, which |
| 1560 | /// resides at the given @p Index within the initializer list @p |
| 1561 | /// IList, is well-formed for a current object of type @p DeclType |
| 1562 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1564 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1565 | /// |
| 1566 | /// @param IList The initializer list in which this designated |
| 1567 | /// initializer occurs. |
| 1568 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1569 | /// @param DIE The designated initializer expression. |
| 1570 | /// |
| 1571 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1572 | /// |
| 1573 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1574 | /// into which the designation in @p DIE should refer. |
| 1575 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1576 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1577 | /// a field, this will be set to the field declaration corresponding |
| 1578 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1579 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1580 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1581 | /// DIE is an array designator or GNU array-range designator, this |
| 1582 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1583 | /// |
| 1584 | /// @param Index Index into @p IList where the designated initializer |
| 1585 | /// @p DIE occurs. |
| 1586 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1587 | /// @param StructuredList The initializer list expression that |
| 1588 | /// describes all of the subobject initializers in the order they'll |
| 1589 | /// actually be initialized. |
| 1590 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1591 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1593 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1594 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1595 | DesignatedInitExpr *DIE, |
| 1596 | unsigned DesigIdx, |
| 1597 | QualType &CurrentObjectType, |
| 1598 | RecordDecl::field_iterator *NextField, |
| 1599 | llvm::APSInt *NextElementIndex, |
| 1600 | unsigned &Index, |
| 1601 | InitListExpr *StructuredList, |
| 1602 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1603 | bool FinishSubobjectInit, |
| 1604 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1605 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1606 | // Check the actual initialization for the designated object type. |
| 1607 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1608 | |
| 1609 | // Temporarily remove the designator expression from the |
| 1610 | // initializer list that the child calls see, so that we don't try |
| 1611 | // to re-process the designator. |
| 1612 | unsigned OldIndex = Index; |
| 1613 | IList->setInit(OldIndex, DIE->getInit()); |
| 1614 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1615 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1616 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1617 | |
| 1618 | // Restore the designated initializer expression in the syntactic |
| 1619 | // form of the initializer list. |
| 1620 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1621 | DIE->setInit(IList->getInit(OldIndex)); |
| 1622 | IList->setInit(OldIndex, DIE); |
| 1623 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1624 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1627 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1628 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1629 | if (!VerifyOnly) { |
| 1630 | assert((IsFirstDesignator || StructuredList) && |
| 1631 | "Need a non-designated initializer list to start from"); |
| 1632 | |
| 1633 | // Determine the structural initializer list that corresponds to the |
| 1634 | // current subobject. |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1635 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1636 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1637 | StructuredList, StructuredIndex, |
| 1638 | SourceRange(D->getStartLocation(), |
| 1639 | DIE->getSourceRange().getEnd())); |
| 1640 | assert(StructuredList && "Expected a structured initializer list"); |
| 1641 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1642 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1643 | if (D->isFieldDesignator()) { |
| 1644 | // C99 6.7.8p7: |
| 1645 | // |
| 1646 | // If a designator has the form |
| 1647 | // |
| 1648 | // . identifier |
| 1649 | // |
| 1650 | // then the current object (defined below) shall have |
| 1651 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1652 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1653 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1654 | if (!RT) { |
| 1655 | SourceLocation Loc = D->getDotLoc(); |
| 1656 | if (Loc.isInvalid()) |
| 1657 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1658 | if (!VerifyOnly) |
| 1659 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1660 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1661 | ++Index; |
| 1662 | return true; |
| 1663 | } |
| 1664 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1665 | // Note: we perform a linear search of the fields here, despite |
| 1666 | // the fact that we have a faster lookup method, because we always |
| 1667 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1668 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1669 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1670 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1672 | Field = RT->getDecl()->field_begin(), |
| 1673 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1674 | for (; Field != FieldEnd; ++Field) { |
| 1675 | if (Field->isUnnamedBitfield()) |
| 1676 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1677 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1678 | // If we find a field representing an anonymous field, look in the |
| 1679 | // IndirectFieldDecl that follow for the designated initializer. |
| 1680 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1681 | if (IndirectFieldDecl *IF = |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1682 | FindIndirectFieldDesignator(&*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1683 | // In verify mode, don't modify the original. |
| 1684 | if (VerifyOnly) |
| 1685 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1686 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1687 | D = DIE->getDesignator(DesigIdx); |
| 1688 | break; |
| 1689 | } |
| 1690 | } |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1691 | if (KnownField && KnownField == &*Field) |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1692 | break; |
| 1693 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1694 | break; |
| 1695 | |
| 1696 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1699 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1700 | if (VerifyOnly) { |
| 1701 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1702 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1703 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1704 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1705 | // There was no normal field in the struct with the designated |
| 1706 | // name. Perform another lookup for this name, which may find |
| 1707 | // something that we can't designate (e.g., a member function), |
| 1708 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1710 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1711 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1712 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1713 | // Name lookup didn't find anything. Determine whether this |
| 1714 | // was a typo for another field name. |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1715 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1716 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1717 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1718 | Sema::LookupMemberName, /*Scope=*/0, /*SS=*/0, Validator, |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1719 | RT->getDecl()); |
| 1720 | if (Corrected) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1721 | std::string CorrectedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1722 | Corrected.getAsString(SemaRef.getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1723 | std::string CorrectedQuotedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1724 | Corrected.getQuoted(SemaRef.getLangOpts())); |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1725 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1726 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1727 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1728 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1729 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1730 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1731 | diag::note_previous_decl) << CorrectedQuotedStr; |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1732 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1733 | } else { |
| 1734 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1735 | << FieldName << CurrentObjectType; |
| 1736 | ++Index; |
| 1737 | return true; |
| 1738 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1739 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1740 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1741 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1742 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1743 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1744 | << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1745 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1746 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1747 | ++Index; |
| 1748 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1749 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1750 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1751 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1752 | // The replacement field comes from typo correction; find it |
| 1753 | // in the list of fields. |
| 1754 | FieldIndex = 0; |
| 1755 | Field = RT->getDecl()->field_begin(); |
| 1756 | for (; Field != FieldEnd; ++Field) { |
| 1757 | if (Field->isUnnamedBitfield()) |
| 1758 | continue; |
| 1759 | |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1760 | if (ReplacementField == &*Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1761 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1762 | break; |
| 1763 | |
| 1764 | ++FieldIndex; |
| 1765 | } |
| 1766 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1767 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1768 | |
| 1769 | // All of the fields of a union are located at the same place in |
| 1770 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1771 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1772 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1773 | if (!VerifyOnly) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1774 | StructuredList->setInitializedFieldInUnion(&*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1775 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1776 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1777 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1778 | bool InvalidUse; |
| 1779 | if (VerifyOnly) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1780 | InvalidUse = !SemaRef.CanUseDecl(&*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1781 | else |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1782 | InvalidUse = SemaRef.DiagnoseUseOfDecl(&*Field, D->getFieldLoc()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1783 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1784 | ++Index; |
| 1785 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1786 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1787 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1788 | if (!VerifyOnly) { |
| 1789 | // Update the designator with the field declaration. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1790 | D->setField(&*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1792 | // Make sure that our non-designated initializer list has space |
| 1793 | // for a subobject corresponding to this field. |
| 1794 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1795 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1796 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1797 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1798 | // This designator names a flexible array member. |
| 1799 | if (Field->getType()->isIncompleteArrayType()) { |
| 1800 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1801 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1802 | // We can't designate an object within the flexible array |
| 1803 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1804 | if (!VerifyOnly) { |
| 1805 | DesignatedInitExpr::Designator *NextD |
| 1806 | = DIE->getDesignator(DesigIdx + 1); |
| 1807 | SemaRef.Diag(NextD->getStartLocation(), |
| 1808 | diag::err_designator_into_flexible_array_member) |
| 1809 | << SourceRange(NextD->getStartLocation(), |
| 1810 | DIE->getSourceRange().getEnd()); |
| 1811 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1812 | << &*Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1813 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1814 | Invalid = true; |
| 1815 | } |
| 1816 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1817 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1818 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1819 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1820 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1821 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1822 | diag::err_flexible_array_init_needs_braces) |
| 1823 | << DIE->getInit()->getSourceRange(); |
| 1824 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1825 | << &*Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1826 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1827 | Invalid = true; |
| 1828 | } |
| 1829 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1830 | // Check GNU flexible array initializer. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1831 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), &*Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1832 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1833 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1834 | |
| 1835 | if (Invalid) { |
| 1836 | ++Index; |
| 1837 | return true; |
| 1838 | } |
| 1839 | |
| 1840 | // Initialize the array. |
| 1841 | bool prevHadError = hadError; |
| 1842 | unsigned newStructuredIndex = FieldIndex; |
| 1843 | unsigned OldIndex = Index; |
| 1844 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1845 | |
| 1846 | InitializedEntity MemberEntity = |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1847 | InitializedEntity::InitializeMember(&*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1848 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1849 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1850 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1851 | IList->setInit(OldIndex, DIE); |
| 1852 | if (hadError && !prevHadError) { |
| 1853 | ++Field; |
| 1854 | ++FieldIndex; |
| 1855 | if (NextField) |
| 1856 | *NextField = Field; |
| 1857 | StructuredIndex = FieldIndex; |
| 1858 | return true; |
| 1859 | } |
| 1860 | } else { |
| 1861 | // Recurse to check later designated subobjects. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1862 | QualType FieldType = Field->getType(); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1863 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1864 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1865 | InitializedEntity MemberEntity = |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1866 | InitializedEntity::InitializeMember(&*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1867 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1868 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1869 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1870 | true, false)) |
| 1871 | return true; |
| 1872 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1873 | |
| 1874 | // Find the position of the next field to be initialized in this |
| 1875 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1876 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1877 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1878 | |
| 1879 | // If this the first designator, our caller will continue checking |
| 1880 | // the rest of this struct/class/union subobject. |
| 1881 | if (IsFirstDesignator) { |
| 1882 | if (NextField) |
| 1883 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1884 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1885 | return false; |
| 1886 | } |
| 1887 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1888 | if (!FinishSubobjectInit) |
| 1889 | return false; |
| 1890 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1891 | // We've already initialized something in the union; we're done. |
| 1892 | if (RT->getDecl()->isUnion()) |
| 1893 | return hadError; |
| 1894 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1895 | // Check the remaining fields within this class/struct/union subobject. |
| 1896 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1897 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1898 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1899 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1900 | return hadError && !prevHadError; |
| 1901 | } |
| 1902 | |
| 1903 | // C99 6.7.8p6: |
| 1904 | // |
| 1905 | // If a designator has the form |
| 1906 | // |
| 1907 | // [ constant-expression ] |
| 1908 | // |
| 1909 | // then the current object (defined below) shall have array |
| 1910 | // type and the expression shall be an integer constant |
| 1911 | // expression. If the array is of unknown size, any |
| 1912 | // nonnegative value is valid. |
| 1913 | // |
| 1914 | // Additionally, cope with the GNU extension that permits |
| 1915 | // designators of the form |
| 1916 | // |
| 1917 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1918 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1919 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1920 | if (!VerifyOnly) |
| 1921 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 1922 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1923 | ++Index; |
| 1924 | return true; |
| 1925 | } |
| 1926 | |
| 1927 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1928 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1929 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1930 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1931 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1932 | DesignatedEndIndex = DesignatedStartIndex; |
| 1933 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1934 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1935 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1937 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1939 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1940 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1941 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1942 | // Codegen can't handle evaluating array range designators that have side |
| 1943 | // effects, because we replicate the AST value for each initialized element. |
| 1944 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1945 | // elements with something that has a side effect, so codegen can emit an |
| 1946 | // "error unsupported" error instead of miscompiling the app. |
| 1947 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1948 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1949 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1952 | if (isa<ConstantArrayType>(AT)) { |
| 1953 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1954 | DesignatedStartIndex |
| 1955 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1956 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1957 | DesignatedEndIndex |
| 1958 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1959 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1960 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 1961 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1962 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1963 | diag::err_array_designator_too_large) |
| 1964 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 1965 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1966 | ++Index; |
| 1967 | return true; |
| 1968 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1969 | } else { |
| 1970 | // Make sure the bit-widths and signedness match. |
| 1971 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1972 | DesignatedEndIndex |
| 1973 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1974 | else if (DesignatedStartIndex.getBitWidth() < |
| 1975 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1976 | DesignatedStartIndex |
| 1977 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1978 | DesignatedStartIndex.setIsUnsigned(true); |
| 1979 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1980 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1982 | // Make sure that our non-designated initializer list has space |
| 1983 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1984 | if (!VerifyOnly && |
| 1985 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1987 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1988 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1989 | // Repeatedly perform subobject initializations in the range |
| 1990 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1991 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1992 | // Move to the next designator |
| 1993 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1994 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1995 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1996 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1997 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1998 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1999 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2000 | // Recurse to check later designated subobjects. |
| 2001 | QualType ElementType = AT->getElementType(); |
| 2002 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2003 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2004 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2005 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 2006 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2007 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2008 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2009 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2010 | return true; |
| 2011 | |
| 2012 | // Move to the next index in the array that we'll be initializing. |
| 2013 | ++DesignatedStartIndex; |
| 2014 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2015 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2016 | |
| 2017 | // If this the first designator, our caller will continue checking |
| 2018 | // the rest of this array subobject. |
| 2019 | if (IsFirstDesignator) { |
| 2020 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2021 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2022 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2023 | return false; |
| 2024 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2026 | if (!FinishSubobjectInit) |
| 2027 | return false; |
| 2028 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2029 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2030 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2031 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2032 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2033 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2037 | // Get the structured initializer list for a subobject of type |
| 2038 | // @p CurrentObjectType. |
| 2039 | InitListExpr * |
| 2040 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2041 | QualType CurrentObjectType, |
| 2042 | InitListExpr *StructuredList, |
| 2043 | unsigned StructuredIndex, |
| 2044 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2045 | if (VerifyOnly) |
| 2046 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2047 | Expr *ExistingInit = 0; |
| 2048 | if (!StructuredList) |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2049 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2050 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2051 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2053 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2054 | return Result; |
| 2055 | |
| 2056 | if (ExistingInit) { |
| 2057 | // We are creating an initializer list that initializes the |
| 2058 | // subobjects of the current object, but there was already an |
| 2059 | // initialization that completely initialized the current |
| 2060 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2062 | // struct X { int a, b; }; |
| 2063 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2065 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2066 | // designated initializer re-initializes the whole |
| 2067 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2068 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2069 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2070 | << InitRange; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2071 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2072 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2073 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2074 | << ExistingInit->getSourceRange(); |
| 2075 | } |
| 2076 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2077 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2078 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 2079 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2080 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2081 | |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2082 | QualType ResultType = CurrentObjectType; |
| 2083 | if (!ResultType->isArrayType()) |
| 2084 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2085 | Result->setType(ResultType); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2086 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2087 | // Pre-allocate storage for the structured initializer list. |
| 2088 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2089 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2090 | bool GotNumInits = false; |
| 2091 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2092 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2093 | GotNumInits = true; |
| 2094 | } else if (Index < IList->getNumInits()) { |
| 2095 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2096 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2097 | GotNumInits = true; |
| 2098 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2102 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2103 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2104 | NumElements = CAType->getSize().getZExtValue(); |
| 2105 | // Simple heuristic so that we don't allocate a very large |
| 2106 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2107 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2108 | NumElements = 0; |
| 2109 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2110 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2111 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2112 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2113 | RecordDecl *RDecl = RType->getDecl(); |
| 2114 | if (RDecl->isUnion()) |
| 2115 | NumElements = 1; |
| 2116 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2118 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2121 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2122 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2123 | // Link this new initializer list into the structured initializer |
| 2124 | // lists. |
| 2125 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2126 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2127 | else { |
| 2128 | Result->setSyntacticForm(IList); |
| 2129 | SyntacticToSemantic[IList] = Result; |
| 2130 | } |
| 2131 | |
| 2132 | return Result; |
| 2133 | } |
| 2134 | |
| 2135 | /// Update the initializer at index @p StructuredIndex within the |
| 2136 | /// structured initializer list to the value @p expr. |
| 2137 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2138 | unsigned &StructuredIndex, |
| 2139 | Expr *expr) { |
| 2140 | // No structured initializer list to update |
| 2141 | if (!StructuredList) |
| 2142 | return; |
| 2143 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2144 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2145 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2146 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2147 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2148 | diag::warn_initializer_overrides) |
| 2149 | << expr->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2150 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2151 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2152 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2153 | << PrevInit->getSourceRange(); |
| 2154 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2156 | ++StructuredIndex; |
| 2157 | } |
| 2158 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2159 | /// Check that the given Index expression is a valid array designator |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2160 | /// value. This is essentially just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2161 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2162 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2163 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2164 | /// added, on success. If everything went okay, Value will receive the |
| 2165 | /// value of the constant expression. |
| 2166 | static ExprResult |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2167 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2168 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2169 | |
| 2170 | // Make sure this is an integer constant expression. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2171 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2172 | if (Result.isInvalid()) |
| 2173 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2174 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2175 | if (Value.isSigned() && Value.isNegative()) |
| 2176 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2177 | << Value.toString(10) << Index->getSourceRange(); |
| 2178 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2179 | Value.setIsUnsigned(true); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2180 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2183 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2184 | SourceLocation Loc, |
| 2185 | bool GNUSyntax, |
| 2186 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2187 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2188 | |
| 2189 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2190 | SmallVector<ASTDesignator, 32> Designators; |
| 2191 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2192 | |
| 2193 | // Build designators and check array designator expressions. |
| 2194 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2195 | const Designator &D = Desig.getDesignator(Idx); |
| 2196 | switch (D.getKind()) { |
| 2197 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2198 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2199 | D.getFieldLoc())); |
| 2200 | break; |
| 2201 | |
| 2202 | case Designator::ArrayDesignator: { |
| 2203 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2204 | llvm::APSInt IndexValue; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2205 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
| 2206 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).take(); |
| 2207 | if (!Index) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2208 | Invalid = true; |
| 2209 | else { |
| 2210 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2211 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2212 | D.getRBracketLoc())); |
| 2213 | InitExpressions.push_back(Index); |
| 2214 | } |
| 2215 | break; |
| 2216 | } |
| 2217 | |
| 2218 | case Designator::ArrayRangeDesignator: { |
| 2219 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2220 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2221 | llvm::APSInt StartValue; |
| 2222 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2223 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2224 | StartIndex->isValueDependent(); |
| 2225 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2226 | EndIndex->isValueDependent(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2227 | if (!StartDependent) |
| 2228 | StartIndex = |
| 2229 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).take(); |
| 2230 | if (!EndDependent) |
| 2231 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).take(); |
| 2232 | |
| 2233 | if (!StartIndex || !EndIndex) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2234 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2235 | else { |
| 2236 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2237 | if (StartDependent || EndDependent) { |
| 2238 | // Nothing to compute. |
| 2239 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2240 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2241 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2242 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2243 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2244 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2245 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2246 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2247 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2248 | Invalid = true; |
| 2249 | } else { |
| 2250 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2252 | D.getEllipsisLoc(), |
| 2253 | D.getRBracketLoc())); |
| 2254 | InitExpressions.push_back(StartIndex); |
| 2255 | InitExpressions.push_back(EndIndex); |
| 2256 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2257 | } |
| 2258 | break; |
| 2259 | } |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | if (Invalid || Init.isInvalid()) |
| 2264 | return ExprError(); |
| 2265 | |
| 2266 | // Clear out the expressions within the designation. |
| 2267 | Desig.ClearExprs(*this); |
| 2268 | |
| 2269 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2270 | = DesignatedInitExpr::Create(Context, |
| 2271 | Designators.data(), Designators.size(), |
| 2272 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 2273 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2274 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2275 | if (!getLangOpts().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2276 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2277 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2278 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2279 | return Owned(DIE); |
| 2280 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2281 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2282 | //===----------------------------------------------------------------------===// |
| 2283 | // Initialization entity |
| 2284 | //===----------------------------------------------------------------------===// |
| 2285 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2286 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2287 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2288 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2289 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2290 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2291 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2292 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2293 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2294 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2295 | Type = VT->getElementType(); |
| 2296 | } else { |
| 2297 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2298 | assert(CT && "Unexpected type"); |
| 2299 | Kind = EK_ComplexElement; |
| 2300 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2301 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2304 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2305 | CXXBaseSpecifier *Base, |
| 2306 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2307 | { |
| 2308 | InitializedEntity Result; |
| 2309 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2310 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2311 | if (IsInheritedVirtualBase) |
| 2312 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2313 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2314 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2315 | return Result; |
| 2316 | } |
| 2317 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2318 | DeclarationName InitializedEntity::getName() const { |
| 2319 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2320 | case EK_Parameter: { |
| 2321 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2322 | return (D ? D->getDeclName() : DeclarationName()); |
| 2323 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2324 | |
| 2325 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2326 | case EK_Member: |
| 2327 | return VariableOrMember->getDeclName(); |
| 2328 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2329 | case EK_LambdaCapture: |
| 2330 | return Capture.Var->getDeclName(); |
| 2331 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2332 | case EK_Result: |
| 2333 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2334 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2335 | case EK_Temporary: |
| 2336 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2337 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2338 | case EK_ArrayElement: |
| 2339 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2340 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2341 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2342 | return DeclarationName(); |
| 2343 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2344 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2345 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2348 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2349 | switch (getKind()) { |
| 2350 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2351 | case EK_Member: |
| 2352 | return VariableOrMember; |
| 2353 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2354 | case EK_Parameter: |
| 2355 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2356 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2357 | case EK_Result: |
| 2358 | case EK_Exception: |
| 2359 | case EK_New: |
| 2360 | case EK_Temporary: |
| 2361 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2362 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2363 | case EK_ArrayElement: |
| 2364 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2365 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2366 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2367 | case EK_LambdaCapture: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2368 | return 0; |
| 2369 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2370 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2371 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2374 | bool InitializedEntity::allowsNRVO() const { |
| 2375 | switch (getKind()) { |
| 2376 | case EK_Result: |
| 2377 | case EK_Exception: |
| 2378 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2379 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2380 | case EK_Variable: |
| 2381 | case EK_Parameter: |
| 2382 | case EK_Member: |
| 2383 | case EK_New: |
| 2384 | case EK_Temporary: |
| 2385 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2386 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2387 | case EK_ArrayElement: |
| 2388 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2389 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2390 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2391 | case EK_LambdaCapture: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2392 | break; |
| 2393 | } |
| 2394 | |
| 2395 | return false; |
| 2396 | } |
| 2397 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2398 | //===----------------------------------------------------------------------===// |
| 2399 | // Initialization sequence |
| 2400 | //===----------------------------------------------------------------------===// |
| 2401 | |
| 2402 | void InitializationSequence::Step::Destroy() { |
| 2403 | switch (Kind) { |
| 2404 | case SK_ResolveAddressOfOverloadedFunction: |
| 2405 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2406 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2407 | case SK_CastDerivedToBaseLValue: |
| 2408 | case SK_BindReference: |
| 2409 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2410 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2411 | case SK_UserConversion: |
| 2412 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2413 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2414 | case SK_QualificationConversionLValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2415 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2416 | case SK_ListConstructorCall: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2417 | case SK_UnwrapInitList: |
| 2418 | case SK_RewrapInitList: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2419 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2420 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2421 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2422 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2423 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2424 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2425 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2426 | case SK_PassByIndirectCopyRestore: |
| 2427 | case SK_PassByIndirectRestore: |
| 2428 | case SK_ProduceObjCObject: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2429 | case SK_StdInitializerList: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2430 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2431 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2432 | case SK_ConversionSequence: |
| 2433 | delete ICS; |
| 2434 | } |
| 2435 | } |
| 2436 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2437 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2438 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2442 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2443 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2444 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2445 | switch (getFailureKind()) { |
| 2446 | case FK_TooManyInitsForReference: |
| 2447 | case FK_ArrayNeedsInitList: |
| 2448 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2449 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2450 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2451 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2452 | case FK_RValueReferenceBindingToLValue: |
| 2453 | case FK_ReferenceInitDropsQualifiers: |
| 2454 | case FK_ReferenceInitFailed: |
| 2455 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2456 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2457 | case FK_TooManyInitsForScalar: |
| 2458 | case FK_ReferenceBindingToInitList: |
| 2459 | case FK_InitListBadDestinationType: |
| 2460 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2461 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2462 | case FK_ArrayTypeMismatch: |
| 2463 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2464 | case FK_ListInitializationFailed: |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2465 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2466 | case FK_PlaceholderType: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2467 | case FK_InitListElementCopyFailure: |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2468 | case FK_ExplicitConstructor: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2469 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2470 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2471 | case FK_ReferenceInitOverloadFailed: |
| 2472 | case FK_UserConversionOverloadFailed: |
| 2473 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2474 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2475 | return FailedOverloadResult == OR_Ambiguous; |
| 2476 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2477 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2478 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2481 | bool InitializationSequence::isConstructorInitialization() const { |
| 2482 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2483 | } |
| 2484 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2485 | void |
| 2486 | InitializationSequence |
| 2487 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2488 | DeclAccessPair Found, |
| 2489 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2490 | Step S; |
| 2491 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2492 | S.Type = Function->getType(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2493 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2494 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2495 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2496 | Steps.push_back(S); |
| 2497 | } |
| 2498 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2499 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2500 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2501 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2502 | switch (VK) { |
| 2503 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2504 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2505 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2506 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2507 | S.Type = BaseType; |
| 2508 | Steps.push_back(S); |
| 2509 | } |
| 2510 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2511 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2512 | bool BindingTemporary) { |
| 2513 | Step S; |
| 2514 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2515 | S.Type = T; |
| 2516 | Steps.push_back(S); |
| 2517 | } |
| 2518 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2519 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2520 | Step S; |
| 2521 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2522 | S.Type = T; |
| 2523 | Steps.push_back(S); |
| 2524 | } |
| 2525 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2526 | void |
| 2527 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2528 | DeclAccessPair FoundDecl, |
| 2529 | QualType T, |
| 2530 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2531 | Step S; |
| 2532 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2533 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2534 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2535 | S.Function.Function = Function; |
| 2536 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2537 | Steps.push_back(S); |
| 2538 | } |
| 2539 | |
| 2540 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2541 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2542 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2543 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2544 | switch (VK) { |
| 2545 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2546 | S.Kind = SK_QualificationConversionRValue; |
| 2547 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2548 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2549 | S.Kind = SK_QualificationConversionXValue; |
| 2550 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2551 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2552 | S.Kind = SK_QualificationConversionLValue; |
| 2553 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2554 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2555 | S.Type = Ty; |
| 2556 | Steps.push_back(S); |
| 2557 | } |
| 2558 | |
| 2559 | void InitializationSequence::AddConversionSequenceStep( |
| 2560 | const ImplicitConversionSequence &ICS, |
| 2561 | QualType T) { |
| 2562 | Step S; |
| 2563 | S.Kind = SK_ConversionSequence; |
| 2564 | S.Type = T; |
| 2565 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2566 | Steps.push_back(S); |
| 2567 | } |
| 2568 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2569 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2570 | Step S; |
| 2571 | S.Kind = SK_ListInitialization; |
| 2572 | S.Type = T; |
| 2573 | Steps.push_back(S); |
| 2574 | } |
| 2575 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2576 | void |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2577 | InitializationSequence |
| 2578 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2579 | AccessSpecifier Access, |
| 2580 | QualType T, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2581 | bool HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2582 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2583 | Step S; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2584 | S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall |
| 2585 | : SK_ConstructorInitialization; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2586 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2587 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2588 | S.Function.Function = Constructor; |
| 2589 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2590 | Steps.push_back(S); |
| 2591 | } |
| 2592 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2593 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2594 | Step S; |
| 2595 | S.Kind = SK_ZeroInitialization; |
| 2596 | S.Type = T; |
| 2597 | Steps.push_back(S); |
| 2598 | } |
| 2599 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2600 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2601 | Step S; |
| 2602 | S.Kind = SK_CAssignment; |
| 2603 | S.Type = T; |
| 2604 | Steps.push_back(S); |
| 2605 | } |
| 2606 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2607 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2608 | Step S; |
| 2609 | S.Kind = SK_StringInit; |
| 2610 | S.Type = T; |
| 2611 | Steps.push_back(S); |
| 2612 | } |
| 2613 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2614 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2615 | Step S; |
| 2616 | S.Kind = SK_ObjCObjectConversion; |
| 2617 | S.Type = T; |
| 2618 | Steps.push_back(S); |
| 2619 | } |
| 2620 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2621 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2622 | Step S; |
| 2623 | S.Kind = SK_ArrayInit; |
| 2624 | S.Type = T; |
| 2625 | Steps.push_back(S); |
| 2626 | } |
| 2627 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2628 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2629 | Step S; |
| 2630 | S.Kind = SK_ParenthesizedArrayInit; |
| 2631 | S.Type = T; |
| 2632 | Steps.push_back(S); |
| 2633 | } |
| 2634 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2635 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2636 | bool shouldCopy) { |
| 2637 | Step s; |
| 2638 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2639 | : SK_PassByIndirectRestore); |
| 2640 | s.Type = type; |
| 2641 | Steps.push_back(s); |
| 2642 | } |
| 2643 | |
| 2644 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2645 | Step S; |
| 2646 | S.Kind = SK_ProduceObjCObject; |
| 2647 | S.Type = T; |
| 2648 | Steps.push_back(S); |
| 2649 | } |
| 2650 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2651 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2652 | Step S; |
| 2653 | S.Kind = SK_StdInitializerList; |
| 2654 | S.Type = T; |
| 2655 | Steps.push_back(S); |
| 2656 | } |
| 2657 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2658 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2659 | InitListExpr *Syntactic) { |
| 2660 | assert(Syntactic->getNumInits() == 1 && |
| 2661 | "Can only rewrap trivial init lists."); |
| 2662 | Step S; |
| 2663 | S.Kind = SK_UnwrapInitList; |
| 2664 | S.Type = Syntactic->getInit(0)->getType(); |
| 2665 | Steps.insert(Steps.begin(), S); |
| 2666 | |
| 2667 | S.Kind = SK_RewrapInitList; |
| 2668 | S.Type = T; |
| 2669 | S.WrappingSyntacticList = Syntactic; |
| 2670 | Steps.push_back(S); |
| 2671 | } |
| 2672 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2673 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2674 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2675 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2676 | this->Failure = Failure; |
| 2677 | this->FailedOverloadResult = Result; |
| 2678 | } |
| 2679 | |
| 2680 | //===----------------------------------------------------------------------===// |
| 2681 | // Attempt initialization |
| 2682 | //===----------------------------------------------------------------------===// |
| 2683 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2684 | static void MaybeProduceObjCObject(Sema &S, |
| 2685 | InitializationSequence &Sequence, |
| 2686 | const InitializedEntity &Entity) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2687 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2688 | |
| 2689 | /// When initializing a parameter, produce the value if it's marked |
| 2690 | /// __attribute__((ns_consumed)). |
| 2691 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2692 | if (!Entity.isParameterConsumed()) |
| 2693 | return; |
| 2694 | |
| 2695 | assert(Entity.getType()->isObjCRetainableType() && |
| 2696 | "consuming an object of unretainable type?"); |
| 2697 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2698 | |
| 2699 | /// When initializing a return value, if the return type is a |
| 2700 | /// retainable type, then returns need to immediately retain the |
| 2701 | /// object. If an autorelease is required, it will be done at the |
| 2702 | /// last instant. |
| 2703 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2704 | if (!Entity.getType()->isObjCRetainableType()) |
| 2705 | return; |
| 2706 | |
| 2707 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2708 | } |
| 2709 | } |
| 2710 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2711 | /// \brief When initializing from init list via constructor, deal with the |
| 2712 | /// empty init list and std::initializer_list special cases. |
| 2713 | /// |
| 2714 | /// \return True if this was a special case, false otherwise. |
| 2715 | static bool TryListConstructionSpecialCases(Sema &S, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2716 | InitListExpr *List, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2717 | CXXRecordDecl *DestRecordDecl, |
| 2718 | QualType DestType, |
| 2719 | InitializationSequence &Sequence) { |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2720 | // C++11 [dcl.init.list]p3: |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 2721 | // List-initialization of an object or reference of type T is defined as |
| 2722 | // follows: |
| 2723 | // - If T is an aggregate, aggregate initialization is performed. |
| 2724 | if (DestType->isAggregateType()) |
| 2725 | return false; |
| 2726 | |
| 2727 | // - Otherwise, if the initializer list has no elements and T is a class |
| 2728 | // type with a default constructor, the object is value-initialized. |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2729 | if (List->getNumInits() == 0) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2730 | if (CXXConstructorDecl *DefaultConstructor = |
| 2731 | S.LookupDefaultConstructor(DestRecordDecl)) { |
| 2732 | if (DefaultConstructor->isDeleted() || |
| 2733 | S.isFunctionConsideredUnavailable(DefaultConstructor)) { |
| 2734 | // Fake an overload resolution failure. |
| 2735 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2736 | DeclAccessPair FoundDecl = DeclAccessPair::make(DefaultConstructor, |
| 2737 | DefaultConstructor->getAccess()); |
| 2738 | if (FunctionTemplateDecl *ConstructorTmpl = |
| 2739 | dyn_cast<FunctionTemplateDecl>(DefaultConstructor)) |
| 2740 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2741 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2742 | ArrayRef<Expr*>(), CandidateSet, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2743 | /*SuppressUserConversions*/ false); |
| 2744 | else |
| 2745 | S.AddOverloadCandidate(DefaultConstructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2746 | ArrayRef<Expr*>(), CandidateSet, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2747 | /*SuppressUserConversions*/ false); |
| 2748 | Sequence.SetOverloadFailure( |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2749 | InitializationSequence::FK_ListConstructorOverloadFailed, |
| 2750 | OR_Deleted); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2751 | } else |
| 2752 | Sequence.AddConstructorInitializationStep(DefaultConstructor, |
| 2753 | DefaultConstructor->getAccess(), |
| 2754 | DestType, |
| 2755 | /*MultipleCandidates=*/false, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2756 | /*FromInitList=*/true, |
| 2757 | /*AsInitList=*/false); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2758 | return true; |
| 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | // - Otherwise, if T is a specialization of std::initializer_list, [...] |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2763 | QualType E; |
| 2764 | if (S.isStdInitializerList(DestType, &E)) { |
| 2765 | // Check that each individual element can be copy-constructed. But since we |
| 2766 | // have no place to store further information, we'll recalculate everything |
| 2767 | // later. |
| 2768 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 2769 | S.Context.getConstantArrayType(E, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2770 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 2771 | List->getNumInits()), |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2772 | ArrayType::Normal, 0)); |
| 2773 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 2774 | 0, HiddenArray); |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2775 | for (unsigned i = 0, n = List->getNumInits(); i < n; ++i) { |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2776 | Element.setElementIndex(i); |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2777 | if (!S.CanPerformCopyInitialization(Element, List->getInit(i))) { |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2778 | Sequence.SetFailed( |
| 2779 | InitializationSequence::FK_InitListElementCopyFailure); |
| 2780 | return true; |
| 2781 | } |
| 2782 | } |
| 2783 | Sequence.AddStdInitializerListConstructionStep(DestType); |
| 2784 | return true; |
| 2785 | } |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2786 | |
| 2787 | // Not a special case. |
| 2788 | return false; |
| 2789 | } |
| 2790 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2791 | static OverloadingResult |
| 2792 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
| 2793 | Expr **Args, unsigned NumArgs, |
| 2794 | OverloadCandidateSet &CandidateSet, |
| 2795 | DeclContext::lookup_iterator Con, |
| 2796 | DeclContext::lookup_iterator ConEnd, |
| 2797 | OverloadCandidateSet::iterator &Best, |
| 2798 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2799 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2800 | CandidateSet.clear(); |
| 2801 | |
| 2802 | for (; Con != ConEnd; ++Con) { |
| 2803 | NamedDecl *D = *Con; |
| 2804 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2805 | bool SuppressUserConversions = false; |
| 2806 | |
| 2807 | // Find the constructor (which may be a template). |
| 2808 | CXXConstructorDecl *Constructor = 0; |
| 2809 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 2810 | if (ConstructorTmpl) |
| 2811 | Constructor = cast<CXXConstructorDecl>( |
| 2812 | ConstructorTmpl->getTemplatedDecl()); |
| 2813 | else { |
| 2814 | Constructor = cast<CXXConstructorDecl>(D); |
| 2815 | |
| 2816 | // If we're performing copy initialization using a copy constructor, we |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2817 | // suppress user-defined conversions on the arguments. We do the same for |
| 2818 | // move constructors. |
| 2819 | if ((CopyInitializing || (InitListSyntax && NumArgs == 1)) && |
| 2820 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2821 | SuppressUserConversions = true; |
| 2822 | } |
| 2823 | |
| 2824 | if (!Constructor->isInvalidDecl() && |
| 2825 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2826 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2827 | if (ConstructorTmpl) |
| 2828 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2829 | /*ExplicitArgs*/ 0, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2830 | llvm::makeArrayRef(Args, NumArgs), |
| 2831 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2832 | else { |
| 2833 | // C++ [over.match.copy]p1: |
| 2834 | // - When initializing a temporary to be bound to the first parameter |
| 2835 | // of a constructor that takes a reference to possibly cv-qualified |
| 2836 | // T as its first argument, called with a single argument in the |
| 2837 | // context of direct-initialization, explicit conversion functions |
| 2838 | // are also considered. |
| 2839 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
| 2840 | NumArgs == 1 && |
| 2841 | Constructor->isCopyOrMoveConstructor(); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2842 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2843 | llvm::makeArrayRef(Args, NumArgs), CandidateSet, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2844 | SuppressUserConversions, |
| 2845 | /*PartialOverloading=*/false, |
| 2846 | /*AllowExplicit=*/AllowExplicitConv); |
| 2847 | } |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | |
| 2851 | // Perform overload resolution and return the result. |
| 2852 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 2853 | } |
| 2854 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2855 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2856 | /// enumerates the constructors of the initialized entity and performs overload |
| 2857 | /// resolution to select the best. |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2858 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2859 | /// class type. |
| 2860 | static void TryConstructorInitialization(Sema &S, |
| 2861 | const InitializedEntity &Entity, |
| 2862 | const InitializationKind &Kind, |
| 2863 | Expr **Args, unsigned NumArgs, |
| 2864 | QualType DestType, |
| 2865 | InitializationSequence &Sequence, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2866 | bool InitListSyntax = false) { |
| 2867 | assert((!InitListSyntax || (NumArgs == 1 && isa<InitListExpr>(Args[0]))) && |
| 2868 | "InitListSyntax must come with a single initializer list argument."); |
| 2869 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2870 | // Check constructor arguments for self reference. |
| 2871 | if (DeclaratorDecl *DD = Entity.getDecl()) |
| 2872 | // Parameters arguments are occassionially constructed with itself, |
| 2873 | // for instance, in recursive functions. Skip them. |
| 2874 | if (!isa<ParmVarDecl>(DD)) |
| 2875 | for (unsigned i = 0; i < NumArgs; ++i) |
| 2876 | S.CheckSelfReference(DD, Args[i]); |
| 2877 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2878 | // The type we're constructing needs to be complete. |
| 2879 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 2880 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2881 | return; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 2885 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 2886 | CXXRecordDecl *DestRecordDecl |
| 2887 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 2888 | |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2889 | if (InitListSyntax && |
| 2890 | TryListConstructionSpecialCases(S, cast<InitListExpr>(Args[0]), |
| 2891 | DestRecordDecl, DestType, Sequence)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2892 | return; |
| 2893 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2894 | // Build the candidate set directly in the initialization sequence |
| 2895 | // structure, so that it will persist if we fail. |
| 2896 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2897 | |
| 2898 | // Determine whether we are allowed to call explicit constructors or |
| 2899 | // explicit conversion operators. |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2900 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2901 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2902 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2903 | // - Otherwise, if T is a class type, constructors are considered. The |
| 2904 | // applicable constructors are enumerated, and the best one is chosen |
| 2905 | // through overload resolution. |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2906 | DeclContext::lookup_iterator ConStart, ConEnd; |
| 2907 | llvm::tie(ConStart, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2908 | |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2909 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2910 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2911 | bool AsInitializerList = false; |
| 2912 | |
| 2913 | // C++11 [over.match.list]p1: |
| 2914 | // When objects of non-aggregate type T are list-initialized, overload |
| 2915 | // resolution selects the constructor in two phases: |
| 2916 | // - Initially, the candidate functions are the initializer-list |
| 2917 | // constructors of the class T and the argument list consists of the |
| 2918 | // initializer list as a single argument. |
| 2919 | if (InitListSyntax) { |
| 2920 | AsInitializerList = true; |
| 2921 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, NumArgs, |
| 2922 | CandidateSet, ConStart, ConEnd, Best, |
| 2923 | CopyInitialization, AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2924 | /*OnlyListConstructor=*/true, |
| 2925 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2926 | |
| 2927 | // Time to unwrap the init list. |
| 2928 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
| 2929 | Args = ILE->getInits(); |
| 2930 | NumArgs = ILE->getNumInits(); |
| 2931 | } |
| 2932 | |
| 2933 | // C++11 [over.match.list]p1: |
| 2934 | // - If no viable initializer-list constructor is found, overload resolution |
| 2935 | // is performed again, where the candidate functions are all the |
| 2936 | // constructors of the class T nad the argument list consists of the |
| 2937 | // elements of the initializer list. |
| 2938 | if (Result == OR_No_Viable_Function) { |
| 2939 | AsInitializerList = false; |
| 2940 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, NumArgs, |
| 2941 | CandidateSet, ConStart, ConEnd, Best, |
| 2942 | CopyInitialization, AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2943 | /*OnlyListConstructors=*/false, |
| 2944 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2945 | } |
| 2946 | if (Result) { |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2947 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2948 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 2949 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2950 | Result); |
| 2951 | return; |
| 2952 | } |
| 2953 | |
| 2954 | // C++0x [dcl.init]p6: |
| 2955 | // If a program calls for the default initialization of an object |
| 2956 | // of a const-qualified type T, T shall be a class type with a |
| 2957 | // user-provided default constructor. |
| 2958 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 2959 | Entity.getType().isConstQualified() && |
| 2960 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 2961 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2962 | return; |
| 2963 | } |
| 2964 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2965 | // C++11 [over.match.list]p1: |
| 2966 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 2967 | // initializer is ill-formed. |
| 2968 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 2969 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 2970 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 2971 | return; |
| 2972 | } |
| 2973 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2974 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 2975 | // subsumed by the initialization. |
| 2976 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2977 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 2978 | Best->FoundDecl.getAccess(), |
| 2979 | DestType, HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2980 | InitListSyntax, AsInitializerList); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2983 | static bool |
| 2984 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 2985 | Expr *Initializer, |
| 2986 | QualType &SourceType, |
| 2987 | QualType &UnqualifiedSourceType, |
| 2988 | QualType UnqualifiedTargetType, |
| 2989 | InitializationSequence &Sequence) { |
| 2990 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 2991 | S.Context.OverloadTy) { |
| 2992 | DeclAccessPair Found; |
| 2993 | bool HadMultipleCandidates = false; |
| 2994 | if (FunctionDecl *Fn |
| 2995 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 2996 | UnqualifiedTargetType, |
| 2997 | false, Found, |
| 2998 | &HadMultipleCandidates)) { |
| 2999 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3000 | HadMultipleCandidates); |
| 3001 | SourceType = Fn->getType(); |
| 3002 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3003 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3004 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3005 | return true; |
| 3006 | } |
| 3007 | } |
| 3008 | return false; |
| 3009 | } |
| 3010 | |
| 3011 | static void TryReferenceInitializationCore(Sema &S, |
| 3012 | const InitializedEntity &Entity, |
| 3013 | const InitializationKind &Kind, |
| 3014 | Expr *Initializer, |
| 3015 | QualType cv1T1, QualType T1, |
| 3016 | Qualifiers T1Quals, |
| 3017 | QualType cv2T2, QualType T2, |
| 3018 | Qualifiers T2Quals, |
| 3019 | InitializationSequence &Sequence); |
| 3020 | |
| 3021 | static void TryListInitialization(Sema &S, |
| 3022 | const InitializedEntity &Entity, |
| 3023 | const InitializationKind &Kind, |
| 3024 | InitListExpr *InitList, |
| 3025 | InitializationSequence &Sequence); |
| 3026 | |
| 3027 | /// \brief Attempt list initialization of a reference. |
| 3028 | static void TryReferenceListInitialization(Sema &S, |
| 3029 | const InitializedEntity &Entity, |
| 3030 | const InitializationKind &Kind, |
| 3031 | InitListExpr *InitList, |
| 3032 | InitializationSequence &Sequence) |
| 3033 | { |
| 3034 | // First, catch C++03 where this isn't possible. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3035 | if (!S.getLangOpts().CPlusPlus0x) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3036 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3037 | return; |
| 3038 | } |
| 3039 | |
| 3040 | QualType DestType = Entity.getType(); |
| 3041 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3042 | Qualifiers T1Quals; |
| 3043 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3044 | |
| 3045 | // Reference initialization via an initializer list works thus: |
| 3046 | // If the initializer list consists of a single element that is |
| 3047 | // reference-related to the referenced type, bind directly to that element |
| 3048 | // (possibly creating temporaries). |
| 3049 | // Otherwise, initialize a temporary with the initializer list and |
| 3050 | // bind to that. |
| 3051 | if (InitList->getNumInits() == 1) { |
| 3052 | Expr *Initializer = InitList->getInit(0); |
| 3053 | QualType cv2T2 = Initializer->getType(); |
| 3054 | Qualifiers T2Quals; |
| 3055 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3056 | |
| 3057 | // If this fails, creating a temporary wouldn't work either. |
| 3058 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3059 | T1, Sequence)) |
| 3060 | return; |
| 3061 | |
| 3062 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3063 | bool dummy1, dummy2, dummy3; |
| 3064 | Sema::ReferenceCompareResult RefRelationship |
| 3065 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3066 | dummy2, dummy3); |
| 3067 | if (RefRelationship >= Sema::Ref_Related) { |
| 3068 | // Try to bind the reference here. |
| 3069 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3070 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3071 | if (Sequence) |
| 3072 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3073 | return; |
| 3074 | } |
| 3075 | } |
| 3076 | |
| 3077 | // Not reference-related. Create a temporary and bind to that. |
| 3078 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3079 | |
| 3080 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3081 | if (Sequence) { |
| 3082 | if (DestType->isRValueReferenceType() || |
| 3083 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3084 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3085 | else |
| 3086 | Sequence.SetFailed( |
| 3087 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3088 | } |
| 3089 | } |
| 3090 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3091 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3092 | static void TryListInitialization(Sema &S, |
| 3093 | const InitializedEntity &Entity, |
| 3094 | const InitializationKind &Kind, |
| 3095 | InitListExpr *InitList, |
| 3096 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3097 | QualType DestType = Entity.getType(); |
| 3098 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3099 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3100 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3101 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3102 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3103 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3104 | return; |
| 3105 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3106 | if (DestType->isReferenceType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3107 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3108 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3109 | } |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3110 | if (DestType->isRecordType()) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3111 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3112 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3113 | return; |
| 3114 | } |
| 3115 | |
| 3116 | if (!DestType->isAggregateType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3117 | if (S.getLangOpts().CPlusPlus0x) { |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3118 | Expr *Arg = InitList; |
| 3119 | // A direct-initializer is not list-syntax, i.e. there's no special |
| 3120 | // treatment of "A a({1, 2});". |
| 3121 | TryConstructorInitialization(S, Entity, Kind, &Arg, 1, DestType, |
| 3122 | Sequence, |
| 3123 | Kind.getKind() != InitializationKind::IK_Direct); |
| 3124 | } else |
| 3125 | Sequence.SetFailed( |
| 3126 | InitializationSequence::FK_InitListBadDestinationType); |
| 3127 | return; |
| 3128 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3131 | InitListChecker CheckInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 3132 | DestType, /*VerifyOnly=*/true, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3133 | Kind.getKind() != InitializationKind::IK_DirectList || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3134 | !S.getLangOpts().CPlusPlus0x); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3135 | if (CheckInitList.HadError()) { |
| 3136 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3137 | return; |
| 3138 | } |
| 3139 | |
| 3140 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3141 | Sequence.AddListInitializationStep(DestType); |
| 3142 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3143 | |
| 3144 | /// \brief Try a reference initialization that involves calling a conversion |
| 3145 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3146 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3147 | const InitializedEntity &Entity, |
| 3148 | const InitializationKind &Kind, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3149 | Expr *Initializer, |
| 3150 | bool AllowRValues, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3151 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3152 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3153 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3154 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3155 | QualType cv2T2 = Initializer->getType(); |
| 3156 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3157 | |
| 3158 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3159 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3160 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3161 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3162 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3163 | ObjCConversion, |
| 3164 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3165 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3166 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3167 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3168 | (void)ObjCLifetimeConversion; |
| 3169 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3170 | // Build the candidate set directly in the initialization sequence |
| 3171 | // structure, so that it will persist if we fail. |
| 3172 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3173 | CandidateSet.clear(); |
| 3174 | |
| 3175 | // Determine whether we are allowed to call explicit constructors or |
| 3176 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3177 | bool AllowExplicit = Kind.AllowExplicit(); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3178 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctions(); |
| 3179 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3180 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3181 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3182 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3183 | // The type we're converting to is a class type. Enumerate its constructors |
| 3184 | // to see if there is a suitable conversion. |
| 3185 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3186 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3187 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3188 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3189 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3190 | NamedDecl *D = *Con; |
| 3191 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3192 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3193 | // Find the constructor (which may be a template). |
| 3194 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3195 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3196 | if (ConstructorTmpl) |
| 3197 | Constructor = cast<CXXConstructorDecl>( |
| 3198 | ConstructorTmpl->getTemplatedDecl()); |
| 3199 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3200 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3201 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3202 | if (!Constructor->isInvalidDecl() && |
| 3203 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3204 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3205 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3206 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3207 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3208 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3209 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3210 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3211 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3212 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3213 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3214 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3215 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3216 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3217 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3218 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3219 | const RecordType *T2RecordType = 0; |
| 3220 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3221 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3222 | // The type we're converting from is a class type, enumerate its conversion |
| 3223 | // functions. |
| 3224 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3225 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3226 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3227 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3228 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 3229 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3230 | NamedDecl *D = *I; |
| 3231 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3232 | if (isa<UsingShadowDecl>(D)) |
| 3233 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
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 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3236 | CXXConversionDecl *Conv; |
| 3237 | if (ConvTemplate) |
| 3238 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3239 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3240 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3241 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3242 | // If the conversion function doesn't return a reference type, |
| 3243 | // it can't be considered for this conversion unless we're allowed to |
| 3244 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3245 | // FIXME: Do we need to make sure that we only consider conversion |
| 3246 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3247 | // break recursion. |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3248 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3249 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3250 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3251 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3252 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3253 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3254 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3255 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3256 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3257 | } |
| 3258 | } |
| 3259 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3260 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3261 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3262 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3263 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3264 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3265 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3266 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3267 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3268 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3269 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3270 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3271 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3272 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3273 | // This is the overload that will actually be used for the initialization, so |
| 3274 | // mark it as used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3275 | S.MarkFunctionReferenced(DeclLoc, Function); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3276 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3277 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3278 | if (isa<CXXConversionDecl>(Function)) |
| 3279 | T2 = Function->getResultType(); |
| 3280 | else |
| 3281 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3282 | |
| 3283 | // Add the user-defined conversion step. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3284 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3285 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3286 | T2.getNonLValueExprType(S.Context), |
| 3287 | HadMultipleCandidates); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3288 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3289 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3290 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3291 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3292 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3293 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3294 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3295 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3296 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3297 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3298 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3299 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3300 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3301 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3302 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3303 | NewDerivedToBase, NewObjCConversion, |
| 3304 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3305 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3306 | // If the type we've converted to is not reference-related to the |
| 3307 | // type we're looking for, then there is another conversion step |
| 3308 | // we need to perform to produce a temporary of the right type |
| 3309 | // that we'll be binding to. |
| 3310 | ImplicitConversionSequence ICS; |
| 3311 | ICS.setStandard(); |
| 3312 | ICS.Standard = Best->FinalConversion; |
| 3313 | T2 = ICS.Standard.getToType(2); |
| 3314 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3315 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3316 | Sequence.AddDerivedToBaseCastStep( |
| 3317 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3318 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3319 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3320 | else if (NewObjCConversion) |
| 3321 | Sequence.AddObjCObjectConversionStep( |
| 3322 | S.Context.getQualifiedType(T1, |
| 3323 | T2.getNonReferenceType().getQualifiers())); |
| 3324 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3325 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3326 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3327 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3328 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3329 | return OR_Success; |
| 3330 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3331 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3332 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3333 | const InitializedEntity &Entity, |
| 3334 | Expr *CurInitExpr); |
| 3335 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3336 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3337 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3338 | const InitializedEntity &Entity, |
| 3339 | const InitializationKind &Kind, |
| 3340 | Expr *Initializer, |
| 3341 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3342 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3343 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3344 | Qualifiers T1Quals; |
| 3345 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3346 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3347 | Qualifiers T2Quals; |
| 3348 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3349 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3350 | // If the initializer is the address of an overloaded function, try |
| 3351 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3352 | // type of the resulting function. |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3353 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3354 | T1, Sequence)) |
| 3355 | return; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3356 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3357 | // Delegate everything else to a subfunction. |
| 3358 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3359 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3360 | } |
| 3361 | |
| 3362 | /// \brief Reference initialization without resolving overloaded functions. |
| 3363 | static void TryReferenceInitializationCore(Sema &S, |
| 3364 | const InitializedEntity &Entity, |
| 3365 | const InitializationKind &Kind, |
| 3366 | Expr *Initializer, |
| 3367 | QualType cv1T1, QualType T1, |
| 3368 | Qualifiers T1Quals, |
| 3369 | QualType cv2T2, QualType T2, |
| 3370 | Qualifiers T2Quals, |
| 3371 | InitializationSequence &Sequence) { |
| 3372 | QualType DestType = Entity.getType(); |
| 3373 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3374 | // Compute some basic properties of the types and the initializer. |
| 3375 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3376 | bool isRValueRef = !isLValueRef; |
| 3377 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3378 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3379 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3380 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3381 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3382 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3383 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3385 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3386 | // 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] | 3387 | // "cv2 T2" as follows: |
| 3388 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3389 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3390 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3391 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3392 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3393 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3394 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3395 | bool T1Function = T1->isFunctionType(); |
| 3396 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3397 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3398 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3399 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3400 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3401 | // - 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] | 3402 | // reference-compatible with "cv2 T2," or |
| 3403 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3404 | // 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] | 3405 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3406 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3407 | // to decide whether we're actually binding to a temporary created from |
| 3408 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3409 | if (DerivedToBase) |
| 3410 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3411 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3412 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3413 | else if (ObjCConversion) |
| 3414 | Sequence.AddObjCObjectConversionStep( |
| 3415 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3416 | |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3417 | if (T1Quals != T2Quals) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3418 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3419 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3420 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3421 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3422 | return; |
| 3423 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3424 | |
| 3425 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3426 | // reference-related to T2, and can be implicitly converted to an |
| 3427 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3428 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3429 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3430 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3431 | // If we have an rvalue ref to function type here, the rhs must be |
| 3432 | // an rvalue. |
| 3433 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3434 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3435 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3436 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3437 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3438 | Sequence); |
| 3439 | if (ConvOvlResult == OR_Success) |
| 3440 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3441 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3442 | Sequence.SetOverloadFailure( |
| 3443 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3444 | ConvOvlResult); |
| 3445 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3446 | } |
| 3447 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3448 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3449 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3450 | // 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] | 3451 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3452 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3453 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3454 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3455 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3456 | Sequence.SetOverloadFailure( |
| 3457 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3458 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3459 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3460 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3461 | ? (RefRelationship == Sema::Ref_Related |
| 3462 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3463 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3464 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3465 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3466 | return; |
| 3467 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3468 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3469 | // - If the initializer expression |
| 3470 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3471 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3472 | // Note: functions are handled below. |
| 3473 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3474 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3475 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3476 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3477 | (InitCategory.isXValue() || |
| 3478 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3479 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3480 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3481 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3482 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3483 | // compiler the freedom to perform a copy here or bind to the |
| 3484 | // object, while C++0x requires that we bind directly to the |
| 3485 | // object. Hence, we always bind to the object without making an |
| 3486 | // extra copy. However, in C++03 requires that we check for the |
| 3487 | // presence of a suitable copy constructor: |
| 3488 | // |
| 3489 | // The constructor that would be used to make the copy shall |
| 3490 | // be callable whether or not the copy is actually done. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3491 | if (!S.getLangOpts().CPlusPlus0x && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3492 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3493 | else if (S.getLangOpts().CPlusPlus0x) |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3494 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3495 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3496 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3497 | if (DerivedToBase) |
| 3498 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3499 | ValueKind); |
| 3500 | else if (ObjCConversion) |
| 3501 | Sequence.AddObjCObjectConversionStep( |
| 3502 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3503 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3504 | if (T1Quals != T2Quals) |
| 3505 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3506 | Sequence.AddReferenceBindingStep(cv1T1, |
Peter Collingbourne | 65bfd68 | 2011-11-13 00:51:30 +0000 | [diff] [blame] | 3507 | /*bindingTemporary=*/InitCategory.isPRValue()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3508 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3509 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3510 | |
| 3511 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3512 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3513 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3514 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3515 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3516 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3517 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3518 | Kind, Initializer, |
| 3519 | /*AllowRValues=*/true, |
| 3520 | Sequence); |
| 3521 | if (ConvOvlResult) |
| 3522 | Sequence.SetOverloadFailure( |
| 3523 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3524 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3525 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3526 | return; |
| 3527 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3528 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3529 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3530 | return; |
| 3531 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3532 | |
| 3533 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3534 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3535 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3536 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3537 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3538 | // Determine whether we are allowed to call explicit constructors or |
| 3539 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3540 | bool AllowExplicit = Kind.AllowExplicit(); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3541 | |
| 3542 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3543 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3544 | ImplicitConversionSequence ICS |
| 3545 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3546 | /*SuppressUserConversions*/ false, |
| 3547 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3548 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3549 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3550 | /*AllowObjCWritebackConversion=*/false); |
| 3551 | |
| 3552 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3553 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3554 | // this into an overloading ambiguity diagnostic. However, we need |
| 3555 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3556 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3557 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3558 | Sequence.SetOverloadFailure( |
| 3559 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3560 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3561 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3562 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3563 | else |
| 3564 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3565 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3566 | } else { |
| 3567 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3571 | // same cv-qualification as, or greater cv-qualification |
| 3572 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3573 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3574 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3575 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3576 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3577 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3578 | return; |
| 3579 | } |
| 3580 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3581 | // [...] 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] | 3582 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3583 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3584 | InitCategory.isLValue()) { |
| 3585 | Sequence.SetFailed( |
| 3586 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3587 | return; |
| 3588 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3589 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3590 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3591 | return; |
| 3592 | } |
| 3593 | |
| 3594 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3595 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3596 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3597 | const InitializedEntity &Entity, |
| 3598 | const InitializationKind &Kind, |
| 3599 | Expr *Initializer, |
| 3600 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3601 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3604 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3605 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3606 | const InitializedEntity &Entity, |
| 3607 | const InitializationKind &Kind, |
| 3608 | InitializationSequence &Sequence) { |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3609 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3610 | // |
| 3611 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3612 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3613 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3614 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3615 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3616 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3617 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3618 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3619 | // C++98: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3620 | // -- if T is a class type (clause 9) with a user-declared |
| 3621 | // constructor (12.1), then the default constructor for T is |
| 3622 | // called (and the initialization is ill-formed if T has no |
| 3623 | // accessible default constructor); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3624 | if (!S.getLangOpts().CPlusPlus0x) { |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3625 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 3626 | // FIXME: we really want to refer to a single subobject of the array, |
| 3627 | // but Entity doesn't have a way to capture that (yet). |
| 3628 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, |
| 3629 | T, Sequence); |
| 3630 | } else { |
| 3631 | // C++11: |
| 3632 | // -- if T is a class type (clause 9) with either no default constructor |
| 3633 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 3634 | // or deleted, then the object is default-initialized; |
| 3635 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 3636 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
| 3637 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, |
| 3638 | T, Sequence); |
| 3639 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3640 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3641 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 3642 | // user-provided or deleted default constructor, then the object is |
| 3643 | // zero-initialized and, if T has a non-trivial default constructor, |
| 3644 | // default-initialized; |
Richard Smith | d079abf | 2012-05-07 01:07:30 +0000 | [diff] [blame] | 3645 | // FIXME: The 'non-union' here is a defect (not yet assigned an issue |
| 3646 | // number). Update the quotation when the defect is resolved. |
| 3647 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3648 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3649 | } |
| 3650 | } |
| 3651 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3652 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3655 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3656 | static void TryDefaultInitialization(Sema &S, |
| 3657 | const InitializedEntity &Entity, |
| 3658 | const InitializationKind &Kind, |
| 3659 | InitializationSequence &Sequence) { |
| 3660 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3661 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3662 | // C++ [dcl.init]p6: |
| 3663 | // To default-initialize an object of type T means: |
| 3664 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3665 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3666 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3667 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3668 | // constructor for T is called (and the initialization is ill-formed if |
| 3669 | // T has no accessible default constructor); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3670 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3671 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3672 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3673 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3674 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3675 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3676 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3677 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3678 | // 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] | 3679 | // default constructor. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3680 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3681 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3682 | return; |
| 3683 | } |
| 3684 | |
| 3685 | // If the destination type has a lifetime property, zero-initialize it. |
| 3686 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3687 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3688 | return; |
| 3689 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3692 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3693 | /// which enumerates all conversion functions and performs overload resolution |
| 3694 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3695 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3696 | const InitializedEntity &Entity, |
| 3697 | const InitializationKind &Kind, |
| 3698 | Expr *Initializer, |
| 3699 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3700 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3701 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3702 | QualType SourceType = Initializer->getType(); |
| 3703 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3704 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3705 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3706 | // Build the candidate set directly in the initialization sequence |
| 3707 | // structure, so that it will persist if we fail. |
| 3708 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3709 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3710 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3711 | // Determine whether we are allowed to call explicit constructors or |
| 3712 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3713 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3714 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3715 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3716 | // The type we're converting to is a class type. Enumerate its constructors |
| 3717 | // to see if there is a suitable conversion. |
| 3718 | CXXRecordDecl *DestRecordDecl |
| 3719 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3720 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3721 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3722 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3723 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3724 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3725 | Con != ConEnd; ++Con) { |
| 3726 | NamedDecl *D = *Con; |
| 3727 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3728 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3729 | // Find the constructor (which may be a template). |
| 3730 | CXXConstructorDecl *Constructor = 0; |
| 3731 | FunctionTemplateDecl *ConstructorTmpl |
| 3732 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3733 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3734 | Constructor = cast<CXXConstructorDecl>( |
| 3735 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3736 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3737 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3738 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3739 | if (!Constructor->isInvalidDecl() && |
| 3740 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3741 | if (ConstructorTmpl) |
| 3742 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3743 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3744 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3745 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3746 | else |
| 3747 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3748 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3749 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3750 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3751 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3752 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3753 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3754 | |
| 3755 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3756 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3757 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3758 | // The type we're converting from is a class type, enumerate its conversion |
| 3759 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3760 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3761 | // We can only enumerate the conversion functions for a complete type; if |
| 3762 | // the type isn't complete, simply skip this step. |
| 3763 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3764 | CXXRecordDecl *SourceRecordDecl |
| 3765 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3766 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3767 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3768 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3769 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3770 | E = Conversions->end(); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3771 | I != E; ++I) { |
| 3772 | NamedDecl *D = *I; |
| 3773 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3774 | if (isa<UsingShadowDecl>(D)) |
| 3775 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3776 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3777 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3778 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3779 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3780 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3781 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3782 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3783 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3784 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3785 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3786 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3787 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3788 | CandidateSet); |
| 3789 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3790 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3791 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3792 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3793 | } |
| 3794 | } |
| 3795 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3796 | |
| 3797 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3798 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3799 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3800 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3801 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3802 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3803 | Result); |
| 3804 | return; |
| 3805 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3806 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3807 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3808 | S.MarkFunctionReferenced(DeclLoc, Function); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3809 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3810 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3811 | if (isa<CXXConstructorDecl>(Function)) { |
| 3812 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 3813 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 3814 | // cv-unqualified type of the destination. |
| 3815 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 3816 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3817 | HadMultipleCandidates); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3818 | return; |
| 3819 | } |
| 3820 | |
| 3821 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3822 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3823 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 3824 | // 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] | 3825 | // the resulting temporary object (possible to create an object of |
| 3826 | // a base class type). That copy is not a separate conversion, so |
| 3827 | // we just make a note of the actual destination type (possibly a |
| 3828 | // base class of the type returned by the conversion function) and |
| 3829 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3830 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 3831 | HadMultipleCandidates); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3832 | return; |
| 3833 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3834 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3835 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 3836 | HadMultipleCandidates); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3837 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3838 | // If the conversion following the call to the conversion function |
| 3839 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3840 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3841 | Best->FinalConversion.Third) { |
| 3842 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3843 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3844 | ICS.Standard = Best->FinalConversion; |
| 3845 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3846 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3849 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3850 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3851 | |
| 3852 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3853 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3854 | bool isAddressOf) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3855 | // Skip parens. |
| 3856 | e = e->IgnoreParens(); |
| 3857 | |
| 3858 | // Skip address-of nodes. |
| 3859 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3860 | if (op->getOpcode() == UO_AddrOf) |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3861 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3862 | |
| 3863 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3864 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3865 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3866 | case CK_Dependent: |
| 3867 | case CK_BitCast: |
| 3868 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3869 | case CK_NoOp: |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3870 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3871 | |
| 3872 | case CK_ArrayToPointerDecay: |
| 3873 | return IIK_nonscalar; |
| 3874 | |
| 3875 | case CK_NullToPointer: |
| 3876 | return IIK_okay; |
| 3877 | |
| 3878 | default: |
| 3879 | break; |
| 3880 | } |
| 3881 | |
| 3882 | // 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] | 3883 | } else if (isa<DeclRefExpr>(e)) { |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3884 | if (!isAddressOf) return IIK_nonlocal; |
| 3885 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3886 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3887 | if (!var) return IIK_nonlocal; |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3888 | |
| 3889 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3890 | |
| 3891 | // If we have a conditional operator, check both sides. |
| 3892 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3893 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3894 | return iik; |
| 3895 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3896 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3897 | |
| 3898 | // These are never scalar. |
| 3899 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3900 | return IIK_nonscalar; |
| 3901 | |
| 3902 | // Otherwise, it needs to be a null pointer constant. |
| 3903 | } else { |
| 3904 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3905 | ? IIK_okay : IIK_nonlocal); |
| 3906 | } |
| 3907 | |
| 3908 | return IIK_nonlocal; |
| 3909 | } |
| 3910 | |
| 3911 | /// Check whether the given expression is a valid operand for an |
| 3912 | /// indirect copy/restore. |
| 3913 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3914 | assert(src->isRValue()); |
| 3915 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3916 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3917 | if (iik == IIK_okay) return; |
| 3918 | |
| 3919 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3920 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3921 | << src->getSourceRange(); |
| 3922 | } |
| 3923 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3924 | /// \brief Determine whether we have compatible array types for the |
| 3925 | /// purposes of GNU by-copy array initialization. |
| 3926 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3927 | const ArrayType *Dest, |
| 3928 | const ArrayType *Source) { |
| 3929 | // If the source and destination array types are equivalent, we're |
| 3930 | // done. |
| 3931 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3932 | return true; |
| 3933 | |
| 3934 | // Make sure that the element types are the same. |
| 3935 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3936 | return false; |
| 3937 | |
| 3938 | // The only mismatch we allow is when the destination is an |
| 3939 | // incomplete array type and the source is a constant array type. |
| 3940 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3941 | } |
| 3942 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3943 | static bool tryObjCWritebackConversion(Sema &S, |
| 3944 | InitializationSequence &Sequence, |
| 3945 | const InitializedEntity &Entity, |
| 3946 | Expr *Initializer) { |
| 3947 | bool ArrayDecay = false; |
| 3948 | QualType ArgType = Initializer->getType(); |
| 3949 | QualType ArgPointee; |
| 3950 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3951 | ArrayDecay = true; |
| 3952 | ArgPointee = ArgArrayType->getElementType(); |
| 3953 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3954 | } |
| 3955 | |
| 3956 | // Handle write-back conversion. |
| 3957 | QualType ConvertedArgType; |
| 3958 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3959 | ConvertedArgType)) |
| 3960 | return false; |
| 3961 | |
| 3962 | // We should copy unless we're passing to an argument explicitly |
| 3963 | // marked 'out'. |
| 3964 | bool ShouldCopy = true; |
| 3965 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3966 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3967 | |
| 3968 | // Do we need an lvalue conversion? |
| 3969 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3970 | ImplicitConversionSequence ICS; |
| 3971 | ICS.setStandard(); |
| 3972 | ICS.Standard.setAsIdentityConversion(); |
| 3973 | |
| 3974 | QualType ResultType; |
| 3975 | if (ArrayDecay) { |
| 3976 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3977 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3978 | } else { |
| 3979 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3980 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3981 | } |
| 3982 | |
| 3983 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3984 | } |
| 3985 | |
| 3986 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3987 | return true; |
| 3988 | } |
| 3989 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3990 | InitializationSequence::InitializationSequence(Sema &S, |
| 3991 | const InitializedEntity &Entity, |
| 3992 | const InitializationKind &Kind, |
| 3993 | Expr **Args, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3994 | unsigned NumArgs) |
| 3995 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3996 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3997 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3998 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3999 | // The semantics of initializers are as follows. The destination type is |
| 4000 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4001 | // 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] | 4002 | // 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] | 4003 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4004 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4005 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4006 | if (DestType->isDependentType() || |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4007 | Expr::hasAnyTypeDependentArguments(llvm::makeArrayRef(Args, NumArgs))) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4008 | SequenceKind = DependentSequence; |
| 4009 | return; |
| 4010 | } |
| 4011 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4012 | // Almost everything is a normal sequence. |
| 4013 | setSequenceKind(NormalSequence); |
| 4014 | |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 4015 | for (unsigned I = 0; I != NumArgs; ++I) |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4016 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4017 | // FIXME: should we be doing this here? |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4018 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4019 | if (result.isInvalid()) { |
| 4020 | SetFailed(FK_PlaceholderType); |
| 4021 | return; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4022 | } |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4023 | Args[I] = result.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4024 | } |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 4025 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4026 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4027 | QualType SourceType; |
| 4028 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4029 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4030 | Initializer = Args[0]; |
| 4031 | if (!isa<InitListExpr>(Initializer)) |
| 4032 | SourceType = Initializer->getType(); |
| 4033 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4034 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4035 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4036 | // object is list-initialized (8.5.4). |
| 4037 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4038 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4039 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4040 | return; |
| 4041 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4042 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4043 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4044 | // - If the destination type is a reference type, see 8.5.3. |
| 4045 | if (DestType->isReferenceType()) { |
| 4046 | // C++0x [dcl.init.ref]p1: |
| 4047 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4048 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4049 | // by an object that can be converted into a T. |
| 4050 | // (Therefore, multiple arguments are not permitted.) |
| 4051 | if (NumArgs != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4052 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4053 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4054 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4055 | return; |
| 4056 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4057 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4058 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4059 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 4060 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4061 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4062 | return; |
| 4063 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4064 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4065 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4066 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4067 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4068 | return; |
| 4069 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4070 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4071 | // - If the destination type is an array of characters, an array of |
| 4072 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4073 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4074 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4075 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4076 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4077 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4078 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4079 | return; |
| 4080 | } |
| 4081 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4082 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4083 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4084 | return; |
| 4085 | } |
| 4086 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4087 | // Note: as an GNU C extension, we allow initialization of an |
| 4088 | // array from a compound literal that creates an array of the same |
| 4089 | // type, so long as the initializer has no side effects. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4090 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4091 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4092 | Initializer->getType()->isArrayType()) { |
| 4093 | const ArrayType *SourceAT |
| 4094 | = Context.getAsArrayType(Initializer->getType()); |
| 4095 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4096 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4097 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4098 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4099 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4100 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4101 | } |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4102 | } |
| 4103 | // Note: as a GNU C++ extension, we allow initialization of a |
| 4104 | // class member from a parenthesized initializer list. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4105 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4106 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4107 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4108 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4109 | *this); |
| 4110 | AddParenthesizedArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4111 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4112 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4113 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4114 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4115 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4116 | return; |
| 4117 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4118 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4119 | // Determine whether we should consider writeback conversions for |
| 4120 | // Objective-C ARC. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4121 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4122 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 4123 | |
| 4124 | // We're at the end of the line for C: it's either a write-back conversion |
| 4125 | // 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] | 4126 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4127 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4128 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4129 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4130 | return; |
| 4131 | } |
| 4132 | |
| 4133 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4134 | AddCAssignmentStep(DestType); |
| 4135 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4136 | return; |
| 4137 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4138 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4139 | assert(S.getLangOpts().CPlusPlus); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4140 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4141 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4142 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4143 | // - If the initialization is direct-initialization, or if it is |
| 4144 | // copy-initialization where the cv-unqualified version of the |
| 4145 | // 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] | 4146 | // class of the destination, constructors are considered. [...] |
| 4147 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4148 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4149 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4150 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4151 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4152 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4153 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4154 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4155 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4156 | // used) to a derived class thereof are enumerated as described in |
| 4157 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4158 | // (13.3). |
| 4159 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4160 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4161 | return; |
| 4162 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4163 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4164 | if (NumArgs > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4165 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4166 | return; |
| 4167 | } |
| 4168 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4169 | |
| 4170 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4171 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4172 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4173 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 4174 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4175 | return; |
| 4176 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4177 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4178 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4179 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4180 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4181 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4182 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4183 | |
| 4184 | ImplicitConversionSequence ICS |
| 4185 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4186 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4187 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4188 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4189 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4190 | allowObjCWritebackConversion); |
| 4191 | |
| 4192 | if (ICS.isStandard() && |
| 4193 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4194 | // Objective-C ARC writeback conversion. |
| 4195 | |
| 4196 | // We should copy unless we're passing to an argument explicitly |
| 4197 | // marked 'out'. |
| 4198 | bool ShouldCopy = true; |
| 4199 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4200 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4201 | |
| 4202 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4203 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4204 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4205 | ImplicitConversionSequence LvalueICS; |
| 4206 | LvalueICS.setStandard(); |
| 4207 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4208 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4209 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4210 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4211 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4212 | |
| 4213 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4214 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4215 | DeclAccessPair dap; |
| 4216 | if (Initializer->getType() == Context.OverloadTy && |
| 4217 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 4218 | , DestType, false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4219 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4220 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4221 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4222 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4223 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4224 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4225 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4226 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
| 4229 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4230 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4231 | StepEnd = Steps.end(); |
| 4232 | Step != StepEnd; ++Step) |
| 4233 | Step->Destroy(); |
| 4234 | } |
| 4235 | |
| 4236 | //===----------------------------------------------------------------------===// |
| 4237 | // Perform initialization |
| 4238 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4239 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4240 | getAssignmentAction(const InitializedEntity &Entity) { |
| 4241 | switch(Entity.getKind()) { |
| 4242 | case InitializedEntity::EK_Variable: |
| 4243 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4244 | case InitializedEntity::EK_Exception: |
| 4245 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4246 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4247 | return Sema::AA_Initializing; |
| 4248 | |
| 4249 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4250 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4251 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4252 | return Sema::AA_Sending; |
| 4253 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4254 | return Sema::AA_Passing; |
| 4255 | |
| 4256 | case InitializedEntity::EK_Result: |
| 4257 | return Sema::AA_Returning; |
| 4258 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4259 | case InitializedEntity::EK_Temporary: |
| 4260 | // FIXME: Can we tell apart casting vs. converting? |
| 4261 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4262 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4263 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4264 | case InitializedEntity::EK_ArrayElement: |
| 4265 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4266 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4267 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4268 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4269 | return Sema::AA_Initializing; |
| 4270 | } |
| 4271 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4272 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4275 | /// \brief Whether we should binding a created object as a temporary when |
| 4276 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4277 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4278 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4279 | case InitializedEntity::EK_ArrayElement: |
| 4280 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4281 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4282 | case InitializedEntity::EK_New: |
| 4283 | case InitializedEntity::EK_Variable: |
| 4284 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4285 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4286 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4287 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4288 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4289 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4290 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4291 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4292 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4293 | case InitializedEntity::EK_Parameter: |
| 4294 | case InitializedEntity::EK_Temporary: |
| 4295 | return true; |
| 4296 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4297 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4298 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4299 | } |
| 4300 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4301 | /// \brief Whether the given entity, when initialized with an object |
| 4302 | /// created for that initialization, requires destruction. |
| 4303 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4304 | switch (Entity.getKind()) { |
| 4305 | case InitializedEntity::EK_Member: |
| 4306 | case InitializedEntity::EK_Result: |
| 4307 | case InitializedEntity::EK_New: |
| 4308 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4309 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4310 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4311 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4312 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4313 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4314 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4315 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4316 | case InitializedEntity::EK_Variable: |
| 4317 | case InitializedEntity::EK_Parameter: |
| 4318 | case InitializedEntity::EK_Temporary: |
| 4319 | case InitializedEntity::EK_ArrayElement: |
| 4320 | case InitializedEntity::EK_Exception: |
| 4321 | return true; |
| 4322 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4323 | |
| 4324 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4325 | } |
| 4326 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4327 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4328 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4329 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4330 | OverloadCandidateSet &CandidateSet, |
| 4331 | CXXRecordDecl *Class, |
| 4332 | Expr *CurInitExpr) { |
| 4333 | DeclContext::lookup_iterator Con, ConEnd; |
| 4334 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
| 4335 | Con != ConEnd; ++Con) { |
| 4336 | CXXConstructorDecl *Constructor = 0; |
| 4337 | |
| 4338 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
| 4339 | // Handle copy/moveconstructors, only. |
| 4340 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4341 | !Constructor->isCopyOrMoveConstructor() || |
| 4342 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4343 | continue; |
| 4344 | |
| 4345 | DeclAccessPair FoundDecl |
| 4346 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4347 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4348 | CurInitExpr, CandidateSet); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4349 | continue; |
| 4350 | } |
| 4351 | |
| 4352 | // Handle constructor templates. |
| 4353 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 4354 | if (ConstructorTmpl->isInvalidDecl()) |
| 4355 | continue; |
| 4356 | |
| 4357 | Constructor = cast<CXXConstructorDecl>( |
| 4358 | ConstructorTmpl->getTemplatedDecl()); |
| 4359 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4360 | continue; |
| 4361 | |
| 4362 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4363 | // candidates? |
| 4364 | DeclAccessPair FoundDecl |
| 4365 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4366 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4367 | CurInitExpr, CandidateSet, true); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4368 | } |
| 4369 | } |
| 4370 | |
| 4371 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4372 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4373 | Expr *Initializer) { |
| 4374 | switch (Entity.getKind()) { |
| 4375 | case InitializedEntity::EK_Result: |
| 4376 | return Entity.getReturnLoc(); |
| 4377 | |
| 4378 | case InitializedEntity::EK_Exception: |
| 4379 | return Entity.getThrowLoc(); |
| 4380 | |
| 4381 | case InitializedEntity::EK_Variable: |
| 4382 | return Entity.getDecl()->getLocation(); |
| 4383 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4384 | case InitializedEntity::EK_LambdaCapture: |
| 4385 | return Entity.getCaptureLoc(); |
| 4386 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4387 | case InitializedEntity::EK_ArrayElement: |
| 4388 | case InitializedEntity::EK_Member: |
| 4389 | case InitializedEntity::EK_Parameter: |
| 4390 | case InitializedEntity::EK_Temporary: |
| 4391 | case InitializedEntity::EK_New: |
| 4392 | case InitializedEntity::EK_Base: |
| 4393 | case InitializedEntity::EK_Delegating: |
| 4394 | case InitializedEntity::EK_VectorElement: |
| 4395 | case InitializedEntity::EK_ComplexElement: |
| 4396 | case InitializedEntity::EK_BlockElement: |
| 4397 | return Initializer->getLocStart(); |
| 4398 | } |
| 4399 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4400 | } |
| 4401 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4402 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4403 | /// provided by the given initializer by calling the appropriate copy |
| 4404 | /// constructor. |
| 4405 | /// |
| 4406 | /// \param S The Sema object used for type-checking. |
| 4407 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4408 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4409 | /// the type of the initializer expression or a superclass thereof. |
| 4410 | /// |
| 4411 | /// \param Enter The entity being initialized. |
| 4412 | /// |
| 4413 | /// \param CurInit The initializer expression. |
| 4414 | /// |
| 4415 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4416 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4417 | /// an rvalue. |
| 4418 | /// |
| 4419 | /// \returns An expression that copies the initializer expression into |
| 4420 | /// a temporary object, or an error expression if a copy could not be |
| 4421 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4422 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4423 | QualType T, |
| 4424 | const InitializedEntity &Entity, |
| 4425 | ExprResult CurInit, |
| 4426 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4427 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4428 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4429 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4430 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4431 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4432 | if (!Class) |
| 4433 | return move(CurInit); |
| 4434 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4435 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4436 | // When certain criteria are met, an implementation is allowed to |
| 4437 | // omit the copy/move construction of a class object, even if the |
| 4438 | // copy/move constructor and/or destructor for the object have |
| 4439 | // side effects. [...] |
| 4440 | // - when a temporary class object that has not been bound to a |
| 4441 | // reference (12.2) would be copied/moved to a class object |
| 4442 | // with the same cv-unqualified type, the copy/move operation |
| 4443 | // can be omitted by constructing the temporary object |
| 4444 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4445 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4446 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4447 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4448 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4449 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4450 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4451 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4452 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4453 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4454 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4455 | return move(CurInit); |
| 4456 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4457 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4458 | // Only consider constructors and constructor templates. Per |
| 4459 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4460 | // is direct-initialization. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4461 | OverloadCandidateSet CandidateSet(Loc); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4462 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4463 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4464 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4465 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4466 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4467 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4468 | case OR_Success: |
| 4469 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4470 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4471 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4472 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4473 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4474 | : diag::err_temp_copy_no_viable) |
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(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4477 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4478 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4479 | return ExprError(); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4480 | return move(CurInit); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4481 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4482 | case OR_Ambiguous: |
| 4483 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4484 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4485 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4486 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4487 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4488 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4489 | case OR_Deleted: |
| 4490 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4491 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4492 | << CurInitExpr->getSourceRange(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4493 | S.NoteDeletedFunction(Best->Function); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4494 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4497 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4498 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4499 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4500 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4501 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4502 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4503 | |
| 4504 | if (IsExtraneousCopy) { |
| 4505 | // If this is a totally extraneous copy for C++03 reference |
| 4506 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4507 | // expression. We don't generate an (elided) copy operation here |
| 4508 | // because doing so would require us to pass down a flag to avoid |
| 4509 | // infinite recursion, where each step adds another extraneous, |
| 4510 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4511 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4512 | // Instantiate the default arguments of any extra parameters in |
| 4513 | // the selected copy constructor, as if we were going to create a |
| 4514 | // proper call to the copy constructor. |
| 4515 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4516 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4517 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4518 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4519 | break; |
| 4520 | |
| 4521 | // Build the default argument expression; we don't actually care |
| 4522 | // if this succeeds or not, because this routine will complain |
| 4523 | // if there was a problem. |
| 4524 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4525 | } |
| 4526 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4527 | return S.Owned(CurInitExpr); |
| 4528 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4529 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 4530 | S.MarkFunctionReferenced(Loc, Constructor); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4531 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4532 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4533 | // constructor call (we might have derived-to-base conversions, or |
| 4534 | // the copy constructor may have default arguments). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4535 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4536 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4537 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4538 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4539 | // Actually perform the constructor call. |
| 4540 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4541 | move_arg(ConstructorArgs), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4542 | HadMultipleCandidates, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4543 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4544 | CXXConstructExpr::CK_Complete, |
| 4545 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4546 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4547 | // If we're supposed to bind temporaries, do so. |
| 4548 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4549 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4550 | return move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4551 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4552 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4553 | /// \brief Check whether elidable copy construction for binding a reference to |
| 4554 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 4555 | /// -Wc++98-compat. |
| 4556 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4557 | const InitializedEntity &Entity, |
| 4558 | Expr *CurInitExpr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4559 | assert(S.getLangOpts().CPlusPlus0x); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4560 | |
| 4561 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 4562 | if (!Record) |
| 4563 | return; |
| 4564 | |
| 4565 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 4566 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 4567 | == DiagnosticsEngine::Ignored) |
| 4568 | return; |
| 4569 | |
| 4570 | // Find constructors which would have been considered. |
| 4571 | OverloadCandidateSet CandidateSet(Loc); |
| 4572 | LookupCopyAndMoveConstructors( |
| 4573 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 4574 | |
| 4575 | // Perform overload resolution. |
| 4576 | OverloadCandidateSet::iterator Best; |
| 4577 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 4578 | |
| 4579 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 4580 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 4581 | << CurInitExpr->getSourceRange(); |
| 4582 | |
| 4583 | switch (OR) { |
| 4584 | case OR_Success: |
| 4585 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 4586 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4587 | // FIXME: Check default arguments as far as that's possible. |
| 4588 | break; |
| 4589 | |
| 4590 | case OR_No_Viable_Function: |
| 4591 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4592 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4593 | break; |
| 4594 | |
| 4595 | case OR_Ambiguous: |
| 4596 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4597 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4598 | break; |
| 4599 | |
| 4600 | case OR_Deleted: |
| 4601 | S.Diag(Loc, Diag); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4602 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4603 | break; |
| 4604 | } |
| 4605 | } |
| 4606 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4607 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4608 | const InitializedEntity &Entity) { |
| 4609 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4610 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4611 | return; |
| 4612 | |
| 4613 | if (Entity.getDecl()->getDeclName()) |
| 4614 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4615 | << Entity.getDecl()->getDeclName(); |
| 4616 | else |
| 4617 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4618 | } |
| 4619 | } |
| 4620 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4621 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4622 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4623 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4624 | } |
| 4625 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4626 | static ExprResult |
| 4627 | PerformConstructorInitialization(Sema &S, |
| 4628 | const InitializedEntity &Entity, |
| 4629 | const InitializationKind &Kind, |
| 4630 | MultiExprArg Args, |
| 4631 | const InitializationSequence::Step& Step, |
| 4632 | bool &ConstructorInitRequiresZeroInit) { |
| 4633 | unsigned NumArgs = Args.size(); |
| 4634 | CXXConstructorDecl *Constructor |
| 4635 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 4636 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 4637 | |
| 4638 | // Build a call to the selected constructor. |
| 4639 | ASTOwningVector<Expr*> ConstructorArgs(S); |
| 4640 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4641 | ? Kind.getEqualLoc() |
| 4642 | : Kind.getLocation(); |
| 4643 | |
| 4644 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4645 | // Force even a trivial, implicit default constructor to be |
| 4646 | // semantically checked. We do this explicitly because we don't build |
| 4647 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 28e4702 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 4648 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4649 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | 5d86f61 | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 4650 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4651 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4652 | } |
| 4653 | |
| 4654 | ExprResult CurInit = S.Owned((Expr *)0); |
| 4655 | |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4656 | // C++ [over.match.copy]p1: |
| 4657 | // - When initializing a temporary to be bound to the first parameter |
| 4658 | // of a constructor that takes a reference to possibly cv-qualified |
| 4659 | // T as its first argument, called with a single argument in the |
| 4660 | // context of direct-initialization, explicit conversion functions |
| 4661 | // are also considered. |
| 4662 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 4663 | Args.size() == 1 && |
| 4664 | Constructor->isCopyOrMoveConstructor(); |
| 4665 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4666 | // Determine the arguments required to actually perform the constructor |
| 4667 | // call. |
| 4668 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4669 | Loc, ConstructorArgs, |
| 4670 | AllowExplicitConv)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4671 | return ExprError(); |
| 4672 | |
| 4673 | |
| 4674 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4675 | (Kind.getKind() == InitializationKind::IK_DirectList || |
| 4676 | (NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
| 4677 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4678 | Kind.getKind() == InitializationKind::IK_Value)))) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4679 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4680 | unsigned NumExprs = ConstructorArgs.size(); |
| 4681 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 4682 | S.MarkFunctionReferenced(Loc, Constructor); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4683 | S.DiagnoseUseOfDecl(Constructor, Loc); |
| 4684 | |
| 4685 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4686 | if (!TSInfo) |
| 4687 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4688 | SourceRange ParenRange; |
| 4689 | if (Kind.getKind() != InitializationKind::IK_DirectList) |
| 4690 | ParenRange = Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4691 | |
| 4692 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4693 | Constructor, |
| 4694 | TSInfo, |
| 4695 | Exprs, |
| 4696 | NumExprs, |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4697 | ParenRange, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4698 | HadMultipleCandidates, |
| 4699 | ConstructorInitRequiresZeroInit)); |
| 4700 | } else { |
| 4701 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4702 | CXXConstructExpr::CK_Complete; |
| 4703 | |
| 4704 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4705 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 4706 | CXXConstructExpr::CK_VirtualBase : |
| 4707 | CXXConstructExpr::CK_NonVirtualBase; |
| 4708 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 4709 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4710 | } |
| 4711 | |
| 4712 | // Only get the parenthesis range if it is a direct construction. |
| 4713 | SourceRange parenRange = |
| 4714 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4715 | Kind.getParenRange() : SourceRange(); |
| 4716 | |
| 4717 | // If the entity allows NRVO, mark the construction as elidable |
| 4718 | // unconditionally. |
| 4719 | if (Entity.allowsNRVO()) |
| 4720 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4721 | Constructor, /*Elidable=*/true, |
| 4722 | move_arg(ConstructorArgs), |
| 4723 | HadMultipleCandidates, |
| 4724 | ConstructorInitRequiresZeroInit, |
| 4725 | ConstructKind, |
| 4726 | parenRange); |
| 4727 | else |
| 4728 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4729 | Constructor, |
| 4730 | move_arg(ConstructorArgs), |
| 4731 | HadMultipleCandidates, |
| 4732 | ConstructorInitRequiresZeroInit, |
| 4733 | ConstructKind, |
| 4734 | parenRange); |
| 4735 | } |
| 4736 | if (CurInit.isInvalid()) |
| 4737 | return ExprError(); |
| 4738 | |
| 4739 | // Only check access if all of that succeeded. |
| 4740 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 4741 | Step.Function.FoundDecl.getAccess()); |
| 4742 | S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc); |
| 4743 | |
| 4744 | if (shouldBindAsTemporary(Entity)) |
| 4745 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4746 | |
| 4747 | return move(CurInit); |
| 4748 | } |
| 4749 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 4750 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 4751 | /// longer than the current full-expression. Conservatively returns false if |
| 4752 | /// it's unclear. |
| 4753 | static bool |
| 4754 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 4755 | const InitializedEntity *Top = &Entity; |
| 4756 | while (Top->getParent()) |
| 4757 | Top = Top->getParent(); |
| 4758 | |
| 4759 | switch (Top->getKind()) { |
| 4760 | case InitializedEntity::EK_Variable: |
| 4761 | case InitializedEntity::EK_Result: |
| 4762 | case InitializedEntity::EK_Exception: |
| 4763 | case InitializedEntity::EK_Member: |
| 4764 | case InitializedEntity::EK_New: |
| 4765 | case InitializedEntity::EK_Base: |
| 4766 | case InitializedEntity::EK_Delegating: |
| 4767 | return true; |
| 4768 | |
| 4769 | case InitializedEntity::EK_ArrayElement: |
| 4770 | case InitializedEntity::EK_VectorElement: |
| 4771 | case InitializedEntity::EK_BlockElement: |
| 4772 | case InitializedEntity::EK_ComplexElement: |
| 4773 | // Could not determine what the full initialization is. Assume it might not |
| 4774 | // outlive the full-expression. |
| 4775 | return false; |
| 4776 | |
| 4777 | case InitializedEntity::EK_Parameter: |
| 4778 | case InitializedEntity::EK_Temporary: |
| 4779 | case InitializedEntity::EK_LambdaCapture: |
| 4780 | // The entity being initialized might not outlive the full-expression. |
| 4781 | return false; |
| 4782 | } |
| 4783 | |
| 4784 | llvm_unreachable("unknown entity kind"); |
| 4785 | } |
| 4786 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4787 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4788 | InitializationSequence::Perform(Sema &S, |
| 4789 | const InitializedEntity &Entity, |
| 4790 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4791 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4792 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4793 | if (Failed()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4794 | unsigned NumArgs = Args.size(); |
| 4795 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4796 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4797 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4798 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4799 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4800 | // If the declaration is a non-dependent, incomplete array type |
| 4801 | // that has an initializer, then its type will be completed once |
| 4802 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4803 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4804 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4805 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4806 | if (const IncompleteArrayType *ArrayT |
| 4807 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4808 | // FIXME: We don't currently have the ability to accurately |
| 4809 | // compute the length of an initializer list without |
| 4810 | // performing full type-checking of the initializer list |
| 4811 | // (since we have to determine where braces are implicitly |
| 4812 | // introduced and such). So, we fall back to making the array |
| 4813 | // type a dependently-sized array type with no specified |
| 4814 | // bound. |
| 4815 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 4816 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4817 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4818 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4819 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4820 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4821 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4822 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4823 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4824 | Brackets = ArrayLoc->getBracketsRange(); |
| 4825 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
| 4828 | *ResultType |
| 4829 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4830 | /*NumElts=*/0, |
| 4831 | ArrayT->getSizeModifier(), |
| 4832 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4833 | Brackets); |
| 4834 | } |
| 4835 | |
| 4836 | } |
| 4837 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4838 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 4839 | !Kind.isExplicitCast()) { |
| 4840 | // Rebuild the ParenListExpr. |
| 4841 | SourceRange ParenRange = Kind.getParenRange(); |
| 4842 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
| 4843 | move(Args)); |
| 4844 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4845 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | a9b55a4 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 4846 | Kind.isExplicitCast() || |
| 4847 | Kind.getKind() == InitializationKind::IK_DirectList); |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4848 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4849 | } |
| 4850 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4851 | // No steps means no initialization. |
| 4852 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4853 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4854 | |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 4855 | if (S.getLangOpts().CPlusPlus0x && Entity.getType()->isReferenceType() && |
| 4856 | Args.size() == 1 && isa<InitListExpr>(Args.get()[0]) && |
| 4857 | Entity.getKind() != InitializedEntity::EK_Parameter) { |
| 4858 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 4859 | // from an initializer list. For parameters, we produce a better warning |
| 4860 | // elsewhere. |
| 4861 | Expr *Init = Args.get()[0]; |
| 4862 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 4863 | << Init->getSourceRange(); |
| 4864 | } |
| 4865 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 4866 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 4867 | // pointer obviously outlives the temporary. |
| 4868 | if (Args.size() == 1 && Args.get()[0]->getType()->isArrayType() && |
| 4869 | Entity.getType()->isPointerType() && |
| 4870 | InitializedEntityOutlivesFullExpression(Entity)) { |
| 4871 | Expr *Init = Args.get()[0]; |
| 4872 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 4873 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 4874 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 4875 | << Init->getSourceRange(); |
| 4876 | } |
| 4877 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4878 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4879 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4880 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4881 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4882 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4883 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4884 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4885 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4886 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4887 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4888 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4889 | // grab the only argument out the Args and place it into the "current" |
| 4890 | // initializer. |
| 4891 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4892 | case SK_ResolveAddressOfOverloadedFunction: |
| 4893 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4894 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4895 | case SK_CastDerivedToBaseLValue: |
| 4896 | case SK_BindReference: |
| 4897 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4898 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4899 | case SK_UserConversion: |
| 4900 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4901 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4902 | case SK_QualificationConversionRValue: |
| 4903 | case SK_ConversionSequence: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 4904 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4905 | case SK_ListInitialization: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4906 | case SK_UnwrapInitList: |
| 4907 | case SK_RewrapInitList: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4908 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4909 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4910 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4911 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4912 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4913 | case SK_PassByIndirectCopyRestore: |
| 4914 | case SK_PassByIndirectRestore: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 4915 | case SK_ProduceObjCObject: |
| 4916 | case SK_StdInitializerList: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4917 | assert(Args.size() == 1); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4918 | CurInit = Args.get()[0]; |
| 4919 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4920 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4921 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4922 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4923 | case SK_ConstructorInitialization: |
| 4924 | case SK_ZeroInitialization: |
| 4925 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4926 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4927 | |
| 4928 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4929 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4930 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4931 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4932 | Step != StepEnd; ++Step) { |
| 4933 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4934 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4935 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4936 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4937 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4938 | switch (Step->Kind) { |
| 4939 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4940 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4941 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4942 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4943 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4944 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4945 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4946 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4947 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4948 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4949 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4950 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4951 | case SK_CastDerivedToBaseLValue: { |
| 4952 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4953 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4954 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4955 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4956 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4957 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4958 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4959 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4960 | CurInit.get()->getLocStart(), |
| 4961 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4962 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4963 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4964 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4965 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4966 | QualType T = SourceType; |
| 4967 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4968 | T = Pointer->getPointeeType(); |
| 4969 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4970 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4971 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4972 | } |
| 4973 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4974 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4975 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4976 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4977 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4978 | VK_XValue : |
| 4979 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4980 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4981 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4982 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4983 | CurInit.get(), |
| 4984 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4985 | break; |
| 4986 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4987 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4988 | case SK_BindReference: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4989 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4990 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4991 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4992 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4993 | << BitField->getDeclName() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4994 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4995 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4996 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4997 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4998 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4999 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5000 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5001 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5002 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5003 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5004 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5005 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5006 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5007 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5008 | // Reference binding does not have any corresponding ASTs. |
| 5009 | |
| 5010 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5011 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5012 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5013 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5014 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5015 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5016 | case SK_BindReferenceToTemporary: |
| 5017 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5018 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5019 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5020 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5021 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 5022 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 5023 | Entity.getType().getNonReferenceType(), |
| 5024 | CurInit.get(), |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5025 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5026 | |
| 5027 | // If we're binding to an Objective-C object that has lifetime, we |
| 5028 | // need cleanups. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5029 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5030 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 5031 | S.ExprNeedsCleanups = true; |
| 5032 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5033 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5034 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5035 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5036 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5037 | /*IsExtraneousCopy=*/true); |
| 5038 | break; |
| 5039 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5040 | case SK_UserConversion: { |
| 5041 | // We have a user-defined conversion that invokes either a constructor |
| 5042 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5043 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5044 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5045 | FunctionDecl *Fn = Step->Function.Function; |
| 5046 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5047 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5048 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5049 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5050 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5051 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5052 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5053 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5054 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5055 | // Determine the arguments required to actually perform the constructor |
| 5056 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5057 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5058 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5059 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5060 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5061 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5062 | |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5063 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5064 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5065 | move_arg(ConstructorArgs), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5066 | HadMultipleCandidates, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5067 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5068 | CXXConstructExpr::CK_Complete, |
| 5069 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5070 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5071 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5072 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5073 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5074 | FoundFn.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5075 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5076 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5077 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5078 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5079 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5080 | S.IsDerivedFrom(SourceType, Class)) |
| 5081 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5082 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5083 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5084 | } else { |
| 5085 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5086 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5087 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5088 | FoundFn); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 5089 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5090 | |
| 5091 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5092 | // derived-to-base conversion? I believe the answer is "no", because |
| 5093 | // 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] | 5094 | ExprResult CurInitExprRes = |
| 5095 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 5096 | FoundFn, Conversion); |
| 5097 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5098 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5099 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5100 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5101 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5102 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 5103 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5104 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5105 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5106 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5107 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5108 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5109 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5110 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5111 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5112 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5113 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 5114 | |
| 5115 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5116 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5117 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5118 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5119 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5120 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5121 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5122 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5123 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5124 | } |
| 5125 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5126 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5127 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5128 | CurInit.get()->getType(), |
| 5129 | CastKind, CurInit.get(), 0, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 5130 | CurInit.get()->getValueKind())); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5131 | if (MaybeBindToTemp) |
| 5132 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5133 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5134 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 5135 | move(CurInit), /*IsExtraneousCopy=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5136 | break; |
| 5137 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5138 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5139 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5140 | case SK_QualificationConversionXValue: |
| 5141 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5142 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5143 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5144 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5145 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5146 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5147 | VK_XValue : |
| 5148 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5149 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5150 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5153 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5154 | Sema::CheckedConversionKind CCK |
| 5155 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 5156 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 5157 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5158 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5159 | ExprResult CurInitExprRes = |
| 5160 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5161 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5162 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5163 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5164 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5165 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5166 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5167 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5168 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5169 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5170 | // Hack: We must pass *ResultType if available in order to set the type |
| 5171 | // of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 5172 | // But in 'const X &x = {1, 2, 3};' we're supposed to initialize a |
| 5173 | // temporary, not a reference, so we should pass Ty. |
| 5174 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 5175 | // Since this step is never used for a reference directly, we explicitly |
| 5176 | // unwrap references here and rewrap them afterwards. |
| 5177 | // We also need to create a InitializeTemporary entity for this. |
| 5178 | QualType Ty = ResultType ? ResultType->getNonReferenceType() : Step->Type; |
Sebastian Redl | cbf8209 | 2012-03-07 16:10:45 +0000 | [diff] [blame] | 5179 | bool IsTemporary = Entity.getType()->isReferenceType(); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5180 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
| 5181 | InitListChecker PerformInitList(S, IsTemporary ? TempEntity : Entity, |
| 5182 | InitList, Ty, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5183 | Kind.getKind() != InitializationKind::IK_DirectList || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5184 | !S.getLangOpts().CPlusPlus0x); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5185 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5186 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5187 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5188 | if (ResultType) { |
| 5189 | if ((*ResultType)->isRValueReferenceType()) |
| 5190 | Ty = S.Context.getRValueReferenceType(Ty); |
| 5191 | else if ((*ResultType)->isLValueReferenceType()) |
| 5192 | Ty = S.Context.getLValueReferenceType(Ty, |
| 5193 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 5194 | *ResultType = Ty; |
| 5195 | } |
| 5196 | |
| 5197 | InitListExpr *StructuredInitList = |
| 5198 | PerformInitList.getFullyStructuredList(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5199 | CurInit.release(); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5200 | CurInit = S.Owned(StructuredInitList); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5201 | break; |
| 5202 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5203 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5204 | case SK_ListConstructorCall: { |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5205 | // When an initializer list is passed for a parameter of type "reference |
| 5206 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5207 | // EK_Parameter entity with reference type. |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5208 | // FIXME: This is a hack. What we really should do is create a user |
| 5209 | // conversion step for this case, but this makes it considerably more |
| 5210 | // complicated. For now, this will do. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5211 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5212 | Entity.getType().getNonReferenceType()); |
| 5213 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5214 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5215 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 5216 | << InitList->getSourceRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5217 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5218 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 5219 | Entity, |
| 5220 | Kind, move(Arg), *Step, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5221 | ConstructorInitRequiresZeroInit); |
| 5222 | break; |
| 5223 | } |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5224 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5225 | case SK_UnwrapInitList: |
| 5226 | CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0)); |
| 5227 | break; |
| 5228 | |
| 5229 | case SK_RewrapInitList: { |
| 5230 | Expr *E = CurInit.take(); |
| 5231 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 5232 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
| 5233 | Syntactic->getLBraceLoc(), &E, 1, Syntactic->getRBraceLoc()); |
| 5234 | ILE->setSyntacticForm(Syntactic); |
| 5235 | ILE->setType(E->getType()); |
| 5236 | ILE->setValueKind(E->getValueKind()); |
| 5237 | CurInit = S.Owned(ILE); |
| 5238 | break; |
| 5239 | } |
| 5240 | |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5241 | case SK_ConstructorInitialization: { |
| 5242 | // When an initializer list is passed for a parameter of type "reference |
| 5243 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5244 | // EK_Parameter entity with reference type. |
| 5245 | // FIXME: This is a hack. What we really should do is create a user |
| 5246 | // conversion step for this case, but this makes it considerably more |
| 5247 | // complicated. For now, this will do. |
| 5248 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5249 | Entity.getType().getNonReferenceType()); |
| 5250 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 5251 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity |
| 5252 | : Entity, |
| 5253 | Kind, move(Args), *Step, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5254 | ConstructorInitRequiresZeroInit); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5255 | break; |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5256 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5257 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5258 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5259 | step_iterator NextStep = Step; |
| 5260 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5261 | if (NextStep != StepEnd && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5262 | NextStep->Kind == SK_ConstructorInitialization) { |
| 5263 | // The need for zero-initialization is recorded directly into |
| 5264 | // the call to the object's constructor within the next step. |
| 5265 | ConstructorInitRequiresZeroInit = true; |
| 5266 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5267 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5268 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5269 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5270 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5271 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5272 | Kind.getRange().getBegin()); |
| 5273 | |
| 5274 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 5275 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 5276 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5277 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5278 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5279 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5280 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5281 | break; |
| 5282 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5283 | |
| 5284 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5285 | QualType SourceType = CurInit.get()->getType(); |
| 5286 | ExprResult Result = move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5287 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5288 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 5289 | if (Result.isInvalid()) |
| 5290 | return ExprError(); |
| 5291 | CurInit = move(Result); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5292 | |
| 5293 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5294 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5295 | if (ConvTy != Sema::Compatible && |
| 5296 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5297 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5298 | == Sema::Compatible) |
| 5299 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5300 | if (CurInitExprRes.isInvalid()) |
| 5301 | return ExprError(); |
| 5302 | CurInit = move(CurInitExprRes); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5303 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5304 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5305 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 5306 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5307 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5308 | getAssignmentAction(Entity), |
| 5309 | &Complained)) { |
| 5310 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5311 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5312 | } else if (Complained) |
| 5313 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5314 | break; |
| 5315 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5316 | |
| 5317 | case SK_StringInit: { |
| 5318 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5319 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 5320 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5321 | break; |
| 5322 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5323 | |
| 5324 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5325 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5326 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 5327 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5328 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5329 | |
| 5330 | case SK_ArrayInit: |
| 5331 | // Okay: we checked everything before creating this step. Note that |
| 5332 | // this is a GNU extension. |
| 5333 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5334 | << Step->Type << CurInit.get()->getType() |
| 5335 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5336 | |
| 5337 | // If the destination type is an incomplete array type, update the |
| 5338 | // type accordingly. |
| 5339 | if (ResultType) { |
| 5340 | if (const IncompleteArrayType *IncompleteDest |
| 5341 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 5342 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5343 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5344 | *ResultType = S.Context.getConstantArrayType( |
| 5345 | IncompleteDest->getElementType(), |
| 5346 | ConstantSource->getSize(), |
| 5347 | ArrayType::Normal, 0); |
| 5348 | } |
| 5349 | } |
| 5350 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5351 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5352 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5353 | case SK_ParenthesizedArrayInit: |
| 5354 | // Okay: we checked everything before creating this step. Note that |
| 5355 | // this is a GNU extension. |
| 5356 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 5357 | << CurInit.get()->getSourceRange(); |
| 5358 | break; |
| 5359 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5360 | case SK_PassByIndirectCopyRestore: |
| 5361 | case SK_PassByIndirectRestore: |
| 5362 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 5363 | CurInit = S.Owned(new (S.Context) |
| 5364 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 5365 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 5366 | break; |
| 5367 | |
| 5368 | case SK_ProduceObjCObject: |
| 5369 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5370 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5371 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5372 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5373 | |
| 5374 | case SK_StdInitializerList: { |
| 5375 | QualType Dest = Step->Type; |
| 5376 | QualType E; |
| 5377 | bool Success = S.isStdInitializerList(Dest, &E); |
| 5378 | (void)Success; |
| 5379 | assert(Success && "Destination type changed?"); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 5380 | |
| 5381 | // If the element type has a destructor, check it. |
| 5382 | if (CXXRecordDecl *RD = E->getAsCXXRecordDecl()) { |
| 5383 | if (!RD->hasIrrelevantDestructor()) { |
| 5384 | if (CXXDestructorDecl *Destructor = S.LookupDestructor(RD)) { |
| 5385 | S.MarkFunctionReferenced(Kind.getLocation(), Destructor); |
| 5386 | S.CheckDestructorAccess(Kind.getLocation(), Destructor, |
| 5387 | S.PDiag(diag::err_access_dtor_temp) << E); |
| 5388 | S.DiagnoseUseOfDecl(Destructor, Kind.getLocation()); |
| 5389 | } |
| 5390 | } |
| 5391 | } |
| 5392 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5393 | InitListExpr *ILE = cast<InitListExpr>(CurInit.take()); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5394 | S.Diag(ILE->getExprLoc(), diag::warn_cxx98_compat_initializer_list_init) |
| 5395 | << ILE->getSourceRange(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5396 | unsigned NumInits = ILE->getNumInits(); |
| 5397 | SmallVector<Expr*, 16> Converted(NumInits); |
| 5398 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 5399 | S.Context.getConstantArrayType(E, |
| 5400 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 5401 | NumInits), |
| 5402 | ArrayType::Normal, 0)); |
| 5403 | InitializedEntity Element =InitializedEntity::InitializeElement(S.Context, |
| 5404 | 0, HiddenArray); |
| 5405 | for (unsigned i = 0; i < NumInits; ++i) { |
| 5406 | Element.setElementIndex(i); |
| 5407 | ExprResult Init = S.Owned(ILE->getInit(i)); |
| 5408 | ExprResult Res = S.PerformCopyInitialization(Element, |
| 5409 | Init.get()->getExprLoc(), |
| 5410 | Init); |
| 5411 | assert(!Res.isInvalid() && "Result changed since try phase."); |
| 5412 | Converted[i] = Res.take(); |
| 5413 | } |
| 5414 | InitListExpr *Semantic = new (S.Context) |
| 5415 | InitListExpr(S.Context, ILE->getLBraceLoc(), |
| 5416 | Converted.data(), NumInits, ILE->getRBraceLoc()); |
| 5417 | Semantic->setSyntacticForm(ILE); |
| 5418 | Semantic->setType(Dest); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 5419 | Semantic->setInitializesStdInitializerList(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5420 | CurInit = S.Owned(Semantic); |
| 5421 | break; |
| 5422 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5423 | } |
| 5424 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5425 | |
| 5426 | // Diagnose non-fatal problems with the completed initialization. |
| 5427 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5428 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 5429 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 5430 | cast<FieldDecl>(Entity.getDecl()), |
| 5431 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5432 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5433 | return move(CurInit); |
| 5434 | } |
| 5435 | |
| 5436 | //===----------------------------------------------------------------------===// |
| 5437 | // Diagnose initialization failures |
| 5438 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5439 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5440 | const InitializedEntity &Entity, |
| 5441 | const InitializationKind &Kind, |
| 5442 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5443 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5444 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5445 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5446 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5447 | switch (Failure) { |
| 5448 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5449 | // FIXME: Customize for the initialized entity? |
| 5450 | if (NumArgs == 0) |
| 5451 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 5452 | << DestType.getNonReferenceType(); |
| 5453 | else // FIXME: diagnostic below could be better! |
| 5454 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 5455 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5456 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5457 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5458 | case FK_ArrayNeedsInitList: |
| 5459 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5460 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 5461 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 5462 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5463 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5464 | case FK_ArrayTypeMismatch: |
| 5465 | case FK_NonConstantArrayInit: |
| 5466 | S.Diag(Kind.getLocation(), |
| 5467 | (Failure == FK_ArrayTypeMismatch |
| 5468 | ? diag::err_array_init_different_type |
| 5469 | : diag::err_array_init_non_constant_array)) |
| 5470 | << DestType.getNonReferenceType() |
| 5471 | << Args[0]->getType() |
| 5472 | << Args[0]->getSourceRange(); |
| 5473 | break; |
| 5474 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5475 | case FK_VariableLengthArrayHasInitializer: |
| 5476 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 5477 | << Args[0]->getSourceRange(); |
| 5478 | break; |
| 5479 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5480 | case FK_AddressOfOverloadFailed: { |
| 5481 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5482 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5483 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5484 | true, |
| 5485 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5486 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5487 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5488 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5489 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5490 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5491 | switch (FailedOverloadResult) { |
| 5492 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5493 | if (Failure == FK_UserConversionOverloadFailed) |
| 5494 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 5495 | << Args[0]->getType() << DestType |
| 5496 | << Args[0]->getSourceRange(); |
| 5497 | else |
| 5498 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 5499 | << DestType << Args[0]->getType() |
| 5500 | << Args[0]->getSourceRange(); |
| 5501 | |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5502 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 5503 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5504 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5505 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5506 | case OR_No_Viable_Function: |
| 5507 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 5508 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5509 | << Args[0]->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5510 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, |
| 5511 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5512 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5513 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5514 | case OR_Deleted: { |
| 5515 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 5516 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5517 | << Args[0]->getSourceRange(); |
| 5518 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5519 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5520 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 5521 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5522 | if (Ovl == OR_Deleted) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5523 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5524 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5525 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5526 | } |
| 5527 | break; |
| 5528 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5529 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5530 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5531 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5532 | } |
| 5533 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5534 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5535 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5536 | if (isa<InitListExpr>(Args[0])) { |
| 5537 | S.Diag(Kind.getLocation(), |
| 5538 | diag::err_lvalue_reference_bind_to_initlist) |
| 5539 | << DestType.getNonReferenceType().isVolatileQualified() |
| 5540 | << DestType.getNonReferenceType() |
| 5541 | << Args[0]->getSourceRange(); |
| 5542 | break; |
| 5543 | } |
| 5544 | // Intentional fallthrough |
| 5545 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5546 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5547 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5548 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 5549 | ? diag::err_lvalue_reference_bind_to_temporary |
| 5550 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 5551 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5552 | << DestType.getNonReferenceType() |
| 5553 | << Args[0]->getType() |
| 5554 | << Args[0]->getSourceRange(); |
| 5555 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5556 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5557 | case FK_RValueReferenceBindingToLValue: |
| 5558 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 5559 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5560 | << Args[0]->getSourceRange(); |
| 5561 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5562 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5563 | case FK_ReferenceInitDropsQualifiers: |
| 5564 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 5565 | << DestType.getNonReferenceType() |
| 5566 | << Args[0]->getType() |
| 5567 | << Args[0]->getSourceRange(); |
| 5568 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5569 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5570 | case FK_ReferenceInitFailed: |
| 5571 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 5572 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5573 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5574 | << Args[0]->getType() |
| 5575 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 5576 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5577 | Args[0]->getType()->isObjCObjectPointerType()) |
| 5578 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5579 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5580 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5581 | case FK_ConversionFailed: { |
| 5582 | QualType FromType = Args[0]->getType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5583 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5584 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5585 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5586 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5587 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5588 | << Args[0]->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5589 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 5590 | S.Diag(Kind.getLocation(), PDiag); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 5591 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5592 | Args[0]->getType()->isObjCObjectPointerType()) |
| 5593 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5594 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5595 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5596 | |
| 5597 | case FK_ConversionFromPropertyFailed: |
| 5598 | // No-op. This error has already been reported. |
| 5599 | break; |
| 5600 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5601 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5602 | SourceRange R; |
| 5603 | |
| 5604 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5605 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5606 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5607 | else |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5608 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5609 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5610 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 5611 | if (Kind.isCStyleOrFunctionalCast()) |
| 5612 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 5613 | << R; |
| 5614 | else |
| 5615 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 5616 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5617 | break; |
| 5618 | } |
| 5619 | |
| 5620 | case FK_ReferenceBindingToInitList: |
| 5621 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 5622 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 5623 | break; |
| 5624 | |
| 5625 | case FK_InitListBadDestinationType: |
| 5626 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 5627 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 5628 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5629 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5630 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5631 | case FK_ConstructorOverloadFailed: { |
| 5632 | SourceRange ArgsRange; |
| 5633 | if (NumArgs) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5634 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5635 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5636 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5637 | if (Failure == FK_ListConstructorOverloadFailed) { |
| 5638 | assert(NumArgs == 1 && "List construction from other than 1 argument."); |
| 5639 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 5640 | Args = InitList->getInits(); |
| 5641 | NumArgs = InitList->getNumInits(); |
| 5642 | } |
| 5643 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5644 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 5645 | // bad. |
| 5646 | switch (FailedOverloadResult) { |
| 5647 | case OR_Ambiguous: |
| 5648 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 5649 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5650 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5651 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5652 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5653 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5654 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5655 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 5656 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 5657 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 5658 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5659 | // This is implicit default initialization of a member or |
| 5660 | // base within a constructor. If no viable function was |
| 5661 | // found, notify the user that she needs to explicitly |
| 5662 | // initialize this base/member. |
| 5663 | CXXConstructorDecl *Constructor |
| 5664 | = cast<CXXConstructorDecl>(S.CurContext); |
| 5665 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5666 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5667 | << Constructor->isImplicit() |
| 5668 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5669 | << /*base=*/0 |
| 5670 | << Entity.getType(); |
| 5671 | |
| 5672 | RecordDecl *BaseDecl |
| 5673 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 5674 | ->getDecl(); |
| 5675 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 5676 | << S.Context.getTagDeclType(BaseDecl); |
| 5677 | } else { |
| 5678 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5679 | << Constructor->isImplicit() |
| 5680 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5681 | << /*member=*/1 |
| 5682 | << Entity.getName(); |
| 5683 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 5684 | |
| 5685 | if (const RecordType *Record |
| 5686 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5687 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5688 | diag::note_previous_decl) |
| 5689 | << S.Context.getTagDeclType(Record->getDecl()); |
| 5690 | } |
| 5691 | break; |
| 5692 | } |
| 5693 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5694 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 5695 | << DestType << ArgsRange; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5696 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, |
| 5697 | llvm::makeArrayRef(Args, NumArgs)); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5698 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5699 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5700 | case OR_Deleted: { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5701 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5702 | OverloadingResult Ovl |
| 5703 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5704 | if (Ovl != OR_Deleted) { |
| 5705 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 5706 | << true << DestType << ArgsRange; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5707 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5708 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5709 | } |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5710 | |
| 5711 | // If this is a defaulted or implicitly-declared function, then |
| 5712 | // it was implicitly deleted. Make it clear that the deletion was |
| 5713 | // implicit. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5714 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5715 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5716 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 5717 | << DestType << ArgsRange; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5718 | else |
| 5719 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 5720 | << true << DestType << ArgsRange; |
| 5721 | |
| 5722 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5723 | break; |
| 5724 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5725 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5726 | case OR_Success: |
| 5727 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5728 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5729 | } |
David Blaikie | 9fdefb3 | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 5730 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5731 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5732 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5733 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5734 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5735 | // This is implicit default-initialization of a const member in |
| 5736 | // a constructor. Complain that it needs to be explicitly |
| 5737 | // initialized. |
| 5738 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 5739 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 5740 | << Constructor->isImplicit() |
| 5741 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5742 | << /*const=*/1 |
| 5743 | << Entity.getName(); |
| 5744 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 5745 | << Entity.getName(); |
| 5746 | } else { |
| 5747 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 5748 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 5749 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5750 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5751 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5752 | case FK_Incomplete: |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 5753 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5754 | diag::err_init_incomplete_type); |
| 5755 | break; |
| 5756 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5757 | case FK_ListInitializationFailed: { |
| 5758 | // Run the init list checker again to emit diagnostics. |
| 5759 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 5760 | QualType DestType = Entity.getType(); |
| 5761 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 5762 | DestType, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5763 | Kind.getKind() != InitializationKind::IK_DirectList || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5764 | !S.getLangOpts().CPlusPlus0x); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5765 | assert(DiagnoseInitList.HadError() && |
| 5766 | "Inconsistent init list check result."); |
| 5767 | break; |
| 5768 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 5769 | |
| 5770 | case FK_PlaceholderType: { |
| 5771 | // FIXME: Already diagnosed! |
| 5772 | break; |
| 5773 | } |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5774 | |
| 5775 | case FK_InitListElementCopyFailure: { |
| 5776 | // Try to perform all copies again. |
| 5777 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 5778 | unsigned NumInits = InitList->getNumInits(); |
| 5779 | QualType DestType = Entity.getType(); |
| 5780 | QualType E; |
| 5781 | bool Success = S.isStdInitializerList(DestType, &E); |
| 5782 | (void)Success; |
| 5783 | assert(Success && "Where did the std::initializer_list go?"); |
| 5784 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 5785 | S.Context.getConstantArrayType(E, |
| 5786 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 5787 | NumInits), |
| 5788 | ArrayType::Normal, 0)); |
| 5789 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 5790 | 0, HiddenArray); |
| 5791 | // Show at most 3 errors. Otherwise, you'd get a lot of errors for errors |
| 5792 | // where the init list type is wrong, e.g. |
| 5793 | // std::initializer_list<void*> list = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 5794 | // FIXME: Emit a note if we hit the limit? |
| 5795 | int ErrorCount = 0; |
| 5796 | for (unsigned i = 0; i < NumInits && ErrorCount < 3; ++i) { |
| 5797 | Element.setElementIndex(i); |
| 5798 | ExprResult Init = S.Owned(InitList->getInit(i)); |
| 5799 | if (S.PerformCopyInitialization(Element, Init.get()->getExprLoc(), Init) |
| 5800 | .isInvalid()) |
| 5801 | ++ErrorCount; |
| 5802 | } |
| 5803 | break; |
| 5804 | } |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 5805 | |
| 5806 | case FK_ExplicitConstructor: { |
| 5807 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 5808 | << Args[0]->getSourceRange(); |
| 5809 | OverloadCandidateSet::iterator Best; |
| 5810 | OverloadingResult Ovl |
| 5811 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | e7d0bbf | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 5812 | (void)Ovl; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 5813 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 5814 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 5815 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 5816 | break; |
| 5817 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5818 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5819 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5820 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5821 | return true; |
| 5822 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5823 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5824 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5825 | switch (SequenceKind) { |
| 5826 | case FailedSequence: { |
| 5827 | OS << "Failed sequence: "; |
| 5828 | switch (Failure) { |
| 5829 | case FK_TooManyInitsForReference: |
| 5830 | OS << "too many initializers for reference"; |
| 5831 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5832 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5833 | case FK_ArrayNeedsInitList: |
| 5834 | OS << "array requires initializer list"; |
| 5835 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5836 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5837 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5838 | OS << "array requires initializer list or string literal"; |
| 5839 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5840 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5841 | case FK_ArrayTypeMismatch: |
| 5842 | OS << "array type mismatch"; |
| 5843 | break; |
| 5844 | |
| 5845 | case FK_NonConstantArrayInit: |
| 5846 | OS << "non-constant array initializer"; |
| 5847 | break; |
| 5848 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5849 | case FK_AddressOfOverloadFailed: |
| 5850 | OS << "address of overloaded function failed"; |
| 5851 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5852 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5853 | case FK_ReferenceInitOverloadFailed: |
| 5854 | OS << "overload resolution for reference initialization failed"; |
| 5855 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5856 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5857 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 5858 | OS << "non-const lvalue reference bound to temporary"; |
| 5859 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5860 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5861 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 5862 | OS << "non-const lvalue reference bound to unrelated type"; |
| 5863 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5864 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5865 | case FK_RValueReferenceBindingToLValue: |
| 5866 | OS << "rvalue reference bound to an lvalue"; |
| 5867 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5868 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5869 | case FK_ReferenceInitDropsQualifiers: |
| 5870 | OS << "reference initialization drops qualifiers"; |
| 5871 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5872 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5873 | case FK_ReferenceInitFailed: |
| 5874 | OS << "reference initialization failed"; |
| 5875 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5876 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5877 | case FK_ConversionFailed: |
| 5878 | OS << "conversion failed"; |
| 5879 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5880 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5881 | case FK_ConversionFromPropertyFailed: |
| 5882 | OS << "conversion from property failed"; |
| 5883 | break; |
| 5884 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5885 | case FK_TooManyInitsForScalar: |
| 5886 | OS << "too many initializers for scalar"; |
| 5887 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5888 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5889 | case FK_ReferenceBindingToInitList: |
| 5890 | OS << "referencing binding to initializer list"; |
| 5891 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5892 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5893 | case FK_InitListBadDestinationType: |
| 5894 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 5895 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5896 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5897 | case FK_UserConversionOverloadFailed: |
| 5898 | OS << "overloading failed for user-defined conversion"; |
| 5899 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5900 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5901 | case FK_ConstructorOverloadFailed: |
| 5902 | OS << "constructor overloading failed"; |
| 5903 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5904 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5905 | case FK_DefaultInitOfConst: |
| 5906 | OS << "default initialization of a const variable"; |
| 5907 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5908 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5909 | case FK_Incomplete: |
| 5910 | OS << "initialization of incomplete type"; |
| 5911 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5912 | |
| 5913 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5914 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 5915 | break; |
| 5916 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5917 | case FK_VariableLengthArrayHasInitializer: |
| 5918 | OS << "variable length array has an initializer"; |
| 5919 | break; |
| 5920 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 5921 | case FK_PlaceholderType: |
| 5922 | OS << "initializer expression isn't contextually valid"; |
| 5923 | break; |
Nick Lewycky | b0c6c33 | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 5924 | |
| 5925 | case FK_ListConstructorOverloadFailed: |
| 5926 | OS << "list constructor overloading failed"; |
| 5927 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5928 | |
| 5929 | case FK_InitListElementCopyFailure: |
| 5930 | OS << "copy construction of initializer list element failed"; |
| 5931 | break; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 5932 | |
| 5933 | case FK_ExplicitConstructor: |
| 5934 | OS << "list copy initialization chose explicit constructor"; |
| 5935 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5936 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5937 | OS << '\n'; |
| 5938 | return; |
| 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 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5942 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5943 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5944 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5945 | case NormalSequence: |
| 5946 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5947 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5948 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5949 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5950 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 5951 | if (S != step_begin()) { |
| 5952 | OS << " -> "; |
| 5953 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5954 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5955 | switch (S->Kind) { |
| 5956 | case SK_ResolveAddressOfOverloadedFunction: |
| 5957 | OS << "resolve address of overloaded function"; |
| 5958 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5959 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5960 | case SK_CastDerivedToBaseRValue: |
| 5961 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 5962 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5963 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5964 | case SK_CastDerivedToBaseXValue: |
| 5965 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 5966 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5967 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5968 | case SK_CastDerivedToBaseLValue: |
| 5969 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 5970 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5971 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5972 | case SK_BindReference: |
| 5973 | OS << "bind reference to lvalue"; |
| 5974 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5975 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5976 | case SK_BindReferenceToTemporary: |
| 5977 | OS << "bind reference to a temporary"; |
| 5978 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5979 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5980 | case SK_ExtraneousCopyToTemporary: |
| 5981 | OS << "extraneous C++03 copy to temporary"; |
| 5982 | break; |
| 5983 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5984 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 5985 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5986 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5987 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5988 | case SK_QualificationConversionRValue: |
| 5989 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5990 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5991 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5992 | case SK_QualificationConversionXValue: |
| 5993 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5994 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5995 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5996 | case SK_QualificationConversionLValue: |
| 5997 | OS << "qualification conversion (lvalue)"; |
| 5998 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5999 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6000 | case SK_ConversionSequence: |
| 6001 | OS << "implicit conversion sequence ("; |
| 6002 | S->ICS->DebugPrint(); // FIXME: use OS |
| 6003 | OS << ")"; |
| 6004 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6005 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6006 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6007 | OS << "list aggregate initialization"; |
| 6008 | break; |
| 6009 | |
| 6010 | case SK_ListConstructorCall: |
| 6011 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6012 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6013 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6014 | case SK_UnwrapInitList: |
| 6015 | OS << "unwrap reference initializer list"; |
| 6016 | break; |
| 6017 | |
| 6018 | case SK_RewrapInitList: |
| 6019 | OS << "rewrap reference initializer list"; |
| 6020 | break; |
| 6021 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6022 | case SK_ConstructorInitialization: |
| 6023 | OS << "constructor initialization"; |
| 6024 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6025 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6026 | case SK_ZeroInitialization: |
| 6027 | OS << "zero initialization"; |
| 6028 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6029 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6030 | case SK_CAssignment: |
| 6031 | OS << "C assignment"; |
| 6032 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6033 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6034 | case SK_StringInit: |
| 6035 | OS << "string initialization"; |
| 6036 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6037 | |
| 6038 | case SK_ObjCObjectConversion: |
| 6039 | OS << "Objective-C object conversion"; |
| 6040 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6041 | |
| 6042 | case SK_ArrayInit: |
| 6043 | OS << "array initialization"; |
| 6044 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6045 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6046 | case SK_ParenthesizedArrayInit: |
| 6047 | OS << "parenthesized array initialization"; |
| 6048 | break; |
| 6049 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6050 | case SK_PassByIndirectCopyRestore: |
| 6051 | OS << "pass by indirect copy and restore"; |
| 6052 | break; |
| 6053 | |
| 6054 | case SK_PassByIndirectRestore: |
| 6055 | OS << "pass by indirect restore"; |
| 6056 | break; |
| 6057 | |
| 6058 | case SK_ProduceObjCObject: |
| 6059 | OS << "Objective-C object retension"; |
| 6060 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6061 | |
| 6062 | case SK_StdInitializerList: |
| 6063 | OS << "std::initializer_list from initializer list"; |
| 6064 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6065 | } |
| 6066 | } |
| 6067 | } |
| 6068 | |
| 6069 | void InitializationSequence::dump() const { |
| 6070 | dump(llvm::errs()); |
| 6071 | } |
| 6072 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6073 | static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq, |
| 6074 | QualType EntityType, |
| 6075 | const Expr *PreInit, |
| 6076 | const Expr *PostInit) { |
| 6077 | if (Seq.step_begin() == Seq.step_end() || PreInit->isValueDependent()) |
| 6078 | return; |
| 6079 | |
| 6080 | // A narrowing conversion can only appear as the final implicit conversion in |
| 6081 | // an initialization sequence. |
| 6082 | const InitializationSequence::Step &LastStep = Seq.step_end()[-1]; |
| 6083 | if (LastStep.Kind != InitializationSequence::SK_ConversionSequence) |
| 6084 | return; |
| 6085 | |
| 6086 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 6087 | const StandardConversionSequence *SCS = 0; |
| 6088 | switch (ICS.getKind()) { |
| 6089 | case ImplicitConversionSequence::StandardConversion: |
| 6090 | SCS = &ICS.Standard; |
| 6091 | break; |
| 6092 | case ImplicitConversionSequence::UserDefinedConversion: |
| 6093 | SCS = &ICS.UserDefined.After; |
| 6094 | break; |
| 6095 | case ImplicitConversionSequence::AmbiguousConversion: |
| 6096 | case ImplicitConversionSequence::EllipsisConversion: |
| 6097 | case ImplicitConversionSequence::BadConversion: |
| 6098 | return; |
| 6099 | } |
| 6100 | |
| 6101 | // Determine the type prior to the narrowing conversion. If a conversion |
| 6102 | // operator was used, this may be different from both the type of the entity |
| 6103 | // and of the pre-initialization expression. |
| 6104 | QualType PreNarrowingType = PreInit->getType(); |
| 6105 | if (Seq.step_begin() + 1 != Seq.step_end()) |
| 6106 | PreNarrowingType = Seq.step_end()[-2].Type; |
| 6107 | |
| 6108 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 6109 | APValue ConstantValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6110 | QualType ConstantType; |
| 6111 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 6112 | ConstantType)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6113 | case NK_Not_Narrowing: |
| 6114 | // No narrowing occurred. |
| 6115 | return; |
| 6116 | |
| 6117 | case NK_Type_Narrowing: |
| 6118 | // This was a floating-to-integer conversion, which is always considered a |
| 6119 | // narrowing conversion even if the value is a constant and can be |
| 6120 | // represented exactly as an integer. |
| 6121 | S.Diag(PostInit->getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6122 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus0x? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6123 | diag::warn_init_list_type_narrowing |
| 6124 | : S.isSFINAEContext()? |
| 6125 | diag::err_init_list_type_narrowing_sfinae |
| 6126 | : diag::err_init_list_type_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6127 | << PostInit->getSourceRange() |
| 6128 | << PreNarrowingType.getLocalUnqualifiedType() |
| 6129 | << EntityType.getLocalUnqualifiedType(); |
| 6130 | break; |
| 6131 | |
| 6132 | case NK_Constant_Narrowing: |
| 6133 | // A constant value was narrowed. |
| 6134 | S.Diag(PostInit->getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6135 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus0x? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6136 | diag::warn_init_list_constant_narrowing |
| 6137 | : S.isSFINAEContext()? |
| 6138 | diag::err_init_list_constant_narrowing_sfinae |
| 6139 | : diag::err_init_list_constant_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6140 | << PostInit->getSourceRange() |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6141 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6142 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6143 | break; |
| 6144 | |
| 6145 | case NK_Variable_Narrowing: |
| 6146 | // A variable's value may have been narrowed. |
| 6147 | S.Diag(PostInit->getLocStart(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6148 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus0x? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6149 | diag::warn_init_list_variable_narrowing |
| 6150 | : S.isSFINAEContext()? |
| 6151 | diag::err_init_list_variable_narrowing_sfinae |
| 6152 | : diag::err_init_list_variable_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6153 | << PostInit->getSourceRange() |
| 6154 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6155 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6156 | break; |
| 6157 | } |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6158 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6159 | SmallString<128> StaticCast; |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6160 | llvm::raw_svector_ostream OS(StaticCast); |
| 6161 | OS << "static_cast<"; |
| 6162 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 6163 | // It's important to use the typedef's name if there is one so that the |
| 6164 | // fixit doesn't break code using types like int64_t. |
| 6165 | // |
| 6166 | // FIXME: This will break if the typedef requires qualification. But |
| 6167 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6168 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6169 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6170 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6171 | else { |
| 6172 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 6173 | // with a broken cast. |
| 6174 | return; |
| 6175 | } |
| 6176 | OS << ">("; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6177 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_override) |
| 6178 | << PostInit->getSourceRange() |
| 6179 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6180 | << FixItHint::CreateInsertion( |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6181 | S.getPreprocessor().getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6182 | } |
| 6183 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6184 | //===----------------------------------------------------------------------===// |
| 6185 | // Initialization helper functions |
| 6186 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6187 | bool |
| 6188 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 6189 | ExprResult Init) { |
| 6190 | if (Init.isInvalid()) |
| 6191 | return false; |
| 6192 | |
| 6193 | Expr *InitE = Init.get(); |
| 6194 | assert(InitE && "No initialization expression"); |
| 6195 | |
| 6196 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 6197 | SourceLocation()); |
| 6198 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 6199 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6200 | } |
| 6201 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6202 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6203 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 6204 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6205 | ExprResult Init, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6206 | bool TopLevelOfInitList, |
| 6207 | bool AllowExplicit) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6208 | if (Init.isInvalid()) |
| 6209 | return ExprError(); |
| 6210 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6211 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6212 | assert(InitE && "No initialization expression?"); |
| 6213 | |
| 6214 | if (EqualLoc.isInvalid()) |
| 6215 | EqualLoc = InitE->getLocStart(); |
| 6216 | |
| 6217 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6218 | EqualLoc, |
| 6219 | AllowExplicit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6220 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 6221 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6222 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6223 | ExprResult Result = Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
| 6224 | |
| 6225 | if (!Result.isInvalid() && TopLevelOfInitList) |
| 6226 | DiagnoseNarrowingInInitList(*this, Seq, Entity.getType(), |
| 6227 | InitE, Result.get()); |
| 6228 | |
| 6229 | return Result; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6230 | } |