2006-08-10 04:23:57 +00:00
|
|
|
//===--- Expression.cpp - Expression Parsing ------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2006-08-12 08:13:25 +00:00
|
|
|
// This file implements the Expression parsing implementation. Expressions in
|
|
|
|
// C99 basically consist of a bunch of binary operators with unary operators and
|
|
|
|
// other random stuff at the leaves.
|
|
|
|
//
|
|
|
|
// In the C99 grammar, these unary operators bind tightest and are represented
|
|
|
|
// as the 'cast-expression' production. Everything else is either a binary
|
2006-08-12 16:45:01 +00:00
|
|
|
// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
|
2006-08-12 08:13:25 +00:00
|
|
|
// handled by ParseCastExpression, the higher level pieces are handled by
|
|
|
|
// ParseBinaryExpression.
|
2006-08-10 04:23:57 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Parse/Parser.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2006-08-10 19:06:03 +00:00
|
|
|
// C99 6.7.8
|
2006-08-11 06:41:18 +00:00
|
|
|
Parser::ExprResult Parser::ParseInitializer() {
|
2006-08-10 19:06:03 +00:00
|
|
|
// FIXME: STUB.
|
2006-08-10 23:56:11 +00:00
|
|
|
if (Tok.getKind() == tok::l_brace) {
|
|
|
|
ConsumeBrace();
|
2006-08-11 01:38:28 +00:00
|
|
|
|
|
|
|
if (Tok.getKind() == tok::numeric_constant)
|
|
|
|
ConsumeToken();
|
|
|
|
|
2006-08-10 23:56:11 +00:00
|
|
|
// FIXME: initializer-list
|
|
|
|
// Match the '}'.
|
|
|
|
MatchRHSPunctuation(tok::r_brace, Tok.getLocation(), "{",
|
|
|
|
diag::err_expected_rbrace);
|
2006-08-11 06:41:18 +00:00
|
|
|
return ExprResult(false);
|
2006-08-10 23:56:11 +00:00
|
|
|
}
|
|
|
|
|
2006-08-11 06:41:18 +00:00
|
|
|
return ParseAssignmentExpression();
|
2006-08-10 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-08-11 06:41:18 +00:00
|
|
|
Parser::ExprResult Parser::ParseExpression() {
|
2006-08-12 08:13:25 +00:00
|
|
|
return ParseBinaryExpression();
|
2006-08-10 04:23:57 +00:00
|
|
|
}
|
|
|
|
|
2006-08-10 19:06:03 +00:00
|
|
|
// Expr that doesn't include commas.
|
2006-08-11 06:41:18 +00:00
|
|
|
Parser::ExprResult Parser::ParseAssignmentExpression() {
|
|
|
|
return ParseExpression();
|
2006-08-10 19:06:03 +00:00
|
|
|
}
|
|
|
|
|
2006-08-12 16:45:01 +00:00
|
|
|
/// PrecedenceLevels - These are precedences for the binary/ternary operators in
|
2006-08-12 08:13:25 +00:00
|
|
|
/// the C99 grammar. These have been named to relate with the C99 grammar
|
|
|
|
/// productions. Low precedences numbers bind more weakly than high numbers.
|
|
|
|
namespace prec {
|
|
|
|
enum Level {
|
|
|
|
Unknown = 0, // Not binary operator.
|
|
|
|
Comma = 1, // ,
|
|
|
|
Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
|
|
|
|
Conditional = 3, // ?
|
|
|
|
LogicalOr = 4, // ||
|
|
|
|
LogicalAnd = 5, // &&
|
|
|
|
InclusiveOr = 6, // |
|
|
|
|
ExclusiveOr = 7, // ^
|
|
|
|
And = 8, // &
|
|
|
|
MinMax = 9, // <?, >? min, max (GCC extensions)
|
|
|
|
Equality = 10, // ==, !=
|
|
|
|
Relational = 11, // >=, <=, >, <
|
|
|
|
Shift = 12, // <<, >>
|
|
|
|
Additive = 13, // -, +
|
|
|
|
Multiplicative = 14 // *, /, %
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// getBinOpPrecedence - Return the precedence of the specified binary operator
|
|
|
|
/// token. This returns:
|
|
|
|
///
|
|
|
|
static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
default: return prec::Unknown;
|
|
|
|
case tok::comma: return prec::Comma;
|
|
|
|
case tok::equal:
|
|
|
|
case tok::starequal:
|
|
|
|
case tok::slashequal:
|
|
|
|
case tok::percentequal:
|
|
|
|
case tok::plusequal:
|
|
|
|
case tok::minusequal:
|
|
|
|
case tok::lesslessequal:
|
|
|
|
case tok::greatergreaterequal:
|
|
|
|
case tok::ampequal:
|
|
|
|
case tok::caretequal:
|
|
|
|
case tok::pipeequal: return prec::Assignment;
|
|
|
|
case tok::question: return prec::Conditional;
|
|
|
|
case tok::pipepipe: return prec::LogicalOr;
|
|
|
|
case tok::ampamp: return prec::LogicalAnd;
|
|
|
|
case tok::pipe: return prec::InclusiveOr;
|
|
|
|
case tok::caret: return prec::ExclusiveOr;
|
|
|
|
case tok::amp: return prec::And;
|
|
|
|
case tok::lessquestion:
|
|
|
|
case tok::greaterquestion: return prec::MinMax;
|
|
|
|
case tok::exclaimequal:
|
|
|
|
case tok::equalequal: return prec::Equality;
|
|
|
|
case tok::lessequal:
|
|
|
|
case tok::less:
|
|
|
|
case tok::greaterequal:
|
|
|
|
case tok::greater: return prec::Relational;
|
|
|
|
case tok::lessless:
|
|
|
|
case tok::greatergreater: return prec::Shift;
|
|
|
|
case tok::plus:
|
|
|
|
case tok::minus: return prec::Additive;
|
|
|
|
case tok::percent:
|
|
|
|
case tok::slash:
|
|
|
|
case tok::star: return prec::Multiplicative;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-12 16:45:01 +00:00
|
|
|
/// ParseBinaryExpression - Simple precedence-based parser for binary/ternary
|
2006-08-12 08:13:25 +00:00
|
|
|
/// operators.
|
|
|
|
///
|
2006-08-12 16:45:01 +00:00
|
|
|
/// Note: we diverge from the C99 grammar when parsing the assignment-expression
|
|
|
|
/// production. C99 specifies that the LHS of an assignment operator should be
|
|
|
|
/// parsed as a unary-expression, but consistency dictates that it be a
|
|
|
|
/// conditional-expession. In practice, the important thing here is that the
|
|
|
|
/// LHS of an assignment has to be an l-value, which productions between
|
|
|
|
/// unary-expression and conditional-expression don't produce. Because we want
|
|
|
|
/// consistency, we parse the LHS as a conditional-expression, then check for
|
|
|
|
/// l-value-ness in semantic analysis stages.
|
|
|
|
///
|
2006-08-12 08:13:25 +00:00
|
|
|
/// multiplicative-expression: [C99 6.5.5]
|
|
|
|
/// cast-expression
|
|
|
|
/// multiplicative-expression '*' cast-expression
|
|
|
|
/// multiplicative-expression '/' cast-expression
|
|
|
|
/// multiplicative-expression '%' cast-expression
|
|
|
|
///
|
|
|
|
/// additive-expression: [C99 6.5.6]
|
|
|
|
/// multiplicative-expression
|
|
|
|
/// additive-expression '+' multiplicative-expression
|
|
|
|
/// additive-expression '-' multiplicative-expression
|
|
|
|
///
|
|
|
|
/// shift-expression: [C99 6.5.7]
|
|
|
|
/// additive-expression
|
|
|
|
/// shift-expression '<<' additive-expression
|
|
|
|
/// shift-expression '>>' additive-expression
|
|
|
|
///
|
|
|
|
/// relational-expression: [C99 6.5.8]
|
|
|
|
/// shift-expression
|
|
|
|
/// relational-expression '<' shift-expression
|
|
|
|
/// relational-expression '>' shift-expression
|
|
|
|
/// relational-expression '<=' shift-expression
|
|
|
|
/// relational-expression '>=' shift-expression
|
|
|
|
///
|
|
|
|
/// equality-expression: [C99 6.5.9]
|
|
|
|
/// relational-expression
|
|
|
|
/// equality-expression '==' relational-expression
|
|
|
|
/// equality-expression '!=' relational-expression
|
|
|
|
///
|
|
|
|
/// AND-expression: [C99 6.5.10]
|
|
|
|
/// equality-expression
|
|
|
|
/// AND-expression '&' equality-expression
|
|
|
|
///
|
|
|
|
/// exclusive-OR-expression: [C99 6.5.11]
|
|
|
|
/// AND-expression
|
|
|
|
/// exclusive-OR-expression '^' AND-expression
|
|
|
|
///
|
|
|
|
/// inclusive-OR-expression: [C99 6.5.12]
|
|
|
|
/// exclusive-OR-expression
|
|
|
|
/// inclusive-OR-expression '|' exclusive-OR-expression
|
|
|
|
///
|
|
|
|
/// logical-AND-expression: [C99 6.5.13]
|
|
|
|
/// inclusive-OR-expression
|
|
|
|
/// logical-AND-expression '&&' inclusive-OR-expression
|
|
|
|
///
|
|
|
|
/// logical-OR-expression: [C99 6.5.14]
|
|
|
|
/// logical-AND-expression
|
|
|
|
/// logical-OR-expression '||' logical-AND-expression
|
|
|
|
///
|
|
|
|
/// conditional-expression: [C99 6.5.15]
|
|
|
|
/// logical-OR-expression
|
|
|
|
/// logical-OR-expression '?' expression ':' conditional-expression
|
|
|
|
/// [GNU] logical-OR-expression '?' ':' conditional-expression
|
|
|
|
///
|
|
|
|
/// assignment-expression: [C99 6.5.16]
|
|
|
|
/// conditional-expression
|
|
|
|
/// unary-expression assignment-operator assignment-expression
|
|
|
|
///
|
|
|
|
/// assignment-operator: one of
|
|
|
|
/// = *= /= %= += -= <<= >>= &= ^= |=
|
|
|
|
///
|
|
|
|
/// expression: [C99 6.5.17]
|
|
|
|
/// assignment-expression
|
|
|
|
/// expression ',' assignment-expression
|
|
|
|
///
|
|
|
|
Parser::ExprResult Parser::ParseBinaryExpression() {
|
|
|
|
ExprResult LHS = ParseCastExpression(false);
|
|
|
|
if (LHS.isInvalid) return LHS;
|
|
|
|
|
|
|
|
return ParseRHSOfBinaryExpression(LHS, prec::Comma);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
|
|
|
|
/// LHS and has a precedence of at least MinPrec.
|
|
|
|
Parser::ExprResult
|
|
|
|
Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
|
|
|
|
unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// If this token has a lower precedence than we are allowed to parse (e.g.
|
|
|
|
// because we are called recursively, or because the token is not a binop),
|
|
|
|
// then we are done!
|
|
|
|
if (NextTokPrec < MinPrec)
|
|
|
|
return LHS;
|
|
|
|
|
|
|
|
// Consume the operator, saving the operator token for error reporting.
|
|
|
|
LexerToken OpToken = Tok;
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// Parse the RHS of the operator.
|
|
|
|
ExprResult RHS;
|
|
|
|
|
|
|
|
// Special case handling of "X ? Y : Z" were Y is empty. This is a GCC
|
|
|
|
// extension.
|
|
|
|
if (OpToken.getKind() != tok::question || Tok.getKind() != tok::colon) {
|
|
|
|
RHS = ParseCastExpression(false);
|
|
|
|
if (RHS.isInvalid) return RHS;
|
|
|
|
} else {
|
|
|
|
RHS = ExprResult(false);
|
|
|
|
Diag(Tok, diag::ext_gnu_conditional_expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember the precedence of this operator and get the precedence of the
|
|
|
|
// operator immediately to the right of the RHS.
|
|
|
|
unsigned ThisPrec = NextTokPrec;
|
|
|
|
NextTokPrec = getBinOpPrecedence(Tok.getKind());
|
|
|
|
|
|
|
|
// FIXME: ASSIGNMENT IS RIGHT ASSOCIATIVE.
|
|
|
|
// FIXME: do we want to handle assignment here??
|
|
|
|
bool isRightAssoc = OpToken.getKind() == tok::question;
|
|
|
|
|
|
|
|
// Get the precedence of the operator to the right of the RHS. If it binds
|
|
|
|
// more tightly with RHS than we do, evaluate it completely first.
|
|
|
|
|
|
|
|
// FIXME: Is this enough for '?:' operators? The second term is suppsed to
|
|
|
|
// be 'expression', not 'assignment'.
|
|
|
|
if (ThisPrec < NextTokPrec ||
|
|
|
|
(ThisPrec == NextTokPrec && isRightAssoc)) {
|
|
|
|
RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec+1);
|
|
|
|
if (RHS.isInvalid) return RHS;
|
|
|
|
|
|
|
|
NextTokPrec = getBinOpPrecedence(Tok.getKind());
|
|
|
|
}
|
|
|
|
assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
|
|
|
|
|
2006-08-12 16:45:01 +00:00
|
|
|
// Handle the special case of our one ternary operator here.
|
2006-08-12 08:13:25 +00:00
|
|
|
if (OpToken.getKind() == tok::question) {
|
|
|
|
if (Tok.getKind() != tok::colon) {
|
|
|
|
Diag(Tok, diag::err_expected_colon);
|
|
|
|
Diag(OpToken, diag::err_matching, "?");
|
|
|
|
return ExprResult(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eat the colon.
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// Parse the value of the colon.
|
|
|
|
ExprResult AfterColonVal = ParseCastExpression(false);
|
|
|
|
if (AfterColonVal.isInvalid) return AfterColonVal;
|
|
|
|
|
|
|
|
// Parse anything after the RRHS that has a higher precedence than ?.
|
|
|
|
AfterColonVal = ParseRHSOfBinaryExpression(AfterColonVal, ThisPrec+1);
|
|
|
|
if (AfterColonVal.isInvalid) return AfterColonVal;
|
|
|
|
|
|
|
|
// TODO: Combine LHS = LHS ? RHS : AfterColonVal.
|
|
|
|
|
|
|
|
// Figure out the precedence of the token after the : part.
|
|
|
|
NextTokPrec = getBinOpPrecedence(Tok.getKind());
|
|
|
|
} else {
|
|
|
|
// TODO: combine the LHS and RHS into the LHS (e.g. build AST).
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-11 02:02:23 +00:00
|
|
|
/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
|
|
|
|
/// true, parse a unary-expression.
|
|
|
|
///
|
2006-08-10 23:14:52 +00:00
|
|
|
/// cast-expression: [C99 6.5.4]
|
|
|
|
/// unary-expression
|
|
|
|
/// '(' type-name ')' cast-expression
|
2006-08-11 02:13:20 +00:00
|
|
|
///
|
2006-08-10 22:57:16 +00:00
|
|
|
/// unary-expression: [C99 6.5.3]
|
|
|
|
/// postfix-expression
|
|
|
|
/// '++' unary-expression
|
|
|
|
/// '--' unary-expression
|
|
|
|
/// unary-operator cast-expression
|
|
|
|
/// 'sizeof' unary-expression
|
|
|
|
/// 'sizeof' '(' type-name ')'
|
|
|
|
/// [GNU] '__alignof' unary-expression
|
|
|
|
/// [GNU] '__alignof' '(' type-name ')'
|
|
|
|
/// [GNU] '&&' identifier
|
2006-08-11 02:13:20 +00:00
|
|
|
///
|
2006-08-10 22:57:16 +00:00
|
|
|
/// unary-operator: one of
|
|
|
|
/// '&' '*' '+' '-' '~' '!'
|
|
|
|
/// [GNU] '__extension__' '__real' '__imag'
|
|
|
|
///
|
2006-08-10 20:56:00 +00:00
|
|
|
/// postfix-expression: [C99 6.5.2]
|
|
|
|
/// primary-expression
|
|
|
|
/// postfix-expression '[' expression ']'
|
|
|
|
/// postfix-expression '(' argument-expression-list[opt] ')'
|
|
|
|
/// postfix-expression '.' identifier
|
|
|
|
/// postfix-expression '->' identifier
|
|
|
|
/// postfix-expression '++'
|
|
|
|
/// postfix-expression '--'
|
|
|
|
/// '(' type-name ')' '{' initializer-list '}'
|
|
|
|
/// '(' type-name ')' '{' initializer-list ',' '}'
|
|
|
|
///
|
|
|
|
/// argument-expression-list: [C99 6.5.2]
|
|
|
|
/// argument-expression
|
|
|
|
/// argument-expression-list ',' argument-expression
|
|
|
|
///
|
|
|
|
/// primary-expression: [C99 6.5.1]
|
2006-08-10 19:06:03 +00:00
|
|
|
/// identifier
|
|
|
|
/// constant
|
|
|
|
/// string-literal
|
|
|
|
/// '(' expression ')'
|
2006-08-10 20:56:00 +00:00
|
|
|
/// '__func__' [C99 6.4.2.2]
|
|
|
|
/// [GNU] '__FUNCTION__'
|
|
|
|
/// [GNU] '__PRETTY_FUNCTION__'
|
|
|
|
/// [GNU] '(' compound-statement ')'
|
|
|
|
/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
|
|
|
|
/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
|
|
|
|
/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
|
|
|
|
/// assign-expr ')'
|
|
|
|
/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
|
|
|
|
/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
|
|
|
|
/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
|
|
|
|
/// [OBC] '@protocol' '(' identifier ')' [TODO]
|
|
|
|
/// [OBC] '@encode' '(' type-name ')' [TODO]
|
|
|
|
/// [OBC] objc-string-literal [TODO]
|
|
|
|
///
|
|
|
|
/// constant: [C99 6.4.4]
|
|
|
|
/// integer-constant
|
|
|
|
/// floating-constant
|
|
|
|
/// enumeration-constant -> identifier
|
|
|
|
/// character-constant
|
|
|
|
///
|
|
|
|
/// [GNU] offsetof-member-designator:
|
|
|
|
/// [GNU] identifier
|
|
|
|
/// [GNU] offsetof-member-designator '.' identifier
|
|
|
|
/// [GNU] offsetof-member-designator '[' expression ']'
|
|
|
|
///
|
2006-08-11 02:13:20 +00:00
|
|
|
///
|
2006-08-11 06:41:18 +00:00
|
|
|
Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
|
|
|
|
ExprResult Res;
|
|
|
|
|
2006-08-11 02:13:20 +00:00
|
|
|
// This handles all of cast-expression, unary-expression, postfix-expression,
|
|
|
|
// and primary-expression. We handle them together like this for efficiency
|
|
|
|
// and to simplify handling of an expression starting with a '(' token: which
|
|
|
|
// may be one of a parenthesized expression, cast-expression, compound literal
|
|
|
|
// expression, or statement expression.
|
|
|
|
//
|
|
|
|
// If the parsed tokens consist of a primary-expression, the cases below
|
|
|
|
// 'break' out of the switch. This allows the postfix expression pieces to
|
|
|
|
// be applied to them. Cases that cannot be followed by postfix exprs should
|
|
|
|
// return instead.
|
2006-08-10 20:56:00 +00:00
|
|
|
switch (Tok.getKind()) {
|
2006-08-11 02:13:20 +00:00
|
|
|
case tok::l_paren:
|
|
|
|
// If this expression is limited to being a unary-expression, the parent can
|
|
|
|
// not start a cast expression.
|
|
|
|
ParenParseOption ParenExprType =
|
|
|
|
isUnaryExpression ? CompoundLiteral : CastExpr;
|
2006-08-11 06:41:18 +00:00
|
|
|
Res = ParseParenExpression(ParenExprType);
|
|
|
|
if (Res.isInvalid) return Res;
|
|
|
|
|
2006-08-11 02:13:20 +00:00
|
|
|
switch (ParenExprType) {
|
|
|
|
case SimpleExpr: break; // Nothing else to do.
|
|
|
|
case CompoundStmt: break; // Nothing else to do.
|
|
|
|
case CompoundLiteral:
|
|
|
|
// We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
|
|
|
|
// postfix-expression exist, parse them now.
|
|
|
|
break;
|
|
|
|
case CastExpr:
|
|
|
|
// We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
|
|
|
|
// the cast-expression that follows it next.
|
2006-08-11 06:41:18 +00:00
|
|
|
return ParseCastExpression(false);
|
2006-08-11 02:13:20 +00:00
|
|
|
}
|
|
|
|
break; // These can be followed by postfix-expr pieces.
|
2006-08-11 06:41:18 +00:00
|
|
|
|
2006-08-10 20:56:00 +00:00
|
|
|
// primary-expression
|
|
|
|
case tok::identifier: // primary-expression: identifier
|
|
|
|
// constant: enumeration-constant
|
|
|
|
case tok::numeric_constant: // constant: integer-constant
|
|
|
|
// constant: floating-constant
|
|
|
|
case tok::char_constant: // constant: character-constant
|
|
|
|
case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
|
|
|
|
case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
|
|
|
|
case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
|
|
|
|
ConsumeToken();
|
|
|
|
break;
|
|
|
|
case tok::string_literal: // primary-expression: string-literal
|
2006-08-11 06:41:18 +00:00
|
|
|
Res = ParseStringLiteralExpression();
|
|
|
|
if (Res.isInvalid) return Res;
|
2006-08-10 20:56:00 +00:00
|
|
|
break;
|
2006-08-10 22:01:51 +00:00
|
|
|
case tok::kw___builtin_va_arg:
|
|
|
|
case tok::kw___builtin_offsetof:
|
|
|
|
case tok::kw___builtin_choose_expr:
|
|
|
|
case tok::kw___builtin_types_compatible_p:
|
|
|
|
assert(0 && "FIXME: UNIMP!");
|
2006-08-11 02:13:20 +00:00
|
|
|
case tok::plusplus: // unary-expression: '++' unary-expression
|
|
|
|
case tok::minusminus: // unary-expression: '--' unary-expression
|
|
|
|
ConsumeToken();
|
2006-08-11 06:41:18 +00:00
|
|
|
return ParseCastExpression(true);
|
2006-08-11 02:13:20 +00:00
|
|
|
case tok::amp: // unary-expression: '&' cast-expression
|
|
|
|
case tok::star: // unary-expression: '*' cast-expression
|
|
|
|
case tok::plus: // unary-expression: '+' cast-expression
|
|
|
|
case tok::minus: // unary-expression: '-' cast-expression
|
|
|
|
case tok::tilde: // unary-expression: '~' cast-expression
|
|
|
|
case tok::exclaim: // unary-expression: '!' cast-expression
|
|
|
|
case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
|
|
|
|
case tok::kw___imag: // unary-expression: '__real' cast-expression [GNU]
|
|
|
|
//case tok::kw__extension__: [TODO]
|
|
|
|
ConsumeToken();
|
2006-08-11 06:41:18 +00:00
|
|
|
return ParseCastExpression(false);
|
2006-08-11 02:13:20 +00:00
|
|
|
|
|
|
|
case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
|
|
|
|
// unary-expression: 'sizeof' '(' type-name ')'
|
|
|
|
case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
|
|
|
|
// unary-expression: '__alignof' '(' type-name ')'
|
2006-08-11 06:41:18 +00:00
|
|
|
return ParseSizeofAlignofExpression();
|
2006-08-11 02:13:20 +00:00
|
|
|
case tok::ampamp: // unary-expression: '&&' identifier
|
|
|
|
Diag(Tok, diag::ext_gnu_address_of_label);
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.getKind() == tok::identifier) {
|
|
|
|
ConsumeToken();
|
|
|
|
} else {
|
|
|
|
Diag(Tok, diag::err_expected_ident);
|
2006-08-11 06:41:18 +00:00
|
|
|
return ExprResult(true);
|
2006-08-11 02:13:20 +00:00
|
|
|
}
|
2006-08-11 06:41:18 +00:00
|
|
|
return ExprResult(false);
|
2006-08-10 20:56:00 +00:00
|
|
|
default:
|
|
|
|
Diag(Tok, diag::err_expected_expression);
|
2006-08-11 06:41:18 +00:00
|
|
|
return ExprResult(true);
|
2006-08-10 22:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the primary-expression piece of the postfix-expression has been
|
|
|
|
// parsed, see if there are any postfix-expression pieces here.
|
|
|
|
SourceLocation Loc;
|
|
|
|
while (1) {
|
|
|
|
switch (Tok.getKind()) {
|
2006-08-11 06:41:18 +00:00
|
|
|
default:
|
|
|
|
return ExprResult(false);
|
|
|
|
case tok::l_square: // postfix-expression: p-e '[' expression ']'
|
|
|
|
Loc = Tok.getLocation();
|
|
|
|
ConsumeBracket();
|
|
|
|
ParseExpression();
|
|
|
|
// Match the ']'.
|
|
|
|
MatchRHSPunctuation(tok::r_square, Loc, "[", diag::err_expected_rsquare);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::l_paren: // p-e: p-e '(' argument-expression-list[opt] ')'
|
|
|
|
Loc = Tok.getLocation();
|
|
|
|
ConsumeParen();
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// FIXME: This should be argument-expression!
|
|
|
|
ParseAssignmentExpression();
|
2006-08-11 02:13:20 +00:00
|
|
|
|
2006-08-11 06:41:18 +00:00
|
|
|
if (Tok.getKind() != tok::comma)
|
|
|
|
break;
|
|
|
|
ConsumeToken(); // Next argument.
|
|
|
|
}
|
2006-08-11 02:13:20 +00:00
|
|
|
|
2006-08-11 06:41:18 +00:00
|
|
|
// Match the ')'.
|
|
|
|
MatchRHSPunctuation(tok::r_paren, Loc, "(", diag::err_expected_rparen);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::arrow: // postfix-expression: p-e '->' identifier
|
|
|
|
case tok::period: // postfix-expression: p-e '.' identifier
|
|
|
|
ConsumeToken();
|
|
|
|
if (Tok.getKind() != tok::identifier) {
|
|
|
|
Diag(Tok, diag::err_expected_ident);
|
|
|
|
return ExprResult(true);
|
|
|
|
}
|
|
|
|
ConsumeToken();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::plusplus: // postfix-expression: postfix-expression '++'
|
|
|
|
case tok::minusminus: // postfix-expression: postfix-expression '--'
|
|
|
|
ConsumeToken();
|
|
|
|
break;
|
2006-08-10 22:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-10 20:56:00 +00:00
|
|
|
}
|
|
|
|
|
2006-08-11 02:13:20 +00:00
|
|
|
/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
|
|
|
|
/// unary-expression: [C99 6.5.3]
|
|
|
|
/// 'sizeof' unary-expression
|
|
|
|
/// 'sizeof' '(' type-name ')'
|
|
|
|
/// [GNU] '__alignof' unary-expression
|
|
|
|
/// [GNU] '__alignof' '(' type-name ')'
|
2006-08-11 06:41:18 +00:00
|
|
|
Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
|
2006-08-11 02:13:20 +00:00
|
|
|
assert((Tok.getKind() == tok::kw_sizeof ||
|
|
|
|
Tok.getKind() == tok::kw___alignof) &&
|
|
|
|
"Not a sizeof/alignof expression!");
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// If the operand doesn't start with an '(', it must be an expression.
|
|
|
|
if (Tok.getKind() != tok::l_paren) {
|
2006-08-11 06:41:18 +00:00
|
|
|
return ParseCastExpression(true);
|
2006-08-11 02:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it starts with a '(', we know that it is either a parenthesized
|
|
|
|
// type-name, or it is a unary-expression that starts with a compound literal,
|
|
|
|
// or starts with a primary-expression that is a parenthesized expression.
|
|
|
|
ParenParseOption ExprType = CastExpr;
|
2006-08-11 06:41:18 +00:00
|
|
|
return ParseParenExpression(ExprType);
|
2006-08-11 02:13:20 +00:00
|
|
|
}
|
|
|
|
|
2006-08-10 20:56:00 +00:00
|
|
|
/// ParseStringLiteralExpression - This handles the various token types that
|
|
|
|
/// form string literals, and also handles string concatenation [C99 5.1.1.2,
|
|
|
|
/// translation phase #6].
|
|
|
|
///
|
|
|
|
/// primary-expression: [C99 6.5.1]
|
|
|
|
/// string-literal
|
2006-08-11 06:41:18 +00:00
|
|
|
Parser::ExprResult Parser::ParseStringLiteralExpression() {
|
2006-08-10 23:14:52 +00:00
|
|
|
assert(isTokenStringLiteral() && "Not a string literal!");
|
2006-08-10 20:56:00 +00:00
|
|
|
ConsumeStringToken();
|
|
|
|
|
|
|
|
// String concat. Note that keywords like __func__ and __FUNCTION__ aren't
|
|
|
|
// considered to be strings.
|
2006-08-10 23:14:52 +00:00
|
|
|
while (isTokenStringLiteral())
|
2006-08-10 20:56:00 +00:00
|
|
|
ConsumeStringToken();
|
2006-08-11 06:41:18 +00:00
|
|
|
return ExprResult(false);
|
2006-08-10 20:56:00 +00:00
|
|
|
}
|
2006-08-10 19:06:03 +00:00
|
|
|
|
2006-08-10 04:23:57 +00:00
|
|
|
|
2006-08-11 01:33:00 +00:00
|
|
|
/// ParseParenExpression - This parses the unit that starts with a '(' token,
|
|
|
|
/// based on what is allowed by ExprType. The actual thing parsed is returned
|
|
|
|
/// in ExprType.
|
|
|
|
///
|
|
|
|
/// primary-expression: [C99 6.5.1]
|
2006-08-10 04:23:57 +00:00
|
|
|
/// '(' expression ')'
|
2006-08-10 22:01:51 +00:00
|
|
|
/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
|
|
|
|
/// postfix-expression: [C99 6.5.2]
|
|
|
|
/// '(' type-name ')' '{' initializer-list '}'
|
|
|
|
/// '(' type-name ')' '{' initializer-list ',' '}'
|
2006-08-11 01:33:00 +00:00
|
|
|
/// cast-expression: [C99 6.5.4]
|
|
|
|
/// '(' type-name ')' cast-expression
|
2006-08-10 22:01:51 +00:00
|
|
|
///
|
2006-08-11 06:41:18 +00:00
|
|
|
Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType) {
|
2006-08-10 04:23:57 +00:00
|
|
|
assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
|
|
|
|
SourceLocation OpenLoc = Tok.getLocation();
|
|
|
|
ConsumeParen();
|
2006-08-11 06:41:18 +00:00
|
|
|
ExprResult Result(false);
|
2006-08-10 04:23:57 +00:00
|
|
|
|
2006-08-11 01:33:00 +00:00
|
|
|
if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
|
2006-08-10 22:01:51 +00:00
|
|
|
!getLang().NoExtensions) {
|
|
|
|
Diag(Tok, diag::ext_gnu_statement_expr);
|
|
|
|
ParseCompoundStatement();
|
2006-08-11 01:33:00 +00:00
|
|
|
ExprType = CompoundStmt;
|
|
|
|
} else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
|
2006-08-10 23:56:11 +00:00
|
|
|
// Otherwise, this is a compound expression.
|
|
|
|
ParseTypeName();
|
|
|
|
|
|
|
|
// Match the ')'.
|
|
|
|
MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
|
|
|
|
|
2006-08-11 01:33:00 +00:00
|
|
|
if (Tok.getKind() == tok::l_brace) {
|
2006-08-11 06:41:18 +00:00
|
|
|
Result = ParseInitializer();
|
2006-08-11 01:33:00 +00:00
|
|
|
ExprType = CompoundLiteral;
|
|
|
|
} else if (ExprType == CastExpr) {
|
|
|
|
// Note that this doesn't parse the subsequence cast-expression.
|
|
|
|
ExprType = CastExpr;
|
|
|
|
} else {
|
2006-08-10 23:56:11 +00:00
|
|
|
Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
|
2006-08-11 06:41:18 +00:00
|
|
|
return ExprResult(true);
|
2006-08-10 23:56:11 +00:00
|
|
|
}
|
2006-08-11 06:41:18 +00:00
|
|
|
return Result;
|
2006-08-11 01:33:00 +00:00
|
|
|
} else {
|
2006-08-11 06:41:18 +00:00
|
|
|
Result = ParseExpression();
|
2006-08-11 01:33:00 +00:00
|
|
|
ExprType = SimpleExpr;
|
2006-08-10 22:01:51 +00:00
|
|
|
}
|
2006-08-10 04:23:57 +00:00
|
|
|
|
2006-08-10 23:14:52 +00:00
|
|
|
// Match the ')'.
|
2006-08-11 06:41:18 +00:00
|
|
|
if (Result.isInvalid)
|
|
|
|
SkipUntil(tok::r_paren);
|
|
|
|
else
|
|
|
|
MatchRHSPunctuation(tok::r_paren, OpenLoc, "(", diag::err_expected_rparen);
|
|
|
|
return Result;
|
2006-08-10 04:23:57 +00:00
|
|
|
}
|