Migrate from using ast::Module to Program

Enforce all places where Dawn passes in or returns a ast::Module, now takes a `const Program* ` or returns a `Program`.

As the end goal of all this is to have immutable Programs, all Program inputs take a pointer instead of moving the actual object.

As consumers of a Program are now all const, we have to const_cast to work around all the places we've been incorrectly mutating a ast::Module.
These const_casts are temporary, and will be fixed in the next set of changes.

Depends on https://dawn-review.googlesource.com/c/dawn/+/38522

Bug: tint:390
Change-Id: Ie05b112b16134937d1b601e9b713ea4ec4e1c677
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38541
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2021-01-26 16:57:10 +00:00
parent be610ba987
commit c40f627bea
252 changed files with 1978 additions and 2017 deletions

View File

@@ -22,8 +22,8 @@ Validator::Validator() = default;
Validator::~Validator() = default;
bool Validator::Validate(const ast::Module* module) {
ValidatorImpl impl(module);
bool Validator::Validate(const Program* program) {
ValidatorImpl impl(program);
bool ret = impl.Validate();
diags_ = impl.diagnostics();
return ret;

View File

@@ -20,7 +20,6 @@
#include "src/ast/assignment_statement.h"
#include "src/ast/expression.h"
#include "src/ast/module.h"
#include "src/ast/statement.h"
#include "src/diagnostic/diagnostic.h"
#include "src/diagnostic/formatter.h"
@@ -28,22 +27,17 @@
namespace tint {
/// Determines if the module is complete and valid
/// Determines if the program is complete and valid
class Validator {
public:
/// Constructor
Validator();
~Validator();
/// Runs the validator
/// @param module the module to validate
/// @returns true if the validation was successful
bool Validate(const ast::Module* module);
/// Runs the validator
/// @param program the program to validate
/// @returns true if the validation was successful
bool Validate(const Program* program) { return Validate(&program->module); }
bool Validate(const Program* program);
/// @returns error messages from the validator
std::string error() {

View File

@@ -40,7 +40,7 @@
namespace tint {
ValidatorImpl::ValidatorImpl(const ast::Module* module) : module_(*module) {}
ValidatorImpl::ValidatorImpl(const Program* program) : program_(program) {}
ValidatorImpl::~ValidatorImpl() = default;
@@ -65,16 +65,16 @@ void ValidatorImpl::add_error(const Source& src, const std::string& msg) {
bool ValidatorImpl::Validate() {
function_stack_.push_scope();
if (!ValidateGlobalVariables(module_.global_variables())) {
if (!ValidateGlobalVariables(program_->global_variables())) {
return false;
}
if (!ValidateConstructedTypes(module_.constructed_types())) {
if (!ValidateConstructedTypes(program_->constructed_types())) {
return false;
}
if (!ValidateFunctions(module_.Functions())) {
if (!ValidateFunctions(program_->Functions())) {
return false;
}
if (!ValidateEntryPoint(module_.Functions())) {
if (!ValidateEntryPoint(program_->Functions())) {
return false;
}
function_stack_.pop_scope();
@@ -99,7 +99,7 @@ bool ValidatorImpl::ValidateConstructedTypes(
add_error(member->source(), "v-0031",
"a struct containing a runtime-sized array "
"must be in the 'storage' storage class: '" +
module_.SymbolToName(st->symbol()) + "'");
program_->SymbolToName(st->symbol()) + "'");
return false;
}
}
@@ -116,7 +116,7 @@ bool ValidatorImpl::ValidateGlobalVariables(
if (variable_stack_.has(var->symbol())) {
add_error(var->source(), "v-0011",
"redeclared global identifier '" +
module_.SymbolToName(var->symbol()) + "'");
program_->SymbolToName(var->symbol()) + "'");
return false;
}
if (!var->is_const() && var->storage_class() == ast::StorageClass::kNone) {
@@ -140,7 +140,7 @@ bool ValidatorImpl::ValidateFunctions(const ast::FunctionList& funcs) {
if (function_stack_.has(func->symbol())) {
add_error(func->source(), "v-0016",
"function names must be unique '" +
module_.SymbolToName(func->symbol()) + "'");
program_->SymbolToName(func->symbol()) + "'");
return false;
}
@@ -163,14 +163,14 @@ bool ValidatorImpl::ValidateEntryPoint(const ast::FunctionList& funcs) {
if (!func->params().empty()) {
add_error(func->source(), "v-0023",
"Entry point function must accept no parameters: '" +
module_.SymbolToName(func->symbol()) + "'");
program_->SymbolToName(func->symbol()) + "'");
return false;
}
if (!func->return_type()->Is<type::Void>()) {
add_error(func->source(), "v-0024",
"Entry point function must return void: '" +
module_.SymbolToName(func->symbol()) + "'");
program_->SymbolToName(func->symbol()) + "'");
return false;
}
auto stage_deco_count = 0;
@@ -275,7 +275,7 @@ bool ValidatorImpl::ValidateDeclStatement(
error_code = "v-0013";
}
add_error(decl->source(), error_code,
"redeclared identifier '" + module_.SymbolToName(symbol) + "'");
"redeclared identifier '" + program_->SymbolToName(symbol) + "'");
return false;
}
// TODO(dneto): Check type compatibility of the initializer.
@@ -415,13 +415,13 @@ bool ValidatorImpl::ValidateCallExpr(const ast::CallExpression* expr) {
if (!function_stack_.has(symbol)) {
add_error(expr->source(), "v-0005",
"function must be declared before use: '" +
module_.SymbolToName(symbol) + "'");
program_->SymbolToName(symbol) + "'");
return false;
}
if (symbol == current_function_->symbol()) {
add_error(
expr->source(), "v-0004",
"recursion is not allowed: '" + module_.SymbolToName(symbol) + "'");
add_error(expr->source(), "v-0004",
"recursion is not allowed: '" +
program_->SymbolToName(symbol) + "'");
return false;
}
}
@@ -447,7 +447,7 @@ bool ValidatorImpl::ValidateBadAssignmentToIdentifier(
if (var->is_const()) {
add_error(assign->source(), "v-0021",
"cannot re-assign a constant: '" +
module_.SymbolToName(ident->symbol()) + "'");
program_->SymbolToName(ident->symbol()) + "'");
return false;
}
} else {
@@ -455,7 +455,7 @@ bool ValidatorImpl::ValidateBadAssignmentToIdentifier(
// when validating the subexpression.
add_error(
ident->source(), "v-0006",
"'" + module_.SymbolToName(ident->symbol()) + "' is not declared");
"'" + program_->SymbolToName(ident->symbol()) + "' is not declared");
return false;
}
return true;
@@ -527,7 +527,7 @@ bool ValidatorImpl::ValidateIdentifier(const ast::IdentifierExpression* ident) {
if (!variable_stack_.get(ident->symbol(), &var)) {
add_error(
ident->source(), "v-0006",
"'" + module_.SymbolToName(ident->symbol()) + "' is not declared");
"'" + program_->SymbolToName(ident->symbol()) + "' is not declared");
return false;
}
return true;

View File

@@ -24,7 +24,6 @@
#include "src/ast/call_expression.h"
#include "src/ast/expression.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/module.h"
#include "src/ast/return_statement.h"
#include "src/ast/statement.h"
#include "src/ast/switch_statement.h"
@@ -32,16 +31,17 @@
#include "src/ast/variable_decl_statement.h"
#include "src/diagnostic/diagnostic.h"
#include "src/diagnostic/formatter.h"
#include "src/program.h"
#include "src/scope_stack.h"
namespace tint {
/// Determines if the module is complete and valid
/// Determines if the program is complete and valid
class ValidatorImpl {
public:
/// Constructor
/// @param module the module to validate
explicit ValidatorImpl(const ast::Module* module);
/// @param program the program to validate
explicit ValidatorImpl(const Program* program);
~ValidatorImpl();
/// Runs the validator
@@ -159,7 +159,7 @@ class ValidatorImpl {
}
private:
const ast::Module& module_;
const Program* program_;
diag::List diags_;
ScopeStack<ast::Variable*> variable_stack_;
ScopeStack<ast::Function*> function_stack_;

View File

@@ -28,13 +28,13 @@
namespace tint {
/// A helper for testing validation
class ValidatorTestHelper : public ast::BuilderWithModule {
class ValidatorTestHelper : public ast::BuilderWithProgram {
public:
/// Constructor
ValidatorTestHelper();
~ValidatorTestHelper() override;
/// Builds and returns a validator from the module.
/// Builds and returns a validator from the program.
/// @note The validator is only built once. Multiple calls to Build() will
/// return the same ValidatorImpl without rebuilding.
/// @return the built validator
@@ -42,7 +42,7 @@ class ValidatorTestHelper : public ast::BuilderWithModule {
if (val_) {
return *val_;
}
val_ = std::make_unique<ValidatorImpl>(mod);
val_ = std::make_unique<ValidatorImpl>(program);
for (auto* var : vars_for_testing_) {
val_->RegisterVariableForTesting(var);
}