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" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 26 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 27 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 28 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // Sema Initialization Checking |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 33 | static Expr *IsStringInit(Expr *Init, const ArrayType *AT, |
| 34 | ASTContext &Context) { |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 35 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 36 | return 0; |
| 37 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 38 | // See if this is a string literal or @encode. |
| 39 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 41 | // Handle @encode, which is a narrow string. |
| 42 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 43 | return Init; |
| 44 | |
| 45 | // Otherwise we can only handle string literals. |
| 46 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 220b636 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 47 | if (SL == 0) return 0; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 48 | |
| 49 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 50 | |
| 51 | switch (SL->getKind()) { |
| 52 | case StringLiteral::Ascii: |
| 53 | case StringLiteral::UTF8: |
| 54 | // char array can be initialized with a narrow string. |
| 55 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 56 | return ElemTy->isCharType() ? Init : 0; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 57 | case StringLiteral::UTF16: |
| 58 | return ElemTy->isChar16Type() ? Init : 0; |
| 59 | case StringLiteral::UTF32: |
| 60 | return ElemTy->isChar32Type() ? Init : 0; |
| 61 | case StringLiteral::Wide: |
| 62 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 63 | // correction from DR343): "An array with element type compatible with a |
| 64 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 65 | // string literal, optionally enclosed in braces." |
| 66 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 67 | ElemTy.getUnqualifiedType())) |
| 68 | return Init; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 73 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 74 | } |
| 75 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 76 | static Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) { |
| 77 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
| 78 | if (!arrayType) return 0; |
| 79 | |
| 80 | return IsStringInit(init, arrayType, Context); |
| 81 | } |
| 82 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 83 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 84 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 85 | // Get the length of the string as parsed. |
| 86 | uint64_t StrLength = |
| 87 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 88 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 90 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | // 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] | 92 | // being initialized to a string literal. |
| 93 | llvm::APSInt ConstVal(32); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 94 | ConstVal = StrLength; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 95 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 96 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 97 | ConstVal, |
| 98 | ArrayType::Normal, 0); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 99 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 102 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 104 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 105 | // the size may be smaller or larger than the string we are initializing. |
| 106 | // FIXME: Avoid truncation for 64-bit length strings. |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 107 | if (S.getLangOptions().CPlusPlus) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 108 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) { |
| 109 | // For Pascal strings it's OK to strip off the terminating null character, |
| 110 | // so the example below is valid: |
| 111 | // |
| 112 | // unsigned char a[2] = "\pa"; |
| 113 | if (SL->isPascal()) |
| 114 | StrLength--; |
| 115 | } |
| 116 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 117 | // [dcl.init.string]p2 |
| 118 | if (StrLength > CAT->getSize().getZExtValue()) |
| 119 | S.Diag(Str->getSourceRange().getBegin(), |
| 120 | diag::err_initializer_string_for_char_array_too_long) |
| 121 | << Str->getSourceRange(); |
| 122 | } else { |
| 123 | // C99 6.7.8p14. |
| 124 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 125 | S.Diag(Str->getSourceRange().getBegin(), |
| 126 | diag::warn_initializer_string_for_char_array_too_long) |
| 127 | << Str->getSourceRange(); |
| 128 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 130 | // Set the type to the actual size that we are initializing. If we have |
| 131 | // something like: |
| 132 | // char x[1] = "foo"; |
| 133 | // then this will set the string literal's type to char[1]. |
| 134 | Str->setType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 137 | //===----------------------------------------------------------------------===// |
| 138 | // Semantic checking for initializer lists. |
| 139 | //===----------------------------------------------------------------------===// |
| 140 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 141 | /// @brief Semantic checking for initializer lists. |
| 142 | /// |
| 143 | /// The InitListChecker class contains a set of routines that each |
| 144 | /// handle the initialization of a certain kind of entity, e.g., |
| 145 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 146 | /// InitListChecker itself performs a recursive walk of the subobject |
| 147 | /// structure of the type to be initialized, while stepping through |
| 148 | /// the initializer list one element at a time. The IList and Index |
| 149 | /// parameters to each of the Check* routines contain the active |
| 150 | /// (syntactic) initializer list and the index into that initializer |
| 151 | /// list that represents the current initializer. Each routine is |
| 152 | /// responsible for moving that Index forward as it consumes elements. |
| 153 | /// |
| 154 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 155 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 156 | /// initializer list and the index into that initializer list where we |
| 157 | /// are copying initializers as we map them over to the semantic |
| 158 | /// list. Once we have completed our recursive walk of the subobject |
| 159 | /// structure, we will have constructed a full semantic initializer |
| 160 | /// list. |
| 161 | /// |
| 162 | /// C99 designators cause changes in the initializer list traversal, |
| 163 | /// because they make the initialization "jump" into a specific |
| 164 | /// subobject and then continue the initialization from that |
| 165 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 166 | /// designated subobject and manages backing out the recursion to |
| 167 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 168 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 169 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 170 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 171 | bool hadError; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 172 | bool VerifyOnly; // no diagnostics, no structure building |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 173 | bool AllowBraceElision; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 174 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 175 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 177 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 178 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 179 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 180 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 181 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 182 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 183 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 184 | unsigned &StructuredIndex, |
| 185 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 186 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 187 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 189 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 190 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 191 | unsigned &StructuredIndex, |
| 192 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 193 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 194 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 195 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 196 | InitListExpr *StructuredList, |
| 197 | unsigned &StructuredIndex); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 198 | void CheckComplexType(const InitializedEntity &Entity, |
| 199 | InitListExpr *IList, QualType DeclType, |
| 200 | unsigned &Index, |
| 201 | InitListExpr *StructuredList, |
| 202 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 203 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 204 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 205 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 206 | InitListExpr *StructuredList, |
| 207 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 208 | void CheckReferenceType(const InitializedEntity &Entity, |
| 209 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 210 | unsigned &Index, |
| 211 | InitListExpr *StructuredList, |
| 212 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 213 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 214 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 215 | InitListExpr *StructuredList, |
| 216 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 217 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 218 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 220 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 221 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 222 | unsigned &StructuredIndex, |
| 223 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 224 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 225 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 227 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 228 | InitListExpr *StructuredList, |
| 229 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 230 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 231 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 232 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 234 | RecordDecl::field_iterator *NextField, |
| 235 | llvm::APSInt *NextElementIndex, |
| 236 | unsigned &Index, |
| 237 | InitListExpr *StructuredList, |
| 238 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 239 | bool FinishSubobjectInit, |
| 240 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 241 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 242 | QualType CurrentObjectType, |
| 243 | InitListExpr *StructuredList, |
| 244 | unsigned StructuredIndex, |
| 245 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 246 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 247 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 248 | Expr *expr); |
| 249 | int numArrayElements(QualType DeclType); |
| 250 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 251 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 252 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 253 | const InitializedEntity &ParentEntity, |
| 254 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 255 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 256 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 257 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 258 | Expr *InitExpr, FieldDecl *Field, |
| 259 | bool TopLevelObject); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 260 | void CheckValueInitializable(const InitializedEntity &Entity); |
| 261 | |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 262 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 263 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 264 | InitListExpr *IL, QualType &T, bool VerifyOnly, |
| 265 | bool AllowBraceElision); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 266 | bool HadError() { return hadError; } |
| 267 | |
| 268 | // @brief Retrieves the fully-structured initializer list used for |
| 269 | // semantic analysis and code generation. |
| 270 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 271 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 272 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 273 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 274 | void InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) { |
| 275 | assert(VerifyOnly && |
| 276 | "CheckValueInitializable is only inteded for verification mode."); |
| 277 | |
| 278 | SourceLocation Loc; |
| 279 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 280 | true); |
| 281 | InitializationSequence InitSeq(SemaRef, Entity, Kind, 0, 0); |
| 282 | if (InitSeq.Failed()) |
| 283 | hadError = true; |
| 284 | } |
| 285 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 286 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 287 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 288 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 289 | bool &RequiresSecondPass) { |
| 290 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 291 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 292 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 293 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 294 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 295 | // FIXME: We probably don't need to handle references |
| 296 | // specially here, since value-initialization of references is |
| 297 | // handled in InitializationSequence. |
| 298 | if (Field->getType()->isReferenceType()) { |
| 299 | // C++ [dcl.init.aggr]p9: |
| 300 | // If an incomplete or empty initializer-list leaves a |
| 301 | // member of reference type uninitialized, the program is |
| 302 | // ill-formed. |
| 303 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 304 | << Field->getType() |
| 305 | << ILE->getSyntacticForm()->getSourceRange(); |
| 306 | SemaRef.Diag(Field->getLocation(), |
| 307 | diag::note_uninit_reference_member); |
| 308 | hadError = true; |
| 309 | return; |
| 310 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 311 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 312 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 313 | true); |
| 314 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 315 | if (!InitSeq) { |
| 316 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 317 | hadError = true; |
| 318 | return; |
| 319 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 320 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 321 | ExprResult MemberInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 322 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 323 | if (MemberInit.isInvalid()) { |
| 324 | hadError = true; |
| 325 | return; |
| 326 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 327 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 328 | if (hadError) { |
| 329 | // Do nothing |
| 330 | } else if (Init < NumInits) { |
| 331 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 332 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 333 | // Value-initialization requires a constructor call, so |
| 334 | // extend the initializer list to include the constructor |
| 335 | // call and make a note that we'll need to take another pass |
| 336 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 337 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 338 | RequiresSecondPass = true; |
| 339 | } |
| 340 | } else if (InitListExpr *InnerILE |
| 341 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 342 | FillInValueInitializations(MemberEntity, InnerILE, |
| 343 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 346 | /// Recursively replaces NULL values within the given initializer list |
| 347 | /// with expressions that perform value-initialization of the |
| 348 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 349 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 350 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 351 | InitListExpr *ILE, |
| 352 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 354 | "Should not have void type"); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 355 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 356 | if (ILE->getSyntacticForm()) |
| 357 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 359 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 360 | if (RType->getDecl()->isUnion() && |
| 361 | ILE->getInitializedFieldInUnion()) |
| 362 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 363 | Entity, ILE, RequiresSecondPass); |
| 364 | else { |
| 365 | unsigned Init = 0; |
| 366 | for (RecordDecl::field_iterator |
| 367 | Field = RType->getDecl()->field_begin(), |
| 368 | FieldEnd = RType->getDecl()->field_end(); |
| 369 | Field != FieldEnd; ++Field) { |
| 370 | if (Field->isUnnamedBitfield()) |
| 371 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 372 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 373 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 374 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 375 | |
| 376 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 377 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 378 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 379 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 380 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 381 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 382 | // Only look at the first initialization of a union. |
| 383 | if (RType->getDecl()->isUnion()) |
| 384 | break; |
| 385 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 390 | |
| 391 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 393 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 394 | unsigned NumInits = ILE->getNumInits(); |
| 395 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 396 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 397 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 398 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 399 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 400 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 401 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 402 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 403 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 404 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 405 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 406 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 408 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 410 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 411 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 412 | if (hadError) |
| 413 | return; |
| 414 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 415 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 416 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 417 | ElementEntity.setElementIndex(Init); |
| 418 | |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame^] | 419 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : 0); |
| 420 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 421 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 422 | true); |
| 423 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 424 | if (!InitSeq) { |
| 425 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 426 | hadError = true; |
| 427 | return; |
| 428 | } |
| 429 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 430 | ExprResult ElementInit |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 431 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg()); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 432 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 433 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
| 437 | if (hadError) { |
| 438 | // Do nothing |
| 439 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 440 | // For arrays, just set the expression used for value-initialization |
| 441 | // of the "holes" in the array. |
| 442 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 443 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 444 | else |
| 445 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 446 | } else { |
| 447 | // For arrays, just set the expression used for value-initialization |
| 448 | // of the rest of elements and exit. |
| 449 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 450 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 451 | return; |
| 452 | } |
| 453 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 454 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 455 | // Value-initialization requires a constructor call, so |
| 456 | // extend the initializer list to include the constructor |
| 457 | // call and make a note that we'll need to take another pass |
| 458 | // through the initializer list. |
| 459 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 460 | RequiresSecondPass = true; |
| 461 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 462 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 463 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame^] | 464 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 465 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 470 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 471 | InitListExpr *IL, QualType &T, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 472 | bool VerifyOnly, bool AllowBraceElision) |
| 473 | : SemaRef(S), VerifyOnly(VerifyOnly), AllowBraceElision(AllowBraceElision) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 474 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 475 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 476 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 477 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 479 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 480 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 481 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 482 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 483 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 484 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 485 | bool RequiresSecondPass = false; |
| 486 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 487 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 488 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 489 | RequiresSecondPass); |
| 490 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 494 | // FIXME: use a proper constant |
| 495 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 496 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 497 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 498 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 499 | } |
| 500 | return maxElements; |
| 501 | } |
| 502 | |
| 503 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 504 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 505 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 506 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 507 | Field = structDecl->field_begin(), |
| 508 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 509 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 510 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 511 | ++InitializableMembers; |
| 512 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 513 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 514 | return std::min(InitializableMembers, 1); |
| 515 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 518 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 519 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 520 | QualType T, unsigned &Index, |
| 521 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 522 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 523 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 524 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 525 | if (T->isArrayType()) |
| 526 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 527 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 528 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 529 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 530 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 531 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 532 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 533 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 534 | if (maxElements == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 535 | if (!VerifyOnly) |
| 536 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 537 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 538 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 539 | hadError = true; |
| 540 | return; |
| 541 | } |
| 542 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 543 | // Build a structured initializer list corresponding to this subobject. |
| 544 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 546 | StructuredIndex, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 547 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 548 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 549 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 550 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 551 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 552 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 553 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 554 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 556 | StructuredSubobjectInitIndex); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 557 | |
| 558 | if (VerifyOnly) { |
| 559 | if (!AllowBraceElision && (T->isArrayType() || T->isRecordType())) |
| 560 | hadError = true; |
| 561 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 562 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 563 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 564 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 565 | // Update the structured sub-object initializer so that it's ending |
| 566 | // range corresponds with the end of the last initializer it used. |
| 567 | if (EndIndex < ParentIList->getNumInits()) { |
| 568 | SourceLocation EndLoc |
| 569 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 570 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 571 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 572 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 573 | // Complain about missing braces. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 574 | if (T->isArrayType() || T->isRecordType()) { |
| 575 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 576 | AllowBraceElision ? diag::warn_missing_braces : |
| 577 | diag::err_missing_braces) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 578 | << StructuredSubobjectInitList->getSourceRange() |
| 579 | << FixItHint::CreateInsertion( |
| 580 | StructuredSubobjectInitList->getLocStart(), "{") |
| 581 | << FixItHint::CreateInsertion( |
| 582 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 583 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 584 | "}"); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 585 | if (!AllowBraceElision) |
| 586 | hadError = true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 587 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 588 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 591 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 592 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 593 | unsigned &Index, |
| 594 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 595 | unsigned &StructuredIndex, |
| 596 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 597 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 598 | if (!VerifyOnly) { |
| 599 | SyntacticToSemantic[IList] = StructuredList; |
| 600 | StructuredList->setSyntacticForm(IList); |
| 601 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 602 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 603 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 604 | if (!VerifyOnly) { |
| 605 | QualType ExprTy = T.getNonLValueExprType(SemaRef.Context); |
| 606 | IList->setType(ExprTy); |
| 607 | StructuredList->setType(ExprTy); |
| 608 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 609 | if (hadError) |
| 610 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 611 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 612 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 613 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 614 | if (VerifyOnly) { |
| 615 | if (SemaRef.getLangOptions().CPlusPlus || |
| 616 | (SemaRef.getLangOptions().OpenCL && |
| 617 | IList->getType()->isVectorType())) { |
| 618 | hadError = true; |
| 619 | } |
| 620 | return; |
| 621 | } |
| 622 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 623 | if (StructuredIndex == 1 && |
| 624 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 625 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 626 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 627 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 628 | hadError = true; |
| 629 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 630 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 631 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 632 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 633 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 634 | // Don't complain for incomplete types, since we'll get an error |
| 635 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 636 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 638 | CurrentObjectType->isArrayType()? 0 : |
| 639 | CurrentObjectType->isVectorType()? 1 : |
| 640 | CurrentObjectType->isScalarType()? 2 : |
| 641 | CurrentObjectType->isUnionType()? 3 : |
| 642 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 643 | |
| 644 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 645 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 646 | DK = diag::err_excess_initializers; |
| 647 | hadError = true; |
| 648 | } |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 649 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 650 | DK = diag::err_excess_initializers; |
| 651 | hadError = true; |
| 652 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 653 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 654 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 655 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 658 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 659 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 660 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 661 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 662 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 663 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 664 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 667 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 668 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 669 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 670 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 671 | unsigned &Index, |
| 672 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 673 | unsigned &StructuredIndex, |
| 674 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 675 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 676 | // Explicitly braced initializer for complex type can be real+imaginary |
| 677 | // parts. |
| 678 | CheckComplexType(Entity, IList, DeclType, Index, |
| 679 | StructuredList, StructuredIndex); |
| 680 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 681 | CheckScalarType(Entity, IList, DeclType, Index, |
| 682 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 683 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 684 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 685 | StructuredList, StructuredIndex); |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 686 | } else if (DeclType->isAggregateType()) { |
| 687 | if (DeclType->isRecordType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 688 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 689 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 690 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 691 | StructuredList, StructuredIndex, |
| 692 | TopLevelObject); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 693 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 694 | llvm::APSInt Zero( |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 695 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 696 | false); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 697 | CheckArrayType(Entity, IList, DeclType, Zero, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 698 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 699 | StructuredList, StructuredIndex); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 700 | } else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 701 | llvm_unreachable("Aggregate that isn't a structure or array?!"); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 702 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 703 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 704 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 705 | if (!VerifyOnly) |
| 706 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 707 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 708 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 709 | } else if (DeclType->isRecordType()) { |
| 710 | // C++ [dcl.init]p14: |
| 711 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 712 | // is a brace-enclosed list, see 8.5.1. |
| 713 | // |
| 714 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 715 | // we have an initializer list and a destination type that is not |
| 716 | // an aggregate. |
| 717 | // FIXME: In C++0x, this is yet another form of initialization. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 718 | if (!VerifyOnly) |
| 719 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 720 | << DeclType << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 721 | hadError = true; |
| 722 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 723 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 724 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 725 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 726 | if (!VerifyOnly) |
| 727 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 728 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 729 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 730 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 731 | if (!VerifyOnly) |
| 732 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 733 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 734 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 738 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 739 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 741 | unsigned &Index, |
| 742 | InitListExpr *StructuredList, |
| 743 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 744 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 745 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 746 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 747 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 748 | InitListExpr *newStructuredList |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 749 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 750 | StructuredList, StructuredIndex, |
| 751 | SubInitList->getSourceRange()); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 752 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 753 | newStructuredList, newStructuredIndex); |
| 754 | ++StructuredIndex; |
| 755 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 756 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 757 | } else if (ElemType->isScalarType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 758 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 759 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 760 | } else if (ElemType->isReferenceType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 761 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 762 | StructuredList, StructuredIndex); |
| 763 | } |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 764 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 765 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 766 | // arrayType can be incomplete if we're initializing a flexible |
| 767 | // array member. There's nothing we can do with the completed |
| 768 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 769 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 770 | if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 771 | if (!VerifyOnly) { |
| 772 | CheckStringInit(Str, ElemType, arrayType, SemaRef); |
| 773 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 774 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 775 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 776 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 777 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 778 | |
| 779 | // Fall through for subaggregate initialization. |
| 780 | |
| 781 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 782 | // C++ [dcl.init.aggr]p12: |
| 783 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 784 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 785 | // an initializer-list. If the initializer can initialize a |
| 786 | // member, the member is initialized. [...] |
| 787 | |
| 788 | // FIXME: Better EqualLoc? |
| 789 | InitializationKind Kind = |
| 790 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 791 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 792 | |
| 793 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 794 | if (!VerifyOnly) { |
| 795 | ExprResult Result = |
| 796 | Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1)); |
| 797 | if (Result.isInvalid()) |
| 798 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 799 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 800 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 801 | Result.takeAs<Expr>()); |
| 802 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 803 | ++Index; |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | // Fall through for subaggregate initialization |
| 808 | } else { |
| 809 | // C99 6.7.8p13: |
| 810 | // |
| 811 | // The initializer for a structure or union object that has |
| 812 | // automatic storage duration shall be either an initializer |
| 813 | // list as described below, or a single expression that has |
| 814 | // compatible structure or union type. In the latter case, the |
| 815 | // initial value of the object, including unnamed members, is |
| 816 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 817 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 818 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 819 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 820 | !VerifyOnly) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 821 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 822 | if (ExprRes.isInvalid()) |
| 823 | hadError = true; |
| 824 | else { |
| 825 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
| 826 | if (ExprRes.isInvalid()) |
| 827 | hadError = true; |
| 828 | } |
| 829 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 830 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 831 | ++Index; |
| 832 | return; |
| 833 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 834 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 835 | // Fall through for subaggregate initialization |
| 836 | } |
| 837 | |
| 838 | // C++ [dcl.init.aggr]p12: |
| 839 | // |
| 840 | // [...] Otherwise, if the member is itself a non-empty |
| 841 | // subaggregate, brace elision is assumed and the initializer is |
| 842 | // considered for the initialization of the first member of |
| 843 | // the subaggregate. |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 844 | if (!SemaRef.getLangOptions().OpenCL && |
| 845 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 846 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 847 | StructuredIndex); |
| 848 | ++StructuredIndex; |
| 849 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 850 | if (!VerifyOnly) { |
| 851 | // We cannot initialize this element, so let |
| 852 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 853 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 854 | SemaRef.Owned(expr), |
| 855 | /*TopLevelOfInitList=*/true); |
| 856 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 857 | hadError = true; |
| 858 | ++Index; |
| 859 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 860 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 863 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 864 | InitListExpr *IList, QualType DeclType, |
| 865 | unsigned &Index, |
| 866 | InitListExpr *StructuredList, |
| 867 | unsigned &StructuredIndex) { |
| 868 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 869 | |
| 870 | // As an extension, clang supports complex initializers, which initialize |
| 871 | // a complex number component-wise. When an explicit initializer list for |
| 872 | // a complex number contains two two initializers, this extension kicks in: |
| 873 | // it exepcts the initializer list to contain two elements convertible to |
| 874 | // the element type of the complex type. The first element initializes |
| 875 | // the real part, and the second element intitializes the imaginary part. |
| 876 | |
| 877 | if (IList->getNumInits() != 2) |
| 878 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 879 | StructuredIndex); |
| 880 | |
| 881 | // This is an extension in C. (The builtin _Complex type does not exist |
| 882 | // in the C++ standard.) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 883 | if (!SemaRef.getLangOptions().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 884 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 885 | << IList->getSourceRange(); |
| 886 | |
| 887 | // Initialize the complex number. |
| 888 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 889 | InitializedEntity ElementEntity = |
| 890 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 891 | |
| 892 | for (unsigned i = 0; i < 2; ++i) { |
| 893 | ElementEntity.setElementIndex(Index); |
| 894 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 895 | StructuredList, StructuredIndex); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 900 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 901 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 902 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 903 | InitListExpr *StructuredList, |
| 904 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 905 | if (Index >= IList->getNumInits()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 906 | if (!VerifyOnly) |
| 907 | SemaRef.Diag(IList->getLocStart(), |
| 908 | SemaRef.getLangOptions().CPlusPlus0x ? |
| 909 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 910 | diag::err_empty_scalar_initializer) |
| 911 | << IList->getSourceRange(); |
| 912 | hadError = !SemaRef.getLangOptions().CPlusPlus0x; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 913 | ++Index; |
| 914 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 915 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 916 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 917 | |
| 918 | Expr *expr = IList->getInit(Index); |
| 919 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 920 | if (!VerifyOnly) |
| 921 | SemaRef.Diag(SubIList->getLocStart(), |
| 922 | diag::warn_many_braces_around_scalar_init) |
| 923 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 924 | |
| 925 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 926 | StructuredIndex); |
| 927 | return; |
| 928 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 929 | if (!VerifyOnly) |
| 930 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
| 931 | diag::err_designator_for_scalar_init) |
| 932 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 933 | hadError = true; |
| 934 | ++Index; |
| 935 | ++StructuredIndex; |
| 936 | return; |
| 937 | } |
| 938 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 939 | if (VerifyOnly) { |
| 940 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 941 | hadError = true; |
| 942 | ++Index; |
| 943 | return; |
| 944 | } |
| 945 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 946 | ExprResult Result = |
| 947 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 948 | SemaRef.Owned(expr), |
| 949 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 950 | |
| 951 | Expr *ResultExpr = 0; |
| 952 | |
| 953 | if (Result.isInvalid()) |
| 954 | hadError = true; // types weren't compatible. |
| 955 | else { |
| 956 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 957 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 958 | if (ResultExpr != expr) { |
| 959 | // The type was promoted, update initializer list. |
| 960 | IList->setInit(Index, ResultExpr); |
| 961 | } |
| 962 | } |
| 963 | if (hadError) |
| 964 | ++StructuredIndex; |
| 965 | else |
| 966 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 967 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 970 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 971 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 972 | unsigned &Index, |
| 973 | InitListExpr *StructuredList, |
| 974 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 975 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 976 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 977 | // general, it would be useful to pass location information down the stack, |
| 978 | // so that we know the location (or decl) of the "current object" being |
| 979 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 980 | if (!VerifyOnly) |
| 981 | SemaRef.Diag(IList->getLocStart(), |
| 982 | diag::err_init_reference_member_uninitialized) |
| 983 | << DeclType |
| 984 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 985 | hadError = true; |
| 986 | ++Index; |
| 987 | ++StructuredIndex; |
| 988 | return; |
| 989 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 990 | |
| 991 | Expr *expr = IList->getInit(Index); |
| 992 | if (isa<InitListExpr>(expr)) { |
| 993 | // FIXME: Allowed in C++11. |
| 994 | if (!VerifyOnly) |
| 995 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 996 | << DeclType << IList->getSourceRange(); |
| 997 | hadError = true; |
| 998 | ++Index; |
| 999 | ++StructuredIndex; |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | if (VerifyOnly) { |
| 1004 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1005 | hadError = true; |
| 1006 | ++Index; |
| 1007 | return; |
| 1008 | } |
| 1009 | |
| 1010 | ExprResult Result = |
| 1011 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1012 | SemaRef.Owned(expr), |
| 1013 | /*TopLevelOfInitList=*/true); |
| 1014 | |
| 1015 | if (Result.isInvalid()) |
| 1016 | hadError = true; |
| 1017 | |
| 1018 | expr = Result.takeAs<Expr>(); |
| 1019 | IList->setInit(Index, expr); |
| 1020 | |
| 1021 | if (hadError) |
| 1022 | ++StructuredIndex; |
| 1023 | else |
| 1024 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1025 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1028 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1029 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1030 | unsigned &Index, |
| 1031 | InitListExpr *StructuredList, |
| 1032 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1033 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1034 | unsigned maxElements = VT->getNumElements(); |
| 1035 | unsigned numEltsInit = 0; |
| 1036 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1037 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1038 | if (Index >= IList->getNumInits()) { |
| 1039 | // Make sure the element type can be value-initialized. |
| 1040 | if (VerifyOnly) |
| 1041 | CheckValueInitializable( |
| 1042 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1043 | return; |
| 1044 | } |
| 1045 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1046 | if (!SemaRef.getLangOptions().OpenCL) { |
| 1047 | // If the initializing element is a vector, try to copy-initialize |
| 1048 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1049 | Expr *Init = IList->getInit(Index); |
| 1050 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1051 | if (VerifyOnly) { |
| 1052 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1053 | hadError = true; |
| 1054 | ++Index; |
| 1055 | return; |
| 1056 | } |
| 1057 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1058 | ExprResult Result = |
| 1059 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1060 | SemaRef.Owned(Init), |
| 1061 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1062 | |
| 1063 | Expr *ResultExpr = 0; |
| 1064 | if (Result.isInvalid()) |
| 1065 | hadError = true; // types weren't compatible. |
| 1066 | else { |
| 1067 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1068 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1069 | if (ResultExpr != Init) { |
| 1070 | // The type was promoted, update initializer list. |
| 1071 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1072 | } |
| 1073 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1074 | if (hadError) |
| 1075 | ++StructuredIndex; |
| 1076 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1077 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1078 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1079 | ++Index; |
| 1080 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1081 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1082 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1083 | InitializedEntity ElementEntity = |
| 1084 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1085 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1086 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1087 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1088 | if (Index >= IList->getNumInits()) { |
| 1089 | if (VerifyOnly) |
| 1090 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1091 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1092 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1093 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1094 | ElementEntity.setElementIndex(Index); |
| 1095 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1096 | StructuredList, StructuredIndex); |
| 1097 | } |
| 1098 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1099 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1100 | |
| 1101 | InitializedEntity ElementEntity = |
| 1102 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1103 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1104 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1105 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1106 | // Don't attempt to go past the end of the init list |
| 1107 | if (Index >= IList->getNumInits()) |
| 1108 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1109 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1110 | ElementEntity.setElementIndex(Index); |
| 1111 | |
| 1112 | QualType IType = IList->getInit(Index)->getType(); |
| 1113 | if (!IType->isVectorType()) { |
| 1114 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1115 | StructuredList, StructuredIndex); |
| 1116 | ++numEltsInit; |
| 1117 | } else { |
| 1118 | QualType VecType; |
| 1119 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1120 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1121 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1122 | if (IType->isExtVectorType()) |
| 1123 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1124 | else |
| 1125 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1126 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1127 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1128 | StructuredList, StructuredIndex); |
| 1129 | numEltsInit += numIElts; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1134 | if (numEltsInit != maxElements) { |
| 1135 | if (!VerifyOnly) |
| 1136 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 1137 | diag::err_vector_incorrect_num_initializers) |
| 1138 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1139 | hadError = true; |
| 1140 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1143 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1144 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1145 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1146 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1147 | unsigned &Index, |
| 1148 | InitListExpr *StructuredList, |
| 1149 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1150 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1151 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1152 | // Check for the special-case of initializing an array with a string. |
| 1153 | if (Index < IList->getNumInits()) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1154 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1155 | SemaRef.Context)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1156 | // We place the string literal directly into the resulting |
| 1157 | // initializer list. This is the only place where the structure |
| 1158 | // of the structured initializer list doesn't match exactly, |
| 1159 | // because doing so would involve allocating one character |
| 1160 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1161 | if (!VerifyOnly) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1162 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1163 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 1164 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1165 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1166 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1167 | return; |
| 1168 | } |
| 1169 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1170 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1171 | // Check for VLAs; in standard C it would be possible to check this |
| 1172 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1173 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1174 | if (!VerifyOnly) |
| 1175 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1176 | diag::err_variable_object_no_init) |
| 1177 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1178 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1179 | ++Index; |
| 1180 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1181 | return; |
| 1182 | } |
| 1183 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1184 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1185 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1186 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1187 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1188 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1189 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1190 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1191 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1192 | maxElementsKnown = true; |
| 1193 | } |
| 1194 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1195 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1196 | while (Index < IList->getNumInits()) { |
| 1197 | Expr *Init = IList->getInit(Index); |
| 1198 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1199 | // If we're not the subobject that matches up with the '{' for |
| 1200 | // the designator, we shouldn't be handling the |
| 1201 | // designator. Return immediately. |
| 1202 | if (!SubobjectIsDesignatorContext) |
| 1203 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1204 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1205 | // Handle this designated initializer. elementIndex will be |
| 1206 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1207 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1208 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1209 | StructuredList, StructuredIndex, true, |
| 1210 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1211 | hadError = true; |
| 1212 | continue; |
| 1213 | } |
| 1214 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1215 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1216 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1217 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1218 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1219 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1220 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1221 | // If the array is of incomplete type, keep track of the number of |
| 1222 | // elements in the initializer. |
| 1223 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1224 | maxElements = elementIndex; |
| 1225 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1226 | continue; |
| 1227 | } |
| 1228 | |
| 1229 | // If we know the maximum number of elements, and we've already |
| 1230 | // hit it, stop consuming elements in the initializer list. |
| 1231 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1232 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1233 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1234 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1235 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1236 | Entity); |
| 1237 | // Check this element. |
| 1238 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1239 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1240 | ++elementIndex; |
| 1241 | |
| 1242 | // If the array is of incomplete type, keep track of the number of |
| 1243 | // elements in the initializer. |
| 1244 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1245 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1246 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1247 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1248 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1249 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1250 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1251 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1252 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1253 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1254 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1255 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1256 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1257 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1259 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1260 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1261 | if (!hadError && VerifyOnly) { |
| 1262 | // Check if there are any members of the array that get value-initialized. |
| 1263 | // If so, check if doing that is possible. |
| 1264 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1265 | if (maxElementsKnown && elementIndex < maxElements) |
| 1266 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1267 | SemaRef.Context, 0, Entity)); |
| 1268 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1271 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1272 | Expr *InitExpr, |
| 1273 | FieldDecl *Field, |
| 1274 | bool TopLevelObject) { |
| 1275 | // Handle GNU flexible array initializers. |
| 1276 | unsigned FlexArrayDiag; |
| 1277 | if (isa<InitListExpr>(InitExpr) && |
| 1278 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1279 | // Empty flexible array init always allowed as an extension |
| 1280 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1281 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 1282 | // Disallow flexible array init in C++; it is not required for gcc |
| 1283 | // compatibility, and it needs work to IRGen correctly in general. |
| 1284 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1285 | } else if (!TopLevelObject) { |
| 1286 | // Disallow flexible array init on non-top-level object |
| 1287 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1288 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1289 | // Disallow flexible array init on anything which is not a variable. |
| 1290 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1291 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1292 | // Disallow flexible array init on local variables. |
| 1293 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1294 | } else { |
| 1295 | // Allow other cases. |
| 1296 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1297 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1298 | |
| 1299 | if (!VerifyOnly) { |
| 1300 | SemaRef.Diag(InitExpr->getSourceRange().getBegin(), |
| 1301 | FlexArrayDiag) |
| 1302 | << InitExpr->getSourceRange().getBegin(); |
| 1303 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1304 | << Field; |
| 1305 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1306 | |
| 1307 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1308 | } |
| 1309 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1310 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1311 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1313 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1315 | unsigned &Index, |
| 1316 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1317 | unsigned &StructuredIndex, |
| 1318 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1319 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1321 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1322 | // confusion, we forgo checking the intializer for the entire record. |
| 1323 | if (structDecl->isInvalidDecl()) { |
| 1324 | hadError = true; |
| 1325 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1326 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1327 | |
| 1328 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1329 | // Value-initialize the first named member of the union. |
| 1330 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 1331 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1332 | Field != FieldEnd; ++Field) { |
| 1333 | if (Field->getDeclName()) { |
| 1334 | if (VerifyOnly) |
| 1335 | CheckValueInitializable( |
| 1336 | InitializedEntity::InitializeMember(*Field, &Entity)); |
| 1337 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1338 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1339 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | return; |
| 1343 | } |
| 1344 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1345 | // If structDecl is a forward declaration, this loop won't do |
| 1346 | // anything except look at designated initializers; That's okay, |
| 1347 | // because an error should get printed out elsewhere. It might be |
| 1348 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1349 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1350 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1351 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1352 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1353 | while (Index < IList->getNumInits()) { |
| 1354 | Expr *Init = IList->getInit(Index); |
| 1355 | |
| 1356 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1357 | // If we're not the subobject that matches up with the '{' for |
| 1358 | // the designator, we shouldn't be handling the |
| 1359 | // designator. Return immediately. |
| 1360 | if (!SubobjectIsDesignatorContext) |
| 1361 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1362 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1363 | // Handle this designated initializer. Field will be updated to |
| 1364 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1365 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1366 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1367 | StructuredList, StructuredIndex, |
| 1368 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1369 | hadError = true; |
| 1370 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1371 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1372 | |
| 1373 | // Disable check for missing fields when designators are used. |
| 1374 | // This matches gcc behaviour. |
| 1375 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1376 | continue; |
| 1377 | } |
| 1378 | |
| 1379 | if (Field == FieldEnd) { |
| 1380 | // We've run out of fields. We're done. |
| 1381 | break; |
| 1382 | } |
| 1383 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1384 | // We've already initialized a member of a union. We're done. |
| 1385 | if (InitializedSomething && DeclType->isUnionType()) |
| 1386 | break; |
| 1387 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1388 | // If we've hit the flexible array member at the end, we're done. |
| 1389 | if (Field->getType()->isIncompleteArrayType()) |
| 1390 | break; |
| 1391 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1392 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1393 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1394 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1395 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1396 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1397 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1398 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1399 | bool InvalidUse; |
| 1400 | if (VerifyOnly) |
| 1401 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
| 1402 | else |
| 1403 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
| 1404 | IList->getInit(Index)->getLocStart()); |
| 1405 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1406 | ++Index; |
| 1407 | ++Field; |
| 1408 | hadError = true; |
| 1409 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1410 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1411 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1412 | InitializedEntity MemberEntity = |
| 1413 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1414 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1415 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1416 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1417 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1418 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1419 | // Initialize the first field within the union. |
| 1420 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1421 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1422 | |
| 1423 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1424 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1425 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1426 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1427 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1428 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1429 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1430 | // It is possible we have one or more unnamed bitfields remaining. |
| 1431 | // Find first (if any) named field and emit warning. |
| 1432 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1433 | it != end; ++it) { |
| 1434 | if (!it->isUnnamedBitfield()) { |
| 1435 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1436 | diag::warn_missing_field_initializers) << it->getName(); |
| 1437 | break; |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1442 | // Check that any remaining fields can be value-initialized. |
| 1443 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1444 | !Field->getType()->isIncompleteArrayType()) { |
| 1445 | // FIXME: Should check for holes left by designated initializers too. |
| 1446 | for (; Field != FieldEnd && !hadError; ++Field) { |
| 1447 | if (!Field->isUnnamedBitfield()) |
| 1448 | CheckValueInitializable( |
| 1449 | InitializedEntity::InitializeMember(*Field, &Entity)); |
| 1450 | } |
| 1451 | } |
| 1452 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1454 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1455 | return; |
| 1456 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1457 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
| 1458 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1459 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1460 | ++Index; |
| 1461 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1464 | InitializedEntity MemberEntity = |
| 1465 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1466 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1467 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1468 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1469 | StructuredList, StructuredIndex); |
| 1470 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1471 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1472 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1473 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1474 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1475 | /// \brief Expand a field designator that refers to a member of an |
| 1476 | /// anonymous struct or union into a series of field designators that |
| 1477 | /// refers to the field within the appropriate subobject. |
| 1478 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1479 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | DesignatedInitExpr *DIE, |
| 1481 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1482 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1483 | typedef DesignatedInitExpr::Designator Designator; |
| 1484 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1485 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1486 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1487 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1488 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1489 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1491 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1492 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1493 | else |
| 1494 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1495 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1496 | assert(isa<FieldDecl>(*PI)); |
| 1497 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | // Expand the current designator into the set of replacement |
| 1501 | // designators, so we have a full subobject path down to where the |
| 1502 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1503 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1504 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1505 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1507 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1508 | /// corresponds to FieldName. |
| 1509 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1510 | IdentifierInfo *FieldName) { |
| 1511 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1512 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1513 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1514 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1515 | return IF; |
| 1516 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1517 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1518 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1521 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1522 | DesignatedInitExpr *DIE) { |
| 1523 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1524 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1525 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1526 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1527 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
| 1528 | DIE->size(), IndexExprs.data(), |
| 1529 | NumIndexExprs, DIE->getEqualOrColonLoc(), |
| 1530 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1531 | } |
| 1532 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1533 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1534 | /// |
| 1535 | /// Determines whether the designated initializer @p DIE, which |
| 1536 | /// resides at the given @p Index within the initializer list @p |
| 1537 | /// IList, is well-formed for a current object of type @p DeclType |
| 1538 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1539 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1540 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1541 | /// |
| 1542 | /// @param IList The initializer list in which this designated |
| 1543 | /// initializer occurs. |
| 1544 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1545 | /// @param DIE The designated initializer expression. |
| 1546 | /// |
| 1547 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1548 | /// |
| 1549 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1550 | /// into which the designation in @p DIE should refer. |
| 1551 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1552 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1553 | /// a field, this will be set to the field declaration corresponding |
| 1554 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1555 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1556 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1557 | /// DIE is an array designator or GNU array-range designator, this |
| 1558 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1559 | /// |
| 1560 | /// @param Index Index into @p IList where the designated initializer |
| 1561 | /// @p DIE occurs. |
| 1562 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1563 | /// @param StructuredList The initializer list expression that |
| 1564 | /// describes all of the subobject initializers in the order they'll |
| 1565 | /// actually be initialized. |
| 1566 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1567 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1569 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1570 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1571 | DesignatedInitExpr *DIE, |
| 1572 | unsigned DesigIdx, |
| 1573 | QualType &CurrentObjectType, |
| 1574 | RecordDecl::field_iterator *NextField, |
| 1575 | llvm::APSInt *NextElementIndex, |
| 1576 | unsigned &Index, |
| 1577 | InitListExpr *StructuredList, |
| 1578 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1579 | bool FinishSubobjectInit, |
| 1580 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1581 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1582 | // Check the actual initialization for the designated object type. |
| 1583 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1584 | |
| 1585 | // Temporarily remove the designator expression from the |
| 1586 | // initializer list that the child calls see, so that we don't try |
| 1587 | // to re-process the designator. |
| 1588 | unsigned OldIndex = Index; |
| 1589 | IList->setInit(OldIndex, DIE->getInit()); |
| 1590 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1591 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1592 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1593 | |
| 1594 | // Restore the designated initializer expression in the syntactic |
| 1595 | // form of the initializer list. |
| 1596 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1597 | DIE->setInit(IList->getInit(OldIndex)); |
| 1598 | IList->setInit(OldIndex, DIE); |
| 1599 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1600 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1603 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1604 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1605 | if (!VerifyOnly) { |
| 1606 | assert((IsFirstDesignator || StructuredList) && |
| 1607 | "Need a non-designated initializer list to start from"); |
| 1608 | |
| 1609 | // Determine the structural initializer list that corresponds to the |
| 1610 | // current subobject. |
| 1611 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
| 1612 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1613 | StructuredList, StructuredIndex, |
| 1614 | SourceRange(D->getStartLocation(), |
| 1615 | DIE->getSourceRange().getEnd())); |
| 1616 | assert(StructuredList && "Expected a structured initializer list"); |
| 1617 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1618 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1619 | if (D->isFieldDesignator()) { |
| 1620 | // C99 6.7.8p7: |
| 1621 | // |
| 1622 | // If a designator has the form |
| 1623 | // |
| 1624 | // . identifier |
| 1625 | // |
| 1626 | // then the current object (defined below) shall have |
| 1627 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1629 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1630 | if (!RT) { |
| 1631 | SourceLocation Loc = D->getDotLoc(); |
| 1632 | if (Loc.isInvalid()) |
| 1633 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1634 | if (!VerifyOnly) |
| 1635 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1636 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1637 | ++Index; |
| 1638 | return true; |
| 1639 | } |
| 1640 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1641 | // Note: we perform a linear search of the fields here, despite |
| 1642 | // the fact that we have a faster lookup method, because we always |
| 1643 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1644 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1645 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1646 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1647 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1648 | Field = RT->getDecl()->field_begin(), |
| 1649 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1650 | for (; Field != FieldEnd; ++Field) { |
| 1651 | if (Field->isUnnamedBitfield()) |
| 1652 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1653 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1654 | // If we find a field representing an anonymous field, look in the |
| 1655 | // IndirectFieldDecl that follow for the designated initializer. |
| 1656 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1657 | if (IndirectFieldDecl *IF = |
| 1658 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1659 | // In verify mode, don't modify the original. |
| 1660 | if (VerifyOnly) |
| 1661 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1662 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1663 | D = DIE->getDesignator(DesigIdx); |
| 1664 | break; |
| 1665 | } |
| 1666 | } |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1667 | if (KnownField && KnownField == *Field) |
| 1668 | break; |
| 1669 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1670 | break; |
| 1671 | |
| 1672 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1675 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1676 | if (VerifyOnly) { |
| 1677 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1678 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1679 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1680 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1681 | // There was no normal field in the struct with the designated |
| 1682 | // name. Perform another lookup for this name, which may find |
| 1683 | // something that we can't designate (e.g., a member function), |
| 1684 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1686 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1687 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1688 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1689 | // Name lookup didn't find anything. Determine whether this |
| 1690 | // was a typo for another field name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1691 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1692 | Sema::LookupMemberName); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1693 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1694 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1695 | Sema::LookupMemberName, /*Scope=*/NULL, /*SS=*/NULL, |
| 1696 | RT->getDecl(), false, Sema::CTC_NoKeywords); |
| 1697 | if ((ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>()) && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1698 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1699 | ->Equals(RT->getDecl())) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1700 | std::string CorrectedStr( |
| 1701 | Corrected.getAsString(SemaRef.getLangOptions())); |
| 1702 | std::string CorrectedQuotedStr( |
| 1703 | Corrected.getQuoted(SemaRef.getLangOptions())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1704 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1705 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1706 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1707 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1708 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1709 | diag::note_previous_decl) << CorrectedQuotedStr; |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1710 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1711 | } else { |
| 1712 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1713 | << FieldName << CurrentObjectType; |
| 1714 | ++Index; |
| 1715 | return true; |
| 1716 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1717 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1718 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1719 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1720 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1721 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1722 | << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1724 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1725 | ++Index; |
| 1726 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1727 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1728 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1729 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1730 | // The replacement field comes from typo correction; find it |
| 1731 | // in the list of fields. |
| 1732 | FieldIndex = 0; |
| 1733 | Field = RT->getDecl()->field_begin(); |
| 1734 | for (; Field != FieldEnd; ++Field) { |
| 1735 | if (Field->isUnnamedBitfield()) |
| 1736 | continue; |
| 1737 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1738 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1739 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1740 | break; |
| 1741 | |
| 1742 | ++FieldIndex; |
| 1743 | } |
| 1744 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1745 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1746 | |
| 1747 | // All of the fields of a union are located at the same place in |
| 1748 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1749 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1750 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1751 | if (!VerifyOnly) |
| 1752 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1753 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1754 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1755 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1756 | bool InvalidUse; |
| 1757 | if (VerifyOnly) |
| 1758 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
| 1759 | else |
| 1760 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
| 1761 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1762 | ++Index; |
| 1763 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1764 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1765 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1766 | if (!VerifyOnly) { |
| 1767 | // Update the designator with the field declaration. |
| 1768 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1770 | // Make sure that our non-designated initializer list has space |
| 1771 | // for a subobject corresponding to this field. |
| 1772 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1773 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1774 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1775 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1776 | // This designator names a flexible array member. |
| 1777 | if (Field->getType()->isIncompleteArrayType()) { |
| 1778 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1779 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1780 | // We can't designate an object within the flexible array |
| 1781 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1782 | if (!VerifyOnly) { |
| 1783 | DesignatedInitExpr::Designator *NextD |
| 1784 | = DIE->getDesignator(DesigIdx + 1); |
| 1785 | SemaRef.Diag(NextD->getStartLocation(), |
| 1786 | diag::err_designator_into_flexible_array_member) |
| 1787 | << SourceRange(NextD->getStartLocation(), |
| 1788 | DIE->getSourceRange().getEnd()); |
| 1789 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1790 | << *Field; |
| 1791 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1792 | Invalid = true; |
| 1793 | } |
| 1794 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1795 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1796 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1797 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1798 | if (!VerifyOnly) { |
| 1799 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
| 1800 | diag::err_flexible_array_init_needs_braces) |
| 1801 | << DIE->getInit()->getSourceRange(); |
| 1802 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1803 | << *Field; |
| 1804 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1805 | Invalid = true; |
| 1806 | } |
| 1807 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1808 | // Check GNU flexible array initializer. |
| 1809 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
| 1810 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1811 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1812 | |
| 1813 | if (Invalid) { |
| 1814 | ++Index; |
| 1815 | return true; |
| 1816 | } |
| 1817 | |
| 1818 | // Initialize the array. |
| 1819 | bool prevHadError = hadError; |
| 1820 | unsigned newStructuredIndex = FieldIndex; |
| 1821 | unsigned OldIndex = Index; |
| 1822 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1823 | |
| 1824 | InitializedEntity MemberEntity = |
| 1825 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1826 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1827 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1828 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1829 | IList->setInit(OldIndex, DIE); |
| 1830 | if (hadError && !prevHadError) { |
| 1831 | ++Field; |
| 1832 | ++FieldIndex; |
| 1833 | if (NextField) |
| 1834 | *NextField = Field; |
| 1835 | StructuredIndex = FieldIndex; |
| 1836 | return true; |
| 1837 | } |
| 1838 | } else { |
| 1839 | // Recurse to check later designated subobjects. |
| 1840 | QualType FieldType = (*Field)->getType(); |
| 1841 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1842 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1843 | InitializedEntity MemberEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1844 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1845 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1846 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1847 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1848 | true, false)) |
| 1849 | return true; |
| 1850 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1851 | |
| 1852 | // Find the position of the next field to be initialized in this |
| 1853 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1854 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1855 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1856 | |
| 1857 | // If this the first designator, our caller will continue checking |
| 1858 | // the rest of this struct/class/union subobject. |
| 1859 | if (IsFirstDesignator) { |
| 1860 | if (NextField) |
| 1861 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1862 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1863 | return false; |
| 1864 | } |
| 1865 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1866 | if (!FinishSubobjectInit) |
| 1867 | return false; |
| 1868 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1869 | // We've already initialized something in the union; we're done. |
| 1870 | if (RT->getDecl()->isUnion()) |
| 1871 | return hadError; |
| 1872 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1873 | // Check the remaining fields within this class/struct/union subobject. |
| 1874 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1875 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1876 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1877 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1878 | return hadError && !prevHadError; |
| 1879 | } |
| 1880 | |
| 1881 | // C99 6.7.8p6: |
| 1882 | // |
| 1883 | // If a designator has the form |
| 1884 | // |
| 1885 | // [ constant-expression ] |
| 1886 | // |
| 1887 | // then the current object (defined below) shall have array |
| 1888 | // type and the expression shall be an integer constant |
| 1889 | // expression. If the array is of unknown size, any |
| 1890 | // nonnegative value is valid. |
| 1891 | // |
| 1892 | // Additionally, cope with the GNU extension that permits |
| 1893 | // designators of the form |
| 1894 | // |
| 1895 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1896 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1897 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1898 | if (!VerifyOnly) |
| 1899 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 1900 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1901 | ++Index; |
| 1902 | return true; |
| 1903 | } |
| 1904 | |
| 1905 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1906 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1907 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1908 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1909 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1910 | DesignatedEndIndex = DesignatedStartIndex; |
| 1911 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1912 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1913 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1914 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1915 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1917 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1918 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1919 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1920 | // Codegen can't handle evaluating array range designators that have side |
| 1921 | // effects, because we replicate the AST value for each initialized element. |
| 1922 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1923 | // elements with something that has a side effect, so codegen can emit an |
| 1924 | // "error unsupported" error instead of miscompiling the app. |
| 1925 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1926 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1927 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1930 | if (isa<ConstantArrayType>(AT)) { |
| 1931 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1932 | DesignatedStartIndex |
| 1933 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1934 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1935 | DesignatedEndIndex |
| 1936 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1937 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1938 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 1939 | if (!VerifyOnly) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1940 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
| 1941 | diag::err_array_designator_too_large) |
| 1942 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 1943 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1944 | ++Index; |
| 1945 | return true; |
| 1946 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1947 | } else { |
| 1948 | // Make sure the bit-widths and signedness match. |
| 1949 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1950 | DesignatedEndIndex |
| 1951 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1952 | else if (DesignatedStartIndex.getBitWidth() < |
| 1953 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1954 | DesignatedStartIndex |
| 1955 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1956 | DesignatedStartIndex.setIsUnsigned(true); |
| 1957 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1958 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1959 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1960 | // Make sure that our non-designated initializer list has space |
| 1961 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1962 | if (!VerifyOnly && |
| 1963 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1964 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1965 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1966 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1967 | // Repeatedly perform subobject initializations in the range |
| 1968 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1969 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1970 | // Move to the next designator |
| 1971 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1972 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1973 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1974 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1975 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1976 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1977 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1978 | // Recurse to check later designated subobjects. |
| 1979 | QualType ElementType = AT->getElementType(); |
| 1980 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1981 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1982 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1983 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1984 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1985 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1986 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1987 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1988 | return true; |
| 1989 | |
| 1990 | // Move to the next index in the array that we'll be initializing. |
| 1991 | ++DesignatedStartIndex; |
| 1992 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1993 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1994 | |
| 1995 | // If this the first designator, our caller will continue checking |
| 1996 | // the rest of this array subobject. |
| 1997 | if (IsFirstDesignator) { |
| 1998 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1999 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2000 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2001 | return false; |
| 2002 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2004 | if (!FinishSubobjectInit) |
| 2005 | return false; |
| 2006 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2007 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2008 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2009 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2010 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2011 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2015 | // Get the structured initializer list for a subobject of type |
| 2016 | // @p CurrentObjectType. |
| 2017 | InitListExpr * |
| 2018 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2019 | QualType CurrentObjectType, |
| 2020 | InitListExpr *StructuredList, |
| 2021 | unsigned StructuredIndex, |
| 2022 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2023 | if (VerifyOnly) |
| 2024 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2025 | Expr *ExistingInit = 0; |
| 2026 | if (!StructuredList) |
| 2027 | ExistingInit = SyntacticToSemantic[IList]; |
| 2028 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2029 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2031 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2032 | return Result; |
| 2033 | |
| 2034 | if (ExistingInit) { |
| 2035 | // We are creating an initializer list that initializes the |
| 2036 | // subobjects of the current object, but there was already an |
| 2037 | // initialization that completely initialized the current |
| 2038 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2039 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2040 | // struct X { int a, b; }; |
| 2041 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2042 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2043 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2044 | // designated initializer re-initializes the whole |
| 2045 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2046 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2047 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2048 | << InitRange; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2049 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2050 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2051 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2052 | << ExistingInit->getSourceRange(); |
| 2053 | } |
| 2054 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2055 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2056 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 2057 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2058 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2060 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2061 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2062 | // Pre-allocate storage for the structured initializer list. |
| 2063 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2064 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2065 | bool GotNumInits = false; |
| 2066 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2067 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2068 | GotNumInits = true; |
| 2069 | } else if (Index < IList->getNumInits()) { |
| 2070 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2071 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2072 | GotNumInits = true; |
| 2073 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2076 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2077 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2078 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2079 | NumElements = CAType->getSize().getZExtValue(); |
| 2080 | // Simple heuristic so that we don't allocate a very large |
| 2081 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2082 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2083 | NumElements = 0; |
| 2084 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2085 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2086 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2087 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2088 | RecordDecl *RDecl = RType->getDecl(); |
| 2089 | if (RDecl->isUnion()) |
| 2090 | NumElements = 1; |
| 2091 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2093 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2096 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2097 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2098 | // Link this new initializer list into the structured initializer |
| 2099 | // lists. |
| 2100 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2101 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2102 | else { |
| 2103 | Result->setSyntacticForm(IList); |
| 2104 | SyntacticToSemantic[IList] = Result; |
| 2105 | } |
| 2106 | |
| 2107 | return Result; |
| 2108 | } |
| 2109 | |
| 2110 | /// Update the initializer at index @p StructuredIndex within the |
| 2111 | /// structured initializer list to the value @p expr. |
| 2112 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2113 | unsigned &StructuredIndex, |
| 2114 | Expr *expr) { |
| 2115 | // No structured initializer list to update |
| 2116 | if (!StructuredList) |
| 2117 | return; |
| 2118 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2119 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2120 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2121 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2122 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2123 | diag::warn_initializer_overrides) |
| 2124 | << expr->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2125 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2126 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2127 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2128 | << PrevInit->getSourceRange(); |
| 2129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2130 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2131 | ++StructuredIndex; |
| 2132 | } |
| 2133 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2134 | /// Check that the given Index expression is a valid array designator |
| 2135 | /// value. This is essentailly just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2136 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2137 | /// and produces a reasonable diagnostic if there is a |
| 2138 | /// failure. Returns true if there was an error, false otherwise. If |
| 2139 | /// everything went okay, Value will receive the value of the constant |
| 2140 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | static bool |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2142 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2143 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 2144 | |
| 2145 | // Make sure this is an integer constant expression. |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2146 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 2147 | return true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2148 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2149 | if (Value.isSigned() && Value.isNegative()) |
| 2150 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2151 | << Value.toString(10) << Index->getSourceRange(); |
| 2152 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2153 | Value.setIsUnsigned(true); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2154 | return false; |
| 2155 | } |
| 2156 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2157 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2158 | SourceLocation Loc, |
| 2159 | bool GNUSyntax, |
| 2160 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2161 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2162 | |
| 2163 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2164 | SmallVector<ASTDesignator, 32> Designators; |
| 2165 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2166 | |
| 2167 | // Build designators and check array designator expressions. |
| 2168 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2169 | const Designator &D = Desig.getDesignator(Idx); |
| 2170 | switch (D.getKind()) { |
| 2171 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2173 | D.getFieldLoc())); |
| 2174 | break; |
| 2175 | |
| 2176 | case Designator::ArrayDesignator: { |
| 2177 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2178 | llvm::APSInt IndexValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2179 | if (!Index->isTypeDependent() && |
| 2180 | !Index->isValueDependent() && |
| 2181 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2182 | Invalid = true; |
| 2183 | else { |
| 2184 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2186 | D.getRBracketLoc())); |
| 2187 | InitExpressions.push_back(Index); |
| 2188 | } |
| 2189 | break; |
| 2190 | } |
| 2191 | |
| 2192 | case Designator::ArrayRangeDesignator: { |
| 2193 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2194 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2195 | llvm::APSInt StartValue; |
| 2196 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2197 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2198 | StartIndex->isValueDependent(); |
| 2199 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2200 | EndIndex->isValueDependent(); |
| 2201 | if ((!StartDependent && |
| 2202 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 2203 | (!EndDependent && |
| 2204 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2205 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2206 | else { |
| 2207 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2208 | if (StartDependent || EndDependent) { |
| 2209 | // Nothing to compute. |
| 2210 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2211 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2212 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2213 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2214 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2215 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2216 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2218 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2219 | Invalid = true; |
| 2220 | } else { |
| 2221 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2222 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2223 | D.getEllipsisLoc(), |
| 2224 | D.getRBracketLoc())); |
| 2225 | InitExpressions.push_back(StartIndex); |
| 2226 | InitExpressions.push_back(EndIndex); |
| 2227 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2228 | } |
| 2229 | break; |
| 2230 | } |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | if (Invalid || Init.isInvalid()) |
| 2235 | return ExprError(); |
| 2236 | |
| 2237 | // Clear out the expressions within the designation. |
| 2238 | Desig.ClearExprs(*this); |
| 2239 | |
| 2240 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2241 | = DesignatedInitExpr::Create(Context, |
| 2242 | Designators.data(), Designators.size(), |
| 2243 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 2244 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2245 | |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2246 | if (getLangOptions().CPlusPlus) |
Eli Friedman | a47317b | 2011-04-24 22:14:22 +0000 | [diff] [blame] | 2247 | Diag(DIE->getLocStart(), diag::ext_designated_init_cxx) |
| 2248 | << DIE->getSourceRange(); |
| 2249 | else if (!getLangOptions().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2250 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2251 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2252 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2253 | return Owned(DIE); |
| 2254 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2255 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2256 | //===----------------------------------------------------------------------===// |
| 2257 | // Initialization entity |
| 2258 | //===----------------------------------------------------------------------===// |
| 2259 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2260 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2261 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2262 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2263 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2264 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2265 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2266 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2267 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2268 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2269 | Type = VT->getElementType(); |
| 2270 | } else { |
| 2271 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2272 | assert(CT && "Unexpected type"); |
| 2273 | Kind = EK_ComplexElement; |
| 2274 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2275 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2278 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2279 | CXXBaseSpecifier *Base, |
| 2280 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2281 | { |
| 2282 | InitializedEntity Result; |
| 2283 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2284 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2285 | if (IsInheritedVirtualBase) |
| 2286 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2287 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2288 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2289 | return Result; |
| 2290 | } |
| 2291 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2292 | DeclarationName InitializedEntity::getName() const { |
| 2293 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2294 | case EK_Parameter: { |
| 2295 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2296 | return (D ? D->getDeclName() : DeclarationName()); |
| 2297 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2298 | |
| 2299 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2300 | case EK_Member: |
| 2301 | return VariableOrMember->getDeclName(); |
| 2302 | |
| 2303 | case EK_Result: |
| 2304 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2305 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2306 | case EK_Temporary: |
| 2307 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2308 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2309 | case EK_ArrayElement: |
| 2310 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2311 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2312 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2313 | return DeclarationName(); |
| 2314 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2315 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2316 | // Silence GCC warning |
| 2317 | return DeclarationName(); |
| 2318 | } |
| 2319 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2320 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2321 | switch (getKind()) { |
| 2322 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2323 | case EK_Member: |
| 2324 | return VariableOrMember; |
| 2325 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2326 | case EK_Parameter: |
| 2327 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2328 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2329 | case EK_Result: |
| 2330 | case EK_Exception: |
| 2331 | case EK_New: |
| 2332 | case EK_Temporary: |
| 2333 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2334 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2335 | case EK_ArrayElement: |
| 2336 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2337 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2338 | case EK_BlockElement: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2339 | return 0; |
| 2340 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2341 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2342 | // Silence GCC warning |
| 2343 | return 0; |
| 2344 | } |
| 2345 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2346 | bool InitializedEntity::allowsNRVO() const { |
| 2347 | switch (getKind()) { |
| 2348 | case EK_Result: |
| 2349 | case EK_Exception: |
| 2350 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2351 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2352 | case EK_Variable: |
| 2353 | case EK_Parameter: |
| 2354 | case EK_Member: |
| 2355 | case EK_New: |
| 2356 | case EK_Temporary: |
| 2357 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2358 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2359 | case EK_ArrayElement: |
| 2360 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2361 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2362 | case EK_BlockElement: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2363 | break; |
| 2364 | } |
| 2365 | |
| 2366 | return false; |
| 2367 | } |
| 2368 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2369 | //===----------------------------------------------------------------------===// |
| 2370 | // Initialization sequence |
| 2371 | //===----------------------------------------------------------------------===// |
| 2372 | |
| 2373 | void InitializationSequence::Step::Destroy() { |
| 2374 | switch (Kind) { |
| 2375 | case SK_ResolveAddressOfOverloadedFunction: |
| 2376 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2377 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2378 | case SK_CastDerivedToBaseLValue: |
| 2379 | case SK_BindReference: |
| 2380 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2381 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2382 | case SK_UserConversion: |
| 2383 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2384 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2385 | case SK_QualificationConversionLValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2386 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2387 | case SK_ListConstructorCall: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2388 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2389 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2390 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2391 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2392 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2393 | case SK_ArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2394 | case SK_PassByIndirectCopyRestore: |
| 2395 | case SK_PassByIndirectRestore: |
| 2396 | case SK_ProduceObjCObject: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2397 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2398 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2399 | case SK_ConversionSequence: |
| 2400 | delete ICS; |
| 2401 | } |
| 2402 | } |
| 2403 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2404 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2405 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2409 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2410 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2411 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2412 | switch (getFailureKind()) { |
| 2413 | case FK_TooManyInitsForReference: |
| 2414 | case FK_ArrayNeedsInitList: |
| 2415 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2416 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2417 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2418 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2419 | case FK_RValueReferenceBindingToLValue: |
| 2420 | case FK_ReferenceInitDropsQualifiers: |
| 2421 | case FK_ReferenceInitFailed: |
| 2422 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2423 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2424 | case FK_TooManyInitsForScalar: |
| 2425 | case FK_ReferenceBindingToInitList: |
| 2426 | case FK_InitListBadDestinationType: |
| 2427 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2428 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2429 | case FK_ArrayTypeMismatch: |
| 2430 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2431 | case FK_ListInitializationFailed: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2432 | case FK_PlaceholderType: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2433 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2434 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2435 | case FK_ReferenceInitOverloadFailed: |
| 2436 | case FK_UserConversionOverloadFailed: |
| 2437 | case FK_ConstructorOverloadFailed: |
| 2438 | return FailedOverloadResult == OR_Ambiguous; |
| 2439 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2440 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2441 | return false; |
| 2442 | } |
| 2443 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2444 | bool InitializationSequence::isConstructorInitialization() const { |
| 2445 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2446 | } |
| 2447 | |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2448 | bool InitializationSequence::endsWithNarrowing(ASTContext &Ctx, |
| 2449 | const Expr *Initializer, |
| 2450 | bool *isInitializerConstant, |
| 2451 | APValue *ConstantValue) const { |
| 2452 | if (Steps.empty() || Initializer->isValueDependent()) |
| 2453 | return false; |
| 2454 | |
| 2455 | const Step &LastStep = Steps.back(); |
| 2456 | if (LastStep.Kind != SK_ConversionSequence) |
| 2457 | return false; |
| 2458 | |
| 2459 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 2460 | const StandardConversionSequence *SCS = NULL; |
| 2461 | switch (ICS.getKind()) { |
| 2462 | case ImplicitConversionSequence::StandardConversion: |
| 2463 | SCS = &ICS.Standard; |
| 2464 | break; |
| 2465 | case ImplicitConversionSequence::UserDefinedConversion: |
| 2466 | SCS = &ICS.UserDefined.After; |
| 2467 | break; |
| 2468 | case ImplicitConversionSequence::AmbiguousConversion: |
| 2469 | case ImplicitConversionSequence::EllipsisConversion: |
| 2470 | case ImplicitConversionSequence::BadConversion: |
| 2471 | return false; |
| 2472 | } |
| 2473 | |
| 2474 | // Check if SCS represents a narrowing conversion, according to C++0x |
| 2475 | // [dcl.init.list]p7: |
| 2476 | // |
| 2477 | // A narrowing conversion is an implicit conversion ... |
| 2478 | ImplicitConversionKind PossibleNarrowing = SCS->Second; |
| 2479 | QualType FromType = SCS->getToType(0); |
| 2480 | QualType ToType = SCS->getToType(1); |
| 2481 | switch (PossibleNarrowing) { |
| 2482 | // * from a floating-point type to an integer type, or |
| 2483 | // |
| 2484 | // * from an integer type or unscoped enumeration type to a floating-point |
| 2485 | // type, except where the source is a constant expression and the actual |
| 2486 | // value after conversion will fit into the target type and will produce |
| 2487 | // the original value when converted back to the original type, or |
| 2488 | case ICK_Floating_Integral: |
| 2489 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 2490 | *isInitializerConstant = false; |
| 2491 | return true; |
| 2492 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 2493 | llvm::APSInt IntConstantValue; |
| 2494 | if (Initializer && |
| 2495 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 2496 | // Convert the integer to the floating type. |
| 2497 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 2498 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 2499 | llvm::APFloat::rmNearestTiesToEven); |
| 2500 | // And back. |
| 2501 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 2502 | bool ignored; |
| 2503 | Result.convertToInteger(ConvertedValue, |
| 2504 | llvm::APFloat::rmTowardZero, &ignored); |
| 2505 | // If the resulting value is different, this was a narrowing conversion. |
| 2506 | if (IntConstantValue != ConvertedValue) { |
| 2507 | *isInitializerConstant = true; |
| 2508 | *ConstantValue = APValue(IntConstantValue); |
| 2509 | return true; |
| 2510 | } |
| 2511 | } else { |
| 2512 | // Variables are always narrowings. |
| 2513 | *isInitializerConstant = false; |
| 2514 | return true; |
| 2515 | } |
| 2516 | } |
| 2517 | return false; |
| 2518 | |
| 2519 | // * from long double to double or float, or from double to float, except |
| 2520 | // where the source is a constant expression and the actual value after |
| 2521 | // conversion is within the range of values that can be represented (even |
| 2522 | // if it cannot be represented exactly), or |
| 2523 | case ICK_Floating_Conversion: |
| 2524 | if (1 == Ctx.getFloatingTypeOrder(FromType, ToType)) { |
| 2525 | // FromType is larger than ToType. |
| 2526 | Expr::EvalResult InitializerValue; |
| 2527 | // FIXME: Check whether Initializer is a constant expression according |
| 2528 | // to C++0x [expr.const], rather than just whether it can be folded. |
| 2529 | if (Initializer->Evaluate(InitializerValue, Ctx) && |
| 2530 | !InitializerValue.HasSideEffects && InitializerValue.Val.isFloat()) { |
| 2531 | // Constant! (Except for FIXME above.) |
| 2532 | llvm::APFloat FloatVal = InitializerValue.Val.getFloat(); |
| 2533 | // Convert the source value into the target type. |
| 2534 | bool ignored; |
| 2535 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 2536 | Ctx.getFloatTypeSemantics(ToType), |
| 2537 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2538 | // If there was no overflow, the source value is within the range of |
| 2539 | // values that can be represented. |
| 2540 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 2541 | *isInitializerConstant = true; |
| 2542 | *ConstantValue = InitializerValue.Val; |
| 2543 | return true; |
| 2544 | } |
| 2545 | } else { |
| 2546 | *isInitializerConstant = false; |
| 2547 | return true; |
| 2548 | } |
| 2549 | } |
| 2550 | return false; |
| 2551 | |
| 2552 | // * from an integer type or unscoped enumeration type to an integer type |
| 2553 | // that cannot represent all the values of the original type, except where |
| 2554 | // the source is a constant expression and the actual value after |
| 2555 | // conversion will fit into the target type and will produce the original |
| 2556 | // value when converted back to the original type. |
Jeffrey Yasskin | 6d0ee8d | 2011-08-12 20:56:43 +0000 | [diff] [blame] | 2557 | case ICK_Boolean_Conversion: // Bools are integers too. |
Jeffrey Yasskin | b89d5ed | 2011-08-30 22:25:41 +0000 | [diff] [blame] | 2558 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 2559 | // Boolean conversions can be from pointers and pointers to members |
| 2560 | // [conv.bool], and those aren't considered narrowing conversions. |
| 2561 | return false; |
| 2562 | } // Otherwise, fall through to the integral case. |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2563 | case ICK_Integral_Conversion: { |
| 2564 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 2565 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 2566 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 2567 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 2568 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 2569 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 2570 | |
| 2571 | if (FromWidth > ToWidth || |
| 2572 | (FromWidth == ToWidth && FromSigned != ToSigned)) { |
| 2573 | // Not all values of FromType can be represented in ToType. |
| 2574 | llvm::APSInt InitializerValue; |
| 2575 | if (Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 2576 | *isInitializerConstant = true; |
| 2577 | *ConstantValue = APValue(InitializerValue); |
| 2578 | |
| 2579 | // Add a bit to the InitializerValue so we don't have to worry about |
| 2580 | // signed vs. unsigned comparisons. |
| 2581 | InitializerValue = InitializerValue.extend( |
| 2582 | InitializerValue.getBitWidth() + 1); |
| 2583 | // Convert the initializer to and from the target width and signed-ness. |
| 2584 | llvm::APSInt ConvertedValue = InitializerValue; |
| 2585 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 2586 | ConvertedValue.setIsSigned(ToSigned); |
| 2587 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 2588 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 2589 | // If the result is different, this was a narrowing conversion. |
| 2590 | return ConvertedValue != InitializerValue; |
| 2591 | } else { |
| 2592 | // Variables are always narrowings. |
| 2593 | *isInitializerConstant = false; |
| 2594 | return true; |
| 2595 | } |
| 2596 | } |
| 2597 | return false; |
| 2598 | } |
| 2599 | |
| 2600 | default: |
| 2601 | // Other kinds of conversions are not narrowings. |
| 2602 | return false; |
| 2603 | } |
| 2604 | } |
| 2605 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2606 | void InitializationSequence::AddAddressOverloadResolutionStep( |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2607 | FunctionDecl *Function, |
| 2608 | DeclAccessPair Found) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2609 | Step S; |
| 2610 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2611 | S.Type = Function->getType(); |
Benjamin Kramer | 8503564 | 2011-10-09 17:58:25 +0000 | [diff] [blame] | 2612 | S.Function.HadMultipleCandidates = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2613 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2614 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2615 | Steps.push_back(S); |
| 2616 | } |
| 2617 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2618 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2619 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2620 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2621 | switch (VK) { |
| 2622 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2623 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2624 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2625 | default: llvm_unreachable("No such category"); |
| 2626 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2627 | S.Type = BaseType; |
| 2628 | Steps.push_back(S); |
| 2629 | } |
| 2630 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2631 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2632 | bool BindingTemporary) { |
| 2633 | Step S; |
| 2634 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2635 | S.Type = T; |
| 2636 | Steps.push_back(S); |
| 2637 | } |
| 2638 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2639 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2640 | Step S; |
| 2641 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2642 | S.Type = T; |
| 2643 | Steps.push_back(S); |
| 2644 | } |
| 2645 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2646 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2647 | DeclAccessPair FoundDecl, |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2648 | QualType T) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2649 | Step S; |
| 2650 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2651 | S.Type = T; |
Benjamin Kramer | 8503564 | 2011-10-09 17:58:25 +0000 | [diff] [blame] | 2652 | S.Function.HadMultipleCandidates = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2653 | S.Function.Function = Function; |
| 2654 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2655 | Steps.push_back(S); |
| 2656 | } |
| 2657 | |
| 2658 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2659 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2660 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2661 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2662 | switch (VK) { |
| 2663 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2664 | S.Kind = SK_QualificationConversionRValue; |
| 2665 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2666 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2667 | S.Kind = SK_QualificationConversionXValue; |
| 2668 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2669 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2670 | S.Kind = SK_QualificationConversionLValue; |
| 2671 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2672 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2673 | S.Type = Ty; |
| 2674 | Steps.push_back(S); |
| 2675 | } |
| 2676 | |
| 2677 | void InitializationSequence::AddConversionSequenceStep( |
| 2678 | const ImplicitConversionSequence &ICS, |
| 2679 | QualType T) { |
| 2680 | Step S; |
| 2681 | S.Kind = SK_ConversionSequence; |
| 2682 | S.Type = T; |
| 2683 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2684 | Steps.push_back(S); |
| 2685 | } |
| 2686 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2687 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2688 | Step S; |
| 2689 | S.Kind = SK_ListInitialization; |
| 2690 | S.Type = T; |
| 2691 | Steps.push_back(S); |
| 2692 | } |
| 2693 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2694 | void |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2695 | InitializationSequence::AddConstructorInitializationStep( |
| 2696 | CXXConstructorDecl *Constructor, |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2697 | AccessSpecifier Access, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2698 | QualType T) { |
| 2699 | Step S; |
| 2700 | S.Kind = SK_ConstructorInitialization; |
| 2701 | S.Type = T; |
Benjamin Kramer | 8503564 | 2011-10-09 17:58:25 +0000 | [diff] [blame] | 2702 | S.Function.HadMultipleCandidates = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2703 | S.Function.Function = Constructor; |
| 2704 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2705 | Steps.push_back(S); |
| 2706 | } |
| 2707 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2708 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2709 | Step S; |
| 2710 | S.Kind = SK_ZeroInitialization; |
| 2711 | S.Type = T; |
| 2712 | Steps.push_back(S); |
| 2713 | } |
| 2714 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2715 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2716 | Step S; |
| 2717 | S.Kind = SK_CAssignment; |
| 2718 | S.Type = T; |
| 2719 | Steps.push_back(S); |
| 2720 | } |
| 2721 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2722 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2723 | Step S; |
| 2724 | S.Kind = SK_StringInit; |
| 2725 | S.Type = T; |
| 2726 | Steps.push_back(S); |
| 2727 | } |
| 2728 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2729 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2730 | Step S; |
| 2731 | S.Kind = SK_ObjCObjectConversion; |
| 2732 | S.Type = T; |
| 2733 | Steps.push_back(S); |
| 2734 | } |
| 2735 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2736 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2737 | Step S; |
| 2738 | S.Kind = SK_ArrayInit; |
| 2739 | S.Type = T; |
| 2740 | Steps.push_back(S); |
| 2741 | } |
| 2742 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2743 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2744 | bool shouldCopy) { |
| 2745 | Step s; |
| 2746 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2747 | : SK_PassByIndirectRestore); |
| 2748 | s.Type = type; |
| 2749 | Steps.push_back(s); |
| 2750 | } |
| 2751 | |
| 2752 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2753 | Step S; |
| 2754 | S.Kind = SK_ProduceObjCObject; |
| 2755 | S.Type = T; |
| 2756 | Steps.push_back(S); |
| 2757 | } |
| 2758 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2759 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2760 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2761 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2762 | this->Failure = Failure; |
| 2763 | this->FailedOverloadResult = Result; |
| 2764 | } |
| 2765 | |
| 2766 | //===----------------------------------------------------------------------===// |
| 2767 | // Attempt initialization |
| 2768 | //===----------------------------------------------------------------------===// |
| 2769 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2770 | static void MaybeProduceObjCObject(Sema &S, |
| 2771 | InitializationSequence &Sequence, |
| 2772 | const InitializedEntity &Entity) { |
| 2773 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 2774 | |
| 2775 | /// When initializing a parameter, produce the value if it's marked |
| 2776 | /// __attribute__((ns_consumed)). |
| 2777 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2778 | if (!Entity.isParameterConsumed()) |
| 2779 | return; |
| 2780 | |
| 2781 | assert(Entity.getType()->isObjCRetainableType() && |
| 2782 | "consuming an object of unretainable type?"); |
| 2783 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2784 | |
| 2785 | /// When initializing a return value, if the return type is a |
| 2786 | /// retainable type, then returns need to immediately retain the |
| 2787 | /// object. If an autorelease is required, it will be done at the |
| 2788 | /// last instant. |
| 2789 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2790 | if (!Entity.getType()->isObjCRetainableType()) |
| 2791 | return; |
| 2792 | |
| 2793 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2794 | } |
| 2795 | } |
| 2796 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2797 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 2798 | static void TryListInitialization(Sema &S, |
| 2799 | const InitializedEntity &Entity, |
| 2800 | const InitializationKind &Kind, |
| 2801 | InitListExpr *InitList, |
| 2802 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2803 | QualType DestType = Entity.getType(); |
| 2804 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2805 | // C++ doesn't allow scalar initialization with more than one argument. |
| 2806 | // But C99 complex numbers are scalars and it makes sense there. |
| 2807 | if (S.getLangOptions().CPlusPlus && DestType->isScalarType() && |
| 2808 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 2809 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2810 | return; |
| 2811 | } |
| 2812 | // FIXME: C++0x defines behavior for these two cases. |
| 2813 | if (DestType->isReferenceType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2814 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2815 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2816 | } |
| 2817 | if (DestType->isRecordType() && !DestType->isAggregateType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2818 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2819 | return; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2822 | InitListChecker CheckInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 2823 | DestType, /*VerifyOnly=*/true, |
| 2824 | Kind.getKind() != InitializationKind::IK_Direct || |
| 2825 | !S.getLangOptions().CPlusPlus0x); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2826 | if (CheckInitList.HadError()) { |
| 2827 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 2828 | return; |
| 2829 | } |
| 2830 | |
| 2831 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 2832 | Sequence.AddListInitializationStep(DestType); |
| 2833 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2834 | |
| 2835 | /// \brief Try a reference initialization that involves calling a conversion |
| 2836 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2837 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2838 | const InitializedEntity &Entity, |
| 2839 | const InitializationKind &Kind, |
| 2840 | Expr *Initializer, |
| 2841 | bool AllowRValues, |
| 2842 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2843 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2844 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2845 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2846 | QualType cv2T2 = Initializer->getType(); |
| 2847 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2848 | |
| 2849 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2850 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2851 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2852 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2853 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2854 | ObjCConversion, |
| 2855 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2856 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2857 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2858 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2859 | (void)ObjCLifetimeConversion; |
| 2860 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2861 | // Build the candidate set directly in the initialization sequence |
| 2862 | // structure, so that it will persist if we fail. |
| 2863 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2864 | CandidateSet.clear(); |
| 2865 | |
| 2866 | // Determine whether we are allowed to call explicit constructors or |
| 2867 | // explicit conversion operators. |
| 2868 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2869 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2870 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2871 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 2872 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2873 | // The type we're converting to is a class type. Enumerate its constructors |
| 2874 | // to see if there is a suitable conversion. |
| 2875 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2876 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2877 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 2878 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2879 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2880 | NamedDecl *D = *Con; |
| 2881 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2882 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2883 | // Find the constructor (which may be a template). |
| 2884 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2885 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2886 | if (ConstructorTmpl) |
| 2887 | Constructor = cast<CXXConstructorDecl>( |
| 2888 | ConstructorTmpl->getTemplatedDecl()); |
| 2889 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2890 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2891 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2892 | if (!Constructor->isInvalidDecl() && |
| 2893 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2894 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2895 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2896 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2897 | &Initializer, 1, CandidateSet, |
| 2898 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2899 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2900 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 2901 | &Initializer, 1, CandidateSet, |
| 2902 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2903 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2904 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2905 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2906 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 2907 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2908 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 2909 | const RecordType *T2RecordType = 0; |
| 2910 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 2911 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2912 | // The type we're converting from is a class type, enumerate its conversion |
| 2913 | // functions. |
| 2914 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2915 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2916 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2917 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2918 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2919 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2920 | NamedDecl *D = *I; |
| 2921 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2922 | if (isa<UsingShadowDecl>(D)) |
| 2923 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2924 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2925 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2926 | CXXConversionDecl *Conv; |
| 2927 | if (ConvTemplate) |
| 2928 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2929 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 2930 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2931 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2932 | // If the conversion function doesn't return a reference type, |
| 2933 | // it can't be considered for this conversion unless we're allowed to |
| 2934 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2935 | // FIXME: Do we need to make sure that we only consider conversion |
| 2936 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2937 | // break recursion. |
| 2938 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2939 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2940 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2941 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2942 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2943 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2944 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2945 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 2946 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2947 | } |
| 2948 | } |
| 2949 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 2950 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 2951 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2952 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2953 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2954 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2955 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2956 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2957 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 2958 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2959 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2960 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2961 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2962 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 2963 | // This is the overload that will actually be used for the initialization, so |
| 2964 | // mark it as used. |
| 2965 | S.MarkDeclarationReferenced(DeclLoc, Function); |
| 2966 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2967 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2968 | if (isa<CXXConversionDecl>(Function)) |
| 2969 | T2 = Function->getResultType(); |
| 2970 | else |
| 2971 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2972 | |
| 2973 | // Add the user-defined conversion step. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2974 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2975 | T2.getNonLValueExprType(S.Context)); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2976 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2977 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2978 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2979 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2980 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2981 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2982 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2983 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2984 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2985 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2986 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2987 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2988 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2989 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2990 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2991 | NewDerivedToBase, NewObjCConversion, |
| 2992 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 2993 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 2994 | // If the type we've converted to is not reference-related to the |
| 2995 | // type we're looking for, then there is another conversion step |
| 2996 | // we need to perform to produce a temporary of the right type |
| 2997 | // that we'll be binding to. |
| 2998 | ImplicitConversionSequence ICS; |
| 2999 | ICS.setStandard(); |
| 3000 | ICS.Standard = Best->FinalConversion; |
| 3001 | T2 = ICS.Standard.getToType(2); |
| 3002 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3003 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3004 | Sequence.AddDerivedToBaseCastStep( |
| 3005 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3006 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3007 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3008 | else if (NewObjCConversion) |
| 3009 | Sequence.AddObjCObjectConversionStep( |
| 3010 | S.Context.getQualifiedType(T1, |
| 3011 | T2.getNonReferenceType().getQualifiers())); |
| 3012 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3013 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3014 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3015 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3016 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3017 | return OR_Success; |
| 3018 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3019 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3020 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3021 | const InitializedEntity &Entity, |
| 3022 | Expr *CurInitExpr); |
| 3023 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3024 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3025 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3026 | const InitializedEntity &Entity, |
| 3027 | const InitializationKind &Kind, |
| 3028 | Expr *Initializer, |
| 3029 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3030 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3031 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3032 | Qualifiers T1Quals; |
| 3033 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3034 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3035 | Qualifiers T2Quals; |
| 3036 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3037 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3038 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3039 | // If the initializer is the address of an overloaded function, try |
| 3040 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3041 | // type of the resulting function. |
| 3042 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3043 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3044 | if (FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3045 | T1, |
| 3046 | false, |
| 3047 | Found)) { |
| 3048 | Sequence.AddAddressOverloadResolutionStep(Fn, Found); |
| 3049 | cv2T2 = Fn->getType(); |
| 3050 | T2 = cv2T2.getUnqualifiedType(); |
| 3051 | } else if (!T1->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3052 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3053 | return; |
| 3054 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3055 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3056 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3057 | // Compute some basic properties of the types and the initializer. |
| 3058 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3059 | bool isRValueRef = !isLValueRef; |
| 3060 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3061 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3062 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3063 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3064 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3065 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3066 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3067 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3068 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3069 | // 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] | 3070 | // "cv2 T2" as follows: |
| 3071 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3072 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3073 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3074 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3075 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3076 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3077 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3078 | bool T1Function = T1->isFunctionType(); |
| 3079 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3080 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3081 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3082 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3083 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3084 | // - 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] | 3085 | // reference-compatible with "cv2 T2," or |
| 3086 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3087 | // 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] | 3088 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3089 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3090 | // to decide whether we're actually binding to a temporary created from |
| 3091 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3092 | if (DerivedToBase) |
| 3093 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3094 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3095 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3096 | else if (ObjCConversion) |
| 3097 | Sequence.AddObjCObjectConversionStep( |
| 3098 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3099 | |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3100 | if (T1Quals != T2Quals) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3101 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3102 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3103 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3104 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3105 | return; |
| 3106 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3107 | |
| 3108 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3109 | // reference-related to T2, and can be implicitly converted to an |
| 3110 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3111 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3112 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3113 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3114 | // If we have an rvalue ref to function type here, the rhs must be |
| 3115 | // an rvalue. |
| 3116 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3117 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3118 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3119 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3120 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3121 | Sequence); |
| 3122 | if (ConvOvlResult == OR_Success) |
| 3123 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3124 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3125 | Sequence.SetOverloadFailure( |
| 3126 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3127 | ConvOvlResult); |
| 3128 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3129 | } |
| 3130 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3131 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3132 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3133 | // 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] | 3134 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3135 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3136 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3137 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3138 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3139 | Sequence.SetOverloadFailure( |
| 3140 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3141 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3142 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3143 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3144 | ? (RefRelationship == Sema::Ref_Related |
| 3145 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3146 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3147 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3148 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3149 | return; |
| 3150 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3151 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3152 | // - If the initializer expression |
| 3153 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3154 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3155 | // Note: functions are handled below. |
| 3156 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3157 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3158 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3159 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3160 | (InitCategory.isXValue() || |
| 3161 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3162 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3163 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3164 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3165 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3166 | // compiler the freedom to perform a copy here or bind to the |
| 3167 | // object, while C++0x requires that we bind directly to the |
| 3168 | // object. Hence, we always bind to the object without making an |
| 3169 | // extra copy. However, in C++03 requires that we check for the |
| 3170 | // presence of a suitable copy constructor: |
| 3171 | // |
| 3172 | // The constructor that would be used to make the copy shall |
| 3173 | // be callable whether or not the copy is actually done. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3174 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3175 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3176 | else if (S.getLangOptions().CPlusPlus0x) |
| 3177 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3178 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3179 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3180 | if (DerivedToBase) |
| 3181 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3182 | ValueKind); |
| 3183 | else if (ObjCConversion) |
| 3184 | Sequence.AddObjCObjectConversionStep( |
| 3185 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3186 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3187 | if (T1Quals != T2Quals) |
| 3188 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3189 | Sequence.AddReferenceBindingStep(cv1T1, |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3190 | /*bindingTemporary=*/(InitCategory.isPRValue() && !T2->isArrayType())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3191 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3192 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3193 | |
| 3194 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3195 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3196 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3197 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3198 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3199 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3200 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3201 | Kind, Initializer, |
| 3202 | /*AllowRValues=*/true, |
| 3203 | Sequence); |
| 3204 | if (ConvOvlResult) |
| 3205 | Sequence.SetOverloadFailure( |
| 3206 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3207 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3208 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3209 | return; |
| 3210 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3211 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3212 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3213 | return; |
| 3214 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3215 | |
| 3216 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3217 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3218 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3219 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3220 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3221 | // Determine whether we are allowed to call explicit constructors or |
| 3222 | // explicit conversion operators. |
| 3223 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3224 | |
| 3225 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3226 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3227 | ImplicitConversionSequence ICS |
| 3228 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3229 | /*SuppressUserConversions*/ false, |
| 3230 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3231 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3232 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3233 | /*AllowObjCWritebackConversion=*/false); |
| 3234 | |
| 3235 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3236 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3237 | // this into an overloading ambiguity diagnostic. However, we need |
| 3238 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3239 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3240 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3241 | Sequence.SetOverloadFailure( |
| 3242 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3243 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3244 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3245 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3246 | else |
| 3247 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3248 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3249 | } else { |
| 3250 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3254 | // same cv-qualification as, or greater cv-qualification |
| 3255 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3256 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3257 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3258 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3259 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3260 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3261 | return; |
| 3262 | } |
| 3263 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3264 | // [...] 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] | 3265 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3266 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3267 | InitCategory.isLValue()) { |
| 3268 | Sequence.SetFailed( |
| 3269 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3270 | return; |
| 3271 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3272 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3273 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3274 | return; |
| 3275 | } |
| 3276 | |
| 3277 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3278 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3279 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3280 | const InitializedEntity &Entity, |
| 3281 | const InitializationKind &Kind, |
| 3282 | Expr *Initializer, |
| 3283 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3284 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3287 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3288 | /// enumerates the constructors of the initialized entity and performs overload |
| 3289 | /// resolution to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3290 | static void TryConstructorInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3291 | const InitializedEntity &Entity, |
| 3292 | const InitializationKind &Kind, |
| 3293 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3294 | QualType DestType, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3295 | InitializationSequence &Sequence) { |
Richard Trieu | 898267f | 2011-09-01 21:44:13 +0000 | [diff] [blame] | 3296 | // Check constructor arguments for self reference. |
| 3297 | if (DeclaratorDecl *DD = Entity.getDecl()) |
| 3298 | // Parameters arguments are occassionially constructed with itself, |
| 3299 | // for instance, in recursive functions. Skip them. |
| 3300 | if (!isa<ParmVarDecl>(DD)) |
| 3301 | for (unsigned i = 0; i < NumArgs; ++i) |
| 3302 | S.CheckSelfReference(DD, Args[i]); |
| 3303 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3304 | // Build the candidate set directly in the initialization sequence |
| 3305 | // structure, so that it will persist if we fail. |
| 3306 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3307 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3308 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3309 | // Determine whether we are allowed to call explicit constructors or |
| 3310 | // explicit conversion operators. |
| 3311 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 3312 | Kind.getKind() == InitializationKind::IK_Value || |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3313 | Kind.getKind() == InitializationKind::IK_Default); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3314 | |
| 3315 | // The type we're constructing needs to be complete. |
| 3316 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 3317 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3318 | return; |
| 3319 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3320 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3321 | // The type we're converting to is a class type. Enumerate its constructors |
| 3322 | // to see if one is suitable. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3323 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3324 | assert(DestRecordType && "Constructor initialization requires record type"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3325 | CXXRecordDecl *DestRecordDecl |
| 3326 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3327 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3328 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3329 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3330 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3331 | NamedDecl *D = *Con; |
| 3332 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3333 | bool SuppressUserConversions = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3334 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3335 | // Find the constructor (which may be a template). |
| 3336 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3337 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3338 | if (ConstructorTmpl) |
| 3339 | Constructor = cast<CXXConstructorDecl>( |
| 3340 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3341 | else { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3342 | Constructor = cast<CXXConstructorDecl>(D); |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3343 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3344 | // If we're performing copy initialization using a copy constructor, we |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3345 | // suppress user-defined conversions on the arguments. |
| 3346 | // FIXME: Move constructors? |
| 3347 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 3348 | Constructor->isCopyConstructor()) |
| 3349 | SuppressUserConversions = true; |
| 3350 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3351 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3352 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3353 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3354 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3355 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3356 | /*ExplicitArgs*/ 0, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3357 | Args, NumArgs, CandidateSet, |
| 3358 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3359 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3360 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 3361 | Args, NumArgs, CandidateSet, |
| 3362 | SuppressUserConversions); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3363 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3366 | SourceLocation DeclLoc = Kind.getLocation(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3367 | |
| 3368 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3369 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3370 | if (OverloadingResult Result |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 3371 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3372 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3373 | InitializationSequence::FK_ConstructorOverloadFailed, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3374 | Result); |
| 3375 | return; |
| 3376 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3377 | |
| 3378 | // C++0x [dcl.init]p6: |
| 3379 | // If a program calls for the default initialization of an object |
| 3380 | // of a const-qualified type T, T shall be a class type with a |
| 3381 | // user-provided default constructor. |
| 3382 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3383 | Entity.getType().isConstQualified() && |
| 3384 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 3385 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3386 | return; |
| 3387 | } |
| 3388 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3389 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3390 | // subsumed by the initialization. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 3391 | Sequence.AddConstructorInitializationStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3392 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3393 | Best->FoundDecl.getAccess(), |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3394 | DestType); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3397 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3398 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3399 | const InitializedEntity &Entity, |
| 3400 | const InitializationKind &Kind, |
| 3401 | InitializationSequence &Sequence) { |
| 3402 | // C++ [dcl.init]p5: |
| 3403 | // |
| 3404 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3405 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3406 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3407 | // -- if T is an array type, then each element is value-initialized; |
| 3408 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 3409 | T = AT->getElementType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3410 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3411 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3412 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3413 | // -- if T is a class type (clause 9) with a user-declared |
| 3414 | // constructor (12.1), then the default constructor for T is |
| 3415 | // called (and the initialization is ill-formed if T has no |
| 3416 | // accessible default constructor); |
| 3417 | // |
| 3418 | // FIXME: we really want to refer to a single subobject of the array, |
| 3419 | // but Entity doesn't have a way to capture that (yet). |
| 3420 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 3421 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3422 | |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3423 | // -- if T is a (possibly cv-qualified) non-union class type |
| 3424 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3425 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3426 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3427 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 3428 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3429 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3430 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3431 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3432 | } |
| 3433 | } |
| 3434 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3435 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3438 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3439 | static void TryDefaultInitialization(Sema &S, |
| 3440 | const InitializedEntity &Entity, |
| 3441 | const InitializationKind &Kind, |
| 3442 | InitializationSequence &Sequence) { |
| 3443 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3444 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3445 | // C++ [dcl.init]p6: |
| 3446 | // To default-initialize an object of type T means: |
| 3447 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3448 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3449 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3450 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3451 | // constructor for T is called (and the initialization is ill-formed if |
| 3452 | // T has no accessible default constructor); |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 3453 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3454 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3455 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3456 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3457 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3458 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3459 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3460 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3461 | // 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] | 3462 | // default constructor. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3463 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3464 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3465 | return; |
| 3466 | } |
| 3467 | |
| 3468 | // If the destination type has a lifetime property, zero-initialize it. |
| 3469 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3470 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3471 | return; |
| 3472 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3475 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3476 | /// which enumerates all conversion functions and performs overload resolution |
| 3477 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3478 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3479 | const InitializedEntity &Entity, |
| 3480 | const InitializationKind &Kind, |
| 3481 | Expr *Initializer, |
| 3482 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3483 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3484 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3485 | QualType SourceType = Initializer->getType(); |
| 3486 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3487 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3488 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3489 | // Build the candidate set directly in the initialization sequence |
| 3490 | // structure, so that it will persist if we fail. |
| 3491 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3492 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3493 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3494 | // Determine whether we are allowed to call explicit constructors or |
| 3495 | // explicit conversion operators. |
| 3496 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3497 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3498 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3499 | // The type we're converting to is a class type. Enumerate its constructors |
| 3500 | // to see if there is a suitable conversion. |
| 3501 | CXXRecordDecl *DestRecordDecl |
| 3502 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3503 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3504 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3505 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3506 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3507 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3508 | Con != ConEnd; ++Con) { |
| 3509 | NamedDecl *D = *Con; |
| 3510 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3511 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3512 | // Find the constructor (which may be a template). |
| 3513 | CXXConstructorDecl *Constructor = 0; |
| 3514 | FunctionTemplateDecl *ConstructorTmpl |
| 3515 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3516 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3517 | Constructor = cast<CXXConstructorDecl>( |
| 3518 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3519 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3520 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3521 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3522 | if (!Constructor->isInvalidDecl() && |
| 3523 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3524 | if (ConstructorTmpl) |
| 3525 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3526 | /*ExplicitArgs*/ 0, |
| 3527 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3528 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3529 | else |
| 3530 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3531 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3532 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3533 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3534 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3535 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3536 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3537 | |
| 3538 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3539 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3540 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3541 | // The type we're converting from is a class type, enumerate its conversion |
| 3542 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3543 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3544 | // We can only enumerate the conversion functions for a complete type; if |
| 3545 | // the type isn't complete, simply skip this step. |
| 3546 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3547 | CXXRecordDecl *SourceRecordDecl |
| 3548 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3549 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3550 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3551 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3552 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3553 | E = Conversions->end(); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3554 | I != E; ++I) { |
| 3555 | NamedDecl *D = *I; |
| 3556 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3557 | if (isa<UsingShadowDecl>(D)) |
| 3558 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3559 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3560 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3561 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3562 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3563 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3564 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3565 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3566 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3567 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3568 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3569 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3570 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3571 | CandidateSet); |
| 3572 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3573 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3574 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3575 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3576 | } |
| 3577 | } |
| 3578 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3579 | |
| 3580 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3581 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3582 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3583 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3584 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3585 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3586 | Result); |
| 3587 | return; |
| 3588 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3589 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3590 | FunctionDecl *Function = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3591 | S.MarkDeclarationReferenced(DeclLoc, Function); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3592 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3593 | if (isa<CXXConstructorDecl>(Function)) { |
| 3594 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3595 | // subsumed by the initialization. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3596 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3597 | return; |
| 3598 | } |
| 3599 | |
| 3600 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3601 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3602 | if (ConvType->getAs<RecordType>()) { |
| 3603 | // If we're converting to a class type, there may be an copy if |
| 3604 | // the resulting temporary object (possible to create an object of |
| 3605 | // a base class type). That copy is not a separate conversion, so |
| 3606 | // we just make a note of the actual destination type (possibly a |
| 3607 | // base class of the type returned by the conversion function) and |
| 3608 | // let the user-defined conversion step handle the conversion. |
| 3609 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType); |
| 3610 | return; |
| 3611 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3612 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3613 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3614 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3615 | // If the conversion following the call to the conversion function |
| 3616 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3617 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3618 | Best->FinalConversion.Third) { |
| 3619 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3620 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3621 | ICS.Standard = Best->FinalConversion; |
| 3622 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3623 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3626 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3627 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3628 | |
| 3629 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3630 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3631 | bool isAddressOf) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3632 | // Skip parens. |
| 3633 | e = e->IgnoreParens(); |
| 3634 | |
| 3635 | // Skip address-of nodes. |
| 3636 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3637 | if (op->getOpcode() == UO_AddrOf) |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3638 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3639 | |
| 3640 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3641 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3642 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3643 | case CK_Dependent: |
| 3644 | case CK_BitCast: |
| 3645 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3646 | case CK_NoOp: |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3647 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3648 | |
| 3649 | case CK_ArrayToPointerDecay: |
| 3650 | return IIK_nonscalar; |
| 3651 | |
| 3652 | case CK_NullToPointer: |
| 3653 | return IIK_okay; |
| 3654 | |
| 3655 | default: |
| 3656 | break; |
| 3657 | } |
| 3658 | |
| 3659 | // If we have a declaration reference, it had better be a local variable. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3660 | } else if (isa<DeclRefExpr>(e) || isa<BlockDeclRefExpr>(e)) { |
| 3661 | if (!isAddressOf) return IIK_nonlocal; |
| 3662 | |
| 3663 | VarDecl *var; |
| 3664 | if (isa<DeclRefExpr>(e)) { |
| 3665 | var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3666 | if (!var) return IIK_nonlocal; |
| 3667 | } else { |
| 3668 | var = cast<BlockDeclRefExpr>(e)->getDecl(); |
| 3669 | } |
| 3670 | |
| 3671 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3672 | |
| 3673 | // If we have a conditional operator, check both sides. |
| 3674 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3675 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3676 | return iik; |
| 3677 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3678 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3679 | |
| 3680 | // These are never scalar. |
| 3681 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3682 | return IIK_nonscalar; |
| 3683 | |
| 3684 | // Otherwise, it needs to be a null pointer constant. |
| 3685 | } else { |
| 3686 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3687 | ? IIK_okay : IIK_nonlocal); |
| 3688 | } |
| 3689 | |
| 3690 | return IIK_nonlocal; |
| 3691 | } |
| 3692 | |
| 3693 | /// Check whether the given expression is a valid operand for an |
| 3694 | /// indirect copy/restore. |
| 3695 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3696 | assert(src->isRValue()); |
| 3697 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3698 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3699 | if (iik == IIK_okay) return; |
| 3700 | |
| 3701 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3702 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3703 | << src->getSourceRange(); |
| 3704 | } |
| 3705 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3706 | /// \brief Determine whether we have compatible array types for the |
| 3707 | /// purposes of GNU by-copy array initialization. |
| 3708 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3709 | const ArrayType *Dest, |
| 3710 | const ArrayType *Source) { |
| 3711 | // If the source and destination array types are equivalent, we're |
| 3712 | // done. |
| 3713 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3714 | return true; |
| 3715 | |
| 3716 | // Make sure that the element types are the same. |
| 3717 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3718 | return false; |
| 3719 | |
| 3720 | // The only mismatch we allow is when the destination is an |
| 3721 | // incomplete array type and the source is a constant array type. |
| 3722 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3723 | } |
| 3724 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3725 | static bool tryObjCWritebackConversion(Sema &S, |
| 3726 | InitializationSequence &Sequence, |
| 3727 | const InitializedEntity &Entity, |
| 3728 | Expr *Initializer) { |
| 3729 | bool ArrayDecay = false; |
| 3730 | QualType ArgType = Initializer->getType(); |
| 3731 | QualType ArgPointee; |
| 3732 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3733 | ArrayDecay = true; |
| 3734 | ArgPointee = ArgArrayType->getElementType(); |
| 3735 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3736 | } |
| 3737 | |
| 3738 | // Handle write-back conversion. |
| 3739 | QualType ConvertedArgType; |
| 3740 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3741 | ConvertedArgType)) |
| 3742 | return false; |
| 3743 | |
| 3744 | // We should copy unless we're passing to an argument explicitly |
| 3745 | // marked 'out'. |
| 3746 | bool ShouldCopy = true; |
| 3747 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3748 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3749 | |
| 3750 | // Do we need an lvalue conversion? |
| 3751 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3752 | ImplicitConversionSequence ICS; |
| 3753 | ICS.setStandard(); |
| 3754 | ICS.Standard.setAsIdentityConversion(); |
| 3755 | |
| 3756 | QualType ResultType; |
| 3757 | if (ArrayDecay) { |
| 3758 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3759 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3760 | } else { |
| 3761 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3762 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3763 | } |
| 3764 | |
| 3765 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3766 | } |
| 3767 | |
| 3768 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3769 | return true; |
| 3770 | } |
| 3771 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3772 | InitializationSequence::InitializationSequence(Sema &S, |
| 3773 | const InitializedEntity &Entity, |
| 3774 | const InitializationKind &Kind, |
| 3775 | Expr **Args, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3776 | unsigned NumArgs) |
| 3777 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3778 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3779 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3780 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3781 | // The semantics of initializers are as follows. The destination type is |
| 3782 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3783 | // 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] | 3784 | // 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] | 3785 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3786 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3787 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3788 | if (DestType->isDependentType() || |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3789 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3790 | SequenceKind = DependentSequence; |
| 3791 | return; |
| 3792 | } |
| 3793 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 3794 | // Almost everything is a normal sequence. |
| 3795 | setSequenceKind(NormalSequence); |
| 3796 | |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3797 | for (unsigned I = 0; I != NumArgs; ++I) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3798 | if (Args[I]->getObjectKind() == OK_ObjCProperty) { |
| 3799 | ExprResult Result = S.ConvertPropertyForRValue(Args[I]); |
| 3800 | if (Result.isInvalid()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3801 | SetFailed(FK_ConversionFromPropertyFailed); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3802 | return; |
| 3803 | } |
| 3804 | Args[I] = Result.take(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3805 | } else if (const BuiltinType *PlaceholderTy |
| 3806 | = Args[I]->getType()->getAsPlaceholderType()) { |
| 3807 | // FIXME: should we be doing this here? |
| 3808 | if (PlaceholderTy->getKind() != BuiltinType::Overload) { |
| 3809 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 3810 | if (result.isInvalid()) { |
| 3811 | SetFailed(FK_PlaceholderType); |
| 3812 | return; |
| 3813 | } |
| 3814 | Args[I] = result.take(); |
| 3815 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3816 | } |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 3817 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3818 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3819 | QualType SourceType; |
| 3820 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3821 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3822 | Initializer = Args[0]; |
| 3823 | if (!isa<InitListExpr>(Initializer)) |
| 3824 | SourceType = Initializer->getType(); |
| 3825 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3826 | |
| 3827 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3828 | // list-initialized (8.5.4). |
| 3829 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3830 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3831 | return; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3832 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3833 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3834 | // - If the destination type is a reference type, see 8.5.3. |
| 3835 | if (DestType->isReferenceType()) { |
| 3836 | // C++0x [dcl.init.ref]p1: |
| 3837 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 3838 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 3839 | // by an object that can be converted into a T. |
| 3840 | // (Therefore, multiple arguments are not permitted.) |
| 3841 | if (NumArgs != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3842 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3843 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3844 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3845 | return; |
| 3846 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3847 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3848 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3849 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 3850 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3851 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3852 | return; |
| 3853 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3854 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3855 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 3856 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3857 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3858 | return; |
| 3859 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3860 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3861 | // - If the destination type is an array of characters, an array of |
| 3862 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 3863 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3864 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3865 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3866 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 3867 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3868 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 3869 | return; |
| 3870 | } |
| 3871 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3872 | // Note: as an GNU C extension, we allow initialization of an |
| 3873 | // array from a compound literal that creates an array of the same |
| 3874 | // type, so long as the initializer has no side effects. |
| 3875 | if (!S.getLangOptions().CPlusPlus && Initializer && |
| 3876 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 3877 | Initializer->getType()->isArrayType()) { |
| 3878 | const ArrayType *SourceAT |
| 3879 | = Context.getAsArrayType(Initializer->getType()); |
| 3880 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3881 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3882 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3883 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3884 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3885 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3886 | } |
| 3887 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3888 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3889 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3890 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3891 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3892 | return; |
| 3893 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3894 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3895 | // Determine whether we should consider writeback conversions for |
| 3896 | // Objective-C ARC. |
| 3897 | bool allowObjCWritebackConversion = S.getLangOptions().ObjCAutoRefCount && |
| 3898 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 3899 | |
| 3900 | // We're at the end of the line for C: it's either a write-back conversion |
| 3901 | // or it's a C assignment. There's no need to check anything else. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3902 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3903 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 3904 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3905 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3906 | return; |
| 3907 | } |
| 3908 | |
| 3909 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3910 | AddCAssignmentStep(DestType); |
| 3911 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3912 | return; |
| 3913 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3914 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3915 | assert(S.getLangOptions().CPlusPlus); |
| 3916 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3917 | // - If the destination type is a (possibly cv-qualified) class type: |
| 3918 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3919 | // - If the initialization is direct-initialization, or if it is |
| 3920 | // copy-initialization where the cv-unqualified version of the |
| 3921 | // 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] | 3922 | // class of the destination, constructors are considered. [...] |
| 3923 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 3924 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 3925 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 3926 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3927 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3928 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3929 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3930 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3931 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3932 | // used) to a derived class thereof are enumerated as described in |
| 3933 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 3934 | // (13.3). |
| 3935 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3936 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3937 | return; |
| 3938 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3939 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3940 | if (NumArgs > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3941 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3942 | return; |
| 3943 | } |
| 3944 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3945 | |
| 3946 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3947 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3948 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3949 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 3950 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3951 | return; |
| 3952 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3953 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3954 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3955 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3956 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3957 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3958 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3959 | |
| 3960 | ImplicitConversionSequence ICS |
| 3961 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 3962 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3963 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3964 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3965 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3966 | allowObjCWritebackConversion); |
| 3967 | |
| 3968 | if (ICS.isStandard() && |
| 3969 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 3970 | // Objective-C ARC writeback conversion. |
| 3971 | |
| 3972 | // We should copy unless we're passing to an argument explicitly |
| 3973 | // marked 'out'. |
| 3974 | bool ShouldCopy = true; |
| 3975 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3976 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3977 | |
| 3978 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 3979 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 3980 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 3981 | ImplicitConversionSequence LvalueICS; |
| 3982 | LvalueICS.setStandard(); |
| 3983 | LvalueICS.Standard.setAsIdentityConversion(); |
| 3984 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 3985 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3986 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3987 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3988 | |
| 3989 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3990 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 3991 | DeclAccessPair dap; |
| 3992 | if (Initializer->getType() == Context.OverloadTy && |
| 3993 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 3994 | , DestType, false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3995 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 3996 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3997 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3998 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3999 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4000 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4001 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4002 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4006 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4007 | StepEnd = Steps.end(); |
| 4008 | Step != StepEnd; ++Step) |
| 4009 | Step->Destroy(); |
| 4010 | } |
| 4011 | |
| 4012 | //===----------------------------------------------------------------------===// |
| 4013 | // Perform initialization |
| 4014 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4015 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4016 | getAssignmentAction(const InitializedEntity &Entity) { |
| 4017 | switch(Entity.getKind()) { |
| 4018 | case InitializedEntity::EK_Variable: |
| 4019 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4020 | case InitializedEntity::EK_Exception: |
| 4021 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4022 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4023 | return Sema::AA_Initializing; |
| 4024 | |
| 4025 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4026 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4027 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4028 | return Sema::AA_Sending; |
| 4029 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4030 | return Sema::AA_Passing; |
| 4031 | |
| 4032 | case InitializedEntity::EK_Result: |
| 4033 | return Sema::AA_Returning; |
| 4034 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4035 | case InitializedEntity::EK_Temporary: |
| 4036 | // FIXME: Can we tell apart casting vs. converting? |
| 4037 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4038 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4039 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4040 | case InitializedEntity::EK_ArrayElement: |
| 4041 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4042 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4043 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4044 | return Sema::AA_Initializing; |
| 4045 | } |
| 4046 | |
| 4047 | return Sema::AA_Converting; |
| 4048 | } |
| 4049 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4050 | /// \brief Whether we should binding a created object as a temporary when |
| 4051 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4052 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4053 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4054 | case InitializedEntity::EK_ArrayElement: |
| 4055 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4056 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4057 | case InitializedEntity::EK_New: |
| 4058 | case InitializedEntity::EK_Variable: |
| 4059 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4060 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4061 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4062 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4063 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4064 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4065 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4066 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4067 | case InitializedEntity::EK_Parameter: |
| 4068 | case InitializedEntity::EK_Temporary: |
| 4069 | return true; |
| 4070 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4071 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4072 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4073 | } |
| 4074 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4075 | /// \brief Whether the given entity, when initialized with an object |
| 4076 | /// created for that initialization, requires destruction. |
| 4077 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4078 | switch (Entity.getKind()) { |
| 4079 | case InitializedEntity::EK_Member: |
| 4080 | case InitializedEntity::EK_Result: |
| 4081 | case InitializedEntity::EK_New: |
| 4082 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4083 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4084 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4085 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4086 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4087 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4088 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4089 | case InitializedEntity::EK_Variable: |
| 4090 | case InitializedEntity::EK_Parameter: |
| 4091 | case InitializedEntity::EK_Temporary: |
| 4092 | case InitializedEntity::EK_ArrayElement: |
| 4093 | case InitializedEntity::EK_Exception: |
| 4094 | return true; |
| 4095 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4096 | |
| 4097 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4098 | } |
| 4099 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4100 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4101 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4102 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4103 | OverloadCandidateSet &CandidateSet, |
| 4104 | CXXRecordDecl *Class, |
| 4105 | Expr *CurInitExpr) { |
| 4106 | DeclContext::lookup_iterator Con, ConEnd; |
| 4107 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
| 4108 | Con != ConEnd; ++Con) { |
| 4109 | CXXConstructorDecl *Constructor = 0; |
| 4110 | |
| 4111 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
| 4112 | // Handle copy/moveconstructors, only. |
| 4113 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4114 | !Constructor->isCopyOrMoveConstructor() || |
| 4115 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4116 | continue; |
| 4117 | |
| 4118 | DeclAccessPair FoundDecl |
| 4119 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4120 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 4121 | &CurInitExpr, 1, CandidateSet); |
| 4122 | continue; |
| 4123 | } |
| 4124 | |
| 4125 | // Handle constructor templates. |
| 4126 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 4127 | if (ConstructorTmpl->isInvalidDecl()) |
| 4128 | continue; |
| 4129 | |
| 4130 | Constructor = cast<CXXConstructorDecl>( |
| 4131 | ConstructorTmpl->getTemplatedDecl()); |
| 4132 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4133 | continue; |
| 4134 | |
| 4135 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4136 | // candidates? |
| 4137 | DeclAccessPair FoundDecl |
| 4138 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4139 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 4140 | &CurInitExpr, 1, CandidateSet, true); |
| 4141 | } |
| 4142 | } |
| 4143 | |
| 4144 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4145 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4146 | Expr *Initializer) { |
| 4147 | switch (Entity.getKind()) { |
| 4148 | case InitializedEntity::EK_Result: |
| 4149 | return Entity.getReturnLoc(); |
| 4150 | |
| 4151 | case InitializedEntity::EK_Exception: |
| 4152 | return Entity.getThrowLoc(); |
| 4153 | |
| 4154 | case InitializedEntity::EK_Variable: |
| 4155 | return Entity.getDecl()->getLocation(); |
| 4156 | |
| 4157 | case InitializedEntity::EK_ArrayElement: |
| 4158 | case InitializedEntity::EK_Member: |
| 4159 | case InitializedEntity::EK_Parameter: |
| 4160 | case InitializedEntity::EK_Temporary: |
| 4161 | case InitializedEntity::EK_New: |
| 4162 | case InitializedEntity::EK_Base: |
| 4163 | case InitializedEntity::EK_Delegating: |
| 4164 | case InitializedEntity::EK_VectorElement: |
| 4165 | case InitializedEntity::EK_ComplexElement: |
| 4166 | case InitializedEntity::EK_BlockElement: |
| 4167 | return Initializer->getLocStart(); |
| 4168 | } |
| 4169 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4170 | } |
| 4171 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4172 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4173 | /// provided by the given initializer by calling the appropriate copy |
| 4174 | /// constructor. |
| 4175 | /// |
| 4176 | /// \param S The Sema object used for type-checking. |
| 4177 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4178 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4179 | /// the type of the initializer expression or a superclass thereof. |
| 4180 | /// |
| 4181 | /// \param Enter The entity being initialized. |
| 4182 | /// |
| 4183 | /// \param CurInit The initializer expression. |
| 4184 | /// |
| 4185 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4186 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4187 | /// an rvalue. |
| 4188 | /// |
| 4189 | /// \returns An expression that copies the initializer expression into |
| 4190 | /// a temporary object, or an error expression if a copy could not be |
| 4191 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4192 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4193 | QualType T, |
| 4194 | const InitializedEntity &Entity, |
| 4195 | ExprResult CurInit, |
| 4196 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4197 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4198 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4199 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4200 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4201 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4202 | if (!Class) |
| 4203 | return move(CurInit); |
| 4204 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4205 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4206 | // When certain criteria are met, an implementation is allowed to |
| 4207 | // omit the copy/move construction of a class object, even if the |
| 4208 | // copy/move constructor and/or destructor for the object have |
| 4209 | // side effects. [...] |
| 4210 | // - when a temporary class object that has not been bound to a |
| 4211 | // reference (12.2) would be copied/moved to a class object |
| 4212 | // with the same cv-unqualified type, the copy/move operation |
| 4213 | // can be omitted by constructing the temporary object |
| 4214 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4215 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4216 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4217 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4218 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4219 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4220 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4221 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4222 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4223 | // Make sure that the type we are copying is complete. |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4224 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 4225 | return move(CurInit); |
| 4226 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4227 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4228 | // Only consider constructors and constructor templates. Per |
| 4229 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4230 | // is direct-initialization. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4231 | OverloadCandidateSet CandidateSet(Loc); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4232 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4233 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4234 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4235 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4236 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4237 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4238 | case OR_Success: |
| 4239 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4240 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4241 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4242 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4243 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4244 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4245 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4246 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4247 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4248 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4249 | return ExprError(); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4250 | return move(CurInit); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4251 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4252 | case OR_Ambiguous: |
| 4253 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4254 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4255 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4256 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4257 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4258 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4259 | case OR_Deleted: |
| 4260 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4261 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4262 | << CurInitExpr->getSourceRange(); |
| 4263 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4264 | << 1 << Best->Function->isDeleted(); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4265 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4268 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4269 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4270 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4271 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4272 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4273 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4274 | |
| 4275 | if (IsExtraneousCopy) { |
| 4276 | // If this is a totally extraneous copy for C++03 reference |
| 4277 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4278 | // expression. We don't generate an (elided) copy operation here |
| 4279 | // because doing so would require us to pass down a flag to avoid |
| 4280 | // infinite recursion, where each step adds another extraneous, |
| 4281 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4282 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4283 | // Instantiate the default arguments of any extra parameters in |
| 4284 | // the selected copy constructor, as if we were going to create a |
| 4285 | // proper call to the copy constructor. |
| 4286 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4287 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4288 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 4289 | S.PDiag(diag::err_call_incomplete_argument))) |
| 4290 | break; |
| 4291 | |
| 4292 | // Build the default argument expression; we don't actually care |
| 4293 | // if this succeeds or not, because this routine will complain |
| 4294 | // if there was a problem. |
| 4295 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4296 | } |
| 4297 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4298 | return S.Owned(CurInitExpr); |
| 4299 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4300 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4301 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 4302 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4303 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4304 | // constructor call (we might have derived-to-base conversions, or |
| 4305 | // the copy constructor may have default arguments). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4306 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4307 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4308 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4309 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4310 | // Actually perform the constructor call. |
| 4311 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4312 | move_arg(ConstructorArgs), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4313 | HadMultipleCandidates, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4314 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4315 | CXXConstructExpr::CK_Complete, |
| 4316 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4317 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4318 | // If we're supposed to bind temporaries, do so. |
| 4319 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4320 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4321 | return move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4322 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4323 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4324 | /// \brief Check whether elidable copy construction for binding a reference to |
| 4325 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 4326 | /// -Wc++98-compat. |
| 4327 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4328 | const InitializedEntity &Entity, |
| 4329 | Expr *CurInitExpr) { |
| 4330 | assert(S.getLangOptions().CPlusPlus0x); |
| 4331 | |
| 4332 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 4333 | if (!Record) |
| 4334 | return; |
| 4335 | |
| 4336 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 4337 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 4338 | == DiagnosticsEngine::Ignored) |
| 4339 | return; |
| 4340 | |
| 4341 | // Find constructors which would have been considered. |
| 4342 | OverloadCandidateSet CandidateSet(Loc); |
| 4343 | LookupCopyAndMoveConstructors( |
| 4344 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 4345 | |
| 4346 | // Perform overload resolution. |
| 4347 | OverloadCandidateSet::iterator Best; |
| 4348 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 4349 | |
| 4350 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 4351 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 4352 | << CurInitExpr->getSourceRange(); |
| 4353 | |
| 4354 | switch (OR) { |
| 4355 | case OR_Success: |
| 4356 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
| 4357 | Best->FoundDecl.getAccess(), Diag); |
| 4358 | // FIXME: Check default arguments as far as that's possible. |
| 4359 | break; |
| 4360 | |
| 4361 | case OR_No_Viable_Function: |
| 4362 | S.Diag(Loc, Diag); |
| 4363 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
| 4364 | break; |
| 4365 | |
| 4366 | case OR_Ambiguous: |
| 4367 | S.Diag(Loc, Diag); |
| 4368 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
| 4369 | break; |
| 4370 | |
| 4371 | case OR_Deleted: |
| 4372 | S.Diag(Loc, Diag); |
| 4373 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 4374 | << 1 << Best->Function->isDeleted(); |
| 4375 | break; |
| 4376 | } |
| 4377 | } |
| 4378 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4379 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4380 | const InitializedEntity &Entity) { |
| 4381 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4382 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4383 | return; |
| 4384 | |
| 4385 | if (Entity.getDecl()->getDeclName()) |
| 4386 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4387 | << Entity.getDecl()->getDeclName(); |
| 4388 | else |
| 4389 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4390 | } |
| 4391 | } |
| 4392 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4393 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4394 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4395 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4396 | } |
| 4397 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4398 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4399 | InitializationSequence::Perform(Sema &S, |
| 4400 | const InitializedEntity &Entity, |
| 4401 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4402 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4403 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4404 | if (Failed()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4405 | unsigned NumArgs = Args.size(); |
| 4406 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4407 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4408 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4409 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4410 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4411 | // If the declaration is a non-dependent, incomplete array type |
| 4412 | // that has an initializer, then its type will be completed once |
| 4413 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4414 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4415 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4416 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4417 | if (const IncompleteArrayType *ArrayT |
| 4418 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4419 | // FIXME: We don't currently have the ability to accurately |
| 4420 | // compute the length of an initializer list without |
| 4421 | // performing full type-checking of the initializer list |
| 4422 | // (since we have to determine where braces are implicitly |
| 4423 | // introduced and such). So, we fall back to making the array |
| 4424 | // type a dependently-sized array type with no specified |
| 4425 | // bound. |
| 4426 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 4427 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4428 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4429 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4430 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4431 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4432 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4433 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4434 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4435 | Brackets = ArrayLoc->getBracketsRange(); |
| 4436 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | *ResultType |
| 4440 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4441 | /*NumElts=*/0, |
| 4442 | ArrayT->getSizeModifier(), |
| 4443 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4444 | Brackets); |
| 4445 | } |
| 4446 | |
| 4447 | } |
| 4448 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4449 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
| 4450 | Kind.isExplicitCast()); |
| 4451 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4452 | } |
| 4453 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4454 | // No steps means no initialization. |
| 4455 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4456 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4457 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4458 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4459 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4460 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4461 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4462 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4463 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4464 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4465 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4466 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4467 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4468 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4469 | // grab the only argument out the Args and place it into the "current" |
| 4470 | // initializer. |
| 4471 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4472 | case SK_ResolveAddressOfOverloadedFunction: |
| 4473 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4474 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4475 | case SK_CastDerivedToBaseLValue: |
| 4476 | case SK_BindReference: |
| 4477 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4478 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4479 | case SK_UserConversion: |
| 4480 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4481 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4482 | case SK_QualificationConversionRValue: |
| 4483 | case SK_ConversionSequence: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 4484 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4485 | case SK_ListInitialization: |
| 4486 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4487 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4488 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4489 | case SK_ArrayInit: |
| 4490 | case SK_PassByIndirectCopyRestore: |
| 4491 | case SK_PassByIndirectRestore: |
| 4492 | case SK_ProduceObjCObject: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4493 | assert(Args.size() == 1); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4494 | CurInit = Args.get()[0]; |
| 4495 | if (!CurInit.get()) return ExprError(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4496 | |
| 4497 | // Read from a property when initializing something with it. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4498 | if (CurInit.get()->getObjectKind() == OK_ObjCProperty) { |
| 4499 | CurInit = S.ConvertPropertyForRValue(CurInit.take()); |
| 4500 | if (CurInit.isInvalid()) |
| 4501 | return ExprError(); |
| 4502 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4503 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4504 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4505 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4506 | case SK_ConstructorInitialization: |
| 4507 | case SK_ZeroInitialization: |
| 4508 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4509 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4510 | |
| 4511 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4512 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4513 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4514 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4515 | Step != StepEnd; ++Step) { |
| 4516 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4517 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4518 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4519 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4520 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4521 | switch (Step->Kind) { |
| 4522 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4523 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4524 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4525 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4526 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4527 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4528 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4529 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4530 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4531 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4532 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4533 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4534 | case SK_CastDerivedToBaseLValue: { |
| 4535 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4536 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4537 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4538 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4539 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4540 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4541 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4542 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4543 | CurInit.get()->getLocStart(), |
| 4544 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4545 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4546 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4547 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4548 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4549 | QualType T = SourceType; |
| 4550 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4551 | T = Pointer->getPointeeType(); |
| 4552 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4553 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4554 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4555 | } |
| 4556 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4557 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4558 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4559 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4560 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4561 | VK_XValue : |
| 4562 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4563 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4564 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4565 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4566 | CurInit.get(), |
| 4567 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4568 | break; |
| 4569 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4570 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4571 | case SK_BindReference: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4572 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4573 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4574 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4575 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4576 | << BitField->getDeclName() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4577 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4578 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4579 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4580 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4581 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4582 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 4583 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4584 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 4585 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4586 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4587 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4588 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4589 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4590 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4591 | // Reference binding does not have any corresponding ASTs. |
| 4592 | |
| 4593 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4594 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4595 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4596 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4597 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4598 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4599 | case SK_BindReferenceToTemporary: |
| 4600 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4601 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4602 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4603 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4604 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 4605 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 4606 | Entity.getType().getNonReferenceType(), |
| 4607 | CurInit.get(), |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4608 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 4609 | |
| 4610 | // If we're binding to an Objective-C object that has lifetime, we |
| 4611 | // need cleanups. |
| 4612 | if (S.getLangOptions().ObjCAutoRefCount && |
| 4613 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 4614 | S.ExprNeedsCleanups = true; |
| 4615 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4616 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4617 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4618 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4619 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4620 | /*IsExtraneousCopy=*/true); |
| 4621 | break; |
| 4622 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4623 | case SK_UserConversion: { |
| 4624 | // We have a user-defined conversion that invokes either a constructor |
| 4625 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4626 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4627 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4628 | FunctionDecl *Fn = Step->Function.Function; |
| 4629 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4630 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4631 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4632 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4633 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4634 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4635 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4636 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4637 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4638 | // Determine the arguments required to actually perform the constructor |
| 4639 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4640 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4641 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4642 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4643 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4644 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4645 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4646 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4647 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4648 | move_arg(ConstructorArgs), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4649 | HadMultipleCandidates, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4650 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4651 | CXXConstructExpr::CK_Complete, |
| 4652 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4653 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4654 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4655 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4656 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4657 | FoundFn.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4658 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4659 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4660 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4661 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 4662 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 4663 | S.IsDerivedFrom(SourceType, Class)) |
| 4664 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4665 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4666 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4667 | } else { |
| 4668 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4669 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4670 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4671 | FoundFn); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4672 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4673 | |
| 4674 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4675 | // derived-to-base conversion? I believe the answer is "no", because |
| 4676 | // 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] | 4677 | ExprResult CurInitExprRes = |
| 4678 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 4679 | FoundFn, Conversion); |
| 4680 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4681 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4682 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4683 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4684 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4685 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 4686 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4687 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4688 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4689 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4690 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4691 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4692 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4693 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4694 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4695 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4696 | if (RequiresCopy || shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4697 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4698 | else if (CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4699 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4700 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4701 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 4702 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4703 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4704 | S.PDiag(diag::err_access_dtor_temp) << T); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4705 | S.MarkDeclarationReferenced(CurInit.get()->getLocStart(), Destructor); |
| 4706 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4707 | } |
| 4708 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4709 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4710 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4711 | CurInit.get()->getType(), |
| 4712 | CastKind, CurInit.get(), 0, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 4713 | CurInit.get()->getValueKind())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4715 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4716 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 4717 | move(CurInit), /*IsExtraneousCopy=*/false); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4718 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4719 | break; |
| 4720 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4721 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4722 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4723 | case SK_QualificationConversionXValue: |
| 4724 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4725 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4726 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4727 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4728 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4729 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4730 | VK_XValue : |
| 4731 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4732 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4733 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4736 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4737 | Sema::CheckedConversionKind CCK |
| 4738 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 4739 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
| 4740 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
| 4741 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4742 | ExprResult CurInitExprRes = |
| 4743 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4744 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4745 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4746 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4747 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4748 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 4749 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4750 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4751 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4752 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4753 | QualType Ty = Step->Type; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4754 | InitListChecker PerformInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 4755 | ResultType ? *ResultType : Ty, /*VerifyOnly=*/false, |
| 4756 | Kind.getKind() != InitializationKind::IK_Direct || |
| 4757 | !S.getLangOptions().CPlusPlus0x); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4758 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4759 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4760 | |
| 4761 | CurInit.release(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 4762 | CurInit = S.Owned(PerformInitList.getFullyStructuredList()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4763 | break; |
| 4764 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4765 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 4766 | case SK_ListConstructorCall: |
| 4767 | assert(false && "List constructor calls not yet supported."); |
| 4768 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4769 | case SK_ConstructorInitialization: { |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4770 | unsigned NumArgs = Args.size(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4771 | CXXConstructorDecl *Constructor |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4772 | = cast<CXXConstructorDecl>(Step->Function.Function); |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4773 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4774 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4775 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4776 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Fariborz Jahanian | 0a2eb56 | 2010-07-21 18:40:47 +0000 | [diff] [blame] | 4777 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4778 | ? Kind.getEqualLoc() |
| 4779 | : Kind.getLocation(); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4780 | |
| 4781 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4782 | // Force even a trivial, implicit default constructor to be |
| 4783 | // semantically checked. We do this explicitly because we don't build |
| 4784 | // the definition for completely trivial constructors. |
| 4785 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4786 | assert(ClassDecl && "No parent class for constructor."); |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 4787 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 4788 | ClassDecl->hasTrivialDefaultConstructor() && |
| 4789 | !Constructor->isUsed(false)) |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4790 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4791 | } |
| 4792 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4793 | // Determine the arguments required to actually perform the constructor |
| 4794 | // call. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4795 | if (S.CompleteConstructorCall(Constructor, move(Args), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4796 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4797 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4798 | |
| 4799 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4800 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 4801 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4802 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4803 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 4804 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4805 | unsigned NumExprs = ConstructorArgs.size(); |
| 4806 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
Fariborz Jahanian | 10f8e31 | 2010-07-21 18:31:47 +0000 | [diff] [blame] | 4807 | S.MarkDeclarationReferenced(Loc, Constructor); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 4808 | S.DiagnoseUseOfDecl(Constructor, Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4809 | |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4810 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4811 | if (!TSInfo) |
| 4812 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4813 | |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4814 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4815 | Constructor, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4816 | TSInfo, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4817 | Exprs, |
Douglas Gregor | 91be6f5 | 2010-03-02 17:18:33 +0000 | [diff] [blame] | 4818 | NumExprs, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4819 | Kind.getParenRange(), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4820 | HadMultipleCandidates, |
Douglas Gregor | 1c63b9c | 2010-04-27 20:36:09 +0000 | [diff] [blame] | 4821 | ConstructorInitRequiresZeroInit)); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4822 | } else { |
| 4823 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4824 | CXXConstructExpr::CK_Complete; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4825 | |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4826 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4827 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4828 | CXXConstructExpr::CK_VirtualBase : |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4829 | CXXConstructExpr::CK_NonVirtualBase; |
Sean Hunt | d49bd55 | 2011-05-03 20:19:28 +0000 | [diff] [blame] | 4830 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4831 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4832 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4833 | |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4834 | // Only get the parenthesis range if it is a direct construction. |
| 4835 | SourceRange parenRange = |
| 4836 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4837 | Kind.getParenRange() : SourceRange(); |
| 4838 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4839 | // If the entity allows NRVO, mark the construction as elidable |
| 4840 | // unconditionally. |
| 4841 | if (Entity.allowsNRVO()) |
| 4842 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4843 | Constructor, /*Elidable=*/true, |
| 4844 | move_arg(ConstructorArgs), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4845 | HadMultipleCandidates, |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4846 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4847 | ConstructKind, |
| 4848 | parenRange); |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4849 | else |
| 4850 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4851 | Constructor, |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4852 | move_arg(ConstructorArgs), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4853 | HadMultipleCandidates, |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4854 | ConstructorInitRequiresZeroInit, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4855 | ConstructKind, |
| 4856 | parenRange); |
Anders Carlsson | 72e96fd | 2010-05-02 22:54:08 +0000 | [diff] [blame] | 4857 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4858 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4859 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4860 | |
| 4861 | // Only check access if all of that succeeded. |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4862 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4863 | Step->Function.FoundDecl.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4864 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Loc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4865 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4866 | if (shouldBindAsTemporary(Entity)) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4867 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4868 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 4869 | break; |
| 4870 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4871 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4872 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4873 | step_iterator NextStep = Step; |
| 4874 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4875 | if (NextStep != StepEnd && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4876 | NextStep->Kind == SK_ConstructorInitialization) { |
| 4877 | // The need for zero-initialization is recorded directly into |
| 4878 | // the call to the object's constructor within the next step. |
| 4879 | ConstructorInitRequiresZeroInit = true; |
| 4880 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 4881 | S.getLangOptions().CPlusPlus && |
| 4882 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4883 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4884 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4885 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 4886 | Kind.getRange().getBegin()); |
| 4887 | |
| 4888 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 4889 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 4890 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4891 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4892 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4893 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4894 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4895 | break; |
| 4896 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4897 | |
| 4898 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4899 | QualType SourceType = CurInit.get()->getType(); |
| 4900 | ExprResult Result = move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4901 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4902 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 4903 | if (Result.isInvalid()) |
| 4904 | return ExprError(); |
| 4905 | CurInit = move(Result); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4906 | |
| 4907 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4908 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4909 | if (ConvTy != Sema::Compatible && |
| 4910 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4911 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4912 | == Sema::Compatible) |
| 4913 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4914 | if (CurInitExprRes.isInvalid()) |
| 4915 | return ExprError(); |
| 4916 | CurInit = move(CurInitExprRes); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4917 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4918 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4919 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 4920 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4921 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4922 | getAssignmentAction(Entity), |
| 4923 | &Complained)) { |
| 4924 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4925 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4926 | } else if (Complained) |
| 4927 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4928 | break; |
| 4929 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4930 | |
| 4931 | case SK_StringInit: { |
| 4932 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4933 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 4934 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4935 | break; |
| 4936 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4937 | |
| 4938 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4939 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4940 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4941 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 4942 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4943 | |
| 4944 | case SK_ArrayInit: |
| 4945 | // Okay: we checked everything before creating this step. Note that |
| 4946 | // this is a GNU extension. |
| 4947 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4948 | << Step->Type << CurInit.get()->getType() |
| 4949 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4950 | |
| 4951 | // If the destination type is an incomplete array type, update the |
| 4952 | // type accordingly. |
| 4953 | if (ResultType) { |
| 4954 | if (const IncompleteArrayType *IncompleteDest |
| 4955 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 4956 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4957 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4958 | *ResultType = S.Context.getConstantArrayType( |
| 4959 | IncompleteDest->getElementType(), |
| 4960 | ConstantSource->getSize(), |
| 4961 | ArrayType::Normal, 0); |
| 4962 | } |
| 4963 | } |
| 4964 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4965 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4966 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4967 | case SK_PassByIndirectCopyRestore: |
| 4968 | case SK_PassByIndirectRestore: |
| 4969 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 4970 | CurInit = S.Owned(new (S.Context) |
| 4971 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 4972 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 4973 | break; |
| 4974 | |
| 4975 | case SK_ProduceObjCObject: |
| 4976 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4977 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4978 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4979 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4980 | } |
| 4981 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 4982 | |
| 4983 | // Diagnose non-fatal problems with the completed initialization. |
| 4984 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 4985 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 4986 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 4987 | cast<FieldDecl>(Entity.getDecl()), |
| 4988 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4989 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4990 | return move(CurInit); |
| 4991 | } |
| 4992 | |
| 4993 | //===----------------------------------------------------------------------===// |
| 4994 | // Diagnose initialization failures |
| 4995 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4996 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4997 | const InitializedEntity &Entity, |
| 4998 | const InitializationKind &Kind, |
| 4999 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5000 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5001 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5002 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5003 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5004 | switch (Failure) { |
| 5005 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5006 | // FIXME: Customize for the initialized entity? |
| 5007 | if (NumArgs == 0) |
| 5008 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 5009 | << DestType.getNonReferenceType(); |
| 5010 | else // FIXME: diagnostic below could be better! |
| 5011 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 5012 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5013 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5014 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5015 | case FK_ArrayNeedsInitList: |
| 5016 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5017 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 5018 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 5019 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5020 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5021 | case FK_ArrayTypeMismatch: |
| 5022 | case FK_NonConstantArrayInit: |
| 5023 | S.Diag(Kind.getLocation(), |
| 5024 | (Failure == FK_ArrayTypeMismatch |
| 5025 | ? diag::err_array_init_different_type |
| 5026 | : diag::err_array_init_non_constant_array)) |
| 5027 | << DestType.getNonReferenceType() |
| 5028 | << Args[0]->getType() |
| 5029 | << Args[0]->getSourceRange(); |
| 5030 | break; |
| 5031 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5032 | case FK_AddressOfOverloadFailed: { |
| 5033 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5034 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5035 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5036 | true, |
| 5037 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5038 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5039 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5040 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5041 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5042 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5043 | switch (FailedOverloadResult) { |
| 5044 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5045 | if (Failure == FK_UserConversionOverloadFailed) |
| 5046 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 5047 | << Args[0]->getType() << DestType |
| 5048 | << Args[0]->getSourceRange(); |
| 5049 | else |
| 5050 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 5051 | << DestType << Args[0]->getType() |
| 5052 | << Args[0]->getSourceRange(); |
| 5053 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5054 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5055 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5056 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5057 | case OR_No_Viable_Function: |
| 5058 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 5059 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5060 | << Args[0]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5061 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5062 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5063 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5064 | case OR_Deleted: { |
| 5065 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 5066 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5067 | << Args[0]->getSourceRange(); |
| 5068 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5069 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5070 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 5071 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5072 | if (Ovl == OR_Deleted) { |
| 5073 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5074 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5075 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5076 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5077 | } |
| 5078 | break; |
| 5079 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5080 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5081 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5082 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5083 | break; |
| 5084 | } |
| 5085 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5086 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5087 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 5088 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5089 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5090 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 5091 | ? diag::err_lvalue_reference_bind_to_temporary |
| 5092 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 5093 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5094 | << DestType.getNonReferenceType() |
| 5095 | << Args[0]->getType() |
| 5096 | << Args[0]->getSourceRange(); |
| 5097 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5098 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5099 | case FK_RValueReferenceBindingToLValue: |
| 5100 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 5101 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5102 | << Args[0]->getSourceRange(); |
| 5103 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5104 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5105 | case FK_ReferenceInitDropsQualifiers: |
| 5106 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 5107 | << DestType.getNonReferenceType() |
| 5108 | << Args[0]->getType() |
| 5109 | << Args[0]->getSourceRange(); |
| 5110 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5111 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5112 | case FK_ReferenceInitFailed: |
| 5113 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 5114 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5115 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5116 | << Args[0]->getType() |
| 5117 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 5118 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5119 | Args[0]->getType()->isObjCObjectPointerType()) |
| 5120 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5121 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5122 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5123 | case FK_ConversionFailed: { |
| 5124 | QualType FromType = Args[0]->getType(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5125 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 5126 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5127 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5128 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5129 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5130 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 5131 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5132 | Args[0]->getType()->isObjCObjectPointerType()) |
| 5133 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5134 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5135 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5136 | |
| 5137 | case FK_ConversionFromPropertyFailed: |
| 5138 | // No-op. This error has already been reported. |
| 5139 | break; |
| 5140 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5141 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5142 | SourceRange R; |
| 5143 | |
| 5144 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5145 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5146 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5147 | else |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5148 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5149 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5150 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 5151 | if (Kind.isCStyleOrFunctionalCast()) |
| 5152 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 5153 | << R; |
| 5154 | else |
| 5155 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 5156 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5157 | break; |
| 5158 | } |
| 5159 | |
| 5160 | case FK_ReferenceBindingToInitList: |
| 5161 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 5162 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 5163 | break; |
| 5164 | |
| 5165 | case FK_InitListBadDestinationType: |
| 5166 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 5167 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 5168 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5169 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5170 | case FK_ConstructorOverloadFailed: { |
| 5171 | SourceRange ArgsRange; |
| 5172 | if (NumArgs) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5173 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5174 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5175 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5176 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 5177 | // bad. |
| 5178 | switch (FailedOverloadResult) { |
| 5179 | case OR_Ambiguous: |
| 5180 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 5181 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5182 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 5183 | Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5184 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5185 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5186 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5187 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 5188 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 5189 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 5190 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5191 | // This is implicit default initialization of a member or |
| 5192 | // base within a constructor. If no viable function was |
| 5193 | // found, notify the user that she needs to explicitly |
| 5194 | // initialize this base/member. |
| 5195 | CXXConstructorDecl *Constructor |
| 5196 | = cast<CXXConstructorDecl>(S.CurContext); |
| 5197 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5198 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5199 | << Constructor->isImplicit() |
| 5200 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5201 | << /*base=*/0 |
| 5202 | << Entity.getType(); |
| 5203 | |
| 5204 | RecordDecl *BaseDecl |
| 5205 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 5206 | ->getDecl(); |
| 5207 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 5208 | << S.Context.getTagDeclType(BaseDecl); |
| 5209 | } else { |
| 5210 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5211 | << Constructor->isImplicit() |
| 5212 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5213 | << /*member=*/1 |
| 5214 | << Entity.getName(); |
| 5215 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 5216 | |
| 5217 | if (const RecordType *Record |
| 5218 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5219 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5220 | diag::note_previous_decl) |
| 5221 | << S.Context.getTagDeclType(Record->getDecl()); |
| 5222 | } |
| 5223 | break; |
| 5224 | } |
| 5225 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5226 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 5227 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5228 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5229 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5230 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5231 | case OR_Deleted: { |
| 5232 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 5233 | << true << DestType << ArgsRange; |
| 5234 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5235 | OverloadingResult Ovl |
| 5236 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5237 | if (Ovl == OR_Deleted) { |
| 5238 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5239 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5240 | } else { |
| 5241 | llvm_unreachable("Inconsistent overload resolution?"); |
| 5242 | } |
| 5243 | break; |
| 5244 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5245 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5246 | case OR_Success: |
| 5247 | llvm_unreachable("Conversion did not fail!"); |
| 5248 | break; |
| 5249 | } |
| 5250 | break; |
| 5251 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5252 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5253 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5254 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5255 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5256 | // This is implicit default-initialization of a const member in |
| 5257 | // a constructor. Complain that it needs to be explicitly |
| 5258 | // initialized. |
| 5259 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 5260 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 5261 | << Constructor->isImplicit() |
| 5262 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5263 | << /*const=*/1 |
| 5264 | << Entity.getName(); |
| 5265 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 5266 | << Entity.getName(); |
| 5267 | } else { |
| 5268 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 5269 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 5270 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5271 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5272 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5273 | case FK_Incomplete: |
| 5274 | S.RequireCompleteType(Kind.getLocation(), DestType, |
| 5275 | diag::err_init_incomplete_type); |
| 5276 | break; |
| 5277 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5278 | case FK_ListInitializationFailed: { |
| 5279 | // Run the init list checker again to emit diagnostics. |
| 5280 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 5281 | QualType DestType = Entity.getType(); |
| 5282 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 5283 | DestType, /*VerifyOnly=*/false, |
| 5284 | Kind.getKind() != InitializationKind::IK_Direct || |
| 5285 | !S.getLangOptions().CPlusPlus0x); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5286 | assert(DiagnoseInitList.HadError() && |
| 5287 | "Inconsistent init list check result."); |
| 5288 | break; |
| 5289 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 5290 | |
| 5291 | case FK_PlaceholderType: { |
| 5292 | // FIXME: Already diagnosed! |
| 5293 | break; |
| 5294 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5295 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5296 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5297 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5298 | return true; |
| 5299 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5300 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5301 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5302 | switch (SequenceKind) { |
| 5303 | case FailedSequence: { |
| 5304 | OS << "Failed sequence: "; |
| 5305 | switch (Failure) { |
| 5306 | case FK_TooManyInitsForReference: |
| 5307 | OS << "too many initializers for reference"; |
| 5308 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5309 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5310 | case FK_ArrayNeedsInitList: |
| 5311 | OS << "array requires initializer list"; |
| 5312 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5313 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5314 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5315 | OS << "array requires initializer list or string literal"; |
| 5316 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5317 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5318 | case FK_ArrayTypeMismatch: |
| 5319 | OS << "array type mismatch"; |
| 5320 | break; |
| 5321 | |
| 5322 | case FK_NonConstantArrayInit: |
| 5323 | OS << "non-constant array initializer"; |
| 5324 | break; |
| 5325 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5326 | case FK_AddressOfOverloadFailed: |
| 5327 | OS << "address of overloaded function failed"; |
| 5328 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5329 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5330 | case FK_ReferenceInitOverloadFailed: |
| 5331 | OS << "overload resolution for reference initialization failed"; |
| 5332 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5333 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5334 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 5335 | OS << "non-const lvalue reference bound to temporary"; |
| 5336 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5337 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5338 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 5339 | OS << "non-const lvalue reference bound to unrelated type"; |
| 5340 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5341 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5342 | case FK_RValueReferenceBindingToLValue: |
| 5343 | OS << "rvalue reference bound to an lvalue"; |
| 5344 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5345 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5346 | case FK_ReferenceInitDropsQualifiers: |
| 5347 | OS << "reference initialization drops qualifiers"; |
| 5348 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5349 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5350 | case FK_ReferenceInitFailed: |
| 5351 | OS << "reference initialization failed"; |
| 5352 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5353 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5354 | case FK_ConversionFailed: |
| 5355 | OS << "conversion failed"; |
| 5356 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5357 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5358 | case FK_ConversionFromPropertyFailed: |
| 5359 | OS << "conversion from property failed"; |
| 5360 | break; |
| 5361 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5362 | case FK_TooManyInitsForScalar: |
| 5363 | OS << "too many initializers for scalar"; |
| 5364 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5365 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5366 | case FK_ReferenceBindingToInitList: |
| 5367 | OS << "referencing binding to initializer list"; |
| 5368 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5369 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5370 | case FK_InitListBadDestinationType: |
| 5371 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 5372 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5373 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5374 | case FK_UserConversionOverloadFailed: |
| 5375 | OS << "overloading failed for user-defined conversion"; |
| 5376 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5377 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5378 | case FK_ConstructorOverloadFailed: |
| 5379 | OS << "constructor overloading failed"; |
| 5380 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5381 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5382 | case FK_DefaultInitOfConst: |
| 5383 | OS << "default initialization of a const variable"; |
| 5384 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5385 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5386 | case FK_Incomplete: |
| 5387 | OS << "initialization of incomplete type"; |
| 5388 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5389 | |
| 5390 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5391 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 5392 | break; |
| 5393 | |
| 5394 | case FK_PlaceholderType: |
| 5395 | OS << "initializer expression isn't contextually valid"; |
| 5396 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5397 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5398 | OS << '\n'; |
| 5399 | return; |
| 5400 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5401 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5402 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5403 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5404 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5405 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5406 | case NormalSequence: |
| 5407 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5408 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5409 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5410 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5411 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 5412 | if (S != step_begin()) { |
| 5413 | OS << " -> "; |
| 5414 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5415 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5416 | switch (S->Kind) { |
| 5417 | case SK_ResolveAddressOfOverloadedFunction: |
| 5418 | OS << "resolve address of overloaded function"; |
| 5419 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5420 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5421 | case SK_CastDerivedToBaseRValue: |
| 5422 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 5423 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5424 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5425 | case SK_CastDerivedToBaseXValue: |
| 5426 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 5427 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5428 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5429 | case SK_CastDerivedToBaseLValue: |
| 5430 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 5431 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5432 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5433 | case SK_BindReference: |
| 5434 | OS << "bind reference to lvalue"; |
| 5435 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5436 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5437 | case SK_BindReferenceToTemporary: |
| 5438 | OS << "bind reference to a temporary"; |
| 5439 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5440 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5441 | case SK_ExtraneousCopyToTemporary: |
| 5442 | OS << "extraneous C++03 copy to temporary"; |
| 5443 | break; |
| 5444 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5445 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 5446 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5447 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5448 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5449 | case SK_QualificationConversionRValue: |
| 5450 | OS << "qualification conversion (rvalue)"; |
| 5451 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5452 | case SK_QualificationConversionXValue: |
| 5453 | OS << "qualification conversion (xvalue)"; |
| 5454 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5455 | case SK_QualificationConversionLValue: |
| 5456 | OS << "qualification conversion (lvalue)"; |
| 5457 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5458 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5459 | case SK_ConversionSequence: |
| 5460 | OS << "implicit conversion sequence ("; |
| 5461 | S->ICS->DebugPrint(); // FIXME: use OS |
| 5462 | OS << ")"; |
| 5463 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5464 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5465 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5466 | OS << "list aggregate initialization"; |
| 5467 | break; |
| 5468 | |
| 5469 | case SK_ListConstructorCall: |
| 5470 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5471 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5472 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5473 | case SK_ConstructorInitialization: |
| 5474 | OS << "constructor initialization"; |
| 5475 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5476 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5477 | case SK_ZeroInitialization: |
| 5478 | OS << "zero initialization"; |
| 5479 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5480 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5481 | case SK_CAssignment: |
| 5482 | OS << "C assignment"; |
| 5483 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5484 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5485 | case SK_StringInit: |
| 5486 | OS << "string initialization"; |
| 5487 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5488 | |
| 5489 | case SK_ObjCObjectConversion: |
| 5490 | OS << "Objective-C object conversion"; |
| 5491 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5492 | |
| 5493 | case SK_ArrayInit: |
| 5494 | OS << "array initialization"; |
| 5495 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5496 | |
| 5497 | case SK_PassByIndirectCopyRestore: |
| 5498 | OS << "pass by indirect copy and restore"; |
| 5499 | break; |
| 5500 | |
| 5501 | case SK_PassByIndirectRestore: |
| 5502 | OS << "pass by indirect restore"; |
| 5503 | break; |
| 5504 | |
| 5505 | case SK_ProduceObjCObject: |
| 5506 | OS << "Objective-C object retension"; |
| 5507 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5508 | } |
| 5509 | } |
| 5510 | } |
| 5511 | |
| 5512 | void InitializationSequence::dump() const { |
| 5513 | dump(llvm::errs()); |
| 5514 | } |
| 5515 | |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5516 | static void DiagnoseNarrowingInInitList( |
| 5517 | Sema& S, QualType EntityType, const Expr *InitE, |
| 5518 | bool Constant, const APValue &ConstantValue) { |
| 5519 | if (Constant) { |
| 5520 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5521 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5522 | ? diag::err_init_list_constant_narrowing |
| 5523 | : diag::warn_init_list_constant_narrowing) |
| 5524 | << InitE->getSourceRange() |
| 5525 | << ConstantValue |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5526 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5527 | } else |
| 5528 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5529 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5530 | ? diag::err_init_list_variable_narrowing |
| 5531 | : diag::warn_init_list_variable_narrowing) |
| 5532 | << InitE->getSourceRange() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5533 | << InitE->getType().getLocalUnqualifiedType() |
| 5534 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5535 | |
| 5536 | llvm::SmallString<128> StaticCast; |
| 5537 | llvm::raw_svector_ostream OS(StaticCast); |
| 5538 | OS << "static_cast<"; |
| 5539 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 5540 | // It's important to use the typedef's name if there is one so that the |
| 5541 | // fixit doesn't break code using types like int64_t. |
| 5542 | // |
| 5543 | // FIXME: This will break if the typedef requires qualification. But |
| 5544 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 5545 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5546 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
| 5547 | OS << BT->getName(S.getLangOptions()); |
| 5548 | else { |
| 5549 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 5550 | // with a broken cast. |
| 5551 | return; |
| 5552 | } |
| 5553 | OS << ">("; |
| 5554 | S.Diag(InitE->getLocStart(), diag::note_init_list_narrowing_override) |
| 5555 | << InitE->getSourceRange() |
| 5556 | << FixItHint::CreateInsertion(InitE->getLocStart(), OS.str()) |
| 5557 | << FixItHint::CreateInsertion( |
| 5558 | S.getPreprocessor().getLocForEndOfToken(InitE->getLocEnd()), ")"); |
| 5559 | } |
| 5560 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5561 | //===----------------------------------------------------------------------===// |
| 5562 | // Initialization helper functions |
| 5563 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5564 | bool |
| 5565 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 5566 | ExprResult Init) { |
| 5567 | if (Init.isInvalid()) |
| 5568 | return false; |
| 5569 | |
| 5570 | Expr *InitE = Init.get(); |
| 5571 | assert(InitE && "No initialization expression"); |
| 5572 | |
| 5573 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 5574 | SourceLocation()); |
| 5575 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5576 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5579 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5580 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 5581 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5582 | ExprResult Init, |
| 5583 | bool TopLevelOfInitList) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5584 | if (Init.isInvalid()) |
| 5585 | return ExprError(); |
| 5586 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5587 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5588 | assert(InitE && "No initialization expression?"); |
| 5589 | |
| 5590 | if (EqualLoc.isInvalid()) |
| 5591 | EqualLoc = InitE->getLocStart(); |
| 5592 | |
| 5593 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 5594 | EqualLoc); |
| 5595 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 5596 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5597 | |
| 5598 | bool Constant = false; |
| 5599 | APValue Result; |
| 5600 | if (TopLevelOfInitList && |
| 5601 | Seq.endsWithNarrowing(Context, InitE, &Constant, &Result)) { |
| 5602 | DiagnoseNarrowingInInitList(*this, Entity.getType(), InitE, |
| 5603 | Constant, Result); |
| 5604 | } |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5605 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5606 | } |