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