tint/reader/wgsl: Have expect_identifier return an ast::Identifier

Instead of a std::string. This avoids unnecessary string allocations, and follows the pattern of all other AST nodes.

Change-Id: I3faf534090a2033d671b2ef463d8b9ed3e47eecd
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/128300
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
This commit is contained in:
Ben Clayton 2023-04-20 20:08:44 +00:00 committed by Dawn LUCI CQ
parent 91a23a3ac3
commit 7eb37948ab
5 changed files with 52 additions and 61 deletions

View File

@ -182,10 +182,8 @@ ParserImpl::TypedIdentifier::TypedIdentifier() = default;
ParserImpl::TypedIdentifier::TypedIdentifier(const TypedIdentifier&) = default;
ParserImpl::TypedIdentifier::TypedIdentifier(ast::Type type_in,
std::string name_in,
Source source_in)
: type(type_in), name(std::move(name_in)), source(std::move(source_in)) {}
ParserImpl::TypedIdentifier::TypedIdentifier(ast::Type type_in, const ast::Identifier* name_in)
: type(type_in), name(name_in) {}
ParserImpl::TypedIdentifier::~TypedIdentifier() = default;
@ -194,7 +192,7 @@ ParserImpl::FunctionHeader::FunctionHeader() = default;
ParserImpl::FunctionHeader::FunctionHeader(const FunctionHeader&) = default;
ParserImpl::FunctionHeader::FunctionHeader(Source src,
std::string n,
const ast::Identifier* n,
utils::VectorRef<const ast::Parameter*> p,
ast::Type ret_ty,
utils::VectorRef<const ast::Attribute*> ret_attrs)
@ -725,15 +723,14 @@ Maybe<const ast::Variable*> ParserImpl::global_constant_decl(AttributeList& attr
}
TINT_DEFER(attrs.Clear());
if (is_overridable) {
return builder_.Override(decl->source, // source
return builder_.Override(decl->name->source, // source
decl->name, // symbol
decl->type, // type
initializer, // initializer
std::move(attrs)); // attributes
}
return builder_.GlobalConst(decl->source, // source
return builder_.GlobalConst(decl->name->source, // source
decl->name, // symbol
decl->type, // type
initializer, // initializer
@ -765,7 +762,7 @@ Maybe<ParserImpl::VarDeclInfo> ParserImpl::variable_decl() {
return Failure::kErrored;
}
return VarDeclInfo{decl->source, decl->name, vq.address_space, vq.access, decl->type};
return VarDeclInfo{decl->name->source, decl->name, vq.address_space, vq.access, decl->type};
}
Expect<ParserImpl::TypedIdentifier> ParserImpl::expect_ident_with_optional_type_specifier(
@ -777,7 +774,7 @@ Expect<ParserImpl::TypedIdentifier> ParserImpl::expect_ident_with_optional_type_
}
if (allow_inferred && !peek_is(Token::Type::kColon)) {
return TypedIdentifier{ast::Type{}, ident.value, ident.source};
return TypedIdentifier{ast::Type{}, ident.value};
}
if (!expect(use, Token::Type::kColon)) {
@ -793,7 +790,7 @@ Expect<ParserImpl::TypedIdentifier> ParserImpl::expect_ident_with_optional_type_
return add_error(t.source(), "invalid type", use);
}
return TypedIdentifier{type.value, ident.value, ident.source};
return TypedIdentifier{type.value, ident.value};
}
// optionally_typed_ident
@ -1009,7 +1006,7 @@ Expect<const ast::StructMember*> ParserImpl::expect_struct_member() {
return Failure::kErrored;
}
return builder_.Member(decl->source, decl->name, decl->type, std::move(attrs.value));
return builder_.Member(decl->name->source, decl->name, decl->type, std::move(attrs.value));
}
// const_assert_statement
@ -1126,8 +1123,7 @@ Maybe<ParserImpl::FunctionHeader> ParserImpl::function_header() {
}
return FunctionHeader{
source, std::move(name.value), std::move(params.value),
return_type, std::move(return_attributes),
source, name.value, std::move(params.value), return_type, std::move(return_attributes),
};
}
@ -1167,7 +1163,7 @@ Expect<const ast::Parameter*> ParserImpl::expect_param() {
return Failure::kErrored;
}
return builder_.Param(decl->source, // source
return builder_.Param(decl->name->source, // source
decl->name, // symbol
decl->type, // type
std::move(attrs.value)); // attributes
@ -1449,7 +1445,7 @@ Maybe<const ast::VariableDeclStatement*> ParserImpl::variable_statement() {
return add_error(peek(), "missing initializer for 'const' declaration");
}
auto* const_ = builder_.Const(typed_ident->source, // source
auto* const_ = builder_.Const(typed_ident->name->source, // source
typed_ident->name, // symbol
typed_ident->type, // type
initializer.value); // initializer
@ -1477,7 +1473,7 @@ Maybe<const ast::VariableDeclStatement*> ParserImpl::variable_statement() {
return add_error(peek(), "missing initializer for 'let' declaration");
}
auto* let = builder_.Let(typed_ident->source, // source
auto* let = builder_.Let(typed_ident->name->source, // source
typed_ident->name, // symbol
typed_ident->type, // type
initializer.value); // initializer
@ -2152,8 +2148,7 @@ Maybe<const ast::Expression*> ParserImpl::component_or_swizzle_specifier(
return Failure::kErrored;
}
prefix = builder_.MemberAccessor(ident.source, prefix,
builder_.Ident(ident.source, ident.value));
prefix = builder_.MemberAccessor(ident.source, prefix, ident.value);
continue;
}
@ -3115,8 +3110,7 @@ Expect<ast::DiagnosticControl> ParserImpl::expect_diagnostic_control() {
}
match(Token::Type::kComma);
return ast::DiagnosticControl(severity_control.value,
builder_.Ident(rule_name.source, rule_name.value));
return ast::DiagnosticControl(severity_control.value, rule_name.value);
});
}
@ -3220,7 +3214,7 @@ Expect<uint32_t> ParserImpl::expect_nonzero_positive_sint(std::string_view use)
return {static_cast<uint32_t>(sint.value), sint.source};
}
Expect<std::string> ParserImpl::expect_ident(std::string_view use) {
Expect<const ast::Identifier*> ParserImpl::expect_ident(std::string_view use) {
auto& t = peek();
if (t.IsIdentifier()) {
synchronized_ = true;
@ -3230,7 +3224,7 @@ Expect<std::string> ParserImpl::expect_ident(std::string_view use) {
return add_error(t.source(), "'" + t.to_str() + "' is a reserved keyword");
}
return {t.to_str(), t.source()};
return builder_.Ident(t.source(), t.to_str());
}
if (handle_error(t)) {
return Failure::kErrored;

View File

@ -211,17 +211,14 @@ class ParserImpl {
/// Constructor
/// @param type_in parsed type
/// @param name_in parsed identifier
/// @param source_in source to the identifier
TypedIdentifier(ast::Type type_in, std::string name_in, Source source_in);
TypedIdentifier(ast::Type type_in, const ast::Identifier* name_in);
/// Destructor
~TypedIdentifier();
/// Parsed type. type.expr be nullptr for inferred types.
ast::Type type;
/// Parsed identifier.
std::string name;
/// Source to the identifier.
Source source;
const ast::Identifier* name = nullptr;
};
/// FunctionHeader contains the parsed information for a function header.
@ -238,7 +235,7 @@ class ParserImpl {
/// @param ret_ty function return type
/// @param ret_attrs return type attributes
FunctionHeader(Source src,
std::string n,
const ast::Identifier* n,
utils::VectorRef<const ast::Parameter*> p,
ast::Type ret_ty,
utils::VectorRef<const ast::Attribute*> ret_attrs);
@ -252,7 +249,7 @@ class ParserImpl {
/// Parsed header source
Source source;
/// Function name
std::string name;
const ast::Identifier* name;
/// Function parameters
utils::Vector<const ast::Parameter*, 8> params;
/// Function return type
@ -266,7 +263,7 @@ class ParserImpl {
/// Variable declaration source
Source source;
/// Variable name
std::string name;
const ast::Identifier* name = nullptr;
/// Variable address space
const ast::Expression* address_space = nullptr;
/// Variable access control
@ -698,7 +695,7 @@ class ParserImpl {
/// Consumes the next token on match.
/// @param use a description of what was being parsed if an error was raised
/// @returns the parsed identifier.
Expect<std::string> expect_ident(std::string_view use);
Expect<const ast::Identifier*> expect_ident(std::string_view use);
/// Parses a lexical block starting with the token `start` and ending with
/// the token `end`. `body` is called to parse the lexical block body
/// between the `start` and `end` tokens. If the `start` or `end` tokens

View File

@ -25,7 +25,7 @@ TEST_F(ParserImplTest, FunctionHeader) {
EXPECT_TRUE(f.matched);
EXPECT_FALSE(f.errored);
EXPECT_EQ(f->name, "main");
ast::CheckIdentifier(f->name, "main");
ASSERT_EQ(f->params.Length(), 2u);
EXPECT_EQ(f->params[0]->name->symbol, p->builder().Symbols().Get("a"));
EXPECT_EQ(f->params[1]->name->symbol, p->builder().Symbols().Get("b"));
@ -38,7 +38,7 @@ TEST_F(ParserImplTest, FunctionHeader_TrailingComma) {
EXPECT_TRUE(f.matched);
EXPECT_FALSE(f.errored);
EXPECT_EQ(f->name, "main");
ast::CheckIdentifier(f->name, "main");
ASSERT_EQ(f->params.Length(), 1u);
EXPECT_EQ(f->params[0]->name->symbol, p->builder().Symbols().Get("a"));
EXPECT_EQ(f->return_type, nullptr);
@ -51,7 +51,7 @@ TEST_F(ParserImplTest, FunctionHeader_AttributeReturnType) {
EXPECT_TRUE(f.matched);
EXPECT_FALSE(f.errored);
EXPECT_EQ(f->name, "main");
ast::CheckIdentifier(f->name, "main");
EXPECT_EQ(f->params.Length(), 0u);
ast::CheckIdentifier(f->return_type, "f32");
ASSERT_EQ(f->return_type_attributes.Length(), 1u);
@ -70,7 +70,7 @@ TEST_F(ParserImplTest, FunctionHeader_InvariantReturnType) {
EXPECT_TRUE(f.matched);
EXPECT_FALSE(f.errored);
EXPECT_EQ(f->name, "main");
ast::CheckIdentifier(f->name, "main");
EXPECT_EQ(f->params.Length(), 0u);
ast::CheckIdentifier(f->return_type, "f32");
ASSERT_EQ(f->return_type_attributes.Length(), 1u);

View File

@ -23,7 +23,7 @@ TEST_F(ParserImplTest, VariableDecl_Parses) {
EXPECT_FALSE(p->has_error());
EXPECT_TRUE(v.matched);
EXPECT_FALSE(v.errored);
EXPECT_EQ(v->name, "my_var");
ast::CheckIdentifier(v->name, "my_var");
EXPECT_NE(v->type, nullptr);
ast::CheckIdentifier(v->type, "f32");
@ -43,7 +43,7 @@ TEST_F(ParserImplTest, VariableDecl_Unicode_Parses) {
EXPECT_FALSE(p->has_error());
EXPECT_TRUE(v.matched);
EXPECT_FALSE(v.errored);
EXPECT_EQ(v->name, ident);
ast::CheckIdentifier(v->name, ident);
EXPECT_NE(v->type, nullptr);
ast::CheckIdentifier(v->type, "f32");
@ -58,7 +58,7 @@ TEST_F(ParserImplTest, VariableDecl_Inferred_Parses) {
EXPECT_FALSE(p->has_error());
EXPECT_TRUE(v.matched);
EXPECT_FALSE(v.errored);
EXPECT_EQ(v->name, "my_var");
ast::CheckIdentifier(v->name, "my_var");
EXPECT_EQ(v->type, nullptr);
EXPECT_EQ(v->source.range, (Source::Range{{1u, 5u}, {1u, 11u}}));
@ -81,7 +81,7 @@ TEST_F(ParserImplTest, VariableDecl_WithAddressSpace) {
EXPECT_TRUE(v.matched);
EXPECT_FALSE(v.errored);
EXPECT_FALSE(p->has_error());
EXPECT_EQ(v->name, "my_var");
ast::CheckIdentifier(v->name, "my_var");
ast::CheckIdentifier(v->type, "f32");
ast::CheckIdentifier(v->address_space, "private");
@ -98,7 +98,7 @@ TEST_F(ParserImplTest, VariableDecl_WithPushConstant) {
EXPECT_TRUE(v.matched);
EXPECT_FALSE(v.errored);
EXPECT_FALSE(p->has_error());
EXPECT_EQ(v->name, "my_var");
ast::CheckIdentifier(v->name, "my_var");
ast::CheckIdentifier(v->type, "f32");
ast::CheckIdentifier(v->address_space, "push_constant");

View File

@ -23,11 +23,11 @@ TEST_F(ParserImplTest, VariableIdentDecl_Parses) {
auto decl = p->expect_ident_with_type_specifier("test");
ASSERT_FALSE(p->has_error()) << p->error();
ASSERT_FALSE(decl.errored);
ASSERT_EQ(decl->name, "my_var");
ast::CheckIdentifier(decl->name, "my_var");
ASSERT_NE(decl->type, nullptr);
ast::CheckIdentifier(decl->type, "f32");
EXPECT_EQ(decl->source.range, (Source::Range{{1u, 1u}, {1u, 7u}}));
EXPECT_EQ(decl->name->source.range, (Source::Range{{1u, 1u}, {1u, 7u}}));
EXPECT_EQ(decl->type->source.range, (Source::Range{{1u, 10u}, {1u, 13u}}));
}
@ -36,11 +36,11 @@ TEST_F(ParserImplTest, VariableIdentDecl_Parses_AllowInferredType) {
auto decl = p->expect_optionally_typed_ident("test");
ASSERT_FALSE(p->has_error()) << p->error();
ASSERT_FALSE(decl.errored);
ASSERT_EQ(decl->name, "my_var");
ast::CheckIdentifier(decl->name, "my_var");
ASSERT_NE(decl->type, nullptr);
ast::CheckIdentifier(decl->type, "f32");
EXPECT_EQ(decl->source.range, (Source::Range{{1u, 1u}, {1u, 7u}}));
EXPECT_EQ(decl->name->source.range, (Source::Range{{1u, 1u}, {1u, 7u}}));
EXPECT_EQ(decl->type->source.range, (Source::Range{{1u, 10u}, {1u, 13u}}));
}
@ -56,10 +56,10 @@ TEST_F(ParserImplTest, VariableIdentDecl_Inferred_Parses_AllowInferredType) {
auto decl = p->expect_optionally_typed_ident("test");
ASSERT_FALSE(p->has_error()) << p->error();
ASSERT_FALSE(decl.errored);
ASSERT_EQ(decl->name, "my_var");
ast::CheckIdentifier(decl->name, "my_var");
ASSERT_EQ(decl->type, nullptr);
EXPECT_EQ(decl->source.range, (Source::Range{{1u, 1u}, {1u, 7u}}));
EXPECT_EQ(decl->name->source.range, (Source::Range{{1u, 1u}, {1u, 7u}}));
}
TEST_F(ParserImplTest, VariableIdentDecl_MissingIdent) {