ast: Merge DecoratedVariable into Variable

Remove all Variable setters (with exception to set_storage_class() which is called by the TypeDeterminer)

Bug: tint:390
Change-Id: I172667e21e2b02e85dcea6703aa1e608ec718250
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35015
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2020-12-11 13:07:02 +00:00
committed by Commit Bot service account
parent ccc67252ff
commit a80511e021
79 changed files with 4259 additions and 2268 deletions

View File

@@ -803,9 +803,9 @@ bool FunctionEmitter::ParseFunctionDeclaration(FunctionDeclaration* decl) {
auto* ast_type = parser_impl_.ConvertType(param->type_id());
if (ast_type != nullptr) {
auto* ast_param = parser_impl_.MakeVariable(
param->result_id(), ast::StorageClass::kNone, ast_type);
param->result_id(), ast::StorageClass::kNone, ast_type, true,
nullptr, ast::VariableDecorationList{});
// Parameters are treated as const declarations.
ast_param->set_is_const(true);
ast_params.emplace_back(ast_param);
// The value is accessible by name.
identifier_values_.insert(param->result_id());
@@ -1864,16 +1864,18 @@ bool FunctionEmitter::EmitFunctionVariables() {
if (failed()) {
return false;
}
auto* var = parser_impl_.MakeVariable(
inst.result_id(), ast::StorageClass::kFunction, var_store_type);
ast::Expression* constructor = nullptr;
if (inst.NumInOperands() > 1) {
// SPIR-V initializers are always constants.
// (OpenCL also allows the ID of an OpVariable, but we don't handle that
// here.)
var->set_constructor(
constructor =
parser_impl_.MakeConstantExpression(inst.GetSingleWordInOperand(1))
.expr);
.expr;
}
auto* var = parser_impl_.MakeVariable(
inst.result_id(), ast::StorageClass::kFunction, var_store_type, false,
constructor, ast::VariableDecorationList{});
auto* var_decl_stmt = create<ast::VariableDeclStatement>(var);
AddStatementForInstruction(var_decl_stmt, inst);
// Save this as an already-named value.
@@ -2141,10 +2143,14 @@ bool FunctionEmitter::EmitIfStart(const BlockInfo& block_info) {
const std::string guard_name = block_info.flow_guard_name;
if (!guard_name.empty()) {
// Declare the guard variable just before the "if", initialized to true.
auto* guard_var = create<ast::Variable>(Source{}, guard_name,
ast::StorageClass::kFunction,
parser_impl_.Bool());
guard_var->set_constructor(MakeTrue());
auto* guard_var =
create<ast::Variable>(Source{}, // source
guard_name, // name
ast::StorageClass::kFunction, // storage_class
parser_impl_.Bool(), // type
false, // is_const
MakeTrue(), // constructor
ast::VariableDecorationList{}); // decorations
auto* guard_decl = create<ast::VariableDeclStatement>(guard_var);
AddStatement(guard_decl);
}
@@ -2674,8 +2680,9 @@ bool FunctionEmitter::EmitStatementsInBasicBlock(const BlockInfo& block_info,
assert(def_inst);
auto* ast_type =
RemapStorageClass(parser_impl_.ConvertType(def_inst->type_id()), id);
AddStatement(create<ast::VariableDeclStatement>(
parser_impl_.MakeVariable(id, ast::StorageClass::kFunction, ast_type)));
AddStatement(create<ast::VariableDeclStatement>(parser_impl_.MakeVariable(
id, ast::StorageClass::kFunction, ast_type, false, nullptr,
ast::VariableDecorationList{})));
// Save this as an already-named value.
identifier_values_.insert(id);
}
@@ -2686,8 +2693,13 @@ bool FunctionEmitter::EmitStatementsInBasicBlock(const BlockInfo& block_info,
const auto phi_var_name = GetDefInfo(id)->phi_var;
assert(!phi_var_name.empty());
auto* var = create<ast::Variable>(
Source{}, phi_var_name, ast::StorageClass::kFunction,
parser_impl_.ConvertType(def_inst->type_id()));
Source{}, // source
phi_var_name, // name
ast::StorageClass::kFunction, // storage_class
parser_impl_.ConvertType(def_inst->type_id()), // type
false, // is_const
nullptr, // constructor
ast::VariableDecorationList{}); // decorations
AddStatement(create<ast::VariableDeclStatement>(var));
}
@@ -2734,12 +2746,11 @@ bool FunctionEmitter::EmitConstDefinition(
return false;
}
auto* ast_const = parser_impl_.MakeVariable(
inst.result_id(), ast::StorageClass::kNone, ast_expr.type);
inst.result_id(), ast::StorageClass::kNone, ast_expr.type, true,
ast_expr.expr, ast::VariableDecorationList{});
if (!ast_const) {
return false;
}
ast_const->set_constructor(ast_expr.expr);
ast_const->set_is_const(true);
AddStatementForInstruction(create<ast::VariableDeclStatement>(ast_const),
inst);
// Save this as an already-named value.

View File

@@ -41,7 +41,6 @@
#include "src/ast/builtin.h"
#include "src/ast/builtin_decoration.h"
#include "src/ast/constant_id_decoration.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/float_literal.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/set_decoration.h"
@@ -1064,8 +1063,6 @@ bool ParserImpl::EmitScalarSpecConstants() {
break;
}
if (ast_type && ast_expr) {
auto* ast_var =
MakeVariable(inst.result_id(), ast::StorageClass::kNone, ast_type);
ast::VariableDecorationList spec_id_decos;
for (const auto& deco : GetDecorationsFor(inst.result_id())) {
if ((deco.size() == 2) && (deco[0] == SpvDecorationSpecId)) {
@@ -1074,18 +1071,10 @@ bool ParserImpl::EmitScalarSpecConstants() {
break;
}
}
if (spec_id_decos.empty()) {
// Register it as a named constant, without specialization id.
ast_var->set_is_const(true);
ast_var->set_constructor(ast_expr);
ast_module_.AddGlobalVariable(ast_var);
} else {
auto* ast_deco_var = create<ast::DecoratedVariable>(ast_var);
ast_deco_var->set_is_const(true);
ast_deco_var->set_constructor(ast_expr);
ast_deco_var->set_decorations(std::move(spec_id_decos));
ast_module_.AddGlobalVariable(ast_deco_var);
}
auto* ast_var =
MakeVariable(inst.result_id(), ast::StorageClass::kNone, ast_type,
true, ast_expr, std::move(spec_id_decos));
ast_module_.AddGlobalVariable(ast_var);
scalar_spec_constants_.insert(inst.result_id());
}
}
@@ -1190,15 +1179,17 @@ bool ParserImpl::EmitModuleScopeVariables() {
auto* ast_store_type = ast_type->As<ast::type::Pointer>()->type();
auto ast_storage_class =
ast_type->As<ast::type::Pointer>()->storage_class();
auto* ast_var =
MakeVariable(var.result_id(), ast_storage_class, ast_store_type);
ast::Expression* ast_constructor = nullptr;
if (var.NumInOperands() > 1) {
// SPIR-V initializers are always constants.
// (OpenCL also allows the ID of an OpVariable, but we don't handle that
// here.)
ast_var->set_constructor(
MakeConstantExpression(var.GetSingleWordInOperand(1)).expr);
ast_constructor =
MakeConstantExpression(var.GetSingleWordInOperand(1)).expr;
}
auto* ast_var =
MakeVariable(var.result_id(), ast_storage_class, ast_store_type, false,
ast_constructor, ast::VariableDecorationList{});
// TODO(dneto): initializers (a.k.a. constructor expression)
ast_module_.AddGlobalVariable(ast_var);
}
@@ -1208,23 +1199,26 @@ bool ParserImpl::EmitModuleScopeVariables() {
// Make sure the variable has a name.
namer_.SuggestSanitizedName(builtin_position_.per_vertex_var_id,
"gl_Position");
auto* var = create<ast::DecoratedVariable>(MakeVariable(
auto* var = MakeVariable(
builtin_position_.per_vertex_var_id,
enum_converter_.ToStorageClass(builtin_position_.storage_class),
ConvertType(builtin_position_.member_type_id)));
ast::VariableDecorationList decos;
decos.push_back(
create<ast::BuiltinDecoration>(ast::Builtin::kPosition, Source{}));
var->set_decorations(std::move(decos));
ConvertType(builtin_position_.member_type_id), false, nullptr,
ast::VariableDecorationList{
create<ast::BuiltinDecoration>(ast::Builtin::kPosition, Source{}),
});
ast_module_.AddGlobalVariable(var);
}
return success_;
}
ast::Variable* ParserImpl::MakeVariable(uint32_t id,
ast::StorageClass sc,
ast::type::Type* type) {
ast::Variable* ParserImpl::MakeVariable(
uint32_t id,
ast::StorageClass sc,
ast::type::Type* type,
bool is_const,
ast::Expression* constructor,
ast::VariableDecorationList decorations) {
if (type == nullptr) {
Fail() << "internal error: can't make ast::Variable for null type";
return nullptr;
@@ -1238,9 +1232,6 @@ ast::Variable* ParserImpl::MakeVariable(uint32_t id,
type = ast_module_.create<ast::type::AccessControl>(access, type);
}
auto* ast_var = create<ast::Variable>(Source{}, namer_.Name(id), sc, type);
ast::VariableDecorationList ast_decorations;
for (auto& deco : GetDecorationsFor(id)) {
if (deco.empty()) {
Fail() << "malformed decoration on ID " << id << ": it is empty";
@@ -1257,7 +1248,7 @@ ast::Variable* ParserImpl::MakeVariable(uint32_t id,
if (ast_builtin == ast::Builtin::kNone) {
return nullptr;
}
ast_decorations.emplace_back(
decorations.emplace_back(
create<ast::BuiltinDecoration>(ast_builtin, Source{}));
}
if (deco[0] == SpvDecorationLocation) {
@@ -1266,7 +1257,7 @@ ast::Variable* ParserImpl::MakeVariable(uint32_t id,
<< ": requires one literal operand";
return nullptr;
}
ast_decorations.emplace_back(
decorations.emplace_back(
create<ast::LocationDecoration>(deco[1], Source{}));
}
if (deco[0] == SpvDecorationDescriptorSet) {
@@ -1275,8 +1266,7 @@ ast::Variable* ParserImpl::MakeVariable(uint32_t id,
<< ": has no operand";
return nullptr;
}
ast_decorations.emplace_back(
create<ast::SetDecoration>(deco[1], Source{}));
decorations.emplace_back(create<ast::SetDecoration>(deco[1], Source{}));
}
if (deco[0] == SpvDecorationBinding) {
if (deco.size() == 1) {
@@ -1284,16 +1274,18 @@ ast::Variable* ParserImpl::MakeVariable(uint32_t id,
<< ": has no operand";
return nullptr;
}
ast_decorations.emplace_back(
decorations.emplace_back(
create<ast::BindingDecoration>(deco[1], Source{}));
}
}
if (!ast_decorations.empty()) {
auto* decorated_var = create<ast::DecoratedVariable>(ast_var);
decorated_var->set_decorations(std::move(ast_decorations));
ast_var = std::move(decorated_var);
}
return ast_var;
return create<ast::Variable>(Source{}, // source
namer_.Name(id), // name
sc, // storage_class
type, // type
is_const, // is_const
constructor, // constructor
decorations); // decorations
}
TypedExpression ParserImpl::MakeConstantExpression(uint32_t id) {

View File

@@ -291,10 +291,16 @@ class ParserImpl : Reader {
/// @param id the SPIR-V result ID
/// @param sc the storage class, which cannot be ast::StorageClass::kNone
/// @param type the type
/// @param is_const if true, the variable is const
/// @param constructor the variable constructor
/// @param decorations the variable decorations
/// @returns a new Variable node, or null in the error case
ast::Variable* MakeVariable(uint32_t id,
ast::StorageClass sc,
ast::type::Type* type);
ast::type::Type* type,
bool is_const,
ast::Expression* constructor,
ast::VariableDecorationList decorations);
/// Creates an AST expression node for a SPIR-V constant.
/// @param id the SPIR-V ID of the constant

View File

@@ -1141,7 +1141,7 @@ INSTANTIATE_TEST_SUITE_P(Samplers,
%10 = OpVariable %ptr UniformConstant
)",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1159,7 +1159,7 @@ INSTANTIATE_TEST_SUITE_P(Images,
%10 = OpVariable %ptr_f_texture_1d UniformConstant
)",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1175,7 +1175,7 @@ INSTANTIATE_TEST_SUITE_P(Images,
%10 = OpVariable %ptr_f_storage_1d UniformConstant
)",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1191,7 +1191,7 @@ INSTANTIATE_TEST_SUITE_P(Images,
%10 = OpVariable %ptr_f_storage_1d UniformConstant
)",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1343,7 +1343,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleImplicitLod "
"%v4float %sampled_image %coords12",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1352,7 +1352,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1376,7 +1376,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleImplicitLod "
"%v4float %sampled_image %coords123",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1385,7 +1385,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1420,7 +1420,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleImplicitLod "
"%v4float %sampled_image %coords12 ConstOffset %offsets2d",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1429,7 +1429,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1455,7 +1455,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleImplicitLod "
"%v4float %sampled_image %coords123 ConstOffset %offsets2d",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1464,7 +1464,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1499,7 +1499,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleImplicitLod "
"%v4float %sampled_image %coords12 Bias %float_7",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1508,7 +1508,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1533,7 +1533,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleImplicitLod "
"%v4float %sampled_image %coords123 Bias %float_7",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1542,7 +1542,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1580,7 +1580,7 @@ INSTANTIATE_TEST_SUITE_P(
"%v4float %sampled_image %coords12 Bias|ConstOffset "
"%float_7 %offsets2d",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1589,7 +1589,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1615,7 +1615,7 @@ INSTANTIATE_TEST_SUITE_P(
"%v4float %sampled_image %coords123 Bias|ConstOffset "
"%float_7 %offsets2d",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1624,7 +1624,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1672,7 +1672,7 @@ INSTANTIATE_TEST_SUITE_P(
%210 = OpImageSampleDrefImplicitLod %v4float %sampled_dref_image %coords12 %depth
)",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1681,7 +1681,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1690,7 +1690,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__depth_texture_2d
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{1}
@@ -1745,7 +1745,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleDrefImplicitLod "
"%v4float %sampled_image %coords12 %depth",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1754,7 +1754,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_comparison
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1778,7 +1778,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleDrefImplicitLod "
"%v4float %sampled_image %coords123 %depth",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1787,7 +1787,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_comparison
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1822,7 +1822,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleDrefImplicitLod %v4float "
"%sampled_image %coords12 %depth ConstOffset %offsets2d",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1831,7 +1831,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_comparison
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1857,7 +1857,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleDrefImplicitLod %v4float "
"%sampled_image %coords123 %depth ConstOffset %offsets2d",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1866,7 +1866,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_comparison
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1907,7 +1907,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleExplicitLod "
"%v4float %sampled_image %coords12 Lod %float_null",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1916,7 +1916,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1941,7 +1941,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleExplicitLod "
"%v4float %sampled_image %coords123 Lod %float_null",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1950,7 +1950,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -1988,7 +1988,7 @@ INSTANTIATE_TEST_SUITE_P(
"%v4float %sampled_image %coords12 Lod|ConstOffset "
"%float_null %offsets2d",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -1997,7 +1997,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2024,7 +2024,7 @@ INSTANTIATE_TEST_SUITE_P(
"%v4float %sampled_image %coords123 Lod|ConstOffset "
"%float_null %offsets2d",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -2033,7 +2033,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2070,7 +2070,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleExplicitLod "
"%v4float %sampled_image %coords12 Grad %float_7 %float_null",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -2079,7 +2079,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2106,7 +2106,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleExplicitLod "
"%v4float %sampled_image %coords123 Grad %float_7 %float_null",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -2115,7 +2115,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2154,7 +2154,7 @@ INSTANTIATE_TEST_SUITE_P(
"%v4float %sampled_image %coords12 Grad|ConstOffset "
"%float_7 %float_null %offsets2d",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -2163,7 +2163,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2192,7 +2192,7 @@ INSTANTIATE_TEST_SUITE_P(
"%v4float %sampled_image %coords123 Grad|ConstOffset "
"%float_7 %float_null %offsets2d",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -2201,7 +2201,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2248,7 +2248,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleExplicitLod %v4float "
"%sampled_image %vf12 Lod %f1",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -2257,7 +2257,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2281,7 +2281,7 @@ INSTANTIATE_TEST_SUITE_P(
"%result = OpImageSampleExplicitLod %v4float "
"%sampled_image %vf12 Lod %f1",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -2290,7 +2290,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2387,7 +2387,7 @@ INSTANTIATE_TEST_SUITE_P(
// OpImageWrite with no extra params
{"%float 2D 0 0 0 2 Rgba32f", "OpImageWrite %im %vu12 %vf1234",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2408,7 +2408,7 @@ INSTANTIATE_TEST_SUITE_P(
{"%float 2D 0 0 0 2 Rgba32f",
"OpImageWrite %im %vu12 %vf1234 ConstOffset %offsets2d",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2437,7 +2437,7 @@ INSTANTIATE_TEST_SUITE_P(
// Source 1 component, dest 1 component
{"%float 2D 0 0 0 2 R32f", "OpImageWrite %im %vu12 %f1",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2457,7 +2457,7 @@ INSTANTIATE_TEST_SUITE_P(
// Source 2 component, dest 1 component
{"%float 2D 0 0 0 2 R32f", "OpImageWrite %im %vu12 %vf12",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2480,7 +2480,7 @@ INSTANTIATE_TEST_SUITE_P(
// Source 3 component, dest 1 component
{"%float 2D 0 0 0 2 R32f", "OpImageWrite %im %vu12 %vf123",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2503,7 +2503,7 @@ INSTANTIATE_TEST_SUITE_P(
// Source 4 component, dest 1 component
{"%float 2D 0 0 0 2 R32f", "OpImageWrite %im %vu12 %vf1234",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2526,7 +2526,7 @@ INSTANTIATE_TEST_SUITE_P(
// Source 2 component, dest 2 component
{"%float 2D 0 0 0 2 Rg32f", "OpImageWrite %im %vu12 %vf12",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2546,7 +2546,7 @@ INSTANTIATE_TEST_SUITE_P(
// Source 3 component, dest 2 component
{"%float 2D 0 0 0 2 Rg32f", "OpImageWrite %im %vu12 %vf123",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2569,7 +2569,7 @@ INSTANTIATE_TEST_SUITE_P(
// Source 4 component, dest 2 component
{"%float 2D 0 0 0 2 Rg32f", "OpImageWrite %im %vu12 %vf1234",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2593,7 +2593,7 @@ INSTANTIATE_TEST_SUITE_P(
// Source 4 component, dest 4 component
{"%float 2D 0 0 0 2 Rgba32f", "OpImageWrite %im %vu12 %vf1234",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2759,7 +2759,7 @@ INSTANTIATE_TEST_SUITE_P(
// Sampled type is unsigned int, texel is unsigned int
{"%uint 2D 0 0 0 2 Rgba32ui", "OpImageWrite %im %vu12 %vu1234",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2779,7 +2779,7 @@ INSTANTIATE_TEST_SUITE_P(
// Sampled type is unsigned int, texel is signed int
{"%uint 2D 0 0 0 2 Rgba32ui", "OpImageWrite %im %vu12 %vi1234",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2801,7 +2801,7 @@ INSTANTIATE_TEST_SUITE_P(
// Sampled type is signed int, texel is unsigned int
{"%int 2D 0 0 0 2 Rgba32i", "OpImageWrite %im %vu12 %vu1234",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2823,7 +2823,7 @@ INSTANTIATE_TEST_SUITE_P(
// Sampled type is signed int, texel is signed int
{"%int 2D 0 0 0 2 Rgba32i", "OpImageWrite %im %vu12 %vi1234",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2847,7 +2847,7 @@ INSTANTIATE_TEST_SUITE_P(
::testing::ValuesIn(std::vector<ImageAccessCase>{
// OpImageRead with no extra params
{"%float 2D 0 0 0 2 Rgba32f", "%99 = OpImageRead %v4float %im %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2875,7 +2875,7 @@ INSTANTIATE_TEST_SUITE_P(
// OpImageRead with ConstOffset
{"%float 2D 0 0 0 2 Rgba32f",
"%99 = OpImageRead %v4float %im %vu12 ConstOffset %offsets2d",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2908,7 +2908,7 @@ INSTANTIATE_TEST_SUITE_P(
::testing::ValuesIn(std::vector<ImageAccessCase>{
// OpImageFetch with no extra params
{"%float 2D 0 0 0 1 Unknown", "%99 = OpImageFetch %v4float %im %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2936,7 +2936,7 @@ INSTANTIATE_TEST_SUITE_P(
// OpImageFetch with ConstOffset
{"%float 2D 0 0 0 1 Unknown",
"%99 = OpImageFetch %v4float %im %vu12 ConstOffset %offsets2d",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -2983,7 +2983,7 @@ INSTANTIATE_TEST_SUITE_P(
// OpImageFetch requires no conversion, float -> v4float
{"%float 2D 0 0 0 1 Unknown", "%99 = OpImageFetch %v4float %im %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3010,7 +3010,7 @@ INSTANTIATE_TEST_SUITE_P(
})"},
// OpImageFetch requires no conversion, uint -> v4uint
{"%uint 2D 0 0 0 1 Unknown", "%99 = OpImageFetch %v4uint %im %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3037,7 +3037,7 @@ INSTANTIATE_TEST_SUITE_P(
})"},
// OpImageFetch requires conversion, uint -> v4int
{"%uint 2D 0 0 0 1 Unknown", "%99 = OpImageFetch %v4int %im %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3066,7 +3066,7 @@ INSTANTIATE_TEST_SUITE_P(
})"},
// OpImageFetch requires no conversion, int -> v4int
{"%int 2D 0 0 0 1 Unknown", "%99 = OpImageFetch %v4int %im %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3093,7 +3093,7 @@ INSTANTIATE_TEST_SUITE_P(
})"},
// OpImageFetch requires conversion, int -> v4uint
{"%int 2D 0 0 0 1 Unknown", "%99 = OpImageFetch %v4uint %im %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3127,7 +3127,7 @@ INSTANTIATE_TEST_SUITE_P(
// OpImageRead requires no conversion, float -> v4float
{"%float 2D 0 0 0 1 Rgba32f", "%99 = OpImageRead %v4float %im %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3154,7 +3154,7 @@ INSTANTIATE_TEST_SUITE_P(
})"},
// OpImageRead requires no conversion, uint -> v4uint
{"%uint 2D 0 0 0 1 Rgba32ui", "%99 = OpImageRead %v4uint %im %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3181,7 +3181,7 @@ INSTANTIATE_TEST_SUITE_P(
})"},
// OpImageRead requires conversion, uint -> v4int
{"%uint 2D 0 0 0 1 Rgba32ui", "%99 = OpImageRead %v4int %im %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3210,7 +3210,7 @@ INSTANTIATE_TEST_SUITE_P(
})"},
// OpImageRead requires no conversion, int -> v4int
{"%int 2D 0 0 0 1 Rgba32i", "%99 = OpImageRead %v4int %im %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3237,7 +3237,7 @@ INSTANTIATE_TEST_SUITE_P(
})"},
// OpImageRead requires conversion, int -> v4uint
{"%int 2D 0 0 0 1 Rgba32i", "%99 = OpImageRead %v4uint %im %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3273,7 +3273,7 @@ INSTANTIATE_TEST_SUITE_P(
{"%float 2D 0 0 0 1 Unknown",
"%99 = OpImageSampleImplicitLod %v4float %sampled_image %vu12",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -3282,7 +3282,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3312,7 +3312,7 @@ INSTANTIATE_TEST_SUITE_P(
{"%uint 2D 0 0 0 1 Unknown",
"%99 = OpImageSampleImplicitLod %v4uint %sampled_image %vu12",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -3321,7 +3321,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3351,7 +3351,7 @@ INSTANTIATE_TEST_SUITE_P(
{"%uint 2D 0 0 0 1 Unknown",
"%99 = OpImageSampleImplicitLod %v4int %sampled_image %vu12",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -3360,7 +3360,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3392,7 +3392,7 @@ INSTANTIATE_TEST_SUITE_P(
{"%int 2D 0 0 0 1 Unknown",
"%99 = OpImageSampleImplicitLod %v4int %sampled_image %vu12",
R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{0}
BindingDecoration{0}
@@ -3401,7 +3401,7 @@ INSTANTIATE_TEST_SUITE_P(
uniform_constant
__sampler_sampler
}
DecoratedVariable{
Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}
@@ -3430,7 +3430,7 @@ INSTANTIATE_TEST_SUITE_P(
// OpImageSampleImplicitLod requires conversion, int -> v4uint
{"%int 2D 0 0 0 1 Unknown",
"%99 = OpImageSampleImplicitLod %v4uint %sampled_image %vu12",
R"(DecoratedVariable{
R"(Variable{
Decorations{
SetDecoration{2}
BindingDecoration{1}

View File

@@ -199,7 +199,7 @@ TEST_F(SpvParserTest, ModuleScopeVar_BuiltinVertexIndex) {
EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{vertex_idx}
}
@@ -249,7 +249,7 @@ TEST_F(SpvParserTest, ModuleScopeVar_BuiltinPosition_MapsToModuleScopeVec4Var) {
EXPECT_EQ(position_info.per_vertex_var_id, 1u);
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
DecoratedVariable{
Variable{
Decorations{
BuiltinDecoration{position}
}
@@ -1146,7 +1146,7 @@ TEST_F(SpvParserTest, ModuleScopeVar_LocationDecoration_Valid) {
EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
DecoratedVariable{
Variable{
Decorations{
LocationDecoration{3}
}
@@ -1198,7 +1198,7 @@ TEST_F(SpvParserTest, ModuleScopeVar_DescriptorSetDecoration_Valid) {
EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
DecoratedVariable{
Variable{
Decorations{
SetDecoration{3}
}
@@ -1252,7 +1252,7 @@ TEST_F(SpvParserTest, ModuleScopeVar_BindingDecoration_Valid) {
EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
DecoratedVariable{
Variable{
Decorations{
BindingDecoration{3}
}
@@ -1501,7 +1501,7 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_True) {
EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
DecoratedVariableConst{
VariableConst{
Decorations{
ConstantIdDecoration{12}
}
@@ -1526,7 +1526,7 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_False) {
EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
DecoratedVariableConst{
VariableConst{
Decorations{
ConstantIdDecoration{12}
}
@@ -1551,7 +1551,7 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_U32) {
EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
DecoratedVariableConst{
VariableConst{
Decorations{
ConstantIdDecoration{12}
}
@@ -1576,7 +1576,7 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_I32) {
EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
DecoratedVariableConst{
VariableConst{
Decorations{
ConstantIdDecoration{12}
}
@@ -1601,7 +1601,7 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_F32) {
EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str();
EXPECT_THAT(module_str, HasSubstr(R"(
DecoratedVariableConst{
VariableConst{
Decorations{
ConstantIdDecoration{12}
}

View File

@@ -28,7 +28,6 @@
#include "src/ast/call_expression.h"
#include "src/ast/case_statement.h"
#include "src/ast/continue_statement.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/discard_statement.h"
#include "src/ast/else_statement.h"
#include "src/ast/fallthrough_statement.h"
@@ -67,6 +66,7 @@
#include "src/ast/uint_literal.h"
#include "src/ast/unary_op.h"
#include "src/ast/unary_op_expression.h"
#include "src/ast/variable.h"
#include "src/ast/variable_decl_statement.h"
#include "src/ast/workgroup_decoration.h"
#include "src/reader/wgsl/lexer.h"
@@ -419,25 +419,25 @@ Maybe<ast::Variable*> ParserImpl::global_variable_decl(
if (!decl.matched)
return Failure::kNoMatch;
auto* var = decl.value;
auto var_decos = cast_decorations<ast::VariableDecoration>(decos);
if (var_decos.errored)
return Failure::kErrored;
if (var_decos.value.size() > 0) {
auto* dv = create<ast::DecoratedVariable>(var);
dv->set_decorations(var_decos.value);
var = dv;
}
ast::Expression* constructor = nullptr;
if (match(Token::Type::kEqual)) {
auto expr = expect_const_expr();
if (expr.errored)
return Failure::kErrored;
var->set_constructor(expr.value);
constructor = expr.value;
}
return var;
return create<ast::Variable>(decl->source, // source
decl->name, // name
decl->storage_class, // storage_class
decl->type, // type
false, // is_const
constructor, // constructor
std::move(var_decos.value)); // decorations
}
// global_constant_decl
@@ -452,10 +452,6 @@ Maybe<ast::Variable*> ParserImpl::global_constant_decl() {
if (decl.errored)
return Failure::kErrored;
auto* var = create<ast::Variable>(decl->source, decl->name,
ast::StorageClass::kNone, decl->type);
var->set_is_const(true);
if (!expect(use, Token::Type::kEqual))
return Failure::kErrored;
@@ -463,14 +459,18 @@ Maybe<ast::Variable*> ParserImpl::global_constant_decl() {
if (init.errored)
return Failure::kErrored;
var->set_constructor(init.value);
return var;
return create<ast::Variable>(decl->source, // source
decl->name, // name
ast::StorageClass::kNone, // storage_class
decl->type, // type
true, // is_const
init.value, // constructor
ast::VariableDecorationList{}); // decorations
}
// variable_decl
// : VAR variable_storage_decoration? variable_ident_decl
Maybe<ast::Variable*> ParserImpl::variable_decl() {
Maybe<ParserImpl::VarDeclInfo> ParserImpl::variable_decl() {
if (!match(Token::Type::kVar))
return Failure::kNoMatch;
@@ -482,9 +482,9 @@ Maybe<ast::Variable*> ParserImpl::variable_decl() {
if (decl.errored)
return Failure::kErrored;
return create<ast::Variable>(decl->source, decl->name,
sc.matched ? sc.value : ast::StorageClass::kNone,
decl->type);
return VarDeclInfo{decl->source, decl->name,
sc.matched ? sc.value : ast::StorageClass::kNone,
decl->type};
}
// texture_sampler_types
@@ -1349,13 +1349,18 @@ Expect<ast::VariableList> ParserImpl::expect_param_list() {
ast::VariableList ret;
for (;;) {
auto* var = create<ast::Variable>(decl->source, decl->name,
ast::StorageClass::kNone, decl->type);
auto* var =
create<ast::Variable>(decl->source, // source
decl->name, // name
ast::StorageClass::kNone, // storage_class
decl->type, // type
true, // is_const
nullptr, // constructor
ast::VariableDecorationList{}); // decorations
// Formal parameters are treated like a const declaration where the
// initializer value is provided by the call's argument. The key point is
// that it's not updatable after intially set. This is unlike C or GLSL
// which treat formal parameters like local variables that can be updated.
var->set_is_const(true);
ret.push_back(var);
if (!match(Token::Type::kComma))
@@ -1610,31 +1615,45 @@ Maybe<ast::VariableDeclStatement*> ParserImpl::variable_stmt() {
if (!constructor.matched)
return add_error(peek(), "missing constructor for const declaration");
auto* var = create<ast::Variable>(decl->source, decl->name,
ast::StorageClass::kNone, decl->type);
var->set_is_const(true);
var->set_constructor(constructor.value);
auto* var =
create<ast::Variable>(decl->source, // source
decl->name, // name
ast::StorageClass::kNone, // storage_class
decl->type, // type
true, // is_const
constructor.value, // constructor
ast::VariableDecorationList{}); // decorations
return create<ast::VariableDeclStatement>(decl->source, var);
}
auto var = variable_decl();
if (var.errored)
auto decl = variable_decl();
if (decl.errored)
return Failure::kErrored;
if (!var.matched)
if (!decl.matched)
return Failure::kNoMatch;
ast::Expression* constructor = nullptr;
if (match(Token::Type::kEqual)) {
auto constructor = logical_or_expression();
if (constructor.errored)
auto constructor_expr = logical_or_expression();
if (constructor_expr.errored)
return Failure::kErrored;
if (!constructor.matched)
if (!constructor_expr.matched)
return add_error(peek(), "missing constructor for variable declaration");
var->set_constructor(constructor.value);
constructor = constructor_expr.value;
}
return create<ast::VariableDeclStatement>(var->source(), var.value);
auto* var =
create<ast::Variable>(decl->source, // source
decl->name, // name
decl->storage_class, // storage_class
decl->type, // type
false, // is_const
constructor, // constructor
ast::VariableDecorationList{}); // decorations
return create<ast::VariableDeclStatement>(var->source(), var);
}
// if_stmt

View File

@@ -262,6 +262,18 @@ class ParserImpl {
ast::type::Type* return_type;
};
/// VarDeclInfo contains the parsed information for variable declaration.
struct VarDeclInfo {
/// Variable declaration source
Source source;
/// Variable name
std::string name;
/// Variable storage class
ast::StorageClass storage_class;
/// Variable type
ast::type::Type* type;
};
/// Creates a new parser using the given file
/// @param file the input source file to parse
explicit ParserImpl(Source::File const* file);
@@ -356,8 +368,8 @@ class ParserImpl {
/// @returns the const object or nullptr
Maybe<ast::Variable*> global_constant_decl();
/// Parses a `variable_decl` grammar element
/// @returns the parsed variable or nullptr otherwise
Maybe<ast::Variable*> variable_decl();
/// @returns the parsed variable declaration info
Maybe<VarDeclInfo> variable_decl();
/// Parses a `variable_ident_decl` grammar element, erroring on parse
/// failure.
/// @param use a description of what was being parsed if an error was raised.

View File

@@ -13,8 +13,8 @@
// limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/variable.h"
#include "src/ast/variable_decoration.h"
#include "src/reader/wgsl/parser_impl.h"
#include "src/reader/wgsl/parser_impl_test_helper.h"

View File

@@ -13,9 +13,9 @@
// limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/variable.h"
#include "src/ast/variable_decoration.h"
#include "src/reader/wgsl/parser_impl.h"
#include "src/reader/wgsl/parser_impl_test_helper.h"
@@ -46,7 +46,6 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithoutConstructor) {
EXPECT_EQ(e->source().range.end.column, 11u);
ASSERT_EQ(e->constructor(), nullptr);
ASSERT_FALSE(e->Is<ast::DecoratedVariable>());
}
TEST_F(ParserImplTest, GlobalVariableDecl_WithConstructor) {
@@ -72,8 +71,6 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithConstructor) {
ASSERT_NE(e->constructor(), nullptr);
ASSERT_TRUE(e->constructor()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(e->constructor()->Is<ast::ScalarConstructorExpression>());
ASSERT_FALSE(e->Is<ast::DecoratedVariable>());
}
TEST_F(ParserImplTest, GlobalVariableDecl_WithDecoration) {
@@ -86,7 +83,6 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithDecoration) {
EXPECT_TRUE(e.matched);
EXPECT_FALSE(e.errored);
ASSERT_NE(e.value, nullptr);
ASSERT_TRUE(e->Is<ast::DecoratedVariable>());
EXPECT_EQ(e->name(), "a");
ASSERT_NE(e->type(), nullptr);
@@ -100,10 +96,7 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithDecoration) {
ASSERT_EQ(e->constructor(), nullptr);
ASSERT_TRUE(e->Is<ast::DecoratedVariable>());
auto* v = e->As<ast::DecoratedVariable>();
auto& decorations = v->decorations();
auto& decorations = e->decorations();
ASSERT_EQ(decorations.size(), 2u);
ASSERT_TRUE(decorations[0]->Is<ast::BindingDecoration>());
ASSERT_TRUE(decorations[1]->Is<ast::SetDecoration>());
@@ -120,7 +113,6 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithDecoration_MulitpleGroups) {
EXPECT_TRUE(e.matched);
EXPECT_FALSE(e.errored);
ASSERT_NE(e.value, nullptr);
ASSERT_TRUE(e->Is<ast::DecoratedVariable>());
EXPECT_EQ(e->name(), "a");
ASSERT_NE(e->type(), nullptr);
@@ -134,10 +126,7 @@ TEST_F(ParserImplTest, GlobalVariableDecl_WithDecoration_MulitpleGroups) {
ASSERT_EQ(e->constructor(), nullptr);
ASSERT_TRUE(e->Is<ast::DecoratedVariable>());
auto* v = e->As<ast::DecoratedVariable>();
auto& decorations = v->decorations();
auto& decorations = e->decorations();
ASSERT_EQ(decorations.size(), 2u);
ASSERT_TRUE(decorations[0]->Is<ast::BindingDecoration>());
ASSERT_TRUE(decorations[1]->Is<ast::SetDecoration>());

View File

@@ -25,25 +25,23 @@ namespace {
TEST_F(ParserImplTest, VariableDecl_Parses) {
auto p = parser("var my_var : f32");
auto var = p->variable_decl();
auto v = p->variable_decl();
EXPECT_FALSE(p->has_error());
EXPECT_TRUE(var.matched);
EXPECT_FALSE(var.errored);
ASSERT_NE(var.value, nullptr);
EXPECT_EQ(var->name(), "my_var");
EXPECT_NE(var->type(), nullptr);
EXPECT_TRUE(var->type()->Is<ast::type::F32>());
EXPECT_TRUE(v.matched);
EXPECT_FALSE(v.errored);
EXPECT_EQ(v->name, "my_var");
EXPECT_NE(v->type, nullptr);
EXPECT_TRUE(v->type->Is<ast::type::F32>());
EXPECT_EQ(var->source().range.begin.line, 1u);
EXPECT_EQ(var->source().range.begin.column, 5u);
EXPECT_EQ(var->source().range.end.line, 1u);
EXPECT_EQ(var->source().range.end.column, 11u);
EXPECT_EQ(v->source.range.begin.line, 1u);
EXPECT_EQ(v->source.range.begin.column, 5u);
EXPECT_EQ(v->source.range.end.line, 1u);
EXPECT_EQ(v->source.range.end.column, 11u);
}
TEST_F(ParserImplTest, VariableDecl_MissingVar) {
auto p = parser("my_var : f32");
auto v = p->variable_decl();
EXPECT_EQ(v.value, nullptr);
EXPECT_FALSE(v.matched);
EXPECT_FALSE(v.errored);
EXPECT_FALSE(p->has_error());
@@ -58,7 +56,6 @@ TEST_F(ParserImplTest, VariableDecl_InvalidIdentDecl) {
EXPECT_FALSE(v.matched);
EXPECT_TRUE(v.errored);
EXPECT_TRUE(p->has_error());
EXPECT_EQ(v.value, nullptr);
EXPECT_EQ(p->error(), "1:12: expected ':' for variable declaration");
}
@@ -68,15 +65,14 @@ TEST_F(ParserImplTest, VariableDecl_WithStorageClass) {
EXPECT_TRUE(v.matched);
EXPECT_FALSE(v.errored);
EXPECT_FALSE(p->has_error());
ASSERT_NE(v.value, nullptr);
EXPECT_EQ(v->name(), "my_var");
EXPECT_TRUE(v->type()->Is<ast::type::F32>());
EXPECT_EQ(v->storage_class(), ast::StorageClass::kPrivate);
EXPECT_EQ(v->name, "my_var");
EXPECT_TRUE(v->type->Is<ast::type::F32>());
EXPECT_EQ(v->storage_class, ast::StorageClass::kPrivate);
EXPECT_EQ(v->source().range.begin.line, 1u);
EXPECT_EQ(v->source().range.begin.column, 14u);
EXPECT_EQ(v->source().range.end.line, 1u);
EXPECT_EQ(v->source().range.end.column, 20u);
EXPECT_EQ(v->source.range.begin.line, 1u);
EXPECT_EQ(v->source.range.begin.column, 14u);
EXPECT_EQ(v->source.range.end.line, 1u);
EXPECT_EQ(v->source.range.end.column, 20u);
}
TEST_F(ParserImplTest, VariableDecl_InvalidStorageClass) {
@@ -85,7 +81,6 @@ TEST_F(ParserImplTest, VariableDecl_InvalidStorageClass) {
EXPECT_FALSE(v.matched);
EXPECT_TRUE(v.errored);
EXPECT_TRUE(p->has_error());
EXPECT_EQ(v.value, nullptr);
EXPECT_EQ(p->error(), "1:5: invalid storage class for variable decoration");
}