Add Symbol to Variable.

This CL adds a Symbol to the Variable AST node along side the name. The
name will be removed in a future CL.

Change-Id: I1c05e5595392b1c4a0afa82387d97b2b4472bade
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35881
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Auto-Submit: dan sinclair <dsinclair@chromium.org>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-12-16 21:42:30 +00:00 committed by Commit Bot service account
parent b5839939e1
commit a57f842be9
19 changed files with 242 additions and 242 deletions

View File

@ -40,8 +40,8 @@ Variable* Builder::Var(const std::string& name,
type::Type* type, type::Type* type,
Expression* constructor, Expression* constructor,
VariableDecorationList decorations) { VariableDecorationList decorations) {
auto* var = auto* var = create<Variable>(mod->RegisterSymbol(name), name, storage, type,
create<Variable>(name, storage, type, false, constructor, decorations); false, constructor, decorations);
OnVariableBuilt(var); OnVariableBuilt(var);
return var; return var;
} }
@ -52,8 +52,8 @@ Variable* Builder::Var(const Source& source,
type::Type* type, type::Type* type,
Expression* constructor, Expression* constructor,
VariableDecorationList decorations) { VariableDecorationList decorations) {
auto* var = create<Variable>(source, name, storage, type, false, constructor, auto* var = create<Variable>(source, mod->RegisterSymbol(name), name, storage,
decorations); type, false, constructor, decorations);
OnVariableBuilt(var); OnVariableBuilt(var);
return var; return var;
} }
@ -69,8 +69,8 @@ Variable* Builder::Const(const std::string& name,
type::Type* type, type::Type* type,
Expression* constructor, Expression* constructor,
VariableDecorationList decorations) { VariableDecorationList decorations) {
auto* var = auto* var = create<Variable>(mod->RegisterSymbol(name), name, storage, type,
create<Variable>(name, storage, type, true, constructor, decorations); true, constructor, decorations);
OnVariableBuilt(var); OnVariableBuilt(var);
return var; return var;
} }
@ -81,8 +81,8 @@ Variable* Builder::Const(const Source& source,
type::Type* type, type::Type* type,
Expression* constructor, Expression* constructor,
VariableDecorationList decorations) { VariableDecorationList decorations) {
auto* var = create<Variable>(source, name, storage, type, true, constructor, auto* var = create<Variable>(source, mod->RegisterSymbol(name), name, storage,
decorations); type, true, constructor, decorations);
OnVariableBuilt(var); OnVariableBuilt(var);
return var; return var;
} }

View File

@ -26,6 +26,7 @@ namespace tint {
namespace ast { namespace ast {
Variable::Variable(const Source& source, Variable::Variable(const Source& source,
const Symbol& sym,
const std::string& name, const std::string& name,
StorageClass sc, StorageClass sc,
type::Type* type, type::Type* type,
@ -33,6 +34,7 @@ Variable::Variable(const Source& source,
Expression* constructor, Expression* constructor,
VariableDecorationList decorations) VariableDecorationList decorations)
: Base(source), : Base(source),
symbol_(sym),
name_(name), name_(name),
type_(type), type_(type),
is_const_(is_const), is_const_(is_const),
@ -82,13 +84,14 @@ uint32_t Variable::constant_id() const {
} }
Variable* Variable::Clone(CloneContext* ctx) const { Variable* Variable::Clone(CloneContext* ctx) const {
return ctx->mod->create<Variable>( return ctx->mod->create<Variable>(ctx->Clone(source()), ctx->Clone(symbol_),
ctx->Clone(source()), name(), storage_class(), ctx->Clone(type()), name(), storage_class(), ctx->Clone(type()),
is_const_, ctx->Clone(constructor()), ctx->Clone(decorations_)); is_const_, ctx->Clone(constructor()),
ctx->Clone(decorations_));
} }
bool Variable::IsValid() const { bool Variable::IsValid() const {
if (name_.length() == 0) { if (name_.length() == 0 || !symbol_.IsValid()) {
return false; return false;
} }
if (type_ == nullptr) { if (type_ == nullptr) {
@ -102,7 +105,7 @@ bool Variable::IsValid() const {
void Variable::info_to_str(std::ostream& out, size_t indent) const { void Variable::info_to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << name_ << std::endl; out << symbol_.to_str() << std::endl;
make_indent(out, indent); make_indent(out, indent);
out << storage_class_ << std::endl; out << storage_class_ << std::endl;
make_indent(out, indent); make_indent(out, indent);

View File

@ -26,6 +26,7 @@
#include "src/ast/storage_class.h" #include "src/ast/storage_class.h"
#include "src/ast/type/type.h" #include "src/ast/type/type.h"
#include "src/ast/variable_decoration.h" #include "src/ast/variable_decoration.h"
#include "src/symbol.h"
namespace tint { namespace tint {
namespace ast { namespace ast {
@ -81,6 +82,7 @@ class Variable : public Castable<Variable, Node> {
public: public:
/// Create a variable /// Create a variable
/// @param source the variable source /// @param source the variable source
/// @param sym the variable symbol
/// @param name the variables name /// @param name the variables name
/// @param sc the variable storage class /// @param sc the variable storage class
/// @param type the value type /// @param type the value type
@ -88,6 +90,7 @@ class Variable : public Castable<Variable, Node> {
/// @param constructor the constructor expression /// @param constructor the constructor expression
/// @param decorations the variable decorations /// @param decorations the variable decorations
Variable(const Source& source, Variable(const Source& source,
const Symbol& sym,
const std::string& name, const std::string& name,
StorageClass sc, StorageClass sc,
type::Type* type, type::Type* type,
@ -99,6 +102,8 @@ class Variable : public Castable<Variable, Node> {
~Variable() override; ~Variable() override;
/// @returns the variable symbol
const Symbol& symbol() const { return symbol_; }
/// @returns the variable name /// @returns the variable name
const std::string& name() const { return name_; } const std::string& name() const { return name_; }
@ -141,7 +146,7 @@ class Variable : public Castable<Variable, Node> {
/// @return the newly cloned node /// @return the newly cloned node
Variable* Clone(CloneContext* ctx) const override; Variable* Clone(CloneContext* ctx) const override;
/// @returns true if the name and path are both present /// @returns true if the variable is valid
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
@ -162,6 +167,7 @@ class Variable : public Castable<Variable, Node> {
private: private:
Variable(const Variable&) = delete; Variable(const Variable&) = delete;
Symbol const symbol_;
std::string const name_; std::string const name_;
// The value type if a const or formal paramter, and the store type if a var // The value type if a const or formal paramter, and the store type if a var
type::Type* const type_; type::Type* const type_;

View File

@ -72,7 +72,7 @@ TEST_F(VariableDeclStatementTest, ToStr) {
create<VariableDeclStatement>(Source{Source::Location{20, 2}}, var); create<VariableDeclStatement>(Source{Source::Location{20, 2}}, var);
std::ostringstream out; std::ostringstream out;
stmt->to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(out.str(), R"( VariableDeclStatement{ EXPECT_EQ(demangle(out.str()), R"( VariableDeclStatement{
Variable{ Variable{
a a
none none

View File

@ -29,6 +29,7 @@ using VariableTest = TestHelper;
TEST_F(VariableTest, Creation) { TEST_F(VariableTest, Creation) {
auto* v = Var("my_var", StorageClass::kFunction, ty.i32); auto* v = Var("my_var", StorageClass::kFunction, ty.i32);
EXPECT_EQ(v->symbol(), Symbol(1));
EXPECT_EQ(v->name(), "my_var"); EXPECT_EQ(v->name(), "my_var");
EXPECT_EQ(v->storage_class(), StorageClass::kFunction); EXPECT_EQ(v->storage_class(), StorageClass::kFunction);
EXPECT_EQ(v->type(), ty.i32); EXPECT_EQ(v->type(), ty.i32);
@ -43,6 +44,7 @@ TEST_F(VariableTest, CreationWithSource) {
Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 5}}}, Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 5}}},
"i", StorageClass::kPrivate, ty.f32, nullptr, VariableDecorationList{}); "i", StorageClass::kPrivate, ty.f32, nullptr, VariableDecorationList{});
EXPECT_EQ(v->symbol(), Symbol(1));
EXPECT_EQ(v->name(), "i"); EXPECT_EQ(v->name(), "i");
EXPECT_EQ(v->storage_class(), StorageClass::kPrivate); EXPECT_EQ(v->storage_class(), StorageClass::kPrivate);
EXPECT_EQ(v->type(), ty.f32); EXPECT_EQ(v->type(), ty.f32);
@ -58,6 +60,7 @@ TEST_F(VariableTest, CreationEmpty) {
"a_var", StorageClass::kWorkgroup, ty.i32, nullptr, "a_var", StorageClass::kWorkgroup, ty.i32, nullptr,
VariableDecorationList{}); VariableDecorationList{});
EXPECT_EQ(v->symbol(), Symbol(1));
EXPECT_EQ(v->name(), "a_var"); EXPECT_EQ(v->name(), "a_var");
EXPECT_EQ(v->storage_class(), StorageClass::kWorkgroup); EXPECT_EQ(v->storage_class(), StorageClass::kWorkgroup);
EXPECT_EQ(v->type(), ty.i32); EXPECT_EQ(v->type(), ty.i32);
@ -78,7 +81,7 @@ TEST_F(VariableTest, IsValid_WithConstructor) {
EXPECT_TRUE(v->IsValid()); EXPECT_TRUE(v->IsValid());
} }
TEST_F(VariableTest, IsValid_MissinName) { TEST_F(VariableTest, IsValid_MissingSymbol) {
auto* v = Var("", StorageClass::kNone, ty.i32); auto* v = Var("", StorageClass::kNone, ty.i32);
EXPECT_FALSE(v->IsValid()); EXPECT_FALSE(v->IsValid());
} }
@ -104,7 +107,7 @@ TEST_F(VariableTest, to_str) {
ast::VariableDecorationList{}); ast::VariableDecorationList{});
std::ostringstream out; std::ostringstream out;
v->to_str(out, 2); v->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Variable{ EXPECT_EQ(demangle(out.str()), R"( Variable{
my_var my_var
function function
__f32 __f32

View File

@ -2248,8 +2248,9 @@ bool FunctionEmitter::EmitIfStart(const BlockInfo& block_info) {
if (!guard_name.empty()) { if (!guard_name.empty()) {
// Declare the guard variable just before the "if", initialized to true. // Declare the guard variable just before the "if", initialized to true.
auto* guard_var = auto* guard_var =
create<ast::Variable>(Source{}, // source create<ast::Variable>(Source{}, // source
guard_name, // name ast_module_.RegisterSymbol(guard_name), // symbol
guard_name, // name
ast::StorageClass::kFunction, // storage_class ast::StorageClass::kFunction, // storage_class
parser_impl_.Bool(), // type parser_impl_.Bool(), // type
false, // is_const false, // is_const
@ -2777,6 +2778,7 @@ bool FunctionEmitter::EmitStatementsInBasicBlock(const BlockInfo& block_info,
assert(!phi_var_name.empty()); assert(!phi_var_name.empty());
auto* var = create<ast::Variable>( auto* var = create<ast::Variable>(
Source{}, // source Source{}, // source
ast_module_.RegisterSymbol(phi_var_name), // symbol
phi_var_name, // name phi_var_name, // name
ast::StorageClass::kFunction, // storage_class ast::StorageClass::kFunction, // storage_class
parser_impl_.ConvertType(def_inst->type_id()), // type parser_impl_.ConvertType(def_inst->type_id()), // type

View File

@ -216,9 +216,10 @@ TEST_F(SpvParserTest, EmitStatement_CallWithParams) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModule()) << p->error(); ASSERT_TRUE(p->BuildAndParseInternalModule()) << p->error();
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_ast_str = p->get_module().to_str(); const auto module_ast_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_ast_str, HasSubstr(R"(Module{ EXPECT_THAT(module_ast_str, HasSubstr(R"(Module{
Function tint_symbol_3 -> __u32 Function x_50 -> __u32
( (
VariableConst{ VariableConst{
x_51 x_51
@ -235,15 +236,14 @@ TEST_F(SpvParserTest, EmitStatement_CallWithParams) {
Return{ Return{
{ {
Binary[not set]{ Binary[not set]{
Identifier[not set]{tint_symbol_1} Identifier[not set]{x_51}
add add
Identifier[not set]{tint_symbol_2} Identifier[not set]{x_52}
} }
} }
} }
} }
Function )" + p->get_module().GetSymbol("x_100").to_str() + Function x_100 -> __void
R"( -> __void
() ()
{ {
VariableDeclStatement{ VariableDeclStatement{
@ -253,7 +253,7 @@ TEST_F(SpvParserTest, EmitStatement_CallWithParams) {
__u32 __u32
{ {
Call[not set]{ Call[not set]{
Identifier[not set]{tint_symbol_3} Identifier[not set]{x_50}
( (
ScalarConstructor[not set]{42} ScalarConstructor[not set]{42}
ScalarConstructor[not set]{84} ScalarConstructor[not set]{84}

View File

@ -59,10 +59,9 @@ TEST_F(SpvParserTest, Emit_VoidFunctionWithoutParams) {
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
FunctionEmitter fe(p.get(), *spirv_function(p.get(), 100)); FunctionEmitter fe(p.get(), *spirv_function(p.get(), 100));
EXPECT_TRUE(fe.Emit()); EXPECT_TRUE(fe.Emit());
auto got = p->get_module().to_str(); auto got = Demangler().Demangle(p->get_module(), p->get_module().to_str());
auto expect = R"(Module{ std::string expect = R"(Module{
Function )" + p->get_module().GetSymbol("x_100").to_str() + Function x_100 -> __void
R"( -> __void
() ()
{ {
Return{} Return{}
@ -84,10 +83,9 @@ TEST_F(SpvParserTest, Emit_NonVoidResultType) {
FunctionEmitter fe(p.get(), *spirv_function(p.get(), 100)); FunctionEmitter fe(p.get(), *spirv_function(p.get(), 100));
EXPECT_TRUE(fe.Emit()); EXPECT_TRUE(fe.Emit());
auto got = p->get_module().to_str(); auto got = Demangler().Demangle(p->get_module(), p->get_module().to_str());
auto expect = R"(Module{ std::string expect = R"(Module{
Function )" + p->get_module().GetSymbol("x_100").to_str() + Function x_100 -> __f32
R"( -> __f32
() ()
{ {
Return{ Return{
@ -117,10 +115,9 @@ TEST_F(SpvParserTest, Emit_MixedParamTypes) {
FunctionEmitter fe(p.get(), *spirv_function(p.get(), 100)); FunctionEmitter fe(p.get(), *spirv_function(p.get(), 100));
EXPECT_TRUE(fe.Emit()); EXPECT_TRUE(fe.Emit());
auto got = p->get_module().to_str(); auto got = Demangler().Demangle(p->get_module(), p->get_module().to_str());
auto expect = R"(Module{ std::string expect = R"(Module{
Function )" + p->get_module().GetSymbol("x_100").to_str() + Function x_100 -> __void
R"( -> __void
( (
VariableConst{ VariableConst{
a a
@ -162,10 +159,9 @@ TEST_F(SpvParserTest, Emit_GenerateParamNames) {
FunctionEmitter fe(p.get(), *spirv_function(p.get(), 100)); FunctionEmitter fe(p.get(), *spirv_function(p.get(), 100));
EXPECT_TRUE(fe.Emit()); EXPECT_TRUE(fe.Emit());
auto got = p->get_module().to_str(); auto got = Demangler().Demangle(p->get_module(), p->get_module().to_str());
auto expect = R"(Module{ std::string expect = R"(Module{
Function )" + p->get_module().GetSymbol("x_100").to_str() + Function x_100 -> __void
R"( -> __void
( (
VariableConst{ VariableConst{
x_14 x_14

View File

@ -1297,13 +1297,15 @@ ast::Variable* ParserImpl::MakeVariable(
} }
} }
return create<ast::Variable>(Source{}, // source std::string name = namer_.Name(id);
namer_.Name(id), // name return create<ast::Variable>(Source{}, // source
sc, // storage_class ast_module_.RegisterSymbol(name), // symbol
type, // type name, // name
is_const, // is_const sc, // storage_class
constructor, // constructor type, // type
decorations); // decorations is_const, // is_const
constructor, // constructor
decorations); // decorations
} }
TypedExpression ParserImpl::MakeConstantExpression(uint32_t id) { TypedExpression ParserImpl::MakeConstantExpression(uint32_t id) {

View File

@ -267,10 +267,10 @@ TEST_F(SpvParserTest, EmitFunctions_NonVoidResultType) {
)")); )"));
EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->BuildAndParseInternalModule());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_ast = p->get_module().to_str(); const auto module_ast =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_ast, HasSubstr(R"( EXPECT_THAT(module_ast, HasSubstr(R"(
Function )" + p->get_module().GetSymbol("ret_float").to_str() + Function ret_float -> __f32
R"( -> __f32
() ()
{ {
Return{ Return{
@ -297,10 +297,10 @@ TEST_F(SpvParserTest, EmitFunctions_MixedParamTypes) {
)")); )"));
EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->BuildAndParseInternalModule());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_ast = p->get_module().to_str(); const auto module_ast =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_ast, HasSubstr(R"( EXPECT_THAT(module_ast, HasSubstr(R"(
Function )" + p->get_module().GetSymbol("mixed_params").to_str() + Function mixed_params -> __void
R"( -> __void
( (
VariableConst{ VariableConst{
a a
@ -337,10 +337,10 @@ TEST_F(SpvParserTest, EmitFunctions_GenerateParamNames) {
)")); )"));
EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->BuildAndParseInternalModule());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_ast = p->get_module().to_str(); const auto module_ast =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_ast, HasSubstr(R"( EXPECT_THAT(module_ast, HasSubstr(R"(
Function )" + p->get_module().GetSymbol("mixed_params").to_str() + Function mixed_params -> __void
R"( -> __void
( (
VariableConst{ VariableConst{
x_14 x_14

View File

@ -141,7 +141,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_AnonWorkgroupVar) {
EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->BuildAndParseInternalModule());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"( EXPECT_THAT(module_str, HasSubstr(R"(
Variable{ Variable{
x_52 x_52
@ -160,7 +161,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_NamedWorkgroupVar) {
EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->BuildAndParseInternalModule());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"( EXPECT_THAT(module_str, HasSubstr(R"(
Variable{ Variable{
the_counter the_counter
@ -179,7 +181,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_PrivateVar) {
EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->BuildAndParseInternalModule());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"( EXPECT_THAT(module_str, HasSubstr(R"(
Variable{ Variable{
my_own_private_idaho my_own_private_idaho
@ -198,7 +201,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_BuiltinVertexIndex) {
EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->BuildAndParseInternalModule());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"( EXPECT_THAT(module_str, HasSubstr(R"(
Variable{ Variable{
Decorations{ Decorations{
@ -248,7 +252,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_BuiltinPosition_MapsToModuleScopeVec4Var) {
EXPECT_EQ(position_info.pointer_type_id, 11u); EXPECT_EQ(position_info.pointer_type_id, 11u);
EXPECT_EQ(position_info.storage_class, SpvStorageClassOutput); EXPECT_EQ(position_info.storage_class, SpvStorageClassOutput);
EXPECT_EQ(position_info.per_vertex_var_id, 1u); EXPECT_EQ(position_info.per_vertex_var_id, 1u);
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"( EXPECT_THAT(module_str, HasSubstr(R"(
Variable{ Variable{
Decorations{ Decorations{
@ -718,7 +723,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarInitializers) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_1 x_1
private private
@ -775,7 +781,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarNullInitializers) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_1 x_1
private private
@ -824,7 +831,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarUndefInitializers) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error();
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_1 x_1
private private
@ -868,7 +876,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -891,7 +900,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorBoolNullInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -914,7 +924,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorBoolUndefInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -937,7 +948,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorUintNullInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -960,7 +972,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorUintUndefInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -983,7 +996,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorIntNullInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -1006,7 +1020,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorIntUndefInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -1029,7 +1044,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorFloatNullInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -1052,7 +1068,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorFloatUndefInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -1081,7 +1098,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_MatrixInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -1117,7 +1135,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_MatrixNullInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -1153,7 +1172,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_MatrixUndefInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -1190,7 +1210,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ArrayInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -1213,7 +1234,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ArrayNullInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -1236,7 +1258,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ArrayUndefInitializer) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions());
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"(Variable{ EXPECT_THAT(module_str, HasSubstr(R"(Variable{
x_200 x_200
private private
@ -1353,7 +1376,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_LocationDecoration_Valid) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error();
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"( EXPECT_THAT(module_str, HasSubstr(R"(
Variable{ Variable{
Decorations{ Decorations{
@ -1716,7 +1740,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_True) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error();
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"( EXPECT_THAT(module_str, HasSubstr(R"(
VariableConst{ VariableConst{
Decorations{ Decorations{
@ -1741,7 +1766,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_False) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error();
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"( EXPECT_THAT(module_str, HasSubstr(R"(
VariableConst{ VariableConst{
Decorations{ Decorations{
@ -1766,7 +1792,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_U32) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error();
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"( EXPECT_THAT(module_str, HasSubstr(R"(
VariableConst{ VariableConst{
Decorations{ Decorations{
@ -1791,7 +1818,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_I32) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error();
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"( EXPECT_THAT(module_str, HasSubstr(R"(
VariableConst{ VariableConst{
Decorations{ Decorations{
@ -1816,7 +1844,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_F32) {
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error();
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"( EXPECT_THAT(module_str, HasSubstr(R"(
VariableConst{ VariableConst{
Decorations{ Decorations{
@ -1842,7 +1871,8 @@ TEST_F(SpvParserTest,
)")); )"));
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error();
EXPECT_TRUE(p->error().empty()); EXPECT_TRUE(p->error().empty());
const auto module_str = p->module().to_str(); const auto module_str =
Demangler().Demangle(p->get_module(), p->get_module().to_str());
EXPECT_THAT(module_str, HasSubstr(R"( EXPECT_THAT(module_str, HasSubstr(R"(
VariableConst{ VariableConst{
myconst myconst

View File

@ -431,8 +431,9 @@ Maybe<ast::Variable*> ParserImpl::global_variable_decl(
constructor = expr.value; constructor = expr.value;
} }
return create<ast::Variable>(decl->source, // source return create<ast::Variable>(decl->source, // source
decl->name, // name module_.RegisterSymbol(decl->name), // symbol
decl->name, // name
decl->storage_class, // storage_class decl->storage_class, // storage_class
decl->type, // type decl->type, // type
false, // is_const false, // is_const
@ -459,8 +460,9 @@ Maybe<ast::Variable*> ParserImpl::global_constant_decl() {
if (init.errored) if (init.errored)
return Failure::kErrored; return Failure::kErrored;
return create<ast::Variable>(decl->source, // source return create<ast::Variable>(decl->source, // source
decl->name, // name module_.RegisterSymbol(decl->name), // symbol
decl->name, // name
ast::StorageClass::kNone, // storage_class ast::StorageClass::kNone, // storage_class
decl->type, // type decl->type, // type
true, // is_const true, // is_const
@ -1353,8 +1355,9 @@ Expect<ast::VariableList> ParserImpl::expect_param_list() {
ast::VariableList ret; ast::VariableList ret;
for (;;) { for (;;) {
auto* var = auto* var =
create<ast::Variable>(decl->source, // source create<ast::Variable>(decl->source, // source
decl->name, // name module_.RegisterSymbol(decl->name), // symbol
decl->name, // name
ast::StorageClass::kNone, // storage_class ast::StorageClass::kNone, // storage_class
decl->type, // type decl->type, // type
true, // is_const true, // is_const
@ -1624,8 +1627,9 @@ Maybe<ast::VariableDeclStatement*> ParserImpl::variable_stmt() {
return add_error(peek(), "missing constructor for const declaration"); return add_error(peek(), "missing constructor for const declaration");
auto* var = auto* var =
create<ast::Variable>(decl->source, // source create<ast::Variable>(decl->source, // source
decl->name, // name module_.RegisterSymbol(decl->name), // symbol
decl->name, // name
ast::StorageClass::kNone, // storage_class ast::StorageClass::kNone, // storage_class
decl->type, // type decl->type, // type
true, // is_const true, // is_const
@ -1653,8 +1657,9 @@ Maybe<ast::VariableDeclStatement*> ParserImpl::variable_stmt() {
} }
auto* var = auto* var =
create<ast::Variable>(decl->source, // source create<ast::Variable>(decl->source, // source
decl->name, // name module_.RegisterSymbol(decl->name), // symbol
decl->name, // name
decl->storage_class, // storage_class decl->storage_class, // storage_class
decl->type, // type decl->type, // type
false, // is_const false, // is_const

View File

@ -14,13 +14,14 @@
#include "src/scope_stack.h" #include "src/scope_stack.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "src/ast/builder.h"
#include "src/ast/type/f32_type.h" #include "src/ast/type/f32_type.h"
#include "src/ast/variable.h" #include "src/ast/variable.h"
namespace tint { namespace tint {
namespace { namespace {
using ScopeStackTest = testing::Test; class ScopeStackTest : public ast::BuilderWithModule, public testing::Test {};
TEST_F(ScopeStackTest, Global) { TEST_F(ScopeStackTest, Global) {
ScopeStack<uint32_t> s; ScopeStack<uint32_t> s;
@ -32,12 +33,9 @@ TEST_F(ScopeStackTest, Global) {
} }
TEST_F(ScopeStackTest, Global_SetWithPointer) { TEST_F(ScopeStackTest, Global_SetWithPointer) {
ast::type::F32 f32; auto* v = Var("my_var", ast::StorageClass::kNone, ty.f32);
ast::Variable v(Source{}, "my_var", ast::StorageClass::kNone, &f32, false,
nullptr, ast::VariableDecorationList{});
ScopeStack<ast::Variable*> s; ScopeStack<ast::Variable*> s;
s.set_global("var", &v); s.set_global("var", v);
ast::Variable* v2 = nullptr; ast::Variable* v2 = nullptr;
EXPECT_TRUE(s.get("var", &v2)); EXPECT_TRUE(s.get("var", &v2));

View File

@ -51,12 +51,13 @@ Transform::Output EmitVertexPointSize::Run(ast::Module* in) {
// Declare the pointsize builtin output variable. // Declare the pointsize builtin output variable.
auto* pointsize_var = out.module.create<ast::Variable>( auto* pointsize_var = out.module.create<ast::Variable>(
Source{}, // source Source{}, // source
kPointSizeVar, // name out.module.RegisterSymbol(kPointSizeVar), // symbol
ast::StorageClass::kOutput, // storage_class kPointSizeVar, // name
f32, // type ast::StorageClass::kOutput, // storage_class
false, // is_const f32, // type
nullptr, // constructor false, // is_const
nullptr, // constructor
ast::VariableDecorationList{ ast::VariableDecorationList{
// decorations // decorations
out.module.create<ast::BuiltinDecoration>(Source{}, out.module.create<ast::BuiltinDecoration>(Source{},

View File

@ -65,13 +65,14 @@ ast::Variable* clone_variable_with_new_name(ast::CloneContext* ctx,
ast::Variable* in, ast::Variable* in,
std::string new_name) { std::string new_name) {
return ctx->mod->create<ast::Variable>( return ctx->mod->create<ast::Variable>(
ctx->Clone(in->source()), // source ctx->Clone(in->source()), // source
new_name, // name ctx->mod->RegisterSymbol(new_name), // symbol
in->storage_class(), // storage_class new_name, // name
ctx->Clone(in->type()), // type in->storage_class(), // storage_class
in->is_const(), // is_const ctx->Clone(in->type()), // type
ctx->Clone(in->constructor()), // constructor in->is_const(), // is_const
ctx->Clone(in->decorations())); // decorations ctx->Clone(in->constructor()), // constructor
ctx->Clone(in->decorations())); // decorations
} }
} // namespace } // namespace
@ -226,12 +227,13 @@ ast::Variable* FirstIndexOffset::AddUniformBuffer(ast::Module* mod) {
mod->create<ast::Struct>(Source{}, std::move(members), std::move(decos))); mod->create<ast::Struct>(Source{}, std::move(members), std::move(decos)));
auto* idx_var = mod->create<ast::Variable>( auto* idx_var = mod->create<ast::Variable>(
Source{}, // source Source{}, // source
kBufferName, // name mod->RegisterSymbol(kBufferName), // symbol
ast::StorageClass::kUniform, // storage_class kBufferName, // name
struct_type, // type ast::StorageClass::kUniform, // storage_class
false, // is_const struct_type, // type
nullptr, // constructor false, // is_const
nullptr, // constructor
ast::VariableDecorationList{ ast::VariableDecorationList{
mod->create<ast::BindingDecoration>(Source{}, binding_), mod->create<ast::BindingDecoration>(Source{}, binding_),
mod->create<ast::SetDecoration>(Source{}, set_), mod->create<ast::SetDecoration>(Source{}, set_),
@ -261,8 +263,9 @@ ast::VariableDeclStatement* FirstIndexOffset::CreateFirstIndexOffset(
mod->create<ast::IdentifierExpression>( mod->create<ast::IdentifierExpression>(
Source{}, mod->RegisterSymbol(field_name), field_name))); Source{}, mod->RegisterSymbol(field_name), field_name)));
auto* var = auto* var =
mod->create<ast::Variable>(Source{}, // source mod->create<ast::Variable>(Source{}, // source
original_name, // name mod->RegisterSymbol(original_name), // symbol
original_name, // name
ast::StorageClass::kNone, // storage_class ast::StorageClass::kNone, // storage_class
mod->create<ast::type::U32>(), // type mod->create<ast::type::U32>(), // type
true, // is_const true, // is_const

View File

@ -165,18 +165,19 @@ void VertexPulling::State::FindOrInsertVertexIndexIfUsed() {
// We didn't find a vertex index builtin, so create one // We didn't find a vertex index builtin, so create one
vertex_index_name = kDefaultVertexIndexName; vertex_index_name = kDefaultVertexIndexName;
auto* var = auto* var = out->create<ast::Variable>(
out->create<ast::Variable>(Source{}, // source Source{}, // source
vertex_index_name, // name out->RegisterSymbol(vertex_index_name), // symbol
ast::StorageClass::kInput, // storage_class vertex_index_name, // name
GetI32Type(), // type ast::StorageClass::kInput, // storage_class
false, // is_const GetI32Type(), // type
nullptr, // constructor false, // is_const
ast::VariableDecorationList{ nullptr, // constructor
// decorations ast::VariableDecorationList{
out->create<ast::BuiltinDecoration>( // decorations
Source{}, ast::Builtin::kVertexIdx), out->create<ast::BuiltinDecoration>(Source{},
}); ast::Builtin::kVertexIdx),
});
out->AddGlobalVariable(var); out->AddGlobalVariable(var);
} }
@ -212,18 +213,19 @@ void VertexPulling::State::FindOrInsertInstanceIndexIfUsed() {
// We didn't find an instance index builtin, so create one // We didn't find an instance index builtin, so create one
instance_index_name = kDefaultInstanceIndexName; instance_index_name = kDefaultInstanceIndexName;
auto* var = auto* var = out->create<ast::Variable>(
out->create<ast::Variable>(Source{}, // source Source{}, // source
instance_index_name, // name out->RegisterSymbol(instance_index_name), // symbol
ast::StorageClass::kInput, // storage_class instance_index_name, // name
GetI32Type(), // type ast::StorageClass::kInput, // storage_class
false, // is_const GetI32Type(), // type
nullptr, // constructor false, // is_const
ast::VariableDecorationList{ nullptr, // constructor
// decorations ast::VariableDecorationList{
out->create<ast::BuiltinDecoration>( // decorations
Source{}, ast::Builtin::kInstanceIdx), out->create<ast::BuiltinDecoration>(Source{},
}); ast::Builtin::kInstanceIdx),
});
out->AddGlobalVariable(var); out->AddGlobalVariable(var);
} }
@ -241,6 +243,7 @@ void VertexPulling::State::ConvertVertexInputVariablesToPrivate() {
// place in the AST. // place in the AST.
v = out->create<ast::Variable>( v = out->create<ast::Variable>(
Source{}, // source Source{}, // source
v->symbol(), // symbol
v->name(), // name v->name(), // name
ast::StorageClass::kPrivate, // storage_class ast::StorageClass::kPrivate, // storage_class
v->type(), // type v->type(), // type
@ -282,9 +285,11 @@ void VertexPulling::State::AddVertexStorageBuffers() {
for (uint32_t i = 0; i < cfg.vertex_state.size(); ++i) { for (uint32_t i = 0; i < cfg.vertex_state.size(); ++i) {
// The decorated variable with struct type // The decorated variable with struct type
std::string name = GetVertexBufferName(i);
auto* var = out->create<ast::Variable>( auto* var = out->create<ast::Variable>(
Source{}, // source Source{}, // source
GetVertexBufferName(i), // name out->RegisterSymbol(name), // symbol
name, // name
ast::StorageClass::kStorageBuffer, // storage_class ast::StorageClass::kStorageBuffer, // storage_class
struct_type, // type struct_type, // type
false, // is_const false, // is_const
@ -308,13 +313,14 @@ ast::BlockStatement* VertexPulling::State::CreateVertexPullingPreamble() const {
// Declare the |kPullingPosVarName| variable in the shader // Declare the |kPullingPosVarName| variable in the shader
auto* pos_declaration = out->create<ast::VariableDeclStatement>( auto* pos_declaration = out->create<ast::VariableDeclStatement>(
Source{}, out->create<ast::Variable>( Source{}, out->create<ast::Variable>(
Source{}, // source Source{}, // source
kPullingPosVarName, // name out->RegisterSymbol(kPullingPosVarName), // symbol
ast::StorageClass::kFunction, // storage_class kPullingPosVarName, // name
GetI32Type(), // type ast::StorageClass::kFunction, // storage_class
false, // is_const GetI32Type(), // type
nullptr, // constructor false, // is_const
ast::VariableDecorationList{})); // decorations nullptr, // constructor
ast::VariableDecorationList{})); // decorations
// |kPullingPosVarName| refers to the byte location of the current read. We // |kPullingPosVarName| refers to the byte location of the current read. We
// declare a variable in the shader to avoid having to reuse Expression // declare a variable in the shader to avoid having to reuse Expression

View File

@ -50,6 +50,7 @@ class VertexPullingHelper : public ast::BuilderWithModule {
Func("main", ast::VariableList{}, ty.void_, ast::StatementList{}, Func("main", ast::VariableList{}, ty.void_, ast::StatementList{},
ast::FunctionDecorationList{ ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex)}); create<ast::StageDecoration>(ast::PipelineStage::kVertex)});
mod->AddFunction(func); mod->AddFunction(func);
} }
@ -399,20 +400,17 @@ TEST_F(VertexPullingTest, OneAttributeDifferentOutputSet) {
TEST_F(VertexPullingTest, ExistingVertexIndexAndInstanceIndex) { TEST_F(VertexPullingTest, ExistingVertexIndexAndInstanceIndex) {
InitBasicModule(); InitBasicModule();
ast::type::F32 f32; AddVertexInputVariable(0, "var_a", ty.f32);
AddVertexInputVariable(0, "var_a", &f32); AddVertexInputVariable(1, "var_b", ty.f32);
AddVertexInputVariable(1, "var_b", &f32);
ast::type::I32 i32;
mod->AddGlobalVariable( mod->AddGlobalVariable(
Var("custom_vertex_index", ast::StorageClass::kInput, &i32, nullptr, Var("custom_vertex_index", ast::StorageClass::kInput, ty.i32, nullptr,
ast::VariableDecorationList{ ast::VariableDecorationList{
create<ast::BuiltinDecoration>(ast::Builtin::kVertexIdx), create<ast::BuiltinDecoration>(ast::Builtin::kVertexIdx),
})); }));
mod->AddGlobalVariable( mod->AddGlobalVariable(
Var("custom_instance_index", ast::StorageClass::kInput, &i32, nullptr, Var("custom_instance_index", ast::StorageClass::kInput, ty.i32, nullptr,
ast::VariableDecorationList{ ast::VariableDecorationList{
create<ast::BuiltinDecoration>(ast::Builtin::kInstanceIdx), create<ast::BuiltinDecoration>(ast::Builtin::kInstanceIdx),
})); }));

View File

@ -35,47 +35,22 @@ namespace {
using MslGeneratorImplTest = TestHelper; using MslGeneratorImplTest = TestHelper;
TEST_F(MslGeneratorImplTest, Emit_ModuleConstant) { TEST_F(MslGeneratorImplTest, Emit_ModuleConstant) {
ast::type::F32 f32; ast::type::Array ary(ty.f32, 3, ast::ArrayDecorationList{});
ast::type::Array ary(&f32, 3, ast::ArrayDecorationList{}); auto* var = Const(
"pos", ast::StorageClass::kNone, &ary,
ast::ExpressionList exprs; create<ast::TypeConstructorExpression>(
exprs.push_back(create<ast::ScalarConstructorExpression>( Source{}, &ary, ast::ExpressionList{Expr(1.f), Expr(2.f), Expr(3.f)}),
Source{}, create<ast::FloatLiteral>(Source{}, &f32, 1.0f))); ast::VariableDecorationList{});
exprs.push_back(create<ast::ScalarConstructorExpression>(
Source{}, create<ast::FloatLiteral>(Source{}, &f32, 2.0f)));
exprs.push_back(create<ast::ScalarConstructorExpression>(
Source{}, create<ast::FloatLiteral>(Source{}, &f32, 3.0f)));
auto* var =
create<ast::Variable>(Source{}, // source
"pos", // name
ast::StorageClass::kNone, // storage_class
&ary, // type
true, // is_const
create<ast::TypeConstructorExpression>(
Source{}, &ary, exprs), // constructor
ast::VariableDecorationList{}); // decorations
ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error(); ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error();
EXPECT_EQ(gen.result(), "constant float pos[3] = {1.0f, 2.0f, 3.0f};\n"); EXPECT_EQ(gen.result(), "constant float pos[3] = {1.0f, 2.0f, 3.0f};\n");
} }
TEST_F(MslGeneratorImplTest, Emit_SpecConstant) { TEST_F(MslGeneratorImplTest, Emit_SpecConstant) {
ast::type::F32 f32; auto* var = Const("pos", ast::StorageClass::kNone, ty.f32, Expr(3.f),
ast::VariableDecorationList{
auto* var = create<ast::Variable>( create<ast::ConstantIdDecoration>(Source{}, 23),
Source{}, // source });
"pos", // name
ast::StorageClass::kNone, // storage_class
&f32, // type
true, // is_const
create<ast::ScalarConstructorExpression>(
Source{},
create<ast::FloatLiteral>(Source{}, &f32, 3.0f)), // constructor
ast::VariableDecorationList{
// decorations
create<ast::ConstantIdDecoration>(Source{}, 23),
});
ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error(); ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error();
EXPECT_EQ(gen.result(), "constant float pos [[function_constant(23)]];\n"); EXPECT_EQ(gen.result(), "constant float pos [[function_constant(23)]];\n");

View File

@ -45,12 +45,10 @@ namespace {
using BuilderTest = TestHelper; using BuilderTest = TestHelper;
TEST_F(BuilderTest, FunctionVar_NoStorageClass) { TEST_F(BuilderTest, FunctionVar_NoStorageClass) {
ast::type::F32 f32; auto* v = Var("var", ast::StorageClass::kNone, ty.f32);
ast::Variable v(Source{}, "var", ast::StorageClass::kNone, &f32, false,
nullptr, ast::VariableDecorationList{});
b.push_function(Function{}); b.push_function(Function{});
EXPECT_TRUE(b.GenerateFunctionVariable(&v)) << b.error(); EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
@ -65,28 +63,17 @@ TEST_F(BuilderTest, FunctionVar_NoStorageClass) {
} }
TEST_F(BuilderTest, FunctionVar_WithConstantConstructor) { TEST_F(BuilderTest, FunctionVar_WithConstantConstructor) {
ast::type::F32 f32; auto* init = create<ast::TypeConstructorExpression>(
ast::type::Vector vec(&f32, 3); Source{}, ty.vec3<f32>(),
ast::ExpressionList{Expr(1.f), Expr(1.f), Expr(3.f)});
ast::ExpressionList vals;
vals.push_back(create<ast::ScalarConstructorExpression>(
Source{}, create<ast::FloatLiteral>(Source{}, &f32, 1.0f)));
vals.push_back(create<ast::ScalarConstructorExpression>(
Source{}, create<ast::FloatLiteral>(Source{}, &f32, 1.0f)));
vals.push_back(create<ast::ScalarConstructorExpression>(
Source{}, create<ast::FloatLiteral>(Source{}, &f32, 3.0f)));
auto* init = create<ast::TypeConstructorExpression>(Source{}, &vec, vals);
EXPECT_TRUE(td.DetermineResultType(init)) << td.error(); EXPECT_TRUE(td.DetermineResultType(init)) << td.error();
ast::Variable v(Source{}, "var", ast::StorageClass::kOutput, &f32, false, auto* v = Var("var", ast::StorageClass::kOutput, ty.f32, init,
init, ast::VariableDecorationList{}); ast::VariableDecorationList{});
td.RegisterVariableForTesting(v);
td.RegisterVariableForTesting(&v);
b.push_function(Function{}); b.push_function(Function{});
EXPECT_TRUE(b.GenerateFunctionVariable(&v)) << b.error(); EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error(); ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %6 "var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %6 "var"
@ -107,31 +94,16 @@ TEST_F(BuilderTest, FunctionVar_WithConstantConstructor) {
} }
TEST_F(BuilderTest, FunctionVar_WithNonConstantConstructor) { TEST_F(BuilderTest, FunctionVar_WithNonConstantConstructor) {
ast::type::F32 f32; auto* init = create<ast::TypeConstructorExpression>(
ast::type::Vector vec(&f32, 2); Source{}, ty.vec2<f32>(), ast::ExpressionList{Expr(1.f), Add(3.f, 3.f)});
auto* rel = create<ast::BinaryExpression>(
Source{}, ast::BinaryOp::kAdd,
create<ast::ScalarConstructorExpression>(
Source{}, create<ast::FloatLiteral>(Source{}, &f32, 3.0f)),
create<ast::ScalarConstructorExpression>(
Source{}, create<ast::FloatLiteral>(Source{}, &f32, 3.0f)));
ast::ExpressionList vals;
vals.push_back(create<ast::ScalarConstructorExpression>(
Source{}, create<ast::FloatLiteral>(Source{}, &f32, 1.0f)));
vals.push_back(rel);
auto* init = create<ast::TypeConstructorExpression>(Source{}, &vec, vals);
EXPECT_TRUE(td.DetermineResultType(init)) << td.error(); EXPECT_TRUE(td.DetermineResultType(init)) << td.error();
ast::Variable v(Source{}, "var", ast::StorageClass::kFunction, &vec, false, auto* v = Var("var", ast::StorageClass::kFunction, ty.vec2<f32>(), init,
init, ast::VariableDecorationList{}); ast::VariableDecorationList{});
td.RegisterVariableForTesting(&v); td.RegisterVariableForTesting(v);
b.push_function(Function{}); b.push_function(Function{});
EXPECT_TRUE(b.GenerateFunctionVariable(&v)) << b.error(); EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error(); ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %7 "var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %7 "var"