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