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