[ast] Capture global declaration order

Adds a vector<CastableBase*> to ast::Module which stores the list of
global variables, functions, and types, in the order that they were
declared.

This will be used to fix validation and backend issues around name
uniqueness.

Change-Id: I14491f6ebc0fc7341bd3fb3b3f408faa234a91f7
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/41301
Commit-Queue: James Price <jrprice@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
James Price 2021-02-09 21:39:10 +00:00 committed by Commit Bot service account
parent 3eaa450984
commit 558385357f
4 changed files with 54 additions and 23 deletions

View File

@ -29,18 +29,33 @@ namespace ast {
Module::Module(const Source& source) : Base(source) {} Module::Module(const Source& source) : Base(source) {}
Module::Module(const Source& source, Module::Module(const Source& source, std::vector<CastableBase*> global_decls)
std::vector<type::Type*> constructed_types, : Base(source), global_declarations_(std::move(global_decls)) {
FunctionList functions, for (auto* decl : global_declarations_) {
VariableList global_variables) if (decl == nullptr) {
: Base(source), continue;
constructed_types_(std::move(constructed_types)), }
functions_(std::move(functions)),
global_variables_(std::move(global_variables)) {} if (auto* ty = decl->As<type::Type>()) {
constructed_types_.push_back(ty);
} else if (auto* func = decl->As<Function>()) {
functions_.push_back(func);
} else if (auto* var = decl->As<Variable>()) {
global_variables_.push_back(var);
} else {
assert(false /* unreachable */);
}
}
}
Module::~Module() = default; Module::~Module() = default;
bool Module::IsValid() const { bool Module::IsValid() const {
for (auto* decl : global_declarations_) {
if (decl == nullptr) {
return false;
}
}
for (auto* var : global_variables_) { for (auto* var : global_variables_) {
if (var == nullptr || !var->IsValid()) { if (var == nullptr || !var->IsValid()) {
return false; return false;
@ -76,9 +91,20 @@ bool Module::IsValid() const {
} }
Module* Module::Clone(CloneContext* ctx) const { Module* Module::Clone(CloneContext* ctx) const {
return ctx->dst->create<Module>(ctx->Clone(constructed_types_), std::vector<CastableBase*> global_decls;
ctx->Clone(functions_), for (auto* decl : global_declarations_) {
ctx->Clone(global_variables_)); assert(decl);
if (auto* ty = decl->As<type::Type>()) {
global_decls.push_back(ctx->Clone(ty));
} else if (auto* func = decl->As<Function>()) {
global_decls.push_back(ctx->Clone(func));
} else if (auto* var = decl->As<Variable>()) {
global_decls.push_back(ctx->Clone(var));
} else {
assert(false /* unreachable */);
}
}
return ctx->dst->create<Module>(global_decls);
} }
void Module::to_str(const semantic::Info& sem, void Module::to_str(const semantic::Info& sem,

View File

@ -36,21 +36,23 @@ class Module : public Castable<Module, Node> {
/// Constructor /// Constructor
/// @param source the source of the module /// @param source the source of the module
/// @param constructed_types the list of types explicitly declared in the AST /// @param global_decls the list of global types, functions, and variables, in
/// @param functions the list of program functions /// the order they were declared in the source program
/// @param global_variables the list of global variables Module(const Source& source, std::vector<CastableBase*> global_decls);
Module(const Source& source,
std::vector<type::Type*> constructed_types,
FunctionList functions,
VariableList global_variables);
/// Destructor /// Destructor
~Module() override; ~Module() override;
/// @returns the ordered global declarations for the translation unit
const std::vector<CastableBase*>& GlobalDeclarations() const {
return global_declarations_;
}
/// Add a global variable to the Builder /// Add a global variable to the Builder
/// @param var the variable to add /// @param var the variable to add
void AddGlobalVariable(ast::Variable* var) { void AddGlobalVariable(ast::Variable* var) {
global_variables_.push_back(var); global_variables_.push_back(var);
global_declarations_.push_back(var);
} }
/// @returns the global variables for the translation unit /// @returns the global variables for the translation unit
@ -64,6 +66,7 @@ class Module : public Castable<Module, Node> {
/// @param type the constructed type to add /// @param type the constructed type to add
void AddConstructedType(type::Type* type) { void AddConstructedType(type::Type* type) {
constructed_types_.push_back(type); constructed_types_.push_back(type);
global_declarations_.push_back(type);
} }
/// @returns the constructed types in the translation unit /// @returns the constructed types in the translation unit
@ -73,7 +76,10 @@ class Module : public Castable<Module, Node> {
/// Add a function to the Builder /// Add a function to the Builder
/// @param func the function to add /// @param func the function to add
void AddFunction(ast::Function* func) { functions_.push_back(func); } void AddFunction(ast::Function* func) {
functions_.push_back(func);
global_declarations_.push_back(func);
}
/// @returns the functions declared in the translation unit /// @returns the functions declared in the translation unit
const FunctionList& Functions() const { return functions_; } const FunctionList& Functions() const { return functions_; }
@ -102,6 +108,7 @@ class Module : public Castable<Module, Node> {
std::string to_str(const semantic::Info& sem) const; std::string to_str(const semantic::Info& sem) const;
private: private:
std::vector<CastableBase*> global_declarations_;
std::vector<type::Type*> constructed_types_; std::vector<type::Type*> constructed_types_;
FunctionList functions_; FunctionList functions_;
VariableList global_variables_; VariableList global_variables_;

View File

@ -57,8 +57,7 @@ Program::Program(ProgramBuilder&& builder) {
ast_nodes_ = std::move(builder.ASTNodes()); ast_nodes_ = std::move(builder.ASTNodes());
sem_nodes_ = std::move(builder.SemNodes()); sem_nodes_ = std::move(builder.SemNodes());
ast_ = ast_nodes_.Create<ast::Module>( ast_ = ast_nodes_.Create<ast::Module>(
Source{}, builder.AST().ConstructedTypes(), builder.AST().Functions(), Source{}, std::move(builder.AST().GlobalDeclarations()));
builder.AST().GlobalVariables());
sem_ = std::move(builder.Sem()); sem_ = std::move(builder.Sem());
symbols_ = std::move(builder.Symbols()); symbols_ = std::move(builder.Symbols());
diagnostics_.add(std::move(builder.Diagnostics())); diagnostics_.add(std::move(builder.Diagnostics()));

View File

@ -60,8 +60,7 @@ ProgramBuilder ProgramBuilder::Wrap(const Program* program) {
ProgramBuilder builder; ProgramBuilder builder;
builder.types_ = type::Manager::Wrap(program->Types()); builder.types_ = type::Manager::Wrap(program->Types());
builder.ast_ = builder.create<ast::Module>( builder.ast_ = builder.create<ast::Module>(
program->AST().source(), program->AST().ConstructedTypes(), program->AST().source(), program->AST().GlobalDeclarations());
program->AST().Functions(), program->AST().GlobalVariables());
builder.sem_ = semantic::Info::Wrap(program->Sem()); builder.sem_ = semantic::Info::Wrap(program->Sem());
builder.symbols_ = program->Symbols(); builder.symbols_ = program->Symbols();
builder.diagnostics_ = program->Diagnostics(); builder.diagnostics_ = program->Diagnostics();