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