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 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 14 | #include "clang/Parse/Designator.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Parser.h" |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 16 | #include "AstGuard.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 17 | #include "clang/Parse/ParseDiagnostic.h" |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
| 21 | |
| 22 | /// MayBeDesignationStart - Return true if this token might be the start of a |
Chris Lattner | 838cb21 | 2008-10-26 21:46:13 +0000 | [diff] [blame] | 23 | /// designator. If we can tell it is impossible that it is a designator, return |
| 24 | /// false. |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 25 | static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | switch (K) { |
| 27 | default: return false; |
| 28 | case tok::period: // designator: '.' identifier |
| 29 | case tok::l_square: // designator: array-designator |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 30 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | case tok::identifier: // designation: identifier ':' |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 32 | return PP.LookAhead(0).is(tok::colon); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
| 36 | /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production |
| 37 | /// checking to see if the token stream starts with a designator. |
| 38 | /// |
| 39 | /// designation: |
| 40 | /// designator-list '=' |
| 41 | /// [GNU] array-designator |
| 42 | /// [GNU] identifier ':' |
| 43 | /// |
| 44 | /// designator-list: |
| 45 | /// designator |
| 46 | /// designator-list designator |
| 47 | /// |
| 48 | /// designator: |
| 49 | /// array-designator |
| 50 | /// '.' identifier |
| 51 | /// |
| 52 | /// array-designator: |
| 53 | /// '[' constant-expression ']' |
| 54 | /// [GNU] '[' constant-expression '...' constant-expression ']' |
| 55 | /// |
| 56 | /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an |
Chris Lattner | 838cb21 | 2008-10-26 21:46:13 +0000 | [diff] [blame] | 57 | /// initializer (because it is an expression). We need to consider this case |
| 58 | /// when parsing array designators. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 60 | Parser::OwningExprResult Parser:: |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 61 | ParseInitializerWithPotentialDesignator(InitListDesignations &Designations, |
| 62 | unsigned InitNum) { |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 63 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 64 | // If this is the old-style GNU extension: |
| 65 | // designation ::= identifier ':' |
| 66 | // Handle it as a field designator. Otherwise, this must be the start of a |
| 67 | // normal expression. |
| 68 | if (Tok.is(tok::identifier)) { |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 69 | Diag(Tok, diag::ext_gnu_old_style_field_designator); |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 70 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 71 | const IdentifierInfo *FieldName = Tok.getIdentifierInfo(); |
| 72 | SourceLocation NameLoc = ConsumeToken(); // Eat the identifier. |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 74 | assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!"); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 75 | SourceLocation ColonLoc = ConsumeToken(); |
| 76 | |
| 77 | Designation &D = Designations.CreateDesignation(InitNum); |
| 78 | D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc)); |
| 79 | return Actions.ActOnDesignatedInitializer(D, ColonLoc, true, |
| 80 | ParseInitializer()); |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 83 | // Desig - This is initialized when we see our first designator. We may have |
| 84 | // an objc message send with no designator, so we don't want to create this |
| 85 | // eagerly. |
| 86 | Designation *Desig = 0; |
| 87 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | // Parse each designator in the designator list until we find an initializer. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 89 | while (Tok.is(tok::period) || Tok.is(tok::l_square)) { |
| 90 | if (Tok.is(tok::period)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | // designator: '.' identifier |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 92 | SourceLocation DotLoc = ConsumeToken(); |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 93 | |
| 94 | // Create designation if we haven't already. |
| 95 | if (Desig == 0) |
| 96 | Desig = &Designations.CreateDesignation(InitNum); |
| 97 | |
| 98 | if (Tok.isNot(tok::identifier)) { |
| 99 | Diag(Tok.getLocation(), diag::err_expected_field_designator); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 100 | return ExprError(); |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 103 | Desig->AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc, |
| 104 | Tok.getLocation())); |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 105 | ConsumeToken(); // Eat the identifier. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 106 | continue; |
| 107 | } |
| 108 | |
| 109 | // We must have either an array designator now or an objc message send. |
| 110 | assert(Tok.is(tok::l_square) && "Unexpected token!"); |
| 111 | |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 112 | // Handle the two forms of array designator: |
| 113 | // array-designator: '[' constant-expression ']' |
| 114 | // array-designator: '[' constant-expression '...' constant-expression ']' |
| 115 | // |
| 116 | // Also, we have to handle the case where the expression after the |
| 117 | // designator an an objc message send: '[' objc-message-expr ']'. |
| 118 | // Interesting cases are: |
| 119 | // [foo bar] -> objc message send |
| 120 | // [foo] -> array designator |
| 121 | // [foo ... bar] -> array designator |
| 122 | // [4][foo bar] -> obsolete GNU designation with objc message send. |
| 123 | // |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 124 | SourceLocation StartLoc = ConsumeBracket(); |
| 125 | |
| 126 | // If Objective-C is enabled and this is a typename or other identifier |
| 127 | // receiver, parse this as a message send expression. |
| 128 | if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) { |
Chris Lattner | 0fc73f7 | 2008-10-26 23:29:41 +0000 | [diff] [blame] | 129 | // If we have exactly one array designator, this used the GNU |
| 130 | // 'designation: array-designator' extension, otherwise there should be no |
| 131 | // designators at all! |
| 132 | if (Desig) { |
| 133 | if (Desig->getNumDesignators() == 1 && |
| 134 | (Desig->getDesignator(0).isArrayDesignator() || |
| 135 | Desig->getDesignator(0).isArrayRangeDesignator())) |
| 136 | Diag(StartLoc, diag::ext_gnu_missing_equal_designator); |
| 137 | else |
| 138 | Diag(Tok, diag::err_expected_equal_designator); |
| 139 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 141 | IdentifierInfo *Name = Tok.getIdentifierInfo(); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 142 | SourceLocation NameLoc = ConsumeToken(); |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 143 | return ParseAssignmentExprWithObjCMessageExprStart( |
| 144 | StartLoc, NameLoc, Name, ExprArg(Actions)); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 145 | } |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 147 | // Note that we parse this as an assignment expression, not a constant |
| 148 | // expression (allowing *=, =, etc) to handle the objc case. Sema needs |
| 149 | // to validate that the expression is a constant. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 150 | OwningExprResult Idx(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 151 | if (Idx.isInvalid()) { |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 152 | SkipUntil(tok::r_square); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 153 | return move(Idx); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Given an expression, we could either have a designator (if the next |
| 157 | // tokens are '...' or ']' or an objc message send. If this is an objc |
| 158 | // message send, handle it now. An objc-message send is the start of |
| 159 | // an assignment-expression production. |
| 160 | if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) && |
| 161 | Tok.isNot(tok::r_square)) { |
Chris Lattner | 0fc73f7 | 2008-10-26 23:29:41 +0000 | [diff] [blame] | 162 | |
| 163 | // If we have exactly one array designator, this used the GNU |
| 164 | // 'designation: array-designator' extension, otherwise there should be no |
| 165 | // designators at all! |
| 166 | if (Desig) { |
| 167 | if (Desig->getNumDesignators() == 1 && |
| 168 | (Desig->getDesignator(0).isArrayDesignator() || |
| 169 | Desig->getDesignator(0).isArrayRangeDesignator())) |
| 170 | Diag(StartLoc, diag::ext_gnu_missing_equal_designator); |
| 171 | else |
| 172 | Diag(Tok, diag::err_expected_equal_designator); |
| 173 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 174 | |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 175 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 176 | SourceLocation(), |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 177 | 0, move(Idx)); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 178 | } |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 179 | |
| 180 | // Create designation if we haven't already. |
| 181 | if (Desig == 0) |
| 182 | Desig = &Designations.CreateDesignation(InitNum); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 183 | |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 184 | // If this is a normal array designator, remember it. |
| 185 | if (Tok.isNot(tok::ellipsis)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 186 | Desig->AddDesignator(Designator::getArray(Idx.release(), |
| 187 | StartLoc)); |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 188 | } else { |
| 189 | // Handle the gnu array range extension. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 190 | Diag(Tok, diag::ext_gnu_array_range); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 191 | SourceLocation EllipsisLoc = ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 192 | |
| 193 | OwningExprResult RHS(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 194 | if (RHS.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 195 | SkipUntil(tok::r_square); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 196 | return move(RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 197 | } |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 198 | Desig->AddDesignator(Designator::getArrayRange(Idx.release(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 199 | RHS.release(), |
| 200 | StartLoc, EllipsisLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 202 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 203 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
| 204 | Desig->getDesignator(Desig->getNumDesignators() - 1).setRBracketLoc(EndLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 205 | } |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 207 | // Okay, we're done with the designator sequence. We know that there must be |
| 208 | // at least one designator, because the only case we can get into this method |
| 209 | // without a designator is when we have an objc message send. That case is |
| 210 | // handled and returned from above. |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 211 | assert(Desig && "Designator didn't get created?"); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 213 | // Handle a normal designator sequence end, which is an equal. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 214 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 215 | SourceLocation EqualLoc = ConsumeToken(); |
| 216 | return Actions.ActOnDesignatedInitializer(*Desig, EqualLoc, false, |
| 217 | ParseInitializer()); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 218 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 220 | // 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] | 221 | // an initializer. If we have exactly one array designator, this |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 222 | // is the GNU 'designation: array-designator' extension. Otherwise, it is a |
| 223 | // parse error. |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 224 | if (Desig->getNumDesignators() == 1 && |
| 225 | (Desig->getDesignator(0).isArrayDesignator() || |
| 226 | Desig->getDesignator(0).isArrayRangeDesignator())) { |
| 227 | Diag(Tok, diag::ext_gnu_missing_equal_designator); |
| 228 | return ParseInitializer(); |
| 229 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 231 | Diag(Tok, diag::err_expected_equal_designator); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 232 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
Chris Lattner | 0eec2b5 | 2008-10-26 22:38:55 +0000 | [diff] [blame] | 236 | /// ParseBraceInitializer - Called when parsing an initializer that has a |
| 237 | /// leading open brace. |
| 238 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 239 | /// initializer: [C99 6.7.8] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 240 | /// '{' initializer-list '}' |
| 241 | /// '{' initializer-list ',' '}' |
| 242 | /// [GNU] '{' '}' |
| 243 | /// |
| 244 | /// initializer-list: |
| 245 | /// designation[opt] initializer |
| 246 | /// initializer-list ',' designation[opt] initializer |
| 247 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 248 | Parser::OwningExprResult Parser::ParseBraceInitializer() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | SourceLocation LBraceLoc = ConsumeBrace(); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 250 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 251 | /// InitExprs - This is the actual list of expressions contained in the |
| 252 | /// initializer. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 253 | ExprVector InitExprs(Actions); |
| 254 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 255 | /// ExprDesignators - For each initializer, keep track of the designator that |
| 256 | /// was specified for it, if any. |
| 257 | InitListDesignations InitExprDesignations(Actions); |
| 258 | |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 259 | if (Tok.is(tok::r_brace)) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 260 | // Empty initializers are a C++ feature and a GNU extension to C. |
| 261 | if (!getLang().CPlusPlus) |
| 262 | Diag(LBraceLoc, diag::ext_gnu_empty_initializer); |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 263 | // Match the '}'. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 264 | return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions), |
| 265 | InitExprDesignations, ConsumeBrace()); |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 266 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 267 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 268 | bool InitExprsOk = true; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 269 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 270 | while (1) { |
| 271 | // Parse: designation[opt] initializer |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 272 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 273 | // If we know that this cannot be a designation, just parse the nested |
| 274 | // initializer directly. |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 275 | OwningExprResult SubElt(Actions); |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 276 | if (!MayBeDesignationStart(Tok.getKind(), PP)) |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 277 | SubElt = ParseInitializer(); |
Chris Lattner | e2f5619 | 2008-11-03 09:28:22 +0000 | [diff] [blame] | 278 | else { |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 279 | SubElt = ParseInitializerWithPotentialDesignator(InitExprDesignations, |
| 280 | InitExprs.size()); |
Chris Lattner | e2f5619 | 2008-11-03 09:28:22 +0000 | [diff] [blame] | 281 | |
| 282 | // If we had an erroneous initializer, and we had a potentially valid |
| 283 | // designator, make sure to remove the designator from |
| 284 | // InitExprDesignations, otherwise we'll end up with a designator with no |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 285 | // matching initializer. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 286 | if (SubElt.isInvalid()) |
Chris Lattner | e2f5619 | 2008-11-03 09:28:22 +0000 | [diff] [blame] | 287 | InitExprDesignations.EraseDesignation(InitExprs.size()); |
| 288 | } |
| 289 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 290 | // If we couldn't parse the subelement, bail out. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 291 | if (!SubElt.isInvalid()) { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 292 | InitExprs.push_back(SubElt.release()); |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 293 | } else { |
| 294 | InitExprsOk = false; |
| 295 | |
| 296 | // We have two ways to try to recover from this error: if the code looks |
Chris Lattner | 838cb21 | 2008-10-26 21:46:13 +0000 | [diff] [blame] | 297 | // gramatically ok (i.e. we have a comma coming up) try to continue |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 298 | // parsing the rest of the initializer. This allows us to emit |
| 299 | // diagnostics for later elements that we find. If we don't see a comma, |
| 300 | // assume there is a parse error, and just skip to recover. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 301 | // FIXME: This comment doesn't sound right. If there is a r_brace |
| 302 | // immediately, it can't be an error, since there is no other way of |
| 303 | // leaving this loop except through this if. |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 304 | if (Tok.isNot(tok::comma)) { |
| 305 | SkipUntil(tok::r_brace, false, true); |
| 306 | break; |
| 307 | } |
| 308 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 309 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 310 | // If we don't have a comma continued list, we're done. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 311 | if (Tok.isNot(tok::comma)) break; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 312 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 313 | // TODO: save comma locations if some client cares. |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 314 | ConsumeToken(); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 315 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 316 | // Handle trailing comma. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 317 | if (Tok.is(tok::r_brace)) break; |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 318 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 319 | if (InitExprsOk && Tok.is(tok::r_brace)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 320 | return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs), |
| 321 | InitExprDesignations, ConsumeBrace()); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 322 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 323 | // Match the '}'. |
| 324 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 325 | return ExprError(); // an error occurred. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 326 | } |
| 327 | |