ParseExpr.cpp revision f7da726f09a6b7c5b9f5308e9690cb015398e671
1//===--- ParseExpr.cpp - Expression Parsing -------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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" 26using 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. 31namespace 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/// 54static 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/// [C++] throw-expression [C++ 15] 161/// 162/// assignment-operator: one of 163/// = *= /= %= += -= <<= >>= &= ^= |= 164/// 165/// expression: [C99 6.5.17] 166/// assignment-expression 167/// expression ',' assignment-expression 168/// 169Parser::ExprResult Parser::ParseExpression() { 170 if (Tok.is(tok::kw_throw)) 171 return ParseThrowExpression(); 172 173 ExprResult LHS = ParseCastExpression(false); 174 if (LHS.isInvalid) return LHS; 175 176 return ParseRHSOfBinaryExpression(LHS, prec::Comma); 177} 178 179/// This routine is called when the '@' is seen and consumed. 180/// Current token is an Identifier and is not a 'try'. This 181/// routine is necessary to disambiguate @try-statement from, 182/// for example, @encode-expression. 183/// 184Parser::ExprResult Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) { 185 ExprResult LHS = ParseObjCAtExpression(AtLoc); 186 if (LHS.isInvalid) return LHS; 187 188 return ParseRHSOfBinaryExpression(LHS, prec::Comma); 189} 190 191/// ParseAssignmentExpression - Parse an expr that doesn't include commas. 192/// 193Parser::ExprResult Parser::ParseAssignmentExpression() { 194 if (Tok.is(tok::kw_throw)) 195 return ParseThrowExpression(); 196 197 ExprResult LHS = ParseCastExpression(false); 198 if (LHS.isInvalid) return LHS; 199 200 return ParseRHSOfBinaryExpression(LHS, prec::Assignment); 201} 202 203/// ParseAssignmentExprWithObjCMessageExprStart - Parse an assignment expression 204/// where part of an objc message send has already been parsed. In this case 205/// LBracLoc indicates the location of the '[' of the message send, and either 206/// ReceiverName or ReceiverExpr is non-null indicating the receiver of the 207/// message. 208/// 209/// Since this handles full assignment-expression's, it handles postfix 210/// expressions and other binary operators for these expressions as well. 211Parser::ExprResult 212Parser::ParseAssignmentExprWithObjCMessageExprStart(SourceLocation LBracLoc, 213 IdentifierInfo *ReceiverName, 214 ExprTy *ReceiverExpr) { 215 ExprResult R = ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 216 ReceiverExpr); 217 if (R.isInvalid) return R; 218 R = ParsePostfixExpressionSuffix(R); 219 if (R.isInvalid) return R; 220 return ParseRHSOfBinaryExpression(R, 2); 221} 222 223 224Parser::ExprResult Parser::ParseConstantExpression() { 225 ExprResult LHS = ParseCastExpression(false); 226 if (LHS.isInvalid) return LHS; 227 228 return ParseRHSOfBinaryExpression(LHS, prec::Conditional); 229} 230 231/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with 232/// LHS and has a precedence of at least MinPrec. 233Parser::ExprResult 234Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) { 235 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind()); 236 SourceLocation ColonLoc; 237 238 while (1) { 239 // If this token has a lower precedence than we are allowed to parse (e.g. 240 // because we are called recursively, or because the token is not a binop), 241 // then we are done! 242 if (NextTokPrec < MinPrec) 243 return LHS; 244 245 // Consume the operator, saving the operator token for error reporting. 246 Token OpToken = Tok; 247 ConsumeToken(); 248 249 // Special case handling for the ternary operator. 250 ExprResult TernaryMiddle(true); 251 if (NextTokPrec == prec::Conditional) { 252 if (Tok.isNot(tok::colon)) { 253 // Handle this production specially: 254 // logical-OR-expression '?' expression ':' conditional-expression 255 // In particular, the RHS of the '?' is 'expression', not 256 // 'logical-OR-expression' as we might expect. 257 TernaryMiddle = ParseExpression(); 258 if (TernaryMiddle.isInvalid) { 259 Actions.DeleteExpr(LHS.Val); 260 return TernaryMiddle; 261 } 262 } else { 263 // Special case handling of "X ? Y : Z" where Y is empty: 264 // logical-OR-expression '?' ':' conditional-expression [GNU] 265 TernaryMiddle = ExprResult(false); 266 Diag(Tok, diag::ext_gnu_conditional_expr); 267 } 268 269 if (Tok.isNot(tok::colon)) { 270 Diag(Tok, diag::err_expected_colon); 271 Diag(OpToken, diag::err_matching, "?"); 272 Actions.DeleteExpr(LHS.Val); 273 Actions.DeleteExpr(TernaryMiddle.Val); 274 return ExprResult(true); 275 } 276 277 // Eat the colon. 278 ColonLoc = ConsumeToken(); 279 } 280 281 // Parse another leaf here for the RHS of the operator. 282 ExprResult RHS = ParseCastExpression(false); 283 if (RHS.isInvalid) { 284 Actions.DeleteExpr(LHS.Val); 285 Actions.DeleteExpr(TernaryMiddle.Val); 286 return RHS; 287 } 288 289 // Remember the precedence of this operator and get the precedence of the 290 // operator immediately to the right of the RHS. 291 unsigned ThisPrec = NextTokPrec; 292 NextTokPrec = getBinOpPrecedence(Tok.getKind()); 293 294 // Assignment and conditional expressions are right-associative. 295 bool isRightAssoc = ThisPrec == prec::Conditional || 296 ThisPrec == prec::Assignment; 297 298 // Get the precedence of the operator to the right of the RHS. If it binds 299 // more tightly with RHS than we do, evaluate it completely first. 300 if (ThisPrec < NextTokPrec || 301 (ThisPrec == NextTokPrec && isRightAssoc)) { 302 // If this is left-associative, only parse things on the RHS that bind 303 // more tightly than the current operator. If it is left-associative, it 304 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as 305 // A=(B=(C=D)), where each paren is a level of recursion here. 306 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc); 307 if (RHS.isInvalid) { 308 Actions.DeleteExpr(LHS.Val); 309 Actions.DeleteExpr(TernaryMiddle.Val); 310 return RHS; 311 } 312 313 NextTokPrec = getBinOpPrecedence(Tok.getKind()); 314 } 315 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); 316 317 if (!LHS.isInvalid) { 318 // Combine the LHS and RHS into the LHS (e.g. build AST). 319 if (TernaryMiddle.isInvalid) 320 LHS = Actions.ActOnBinOp(OpToken.getLocation(), OpToken.getKind(), 321 LHS.Val, RHS.Val); 322 else 323 LHS = Actions.ActOnConditionalOp(OpToken.getLocation(), ColonLoc, 324 LHS.Val, TernaryMiddle.Val, RHS.Val); 325 } else { 326 // We had a semantic error on the LHS. Just free the RHS and continue. 327 Actions.DeleteExpr(TernaryMiddle.Val); 328 Actions.DeleteExpr(RHS.Val); 329 } 330 } 331} 332 333/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is 334/// true, parse a unary-expression. 335/// 336/// cast-expression: [C99 6.5.4] 337/// unary-expression 338/// '(' type-name ')' cast-expression 339/// 340/// unary-expression: [C99 6.5.3] 341/// postfix-expression 342/// '++' unary-expression 343/// '--' unary-expression 344/// unary-operator cast-expression 345/// 'sizeof' unary-expression 346/// 'sizeof' '(' type-name ')' 347/// [GNU] '__alignof' unary-expression 348/// [GNU] '__alignof' '(' type-name ')' 349/// [GNU] '&&' identifier 350/// 351/// unary-operator: one of 352/// '&' '*' '+' '-' '~' '!' 353/// [GNU] '__extension__' '__real' '__imag' 354/// 355/// primary-expression: [C99 6.5.1] 356/// identifier 357/// constant 358/// string-literal 359/// [C++] boolean-literal [C++ 2.13.5] 360/// '(' expression ')' 361/// '__func__' [C99 6.4.2.2] 362/// [GNU] '__FUNCTION__' 363/// [GNU] '__PRETTY_FUNCTION__' 364/// [GNU] '(' compound-statement ')' 365/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' 366/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' 367/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' 368/// assign-expr ')' 369/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' 370/// [OBJC] '[' objc-message-expr ']' 371/// [OBJC] '@selector' '(' objc-selector-arg ')' 372/// [OBJC] '@protocol' '(' identifier ')' 373/// [OBJC] '@encode' '(' type-name ')' 374/// [OBJC] objc-string-literal 375/// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 376/// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 377/// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 378/// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1] 379/// 380/// constant: [C99 6.4.4] 381/// integer-constant 382/// floating-constant 383/// enumeration-constant -> identifier 384/// character-constant 385/// 386Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) { 387 ExprResult Res; 388 tok::TokenKind SavedKind = Tok.getKind(); 389 390 // This handles all of cast-expression, unary-expression, postfix-expression, 391 // and primary-expression. We handle them together like this for efficiency 392 // and to simplify handling of an expression starting with a '(' token: which 393 // may be one of a parenthesized expression, cast-expression, compound literal 394 // expression, or statement expression. 395 // 396 // If the parsed tokens consist of a primary-expression, the cases below 397 // call ParsePostfixExpressionSuffix to handle the postfix expression 398 // suffixes. Cases that cannot be followed by postfix exprs should 399 // return without invoking ParsePostfixExpressionSuffix. 400 switch (SavedKind) { 401 case tok::l_paren: { 402 // If this expression is limited to being a unary-expression, the parent can 403 // not start a cast expression. 404 ParenParseOption ParenExprType = 405 isUnaryExpression ? CompoundLiteral : CastExpr; 406 TypeTy *CastTy; 407 SourceLocation LParenLoc = Tok.getLocation(); 408 SourceLocation RParenLoc; 409 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc); 410 if (Res.isInvalid) return Res; 411 412 switch (ParenExprType) { 413 case SimpleExpr: break; // Nothing else to do. 414 case CompoundStmt: break; // Nothing else to do. 415 case CompoundLiteral: 416 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of 417 // postfix-expression exist, parse them now. 418 break; 419 case CastExpr: 420 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse 421 // the cast-expression that follows it next. 422 // TODO: For cast expression with CastTy. 423 Res = ParseCastExpression(false); 424 if (!Res.isInvalid) 425 Res = Actions.ActOnCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val); 426 return Res; 427 } 428 429 // These can be followed by postfix-expr pieces. 430 return ParsePostfixExpressionSuffix(Res); 431 } 432 433 // primary-expression 434 case tok::numeric_constant: 435 // constant: integer-constant 436 // constant: floating-constant 437 438 Res = Actions.ActOnNumericConstant(Tok); 439 ConsumeToken(); 440 441 // These can be followed by postfix-expr pieces. 442 return ParsePostfixExpressionSuffix(Res); 443 444 case tok::kw_true: 445 case tok::kw_false: 446 return ParseCXXBoolLiteral(); 447 448 case tok::identifier: { // primary-expression: identifier 449 // constant: enumeration-constant 450 // Consume the identifier so that we can see if it is followed by a '('. 451 // Function designators are allowed to be undeclared (C99 6.5.1p2), so we 452 // need to know whether or not this identifier is a function designator or 453 // not. 454 IdentifierInfo &II = *Tok.getIdentifierInfo(); 455 SourceLocation L = ConsumeToken(); 456 Res = Actions.ActOnIdentifierExpr(CurScope, L, II, Tok.is(tok::l_paren)); 457 // These can be followed by postfix-expr pieces. 458 return ParsePostfixExpressionSuffix(Res); 459 } 460 case tok::char_constant: // constant: character-constant 461 Res = Actions.ActOnCharacterConstant(Tok); 462 ConsumeToken(); 463 // These can be followed by postfix-expr pieces. 464 return ParsePostfixExpressionSuffix(Res); 465 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] 466 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] 467 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] 468 Res = Actions.ActOnPreDefinedExpr(Tok.getLocation(), SavedKind); 469 ConsumeToken(); 470 // These can be followed by postfix-expr pieces. 471 return ParsePostfixExpressionSuffix(Res); 472 case tok::string_literal: // primary-expression: string-literal 473 case tok::wide_string_literal: 474 Res = ParseStringLiteralExpression(); 475 if (Res.isInvalid) return Res; 476 // This can be followed by postfix-expr pieces (e.g. "foo"[1]). 477 return ParsePostfixExpressionSuffix(Res); 478 case tok::kw___builtin_va_arg: 479 case tok::kw___builtin_offsetof: 480 case tok::kw___builtin_choose_expr: 481 case tok::kw___builtin_overload: 482 case tok::kw___builtin_types_compatible_p: 483 return ParseBuiltinPrimaryExpression(); 484 case tok::plusplus: // unary-expression: '++' unary-expression 485 case tok::minusminus: { // unary-expression: '--' unary-expression 486 SourceLocation SavedLoc = ConsumeToken(); 487 Res = ParseCastExpression(true); 488 if (!Res.isInvalid) 489 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val); 490 return Res; 491 } 492 case tok::amp: // unary-expression: '&' cast-expression 493 case tok::star: // unary-expression: '*' cast-expression 494 case tok::plus: // unary-expression: '+' cast-expression 495 case tok::minus: // unary-expression: '-' cast-expression 496 case tok::tilde: // unary-expression: '~' cast-expression 497 case tok::exclaim: // unary-expression: '!' cast-expression 498 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] 499 case tok::kw___imag: { // unary-expression: '__imag' cast-expression [GNU] 500 SourceLocation SavedLoc = ConsumeToken(); 501 Res = ParseCastExpression(false); 502 if (!Res.isInvalid) 503 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val); 504 return Res; 505 } 506 507 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU] 508 // __extension__ silences extension warnings in the subexpression. 509 bool SavedExtWarn = Diags.getWarnOnExtensions(); 510 Diags.setWarnOnExtensions(false); 511 SourceLocation SavedLoc = ConsumeToken(); 512 Res = ParseCastExpression(false); 513 if (!Res.isInvalid) 514 Res = Actions.ActOnUnaryOp(SavedLoc, SavedKind, Res.Val); 515 Diags.setWarnOnExtensions(SavedExtWarn); 516 return Res; 517 } 518 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression 519 // unary-expression: 'sizeof' '(' type-name ')' 520 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression 521 // unary-expression: '__alignof' '(' type-name ')' 522 return ParseSizeofAlignofExpression(); 523 case tok::ampamp: { // unary-expression: '&&' identifier 524 SourceLocation AmpAmpLoc = ConsumeToken(); 525 if (Tok.isNot(tok::identifier)) { 526 Diag(Tok, diag::err_expected_ident); 527 return ExprResult(true); 528 } 529 530 Diag(AmpAmpLoc, diag::ext_gnu_address_of_label); 531 Res = Actions.ActOnAddrLabel(AmpAmpLoc, Tok.getLocation(), 532 Tok.getIdentifierInfo()); 533 ConsumeToken(); 534 return Res; 535 } 536 case tok::kw_const_cast: 537 case tok::kw_dynamic_cast: 538 case tok::kw_reinterpret_cast: 539 case tok::kw_static_cast: 540 return ParseCXXCasts(); 541 case tok::kw_this: 542 return ParseCXXThis(); 543 case tok::at: { 544 SourceLocation AtLoc = ConsumeToken(); 545 return ParseObjCAtExpression(AtLoc); 546 } 547 case tok::l_square: 548 // These can be followed by postfix-expr pieces. 549 if (getLang().ObjC1) 550 return ParsePostfixExpressionSuffix(ParseObjCMessageExpression()); 551 // FALL THROUGH. 552 default: 553 Diag(Tok, diag::err_expected_expression); 554 return ExprResult(true); 555 } 556 557 // unreachable. 558 abort(); 559} 560 561/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression 562/// is parsed, this method parses any suffixes that apply. 563/// 564/// postfix-expression: [C99 6.5.2] 565/// primary-expression 566/// postfix-expression '[' expression ']' 567/// postfix-expression '(' argument-expression-list[opt] ')' 568/// postfix-expression '.' identifier 569/// postfix-expression '->' identifier 570/// postfix-expression '++' 571/// postfix-expression '--' 572/// '(' type-name ')' '{' initializer-list '}' 573/// '(' type-name ')' '{' initializer-list ',' '}' 574/// 575/// argument-expression-list: [C99 6.5.2] 576/// argument-expression 577/// argument-expression-list ',' assignment-expression 578/// 579Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) { 580 581 // Now that the primary-expression piece of the postfix-expression has been 582 // parsed, see if there are any postfix-expression pieces here. 583 SourceLocation Loc; 584 while (1) { 585 switch (Tok.getKind()) { 586 default: // Not a postfix-expression suffix. 587 return LHS; 588 case tok::l_square: { // postfix-expression: p-e '[' expression ']' 589 Loc = ConsumeBracket(); 590 ExprResult Idx = ParseExpression(); 591 592 SourceLocation RLoc = Tok.getLocation(); 593 594 if (!LHS.isInvalid && !Idx.isInvalid && Tok.is(tok::r_square)) 595 LHS = Actions.ActOnArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc); 596 else 597 LHS = ExprResult(true); 598 599 // Match the ']'. 600 MatchRHSPunctuation(tok::r_square, Loc); 601 break; 602 } 603 604 case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')' 605 llvm::SmallVector<ExprTy*, 8> ArgExprs; 606 llvm::SmallVector<SourceLocation, 8> CommaLocs; 607 608 Loc = ConsumeParen(); 609 610 if (Tok.isNot(tok::r_paren)) { 611 while (1) { 612 ExprResult ArgExpr = ParseAssignmentExpression(); 613 if (ArgExpr.isInvalid) { 614 SkipUntil(tok::r_paren); 615 return ExprResult(true); 616 } else 617 ArgExprs.push_back(ArgExpr.Val); 618 619 if (Tok.isNot(tok::comma)) 620 break; 621 // Move to the next argument, remember where the comma was. 622 CommaLocs.push_back(ConsumeToken()); 623 } 624 } 625 626 // Match the ')'. 627 if (!LHS.isInvalid && Tok.is(tok::r_paren)) { 628 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&& 629 "Unexpected number of commas!"); 630 LHS = Actions.ActOnCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(), 631 &CommaLocs[0], Tok.getLocation()); 632 } 633 634 MatchRHSPunctuation(tok::r_paren, Loc); 635 break; 636 } 637 case tok::arrow: // postfix-expression: p-e '->' identifier 638 case tok::period: { // postfix-expression: p-e '.' identifier 639 tok::TokenKind OpKind = Tok.getKind(); 640 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token. 641 642 if (Tok.isNot(tok::identifier)) { 643 Diag(Tok, diag::err_expected_ident); 644 return ExprResult(true); 645 } 646 647 if (!LHS.isInvalid) 648 LHS = Actions.ActOnMemberReferenceExpr(LHS.Val, OpLoc, OpKind, 649 Tok.getLocation(), 650 *Tok.getIdentifierInfo()); 651 ConsumeToken(); 652 break; 653 } 654 case tok::plusplus: // postfix-expression: postfix-expression '++' 655 case tok::minusminus: // postfix-expression: postfix-expression '--' 656 if (!LHS.isInvalid) 657 LHS = Actions.ActOnPostfixUnaryOp(Tok.getLocation(), Tok.getKind(), 658 LHS.Val); 659 ConsumeToken(); 660 break; 661 } 662 } 663} 664 665 666/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression. 667/// unary-expression: [C99 6.5.3] 668/// 'sizeof' unary-expression 669/// 'sizeof' '(' type-name ')' 670/// [GNU] '__alignof' unary-expression 671/// [GNU] '__alignof' '(' type-name ')' 672Parser::ExprResult Parser::ParseSizeofAlignofExpression() { 673 assert((Tok.is(tok::kw_sizeof) || Tok.is(tok::kw___alignof)) && 674 "Not a sizeof/alignof expression!"); 675 Token OpTok = Tok; 676 ConsumeToken(); 677 678 // If the operand doesn't start with an '(', it must be an expression. 679 ExprResult Operand; 680 if (Tok.isNot(tok::l_paren)) { 681 Operand = ParseCastExpression(true); 682 } else { 683 // If it starts with a '(', we know that it is either a parenthesized 684 // type-name, or it is a unary-expression that starts with a compound 685 // literal, or starts with a primary-expression that is a parenthesized 686 // expression. 687 ParenParseOption ExprType = CastExpr; 688 TypeTy *CastTy; 689 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; 690 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc); 691 692 // If ParseParenExpression parsed a '(typename)' sequence only, the this is 693 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression. 694 if (ExprType == CastExpr) 695 return Actions.ActOnSizeOfAlignOfTypeExpr(OpTok.getLocation(), 696 OpTok.is(tok::kw_sizeof), 697 LParenLoc, CastTy, RParenLoc); 698 699 // If this is a parenthesized expression, it is the start of a 700 // unary-expression, but doesn't include any postfix pieces. Parse these 701 // now if present. 702 Operand = ParsePostfixExpressionSuffix(Operand); 703 } 704 705 // If we get here, the operand to the sizeof/alignof was an expresion. 706 if (!Operand.isInvalid) 707 Operand = Actions.ActOnUnaryOp(OpTok.getLocation(), OpTok.getKind(), 708 Operand.Val); 709 return Operand; 710} 711 712/// ParseBuiltinPrimaryExpression 713/// 714/// primary-expression: [C99 6.5.1] 715/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' 716/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' 717/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' 718/// assign-expr ')' 719/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' 720/// [CLANG] '__builtin_overload' '(' expr (',' expr)* ')' 721/// 722/// [GNU] offsetof-member-designator: 723/// [GNU] identifier 724/// [GNU] offsetof-member-designator '.' identifier 725/// [GNU] offsetof-member-designator '[' expression ']' 726/// 727Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() { 728 ExprResult Res(false); 729 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); 730 731 tok::TokenKind T = Tok.getKind(); 732 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier. 733 734 // All of these start with an open paren. 735 if (Tok.isNot(tok::l_paren)) { 736 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName()); 737 return ExprResult(true); 738 } 739 740 SourceLocation LParenLoc = ConsumeParen(); 741 // TODO: Build AST. 742 743 switch (T) { 744 default: assert(0 && "Not a builtin primary expression!"); 745 case tok::kw___builtin_va_arg: { 746 ExprResult Expr = ParseAssignmentExpression(); 747 if (Expr.isInvalid) { 748 SkipUntil(tok::r_paren); 749 return Res; 750 } 751 752 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 753 return ExprResult(true); 754 755 TypeTy *Ty = ParseTypeName(); 756 757 if (Tok.isNot(tok::r_paren)) { 758 Diag(Tok, diag::err_expected_rparen); 759 return ExprResult(true); 760 } 761 Res = Actions.ActOnVAArg(StartLoc, Expr.Val, Ty, ConsumeParen()); 762 break; 763 } 764 case tok::kw___builtin_offsetof: { 765 SourceLocation TypeLoc = Tok.getLocation(); 766 TypeTy *Ty = ParseTypeName(); 767 768 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 769 return ExprResult(true); 770 771 // We must have at least one identifier here. 772 if (Tok.isNot(tok::identifier)) { 773 Diag(Tok, diag::err_expected_ident); 774 SkipUntil(tok::r_paren); 775 return true; 776 } 777 778 // Keep track of the various subcomponents we see. 779 llvm::SmallVector<Action::OffsetOfComponent, 4> Comps; 780 781 Comps.push_back(Action::OffsetOfComponent()); 782 Comps.back().isBrackets = false; 783 Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); 784 Comps.back().LocStart = Comps.back().LocEnd = ConsumeToken(); 785 786 while (1) { 787 if (Tok.is(tok::period)) { 788 // offsetof-member-designator: offsetof-member-designator '.' identifier 789 Comps.push_back(Action::OffsetOfComponent()); 790 Comps.back().isBrackets = false; 791 Comps.back().LocStart = ConsumeToken(); 792 793 if (Tok.isNot(tok::identifier)) { 794 Diag(Tok, diag::err_expected_ident); 795 SkipUntil(tok::r_paren); 796 return true; 797 } 798 Comps.back().U.IdentInfo = Tok.getIdentifierInfo(); 799 Comps.back().LocEnd = ConsumeToken(); 800 801 } else if (Tok.is(tok::l_square)) { 802 // offsetof-member-designator: offsetof-member-design '[' expression ']' 803 Comps.push_back(Action::OffsetOfComponent()); 804 Comps.back().isBrackets = true; 805 Comps.back().LocStart = ConsumeBracket(); 806 Res = ParseExpression(); 807 if (Res.isInvalid) { 808 SkipUntil(tok::r_paren); 809 return Res; 810 } 811 Comps.back().U.E = Res.Val; 812 813 Comps.back().LocEnd = 814 MatchRHSPunctuation(tok::r_square, Comps.back().LocStart); 815 } else if (Tok.is(tok::r_paren)) { 816 Res = Actions.ActOnBuiltinOffsetOf(StartLoc, TypeLoc, Ty, &Comps[0], 817 Comps.size(), ConsumeParen()); 818 break; 819 } else { 820 // Error occurred. 821 return ExprResult(true); 822 } 823 } 824 break; 825 } 826 case tok::kw___builtin_choose_expr: { 827 ExprResult Cond = ParseAssignmentExpression(); 828 if (Cond.isInvalid) { 829 SkipUntil(tok::r_paren); 830 return Cond; 831 } 832 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 833 return ExprResult(true); 834 835 ExprResult Expr1 = ParseAssignmentExpression(); 836 if (Expr1.isInvalid) { 837 SkipUntil(tok::r_paren); 838 return Expr1; 839 } 840 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 841 return ExprResult(true); 842 843 ExprResult Expr2 = ParseAssignmentExpression(); 844 if (Expr2.isInvalid) { 845 SkipUntil(tok::r_paren); 846 return Expr2; 847 } 848 if (Tok.isNot(tok::r_paren)) { 849 Diag(Tok, diag::err_expected_rparen); 850 return ExprResult(true); 851 } 852 Res = Actions.ActOnChooseExpr(StartLoc, Cond.Val, Expr1.Val, Expr2.Val, 853 ConsumeParen()); 854 break; 855 } 856 case tok::kw___builtin_overload: { 857 llvm::SmallVector<ExprTy*, 8> ArgExprs; 858 llvm::SmallVector<SourceLocation, 8> CommaLocs; 859 860 // For each iteration through the loop look for assign-expr followed by a 861 // comma. If there is no comma, break and attempt to match r-paren. 862 if (Tok.isNot(tok::r_paren)) { 863 while (1) { 864 ExprResult ArgExpr = ParseAssignmentExpression(); 865 if (ArgExpr.isInvalid) { 866 SkipUntil(tok::r_paren); 867 return ExprResult(true); 868 } else 869 ArgExprs.push_back(ArgExpr.Val); 870 871 if (Tok.isNot(tok::comma)) 872 break; 873 // Move to the next argument, remember where the comma was. 874 CommaLocs.push_back(ConsumeToken()); 875 } 876 } 877 878 // Attempt to consume the r-paren 879 if (Tok.isNot(tok::r_paren)) { 880 Diag(Tok, diag::err_expected_rparen); 881 SkipUntil(tok::r_paren); 882 return ExprResult(true); 883 } 884 Res = Actions.ActOnOverloadExpr(&ArgExprs[0], ArgExprs.size(), 885 &CommaLocs[0], StartLoc, ConsumeParen()); 886 break; 887 } 888 case tok::kw___builtin_types_compatible_p: 889 TypeTy *Ty1 = ParseTypeName(); 890 891 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) 892 return ExprResult(true); 893 894 TypeTy *Ty2 = ParseTypeName(); 895 896 if (Tok.isNot(tok::r_paren)) { 897 Diag(Tok, diag::err_expected_rparen); 898 return ExprResult(true); 899 } 900 Res = Actions.ActOnTypesCompatibleExpr(StartLoc, Ty1, Ty2, ConsumeParen()); 901 break; 902 } 903 904 // These can be followed by postfix-expr pieces because they are 905 // primary-expressions. 906 return ParsePostfixExpressionSuffix(Res); 907} 908 909/// ParseParenExpression - This parses the unit that starts with a '(' token, 910/// based on what is allowed by ExprType. The actual thing parsed is returned 911/// in ExprType. 912/// 913/// primary-expression: [C99 6.5.1] 914/// '(' expression ')' 915/// [GNU] '(' compound-statement ')' (if !ParenExprOnly) 916/// postfix-expression: [C99 6.5.2] 917/// '(' type-name ')' '{' initializer-list '}' 918/// '(' type-name ')' '{' initializer-list ',' '}' 919/// cast-expression: [C99 6.5.4] 920/// '(' type-name ')' cast-expression 921/// 922Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType, 923 TypeTy *&CastTy, 924 SourceLocation &RParenLoc) { 925 assert(Tok.is(tok::l_paren) && "Not a paren expr!"); 926 SourceLocation OpenLoc = ConsumeParen(); 927 ExprResult Result(true); 928 CastTy = 0; 929 930 if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) { 931 Diag(Tok, diag::ext_gnu_statement_expr); 932 Parser::StmtResult Stmt = ParseCompoundStatement(true); 933 ExprType = CompoundStmt; 934 935 // If the substmt parsed correctly, build the AST node. 936 if (!Stmt.isInvalid && Tok.is(tok::r_paren)) 937 Result = Actions.ActOnStmtExpr(OpenLoc, Stmt.Val, Tok.getLocation()); 938 939 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) { 940 // Otherwise, this is a compound literal expression or cast expression. 941 TypeTy *Ty = ParseTypeName(); 942 943 // Match the ')'. 944 if (Tok.is(tok::r_paren)) 945 RParenLoc = ConsumeParen(); 946 else 947 MatchRHSPunctuation(tok::r_paren, OpenLoc); 948 949 if (Tok.is(tok::l_brace)) { 950 if (!getLang().C99) // Compound literals don't exist in C90. 951 Diag(OpenLoc, diag::ext_c99_compound_literal); 952 Result = ParseInitializer(); 953 ExprType = CompoundLiteral; 954 if (!Result.isInvalid) 955 return Actions.ActOnCompoundLiteral(OpenLoc, Ty, RParenLoc, Result.Val); 956 } else if (ExprType == CastExpr) { 957 // Note that this doesn't parse the subsequence cast-expression, it just 958 // returns the parsed type to the callee. 959 ExprType = CastExpr; 960 CastTy = Ty; 961 return ExprResult(false); 962 } else { 963 Diag(Tok, diag::err_expected_lbrace_in_compound_literal); 964 return ExprResult(true); 965 } 966 return Result; 967 } else { 968 Result = ParseExpression(); 969 ExprType = SimpleExpr; 970 if (!Result.isInvalid && Tok.is(tok::r_paren)) 971 Result = Actions.ActOnParenExpr(OpenLoc, Tok.getLocation(), Result.Val); 972 } 973 974 // Match the ')'. 975 if (Result.isInvalid) 976 SkipUntil(tok::r_paren); 977 else { 978 if (Tok.is(tok::r_paren)) 979 RParenLoc = ConsumeParen(); 980 else 981 MatchRHSPunctuation(tok::r_paren, OpenLoc); 982 } 983 984 return Result; 985} 986 987/// ParseStringLiteralExpression - This handles the various token types that 988/// form string literals, and also handles string concatenation [C99 5.1.1.2, 989/// translation phase #6]. 990/// 991/// primary-expression: [C99 6.5.1] 992/// string-literal 993Parser::ExprResult Parser::ParseStringLiteralExpression() { 994 assert(isTokenStringLiteral() && "Not a string literal!"); 995 996 // String concat. Note that keywords like __func__ and __FUNCTION__ are not 997 // considered to be strings for concatenation purposes. 998 llvm::SmallVector<Token, 4> StringToks; 999 1000 do { 1001 StringToks.push_back(Tok); 1002 ConsumeStringToken(); 1003 } while (isTokenStringLiteral()); 1004 1005 // Pass the set of string tokens, ready for concatenation, to the actions. 1006 return Actions.ActOnStringLiteral(&StringToks[0], StringToks.size()); 1007} 1008