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