From b77399cbaddff1d188f90cf4e962ac17abf58468 Mon Sep 17 00:00:00 2001 From: Sarah Mashayekhi Date: Wed, 5 Aug 2020 15:23:47 +0000 Subject: [PATCH] [validator] implement variable_stack_ Change-Id: I388847770de8dc703e92030f0fab8f9001643f95 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/26006 Reviewed-by: David Neto --- src/validator_impl.cc | 28 ++++++++++++++++++++++++---- src/validator_impl.h | 3 +++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/validator_impl.cc b/src/validator_impl.cc index cf953ba2ae..923ffa86c9 100644 --- a/src/validator_impl.cc +++ b/src/validator_impl.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "src/validator_impl.h" +#include "src/ast/variable_decl_statement.h" namespace tint { @@ -26,12 +27,18 @@ void ValidatorImpl::set_error(const Source& src, const std::string& msg) { } bool ValidatorImpl::Validate(const ast::Module* module) { - if (!module) + if (!module) { return false; - if (!CheckImports(module)) + } + for (const auto& var : module->global_variables()) { + variable_stack_.set_global(var->name(), var.get()); + } + if (!CheckImports(module)) { return false; - if (!ValidateFunctions(module->functions())) + } + if (!ValidateFunctions(module->functions())) { return false; + } return true; } @@ -45,13 +52,26 @@ bool ValidatorImpl::ValidateFunctions(const ast::FunctionList& funcs) { } bool ValidatorImpl::ValidateFunction(const ast::Function* func) { - if (!ValidateStatements(func->body())) + variable_stack_.push_scope(); + + for (const auto& param : func->params()) { + variable_stack_.set(param->name(), param.get()); + } + if (!ValidateStatements(func->body())) { return false; + } + + variable_stack_.pop_scope(); return true; } bool ValidatorImpl::ValidateStatements(const ast::BlockStatement* block) { for (const auto& stmt : *block) { + // TODO(sarahM0): move the folowing to a function + if (stmt->IsVariableDecl()) { + auto* v = stmt->AsVariableDecl(); + variable_stack_.set(v->variable()->name(), v->variable()); + } if (!ValidateStatement(stmt.get())) { return false; } diff --git a/src/validator_impl.h b/src/validator_impl.h index 85e43e8417..d4f4534600 100644 --- a/src/validator_impl.h +++ b/src/validator_impl.h @@ -21,6 +21,8 @@ #include "src/ast/expression.h" #include "src/ast/module.h" #include "src/ast/statement.h" +#include "src/ast/variable.h" +#include "src/scope_stack.h" namespace tint { @@ -73,6 +75,7 @@ class ValidatorImpl { private: std::string error_; + ScopeStack variable_stack_; }; } // namespace tint