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 | |
| 18 | #include "Sema.h" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Designator.h" |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 23 | #include <map> |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 24 | using namespace clang; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // Sema Initialization Checking |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 30 | static Expr *IsStringInit(Expr *Init, QualType DeclType, ASTContext &Context) { |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 31 | const ArrayType *AT = Context.getAsArrayType(DeclType); |
| 32 | if (!AT) return 0; |
| 33 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 34 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
| 35 | return 0; |
| 36 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 37 | // See if this is a string literal or @encode. |
| 38 | Init = Init->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 40 | // Handle @encode, which is a narrow string. |
| 41 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
| 42 | return Init; |
| 43 | |
| 44 | // Otherwise we can only handle string literals. |
| 45 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Chris Lattner | 012b339 | 2009-02-26 23:42:47 +0000 | [diff] [blame] | 46 | if (SL == 0) return 0; |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 47 | |
| 48 | QualType ElemTy = Context.getCanonicalType(AT->getElementType()); |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 49 | // char array can be initialized with a narrow string. |
| 50 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
| 51 | if (!SL->isWide()) |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 52 | return ElemTy->isCharType() ? Init : 0; |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 53 | |
Eli Friedman | 42a8465 | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 54 | // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with |
| 55 | // correction from DR343): "An array with element type compatible with a |
| 56 | // qualified or unqualified version of wchar_t may be initialized by a wide |
| 57 | // string literal, optionally enclosed in braces." |
| 58 | if (Context.typesAreCompatible(Context.getWCharType(), |
| 59 | ElemTy.getUnqualifiedType())) |
Chris Lattner | a919681 | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 60 | return Init; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | static bool CheckSingleInitializer(Expr *&Init, QualType DeclType, |
Chris Lattner | 94d2f68 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 66 | bool DirectInit, Sema &S) { |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 67 | // Get the type before calling CheckSingleAssignmentConstraints(), since |
| 68 | // it can promote the expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | QualType InitType = Init->getType(); |
| 70 | |
Chris Lattner | 94d2f68 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 71 | if (S.getLangOptions().CPlusPlus) { |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 72 | // FIXME: I dislike this error message. A lot. |
Chris Lattner | 94d2f68 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 73 | if (S.PerformImplicitConversion(Init, DeclType, "initializing", DirectInit)) |
| 74 | return S.Diag(Init->getSourceRange().getBegin(), |
| 75 | diag::err_typecheck_convert_incompatible) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | << DeclType << Init->getType() << "initializing" |
Chris Lattner | 94d2f68 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 77 | << Init->getSourceRange(); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 94d2f68 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 81 | Sema::AssignConvertType ConvTy = |
| 82 | S.CheckSingleAssignmentConstraints(DeclType, Init); |
| 83 | return S.DiagnoseAssignmentResult(ConvTy, Init->getLocStart(), DeclType, |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 84 | InitType, Init, "initializing"); |
| 85 | } |
| 86 | |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 87 | static void CheckStringInit(Expr *Str, QualType &DeclT, Sema &S) { |
| 88 | // Get the length of the string as parsed. |
| 89 | uint64_t StrLength = |
| 90 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 91 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 93 | const ArrayType *AT = S.Context.getAsArrayType(DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 94 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | // 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] | 96 | // being initialized to a string literal. |
| 97 | llvm::APSInt ConstVal(32); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 98 | ConstVal = StrLength; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 99 | // Return a new array type (C99 6.7.8p22). |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 100 | DeclT = S.Context.getConstantArrayWithoutExprType(IAT->getElementType(), |
| 101 | ConstVal, |
| 102 | ArrayType::Normal, 0); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 103 | return; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 104 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 106 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 108 | // C99 6.7.8p14. We have an array of character type with known size. However, |
| 109 | // the size may be smaller or larger than the string we are initializing. |
| 110 | // FIXME: Avoid truncation for 64-bit length strings. |
| 111 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
| 112 | S.Diag(Str->getSourceRange().getBegin(), |
| 113 | diag::warn_initializer_string_for_char_array_too_long) |
| 114 | << Str->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 116 | // Set the type to the actual size that we are initializing. If we have |
| 117 | // something like: |
| 118 | // char x[1] = "foo"; |
| 119 | // then this will set the string literal's type to char[1]. |
| 120 | Str->setType(DeclT); |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType, |
| 124 | SourceLocation InitLoc, |
Anders Carlsson | 28486d4 | 2009-05-30 20:41:30 +0000 | [diff] [blame] | 125 | DeclarationName InitEntity, bool DirectInit) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | if (DeclType->isDependentType() || |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 127 | Init->isTypeDependent() || Init->isValueDependent()) |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 128 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 130 | // C++ [dcl.init.ref]p1: |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 131 | // A variable declared to be a T& or T&&, that is "reference to type T" |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 132 | // (8.3.2), shall be initialized by an object, or function, of |
| 133 | // type T or by an object that can be converted into a T. |
| 134 | if (DeclType->isReferenceType()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | return CheckReferenceInit(Init, DeclType, |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 136 | /*SuppressUserConversions=*/false, |
| 137 | /*AllowExplicit=*/DirectInit, |
| 138 | /*ForceRValue=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 140 | // C99 6.7.8p3: The type of the entity to be initialized shall be an array |
| 141 | // of unknown size ("[]") or an object type that is not a variable array type. |
| 142 | if (const VariableArrayType *VAT = Context.getAsVariableArrayType(DeclType)) |
| 143 | return Diag(InitLoc, diag::err_variable_object_no_init) |
| 144 | << VAT->getSizeExpr()->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 146 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
| 147 | if (!InitList) { |
| 148 | // FIXME: Handle wide strings |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 149 | if (Expr *Str = IsStringInit(Init, DeclType, Context)) { |
| 150 | CheckStringInit(Str, DeclType, *this); |
Chris Lattner | 94e6c4b | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 151 | return false; |
| 152 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 154 | // C++ [dcl.init]p14: |
| 155 | // -- If the destination type is a (possibly cv-qualified) class |
| 156 | // type: |
| 157 | if (getLangOptions().CPlusPlus && DeclType->isRecordType()) { |
| 158 | QualType DeclTypeC = Context.getCanonicalType(DeclType); |
| 159 | QualType InitTypeC = Context.getCanonicalType(Init->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 161 | // -- If the initialization is direct-initialization, or if it is |
| 162 | // copy-initialization where the cv-unqualified version of the |
| 163 | // source type is the same class as, or a derived class of, the |
| 164 | // class of the destination, constructors are considered. |
| 165 | if ((DeclTypeC.getUnqualifiedType() == InitTypeC.getUnqualifiedType()) || |
| 166 | IsDerivedFrom(InitTypeC, DeclTypeC)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | const CXXRecordDecl *RD = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 168 | cast<CXXRecordDecl>(DeclType->getAs<RecordType>()->getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Anders Carlsson | 5063613 | 2009-05-27 16:38:58 +0000 | [diff] [blame] | 170 | // No need to make a CXXConstructExpr if both the ctor and dtor are |
| 171 | // trivial. |
| 172 | if (RD->hasTrivialConstructor() && RD->hasTrivialDestructor()) |
| 173 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 175 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 176 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | CXXConstructorDecl *Constructor |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 178 | = PerformInitializationByConstructor(DeclType, |
| 179 | MultiExprArg(*this, |
| 180 | (void **)&Init, 1), |
| 181 | InitLoc, Init->getSourceRange(), |
| 182 | InitEntity, |
| 183 | DirectInit? IK_Direct : IK_Copy, |
| 184 | ConstructorArgs); |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 185 | if (!Constructor) |
| 186 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
| 188 | OwningExprResult InitResult = |
Anders Carlsson | 1b4ebfa | 2009-09-05 07:40:38 +0000 | [diff] [blame] | 189 | BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | DeclType, Constructor, |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 191 | move_arg(ConstructorArgs)); |
Anders Carlsson | 6eb5557 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 192 | if (InitResult.isInvalid()) |
| 193 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Anders Carlsson | 6eb5557 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 195 | Init = InitResult.takeAs<Expr>(); |
Anders Carlsson | 98cee2f | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 196 | return false; |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 197 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 199 | // -- Otherwise (i.e., for the remaining copy-initialization |
| 200 | // cases), user-defined conversion sequences that can |
| 201 | // convert from the source type to the destination type or |
| 202 | // (when a conversion function is used) to a derived class |
| 203 | // thereof are enumerated as described in 13.3.1.4, and the |
| 204 | // best one is chosen through overload resolution |
| 205 | // (13.3). If the conversion cannot be done or is |
| 206 | // ambiguous, the initialization is ill-formed. The |
| 207 | // function selected is called with the initializer |
| 208 | // expression as its argument; if the function is a |
| 209 | // constructor, the call initializes a temporary of the |
| 210 | // destination type. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 211 | // FIXME: We're pretending to do copy elision here; return to this when we |
| 212 | // have ASTs for such things. |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 213 | if (!PerformImplicitConversion(Init, DeclType, "initializing")) |
| 214 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 216 | if (InitEntity) |
| 217 | return Diag(InitLoc, diag::err_cannot_initialize_decl) |
Chris Lattner | d86a13e | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 218 | << InitEntity << (int)(Init->isLvalue(Context) == Expr::LV_Valid) |
| 219 | << Init->getType() << Init->getSourceRange(); |
| 220 | return Diag(InitLoc, diag::err_cannot_initialize_decl_noname) |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 221 | << DeclType << (int)(Init->isLvalue(Context) == Expr::LV_Valid) |
| 222 | << Init->getType() << Init->getSourceRange(); |
| 223 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 225 | // C99 6.7.8p16. |
| 226 | if (DeclType->isArrayType()) |
| 227 | return Diag(Init->getLocStart(), diag::err_array_init_list_required) |
Chris Lattner | d86a13e | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 228 | << Init->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 94d2f68 | 2009-02-24 22:46:58 +0000 | [diff] [blame] | 230 | return CheckSingleInitializer(Init, DeclType, DirectInit, *this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Chris Lattner | 0cb7803 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 233 | bool hadError = CheckInitList(InitList, DeclType); |
| 234 | Init = InitList; |
| 235 | return hadError; |
| 236 | } |
| 237 | |
| 238 | //===----------------------------------------------------------------------===// |
| 239 | // Semantic checking for initializer lists. |
| 240 | //===----------------------------------------------------------------------===// |
| 241 | |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 242 | /// @brief Semantic checking for initializer lists. |
| 243 | /// |
| 244 | /// The InitListChecker class contains a set of routines that each |
| 245 | /// handle the initialization of a certain kind of entity, e.g., |
| 246 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 247 | /// InitListChecker itself performs a recursive walk of the subobject |
| 248 | /// structure of the type to be initialized, while stepping through |
| 249 | /// the initializer list one element at a time. The IList and Index |
| 250 | /// parameters to each of the Check* routines contain the active |
| 251 | /// (syntactic) initializer list and the index into that initializer |
| 252 | /// list that represents the current initializer. Each routine is |
| 253 | /// responsible for moving that Index forward as it consumes elements. |
| 254 | /// |
| 255 | /// Each Check* routine also has a StructuredList/StructuredIndex |
| 256 | /// arguments, which contains the current the "structured" (semantic) |
| 257 | /// initializer list and the index into that initializer list where we |
| 258 | /// are copying initializers as we map them over to the semantic |
| 259 | /// list. Once we have completed our recursive walk of the subobject |
| 260 | /// structure, we will have constructed a full semantic initializer |
| 261 | /// list. |
| 262 | /// |
| 263 | /// C99 designators cause changes in the initializer list traversal, |
| 264 | /// because they make the initialization "jump" into a specific |
| 265 | /// subobject and then continue the initialization from that |
| 266 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 267 | /// designated subobject and manages backing out the recursion to |
| 268 | /// initialize the subobjects after the one designated. |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 269 | namespace { |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 270 | class InitListChecker { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 271 | Sema &SemaRef; |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 272 | bool hadError; |
| 273 | std::map<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
| 274 | InitListExpr *FullyStructuredList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | |
| 276 | void CheckImplicitInitList(InitListExpr *ParentIList, QualType T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 277 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 278 | unsigned &StructuredIndex, |
| 279 | bool TopLevelObject = false); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 280 | void CheckExplicitInitList(InitListExpr *IList, QualType &T, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 281 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 282 | unsigned &StructuredIndex, |
| 283 | bool TopLevelObject = false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | void CheckListElementTypes(InitListExpr *IList, QualType &DeclType, |
| 285 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 286 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 287 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 288 | unsigned &StructuredIndex, |
| 289 | bool TopLevelObject = false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | void CheckSubElementType(InitListExpr *IList, QualType ElemType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 291 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 292 | InitListExpr *StructuredList, |
| 293 | unsigned &StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | void CheckScalarType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 295 | unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 296 | InitListExpr *StructuredList, |
| 297 | unsigned &StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | void CheckReferenceType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 299 | unsigned &Index, |
| 300 | InitListExpr *StructuredList, |
| 301 | unsigned &StructuredIndex); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 302 | void CheckVectorType(InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 303 | InitListExpr *StructuredList, |
| 304 | unsigned &StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | void CheckStructUnionTypes(InitListExpr *IList, QualType DeclType, |
| 306 | RecordDecl::field_iterator Field, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 307 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 308 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 309 | unsigned &StructuredIndex, |
| 310 | bool TopLevelObject = false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | void CheckArrayType(InitListExpr *IList, QualType &DeclType, |
| 312 | llvm::APSInt elementIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 313 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 314 | InitListExpr *StructuredList, |
| 315 | unsigned &StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | bool CheckDesignatedInitializer(InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 317 | unsigned DesigIdx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | QualType &CurrentObjectType, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 319 | RecordDecl::field_iterator *NextField, |
| 320 | llvm::APSInt *NextElementIndex, |
| 321 | unsigned &Index, |
| 322 | InitListExpr *StructuredList, |
| 323 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 324 | bool FinishSubobjectInit, |
| 325 | bool TopLevelObject); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 326 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 327 | QualType CurrentObjectType, |
| 328 | InitListExpr *StructuredList, |
| 329 | unsigned StructuredIndex, |
| 330 | SourceRange InitRange); |
Douglas Gregor | cde232f | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 331 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 332 | unsigned &StructuredIndex, |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 333 | Expr *expr); |
| 334 | int numArrayElements(QualType DeclType); |
| 335 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 336 | |
| 337 | void FillInValueInitializations(InitListExpr *ILE); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 338 | public: |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 339 | InitListChecker(Sema &S, InitListExpr *IL, QualType &T); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 340 | bool HadError() { return hadError; } |
| 341 | |
| 342 | // @brief Retrieves the fully-structured initializer list used for |
| 343 | // semantic analysis and code generation. |
| 344 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 345 | }; |
Chris Lattner | 9ececce | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 346 | } // end anonymous namespace |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 347 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 348 | /// Recursively replaces NULL values within the given initializer list |
| 349 | /// with expressions that perform value-initialization of the |
| 350 | /// appropriate type. |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 351 | void InitListChecker::FillInValueInitializations(InitListExpr *ILE) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 353 | "Should not have void type"); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 354 | SourceLocation Loc = ILE->getSourceRange().getBegin(); |
| 355 | if (ILE->getSyntacticForm()) |
| 356 | Loc = ILE->getSyntacticForm()->getSourceRange().getBegin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 358 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 359 | unsigned Init = 0, NumInits = ILE->getNumInits(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 361 | Field = RType->getDecl()->field_begin(), |
| 362 | FieldEnd = RType->getDecl()->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 363 | Field != FieldEnd; ++Field) { |
| 364 | if (Field->isUnnamedBitfield()) |
| 365 | continue; |
| 366 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 367 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 368 | if (Field->getType()->isReferenceType()) { |
| 369 | // C++ [dcl.init.aggr]p9: |
| 370 | // If an incomplete or empty initializer-list leaves a |
| 371 | // member of reference type uninitialized, the program is |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | // ill-formed. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 373 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 374 | << Field->getType() |
| 375 | << ILE->getSyntacticForm()->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | SemaRef.Diag(Field->getLocation(), |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 377 | diag::note_uninit_reference_member); |
| 378 | hadError = true; |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 379 | return; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 380 | } else if (SemaRef.CheckValueInitialization(Field->getType(), Loc)) { |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 381 | hadError = true; |
| 382 | return; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 383 | } |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 384 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 385 | // FIXME: If value-initialization involves calling a constructor, should |
| 386 | // we make that call explicit in the representation (even when it means |
| 387 | // extending the initializer list)? |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 388 | if (Init < NumInits && !hadError) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | ILE->setInit(Init, |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 390 | new (SemaRef.Context) ImplicitValueInitExpr(Field->getType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | } else if (InitListExpr *InnerILE |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 392 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 393 | FillInValueInitializations(InnerILE); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 394 | ++Init; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 395 | |
| 396 | // Only look at the first initialization of a union. |
| 397 | if (RType->getDecl()->isUnion()) |
| 398 | break; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 403 | |
| 404 | QualType ElementType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 406 | unsigned NumInits = ILE->getNumInits(); |
| 407 | unsigned NumElements = NumInits; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 408 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 409 | ElementType = AType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 410 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 411 | NumElements = CAType->getSize().getZExtValue(); |
| 412 | } else if (const VectorType *VType = ILE->getType()->getAsVectorType()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 413 | ElementType = VType->getElementType(); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 414 | NumElements = VType->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 416 | ElementType = ILE->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 418 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
| 419 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 420 | if (SemaRef.CheckValueInitialization(ElementType, Loc)) { |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 421 | hadError = true; |
| 422 | return; |
| 423 | } |
| 424 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 425 | // FIXME: If value-initialization involves calling a constructor, should |
| 426 | // we make that call explicit in the representation (even when it means |
| 427 | // extending the initializer list)? |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 428 | if (Init < NumInits && !hadError) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | ILE->setInit(Init, |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 430 | new (SemaRef.Context) ImplicitValueInitExpr(ElementType)); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 431 | } else if (InitListExpr *InnerILE |
| 432 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 433 | FillInValueInitializations(InnerILE); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Chris Lattner | d9ae05b | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 437 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 438 | InitListChecker::InitListChecker(Sema &S, InitListExpr *IL, QualType &T) |
| 439 | : SemaRef(S) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 440 | hadError = false; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 441 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 442 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 443 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | FullyStructuredList |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 445 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 446 | CheckExplicitInitList(IL, T, newIndex, FullyStructuredList, newStructuredIndex, |
| 447 | /*TopLevelObject=*/true); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 448 | |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 449 | if (!hadError) |
| 450 | FillInValueInitializations(FullyStructuredList); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 454 | // FIXME: use a proper constant |
| 455 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 456 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 457 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 458 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 459 | } |
| 460 | return maxElements; |
| 461 | } |
| 462 | |
| 463 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 464 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 465 | int InitializableMembers = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 467 | Field = structDecl->field_begin(), |
| 468 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 469 | Field != FieldEnd; ++Field) { |
| 470 | if ((*Field)->getIdentifier() || !(*Field)->isBitField()) |
| 471 | ++InitializableMembers; |
| 472 | } |
Argyrios Kyrtzidis | 554a07b | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 473 | if (structDecl->isUnion()) |
Eli Friedman | 0e56c82 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 474 | return std::min(InitializableMembers, 1); |
| 475 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | void InitListChecker::CheckImplicitInitList(InitListExpr *ParentIList, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 479 | QualType T, unsigned &Index, |
| 480 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 481 | unsigned &StructuredIndex, |
| 482 | bool TopLevelObject) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 483 | int maxElements = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 485 | if (T->isArrayType()) |
| 486 | maxElements = numArrayElements(T); |
| 487 | else if (T->isStructureType() || T->isUnionType()) |
| 488 | maxElements = numStructUnionElements(T); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 489 | else if (T->isVectorType()) |
| 490 | maxElements = T->getAsVectorType()->getNumElements(); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 491 | else |
| 492 | assert(0 && "CheckImplicitInitList(): Illegal type"); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 493 | |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 494 | if (maxElements == 0) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 495 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 496 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 497 | ++Index; |
Eli Friedman | e0f832b | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 498 | hadError = true; |
| 499 | return; |
| 500 | } |
| 501 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 502 | // Build a structured initializer list corresponding to this subobject. |
| 503 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 505 | StructuredIndex, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 506 | SourceRange(ParentIList->getInit(Index)->getSourceRange().getBegin(), |
| 507 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 508 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 509 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 510 | // Check the element types and build the structural subobject. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 511 | unsigned StartIndex = Index; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 512 | CheckListElementTypes(ParentIList, T, false, Index, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | StructuredSubobjectInitList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 514 | StructuredSubobjectInitIndex, |
| 515 | TopLevelObject); |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 516 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 517 | StructuredSubobjectInitList->setType(T); |
| 518 | |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 519 | // Update the structured sub-object initializer so that it's ending |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 520 | // range corresponds with the end of the last initializer it used. |
| 521 | if (EndIndex < ParentIList->getNumInits()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | SourceLocation EndLoc |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 523 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 524 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 525 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Steve Naroff | 125d73d | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 528 | void InitListChecker::CheckExplicitInitList(InitListExpr *IList, QualType &T, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 529 | unsigned &Index, |
| 530 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 531 | unsigned &StructuredIndex, |
| 532 | bool TopLevelObject) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 533 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 534 | SyntacticToSemantic[IList] = StructuredList; |
| 535 | StructuredList->setSyntacticForm(IList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | CheckListElementTypes(IList, T, true, Index, StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 537 | StructuredIndex, TopLevelObject); |
Steve Naroff | 125d73d | 2008-05-06 00:23:44 +0000 | [diff] [blame] | 538 | IList->setType(T); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 539 | StructuredList->setType(T); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 540 | if (hadError) |
| 541 | return; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 542 | |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 543 | if (Index < IList->getNumInits()) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 544 | // We have leftover initializers |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 545 | if (StructuredIndex == 1 && |
| 546 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 547 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 548 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 549 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 550 | hadError = true; |
| 551 | } |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 552 | // Special-case |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 553 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 554 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 555 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 556 | // Don't complain for incomplete types, since we'll get an error |
| 557 | // elsewhere |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 558 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | int initKind = |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 560 | CurrentObjectType->isArrayType()? 0 : |
| 561 | CurrentObjectType->isVectorType()? 1 : |
| 562 | CurrentObjectType->isScalarType()? 2 : |
| 563 | CurrentObjectType->isUnionType()? 3 : |
| 564 | 4; |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 565 | |
| 566 | unsigned DK = diag::warn_excess_initializers; |
Eli Friedman | bd32745 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 567 | if (SemaRef.getLangOptions().CPlusPlus) { |
| 568 | DK = diag::err_excess_initializers; |
| 569 | hadError = true; |
| 570 | } |
Nate Begeman | 425038c | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 571 | if (SemaRef.getLangOptions().OpenCL && initKind == 1) { |
| 572 | DK = diag::err_excess_initializers; |
| 573 | hadError = true; |
| 574 | } |
Douglas Gregor | 1cba5fe | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 575 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 576 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 577 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
Eli Friedman | 6fcdec2 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 580 | |
Eli Friedman | 0b4af8f | 2009-05-16 11:45:48 +0000 | [diff] [blame] | 581 | if (T->isScalarType() && !TopLevelObject) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 582 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 583 | << IList->getSourceRange() |
| 584 | << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocStart())) |
| 585 | << CodeModificationHint::CreateRemoval(SourceRange(IList->getLocEnd())); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 588 | void InitListChecker::CheckListElementTypes(InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 590 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 591 | unsigned &Index, |
| 592 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 593 | unsigned &StructuredIndex, |
| 594 | bool TopLevelObject) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 595 | if (DeclType->isScalarType()) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 596 | CheckScalarType(IList, DeclType, Index, StructuredList, StructuredIndex); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 597 | } else if (DeclType->isVectorType()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 598 | CheckVectorType(IList, DeclType, Index, StructuredList, StructuredIndex); |
Douglas Gregor | ddb2485 | 2009-01-30 17:31:00 +0000 | [diff] [blame] | 599 | } else if (DeclType->isAggregateType()) { |
| 600 | if (DeclType->isRecordType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 601 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | CheckStructUnionTypes(IList, DeclType, RD->field_begin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 603 | SubobjectIsDesignatorContext, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 604 | StructuredList, StructuredIndex, |
| 605 | TopLevelObject); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 606 | } else if (DeclType->isArrayType()) { |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 607 | llvm::APSInt Zero( |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 608 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 609 | false); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 610 | CheckArrayType(IList, DeclType, Zero, SubobjectIsDesignatorContext, Index, |
| 611 | StructuredList, StructuredIndex); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 612 | } else |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 613 | assert(0 && "Aggregate that isn't a structure or array?!"); |
Steve Naroff | eaf5853 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 614 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 615 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 616 | ++Index; |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 617 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 618 | << DeclType; |
Eli Friedman | d0e48ea | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 619 | hadError = true; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 620 | } else if (DeclType->isRecordType()) { |
| 621 | // C++ [dcl.init]p14: |
| 622 | // [...] If the class is an aggregate (8.5.1), and the initializer |
| 623 | // is a brace-enclosed list, see 8.5.1. |
| 624 | // |
| 625 | // Note: 8.5.1 is handled below; here, we diagnose the case where |
| 626 | // we have an initializer list and a destination type that is not |
| 627 | // an aggregate. |
| 628 | // FIXME: In C++0x, this is yet another form of initialization. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 629 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 630 | << DeclType << IList->getSourceRange(); |
| 631 | hadError = true; |
| 632 | } else if (DeclType->isReferenceType()) { |
| 633 | CheckReferenceType(IList, DeclType, Index, StructuredList, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 634 | } else { |
| 635 | // In C, all types are either scalars or aggregates, but |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | // additional handling is needed here for C++ (and possibly others?). |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 637 | assert(0 && "Unsupported initializer type"); |
| 638 | } |
| 639 | } |
| 640 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 641 | void InitListChecker::CheckSubElementType(InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | QualType ElemType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 643 | unsigned &Index, |
| 644 | InitListExpr *StructuredList, |
| 645 | unsigned &StructuredIndex) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 646 | Expr *expr = IList->getInit(Index); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 647 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
| 648 | unsigned newIndex = 0; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 649 | unsigned newStructuredIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | InitListExpr *newStructuredList |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 651 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 652 | StructuredList, StructuredIndex, |
| 653 | SubInitList->getSourceRange()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | CheckExplicitInitList(SubInitList, ElemType, newIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 655 | newStructuredList, newStructuredIndex); |
| 656 | ++StructuredIndex; |
| 657 | ++Index; |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 658 | } else if (Expr *Str = IsStringInit(expr, ElemType, SemaRef.Context)) { |
| 659 | CheckStringInit(Str, ElemType, SemaRef); |
Chris Lattner | edbf3ba | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 660 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 661 | ++Index; |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 662 | } else if (ElemType->isScalarType()) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 663 | CheckScalarType(IList, ElemType, Index, StructuredList, StructuredIndex); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 664 | } else if (ElemType->isReferenceType()) { |
| 665 | CheckReferenceType(IList, ElemType, Index, StructuredList, StructuredIndex); |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 666 | } else { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 667 | if (SemaRef.getLangOptions().CPlusPlus) { |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 668 | // C++ [dcl.init.aggr]p12: |
| 669 | // All implicit type conversions (clause 4) are considered when |
| 670 | // initializing the aggregate member with an ini- tializer from |
| 671 | // an initializer-list. If the initializer can initialize a |
| 672 | // member, the member is initialized. [...] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | ImplicitConversionSequence ICS |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 674 | = SemaRef.TryCopyInitialization(expr, ElemType, |
| 675 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 20d1332 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 676 | /*ForceRValue=*/false, |
| 677 | /*InOverloadResolution=*/false); |
Anders Carlsson | 03068aa | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 679 | if (ICS.ConversionKind != ImplicitConversionSequence::BadConversion) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | if (SemaRef.PerformImplicitConversion(expr, ElemType, ICS, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 681 | "initializing")) |
| 682 | hadError = true; |
| 683 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 684 | ++Index; |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | // Fall through for subaggregate initialization |
| 689 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 690 | // C99 6.7.8p13: |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 691 | // |
| 692 | // The initializer for a structure or union object that has |
| 693 | // automatic storage duration shall be either an initializer |
| 694 | // list as described below, or a single expression that has |
| 695 | // compatible structure or union type. In the latter case, the |
| 696 | // initial value of the object, including unnamed members, is |
| 697 | // that of the expression. |
Eli Friedman | 9782caa | 2009-06-13 10:38:46 +0000 | [diff] [blame] | 698 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Eli Friedman | 893abe4 | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 699 | SemaRef.Context.hasSameUnqualifiedType(expr->getType(), ElemType)) { |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 700 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 701 | ++Index; |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | // Fall through for subaggregate initialization |
| 706 | } |
| 707 | |
| 708 | // C++ [dcl.init.aggr]p12: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | // |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 710 | // [...] Otherwise, if the member is itself a non-empty |
| 711 | // subaggregate, brace elision is assumed and the initializer is |
| 712 | // considered for the initialization of the first member of |
| 713 | // the subaggregate. |
| 714 | if (ElemType->isAggregateType() || ElemType->isVectorType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | CheckImplicitInitList(IList, ElemType, Index, StructuredList, |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 716 | StructuredIndex); |
| 717 | ++StructuredIndex; |
| 718 | } else { |
| 719 | // We cannot initialize this element, so let |
| 720 | // PerformCopyInitialization produce the appropriate diagnostic. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 721 | SemaRef.PerformCopyInitialization(expr, ElemType, "initializing"); |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 722 | hadError = true; |
| 723 | ++Index; |
| 724 | ++StructuredIndex; |
| 725 | } |
| 726 | } |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 729 | void InitListChecker::CheckScalarType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 730 | unsigned &Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 731 | InitListExpr *StructuredList, |
| 732 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 733 | if (Index < IList->getNumInits()) { |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 734 | Expr *expr = IList->getInit(Index); |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 735 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 736 | SemaRef.Diag(IList->getLocStart(), |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 737 | diag::err_many_braces_around_scalar_init) |
| 738 | << IList->getSourceRange(); |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 739 | hadError = true; |
| 740 | ++Index; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 741 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 742 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 743 | } else if (isa<DesignatedInitExpr>(expr)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 745 | diag::err_designator_for_scalar_init) |
| 746 | << DeclType << expr->getSourceRange(); |
| 747 | hadError = true; |
| 748 | ++Index; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 749 | ++StructuredIndex; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 750 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 751 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 752 | |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 753 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 754 | if (CheckSingleInitializer(expr, DeclType, false, SemaRef)) |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 755 | hadError = true; // types weren't compatible. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 756 | else if (savExpr != expr) { |
Eli Friedman | 5a36d3f | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 757 | // The type was promoted, update initializer list. |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 758 | IList->setInit(Index, expr); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 759 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 760 | if (hadError) |
| 761 | ++StructuredIndex; |
| 762 | else |
| 763 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 764 | ++Index; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 765 | } else { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 766 | SemaRef.Diag(IList->getLocStart(), diag::err_empty_scalar_initializer) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 767 | << IList->getSourceRange(); |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 768 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 769 | ++Index; |
| 770 | ++StructuredIndex; |
Eli Friedman | feb4cc1 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 771 | return; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 772 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 775 | void InitListChecker::CheckReferenceType(InitListExpr *IList, QualType DeclType, |
| 776 | unsigned &Index, |
| 777 | InitListExpr *StructuredList, |
| 778 | unsigned &StructuredIndex) { |
| 779 | if (Index < IList->getNumInits()) { |
| 780 | Expr *expr = IList->getInit(Index); |
| 781 | if (isa<InitListExpr>(expr)) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 782 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 783 | << DeclType << IList->getSourceRange(); |
| 784 | hadError = true; |
| 785 | ++Index; |
| 786 | ++StructuredIndex; |
| 787 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 788 | } |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 789 | |
| 790 | Expr *savExpr = expr; // Might be promoted by CheckSingleInitializer. |
Anders Carlsson | 271e3a4 | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 791 | if (SemaRef.CheckReferenceInit(expr, DeclType, |
| 792 | /*SuppressUserConversions=*/false, |
| 793 | /*AllowExplicit=*/false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | /*ForceRValue=*/false)) |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 795 | hadError = true; |
| 796 | else if (savExpr != expr) { |
| 797 | // The type was promoted, update initializer list. |
| 798 | IList->setInit(Index, expr); |
| 799 | } |
| 800 | if (hadError) |
| 801 | ++StructuredIndex; |
| 802 | else |
| 803 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 804 | ++Index; |
| 805 | } else { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 806 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 807 | // general, it would be useful to pass location information down the stack, |
| 808 | // so that we know the location (or decl) of the "current object" being |
| 809 | // initialized. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | SemaRef.Diag(IList->getLocStart(), |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 811 | diag::err_init_reference_member_uninitialized) |
| 812 | << DeclType |
| 813 | << IList->getSourceRange(); |
| 814 | hadError = true; |
| 815 | ++Index; |
| 816 | ++StructuredIndex; |
| 817 | return; |
| 818 | } |
| 819 | } |
| 820 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 821 | void InitListChecker::CheckVectorType(InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 822 | unsigned &Index, |
| 823 | InitListExpr *StructuredList, |
| 824 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 825 | if (Index < IList->getNumInits()) { |
| 826 | const VectorType *VT = DeclType->getAsVectorType(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 827 | unsigned maxElements = VT->getNumElements(); |
| 828 | unsigned numEltsInit = 0; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 829 | QualType elementType = VT->getElementType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 830 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 831 | if (!SemaRef.getLangOptions().OpenCL) { |
| 832 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 833 | // Don't attempt to go past the end of the init list |
| 834 | if (Index >= IList->getNumInits()) |
| 835 | break; |
| 836 | CheckSubElementType(IList, elementType, Index, |
| 837 | StructuredList, StructuredIndex); |
| 838 | } |
| 839 | } else { |
| 840 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 841 | for (unsigned i = 0; i < maxElements; ++i) { |
| 842 | // Don't attempt to go past the end of the init list |
| 843 | if (Index >= IList->getNumInits()) |
| 844 | break; |
| 845 | QualType IType = IList->getInit(Index)->getType(); |
| 846 | if (!IType->isVectorType()) { |
| 847 | CheckSubElementType(IList, elementType, Index, |
| 848 | StructuredList, StructuredIndex); |
| 849 | ++numEltsInit; |
| 850 | } else { |
| 851 | const VectorType *IVT = IType->getAsVectorType(); |
| 852 | unsigned numIElts = IVT->getNumElements(); |
| 853 | QualType VecType = SemaRef.Context.getExtVectorType(elementType, |
| 854 | numIElts); |
| 855 | CheckSubElementType(IList, VecType, Index, |
| 856 | StructuredList, StructuredIndex); |
| 857 | numEltsInit += numIElts; |
| 858 | } |
| 859 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 860 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 862 | // OpenCL & AltiVec require all elements to be initialized. |
| 863 | if (numEltsInit != maxElements) |
| 864 | if (SemaRef.getLangOptions().OpenCL || SemaRef.getLangOptions().AltiVec) |
| 865 | SemaRef.Diag(IList->getSourceRange().getBegin(), |
| 866 | diag::err_vector_incorrect_num_initializers) |
| 867 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 871 | void InitListChecker::CheckArrayType(InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 872 | llvm::APSInt elementIndex, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 874 | unsigned &Index, |
| 875 | InitListExpr *StructuredList, |
| 876 | unsigned &StructuredIndex) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 877 | // Check for the special-case of initializing an array with a string. |
| 878 | if (Index < IList->getNumInits()) { |
Chris Lattner | d8b741c8 | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 879 | if (Expr *Str = IsStringInit(IList->getInit(Index), DeclType, |
| 880 | SemaRef.Context)) { |
| 881 | CheckStringInit(Str, DeclType, SemaRef); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 882 | // We place the string literal directly into the resulting |
| 883 | // initializer list. This is the only place where the structure |
| 884 | // of the structured initializer list doesn't match exactly, |
| 885 | // because doing so would involve allocating one character |
| 886 | // constant for each string. |
Chris Lattner | edbf3ba | 2009-02-24 22:41:04 +0000 | [diff] [blame] | 887 | UpdateStructuredListElement(StructuredList, StructuredIndex, Str); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 888 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 889 | ++Index; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 890 | return; |
| 891 | } |
| 892 | } |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 893 | if (const VariableArrayType *VAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 894 | SemaRef.Context.getAsVariableArrayType(DeclType)) { |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 895 | // Check for VLAs; in standard C it would be possible to check this |
| 896 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 897 | // them in all sorts of strange places). |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 898 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 899 | diag::err_variable_object_no_init) |
| 900 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 901 | hadError = true; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 902 | ++Index; |
| 903 | ++StructuredIndex; |
Eli Friedman | 85f5497 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 904 | return; |
| 905 | } |
| 906 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 907 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 908 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 909 | elementIndex.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 910 | bool maxElementsKnown = false; |
| 911 | if (const ConstantArrayType *CAT = |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 912 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 913 | maxElements = CAT->getSize(); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 914 | elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 915 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 916 | maxElementsKnown = true; |
| 917 | } |
| 918 | |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 919 | QualType elementType = SemaRef.Context.getAsArrayType(DeclType) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 920 | ->getElementType(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 921 | while (Index < IList->getNumInits()) { |
| 922 | Expr *Init = IList->getInit(Index); |
| 923 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 924 | // If we're not the subobject that matches up with the '{' for |
| 925 | // the designator, we shouldn't be handling the |
| 926 | // designator. Return immediately. |
| 927 | if (!SubobjectIsDesignatorContext) |
| 928 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 929 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 930 | // Handle this designated initializer. elementIndex will be |
| 931 | // updated to be the next array element we'll initialize. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 932 | if (CheckDesignatedInitializer(IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 933 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 934 | StructuredList, StructuredIndex, true, |
| 935 | false)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 936 | hadError = true; |
| 937 | continue; |
| 938 | } |
| 939 | |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 940 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
| 941 | maxElements.extend(elementIndex.getBitWidth()); |
| 942 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
| 943 | elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 944 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 033d125 | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 945 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 946 | // If the array is of incomplete type, keep track of the number of |
| 947 | // elements in the initializer. |
| 948 | if (!maxElementsKnown && elementIndex > maxElements) |
| 949 | maxElements = elementIndex; |
| 950 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 951 | continue; |
| 952 | } |
| 953 | |
| 954 | // If we know the maximum number of elements, and we've already |
| 955 | // hit it, stop consuming elements in the initializer list. |
| 956 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 957 | break; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 958 | |
| 959 | // Check this element. |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 960 | CheckSubElementType(IList, elementType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 961 | StructuredList, StructuredIndex); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 962 | ++elementIndex; |
| 963 | |
| 964 | // If the array is of incomplete type, keep track of the number of |
| 965 | // elements in the initializer. |
| 966 | if (!maxElementsKnown && elementIndex > maxElements) |
| 967 | maxElements = elementIndex; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 968 | } |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 969 | if (!hadError && DeclType->isIncompleteArrayType()) { |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 970 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 971 | // be calculated here. |
Douglas Gregor | 583cf0a | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 972 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 973 | if (maxElements == Zero) { |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 974 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 975 | // but is supported by GNU. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 976 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 977 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 978 | } |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 979 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 980 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | aa64b7e | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 981 | ArrayType::Normal, 0); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | void InitListChecker::CheckStructUnionTypes(InitListExpr *IList, |
| 986 | QualType DeclType, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 987 | RecordDecl::field_iterator Field, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 989 | unsigned &Index, |
| 990 | InitListExpr *StructuredList, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 991 | unsigned &StructuredIndex, |
| 992 | bool TopLevelObject) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 993 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 995 | // If the record is invalid, some of it's members are invalid. To avoid |
| 996 | // confusion, we forgo checking the intializer for the entire record. |
| 997 | if (structDecl->isInvalidDecl()) { |
| 998 | hadError = true; |
| 999 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1000 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1001 | |
| 1002 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
| 1003 | // Value-initialize the first named member of the union. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1004 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1005 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1006 | Field != FieldEnd; ++Field) { |
| 1007 | if (Field->getDeclName()) { |
| 1008 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1009 | break; |
| 1010 | } |
| 1011 | } |
| 1012 | return; |
| 1013 | } |
| 1014 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1015 | // If structDecl is a forward declaration, this loop won't do |
| 1016 | // anything except look at designated initializers; That's okay, |
| 1017 | // because an error should get printed out elsewhere. It might be |
| 1018 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1019 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1020 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1021 | bool InitializedSomething = false; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1022 | while (Index < IList->getNumInits()) { |
| 1023 | Expr *Init = IList->getInit(Index); |
| 1024 | |
| 1025 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1026 | // If we're not the subobject that matches up with the '{' for |
| 1027 | // the designator, we shouldn't be handling the |
| 1028 | // designator. Return immediately. |
| 1029 | if (!SubobjectIsDesignatorContext) |
| 1030 | return; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1031 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1032 | // Handle this designated initializer. Field will be updated to |
| 1033 | // the next field that we'll be initializing. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 | if (CheckDesignatedInitializer(IList, DIE, 0, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1035 | DeclType, &Field, 0, Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1036 | StructuredList, StructuredIndex, |
| 1037 | true, TopLevelObject)) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1038 | hadError = true; |
| 1039 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1040 | InitializedSomething = true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1041 | continue; |
| 1042 | } |
| 1043 | |
| 1044 | if (Field == FieldEnd) { |
| 1045 | // We've run out of fields. We're done. |
| 1046 | break; |
| 1047 | } |
| 1048 | |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1049 | // We've already initialized a member of a union. We're done. |
| 1050 | if (InitializedSomething && DeclType->isUnionType()) |
| 1051 | break; |
| 1052 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1053 | // If we've hit the flexible array member at the end, we're done. |
| 1054 | if (Field->getType()->isIncompleteArrayType()) |
| 1055 | break; |
| 1056 | |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1057 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1058 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1059 | ++Field; |
Eli Friedman | 23a9e31 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1060 | continue; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1061 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1062 | |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1063 | CheckSubElementType(IList, Field->getType(), Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1064 | StructuredList, StructuredIndex); |
Douglas Gregor | a9add4e | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1065 | InitializedSomething = true; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1066 | |
| 1067 | if (DeclType->isUnionType()) { |
| 1068 | // Initialize the first field within the union. |
| 1069 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1070 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1071 | |
| 1072 | ++Field; |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1073 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1074 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1076 | Index >= IList->getNumInits()) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1077 | return; |
| 1078 | |
| 1079 | // Handle GNU flexible array initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | if (!TopLevelObject && |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1081 | (!isa<InitListExpr>(IList->getInit(Index)) || |
| 1082 | cast<InitListExpr>(IList->getInit(Index))->getNumInits() > 0)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1084 | diag::err_flexible_array_init_nonempty) |
| 1085 | << IList->getInit(Index)->getSourceRange().getBegin(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1086 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1087 | << *Field; |
| 1088 | hadError = true; |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1089 | ++Index; |
| 1090 | return; |
| 1091 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | SemaRef.Diag(IList->getInit(Index)->getSourceRange().getBegin(), |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1093 | diag::ext_flexible_array_init) |
| 1094 | << IList->getInit(Index)->getSourceRange().getBegin(); |
| 1095 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1096 | << *Field; |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Douglas Gregor | 07d8e3a | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1099 | if (isa<InitListExpr>(IList->getInit(Index))) |
| 1100 | CheckSubElementType(IList, Field->getType(), Index, StructuredList, |
| 1101 | StructuredIndex); |
| 1102 | else |
| 1103 | CheckImplicitInitList(IList, Field->getType(), Index, StructuredList, |
| 1104 | StructuredIndex); |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1105 | } |
Steve Naroff | f8ecff2 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1106 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1107 | /// \brief Expand a field designator that refers to a member of an |
| 1108 | /// anonymous struct or union into a series of field designators that |
| 1109 | /// refers to the field within the appropriate subobject. |
| 1110 | /// |
| 1111 | /// Field/FieldIndex will be updated to point to the (new) |
| 1112 | /// currently-designated field. |
| 1113 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | DesignatedInitExpr *DIE, |
| 1115 | unsigned DesigIdx, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1116 | FieldDecl *Field, |
| 1117 | RecordDecl::field_iterator &FieldIter, |
| 1118 | unsigned &FieldIndex) { |
| 1119 | typedef DesignatedInitExpr::Designator Designator; |
| 1120 | |
| 1121 | // Build the path from the current object to the member of the |
| 1122 | // anonymous struct/union (backwards). |
| 1123 | llvm::SmallVector<FieldDecl *, 4> Path; |
| 1124 | SemaRef.BuildAnonymousStructUnionMemberPath(Field, Path); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1126 | // Build the replacement designators. |
| 1127 | llvm::SmallVector<Designator, 4> Replacements; |
| 1128 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 1129 | FI = Path.rbegin(), FIEnd = Path.rend(); |
| 1130 | FI != FIEnd; ++FI) { |
| 1131 | if (FI + 1 == FIEnd) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1133 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1134 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1135 | else |
| 1136 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1137 | SourceLocation())); |
| 1138 | Replacements.back().setField(*FI); |
| 1139 | } |
| 1140 | |
| 1141 | // Expand the current designator into the set of replacement |
| 1142 | // designators, so we have a full subobject path down to where the |
| 1143 | // member of the anonymous struct/union is actually stored. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1144 | DIE->ExpandDesignator(DesigIdx, &Replacements[0], |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1145 | &Replacements[0] + Replacements.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1146 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1147 | // Update FieldIter/FieldIndex; |
| 1148 | RecordDecl *Record = cast<RecordDecl>(Path.back()->getDeclContext()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1149 | FieldIter = Record->field_begin(); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1150 | FieldIndex = 0; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1151 | for (RecordDecl::field_iterator FEnd = Record->field_end(); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1152 | FieldIter != FEnd; ++FieldIter) { |
| 1153 | if (FieldIter->isUnnamedBitfield()) |
| 1154 | continue; |
| 1155 | |
| 1156 | if (*FieldIter == Path.back()) |
| 1157 | return; |
| 1158 | |
| 1159 | ++FieldIndex; |
| 1160 | } |
| 1161 | |
| 1162 | assert(false && "Unable to find anonymous struct/union field"); |
| 1163 | } |
| 1164 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1165 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1166 | /// |
| 1167 | /// Determines whether the designated initializer @p DIE, which |
| 1168 | /// resides at the given @p Index within the initializer list @p |
| 1169 | /// IList, is well-formed for a current object of type @p DeclType |
| 1170 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1171 | /// within the current subobject is returned in either |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1172 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1173 | /// |
| 1174 | /// @param IList The initializer list in which this designated |
| 1175 | /// initializer occurs. |
| 1176 | /// |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1177 | /// @param DIE The designated initializer expression. |
| 1178 | /// |
| 1179 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1180 | /// |
| 1181 | /// @param DeclType The type of the "current object" (C99 6.7.8p17), |
| 1182 | /// into which the designation in @p DIE should refer. |
| 1183 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1184 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1185 | /// a field, this will be set to the field declaration corresponding |
| 1186 | /// to the field named by the designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1187 | /// |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1188 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1189 | /// DIE is an array designator or GNU array-range designator, this |
| 1190 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1191 | /// |
| 1192 | /// @param Index Index into @p IList where the designated initializer |
| 1193 | /// @p DIE occurs. |
| 1194 | /// |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1195 | /// @param StructuredList The initializer list expression that |
| 1196 | /// describes all of the subobject initializers in the order they'll |
| 1197 | /// actually be initialized. |
| 1198 | /// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1199 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | bool |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1201 | InitListChecker::CheckDesignatedInitializer(InitListExpr *IList, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | DesignatedInitExpr *DIE, |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1203 | unsigned DesigIdx, |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1204 | QualType &CurrentObjectType, |
| 1205 | RecordDecl::field_iterator *NextField, |
| 1206 | llvm::APSInt *NextElementIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1207 | unsigned &Index, |
| 1208 | InitListExpr *StructuredList, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1209 | unsigned &StructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1210 | bool FinishSubobjectInit, |
| 1211 | bool TopLevelObject) { |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1212 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1213 | // Check the actual initialization for the designated object type. |
| 1214 | bool prevHadError = hadError; |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1215 | |
| 1216 | // Temporarily remove the designator expression from the |
| 1217 | // initializer list that the child calls see, so that we don't try |
| 1218 | // to re-process the designator. |
| 1219 | unsigned OldIndex = Index; |
| 1220 | IList->setInit(OldIndex, DIE->getInit()); |
| 1221 | |
| 1222 | CheckSubElementType(IList, CurrentObjectType, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1223 | StructuredList, StructuredIndex); |
Douglas Gregor | f6d2752 | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1224 | |
| 1225 | // Restore the designated initializer expression in the syntactic |
| 1226 | // form of the initializer list. |
| 1227 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1228 | DIE->setInit(IList->getInit(OldIndex)); |
| 1229 | IList->setInit(OldIndex, DIE); |
| 1230 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1231 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1234 | bool IsFirstDesignator = (DesigIdx == 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1235 | assert((IsFirstDesignator || StructuredList) && |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1236 | "Need a non-designated initializer list to start from"); |
| 1237 | |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1238 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1239 | // Determine the structural initializer list that corresponds to the |
| 1240 | // current subobject. |
| 1241 | StructuredList = IsFirstDesignator? SyntacticToSemantic[IList] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1243 | StructuredList, StructuredIndex, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1244 | SourceRange(D->getStartLocation(), |
| 1245 | DIE->getSourceRange().getEnd())); |
| 1246 | assert(StructuredList && "Expected a structured initializer list"); |
| 1247 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1248 | if (D->isFieldDesignator()) { |
| 1249 | // C99 6.7.8p7: |
| 1250 | // |
| 1251 | // If a designator has the form |
| 1252 | // |
| 1253 | // . identifier |
| 1254 | // |
| 1255 | // then the current object (defined below) shall have |
| 1256 | // structure or union type and the identifier shall be the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | // name of a member of that type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1258 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1259 | if (!RT) { |
| 1260 | SourceLocation Loc = D->getDotLoc(); |
| 1261 | if (Loc.isInvalid()) |
| 1262 | Loc = D->getFieldLoc(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1263 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
| 1264 | << SemaRef.getLangOptions().CPlusPlus << CurrentObjectType; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1265 | ++Index; |
| 1266 | return true; |
| 1267 | } |
| 1268 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1269 | // Note: we perform a linear search of the fields here, despite |
| 1270 | // the fact that we have a faster lookup method, because we always |
| 1271 | // need to compute the field's index. |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1272 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1273 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1274 | unsigned FieldIndex = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1276 | Field = RT->getDecl()->field_begin(), |
| 1277 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1278 | for (; Field != FieldEnd; ++Field) { |
| 1279 | if (Field->isUnnamedBitfield()) |
| 1280 | continue; |
| 1281 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1282 | if (KnownField == *Field || Field->getIdentifier() == FieldName) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1283 | break; |
| 1284 | |
| 1285 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1288 | if (Field == FieldEnd) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1289 | // There was no normal field in the struct with the designated |
| 1290 | // name. Perform another lookup for this name, which may find |
| 1291 | // something that we can't designate (e.g., a member function), |
| 1292 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | // struct/union. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1294 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1295 | if (Lookup.first == Lookup.second) { |
| 1296 | // Name lookup didn't find anything. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1297 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1298 | << FieldName << CurrentObjectType; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1299 | ++Index; |
| 1300 | return true; |
| 1301 | } else if (!KnownField && isa<FieldDecl>(*Lookup.first) && |
| 1302 | cast<RecordDecl>((*Lookup.first)->getDeclContext()) |
| 1303 | ->isAnonymousStructOrUnion()) { |
| 1304 | // Handle an field designator that refers to a member of an |
| 1305 | // anonymous struct or union. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1307 | cast<FieldDecl>(*Lookup.first), |
| 1308 | Field, FieldIndex); |
Eli Friedman | 8d25b09 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1309 | D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1310 | } else { |
| 1311 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1312 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1313 | << FieldName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | SemaRef.Diag((*Lookup.first)->getLocation(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1315 | diag::note_field_designator_found); |
Eli Friedman | 8d25b09 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1316 | ++Index; |
| 1317 | return true; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1318 | } |
| 1319 | } else if (!KnownField && |
| 1320 | cast<RecordDecl>((*Field)->getDeclContext()) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1321 | ->isAnonymousStructOrUnion()) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1322 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, *Field, |
| 1323 | Field, FieldIndex); |
| 1324 | D = DIE->getDesignator(DesigIdx); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1325 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1326 | |
| 1327 | // All of the fields of a union are located at the same place in |
| 1328 | // the initializer list. |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1329 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1330 | FieldIndex = 0; |
Douglas Gregor | 5169570 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1331 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1332 | } |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1333 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1334 | // Update the designator with the field declaration. |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1335 | D->setField(*Field); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1337 | // Make sure that our non-designated initializer list has space |
| 1338 | // for a subobject corresponding to this field. |
| 1339 | if (FieldIndex >= StructuredList->getNumInits()) |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1340 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1341 | |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1342 | // This designator names a flexible array member. |
| 1343 | if (Field->getType()->isIncompleteArrayType()) { |
| 1344 | bool Invalid = false; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1345 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1346 | // We can't designate an object within the flexible array |
| 1347 | // member (because GCC doesn't allow it). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1348 | DesignatedInitExpr::Designator *NextD |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1349 | = DIE->getDesignator(DesigIdx + 1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | SemaRef.Diag(NextD->getStartLocation(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1351 | diag::err_designator_into_flexible_array_member) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | << SourceRange(NextD->getStartLocation(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1353 | DIE->getSourceRange().getEnd()); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1354 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1355 | << *Field; |
| 1356 | Invalid = true; |
| 1357 | } |
| 1358 | |
| 1359 | if (!hadError && !isa<InitListExpr>(DIE->getInit())) { |
| 1360 | // The initializer is not an initializer list. |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1361 | SemaRef.Diag(DIE->getInit()->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1362 | diag::err_flexible_array_init_needs_braces) |
| 1363 | << DIE->getInit()->getSourceRange(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1364 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1365 | << *Field; |
| 1366 | Invalid = true; |
| 1367 | } |
| 1368 | |
| 1369 | // Handle GNU flexible array initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | if (!Invalid && !TopLevelObject && |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1371 | cast<InitListExpr>(DIE->getInit())->getNumInits() > 0) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | SemaRef.Diag(DIE->getSourceRange().getBegin(), |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1373 | diag::err_flexible_array_init_nonempty) |
| 1374 | << DIE->getSourceRange().getBegin(); |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1375 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1376 | << *Field; |
| 1377 | Invalid = true; |
| 1378 | } |
| 1379 | |
| 1380 | if (Invalid) { |
| 1381 | ++Index; |
| 1382 | return true; |
| 1383 | } |
| 1384 | |
| 1385 | // Initialize the array. |
| 1386 | bool prevHadError = hadError; |
| 1387 | unsigned newStructuredIndex = FieldIndex; |
| 1388 | unsigned OldIndex = Index; |
| 1389 | IList->setInit(Index, DIE->getInit()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1390 | CheckSubElementType(IList, Field->getType(), Index, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1391 | StructuredList, newStructuredIndex); |
| 1392 | IList->setInit(OldIndex, DIE); |
| 1393 | if (hadError && !prevHadError) { |
| 1394 | ++Field; |
| 1395 | ++FieldIndex; |
| 1396 | if (NextField) |
| 1397 | *NextField = Field; |
| 1398 | StructuredIndex = FieldIndex; |
| 1399 | return true; |
| 1400 | } |
| 1401 | } else { |
| 1402 | // Recurse to check later designated subobjects. |
| 1403 | QualType FieldType = (*Field)->getType(); |
| 1404 | unsigned newStructuredIndex = FieldIndex; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1405 | if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, FieldType, 0, 0, |
| 1406 | Index, StructuredList, newStructuredIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1407 | true, false)) |
| 1408 | return true; |
| 1409 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1410 | |
| 1411 | // Find the position of the next field to be initialized in this |
| 1412 | // subobject. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1413 | ++Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1414 | ++FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1415 | |
| 1416 | // If this the first designator, our caller will continue checking |
| 1417 | // the rest of this struct/class/union subobject. |
| 1418 | if (IsFirstDesignator) { |
| 1419 | if (NextField) |
| 1420 | *NextField = Field; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1421 | StructuredIndex = FieldIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1422 | return false; |
| 1423 | } |
| 1424 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1425 | if (!FinishSubobjectInit) |
| 1426 | return false; |
| 1427 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1428 | // We've already initialized something in the union; we're done. |
| 1429 | if (RT->getDecl()->isUnion()) |
| 1430 | return hadError; |
| 1431 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1432 | // Check the remaining fields within this class/struct/union subobject. |
| 1433 | bool prevHadError = hadError; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1434 | CheckStructUnionTypes(IList, CurrentObjectType, Field, false, Index, |
| 1435 | StructuredList, FieldIndex); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1436 | return hadError && !prevHadError; |
| 1437 | } |
| 1438 | |
| 1439 | // C99 6.7.8p6: |
| 1440 | // |
| 1441 | // If a designator has the form |
| 1442 | // |
| 1443 | // [ constant-expression ] |
| 1444 | // |
| 1445 | // then the current object (defined below) shall have array |
| 1446 | // type and the expression shall be an integer constant |
| 1447 | // expression. If the array is of unknown size, any |
| 1448 | // nonnegative value is valid. |
| 1449 | // |
| 1450 | // Additionally, cope with the GNU extension that permits |
| 1451 | // designators of the form |
| 1452 | // |
| 1453 | // [ constant-expression ... constant-expression ] |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1454 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1455 | if (!AT) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1456 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1457 | << CurrentObjectType; |
| 1458 | ++Index; |
| 1459 | return true; |
| 1460 | } |
| 1461 | |
| 1462 | Expr *IndexExpr = 0; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1463 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 1464 | if (D->isArrayDesignator()) { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1465 | IndexExpr = DIE->getArrayIndex(*D); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1466 | DesignatedStartIndex = IndexExpr->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1467 | DesignatedEndIndex = DesignatedStartIndex; |
| 1468 | } else { |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1469 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1470 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | |
| 1472 | DesignatedStartIndex = |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1473 | DIE->getArrayRangeStart(*D)->EvaluateAsInt(SemaRef.Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | DesignatedEndIndex = |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1475 | DIE->getArrayRangeEnd(*D)->EvaluateAsInt(SemaRef.Context); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1476 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1477 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1478 | if (DesignatedStartIndex.getZExtValue() !=DesignatedEndIndex.getZExtValue()) |
Douglas Gregor | bf7207a | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 1479 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1482 | if (isa<ConstantArrayType>(AT)) { |
| 1483 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1484 | DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 1485 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1486 | DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
| 1487 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 1488 | if (DesignatedEndIndex >= MaxElements) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1489 | SemaRef.Diag(IndexExpr->getSourceRange().getBegin(), |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1490 | diag::err_array_designator_too_large) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1491 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1492 | << IndexExpr->getSourceRange(); |
| 1493 | ++Index; |
| 1494 | return true; |
| 1495 | } |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1496 | } else { |
| 1497 | // Make sure the bit-widths and signedness match. |
| 1498 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
| 1499 | DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1500 | else if (DesignatedStartIndex.getBitWidth() < |
| 1501 | DesignatedEndIndex.getBitWidth()) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1502 | DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
| 1503 | DesignatedStartIndex.setIsUnsigned(true); |
| 1504 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1505 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1507 | // Make sure that our non-designated initializer list has space |
| 1508 | // for a subobject corresponding to this array element. |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1509 | if (DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1510 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1511 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1512 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1513 | // Repeatedly perform subobject initializations in the range |
| 1514 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1515 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1516 | // Move to the next designator |
| 1517 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1518 | unsigned OldIndex = Index; |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1519 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 1520 | // Recurse to check later designated subobjects. |
| 1521 | QualType ElementType = AT->getElementType(); |
| 1522 | Index = OldIndex; |
Douglas Gregor | a532416 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1523 | if (CheckDesignatedInitializer(IList, DIE, DesigIdx + 1, ElementType, 0, 0, |
| 1524 | Index, StructuredList, ElementIndex, |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1525 | (DesignatedStartIndex == DesignatedEndIndex), |
| 1526 | false)) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1527 | return true; |
| 1528 | |
| 1529 | // Move to the next index in the array that we'll be initializing. |
| 1530 | ++DesignatedStartIndex; |
| 1531 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 1532 | } |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1533 | |
| 1534 | // If this the first designator, our caller will continue checking |
| 1535 | // the rest of this array subobject. |
| 1536 | if (IsFirstDesignator) { |
| 1537 | if (NextElementIndex) |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1538 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1539 | StructuredIndex = ElementIndex; |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1540 | return false; |
| 1541 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | |
Douglas Gregor | 17bd094 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1543 | if (!FinishSubobjectInit) |
| 1544 | return false; |
| 1545 | |
Douglas Gregor | d7fb85e | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1546 | // Check the remaining elements within this array subobject. |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1547 | bool prevHadError = hadError; |
Douglas Gregor | aef040a | 2009-02-09 19:45:19 +0000 | [diff] [blame] | 1548 | CheckArrayType(IList, CurrentObjectType, DesignatedStartIndex, false, Index, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1549 | StructuredList, ElementIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | return hadError && !prevHadError; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1553 | // Get the structured initializer list for a subobject of type |
| 1554 | // @p CurrentObjectType. |
| 1555 | InitListExpr * |
| 1556 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 1557 | QualType CurrentObjectType, |
| 1558 | InitListExpr *StructuredList, |
| 1559 | unsigned StructuredIndex, |
| 1560 | SourceRange InitRange) { |
| 1561 | Expr *ExistingInit = 0; |
| 1562 | if (!StructuredList) |
| 1563 | ExistingInit = SyntacticToSemantic[IList]; |
| 1564 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 1565 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1567 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 1568 | return Result; |
| 1569 | |
| 1570 | if (ExistingInit) { |
| 1571 | // We are creating an initializer list that initializes the |
| 1572 | // subobjects of the current object, but there was already an |
| 1573 | // initialization that completely initialized the current |
| 1574 | // subobject, e.g., by a compound literal: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1576 | // struct X { int a, b; }; |
| 1577 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1578 | // |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1579 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 1580 | // designated initializer re-initializes the whole |
| 1581 | // subobject [0], overwriting previous initializers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1583 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1584 | << InitRange; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | SemaRef.Diag(ExistingInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1586 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1587 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1588 | << ExistingInit->getSourceRange(); |
| 1589 | } |
| 1590 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | InitListExpr *Result |
| 1592 | = new (SemaRef.Context) InitListExpr(InitRange.getBegin(), 0, 0, |
Douglas Gregor | 5741efb | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 1593 | InitRange.getEnd()); |
| 1594 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1595 | Result->setType(CurrentObjectType); |
| 1596 | |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1597 | // Pre-allocate storage for the structured initializer list. |
| 1598 | unsigned NumElements = 0; |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1599 | unsigned NumInits = 0; |
| 1600 | if (!StructuredList) |
| 1601 | NumInits = IList->getNumInits(); |
| 1602 | else if (Index < IList->getNumInits()) { |
| 1603 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) |
| 1604 | NumInits = SubList->getNumInits(); |
| 1605 | } |
| 1606 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | if (const ArrayType *AType |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1608 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 1609 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 1610 | NumElements = CAType->getSize().getZExtValue(); |
| 1611 | // Simple heuristic so that we don't allocate a very large |
| 1612 | // initializer with many empty entries at the end. |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1613 | if (NumInits && NumElements > NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1614 | NumElements = 0; |
| 1615 | } |
| 1616 | } else if (const VectorType *VType = CurrentObjectType->getAsVectorType()) |
| 1617 | NumElements = VType->getNumElements(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1618 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1619 | RecordDecl *RDecl = RType->getDecl(); |
| 1620 | if (RDecl->isUnion()) |
| 1621 | NumElements = 1; |
| 1622 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1624 | RDecl->field_end()); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Douglas Gregor | 221c9a5 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 1627 | if (NumElements < NumInits) |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1628 | NumElements = IList->getNumInits(); |
| 1629 | |
| 1630 | Result->reserveInits(NumElements); |
| 1631 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1632 | // Link this new initializer list into the structured initializer |
| 1633 | // lists. |
| 1634 | if (StructuredList) |
| 1635 | StructuredList->updateInit(StructuredIndex, Result); |
| 1636 | else { |
| 1637 | Result->setSyntacticForm(IList); |
| 1638 | SyntacticToSemantic[IList] = Result; |
| 1639 | } |
| 1640 | |
| 1641 | return Result; |
| 1642 | } |
| 1643 | |
| 1644 | /// Update the initializer at index @p StructuredIndex within the |
| 1645 | /// structured initializer list to the value @p expr. |
| 1646 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 1647 | unsigned &StructuredIndex, |
| 1648 | Expr *expr) { |
| 1649 | // No structured initializer list to update |
| 1650 | if (!StructuredList) |
| 1651 | return; |
| 1652 | |
| 1653 | if (Expr *PrevInit = StructuredList->updateInit(StructuredIndex, expr)) { |
| 1654 | // This initializer overwrites a previous initializer. Warn. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | SemaRef.Diag(expr->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1656 | diag::warn_initializer_overrides) |
| 1657 | << expr->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | SemaRef.Diag(PrevInit->getSourceRange().getBegin(), |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1659 | diag::note_previous_initializer) |
Douglas Gregor | e6af7a0 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 1660 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1661 | << PrevInit->getSourceRange(); |
| 1662 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1664 | ++StructuredIndex; |
| 1665 | } |
| 1666 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1667 | /// Check that the given Index expression is a valid array designator |
| 1668 | /// value. This is essentailly just a wrapper around |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1669 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1670 | /// and produces a reasonable diagnostic if there is a |
| 1671 | /// failure. Returns true if there was an error, false otherwise. If |
| 1672 | /// everything went okay, Value will receive the value of the constant |
| 1673 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | static bool |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1675 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1676 | SourceLocation Loc = Index->getSourceRange().getBegin(); |
| 1677 | |
| 1678 | // Make sure this is an integer constant expression. |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1679 | if (S.VerifyIntegerConstantExpression(Index, &Value)) |
| 1680 | return true; |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1681 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 1682 | if (Value.isSigned() && Value.isNegative()) |
| 1683 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1684 | << Value.toString(10) << Index->getSourceRange(); |
| 1685 | |
Douglas Gregor | 51650d3 | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 1686 | Value.setIsUnsigned(true); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1687 | return false; |
| 1688 | } |
| 1689 | |
| 1690 | Sema::OwningExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
| 1691 | SourceLocation Loc, |
Douglas Gregor | 5c7c9cb | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 1692 | bool GNUSyntax, |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1693 | OwningExprResult Init) { |
| 1694 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 1695 | |
| 1696 | bool Invalid = false; |
| 1697 | llvm::SmallVector<ASTDesignator, 32> Designators; |
| 1698 | llvm::SmallVector<Expr *, 32> InitExpressions; |
| 1699 | |
| 1700 | // Build designators and check array designator expressions. |
| 1701 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 1702 | const Designator &D = Desig.getDesignator(Idx); |
| 1703 | switch (D.getKind()) { |
| 1704 | case Designator::FieldDesignator: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1706 | D.getFieldLoc())); |
| 1707 | break; |
| 1708 | |
| 1709 | case Designator::ArrayDesignator: { |
| 1710 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 1711 | llvm::APSInt IndexValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1712 | if (!Index->isTypeDependent() && |
| 1713 | !Index->isValueDependent() && |
| 1714 | CheckArrayDesignatorExpr(*this, Index, IndexValue)) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1715 | Invalid = true; |
| 1716 | else { |
| 1717 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | D.getLBracketLoc(), |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1719 | D.getRBracketLoc())); |
| 1720 | InitExpressions.push_back(Index); |
| 1721 | } |
| 1722 | break; |
| 1723 | } |
| 1724 | |
| 1725 | case Designator::ArrayRangeDesignator: { |
| 1726 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 1727 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 1728 | llvm::APSInt StartValue; |
| 1729 | llvm::APSInt EndValue; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1730 | bool StartDependent = StartIndex->isTypeDependent() || |
| 1731 | StartIndex->isValueDependent(); |
| 1732 | bool EndDependent = EndIndex->isTypeDependent() || |
| 1733 | EndIndex->isValueDependent(); |
| 1734 | if ((!StartDependent && |
| 1735 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue)) || |
| 1736 | (!EndDependent && |
| 1737 | CheckArrayDesignatorExpr(*this, EndIndex, EndValue))) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1738 | Invalid = true; |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1739 | else { |
| 1740 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 1741 | if (StartDependent || EndDependent) { |
| 1742 | // Nothing to compute. |
| 1743 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1744 | EndValue.extend(StartValue.getBitWidth()); |
| 1745 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
| 1746 | StartValue.extend(EndValue.getBitWidth()); |
| 1747 | |
Douglas Gregor | 0f9d400 | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 1748 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1749 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1751 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 1752 | Invalid = true; |
| 1753 | } else { |
| 1754 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | D.getLBracketLoc(), |
Douglas Gregor | 7a95b08 | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 1756 | D.getEllipsisLoc(), |
| 1757 | D.getRBracketLoc())); |
| 1758 | InitExpressions.push_back(StartIndex); |
| 1759 | InitExpressions.push_back(EndIndex); |
| 1760 | } |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1761 | } |
| 1762 | break; |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | if (Invalid || Init.isInvalid()) |
| 1768 | return ExprError(); |
| 1769 | |
| 1770 | // Clear out the expressions within the designation. |
| 1771 | Desig.ClearExprs(*this); |
| 1772 | |
| 1773 | DesignatedInitExpr *DIE |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1774 | = DesignatedInitExpr::Create(Context, |
| 1775 | Designators.data(), Designators.size(), |
| 1776 | InitExpressions.data(), InitExpressions.size(), |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1777 | Loc, GNUSyntax, Init.takeAs<Expr>()); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1778 | return Owned(DIE); |
| 1779 | } |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1780 | |
| 1781 | bool Sema::CheckInitList(InitListExpr *&InitList, QualType &DeclType) { |
Chris Lattner | b0912a5 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1782 | InitListChecker CheckInitList(*this, InitList, DeclType); |
Douglas Gregor | 85df8d8 | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 1783 | if (!CheckInitList.HadError()) |
| 1784 | InitList = CheckInitList.getFullyStructuredList(); |
| 1785 | |
| 1786 | return CheckInitList.HadError(); |
| 1787 | } |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1788 | |
| 1789 | /// \brief Diagnose any semantic errors with value-initialization of |
| 1790 | /// the given type. |
| 1791 | /// |
| 1792 | /// Value-initialization effectively zero-initializes any types |
| 1793 | /// without user-declared constructors, and calls the default |
| 1794 | /// constructor for a for any type that has a user-declared |
| 1795 | /// constructor (C++ [dcl.init]p5). Value-initialization can fail when |
| 1796 | /// a type with a user-declared constructor does not have an |
| 1797 | /// accessible, non-deleted default constructor. In C, everything can |
| 1798 | /// be value-initialized, which corresponds to C's notion of |
| 1799 | /// initializing objects with static storage duration when no |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1800 | /// initializer is provided for that object. |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1801 | /// |
| 1802 | /// \returns true if there was an error, false otherwise. |
| 1803 | bool Sema::CheckValueInitialization(QualType Type, SourceLocation Loc) { |
| 1804 | // C++ [dcl.init]p5: |
| 1805 | // |
| 1806 | // To value-initialize an object of type T means: |
| 1807 | |
| 1808 | // -- if T is an array type, then each element is value-initialized; |
| 1809 | if (const ArrayType *AT = Context.getAsArrayType(Type)) |
| 1810 | return CheckValueInitialization(AT->getElementType(), Loc); |
| 1811 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1812 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
Douglas Gregor | 89ee682 | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1813 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1814 | // -- if T is a class type (clause 9) with a user-declared |
| 1815 | // constructor (12.1), then the default constructor for T is |
| 1816 | // called (and the initialization is ill-formed if T has no |
| 1817 | // accessible default constructor); |
Douglas Gregor | 5d3507d | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1818 | if (ClassDecl->hasUserDeclaredConstructor()) { |
| 1819 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); |
| 1820 | |
| 1821 | CXXConstructorDecl *Constructor |
| 1822 | = PerformInitializationByConstructor(Type, |
| 1823 | MultiExprArg(*this, 0, 0), |
| 1824 | Loc, SourceRange(Loc), |
| 1825 | DeclarationName(), |
| 1826 | IK_Direct, |
| 1827 | ConstructorArgs); |
| 1828 | if (!Constructor) |
| 1829 | return true; |
| 1830 | |
| 1831 | OwningExprResult Init |
| 1832 | = BuildCXXConstructExpr(Loc, Type, Constructor, |
| 1833 | move_arg(ConstructorArgs)); |
| 1834 | if (Init.isInvalid()) |
| 1835 | return true; |
| 1836 | |
| 1837 | // FIXME: Actually perform the value-initialization! |
| 1838 | return false; |
| 1839 | } |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | if (Type->isReferenceType()) { |
| 1844 | // C++ [dcl.init]p5: |
| 1845 | // [...] A program that calls for default-initialization or |
| 1846 | // value-initialization of an entity of reference type is |
| 1847 | // ill-formed. [...] |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1848 | // FIXME: Once we have code that goes through this path, add an actual |
| 1849 | // diagnostic :) |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | return false; |
| 1853 | } |