Remove source copies in the parser.

This CL removes the copies of the `Source` and, in some cases, the token
name in the parser implementation.

Change-Id: I91c0797244a19fa5a36d39029d02823a20cee0cd
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/97072
Reviewed-by: Ben Clayton <bclayton@chromium.org>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair 2022-07-26 00:28:37 +00:00 committed by Dawn LUCI CQ
parent 4d1d143977
commit d9a61cedaf
2 changed files with 38 additions and 53 deletions

View File

@ -284,7 +284,7 @@ const Token& ParserImpl::next() {
next_token_idx_++; next_token_idx_++;
} }
} }
last_source_ = tokens_[next_token_idx_].source(); last_source_idx_ = next_token_idx_;
return tokens_[next_token_idx_++]; return tokens_[next_token_idx_++];
} }
@ -325,7 +325,7 @@ void ParserImpl::split_token(Token::Type lhs, Token::Type rhs) {
} }
Source ParserImpl::last_source() const { Source ParserImpl::last_source() const {
return last_source_; return tokens_[last_source_idx_].source();
} }
void ParserImpl::InitializeLex() { void ParserImpl::InitializeLex() {
@ -1306,7 +1306,7 @@ Expect<const ast::Type*> ParserImpl::expect_type_decl_matrix(const Token& t) {
} }
Expect<ast::StorageClass> ParserImpl::expect_storage_class(std::string_view use) { Expect<ast::StorageClass> ParserImpl::expect_storage_class(std::string_view use) {
auto source = peek().source(); auto& t = peek();
auto ident = expect_ident("storage class"); auto ident = expect_ident("storage class");
if (ident.errored) { if (ident.errored) {
return Failure::kErrored; return Failure::kErrored;
@ -1314,33 +1314,32 @@ Expect<ast::StorageClass> ParserImpl::expect_storage_class(std::string_view use)
auto name = ident.value; auto name = ident.value;
if (name == "uniform") { if (name == "uniform") {
return {ast::StorageClass::kUniform, source}; return {ast::StorageClass::kUniform, t.source()};
} }
if (name == "workgroup") { if (name == "workgroup") {
return {ast::StorageClass::kWorkgroup, source}; return {ast::StorageClass::kWorkgroup, t.source()};
} }
if (name == "storage" || name == "storage_buffer") { if (name == "storage" || name == "storage_buffer") {
return {ast::StorageClass::kStorage, source}; return {ast::StorageClass::kStorage, t.source()};
} }
if (name == "private") { if (name == "private") {
return {ast::StorageClass::kPrivate, source}; return {ast::StorageClass::kPrivate, t.source()};
} }
if (name == "function") { if (name == "function") {
return {ast::StorageClass::kFunction, source}; return {ast::StorageClass::kFunction, t.source()};
} }
return add_error(source, "invalid storage class", use); return add_error(t.source(), "invalid storage class", use);
} }
// struct_decl // struct_decl
// : STRUCT IDENT struct_body_decl // : STRUCT IDENT struct_body_decl
Maybe<const ast::Struct*> ParserImpl::struct_decl() { Maybe<const ast::Struct*> ParserImpl::struct_decl() {
auto& t = peek(); auto& t = peek();
auto source = t.source();
if (!match(Token::Type::kStruct)) { if (!match(Token::Type::kStruct)) {
return Failure::kNoMatch; return Failure::kNoMatch;
@ -1357,7 +1356,7 @@ Maybe<const ast::Struct*> ParserImpl::struct_decl() {
} }
auto sym = builder_.Symbols().Register(name.value); auto sym = builder_.Symbols().Register(name.value);
return create<ast::Struct>(source, sym, std::move(body.value), ast::AttributeList{}); return create<ast::Struct>(t.source(), sym, std::move(body.value), ast::AttributeList{});
} }
// struct_body_decl // struct_body_decl
@ -2045,7 +2044,6 @@ Maybe<const ast::CaseStatement*> ParserImpl::switch_body() {
} }
auto& t = next(); auto& t = next();
auto source = t.source();
ast::CaseSelectorList selector_list; ast::CaseSelectorList selector_list;
if (t.Is(Token::Type::kCase)) { if (t.Is(Token::Type::kCase)) {
@ -2070,7 +2068,7 @@ Maybe<const ast::CaseStatement*> ParserImpl::switch_body() {
return add_error(body.source, "expected case body"); return add_error(body.source, "expected case body");
} }
return create<ast::CaseStatement>(source, selector_list, body.value); return create<ast::CaseStatement>(t.source(), selector_list, body.value);
} }
// case_selectors // case_selectors
@ -2305,18 +2303,16 @@ Maybe<const ast::CallStatement*> ParserImpl::func_call_stmt() {
next(); // Consume the first peek next(); // Consume the first peek
auto source = t.source();
auto name = t.to_str();
auto params = expect_argument_expression_list("function call"); auto params = expect_argument_expression_list("function call");
if (params.errored) { if (params.errored) {
return Failure::kErrored; return Failure::kErrored;
} }
return create<ast::CallStatement>( return create<ast::CallStatement>(
source, t.source(),
create<ast::CallExpression>( create<ast::CallExpression>(
source, create<ast::IdentifierExpression>(source, builder_.Symbols().Register(name)), t.source(),
create<ast::IdentifierExpression>(t.source(), builder_.Symbols().Register(t.to_str())),
std::move(params.value))); std::move(params.value)));
} }
@ -2360,7 +2356,6 @@ Maybe<const ast::BlockStatement*> ParserImpl::continuing_stmt() {
// | BITCAST LESS_THAN type_decl GREATER_THAN paren_expression // | BITCAST LESS_THAN type_decl GREATER_THAN paren_expression
Maybe<const ast::Expression*> ParserImpl::primary_expression() { Maybe<const ast::Expression*> ParserImpl::primary_expression() {
auto& t = peek(); auto& t = peek();
auto source = t.source();
auto lit = const_literal(); auto lit = const_literal();
if (lit.errored) { if (lit.errored) {
@ -2392,7 +2387,7 @@ Maybe<const ast::Expression*> ParserImpl::primary_expression() {
return Failure::kErrored; return Failure::kErrored;
} }
return create<ast::BitcastExpression>(source, type.value, params.value); return create<ast::BitcastExpression>(t.source(), type.value, params.value);
} }
if (t.IsIdentifier()) { if (t.IsIdentifier()) {
@ -2407,7 +2402,7 @@ Maybe<const ast::Expression*> ParserImpl::primary_expression() {
return Failure::kErrored; return Failure::kErrored;
} }
return create<ast::CallExpression>(source, ident, std::move(params.value)); return create<ast::CallExpression>(t.source(), ident, std::move(params.value));
} }
return ident; return ident;
@ -2423,7 +2418,7 @@ Maybe<const ast::Expression*> ParserImpl::primary_expression() {
return Failure::kErrored; return Failure::kErrored;
} }
return builder_.Construct(source, type.value, std::move(params.value)); return builder_.Construct(t.source(), type.value, std::move(params.value));
} }
return Failure::kNoMatch; return Failure::kNoMatch;
@ -2591,19 +2586,17 @@ Expect<const ast::Expression*> ParserImpl::expect_multiplicative_expr(const ast:
} }
auto& t = next(); auto& t = next();
auto source = t.source();
auto name = t.to_name();
auto rhs = unary_expression(); auto rhs = unary_expression();
if (rhs.errored) { if (rhs.errored) {
return Failure::kErrored; return Failure::kErrored;
} }
if (!rhs.matched) { if (!rhs.matched) {
return add_error(peek(), return add_error(peek(), "unable to parse right side of " + std::string(t.to_name()) +
"unable to parse right side of " + std::string(name) + " expression"); " expression");
} }
lhs = create<ast::BinaryExpression>(source, op, lhs, rhs.value); lhs = create<ast::BinaryExpression>(t.source(), op, lhs, rhs.value);
} }
return Failure::kErrored; return Failure::kErrored;
} }
@ -2638,7 +2631,6 @@ Expect<const ast::Expression*> ParserImpl::expect_additive_expr(const ast::Expre
} }
auto& t = next(); auto& t = next();
auto source = t.source();
auto rhs = multiplicative_expression(); auto rhs = multiplicative_expression();
if (rhs.errored) { if (rhs.errored) {
@ -2648,7 +2640,7 @@ Expect<const ast::Expression*> ParserImpl::expect_additive_expr(const ast::Expre
return add_error(peek(), "unable to parse right side of + expression"); return add_error(peek(), "unable to parse right side of + expression");
} }
lhs = create<ast::BinaryExpression>(source, op, lhs, rhs.value); lhs = create<ast::BinaryExpression>(t.source(), op, lhs, rhs.value);
} }
return Failure::kErrored; return Failure::kErrored;
} }
@ -2686,7 +2678,6 @@ Expect<const ast::Expression*> ParserImpl::expect_shift_expr(const ast::Expressi
} }
auto& t = next(); auto& t = next();
auto source = t.source();
auto rhs = additive_expression(); auto rhs = additive_expression();
if (rhs.errored) { if (rhs.errored) {
return Failure::kErrored; return Failure::kErrored;
@ -2696,7 +2687,7 @@ Expect<const ast::Expression*> ParserImpl::expect_shift_expr(const ast::Expressi
std::string("unable to parse right side of ") + name + " expression"); std::string("unable to parse right side of ") + name + " expression");
} }
return lhs = create<ast::BinaryExpression>(source, op, lhs, rhs.value); return lhs = create<ast::BinaryExpression>(t.source(), op, lhs, rhs.value);
} }
return Failure::kErrored; return Failure::kErrored;
} }
@ -2737,19 +2728,17 @@ Expect<const ast::Expression*> ParserImpl::expect_relational_expr(const ast::Exp
} }
auto& t = next(); auto& t = next();
auto source = t.source();
auto name = t.to_name();
auto rhs = shift_expression(); auto rhs = shift_expression();
if (rhs.errored) { if (rhs.errored) {
return Failure::kErrored; return Failure::kErrored;
} }
if (!rhs.matched) { if (!rhs.matched) {
return add_error(peek(), return add_error(peek(), "unable to parse right side of " + std::string(t.to_name()) +
"unable to parse right side of " + std::string(name) + " expression"); " expression");
} }
lhs = create<ast::BinaryExpression>(source, op, lhs, rhs.value); lhs = create<ast::BinaryExpression>(t.source(), op, lhs, rhs.value);
} }
return Failure::kErrored; return Failure::kErrored;
} }
@ -2784,19 +2773,17 @@ Expect<const ast::Expression*> ParserImpl::expect_equality_expr(const ast::Expre
} }
auto& t = next(); auto& t = next();
auto source = t.source();
auto name = t.to_name();
auto rhs = relational_expression(); auto rhs = relational_expression();
if (rhs.errored) { if (rhs.errored) {
return Failure::kErrored; return Failure::kErrored;
} }
if (!rhs.matched) { if (!rhs.matched) {
return add_error(peek(), return add_error(peek(), "unable to parse right side of " + std::string(t.to_name()) +
"unable to parse right side of " + std::string(name) + " expression"); " expression");
} }
lhs = create<ast::BinaryExpression>(source, op, lhs, rhs.value); lhs = create<ast::BinaryExpression>(t.source(), op, lhs, rhs.value);
} }
return Failure::kErrored; return Failure::kErrored;
} }
@ -2825,7 +2812,6 @@ Expect<const ast::Expression*> ParserImpl::expect_and_expr(const ast::Expression
} }
auto& t = next(); auto& t = next();
auto source = t.source();
auto rhs = equality_expression(); auto rhs = equality_expression();
if (rhs.errored) { if (rhs.errored) {
@ -2835,7 +2821,7 @@ Expect<const ast::Expression*> ParserImpl::expect_and_expr(const ast::Expression
return add_error(peek(), "unable to parse right side of & expression"); return add_error(peek(), "unable to parse right side of & expression");
} }
lhs = create<ast::BinaryExpression>(source, ast::BinaryOp::kAnd, lhs, rhs.value); lhs = create<ast::BinaryExpression>(t.source(), ast::BinaryOp::kAnd, lhs, rhs.value);
} }
return Failure::kErrored; return Failure::kErrored;
} }
@ -2938,7 +2924,6 @@ Expect<const ast::Expression*> ParserImpl::expect_logical_and_expr(const ast::Ex
} }
auto& t = next(); auto& t = next();
auto source = t.source();
auto rhs = inclusive_or_expression(); auto rhs = inclusive_or_expression();
if (rhs.errored) { if (rhs.errored) {
@ -2948,7 +2933,7 @@ Expect<const ast::Expression*> ParserImpl::expect_logical_and_expr(const ast::Ex
return add_error(peek(), "unable to parse right side of && expression"); return add_error(peek(), "unable to parse right side of && expression");
} }
lhs = create<ast::BinaryExpression>(source, ast::BinaryOp::kLogicalAnd, lhs, rhs.value); lhs = create<ast::BinaryExpression>(t.source(), ast::BinaryOp::kLogicalAnd, lhs, rhs.value);
} }
return Failure::kErrored; return Failure::kErrored;
} }
@ -3042,13 +3027,14 @@ Maybe<ast::BinaryOp> ParserImpl::compound_assignment_operator() {
// assignment_stmt // assignment_stmt
// | lhs_expression ( equal | compound_assignment_operator ) expression // | lhs_expression ( equal | compound_assignment_operator ) expression
// | underscore equal expression // | underscore equal expression
//
// increment_stmt // increment_stmt
// | lhs_expression PLUS_PLUS // | lhs_expression PLUS_PLUS
//
// decrement_stmt // decrement_stmt
// | lhs_expression MINUS_MINUS // | lhs_expression MINUS_MINUS
Maybe<const ast::Statement*> ParserImpl::assignment_stmt() { Maybe<const ast::Statement*> ParserImpl::assignment_stmt() {
auto& t = peek(); auto& t = peek();
auto source = t.source();
// tint:295 - Test for `ident COLON` - this is invalid grammar, and without // tint:295 - Test for `ident COLON` - this is invalid grammar, and without
// special casing will error as "missing = for assignment", which is less // special casing will error as "missing = for assignment", which is less
@ -3062,6 +3048,7 @@ Maybe<const ast::Statement*> ParserImpl::assignment_stmt() {
return Failure::kErrored; return Failure::kErrored;
} }
if (!lhs.matched) { if (!lhs.matched) {
Source source = t.source();
if (!match(Token::Type::kUnderscore, &source)) { if (!match(Token::Type::kUnderscore, &source)) {
return Failure::kNoMatch; return Failure::kNoMatch;
} }
@ -3073,9 +3060,9 @@ Maybe<const ast::Statement*> ParserImpl::assignment_stmt() {
// the assignment statement, and we cannot tell which we are parsing until we // the assignment statement, and we cannot tell which we are parsing until we
// hit the ++/--/= token. // hit the ++/--/= token.
if (match(Token::Type::kPlusPlus)) { if (match(Token::Type::kPlusPlus)) {
return create<ast::IncrementDecrementStatement>(source, lhs.value, true); return create<ast::IncrementDecrementStatement>(t.source(), lhs.value, true);
} else if (match(Token::Type::kMinusMinus)) { } else if (match(Token::Type::kMinusMinus)) {
return create<ast::IncrementDecrementStatement>(source, lhs.value, false); return create<ast::IncrementDecrementStatement>(t.source(), lhs.value, false);
} }
auto compound_op = compound_assignment_operator(); auto compound_op = compound_assignment_operator();
@ -3097,10 +3084,10 @@ Maybe<const ast::Statement*> ParserImpl::assignment_stmt() {
} }
if (compound_op.value != ast::BinaryOp::kNone) { if (compound_op.value != ast::BinaryOp::kNone) {
return create<ast::CompoundAssignmentStatement>(source, lhs.value, rhs.value, return create<ast::CompoundAssignmentStatement>(t.source(), lhs.value, rhs.value,
compound_op.value); compound_op.value);
} else { } else {
return create<ast::AssignmentStatement>(source, lhs.value, rhs.value); return create<ast::AssignmentStatement>(t.source(), lhs.value, rhs.value);
} }
} }
@ -3444,8 +3431,6 @@ bool ParserImpl::expect(std::string_view use, Token::Type tok) {
next(); next();
// Push the second character to the token queue. // Push the second character to the token queue.
auto source = t.source();
source.range.begin.column++;
if (t.Is(Token::Type::kShiftRight)) { if (t.Is(Token::Type::kShiftRight)) {
split_token(Token::Type::kGreaterThan, Token::Type::kGreaterThan); split_token(Token::Type::kGreaterThan, Token::Type::kGreaterThan);
} else if (t.Is(Token::Type::kGreaterThanEqual)) { } else if (t.Is(Token::Type::kGreaterThanEqual)) {

View File

@ -845,7 +845,7 @@ class ParserImpl {
Source::File const* const file_; Source::File const* const file_;
std::vector<Token> tokens_; std::vector<Token> tokens_;
size_t next_token_idx_ = 0; size_t next_token_idx_ = 0;
Source last_source_; size_t last_source_idx_ = 0;
bool synchronized_ = true; bool synchronized_ = true;
uint32_t parse_depth_ = 0; uint32_t parse_depth_ = 0;
std::vector<Token::Type> sync_tokens_; std::vector<Token::Type> sync_tokens_;