Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- ParseInit.cpp - Initializer Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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" |
| 15 | #include "clang/Basic/Diagnostic.h" |
| 16 | #include "llvm/ADT/SmallString.h" |
| 17 | using namespace clang; |
| 18 | |
| 19 | |
| 20 | /// MayBeDesignationStart - Return true if this token might be the start of a |
Chris Lattner | e29ac82 | 2008-10-26 21:46:13 +0000 | [diff] [blame^] | 21 | /// designator. If we can tell it is impossible that it is a designator, return |
| 22 | /// false. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 23 | static bool MayBeDesignationStart(tok::TokenKind K) { |
| 24 | switch (K) { |
| 25 | default: return false; |
| 26 | case tok::period: // designator: '.' identifier |
| 27 | case tok::l_square: // designator: array-designator |
| 28 | case tok::identifier: // designation: identifier ':' |
| 29 | return true; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production |
| 34 | /// checking to see if the token stream starts with a designator. |
| 35 | /// |
| 36 | /// designation: |
| 37 | /// designator-list '=' |
| 38 | /// [GNU] array-designator |
| 39 | /// [GNU] identifier ':' |
| 40 | /// |
| 41 | /// designator-list: |
| 42 | /// designator |
| 43 | /// designator-list designator |
| 44 | /// |
| 45 | /// designator: |
| 46 | /// array-designator |
| 47 | /// '.' identifier |
| 48 | /// |
| 49 | /// array-designator: |
| 50 | /// '[' constant-expression ']' |
| 51 | /// [GNU] '[' constant-expression '...' constant-expression ']' |
| 52 | /// |
| 53 | /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an |
Chris Lattner | e29ac82 | 2008-10-26 21:46:13 +0000 | [diff] [blame^] | 54 | /// initializer (because it is an expression). We need to consider this case |
| 55 | /// when parsing array designators. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 56 | /// |
| 57 | Parser::ExprResult Parser::ParseInitializerWithPotentialDesignator() { |
| 58 | // Parse each designator in the designator list until we find an initializer. |
| 59 | while (1) { |
| 60 | switch (Tok.getKind()) { |
| 61 | case tok::equal: |
| 62 | // We read some number (at least one due to the grammar we implemented) |
| 63 | // of designators and found an '=' sign. The following tokens must be |
| 64 | // the initializer. |
| 65 | ConsumeToken(); |
| 66 | return ParseInitializer(); |
| 67 | |
| 68 | default: { |
| 69 | // We read some number (at least one due to the grammar we implemented) |
| 70 | // of designators and found something that isn't an = or an initializer. |
| 71 | // If we have exactly one array designator [TODO CHECK], this is the GNU |
| 72 | // 'designation: array-designator' extension. Otherwise, it is a parse |
| 73 | // error. |
| 74 | SourceLocation Loc = Tok.getLocation(); |
| 75 | ExprResult Init = ParseInitializer(); |
| 76 | if (Init.isInvalid) return Init; |
| 77 | |
| 78 | Diag(Tok, diag::ext_gnu_missing_equal_designator); |
| 79 | return Init; |
| 80 | } |
| 81 | case tok::period: |
| 82 | // designator: '.' identifier |
| 83 | ConsumeToken(); |
| 84 | if (ExpectAndConsume(tok::identifier, diag::err_expected_ident)) |
| 85 | return ExprResult(true); |
| 86 | break; |
| 87 | |
| 88 | case tok::l_square: { |
| 89 | // array-designator: '[' constant-expression ']' |
| 90 | // array-designator: '[' constant-expression '...' constant-expression ']' |
Chris Lattner | 16c865e | 2008-01-25 19:37:24 +0000 | [diff] [blame] | 91 | // When designation is empty, this can be '[' objc-message-expr ']'. Note |
| 92 | // that we also have the case of [4][foo bar], which is the gnu designator |
| 93 | // extension + objc message send. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 94 | SourceLocation StartLoc = ConsumeBracket(); |
| 95 | |
Chris Lattner | 16c865e | 2008-01-25 19:37:24 +0000 | [diff] [blame] | 96 | // If Objective-C is enabled and this is a typename or other identifier |
| 97 | // receiver, parse this as a message send expression. |
| 98 | if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) { |
Chris Lattner | e69015d | 2008-01-25 19:43:26 +0000 | [diff] [blame] | 99 | // FIXME: Emit ext_gnu_missing_equal_designator for inits like |
| 100 | // [4][foo bar]. |
Chris Lattner | 16c865e | 2008-01-25 19:37:24 +0000 | [diff] [blame] | 101 | IdentifierInfo *Name = Tok.getIdentifierInfo(); |
| 102 | ConsumeToken(); |
Chris Lattner | bfcf477 | 2008-06-02 21:31:07 +0000 | [diff] [blame] | 103 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, Name, 0); |
Chris Lattner | 16c865e | 2008-01-25 19:37:24 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Note that we parse this as an assignment expression, not a constant |
Chris Lattner | e69015d | 2008-01-25 19:43:26 +0000 | [diff] [blame] | 107 | // expression (allowing *=, =, etc) to handle the objc case. Sema needs |
| 108 | // to validate that the expression is a constant. |
Chris Lattner | 16c865e | 2008-01-25 19:37:24 +0000 | [diff] [blame] | 109 | ExprResult Idx = ParseAssignmentExpression(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 110 | if (Idx.isInvalid) { |
| 111 | SkipUntil(tok::r_square); |
| 112 | return Idx; |
| 113 | } |
| 114 | |
Chris Lattner | e69015d | 2008-01-25 19:43:26 +0000 | [diff] [blame] | 115 | // Given an expression, we could either have a designator (if the next |
| 116 | // tokens are '...' or ']' or an objc message send. If this is an objc |
Chris Lattner | bfcf477 | 2008-06-02 21:31:07 +0000 | [diff] [blame] | 117 | // message send, handle it now. An objc-message send is the start of |
| 118 | // an assignment-expression production. |
Chris Lattner | e69015d | 2008-01-25 19:43:26 +0000 | [diff] [blame] | 119 | if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) && |
| 120 | Tok.isNot(tok::r_square)) { |
| 121 | // FIXME: Emit ext_gnu_missing_equal_designator for inits like |
| 122 | // [4][foo bar]. |
Chris Lattner | bfcf477 | 2008-06-02 21:31:07 +0000 | [diff] [blame] | 123 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 0,Idx.Val); |
Chris Lattner | e69015d | 2008-01-25 19:43:26 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 126 | // Handle the gnu array range extension. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 127 | if (Tok.is(tok::ellipsis)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 128 | Diag(Tok, diag::ext_gnu_array_range); |
| 129 | ConsumeToken(); |
| 130 | |
| 131 | ExprResult RHS = ParseConstantExpression(); |
| 132 | if (RHS.isInvalid) { |
| 133 | SkipUntil(tok::r_square); |
| 134 | return RHS; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | MatchRHSPunctuation(tok::r_square, StartLoc); |
| 139 | break; |
| 140 | } |
| 141 | case tok::identifier: { |
| 142 | // Due to the GNU "designation: identifier ':'" extension, we don't know |
| 143 | // whether something starting with an identifier is an |
| 144 | // assignment-expression or if it is an old-style structure field |
| 145 | // designator. |
| 146 | // TODO: Check that this is the first designator. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 147 | |
| 148 | // If this is the gross GNU extension, handle it now. |
Argiris Kirtzidis | 680e1d9 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 149 | if (NextToken().is(tok::colon)) { |
| 150 | Diag(Tok, diag::ext_gnu_old_style_field_designator); |
| 151 | ConsumeToken(); // The identifier. |
| 152 | assert(Tok.is(tok::colon) && "NextToken() not working properly!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 153 | ConsumeToken(); |
| 154 | return ParseInitializer(); |
| 155 | } |
| 156 | |
Argiris Kirtzidis | 680e1d9 | 2008-07-09 22:53:07 +0000 | [diff] [blame] | 157 | // Otherwise, parse the assignment-expression. |
| 158 | return ParseAssignmentExpression(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /// ParseInitializer |
| 166 | /// initializer: [C99 6.7.8] |
| 167 | /// assignment-expression |
| 168 | /// '{' initializer-list '}' |
| 169 | /// '{' initializer-list ',' '}' |
| 170 | /// [GNU] '{' '}' |
| 171 | /// |
| 172 | /// initializer-list: |
| 173 | /// designation[opt] initializer |
| 174 | /// initializer-list ',' designation[opt] initializer |
| 175 | /// |
| 176 | Parser::ExprResult Parser::ParseInitializer() { |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 177 | if (Tok.isNot(tok::l_brace)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 178 | return ParseAssignmentExpression(); |
| 179 | |
| 180 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 181 | |
| 182 | // We support empty initializers, but tell the user that they aren't using |
| 183 | // C99-clean code. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 184 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 185 | Diag(LBraceLoc, diag::ext_gnu_empty_initializer); |
| 186 | // Match the '}'. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 187 | return Actions.ActOnInitList(LBraceLoc, 0, 0, ConsumeBrace()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 188 | } |
| 189 | llvm::SmallVector<ExprTy*, 8> InitExprs; |
| 190 | bool InitExprsOk = true; |
| 191 | |
| 192 | while (1) { |
| 193 | // Parse: designation[opt] initializer |
| 194 | |
| 195 | // If we know that this cannot be a designation, just parse the nested |
| 196 | // initializer directly. |
| 197 | ExprResult SubElt; |
| 198 | if (!MayBeDesignationStart(Tok.getKind())) |
| 199 | SubElt = ParseInitializer(); |
| 200 | else |
| 201 | SubElt = ParseInitializerWithPotentialDesignator(); |
| 202 | |
| 203 | // If we couldn't parse the subelement, bail out. |
Chris Lattner | e77e03d | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 204 | if (!SubElt.isInvalid) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 205 | InitExprs.push_back(SubElt.Val); |
Chris Lattner | e77e03d | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 206 | } else { |
| 207 | InitExprsOk = false; |
| 208 | |
| 209 | // We have two ways to try to recover from this error: if the code looks |
Chris Lattner | e29ac82 | 2008-10-26 21:46:13 +0000 | [diff] [blame^] | 210 | // gramatically ok (i.e. we have a comma coming up) try to continue |
Chris Lattner | e77e03d | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 211 | // parsing the rest of the initializer. This allows us to emit |
| 212 | // diagnostics for later elements that we find. If we don't see a comma, |
| 213 | // assume there is a parse error, and just skip to recover. |
| 214 | if (Tok.isNot(tok::comma)) { |
| 215 | SkipUntil(tok::r_brace, false, true); |
| 216 | break; |
| 217 | } |
| 218 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 219 | |
| 220 | // If we don't have a comma continued list, we're done. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 221 | if (Tok.isNot(tok::comma)) break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 222 | |
| 223 | // FIXME: save comma locations. |
| 224 | ConsumeToken(); |
| 225 | |
| 226 | // Handle trailing comma. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 227 | if (Tok.is(tok::r_brace)) break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 228 | } |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 229 | if (InitExprsOk && Tok.is(tok::r_brace)) |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 230 | return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 231 | ConsumeBrace()); |
Chris Lattner | e77e03d | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 232 | |
| 233 | // Delete any parsed subexpressions. |
| 234 | for (unsigned i = 0, e = InitExprs.size(); i != e; ++i) |
| 235 | Actions.DeleteExpr(InitExprs[i]); |
| 236 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 237 | // Match the '}'. |
| 238 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 239 | return ExprResult(true); // an error occurred. |
| 240 | } |
| 241 | |