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