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