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 |
Chris Lattner | b7f1fc9 | 2006-08-12 16:45:01 +0000 | [diff] [blame] | 16 | // operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 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 | b7f1fc9 | 2006-08-12 16:45:01 +0000 | [diff] [blame] | 57 | /// PrecedenceLevels - These are precedences for the binary/ternary operators in |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 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 | |
Chris Lattner | b7f1fc9 | 2006-08-12 16:45:01 +0000 | [diff] [blame] | 124 | /// ParseBinaryExpression - Simple precedence-based parser for binary/ternary |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 125 | /// operators. |
| 126 | /// |
Chris Lattner | b7f1fc9 | 2006-08-12 16:45:01 +0000 | [diff] [blame] | 127 | /// Note: we diverge from the C99 grammar when parsing the assignment-expression |
| 128 | /// production. C99 specifies that the LHS of an assignment operator should be |
| 129 | /// parsed as a unary-expression, but consistency dictates that it be a |
| 130 | /// conditional-expession. In practice, the important thing here is that the |
| 131 | /// LHS of an assignment has to be an l-value, which productions between |
| 132 | /// unary-expression and conditional-expression don't produce. Because we want |
| 133 | /// consistency, we parse the LHS as a conditional-expression, then check for |
| 134 | /// l-value-ness in semantic analysis stages. |
| 135 | /// |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 136 | /// multiplicative-expression: [C99 6.5.5] |
| 137 | /// cast-expression |
| 138 | /// multiplicative-expression '*' cast-expression |
| 139 | /// multiplicative-expression '/' cast-expression |
| 140 | /// multiplicative-expression '%' cast-expression |
| 141 | /// |
| 142 | /// additive-expression: [C99 6.5.6] |
| 143 | /// multiplicative-expression |
| 144 | /// additive-expression '+' multiplicative-expression |
| 145 | /// additive-expression '-' multiplicative-expression |
| 146 | /// |
| 147 | /// shift-expression: [C99 6.5.7] |
| 148 | /// additive-expression |
| 149 | /// shift-expression '<<' additive-expression |
| 150 | /// shift-expression '>>' additive-expression |
| 151 | /// |
| 152 | /// relational-expression: [C99 6.5.8] |
| 153 | /// shift-expression |
| 154 | /// relational-expression '<' shift-expression |
| 155 | /// relational-expression '>' shift-expression |
| 156 | /// relational-expression '<=' shift-expression |
| 157 | /// relational-expression '>=' shift-expression |
| 158 | /// |
| 159 | /// equality-expression: [C99 6.5.9] |
| 160 | /// relational-expression |
| 161 | /// equality-expression '==' relational-expression |
| 162 | /// equality-expression '!=' relational-expression |
| 163 | /// |
| 164 | /// AND-expression: [C99 6.5.10] |
| 165 | /// equality-expression |
| 166 | /// AND-expression '&' equality-expression |
| 167 | /// |
| 168 | /// exclusive-OR-expression: [C99 6.5.11] |
| 169 | /// AND-expression |
| 170 | /// exclusive-OR-expression '^' AND-expression |
| 171 | /// |
| 172 | /// inclusive-OR-expression: [C99 6.5.12] |
| 173 | /// exclusive-OR-expression |
| 174 | /// inclusive-OR-expression '|' exclusive-OR-expression |
| 175 | /// |
| 176 | /// logical-AND-expression: [C99 6.5.13] |
| 177 | /// inclusive-OR-expression |
| 178 | /// logical-AND-expression '&&' inclusive-OR-expression |
| 179 | /// |
| 180 | /// logical-OR-expression: [C99 6.5.14] |
| 181 | /// logical-AND-expression |
| 182 | /// logical-OR-expression '||' logical-AND-expression |
| 183 | /// |
| 184 | /// conditional-expression: [C99 6.5.15] |
| 185 | /// logical-OR-expression |
| 186 | /// logical-OR-expression '?' expression ':' conditional-expression |
| 187 | /// [GNU] logical-OR-expression '?' ':' conditional-expression |
| 188 | /// |
| 189 | /// assignment-expression: [C99 6.5.16] |
| 190 | /// conditional-expression |
| 191 | /// unary-expression assignment-operator assignment-expression |
| 192 | /// |
| 193 | /// assignment-operator: one of |
| 194 | /// = *= /= %= += -= <<= >>= &= ^= |= |
| 195 | /// |
| 196 | /// expression: [C99 6.5.17] |
| 197 | /// assignment-expression |
| 198 | /// expression ',' assignment-expression |
| 199 | /// |
| 200 | Parser::ExprResult Parser::ParseBinaryExpression() { |
| 201 | ExprResult LHS = ParseCastExpression(false); |
| 202 | if (LHS.isInvalid) return LHS; |
| 203 | |
| 204 | return ParseRHSOfBinaryExpression(LHS, prec::Comma); |
| 205 | } |
| 206 | |
| 207 | /// ParseRHSOfBinaryExpression - Parse a binary expression that starts with |
| 208 | /// LHS and has a precedence of at least MinPrec. |
| 209 | Parser::ExprResult |
| 210 | Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) { |
| 211 | unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 212 | |
| 213 | while (1) { |
| 214 | // If this token has a lower precedence than we are allowed to parse (e.g. |
| 215 | // because we are called recursively, or because the token is not a binop), |
| 216 | // then we are done! |
| 217 | if (NextTokPrec < MinPrec) |
| 218 | return LHS; |
| 219 | |
| 220 | // Consume the operator, saving the operator token for error reporting. |
| 221 | LexerToken OpToken = Tok; |
| 222 | ConsumeToken(); |
| 223 | |
| 224 | // Parse the RHS of the operator. |
| 225 | ExprResult RHS; |
| 226 | |
| 227 | // Special case handling of "X ? Y : Z" were Y is empty. This is a GCC |
| 228 | // extension. |
| 229 | if (OpToken.getKind() != tok::question || Tok.getKind() != tok::colon) { |
| 230 | RHS = ParseCastExpression(false); |
| 231 | if (RHS.isInvalid) return RHS; |
| 232 | } else { |
| 233 | RHS = ExprResult(false); |
| 234 | Diag(Tok, diag::ext_gnu_conditional_expr); |
| 235 | } |
| 236 | |
| 237 | // Remember the precedence of this operator and get the precedence of the |
| 238 | // operator immediately to the right of the RHS. |
| 239 | unsigned ThisPrec = NextTokPrec; |
| 240 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 241 | |
| 242 | // FIXME: ASSIGNMENT IS RIGHT ASSOCIATIVE. |
| 243 | // FIXME: do we want to handle assignment here?? |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 244 | bool isRightAssoc = OpToken.getKind() == tok::question; |
| 245 | |
| 246 | // Get the precedence of the operator to the right of the RHS. If it binds |
| 247 | // more tightly with RHS than we do, evaluate it completely first. |
| 248 | |
| 249 | // FIXME: Is this enough for '?:' operators? The second term is suppsed to |
| 250 | // be 'expression', not 'assignment'. |
| 251 | if (ThisPrec < NextTokPrec || |
| 252 | (ThisPrec == NextTokPrec && isRightAssoc)) { |
| 253 | RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec+1); |
| 254 | if (RHS.isInvalid) return RHS; |
| 255 | |
| 256 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 257 | } |
| 258 | assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); |
| 259 | |
Chris Lattner | b7f1fc9 | 2006-08-12 16:45:01 +0000 | [diff] [blame] | 260 | // Handle the special case of our one ternary operator here. |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 261 | if (OpToken.getKind() == tok::question) { |
| 262 | if (Tok.getKind() != tok::colon) { |
| 263 | Diag(Tok, diag::err_expected_colon); |
| 264 | Diag(OpToken, diag::err_matching, "?"); |
| 265 | return ExprResult(true); |
| 266 | } |
| 267 | |
| 268 | // Eat the colon. |
| 269 | ConsumeToken(); |
| 270 | |
| 271 | // Parse the value of the colon. |
| 272 | ExprResult AfterColonVal = ParseCastExpression(false); |
| 273 | if (AfterColonVal.isInvalid) return AfterColonVal; |
| 274 | |
| 275 | // Parse anything after the RRHS that has a higher precedence than ?. |
| 276 | AfterColonVal = ParseRHSOfBinaryExpression(AfterColonVal, ThisPrec+1); |
| 277 | if (AfterColonVal.isInvalid) return AfterColonVal; |
| 278 | |
| 279 | // TODO: Combine LHS = LHS ? RHS : AfterColonVal. |
| 280 | |
| 281 | // Figure out the precedence of the token after the : part. |
| 282 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 283 | } else { |
| 284 | // TODO: combine the LHS and RHS into the LHS (e.g. build AST). |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | |
Chris Lattner | eaf0659 | 2006-08-11 02:02:23 +0000 | [diff] [blame] | 290 | /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is |
| 291 | /// true, parse a unary-expression. |
| 292 | /// |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 293 | /// cast-expression: [C99 6.5.4] |
| 294 | /// unary-expression |
| 295 | /// '(' type-name ')' cast-expression |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 296 | /// |
Chris Lattner | c2dd85a | 2006-08-10 22:57:16 +0000 | [diff] [blame] | 297 | /// unary-expression: [C99 6.5.3] |
| 298 | /// postfix-expression |
| 299 | /// '++' unary-expression |
| 300 | /// '--' unary-expression |
| 301 | /// unary-operator cast-expression |
| 302 | /// 'sizeof' unary-expression |
| 303 | /// 'sizeof' '(' type-name ')' |
| 304 | /// [GNU] '__alignof' unary-expression |
| 305 | /// [GNU] '__alignof' '(' type-name ')' |
| 306 | /// [GNU] '&&' identifier |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 307 | /// |
Chris Lattner | c2dd85a | 2006-08-10 22:57:16 +0000 | [diff] [blame] | 308 | /// unary-operator: one of |
| 309 | /// '&' '*' '+' '-' '~' '!' |
| 310 | /// [GNU] '__extension__' '__real' '__imag' |
| 311 | /// |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 312 | /// postfix-expression: [C99 6.5.2] |
| 313 | /// primary-expression |
| 314 | /// postfix-expression '[' expression ']' |
| 315 | /// postfix-expression '(' argument-expression-list[opt] ')' |
| 316 | /// postfix-expression '.' identifier |
| 317 | /// postfix-expression '->' identifier |
| 318 | /// postfix-expression '++' |
| 319 | /// postfix-expression '--' |
| 320 | /// '(' type-name ')' '{' initializer-list '}' |
| 321 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 322 | /// |
| 323 | /// argument-expression-list: [C99 6.5.2] |
| 324 | /// argument-expression |
| 325 | /// argument-expression-list ',' argument-expression |
| 326 | /// |
| 327 | /// primary-expression: [C99 6.5.1] |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 328 | /// identifier |
| 329 | /// constant |
| 330 | /// string-literal |
| 331 | /// '(' expression ')' |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 332 | /// '__func__' [C99 6.4.2.2] |
| 333 | /// [GNU] '__FUNCTION__' |
| 334 | /// [GNU] '__PRETTY_FUNCTION__' |
| 335 | /// [GNU] '(' compound-statement ')' |
| 336 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 337 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 338 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 339 | /// assign-expr ')' |
| 340 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
| 341 | /// [OBC] '[' objc-receiver objc-message-args ']' [TODO] |
| 342 | /// [OBC] '@selector' '(' objc-selector-arg ')' [TODO] |
| 343 | /// [OBC] '@protocol' '(' identifier ')' [TODO] |
| 344 | /// [OBC] '@encode' '(' type-name ')' [TODO] |
| 345 | /// [OBC] objc-string-literal [TODO] |
| 346 | /// |
| 347 | /// constant: [C99 6.4.4] |
| 348 | /// integer-constant |
| 349 | /// floating-constant |
| 350 | /// enumeration-constant -> identifier |
| 351 | /// character-constant |
| 352 | /// |
| 353 | /// [GNU] offsetof-member-designator: |
| 354 | /// [GNU] identifier |
| 355 | /// [GNU] offsetof-member-designator '.' identifier |
| 356 | /// [GNU] offsetof-member-designator '[' expression ']' |
| 357 | /// |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 358 | /// |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 359 | Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) { |
| 360 | ExprResult Res; |
| 361 | |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 362 | // This handles all of cast-expression, unary-expression, postfix-expression, |
| 363 | // and primary-expression. We handle them together like this for efficiency |
| 364 | // and to simplify handling of an expression starting with a '(' token: which |
| 365 | // may be one of a parenthesized expression, cast-expression, compound literal |
| 366 | // expression, or statement expression. |
| 367 | // |
| 368 | // If the parsed tokens consist of a primary-expression, the cases below |
| 369 | // 'break' out of the switch. This allows the postfix expression pieces to |
| 370 | // be applied to them. Cases that cannot be followed by postfix exprs should |
| 371 | // return instead. |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 372 | switch (Tok.getKind()) { |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 373 | case tok::l_paren: |
| 374 | // If this expression is limited to being a unary-expression, the parent can |
| 375 | // not start a cast expression. |
| 376 | ParenParseOption ParenExprType = |
| 377 | isUnaryExpression ? CompoundLiteral : CastExpr; |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 378 | Res = ParseParenExpression(ParenExprType); |
| 379 | if (Res.isInvalid) return Res; |
| 380 | |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 381 | switch (ParenExprType) { |
| 382 | case SimpleExpr: break; // Nothing else to do. |
| 383 | case CompoundStmt: break; // Nothing else to do. |
| 384 | case CompoundLiteral: |
| 385 | // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of |
| 386 | // postfix-expression exist, parse them now. |
| 387 | break; |
| 388 | case CastExpr: |
| 389 | // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse |
| 390 | // the cast-expression that follows it next. |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 391 | return ParseCastExpression(false); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 392 | } |
| 393 | break; // These can be followed by postfix-expr pieces. |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 395 | // primary-expression |
| 396 | case tok::identifier: // primary-expression: identifier |
| 397 | // constant: enumeration-constant |
| 398 | case tok::numeric_constant: // constant: integer-constant |
| 399 | // constant: floating-constant |
| 400 | case tok::char_constant: // constant: character-constant |
| 401 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 402 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 403 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
| 404 | ConsumeToken(); |
| 405 | break; |
| 406 | case tok::string_literal: // primary-expression: string-literal |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 407 | Res = ParseStringLiteralExpression(); |
| 408 | if (Res.isInvalid) return Res; |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 409 | break; |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 410 | case tok::kw___builtin_va_arg: |
| 411 | case tok::kw___builtin_offsetof: |
| 412 | case tok::kw___builtin_choose_expr: |
| 413 | case tok::kw___builtin_types_compatible_p: |
| 414 | assert(0 && "FIXME: UNIMP!"); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 415 | case tok::plusplus: // unary-expression: '++' unary-expression |
| 416 | case tok::minusminus: // unary-expression: '--' unary-expression |
| 417 | ConsumeToken(); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 418 | return ParseCastExpression(true); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 419 | case tok::amp: // unary-expression: '&' cast-expression |
| 420 | case tok::star: // unary-expression: '*' cast-expression |
| 421 | case tok::plus: // unary-expression: '+' cast-expression |
| 422 | case tok::minus: // unary-expression: '-' cast-expression |
| 423 | case tok::tilde: // unary-expression: '~' cast-expression |
| 424 | case tok::exclaim: // unary-expression: '!' cast-expression |
| 425 | case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] |
| 426 | case tok::kw___imag: // unary-expression: '__real' cast-expression [GNU] |
| 427 | //case tok::kw__extension__: [TODO] |
| 428 | ConsumeToken(); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 429 | return ParseCastExpression(false); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 430 | |
| 431 | case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression |
| 432 | // unary-expression: 'sizeof' '(' type-name ')' |
| 433 | case tok::kw___alignof: // unary-expression: '__alignof' unary-expression |
| 434 | // unary-expression: '__alignof' '(' type-name ')' |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 435 | return ParseSizeofAlignofExpression(); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 436 | case tok::ampamp: // unary-expression: '&&' identifier |
| 437 | Diag(Tok, diag::ext_gnu_address_of_label); |
| 438 | ConsumeToken(); |
| 439 | if (Tok.getKind() == tok::identifier) { |
| 440 | ConsumeToken(); |
| 441 | } else { |
| 442 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 443 | return ExprResult(true); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 444 | } |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 445 | return ExprResult(false); |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 446 | default: |
| 447 | Diag(Tok, diag::err_expected_expression); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 448 | return ExprResult(true); |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // Now that the primary-expression piece of the postfix-expression has been |
| 452 | // parsed, see if there are any postfix-expression pieces here. |
| 453 | SourceLocation Loc; |
| 454 | while (1) { |
| 455 | switch (Tok.getKind()) { |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 456 | default: |
| 457 | return ExprResult(false); |
| 458 | case tok::l_square: // postfix-expression: p-e '[' expression ']' |
| 459 | Loc = Tok.getLocation(); |
| 460 | ConsumeBracket(); |
| 461 | ParseExpression(); |
| 462 | // Match the ']'. |
| 463 | MatchRHSPunctuation(tok::r_square, Loc, "[", diag::err_expected_rsquare); |
| 464 | break; |
| 465 | |
| 466 | case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')' |
| 467 | Loc = Tok.getLocation(); |
| 468 | ConsumeParen(); |
| 469 | |
| 470 | while (1) { |
| 471 | // FIXME: This should be argument-expression! |
| 472 | ParseAssignmentExpression(); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 474 | if (Tok.getKind() != tok::comma) |
| 475 | break; |
| 476 | ConsumeToken(); // Next argument. |
| 477 | } |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 478 | |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 479 | // Match the ')'. |
| 480 | MatchRHSPunctuation(tok::r_paren, Loc, "(", diag::err_expected_rparen); |
| 481 | break; |
| 482 | |
| 483 | case tok::arrow: // postfix-expression: p-e '->' identifier |
| 484 | case tok::period: // postfix-expression: p-e '.' identifier |
| 485 | ConsumeToken(); |
| 486 | if (Tok.getKind() != tok::identifier) { |
| 487 | Diag(Tok, diag::err_expected_ident); |
| 488 | return ExprResult(true); |
| 489 | } |
| 490 | ConsumeToken(); |
| 491 | break; |
| 492 | |
| 493 | case tok::plusplus: // postfix-expression: postfix-expression '++' |
| 494 | case tok::minusminus: // postfix-expression: postfix-expression '--' |
| 495 | ConsumeToken(); |
| 496 | break; |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 501 | /// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression. |
| 502 | /// unary-expression: [C99 6.5.3] |
| 503 | /// 'sizeof' unary-expression |
| 504 | /// 'sizeof' '(' type-name ')' |
| 505 | /// [GNU] '__alignof' unary-expression |
| 506 | /// [GNU] '__alignof' '(' type-name ')' |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 507 | Parser::ExprResult Parser::ParseSizeofAlignofExpression() { |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 508 | assert((Tok.getKind() == tok::kw_sizeof || |
| 509 | Tok.getKind() == tok::kw___alignof) && |
| 510 | "Not a sizeof/alignof expression!"); |
| 511 | ConsumeToken(); |
| 512 | |
| 513 | // If the operand doesn't start with an '(', it must be an expression. |
| 514 | if (Tok.getKind() != tok::l_paren) { |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 515 | return ParseCastExpression(true); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | // If it starts with a '(', we know that it is either a parenthesized |
| 519 | // type-name, or it is a unary-expression that starts with a compound literal, |
| 520 | // or starts with a primary-expression that is a parenthesized expression. |
| 521 | ParenParseOption ExprType = CastExpr; |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 522 | return ParseParenExpression(ExprType); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 525 | /// ParseStringLiteralExpression - This handles the various token types that |
| 526 | /// form string literals, and also handles string concatenation [C99 5.1.1.2, |
| 527 | /// translation phase #6]. |
| 528 | /// |
| 529 | /// primary-expression: [C99 6.5.1] |
| 530 | /// string-literal |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 531 | Parser::ExprResult Parser::ParseStringLiteralExpression() { |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 532 | assert(isTokenStringLiteral() && "Not a string literal!"); |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 533 | ConsumeStringToken(); |
| 534 | |
| 535 | // String concat. Note that keywords like __func__ and __FUNCTION__ aren't |
| 536 | // considered to be strings. |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 537 | while (isTokenStringLiteral()) |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 538 | ConsumeStringToken(); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 539 | return ExprResult(false); |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 540 | } |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 541 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 542 | |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 543 | /// ParseParenExpression - This parses the unit that starts with a '(' token, |
| 544 | /// based on what is allowed by ExprType. The actual thing parsed is returned |
| 545 | /// in ExprType. |
| 546 | /// |
| 547 | /// primary-expression: [C99 6.5.1] |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 548 | /// '(' expression ')' |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 549 | /// [GNU] '(' compound-statement ')' (if !ParenExprOnly) |
| 550 | /// postfix-expression: [C99 6.5.2] |
| 551 | /// '(' type-name ')' '{' initializer-list '}' |
| 552 | /// '(' type-name ')' '{' initializer-list ',' '}' |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 553 | /// cast-expression: [C99 6.5.4] |
| 554 | /// '(' type-name ')' cast-expression |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 555 | /// |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 556 | Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) { |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 557 | assert(Tok.getKind() == tok::l_paren && "Not a paren expr!"); |
| 558 | SourceLocation OpenLoc = Tok.getLocation(); |
| 559 | ConsumeParen(); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 560 | ExprResult Result(false); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 561 | |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 562 | if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace && |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 563 | !getLang().NoExtensions) { |
| 564 | Diag(Tok, diag::ext_gnu_statement_expr); |
| 565 | ParseCompoundStatement(); |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 566 | ExprType = CompoundStmt; |
| 567 | } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) { |
Chris Lattner | 6c3f05d | 2006-08-12 16:54:25 +0000 | [diff] [blame] | 568 | // Otherwise, this is a compound literal expression or cast expression. |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 569 | ParseTypeName(); |
| 570 | |
| 571 | // Match the ')'. |
| 572 | MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen); |
| 573 | |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 574 | if (Tok.getKind() == tok::l_brace) { |
Chris Lattner | 6c3f05d | 2006-08-12 16:54:25 +0000 | [diff] [blame] | 575 | if (!getLang().C99) // Compound literals don't exist in C90. |
| 576 | Diag(OpenLoc, diag::ext_c99_compound_literal); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 577 | Result = ParseInitializer(); |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 578 | ExprType = CompoundLiteral; |
| 579 | } else if (ExprType == CastExpr) { |
| 580 | // Note that this doesn't parse the subsequence cast-expression. |
| 581 | ExprType = CastExpr; |
| 582 | } else { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 583 | Diag(Tok, diag::err_expected_lbrace_in_compound_literal); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 584 | return ExprResult(true); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 585 | } |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 586 | return Result; |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 587 | } else { |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 588 | Result = ParseExpression(); |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 589 | ExprType = SimpleExpr; |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 590 | } |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 591 | |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 592 | // Match the ')'. |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 593 | if (Result.isInvalid) |
| 594 | SkipUntil(tok::r_paren); |
| 595 | else |
| 596 | MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen); |
| 597 | return Result; |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 598 | } |