Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseExpr.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 | // |
| 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 ternary operator ("?:"). The unary leaves are |
| 17 | // handled by ParseCastExpression, the higher level pieces are handled by |
| 18 | // ParseBinaryExpression. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "clang/Parse/Parser.h" |
| 23 | #include "clang/Basic/Diagnostic.h" |
| 24 | #include "llvm/ADT/SmallVector.h" |
| 25 | #include "llvm/ADT/SmallString.h" |
| 26 | using namespace clang; |
| 27 | |
| 28 | /// PrecedenceLevels - These are precedences for the binary/ternary operators in |
| 29 | /// the C99 grammar. These have been named to relate with the C99 grammar |
| 30 | /// productions. Low precedences numbers bind more weakly than high numbers. |
| 31 | namespace prec { |
| 32 | enum Level { |
| 33 | Unknown = 0, // Not binary operator. |
| 34 | Comma = 1, // , |
| 35 | Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= |
| 36 | Conditional = 3, // ? |
| 37 | LogicalOr = 4, // || |
| 38 | LogicalAnd = 5, // && |
| 39 | InclusiveOr = 6, // | |
| 40 | ExclusiveOr = 7, // ^ |
| 41 | And = 8, // & |
| 42 | Equality = 9, // ==, != |
| 43 | Relational = 10, // >=, <=, >, < |
| 44 | Shift = 11, // <<, >> |
| 45 | Additive = 12, // -, + |
| 46 | Multiplicative = 13 // *, /, % |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /// getBinOpPrecedence - Return the precedence of the specified binary operator |
| 52 | /// token. This returns: |
| 53 | /// |
| 54 | static prec::Level getBinOpPrecedence(tok::TokenKind Kind) { |
| 55 | switch (Kind) { |
| 56 | default: return prec::Unknown; |
| 57 | case tok::comma: return prec::Comma; |
| 58 | case tok::equal: |
| 59 | case tok::starequal: |
| 60 | case tok::slashequal: |
| 61 | case tok::percentequal: |
| 62 | case tok::plusequal: |
| 63 | case tok::minusequal: |
| 64 | case tok::lesslessequal: |
| 65 | case tok::greatergreaterequal: |
| 66 | case tok::ampequal: |
| 67 | case tok::caretequal: |
| 68 | case tok::pipeequal: return prec::Assignment; |
| 69 | case tok::question: return prec::Conditional; |
| 70 | case tok::pipepipe: return prec::LogicalOr; |
| 71 | case tok::ampamp: return prec::LogicalAnd; |
| 72 | case tok::pipe: return prec::InclusiveOr; |
| 73 | case tok::caret: return prec::ExclusiveOr; |
| 74 | case tok::amp: return prec::And; |
| 75 | case tok::exclaimequal: |
| 76 | case tok::equalequal: return prec::Equality; |
| 77 | case tok::lessequal: |
| 78 | case tok::less: |
| 79 | case tok::greaterequal: |
| 80 | case tok::greater: return prec::Relational; |
| 81 | case tok::lessless: |
| 82 | case tok::greatergreater: return prec::Shift; |
| 83 | case tok::plus: |
| 84 | case tok::minus: return prec::Additive; |
| 85 | case tok::percent: |
| 86 | case tok::slash: |
| 87 | case tok::star: return prec::Multiplicative; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /// ParseExpression - Simple precedence-based parser for binary/ternary |
| 93 | /// operators. |
| 94 | /// |
| 95 | /// Note: we diverge from the C99 grammar when parsing the assignment-expression |
| 96 | /// production. C99 specifies that the LHS of an assignment operator should be |
| 97 | /// parsed as a unary-expression, but consistency dictates that it be a |
| 98 | /// conditional-expession. In practice, the important thing here is that the |
| 99 | /// LHS of an assignment has to be an l-value, which productions between |
| 100 | /// unary-expression and conditional-expression don't produce. Because we want |
| 101 | /// consistency, we parse the LHS as a conditional-expression, then check for |
| 102 | /// l-value-ness in semantic analysis stages. |
| 103 | /// |
| 104 | /// multiplicative-expression: [C99 6.5.5] |
| 105 | /// cast-expression |
| 106 | /// multiplicative-expression '*' cast-expression |
| 107 | /// multiplicative-expression '/' cast-expression |
| 108 | /// multiplicative-expression '%' cast-expression |
| 109 | /// |
| 110 | /// additive-expression: [C99 6.5.6] |
| 111 | /// multiplicative-expression |
| 112 | /// additive-expression '+' multiplicative-expression |
| 113 | /// additive-expression '-' multiplicative-expression |
| 114 | /// |
| 115 | /// shift-expression: [C99 6.5.7] |
| 116 | /// additive-expression |
| 117 | /// shift-expression '<<' additive-expression |
| 118 | /// shift-expression '>>' additive-expression |
| 119 | /// |
| 120 | /// relational-expression: [C99 6.5.8] |
| 121 | /// shift-expression |
| 122 | /// relational-expression '<' shift-expression |
| 123 | /// relational-expression '>' shift-expression |
| 124 | /// relational-expression '<=' shift-expression |
| 125 | /// relational-expression '>=' shift-expression |
| 126 | /// |
| 127 | /// equality-expression: [C99 6.5.9] |
| 128 | /// relational-expression |
| 129 | /// equality-expression '==' relational-expression |
| 130 | /// equality-expression '!=' relational-expression |
| 131 | /// |
| 132 | /// AND-expression: [C99 6.5.10] |
| 133 | /// equality-expression |
| 134 | /// AND-expression '&' equality-expression |
| 135 | /// |
| 136 | /// exclusive-OR-expression: [C99 6.5.11] |
| 137 | /// AND-expression |
| 138 | /// exclusive-OR-expression '^' AND-expression |
| 139 | /// |
| 140 | /// inclusive-OR-expression: [C99 6.5.12] |
| 141 | /// exclusive-OR-expression |
| 142 | /// inclusive-OR-expression '|' exclusive-OR-expression |
| 143 | /// |
| 144 | /// logical-AND-expression: [C99 6.5.13] |
| 145 | /// inclusive-OR-expression |
| 146 | /// logical-AND-expression '&&' inclusive-OR-expression |
| 147 | /// |
| 148 | /// logical-OR-expression: [C99 6.5.14] |
| 149 | /// logical-AND-expression |
| 150 | /// logical-OR-expression '||' logical-AND-expression |
| 151 | /// |
| 152 | /// conditional-expression: [C99 6.5.15] |
| 153 | /// logical-OR-expression |
| 154 | /// logical-OR-expression '?' expression ':' conditional-expression |
| 155 | /// [GNU] logical-OR-expression '?' ':' conditional-expression |
| 156 | /// |
| 157 | /// assignment-expression: [C99 6.5.16] |
| 158 | /// conditional-expression |
| 159 | /// unary-expression assignment-operator assignment-expression |
| 160 | /// |
| 161 | /// assignment-operator: one of |
| 162 | /// = *= /= %= += -= <<= >>= &= ^= |= |
| 163 | /// |
| 164 | /// expression: [C99 6.5.17] |
| 165 | /// assignment-expression |
| 166 | /// expression ',' assignment-expression |
| 167 | /// |
| 168 | Parser::ExprResult Parser::ParseExpression() { |
| 169 | ExprResult LHS = ParseCastExpression(false); |
| 170 | if (LHS.isInvalid) return LHS; |
| 171 | |
| 172 | return ParseRHSOfBinaryExpression(LHS, prec::Comma); |
| 173 | } |
| 174 | |
| 175 | /// ParseAssignmentExpression - Parse an expr that doesn't include commas. |
| 176 | /// |
| 177 | Parser::ExprResult Parser::ParseAssignmentExpression() { |
| 178 | ExprResult LHS = ParseCastExpression(false); |
| 179 | if (LHS.isInvalid) return LHS; |
| 180 | |
| 181 | return ParseRHSOfBinaryExpression(LHS, prec::Assignment); |
| 182 | } |
| 183 | |
| 184 | Parser::ExprResult Parser::ParseConstantExpression() { |
| 185 | ExprResult LHS = ParseCastExpression(false); |
| 186 | if (LHS.isInvalid) return LHS; |
| 187 | |
| 188 | // TODO: Validate that this is a constant expr! |
| 189 | return ParseRHSOfBinaryExpression(LHS, prec::Conditional); |
| 190 | } |
| 191 | |
| 192 | /// ParseExpressionWithLeadingIdentifier - This special purpose method is used |
| 193 | /// in contexts where we have already consumed an identifier (which we saved in |
| 194 | /// 'IdTok'), then discovered that the identifier was really the leading token |
| 195 | /// of part of an expression. For example, in "A[1]+B", we consumed "A" (which |
| 196 | /// is now in 'IdTok') and the current token is "[". |
| 197 | Parser::ExprResult Parser:: |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 198 | ParseExpressionWithLeadingIdentifier(const Token &IdTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | // We know that 'IdTok' must correspond to this production: |
| 200 | // primary-expression: identifier |
| 201 | |
| 202 | // Let the actions module handle the identifier. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame^] | 203 | ExprResult Res = Actions.ActOnIdentifierExpr(CurScope, IdTok.getLocation(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | *IdTok.getIdentifierInfo(), |
| 205 | Tok.getKind() == tok::l_paren); |
| 206 | |
| 207 | // Because we have to parse an entire cast-expression before starting the |
| 208 | // ParseRHSOfBinaryExpression method (which parses any trailing binops), we |
| 209 | // need to handle the 'postfix-expression' rules. We do this by invoking |
| 210 | // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes: |
| 211 | Res = ParsePostfixExpressionSuffix(Res); |
| 212 | if (Res.isInvalid) return Res; |
| 213 | |
| 214 | // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is |
| 215 | // done, we know we don't have to do anything for cast-expression, because the |
| 216 | // only non-postfix-expression production starts with a '(' token, and we know |
| 217 | // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression |
| 218 | // to consume any trailing operators (e.g. "+" in this example) and connected |
| 219 | // chunks of the expression. |
| 220 | return ParseRHSOfBinaryExpression(Res, prec::Comma); |
| 221 | } |
| 222 | |
| 223 | /// ParseExpressionWithLeadingIdentifier - This special purpose method is used |
| 224 | /// in contexts where we have already consumed an identifier (which we saved in |
| 225 | /// 'IdTok'), then discovered that the identifier was really the leading token |
| 226 | /// of part of an assignment-expression. For example, in "A[1]+B", we consumed |
| 227 | /// "A" (which is now in 'IdTok') and the current token is "[". |
| 228 | Parser::ExprResult Parser:: |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 229 | ParseAssignmentExprWithLeadingIdentifier(const Token &IdTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 230 | // We know that 'IdTok' must correspond to this production: |
| 231 | // primary-expression: identifier |
| 232 | |
| 233 | // Let the actions module handle the identifier. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame^] | 234 | ExprResult Res = Actions.ActOnIdentifierExpr(CurScope, IdTok.getLocation(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | *IdTok.getIdentifierInfo(), |
| 236 | Tok.getKind() == tok::l_paren); |
| 237 | |
| 238 | // Because we have to parse an entire cast-expression before starting the |
| 239 | // ParseRHSOfBinaryExpression method (which parses any trailing binops), we |
| 240 | // need to handle the 'postfix-expression' rules. We do this by invoking |
| 241 | // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes: |
| 242 | Res = ParsePostfixExpressionSuffix(Res); |
| 243 | if (Res.isInvalid) return Res; |
| 244 | |
| 245 | // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is |
| 246 | // done, we know we don't have to do anything for cast-expression, because the |
| 247 | // only non-postfix-expression production starts with a '(' token, and we know |
| 248 | // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression |
| 249 | // to consume any trailing operators (e.g. "+" in this example) and connected |
| 250 | // chunks of the expression. |
| 251 | return ParseRHSOfBinaryExpression(Res, prec::Assignment); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /// ParseAssignmentExpressionWithLeadingStar - This special purpose method is |
| 256 | /// used in contexts where we have already consumed a '*' (which we saved in |
| 257 | /// 'StarTok'), then discovered that the '*' was really the leading token of an |
| 258 | /// expression. For example, in "*(int*)P+B", we consumed "*" (which is |
| 259 | /// now in 'StarTok') and the current token is "(". |
| 260 | Parser::ExprResult Parser:: |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 261 | ParseAssignmentExpressionWithLeadingStar(const Token &StarTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 262 | // We know that 'StarTok' must correspond to this production: |
| 263 | // unary-expression: unary-operator cast-expression |
| 264 | // where 'unary-operator' is '*'. |
| 265 | |
| 266 | // Parse the cast-expression that follows the '*'. This will parse the |
| 267 | // "*(int*)P" part of "*(int*)P+B". |
| 268 | ExprResult Res = ParseCastExpression(false); |
| 269 | if (Res.isInvalid) return Res; |
| 270 | |
| 271 | // Combine StarTok + Res to get the new AST for the combined expression.. |
| 272 | Res = Actions.ParseUnaryOp(StarTok.getLocation(), tok::star, Res.Val); |
| 273 | if (Res.isInvalid) return Res; |
| 274 | |
| 275 | |
| 276 | // We have to parse an entire cast-expression before starting the |
| 277 | // ParseRHSOfBinaryExpression method (which parses any trailing binops). Since |
| 278 | // we know that the only production above us is the cast-expression |
| 279 | // production, and because the only alternative productions start with a '(' |
| 280 | // token (we know we had a '*'), there is no work to do to get a whole |
| 281 | // cast-expression. |
| 282 | |
| 283 | // At this point, the "*(int*)P" part of "*(int*)P+B" has been consumed. Once |
| 284 | // this is done, we can invoke ParseRHSOfBinaryExpression to consume any |
| 285 | // trailing operators (e.g. "+" in this example) and connected chunks of the |
| 286 | // assignment-expression. |
| 287 | return ParseRHSOfBinaryExpression(Res, prec::Assignment); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | /// ParseRHSOfBinaryExpression - Parse a binary expression that starts with |
| 292 | /// LHS and has a precedence of at least MinPrec. |
| 293 | Parser::ExprResult |
| 294 | Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) { |
| 295 | unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 296 | SourceLocation ColonLoc; |
| 297 | |
| 298 | while (1) { |
| 299 | // If this token has a lower precedence than we are allowed to parse (e.g. |
| 300 | // because we are called recursively, or because the token is not a binop), |
| 301 | // then we are done! |
| 302 | if (NextTokPrec < MinPrec) |
| 303 | return LHS; |
| 304 | |
| 305 | // Consume the operator, saving the operator token for error reporting. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 306 | Token OpToken = Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | ConsumeToken(); |
| 308 | |
| 309 | // Special case handling for the ternary operator. |
| 310 | ExprResult TernaryMiddle(true); |
| 311 | if (NextTokPrec == prec::Conditional) { |
| 312 | if (Tok.getKind() != tok::colon) { |
| 313 | // Handle this production specially: |
| 314 | // logical-OR-expression '?' expression ':' conditional-expression |
| 315 | // In particular, the RHS of the '?' is 'expression', not |
| 316 | // 'logical-OR-expression' as we might expect. |
| 317 | TernaryMiddle = ParseExpression(); |
Chris Lattner | dbd583c | 2007-08-31 04:58:34 +0000 | [diff] [blame] | 318 | if (TernaryMiddle.isInvalid) { |
| 319 | Actions.DeleteExpr(LHS.Val); |
| 320 | return TernaryMiddle; |
| 321 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 322 | } else { |
| 323 | // Special case handling of "X ? Y : Z" where Y is empty: |
| 324 | // logical-OR-expression '?' ':' conditional-expression [GNU] |
| 325 | TernaryMiddle = ExprResult(false); |
| 326 | Diag(Tok, diag::ext_gnu_conditional_expr); |
| 327 | } |
| 328 | |
| 329 | if (Tok.getKind() != tok::colon) { |
| 330 | Diag(Tok, diag::err_expected_colon); |
| 331 | Diag(OpToken, diag::err_matching, "?"); |
Chris Lattner | dbd583c | 2007-08-31 04:58:34 +0000 | [diff] [blame] | 332 | Actions.DeleteExpr(LHS.Val); |
| 333 | Actions.DeleteExpr(TernaryMiddle.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 334 | return ExprResult(true); |
| 335 | } |
| 336 | |
| 337 | // Eat the colon. |
| 338 | ColonLoc = ConsumeToken(); |
| 339 | } |
| 340 | |
| 341 | // Parse another leaf here for the RHS of the operator. |
| 342 | ExprResult RHS = ParseCastExpression(false); |
Chris Lattner | dbd583c | 2007-08-31 04:58:34 +0000 | [diff] [blame] | 343 | if (RHS.isInvalid) { |
| 344 | Actions.DeleteExpr(LHS.Val); |
| 345 | Actions.DeleteExpr(TernaryMiddle.Val); |
| 346 | return RHS; |
| 347 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 348 | |
| 349 | // Remember the precedence of this operator and get the precedence of the |
| 350 | // operator immediately to the right of the RHS. |
| 351 | unsigned ThisPrec = NextTokPrec; |
| 352 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 353 | |
| 354 | // Assignment and conditional expressions are right-associative. |
| 355 | bool isRightAssoc = NextTokPrec == prec::Conditional || |
| 356 | NextTokPrec == prec::Assignment; |
| 357 | |
| 358 | // Get the precedence of the operator to the right of the RHS. If it binds |
| 359 | // more tightly with RHS than we do, evaluate it completely first. |
| 360 | if (ThisPrec < NextTokPrec || |
| 361 | (ThisPrec == NextTokPrec && isRightAssoc)) { |
| 362 | // If this is left-associative, only parse things on the RHS that bind |
| 363 | // more tightly than the current operator. If it is left-associative, it |
| 364 | // is okay, to bind exactly as tightly. For example, compile A=B=C=D as |
| 365 | // A=(B=(C=D)), where each paren is a level of recursion here. |
| 366 | RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc); |
Chris Lattner | dbd583c | 2007-08-31 04:58:34 +0000 | [diff] [blame] | 367 | if (RHS.isInvalid) { |
| 368 | Actions.DeleteExpr(LHS.Val); |
| 369 | Actions.DeleteExpr(TernaryMiddle.Val); |
| 370 | return RHS; |
| 371 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 372 | |
| 373 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 374 | } |
| 375 | assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); |
| 376 | |
Chris Lattner | d56d6b6 | 2007-08-31 05:01:50 +0000 | [diff] [blame] | 377 | if (!LHS.isInvalid) { |
| 378 | // Combine the LHS and RHS into the LHS (e.g. build AST). |
| 379 | if (TernaryMiddle.isInvalid) |
| 380 | LHS = Actions.ParseBinOp(OpToken.getLocation(), OpToken.getKind(), |
| 381 | LHS.Val, RHS.Val); |
| 382 | else |
| 383 | LHS = Actions.ParseConditionalOp(OpToken.getLocation(), ColonLoc, |
| 384 | LHS.Val, TernaryMiddle.Val, RHS.Val); |
| 385 | } else { |
| 386 | // We had a semantic error on the LHS. Just free the RHS and continue. |
| 387 | Actions.DeleteExpr(TernaryMiddle.Val); |
| 388 | Actions.DeleteExpr(RHS.Val); |
| 389 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is |
| 394 | /// true, parse a unary-expression. |
| 395 | /// |
| 396 | /// cast-expression: [C99 6.5.4] |
| 397 | /// unary-expression |
| 398 | /// '(' type-name ')' cast-expression |
| 399 | /// |
| 400 | /// unary-expression: [C99 6.5.3] |
| 401 | /// postfix-expression |
| 402 | /// '++' unary-expression |
| 403 | /// '--' unary-expression |
| 404 | /// unary-operator cast-expression |
| 405 | /// 'sizeof' unary-expression |
| 406 | /// 'sizeof' '(' type-name ')' |
| 407 | /// [GNU] '__alignof' unary-expression |
| 408 | /// [GNU] '__alignof' '(' type-name ')' |
| 409 | /// [GNU] '&&' identifier |
| 410 | /// |
| 411 | /// unary-operator: one of |
| 412 | /// '&' '*' '+' '-' '~' '!' |
| 413 | /// [GNU] '__extension__' '__real' '__imag' |
| 414 | /// |
| 415 | /// primary-expression: [C99 6.5.1] |
| 416 | /// identifier |
| 417 | /// constant |
| 418 | /// string-literal |
| 419 | /// [C++] boolean-literal [C++ 2.13.5] |
| 420 | /// '(' expression ')' |
| 421 | /// '__func__' [C99 6.4.2.2] |
| 422 | /// [GNU] '__FUNCTION__' |
| 423 | /// [GNU] '__PRETTY_FUNCTION__' |
| 424 | /// [GNU] '(' compound-statement ')' |
| 425 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 426 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 427 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 428 | /// assign-expr ')' |
| 429 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 430 | /// [OBJC] '[' objc-message-expr ']' [TODO] |
| 431 | /// [OBJC] '@selector' '(' objc-selector-arg ')' [TODO] |
| 432 | /// [OBJC] '@protocol' '(' identifier ')' [TODO] |
| 433 | /// [OBJC] '@encode' '(' type-name ')' [TODO] |
| 434 | /// [OBJC] objc-string-literal |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 435 | /// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 436 | /// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 437 | /// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 438 | /// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] |
| 439 | /// |
| 440 | /// constant: [C99 6.4.4] |
| 441 | /// integer-constant |
| 442 | /// floating-constant |
| 443 | /// enumeration-constant -> identifier |
| 444 | /// character-constant |
| 445 | /// |
| 446 | Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) { |
| 447 | ExprResult Res; |
| 448 | tok::TokenKind SavedKind = Tok.getKind(); |
| 449 | |
| 450 | // This handles all of cast-expression, unary-expression, postfix-expression, |
| 451 | // and primary-expression. We handle them together like this for efficiency |
| 452 | // and to simplify handling of an expression starting with a '(' token: which |
| 453 | // may be one of a parenthesized expression, cast-expression, compound literal |
| 454 | // expression, or statement expression. |
| 455 | // |
| 456 | // If the parsed tokens consist of a primary-expression, the cases below |
| 457 | // call ParsePostfixExpressionSuffix to handle the postfix expression |
| 458 | // suffixes. Cases that cannot be followed by postfix exprs should |
| 459 | // return without invoking ParsePostfixExpressionSuffix. |
| 460 | switch (SavedKind) { |
| 461 | case tok::l_paren: { |
| 462 | // If this expression is limited to being a unary-expression, the parent can |
| 463 | // not start a cast expression. |
| 464 | ParenParseOption ParenExprType = |
| 465 | isUnaryExpression ? CompoundLiteral : CastExpr; |
| 466 | TypeTy *CastTy; |
| 467 | SourceLocation LParenLoc = Tok.getLocation(); |
| 468 | SourceLocation RParenLoc; |
| 469 | Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc); |
| 470 | if (Res.isInvalid) return Res; |
| 471 | |
| 472 | switch (ParenExprType) { |
| 473 | case SimpleExpr: break; // Nothing else to do. |
| 474 | case CompoundStmt: break; // Nothing else to do. |
| 475 | case CompoundLiteral: |
| 476 | // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of |
| 477 | // postfix-expression exist, parse them now. |
| 478 | break; |
| 479 | case CastExpr: |
| 480 | // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse |
| 481 | // the cast-expression that follows it next. |
| 482 | // TODO: For cast expression with CastTy. |
| 483 | Res = ParseCastExpression(false); |
| 484 | if (!Res.isInvalid) |
| 485 | Res = Actions.ParseCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val); |
| 486 | return Res; |
| 487 | } |
| 488 | |
| 489 | // These can be followed by postfix-expr pieces. |
| 490 | return ParsePostfixExpressionSuffix(Res); |
| 491 | } |
| 492 | |
| 493 | // primary-expression |
| 494 | case tok::numeric_constant: |
| 495 | // constant: integer-constant |
| 496 | // constant: floating-constant |
| 497 | |
| 498 | Res = Actions.ParseNumericConstant(Tok); |
| 499 | ConsumeToken(); |
| 500 | |
| 501 | // These can be followed by postfix-expr pieces. |
| 502 | return ParsePostfixExpressionSuffix(Res); |
| 503 | |
| 504 | case tok::kw_true: |
| 505 | case tok::kw_false: |
| 506 | return ParseCXXBoolLiteral(); |
| 507 | |
| 508 | case tok::identifier: { // primary-expression: identifier |
| 509 | // constant: enumeration-constant |
| 510 | // Consume the identifier so that we can see if it is followed by a '('. |
| 511 | // Function designators are allowed to be undeclared (C99 6.5.1p2), so we |
| 512 | // need to know whether or not this identifier is a function designator or |
| 513 | // not. |
| 514 | IdentifierInfo &II = *Tok.getIdentifierInfo(); |
| 515 | SourceLocation L = ConsumeToken(); |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame^] | 516 | Res = Actions.ActOnIdentifierExpr(CurScope, L, II, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 517 | Tok.getKind() == tok::l_paren); |
| 518 | // These can be followed by postfix-expr pieces. |
| 519 | return ParsePostfixExpressionSuffix(Res); |
| 520 | } |
| 521 | case tok::char_constant: // constant: character-constant |
| 522 | Res = Actions.ParseCharacterConstant(Tok); |
| 523 | ConsumeToken(); |
| 524 | // These can be followed by postfix-expr pieces. |
| 525 | return ParsePostfixExpressionSuffix(Res); |
| 526 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 527 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 528 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 529 | Res = Actions.ParsePreDefinedExpr(Tok.getLocation(), SavedKind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 530 | ConsumeToken(); |
| 531 | // These can be followed by postfix-expr pieces. |
| 532 | return ParsePostfixExpressionSuffix(Res); |
| 533 | case tok::string_literal: // primary-expression: string-literal |
| 534 | case tok::wide_string_literal: |
| 535 | Res = ParseStringLiteralExpression(); |
| 536 | if (Res.isInvalid) return Res; |
| 537 | // This can be followed by postfix-expr pieces (e.g. "foo"[1]). |
| 538 | return ParsePostfixExpressionSuffix(Res); |
| 539 | case tok::kw___builtin_va_arg: |
| 540 | case tok::kw___builtin_offsetof: |
| 541 | case tok::kw___builtin_choose_expr: |
| 542 | case tok::kw___builtin_types_compatible_p: |
| 543 | return ParseBuiltinPrimaryExpression(); |
| 544 | case tok::plusplus: // unary-expression: '++' unary-expression |
| 545 | case tok::minusminus: { // unary-expression: '--' unary-expression |
| 546 | SourceLocation SavedLoc = ConsumeToken(); |
| 547 | Res = ParseCastExpression(true); |
| 548 | if (!Res.isInvalid) |
| 549 | Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val); |
| 550 | return Res; |
| 551 | } |
| 552 | case tok::amp: // unary-expression: '&' cast-expression |
| 553 | case tok::star: // unary-expression: '*' cast-expression |
| 554 | case tok::plus: // unary-expression: '+' cast-expression |
| 555 | case tok::minus: // unary-expression: '-' cast-expression |
| 556 | case tok::tilde: // unary-expression: '~' cast-expression |
| 557 | case tok::exclaim: // unary-expression: '!' cast-expression |
| 558 | case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] |
| 559 | case tok::kw___imag: // unary-expression: '__imag' cast-expression [GNU] |
| 560 | case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU] |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 561 | // FIXME: Extension should silence extwarns in subexpressions. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 562 | SourceLocation SavedLoc = ConsumeToken(); |
| 563 | Res = ParseCastExpression(false); |
| 564 | if (!Res.isInvalid) |
| 565 | Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val); |
| 566 | return Res; |
| 567 | } |
| 568 | case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression |
| 569 | // unary-expression: 'sizeof' '(' type-name ')' |
| 570 | case tok::kw___alignof: // unary-expression: '__alignof' unary-expression |
| 571 | // unary-expression: '__alignof' '(' type-name ')' |
| 572 | return ParseSizeofAlignofExpression(); |
| 573 | case tok::ampamp: { // unary-expression: '&&' identifier |
| 574 | SourceLocation AmpAmpLoc = ConsumeToken(); |
| 575 | if (Tok.getKind() != tok::identifier) { |
| 576 | Diag(Tok, diag::err_expected_ident); |
| 577 | return ExprResult(true); |
| 578 | } |
| 579 | |
| 580 | Diag(AmpAmpLoc, diag::ext_gnu_address_of_label); |
| 581 | Res = Actions.ParseAddrLabel(AmpAmpLoc, Tok.getLocation(), |
| 582 | Tok.getIdentifierInfo()); |
| 583 | ConsumeToken(); |
| 584 | return Res; |
| 585 | } |
| 586 | case tok::kw_const_cast: |
| 587 | case tok::kw_dynamic_cast: |
| 588 | case tok::kw_reinterpret_cast: |
| 589 | case tok::kw_static_cast: |
| 590 | return ParseCXXCasts(); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 591 | case tok::at: |
| 592 | return ParseObjCExpression(); |
Fariborz Jahanian | 0ccb27d | 2007-09-05 19:52:07 +0000 | [diff] [blame] | 593 | case tok::l_square: |
| 594 | return ParseObjCMessageExpression (); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 595 | default: |
| 596 | Diag(Tok, diag::err_expected_expression); |
| 597 | return ExprResult(true); |
| 598 | } |
| 599 | |
| 600 | // unreachable. |
| 601 | abort(); |
| 602 | } |
| 603 | |
| 604 | /// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression |
| 605 | /// is parsed, this method parses any suffixes that apply. |
| 606 | /// |
| 607 | /// postfix-expression: [C99 6.5.2] |
| 608 | /// primary-expression |
| 609 | /// postfix-expression '[' expression ']' |
| 610 | /// postfix-expression '(' argument-expression-list[opt] ')' |
| 611 | /// postfix-expression '.' identifier |
| 612 | /// postfix-expression '->' identifier |
| 613 | /// postfix-expression '++' |
| 614 | /// postfix-expression '--' |
| 615 | /// '(' type-name ')' '{' initializer-list '}' |
| 616 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 617 | /// |
| 618 | /// argument-expression-list: [C99 6.5.2] |
| 619 | /// argument-expression |
| 620 | /// argument-expression-list ',' assignment-expression |
| 621 | /// |
| 622 | Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) { |
| 623 | |
| 624 | // Now that the primary-expression piece of the postfix-expression has been |
| 625 | // parsed, see if there are any postfix-expression pieces here. |
| 626 | SourceLocation Loc; |
| 627 | while (1) { |
| 628 | switch (Tok.getKind()) { |
| 629 | default: // Not a postfix-expression suffix. |
| 630 | return LHS; |
| 631 | case tok::l_square: { // postfix-expression: p-e '[' expression ']' |
| 632 | Loc = ConsumeBracket(); |
| 633 | ExprResult Idx = ParseExpression(); |
| 634 | |
| 635 | SourceLocation RLoc = Tok.getLocation(); |
| 636 | |
| 637 | if (!LHS.isInvalid && !Idx.isInvalid && Tok.getKind() == tok::r_square) |
| 638 | LHS = Actions.ParseArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc); |
| 639 | else |
| 640 | LHS = ExprResult(true); |
| 641 | |
| 642 | // Match the ']'. |
| 643 | MatchRHSPunctuation(tok::r_square, Loc); |
| 644 | break; |
| 645 | } |
| 646 | |
| 647 | case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')' |
| 648 | llvm::SmallVector<ExprTy*, 8> ArgExprs; |
| 649 | llvm::SmallVector<SourceLocation, 8> CommaLocs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 650 | |
| 651 | Loc = ConsumeParen(); |
| 652 | |
| 653 | if (Tok.getKind() != tok::r_paren) { |
| 654 | while (1) { |
| 655 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 656 | if (ArgExpr.isInvalid) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | SkipUntil(tok::r_paren); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 658 | return ExprResult(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 659 | } else |
| 660 | ArgExprs.push_back(ArgExpr.Val); |
| 661 | |
| 662 | if (Tok.getKind() != tok::comma) |
| 663 | break; |
| 664 | // Move to the next argument, remember where the comma was. |
| 665 | CommaLocs.push_back(ConsumeToken()); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | // Match the ')'. |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 670 | if (!LHS.isInvalid && Tok.getKind() == tok::r_paren) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 671 | assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&& |
| 672 | "Unexpected number of commas!"); |
| 673 | LHS = Actions.ParseCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(), |
| 674 | &CommaLocs[0], Tok.getLocation()); |
| 675 | } |
| 676 | |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 677 | MatchRHSPunctuation(tok::r_paren, Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 678 | break; |
| 679 | } |
| 680 | case tok::arrow: // postfix-expression: p-e '->' identifier |
| 681 | case tok::period: { // postfix-expression: p-e '.' identifier |
| 682 | tok::TokenKind OpKind = Tok.getKind(); |
| 683 | SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token. |
| 684 | |
| 685 | if (Tok.getKind() != tok::identifier) { |
| 686 | Diag(Tok, diag::err_expected_ident); |
| 687 | return ExprResult(true); |
| 688 | } |
| 689 | |
| 690 | if (!LHS.isInvalid) |
| 691 | LHS = Actions.ParseMemberReferenceExpr(LHS.Val, OpLoc, OpKind, |
| 692 | Tok.getLocation(), |
| 693 | *Tok.getIdentifierInfo()); |
| 694 | ConsumeToken(); |
| 695 | break; |
| 696 | } |
| 697 | case tok::plusplus: // postfix-expression: postfix-expression '++' |
| 698 | case tok::minusminus: // postfix-expression: postfix-expression '--' |
| 699 | if (!LHS.isInvalid) |
| 700 | LHS = Actions.ParsePostfixUnaryOp(Tok.getLocation(), Tok.getKind(), |
| 701 | LHS.Val); |
| 702 | ConsumeToken(); |
| 703 | break; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | |
| 709 | /// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression. |
| 710 | /// unary-expression: [C99 6.5.3] |
| 711 | /// 'sizeof' unary-expression |
| 712 | /// 'sizeof' '(' type-name ')' |
| 713 | /// [GNU] '__alignof' unary-expression |
| 714 | /// [GNU] '__alignof' '(' type-name ')' |
| 715 | Parser::ExprResult Parser::ParseSizeofAlignofExpression() { |
| 716 | assert((Tok.getKind() == tok::kw_sizeof || |
| 717 | Tok.getKind() == tok::kw___alignof) && |
| 718 | "Not a sizeof/alignof expression!"); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 719 | Token OpTok = Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 720 | ConsumeToken(); |
| 721 | |
| 722 | // If the operand doesn't start with an '(', it must be an expression. |
| 723 | ExprResult Operand; |
| 724 | if (Tok.getKind() != tok::l_paren) { |
| 725 | Operand = ParseCastExpression(true); |
| 726 | } else { |
| 727 | // If it starts with a '(', we know that it is either a parenthesized |
| 728 | // type-name, or it is a unary-expression that starts with a compound |
| 729 | // literal, or starts with a primary-expression that is a parenthesized |
| 730 | // expression. |
| 731 | ParenParseOption ExprType = CastExpr; |
| 732 | TypeTy *CastTy; |
| 733 | SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; |
| 734 | Operand = ParseParenExpression(ExprType, CastTy, RParenLoc); |
| 735 | |
| 736 | // If ParseParenExpression parsed a '(typename)' sequence only, the this is |
| 737 | // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression. |
| 738 | if (ExprType == CastExpr) { |
| 739 | return Actions.ParseSizeOfAlignOfTypeExpr(OpTok.getLocation(), |
| 740 | OpTok.getKind() == tok::kw_sizeof, |
| 741 | LParenLoc, CastTy, RParenLoc); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | // If we get here, the operand to the sizeof/alignof was an expresion. |
| 746 | if (!Operand.isInvalid) |
| 747 | Operand = Actions.ParseUnaryOp(OpTok.getLocation(), OpTok.getKind(), |
| 748 | Operand.Val); |
| 749 | return Operand; |
| 750 | } |
| 751 | |
| 752 | /// ParseBuiltinPrimaryExpression |
| 753 | /// |
| 754 | /// primary-expression: [C99 6.5.1] |
| 755 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 756 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 757 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 758 | /// assign-expr ')' |
| 759 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
| 760 | /// |
| 761 | /// [GNU] offsetof-member-designator: |
| 762 | /// [GNU] identifier |
| 763 | /// [GNU] offsetof-member-designator '.' identifier |
| 764 | /// [GNU] offsetof-member-designator '[' expression ']' |
| 765 | /// |
| 766 | Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() { |
| 767 | ExprResult Res(false); |
| 768 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
| 769 | |
| 770 | tok::TokenKind T = Tok.getKind(); |
| 771 | SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier. |
| 772 | |
| 773 | // All of these start with an open paren. |
| 774 | if (Tok.getKind() != tok::l_paren) { |
| 775 | Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName()); |
| 776 | return ExprResult(true); |
| 777 | } |
| 778 | |
| 779 | SourceLocation LParenLoc = ConsumeParen(); |
| 780 | // TODO: Build AST. |
| 781 | |
| 782 | switch (T) { |
| 783 | default: assert(0 && "Not a builtin primary expression!"); |
| 784 | case tok::kw___builtin_va_arg: |
| 785 | Res = ParseAssignmentExpression(); |
| 786 | if (Res.isInvalid) { |
| 787 | SkipUntil(tok::r_paren); |
| 788 | return Res; |
| 789 | } |
| 790 | |
| 791 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
| 792 | return ExprResult(true); |
| 793 | |
| 794 | ParseTypeName(); |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 795 | |
| 796 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 797 | break; |
| 798 | |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 799 | case tok::kw___builtin_offsetof: { |
Chris Lattner | 9fddf0a | 2007-08-30 17:08:45 +0000 | [diff] [blame] | 800 | SourceLocation TypeLoc = Tok.getLocation(); |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 801 | TypeTy *Ty = ParseTypeName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 802 | |
| 803 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
| 804 | return ExprResult(true); |
| 805 | |
| 806 | // We must have at least one identifier here. |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 807 | if (Tok.getKind() != tok::identifier) { |
| 808 | Diag(Tok, diag::err_expected_ident); |
| 809 | SkipUntil(tok::r_paren); |
| 810 | return true; |
| 811 | } |
| 812 | |
| 813 | // Keep track of the various subcomponents we see. |
| 814 | llvm::SmallVector<Action::OffsetOfComponent, 4> Comps; |
| 815 | |
| 816 | Comps.push_back(Action::OffsetOfComponent()); |
| 817 | Comps.back().isBrackets = false; |
| 818 | Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); |
| 819 | Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 820 | |
| 821 | while (1) { |
| 822 | if (Tok.getKind() == tok::period) { |
| 823 | // offsetof-member-designator: offsetof-member-designator '.' identifier |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 824 | Comps.push_back(Action::OffsetOfComponent()); |
| 825 | Comps.back().isBrackets = false; |
| 826 | Comps.back().LocStart = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 827 | |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 828 | if (Tok.getKind() != tok::identifier) { |
| 829 | Diag(Tok, diag::err_expected_ident); |
| 830 | SkipUntil(tok::r_paren); |
| 831 | return true; |
| 832 | } |
| 833 | Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); |
| 834 | Comps.back().LocEnd = ConsumeToken(); |
| 835 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 836 | } else if (Tok.getKind() == tok::l_square) { |
| 837 | // offsetof-member-designator: offsetof-member-design '[' expression ']' |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 838 | Comps.push_back(Action::OffsetOfComponent()); |
| 839 | Comps.back().isBrackets = true; |
| 840 | Comps.back().LocStart = ConsumeBracket(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 841 | Res = ParseExpression(); |
| 842 | if (Res.isInvalid) { |
| 843 | SkipUntil(tok::r_paren); |
| 844 | return Res; |
| 845 | } |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 846 | Comps.back().U.E = Res.Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 847 | |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 848 | Comps.back().LocEnd = |
| 849 | MatchRHSPunctuation(tok::r_square, Comps.back().LocStart); |
| 850 | } else if (Tok.getKind() == tok::r_paren) { |
Chris Lattner | 9fddf0a | 2007-08-30 17:08:45 +0000 | [diff] [blame] | 851 | Res = Actions.ParseBuiltinOffsetOf(StartLoc, TypeLoc, Ty, &Comps[0], |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 852 | Comps.size(), ConsumeParen()); |
| 853 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 854 | } else { |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 855 | // Error occurred. |
| 856 | return ExprResult(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | break; |
Chris Lattner | f9aa3cb | 2007-08-30 15:51:11 +0000 | [diff] [blame] | 860 | } |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 861 | case tok::kw___builtin_choose_expr: { |
| 862 | ExprResult Cond = ParseAssignmentExpression(); |
| 863 | if (Cond.isInvalid) { |
| 864 | SkipUntil(tok::r_paren); |
| 865 | return Cond; |
| 866 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 867 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
| 868 | return ExprResult(true); |
| 869 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 870 | ExprResult Expr1 = ParseAssignmentExpression(); |
| 871 | if (Expr1.isInvalid) { |
| 872 | SkipUntil(tok::r_paren); |
| 873 | return Expr1; |
| 874 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 875 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
| 876 | return ExprResult(true); |
| 877 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 878 | ExprResult Expr2 = ParseAssignmentExpression(); |
| 879 | if (Expr2.isInvalid) { |
| 880 | SkipUntil(tok::r_paren); |
| 881 | return Expr2; |
| 882 | } |
| 883 | if (Tok.getKind() != tok::r_paren) { |
| 884 | Diag(Tok, diag::err_expected_rparen); |
| 885 | return ExprResult(true); |
| 886 | } |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 887 | Res = Actions.ParseChooseExpr(StartLoc, Cond.Val, Expr1.Val, Expr2.Val, |
| 888 | ConsumeParen()); |
| 889 | break; |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 890 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 891 | case tok::kw___builtin_types_compatible_p: |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 892 | TypeTy *Ty1 = ParseTypeName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 893 | |
| 894 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
| 895 | return ExprResult(true); |
| 896 | |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 897 | TypeTy *Ty2 = ParseTypeName(); |
| 898 | |
| 899 | if (Tok.getKind() != tok::r_paren) { |
| 900 | Diag(Tok, diag::err_expected_rparen); |
| 901 | return ExprResult(true); |
| 902 | } |
Chris Lattner | 6eb2109 | 2007-08-30 15:52:49 +0000 | [diff] [blame] | 903 | Res = Actions.ParseTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen()); |
| 904 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 907 | // These can be followed by postfix-expr pieces because they are |
| 908 | // primary-expressions. |
| 909 | return ParsePostfixExpressionSuffix(Res); |
| 910 | } |
| 911 | |
| 912 | /// ParseParenExpression - This parses the unit that starts with a '(' token, |
| 913 | /// based on what is allowed by ExprType. The actual thing parsed is returned |
| 914 | /// in ExprType. |
| 915 | /// |
| 916 | /// primary-expression: [C99 6.5.1] |
| 917 | /// '(' expression ')' |
| 918 | /// [GNU] '(' compound-statement ')' (if !ParenExprOnly) |
| 919 | /// postfix-expression: [C99 6.5.2] |
| 920 | /// '(' type-name ')' '{' initializer-list '}' |
| 921 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 922 | /// cast-expression: [C99 6.5.4] |
| 923 | /// '(' type-name ')' cast-expression |
| 924 | /// |
| 925 | Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType, |
| 926 | TypeTy *&CastTy, |
| 927 | SourceLocation &RParenLoc) { |
| 928 | assert(Tok.getKind() == tok::l_paren && "Not a paren expr!"); |
| 929 | SourceLocation OpenLoc = ConsumeParen(); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 930 | ExprResult Result(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 931 | CastTy = 0; |
| 932 | |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 933 | if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 934 | Diag(Tok, diag::ext_gnu_statement_expr); |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 935 | Parser::StmtResult Stmt = ParseCompoundStatement(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 936 | ExprType = CompoundStmt; |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 937 | |
| 938 | // If the substmt parsed correctly, build the AST node. |
| 939 | if (!Stmt.isInvalid && Tok.getKind() == tok::r_paren) |
| 940 | Result = Actions.ParseStmtExpr(OpenLoc, Stmt.Val, Tok.getLocation()); |
| 941 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 942 | } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) { |
| 943 | // Otherwise, this is a compound literal expression or cast expression. |
| 944 | TypeTy *Ty = ParseTypeName(); |
| 945 | |
| 946 | // Match the ')'. |
| 947 | if (Tok.getKind() == tok::r_paren) |
| 948 | RParenLoc = ConsumeParen(); |
| 949 | else |
| 950 | MatchRHSPunctuation(tok::r_paren, OpenLoc); |
| 951 | |
| 952 | if (Tok.getKind() == tok::l_brace) { |
| 953 | if (!getLang().C99) // Compound literals don't exist in C90. |
| 954 | Diag(OpenLoc, diag::ext_c99_compound_literal); |
| 955 | Result = ParseInitializer(); |
| 956 | ExprType = CompoundLiteral; |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 957 | if (!Result.isInvalid) |
| 958 | return Actions.ParseCompoundLiteral(OpenLoc, Ty, RParenLoc, Result.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 959 | } else if (ExprType == CastExpr) { |
| 960 | // Note that this doesn't parse the subsequence cast-expression, it just |
| 961 | // returns the parsed type to the callee. |
| 962 | ExprType = CastExpr; |
| 963 | CastTy = Ty; |
| 964 | return ExprResult(false); |
| 965 | } else { |
| 966 | Diag(Tok, diag::err_expected_lbrace_in_compound_literal); |
| 967 | return ExprResult(true); |
| 968 | } |
| 969 | return Result; |
| 970 | } else { |
| 971 | Result = ParseExpression(); |
| 972 | ExprType = SimpleExpr; |
| 973 | if (!Result.isInvalid && Tok.getKind() == tok::r_paren) |
| 974 | Result = Actions.ParseParenExpr(OpenLoc, Tok.getLocation(), Result.Val); |
| 975 | } |
| 976 | |
| 977 | // Match the ')'. |
| 978 | if (Result.isInvalid) |
| 979 | SkipUntil(tok::r_paren); |
| 980 | else { |
| 981 | if (Tok.getKind() == tok::r_paren) |
| 982 | RParenLoc = ConsumeParen(); |
| 983 | else |
| 984 | MatchRHSPunctuation(tok::r_paren, OpenLoc); |
| 985 | } |
| 986 | |
| 987 | return Result; |
| 988 | } |
| 989 | |
| 990 | /// ParseStringLiteralExpression - This handles the various token types that |
| 991 | /// form string literals, and also handles string concatenation [C99 5.1.1.2, |
| 992 | /// translation phase #6]. |
| 993 | /// |
| 994 | /// primary-expression: [C99 6.5.1] |
| 995 | /// string-literal |
| 996 | Parser::ExprResult Parser::ParseStringLiteralExpression() { |
| 997 | assert(isTokenStringLiteral() && "Not a string literal!"); |
| 998 | |
| 999 | // String concat. Note that keywords like __func__ and __FUNCTION__ are not |
| 1000 | // considered to be strings for concatenation purposes. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1001 | llvm::SmallVector<Token, 4> StringToks; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1002 | |
| 1003 | do { |
| 1004 | StringToks.push_back(Tok); |
| 1005 | ConsumeStringToken(); |
| 1006 | } while (isTokenStringLiteral()); |
| 1007 | |
| 1008 | // Pass the set of string tokens, ready for concatenation, to the actions. |
| 1009 | return Actions.ParseStringLiteral(&StringToks[0], StringToks.size()); |
| 1010 | } |