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 | 23c12ef | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 24 | static bool MayBeDesignationStart(tok::TokenKind K, Preprocessor &PP) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 23c12ef | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 29 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 30 | case tok::identifier: // designation: identifier ':' |
Chris Lattner | 23c12ef | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 31 | return PP.LookAhead(0).is(tok::colon); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | e29ac82 | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 58 | /// |
Chris Lattner | bc9e95d | 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 | 23c12ef | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 68 | Diag(Tok, diag::ext_gnu_old_style_field_designator); |
Chris Lattner | bc9e95d | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 23c12ef | 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 | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 74 | assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!"); |
Chris Lattner | 23c12ef | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 75 | ConsumeToken(); |
| 76 | return ParseInitializer(); |
Chris Lattner | bc9e95d | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Chris Lattner | 1aee232 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 79 | // Desig - This is initialized when we see our first designator. We may have |
| 80 | // an objc message send with no designator, so we don't want to create this |
| 81 | // eagerly. |
| 82 | Designation *Desig = 0; |
| 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. |
Chris Lattner | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 85 | while (Tok.is(tok::period) || Tok.is(tok::l_square)) { |
| 86 | if (Tok.is(tok::period)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 87 | // designator: '.' identifier |
| 88 | ConsumeToken(); |
Chris Lattner | 1aee232 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 89 | |
| 90 | // Create designation if we haven't already. |
| 91 | if (Desig == 0) |
| 92 | Desig = &Designations.CreateDesignation(InitNum); |
| 93 | |
| 94 | if (Tok.isNot(tok::identifier)) { |
| 95 | Diag(Tok.getLocation(), diag::err_expected_field_designator); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 96 | return ExprResult(true); |
Chris Lattner | 1aee232 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | Desig->AddDesignator(Designator::getField(Tok.getIdentifierInfo())); |
| 100 | ConsumeToken(); // Eat the identifier. |
Chris Lattner | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 101 | continue; |
| 102 | } |
| 103 | |
| 104 | // We must have either an array designator now or an objc message send. |
| 105 | assert(Tok.is(tok::l_square) && "Unexpected token!"); |
| 106 | |
Chris Lattner | 9c665cf | 2008-10-26 23:06:54 +0000 | [diff] [blame^] | 107 | // Handle the two forms of array designator: |
| 108 | // array-designator: '[' constant-expression ']' |
| 109 | // array-designator: '[' constant-expression '...' constant-expression ']' |
| 110 | // |
| 111 | // Also, we have to handle the case where the expression after the |
| 112 | // designator an an objc message send: '[' objc-message-expr ']'. |
| 113 | // Interesting cases are: |
| 114 | // [foo bar] -> objc message send |
| 115 | // [foo] -> array designator |
| 116 | // [foo ... bar] -> array designator |
| 117 | // [4][foo bar] -> obsolete GNU designation with objc message send. |
| 118 | // |
Chris Lattner | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 119 | SourceLocation StartLoc = ConsumeBracket(); |
| 120 | |
| 121 | // If Objective-C is enabled and this is a typename or other identifier |
| 122 | // receiver, parse this as a message send expression. |
| 123 | if (getLang().ObjC1 && isTokObjCMessageIdentifierReceiver()) { |
| 124 | // FIXME: Emit ext_gnu_missing_equal_designator for inits like |
| 125 | // [4][foo bar]. |
| 126 | IdentifierInfo *Name = Tok.getIdentifierInfo(); |
| 127 | ConsumeToken(); |
| 128 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, Name, 0); |
| 129 | } |
| 130 | |
| 131 | // Note that we parse this as an assignment expression, not a constant |
| 132 | // expression (allowing *=, =, etc) to handle the objc case. Sema needs |
| 133 | // to validate that the expression is a constant. |
| 134 | ExprResult Idx = ParseAssignmentExpression(); |
| 135 | if (Idx.isInvalid) { |
| 136 | SkipUntil(tok::r_square); |
| 137 | return Idx; |
| 138 | } |
| 139 | |
| 140 | // Given an expression, we could either have a designator (if the next |
| 141 | // tokens are '...' or ']' or an objc message send. If this is an objc |
| 142 | // message send, handle it now. An objc-message send is the start of |
| 143 | // an assignment-expression production. |
| 144 | if (getLang().ObjC1 && Tok.isNot(tok::ellipsis) && |
| 145 | Tok.isNot(tok::r_square)) { |
| 146 | // FIXME: Emit ext_gnu_missing_equal_designator for inits like |
| 147 | // [4][foo bar]. |
| 148 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, 0,Idx.Val); |
| 149 | } |
Chris Lattner | 9c665cf | 2008-10-26 23:06:54 +0000 | [diff] [blame^] | 150 | |
| 151 | // Create designation if we haven't already. |
| 152 | if (Desig == 0) |
| 153 | Desig = &Designations.CreateDesignation(InitNum); |
Chris Lattner | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 9c665cf | 2008-10-26 23:06:54 +0000 | [diff] [blame^] | 155 | // If this is a normal array designator, remember it. |
| 156 | if (Tok.isNot(tok::ellipsis)) { |
| 157 | Desig->AddDesignator(Designator::getArray(Idx.Val)); |
| 158 | } else { |
| 159 | // Handle the gnu array range extension. |
Chris Lattner | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 160 | Diag(Tok, diag::ext_gnu_array_range); |
| 161 | ConsumeToken(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 163 | ExprResult RHS = ParseConstantExpression(); |
| 164 | if (RHS.isInvalid) { |
Chris Lattner | 9c665cf | 2008-10-26 23:06:54 +0000 | [diff] [blame^] | 165 | Actions.DeleteExpr(Idx.Val); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 166 | SkipUntil(tok::r_square); |
Chris Lattner | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 167 | return RHS; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 168 | } |
Chris Lattner | 9c665cf | 2008-10-26 23:06:54 +0000 | [diff] [blame^] | 169 | Desig->AddDesignator(Designator::getArrayRange(Idx.Val, RHS.Val)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 170 | } |
Chris Lattner | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 171 | |
| 172 | MatchRHSPunctuation(tok::r_square, StartLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 173 | } |
Chris Lattner | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 1aee232 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 175 | // Okay, we're done with the designator sequence. We know that there must be |
| 176 | // at least one designator, because the only case we can get into this method |
| 177 | // without a designator is when we have an objc message send. That case is |
| 178 | // handled and returned from above. |
| 179 | |
| 180 | // Handle a normal designator sequence end, which is an equal. |
Chris Lattner | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 181 | if (Tok.is(tok::equal)) { |
Chris Lattner | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 182 | ConsumeToken(); |
| 183 | return ParseInitializer(); |
| 184 | } |
| 185 | |
Chris Lattner | 1aee232 | 2008-10-26 22:59:19 +0000 | [diff] [blame] | 186 | // We read some number of designators and found something that isn't an = or |
| 187 | // an initializer. If we have exactly one array designator [TODO CHECK], this |
| 188 | // is the GNU 'designation: array-designator' extension. Otherwise, it is a |
| 189 | // parse error. |
Chris Lattner | 620da26 | 2008-10-26 22:49:49 +0000 | [diff] [blame] | 190 | SourceLocation Loc = Tok.getLocation(); |
| 191 | ExprResult Init = ParseInitializer(); |
| 192 | if (Init.isInvalid) return Init; |
| 193 | |
| 194 | Diag(Tok, diag::ext_gnu_missing_equal_designator); |
| 195 | return Init; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
Chris Lattner | 70e9a09 | 2008-10-26 22:38:55 +0000 | [diff] [blame] | 199 | /// ParseBraceInitializer - Called when parsing an initializer that has a |
| 200 | /// leading open brace. |
| 201 | /// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 202 | /// initializer: [C99 6.7.8] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 203 | /// '{' initializer-list '}' |
| 204 | /// '{' initializer-list ',' '}' |
| 205 | /// [GNU] '{' '}' |
| 206 | /// |
| 207 | /// initializer-list: |
| 208 | /// designation[opt] initializer |
| 209 | /// initializer-list ',' designation[opt] initializer |
| 210 | /// |
Chris Lattner | 70e9a09 | 2008-10-26 22:38:55 +0000 | [diff] [blame] | 211 | Parser::ExprResult Parser::ParseBraceInitializer() { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 212 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 213 | |
| 214 | // We support empty initializers, but tell the user that they aren't using |
| 215 | // C99-clean code. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 216 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 217 | Diag(LBraceLoc, diag::ext_gnu_empty_initializer); |
| 218 | // Match the '}'. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 219 | return Actions.ActOnInitList(LBraceLoc, 0, 0, ConsumeBrace()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | bc9e95d | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 221 | |
| 222 | /// InitExprs - This is the actual list of expressions contained in the |
| 223 | /// initializer. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 224 | llvm::SmallVector<ExprTy*, 8> InitExprs; |
Chris Lattner | bc9e95d | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 225 | |
| 226 | /// ExprDesignators - For each initializer, keep track of the designator that |
| 227 | /// was specified for it, if any. |
| 228 | InitListDesignations InitExprDesignations(Actions); |
| 229 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 230 | bool InitExprsOk = true; |
| 231 | |
| 232 | while (1) { |
| 233 | // Parse: designation[opt] initializer |
| 234 | |
| 235 | // If we know that this cannot be a designation, just parse the nested |
| 236 | // initializer directly. |
| 237 | ExprResult SubElt; |
Chris Lattner | 23c12ef | 2008-10-26 22:41:58 +0000 | [diff] [blame] | 238 | if (!MayBeDesignationStart(Tok.getKind(), PP)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 239 | SubElt = ParseInitializer(); |
| 240 | else |
Chris Lattner | bc9e95d | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 241 | SubElt = ParseInitializerWithPotentialDesignator(InitExprDesignations, |
| 242 | InitExprs.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 243 | |
| 244 | // If we couldn't parse the subelement, bail out. |
Chris Lattner | e77e03d | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 245 | if (!SubElt.isInvalid) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 246 | InitExprs.push_back(SubElt.Val); |
Chris Lattner | e77e03d | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 247 | } else { |
| 248 | InitExprsOk = false; |
| 249 | |
| 250 | // 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] | 251 | // 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] | 252 | // parsing the rest of the initializer. This allows us to emit |
| 253 | // diagnostics for later elements that we find. If we don't see a comma, |
| 254 | // assume there is a parse error, and just skip to recover. |
| 255 | if (Tok.isNot(tok::comma)) { |
| 256 | SkipUntil(tok::r_brace, false, true); |
| 257 | break; |
| 258 | } |
| 259 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 260 | |
| 261 | // If we don't have a comma continued list, we're done. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 262 | if (Tok.isNot(tok::comma)) break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 263 | |
Chris Lattner | bc9e95d | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 264 | // TODO: save comma locations if some client cares. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 265 | ConsumeToken(); |
| 266 | |
| 267 | // Handle trailing comma. |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 268 | if (Tok.is(tok::r_brace)) break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 269 | } |
Chris Lattner | 34a01ad | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 270 | if (InitExprsOk && Tok.is(tok::r_brace)) |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 271 | return Actions.ActOnInitList(LBraceLoc, &InitExprs[0], InitExprs.size(), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 272 | ConsumeBrace()); |
Chris Lattner | e77e03d | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 273 | |
Chris Lattner | bc9e95d | 2008-10-26 22:36:07 +0000 | [diff] [blame] | 274 | // On error, delete any parsed subexpressions. |
Chris Lattner | e77e03d | 2008-04-20 19:07:56 +0000 | [diff] [blame] | 275 | for (unsigned i = 0, e = InitExprs.size(); i != e; ++i) |
| 276 | Actions.DeleteExpr(InitExprs[i]); |
| 277 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 278 | // Match the '}'. |
| 279 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 280 | return ExprResult(true); // an error occurred. |
| 281 | } |
| 282 | |