Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseInit.cpp - Initializer Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements initializer parsing as specified by C99 6.7.8. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 16 | #include "RAIIObjectsForParser.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Designator.h" |
| 18 | #include "clang/Sema/Scope.h" |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | 62a7217 | 2009-10-17 23:52:50 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | |
| 24 | /// MayBeDesignationStart - Return true if this token might be the start of a |
Chris Lattner | 838cb21 | 2008-10-26 21:46:13 +0000 | [diff] [blame] | 25 | /// designator. If we can tell it is impossible that it is a designator, return |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 26 | /// false. |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 27 | static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | switch (K) { |
| 29 | default: return false; |
| 30 | case tok::period: // designator: '.' identifier |
| 31 | case tok::l_square: // designator: array-designator |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 32 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | case tok::identifier: // designation: identifier ':' |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 34 | return PP.LookAhead(0).is(tok::colon); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 38 | static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc, |
| 39 | Designation &Desig) { |
| 40 | // If we have exactly one array designator, this used the GNU |
| 41 | // 'designation: array-designator' extension, otherwise there should be no |
| 42 | // designators at all! |
| 43 | if (Desig.getNumDesignators() == 1 && |
| 44 | (Desig.getDesignator(0).isArrayDesignator() || |
| 45 | Desig.getDesignator(0).isArrayRangeDesignator())) |
| 46 | P.Diag(Loc, diag::ext_gnu_missing_equal_designator); |
| 47 | else if (Desig.getNumDesignators() > 0) |
| 48 | P.Diag(Loc, diag::err_expected_equal_designator); |
| 49 | } |
| 50 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production |
| 52 | /// checking to see if the token stream starts with a designator. |
| 53 | /// |
| 54 | /// designation: |
| 55 | /// designator-list '=' |
| 56 | /// [GNU] array-designator |
| 57 | /// [GNU] identifier ':' |
| 58 | /// |
| 59 | /// designator-list: |
| 60 | /// designator |
| 61 | /// designator-list designator |
| 62 | /// |
| 63 | /// designator: |
| 64 | /// array-designator |
| 65 | /// '.' identifier |
| 66 | /// |
| 67 | /// array-designator: |
| 68 | /// '[' constant-expression ']' |
| 69 | /// [GNU] '[' constant-expression '...' constant-expression ']' |
| 70 | /// |
| 71 | /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an |
Chris Lattner | 838cb21 | 2008-10-26 21:46:13 +0000 | [diff] [blame] | 72 | /// initializer (because it is an expression). We need to consider this case |
| 73 | /// when parsing array designators. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 75 | ExprResult Parser::ParseInitializerWithPotentialDesignator() { |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 76 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 77 | // If this is the old-style GNU extension: |
| 78 | // designation ::= identifier ':' |
| 79 | // Handle it as a field designator. Otherwise, this must be the start of a |
| 80 | // normal expression. |
| 81 | if (Tok.is(tok::identifier)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 82 | const IdentifierInfo *FieldName = Tok.getIdentifierInfo(); |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 83 | |
Daniel Dunbar | 62a7217 | 2009-10-17 23:52:50 +0000 | [diff] [blame] | 84 | llvm::SmallString<256> NewSyntax; |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 85 | llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName() |
Daniel Dunbar | 62a7217 | 2009-10-17 23:52:50 +0000 | [diff] [blame] | 86 | << " = "; |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 87 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 88 | SourceLocation NameLoc = ConsumeToken(); // Eat the identifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 90 | assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!"); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 91 | SourceLocation ColonLoc = ConsumeToken(); |
| 92 | |
Douglas Gregor | 40a0f9c | 2011-08-27 00:13:16 +0000 | [diff] [blame] | 93 | Diag(NameLoc, diag::ext_gnu_old_style_field_designator) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 94 | << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc), |
| 95 | NewSyntax.str()); |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 96 | |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 97 | Designation D; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 98 | D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | return Actions.ActOnDesignatedInitializer(D, ColonLoc, true, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 100 | ParseInitializer()); |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 103 | // Desig - This is initialized when we see our first designator. We may have |
| 104 | // an objc message send with no designator, so we don't want to create this |
| 105 | // eagerly. |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 106 | Designation Desig; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | // Parse each designator in the designator list until we find an initializer. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 109 | while (Tok.is(tok::period) || Tok.is(tok::l_square)) { |
| 110 | if (Tok.is(tok::period)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | // designator: '.' identifier |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 112 | SourceLocation DotLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 114 | if (Tok.isNot(tok::identifier)) { |
| 115 | Diag(Tok.getLocation(), diag::err_expected_field_designator); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 116 | return ExprError(); |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 119 | Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc, |
| 120 | Tok.getLocation())); |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 121 | ConsumeToken(); // Eat the identifier. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 122 | continue; |
| 123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 125 | // We must have either an array designator now or an objc message send. |
| 126 | assert(Tok.is(tok::l_square) && "Unexpected token!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 128 | // Handle the two forms of array designator: |
| 129 | // array-designator: '[' constant-expression ']' |
| 130 | // array-designator: '[' constant-expression '...' constant-expression ']' |
| 131 | // |
| 132 | // Also, we have to handle the case where the expression after the |
| 133 | // designator an an objc message send: '[' objc-message-expr ']'. |
| 134 | // Interesting cases are: |
| 135 | // [foo bar] -> objc message send |
| 136 | // [foo] -> array designator |
| 137 | // [foo ... bar] -> array designator |
| 138 | // [4][foo bar] -> obsolete GNU designation with objc message send. |
| 139 | // |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 140 | InMessageExpressionRAIIObject InMessage(*this, true); |
| 141 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 142 | SourceLocation StartLoc = ConsumeBracket(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 143 | ExprResult Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 145 | // If Objective-C is enabled and this is a typename (class message |
| 146 | // send) or send to 'super', parse this as a message send |
| 147 | // expression. We handle C++ and C separately, since C++ requires |
| 148 | // much more complicated parsing. |
| 149 | if (getLang().ObjC1 && getLang().CPlusPlus) { |
| 150 | // Send to 'super'. |
| 151 | if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super && |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 152 | NextToken().isNot(tok::period) && |
| 153 | getCurScope()->isInObjcMethodScope()) { |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 154 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
| 155 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 156 | ConsumeToken(), |
| 157 | ParsedType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 158 | 0); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Parse the receiver, which is either a type or an expression. |
| 162 | bool IsExpr; |
| 163 | void *TypeOrExpr; |
| 164 | if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) { |
| 165 | SkipUntil(tok::r_square); |
| 166 | return ExprError(); |
| 167 | } |
| 168 | |
| 169 | // If the receiver was a type, we have a class message; parse |
| 170 | // the rest of it. |
| 171 | if (!IsExpr) { |
| 172 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
| 173 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 174 | SourceLocation(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 175 | ParsedType::getFromOpaquePtr(TypeOrExpr), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 176 | 0); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // If the receiver was an expression, we still don't know |
| 180 | // whether we have a message send or an array designator; just |
| 181 | // adopt the expression for further analysis below. |
| 182 | // FIXME: potentially-potentially evaluated expression above? |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 183 | Idx = ExprResult(static_cast<Expr*>(TypeOrExpr)); |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 184 | } else if (getLang().ObjC1 && Tok.is(tok::identifier)) { |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 185 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 186 | SourceLocation IILoc = Tok.getLocation(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 187 | ParsedType ReceiverType; |
Chris Lattner | 1e46136 | 2010-04-12 06:36:00 +0000 | [diff] [blame] | 188 | // Three cases. This is a message send to a type: [type foo] |
| 189 | // This is a message send to super: [super foo] |
| 190 | // This is a message sent to an expr: [super.bar foo] |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 191 | switch (Sema::ObjCMessageKind Kind |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 192 | = Actions.getObjCMessageKind(getCurScope(), II, IILoc, |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 193 | II == Ident_super, |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 194 | NextToken().is(tok::period), |
| 195 | ReceiverType)) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 196 | case Sema::ObjCSuperMessage: |
| 197 | case Sema::ObjCClassMessage: |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 198 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 199 | if (Kind == Sema::ObjCSuperMessage) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 200 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 201 | ConsumeToken(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 202 | ParsedType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 203 | 0); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 204 | ConsumeToken(); // the identifier |
| 205 | if (!ReceiverType) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 206 | SkipUntil(tok::r_square); |
| 207 | return ExprError(); |
| 208 | } |
| 209 | |
| 210 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 211 | SourceLocation(), |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 212 | ReceiverType, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 213 | 0); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 214 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 215 | case Sema::ObjCInstanceMessage: |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 216 | // Fall through; we'll just parse the expression and |
| 217 | // (possibly) treat this like an Objective-C message send |
| 218 | // later. |
| 219 | break; |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 221 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 222 | |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 223 | // Parse the index expression, if we haven't already gotten one |
| 224 | // above (which can only happen in Objective-C++). |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 225 | // Note that we parse this as an assignment expression, not a constant |
| 226 | // expression (allowing *=, =, etc) to handle the objc case. Sema needs |
| 227 | // to validate that the expression is a constant. |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 228 | // FIXME: We also need to tell Sema that we're in a |
| 229 | // potentially-potentially evaluated context. |
| 230 | if (!Idx.get()) { |
| 231 | Idx = ParseAssignmentExpression(); |
| 232 | if (Idx.isInvalid()) { |
| 233 | SkipUntil(tok::r_square); |
| 234 | return move(Idx); |
| 235 | } |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 236 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 238 | // Given an expression, we could either have a designator (if the next |
| 239 | // tokens are '...' or ']' or an objc message send. If this is an objc |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | // message send, handle it now. An objc-message send is the start of |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 241 | // an assignment-expression production. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) && |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 243 | Tok.isNot(tok::r_square)) { |
Douglas Gregor | 6aa14d8 | 2010-04-21 22:36:40 +0000 | [diff] [blame] | 244 | CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 245 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 246 | SourceLocation(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 247 | ParsedType(), |
| 248 | Idx.take()); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 249 | } |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 250 | |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 251 | // If this is a normal array designator, remember it. |
| 252 | if (Tok.isNot(tok::ellipsis)) { |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 253 | Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc)); |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 254 | } else { |
| 255 | // Handle the gnu array range extension. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 256 | Diag(Tok, diag::ext_gnu_array_range); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 257 | SourceLocation EllipsisLoc = ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 258 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 259 | ExprResult RHS(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 260 | if (RHS.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | SkipUntil(tok::r_square); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 262 | return move(RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | } |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 264 | Desig.AddDesignator(Designator::getArrayRange(Idx.release(), |
| 265 | RHS.release(), |
| 266 | StartLoc, EllipsisLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 268 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 269 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 270 | Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 271 | } |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 273 | // Okay, we're done with the designator sequence. We know that there must be |
| 274 | // at least one designator, because the only case we can get into this method |
| 275 | // without a designator is when we have an objc message send. That case is |
| 276 | // handled and returned from above. |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 277 | assert(!Desig.empty() && "Designator is empty?"); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 279 | // Handle a normal designator sequence end, which is an equal. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 280 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 281 | SourceLocation EqualLoc = ConsumeToken(); |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 282 | return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 283 | ParseInitializer()); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 284 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 286 | // We read some number of designators and found something that isn't an = or |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 287 | // an initializer. If we have exactly one array designator, this |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 288 | // is the GNU 'designation: array-designator' extension. Otherwise, it is a |
| 289 | // parse error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | if (Desig.getNumDesignators() == 1 && |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 291 | (Desig.getDesignator(0).isArrayDesignator() || |
| 292 | Desig.getDesignator(0).isArrayRangeDesignator())) { |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 293 | Diag(Tok, diag::ext_gnu_missing_equal_designator) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 294 | << FixItHint::CreateInsertion(Tok.getLocation(), "= "); |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 295 | return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(), |
Douglas Gregor | 68c56de | 2009-03-27 23:40:29 +0000 | [diff] [blame] | 296 | true, ParseInitializer()); |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 297 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 299 | Diag(Tok, diag::err_expected_equal_designator); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 300 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
Chris Lattner | 0eec2b5 | 2008-10-26 22:38:55 +0000 | [diff] [blame] | 304 | /// ParseBraceInitializer - Called when parsing an initializer that has a |
| 305 | /// leading open brace. |
| 306 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | /// initializer: [C99 6.7.8] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 308 | /// '{' initializer-list '}' |
| 309 | /// '{' initializer-list ',' '}' |
| 310 | /// [GNU] '{' '}' |
| 311 | /// |
| 312 | /// initializer-list: |
Douglas Gregor | dcaa1ca | 2011-01-03 19:31:53 +0000 | [diff] [blame] | 313 | /// designation[opt] initializer ...[opt] |
| 314 | /// initializer-list ',' designation[opt] initializer ...[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 316 | ExprResult Parser::ParseBraceInitializer() { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 317 | InMessageExpressionRAIIObject InMessage(*this, false); |
| 318 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 319 | SourceLocation LBraceLoc = ConsumeBrace(); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 320 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 321 | /// InitExprs - This is the actual list of expressions contained in the |
| 322 | /// initializer. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 323 | ExprVector InitExprs(Actions); |
| 324 | |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 325 | if (Tok.is(tok::r_brace)) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 326 | // Empty initializers are a C++ feature and a GNU extension to C. |
| 327 | if (!getLang().CPlusPlus) |
| 328 | Diag(LBraceLoc, diag::ext_gnu_empty_initializer); |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 329 | // Match the '}'. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 330 | return Actions.ActOnInitList(LBraceLoc, MultiExprArg(Actions), |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 331 | ConsumeBrace()); |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 332 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 333 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 334 | bool InitExprsOk = true; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 335 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 336 | while (1) { |
| 337 | // Parse: designation[opt] initializer |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 338 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 339 | // If we know that this cannot be a designation, just parse the nested |
| 340 | // initializer directly. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 341 | ExprResult SubElt; |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 342 | if (MayBeDesignationStart(Tok.getKind(), PP)) |
| 343 | SubElt = ParseInitializerWithPotentialDesignator(); |
| 344 | else |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 345 | SubElt = ParseInitializer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Douglas Gregor | dcaa1ca | 2011-01-03 19:31:53 +0000 | [diff] [blame] | 347 | if (Tok.is(tok::ellipsis)) |
| 348 | SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken()); |
| 349 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 350 | // If we couldn't parse the subelement, bail out. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 351 | if (!SubElt.isInvalid()) { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 352 | InitExprs.push_back(SubElt.release()); |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 353 | } else { |
| 354 | InitExprsOk = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 356 | // We have two ways to try to recover from this error: if the code looks |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 357 | // grammatically ok (i.e. we have a comma coming up) try to continue |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 358 | // parsing the rest of the initializer. This allows us to emit |
| 359 | // diagnostics for later elements that we find. If we don't see a comma, |
| 360 | // assume there is a parse error, and just skip to recover. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 361 | // FIXME: This comment doesn't sound right. If there is a r_brace |
| 362 | // immediately, it can't be an error, since there is no other way of |
| 363 | // leaving this loop except through this if. |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 364 | if (Tok.isNot(tok::comma)) { |
| 365 | SkipUntil(tok::r_brace, false, true); |
| 366 | break; |
| 367 | } |
| 368 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 369 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 370 | // If we don't have a comma continued list, we're done. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 371 | if (Tok.isNot(tok::comma)) break; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 372 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 373 | // TODO: save comma locations if some client cares. |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 374 | ConsumeToken(); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 375 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 376 | // Handle trailing comma. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 377 | if (Tok.is(tok::r_brace)) break; |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 378 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 379 | if (InitExprsOk && Tok.is(tok::r_brace)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 380 | return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs), |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 381 | ConsumeBrace()); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 382 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | // Match the '}'. |
| 384 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 385 | return ExprError(); // an error occurred. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 386 | } |
| 387 | |