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