[validator] implement variable_stack_ Change-Id: I388847770de8dc703e92030f0fab8f9001643f95 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/26006 Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/validator_impl.cc b/src/validator_impl.cc index cf953ba..923ffa8 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 @@ } 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::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 85e43e8..d4f4534 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 @@ private: std::string error_; + ScopeStack<ast::Variable*> variable_stack_; }; } // namespace tint