Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1 | //===--- Expression.cpp - Expression Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame^] | 10 | // This file implements the Expression parsing implementation. Expressions in |
| 11 | // C99 basically consist of a bunch of binary operators with unary operators and |
| 12 | // other random stuff at the leaves. |
| 13 | // |
| 14 | // In the C99 grammar, these unary operators bind tightest and are represented |
| 15 | // as the 'cast-expression' production. Everything else is either a binary |
| 16 | // operator (e.g. '/') or a trinary operator ("?:"). The unary leaves are |
| 17 | // handled by ParseCastExpression, the higher level pieces are handled by |
| 18 | // ParseBinaryExpression. |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "clang/Parse/Parser.h" |
| 23 | #include "clang/Basic/Diagnostic.h" |
| 24 | using namespace llvm; |
| 25 | using namespace clang; |
| 26 | |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 27 | // C99 6.7.8 |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 28 | Parser::ExprResult Parser::ParseInitializer() { |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 29 | // FIXME: STUB. |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 30 | if (Tok.getKind() == tok::l_brace) { |
| 31 | ConsumeBrace(); |
Chris Lattner | a092cd1f | 2006-08-11 01:38:28 +0000 | [diff] [blame] | 32 | |
| 33 | if (Tok.getKind() == tok::numeric_constant) |
| 34 | ConsumeToken(); |
| 35 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 36 | // FIXME: initializer-list |
| 37 | // Match the '}'. |
| 38 | MatchRHSPunctuation(tok::r_brace, Tok.getLocation(), "{", |
| 39 | diag::err_expected_rbrace); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 40 | return ExprResult(false); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 43 | return ParseAssignmentExpression(); |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
| 47 | |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 48 | Parser::ExprResult Parser::ParseExpression() { |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame^] | 49 | return ParseBinaryExpression(); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 52 | // Expr that doesn't include commas. |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 53 | Parser::ExprResult Parser::ParseAssignmentExpression() { |
| 54 | return ParseExpression(); |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame^] | 57 | /// PrecedenceLevels - These are precedences for the binary/trinary operators in |
| 58 | /// the C99 grammar. These have been named to relate with the C99 grammar |
| 59 | /// productions. Low precedences numbers bind more weakly than high numbers. |
| 60 | namespace prec { |
| 61 | enum Level { |
| 62 | Unknown = 0, // Not binary operator. |
| 63 | Comma = 1, // , |
| 64 | Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= |
| 65 | Conditional = 3, // ? |
| 66 | LogicalOr = 4, // || |
| 67 | LogicalAnd = 5, // && |
| 68 | InclusiveOr = 6, // | |
| 69 | ExclusiveOr = 7, // ^ |
| 70 | And = 8, // & |
| 71 | MinMax = 9, // <?, >? min, max (GCC extensions) |
| 72 | Equality = 10, // ==, != |
| 73 | Relational = 11, // >=, <=, >, < |
| 74 | Shift = 12, // <<, >> |
| 75 | Additive = 13, // -, + |
| 76 | Multiplicative = 14 // *, /, % |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /// getBinOpPrecedence - Return the precedence of the specified binary operator |
| 82 | /// token. This returns: |
| 83 | /// |
| 84 | static prec::Level getBinOpPrecedence(tok::TokenKind Kind) { |
| 85 | switch (Kind) { |
| 86 | default: return prec::Unknown; |
| 87 | case tok::comma: return prec::Comma; |
| 88 | case tok::equal: |
| 89 | case tok::starequal: |
| 90 | case tok::slashequal: |
| 91 | case tok::percentequal: |
| 92 | case tok::plusequal: |
| 93 | case tok::minusequal: |
| 94 | case tok::lesslessequal: |
| 95 | case tok::greatergreaterequal: |
| 96 | case tok::ampequal: |
| 97 | case tok::caretequal: |
| 98 | case tok::pipeequal: return prec::Assignment; |
| 99 | case tok::question: return prec::Conditional; |
| 100 | case tok::pipepipe: return prec::LogicalOr; |
| 101 | case tok::ampamp: return prec::LogicalAnd; |
| 102 | case tok::pipe: return prec::InclusiveOr; |
| 103 | case tok::caret: return prec::ExclusiveOr; |
| 104 | case tok::amp: return prec::And; |
| 105 | case tok::lessquestion: |
| 106 | case tok::greaterquestion: return prec::MinMax; |
| 107 | case tok::exclaimequal: |
| 108 | case tok::equalequal: return prec::Equality; |
| 109 | case tok::lessequal: |
| 110 | case tok::less: |
| 111 | case tok::greaterequal: |
| 112 | case tok::greater: return prec::Relational; |
| 113 | case tok::lessless: |
| 114 | case tok::greatergreater: return prec::Shift; |
| 115 | case tok::plus: |
| 116 | case tok::minus: return prec::Additive; |
| 117 | case tok::percent: |
| 118 | case tok::slash: |
| 119 | case tok::star: return prec::Multiplicative; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /// ParseBinaryExpression - Simple precedence-based parser for binary/trinary |
| 125 | /// operators. |
| 126 | /// |
| 127 | /// multiplicative-expression: [C99 6.5.5] |
| 128 | /// cast-expression |
| 129 | /// multiplicative-expression '*' cast-expression |
| 130 | /// multiplicative-expression '/' cast-expression |
| 131 | /// multiplicative-expression '%' cast-expression |
| 132 | /// |
| 133 | /// additive-expression: [C99 6.5.6] |
| 134 | /// multiplicative-expression |
| 135 | /// additive-expression '+' multiplicative-expression |
| 136 | /// additive-expression '-' multiplicative-expression |
| 137 | /// |
| 138 | /// shift-expression: [C99 6.5.7] |
| 139 | /// additive-expression |
| 140 | /// shift-expression '<<' additive-expression |
| 141 | /// shift-expression '>>' additive-expression |
| 142 | /// |
| 143 | /// relational-expression: [C99 6.5.8] |
| 144 | /// shift-expression |
| 145 | /// relational-expression '<' shift-expression |
| 146 | /// relational-expression '>' shift-expression |
| 147 | /// relational-expression '<=' shift-expression |
| 148 | /// relational-expression '>=' shift-expression |
| 149 | /// |
| 150 | /// equality-expression: [C99 6.5.9] |
| 151 | /// relational-expression |
| 152 | /// equality-expression '==' relational-expression |
| 153 | /// equality-expression '!=' relational-expression |
| 154 | /// |
| 155 | /// AND-expression: [C99 6.5.10] |
| 156 | /// equality-expression |
| 157 | /// AND-expression '&' equality-expression |
| 158 | /// |
| 159 | /// exclusive-OR-expression: [C99 6.5.11] |
| 160 | /// AND-expression |
| 161 | /// exclusive-OR-expression '^' AND-expression |
| 162 | /// |
| 163 | /// inclusive-OR-expression: [C99 6.5.12] |
| 164 | /// exclusive-OR-expression |
| 165 | /// inclusive-OR-expression '|' exclusive-OR-expression |
| 166 | /// |
| 167 | /// logical-AND-expression: [C99 6.5.13] |
| 168 | /// inclusive-OR-expression |
| 169 | /// logical-AND-expression '&&' inclusive-OR-expression |
| 170 | /// |
| 171 | /// logical-OR-expression: [C99 6.5.14] |
| 172 | /// logical-AND-expression |
| 173 | /// logical-OR-expression '||' logical-AND-expression |
| 174 | /// |
| 175 | /// conditional-expression: [C99 6.5.15] |
| 176 | /// logical-OR-expression |
| 177 | /// logical-OR-expression '?' expression ':' conditional-expression |
| 178 | /// [GNU] logical-OR-expression '?' ':' conditional-expression |
| 179 | /// |
| 180 | /// assignment-expression: [C99 6.5.16] |
| 181 | /// conditional-expression |
| 182 | /// unary-expression assignment-operator assignment-expression |
| 183 | /// |
| 184 | /// assignment-operator: one of |
| 185 | /// = *= /= %= += -= <<= >>= &= ^= |= |
| 186 | /// |
| 187 | /// expression: [C99 6.5.17] |
| 188 | /// assignment-expression |
| 189 | /// expression ',' assignment-expression |
| 190 | /// |
| 191 | Parser::ExprResult Parser::ParseBinaryExpression() { |
| 192 | ExprResult LHS = ParseCastExpression(false); |
| 193 | if (LHS.isInvalid) return LHS; |
| 194 | |
| 195 | return ParseRHSOfBinaryExpression(LHS, prec::Comma); |
| 196 | } |
| 197 | |
| 198 | /// ParseRHSOfBinaryExpression - Parse a binary expression that starts with |
| 199 | /// LHS and has a precedence of at least MinPrec. |
| 200 | Parser::ExprResult |
| 201 | Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) { |
| 202 | unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 203 | |
| 204 | while (1) { |
| 205 | // If this token has a lower precedence than we are allowed to parse (e.g. |
| 206 | // because we are called recursively, or because the token is not a binop), |
| 207 | // then we are done! |
| 208 | if (NextTokPrec < MinPrec) |
| 209 | return LHS; |
| 210 | |
| 211 | // Consume the operator, saving the operator token for error reporting. |
| 212 | LexerToken OpToken = Tok; |
| 213 | ConsumeToken(); |
| 214 | |
| 215 | // Parse the RHS of the operator. |
| 216 | ExprResult RHS; |
| 217 | |
| 218 | // Special case handling of "X ? Y : Z" were Y is empty. This is a GCC |
| 219 | // extension. |
| 220 | if (OpToken.getKind() != tok::question || Tok.getKind() != tok::colon) { |
| 221 | RHS = ParseCastExpression(false); |
| 222 | if (RHS.isInvalid) return RHS; |
| 223 | } else { |
| 224 | RHS = ExprResult(false); |
| 225 | Diag(Tok, diag::ext_gnu_conditional_expr); |
| 226 | } |
| 227 | |
| 228 | // Remember the precedence of this operator and get the precedence of the |
| 229 | // operator immediately to the right of the RHS. |
| 230 | unsigned ThisPrec = NextTokPrec; |
| 231 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 232 | |
| 233 | // FIXME: ASSIGNMENT IS RIGHT ASSOCIATIVE. |
| 234 | // FIXME: do we want to handle assignment here?? |
| 235 | // ASSIGNMENT: Parse LHS as conditional expr, then catch errors in semantic |
| 236 | // analysis. |
| 237 | bool isRightAssoc = OpToken.getKind() == tok::question; |
| 238 | |
| 239 | // Get the precedence of the operator to the right of the RHS. If it binds |
| 240 | // more tightly with RHS than we do, evaluate it completely first. |
| 241 | |
| 242 | // FIXME: Is this enough for '?:' operators? The second term is suppsed to |
| 243 | // be 'expression', not 'assignment'. |
| 244 | if (ThisPrec < NextTokPrec || |
| 245 | (ThisPrec == NextTokPrec && isRightAssoc)) { |
| 246 | RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec+1); |
| 247 | if (RHS.isInvalid) return RHS; |
| 248 | |
| 249 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 250 | } |
| 251 | assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); |
| 252 | |
| 253 | // Handle the special case of our one trinary operator here. |
| 254 | if (OpToken.getKind() == tok::question) { |
| 255 | if (Tok.getKind() != tok::colon) { |
| 256 | Diag(Tok, diag::err_expected_colon); |
| 257 | Diag(OpToken, diag::err_matching, "?"); |
| 258 | return ExprResult(true); |
| 259 | } |
| 260 | |
| 261 | // Eat the colon. |
| 262 | ConsumeToken(); |
| 263 | |
| 264 | // Parse the value of the colon. |
| 265 | ExprResult AfterColonVal = ParseCastExpression(false); |
| 266 | if (AfterColonVal.isInvalid) return AfterColonVal; |
| 267 | |
| 268 | // Parse anything after the RRHS that has a higher precedence than ?. |
| 269 | AfterColonVal = ParseRHSOfBinaryExpression(AfterColonVal, ThisPrec+1); |
| 270 | if (AfterColonVal.isInvalid) return AfterColonVal; |
| 271 | |
| 272 | // TODO: Combine LHS = LHS ? RHS : AfterColonVal. |
| 273 | |
| 274 | // Figure out the precedence of the token after the : part. |
| 275 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 276 | } else { |
| 277 | // TODO: combine the LHS and RHS into the LHS (e.g. build AST). |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | |
Chris Lattner | eaf0659 | 2006-08-11 02:02:23 +0000 | [diff] [blame] | 283 | /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is |
| 284 | /// true, parse a unary-expression. |
| 285 | /// |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 286 | /// cast-expression: [C99 6.5.4] |
| 287 | /// unary-expression |
| 288 | /// '(' type-name ')' cast-expression |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 289 | /// |
Chris Lattner | c2dd85a | 2006-08-10 22:57:16 +0000 | [diff] [blame] | 290 | /// unary-expression: [C99 6.5.3] |
| 291 | /// postfix-expression |
| 292 | /// '++' unary-expression |
| 293 | /// '--' unary-expression |
| 294 | /// unary-operator cast-expression |
| 295 | /// 'sizeof' unary-expression |
| 296 | /// 'sizeof' '(' type-name ')' |
| 297 | /// [GNU] '__alignof' unary-expression |
| 298 | /// [GNU] '__alignof' '(' type-name ')' |
| 299 | /// [GNU] '&&' identifier |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 300 | /// |
Chris Lattner | c2dd85a | 2006-08-10 22:57:16 +0000 | [diff] [blame] | 301 | /// unary-operator: one of |
| 302 | /// '&' '*' '+' '-' '~' '!' |
| 303 | /// [GNU] '__extension__' '__real' '__imag' |
| 304 | /// |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 305 | /// postfix-expression: [C99 6.5.2] |
| 306 | /// primary-expression |
| 307 | /// postfix-expression '[' expression ']' |
| 308 | /// postfix-expression '(' argument-expression-list[opt] ')' |
| 309 | /// postfix-expression '.' identifier |
| 310 | /// postfix-expression '->' identifier |
| 311 | /// postfix-expression '++' |
| 312 | /// postfix-expression '--' |
| 313 | /// '(' type-name ')' '{' initializer-list '}' |
| 314 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 315 | /// |
| 316 | /// argument-expression-list: [C99 6.5.2] |
| 317 | /// argument-expression |
| 318 | /// argument-expression-list ',' argument-expression |
| 319 | /// |
| 320 | /// primary-expression: [C99 6.5.1] |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 321 | /// identifier |
| 322 | /// constant |
| 323 | /// string-literal |
| 324 | /// '(' expression ')' |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 325 | /// '__func__' [C99 6.4.2.2] |
| 326 | /// [GNU] '__FUNCTION__' |
| 327 | /// [GNU] '__PRETTY_FUNCTION__' |
| 328 | /// [GNU] '(' compound-statement ')' |
| 329 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 330 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 331 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 332 | /// assign-expr ')' |
| 333 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
| 334 | /// [OBC] '[' objc-receiver objc-message-args ']' [TODO] |
| 335 | /// [OBC] '@selector' '(' objc-selector-arg ')' [TODO] |
| 336 | /// [OBC] '@protocol' '(' identifier ')' [TODO] |
| 337 | /// [OBC] '@encode' '(' type-name ')' [TODO] |
| 338 | /// [OBC] objc-string-literal [TODO] |
| 339 | /// |
| 340 | /// constant: [C99 6.4.4] |
| 341 | /// integer-constant |
| 342 | /// floating-constant |
| 343 | /// enumeration-constant -> identifier |
| 344 | /// character-constant |
| 345 | /// |
| 346 | /// [GNU] offsetof-member-designator: |
| 347 | /// [GNU] identifier |
| 348 | /// [GNU] offsetof-member-designator '.' identifier |
| 349 | /// [GNU] offsetof-member-designator '[' expression ']' |
| 350 | /// |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 351 | /// |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 352 | Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) { |
| 353 | ExprResult Res; |
| 354 | |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 355 | // This handles all of cast-expression, unary-expression, postfix-expression, |
| 356 | // and primary-expression. We handle them together like this for efficiency |
| 357 | // and to simplify handling of an expression starting with a '(' token: which |
| 358 | // may be one of a parenthesized expression, cast-expression, compound literal |
| 359 | // expression, or statement expression. |
| 360 | // |
| 361 | // If the parsed tokens consist of a primary-expression, the cases below |
| 362 | // 'break' out of the switch. This allows the postfix expression pieces to |
| 363 | // be applied to them. Cases that cannot be followed by postfix exprs should |
| 364 | // return instead. |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 365 | switch (Tok.getKind()) { |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 366 | case tok::l_paren: |
| 367 | // If this expression is limited to being a unary-expression, the parent can |
| 368 | // not start a cast expression. |
| 369 | ParenParseOption ParenExprType = |
| 370 | isUnaryExpression ? CompoundLiteral : CastExpr; |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 371 | Res = ParseParenExpression(ParenExprType); |
| 372 | if (Res.isInvalid) return Res; |
| 373 | |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 374 | switch (ParenExprType) { |
| 375 | case SimpleExpr: break; // Nothing else to do. |
| 376 | case CompoundStmt: break; // Nothing else to do. |
| 377 | case CompoundLiteral: |
| 378 | // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of |
| 379 | // postfix-expression exist, parse them now. |
| 380 | break; |
| 381 | case CastExpr: |
| 382 | // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse |
| 383 | // the cast-expression that follows it next. |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 384 | return ParseCastExpression(false); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 385 | } |
| 386 | break; // These can be followed by postfix-expr pieces. |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 387 | |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 388 | // primary-expression |
| 389 | case tok::identifier: // primary-expression: identifier |
| 390 | // constant: enumeration-constant |
| 391 | case tok::numeric_constant: // constant: integer-constant |
| 392 | // constant: floating-constant |
| 393 | case tok::char_constant: // constant: character-constant |
| 394 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 395 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 396 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
| 397 | ConsumeToken(); |
| 398 | break; |
| 399 | case tok::string_literal: // primary-expression: string-literal |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 400 | Res = ParseStringLiteralExpression(); |
| 401 | if (Res.isInvalid) return Res; |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 402 | break; |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 403 | case tok::kw___builtin_va_arg: |
| 404 | case tok::kw___builtin_offsetof: |
| 405 | case tok::kw___builtin_choose_expr: |
| 406 | case tok::kw___builtin_types_compatible_p: |
| 407 | assert(0 && "FIXME: UNIMP!"); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 408 | case tok::plusplus: // unary-expression: '++' unary-expression |
| 409 | case tok::minusminus: // unary-expression: '--' unary-expression |
| 410 | ConsumeToken(); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 411 | return ParseCastExpression(true); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 412 | case tok::amp: // unary-expression: '&' cast-expression |
| 413 | case tok::star: // unary-expression: '*' cast-expression |
| 414 | case tok::plus: // unary-expression: '+' cast-expression |
| 415 | case tok::minus: // unary-expression: '-' cast-expression |
| 416 | case tok::tilde: // unary-expression: '~' cast-expression |
| 417 | case tok::exclaim: // unary-expression: '!' cast-expression |
| 418 | case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] |
| 419 | case tok::kw___imag: // unary-expression: '__real' cast-expression [GNU] |
| 420 | //case tok::kw__extension__: [TODO] |
| 421 | ConsumeToken(); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 422 | return ParseCastExpression(false); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 423 | |
| 424 | case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression |
| 425 | // unary-expression: 'sizeof' '(' type-name ')' |
| 426 | case tok::kw___alignof: // unary-expression: '__alignof' unary-expression |
| 427 | // unary-expression: '__alignof' '(' type-name ')' |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 428 | return ParseSizeofAlignofExpression(); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 429 | case tok::ampamp: // unary-expression: '&&' identifier |
| 430 | Diag(Tok, diag::ext_gnu_address_of_label); |
| 431 | ConsumeToken(); |
| 432 | if (Tok.getKind() == tok::identifier) { |
| 433 | ConsumeToken(); |
| 434 | } else { |
| 435 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 436 | return ExprResult(true); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 437 | } |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 438 | return ExprResult(false); |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 439 | default: |
| 440 | Diag(Tok, diag::err_expected_expression); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 441 | return ExprResult(true); |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // Now that the primary-expression piece of the postfix-expression has been |
| 445 | // parsed, see if there are any postfix-expression pieces here. |
| 446 | SourceLocation Loc; |
| 447 | while (1) { |
| 448 | switch (Tok.getKind()) { |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 449 | default: |
| 450 | return ExprResult(false); |
| 451 | case tok::l_square: // postfix-expression: p-e '[' expression ']' |
| 452 | Loc = Tok.getLocation(); |
| 453 | ConsumeBracket(); |
| 454 | ParseExpression(); |
| 455 | // Match the ']'. |
| 456 | MatchRHSPunctuation(tok::r_square, Loc, "[", diag::err_expected_rsquare); |
| 457 | break; |
| 458 | |
| 459 | case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')' |
| 460 | Loc = Tok.getLocation(); |
| 461 | ConsumeParen(); |
| 462 | |
| 463 | while (1) { |
| 464 | // FIXME: This should be argument-expression! |
| 465 | ParseAssignmentExpression(); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 466 | |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 467 | if (Tok.getKind() != tok::comma) |
| 468 | break; |
| 469 | ConsumeToken(); // Next argument. |
| 470 | } |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 471 | |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 472 | // Match the ')'. |
| 473 | MatchRHSPunctuation(tok::r_paren, Loc, "(", diag::err_expected_rparen); |
| 474 | break; |
| 475 | |
| 476 | case tok::arrow: // postfix-expression: p-e '->' identifier |
| 477 | case tok::period: // postfix-expression: p-e '.' identifier |
| 478 | ConsumeToken(); |
| 479 | if (Tok.getKind() != tok::identifier) { |
| 480 | Diag(Tok, diag::err_expected_ident); |
| 481 | return ExprResult(true); |
| 482 | } |
| 483 | ConsumeToken(); |
| 484 | break; |
| 485 | |
| 486 | case tok::plusplus: // postfix-expression: postfix-expression '++' |
| 487 | case tok::minusminus: // postfix-expression: postfix-expression '--' |
| 488 | ConsumeToken(); |
| 489 | break; |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 494 | /// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression. |
| 495 | /// unary-expression: [C99 6.5.3] |
| 496 | /// 'sizeof' unary-expression |
| 497 | /// 'sizeof' '(' type-name ')' |
| 498 | /// [GNU] '__alignof' unary-expression |
| 499 | /// [GNU] '__alignof' '(' type-name ')' |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 500 | Parser::ExprResult Parser::ParseSizeofAlignofExpression() { |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 501 | assert((Tok.getKind() == tok::kw_sizeof || |
| 502 | Tok.getKind() == tok::kw___alignof) && |
| 503 | "Not a sizeof/alignof expression!"); |
| 504 | ConsumeToken(); |
| 505 | |
| 506 | // If the operand doesn't start with an '(', it must be an expression. |
| 507 | if (Tok.getKind() != tok::l_paren) { |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 508 | return ParseCastExpression(true); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | // If it starts with a '(', we know that it is either a parenthesized |
| 512 | // type-name, or it is a unary-expression that starts with a compound literal, |
| 513 | // or starts with a primary-expression that is a parenthesized expression. |
| 514 | ParenParseOption ExprType = CastExpr; |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 515 | return ParseParenExpression(ExprType); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 518 | /// ParseStringLiteralExpression - This handles the various token types that |
| 519 | /// form string literals, and also handles string concatenation [C99 5.1.1.2, |
| 520 | /// translation phase #6]. |
| 521 | /// |
| 522 | /// primary-expression: [C99 6.5.1] |
| 523 | /// string-literal |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 524 | Parser::ExprResult Parser::ParseStringLiteralExpression() { |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 525 | assert(isTokenStringLiteral() && "Not a string literal!"); |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 526 | ConsumeStringToken(); |
| 527 | |
| 528 | // String concat. Note that keywords like __func__ and __FUNCTION__ aren't |
| 529 | // considered to be strings. |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 530 | while (isTokenStringLiteral()) |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 531 | ConsumeStringToken(); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 532 | return ExprResult(false); |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 533 | } |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 534 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 535 | |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 536 | /// ParseParenExpression - This parses the unit that starts with a '(' token, |
| 537 | /// based on what is allowed by ExprType. The actual thing parsed is returned |
| 538 | /// in ExprType. |
| 539 | /// |
| 540 | /// primary-expression: [C99 6.5.1] |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 541 | /// '(' expression ')' |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 542 | /// [GNU] '(' compound-statement ')' (if !ParenExprOnly) |
| 543 | /// postfix-expression: [C99 6.5.2] |
| 544 | /// '(' type-name ')' '{' initializer-list '}' |
| 545 | /// '(' type-name ')' '{' initializer-list ',' '}' |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 546 | /// cast-expression: [C99 6.5.4] |
| 547 | /// '(' type-name ')' cast-expression |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 548 | /// |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 549 | Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) { |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 550 | assert(Tok.getKind() == tok::l_paren && "Not a paren expr!"); |
| 551 | SourceLocation OpenLoc = Tok.getLocation(); |
| 552 | ConsumeParen(); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 553 | ExprResult Result(false); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 555 | if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace && |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 556 | !getLang().NoExtensions) { |
| 557 | Diag(Tok, diag::ext_gnu_statement_expr); |
| 558 | ParseCompoundStatement(); |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 559 | ExprType = CompoundStmt; |
| 560 | } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 561 | // Otherwise, this is a compound expression. |
| 562 | ParseTypeName(); |
| 563 | |
| 564 | // Match the ')'. |
| 565 | MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen); |
| 566 | |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 567 | if (Tok.getKind() == tok::l_brace) { |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 568 | Result = ParseInitializer(); |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 569 | ExprType = CompoundLiteral; |
| 570 | } else if (ExprType == CastExpr) { |
| 571 | // Note that this doesn't parse the subsequence cast-expression. |
| 572 | ExprType = CastExpr; |
| 573 | } else { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 574 | Diag(Tok, diag::err_expected_lbrace_in_compound_literal); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 575 | return ExprResult(true); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 576 | } |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 577 | return Result; |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 578 | } else { |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 579 | Result = ParseExpression(); |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 580 | ExprType = SimpleExpr; |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 581 | } |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 582 | |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 583 | // Match the ')'. |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 584 | if (Result.isInvalid) |
| 585 | SkipUntil(tok::r_paren); |
| 586 | else |
| 587 | MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen); |
| 588 | return Result; |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 589 | } |