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 | // |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. The main entry |
| 11 | // point is Sema::CheckInitList(), but all of the work is performed |
| 12 | // within the InitListChecker class. |
| 13 | // |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 14 | // This file also implements Sema::CheckInitializerTypes. |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 18 | #include "SemaInit.h" |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 19 | #include "Lookup.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 20 | #include "Sema.h" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 21 | #include "clang/Parse/Designator.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 25 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 27 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 28 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 29 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Sema Initialization Checking |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 34 | static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) { |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 35 | const ArrayType *AT = Context.getAsArrayType(DeclType); |
| 36 | if (!AT) return 0; |
| 37 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 38 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 39 | return 0; |
| 40 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 41 | // See if this is a string literal or @encode. |
| 42 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 44 | // Handle @encode, which is a narrow string. |
| 45 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 46 | return Init; |
| 47 | |
| 48 | // Otherwise we can only handle string literals. |
| 49 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 220b636 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 50 | if (SL == 0) return 0; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 51 | |
| 52 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 53 | // char array can be initialized with a narrow string. |
| 54 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
| 55 | if (!SL->isWide()) |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 56 | return ElemTy->isCharType() ? Init : 0; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 57 | |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 58 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 59 | // correction from DR343): "An array with element type compatible with a |
| 60 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 61 | // string literal, optionally enclosed in braces." |
| 62 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 63 | ElemTy.getUnqualifiedType())) |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 64 | return Init; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 69 | static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) { |
| 70 | // Get the length of the string as parsed. |
| 71 | uint64_t StrLength = |
| 72 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 73 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 75 | const ArrayType *AT = S.Context.getAsArrayType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 76 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | // 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] | 78 | // being initialized to a string literal. |
| 79 | llvm::APSInt ConstVal(32); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 80 | ConstVal = StrLength; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 81 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 82 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 83 | ConstVal, |
| 84 | ArrayType::Normal, 0); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 85 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 86 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 88 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 90 | // C99 6.7.8p14. We have an array of character type with known size. However, |
| 91 | // the size may be smaller or larger than the string we are initializing. |
| 92 | // FIXME: Avoid truncation for 64-bit length strings. |
| 93 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 94 | S.Diag(Str->getSourceRange().getBegin(), |
| 95 | diag::warn_initializer_string_for_char_array_too_long) |
| 96 | << Str->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 98 | // Set the type to the actual size that we are initializing. If we have |
| 99 | // something like: |
| 100 | // char x[1] = "foo"; |
| 101 | // then this will set the string literal's type to char[1]. |
| 102 | Str->setType(DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 105 | //===----------------------------------------------------------------------===// |
| 106 | // Semantic checking for initializer lists. |
| 107 | //===----------------------------------------------------------------------===// |
| 108 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 109 | /// @brief Semantic checking for initializer lists. |
| 110 | /// |
| 111 | /// The InitListChecker class contains a set of routines that each |
| 112 | /// handle the initialization of a certain kind of entity, e.g., |
| 113 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 114 | /// InitListChecker itself performs a recursive walk of the subobject |
| 115 | /// structure of the type to be initialized, while stepping through |
| 116 | /// the initializer list one element at a time. The IList and Index |
| 117 | /// parameters to each of the Check* routines contain the active |
| 118 | /// (syntactic) initializer list and the index into that initializer |
| 119 | /// list that represents the current initializer. Each routine is |
| 120 | /// responsible for moving that Index forward as it consumes elements. |
| 121 | /// |
| 122 | /// Each Check* routine also has a StructuredList/StructuredIndex |
| 123 | /// arguments, which contains the current the "structured" (semantic) |
| 124 | /// initializer list and the index into that initializer list where we |
| 125 | /// are copying initializers as we map them over to the semantic |
| 126 | /// list. Once we have completed our recursive walk of the subobject |
| 127 | /// structure, we will have constructed a full semantic initializer |
| 128 | /// list. |
| 129 | /// |
| 130 | /// C99 designators cause changes in the initializer list traversal, |
| 131 | /// because they make the initialization "jump" into a specific |
| 132 | /// subobject and then continue the initialization from that |
| 133 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 134 | /// designated subobject and manages backing out the recursion to |
| 135 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 136 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 137 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 138 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 139 | bool hadError; |
| 140 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 141 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 143 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 144 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 145 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 146 | unsigned &StructuredIndex, |
| 147 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 148 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 149 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 150 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 151 | unsigned &StructuredIndex, |
| 152 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 153 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 154 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 156 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 157 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 158 | unsigned &StructuredIndex, |
| 159 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 160 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 161 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 162 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 163 | InitListExpr *StructuredList, |
| 164 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 165 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 166 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 167 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 168 | InitListExpr *StructuredList, |
| 169 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 170 | void CheckReferenceType(const InitializedEntity &Entity, |
| 171 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 172 | unsigned &Index, |
| 173 | InitListExpr *StructuredList, |
| 174 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 175 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 176 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 177 | InitListExpr *StructuredList, |
| 178 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 179 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 180 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 182 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 183 | 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 CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 187 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 189 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 190 | InitListExpr *StructuredList, |
| 191 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 192 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 193 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 194 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 196 | RecordDecl::field_iterator *NextField, |
| 197 | llvm::APSInt *NextElementIndex, |
| 198 | unsigned &Index, |
| 199 | InitListExpr *StructuredList, |
| 200 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 201 | bool FinishSubobjectInit, |
| 202 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 203 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 204 | QualType CurrentObjectType, |
| 205 | InitListExpr *StructuredList, |
| 206 | unsigned StructuredIndex, |
| 207 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 208 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 209 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 210 | Expr *expr); |
| 211 | int numArrayElements(QualType DeclType); |
| 212 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 213 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 214 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 215 | const InitializedEntity &ParentEntity, |
| 216 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 217 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 218 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 219 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 220 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 221 | InitListExpr *IL, QualType &T); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 222 | bool HadError() { return hadError; } |
| 223 | |
| 224 | // @brief Retrieves the fully-structured initializer list used for |
| 225 | // semantic analysis and code generation. |
| 226 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 227 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 228 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 229 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 230 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 231 | const InitializedEntity &ParentEntity, |
| 232 | InitListExpr *ILE, |
| 233 | bool &RequiresSecondPass) { |
| 234 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 235 | unsigned NumInits = ILE->getNumInits(); |
| 236 | InitializedEntity MemberEntity |
| 237 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 238 | if (Init >= NumInits || !ILE->getInit(Init)) { |
| 239 | // FIXME: We probably don't need to handle references |
| 240 | // specially here, since value-initialization of references is |
| 241 | // handled in InitializationSequence. |
| 242 | if (Field->getType()->isReferenceType()) { |
| 243 | // C++ [dcl.init.aggr]p9: |
| 244 | // If an incomplete or empty initializer-list leaves a |
| 245 | // member of reference type uninitialized, the program is |
| 246 | // ill-formed. |
| 247 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 248 | << Field->getType() |
| 249 | << ILE->getSyntacticForm()->getSourceRange(); |
| 250 | SemaRef.Diag(Field->getLocation(), |
| 251 | diag::note_uninit_reference_member); |
| 252 | hadError = true; |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 257 | true); |
| 258 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0); |
| 259 | if (!InitSeq) { |
| 260 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0); |
| 261 | hadError = true; |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | Sema::OwningExprResult MemberInit |
| 266 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, |
| 267 | Sema::MultiExprArg(SemaRef, 0, 0)); |
| 268 | if (MemberInit.isInvalid()) { |
| 269 | hadError = true; |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | if (hadError) { |
| 274 | // Do nothing |
| 275 | } else if (Init < NumInits) { |
| 276 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
| 277 | } else if (InitSeq.getKind() |
| 278 | == InitializationSequence::ConstructorInitialization) { |
| 279 | // Value-initialization requires a constructor call, so |
| 280 | // extend the initializer list to include the constructor |
| 281 | // call and make a note that we'll need to take another pass |
| 282 | // through the initializer list. |
| 283 | ILE->updateInit(Init, MemberInit.takeAs<Expr>()); |
| 284 | RequiresSecondPass = true; |
| 285 | } |
| 286 | } else if (InitListExpr *InnerILE |
| 287 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 288 | FillInValueInitializations(MemberEntity, InnerILE, |
| 289 | RequiresSecondPass); |
| 290 | } |
| 291 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 292 | /// Recursively replaces NULL values within the given initializer list |
| 293 | /// with expressions that perform value-initialization of the |
| 294 | /// appropriate type. |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 295 | void |
| 296 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 297 | InitListExpr *ILE, |
| 298 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 300 | "Should not have void type"); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 301 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 302 | if (ILE->getSyntacticForm()) |
| 303 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 305 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 306 | if (RType->getDecl()->isUnion() && |
| 307 | ILE->getInitializedFieldInUnion()) |
| 308 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 309 | Entity, ILE, RequiresSecondPass); |
| 310 | else { |
| 311 | unsigned Init = 0; |
| 312 | for (RecordDecl::field_iterator |
| 313 | Field = RType->getDecl()->field_begin(), |
| 314 | FieldEnd = RType->getDecl()->field_end(); |
| 315 | Field != FieldEnd; ++Field) { |
| 316 | if (Field->isUnnamedBitfield()) |
| 317 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 319 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 320 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 321 | |
| 322 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
| 323 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 324 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 325 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 326 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 327 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 328 | // Only look at the first initialization of a union. |
| 329 | if (RType->getDecl()->isUnion()) |
| 330 | break; |
| 331 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 336 | |
| 337 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 339 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 340 | unsigned NumInits = ILE->getNumInits(); |
| 341 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 342 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 343 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 344 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 345 | NumElements = CAType->getSize().getZExtValue(); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 346 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
| 347 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 348 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 349 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 350 | NumElements = VType->getNumElements(); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 351 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
| 352 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 354 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 357 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 358 | if (hadError) |
| 359 | return; |
| 360 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 361 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 362 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 363 | ElementEntity.setElementIndex(Init); |
| 364 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 365 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 366 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 367 | true); |
| 368 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0); |
| 369 | if (!InitSeq) { |
| 370 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 371 | hadError = true; |
| 372 | return; |
| 373 | } |
| 374 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 375 | Sema::OwningExprResult ElementInit |
| 376 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, |
| 377 | Sema::MultiExprArg(SemaRef, 0, 0)); |
| 378 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 379 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | |
| 383 | if (hadError) { |
| 384 | // Do nothing |
| 385 | } else if (Init < NumInits) { |
| 386 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
| 387 | } else if (InitSeq.getKind() |
| 388 | == InitializationSequence::ConstructorInitialization) { |
| 389 | // Value-initialization requires a constructor call, so |
| 390 | // extend the initializer list to include the constructor |
| 391 | // call and make a note that we'll need to take another pass |
| 392 | // through the initializer list. |
| 393 | ILE->updateInit(Init, ElementInit.takeAs<Expr>()); |
| 394 | RequiresSecondPass = true; |
| 395 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 396 | } else if (InitListExpr *InnerILE |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 397 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
| 398 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 402 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 403 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
| 404 | InitListExpr *IL, QualType &T) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 405 | : SemaRef(S) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 406 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 407 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 408 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 409 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 411 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 412 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 413 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 414 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 415 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 416 | if (!hadError) { |
| 417 | bool RequiresSecondPass = false; |
| 418 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 419 | if (RequiresSecondPass && !hadError) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 420 | FillInValueInitializations(Entity, FullyStructuredList, |
| 421 | RequiresSecondPass); |
| 422 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 426 | // FIXME: use a proper constant |
| 427 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 428 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 429 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 430 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 431 | } |
| 432 | return maxElements; |
| 433 | } |
| 434 | |
| 435 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 436 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 437 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 439 | Field = structDecl->field_begin(), |
| 440 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 441 | Field != FieldEnd; ++Field) { |
| 442 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 443 | ++InitializableMembers; |
| 444 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 445 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 446 | return std::min(InitializableMembers, 1); |
| 447 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 450 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 451 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 452 | QualType T, unsigned &Index, |
| 453 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 454 | unsigned &StructuredIndex, |
| 455 | bool TopLevelObject) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 456 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 458 | if (T->isArrayType()) |
| 459 | maxElements = numArrayElements(T); |
| 460 | else if (T->isStructureType() || T->isUnionType()) |
| 461 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 462 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 463 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 464 | else |
| 465 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 466 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 467 | if (maxElements == 0) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 468 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 469 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 470 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 471 | hadError = true; |
| 472 | return; |
| 473 | } |
| 474 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 475 | // Build a structured initializer list corresponding to this subobject. |
| 476 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 478 | StructuredIndex, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 479 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 480 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 481 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 483 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 484 | unsigned StartIndex = Index; |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 485 | CheckListElementTypes(Entity, ParentIList, T, |
| 486 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | StructuredSubobjectInitList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 488 | StructuredSubobjectInitIndex, |
| 489 | TopLevelObject); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 490 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 491 | StructuredSubobjectInitList->setType(T); |
| 492 | |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 493 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 494 | // range corresponds with the end of the last initializer it used. |
| 495 | if (EndIndex < ParentIList->getNumInits()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | SourceLocation EndLoc |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 497 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 498 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 499 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 502 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 503 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 504 | unsigned &Index, |
| 505 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 506 | unsigned &StructuredIndex, |
| 507 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 508 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 509 | SyntacticToSemantic[IList] = StructuredList; |
| 510 | StructuredList->setSyntacticForm(IList); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 511 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
| 512 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Steve Naroff | a647caa | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 513 | IList->setType(T); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 514 | StructuredList->setType(T); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 515 | if (hadError) |
| 516 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 517 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 518 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 519 | // We have leftover initializers |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 520 | if (StructuredIndex == 1 && |
| 521 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 522 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 523 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 524 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 525 | hadError = true; |
| 526 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 527 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 528 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 529 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 530 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 531 | // Don't complain for incomplete types, since we'll get an error |
| 532 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 533 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 534 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 535 | CurrentObjectType->isArrayType()? 0 : |
| 536 | CurrentObjectType->isVectorType()? 1 : |
| 537 | CurrentObjectType->isScalarType()? 2 : |
| 538 | CurrentObjectType->isUnionType()? 3 : |
| 539 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 540 | |
| 541 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 542 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 543 | DK = diag::err_excess_initializers; |
| 544 | hadError = true; |
| 545 | } |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 546 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 547 | DK = diag::err_excess_initializers; |
| 548 | hadError = true; |
| 549 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 551 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 552 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 555 | |
Eli Friedman | 759f252 | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 556 | if (T->isScalarType() && !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 557 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 558 | << IList->getSourceRange() |
Chris Lattner | 29d9c1a | 2009-12-06 17:36:05 +0000 | [diff] [blame] | 559 | << CodeModificationHint::CreateRemoval(IList->getLocStart()) |
| 560 | << CodeModificationHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 563 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 564 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 566 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 567 | unsigned &Index, |
| 568 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 569 | unsigned &StructuredIndex, |
| 570 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 571 | if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 572 | CheckScalarType(Entity, IList, DeclType, Index, |
| 573 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 574 | } else if (DeclType->isVectorType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 575 | CheckVectorType(Entity, IList, DeclType, Index, |
| 576 | StructuredList, StructuredIndex); |
Douglas Gregor | d7eb846 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 577 | } else if (DeclType->isAggregateType()) { |
| 578 | if (DeclType->isRecordType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 579 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 580 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 581 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 582 | StructuredList, StructuredIndex, |
| 583 | TopLevelObject); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 584 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 585 | llvm::APSInt Zero( |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 586 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 587 | false); |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 588 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 589 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 590 | StructuredList, StructuredIndex); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 591 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 592 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 593 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 594 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 595 | ++Index; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 596 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 597 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 598 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 599 | } else if (DeclType->isRecordType()) { |
| 600 | // C++ [dcl.init]p14: |
| 601 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 602 | // is a brace-enclosed list, see 8.5.1. |
| 603 | // |
| 604 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 605 | // we have an initializer list and a destination type that is not |
| 606 | // an aggregate. |
| 607 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 608 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 609 | << DeclType << IList->getSourceRange(); |
| 610 | hadError = true; |
| 611 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 612 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 613 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 614 | } else { |
| 615 | // In C, all types are either scalars or aggregates, but |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 616 | // additional handling is needed here for C++ (and possibly others?). |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 617 | assert(0 && "Unsupported initializer type"); |
| 618 | } |
| 619 | } |
| 620 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 621 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 622 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 624 | unsigned &Index, |
| 625 | InitListExpr *StructuredList, |
| 626 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 627 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 628 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 629 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 630 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | InitListExpr *newStructuredList |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 632 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 633 | StructuredList, StructuredIndex, |
| 634 | SubInitList->getSourceRange()); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 635 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 636 | newStructuredList, newStructuredIndex); |
| 637 | ++StructuredIndex; |
| 638 | ++Index; |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 639 | } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) { |
| 640 | CheckStringInit(Str, ElemType, SemaRef); |
Chris Lattner | f71ae8d | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 641 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 642 | ++Index; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 643 | } else if (ElemType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 644 | CheckScalarType(Entity, IList, ElemType, Index, |
| 645 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 646 | } else if (ElemType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 647 | CheckReferenceType(Entity, IList, ElemType, Index, |
| 648 | StructuredList, StructuredIndex); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 649 | } else { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 650 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 651 | // C++ [dcl.init.aggr]p12: |
| 652 | // All implicit type conversions (clause 4) are considered when |
| 653 | // initializing the aggregate member with an ini- tializer from |
| 654 | // an initializer-list. If the initializer can initialize a |
| 655 | // member, the member is initialized. [...] |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 656 | |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 657 | // FIXME: Better EqualLoc? |
| 658 | InitializationKind Kind = |
| 659 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
| 660 | InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1); |
| 661 | |
| 662 | if (Seq) { |
| 663 | Sema::OwningExprResult Result = |
| 664 | Seq.Perform(SemaRef, Entity, Kind, |
| 665 | Sema::MultiExprArg(SemaRef, (void **)&expr, 1)); |
| 666 | if (Result.isInvalid()) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 667 | hadError = true; |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 668 | |
| 669 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 670 | Result.takeAs<Expr>()); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 671 | ++Index; |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | // Fall through for subaggregate initialization |
| 676 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 677 | // C99 6.7.8p13: |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 678 | // |
| 679 | // The initializer for a structure or union object that has |
| 680 | // automatic storage duration shall be either an initializer |
| 681 | // list as described below, or a single expression that has |
| 682 | // compatible structure or union type. In the latter case, the |
| 683 | // initial value of the object, including unnamed members, is |
| 684 | // that of the expression. |
Eli Friedman | 6b5374f | 2009-06-13 10:38:46 +0000 | [diff] [blame] | 685 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 686 | SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 687 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 688 | ++Index; |
| 689 | return; |
| 690 | } |
| 691 | |
| 692 | // Fall through for subaggregate initialization |
| 693 | } |
| 694 | |
| 695 | // C++ [dcl.init.aggr]p12: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | // |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 697 | // [...] Otherwise, if the member is itself a non-empty |
| 698 | // subaggregate, brace elision is assumed and the initializer is |
| 699 | // considered for the initialization of the first member of |
| 700 | // the subaggregate. |
| 701 | if (ElemType->isAggregateType() || ElemType->isVectorType()) { |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 702 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 703 | StructuredIndex); |
| 704 | ++StructuredIndex; |
| 705 | } else { |
| 706 | // We cannot initialize this element, so let |
| 707 | // PerformCopyInitialization produce the appropriate diagnostic. |
Anders Carlsson | ca755fe | 2010-01-30 01:56:32 +0000 | [diff] [blame] | 708 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 709 | SemaRef.Owned(expr)); |
| 710 | IList->setInit(Index, 0); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 711 | hadError = true; |
| 712 | ++Index; |
| 713 | ++StructuredIndex; |
| 714 | } |
| 715 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 718 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 719 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 720 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 721 | InitListExpr *StructuredList, |
| 722 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 723 | if (Index < IList->getNumInits()) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 724 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 725 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 726 | SemaRef.Diag(IList->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 727 | diag::err_many_braces_around_scalar_init) |
| 728 | << IList->getSourceRange(); |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 729 | hadError = true; |
| 730 | ++Index; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 731 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 732 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 733 | } else if (isa<DesignatedInitExpr>(expr)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 734 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 735 | diag::err_designator_for_scalar_init) |
| 736 | << DeclType << expr->getSourceRange(); |
| 737 | hadError = true; |
| 738 | ++Index; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 739 | ++StructuredIndex; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 740 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 741 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 742 | |
Anders Carlsson | c07b8c0 | 2010-01-23 18:35:41 +0000 | [diff] [blame] | 743 | Sema::OwningExprResult Result = |
Eli Friedman | a1635d9 | 2010-01-25 17:04:54 +0000 | [diff] [blame] | 744 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 745 | SemaRef.Owned(expr)); |
Anders Carlsson | c07b8c0 | 2010-01-23 18:35:41 +0000 | [diff] [blame] | 746 | |
| 747 | Expr *ResultExpr; |
| 748 | |
| 749 | if (Result.isInvalid()) |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 750 | hadError = true; // types weren't compatible. |
Anders Carlsson | c07b8c0 | 2010-01-23 18:35:41 +0000 | [diff] [blame] | 751 | else { |
| 752 | ResultExpr = Result.takeAs<Expr>(); |
| 753 | |
| 754 | if (ResultExpr != expr) { |
| 755 | // The type was promoted, update initializer list. |
| 756 | IList->setInit(Index, ResultExpr); |
| 757 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 758 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 759 | if (hadError) |
| 760 | ++StructuredIndex; |
| 761 | else |
Anders Carlsson | c07b8c0 | 2010-01-23 18:35:41 +0000 | [diff] [blame] | 762 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 763 | ++Index; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 764 | } else { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 765 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 766 | << IList->getSourceRange(); |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 767 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 768 | ++Index; |
| 769 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 770 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 771 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 774 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 775 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 776 | unsigned &Index, |
| 777 | InitListExpr *StructuredList, |
| 778 | unsigned &StructuredIndex) { |
| 779 | if (Index < IList->getNumInits()) { |
| 780 | Expr *expr = IList->getInit(Index); |
| 781 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 782 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 783 | << DeclType << IList->getSourceRange(); |
| 784 | hadError = true; |
| 785 | ++Index; |
| 786 | ++StructuredIndex; |
| 787 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 789 | |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 790 | Sema::OwningExprResult Result = |
| 791 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 792 | SemaRef.Owned(expr)); |
| 793 | |
| 794 | if (Result.isInvalid()) |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 795 | hadError = true; |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 796 | |
| 797 | expr = Result.takeAs<Expr>(); |
| 798 | IList->setInit(Index, expr); |
| 799 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 800 | if (hadError) |
| 801 | ++StructuredIndex; |
| 802 | else |
| 803 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 804 | ++Index; |
| 805 | } else { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 806 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 807 | // general, it would be useful to pass location information down the stack, |
| 808 | // so that we know the location (or decl) of the "current object" being |
| 809 | // initialized. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 811 | diag::err_init_reference_member_uninitialized) |
| 812 | << DeclType |
| 813 | << IList->getSourceRange(); |
| 814 | hadError = true; |
| 815 | ++Index; |
| 816 | ++StructuredIndex; |
| 817 | return; |
| 818 | } |
| 819 | } |
| 820 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 821 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 822 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 823 | unsigned &Index, |
| 824 | InitListExpr *StructuredList, |
| 825 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 826 | if (Index < IList->getNumInits()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 827 | const VectorType *VT = DeclType->getAs<VectorType>(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 828 | unsigned maxElements = VT->getNumElements(); |
| 829 | unsigned numEltsInit = 0; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 830 | QualType elementType = VT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 832 | if (!SemaRef.getLangOptions().OpenCL) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 833 | InitializedEntity ElementEntity = |
| 834 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 835 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 836 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 837 | // Don't attempt to go past the end of the init list |
| 838 | if (Index >= IList->getNumInits()) |
| 839 | break; |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 840 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 841 | ElementEntity.setElementIndex(Index); |
| 842 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 843 | StructuredList, StructuredIndex); |
| 844 | } |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 845 | } else { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 846 | InitializedEntity ElementEntity = |
| 847 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 848 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 849 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 850 | for (unsigned i = 0; i < maxElements; ++i) { |
| 851 | // Don't attempt to go past the end of the init list |
| 852 | if (Index >= IList->getNumInits()) |
| 853 | break; |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 854 | |
| 855 | ElementEntity.setElementIndex(Index); |
| 856 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 857 | QualType IType = IList->getInit(Index)->getType(); |
| 858 | if (!IType->isVectorType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 859 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 860 | StructuredList, StructuredIndex); |
| 861 | ++numEltsInit; |
| 862 | } else { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 863 | const VectorType *IVT = IType->getAs<VectorType>(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 864 | unsigned numIElts = IVT->getNumElements(); |
| 865 | QualType VecType = SemaRef.Context.getExtVectorType(elementType, |
| 866 | numIElts); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 867 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 868 | StructuredList, StructuredIndex); |
| 869 | numEltsInit += numIElts; |
| 870 | } |
| 871 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 872 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 874 | // OpenCL & AltiVec require all elements to be initialized. |
| 875 | if (numEltsInit != maxElements) |
| 876 | if (SemaRef.getLangOptions().OpenCL || SemaRef.getLangOptions().AltiVec) |
| 877 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 878 | diag::err_vector_incorrect_num_initializers) |
| 879 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 883 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 884 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 885 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 887 | unsigned &Index, |
| 888 | InitListExpr *StructuredList, |
| 889 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 890 | // Check for the special-case of initializing an array with a string. |
| 891 | if (Index < IList->getNumInits()) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 892 | if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType, |
| 893 | SemaRef.Context)) { |
| 894 | CheckStringInit(Str, DeclType, SemaRef); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 895 | // We place the string literal directly into the resulting |
| 896 | // initializer list. This is the only place where the structure |
| 897 | // of the structured initializer list doesn't match exactly, |
| 898 | // because doing so would involve allocating one character |
| 899 | // constant for each string. |
Chris Lattner | f71ae8d | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 900 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 901 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 902 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 903 | return; |
| 904 | } |
| 905 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 906 | if (const VariableArrayType *VAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 907 | SemaRef.Context.getAsVariableArrayType(DeclType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 908 | // Check for VLAs; in standard C it would be possible to check this |
| 909 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 910 | // them in all sorts of strange places). |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 911 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 912 | diag::err_variable_object_no_init) |
| 913 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 914 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 915 | ++Index; |
| 916 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 917 | return; |
| 918 | } |
| 919 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 920 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 921 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 922 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 923 | bool maxElementsKnown = false; |
| 924 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 925 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 926 | maxElements = CAT->getSize(); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 927 | elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 928 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 929 | maxElementsKnown = true; |
| 930 | } |
| 931 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 932 | QualType elementType = SemaRef.Context.getAsArrayType(DeclType) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 933 | ->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 934 | while (Index < IList->getNumInits()) { |
| 935 | Expr *Init = IList->getInit(Index); |
| 936 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 937 | // If we're not the subobject that matches up with the '{' for |
| 938 | // the designator, we shouldn't be handling the |
| 939 | // designator. Return immediately. |
| 940 | if (!SubobjectIsDesignatorContext) |
| 941 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 942 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 943 | // Handle this designated initializer. elementIndex will be |
| 944 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 945 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 946 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 947 | StructuredList, StructuredIndex, true, |
| 948 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 949 | hadError = true; |
| 950 | continue; |
| 951 | } |
| 952 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 953 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
| 954 | maxElements.extend(elementIndex.getBitWidth()); |
| 955 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
| 956 | elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 957 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 958 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 959 | // If the array is of incomplete type, keep track of the number of |
| 960 | // elements in the initializer. |
| 961 | if (!maxElementsKnown && elementIndex > maxElements) |
| 962 | maxElements = elementIndex; |
| 963 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 964 | continue; |
| 965 | } |
| 966 | |
| 967 | // If we know the maximum number of elements, and we've already |
| 968 | // hit it, stop consuming elements in the initializer list. |
| 969 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 970 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 971 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 972 | InitializedEntity ElementEntity = |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 973 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 974 | Entity); |
| 975 | // Check this element. |
| 976 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 977 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 978 | ++elementIndex; |
| 979 | |
| 980 | // If the array is of incomplete type, keep track of the number of |
| 981 | // elements in the initializer. |
| 982 | if (!maxElementsKnown && elementIndex > maxElements) |
| 983 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 984 | } |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 985 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 986 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 987 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 988 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 989 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 990 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 991 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 992 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 993 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 994 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 995 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 997 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1001 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1002 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1004 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1006 | unsigned &Index, |
| 1007 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1008 | unsigned &StructuredIndex, |
| 1009 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1010 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1011 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1012 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1013 | // confusion, we forgo checking the intializer for the entire record. |
| 1014 | if (structDecl->isInvalidDecl()) { |
| 1015 | hadError = true; |
| 1016 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1018 | |
| 1019 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1020 | // Value-initialize the first named member of the union. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1021 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1022 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1023 | Field != FieldEnd; ++Field) { |
| 1024 | if (Field->getDeclName()) { |
| 1025 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1026 | break; |
| 1027 | } |
| 1028 | } |
| 1029 | return; |
| 1030 | } |
| 1031 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1032 | // If structDecl is a forward declaration, this loop won't do |
| 1033 | // anything except look at designated initializers; That's okay, |
| 1034 | // because an error should get printed out elsewhere. It might be |
| 1035 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1036 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1037 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1038 | bool InitializedSomething = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1039 | while (Index < IList->getNumInits()) { |
| 1040 | Expr *Init = IList->getInit(Index); |
| 1041 | |
| 1042 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1043 | // If we're not the subobject that matches up with the '{' for |
| 1044 | // the designator, we shouldn't be handling the |
| 1045 | // designator. Return immediately. |
| 1046 | if (!SubobjectIsDesignatorContext) |
| 1047 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1048 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1049 | // Handle this designated initializer. Field will be updated to |
| 1050 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1051 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1052 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1053 | StructuredList, StructuredIndex, |
| 1054 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1055 | hadError = true; |
| 1056 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1057 | InitializedSomething = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1058 | continue; |
| 1059 | } |
| 1060 | |
| 1061 | if (Field == FieldEnd) { |
| 1062 | // We've run out of fields. We're done. |
| 1063 | break; |
| 1064 | } |
| 1065 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1066 | // We've already initialized a member of a union. We're done. |
| 1067 | if (InitializedSomething && DeclType->isUnionType()) |
| 1068 | break; |
| 1069 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1070 | // If we've hit the flexible array member at the end, we're done. |
| 1071 | if (Field->getType()->isIncompleteArrayType()) |
| 1072 | break; |
| 1073 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1074 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1075 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1076 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1077 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1078 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1079 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1080 | InitializedEntity MemberEntity = |
| 1081 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1082 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1083 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1084 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1085 | |
| 1086 | if (DeclType->isUnionType()) { |
| 1087 | // Initialize the first field within the union. |
| 1088 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1089 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1090 | |
| 1091 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1092 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1093 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1095 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1096 | return; |
| 1097 | |
| 1098 | // Handle GNU flexible array initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | if (!TopLevelObject && |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1100 | (!isa<InitListExpr>(IList->getInit(Index)) || |
| 1101 | cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1102 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1103 | diag::err_flexible_array_init_nonempty) |
| 1104 | << IList->getInit(Index)->getSourceRange().getBegin(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1105 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1106 | << *Field; |
| 1107 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1108 | ++Index; |
| 1109 | return; |
| 1110 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1112 | diag::ext_flexible_array_init) |
| 1113 | << IList->getInit(Index)->getSourceRange().getBegin(); |
| 1114 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1115 | << *Field; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1118 | InitializedEntity MemberEntity = |
| 1119 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1120 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1121 | if (isa<InitListExpr>(IList->getInit(Index))) |
| 1122 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1123 | StructuredList, StructuredIndex); |
| 1124 | else |
| 1125 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1126 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1127 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1128 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1129 | /// \brief Expand a field designator that refers to a member of an |
| 1130 | /// anonymous struct or union into a series of field designators that |
| 1131 | /// refers to the field within the appropriate subobject. |
| 1132 | /// |
| 1133 | /// Field/FieldIndex will be updated to point to the (new) |
| 1134 | /// currently-designated field. |
| 1135 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | DesignatedInitExpr *DIE, |
| 1137 | unsigned DesigIdx, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1138 | FieldDecl *Field, |
| 1139 | RecordDecl::field_iterator &FieldIter, |
| 1140 | unsigned &FieldIndex) { |
| 1141 | typedef DesignatedInitExpr::Designator Designator; |
| 1142 | |
| 1143 | // Build the path from the current object to the member of the |
| 1144 | // anonymous struct/union (backwards). |
| 1145 | llvm::SmallVector<FieldDecl *, 4> Path; |
| 1146 | SemaRef.BuildAnonymousStructUnionMemberPath(Field, Path); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1148 | // Build the replacement designators. |
| 1149 | llvm::SmallVector<Designator, 4> Replacements; |
| 1150 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 1151 | FI = Path.rbegin(), FIEnd = Path.rend(); |
| 1152 | FI != FIEnd; ++FI) { |
| 1153 | if (FI + 1 == FIEnd) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1154 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1155 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1156 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1157 | else |
| 1158 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1159 | SourceLocation())); |
| 1160 | Replacements.back().setField(*FI); |
| 1161 | } |
| 1162 | |
| 1163 | // Expand the current designator into the set of replacement |
| 1164 | // designators, so we have a full subobject path down to where the |
| 1165 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1166 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1167 | &Replacements[0] + Replacements.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1169 | // Update FieldIter/FieldIndex; |
| 1170 | RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1171 | FieldIter = Record->field_begin(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1172 | FieldIndex = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1173 | for (RecordDecl::field_iterator FEnd = Record->field_end(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1174 | FieldIter != FEnd; ++FieldIter) { |
| 1175 | if (FieldIter->isUnnamedBitfield()) |
| 1176 | continue; |
| 1177 | |
| 1178 | if (*FieldIter == Path.back()) |
| 1179 | return; |
| 1180 | |
| 1181 | ++FieldIndex; |
| 1182 | } |
| 1183 | |
| 1184 | assert(false && "Unable to find anonymous struct/union field"); |
| 1185 | } |
| 1186 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1187 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1188 | /// |
| 1189 | /// Determines whether the designated initializer @p DIE, which |
| 1190 | /// resides at the given @p Index within the initializer list @p |
| 1191 | /// IList, is well-formed for a current object of type @p DeclType |
| 1192 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1194 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1195 | /// |
| 1196 | /// @param IList The initializer list in which this designated |
| 1197 | /// initializer occurs. |
| 1198 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1199 | /// @param DIE The designated initializer expression. |
| 1200 | /// |
| 1201 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1202 | /// |
| 1203 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1204 | /// into which the designation in @p DIE should refer. |
| 1205 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1206 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1207 | /// a field, this will be set to the field declaration corresponding |
| 1208 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1209 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1210 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1211 | /// DIE is an array designator or GNU array-range designator, this |
| 1212 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1213 | /// |
| 1214 | /// @param Index Index into @p IList where the designated initializer |
| 1215 | /// @p DIE occurs. |
| 1216 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1217 | /// @param StructuredList The initializer list expression that |
| 1218 | /// describes all of the subobject initializers in the order they'll |
| 1219 | /// actually be initialized. |
| 1220 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1221 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1223 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1224 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1226 | unsigned DesigIdx, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1227 | QualType &CurrentObjectType, |
| 1228 | RecordDecl::field_iterator *NextField, |
| 1229 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1230 | unsigned &Index, |
| 1231 | InitListExpr *StructuredList, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1232 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1233 | bool FinishSubobjectInit, |
| 1234 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1235 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1236 | // Check the actual initialization for the designated object type. |
| 1237 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1238 | |
| 1239 | // Temporarily remove the designator expression from the |
| 1240 | // initializer list that the child calls see, so that we don't try |
| 1241 | // to re-process the designator. |
| 1242 | unsigned OldIndex = Index; |
| 1243 | IList->setInit(OldIndex, DIE->getInit()); |
| 1244 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1245 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1246 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1247 | |
| 1248 | // Restore the designated initializer expression in the syntactic |
| 1249 | // form of the initializer list. |
| 1250 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1251 | DIE->setInit(IList->getInit(OldIndex)); |
| 1252 | IList->setInit(OldIndex, DIE); |
| 1253 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1254 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1257 | bool IsFirstDesignator = (DesigIdx == 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | assert((IsFirstDesignator || StructuredList) && |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1259 | "Need a non-designated initializer list to start from"); |
| 1260 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1261 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1262 | // Determine the structural initializer list that corresponds to the |
| 1263 | // current subobject. |
| 1264 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1266 | StructuredList, StructuredIndex, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1267 | SourceRange(D->getStartLocation(), |
| 1268 | DIE->getSourceRange().getEnd())); |
| 1269 | assert(StructuredList && "Expected a structured initializer list"); |
| 1270 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1271 | if (D->isFieldDesignator()) { |
| 1272 | // C99 6.7.8p7: |
| 1273 | // |
| 1274 | // If a designator has the form |
| 1275 | // |
| 1276 | // . identifier |
| 1277 | // |
| 1278 | // then the current object (defined below) shall have |
| 1279 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1280 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1281 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1282 | if (!RT) { |
| 1283 | SourceLocation Loc = D->getDotLoc(); |
| 1284 | if (Loc.isInvalid()) |
| 1285 | Loc = D->getFieldLoc(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1286 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1287 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1288 | ++Index; |
| 1289 | return true; |
| 1290 | } |
| 1291 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1292 | // Note: we perform a linear search of the fields here, despite |
| 1293 | // the fact that we have a faster lookup method, because we always |
| 1294 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1295 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1296 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1297 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1299 | Field = RT->getDecl()->field_begin(), |
| 1300 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1301 | for (; Field != FieldEnd; ++Field) { |
| 1302 | if (Field->isUnnamedBitfield()) |
| 1303 | continue; |
| 1304 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1305 | if (KnownField == *Field || Field->getIdentifier() == FieldName) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1306 | break; |
| 1307 | |
| 1308 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1311 | if (Field == FieldEnd) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1312 | // There was no normal field in the struct with the designated |
| 1313 | // name. Perform another lookup for this name, which may find |
| 1314 | // something that we can't designate (e.g., a member function), |
| 1315 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1317 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1318 | FieldDecl *ReplacementField = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1319 | if (Lookup.first == Lookup.second) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1320 | // Name lookup didn't find anything. Determine whether this |
| 1321 | // was a typo for another field name. |
| 1322 | LookupResult R(SemaRef, FieldName, D->getFieldLoc(), |
| 1323 | Sema::LookupMemberName); |
| 1324 | if (SemaRef.CorrectTypo(R, /*Scope=*/0, /*SS=*/0, RT->getDecl()) && |
| 1325 | (ReplacementField = R.getAsSingle<FieldDecl>()) && |
| 1326 | ReplacementField->getDeclContext()->getLookupContext() |
| 1327 | ->Equals(RT->getDecl())) { |
| 1328 | SemaRef.Diag(D->getFieldLoc(), |
| 1329 | diag::err_field_designator_unknown_suggest) |
| 1330 | << FieldName << CurrentObjectType << R.getLookupName() |
| 1331 | << CodeModificationHint::CreateReplacement(D->getFieldLoc(), |
| 1332 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 1333 | SemaRef.Diag(ReplacementField->getLocation(), |
| 1334 | diag::note_previous_decl) |
| 1335 | << ReplacementField->getDeclName(); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1336 | } else { |
| 1337 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1338 | << FieldName << CurrentObjectType; |
| 1339 | ++Index; |
| 1340 | return true; |
| 1341 | } |
| 1342 | } else if (!KnownField) { |
| 1343 | // Determine whether we found a field at all. |
| 1344 | ReplacementField = dyn_cast<FieldDecl>(*Lookup.first); |
| 1345 | } |
| 1346 | |
| 1347 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1348 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1349 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1350 | << FieldName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1352 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1353 | ++Index; |
| 1354 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1355 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1356 | |
| 1357 | if (!KnownField && |
| 1358 | cast<RecordDecl>((ReplacementField)->getDeclContext()) |
| 1359 | ->isAnonymousStructOrUnion()) { |
| 1360 | // Handle an field designator that refers to a member of an |
| 1361 | // anonymous struct or union. |
| 1362 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, |
| 1363 | ReplacementField, |
| 1364 | Field, FieldIndex); |
| 1365 | D = DIE->getDesignator(DesigIdx); |
| 1366 | } else if (!KnownField) { |
| 1367 | // The replacement field comes from typo correction; find it |
| 1368 | // in the list of fields. |
| 1369 | FieldIndex = 0; |
| 1370 | Field = RT->getDecl()->field_begin(); |
| 1371 | for (; Field != FieldEnd; ++Field) { |
| 1372 | if (Field->isUnnamedBitfield()) |
| 1373 | continue; |
| 1374 | |
| 1375 | if (ReplacementField == *Field || |
| 1376 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1377 | break; |
| 1378 | |
| 1379 | ++FieldIndex; |
| 1380 | } |
| 1381 | } |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1382 | } else if (!KnownField && |
| 1383 | cast<RecordDecl>((*Field)->getDeclContext()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1384 | ->isAnonymousStructOrUnion()) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1385 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, *Field, |
| 1386 | Field, FieldIndex); |
| 1387 | D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1388 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1389 | |
| 1390 | // All of the fields of a union are located at the same place in |
| 1391 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1392 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1393 | FieldIndex = 0; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1394 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1395 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1396 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1397 | // Update the designator with the field declaration. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1398 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1400 | // Make sure that our non-designated initializer list has space |
| 1401 | // for a subobject corresponding to this field. |
| 1402 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1403 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1404 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1405 | // This designator names a flexible array member. |
| 1406 | if (Field->getType()->isIncompleteArrayType()) { |
| 1407 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1408 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1409 | // We can't designate an object within the flexible array |
| 1410 | // member (because GCC doesn't allow it). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | DesignatedInitExpr::Designator *NextD |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1412 | = DIE->getDesignator(DesigIdx + 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1414 | diag::err_designator_into_flexible_array_member) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | << SourceRange(NextD->getStartLocation(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1416 | DIE->getSourceRange().getEnd()); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1417 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1418 | << *Field; |
| 1419 | Invalid = true; |
| 1420 | } |
| 1421 | |
| 1422 | if (!hadError && !isa<InitListExpr>(DIE->getInit())) { |
| 1423 | // The initializer is not an initializer list. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1424 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1425 | diag::err_flexible_array_init_needs_braces) |
| 1426 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1427 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1428 | << *Field; |
| 1429 | Invalid = true; |
| 1430 | } |
| 1431 | |
| 1432 | // Handle GNU flexible array initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | if (!Invalid && !TopLevelObject && |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1434 | cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | SemaRef.Diag(DIE->getSourceRange().getBegin(), |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1436 | diag::err_flexible_array_init_nonempty) |
| 1437 | << DIE->getSourceRange().getBegin(); |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1438 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1439 | << *Field; |
| 1440 | Invalid = true; |
| 1441 | } |
| 1442 | |
| 1443 | if (Invalid) { |
| 1444 | ++Index; |
| 1445 | return true; |
| 1446 | } |
| 1447 | |
| 1448 | // Initialize the array. |
| 1449 | bool prevHadError = hadError; |
| 1450 | unsigned newStructuredIndex = FieldIndex; |
| 1451 | unsigned OldIndex = Index; |
| 1452 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1453 | |
| 1454 | InitializedEntity MemberEntity = |
| 1455 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1456 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1457 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1459 | IList->setInit(OldIndex, DIE); |
| 1460 | if (hadError && !prevHadError) { |
| 1461 | ++Field; |
| 1462 | ++FieldIndex; |
| 1463 | if (NextField) |
| 1464 | *NextField = Field; |
| 1465 | StructuredIndex = FieldIndex; |
| 1466 | return true; |
| 1467 | } |
| 1468 | } else { |
| 1469 | // Recurse to check later designated subobjects. |
| 1470 | QualType FieldType = (*Field)->getType(); |
| 1471 | unsigned newStructuredIndex = FieldIndex; |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1472 | |
| 1473 | InitializedEntity MemberEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1474 | InitializedEntity::InitializeMember(*Field, &Entity); |
| 1475 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1476 | FieldType, 0, 0, Index, |
| 1477 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1478 | true, false)) |
| 1479 | return true; |
| 1480 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1481 | |
| 1482 | // Find the position of the next field to be initialized in this |
| 1483 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1484 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1485 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1486 | |
| 1487 | // If this the first designator, our caller will continue checking |
| 1488 | // the rest of this struct/class/union subobject. |
| 1489 | if (IsFirstDesignator) { |
| 1490 | if (NextField) |
| 1491 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1492 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1493 | return false; |
| 1494 | } |
| 1495 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1496 | if (!FinishSubobjectInit) |
| 1497 | return false; |
| 1498 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1499 | // We've already initialized something in the union; we're done. |
| 1500 | if (RT->getDecl()->isUnion()) |
| 1501 | return hadError; |
| 1502 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1503 | // Check the remaining fields within this class/struct/union subobject. |
| 1504 | bool prevHadError = hadError; |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1505 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1506 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1507 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1508 | return hadError && !prevHadError; |
| 1509 | } |
| 1510 | |
| 1511 | // C99 6.7.8p6: |
| 1512 | // |
| 1513 | // If a designator has the form |
| 1514 | // |
| 1515 | // [ constant-expression ] |
| 1516 | // |
| 1517 | // then the current object (defined below) shall have array |
| 1518 | // type and the expression shall be an integer constant |
| 1519 | // expression. If the array is of unknown size, any |
| 1520 | // nonnegative value is valid. |
| 1521 | // |
| 1522 | // Additionally, cope with the GNU extension that permits |
| 1523 | // designators of the form |
| 1524 | // |
| 1525 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1526 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1527 | if (!AT) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1528 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1529 | << CurrentObjectType; |
| 1530 | ++Index; |
| 1531 | return true; |
| 1532 | } |
| 1533 | |
| 1534 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1535 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1536 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1537 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1538 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1539 | DesignatedEndIndex = DesignatedStartIndex; |
| 1540 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1541 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1542 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | |
| 1544 | DesignatedStartIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1545 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1546 | DesignatedEndIndex = |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1547 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1548 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1549 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1550 | if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue()) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1551 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1554 | if (isa<ConstantArrayType>(AT)) { |
| 1555 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1556 | DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 1557 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1558 | DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 1559 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1560 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1561 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1562 | diag::err_array_designator_too_large) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1563 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1564 | << IndexExpr->getSourceRange(); |
| 1565 | ++Index; |
| 1566 | return true; |
| 1567 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1568 | } else { |
| 1569 | // Make sure the bit-widths and signedness match. |
| 1570 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
| 1571 | DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1572 | else if (DesignatedStartIndex.getBitWidth() < |
| 1573 | DesignatedEndIndex.getBitWidth()) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1574 | DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
| 1575 | DesignatedStartIndex.setIsUnsigned(true); |
| 1576 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1577 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1578 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1579 | // Make sure that our non-designated initializer list has space |
| 1580 | // for a subobject corresponding to this array element. |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1581 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1583 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1584 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1585 | // Repeatedly perform subobject initializations in the range |
| 1586 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1587 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1588 | // Move to the next designator |
| 1589 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1590 | unsigned OldIndex = Index; |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1591 | |
| 1592 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1593 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1594 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1595 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1596 | // Recurse to check later designated subobjects. |
| 1597 | QualType ElementType = AT->getElementType(); |
| 1598 | Index = OldIndex; |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1599 | |
| 1600 | ElementEntity.setElementIndex(ElementIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1601 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1602 | ElementType, 0, 0, Index, |
| 1603 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1604 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1605 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1606 | return true; |
| 1607 | |
| 1608 | // Move to the next index in the array that we'll be initializing. |
| 1609 | ++DesignatedStartIndex; |
| 1610 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1611 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1612 | |
| 1613 | // If this the first designator, our caller will continue checking |
| 1614 | // the rest of this array subobject. |
| 1615 | if (IsFirstDesignator) { |
| 1616 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1617 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1618 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1619 | return false; |
| 1620 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1622 | if (!FinishSubobjectInit) |
| 1623 | return false; |
| 1624 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1625 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1626 | bool prevHadError = hadError; |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1627 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1628 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1629 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1630 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1633 | // Get the structured initializer list for a subobject of type |
| 1634 | // @p CurrentObjectType. |
| 1635 | InitListExpr * |
| 1636 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1637 | QualType CurrentObjectType, |
| 1638 | InitListExpr *StructuredList, |
| 1639 | unsigned StructuredIndex, |
| 1640 | SourceRange InitRange) { |
| 1641 | Expr *ExistingInit = 0; |
| 1642 | if (!StructuredList) |
| 1643 | ExistingInit = SyntacticToSemantic[IList]; |
| 1644 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1645 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1647 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1648 | return Result; |
| 1649 | |
| 1650 | if (ExistingInit) { |
| 1651 | // We are creating an initializer list that initializes the |
| 1652 | // subobjects of the current object, but there was already an |
| 1653 | // initialization that completely initialized the current |
| 1654 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1656 | // struct X { int a, b; }; |
| 1657 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1659 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1660 | // designated initializer re-initializes the whole |
| 1661 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1663 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1664 | << InitRange; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1666 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1667 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1668 | << ExistingInit->getSourceRange(); |
| 1669 | } |
| 1670 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | InitListExpr *Result |
| 1672 | = new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0, |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1673 | InitRange.getEnd()); |
| 1674 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1675 | Result->setType(CurrentObjectType); |
| 1676 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1677 | // Pre-allocate storage for the structured initializer list. |
| 1678 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1679 | unsigned NumInits = 0; |
| 1680 | if (!StructuredList) |
| 1681 | NumInits = IList->getNumInits(); |
| 1682 | else if (Index < IList->getNumInits()) { |
| 1683 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) |
| 1684 | NumInits = SubList->getNumInits(); |
| 1685 | } |
| 1686 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1687 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1688 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1689 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1690 | NumElements = CAType->getSize().getZExtValue(); |
| 1691 | // Simple heuristic so that we don't allocate a very large |
| 1692 | // initializer with many empty entries at the end. |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1693 | if (NumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1694 | NumElements = 0; |
| 1695 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1696 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1697 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1698 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1699 | RecordDecl *RDecl = RType->getDecl(); |
| 1700 | if (RDecl->isUnion()) |
| 1701 | NumElements = 1; |
| 1702 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1704 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1707 | if (NumElements < NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1708 | NumElements = IList->getNumInits(); |
| 1709 | |
| 1710 | Result->reserveInits(NumElements); |
| 1711 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1712 | // Link this new initializer list into the structured initializer |
| 1713 | // lists. |
| 1714 | if (StructuredList) |
| 1715 | StructuredList->updateInit(StructuredIndex, Result); |
| 1716 | else { |
| 1717 | Result->setSyntacticForm(IList); |
| 1718 | SyntacticToSemantic[IList] = Result; |
| 1719 | } |
| 1720 | |
| 1721 | return Result; |
| 1722 | } |
| 1723 | |
| 1724 | /// Update the initializer at index @p StructuredIndex within the |
| 1725 | /// structured initializer list to the value @p expr. |
| 1726 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1727 | unsigned &StructuredIndex, |
| 1728 | Expr *expr) { |
| 1729 | // No structured initializer list to update |
| 1730 | if (!StructuredList) |
| 1731 | return; |
| 1732 | |
| 1733 | if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) { |
| 1734 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1736 | diag::warn_initializer_overrides) |
| 1737 | << expr->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1739 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1740 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1741 | << PrevInit->getSourceRange(); |
| 1742 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1744 | ++StructuredIndex; |
| 1745 | } |
| 1746 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1747 | /// Check that the given Index expression is a valid array designator |
| 1748 | /// value. This is essentailly just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1749 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1750 | /// and produces a reasonable diagnostic if there is a |
| 1751 | /// failure. Returns true if there was an error, false otherwise. If |
| 1752 | /// everything went okay, Value will receive the value of the constant |
| 1753 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1754 | static bool |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1755 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1756 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1757 | |
| 1758 | // Make sure this is an integer constant expression. |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1759 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1760 | return true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1761 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1762 | if (Value.isSigned() && Value.isNegative()) |
| 1763 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1764 | << Value.toString(10) << Index->getSourceRange(); |
| 1765 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1766 | Value.setIsUnsigned(true); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1767 | return false; |
| 1768 | } |
| 1769 | |
| 1770 | Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
| 1771 | SourceLocation Loc, |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 1772 | bool GNUSyntax, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1773 | OwningExprResult Init) { |
| 1774 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1775 | |
| 1776 | bool Invalid = false; |
| 1777 | llvm::SmallVector<ASTDesignator, 32> Designators; |
| 1778 | llvm::SmallVector<Expr *, 32> InitExpressions; |
| 1779 | |
| 1780 | // Build designators and check array designator expressions. |
| 1781 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1782 | const Designator &D = Desig.getDesignator(Idx); |
| 1783 | switch (D.getKind()) { |
| 1784 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1786 | D.getFieldLoc())); |
| 1787 | break; |
| 1788 | |
| 1789 | case Designator::ArrayDesignator: { |
| 1790 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1791 | llvm::APSInt IndexValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1792 | if (!Index->isTypeDependent() && |
| 1793 | !Index->isValueDependent() && |
| 1794 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1795 | Invalid = true; |
| 1796 | else { |
| 1797 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1799 | D.getRBracketLoc())); |
| 1800 | InitExpressions.push_back(Index); |
| 1801 | } |
| 1802 | break; |
| 1803 | } |
| 1804 | |
| 1805 | case Designator::ArrayRangeDesignator: { |
| 1806 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1807 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1808 | llvm::APSInt StartValue; |
| 1809 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1810 | bool StartDependent = StartIndex->isTypeDependent() || |
| 1811 | StartIndex->isValueDependent(); |
| 1812 | bool EndDependent = EndIndex->isTypeDependent() || |
| 1813 | EndIndex->isValueDependent(); |
| 1814 | if ((!StartDependent && |
| 1815 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 1816 | (!EndDependent && |
| 1817 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1818 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1819 | else { |
| 1820 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1821 | if (StartDependent || EndDependent) { |
| 1822 | // Nothing to compute. |
| 1823 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1824 | EndValue.extend(StartValue.getBitWidth()); |
| 1825 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
| 1826 | StartValue.extend(EndValue.getBitWidth()); |
| 1827 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 1828 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1829 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1831 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 1832 | Invalid = true; |
| 1833 | } else { |
| 1834 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1835 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1836 | D.getEllipsisLoc(), |
| 1837 | D.getRBracketLoc())); |
| 1838 | InitExpressions.push_back(StartIndex); |
| 1839 | InitExpressions.push_back(EndIndex); |
| 1840 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1841 | } |
| 1842 | break; |
| 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | if (Invalid || Init.isInvalid()) |
| 1848 | return ExprError(); |
| 1849 | |
| 1850 | // Clear out the expressions within the designation. |
| 1851 | Desig.ClearExprs(*this); |
| 1852 | |
| 1853 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1854 | = DesignatedInitExpr::Create(Context, |
| 1855 | Designators.data(), Designators.size(), |
| 1856 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1857 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1858 | return Owned(DIE); |
| 1859 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1860 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 1861 | bool Sema::CheckInitList(const InitializedEntity &Entity, |
| 1862 | InitListExpr *&InitList, QualType &DeclType) { |
| 1863 | InitListChecker CheckInitList(*this, Entity, InitList, DeclType); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1864 | if (!CheckInitList.HadError()) |
| 1865 | InitList = CheckInitList.getFullyStructuredList(); |
| 1866 | |
| 1867 | return CheckInitList.HadError(); |
| 1868 | } |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1869 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1870 | //===----------------------------------------------------------------------===// |
| 1871 | // Initialization entity |
| 1872 | //===----------------------------------------------------------------------===// |
| 1873 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 1874 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
| 1875 | const InitializedEntity &Parent) |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1876 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 1877 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1878 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 1879 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1880 | Type = AT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1881 | } else { |
| 1882 | Kind = EK_VectorElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1883 | Type = Parent.getType()->getAs<VectorType>()->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1884 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
| 1888 | CXXBaseSpecifier *Base) |
| 1889 | { |
| 1890 | InitializedEntity Result; |
| 1891 | Result.Kind = EK_Base; |
| 1892 | Result.Base = Base; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1893 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1894 | return Result; |
| 1895 | } |
| 1896 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1897 | DeclarationName InitializedEntity::getName() const { |
| 1898 | switch (getKind()) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1899 | case EK_Parameter: |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 1900 | if (!VariableOrMember) |
| 1901 | return DeclarationName(); |
| 1902 | // Fall through |
| 1903 | |
| 1904 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1905 | case EK_Member: |
| 1906 | return VariableOrMember->getDeclName(); |
| 1907 | |
| 1908 | case EK_Result: |
| 1909 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 1910 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1911 | case EK_Temporary: |
| 1912 | case EK_Base: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1913 | case EK_ArrayElement: |
| 1914 | case EK_VectorElement: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1915 | return DeclarationName(); |
| 1916 | } |
| 1917 | |
| 1918 | // Silence GCC warning |
| 1919 | return DeclarationName(); |
| 1920 | } |
| 1921 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 1922 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 1923 | switch (getKind()) { |
| 1924 | case EK_Variable: |
| 1925 | case EK_Parameter: |
| 1926 | case EK_Member: |
| 1927 | return VariableOrMember; |
| 1928 | |
| 1929 | case EK_Result: |
| 1930 | case EK_Exception: |
| 1931 | case EK_New: |
| 1932 | case EK_Temporary: |
| 1933 | case EK_Base: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 1934 | case EK_ArrayElement: |
| 1935 | case EK_VectorElement: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 1936 | return 0; |
| 1937 | } |
| 1938 | |
| 1939 | // Silence GCC warning |
| 1940 | return 0; |
| 1941 | } |
| 1942 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1943 | //===----------------------------------------------------------------------===// |
| 1944 | // Initialization sequence |
| 1945 | //===----------------------------------------------------------------------===// |
| 1946 | |
| 1947 | void InitializationSequence::Step::Destroy() { |
| 1948 | switch (Kind) { |
| 1949 | case SK_ResolveAddressOfOverloadedFunction: |
| 1950 | case SK_CastDerivedToBaseRValue: |
| 1951 | case SK_CastDerivedToBaseLValue: |
| 1952 | case SK_BindReference: |
| 1953 | case SK_BindReferenceToTemporary: |
| 1954 | case SK_UserConversion: |
| 1955 | case SK_QualificationConversionRValue: |
| 1956 | case SK_QualificationConversionLValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 1957 | case SK_ListInitialization: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 1958 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 1959 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 1960 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 1961 | case SK_StringInit: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1962 | break; |
| 1963 | |
| 1964 | case SK_ConversionSequence: |
| 1965 | delete ICS; |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | void InitializationSequence::AddAddressOverloadResolutionStep( |
| 1970 | FunctionDecl *Function) { |
| 1971 | Step S; |
| 1972 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 1973 | S.Type = Function->getType(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 1974 | // Access is currently ignored for these. |
| 1975 | S.Function = DeclAccessPair::make(Function, AccessSpecifier(0)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1976 | Steps.push_back(S); |
| 1977 | } |
| 1978 | |
| 1979 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
| 1980 | bool IsLValue) { |
| 1981 | Step S; |
| 1982 | S.Kind = IsLValue? SK_CastDerivedToBaseLValue : SK_CastDerivedToBaseRValue; |
| 1983 | S.Type = BaseType; |
| 1984 | Steps.push_back(S); |
| 1985 | } |
| 1986 | |
| 1987 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
| 1988 | bool BindingTemporary) { |
| 1989 | Step S; |
| 1990 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 1991 | S.Type = T; |
| 1992 | Steps.push_back(S); |
| 1993 | } |
| 1994 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 1995 | void InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 1996 | AccessSpecifier Access, |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 1997 | QualType T) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 1998 | Step S; |
| 1999 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2000 | S.Type = T; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2001 | S.Function = DeclAccessPair::make(Function, Access); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2002 | Steps.push_back(S); |
| 2003 | } |
| 2004 | |
| 2005 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
| 2006 | bool IsLValue) { |
| 2007 | Step S; |
| 2008 | S.Kind = IsLValue? SK_QualificationConversionLValue |
| 2009 | : SK_QualificationConversionRValue; |
| 2010 | S.Type = Ty; |
| 2011 | Steps.push_back(S); |
| 2012 | } |
| 2013 | |
| 2014 | void InitializationSequence::AddConversionSequenceStep( |
| 2015 | const ImplicitConversionSequence &ICS, |
| 2016 | QualType T) { |
| 2017 | Step S; |
| 2018 | S.Kind = SK_ConversionSequence; |
| 2019 | S.Type = T; |
| 2020 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2021 | Steps.push_back(S); |
| 2022 | } |
| 2023 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2024 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2025 | Step S; |
| 2026 | S.Kind = SK_ListInitialization; |
| 2027 | S.Type = T; |
| 2028 | Steps.push_back(S); |
| 2029 | } |
| 2030 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2031 | void |
| 2032 | InitializationSequence::AddConstructorInitializationStep( |
| 2033 | CXXConstructorDecl *Constructor, |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2034 | AccessSpecifier Access, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2035 | QualType T) { |
| 2036 | Step S; |
| 2037 | S.Kind = SK_ConstructorInitialization; |
| 2038 | S.Type = T; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2039 | S.Function = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2040 | Steps.push_back(S); |
| 2041 | } |
| 2042 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2043 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2044 | Step S; |
| 2045 | S.Kind = SK_ZeroInitialization; |
| 2046 | S.Type = T; |
| 2047 | Steps.push_back(S); |
| 2048 | } |
| 2049 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2050 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2051 | Step S; |
| 2052 | S.Kind = SK_CAssignment; |
| 2053 | S.Type = T; |
| 2054 | Steps.push_back(S); |
| 2055 | } |
| 2056 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2057 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2058 | Step S; |
| 2059 | S.Kind = SK_StringInit; |
| 2060 | S.Type = T; |
| 2061 | Steps.push_back(S); |
| 2062 | } |
| 2063 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2064 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
| 2065 | OverloadingResult Result) { |
| 2066 | SequenceKind = FailedSequence; |
| 2067 | this->Failure = Failure; |
| 2068 | this->FailedOverloadResult = Result; |
| 2069 | } |
| 2070 | |
| 2071 | //===----------------------------------------------------------------------===// |
| 2072 | // Attempt initialization |
| 2073 | //===----------------------------------------------------------------------===// |
| 2074 | |
| 2075 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2076 | static void TryListInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2077 | const InitializedEntity &Entity, |
| 2078 | const InitializationKind &Kind, |
| 2079 | InitListExpr *InitList, |
| 2080 | InitializationSequence &Sequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2081 | // FIXME: We only perform rudimentary checking of list |
| 2082 | // initializations at this point, then assume that any list |
| 2083 | // initialization of an array, aggregate, or scalar will be |
| 2084 | // well-formed. We we actually "perform" list initialization, we'll |
| 2085 | // do all of the necessary checking. C++0x initializer lists will |
| 2086 | // force us to perform more checking here. |
| 2087 | Sequence.setSequenceKind(InitializationSequence::ListInitialization); |
| 2088 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2089 | QualType DestType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2090 | |
| 2091 | // C++ [dcl.init]p13: |
| 2092 | // If T is a scalar type, then a declaration of the form |
| 2093 | // |
| 2094 | // T x = { a }; |
| 2095 | // |
| 2096 | // is equivalent to |
| 2097 | // |
| 2098 | // T x = a; |
| 2099 | if (DestType->isScalarType()) { |
| 2100 | if (InitList->getNumInits() > 1 && S.getLangOptions().CPlusPlus) { |
| 2101 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 2102 | return; |
| 2103 | } |
| 2104 | |
| 2105 | // Assume scalar initialization from a single value works. |
| 2106 | } else if (DestType->isAggregateType()) { |
| 2107 | // Assume aggregate initialization works. |
| 2108 | } else if (DestType->isVectorType()) { |
| 2109 | // Assume vector initialization works. |
| 2110 | } else if (DestType->isReferenceType()) { |
| 2111 | // FIXME: C++0x defines behavior for this. |
| 2112 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 2113 | return; |
| 2114 | } else if (DestType->isRecordType()) { |
| 2115 | // FIXME: C++0x defines behavior for this |
| 2116 | Sequence.SetFailed(InitializationSequence::FK_InitListBadDestinationType); |
| 2117 | } |
| 2118 | |
| 2119 | // Add a general "list initialization" step. |
| 2120 | Sequence.AddListInitializationStep(DestType); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | /// \brief Try a reference initialization that involves calling a conversion |
| 2124 | /// function. |
| 2125 | /// |
| 2126 | /// FIXME: look intos DRs 656, 896 |
| 2127 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 2128 | const InitializedEntity &Entity, |
| 2129 | const InitializationKind &Kind, |
| 2130 | Expr *Initializer, |
| 2131 | bool AllowRValues, |
| 2132 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2133 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2134 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 2135 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 2136 | QualType cv2T2 = Initializer->getType(); |
| 2137 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 2138 | |
| 2139 | bool DerivedToBase; |
| 2140 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
| 2141 | T1, T2, DerivedToBase) && |
| 2142 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 2143 | (void)DerivedToBase; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2144 | |
| 2145 | // Build the candidate set directly in the initialization sequence |
| 2146 | // structure, so that it will persist if we fail. |
| 2147 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2148 | CandidateSet.clear(); |
| 2149 | |
| 2150 | // Determine whether we are allowed to call explicit constructors or |
| 2151 | // explicit conversion operators. |
| 2152 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
| 2153 | |
| 2154 | const RecordType *T1RecordType = 0; |
| 2155 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>())) { |
| 2156 | // The type we're converting to is a class type. Enumerate its constructors |
| 2157 | // to see if there is a suitable conversion. |
| 2158 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
| 2159 | |
| 2160 | DeclarationName ConstructorName |
| 2161 | = S.Context.DeclarationNames.getCXXConstructorName( |
| 2162 | S.Context.getCanonicalType(T1).getUnqualifiedType()); |
| 2163 | DeclContext::lookup_iterator Con, ConEnd; |
| 2164 | for (llvm::tie(Con, ConEnd) = T1RecordDecl->lookup(ConstructorName); |
| 2165 | Con != ConEnd; ++Con) { |
| 2166 | // Find the constructor (which may be a template). |
| 2167 | CXXConstructorDecl *Constructor = 0; |
| 2168 | FunctionTemplateDecl *ConstructorTmpl |
| 2169 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 2170 | if (ConstructorTmpl) |
| 2171 | Constructor = cast<CXXConstructorDecl>( |
| 2172 | ConstructorTmpl->getTemplatedDecl()); |
| 2173 | else |
| 2174 | Constructor = cast<CXXConstructorDecl>(*Con); |
| 2175 | |
| 2176 | if (!Constructor->isInvalidDecl() && |
| 2177 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2178 | if (ConstructorTmpl) |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2179 | S.AddTemplateOverloadCandidate(ConstructorTmpl, |
| 2180 | ConstructorTmpl->getAccess(), |
| 2181 | /*ExplicitArgs*/ 0, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2182 | &Initializer, 1, CandidateSet); |
| 2183 | else |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2184 | S.AddOverloadCandidate(Constructor, Constructor->getAccess(), |
| 2185 | &Initializer, 1, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | } |
| 2189 | |
| 2190 | if (const RecordType *T2RecordType = T2->getAs<RecordType>()) { |
| 2191 | // The type we're converting from is a class type, enumerate its conversion |
| 2192 | // functions. |
| 2193 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 2194 | |
| 2195 | // Determine the type we are converting to. If we are allowed to |
| 2196 | // convert to an rvalue, take the type that the destination type |
| 2197 | // refers to. |
| 2198 | QualType ToType = AllowRValues? cv1T1 : DestType; |
| 2199 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2200 | const UnresolvedSetImpl *Conversions |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2201 | = T2RecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2202 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
| 2203 | E = Conversions->end(); I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2204 | NamedDecl *D = *I; |
| 2205 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2206 | if (isa<UsingShadowDecl>(D)) |
| 2207 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2208 | |
| 2209 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2210 | CXXConversionDecl *Conv; |
| 2211 | if (ConvTemplate) |
| 2212 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 2213 | else |
| 2214 | Conv = cast<CXXConversionDecl>(*I); |
| 2215 | |
| 2216 | // If the conversion function doesn't return a reference type, |
| 2217 | // it can't be considered for this conversion unless we're allowed to |
| 2218 | // consider rvalues. |
| 2219 | // FIXME: Do we need to make sure that we only consider conversion |
| 2220 | // candidates with reference-compatible results? That might be needed to |
| 2221 | // break recursion. |
| 2222 | if ((AllowExplicit || !Conv->isExplicit()) && |
| 2223 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 2224 | if (ConvTemplate) |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2225 | S.AddTemplateConversionCandidate(ConvTemplate, I.getAccess(), |
| 2226 | ActingDC, Initializer, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2227 | ToType, CandidateSet); |
| 2228 | else |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2229 | S.AddConversionCandidate(Conv, I.getAccess(), ActingDC, |
| 2230 | Initializer, cv1T1, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2231 | } |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2236 | |
| 2237 | // Perform overload resolution. If it fails, return the failed result. |
| 2238 | OverloadCandidateSet::iterator Best; |
| 2239 | if (OverloadingResult Result |
| 2240 | = S.BestViableFunction(CandidateSet, DeclLoc, Best)) |
| 2241 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2242 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2243 | FunctionDecl *Function = Best->Function; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2244 | |
| 2245 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2246 | if (isa<CXXConversionDecl>(Function)) |
| 2247 | T2 = Function->getResultType(); |
| 2248 | else |
| 2249 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2250 | |
| 2251 | // Add the user-defined conversion step. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2252 | Sequence.AddUserConversionStep(Function, Best->getAccess(), |
| 2253 | T2.getNonReferenceType()); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2254 | |
| 2255 | // Determine whether we need to perform derived-to-base or |
| 2256 | // cv-qualification adjustments. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2257 | bool NewDerivedToBase = false; |
| 2258 | Sema::ReferenceCompareResult NewRefRelationship |
| 2259 | = S.CompareReferenceRelationship(DeclLoc, T1, T2.getNonReferenceType(), |
| 2260 | NewDerivedToBase); |
| 2261 | assert(NewRefRelationship != Sema::Ref_Incompatible && |
| 2262 | "Overload resolution picked a bad conversion function"); |
| 2263 | (void)NewRefRelationship; |
| 2264 | if (NewDerivedToBase) |
| 2265 | Sequence.AddDerivedToBaseCastStep( |
| 2266 | S.Context.getQualifiedType(T1, |
| 2267 | T2.getNonReferenceType().getQualifiers()), |
| 2268 | /*isLValue=*/true); |
| 2269 | |
| 2270 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
| 2271 | Sequence.AddQualificationConversionStep(cv1T1, T2->isReferenceType()); |
| 2272 | |
| 2273 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 2274 | return OR_Success; |
| 2275 | } |
| 2276 | |
| 2277 | /// \brief Attempt reference initialization (C++0x [dcl.init.list]) |
| 2278 | static void TryReferenceInitialization(Sema &S, |
| 2279 | const InitializedEntity &Entity, |
| 2280 | const InitializationKind &Kind, |
| 2281 | Expr *Initializer, |
| 2282 | InitializationSequence &Sequence) { |
| 2283 | Sequence.setSequenceKind(InitializationSequence::ReferenceBinding); |
| 2284 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2285 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2286 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2287 | Qualifiers T1Quals; |
| 2288 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2289 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2290 | Qualifiers T2Quals; |
| 2291 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2292 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2293 | |
| 2294 | // If the initializer is the address of an overloaded function, try |
| 2295 | // to resolve the overloaded function. If all goes well, T2 is the |
| 2296 | // type of the resulting function. |
| 2297 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) { |
| 2298 | FunctionDecl *Fn = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 2299 | T1, |
| 2300 | false); |
| 2301 | if (!Fn) { |
| 2302 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 2303 | return; |
| 2304 | } |
| 2305 | |
| 2306 | Sequence.AddAddressOverloadResolutionStep(Fn); |
| 2307 | cv2T2 = Fn->getType(); |
| 2308 | T2 = cv2T2.getUnqualifiedType(); |
| 2309 | } |
| 2310 | |
| 2311 | // FIXME: Rvalue references |
| 2312 | bool ForceRValue = false; |
| 2313 | |
| 2314 | // Compute some basic properties of the types and the initializer. |
| 2315 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 2316 | bool isRValueRef = !isLValueRef; |
| 2317 | bool DerivedToBase = false; |
| 2318 | Expr::isLvalueResult InitLvalue = ForceRValue ? Expr::LV_InvalidExpression : |
| 2319 | Initializer->isLvalue(S.Context); |
| 2320 | Sema::ReferenceCompareResult RefRelationship |
| 2321 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase); |
| 2322 | |
| 2323 | // C++0x [dcl.init.ref]p5: |
| 2324 | // A reference to type "cv1 T1" is initialized by an expression of type |
| 2325 | // "cv2 T2" as follows: |
| 2326 | // |
| 2327 | // - If the reference is an lvalue reference and the initializer |
| 2328 | // expression |
| 2329 | OverloadingResult ConvOvlResult = OR_Success; |
| 2330 | if (isLValueRef) { |
| 2331 | if (InitLvalue == Expr::LV_Valid && |
| 2332 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2333 | // - is an lvalue (but is not a bit-field), and "cv1 T1" is |
| 2334 | // reference-compatible with "cv2 T2," or |
| 2335 | // |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2336 | // 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] | 2337 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2338 | // can occur. However, we do pay attention to whether it is a bit-field |
| 2339 | // to decide whether we're actually binding to a temporary created from |
| 2340 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2341 | if (DerivedToBase) |
| 2342 | Sequence.AddDerivedToBaseCastStep( |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2343 | S.Context.getQualifiedType(T1, T2Quals), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2344 | /*isLValue=*/true); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2345 | if (T1Quals != T2Quals) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2346 | Sequence.AddQualificationConversionStep(cv1T1, /*IsLValue=*/true); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2347 | bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() && |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2348 | (Initializer->getBitField() || Initializer->refersToVectorElement()); |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2349 | Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2350 | return; |
| 2351 | } |
| 2352 | |
| 2353 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 2354 | // reference-related to T2, and can be implicitly converted to an |
| 2355 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 2356 | // with "cv3 T3" (this conversion is selected by enumerating the |
| 2357 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 2358 | // one through overload resolution (13.3)), |
| 2359 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType()) { |
| 2360 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
| 2361 | Initializer, |
| 2362 | /*AllowRValues=*/false, |
| 2363 | Sequence); |
| 2364 | if (ConvOvlResult == OR_Success) |
| 2365 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2366 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 2367 | Sequence.SetOverloadFailure( |
| 2368 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2369 | ConvOvlResult); |
| 2370 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | // - Otherwise, the reference shall be an lvalue reference to a |
| 2375 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
| 2376 | // shall be an rvalue reference and the initializer expression shall |
| 2377 | // be an rvalue. |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 2378 | if (!((isLValueRef && T1Quals.hasConst() && !T1Quals.hasVolatile()) || |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2379 | (isRValueRef && InitLvalue != Expr::LV_Valid))) { |
| 2380 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 2381 | Sequence.SetOverloadFailure( |
| 2382 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2383 | ConvOvlResult); |
| 2384 | else if (isLValueRef) |
| 2385 | Sequence.SetFailed(InitLvalue == Expr::LV_Valid |
| 2386 | ? (RefRelationship == Sema::Ref_Related |
| 2387 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 2388 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 2389 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 2390 | else |
| 2391 | Sequence.SetFailed( |
| 2392 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 2393 | |
| 2394 | return; |
| 2395 | } |
| 2396 | |
| 2397 | // - If T1 and T2 are class types and |
| 2398 | if (T1->isRecordType() && T2->isRecordType()) { |
| 2399 | // - the initializer expression is an rvalue and "cv1 T1" is |
| 2400 | // reference-compatible with "cv2 T2", or |
| 2401 | if (InitLvalue != Expr::LV_Valid && |
| 2402 | RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification) { |
| 2403 | if (DerivedToBase) |
| 2404 | Sequence.AddDerivedToBaseCastStep( |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2405 | S.Context.getQualifiedType(T1, T2Quals), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2406 | /*isLValue=*/false); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2407 | if (T1Quals != T2Quals) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2408 | Sequence.AddQualificationConversionStep(cv1T1, /*IsLValue=*/false); |
| 2409 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 2410 | return; |
| 2411 | } |
| 2412 | |
| 2413 | // - T1 is not reference-related to T2 and the initializer expression |
| 2414 | // can be implicitly converted to an rvalue of type "cv3 T3" (this |
| 2415 | // conversion is selected by enumerating the applicable conversion |
| 2416 | // functions (13.3.1.6) and choosing the best one through overload |
| 2417 | // resolution (13.3)), |
| 2418 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 2419 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 2420 | Kind, Initializer, |
| 2421 | /*AllowRValues=*/true, |
| 2422 | Sequence); |
| 2423 | if (ConvOvlResult) |
| 2424 | Sequence.SetOverloadFailure( |
| 2425 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2426 | ConvOvlResult); |
| 2427 | |
| 2428 | return; |
| 2429 | } |
| 2430 | |
| 2431 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2432 | return; |
| 2433 | } |
| 2434 | |
| 2435 | // - If the initializer expression is an rvalue, with T2 an array type, |
| 2436 | // and "cv1 T1" is reference-compatible with "cv2 T2," the reference |
| 2437 | // is bound to the object represented by the rvalue (see 3.10). |
| 2438 | // FIXME: How can an array type be reference-compatible with anything? |
| 2439 | // Don't we mean the element types of T1 and T2? |
| 2440 | |
| 2441 | // - Otherwise, a temporary of type “cv1 T1” is created and initialized |
| 2442 | // from the initializer expression using the rules for a non-reference |
| 2443 | // copy initialization (8.5). The reference is then bound to the |
| 2444 | // temporary. [...] |
| 2445 | // Determine whether we are allowed to call explicit constructors or |
| 2446 | // explicit conversion operators. |
| 2447 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct); |
| 2448 | ImplicitConversionSequence ICS |
| 2449 | = S.TryImplicitConversion(Initializer, cv1T1, |
| 2450 | /*SuppressUserConversions=*/false, AllowExplicit, |
| 2451 | /*ForceRValue=*/false, |
| 2452 | /*FIXME:InOverloadResolution=*/false, |
| 2453 | /*UserCast=*/Kind.isExplicitCast()); |
| 2454 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2455 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2456 | // FIXME: Use the conversion function set stored in ICS to turn |
| 2457 | // this into an overloading ambiguity diagnostic. However, we need |
| 2458 | // to keep that set as an OverloadCandidateSet rather than as some |
| 2459 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2460 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 2461 | Sequence.SetOverloadFailure( |
| 2462 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 2463 | ConvOvlResult); |
| 2464 | else |
| 2465 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2466 | return; |
| 2467 | } |
| 2468 | |
| 2469 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 2470 | // same cv-qualification as, or greater cv-qualification |
| 2471 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2472 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 2473 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2474 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 2475 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2476 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 2477 | return; |
| 2478 | } |
| 2479 | |
| 2480 | // Perform the actual conversion. |
| 2481 | Sequence.AddConversionSequenceStep(ICS, cv1T1); |
| 2482 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 2483 | return; |
| 2484 | } |
| 2485 | |
| 2486 | /// \brief Attempt character array initialization from a string literal |
| 2487 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 2488 | static void TryStringLiteralInitialization(Sema &S, |
| 2489 | const InitializedEntity &Entity, |
| 2490 | const InitializationKind &Kind, |
| 2491 | Expr *Initializer, |
| 2492 | InitializationSequence &Sequence) { |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2493 | Sequence.setSequenceKind(InitializationSequence::StringInit); |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2494 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2497 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2498 | /// enumerates the constructors of the initialized entity and performs overload |
| 2499 | /// resolution to select the best. |
| 2500 | static void TryConstructorInitialization(Sema &S, |
| 2501 | const InitializedEntity &Entity, |
| 2502 | const InitializationKind &Kind, |
| 2503 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2504 | QualType DestType, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2505 | InitializationSequence &Sequence) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2506 | if (Kind.getKind() == InitializationKind::IK_Copy) |
| 2507 | Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion); |
| 2508 | else |
| 2509 | Sequence.setSequenceKind(InitializationSequence::ConstructorInitialization); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2510 | |
| 2511 | // Build the candidate set directly in the initialization sequence |
| 2512 | // structure, so that it will persist if we fail. |
| 2513 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2514 | CandidateSet.clear(); |
| 2515 | |
| 2516 | // Determine whether we are allowed to call explicit constructors or |
| 2517 | // explicit conversion operators. |
| 2518 | bool AllowExplicit = (Kind.getKind() == InitializationKind::IK_Direct || |
| 2519 | Kind.getKind() == InitializationKind::IK_Value || |
| 2520 | Kind.getKind() == InitializationKind::IK_Default); |
| 2521 | |
| 2522 | // The type we're converting to is a class type. Enumerate its constructors |
| 2523 | // to see if one is suitable. |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2524 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 2525 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 2526 | CXXRecordDecl *DestRecordDecl |
| 2527 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 2528 | |
| 2529 | DeclarationName ConstructorName |
| 2530 | = S.Context.DeclarationNames.getCXXConstructorName( |
| 2531 | S.Context.getCanonicalType(DestType).getUnqualifiedType()); |
| 2532 | DeclContext::lookup_iterator Con, ConEnd; |
| 2533 | for (llvm::tie(Con, ConEnd) = DestRecordDecl->lookup(ConstructorName); |
| 2534 | Con != ConEnd; ++Con) { |
| 2535 | // Find the constructor (which may be a template). |
| 2536 | CXXConstructorDecl *Constructor = 0; |
| 2537 | FunctionTemplateDecl *ConstructorTmpl |
| 2538 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 2539 | if (ConstructorTmpl) |
| 2540 | Constructor = cast<CXXConstructorDecl>( |
| 2541 | ConstructorTmpl->getTemplatedDecl()); |
| 2542 | else |
| 2543 | Constructor = cast<CXXConstructorDecl>(*Con); |
| 2544 | |
| 2545 | if (!Constructor->isInvalidDecl() && |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2546 | (AllowExplicit || !Constructor->isExplicit())) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2547 | if (ConstructorTmpl) |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2548 | S.AddTemplateOverloadCandidate(ConstructorTmpl, |
| 2549 | ConstructorTmpl->getAccess(), |
| 2550 | /*ExplicitArgs*/ 0, |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2551 | Args, NumArgs, CandidateSet); |
| 2552 | else |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2553 | S.AddOverloadCandidate(Constructor, Constructor->getAccess(), |
| 2554 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | SourceLocation DeclLoc = Kind.getLocation(); |
| 2559 | |
| 2560 | // Perform overload resolution. If it fails, return the failed result. |
| 2561 | OverloadCandidateSet::iterator Best; |
| 2562 | if (OverloadingResult Result |
| 2563 | = S.BestViableFunction(CandidateSet, DeclLoc, Best)) { |
| 2564 | Sequence.SetOverloadFailure( |
| 2565 | InitializationSequence::FK_ConstructorOverloadFailed, |
| 2566 | Result); |
| 2567 | return; |
| 2568 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2569 | |
| 2570 | // C++0x [dcl.init]p6: |
| 2571 | // If a program calls for the default initialization of an object |
| 2572 | // of a const-qualified type T, T shall be a class type with a |
| 2573 | // user-provided default constructor. |
| 2574 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 2575 | Entity.getType().isConstQualified() && |
| 2576 | cast<CXXConstructorDecl>(Best->Function)->isImplicit()) { |
| 2577 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2578 | return; |
| 2579 | } |
| 2580 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2581 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 2582 | // subsumed by the initialization. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2583 | if (Kind.getKind() == InitializationKind::IK_Copy) { |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2584 | Sequence.AddUserConversionStep(Best->Function, Best->getAccess(), DestType); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2585 | } else { |
| 2586 | Sequence.AddConstructorInitializationStep( |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2587 | cast<CXXConstructorDecl>(Best->Function), |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2588 | Best->getAccess(), |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2589 | DestType); |
| 2590 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2593 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
| 2594 | static void TryValueInitialization(Sema &S, |
| 2595 | const InitializedEntity &Entity, |
| 2596 | const InitializationKind &Kind, |
| 2597 | InitializationSequence &Sequence) { |
| 2598 | // C++ [dcl.init]p5: |
| 2599 | // |
| 2600 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2601 | QualType T = Entity.getType(); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2602 | |
| 2603 | // -- if T is an array type, then each element is value-initialized; |
| 2604 | while (const ArrayType *AT = S.Context.getAsArrayType(T)) |
| 2605 | T = AT->getElementType(); |
| 2606 | |
| 2607 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2608 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 2609 | // -- if T is a class type (clause 9) with a user-declared |
| 2610 | // constructor (12.1), then the default constructor for T is |
| 2611 | // called (and the initialization is ill-formed if T has no |
| 2612 | // accessible default constructor); |
| 2613 | // |
| 2614 | // FIXME: we really want to refer to a single subobject of the array, |
| 2615 | // but Entity doesn't have a way to capture that (yet). |
| 2616 | if (ClassDecl->hasUserDeclaredConstructor()) |
| 2617 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
| 2618 | |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2619 | // -- if T is a (possibly cv-qualified) non-union class type |
| 2620 | // without a user-provided constructor, then the object is |
| 2621 | // zero-initialized and, if T’s implicitly-declared default |
| 2622 | // constructor is non-trivial, that constructor is called. |
| 2623 | if ((ClassDecl->getTagKind() == TagDecl::TK_class || |
| 2624 | ClassDecl->getTagKind() == TagDecl::TK_struct) && |
| 2625 | !ClassDecl->hasTrivialConstructor()) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2626 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 2627 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence); |
| 2628 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2629 | } |
| 2630 | } |
| 2631 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2632 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2633 | Sequence.setSequenceKind(InitializationSequence::ZeroInitialization); |
| 2634 | } |
| 2635 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2636 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 2637 | static void TryDefaultInitialization(Sema &S, |
| 2638 | const InitializedEntity &Entity, |
| 2639 | const InitializationKind &Kind, |
| 2640 | InitializationSequence &Sequence) { |
| 2641 | assert(Kind.getKind() == InitializationKind::IK_Default); |
| 2642 | |
| 2643 | // C++ [dcl.init]p6: |
| 2644 | // To default-initialize an object of type T means: |
| 2645 | // - if T is an array type, each element is default-initialized; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2646 | QualType DestType = Entity.getType(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2647 | while (const ArrayType *Array = S.Context.getAsArrayType(DestType)) |
| 2648 | DestType = Array->getElementType(); |
| 2649 | |
| 2650 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 2651 | // constructor for T is called (and the initialization is ill-formed if |
| 2652 | // T has no accessible default constructor); |
| 2653 | if (DestType->isRecordType()) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2654 | return TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, |
| 2655 | Sequence); |
| 2656 | } |
| 2657 | |
| 2658 | // - otherwise, no initialization is performed. |
| 2659 | Sequence.setSequenceKind(InitializationSequence::NoInitialization); |
| 2660 | |
| 2661 | // If a program calls for the default initialization of an object of |
| 2662 | // a const-qualified type T, T shall be a class type with a user-provided |
| 2663 | // default constructor. |
| 2664 | if (DestType.isConstQualified()) |
| 2665 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 2666 | } |
| 2667 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2668 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 2669 | /// which enumerates all conversion functions and performs overload resolution |
| 2670 | /// to select the best. |
| 2671 | static void TryUserDefinedConversion(Sema &S, |
| 2672 | const InitializedEntity &Entity, |
| 2673 | const InitializationKind &Kind, |
| 2674 | Expr *Initializer, |
| 2675 | InitializationSequence &Sequence) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2676 | Sequence.setSequenceKind(InitializationSequence::UserDefinedConversion); |
| 2677 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2678 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2679 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 2680 | QualType SourceType = Initializer->getType(); |
| 2681 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 2682 | "Must have a class type to perform a user-defined conversion"); |
| 2683 | |
| 2684 | // Build the candidate set directly in the initialization sequence |
| 2685 | // structure, so that it will persist if we fail. |
| 2686 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2687 | CandidateSet.clear(); |
| 2688 | |
| 2689 | // Determine whether we are allowed to call explicit constructors or |
| 2690 | // explicit conversion operators. |
| 2691 | bool AllowExplicit = Kind.getKind() == InitializationKind::IK_Direct; |
| 2692 | |
| 2693 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 2694 | // The type we're converting to is a class type. Enumerate its constructors |
| 2695 | // to see if there is a suitable conversion. |
| 2696 | CXXRecordDecl *DestRecordDecl |
| 2697 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 2698 | |
| 2699 | DeclarationName ConstructorName |
| 2700 | = S.Context.DeclarationNames.getCXXConstructorName( |
| 2701 | S.Context.getCanonicalType(DestType).getUnqualifiedType()); |
| 2702 | DeclContext::lookup_iterator Con, ConEnd; |
| 2703 | for (llvm::tie(Con, ConEnd) = DestRecordDecl->lookup(ConstructorName); |
| 2704 | Con != ConEnd; ++Con) { |
| 2705 | // Find the constructor (which may be a template). |
| 2706 | CXXConstructorDecl *Constructor = 0; |
| 2707 | FunctionTemplateDecl *ConstructorTmpl |
| 2708 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 2709 | if (ConstructorTmpl) |
| 2710 | Constructor = cast<CXXConstructorDecl>( |
| 2711 | ConstructorTmpl->getTemplatedDecl()); |
| 2712 | else |
| 2713 | Constructor = cast<CXXConstructorDecl>(*Con); |
| 2714 | |
| 2715 | if (!Constructor->isInvalidDecl() && |
| 2716 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 2717 | if (ConstructorTmpl) |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2718 | S.AddTemplateOverloadCandidate(ConstructorTmpl, |
| 2719 | ConstructorTmpl->getAccess(), |
| 2720 | /*ExplicitArgs*/ 0, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2721 | &Initializer, 1, CandidateSet); |
| 2722 | else |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2723 | S.AddOverloadCandidate(Constructor, Constructor->getAccess(), |
| 2724 | &Initializer, 1, CandidateSet); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2725 | } |
| 2726 | } |
| 2727 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2728 | |
| 2729 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 2730 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2731 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 2732 | // The type we're converting from is a class type, enumerate its conversion |
| 2733 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2734 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 2735 | // We can only enumerate the conversion functions for a complete type; if |
| 2736 | // the type isn't complete, simply skip this step. |
| 2737 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 2738 | CXXRecordDecl *SourceRecordDecl |
| 2739 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2740 | |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2741 | const UnresolvedSetImpl *Conversions |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 2742 | = SourceRecordDecl->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 2743 | for (UnresolvedSetImpl::const_iterator I = Conversions->begin(), |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 2744 | E = Conversions->end(); |
| 2745 | I != E; ++I) { |
| 2746 | NamedDecl *D = *I; |
| 2747 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 2748 | if (isa<UsingShadowDecl>(D)) |
| 2749 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2750 | |
| 2751 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 2752 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2753 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 2754 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2755 | else |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 2756 | Conv = cast<CXXConversionDecl>(*I); |
| 2757 | |
| 2758 | if (AllowExplicit || !Conv->isExplicit()) { |
| 2759 | if (ConvTemplate) |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2760 | S.AddTemplateConversionCandidate(ConvTemplate, I.getAccess(), |
| 2761 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 2762 | CandidateSet); |
| 2763 | else |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 2764 | S.AddConversionCandidate(Conv, I.getAccess(), ActingDC, |
| 2765 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 2766 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2767 | } |
| 2768 | } |
| 2769 | } |
| 2770 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2771 | // Perform overload resolution. If it fails, return the failed result. |
| 2772 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2773 | if (OverloadingResult Result |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2774 | = S.BestViableFunction(CandidateSet, DeclLoc, Best)) { |
| 2775 | Sequence.SetOverloadFailure( |
| 2776 | InitializationSequence::FK_UserConversionOverloadFailed, |
| 2777 | Result); |
| 2778 | return; |
| 2779 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2780 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2781 | FunctionDecl *Function = Best->Function; |
| 2782 | |
| 2783 | if (isa<CXXConstructorDecl>(Function)) { |
| 2784 | // Add the user-defined conversion step. Any cv-qualification conversion is |
| 2785 | // subsumed by the initialization. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2786 | Sequence.AddUserConversionStep(Function, Best->getAccess(), DestType); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2787 | return; |
| 2788 | } |
| 2789 | |
| 2790 | // Add the user-defined conversion step that calls the conversion function. |
| 2791 | QualType ConvType = Function->getResultType().getNonReferenceType(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 2792 | Sequence.AddUserConversionStep(Function, Best->getAccess(), ConvType); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2793 | |
| 2794 | // If the conversion following the call to the conversion function is |
| 2795 | // interesting, add it as a separate step. |
| 2796 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 2797 | Best->FinalConversion.Third) { |
| 2798 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2799 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2800 | ICS.Standard = Best->FinalConversion; |
| 2801 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 2802 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | /// \brief Attempt an implicit conversion (C++ [conv]) converting from one |
| 2806 | /// non-class type to another. |
| 2807 | static void TryImplicitConversion(Sema &S, |
| 2808 | const InitializedEntity &Entity, |
| 2809 | const InitializationKind &Kind, |
| 2810 | Expr *Initializer, |
| 2811 | InitializationSequence &Sequence) { |
| 2812 | ImplicitConversionSequence ICS |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2813 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2814 | /*SuppressUserConversions=*/true, |
| 2815 | /*AllowExplicit=*/false, |
| 2816 | /*ForceRValue=*/false, |
| 2817 | /*FIXME:InOverloadResolution=*/false, |
| 2818 | /*UserCast=*/Kind.isExplicitCast()); |
| 2819 | |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2820 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2821 | Sequence.SetFailed(InitializationSequence::FK_ConversionFailed); |
| 2822 | return; |
| 2823 | } |
| 2824 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2825 | Sequence.AddConversionSequenceStep(ICS, Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | InitializationSequence::InitializationSequence(Sema &S, |
| 2829 | const InitializedEntity &Entity, |
| 2830 | const InitializationKind &Kind, |
| 2831 | Expr **Args, |
| 2832 | unsigned NumArgs) { |
| 2833 | ASTContext &Context = S.Context; |
| 2834 | |
| 2835 | // C++0x [dcl.init]p16: |
| 2836 | // The semantics of initializers are as follows. The destination type is |
| 2837 | // the type of the object or reference being initialized and the source |
| 2838 | // type is the type of the initializer expression. The source type is not |
| 2839 | // defined when the initializer is a braced-init-list or when it is a |
| 2840 | // parenthesized list of expressions. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2841 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2842 | |
| 2843 | if (DestType->isDependentType() || |
| 2844 | Expr::hasAnyTypeDependentArguments(Args, NumArgs)) { |
| 2845 | SequenceKind = DependentSequence; |
| 2846 | return; |
| 2847 | } |
| 2848 | |
| 2849 | QualType SourceType; |
| 2850 | Expr *Initializer = 0; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2851 | if (NumArgs == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2852 | Initializer = Args[0]; |
| 2853 | if (!isa<InitListExpr>(Initializer)) |
| 2854 | SourceType = Initializer->getType(); |
| 2855 | } |
| 2856 | |
| 2857 | // - If the initializer is a braced-init-list, the object is |
| 2858 | // list-initialized (8.5.4). |
| 2859 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 2860 | TryListInitialization(S, Entity, Kind, InitList, *this); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2861 | return; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | // - If the destination type is a reference type, see 8.5.3. |
| 2865 | if (DestType->isReferenceType()) { |
| 2866 | // C++0x [dcl.init.ref]p1: |
| 2867 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 2868 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 2869 | // by an object that can be converted into a T. |
| 2870 | // (Therefore, multiple arguments are not permitted.) |
| 2871 | if (NumArgs != 1) |
| 2872 | SetFailed(FK_TooManyInitsForReference); |
| 2873 | else |
| 2874 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
| 2875 | return; |
| 2876 | } |
| 2877 | |
| 2878 | // - If the destination type is an array of characters, an array of |
| 2879 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 2880 | // initializer is a string literal, see 8.5.2. |
| 2881 | if (Initializer && IsStringInit(Initializer, DestType, Context)) { |
| 2882 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 2883 | return; |
| 2884 | } |
| 2885 | |
| 2886 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2887 | if (Kind.getKind() == InitializationKind::IK_Value || |
| 2888 | (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2889 | TryValueInitialization(S, Entity, Kind, *this); |
| 2890 | return; |
| 2891 | } |
| 2892 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2893 | // Handle default initialization. |
| 2894 | if (Kind.getKind() == InitializationKind::IK_Default){ |
| 2895 | TryDefaultInitialization(S, Entity, Kind, *this); |
| 2896 | return; |
| 2897 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2898 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2899 | // - Otherwise, if the destination type is an array, the program is |
| 2900 | // ill-formed. |
| 2901 | if (const ArrayType *AT = Context.getAsArrayType(DestType)) { |
| 2902 | if (AT->getElementType()->isAnyCharacterType()) |
| 2903 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
| 2904 | else |
| 2905 | SetFailed(FK_ArrayNeedsInitList); |
| 2906 | |
| 2907 | return; |
| 2908 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2909 | |
| 2910 | // Handle initialization in C |
| 2911 | if (!S.getLangOptions().CPlusPlus) { |
| 2912 | setSequenceKind(CAssignment); |
| 2913 | AddCAssignmentStep(DestType); |
| 2914 | return; |
| 2915 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2916 | |
| 2917 | // - If the destination type is a (possibly cv-qualified) class type: |
| 2918 | if (DestType->isRecordType()) { |
| 2919 | // - If the initialization is direct-initialization, or if it is |
| 2920 | // copy-initialization where the cv-unqualified version of the |
| 2921 | // source type is the same class as, or a derived class of, the |
| 2922 | // class of the destination, constructors are considered. [...] |
| 2923 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 2924 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 2925 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 2926 | S.IsDerivedFrom(SourceType, DestType)))) |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2927 | TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2928 | Entity.getType(), *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2929 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
| 2930 | // user-defined conversion sequences that can convert from the source |
| 2931 | // type to the destination type or (when a conversion function is |
| 2932 | // used) to a derived class thereof are enumerated as described in |
| 2933 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 2934 | // (13.3). |
| 2935 | else |
| 2936 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 2937 | return; |
| 2938 | } |
| 2939 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2940 | if (NumArgs > 1) { |
| 2941 | SetFailed(FK_TooManyInitsForScalar); |
| 2942 | return; |
| 2943 | } |
| 2944 | assert(NumArgs == 1 && "Zero-argument case handled above"); |
| 2945 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2946 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
| 2947 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2948 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2949 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 2950 | return; |
| 2951 | } |
| 2952 | |
| 2953 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 2954 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2955 | // conversions (Clause 4) will be used, if necessary, to convert the |
| 2956 | // initializer expression to the cv-unqualified version of the |
| 2957 | // destination type; no user-defined conversions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2958 | setSequenceKind(StandardConversion); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2959 | TryImplicitConversion(S, Entity, Kind, Initializer, *this); |
| 2960 | } |
| 2961 | |
| 2962 | InitializationSequence::~InitializationSequence() { |
| 2963 | for (llvm::SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
| 2964 | StepEnd = Steps.end(); |
| 2965 | Step != StepEnd; ++Step) |
| 2966 | Step->Destroy(); |
| 2967 | } |
| 2968 | |
| 2969 | //===----------------------------------------------------------------------===// |
| 2970 | // Perform initialization |
| 2971 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2972 | static Sema::AssignmentAction |
| 2973 | getAssignmentAction(const InitializedEntity &Entity) { |
| 2974 | switch(Entity.getKind()) { |
| 2975 | case InitializedEntity::EK_Variable: |
| 2976 | case InitializedEntity::EK_New: |
| 2977 | return Sema::AA_Initializing; |
| 2978 | |
| 2979 | case InitializedEntity::EK_Parameter: |
| 2980 | // FIXME: Can we tell when we're sending vs. passing? |
| 2981 | return Sema::AA_Passing; |
| 2982 | |
| 2983 | case InitializedEntity::EK_Result: |
| 2984 | return Sema::AA_Returning; |
| 2985 | |
| 2986 | case InitializedEntity::EK_Exception: |
| 2987 | case InitializedEntity::EK_Base: |
| 2988 | llvm_unreachable("No assignment action for C++-specific initialization"); |
| 2989 | break; |
| 2990 | |
| 2991 | case InitializedEntity::EK_Temporary: |
| 2992 | // FIXME: Can we tell apart casting vs. converting? |
| 2993 | return Sema::AA_Casting; |
| 2994 | |
| 2995 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2996 | case InitializedEntity::EK_ArrayElement: |
| 2997 | case InitializedEntity::EK_VectorElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2998 | return Sema::AA_Initializing; |
| 2999 | } |
| 3000 | |
| 3001 | return Sema::AA_Converting; |
| 3002 | } |
| 3003 | |
| 3004 | static bool shouldBindAsTemporary(const InitializedEntity &Entity, |
| 3005 | bool IsCopy) { |
| 3006 | switch (Entity.getKind()) { |
| 3007 | case InitializedEntity::EK_Result: |
| 3008 | case InitializedEntity::EK_Exception: |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3009 | case InitializedEntity::EK_ArrayElement: |
| 3010 | case InitializedEntity::EK_Member: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3011 | return !IsCopy; |
| 3012 | |
| 3013 | case InitializedEntity::EK_New: |
| 3014 | case InitializedEntity::EK_Variable: |
| 3015 | case InitializedEntity::EK_Base: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3016 | case InitializedEntity::EK_VectorElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3017 | return false; |
| 3018 | |
| 3019 | case InitializedEntity::EK_Parameter: |
| 3020 | case InitializedEntity::EK_Temporary: |
| 3021 | return true; |
| 3022 | } |
| 3023 | |
| 3024 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 3025 | } |
| 3026 | |
| 3027 | /// \brief If we need to perform an additional copy of the initialized object |
| 3028 | /// for this kind of entity (e.g., the result of a function or an object being |
| 3029 | /// thrown), make the copy. |
| 3030 | static Sema::OwningExprResult CopyIfRequiredForEntity(Sema &S, |
| 3031 | const InitializedEntity &Entity, |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3032 | const InitializationKind &Kind, |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3033 | Sema::OwningExprResult CurInit) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3034 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
| 3035 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3036 | SourceLocation Loc; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3037 | |
| 3038 | switch (Entity.getKind()) { |
| 3039 | case InitializedEntity::EK_Result: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3040 | if (Entity.getType()->isReferenceType()) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3041 | return move(CurInit); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3042 | Loc = Entity.getReturnLoc(); |
| 3043 | break; |
| 3044 | |
| 3045 | case InitializedEntity::EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3046 | Loc = Entity.getThrowLoc(); |
| 3047 | break; |
| 3048 | |
| 3049 | case InitializedEntity::EK_Variable: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3050 | if (Entity.getType()->isReferenceType() || |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3051 | Kind.getKind() != InitializationKind::IK_Copy) |
| 3052 | return move(CurInit); |
| 3053 | Loc = Entity.getDecl()->getLocation(); |
| 3054 | break; |
| 3055 | |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 3056 | case InitializedEntity::EK_ArrayElement: |
| 3057 | case InitializedEntity::EK_Member: |
| 3058 | if (Entity.getType()->isReferenceType() || |
| 3059 | Kind.getKind() != InitializationKind::IK_Copy) |
| 3060 | return move(CurInit); |
| 3061 | Loc = CurInitExpr->getLocStart(); |
| 3062 | break; |
| 3063 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3064 | case InitializedEntity::EK_Parameter: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3065 | // FIXME: Do we need this initialization for a parameter? |
| 3066 | return move(CurInit); |
| 3067 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3068 | case InitializedEntity::EK_New: |
| 3069 | case InitializedEntity::EK_Temporary: |
| 3070 | case InitializedEntity::EK_Base: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 3071 | case InitializedEntity::EK_VectorElement: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3072 | // We don't need to copy for any of these initialized entities. |
| 3073 | return move(CurInit); |
| 3074 | } |
| 3075 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3076 | CXXRecordDecl *Class = 0; |
| 3077 | if (const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>()) |
| 3078 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 3079 | if (!Class) |
| 3080 | return move(CurInit); |
| 3081 | |
| 3082 | // Perform overload resolution using the class's copy constructors. |
| 3083 | DeclarationName ConstructorName |
| 3084 | = S.Context.DeclarationNames.getCXXConstructorName( |
| 3085 | S.Context.getCanonicalType(S.Context.getTypeDeclType(Class))); |
| 3086 | DeclContext::lookup_iterator Con, ConEnd; |
| 3087 | OverloadCandidateSet CandidateSet; |
| 3088 | for (llvm::tie(Con, ConEnd) = Class->lookup(ConstructorName); |
| 3089 | Con != ConEnd; ++Con) { |
| 3090 | // Find the constructor (which may be a template). |
| 3091 | CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(*Con); |
| 3092 | if (!Constructor || Constructor->isInvalidDecl() || |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 3093 | !Constructor->isCopyConstructor()) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3094 | continue; |
| 3095 | |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3096 | S.AddOverloadCandidate(Constructor, Constructor->getAccess(), |
| 3097 | &CurInitExpr, 1, CandidateSet); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | OverloadCandidateSet::iterator Best; |
| 3101 | switch (S.BestViableFunction(CandidateSet, Loc, Best)) { |
| 3102 | case OR_Success: |
| 3103 | break; |
| 3104 | |
| 3105 | case OR_No_Viable_Function: |
| 3106 | S.Diag(Loc, diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3107 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3108 | << CurInitExpr->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 3109 | S.PrintOverloadCandidates(CandidateSet, Sema::OCD_AllCandidates, |
| 3110 | &CurInitExpr, 1); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3111 | return S.ExprError(); |
| 3112 | |
| 3113 | case OR_Ambiguous: |
| 3114 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3115 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3116 | << CurInitExpr->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 3117 | S.PrintOverloadCandidates(CandidateSet, Sema::OCD_ViableCandidates, |
| 3118 | &CurInitExpr, 1); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3119 | return S.ExprError(); |
| 3120 | |
| 3121 | case OR_Deleted: |
| 3122 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3123 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3124 | << CurInitExpr->getSourceRange(); |
| 3125 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 3126 | << Best->Function->isDeleted(); |
| 3127 | return S.ExprError(); |
| 3128 | } |
| 3129 | |
| 3130 | CurInit.release(); |
| 3131 | return S.BuildCXXConstructExpr(Loc, CurInitExpr->getType(), |
| 3132 | cast<CXXConstructorDecl>(Best->Function), |
| 3133 | /*Elidable=*/true, |
| 3134 | Sema::MultiExprArg(S, |
| 3135 | (void**)&CurInitExpr, 1)); |
| 3136 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3137 | |
| 3138 | Action::OwningExprResult |
| 3139 | InitializationSequence::Perform(Sema &S, |
| 3140 | const InitializedEntity &Entity, |
| 3141 | const InitializationKind &Kind, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3142 | Action::MultiExprArg Args, |
| 3143 | QualType *ResultType) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3144 | if (SequenceKind == FailedSequence) { |
| 3145 | unsigned NumArgs = Args.size(); |
| 3146 | Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs); |
| 3147 | return S.ExprError(); |
| 3148 | } |
| 3149 | |
| 3150 | if (SequenceKind == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3151 | // If the declaration is a non-dependent, incomplete array type |
| 3152 | // that has an initializer, then its type will be completed once |
| 3153 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3154 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3155 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3156 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3157 | if (const IncompleteArrayType *ArrayT |
| 3158 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 3159 | // FIXME: We don't currently have the ability to accurately |
| 3160 | // compute the length of an initializer list without |
| 3161 | // performing full type-checking of the initializer list |
| 3162 | // (since we have to determine where braces are implicitly |
| 3163 | // introduced and such). So, we fall back to making the array |
| 3164 | // type a dependently-sized array type with no specified |
| 3165 | // bound. |
| 3166 | if (isa<InitListExpr>((Expr *)Args.get()[0])) { |
| 3167 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3168 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3169 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3170 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 3171 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 3172 | TypeLoc TL = TInfo->getTypeLoc(); |
| 3173 | if (IncompleteArrayTypeLoc *ArrayLoc |
| 3174 | = dyn_cast<IncompleteArrayTypeLoc>(&TL)) |
| 3175 | Brackets = ArrayLoc->getBracketsRange(); |
| 3176 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | *ResultType |
| 3180 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 3181 | /*NumElts=*/0, |
| 3182 | ArrayT->getSizeModifier(), |
| 3183 | ArrayT->getIndexTypeCVRQualifiers(), |
| 3184 | Brackets); |
| 3185 | } |
| 3186 | |
| 3187 | } |
| 3188 | } |
| 3189 | |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3190 | if (Kind.getKind() == InitializationKind::IK_Copy || Kind.isExplicitCast()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3191 | return Sema::OwningExprResult(S, Args.release()[0]); |
| 3192 | |
| 3193 | unsigned NumArgs = Args.size(); |
| 3194 | return S.Owned(new (S.Context) ParenListExpr(S.Context, |
| 3195 | SourceLocation(), |
| 3196 | (Expr **)Args.release(), |
| 3197 | NumArgs, |
| 3198 | SourceLocation())); |
| 3199 | } |
| 3200 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3201 | if (SequenceKind == NoInitialization) |
| 3202 | return S.Owned((Expr *)0); |
| 3203 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3204 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 3205 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 3206 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 3207 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3208 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 3209 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3210 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3211 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3212 | Sema::OwningExprResult CurInit = S.Owned((Expr *)0); |
| 3213 | |
| 3214 | assert(!Steps.empty() && "Cannot have an empty initialization sequence"); |
| 3215 | |
| 3216 | // For initialization steps that start with a single initializer, |
| 3217 | // grab the only argument out the Args and place it into the "current" |
| 3218 | // initializer. |
| 3219 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3220 | case SK_ResolveAddressOfOverloadedFunction: |
| 3221 | case SK_CastDerivedToBaseRValue: |
| 3222 | case SK_CastDerivedToBaseLValue: |
| 3223 | case SK_BindReference: |
| 3224 | case SK_BindReferenceToTemporary: |
| 3225 | case SK_UserConversion: |
| 3226 | case SK_QualificationConversionLValue: |
| 3227 | case SK_QualificationConversionRValue: |
| 3228 | case SK_ConversionSequence: |
| 3229 | case SK_ListInitialization: |
| 3230 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3231 | case SK_StringInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3232 | assert(Args.size() == 1); |
| 3233 | CurInit = Sema::OwningExprResult(S, ((Expr **)(Args.get()))[0]->Retain()); |
| 3234 | if (CurInit.isInvalid()) |
| 3235 | return S.ExprError(); |
| 3236 | break; |
| 3237 | |
| 3238 | case SK_ConstructorInitialization: |
| 3239 | case SK_ZeroInitialization: |
| 3240 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
| 3243 | // Walk through the computed steps for the initialization sequence, |
| 3244 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3245 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3246 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 3247 | Step != StepEnd; ++Step) { |
| 3248 | if (CurInit.isInvalid()) |
| 3249 | return S.ExprError(); |
| 3250 | |
| 3251 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3252 | QualType SourceType = CurInitExpr? CurInitExpr->getType() : QualType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3253 | |
| 3254 | switch (Step->Kind) { |
| 3255 | case SK_ResolveAddressOfOverloadedFunction: |
| 3256 | // Overload resolution determined which function invoke; update the |
| 3257 | // initializer to reflect that choice. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3258 | // Access control was done in overload resolution. |
| 3259 | CurInit = S.FixOverloadedFunctionReference(move(CurInit), |
| 3260 | cast<FunctionDecl>(Step->Function.getDecl())); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3261 | break; |
| 3262 | |
| 3263 | case SK_CastDerivedToBaseRValue: |
| 3264 | case SK_CastDerivedToBaseLValue: { |
| 3265 | // We have a derived-to-base cast that produces either an rvalue or an |
| 3266 | // lvalue. Perform that cast. |
| 3267 | |
| 3268 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 3269 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 3270 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
| 3271 | CurInitExpr->getLocStart(), |
| 3272 | CurInitExpr->getSourceRange(), |
| 3273 | IgnoreBaseAccess)) |
| 3274 | return S.ExprError(); |
| 3275 | |
| 3276 | CurInit = S.Owned(new (S.Context) ImplicitCastExpr(Step->Type, |
| 3277 | CastExpr::CK_DerivedToBase, |
| 3278 | (Expr*)CurInit.release(), |
| 3279 | Step->Kind == SK_CastDerivedToBaseLValue)); |
| 3280 | break; |
| 3281 | } |
| 3282 | |
| 3283 | case SK_BindReference: |
| 3284 | if (FieldDecl *BitField = CurInitExpr->getBitField()) { |
| 3285 | // References cannot bind to bit fields (C++ [dcl.init.ref]p5). |
| 3286 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3287 | << Entity.getType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3288 | << BitField->getDeclName() |
| 3289 | << CurInitExpr->getSourceRange(); |
| 3290 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 3291 | return S.ExprError(); |
| 3292 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 3293 | |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3294 | if (CurInitExpr->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 3295 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3296 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 3297 | << Entity.getType().isVolatileQualified() |
| 3298 | << CurInitExpr->getSourceRange(); |
| 3299 | return S.ExprError(); |
| 3300 | } |
| 3301 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3302 | // Reference binding does not have any corresponding ASTs. |
| 3303 | |
| 3304 | // Check exception specifications |
| 3305 | if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType)) |
| 3306 | return S.ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 3307 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3308 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 3309 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3310 | case SK_BindReferenceToTemporary: |
Anders Carlsson | a64a869 | 2010-02-03 16:38:03 +0000 | [diff] [blame^] | 3311 | // Reference binding does not have any corresponding ASTs. |
| 3312 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3313 | // Check exception specifications |
| 3314 | if (S.CheckExceptionSpecCompatibility(CurInitExpr, DestType)) |
| 3315 | return S.ExprError(); |
| 3316 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3317 | break; |
| 3318 | |
| 3319 | case SK_UserConversion: { |
| 3320 | // We have a user-defined conversion that invokes either a constructor |
| 3321 | // or a conversion function. |
| 3322 | CastExpr::CastKind CastKind = CastExpr::CK_Unknown; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3323 | bool IsCopy = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3324 | FunctionDecl *Fn = cast<FunctionDecl>(Step->Function.getDecl()); |
| 3325 | AccessSpecifier FnAccess = Step->Function.getAccess(); |
| 3326 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3327 | // Build a call to the selected constructor. |
| 3328 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(S); |
| 3329 | SourceLocation Loc = CurInitExpr->getLocStart(); |
| 3330 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3332 | // Determine the arguments required to actually perform the constructor |
| 3333 | // call. |
| 3334 | if (S.CompleteConstructorCall(Constructor, |
| 3335 | Sema::MultiExprArg(S, |
| 3336 | (void **)&CurInitExpr, |
| 3337 | 1), |
| 3338 | Loc, ConstructorArgs)) |
| 3339 | return S.ExprError(); |
| 3340 | |
| 3341 | // Build the an expression that constructs a temporary. |
| 3342 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
| 3343 | move_arg(ConstructorArgs)); |
| 3344 | if (CurInit.isInvalid()) |
| 3345 | return S.ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3346 | |
| 3347 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, FnAccess); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3348 | |
| 3349 | CastKind = CastExpr::CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3350 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 3351 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 3352 | S.IsDerivedFrom(SourceType, Class)) |
| 3353 | IsCopy = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3354 | } else { |
| 3355 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3356 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3357 | |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3358 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInitExpr, |
| 3359 | Conversion, FnAccess); |
| 3360 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3361 | // FIXME: Should we move this initialization into a separate |
| 3362 | // derived-to-base conversion? I believe the answer is "no", because |
| 3363 | // we don't want to turn off access control here for c-style casts. |
| 3364 | if (S.PerformObjectArgumentInitialization(CurInitExpr, Conversion)) |
| 3365 | return S.ExprError(); |
| 3366 | |
| 3367 | // Do a little dance to make sure that CurInit has the proper |
| 3368 | // pointer. |
| 3369 | CurInit.release(); |
| 3370 | |
| 3371 | // Build the actual call to the conversion function. |
| 3372 | CurInit = S.Owned(S.BuildCXXMemberCallExpr(CurInitExpr, Conversion)); |
| 3373 | if (CurInit.isInvalid() || !CurInit.get()) |
| 3374 | return S.ExprError(); |
| 3375 | |
| 3376 | CastKind = CastExpr::CK_UserDefinedConversion; |
| 3377 | } |
| 3378 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3379 | if (shouldBindAsTemporary(Entity, IsCopy)) |
| 3380 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 3381 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3382 | CurInitExpr = CurInit.takeAs<Expr>(); |
| 3383 | CurInit = S.Owned(new (S.Context) ImplicitCastExpr(CurInitExpr->getType(), |
| 3384 | CastKind, |
| 3385 | CurInitExpr, |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3386 | false)); |
| 3387 | |
| 3388 | if (!IsCopy) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3389 | CurInit = CopyIfRequiredForEntity(S, Entity, Kind, move(CurInit)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3390 | break; |
| 3391 | } |
| 3392 | |
| 3393 | case SK_QualificationConversionLValue: |
| 3394 | case SK_QualificationConversionRValue: |
| 3395 | // Perform a qualification conversion; these can never go wrong. |
| 3396 | S.ImpCastExprToType(CurInitExpr, Step->Type, |
| 3397 | CastExpr::CK_NoOp, |
| 3398 | Step->Kind == SK_QualificationConversionLValue); |
| 3399 | CurInit.release(); |
| 3400 | CurInit = S.Owned(CurInitExpr); |
| 3401 | break; |
| 3402 | |
| 3403 | case SK_ConversionSequence: |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 3404 | if (S.PerformImplicitConversion(CurInitExpr, Step->Type, Sema::AA_Converting, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3405 | false, false, *Step->ICS)) |
| 3406 | return S.ExprError(); |
| 3407 | |
| 3408 | CurInit.release(); |
| 3409 | CurInit = S.Owned(CurInitExpr); |
| 3410 | break; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3411 | |
| 3412 | case SK_ListInitialization: { |
| 3413 | InitListExpr *InitList = cast<InitListExpr>(CurInitExpr); |
| 3414 | QualType Ty = Step->Type; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 3415 | if (S.CheckInitList(Entity, InitList, ResultType? *ResultType : Ty)) |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3416 | return S.ExprError(); |
| 3417 | |
| 3418 | CurInit.release(); |
| 3419 | CurInit = S.Owned(InitList); |
| 3420 | break; |
| 3421 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3422 | |
| 3423 | case SK_ConstructorInitialization: { |
| 3424 | CXXConstructorDecl *Constructor |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3425 | = cast<CXXConstructorDecl>(Step->Function.getDecl()); |
| 3426 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3427 | // Build a call to the selected constructor. |
| 3428 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(S); |
| 3429 | SourceLocation Loc = Kind.getLocation(); |
| 3430 | |
| 3431 | // Determine the arguments required to actually perform the constructor |
| 3432 | // call. |
| 3433 | if (S.CompleteConstructorCall(Constructor, move(Args), |
| 3434 | Loc, ConstructorArgs)) |
| 3435 | return S.ExprError(); |
| 3436 | |
| 3437 | // Build the an expression that constructs a temporary. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3438 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
Douglas Gregor | 745880f | 2009-12-20 22:01:25 +0000 | [diff] [blame] | 3439 | Constructor, |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3440 | move_arg(ConstructorArgs), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3441 | ConstructorInitRequiresZeroInit, |
| 3442 | Entity.getKind() == InitializedEntity::EK_Base); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3443 | if (CurInit.isInvalid()) |
| 3444 | return S.ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 3445 | |
| 3446 | // Only check access if all of that succeeded. |
| 3447 | S.CheckConstructorAccess(Loc, Constructor, Step->Function.getAccess()); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3448 | |
| 3449 | bool Elidable |
| 3450 | = cast<CXXConstructExpr>((Expr *)CurInit.get())->isElidable(); |
| 3451 | if (shouldBindAsTemporary(Entity, Elidable)) |
| 3452 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 3453 | |
| 3454 | if (!Elidable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 3455 | CurInit = CopyIfRequiredForEntity(S, Entity, Kind, move(CurInit)); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3456 | break; |
| 3457 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3458 | |
| 3459 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3460 | step_iterator NextStep = Step; |
| 3461 | ++NextStep; |
| 3462 | if (NextStep != StepEnd && |
| 3463 | NextStep->Kind == SK_ConstructorInitialization) { |
| 3464 | // The need for zero-initialization is recorded directly into |
| 3465 | // the call to the object's constructor within the next step. |
| 3466 | ConstructorInitRequiresZeroInit = true; |
| 3467 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
| 3468 | S.getLangOptions().CPlusPlus && |
| 3469 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3470 | CurInit = S.Owned(new (S.Context) CXXZeroInitValueExpr(Step->Type, |
| 3471 | Kind.getRange().getBegin(), |
| 3472 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3473 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3474 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 3475 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3476 | break; |
| 3477 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3478 | |
| 3479 | case SK_CAssignment: { |
| 3480 | QualType SourceType = CurInitExpr->getType(); |
| 3481 | Sema::AssignConvertType ConvTy = |
| 3482 | S.CheckSingleAssignmentConstraints(Step->Type, CurInitExpr); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3483 | |
| 3484 | // If this is a call, allow conversion to a transparent union. |
| 3485 | if (ConvTy != Sema::Compatible && |
| 3486 | Entity.getKind() == InitializedEntity::EK_Parameter && |
| 3487 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExpr) |
| 3488 | == Sema::Compatible) |
| 3489 | ConvTy = Sema::Compatible; |
| 3490 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3491 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 3492 | Step->Type, SourceType, |
| 3493 | CurInitExpr, getAssignmentAction(Entity))) |
| 3494 | return S.ExprError(); |
| 3495 | |
| 3496 | CurInit.release(); |
| 3497 | CurInit = S.Owned(CurInitExpr); |
| 3498 | break; |
| 3499 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3500 | |
| 3501 | case SK_StringInit: { |
| 3502 | QualType Ty = Step->Type; |
| 3503 | CheckStringInit(CurInitExpr, ResultType ? *ResultType : Ty, S); |
| 3504 | break; |
| 3505 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3506 | } |
| 3507 | } |
| 3508 | |
| 3509 | return move(CurInit); |
| 3510 | } |
| 3511 | |
| 3512 | //===----------------------------------------------------------------------===// |
| 3513 | // Diagnose initialization failures |
| 3514 | //===----------------------------------------------------------------------===// |
| 3515 | bool InitializationSequence::Diagnose(Sema &S, |
| 3516 | const InitializedEntity &Entity, |
| 3517 | const InitializationKind &Kind, |
| 3518 | Expr **Args, unsigned NumArgs) { |
| 3519 | if (SequenceKind != FailedSequence) |
| 3520 | return false; |
| 3521 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3522 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3523 | switch (Failure) { |
| 3524 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3525 | // FIXME: Customize for the initialized entity? |
| 3526 | if (NumArgs == 0) |
| 3527 | S.Diag(Kind.getLocation(), diag::err_reference_without_init) |
| 3528 | << DestType.getNonReferenceType(); |
| 3529 | else // FIXME: diagnostic below could be better! |
| 3530 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
| 3531 | << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3532 | break; |
| 3533 | |
| 3534 | case FK_ArrayNeedsInitList: |
| 3535 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 3536 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) |
| 3537 | << (Failure == FK_ArrayNeedsInitListOrStringLiteral); |
| 3538 | break; |
| 3539 | |
| 3540 | case FK_AddressOfOverloadFailed: |
| 3541 | S.ResolveAddressOfOverloadedFunction(Args[0], |
| 3542 | DestType.getNonReferenceType(), |
| 3543 | true); |
| 3544 | break; |
| 3545 | |
| 3546 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3547 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3548 | switch (FailedOverloadResult) { |
| 3549 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3550 | if (Failure == FK_UserConversionOverloadFailed) |
| 3551 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 3552 | << Args[0]->getType() << DestType |
| 3553 | << Args[0]->getSourceRange(); |
| 3554 | else |
| 3555 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 3556 | << DestType << Args[0]->getType() |
| 3557 | << Args[0]->getSourceRange(); |
| 3558 | |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 3559 | S.PrintOverloadCandidates(FailedCandidateSet, Sema::OCD_ViableCandidates, |
| 3560 | Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3561 | break; |
| 3562 | |
| 3563 | case OR_No_Viable_Function: |
| 3564 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 3565 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 3566 | << Args[0]->getSourceRange(); |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 3567 | S.PrintOverloadCandidates(FailedCandidateSet, Sema::OCD_AllCandidates, |
| 3568 | Args, NumArgs); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3569 | break; |
| 3570 | |
| 3571 | case OR_Deleted: { |
| 3572 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 3573 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 3574 | << Args[0]->getSourceRange(); |
| 3575 | OverloadCandidateSet::iterator Best; |
| 3576 | OverloadingResult Ovl = S.BestViableFunction(FailedCandidateSet, |
| 3577 | Kind.getLocation(), |
| 3578 | Best); |
| 3579 | if (Ovl == OR_Deleted) { |
| 3580 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 3581 | << Best->Function->isDeleted(); |
| 3582 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3583 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3584 | } |
| 3585 | break; |
| 3586 | } |
| 3587 | |
| 3588 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3589 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3590 | break; |
| 3591 | } |
| 3592 | break; |
| 3593 | |
| 3594 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 3595 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 3596 | S.Diag(Kind.getLocation(), |
| 3597 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 3598 | ? diag::err_lvalue_reference_bind_to_temporary |
| 3599 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 3600 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3601 | << DestType.getNonReferenceType() |
| 3602 | << Args[0]->getType() |
| 3603 | << Args[0]->getSourceRange(); |
| 3604 | break; |
| 3605 | |
| 3606 | case FK_RValueReferenceBindingToLValue: |
| 3607 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
| 3608 | << Args[0]->getSourceRange(); |
| 3609 | break; |
| 3610 | |
| 3611 | case FK_ReferenceInitDropsQualifiers: |
| 3612 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 3613 | << DestType.getNonReferenceType() |
| 3614 | << Args[0]->getType() |
| 3615 | << Args[0]->getSourceRange(); |
| 3616 | break; |
| 3617 | |
| 3618 | case FK_ReferenceInitFailed: |
| 3619 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 3620 | << DestType.getNonReferenceType() |
| 3621 | << (Args[0]->isLvalue(S.Context) == Expr::LV_Valid) |
| 3622 | << Args[0]->getType() |
| 3623 | << Args[0]->getSourceRange(); |
| 3624 | break; |
| 3625 | |
| 3626 | case FK_ConversionFailed: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3627 | S.Diag(Kind.getLocation(), diag::err_init_conversion_failed) |
| 3628 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3629 | << DestType |
| 3630 | << (Args[0]->isLvalue(S.Context) == Expr::LV_Valid) |
| 3631 | << Args[0]->getType() |
| 3632 | << Args[0]->getSourceRange(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3633 | break; |
| 3634 | |
| 3635 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3636 | SourceRange R; |
| 3637 | |
| 3638 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
| 3639 | R = SourceRange(InitList->getInit(1)->getLocStart(), |
| 3640 | InitList->getLocEnd()); |
| 3641 | else |
| 3642 | R = SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3643 | |
| 3644 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3645 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 3646 | break; |
| 3647 | } |
| 3648 | |
| 3649 | case FK_ReferenceBindingToInitList: |
| 3650 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 3651 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 3652 | break; |
| 3653 | |
| 3654 | case FK_InitListBadDestinationType: |
| 3655 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 3656 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 3657 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3658 | |
| 3659 | case FK_ConstructorOverloadFailed: { |
| 3660 | SourceRange ArgsRange; |
| 3661 | if (NumArgs) |
| 3662 | ArgsRange = SourceRange(Args[0]->getLocStart(), |
| 3663 | Args[NumArgs - 1]->getLocEnd()); |
| 3664 | |
| 3665 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 3666 | // bad. |
| 3667 | switch (FailedOverloadResult) { |
| 3668 | case OR_Ambiguous: |
| 3669 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 3670 | << DestType << ArgsRange; |
John McCall | 8120162 | 2010-01-08 04:41:39 +0000 | [diff] [blame] | 3671 | S.PrintOverloadCandidates(FailedCandidateSet, |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 3672 | Sema::OCD_ViableCandidates, Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3673 | break; |
| 3674 | |
| 3675 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3676 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3677 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 3678 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 3679 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 3680 | // This is implicit default initialization of a member or |
| 3681 | // base within a constructor. If no viable function was |
| 3682 | // found, notify the user that she needs to explicitly |
| 3683 | // initialize this base/member. |
| 3684 | CXXConstructorDecl *Constructor |
| 3685 | = cast<CXXConstructorDecl>(S.CurContext); |
| 3686 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 3687 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 3688 | << Constructor->isImplicit() |
| 3689 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 3690 | << /*base=*/0 |
| 3691 | << Entity.getType(); |
| 3692 | |
| 3693 | RecordDecl *BaseDecl |
| 3694 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 3695 | ->getDecl(); |
| 3696 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 3697 | << S.Context.getTagDeclType(BaseDecl); |
| 3698 | } else { |
| 3699 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
| 3700 | << Constructor->isImplicit() |
| 3701 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 3702 | << /*member=*/1 |
| 3703 | << Entity.getName(); |
| 3704 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 3705 | |
| 3706 | if (const RecordType *Record |
| 3707 | = Entity.getType()->getAs<RecordType>()) |
| 3708 | S.Diag(Record->getDecl()->getLocation(), |
| 3709 | diag::note_previous_decl) |
| 3710 | << S.Context.getTagDeclType(Record->getDecl()); |
| 3711 | } |
| 3712 | break; |
| 3713 | } |
| 3714 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3715 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 3716 | << DestType << ArgsRange; |
John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 3717 | S.PrintOverloadCandidates(FailedCandidateSet, Sema::OCD_AllCandidates, |
| 3718 | Args, NumArgs); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 3719 | break; |
| 3720 | |
| 3721 | case OR_Deleted: { |
| 3722 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 3723 | << true << DestType << ArgsRange; |
| 3724 | OverloadCandidateSet::iterator Best; |
| 3725 | OverloadingResult Ovl = S.BestViableFunction(FailedCandidateSet, |
| 3726 | Kind.getLocation(), |
| 3727 | Best); |
| 3728 | if (Ovl == OR_Deleted) { |
| 3729 | S.Diag(Best->Function->getLocation(), diag::note_unavailable_here) |
| 3730 | << Best->Function->isDeleted(); |
| 3731 | } else { |
| 3732 | llvm_unreachable("Inconsistent overload resolution?"); |
| 3733 | } |
| 3734 | break; |
| 3735 | } |
| 3736 | |
| 3737 | case OR_Success: |
| 3738 | llvm_unreachable("Conversion did not fail!"); |
| 3739 | break; |
| 3740 | } |
| 3741 | break; |
| 3742 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3743 | |
| 3744 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 3745 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 3746 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 3747 | // This is implicit default-initialization of a const member in |
| 3748 | // a constructor. Complain that it needs to be explicitly |
| 3749 | // initialized. |
| 3750 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 3751 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
| 3752 | << Constructor->isImplicit() |
| 3753 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 3754 | << /*const=*/1 |
| 3755 | << Entity.getName(); |
| 3756 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 3757 | << Entity.getName(); |
| 3758 | } else { |
| 3759 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 3760 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 3761 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3762 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | return true; |
| 3766 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3767 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3768 | void InitializationSequence::dump(llvm::raw_ostream &OS) const { |
| 3769 | switch (SequenceKind) { |
| 3770 | case FailedSequence: { |
| 3771 | OS << "Failed sequence: "; |
| 3772 | switch (Failure) { |
| 3773 | case FK_TooManyInitsForReference: |
| 3774 | OS << "too many initializers for reference"; |
| 3775 | break; |
| 3776 | |
| 3777 | case FK_ArrayNeedsInitList: |
| 3778 | OS << "array requires initializer list"; |
| 3779 | break; |
| 3780 | |
| 3781 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 3782 | OS << "array requires initializer list or string literal"; |
| 3783 | break; |
| 3784 | |
| 3785 | case FK_AddressOfOverloadFailed: |
| 3786 | OS << "address of overloaded function failed"; |
| 3787 | break; |
| 3788 | |
| 3789 | case FK_ReferenceInitOverloadFailed: |
| 3790 | OS << "overload resolution for reference initialization failed"; |
| 3791 | break; |
| 3792 | |
| 3793 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 3794 | OS << "non-const lvalue reference bound to temporary"; |
| 3795 | break; |
| 3796 | |
| 3797 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 3798 | OS << "non-const lvalue reference bound to unrelated type"; |
| 3799 | break; |
| 3800 | |
| 3801 | case FK_RValueReferenceBindingToLValue: |
| 3802 | OS << "rvalue reference bound to an lvalue"; |
| 3803 | break; |
| 3804 | |
| 3805 | case FK_ReferenceInitDropsQualifiers: |
| 3806 | OS << "reference initialization drops qualifiers"; |
| 3807 | break; |
| 3808 | |
| 3809 | case FK_ReferenceInitFailed: |
| 3810 | OS << "reference initialization failed"; |
| 3811 | break; |
| 3812 | |
| 3813 | case FK_ConversionFailed: |
| 3814 | OS << "conversion failed"; |
| 3815 | break; |
| 3816 | |
| 3817 | case FK_TooManyInitsForScalar: |
| 3818 | OS << "too many initializers for scalar"; |
| 3819 | break; |
| 3820 | |
| 3821 | case FK_ReferenceBindingToInitList: |
| 3822 | OS << "referencing binding to initializer list"; |
| 3823 | break; |
| 3824 | |
| 3825 | case FK_InitListBadDestinationType: |
| 3826 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 3827 | break; |
| 3828 | |
| 3829 | case FK_UserConversionOverloadFailed: |
| 3830 | OS << "overloading failed for user-defined conversion"; |
| 3831 | break; |
| 3832 | |
| 3833 | case FK_ConstructorOverloadFailed: |
| 3834 | OS << "constructor overloading failed"; |
| 3835 | break; |
| 3836 | |
| 3837 | case FK_DefaultInitOfConst: |
| 3838 | OS << "default initialization of a const variable"; |
| 3839 | break; |
| 3840 | } |
| 3841 | OS << '\n'; |
| 3842 | return; |
| 3843 | } |
| 3844 | |
| 3845 | case DependentSequence: |
| 3846 | OS << "Dependent sequence: "; |
| 3847 | return; |
| 3848 | |
| 3849 | case UserDefinedConversion: |
| 3850 | OS << "User-defined conversion sequence: "; |
| 3851 | break; |
| 3852 | |
| 3853 | case ConstructorInitialization: |
| 3854 | OS << "Constructor initialization sequence: "; |
| 3855 | break; |
| 3856 | |
| 3857 | case ReferenceBinding: |
| 3858 | OS << "Reference binding: "; |
| 3859 | break; |
| 3860 | |
| 3861 | case ListInitialization: |
| 3862 | OS << "List initialization: "; |
| 3863 | break; |
| 3864 | |
| 3865 | case ZeroInitialization: |
| 3866 | OS << "Zero initialization\n"; |
| 3867 | return; |
| 3868 | |
| 3869 | case NoInitialization: |
| 3870 | OS << "No initialization\n"; |
| 3871 | return; |
| 3872 | |
| 3873 | case StandardConversion: |
| 3874 | OS << "Standard conversion: "; |
| 3875 | break; |
| 3876 | |
| 3877 | case CAssignment: |
| 3878 | OS << "C assignment: "; |
| 3879 | break; |
| 3880 | |
| 3881 | case StringInit: |
| 3882 | OS << "String initialization: "; |
| 3883 | break; |
| 3884 | } |
| 3885 | |
| 3886 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 3887 | if (S != step_begin()) { |
| 3888 | OS << " -> "; |
| 3889 | } |
| 3890 | |
| 3891 | switch (S->Kind) { |
| 3892 | case SK_ResolveAddressOfOverloadedFunction: |
| 3893 | OS << "resolve address of overloaded function"; |
| 3894 | break; |
| 3895 | |
| 3896 | case SK_CastDerivedToBaseRValue: |
| 3897 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 3898 | break; |
| 3899 | |
| 3900 | case SK_CastDerivedToBaseLValue: |
| 3901 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 3902 | break; |
| 3903 | |
| 3904 | case SK_BindReference: |
| 3905 | OS << "bind reference to lvalue"; |
| 3906 | break; |
| 3907 | |
| 3908 | case SK_BindReferenceToTemporary: |
| 3909 | OS << "bind reference to a temporary"; |
| 3910 | break; |
| 3911 | |
| 3912 | case SK_UserConversion: |
| 3913 | OS << "user-defined conversion via " << S->Function->getNameAsString(); |
| 3914 | break; |
| 3915 | |
| 3916 | case SK_QualificationConversionRValue: |
| 3917 | OS << "qualification conversion (rvalue)"; |
| 3918 | |
| 3919 | case SK_QualificationConversionLValue: |
| 3920 | OS << "qualification conversion (lvalue)"; |
| 3921 | break; |
| 3922 | |
| 3923 | case SK_ConversionSequence: |
| 3924 | OS << "implicit conversion sequence ("; |
| 3925 | S->ICS->DebugPrint(); // FIXME: use OS |
| 3926 | OS << ")"; |
| 3927 | break; |
| 3928 | |
| 3929 | case SK_ListInitialization: |
| 3930 | OS << "list initialization"; |
| 3931 | break; |
| 3932 | |
| 3933 | case SK_ConstructorInitialization: |
| 3934 | OS << "constructor initialization"; |
| 3935 | break; |
| 3936 | |
| 3937 | case SK_ZeroInitialization: |
| 3938 | OS << "zero initialization"; |
| 3939 | break; |
| 3940 | |
| 3941 | case SK_CAssignment: |
| 3942 | OS << "C assignment"; |
| 3943 | break; |
| 3944 | |
| 3945 | case SK_StringInit: |
| 3946 | OS << "string initialization"; |
| 3947 | break; |
| 3948 | } |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | void InitializationSequence::dump() const { |
| 3953 | dump(llvm::errs()); |
| 3954 | } |
| 3955 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3956 | //===----------------------------------------------------------------------===// |
| 3957 | // Initialization helper functions |
| 3958 | //===----------------------------------------------------------------------===// |
| 3959 | Sema::OwningExprResult |
| 3960 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 3961 | SourceLocation EqualLoc, |
| 3962 | OwningExprResult Init) { |
| 3963 | if (Init.isInvalid()) |
| 3964 | return ExprError(); |
| 3965 | |
| 3966 | Expr *InitE = (Expr *)Init.get(); |
| 3967 | assert(InitE && "No initialization expression?"); |
| 3968 | |
| 3969 | if (EqualLoc.isInvalid()) |
| 3970 | EqualLoc = InitE->getLocStart(); |
| 3971 | |
| 3972 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
| 3973 | EqualLoc); |
| 3974 | InitializationSequence Seq(*this, Entity, Kind, &InitE, 1); |
| 3975 | Init.release(); |
| 3976 | return Seq.Perform(*this, Entity, Kind, |
| 3977 | MultiExprArg(*this, (void**)&InitE, 1)); |
| 3978 | } |