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