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 | /// |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 60 | Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() { |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 61 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 62 | // If this is the old-style GNU extension: |
| 63 | // designation ::= identifier ':' |
| 64 | // Handle it as a field designator. Otherwise, this must be the start of a |
| 65 | // normal expression. |
| 66 | if (Tok.is(tok::identifier)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 67 | const IdentifierInfo *FieldName = Tok.getIdentifierInfo(); |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 68 | |
| 69 | std::string NewSyntax("."); |
| 70 | NewSyntax += FieldName->getName(); |
| 71 | NewSyntax += " = "; |
| 72 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 73 | SourceLocation NameLoc = ConsumeToken(); // Eat the identifier. |
Chris Lattner | efcadc6 | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 75 | assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!"); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 76 | SourceLocation ColonLoc = ConsumeToken(); |
| 77 | |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 78 | Diag(Tok, diag::ext_gnu_old_style_field_designator) |
| 79 | << CodeModificationHint::CreateReplacement(SourceRange(NameLoc, |
| 80 | ColonLoc), |
| 81 | NewSyntax); |
| 82 | |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 83 | Designation D; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 84 | D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc)); |
| 85 | return Actions.ActOnDesignatedInitializer(D, ColonLoc, true, |
| 86 | ParseInitializer()); |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 89 | // Desig - This is initialized when we see our first designator. We may have |
| 90 | // an objc message send with no designator, so we don't want to create this |
| 91 | // eagerly. |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 92 | Designation Desig; |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 93 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | // Parse each designator in the designator list until we find an initializer. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 95 | while (Tok.is(tok::period) || Tok.is(tok::l_square)) { |
| 96 | if (Tok.is(tok::period)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | // designator: '.' identifier |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 98 | SourceLocation DotLoc = ConsumeToken(); |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 100 | if (Tok.isNot(tok::identifier)) { |
| 101 | Diag(Tok.getLocation(), diag::err_expected_field_designator); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 102 | return ExprError(); |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 105 | Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc, |
| 106 | Tok.getLocation())); |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 107 | ConsumeToken(); // Eat the identifier. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 108 | continue; |
| 109 | } |
| 110 | |
| 111 | // We must have either an array designator now or an objc message send. |
| 112 | assert(Tok.is(tok::l_square) && "Unexpected token!"); |
| 113 | |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 114 | // Handle the two forms of array designator: |
| 115 | // array-designator: '[' constant-expression ']' |
| 116 | // array-designator: '[' constant-expression '...' constant-expression ']' |
| 117 | // |
| 118 | // Also, we have to handle the case where the expression after the |
| 119 | // designator an an objc message send: '[' objc-message-expr ']'. |
| 120 | // Interesting cases are: |
| 121 | // [foo bar] -> objc message send |
| 122 | // [foo] -> array designator |
| 123 | // [foo ... bar] -> array designator |
| 124 | // [4][foo bar] -> obsolete GNU designation with objc message send. |
| 125 | // |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 126 | SourceLocation StartLoc = ConsumeBracket(); |
| 127 | |
| 128 | // If Objective-C is enabled and this is a typename or other identifier |
| 129 | // receiver, parse this as a message send expression. |
| 130 | if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) { |
Chris Lattner | 0fc73f7 | 2008-10-26 23:29:41 +0000 | [diff] [blame] | 131 | // If we have exactly one array designator, this used the GNU |
| 132 | // 'designation: array-designator' extension, otherwise there should be no |
| 133 | // designators at all! |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 134 | if (Desig.getNumDesignators() == 1 && |
| 135 | (Desig.getDesignator(0).isArrayDesignator() || |
| 136 | Desig.getDesignator(0).isArrayRangeDesignator())) |
| 137 | Diag(StartLoc, diag::ext_gnu_missing_equal_designator); |
| 138 | else if (Desig.getNumDesignators() > 0) |
| 139 | Diag(Tok, diag::err_expected_equal_designator); |
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! |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 166 | if (Desig.getNumDesignators() == 1 && |
| 167 | (Desig.getDesignator(0).isArrayDesignator() || |
| 168 | Desig.getDesignator(0).isArrayRangeDesignator())) |
| 169 | Diag(StartLoc, diag::ext_gnu_missing_equal_designator); |
| 170 | else if (Desig.getNumDesignators() > 0) |
| 171 | Diag(Tok, diag::err_expected_equal_designator); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 172 | |
Sebastian Redl | 1d92296 | 2008-12-13 15:32:12 +0000 | [diff] [blame] | 173 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
| 174 | SourceLocation(), |
Sebastian Redl | 76ad2e8 | 2009-02-05 15:02:23 +0000 | [diff] [blame] | 175 | 0, move(Idx)); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 176 | } |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 177 | |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 178 | // If this is a normal array designator, remember it. |
| 179 | if (Tok.isNot(tok::ellipsis)) { |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 180 | Desig.AddDesignator(Designator::getArray(Idx.release(), StartLoc)); |
Chris Lattner | e232942 | 2008-10-26 23:06:54 +0000 | [diff] [blame] | 181 | } else { |
| 182 | // Handle the gnu array range extension. |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 183 | Diag(Tok, diag::ext_gnu_array_range); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 184 | SourceLocation EllipsisLoc = ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 185 | |
| 186 | OwningExprResult RHS(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 187 | if (RHS.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 188 | SkipUntil(tok::r_square); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 189 | return move(RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | } |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 191 | Desig.AddDesignator(Designator::getArrayRange(Idx.release(), |
| 192 | RHS.release(), |
| 193 | StartLoc, EllipsisLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 196 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 197 | Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc(EndLoc); |
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. |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 204 | assert(!Desig.empty() && "Designator is empty?"); |
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)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 208 | SourceLocation EqualLoc = ConsumeToken(); |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 209 | return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 210 | ParseInitializer()); |
Chris Lattner | 7f9690d | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 211 | } |
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 | // 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] | 214 | // an initializer. If we have exactly one array designator, this |
Chris Lattner | 0a68b94 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 215 | // is the GNU 'designation: array-designator' extension. Otherwise, it is a |
| 216 | // parse error. |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 217 | if (Desig.getNumDesignators() == 1 && |
| 218 | (Desig.getDesignator(0).isArrayDesignator() || |
| 219 | Desig.getDesignator(0).isArrayRangeDesignator())) { |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 220 | Diag(Tok, diag::ext_gnu_missing_equal_designator) |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame^] | 221 | << CodeModificationHint::CreateInsertion(Tok.getLocation(), "= "); |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 222 | return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(), |
Douglas Gregor | 68c56de | 2009-03-27 23:40:29 +0000 | [diff] [blame] | 223 | true, ParseInitializer()); |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 224 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 79ed6b5 | 2008-10-26 23:22:23 +0000 | [diff] [blame] | 226 | Diag(Tok, diag::err_expected_equal_designator); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 227 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
Chris Lattner | 0eec2b5 | 2008-10-26 22:38:55 +0000 | [diff] [blame] | 231 | /// ParseBraceInitializer - Called when parsing an initializer that has a |
| 232 | /// leading open brace. |
| 233 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | /// initializer: [C99 6.7.8] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | /// '{' initializer-list '}' |
| 236 | /// '{' initializer-list ',' '}' |
| 237 | /// [GNU] '{' '}' |
| 238 | /// |
| 239 | /// initializer-list: |
| 240 | /// designation[opt] initializer |
| 241 | /// initializer-list ',' designation[opt] initializer |
| 242 | /// |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 243 | Parser::OwningExprResult Parser::ParseBraceInitializer() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 244 | SourceLocation LBraceLoc = ConsumeBrace(); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 245 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 246 | /// InitExprs - This is the actual list of expressions contained in the |
| 247 | /// initializer. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 248 | ExprVector InitExprs(Actions); |
| 249 | |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 250 | if (Tok.is(tok::r_brace)) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 251 | // Empty initializers are a C++ feature and a GNU extension to C. |
| 252 | if (!getLang().CPlusPlus) |
| 253 | Diag(LBraceLoc, diag::ext_gnu_empty_initializer); |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 254 | // Match the '}'. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 255 | return Actions.ActOnInitList(LBraceLoc, Action::MultiExprArg(Actions), |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 256 | ConsumeBrace()); |
Chris Lattner | 220ad7c | 2008-10-26 23:35:51 +0000 | [diff] [blame] | 257 | } |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 258 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 259 | bool InitExprsOk = true; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 260 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 261 | while (1) { |
| 262 | // Parse: designation[opt] initializer |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 263 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 264 | // If we know that this cannot be a designation, just parse the nested |
| 265 | // initializer directly. |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 266 | OwningExprResult SubElt(Actions); |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 267 | if (MayBeDesignationStart(Tok.getKind(), PP)) |
| 268 | SubElt = ParseInitializerWithPotentialDesignator(); |
| 269 | else |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 270 | SubElt = ParseInitializer(); |
Chris Lattner | e2f5619 | 2008-11-03 09:28:22 +0000 | [diff] [blame] | 271 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 272 | // If we couldn't parse the subelement, bail out. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 273 | if (!SubElt.isInvalid()) { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 274 | InitExprs.push_back(SubElt.release()); |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 275 | } else { |
| 276 | InitExprsOk = false; |
| 277 | |
| 278 | // 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] | 279 | // 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] | 280 | // parsing the rest of the initializer. This allows us to emit |
| 281 | // diagnostics for later elements that we find. If we don't see a comma, |
| 282 | // assume there is a parse error, and just skip to recover. |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 283 | // FIXME: This comment doesn't sound right. If there is a r_brace |
| 284 | // immediately, it can't be an error, since there is no other way of |
| 285 | // leaving this loop except through this if. |
Chris Lattner | 65bb89c | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 286 | if (Tok.isNot(tok::comma)) { |
| 287 | SkipUntil(tok::r_brace, false, true); |
| 288 | break; |
| 289 | } |
| 290 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 291 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 292 | // If we don't have a comma continued list, we're done. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 293 | if (Tok.isNot(tok::comma)) break; |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 294 | |
Chris Lattner | eccc53a | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 295 | // TODO: save comma locations if some client cares. |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 296 | ConsumeToken(); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 297 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 298 | // Handle trailing comma. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 299 | if (Tok.is(tok::r_brace)) break; |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 300 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 301 | if (InitExprsOk && Tok.is(tok::r_brace)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 302 | return Actions.ActOnInitList(LBraceLoc, move_arg(InitExprs), |
Douglas Gregor | 5908a92 | 2009-03-20 23:11:49 +0000 | [diff] [blame] | 303 | ConsumeBrace()); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 304 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 305 | // Match the '}'. |
| 306 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Sebastian Redl | 20df9b7 | 2008-12-11 22:51:44 +0000 | [diff] [blame] | 307 | return ExprError(); // an error occurred. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 308 | } |
| 309 | |