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) |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 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) { |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 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, |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 801 | Result.takeAs<Expr>()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 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); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 992 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOptions().CPlusPlus0x) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 993 | if (!VerifyOnly) |
| 994 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 995 | << DeclType << IList->getSourceRange(); |
| 996 | hadError = true; |
| 997 | ++Index; |
| 998 | ++StructuredIndex; |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | if (VerifyOnly) { |
| 1003 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1004 | hadError = true; |
| 1005 | ++Index; |
| 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | ExprResult Result = |
| 1010 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1011 | SemaRef.Owned(expr), |
| 1012 | /*TopLevelOfInitList=*/true); |
| 1013 | |
| 1014 | if (Result.isInvalid()) |
| 1015 | hadError = true; |
| 1016 | |
| 1017 | expr = Result.takeAs<Expr>(); |
| 1018 | IList->setInit(Index, expr); |
| 1019 | |
| 1020 | if (hadError) |
| 1021 | ++StructuredIndex; |
| 1022 | else |
| 1023 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1024 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1027 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1028 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1029 | unsigned &Index, |
| 1030 | InitListExpr *StructuredList, |
| 1031 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1032 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1033 | unsigned maxElements = VT->getNumElements(); |
| 1034 | unsigned numEltsInit = 0; |
| 1035 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1036 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1037 | if (Index >= IList->getNumInits()) { |
| 1038 | // Make sure the element type can be value-initialized. |
| 1039 | if (VerifyOnly) |
| 1040 | CheckValueInitializable( |
| 1041 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1042 | return; |
| 1043 | } |
| 1044 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1045 | if (!SemaRef.getLangOptions().OpenCL) { |
| 1046 | // If the initializing element is a vector, try to copy-initialize |
| 1047 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1048 | Expr *Init = IList->getInit(Index); |
| 1049 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1050 | if (VerifyOnly) { |
| 1051 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1052 | hadError = true; |
| 1053 | ++Index; |
| 1054 | return; |
| 1055 | } |
| 1056 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1057 | ExprResult Result = |
| 1058 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1059 | SemaRef.Owned(Init), |
| 1060 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1061 | |
| 1062 | Expr *ResultExpr = 0; |
| 1063 | if (Result.isInvalid()) |
| 1064 | hadError = true; // types weren't compatible. |
| 1065 | else { |
| 1066 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1067 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1068 | if (ResultExpr != Init) { |
| 1069 | // The type was promoted, update initializer list. |
| 1070 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1073 | if (hadError) |
| 1074 | ++StructuredIndex; |
| 1075 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1076 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1077 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1078 | ++Index; |
| 1079 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1080 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1082 | InitializedEntity ElementEntity = |
| 1083 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1084 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1085 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1086 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1087 | if (Index >= IList->getNumInits()) { |
| 1088 | if (VerifyOnly) |
| 1089 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1090 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1091 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1092 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1093 | ElementEntity.setElementIndex(Index); |
| 1094 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1095 | StructuredList, StructuredIndex); |
| 1096 | } |
| 1097 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1098 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1099 | |
| 1100 | InitializedEntity ElementEntity = |
| 1101 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1102 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1103 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1104 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1105 | // Don't attempt to go past the end of the init list |
| 1106 | if (Index >= IList->getNumInits()) |
| 1107 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1108 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1109 | ElementEntity.setElementIndex(Index); |
| 1110 | |
| 1111 | QualType IType = IList->getInit(Index)->getType(); |
| 1112 | if (!IType->isVectorType()) { |
| 1113 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1114 | StructuredList, StructuredIndex); |
| 1115 | ++numEltsInit; |
| 1116 | } else { |
| 1117 | QualType VecType; |
| 1118 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1119 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1120 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1121 | if (IType->isExtVectorType()) |
| 1122 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1123 | else |
| 1124 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1125 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1126 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1127 | StructuredList, StructuredIndex); |
| 1128 | numEltsInit += numIElts; |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1133 | if (numEltsInit != maxElements) { |
| 1134 | if (!VerifyOnly) |
| 1135 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 1136 | diag::err_vector_incorrect_num_initializers) |
| 1137 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1138 | hadError = true; |
| 1139 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1142 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1143 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1144 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1146 | unsigned &Index, |
| 1147 | InitListExpr *StructuredList, |
| 1148 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1149 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1150 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1151 | // Check for the special-case of initializing an array with a string. |
| 1152 | if (Index < IList->getNumInits()) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1153 | if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType, |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 1154 | SemaRef.Context)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1155 | // We place the string literal directly into the resulting |
| 1156 | // initializer list. This is the only place where the structure |
| 1157 | // of the structured initializer list doesn't match exactly, |
| 1158 | // because doing so would involve allocating one character |
| 1159 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1160 | if (!VerifyOnly) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 1161 | CheckStringInit(Str, DeclType, arrayType, SemaRef); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1162 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
| 1163 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1164 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1165 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1166 | return; |
| 1167 | } |
| 1168 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1169 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1170 | // Check for VLAs; in standard C it would be possible to check this |
| 1171 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1172 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1173 | if (!VerifyOnly) |
| 1174 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1175 | diag::err_variable_object_no_init) |
| 1176 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1177 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1178 | ++Index; |
| 1179 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1180 | return; |
| 1181 | } |
| 1182 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1183 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1184 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1185 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1186 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1187 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1188 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1189 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1190 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1191 | maxElementsKnown = true; |
| 1192 | } |
| 1193 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1194 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1195 | while (Index < IList->getNumInits()) { |
| 1196 | Expr *Init = IList->getInit(Index); |
| 1197 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1198 | // If we're not the subobject that matches up with the '{' for |
| 1199 | // the designator, we shouldn't be handling the |
| 1200 | // designator. Return immediately. |
| 1201 | if (!SubobjectIsDesignatorContext) |
| 1202 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1203 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1204 | // Handle this designated initializer. elementIndex will be |
| 1205 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1206 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1207 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1208 | StructuredList, StructuredIndex, true, |
| 1209 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1210 | hadError = true; |
| 1211 | continue; |
| 1212 | } |
| 1213 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1214 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1215 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1216 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1217 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1218 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1219 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1220 | // If the array is of incomplete type, keep track of the number of |
| 1221 | // elements in the initializer. |
| 1222 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1223 | maxElements = elementIndex; |
| 1224 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1225 | continue; |
| 1226 | } |
| 1227 | |
| 1228 | // If we know the maximum number of elements, and we've already |
| 1229 | // hit it, stop consuming elements in the initializer list. |
| 1230 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1231 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1232 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1233 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1234 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1235 | Entity); |
| 1236 | // Check this element. |
| 1237 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1238 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1239 | ++elementIndex; |
| 1240 | |
| 1241 | // If the array is of incomplete type, keep track of the number of |
| 1242 | // elements in the initializer. |
| 1243 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1244 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1245 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1246 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1247 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1248 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1249 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1250 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1251 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1252 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1253 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1254 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1255 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1256 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1258 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1259 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1260 | if (!hadError && VerifyOnly) { |
| 1261 | // Check if there are any members of the array that get value-initialized. |
| 1262 | // If so, check if doing that is possible. |
| 1263 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1264 | if (maxElementsKnown && elementIndex < maxElements) |
| 1265 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1266 | SemaRef.Context, 0, Entity)); |
| 1267 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1270 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1271 | Expr *InitExpr, |
| 1272 | FieldDecl *Field, |
| 1273 | bool TopLevelObject) { |
| 1274 | // Handle GNU flexible array initializers. |
| 1275 | unsigned FlexArrayDiag; |
| 1276 | if (isa<InitListExpr>(InitExpr) && |
| 1277 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1278 | // Empty flexible array init always allowed as an extension |
| 1279 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1280 | } else if (SemaRef.getLangOptions().CPlusPlus) { |
| 1281 | // Disallow flexible array init in C++; it is not required for gcc |
| 1282 | // compatibility, and it needs work to IRGen correctly in general. |
| 1283 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1284 | } else if (!TopLevelObject) { |
| 1285 | // Disallow flexible array init on non-top-level object |
| 1286 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1287 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1288 | // Disallow flexible array init on anything which is not a variable. |
| 1289 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1290 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1291 | // Disallow flexible array init on local variables. |
| 1292 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1293 | } else { |
| 1294 | // Allow other cases. |
| 1295 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1296 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1297 | |
| 1298 | if (!VerifyOnly) { |
| 1299 | SemaRef.Diag(InitExpr->getSourceRange().getBegin(), |
| 1300 | FlexArrayDiag) |
| 1301 | << InitExpr->getSourceRange().getBegin(); |
| 1302 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1303 | << Field; |
| 1304 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1305 | |
| 1306 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1307 | } |
| 1308 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1309 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1310 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1312 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1314 | unsigned &Index, |
| 1315 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1316 | unsigned &StructuredIndex, |
| 1317 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1318 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1319 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1320 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1321 | // confusion, we forgo checking the intializer for the entire record. |
| 1322 | if (structDecl->isInvalidDecl()) { |
| 1323 | hadError = true; |
| 1324 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1325 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1326 | |
| 1327 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1328 | // Value-initialize the first named member of the union. |
| 1329 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 1330 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1331 | Field != FieldEnd; ++Field) { |
| 1332 | if (Field->getDeclName()) { |
| 1333 | if (VerifyOnly) |
| 1334 | CheckValueInitializable( |
| 1335 | InitializedEntity::InitializeMember(*Field, &Entity)); |
| 1336 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1337 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1338 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | return; |
| 1342 | } |
| 1343 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1344 | // If structDecl is a forward declaration, this loop won't do |
| 1345 | // anything except look at designated initializers; That's okay, |
| 1346 | // because an error should get printed out elsewhere. It might be |
| 1347 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1348 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1349 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1350 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1351 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1352 | while (Index < IList->getNumInits()) { |
| 1353 | Expr *Init = IList->getInit(Index); |
| 1354 | |
| 1355 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1356 | // If we're not the subobject that matches up with the '{' for |
| 1357 | // the designator, we shouldn't be handling the |
| 1358 | // designator. Return immediately. |
| 1359 | if (!SubobjectIsDesignatorContext) |
| 1360 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1361 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1362 | // Handle this designated initializer. Field will be updated to |
| 1363 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1364 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1365 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1366 | StructuredList, StructuredIndex, |
| 1367 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1368 | hadError = true; |
| 1369 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1370 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1371 | |
| 1372 | // Disable check for missing fields when designators are used. |
| 1373 | // This matches gcc behaviour. |
| 1374 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1375 | continue; |
| 1376 | } |
| 1377 | |
| 1378 | if (Field == FieldEnd) { |
| 1379 | // We've run out of fields. We're done. |
| 1380 | break; |
| 1381 | } |
| 1382 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1383 | // We've already initialized a member of a union. We're done. |
| 1384 | if (InitializedSomething && DeclType->isUnionType()) |
| 1385 | break; |
| 1386 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1387 | // If we've hit the flexible array member at the end, we're done. |
| 1388 | if (Field->getType()->isIncompleteArrayType()) |
| 1389 | break; |
| 1390 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1391 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1392 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1393 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1394 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1395 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1396 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1397 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1398 | bool InvalidUse; |
| 1399 | if (VerifyOnly) |
| 1400 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
| 1401 | else |
| 1402 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
| 1403 | IList->getInit(Index)->getLocStart()); |
| 1404 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1405 | ++Index; |
| 1406 | ++Field; |
| 1407 | hadError = true; |
| 1408 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1409 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1410 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1411 | InitializedEntity MemberEntity = |
| 1412 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1413 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1414 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1415 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1416 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1417 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1418 | // Initialize the first field within the union. |
| 1419 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1420 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1421 | |
| 1422 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1423 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1424 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1425 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1426 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1427 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1428 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1429 | // It is possible we have one or more unnamed bitfields remaining. |
| 1430 | // Find first (if any) named field and emit warning. |
| 1431 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1432 | it != end; ++it) { |
| 1433 | if (!it->isUnnamedBitfield()) { |
| 1434 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1435 | diag::warn_missing_field_initializers) << it->getName(); |
| 1436 | break; |
| 1437 | } |
| 1438 | } |
| 1439 | } |
| 1440 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1441 | // Check that any remaining fields can be value-initialized. |
| 1442 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1443 | !Field->getType()->isIncompleteArrayType()) { |
| 1444 | // FIXME: Should check for holes left by designated initializers too. |
| 1445 | for (; Field != FieldEnd && !hadError; ++Field) { |
| 1446 | if (!Field->isUnnamedBitfield()) |
| 1447 | CheckValueInitializable( |
| 1448 | InitializedEntity::InitializeMember(*Field, &Entity)); |
| 1449 | } |
| 1450 | } |
| 1451 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1453 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1454 | return; |
| 1455 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1456 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
| 1457 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1458 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1459 | ++Index; |
| 1460 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1463 | InitializedEntity MemberEntity = |
| 1464 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1465 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1466 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1467 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1468 | StructuredList, StructuredIndex); |
| 1469 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1470 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1471 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1472 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1473 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1474 | /// \brief Expand a field designator that refers to a member of an |
| 1475 | /// anonymous struct or union into a series of field designators that |
| 1476 | /// refers to the field within the appropriate subobject. |
| 1477 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1478 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 | DesignatedInitExpr *DIE, |
| 1480 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1481 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1482 | typedef DesignatedInitExpr::Designator Designator; |
| 1483 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1484 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1485 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1486 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1487 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1488 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1489 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1490 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1491 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1492 | else |
| 1493 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1494 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1495 | assert(isa<FieldDecl>(*PI)); |
| 1496 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | // Expand the current designator into the set of replacement |
| 1500 | // designators, so we have a full subobject path down to where the |
| 1501 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1502 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1503 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1504 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1506 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1507 | /// corresponds to FieldName. |
| 1508 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1509 | IdentifierInfo *FieldName) { |
| 1510 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1511 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
| 1512 | while (IndirectFieldDecl *IF = dyn_cast<IndirectFieldDecl>(NextDecl)) { |
| 1513 | if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) |
| 1514 | return IF; |
| 1515 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1516 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1517 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1520 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1521 | DesignatedInitExpr *DIE) { |
| 1522 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1523 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1524 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1525 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1526 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
| 1527 | DIE->size(), IndexExprs.data(), |
| 1528 | NumIndexExprs, DIE->getEqualOrColonLoc(), |
| 1529 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1530 | } |
| 1531 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1532 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1533 | /// |
| 1534 | /// Determines whether the designated initializer @p DIE, which |
| 1535 | /// resides at the given @p Index within the initializer list @p |
| 1536 | /// IList, is well-formed for a current object of type @p DeclType |
| 1537 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1538 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1539 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1540 | /// |
| 1541 | /// @param IList The initializer list in which this designated |
| 1542 | /// initializer occurs. |
| 1543 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1544 | /// @param DIE The designated initializer expression. |
| 1545 | /// |
| 1546 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1547 | /// |
| 1548 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1549 | /// into which the designation in @p DIE should refer. |
| 1550 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1551 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1552 | /// a field, this will be set to the field declaration corresponding |
| 1553 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1554 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1555 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1556 | /// DIE is an array designator or GNU array-range designator, this |
| 1557 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1558 | /// |
| 1559 | /// @param Index Index into @p IList where the designated initializer |
| 1560 | /// @p DIE occurs. |
| 1561 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1562 | /// @param StructuredList The initializer list expression that |
| 1563 | /// describes all of the subobject initializers in the order they'll |
| 1564 | /// actually be initialized. |
| 1565 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1566 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1568 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1569 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1570 | DesignatedInitExpr *DIE, |
| 1571 | unsigned DesigIdx, |
| 1572 | QualType &CurrentObjectType, |
| 1573 | RecordDecl::field_iterator *NextField, |
| 1574 | llvm::APSInt *NextElementIndex, |
| 1575 | unsigned &Index, |
| 1576 | InitListExpr *StructuredList, |
| 1577 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1578 | bool FinishSubobjectInit, |
| 1579 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1580 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1581 | // Check the actual initialization for the designated object type. |
| 1582 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1583 | |
| 1584 | // Temporarily remove the designator expression from the |
| 1585 | // initializer list that the child calls see, so that we don't try |
| 1586 | // to re-process the designator. |
| 1587 | unsigned OldIndex = Index; |
| 1588 | IList->setInit(OldIndex, DIE->getInit()); |
| 1589 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1590 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1591 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1592 | |
| 1593 | // Restore the designated initializer expression in the syntactic |
| 1594 | // form of the initializer list. |
| 1595 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1596 | DIE->setInit(IList->getInit(OldIndex)); |
| 1597 | IList->setInit(OldIndex, DIE); |
| 1598 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1599 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1602 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1603 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1604 | if (!VerifyOnly) { |
| 1605 | assert((IsFirstDesignator || StructuredList) && |
| 1606 | "Need a non-designated initializer list to start from"); |
| 1607 | |
| 1608 | // Determine the structural initializer list that corresponds to the |
| 1609 | // current subobject. |
| 1610 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
| 1611 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1612 | StructuredList, StructuredIndex, |
| 1613 | SourceRange(D->getStartLocation(), |
| 1614 | DIE->getSourceRange().getEnd())); |
| 1615 | assert(StructuredList && "Expected a structured initializer list"); |
| 1616 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1617 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1618 | if (D->isFieldDesignator()) { |
| 1619 | // C99 6.7.8p7: |
| 1620 | // |
| 1621 | // If a designator has the form |
| 1622 | // |
| 1623 | // . identifier |
| 1624 | // |
| 1625 | // then the current object (defined below) shall have |
| 1626 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1628 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1629 | if (!RT) { |
| 1630 | SourceLocation Loc = D->getDotLoc(); |
| 1631 | if (Loc.isInvalid()) |
| 1632 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1633 | if (!VerifyOnly) |
| 1634 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1635 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1636 | ++Index; |
| 1637 | return true; |
| 1638 | } |
| 1639 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1640 | // Note: we perform a linear search of the fields here, despite |
| 1641 | // the fact that we have a faster lookup method, because we always |
| 1642 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1643 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1644 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1645 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1647 | Field = RT->getDecl()->field_begin(), |
| 1648 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1649 | for (; Field != FieldEnd; ++Field) { |
| 1650 | if (Field->isUnnamedBitfield()) |
| 1651 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1652 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1653 | // If we find a field representing an anonymous field, look in the |
| 1654 | // IndirectFieldDecl that follow for the designated initializer. |
| 1655 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1656 | if (IndirectFieldDecl *IF = |
| 1657 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1658 | // In verify mode, don't modify the original. |
| 1659 | if (VerifyOnly) |
| 1660 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1661 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1662 | D = DIE->getDesignator(DesigIdx); |
| 1663 | break; |
| 1664 | } |
| 1665 | } |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1666 | if (KnownField && KnownField == *Field) |
| 1667 | break; |
| 1668 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1669 | break; |
| 1670 | |
| 1671 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1674 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1675 | if (VerifyOnly) { |
| 1676 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1677 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1678 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1679 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1680 | // There was no normal field in the struct with the designated |
| 1681 | // name. Perform another lookup for this name, which may find |
| 1682 | // something that we can't designate (e.g., a member function), |
| 1683 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1685 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1686 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1687 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1688 | // Name lookup didn't find anything. Determine whether this |
| 1689 | // was a typo for another field name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1690 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1691 | Sema::LookupMemberName); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1692 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1693 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1694 | Sema::LookupMemberName, /*Scope=*/NULL, /*SS=*/NULL, |
| 1695 | RT->getDecl(), false, Sema::CTC_NoKeywords); |
| 1696 | if ((ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>()) && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1697 | ReplacementField->getDeclContext()->getRedeclContext() |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1698 | ->Equals(RT->getDecl())) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1699 | std::string CorrectedStr( |
| 1700 | Corrected.getAsString(SemaRef.getLangOptions())); |
| 1701 | std::string CorrectedQuotedStr( |
| 1702 | Corrected.getQuoted(SemaRef.getLangOptions())); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1703 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1704 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1705 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1706 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1707 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1708 | diag::note_previous_decl) << CorrectedQuotedStr; |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1709 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1710 | } else { |
| 1711 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1712 | << FieldName << CurrentObjectType; |
| 1713 | ++Index; |
| 1714 | return true; |
| 1715 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1716 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1717 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1718 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1719 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1720 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1721 | << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1722 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1723 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1724 | ++Index; |
| 1725 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1726 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1727 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1728 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1729 | // The replacement field comes from typo correction; find it |
| 1730 | // in the list of fields. |
| 1731 | FieldIndex = 0; |
| 1732 | Field = RT->getDecl()->field_begin(); |
| 1733 | for (; Field != FieldEnd; ++Field) { |
| 1734 | if (Field->isUnnamedBitfield()) |
| 1735 | continue; |
| 1736 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1737 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1738 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1739 | break; |
| 1740 | |
| 1741 | ++FieldIndex; |
| 1742 | } |
| 1743 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1744 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1745 | |
| 1746 | // All of the fields of a union are located at the same place in |
| 1747 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1748 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1749 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1750 | if (!VerifyOnly) |
| 1751 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1752 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1753 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1754 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1755 | bool InvalidUse; |
| 1756 | if (VerifyOnly) |
| 1757 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
| 1758 | else |
| 1759 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
| 1760 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1761 | ++Index; |
| 1762 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1763 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1764 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1765 | if (!VerifyOnly) { |
| 1766 | // Update the designator with the field declaration. |
| 1767 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1768 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1769 | // Make sure that our non-designated initializer list has space |
| 1770 | // for a subobject corresponding to this field. |
| 1771 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1772 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1773 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1774 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1775 | // This designator names a flexible array member. |
| 1776 | if (Field->getType()->isIncompleteArrayType()) { |
| 1777 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1778 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1779 | // We can't designate an object within the flexible array |
| 1780 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1781 | if (!VerifyOnly) { |
| 1782 | DesignatedInitExpr::Designator *NextD |
| 1783 | = DIE->getDesignator(DesigIdx + 1); |
| 1784 | SemaRef.Diag(NextD->getStartLocation(), |
| 1785 | diag::err_designator_into_flexible_array_member) |
| 1786 | << SourceRange(NextD->getStartLocation(), |
| 1787 | DIE->getSourceRange().getEnd()); |
| 1788 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1789 | << *Field; |
| 1790 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1791 | Invalid = true; |
| 1792 | } |
| 1793 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1794 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1795 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1796 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1797 | if (!VerifyOnly) { |
| 1798 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
| 1799 | diag::err_flexible_array_init_needs_braces) |
| 1800 | << DIE->getInit()->getSourceRange(); |
| 1801 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1802 | << *Field; |
| 1803 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1804 | Invalid = true; |
| 1805 | } |
| 1806 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1807 | // Check GNU flexible array initializer. |
| 1808 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
| 1809 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1810 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1811 | |
| 1812 | if (Invalid) { |
| 1813 | ++Index; |
| 1814 | return true; |
| 1815 | } |
| 1816 | |
| 1817 | // Initialize the array. |
| 1818 | bool prevHadError = hadError; |
| 1819 | unsigned newStructuredIndex = FieldIndex; |
| 1820 | unsigned OldIndex = Index; |
| 1821 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1822 | |
| 1823 | InitializedEntity MemberEntity = |
| 1824 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1825 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1826 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1827 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1828 | IList->setInit(OldIndex, DIE); |
| 1829 | if (hadError && !prevHadError) { |
| 1830 | ++Field; |
| 1831 | ++FieldIndex; |
| 1832 | if (NextField) |
| 1833 | *NextField = Field; |
| 1834 | StructuredIndex = FieldIndex; |
| 1835 | return true; |
| 1836 | } |
| 1837 | } else { |
| 1838 | // Recurse to check later designated subobjects. |
| 1839 | QualType FieldType = (*Field)->getType(); |
| 1840 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1841 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1842 | InitializedEntity MemberEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1843 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1844 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1845 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1846 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1847 | true, false)) |
| 1848 | return true; |
| 1849 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1850 | |
| 1851 | // Find the position of the next field to be initialized in this |
| 1852 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1853 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1854 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1855 | |
| 1856 | // If this the first designator, our caller will continue checking |
| 1857 | // the rest of this struct/class/union subobject. |
| 1858 | if (IsFirstDesignator) { |
| 1859 | if (NextField) |
| 1860 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1861 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1862 | return false; |
| 1863 | } |
| 1864 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1865 | if (!FinishSubobjectInit) |
| 1866 | return false; |
| 1867 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1868 | // We've already initialized something in the union; we're done. |
| 1869 | if (RT->getDecl()->isUnion()) |
| 1870 | return hadError; |
| 1871 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1872 | // Check the remaining fields within this class/struct/union subobject. |
| 1873 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1874 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1875 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1876 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1877 | return hadError && !prevHadError; |
| 1878 | } |
| 1879 | |
| 1880 | // C99 6.7.8p6: |
| 1881 | // |
| 1882 | // If a designator has the form |
| 1883 | // |
| 1884 | // [ constant-expression ] |
| 1885 | // |
| 1886 | // then the current object (defined below) shall have array |
| 1887 | // type and the expression shall be an integer constant |
| 1888 | // expression. If the array is of unknown size, any |
| 1889 | // nonnegative value is valid. |
| 1890 | // |
| 1891 | // Additionally, cope with the GNU extension that permits |
| 1892 | // designators of the form |
| 1893 | // |
| 1894 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1895 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1896 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1897 | if (!VerifyOnly) |
| 1898 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 1899 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1900 | ++Index; |
| 1901 | return true; |
| 1902 | } |
| 1903 | |
| 1904 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1905 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1906 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1907 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1908 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1909 | DesignatedEndIndex = DesignatedStartIndex; |
| 1910 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1911 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1912 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1913 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1914 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1916 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1917 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1918 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 1919 | // Codegen can't handle evaluating array range designators that have side |
| 1920 | // effects, because we replicate the AST value for each initialized element. |
| 1921 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 1922 | // elements with something that has a side effect, so codegen can emit an |
| 1923 | // "error unsupported" error instead of miscompiling the app. |
| 1924 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1925 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1926 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1929 | if (isa<ConstantArrayType>(AT)) { |
| 1930 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1931 | DesignatedStartIndex |
| 1932 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1933 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1934 | DesignatedEndIndex |
| 1935 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1936 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1937 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 1938 | if (!VerifyOnly) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1939 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
| 1940 | diag::err_array_designator_too_large) |
| 1941 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 1942 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1943 | ++Index; |
| 1944 | return true; |
| 1945 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1946 | } else { |
| 1947 | // Make sure the bit-widths and signedness match. |
| 1948 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1949 | DesignatedEndIndex |
| 1950 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1951 | else if (DesignatedStartIndex.getBitWidth() < |
| 1952 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1953 | DesignatedStartIndex |
| 1954 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1955 | DesignatedStartIndex.setIsUnsigned(true); |
| 1956 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1957 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1959 | // Make sure that our non-designated initializer list has space |
| 1960 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1961 | if (!VerifyOnly && |
| 1962 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1964 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1965 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1966 | // Repeatedly perform subobject initializations in the range |
| 1967 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1968 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1969 | // Move to the next designator |
| 1970 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1971 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1972 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1973 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1974 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1975 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1976 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1977 | // Recurse to check later designated subobjects. |
| 1978 | QualType ElementType = AT->getElementType(); |
| 1979 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1980 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1981 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1982 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 1983 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1984 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1985 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1986 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1987 | return true; |
| 1988 | |
| 1989 | // Move to the next index in the array that we'll be initializing. |
| 1990 | ++DesignatedStartIndex; |
| 1991 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1992 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1993 | |
| 1994 | // If this the first designator, our caller will continue checking |
| 1995 | // the rest of this array subobject. |
| 1996 | if (IsFirstDesignator) { |
| 1997 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1998 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1999 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2000 | return false; |
| 2001 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2003 | if (!FinishSubobjectInit) |
| 2004 | return false; |
| 2005 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2006 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2007 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2008 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2009 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2010 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2014 | // Get the structured initializer list for a subobject of type |
| 2015 | // @p CurrentObjectType. |
| 2016 | InitListExpr * |
| 2017 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2018 | QualType CurrentObjectType, |
| 2019 | InitListExpr *StructuredList, |
| 2020 | unsigned StructuredIndex, |
| 2021 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2022 | if (VerifyOnly) |
| 2023 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2024 | Expr *ExistingInit = 0; |
| 2025 | if (!StructuredList) |
| 2026 | ExistingInit = SyntacticToSemantic[IList]; |
| 2027 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2028 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2029 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2030 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2031 | return Result; |
| 2032 | |
| 2033 | if (ExistingInit) { |
| 2034 | // We are creating an initializer list that initializes the |
| 2035 | // subobjects of the current object, but there was already an |
| 2036 | // initialization that completely initialized the current |
| 2037 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2039 | // struct X { int a, b; }; |
| 2040 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2041 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2042 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2043 | // designated initializer re-initializes the whole |
| 2044 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2045 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2046 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2047 | << InitRange; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2049 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2050 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2051 | << ExistingInit->getSourceRange(); |
| 2052 | } |
| 2053 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2055 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
| 2056 | InitRange.getBegin(), 0, 0, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2057 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2058 | |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2059 | Result->setType(CurrentObjectType.getNonLValueExprType(SemaRef.Context)); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2060 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2061 | // Pre-allocate storage for the structured initializer list. |
| 2062 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2063 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2064 | bool GotNumInits = false; |
| 2065 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2066 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2067 | GotNumInits = true; |
| 2068 | } else if (Index < IList->getNumInits()) { |
| 2069 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2070 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2071 | GotNumInits = true; |
| 2072 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2076 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2077 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2078 | NumElements = CAType->getSize().getZExtValue(); |
| 2079 | // Simple heuristic so that we don't allocate a very large |
| 2080 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2081 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2082 | NumElements = 0; |
| 2083 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2084 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2085 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2086 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2087 | RecordDecl *RDecl = RType->getDecl(); |
| 2088 | if (RDecl->isUnion()) |
| 2089 | NumElements = 1; |
| 2090 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2092 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2095 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2096 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2097 | // Link this new initializer list into the structured initializer |
| 2098 | // lists. |
| 2099 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2100 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2101 | else { |
| 2102 | Result->setSyntacticForm(IList); |
| 2103 | SyntacticToSemantic[IList] = Result; |
| 2104 | } |
| 2105 | |
| 2106 | return Result; |
| 2107 | } |
| 2108 | |
| 2109 | /// Update the initializer at index @p StructuredIndex within the |
| 2110 | /// structured initializer list to the value @p expr. |
| 2111 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2112 | unsigned &StructuredIndex, |
| 2113 | Expr *expr) { |
| 2114 | // No structured initializer list to update |
| 2115 | if (!StructuredList) |
| 2116 | return; |
| 2117 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2118 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2119 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2120 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2121 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2122 | diag::warn_initializer_overrides) |
| 2123 | << expr->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2124 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2125 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2126 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2127 | << PrevInit->getSourceRange(); |
| 2128 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2129 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2130 | ++StructuredIndex; |
| 2131 | } |
| 2132 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2133 | /// Check that the given Index expression is a valid array designator |
| 2134 | /// value. This is essentailly just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2135 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2136 | /// and produces a reasonable diagnostic if there is a |
| 2137 | /// failure. Returns true if there was an error, false otherwise. If |
| 2138 | /// everything went okay, Value will receive the value of the constant |
| 2139 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2140 | static bool |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2141 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2142 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 2143 | |
| 2144 | // Make sure this is an integer constant expression. |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2145 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 2146 | return true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2147 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2148 | if (Value.isSigned() && Value.isNegative()) |
| 2149 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2150 | << Value.toString(10) << Index->getSourceRange(); |
| 2151 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2152 | Value.setIsUnsigned(true); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2153 | return false; |
| 2154 | } |
| 2155 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2156 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2157 | SourceLocation Loc, |
| 2158 | bool GNUSyntax, |
| 2159 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2160 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2161 | |
| 2162 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2163 | SmallVector<ASTDesignator, 32> Designators; |
| 2164 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2165 | |
| 2166 | // Build designators and check array designator expressions. |
| 2167 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2168 | const Designator &D = Desig.getDesignator(Idx); |
| 2169 | switch (D.getKind()) { |
| 2170 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2172 | D.getFieldLoc())); |
| 2173 | break; |
| 2174 | |
| 2175 | case Designator::ArrayDesignator: { |
| 2176 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2177 | llvm::APSInt IndexValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2178 | if (!Index->isTypeDependent() && |
| 2179 | !Index->isValueDependent() && |
| 2180 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2181 | Invalid = true; |
| 2182 | else { |
| 2183 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2185 | D.getRBracketLoc())); |
| 2186 | InitExpressions.push_back(Index); |
| 2187 | } |
| 2188 | break; |
| 2189 | } |
| 2190 | |
| 2191 | case Designator::ArrayRangeDesignator: { |
| 2192 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2193 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2194 | llvm::APSInt StartValue; |
| 2195 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2196 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2197 | StartIndex->isValueDependent(); |
| 2198 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2199 | EndIndex->isValueDependent(); |
| 2200 | if ((!StartDependent && |
| 2201 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 2202 | (!EndDependent && |
| 2203 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2204 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2205 | else { |
| 2206 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2207 | if (StartDependent || EndDependent) { |
| 2208 | // Nothing to compute. |
| 2209 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2210 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2211 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2212 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2213 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2214 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2215 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2216 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2217 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2218 | Invalid = true; |
| 2219 | } else { |
| 2220 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2221 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2222 | D.getEllipsisLoc(), |
| 2223 | D.getRBracketLoc())); |
| 2224 | InitExpressions.push_back(StartIndex); |
| 2225 | InitExpressions.push_back(EndIndex); |
| 2226 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2227 | } |
| 2228 | break; |
| 2229 | } |
| 2230 | } |
| 2231 | } |
| 2232 | |
| 2233 | if (Invalid || Init.isInvalid()) |
| 2234 | return ExprError(); |
| 2235 | |
| 2236 | // Clear out the expressions within the designation. |
| 2237 | Desig.ClearExprs(*this); |
| 2238 | |
| 2239 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2240 | = DesignatedInitExpr::Create(Context, |
| 2241 | Designators.data(), Designators.size(), |
| 2242 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 2243 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2244 | |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame^] | 2245 | if (!getLangOptions().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2246 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2247 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2248 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2249 | return Owned(DIE); |
| 2250 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2251 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2252 | //===----------------------------------------------------------------------===// |
| 2253 | // Initialization entity |
| 2254 | //===----------------------------------------------------------------------===// |
| 2255 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2256 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2257 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2258 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2259 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2260 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2261 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2262 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2263 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2264 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2265 | Type = VT->getElementType(); |
| 2266 | } else { |
| 2267 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2268 | assert(CT && "Unexpected type"); |
| 2269 | Kind = EK_ComplexElement; |
| 2270 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2271 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2274 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2275 | CXXBaseSpecifier *Base, |
| 2276 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2277 | { |
| 2278 | InitializedEntity Result; |
| 2279 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2280 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2281 | if (IsInheritedVirtualBase) |
| 2282 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2283 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2284 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2285 | return Result; |
| 2286 | } |
| 2287 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2288 | DeclarationName InitializedEntity::getName() const { |
| 2289 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2290 | case EK_Parameter: { |
| 2291 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2292 | return (D ? D->getDeclName() : DeclarationName()); |
| 2293 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2294 | |
| 2295 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2296 | case EK_Member: |
| 2297 | return VariableOrMember->getDeclName(); |
| 2298 | |
| 2299 | case EK_Result: |
| 2300 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2301 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2302 | case EK_Temporary: |
| 2303 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2304 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2305 | case EK_ArrayElement: |
| 2306 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2307 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2308 | case EK_BlockElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2309 | return DeclarationName(); |
| 2310 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2311 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2312 | // Silence GCC warning |
| 2313 | return DeclarationName(); |
| 2314 | } |
| 2315 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2316 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2317 | switch (getKind()) { |
| 2318 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2319 | case EK_Member: |
| 2320 | return VariableOrMember; |
| 2321 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2322 | case EK_Parameter: |
| 2323 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2324 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2325 | case EK_Result: |
| 2326 | case EK_Exception: |
| 2327 | case EK_New: |
| 2328 | case EK_Temporary: |
| 2329 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2330 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2331 | case EK_ArrayElement: |
| 2332 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2333 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2334 | case EK_BlockElement: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2335 | return 0; |
| 2336 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2337 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2338 | // Silence GCC warning |
| 2339 | return 0; |
| 2340 | } |
| 2341 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2342 | bool InitializedEntity::allowsNRVO() const { |
| 2343 | switch (getKind()) { |
| 2344 | case EK_Result: |
| 2345 | case EK_Exception: |
| 2346 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2347 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2348 | case EK_Variable: |
| 2349 | case EK_Parameter: |
| 2350 | case EK_Member: |
| 2351 | case EK_New: |
| 2352 | case EK_Temporary: |
| 2353 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2354 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2355 | case EK_ArrayElement: |
| 2356 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2357 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2358 | case EK_BlockElement: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2359 | break; |
| 2360 | } |
| 2361 | |
| 2362 | return false; |
| 2363 | } |
| 2364 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2365 | //===----------------------------------------------------------------------===// |
| 2366 | // Initialization sequence |
| 2367 | //===----------------------------------------------------------------------===// |
| 2368 | |
| 2369 | void InitializationSequence::Step::Destroy() { |
| 2370 | switch (Kind) { |
| 2371 | case SK_ResolveAddressOfOverloadedFunction: |
| 2372 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2373 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2374 | case SK_CastDerivedToBaseLValue: |
| 2375 | case SK_BindReference: |
| 2376 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2377 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2378 | case SK_UserConversion: |
| 2379 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2380 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2381 | case SK_QualificationConversionLValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2382 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2383 | case SK_ListConstructorCall: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2384 | case SK_UnwrapInitList: |
| 2385 | case SK_RewrapInitList: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2386 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2387 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2388 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2389 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2390 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2391 | case SK_ArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2392 | case SK_PassByIndirectCopyRestore: |
| 2393 | case SK_PassByIndirectRestore: |
| 2394 | case SK_ProduceObjCObject: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2395 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2396 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2397 | case SK_ConversionSequence: |
| 2398 | delete ICS; |
| 2399 | } |
| 2400 | } |
| 2401 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2402 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2403 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2407 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2408 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2409 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2410 | switch (getFailureKind()) { |
| 2411 | case FK_TooManyInitsForReference: |
| 2412 | case FK_ArrayNeedsInitList: |
| 2413 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 2414 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2415 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2416 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2417 | case FK_RValueReferenceBindingToLValue: |
| 2418 | case FK_ReferenceInitDropsQualifiers: |
| 2419 | case FK_ReferenceInitFailed: |
| 2420 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2421 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2422 | case FK_TooManyInitsForScalar: |
| 2423 | case FK_ReferenceBindingToInitList: |
| 2424 | case FK_InitListBadDestinationType: |
| 2425 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2426 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2427 | case FK_ArrayTypeMismatch: |
| 2428 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2429 | case FK_ListInitializationFailed: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2430 | case FK_PlaceholderType: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2431 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2432 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2433 | case FK_ReferenceInitOverloadFailed: |
| 2434 | case FK_UserConversionOverloadFailed: |
| 2435 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2436 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2437 | return FailedOverloadResult == OR_Ambiguous; |
| 2438 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2439 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2440 | return false; |
| 2441 | } |
| 2442 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2443 | bool InitializationSequence::isConstructorInitialization() const { |
| 2444 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2445 | } |
| 2446 | |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2447 | bool InitializationSequence::endsWithNarrowing(ASTContext &Ctx, |
| 2448 | const Expr *Initializer, |
| 2449 | bool *isInitializerConstant, |
| 2450 | APValue *ConstantValue) const { |
| 2451 | if (Steps.empty() || Initializer->isValueDependent()) |
| 2452 | return false; |
| 2453 | |
| 2454 | const Step &LastStep = Steps.back(); |
| 2455 | if (LastStep.Kind != SK_ConversionSequence) |
| 2456 | return false; |
| 2457 | |
| 2458 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 2459 | const StandardConversionSequence *SCS = NULL; |
| 2460 | switch (ICS.getKind()) { |
| 2461 | case ImplicitConversionSequence::StandardConversion: |
| 2462 | SCS = &ICS.Standard; |
| 2463 | break; |
| 2464 | case ImplicitConversionSequence::UserDefinedConversion: |
| 2465 | SCS = &ICS.UserDefined.After; |
| 2466 | break; |
| 2467 | case ImplicitConversionSequence::AmbiguousConversion: |
| 2468 | case ImplicitConversionSequence::EllipsisConversion: |
| 2469 | case ImplicitConversionSequence::BadConversion: |
| 2470 | return false; |
| 2471 | } |
| 2472 | |
| 2473 | // Check if SCS represents a narrowing conversion, according to C++0x |
| 2474 | // [dcl.init.list]p7: |
| 2475 | // |
| 2476 | // A narrowing conversion is an implicit conversion ... |
| 2477 | ImplicitConversionKind PossibleNarrowing = SCS->Second; |
| 2478 | QualType FromType = SCS->getToType(0); |
| 2479 | QualType ToType = SCS->getToType(1); |
| 2480 | switch (PossibleNarrowing) { |
| 2481 | // * from a floating-point type to an integer type, or |
| 2482 | // |
| 2483 | // * from an integer type or unscoped enumeration type to a floating-point |
| 2484 | // type, except where the source is a constant expression and the actual |
| 2485 | // value after conversion will fit into the target type and will produce |
| 2486 | // the original value when converted back to the original type, or |
| 2487 | case ICK_Floating_Integral: |
| 2488 | if (FromType->isRealFloatingType() && ToType->isIntegralType(Ctx)) { |
| 2489 | *isInitializerConstant = false; |
| 2490 | return true; |
| 2491 | } else if (FromType->isIntegralType(Ctx) && ToType->isRealFloatingType()) { |
| 2492 | llvm::APSInt IntConstantValue; |
| 2493 | if (Initializer && |
| 2494 | Initializer->isIntegerConstantExpr(IntConstantValue, Ctx)) { |
| 2495 | // Convert the integer to the floating type. |
| 2496 | llvm::APFloat Result(Ctx.getFloatTypeSemantics(ToType)); |
| 2497 | Result.convertFromAPInt(IntConstantValue, IntConstantValue.isSigned(), |
| 2498 | llvm::APFloat::rmNearestTiesToEven); |
| 2499 | // And back. |
| 2500 | llvm::APSInt ConvertedValue = IntConstantValue; |
| 2501 | bool ignored; |
| 2502 | Result.convertToInteger(ConvertedValue, |
| 2503 | llvm::APFloat::rmTowardZero, &ignored); |
| 2504 | // If the resulting value is different, this was a narrowing conversion. |
| 2505 | if (IntConstantValue != ConvertedValue) { |
| 2506 | *isInitializerConstant = true; |
| 2507 | *ConstantValue = APValue(IntConstantValue); |
| 2508 | return true; |
| 2509 | } |
| 2510 | } else { |
| 2511 | // Variables are always narrowings. |
| 2512 | *isInitializerConstant = false; |
| 2513 | return true; |
| 2514 | } |
| 2515 | } |
| 2516 | return false; |
| 2517 | |
| 2518 | // * from long double to double or float, or from double to float, except |
| 2519 | // where the source is a constant expression and the actual value after |
| 2520 | // conversion is within the range of values that can be represented (even |
| 2521 | // if it cannot be represented exactly), or |
| 2522 | case ICK_Floating_Conversion: |
| 2523 | if (1 == Ctx.getFloatingTypeOrder(FromType, ToType)) { |
| 2524 | // FromType is larger than ToType. |
| 2525 | Expr::EvalResult InitializerValue; |
| 2526 | // FIXME: Check whether Initializer is a constant expression according |
| 2527 | // to C++0x [expr.const], rather than just whether it can be folded. |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 2528 | if (Initializer->EvaluateAsRValue(InitializerValue, Ctx) && |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2529 | !InitializerValue.HasSideEffects && InitializerValue.Val.isFloat()) { |
| 2530 | // Constant! (Except for FIXME above.) |
| 2531 | llvm::APFloat FloatVal = InitializerValue.Val.getFloat(); |
| 2532 | // Convert the source value into the target type. |
| 2533 | bool ignored; |
| 2534 | llvm::APFloat::opStatus ConvertStatus = FloatVal.convert( |
| 2535 | Ctx.getFloatTypeSemantics(ToType), |
| 2536 | llvm::APFloat::rmNearestTiesToEven, &ignored); |
| 2537 | // If there was no overflow, the source value is within the range of |
| 2538 | // values that can be represented. |
| 2539 | if (ConvertStatus & llvm::APFloat::opOverflow) { |
| 2540 | *isInitializerConstant = true; |
| 2541 | *ConstantValue = InitializerValue.Val; |
| 2542 | return true; |
| 2543 | } |
| 2544 | } else { |
| 2545 | *isInitializerConstant = false; |
| 2546 | return true; |
| 2547 | } |
| 2548 | } |
| 2549 | return false; |
| 2550 | |
| 2551 | // * from an integer type or unscoped enumeration type to an integer type |
| 2552 | // that cannot represent all the values of the original type, except where |
| 2553 | // the source is a constant expression and the actual value after |
| 2554 | // conversion will fit into the target type and will produce the original |
| 2555 | // value when converted back to the original type. |
Jeffrey Yasskin | 6d0ee8d | 2011-08-12 20:56:43 +0000 | [diff] [blame] | 2556 | case ICK_Boolean_Conversion: // Bools are integers too. |
Jeffrey Yasskin | b89d5ed | 2011-08-30 22:25:41 +0000 | [diff] [blame] | 2557 | if (!FromType->isIntegralOrUnscopedEnumerationType()) { |
| 2558 | // Boolean conversions can be from pointers and pointers to members |
| 2559 | // [conv.bool], and those aren't considered narrowing conversions. |
| 2560 | return false; |
| 2561 | } // Otherwise, fall through to the integral case. |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 2562 | case ICK_Integral_Conversion: { |
| 2563 | assert(FromType->isIntegralOrUnscopedEnumerationType()); |
| 2564 | assert(ToType->isIntegralOrUnscopedEnumerationType()); |
| 2565 | const bool FromSigned = FromType->isSignedIntegerOrEnumerationType(); |
| 2566 | const unsigned FromWidth = Ctx.getIntWidth(FromType); |
| 2567 | const bool ToSigned = ToType->isSignedIntegerOrEnumerationType(); |
| 2568 | const unsigned ToWidth = Ctx.getIntWidth(ToType); |
| 2569 | |
| 2570 | if (FromWidth > ToWidth || |
| 2571 | (FromWidth == ToWidth && FromSigned != ToSigned)) { |
| 2572 | // Not all values of FromType can be represented in ToType. |
| 2573 | llvm::APSInt InitializerValue; |
| 2574 | if (Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { |
| 2575 | *isInitializerConstant = true; |
| 2576 | *ConstantValue = APValue(InitializerValue); |
| 2577 | |
| 2578 | // Add a bit to the InitializerValue so we don't have to worry about |
| 2579 | // signed vs. unsigned comparisons. |
| 2580 | InitializerValue = InitializerValue.extend( |
| 2581 | InitializerValue.getBitWidth() + 1); |
| 2582 | // Convert the initializer to and from the target width and signed-ness. |
| 2583 | llvm::APSInt ConvertedValue = InitializerValue; |
| 2584 | ConvertedValue = ConvertedValue.trunc(ToWidth); |
| 2585 | ConvertedValue.setIsSigned(ToSigned); |
| 2586 | ConvertedValue = ConvertedValue.extend(InitializerValue.getBitWidth()); |
| 2587 | ConvertedValue.setIsSigned(InitializerValue.isSigned()); |
| 2588 | // If the result is different, this was a narrowing conversion. |
| 2589 | return ConvertedValue != InitializerValue; |
| 2590 | } else { |
| 2591 | // Variables are always narrowings. |
| 2592 | *isInitializerConstant = false; |
| 2593 | return true; |
| 2594 | } |
| 2595 | } |
| 2596 | return false; |
| 2597 | } |
| 2598 | |
| 2599 | default: |
| 2600 | // Other kinds of conversions are not narrowings. |
| 2601 | return false; |
| 2602 | } |
| 2603 | } |
| 2604 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2605 | void |
| 2606 | InitializationSequence |
| 2607 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2608 | DeclAccessPair Found, |
| 2609 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2610 | Step S; |
| 2611 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2612 | S.Type = Function->getType(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2613 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2614 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2615 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2616 | Steps.push_back(S); |
| 2617 | } |
| 2618 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2619 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2620 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2621 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2622 | switch (VK) { |
| 2623 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2624 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2625 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2626 | default: llvm_unreachable("No such category"); |
| 2627 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2628 | S.Type = BaseType; |
| 2629 | Steps.push_back(S); |
| 2630 | } |
| 2631 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2632 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2633 | bool BindingTemporary) { |
| 2634 | Step S; |
| 2635 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2636 | S.Type = T; |
| 2637 | Steps.push_back(S); |
| 2638 | } |
| 2639 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2640 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2641 | Step S; |
| 2642 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2643 | S.Type = T; |
| 2644 | Steps.push_back(S); |
| 2645 | } |
| 2646 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2647 | void |
| 2648 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2649 | DeclAccessPair FoundDecl, |
| 2650 | QualType T, |
| 2651 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2652 | Step S; |
| 2653 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2654 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2655 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2656 | S.Function.Function = Function; |
| 2657 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2658 | Steps.push_back(S); |
| 2659 | } |
| 2660 | |
| 2661 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2662 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2663 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2664 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2665 | switch (VK) { |
| 2666 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2667 | S.Kind = SK_QualificationConversionRValue; |
| 2668 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2669 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2670 | S.Kind = SK_QualificationConversionXValue; |
| 2671 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2672 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2673 | S.Kind = SK_QualificationConversionLValue; |
| 2674 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2675 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2676 | S.Type = Ty; |
| 2677 | Steps.push_back(S); |
| 2678 | } |
| 2679 | |
| 2680 | void InitializationSequence::AddConversionSequenceStep( |
| 2681 | const ImplicitConversionSequence &ICS, |
| 2682 | QualType T) { |
| 2683 | Step S; |
| 2684 | S.Kind = SK_ConversionSequence; |
| 2685 | S.Type = T; |
| 2686 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2687 | Steps.push_back(S); |
| 2688 | } |
| 2689 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2690 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2691 | Step S; |
| 2692 | S.Kind = SK_ListInitialization; |
| 2693 | S.Type = T; |
| 2694 | Steps.push_back(S); |
| 2695 | } |
| 2696 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2697 | void |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2698 | InitializationSequence |
| 2699 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2700 | AccessSpecifier Access, |
| 2701 | QualType T, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2702 | bool HadMultipleCandidates, |
| 2703 | bool FromInitList) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2704 | Step S; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2705 | S.Kind = FromInitList ? SK_ListConstructorCall : SK_ConstructorInitialization; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2706 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2707 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2708 | S.Function.Function = Constructor; |
| 2709 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2710 | Steps.push_back(S); |
| 2711 | } |
| 2712 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2713 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2714 | Step S; |
| 2715 | S.Kind = SK_ZeroInitialization; |
| 2716 | S.Type = T; |
| 2717 | Steps.push_back(S); |
| 2718 | } |
| 2719 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2720 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2721 | Step S; |
| 2722 | S.Kind = SK_CAssignment; |
| 2723 | S.Type = T; |
| 2724 | Steps.push_back(S); |
| 2725 | } |
| 2726 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2727 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2728 | Step S; |
| 2729 | S.Kind = SK_StringInit; |
| 2730 | S.Type = T; |
| 2731 | Steps.push_back(S); |
| 2732 | } |
| 2733 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2734 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2735 | Step S; |
| 2736 | S.Kind = SK_ObjCObjectConversion; |
| 2737 | S.Type = T; |
| 2738 | Steps.push_back(S); |
| 2739 | } |
| 2740 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2741 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2742 | Step S; |
| 2743 | S.Kind = SK_ArrayInit; |
| 2744 | S.Type = T; |
| 2745 | Steps.push_back(S); |
| 2746 | } |
| 2747 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2748 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2749 | bool shouldCopy) { |
| 2750 | Step s; |
| 2751 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2752 | : SK_PassByIndirectRestore); |
| 2753 | s.Type = type; |
| 2754 | Steps.push_back(s); |
| 2755 | } |
| 2756 | |
| 2757 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2758 | Step S; |
| 2759 | S.Kind = SK_ProduceObjCObject; |
| 2760 | S.Type = T; |
| 2761 | Steps.push_back(S); |
| 2762 | } |
| 2763 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2764 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2765 | InitListExpr *Syntactic) { |
| 2766 | assert(Syntactic->getNumInits() == 1 && |
| 2767 | "Can only rewrap trivial init lists."); |
| 2768 | Step S; |
| 2769 | S.Kind = SK_UnwrapInitList; |
| 2770 | S.Type = Syntactic->getInit(0)->getType(); |
| 2771 | Steps.insert(Steps.begin(), S); |
| 2772 | |
| 2773 | S.Kind = SK_RewrapInitList; |
| 2774 | S.Type = T; |
| 2775 | S.WrappingSyntacticList = Syntactic; |
| 2776 | Steps.push_back(S); |
| 2777 | } |
| 2778 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2779 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2780 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2781 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2782 | this->Failure = Failure; |
| 2783 | this->FailedOverloadResult = Result; |
| 2784 | } |
| 2785 | |
| 2786 | //===----------------------------------------------------------------------===// |
| 2787 | // Attempt initialization |
| 2788 | //===----------------------------------------------------------------------===// |
| 2789 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2790 | static void MaybeProduceObjCObject(Sema &S, |
| 2791 | InitializationSequence &Sequence, |
| 2792 | const InitializedEntity &Entity) { |
| 2793 | if (!S.getLangOptions().ObjCAutoRefCount) return; |
| 2794 | |
| 2795 | /// When initializing a parameter, produce the value if it's marked |
| 2796 | /// __attribute__((ns_consumed)). |
| 2797 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2798 | if (!Entity.isParameterConsumed()) |
| 2799 | return; |
| 2800 | |
| 2801 | assert(Entity.getType()->isObjCRetainableType() && |
| 2802 | "consuming an object of unretainable type?"); |
| 2803 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2804 | |
| 2805 | /// When initializing a return value, if the return type is a |
| 2806 | /// retainable type, then returns need to immediately retain the |
| 2807 | /// object. If an autorelease is required, it will be done at the |
| 2808 | /// last instant. |
| 2809 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2810 | if (!Entity.getType()->isObjCRetainableType()) |
| 2811 | return; |
| 2812 | |
| 2813 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2814 | } |
| 2815 | } |
| 2816 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2817 | /// \brief When initializing from init list via constructor, deal with the |
| 2818 | /// empty init list and std::initializer_list special cases. |
| 2819 | /// |
| 2820 | /// \return True if this was a special case, false otherwise. |
| 2821 | static bool TryListConstructionSpecialCases(Sema &S, |
| 2822 | Expr **Args, unsigned NumArgs, |
| 2823 | CXXRecordDecl *DestRecordDecl, |
| 2824 | QualType DestType, |
| 2825 | InitializationSequence &Sequence) { |
| 2826 | // C++0x [dcl.init.list]p3: |
| 2827 | // List-initialization of an object of type T is defined as follows: |
| 2828 | // - If the initializer list has no elements and T is a class type with |
| 2829 | // a default constructor, the object is value-initialized. |
| 2830 | if (NumArgs == 0) { |
| 2831 | if (CXXConstructorDecl *DefaultConstructor = |
| 2832 | S.LookupDefaultConstructor(DestRecordDecl)) { |
| 2833 | if (DefaultConstructor->isDeleted() || |
| 2834 | S.isFunctionConsideredUnavailable(DefaultConstructor)) { |
| 2835 | // Fake an overload resolution failure. |
| 2836 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2837 | DeclAccessPair FoundDecl = DeclAccessPair::make(DefaultConstructor, |
| 2838 | DefaultConstructor->getAccess()); |
| 2839 | if (FunctionTemplateDecl *ConstructorTmpl = |
| 2840 | dyn_cast<FunctionTemplateDecl>(DefaultConstructor)) |
| 2841 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2842 | /*ExplicitArgs*/ 0, |
| 2843 | Args, NumArgs, CandidateSet, |
| 2844 | /*SuppressUserConversions*/ false); |
| 2845 | else |
| 2846 | S.AddOverloadCandidate(DefaultConstructor, FoundDecl, |
| 2847 | Args, NumArgs, CandidateSet, |
| 2848 | /*SuppressUserConversions*/ false); |
| 2849 | Sequence.SetOverloadFailure( |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2850 | InitializationSequence::FK_ListConstructorOverloadFailed, |
| 2851 | OR_Deleted); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2852 | } else |
| 2853 | Sequence.AddConstructorInitializationStep(DefaultConstructor, |
| 2854 | DefaultConstructor->getAccess(), |
| 2855 | DestType, |
| 2856 | /*MultipleCandidates=*/false, |
| 2857 | /*FromInitList=*/true); |
| 2858 | return true; |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | // - Otherwise, if T is a specialization of std::initializer_list, [...] |
| 2863 | // FIXME: Implement. |
| 2864 | |
| 2865 | // Not a special case. |
| 2866 | return false; |
| 2867 | } |
| 2868 | |
| 2869 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2870 | /// enumerates the constructors of the initialized entity and performs overload |
| 2871 | /// resolution to select the best. |
| 2872 | /// If FromInitList is true, this is list-initialization of a non-aggregate |
| 2873 | /// class type. |
| 2874 | static void TryConstructorInitialization(Sema &S, |
| 2875 | const InitializedEntity &Entity, |
| 2876 | const InitializationKind &Kind, |
| 2877 | Expr **Args, unsigned NumArgs, |
| 2878 | QualType DestType, |
| 2879 | InitializationSequence &Sequence, |
| 2880 | bool FromInitList = false) { |
| 2881 | // Check constructor arguments for self reference. |
| 2882 | if (DeclaratorDecl *DD = Entity.getDecl()) |
| 2883 | // Parameters arguments are occassionially constructed with itself, |
| 2884 | // for instance, in recursive functions. Skip them. |
| 2885 | if (!isa<ParmVarDecl>(DD)) |
| 2886 | for (unsigned i = 0; i < NumArgs; ++i) |
| 2887 | S.CheckSelfReference(DD, Args[i]); |
| 2888 | |
| 2889 | // Build the candidate set directly in the initialization sequence |
| 2890 | // structure, so that it will persist if we fail. |
| 2891 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2892 | CandidateSet.clear(); |
| 2893 | |
| 2894 | // Determine whether we are allowed to call explicit constructors or |
| 2895 | // explicit conversion operators. |
| 2896 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 2897 | Kind.getKind() == InitializationKind::IK_Value || |
| 2898 | Kind.getKind() == InitializationKind::IK_Default); |
| 2899 | |
| 2900 | // The type we're constructing needs to be complete. |
| 2901 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
| 2902 | Sequence.SetFailed(InitializationSequence::FK_Incomplete); |
| 2903 | } |
| 2904 | |
| 2905 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 2906 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 2907 | CXXRecordDecl *DestRecordDecl |
| 2908 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 2909 | |
| 2910 | if (FromInitList && |
| 2911 | TryListConstructionSpecialCases(S, Args, NumArgs, DestRecordDecl, |
| 2912 | DestType, Sequence)) |
| 2913 | return; |
| 2914 | |
| 2915 | // - Otherwise, if T is a class type, constructors are considered. The |
| 2916 | // applicable constructors are enumerated, and the best one is chosen |
| 2917 | // through overload resolution. |
| 2918 | DeclContext::lookup_iterator Con, ConEnd; |
| 2919 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
| 2920 | Con != ConEnd; ++Con) { |
| 2921 | NamedDecl *D = *Con; |
| 2922 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2923 | bool SuppressUserConversions = false; |
| 2924 | |
| 2925 | // Find the constructor (which may be a template). |
| 2926 | CXXConstructorDecl *Constructor = 0; |
| 2927 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 2928 | if (ConstructorTmpl) |
| 2929 | Constructor = cast<CXXConstructorDecl>( |
| 2930 | ConstructorTmpl->getTemplatedDecl()); |
| 2931 | else { |
| 2932 | Constructor = cast<CXXConstructorDecl>(D); |
| 2933 | |
| 2934 | // If we're performing copy initialization using a copy constructor, we |
| 2935 | // suppress user-defined conversions on the arguments. |
| 2936 | // FIXME: Move constructors? |
| 2937 | if (Kind.getKind() == InitializationKind::IK_Copy && |
| 2938 | Constructor->isCopyConstructor()) |
| 2939 | SuppressUserConversions = true; |
| 2940 | } |
| 2941 | |
| 2942 | if (!Constructor->isInvalidDecl() && |
| 2943 | (AllowExplicit || !Constructor->isExplicit())) { |
| 2944 | if (ConstructorTmpl) |
| 2945 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 2946 | /*ExplicitArgs*/ 0, |
| 2947 | Args, NumArgs, CandidateSet, |
| 2948 | SuppressUserConversions); |
| 2949 | else |
| 2950 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 2951 | Args, NumArgs, CandidateSet, |
| 2952 | SuppressUserConversions); |
| 2953 | } |
| 2954 | } |
| 2955 | |
| 2956 | SourceLocation DeclLoc = Kind.getLocation(); |
| 2957 | |
| 2958 | // Perform overload resolution. If it fails, return the failed result. |
| 2959 | OverloadCandidateSet::iterator Best; |
| 2960 | if (OverloadingResult Result |
| 2961 | = CandidateSet.BestViableFunction(S, DeclLoc, Best)) { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2962 | Sequence.SetOverloadFailure(FromInitList ? |
| 2963 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 2964 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2965 | Result); |
| 2966 | return; |
| 2967 | } |
| 2968 | |
| 2969 | // C++0x [dcl.init]p6: |
| 2970 | // If a program calls for the default initialization of an object |
| 2971 | // of a const-qualified type T, T shall be a class type with a |
| 2972 | // user-provided default constructor. |
| 2973 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 2974 | Entity.getType().isConstQualified() && |
| 2975 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 2976 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2977 | return; |
| 2978 | } |
| 2979 | |
| 2980 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 2981 | // subsumed by the initialization. |
| 2982 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 2983 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 2984 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 2985 | Best->FoundDecl.getAccess(), |
| 2986 | DestType, HadMultipleCandidates, |
| 2987 | FromInitList); |
| 2988 | } |
| 2989 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2990 | static bool |
| 2991 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 2992 | Expr *Initializer, |
| 2993 | QualType &SourceType, |
| 2994 | QualType &UnqualifiedSourceType, |
| 2995 | QualType UnqualifiedTargetType, |
| 2996 | InitializationSequence &Sequence) { |
| 2997 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 2998 | S.Context.OverloadTy) { |
| 2999 | DeclAccessPair Found; |
| 3000 | bool HadMultipleCandidates = false; |
| 3001 | if (FunctionDecl *Fn |
| 3002 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3003 | UnqualifiedTargetType, |
| 3004 | false, Found, |
| 3005 | &HadMultipleCandidates)) { |
| 3006 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3007 | HadMultipleCandidates); |
| 3008 | SourceType = Fn->getType(); |
| 3009 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3010 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3011 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3012 | return true; |
| 3013 | } |
| 3014 | } |
| 3015 | return false; |
| 3016 | } |
| 3017 | |
| 3018 | static void TryReferenceInitializationCore(Sema &S, |
| 3019 | const InitializedEntity &Entity, |
| 3020 | const InitializationKind &Kind, |
| 3021 | Expr *Initializer, |
| 3022 | QualType cv1T1, QualType T1, |
| 3023 | Qualifiers T1Quals, |
| 3024 | QualType cv2T2, QualType T2, |
| 3025 | Qualifiers T2Quals, |
| 3026 | InitializationSequence &Sequence); |
| 3027 | |
| 3028 | static void TryListInitialization(Sema &S, |
| 3029 | const InitializedEntity &Entity, |
| 3030 | const InitializationKind &Kind, |
| 3031 | InitListExpr *InitList, |
| 3032 | InitializationSequence &Sequence); |
| 3033 | |
| 3034 | /// \brief Attempt list initialization of a reference. |
| 3035 | static void TryReferenceListInitialization(Sema &S, |
| 3036 | const InitializedEntity &Entity, |
| 3037 | const InitializationKind &Kind, |
| 3038 | InitListExpr *InitList, |
| 3039 | InitializationSequence &Sequence) |
| 3040 | { |
| 3041 | // First, catch C++03 where this isn't possible. |
| 3042 | if (!S.getLangOptions().CPlusPlus0x) { |
| 3043 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3044 | return; |
| 3045 | } |
| 3046 | |
| 3047 | QualType DestType = Entity.getType(); |
| 3048 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3049 | Qualifiers T1Quals; |
| 3050 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3051 | |
| 3052 | // Reference initialization via an initializer list works thus: |
| 3053 | // If the initializer list consists of a single element that is |
| 3054 | // reference-related to the referenced type, bind directly to that element |
| 3055 | // (possibly creating temporaries). |
| 3056 | // Otherwise, initialize a temporary with the initializer list and |
| 3057 | // bind to that. |
| 3058 | if (InitList->getNumInits() == 1) { |
| 3059 | Expr *Initializer = InitList->getInit(0); |
| 3060 | QualType cv2T2 = Initializer->getType(); |
| 3061 | Qualifiers T2Quals; |
| 3062 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3063 | |
| 3064 | // If this fails, creating a temporary wouldn't work either. |
| 3065 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3066 | T1, Sequence)) |
| 3067 | return; |
| 3068 | |
| 3069 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3070 | bool dummy1, dummy2, dummy3; |
| 3071 | Sema::ReferenceCompareResult RefRelationship |
| 3072 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3073 | dummy2, dummy3); |
| 3074 | if (RefRelationship >= Sema::Ref_Related) { |
| 3075 | // Try to bind the reference here. |
| 3076 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3077 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3078 | if (Sequence) |
| 3079 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3080 | return; |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | // Not reference-related. Create a temporary and bind to that. |
| 3085 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3086 | |
| 3087 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3088 | if (Sequence) { |
| 3089 | if (DestType->isRValueReferenceType() || |
| 3090 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3091 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3092 | else |
| 3093 | Sequence.SetFailed( |
| 3094 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3095 | } |
| 3096 | } |
| 3097 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3098 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3099 | static void TryListInitialization(Sema &S, |
| 3100 | const InitializedEntity &Entity, |
| 3101 | const InitializationKind &Kind, |
| 3102 | InitListExpr *InitList, |
| 3103 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3104 | QualType DestType = Entity.getType(); |
| 3105 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3106 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3107 | // But C99 complex numbers are scalars and it makes sense there. |
| 3108 | if (S.getLangOptions().CPlusPlus && DestType->isScalarType() && |
| 3109 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3110 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3111 | return; |
| 3112 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3113 | if (DestType->isReferenceType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3114 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3115 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3116 | } |
| 3117 | if (DestType->isRecordType() && !DestType->isAggregateType()) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3118 | if (S.getLangOptions().CPlusPlus0x) |
| 3119 | TryConstructorInitialization(S, Entity, Kind, InitList->getInits(), |
| 3120 | InitList->getNumInits(), DestType, Sequence, |
| 3121 | /*FromInitList=*/true); |
| 3122 | else |
| 3123 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3124 | return; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3127 | InitListChecker CheckInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 3128 | DestType, /*VerifyOnly=*/true, |
| 3129 | Kind.getKind() != InitializationKind::IK_Direct || |
| 3130 | !S.getLangOptions().CPlusPlus0x); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3131 | if (CheckInitList.HadError()) { |
| 3132 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3133 | return; |
| 3134 | } |
| 3135 | |
| 3136 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3137 | Sequence.AddListInitializationStep(DestType); |
| 3138 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3139 | |
| 3140 | /// \brief Try a reference initialization that involves calling a conversion |
| 3141 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3142 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3143 | const InitializedEntity &Entity, |
| 3144 | const InitializationKind &Kind, |
| 3145 | Expr *Initializer, |
| 3146 | bool AllowRValues, |
| 3147 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3148 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3149 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3150 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3151 | QualType cv2T2 = Initializer->getType(); |
| 3152 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3153 | |
| 3154 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3155 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3156 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3157 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3158 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3159 | ObjCConversion, |
| 3160 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3161 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3162 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3163 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3164 | (void)ObjCLifetimeConversion; |
| 3165 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3166 | // Build the candidate set directly in the initialization sequence |
| 3167 | // structure, so that it will persist if we fail. |
| 3168 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3169 | CandidateSet.clear(); |
| 3170 | |
| 3171 | // Determine whether we are allowed to call explicit constructors or |
| 3172 | // explicit conversion operators. |
| 3173 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3174 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3175 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3176 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3177 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3178 | // The type we're converting to is a class type. Enumerate its constructors |
| 3179 | // to see if there is a suitable conversion. |
| 3180 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3181 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3182 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3183 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3184 | Con != ConEnd; ++Con) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3185 | NamedDecl *D = *Con; |
| 3186 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3187 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3188 | // Find the constructor (which may be a template). |
| 3189 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3190 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3191 | if (ConstructorTmpl) |
| 3192 | Constructor = cast<CXXConstructorDecl>( |
| 3193 | ConstructorTmpl->getTemplatedDecl()); |
| 3194 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3195 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3196 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3197 | if (!Constructor->isInvalidDecl() && |
| 3198 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3199 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3200 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3201 | /*ExplicitArgs*/ 0, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3202 | &Initializer, 1, CandidateSet, |
| 3203 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3204 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3205 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3206 | &Initializer, 1, CandidateSet, |
| 3207 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3208 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3209 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3210 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3211 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3212 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3213 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3214 | const RecordType *T2RecordType = 0; |
| 3215 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3216 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3217 | // The type we're converting from is a class type, enumerate its conversion |
| 3218 | // functions. |
| 3219 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3220 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3221 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3222 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3223 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 3224 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3225 | NamedDecl *D = *I; |
| 3226 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3227 | if (isa<UsingShadowDecl>(D)) |
| 3228 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3229 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3230 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3231 | CXXConversionDecl *Conv; |
| 3232 | if (ConvTemplate) |
| 3233 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3234 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3235 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3236 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3237 | // If the conversion function doesn't return a reference type, |
| 3238 | // it can't be considered for this conversion unless we're allowed to |
| 3239 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3240 | // FIXME: Do we need to make sure that we only consider conversion |
| 3241 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3242 | // break recursion. |
| 3243 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 3244 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3245 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3246 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3247 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3248 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3249 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3250 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3251 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3252 | } |
| 3253 | } |
| 3254 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3255 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3256 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3257 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3258 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3259 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3260 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3261 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3262 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3263 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3264 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3265 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3266 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3267 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3268 | // This is the overload that will actually be used for the initialization, so |
| 3269 | // mark it as used. |
| 3270 | S.MarkDeclarationReferenced(DeclLoc, Function); |
| 3271 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3272 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3273 | if (isa<CXXConversionDecl>(Function)) |
| 3274 | T2 = Function->getResultType(); |
| 3275 | else |
| 3276 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3277 | |
| 3278 | // Add the user-defined conversion step. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3279 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3280 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3281 | T2.getNonLValueExprType(S.Context), |
| 3282 | HadMultipleCandidates); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3283 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3284 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3285 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3286 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3287 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3288 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3289 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3290 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3291 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3292 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3293 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3294 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3295 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3296 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3297 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3298 | NewDerivedToBase, NewObjCConversion, |
| 3299 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3300 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3301 | // If the type we've converted to is not reference-related to the |
| 3302 | // type we're looking for, then there is another conversion step |
| 3303 | // we need to perform to produce a temporary of the right type |
| 3304 | // that we'll be binding to. |
| 3305 | ImplicitConversionSequence ICS; |
| 3306 | ICS.setStandard(); |
| 3307 | ICS.Standard = Best->FinalConversion; |
| 3308 | T2 = ICS.Standard.getToType(2); |
| 3309 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3310 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3311 | Sequence.AddDerivedToBaseCastStep( |
| 3312 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3313 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3314 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3315 | else if (NewObjCConversion) |
| 3316 | Sequence.AddObjCObjectConversionStep( |
| 3317 | S.Context.getQualifiedType(T1, |
| 3318 | T2.getNonReferenceType().getQualifiers())); |
| 3319 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3320 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3321 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3322 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3323 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3324 | return OR_Success; |
| 3325 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3326 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3327 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3328 | const InitializedEntity &Entity, |
| 3329 | Expr *CurInitExpr); |
| 3330 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3331 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3332 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3333 | const InitializedEntity &Entity, |
| 3334 | const InitializationKind &Kind, |
| 3335 | Expr *Initializer, |
| 3336 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3337 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3338 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3339 | Qualifiers T1Quals; |
| 3340 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3341 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3342 | Qualifiers T2Quals; |
| 3343 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3344 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3345 | // If the initializer is the address of an overloaded function, try |
| 3346 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3347 | // type of the resulting function. |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3348 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3349 | T1, Sequence)) |
| 3350 | return; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3351 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3352 | // Delegate everything else to a subfunction. |
| 3353 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3354 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3355 | } |
| 3356 | |
| 3357 | /// \brief Reference initialization without resolving overloaded functions. |
| 3358 | static void TryReferenceInitializationCore(Sema &S, |
| 3359 | const InitializedEntity &Entity, |
| 3360 | const InitializationKind &Kind, |
| 3361 | Expr *Initializer, |
| 3362 | QualType cv1T1, QualType T1, |
| 3363 | Qualifiers T1Quals, |
| 3364 | QualType cv2T2, QualType T2, |
| 3365 | Qualifiers T2Quals, |
| 3366 | InitializationSequence &Sequence) { |
| 3367 | QualType DestType = Entity.getType(); |
| 3368 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3369 | // Compute some basic properties of the types and the initializer. |
| 3370 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3371 | bool isRValueRef = !isLValueRef; |
| 3372 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3373 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3374 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3375 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3376 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3377 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3378 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3379 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3380 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3381 | // 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] | 3382 | // "cv2 T2" as follows: |
| 3383 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3384 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3385 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3386 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3387 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3388 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3389 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3390 | bool T1Function = T1->isFunctionType(); |
| 3391 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3392 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3393 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3395 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3396 | // - 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] | 3397 | // reference-compatible with "cv2 T2," or |
| 3398 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3399 | // 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] | 3400 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3401 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3402 | // to decide whether we're actually binding to a temporary created from |
| 3403 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3404 | if (DerivedToBase) |
| 3405 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3406 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3407 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3408 | else if (ObjCConversion) |
| 3409 | Sequence.AddObjCObjectConversionStep( |
| 3410 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3411 | |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3412 | if (T1Quals != T2Quals) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3413 | Sequence.AddQualificationConversionStep(cv1T1, VK_LValue); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3414 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3415 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3416 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3417 | return; |
| 3418 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3419 | |
| 3420 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3421 | // reference-related to T2, and can be implicitly converted to an |
| 3422 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3423 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3424 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3425 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3426 | // If we have an rvalue ref to function type here, the rhs must be |
| 3427 | // an rvalue. |
| 3428 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3429 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3430 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3431 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3432 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3433 | Sequence); |
| 3434 | if (ConvOvlResult == OR_Success) |
| 3435 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3436 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3437 | Sequence.SetOverloadFailure( |
| 3438 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3439 | ConvOvlResult); |
| 3440 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3441 | } |
| 3442 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3443 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3444 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3445 | // 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] | 3446 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3447 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3448 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3449 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3450 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3451 | Sequence.SetOverloadFailure( |
| 3452 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3453 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3454 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3455 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3456 | ? (RefRelationship == Sema::Ref_Related |
| 3457 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3458 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3459 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3460 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3461 | return; |
| 3462 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3463 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3464 | // - If the initializer expression |
| 3465 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3466 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3467 | // Note: functions are handled below. |
| 3468 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3469 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3470 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3471 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3472 | (InitCategory.isXValue() || |
| 3473 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3474 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3475 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3476 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3477 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3478 | // compiler the freedom to perform a copy here or bind to the |
| 3479 | // object, while C++0x requires that we bind directly to the |
| 3480 | // object. Hence, we always bind to the object without making an |
| 3481 | // extra copy. However, in C++03 requires that we check for the |
| 3482 | // presence of a suitable copy constructor: |
| 3483 | // |
| 3484 | // The constructor that would be used to make the copy shall |
| 3485 | // be callable whether or not the copy is actually done. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3486 | if (!S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3487 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3488 | else if (S.getLangOptions().CPlusPlus0x) |
| 3489 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3490 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3491 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3492 | if (DerivedToBase) |
| 3493 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3494 | ValueKind); |
| 3495 | else if (ObjCConversion) |
| 3496 | Sequence.AddObjCObjectConversionStep( |
| 3497 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3498 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3499 | if (T1Quals != T2Quals) |
| 3500 | Sequence.AddQualificationConversionStep(cv1T1, ValueKind); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3501 | Sequence.AddReferenceBindingStep(cv1T1, |
Peter Collingbourne | 65bfd68 | 2011-11-13 00:51:30 +0000 | [diff] [blame] | 3502 | /*bindingTemporary=*/InitCategory.isPRValue()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3503 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3504 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3505 | |
| 3506 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3507 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3508 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3509 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3510 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3511 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3512 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3513 | Kind, Initializer, |
| 3514 | /*AllowRValues=*/true, |
| 3515 | Sequence); |
| 3516 | if (ConvOvlResult) |
| 3517 | Sequence.SetOverloadFailure( |
| 3518 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3519 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3520 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3521 | return; |
| 3522 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3523 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3524 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3525 | return; |
| 3526 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3527 | |
| 3528 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3529 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3530 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3531 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3532 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3533 | // Determine whether we are allowed to call explicit constructors or |
| 3534 | // explicit conversion operators. |
| 3535 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3536 | |
| 3537 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3538 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3539 | ImplicitConversionSequence ICS |
| 3540 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3541 | /*SuppressUserConversions*/ false, |
| 3542 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3543 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3544 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3545 | /*AllowObjCWritebackConversion=*/false); |
| 3546 | |
| 3547 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3548 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3549 | // this into an overloading ambiguity diagnostic. However, we need |
| 3550 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3551 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3552 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3553 | Sequence.SetOverloadFailure( |
| 3554 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3555 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3556 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3557 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3558 | else |
| 3559 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3560 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3561 | } else { |
| 3562 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3566 | // same cv-qualification as, or greater cv-qualification |
| 3567 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3568 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3569 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3570 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3571 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3572 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3573 | return; |
| 3574 | } |
| 3575 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3576 | // [...] 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] | 3577 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3578 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3579 | InitCategory.isLValue()) { |
| 3580 | Sequence.SetFailed( |
| 3581 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3582 | return; |
| 3583 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3584 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3585 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3586 | return; |
| 3587 | } |
| 3588 | |
| 3589 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3590 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3591 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3592 | const InitializedEntity &Entity, |
| 3593 | const InitializationKind &Kind, |
| 3594 | Expr *Initializer, |
| 3595 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3596 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3599 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3600 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3601 | const InitializedEntity &Entity, |
| 3602 | const InitializationKind &Kind, |
| 3603 | InitializationSequence &Sequence) { |
| 3604 | // C++ [dcl.init]p5: |
| 3605 | // |
| 3606 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3607 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3608 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3609 | // -- if T is an array type, then each element is value-initialized; |
| 3610 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 3611 | T = AT->getElementType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3612 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3613 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3614 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3615 | // -- if T is a class type (clause 9) with a user-declared |
| 3616 | // constructor (12.1), then the default constructor for T is |
| 3617 | // called (and the initialization is ill-formed if T has no |
| 3618 | // accessible default constructor); |
| 3619 | // |
| 3620 | // FIXME: we really want to refer to a single subobject of the array, |
| 3621 | // but Entity doesn't have a way to capture that (yet). |
| 3622 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 3623 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3624 | |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3625 | // -- if T is a (possibly cv-qualified) non-union class type |
| 3626 | // without a user-provided constructor, then the object is |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3627 | // zero-initialized and, if T's implicitly-declared default |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3628 | // constructor is non-trivial, that constructor is called. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3629 | if ((ClassDecl->getTagKind() == TTK_Class || |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 3630 | ClassDecl->getTagKind() == TTK_Struct)) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3631 | Sequence.AddZeroInitializationStep(Entity.getType()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3632 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3633 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3634 | } |
| 3635 | } |
| 3636 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3637 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3640 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3641 | static void TryDefaultInitialization(Sema &S, |
| 3642 | const InitializedEntity &Entity, |
| 3643 | const InitializationKind &Kind, |
| 3644 | InitializationSequence &Sequence) { |
| 3645 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3646 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3647 | // C++ [dcl.init]p6: |
| 3648 | // To default-initialize an object of type T means: |
| 3649 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3650 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3651 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3652 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3653 | // constructor for T is called (and the initialization is ill-formed if |
| 3654 | // T has no accessible default constructor); |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 3655 | if (DestType->isRecordType() && S.getLangOptions().CPlusPlus) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3656 | TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence); |
| 3657 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3658 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3659 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3660 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3661 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3662 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3663 | // 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] | 3664 | // default constructor. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3665 | if (DestType.isConstQualified() && S.getLangOptions().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3666 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3667 | return; |
| 3668 | } |
| 3669 | |
| 3670 | // If the destination type has a lifetime property, zero-initialize it. |
| 3671 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3672 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3673 | return; |
| 3674 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3677 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3678 | /// which enumerates all conversion functions and performs overload resolution |
| 3679 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3680 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3681 | const InitializedEntity &Entity, |
| 3682 | const InitializationKind &Kind, |
| 3683 | Expr *Initializer, |
| 3684 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3685 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3686 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3687 | QualType SourceType = Initializer->getType(); |
| 3688 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3689 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3690 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3691 | // Build the candidate set directly in the initialization sequence |
| 3692 | // structure, so that it will persist if we fail. |
| 3693 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3694 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3695 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3696 | // Determine whether we are allowed to call explicit constructors or |
| 3697 | // explicit conversion operators. |
| 3698 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3699 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3700 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3701 | // The type we're converting to is a class type. Enumerate its constructors |
| 3702 | // to see if there is a suitable conversion. |
| 3703 | CXXRecordDecl *DestRecordDecl |
| 3704 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3705 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3706 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3707 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3708 | DeclContext::lookup_iterator Con, ConEnd; |
Douglas Gregor | e5eee5a | 2010-07-02 23:12:18 +0000 | [diff] [blame] | 3709 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3710 | Con != ConEnd; ++Con) { |
| 3711 | NamedDecl *D = *Con; |
| 3712 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3713 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3714 | // Find the constructor (which may be a template). |
| 3715 | CXXConstructorDecl *Constructor = 0; |
| 3716 | FunctionTemplateDecl *ConstructorTmpl |
| 3717 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3718 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3719 | Constructor = cast<CXXConstructorDecl>( |
| 3720 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3721 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3722 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3723 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3724 | if (!Constructor->isInvalidDecl() && |
| 3725 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3726 | if (ConstructorTmpl) |
| 3727 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3728 | /*ExplicitArgs*/ 0, |
| 3729 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3730 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3731 | else |
| 3732 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 3733 | &Initializer, 1, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3734 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3735 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3736 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3737 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3738 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3739 | |
| 3740 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3741 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3742 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3743 | // The type we're converting from is a class type, enumerate its conversion |
| 3744 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3745 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3746 | // We can only enumerate the conversion functions for a complete type; if |
| 3747 | // the type isn't complete, simply skip this step. |
| 3748 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3749 | CXXRecordDecl *SourceRecordDecl |
| 3750 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3751 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3752 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3753 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 3754 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3755 | E = Conversions->end(); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3756 | I != E; ++I) { |
| 3757 | NamedDecl *D = *I; |
| 3758 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3759 | if (isa<UsingShadowDecl>(D)) |
| 3760 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3761 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3762 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3763 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3764 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3765 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3766 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3767 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3768 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3769 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3770 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3771 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3772 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3773 | CandidateSet); |
| 3774 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3775 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3776 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3777 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3778 | } |
| 3779 | } |
| 3780 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3781 | |
| 3782 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3783 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3784 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3785 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3786 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3787 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3788 | Result); |
| 3789 | return; |
| 3790 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3791 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3792 | FunctionDecl *Function = Best->Function; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3793 | S.MarkDeclarationReferenced(DeclLoc, Function); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3794 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3795 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3796 | if (isa<CXXConstructorDecl>(Function)) { |
| 3797 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 3798 | // subsumed by the initialization. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3799 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 3800 | HadMultipleCandidates); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3801 | return; |
| 3802 | } |
| 3803 | |
| 3804 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3805 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3806 | if (ConvType->getAs<RecordType>()) { |
| 3807 | // If we're converting to a class type, there may be an copy if |
| 3808 | // the resulting temporary object (possible to create an object of |
| 3809 | // a base class type). That copy is not a separate conversion, so |
| 3810 | // we just make a note of the actual destination type (possibly a |
| 3811 | // base class of the type returned by the conversion function) and |
| 3812 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3813 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 3814 | HadMultipleCandidates); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3815 | return; |
| 3816 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3817 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3818 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 3819 | HadMultipleCandidates); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3820 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 3821 | // If the conversion following the call to the conversion function |
| 3822 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3823 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 3824 | Best->FinalConversion.Third) { |
| 3825 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3826 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3827 | ICS.Standard = Best->FinalConversion; |
| 3828 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 3829 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3830 | } |
| 3831 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3832 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 3833 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 3834 | |
| 3835 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3836 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
| 3837 | bool isAddressOf) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3838 | // Skip parens. |
| 3839 | e = e->IgnoreParens(); |
| 3840 | |
| 3841 | // Skip address-of nodes. |
| 3842 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 3843 | if (op->getOpcode() == UO_AddrOf) |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3844 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3845 | |
| 3846 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3847 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 3848 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3849 | case CK_Dependent: |
| 3850 | case CK_BitCast: |
| 3851 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3852 | case CK_NoOp: |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3853 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3854 | |
| 3855 | case CK_ArrayToPointerDecay: |
| 3856 | return IIK_nonscalar; |
| 3857 | |
| 3858 | case CK_NullToPointer: |
| 3859 | return IIK_okay; |
| 3860 | |
| 3861 | default: |
| 3862 | break; |
| 3863 | } |
| 3864 | |
| 3865 | // 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] | 3866 | } else if (isa<DeclRefExpr>(e) || isa<BlockDeclRefExpr>(e)) { |
| 3867 | if (!isAddressOf) return IIK_nonlocal; |
| 3868 | |
| 3869 | VarDecl *var; |
| 3870 | if (isa<DeclRefExpr>(e)) { |
| 3871 | var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 3872 | if (!var) return IIK_nonlocal; |
| 3873 | } else { |
| 3874 | var = cast<BlockDeclRefExpr>(e)->getDecl(); |
| 3875 | } |
| 3876 | |
| 3877 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3878 | |
| 3879 | // If we have a conditional operator, check both sides. |
| 3880 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3881 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3882 | return iik; |
| 3883 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3884 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3885 | |
| 3886 | // These are never scalar. |
| 3887 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 3888 | return IIK_nonscalar; |
| 3889 | |
| 3890 | // Otherwise, it needs to be a null pointer constant. |
| 3891 | } else { |
| 3892 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 3893 | ? IIK_okay : IIK_nonlocal); |
| 3894 | } |
| 3895 | |
| 3896 | return IIK_nonlocal; |
| 3897 | } |
| 3898 | |
| 3899 | /// Check whether the given expression is a valid operand for an |
| 3900 | /// indirect copy/restore. |
| 3901 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 3902 | assert(src->isRValue()); |
| 3903 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 3904 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3905 | if (iik == IIK_okay) return; |
| 3906 | |
| 3907 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 3908 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 3909 | << src->getSourceRange(); |
| 3910 | } |
| 3911 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 3912 | /// \brief Determine whether we have compatible array types for the |
| 3913 | /// purposes of GNU by-copy array initialization. |
| 3914 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 3915 | const ArrayType *Dest, |
| 3916 | const ArrayType *Source) { |
| 3917 | // If the source and destination array types are equivalent, we're |
| 3918 | // done. |
| 3919 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 3920 | return true; |
| 3921 | |
| 3922 | // Make sure that the element types are the same. |
| 3923 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 3924 | return false; |
| 3925 | |
| 3926 | // The only mismatch we allow is when the destination is an |
| 3927 | // incomplete array type and the source is a constant array type. |
| 3928 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 3929 | } |
| 3930 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3931 | static bool tryObjCWritebackConversion(Sema &S, |
| 3932 | InitializationSequence &Sequence, |
| 3933 | const InitializedEntity &Entity, |
| 3934 | Expr *Initializer) { |
| 3935 | bool ArrayDecay = false; |
| 3936 | QualType ArgType = Initializer->getType(); |
| 3937 | QualType ArgPointee; |
| 3938 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 3939 | ArrayDecay = true; |
| 3940 | ArgPointee = ArgArrayType->getElementType(); |
| 3941 | ArgType = S.Context.getPointerType(ArgPointee); |
| 3942 | } |
| 3943 | |
| 3944 | // Handle write-back conversion. |
| 3945 | QualType ConvertedArgType; |
| 3946 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 3947 | ConvertedArgType)) |
| 3948 | return false; |
| 3949 | |
| 3950 | // We should copy unless we're passing to an argument explicitly |
| 3951 | // marked 'out'. |
| 3952 | bool ShouldCopy = true; |
| 3953 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 3954 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 3955 | |
| 3956 | // Do we need an lvalue conversion? |
| 3957 | if (ArrayDecay || Initializer->isGLValue()) { |
| 3958 | ImplicitConversionSequence ICS; |
| 3959 | ICS.setStandard(); |
| 3960 | ICS.Standard.setAsIdentityConversion(); |
| 3961 | |
| 3962 | QualType ResultType; |
| 3963 | if (ArrayDecay) { |
| 3964 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 3965 | ResultType = S.Context.getPointerType(ArgPointee); |
| 3966 | } else { |
| 3967 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 3968 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 3969 | } |
| 3970 | |
| 3971 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 3972 | } |
| 3973 | |
| 3974 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 3975 | return true; |
| 3976 | } |
| 3977 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3978 | InitializationSequence::InitializationSequence(Sema &S, |
| 3979 | const InitializedEntity &Entity, |
| 3980 | const InitializationKind &Kind, |
| 3981 | Expr **Args, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3982 | unsigned NumArgs) |
| 3983 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3984 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3985 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3986 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3987 | // The semantics of initializers are as follows. The destination type is |
| 3988 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3989 | // 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] | 3990 | // 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] | 3991 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3992 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3993 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3994 | if (DestType->isDependentType() || |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3995 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 3996 | SequenceKind = DependentSequence; |
| 3997 | return; |
| 3998 | } |
| 3999 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4000 | // Almost everything is a normal sequence. |
| 4001 | setSequenceKind(NormalSequence); |
| 4002 | |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 4003 | for (unsigned I = 0; I != NumArgs; ++I) |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4004 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4005 | // FIXME: should we be doing this here? |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4006 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4007 | if (result.isInvalid()) { |
| 4008 | SetFailed(FK_PlaceholderType); |
| 4009 | return; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4010 | } |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4011 | Args[I] = result.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4012 | } |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 4013 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4014 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4015 | QualType SourceType; |
| 4016 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4017 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4018 | Initializer = Args[0]; |
| 4019 | if (!isa<InitListExpr>(Initializer)) |
| 4020 | SourceType = Initializer->getType(); |
| 4021 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4022 | |
| 4023 | // - If the initializer is a braced-init-list, the object is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4024 | // list-initialized (8.5.4). |
| 4025 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4026 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4027 | return; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4028 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4029 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4030 | // - If the destination type is a reference type, see 8.5.3. |
| 4031 | if (DestType->isReferenceType()) { |
| 4032 | // C++0x [dcl.init.ref]p1: |
| 4033 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4034 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4035 | // by an object that can be converted into a T. |
| 4036 | // (Therefore, multiple arguments are not permitted.) |
| 4037 | if (NumArgs != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4038 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4039 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4040 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4041 | return; |
| 4042 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4043 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4044 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4045 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 4046 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4047 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4048 | return; |
| 4049 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4050 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4051 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4052 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4053 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4054 | return; |
| 4055 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4056 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4057 | // - If the destination type is an array of characters, an array of |
| 4058 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4059 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4060 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4061 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4062 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
| 4063 | if (Initializer && IsStringInit(Initializer, DestAT, Context)) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4064 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4065 | return; |
| 4066 | } |
| 4067 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4068 | // Note: as an GNU C extension, we allow initialization of an |
| 4069 | // array from a compound literal that creates an array of the same |
| 4070 | // type, so long as the initializer has no side effects. |
| 4071 | if (!S.getLangOptions().CPlusPlus && Initializer && |
| 4072 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4073 | Initializer->getType()->isArrayType()) { |
| 4074 | const ArrayType *SourceAT |
| 4075 | = Context.getAsArrayType(Initializer->getType()); |
| 4076 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4077 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4078 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4079 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4080 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4081 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4082 | } |
| 4083 | } else if (DestAT->getElementType()->isAnyCharacterType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4084 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4085 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4086 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4087 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4088 | return; |
| 4089 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4090 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4091 | // Determine whether we should consider writeback conversions for |
| 4092 | // Objective-C ARC. |
| 4093 | bool allowObjCWritebackConversion = S.getLangOptions().ObjCAutoRefCount && |
| 4094 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 4095 | |
| 4096 | // We're at the end of the line for C: it's either a write-back conversion |
| 4097 | // 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] | 4098 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4099 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4100 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4101 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4102 | return; |
| 4103 | } |
| 4104 | |
| 4105 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4106 | AddCAssignmentStep(DestType); |
| 4107 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4108 | return; |
| 4109 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4110 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4111 | assert(S.getLangOptions().CPlusPlus); |
| 4112 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4113 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4114 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4115 | // - If the initialization is direct-initialization, or if it is |
| 4116 | // copy-initialization where the cv-unqualified version of the |
| 4117 | // 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] | 4118 | // class of the destination, constructors are considered. [...] |
| 4119 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4120 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4121 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4122 | S.IsDerivedFrom(SourceType, DestType)))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4123 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4124 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4125 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4126 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4127 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4128 | // used) to a derived class thereof are enumerated as described in |
| 4129 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4130 | // (13.3). |
| 4131 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4132 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4133 | return; |
| 4134 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4135 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4136 | if (NumArgs > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4137 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4138 | return; |
| 4139 | } |
| 4140 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4141 | |
| 4142 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4143 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4144 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4145 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 4146 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4147 | return; |
| 4148 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4149 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4150 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4151 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4152 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4153 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4154 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4155 | |
| 4156 | ImplicitConversionSequence ICS |
| 4157 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4158 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4159 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4160 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4161 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4162 | allowObjCWritebackConversion); |
| 4163 | |
| 4164 | if (ICS.isStandard() && |
| 4165 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4166 | // Objective-C ARC writeback conversion. |
| 4167 | |
| 4168 | // We should copy unless we're passing to an argument explicitly |
| 4169 | // marked 'out'. |
| 4170 | bool ShouldCopy = true; |
| 4171 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4172 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4173 | |
| 4174 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4175 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4176 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4177 | ImplicitConversionSequence LvalueICS; |
| 4178 | LvalueICS.setStandard(); |
| 4179 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4180 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4181 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4182 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4183 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4184 | |
| 4185 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4186 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4187 | DeclAccessPair dap; |
| 4188 | if (Initializer->getType() == Context.OverloadTy && |
| 4189 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 4190 | , DestType, false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4191 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4192 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4193 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4194 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4195 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4196 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4197 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4198 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4202 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4203 | StepEnd = Steps.end(); |
| 4204 | Step != StepEnd; ++Step) |
| 4205 | Step->Destroy(); |
| 4206 | } |
| 4207 | |
| 4208 | //===----------------------------------------------------------------------===// |
| 4209 | // Perform initialization |
| 4210 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4211 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4212 | getAssignmentAction(const InitializedEntity &Entity) { |
| 4213 | switch(Entity.getKind()) { |
| 4214 | case InitializedEntity::EK_Variable: |
| 4215 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4216 | case InitializedEntity::EK_Exception: |
| 4217 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4218 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4219 | return Sema::AA_Initializing; |
| 4220 | |
| 4221 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4222 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4223 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4224 | return Sema::AA_Sending; |
| 4225 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4226 | return Sema::AA_Passing; |
| 4227 | |
| 4228 | case InitializedEntity::EK_Result: |
| 4229 | return Sema::AA_Returning; |
| 4230 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4231 | case InitializedEntity::EK_Temporary: |
| 4232 | // FIXME: Can we tell apart casting vs. converting? |
| 4233 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4234 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4235 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4236 | case InitializedEntity::EK_ArrayElement: |
| 4237 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4238 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4239 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4240 | return Sema::AA_Initializing; |
| 4241 | } |
| 4242 | |
| 4243 | return Sema::AA_Converting; |
| 4244 | } |
| 4245 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4246 | /// \brief Whether we should binding a created object as a temporary when |
| 4247 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4248 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4249 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4250 | case InitializedEntity::EK_ArrayElement: |
| 4251 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4252 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4253 | case InitializedEntity::EK_New: |
| 4254 | case InitializedEntity::EK_Variable: |
| 4255 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4256 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4257 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4258 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4259 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4260 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4261 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4262 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4263 | case InitializedEntity::EK_Parameter: |
| 4264 | case InitializedEntity::EK_Temporary: |
| 4265 | return true; |
| 4266 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4267 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4268 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4269 | } |
| 4270 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4271 | /// \brief Whether the given entity, when initialized with an object |
| 4272 | /// created for that initialization, requires destruction. |
| 4273 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4274 | switch (Entity.getKind()) { |
| 4275 | case InitializedEntity::EK_Member: |
| 4276 | case InitializedEntity::EK_Result: |
| 4277 | case InitializedEntity::EK_New: |
| 4278 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4279 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4280 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4281 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4282 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4283 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4284 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4285 | case InitializedEntity::EK_Variable: |
| 4286 | case InitializedEntity::EK_Parameter: |
| 4287 | case InitializedEntity::EK_Temporary: |
| 4288 | case InitializedEntity::EK_ArrayElement: |
| 4289 | case InitializedEntity::EK_Exception: |
| 4290 | return true; |
| 4291 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4292 | |
| 4293 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4296 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4297 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4298 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4299 | OverloadCandidateSet &CandidateSet, |
| 4300 | CXXRecordDecl *Class, |
| 4301 | Expr *CurInitExpr) { |
| 4302 | DeclContext::lookup_iterator Con, ConEnd; |
| 4303 | for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class); |
| 4304 | Con != ConEnd; ++Con) { |
| 4305 | CXXConstructorDecl *Constructor = 0; |
| 4306 | |
| 4307 | if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) { |
| 4308 | // Handle copy/moveconstructors, only. |
| 4309 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4310 | !Constructor->isCopyOrMoveConstructor() || |
| 4311 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4312 | continue; |
| 4313 | |
| 4314 | DeclAccessPair FoundDecl |
| 4315 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4316 | S.AddOverloadCandidate(Constructor, FoundDecl, |
| 4317 | &CurInitExpr, 1, CandidateSet); |
| 4318 | continue; |
| 4319 | } |
| 4320 | |
| 4321 | // Handle constructor templates. |
| 4322 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con); |
| 4323 | if (ConstructorTmpl->isInvalidDecl()) |
| 4324 | continue; |
| 4325 | |
| 4326 | Constructor = cast<CXXConstructorDecl>( |
| 4327 | ConstructorTmpl->getTemplatedDecl()); |
| 4328 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4329 | continue; |
| 4330 | |
| 4331 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4332 | // candidates? |
| 4333 | DeclAccessPair FoundDecl |
| 4334 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4335 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
| 4336 | &CurInitExpr, 1, CandidateSet, true); |
| 4337 | } |
| 4338 | } |
| 4339 | |
| 4340 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4341 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4342 | Expr *Initializer) { |
| 4343 | switch (Entity.getKind()) { |
| 4344 | case InitializedEntity::EK_Result: |
| 4345 | return Entity.getReturnLoc(); |
| 4346 | |
| 4347 | case InitializedEntity::EK_Exception: |
| 4348 | return Entity.getThrowLoc(); |
| 4349 | |
| 4350 | case InitializedEntity::EK_Variable: |
| 4351 | return Entity.getDecl()->getLocation(); |
| 4352 | |
| 4353 | case InitializedEntity::EK_ArrayElement: |
| 4354 | case InitializedEntity::EK_Member: |
| 4355 | case InitializedEntity::EK_Parameter: |
| 4356 | case InitializedEntity::EK_Temporary: |
| 4357 | case InitializedEntity::EK_New: |
| 4358 | case InitializedEntity::EK_Base: |
| 4359 | case InitializedEntity::EK_Delegating: |
| 4360 | case InitializedEntity::EK_VectorElement: |
| 4361 | case InitializedEntity::EK_ComplexElement: |
| 4362 | case InitializedEntity::EK_BlockElement: |
| 4363 | return Initializer->getLocStart(); |
| 4364 | } |
| 4365 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4366 | } |
| 4367 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4368 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4369 | /// provided by the given initializer by calling the appropriate copy |
| 4370 | /// constructor. |
| 4371 | /// |
| 4372 | /// \param S The Sema object used for type-checking. |
| 4373 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4374 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4375 | /// the type of the initializer expression or a superclass thereof. |
| 4376 | /// |
| 4377 | /// \param Enter The entity being initialized. |
| 4378 | /// |
| 4379 | /// \param CurInit The initializer expression. |
| 4380 | /// |
| 4381 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4382 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4383 | /// an rvalue. |
| 4384 | /// |
| 4385 | /// \returns An expression that copies the initializer expression into |
| 4386 | /// a temporary object, or an error expression if a copy could not be |
| 4387 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4388 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4389 | QualType T, |
| 4390 | const InitializedEntity &Entity, |
| 4391 | ExprResult CurInit, |
| 4392 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4393 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4394 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4395 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4396 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4397 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4398 | if (!Class) |
| 4399 | return move(CurInit); |
| 4400 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4401 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4402 | // When certain criteria are met, an implementation is allowed to |
| 4403 | // omit the copy/move construction of a class object, even if the |
| 4404 | // copy/move constructor and/or destructor for the object have |
| 4405 | // side effects. [...] |
| 4406 | // - when a temporary class object that has not been bound to a |
| 4407 | // reference (12.2) would be copied/moved to a class object |
| 4408 | // with the same cv-unqualified type, the copy/move operation |
| 4409 | // can be omitted by constructing the temporary object |
| 4410 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4411 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4412 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4413 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4414 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4415 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4416 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4417 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4418 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4419 | // Make sure that the type we are copying is complete. |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4420 | if (S.RequireCompleteType(Loc, T, S.PDiag(diag::err_temp_copy_incomplete))) |
| 4421 | return move(CurInit); |
| 4422 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4423 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4424 | // Only consider constructors and constructor templates. Per |
| 4425 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4426 | // is direct-initialization. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4427 | OverloadCandidateSet CandidateSet(Loc); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4428 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4429 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4430 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4431 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4432 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4433 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4434 | case OR_Success: |
| 4435 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4436 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4437 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4438 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4439 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4440 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4441 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4442 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4443 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4444 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4445 | return ExprError(); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4446 | return move(CurInit); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4447 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4448 | case OR_Ambiguous: |
| 4449 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4450 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4451 | << CurInitExpr->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 4452 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4453 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4454 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4455 | case OR_Deleted: |
| 4456 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4457 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4458 | << CurInitExpr->getSourceRange(); |
| 4459 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4460 | << 1 << Best->Function->isDeleted(); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4461 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4464 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4465 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4466 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4467 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4468 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4469 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4470 | |
| 4471 | if (IsExtraneousCopy) { |
| 4472 | // If this is a totally extraneous copy for C++03 reference |
| 4473 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4474 | // expression. We don't generate an (elided) copy operation here |
| 4475 | // because doing so would require us to pass down a flag to avoid |
| 4476 | // infinite recursion, where each step adds another extraneous, |
| 4477 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4478 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4479 | // Instantiate the default arguments of any extra parameters in |
| 4480 | // the selected copy constructor, as if we were going to create a |
| 4481 | // proper call to the copy constructor. |
| 4482 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4483 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4484 | if (S.RequireCompleteType(Loc, Parm->getType(), |
| 4485 | S.PDiag(diag::err_call_incomplete_argument))) |
| 4486 | break; |
| 4487 | |
| 4488 | // Build the default argument expression; we don't actually care |
| 4489 | // if this succeeds or not, because this routine will complain |
| 4490 | // if there was a problem. |
| 4491 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4492 | } |
| 4493 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4494 | return S.Owned(CurInitExpr); |
| 4495 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4496 | |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4497 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 4498 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4499 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4500 | // constructor call (we might have derived-to-base conversions, or |
| 4501 | // the copy constructor may have default arguments). |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4502 | if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1), |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4503 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4504 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4505 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4506 | // Actually perform the constructor call. |
| 4507 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4508 | move_arg(ConstructorArgs), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4509 | HadMultipleCandidates, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4510 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4511 | CXXConstructExpr::CK_Complete, |
| 4512 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4513 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4514 | // If we're supposed to bind temporaries, do so. |
| 4515 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4516 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4517 | return move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4518 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4519 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4520 | /// \brief Check whether elidable copy construction for binding a reference to |
| 4521 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 4522 | /// -Wc++98-compat. |
| 4523 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4524 | const InitializedEntity &Entity, |
| 4525 | Expr *CurInitExpr) { |
| 4526 | assert(S.getLangOptions().CPlusPlus0x); |
| 4527 | |
| 4528 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 4529 | if (!Record) |
| 4530 | return; |
| 4531 | |
| 4532 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 4533 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 4534 | == DiagnosticsEngine::Ignored) |
| 4535 | return; |
| 4536 | |
| 4537 | // Find constructors which would have been considered. |
| 4538 | OverloadCandidateSet CandidateSet(Loc); |
| 4539 | LookupCopyAndMoveConstructors( |
| 4540 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 4541 | |
| 4542 | // Perform overload resolution. |
| 4543 | OverloadCandidateSet::iterator Best; |
| 4544 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 4545 | |
| 4546 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 4547 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 4548 | << CurInitExpr->getSourceRange(); |
| 4549 | |
| 4550 | switch (OR) { |
| 4551 | case OR_Success: |
| 4552 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
| 4553 | Best->FoundDecl.getAccess(), Diag); |
| 4554 | // FIXME: Check default arguments as far as that's possible. |
| 4555 | break; |
| 4556 | |
| 4557 | case OR_No_Viable_Function: |
| 4558 | S.Diag(Loc, Diag); |
| 4559 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, &CurInitExpr, 1); |
| 4560 | break; |
| 4561 | |
| 4562 | case OR_Ambiguous: |
| 4563 | S.Diag(Loc, Diag); |
| 4564 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, &CurInitExpr, 1); |
| 4565 | break; |
| 4566 | |
| 4567 | case OR_Deleted: |
| 4568 | S.Diag(Loc, Diag); |
| 4569 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 4570 | << 1 << Best->Function->isDeleted(); |
| 4571 | break; |
| 4572 | } |
| 4573 | } |
| 4574 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4575 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4576 | const InitializedEntity &Entity) { |
| 4577 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4578 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4579 | return; |
| 4580 | |
| 4581 | if (Entity.getDecl()->getDeclName()) |
| 4582 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4583 | << Entity.getDecl()->getDeclName(); |
| 4584 | else |
| 4585 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4586 | } |
| 4587 | } |
| 4588 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4589 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4590 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4591 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4592 | } |
| 4593 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4594 | static ExprResult |
| 4595 | PerformConstructorInitialization(Sema &S, |
| 4596 | const InitializedEntity &Entity, |
| 4597 | const InitializationKind &Kind, |
| 4598 | MultiExprArg Args, |
| 4599 | const InitializationSequence::Step& Step, |
| 4600 | bool &ConstructorInitRequiresZeroInit) { |
| 4601 | unsigned NumArgs = Args.size(); |
| 4602 | CXXConstructorDecl *Constructor |
| 4603 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 4604 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 4605 | |
| 4606 | // Build a call to the selected constructor. |
| 4607 | ASTOwningVector<Expr*> ConstructorArgs(S); |
| 4608 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4609 | ? Kind.getEqualLoc() |
| 4610 | : Kind.getLocation(); |
| 4611 | |
| 4612 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4613 | // Force even a trivial, implicit default constructor to be |
| 4614 | // semantically checked. We do this explicitly because we don't build |
| 4615 | // the definition for completely trivial constructors. |
| 4616 | CXXRecordDecl *ClassDecl = Constructor->getParent(); |
| 4617 | assert(ClassDecl && "No parent class for constructor."); |
| 4618 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
| 4619 | ClassDecl->hasTrivialDefaultConstructor() && |
| 4620 | !Constructor->isUsed(false)) |
| 4621 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4622 | } |
| 4623 | |
| 4624 | ExprResult CurInit = S.Owned((Expr *)0); |
| 4625 | |
| 4626 | // Determine the arguments required to actually perform the constructor |
| 4627 | // call. |
| 4628 | if (S.CompleteConstructorCall(Constructor, move(Args), |
| 4629 | Loc, ConstructorArgs)) |
| 4630 | return ExprError(); |
| 4631 | |
| 4632 | |
| 4633 | if (Entity.getKind() == InitializedEntity::EK_Temporary && |
| 4634 | NumArgs != 1 && // FIXME: Hack to work around cast weirdness |
| 4635 | (Kind.getKind() == InitializationKind::IK_Direct || |
| 4636 | Kind.getKind() == InitializationKind::IK_Value)) { |
| 4637 | // An explicitly-constructed temporary, e.g., X(1, 2). |
| 4638 | unsigned NumExprs = ConstructorArgs.size(); |
| 4639 | Expr **Exprs = (Expr **)ConstructorArgs.take(); |
| 4640 | S.MarkDeclarationReferenced(Loc, Constructor); |
| 4641 | S.DiagnoseUseOfDecl(Constructor, Loc); |
| 4642 | |
| 4643 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4644 | if (!TSInfo) |
| 4645 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
| 4646 | |
| 4647 | CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context, |
| 4648 | Constructor, |
| 4649 | TSInfo, |
| 4650 | Exprs, |
| 4651 | NumExprs, |
| 4652 | Kind.getParenRange(), |
| 4653 | HadMultipleCandidates, |
| 4654 | ConstructorInitRequiresZeroInit)); |
| 4655 | } else { |
| 4656 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 4657 | CXXConstructExpr::CK_Complete; |
| 4658 | |
| 4659 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 4660 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 4661 | CXXConstructExpr::CK_VirtualBase : |
| 4662 | CXXConstructExpr::CK_NonVirtualBase; |
| 4663 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 4664 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 4665 | } |
| 4666 | |
| 4667 | // Only get the parenthesis range if it is a direct construction. |
| 4668 | SourceRange parenRange = |
| 4669 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 4670 | Kind.getParenRange() : SourceRange(); |
| 4671 | |
| 4672 | // If the entity allows NRVO, mark the construction as elidable |
| 4673 | // unconditionally. |
| 4674 | if (Entity.allowsNRVO()) |
| 4675 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4676 | Constructor, /*Elidable=*/true, |
| 4677 | move_arg(ConstructorArgs), |
| 4678 | HadMultipleCandidates, |
| 4679 | ConstructorInitRequiresZeroInit, |
| 4680 | ConstructKind, |
| 4681 | parenRange); |
| 4682 | else |
| 4683 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 4684 | Constructor, |
| 4685 | move_arg(ConstructorArgs), |
| 4686 | HadMultipleCandidates, |
| 4687 | ConstructorInitRequiresZeroInit, |
| 4688 | ConstructKind, |
| 4689 | parenRange); |
| 4690 | } |
| 4691 | if (CurInit.isInvalid()) |
| 4692 | return ExprError(); |
| 4693 | |
| 4694 | // Only check access if all of that succeeded. |
| 4695 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 4696 | Step.Function.FoundDecl.getAccess()); |
| 4697 | S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc); |
| 4698 | |
| 4699 | if (shouldBindAsTemporary(Entity)) |
| 4700 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 4701 | |
| 4702 | return move(CurInit); |
| 4703 | } |
| 4704 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4705 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4706 | InitializationSequence::Perform(Sema &S, |
| 4707 | const InitializedEntity &Entity, |
| 4708 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4709 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4710 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 4711 | if (Failed()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4712 | unsigned NumArgs = Args.size(); |
| 4713 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4714 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4715 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4716 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4717 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4718 | // If the declaration is a non-dependent, incomplete array type |
| 4719 | // that has an initializer, then its type will be completed once |
| 4720 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4721 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4722 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4723 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4724 | if (const IncompleteArrayType *ArrayT |
| 4725 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 4726 | // FIXME: We don't currently have the ability to accurately |
| 4727 | // compute the length of an initializer list without |
| 4728 | // performing full type-checking of the initializer list |
| 4729 | // (since we have to determine where braces are implicitly |
| 4730 | // introduced and such). So, we fall back to making the array |
| 4731 | // type a dependently-sized array type with no specified |
| 4732 | // bound. |
| 4733 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 4734 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4735 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4736 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4737 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 4738 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 4739 | TypeLoc TL = TInfo->getTypeLoc(); |
| 4740 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 4741 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 4742 | Brackets = ArrayLoc->getBracketsRange(); |
| 4743 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4744 | } |
| 4745 | |
| 4746 | *ResultType |
| 4747 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 4748 | /*NumElts=*/0, |
| 4749 | ArrayT->getSizeModifier(), |
| 4750 | ArrayT->getIndexTypeCVRQualifiers(), |
| 4751 | Brackets); |
| 4752 | } |
| 4753 | |
| 4754 | } |
| 4755 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4756 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
| 4757 | Kind.isExplicitCast()); |
| 4758 | return ExprResult(Args.release()[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4761 | // No steps means no initialization. |
| 4762 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4763 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4764 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4765 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 4766 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4767 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 4768 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 4769 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 4770 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4771 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4772 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4773 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4774 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4775 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4776 | // grab the only argument out the Args and place it into the "current" |
| 4777 | // initializer. |
| 4778 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4779 | case SK_ResolveAddressOfOverloadedFunction: |
| 4780 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4781 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4782 | case SK_CastDerivedToBaseLValue: |
| 4783 | case SK_BindReference: |
| 4784 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4785 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4786 | case SK_UserConversion: |
| 4787 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4788 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4789 | case SK_QualificationConversionRValue: |
| 4790 | case SK_ConversionSequence: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 4791 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4792 | case SK_ListInitialization: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 4793 | case SK_UnwrapInitList: |
| 4794 | case SK_RewrapInitList: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4795 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4796 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4797 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4798 | case SK_ArrayInit: |
| 4799 | case SK_PassByIndirectCopyRestore: |
| 4800 | case SK_PassByIndirectRestore: |
| 4801 | case SK_ProduceObjCObject: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4802 | assert(Args.size() == 1); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4803 | CurInit = Args.get()[0]; |
| 4804 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4805 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4806 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4807 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4808 | case SK_ConstructorInitialization: |
| 4809 | case SK_ZeroInitialization: |
| 4810 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4811 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4812 | |
| 4813 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4814 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 4815 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4816 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 4817 | Step != StepEnd; ++Step) { |
| 4818 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4819 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4820 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4821 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4822 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4823 | switch (Step->Kind) { |
| 4824 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4825 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4826 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4827 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4828 | S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4829 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4830 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4831 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4832 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4833 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4834 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4835 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4836 | case SK_CastDerivedToBaseLValue: { |
| 4837 | // We have a derived-to-base cast that produces either an rvalue or an |
| 4838 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4839 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4840 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4841 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4842 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 4843 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 4844 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4845 | CurInit.get()->getLocStart(), |
| 4846 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4847 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4848 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4849 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4850 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 4851 | QualType T = SourceType; |
| 4852 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 4853 | T = Pointer->getPointeeType(); |
| 4854 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4855 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 4856 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 4857 | } |
| 4858 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4859 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4860 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4861 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 4862 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4863 | VK_XValue : |
| 4864 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4865 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 4866 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4867 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 4868 | CurInit.get(), |
| 4869 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4870 | break; |
| 4871 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4872 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4873 | case SK_BindReference: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4874 | if (FieldDecl *BitField = CurInit.get()->getBitField()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4875 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 4876 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4877 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4878 | << BitField->getDeclName() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4879 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4880 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4881 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4882 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 4883 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4884 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 4885 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4886 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 4887 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4888 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4889 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4890 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 4891 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4892 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4893 | // Reference binding does not have any corresponding ASTs. |
| 4894 | |
| 4895 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4896 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4897 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4898 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4899 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 4900 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4901 | case SK_BindReferenceToTemporary: |
| 4902 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4903 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4904 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4905 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4906 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 4907 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 4908 | Entity.getType().getNonReferenceType(), |
| 4909 | CurInit.get(), |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 4910 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 4911 | |
| 4912 | // If we're binding to an Objective-C object that has lifetime, we |
| 4913 | // need cleanups. |
| 4914 | if (S.getLangOptions().ObjCAutoRefCount && |
| 4915 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 4916 | S.ExprNeedsCleanups = true; |
| 4917 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4918 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4919 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4920 | case SK_ExtraneousCopyToTemporary: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4921 | CurInit = CopyObject(S, Step->Type, Entity, move(CurInit), |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4922 | /*IsExtraneousCopy=*/true); |
| 4923 | break; |
| 4924 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4925 | case SK_UserConversion: { |
| 4926 | // We have a user-defined conversion that invokes either a constructor |
| 4927 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4928 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4929 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4930 | FunctionDecl *Fn = Step->Function.Function; |
| 4931 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4932 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4933 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4934 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4935 | // Build a call to the selected constructor. |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4936 | ASTOwningVector<Expr*> ConstructorArgs(S); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4937 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4938 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4939 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4940 | // Determine the arguments required to actually perform the constructor |
| 4941 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4942 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4943 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4944 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4945 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4946 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4947 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4948 | // Build the an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4949 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4950 | move_arg(ConstructorArgs), |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4951 | HadMultipleCandidates, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4952 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4953 | CXXConstructExpr::CK_Complete, |
| 4954 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4955 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4956 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4957 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4958 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4959 | FoundFn.getAccess()); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4960 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4961 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4962 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4963 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 4964 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 4965 | S.IsDerivedFrom(SourceType, Class)) |
| 4966 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4967 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4968 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4969 | } else { |
| 4970 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 4971 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4972 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4973 | FoundFn); |
John McCall | b697e08 | 2010-05-06 18:15:07 +0000 | [diff] [blame] | 4974 | S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4975 | |
| 4976 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4977 | // derived-to-base conversion? I believe the answer is "no", because |
| 4978 | // 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] | 4979 | ExprResult CurInitExprRes = |
| 4980 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 4981 | FoundFn, Conversion); |
| 4982 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4983 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4984 | CurInit = move(CurInitExprRes); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4985 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4986 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4987 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 4988 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4989 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4990 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4991 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4992 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4993 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4994 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4995 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4996 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4997 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 4998 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 4999 | |
| 5000 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5001 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5002 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5003 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5004 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5005 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5006 | S.PDiag(diag::err_access_dtor_temp) << T); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5007 | S.MarkDeclarationReferenced(CurInit.get()->getLocStart(), Destructor); |
| 5008 | S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5009 | } |
| 5010 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5011 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5012 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5013 | CurInit.get()->getType(), |
| 5014 | CastKind, CurInit.get(), 0, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 5015 | CurInit.get()->getValueKind())); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5016 | if (MaybeBindToTemp) |
| 5017 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5018 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5019 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
| 5020 | move(CurInit), /*IsExtraneousCopy=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5021 | break; |
| 5022 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5023 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5024 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5025 | case SK_QualificationConversionXValue: |
| 5026 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5027 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5028 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5029 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5030 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5031 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5032 | VK_XValue : |
| 5033 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5034 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5035 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5038 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5039 | Sema::CheckedConversionKind CCK |
| 5040 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 5041 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 5042 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5043 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5044 | ExprResult CurInitExprRes = |
| 5045 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5046 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5047 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5048 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5049 | CurInit = move(CurInitExprRes); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5050 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5051 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5052 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5053 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5054 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5055 | // Hack: We must pass *ResultType if available in order to set the type |
| 5056 | // of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 5057 | // But in 'const X &x = {1, 2, 3};' we're supposed to initialize a |
| 5058 | // temporary, not a reference, so we should pass Ty. |
| 5059 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 5060 | // Since this step is never used for a reference directly, we explicitly |
| 5061 | // unwrap references here and rewrap them afterwards. |
| 5062 | // We also need to create a InitializeTemporary entity for this. |
| 5063 | QualType Ty = ResultType ? ResultType->getNonReferenceType() : Step->Type; |
| 5064 | bool IsTemporary = ResultType && (*ResultType)->isReferenceType(); |
| 5065 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
| 5066 | InitListChecker PerformInitList(S, IsTemporary ? TempEntity : Entity, |
| 5067 | InitList, Ty, /*VerifyOnly=*/false, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 5068 | Kind.getKind() != InitializationKind::IK_Direct || |
| 5069 | !S.getLangOptions().CPlusPlus0x); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5070 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5071 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5072 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5073 | if (ResultType) { |
| 5074 | if ((*ResultType)->isRValueReferenceType()) |
| 5075 | Ty = S.Context.getRValueReferenceType(Ty); |
| 5076 | else if ((*ResultType)->isLValueReferenceType()) |
| 5077 | Ty = S.Context.getLValueReferenceType(Ty, |
| 5078 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 5079 | *ResultType = Ty; |
| 5080 | } |
| 5081 | |
| 5082 | InitListExpr *StructuredInitList = |
| 5083 | PerformInitList.getFullyStructuredList(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5084 | CurInit.release(); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5085 | CurInit = S.Owned(StructuredInitList); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5086 | break; |
| 5087 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5088 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5089 | case SK_ListConstructorCall: { |
| 5090 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
| 5091 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
| 5092 | CurInit = PerformConstructorInitialization(S, Entity, Kind, |
| 5093 | move(Arg), *Step, |
| 5094 | ConstructorInitRequiresZeroInit); |
| 5095 | break; |
| 5096 | } |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5097 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5098 | case SK_UnwrapInitList: |
| 5099 | CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0)); |
| 5100 | break; |
| 5101 | |
| 5102 | case SK_RewrapInitList: { |
| 5103 | Expr *E = CurInit.take(); |
| 5104 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 5105 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
| 5106 | Syntactic->getLBraceLoc(), &E, 1, Syntactic->getRBraceLoc()); |
| 5107 | ILE->setSyntacticForm(Syntactic); |
| 5108 | ILE->setType(E->getType()); |
| 5109 | ILE->setValueKind(E->getValueKind()); |
| 5110 | CurInit = S.Owned(ILE); |
| 5111 | break; |
| 5112 | } |
| 5113 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5114 | case SK_ConstructorInitialization: |
| 5115 | CurInit = PerformConstructorInitialization(S, Entity, Kind, move(Args), |
| 5116 | *Step, |
| 5117 | ConstructorInitRequiresZeroInit); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5118 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5119 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5120 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5121 | step_iterator NextStep = Step; |
| 5122 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5123 | if (NextStep != StepEnd && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5124 | NextStep->Kind == SK_ConstructorInitialization) { |
| 5125 | // The need for zero-initialization is recorded directly into |
| 5126 | // the call to the object's constructor within the next step. |
| 5127 | ConstructorInitRequiresZeroInit = true; |
| 5128 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 5129 | S.getLangOptions().CPlusPlus && |
| 5130 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5131 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5132 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5133 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5134 | Kind.getRange().getBegin()); |
| 5135 | |
| 5136 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 5137 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 5138 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5139 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5140 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5141 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5142 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5143 | break; |
| 5144 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5145 | |
| 5146 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5147 | QualType SourceType = CurInit.get()->getType(); |
| 5148 | ExprResult Result = move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5149 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5150 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 5151 | if (Result.isInvalid()) |
| 5152 | return ExprError(); |
| 5153 | CurInit = move(Result); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5154 | |
| 5155 | // If this is a call, allow conversion to a transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5156 | ExprResult CurInitExprRes = move(CurInit); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5157 | if (ConvTy != Sema::Compatible && |
| 5158 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5159 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5160 | == Sema::Compatible) |
| 5161 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5162 | if (CurInitExprRes.isInvalid()) |
| 5163 | return ExprError(); |
| 5164 | CurInit = move(CurInitExprRes); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5165 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5166 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5167 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 5168 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5169 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5170 | getAssignmentAction(Entity), |
| 5171 | &Complained)) { |
| 5172 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5173 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5174 | } else if (Complained) |
| 5175 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5176 | break; |
| 5177 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5178 | |
| 5179 | case SK_StringInit: { |
| 5180 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5181 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 5182 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5183 | break; |
| 5184 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5185 | |
| 5186 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5187 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5188 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 5189 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5190 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5191 | |
| 5192 | case SK_ArrayInit: |
| 5193 | // Okay: we checked everything before creating this step. Note that |
| 5194 | // this is a GNU extension. |
| 5195 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5196 | << Step->Type << CurInit.get()->getType() |
| 5197 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5198 | |
| 5199 | // If the destination type is an incomplete array type, update the |
| 5200 | // type accordingly. |
| 5201 | if (ResultType) { |
| 5202 | if (const IncompleteArrayType *IncompleteDest |
| 5203 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 5204 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5205 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5206 | *ResultType = S.Context.getConstantArrayType( |
| 5207 | IncompleteDest->getElementType(), |
| 5208 | ConstantSource->getSize(), |
| 5209 | ArrayType::Normal, 0); |
| 5210 | } |
| 5211 | } |
| 5212 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5213 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5214 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5215 | case SK_PassByIndirectCopyRestore: |
| 5216 | case SK_PassByIndirectRestore: |
| 5217 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 5218 | CurInit = S.Owned(new (S.Context) |
| 5219 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 5220 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 5221 | break; |
| 5222 | |
| 5223 | case SK_ProduceObjCObject: |
| 5224 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5225 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5226 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5227 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5228 | } |
| 5229 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5230 | |
| 5231 | // Diagnose non-fatal problems with the completed initialization. |
| 5232 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5233 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 5234 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 5235 | cast<FieldDecl>(Entity.getDecl()), |
| 5236 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5237 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5238 | return move(CurInit); |
| 5239 | } |
| 5240 | |
| 5241 | //===----------------------------------------------------------------------===// |
| 5242 | // Diagnose initialization failures |
| 5243 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5244 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5245 | const InitializedEntity &Entity, |
| 5246 | const InitializationKind &Kind, |
| 5247 | Expr **Args, unsigned NumArgs) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5248 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5249 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5250 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5251 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5252 | switch (Failure) { |
| 5253 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5254 | // FIXME: Customize for the initialized entity? |
| 5255 | if (NumArgs == 0) |
| 5256 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 5257 | << DestType.getNonReferenceType(); |
| 5258 | else // FIXME: diagnostic below could be better! |
| 5259 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 5260 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5261 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5262 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5263 | case FK_ArrayNeedsInitList: |
| 5264 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5265 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 5266 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 5267 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5268 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5269 | case FK_ArrayTypeMismatch: |
| 5270 | case FK_NonConstantArrayInit: |
| 5271 | S.Diag(Kind.getLocation(), |
| 5272 | (Failure == FK_ArrayTypeMismatch |
| 5273 | ? diag::err_array_init_different_type |
| 5274 | : diag::err_array_init_non_constant_array)) |
| 5275 | << DestType.getNonReferenceType() |
| 5276 | << Args[0]->getType() |
| 5277 | << Args[0]->getSourceRange(); |
| 5278 | break; |
| 5279 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5280 | case FK_AddressOfOverloadFailed: { |
| 5281 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5282 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5283 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5284 | true, |
| 5285 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5286 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5287 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5288 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5289 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5290 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5291 | switch (FailedOverloadResult) { |
| 5292 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5293 | if (Failure == FK_UserConversionOverloadFailed) |
| 5294 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 5295 | << Args[0]->getType() << DestType |
| 5296 | << Args[0]->getSourceRange(); |
| 5297 | else |
| 5298 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 5299 | << DestType << Args[0]->getType() |
| 5300 | << Args[0]->getSourceRange(); |
| 5301 | |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5302 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5303 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5304 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5305 | case OR_No_Viable_Function: |
| 5306 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 5307 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5308 | << Args[0]->getSourceRange(); |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5309 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5310 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5311 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5312 | case OR_Deleted: { |
| 5313 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 5314 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5315 | << Args[0]->getSourceRange(); |
| 5316 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5317 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5318 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 5319 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5320 | if (Ovl == OR_Deleted) { |
| 5321 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5322 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5323 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5324 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5325 | } |
| 5326 | break; |
| 5327 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5328 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5329 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5330 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5331 | break; |
| 5332 | } |
| 5333 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5334 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5335 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5336 | if (isa<InitListExpr>(Args[0])) { |
| 5337 | S.Diag(Kind.getLocation(), |
| 5338 | diag::err_lvalue_reference_bind_to_initlist) |
| 5339 | << DestType.getNonReferenceType().isVolatileQualified() |
| 5340 | << DestType.getNonReferenceType() |
| 5341 | << Args[0]->getSourceRange(); |
| 5342 | break; |
| 5343 | } |
| 5344 | // Intentional fallthrough |
| 5345 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5346 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5347 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5348 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 5349 | ? diag::err_lvalue_reference_bind_to_temporary |
| 5350 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 5351 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5352 | << DestType.getNonReferenceType() |
| 5353 | << Args[0]->getType() |
| 5354 | << Args[0]->getSourceRange(); |
| 5355 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5356 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5357 | case FK_RValueReferenceBindingToLValue: |
| 5358 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 5359 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5360 | << Args[0]->getSourceRange(); |
| 5361 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5362 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5363 | case FK_ReferenceInitDropsQualifiers: |
| 5364 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 5365 | << DestType.getNonReferenceType() |
| 5366 | << Args[0]->getType() |
| 5367 | << Args[0]->getSourceRange(); |
| 5368 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5369 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5370 | case FK_ReferenceInitFailed: |
| 5371 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 5372 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5373 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5374 | << Args[0]->getType() |
| 5375 | << Args[0]->getSourceRange(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 5376 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5377 | Args[0]->getType()->isObjCObjectPointerType()) |
| 5378 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5379 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5380 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5381 | case FK_ConversionFailed: { |
| 5382 | QualType FromType = Args[0]->getType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5383 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5384 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5385 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 5386 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5387 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5388 | << Args[0]->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5389 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 5390 | S.Diag(Kind.getLocation(), PDiag); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 5391 | if (DestType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5392 | Args[0]->getType()->isObjCObjectPointerType()) |
| 5393 | S.EmitRelatedResultTypeNote(Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5394 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5395 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5396 | |
| 5397 | case FK_ConversionFromPropertyFailed: |
| 5398 | // No-op. This error has already been reported. |
| 5399 | break; |
| 5400 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5401 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5402 | SourceRange R; |
| 5403 | |
| 5404 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5405 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5406 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5407 | else |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5408 | R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5409 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 5410 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 5411 | if (Kind.isCStyleOrFunctionalCast()) |
| 5412 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 5413 | << R; |
| 5414 | else |
| 5415 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 5416 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5417 | break; |
| 5418 | } |
| 5419 | |
| 5420 | case FK_ReferenceBindingToInitList: |
| 5421 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 5422 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 5423 | break; |
| 5424 | |
| 5425 | case FK_InitListBadDestinationType: |
| 5426 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 5427 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 5428 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5429 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5430 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5431 | case FK_ConstructorOverloadFailed: { |
| 5432 | SourceRange ArgsRange; |
| 5433 | if (NumArgs) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5434 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5435 | Args[NumArgs - 1]->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5436 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 5437 | if (Failure == FK_ListConstructorOverloadFailed) { |
| 5438 | assert(NumArgs == 1 && "List construction from other than 1 argument."); |
| 5439 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 5440 | Args = InitList->getInits(); |
| 5441 | NumArgs = InitList->getNumInits(); |
| 5442 | } |
| 5443 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5444 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 5445 | // bad. |
| 5446 | switch (FailedOverloadResult) { |
| 5447 | case OR_Ambiguous: |
| 5448 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 5449 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5450 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, |
| 5451 | Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5452 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5453 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5454 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5455 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 5456 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 5457 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 5458 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5459 | // This is implicit default initialization of a member or |
| 5460 | // base within a constructor. If no viable function was |
| 5461 | // found, notify the user that she needs to explicitly |
| 5462 | // initialize this base/member. |
| 5463 | CXXConstructorDecl *Constructor |
| 5464 | = cast<CXXConstructorDecl>(S.CurContext); |
| 5465 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5466 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5467 | << Constructor->isImplicit() |
| 5468 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5469 | << /*base=*/0 |
| 5470 | << Entity.getType(); |
| 5471 | |
| 5472 | RecordDecl *BaseDecl |
| 5473 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 5474 | ->getDecl(); |
| 5475 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 5476 | << S.Context.getTagDeclType(BaseDecl); |
| 5477 | } else { |
| 5478 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 5479 | << Constructor->isImplicit() |
| 5480 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5481 | << /*member=*/1 |
| 5482 | << Entity.getName(); |
| 5483 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 5484 | |
| 5485 | if (const RecordType *Record |
| 5486 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5487 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5488 | diag::note_previous_decl) |
| 5489 | << S.Context.getTagDeclType(Record->getDecl()); |
| 5490 | } |
| 5491 | break; |
| 5492 | } |
| 5493 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5494 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 5495 | << DestType << ArgsRange; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5496 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5497 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5498 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5499 | case OR_Deleted: { |
| 5500 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 5501 | << true << DestType << ArgsRange; |
| 5502 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5503 | OverloadingResult Ovl |
| 5504 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5505 | if (Ovl == OR_Deleted) { |
| 5506 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5507 | << 1 << Best->Function->isDeleted(); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5508 | } else { |
| 5509 | llvm_unreachable("Inconsistent overload resolution?"); |
| 5510 | } |
| 5511 | break; |
| 5512 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5513 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5514 | case OR_Success: |
| 5515 | llvm_unreachable("Conversion did not fail!"); |
| 5516 | break; |
| 5517 | } |
| 5518 | break; |
| 5519 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5520 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5521 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5522 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5523 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 5524 | // This is implicit default-initialization of a const member in |
| 5525 | // a constructor. Complain that it needs to be explicitly |
| 5526 | // initialized. |
| 5527 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 5528 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 5529 | << Constructor->isImplicit() |
| 5530 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 5531 | << /*const=*/1 |
| 5532 | << Entity.getName(); |
| 5533 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 5534 | << Entity.getName(); |
| 5535 | } else { |
| 5536 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 5537 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 5538 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5539 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5540 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5541 | case FK_Incomplete: |
| 5542 | S.RequireCompleteType(Kind.getLocation(), DestType, |
| 5543 | diag::err_init_incomplete_type); |
| 5544 | break; |
| 5545 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5546 | case FK_ListInitializationFailed: { |
| 5547 | // Run the init list checker again to emit diagnostics. |
| 5548 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 5549 | QualType DestType = Entity.getType(); |
| 5550 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 5551 | DestType, /*VerifyOnly=*/false, |
| 5552 | Kind.getKind() != InitializationKind::IK_Direct || |
| 5553 | !S.getLangOptions().CPlusPlus0x); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5554 | assert(DiagnoseInitList.HadError() && |
| 5555 | "Inconsistent init list check result."); |
| 5556 | break; |
| 5557 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 5558 | |
| 5559 | case FK_PlaceholderType: { |
| 5560 | // FIXME: Already diagnosed! |
| 5561 | break; |
| 5562 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5563 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5564 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5565 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5566 | return true; |
| 5567 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5568 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5569 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5570 | switch (SequenceKind) { |
| 5571 | case FailedSequence: { |
| 5572 | OS << "Failed sequence: "; |
| 5573 | switch (Failure) { |
| 5574 | case FK_TooManyInitsForReference: |
| 5575 | OS << "too many initializers for reference"; |
| 5576 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5577 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5578 | case FK_ArrayNeedsInitList: |
| 5579 | OS << "array requires initializer list"; |
| 5580 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5581 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5582 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5583 | OS << "array requires initializer list or string literal"; |
| 5584 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5585 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5586 | case FK_ArrayTypeMismatch: |
| 5587 | OS << "array type mismatch"; |
| 5588 | break; |
| 5589 | |
| 5590 | case FK_NonConstantArrayInit: |
| 5591 | OS << "non-constant array initializer"; |
| 5592 | break; |
| 5593 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5594 | case FK_AddressOfOverloadFailed: |
| 5595 | OS << "address of overloaded function failed"; |
| 5596 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5597 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5598 | case FK_ReferenceInitOverloadFailed: |
| 5599 | OS << "overload resolution for reference initialization failed"; |
| 5600 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5601 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5602 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 5603 | OS << "non-const lvalue reference bound to temporary"; |
| 5604 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5605 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5606 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 5607 | OS << "non-const lvalue reference bound to unrelated type"; |
| 5608 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5609 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5610 | case FK_RValueReferenceBindingToLValue: |
| 5611 | OS << "rvalue reference bound to an lvalue"; |
| 5612 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5613 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5614 | case FK_ReferenceInitDropsQualifiers: |
| 5615 | OS << "reference initialization drops qualifiers"; |
| 5616 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5617 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5618 | case FK_ReferenceInitFailed: |
| 5619 | OS << "reference initialization failed"; |
| 5620 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5621 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5622 | case FK_ConversionFailed: |
| 5623 | OS << "conversion failed"; |
| 5624 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5625 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5626 | case FK_ConversionFromPropertyFailed: |
| 5627 | OS << "conversion from property failed"; |
| 5628 | break; |
| 5629 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5630 | case FK_TooManyInitsForScalar: |
| 5631 | OS << "too many initializers for scalar"; |
| 5632 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5633 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5634 | case FK_ReferenceBindingToInitList: |
| 5635 | OS << "referencing binding to initializer list"; |
| 5636 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5637 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5638 | case FK_InitListBadDestinationType: |
| 5639 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 5640 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5641 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5642 | case FK_UserConversionOverloadFailed: |
| 5643 | OS << "overloading failed for user-defined conversion"; |
| 5644 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5645 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5646 | case FK_ConstructorOverloadFailed: |
| 5647 | OS << "constructor overloading failed"; |
| 5648 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5649 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5650 | case FK_DefaultInitOfConst: |
| 5651 | OS << "default initialization of a const variable"; |
| 5652 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5653 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 5654 | case FK_Incomplete: |
| 5655 | OS << "initialization of incomplete type"; |
| 5656 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5657 | |
| 5658 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5659 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 5660 | break; |
| 5661 | |
| 5662 | case FK_PlaceholderType: |
| 5663 | OS << "initializer expression isn't contextually valid"; |
| 5664 | break; |
Nick Lewycky | b0c6c33 | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 5665 | |
| 5666 | case FK_ListConstructorOverloadFailed: |
| 5667 | OS << "list constructor overloading failed"; |
| 5668 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5669 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5670 | OS << '\n'; |
| 5671 | return; |
| 5672 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5673 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5674 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5675 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5676 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5677 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5678 | case NormalSequence: |
| 5679 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5680 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5681 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5682 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5683 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 5684 | if (S != step_begin()) { |
| 5685 | OS << " -> "; |
| 5686 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5687 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5688 | switch (S->Kind) { |
| 5689 | case SK_ResolveAddressOfOverloadedFunction: |
| 5690 | OS << "resolve address of overloaded function"; |
| 5691 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5692 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5693 | case SK_CastDerivedToBaseRValue: |
| 5694 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 5695 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5696 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5697 | case SK_CastDerivedToBaseXValue: |
| 5698 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 5699 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5700 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5701 | case SK_CastDerivedToBaseLValue: |
| 5702 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 5703 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5704 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5705 | case SK_BindReference: |
| 5706 | OS << "bind reference to lvalue"; |
| 5707 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5708 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5709 | case SK_BindReferenceToTemporary: |
| 5710 | OS << "bind reference to a temporary"; |
| 5711 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5712 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5713 | case SK_ExtraneousCopyToTemporary: |
| 5714 | OS << "extraneous C++03 copy to temporary"; |
| 5715 | break; |
| 5716 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5717 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 5718 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5719 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5720 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5721 | case SK_QualificationConversionRValue: |
| 5722 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5723 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5724 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5725 | case SK_QualificationConversionXValue: |
| 5726 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5727 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5728 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5729 | case SK_QualificationConversionLValue: |
| 5730 | OS << "qualification conversion (lvalue)"; |
| 5731 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5732 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5733 | case SK_ConversionSequence: |
| 5734 | OS << "implicit conversion sequence ("; |
| 5735 | S->ICS->DebugPrint(); // FIXME: use OS |
| 5736 | OS << ")"; |
| 5737 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5738 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5739 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5740 | OS << "list aggregate initialization"; |
| 5741 | break; |
| 5742 | |
| 5743 | case SK_ListConstructorCall: |
| 5744 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5745 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5746 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5747 | case SK_UnwrapInitList: |
| 5748 | OS << "unwrap reference initializer list"; |
| 5749 | break; |
| 5750 | |
| 5751 | case SK_RewrapInitList: |
| 5752 | OS << "rewrap reference initializer list"; |
| 5753 | break; |
| 5754 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5755 | case SK_ConstructorInitialization: |
| 5756 | OS << "constructor initialization"; |
| 5757 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5758 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5759 | case SK_ZeroInitialization: |
| 5760 | OS << "zero initialization"; |
| 5761 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5762 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5763 | case SK_CAssignment: |
| 5764 | OS << "C assignment"; |
| 5765 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5766 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5767 | case SK_StringInit: |
| 5768 | OS << "string initialization"; |
| 5769 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5770 | |
| 5771 | case SK_ObjCObjectConversion: |
| 5772 | OS << "Objective-C object conversion"; |
| 5773 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5774 | |
| 5775 | case SK_ArrayInit: |
| 5776 | OS << "array initialization"; |
| 5777 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5778 | |
| 5779 | case SK_PassByIndirectCopyRestore: |
| 5780 | OS << "pass by indirect copy and restore"; |
| 5781 | break; |
| 5782 | |
| 5783 | case SK_PassByIndirectRestore: |
| 5784 | OS << "pass by indirect restore"; |
| 5785 | break; |
| 5786 | |
| 5787 | case SK_ProduceObjCObject: |
| 5788 | OS << "Objective-C object retension"; |
| 5789 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 5790 | } |
| 5791 | } |
| 5792 | } |
| 5793 | |
| 5794 | void InitializationSequence::dump() const { |
| 5795 | dump(llvm::errs()); |
| 5796 | } |
| 5797 | |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5798 | static void DiagnoseNarrowingInInitList( |
| 5799 | Sema& S, QualType EntityType, const Expr *InitE, |
| 5800 | bool Constant, const APValue &ConstantValue) { |
| 5801 | if (Constant) { |
| 5802 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5803 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5804 | ? diag::err_init_list_constant_narrowing |
| 5805 | : diag::warn_init_list_constant_narrowing) |
| 5806 | << InitE->getSourceRange() |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 5807 | << ConstantValue.getAsString(S.getASTContext(), EntityType) |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5808 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5809 | } else |
| 5810 | S.Diag(InitE->getLocStart(), |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 5811 | S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5812 | ? diag::err_init_list_variable_narrowing |
| 5813 | : diag::warn_init_list_variable_narrowing) |
| 5814 | << InitE->getSourceRange() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 5815 | << InitE->getType().getLocalUnqualifiedType() |
| 5816 | << EntityType.getLocalUnqualifiedType(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5817 | |
| 5818 | llvm::SmallString<128> StaticCast; |
| 5819 | llvm::raw_svector_ostream OS(StaticCast); |
| 5820 | OS << "static_cast<"; |
| 5821 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 5822 | // It's important to use the typedef's name if there is one so that the |
| 5823 | // fixit doesn't break code using types like int64_t. |
| 5824 | // |
| 5825 | // FIXME: This will break if the typedef requires qualification. But |
| 5826 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 5827 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5828 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
| 5829 | OS << BT->getName(S.getLangOptions()); |
| 5830 | else { |
| 5831 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 5832 | // with a broken cast. |
| 5833 | return; |
| 5834 | } |
| 5835 | OS << ">("; |
| 5836 | S.Diag(InitE->getLocStart(), diag::note_init_list_narrowing_override) |
| 5837 | << InitE->getSourceRange() |
| 5838 | << FixItHint::CreateInsertion(InitE->getLocStart(), OS.str()) |
| 5839 | << FixItHint::CreateInsertion( |
| 5840 | S.getPreprocessor().getLocForEndOfToken(InitE->getLocEnd()), ")"); |
| 5841 | } |
| 5842 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5843 | //===----------------------------------------------------------------------===// |
| 5844 | // Initialization helper functions |
| 5845 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5846 | bool |
| 5847 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 5848 | ExprResult Init) { |
| 5849 | if (Init.isInvalid()) |
| 5850 | return false; |
| 5851 | |
| 5852 | Expr *InitE = Init.get(); |
| 5853 | assert(InitE && "No initialization expression"); |
| 5854 | |
| 5855 | InitializationKind Kind = InitializationKind::CreateCopy(SourceLocation(), |
| 5856 | SourceLocation()); |
| 5857 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 5858 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 5859 | } |
| 5860 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5861 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5862 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 5863 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5864 | ExprResult Init, |
| 5865 | bool TopLevelOfInitList) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5866 | if (Init.isInvalid()) |
| 5867 | return ExprError(); |
| 5868 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5869 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5870 | assert(InitE && "No initialization expression?"); |
| 5871 | |
| 5872 | if (EqualLoc.isInvalid()) |
| 5873 | EqualLoc = InitE->getLocStart(); |
| 5874 | |
| 5875 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 5876 | EqualLoc); |
| 5877 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 5878 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 5879 | |
| 5880 | bool Constant = false; |
| 5881 | APValue Result; |
| 5882 | if (TopLevelOfInitList && |
| 5883 | Seq.endsWithNarrowing(Context, InitE, &Constant, &Result)) { |
| 5884 | DiagnoseNarrowingInInitList(*this, Entity.getType(), InitE, |
| 5885 | Constant, Result); |
| 5886 | } |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5887 | return Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1)); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5888 | } |