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