[validation] Validate globals in declared order

Instead of validating all global variables and then functions,
validate global declarations in the order they were added to the AST.

This fixes false-positive "redeclared identifier" errors when a global
variable is declared after a function that declares a variable of the
same name, and false-negative "identifier not declared" errors when a
global variable is declared after a function that tries to use it.

Change-Id: Ibf5e5265bc2f8ca892096f0420757b70e1984525
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/41302
Commit-Queue: James Price <jrprice@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
James Price
2021-02-09 23:22:32 +00:00
committed by Commit Bot service account
parent 558385357f
commit c0f30195a0
5 changed files with 149 additions and 121 deletions

View File

@@ -76,14 +76,10 @@ class ValidatorImpl {
/// @param msg the error message
void add_error(const Source& src, const std::string& msg);
/// Validate global variables
/// @param global_vars list of global variables to check
/// Validates a global variable
/// @param var the global variable to check
/// @returns true if the validation was successful
bool ValidateGlobalVariables(const ast::VariableList& global_vars);
/// Validates Functions
/// @param funcs the functions to check
/// @returns true if the validation was successful
bool ValidateFunctions(const ast::FunctionList& funcs);
bool ValidateGlobalVariable(const ast::Variable* var);
/// Validates a function
/// @param func the function to check
/// @returns true if the validation was successful
@@ -144,11 +140,10 @@ class ValidatorImpl {
/// @returns true if the valdiation was successful
bool ValidateEntryPoint(const ast::FunctionList& funcs);
/// Validates constructed types
/// @param constructed_types the types to check
/// Validates a constructed type
/// @param type the type to check
/// @returns true if the valdiation was successful
bool ValidateConstructedTypes(
const std::vector<type::Type*>& constructed_types);
bool ValidateConstructedType(const type::Type* type);
/// Returns true if the given type is storable. This uses and
/// updates `storable_` and `not_storable_`.
@@ -165,8 +160,8 @@ class ValidatorImpl {
private:
const Program* program_;
diag::List diags_;
ScopeStack<ast::Variable*> variable_stack_;
ScopeStack<ast::Function*> function_stack_;
ScopeStack<const ast::Variable*> variable_stack_;
ScopeStack<const ast::Function*> function_stack_;
ast::Function* current_function_ = nullptr;
};