diff --git a/src/ast/function.h b/src/ast/function.h index 4f22016b2a..151794f187 100644 --- a/src/ast/function.h +++ b/src/ast/function.h @@ -74,7 +74,7 @@ class Function : public Castable { /// @returns the function symbol Symbol symbol() const { return symbol_; } /// @returns the function name - const std::string& name() { return name_; } + const std::string& name_for_clone() { return name_; } /// @returns the function params const VariableList& params() const { return params_; } diff --git a/src/ast/function_test.cc b/src/ast/function_test.cc index 039bc286b8..548aedc791 100644 --- a/src/ast/function_test.cc +++ b/src/ast/function_test.cc @@ -36,7 +36,6 @@ TEST_F(FunctionTest, Creation) { auto* f = Func("func", params, ty.void_, StatementList{}, FunctionDecorationList{}); EXPECT_EQ(f->symbol(), mod->RegisterSymbol("func")); - EXPECT_EQ(f->name(), "func"); ASSERT_EQ(f->params().size(), 1u); EXPECT_EQ(f->return_type(), ty.void_); EXPECT_EQ(f->params()[0], var); diff --git a/src/inspector/inspector.cc b/src/inspector/inspector.cc index a06bd77518..47995d15d3 100644 --- a/src/inspector/inspector.cc +++ b/src/inspector/inspector.cc @@ -57,8 +57,8 @@ std::vector Inspector::GetEntryPoints() { } EntryPoint entry_point; - entry_point.name = func->name(); - entry_point.remapped_name = func->name(); + entry_point.name = module_.SymbolToName(func->symbol()); + entry_point.remapped_name = namer_->NameFor(func->symbol()); entry_point.stage = func->pipeline_stage(); std::tie(entry_point.workgroup_size_x, entry_point.workgroup_size_y, entry_point.workgroup_size_z) = func->workgroup_size(); diff --git a/src/reader/wgsl/parser_impl_function_decl_test.cc b/src/reader/wgsl/parser_impl_function_decl_test.cc index c8de53554a..519e40848e 100644 --- a/src/reader/wgsl/parser_impl_function_decl_test.cc +++ b/src/reader/wgsl/parser_impl_function_decl_test.cc @@ -37,7 +37,7 @@ TEST_F(ParserImplTest, FunctionDecl) { EXPECT_TRUE(f.matched); ASSERT_NE(f.value, nullptr); - EXPECT_EQ(f->name(), "main"); + EXPECT_EQ(f->symbol(), p->get_module().RegisterSymbol("main")); ASSERT_NE(f->return_type(), nullptr); EXPECT_TRUE(f->return_type()->Is()); @@ -65,7 +65,7 @@ TEST_F(ParserImplTest, FunctionDecl_DecorationList) { EXPECT_TRUE(f.matched); ASSERT_NE(f.value, nullptr); - EXPECT_EQ(f->name(), "main"); + EXPECT_EQ(f->symbol(), p->get_module().RegisterSymbol("main")); ASSERT_NE(f->return_type(), nullptr); EXPECT_TRUE(f->return_type()->Is()); ASSERT_EQ(f->params().size(), 0u); @@ -103,7 +103,7 @@ fn main() -> void { return; })"); EXPECT_TRUE(f.matched); ASSERT_NE(f.value, nullptr); - EXPECT_EQ(f->name(), "main"); + EXPECT_EQ(f->symbol(), p->get_module().RegisterSymbol("main")); ASSERT_NE(f->return_type(), nullptr); EXPECT_TRUE(f->return_type()->Is()); ASSERT_EQ(f->params().size(), 0u); @@ -148,7 +148,7 @@ fn main() -> void { return; })"); EXPECT_TRUE(f.matched); ASSERT_NE(f.value, nullptr); - EXPECT_EQ(f->name(), "main"); + EXPECT_EQ(f->symbol(), p->get_module().RegisterSymbol("main")); ASSERT_NE(f->return_type(), nullptr); EXPECT_TRUE(f->return_type()->Is()); ASSERT_EQ(f->params().size(), 0u); diff --git a/src/transform/transform.cc b/src/transform/transform.cc index 51fe645aa9..3b6da171f9 100644 --- a/src/transform/transform.cc +++ b/src/transform/transform.cc @@ -32,7 +32,7 @@ ast::Function* Transform::CloneWithStatementsAtStart( statements.emplace_back(ctx->Clone(s)); } return ctx->mod->create( - ctx->Clone(in->source()), ctx->Clone(in->symbol()), in->name(), + ctx->Clone(in->source()), ctx->Clone(in->symbol()), in->name_for_clone(), ctx->Clone(in->params()), ctx->Clone(in->return_type()), ctx->mod->create(ctx->Clone(in->body()->source()), statements), diff --git a/src/type_determiner.cc b/src/type_determiner.cc index 967f0e2866..d6a94be34c 100644 --- a/src/type_determiner.cc +++ b/src/type_determiner.cc @@ -121,7 +121,7 @@ bool TypeDeterminer::Determine() { if (!func->IsEntryPoint()) { continue; } - for (const auto& callee : caller_to_callee_[func->name()]) { + for (const auto& callee : caller_to_callee_[func->symbol().value()]) { set_entry_points(callee, func->symbol()); } } @@ -129,11 +129,10 @@ bool TypeDeterminer::Determine() { return true; } -void TypeDeterminer::set_entry_points(const std::string& fn_name, - Symbol ep_sym) { - name_to_function_[fn_name]->add_ancestor_entry_point(ep_sym); +void TypeDeterminer::set_entry_points(const Symbol& fn_sym, Symbol ep_sym) { + symbol_to_function_[fn_sym.value()]->add_ancestor_entry_point(ep_sym); - for (const auto& callee : caller_to_callee_[fn_name]) { + for (const auto& callee : caller_to_callee_[fn_sym.value()]) { set_entry_points(callee, ep_sym); } } @@ -148,7 +147,7 @@ bool TypeDeterminer::DetermineFunctions(const ast::FunctionList& funcs) { } bool TypeDeterminer::DetermineFunction(ast::Function* func) { - name_to_function_[func->name()] = func; + symbol_to_function_[func->symbol().value()] = func; current_function_ = func; @@ -387,13 +386,13 @@ bool TypeDeterminer::DetermineCall(ast::CallExpression* expr) { } } else { if (current_function_) { - caller_to_callee_[current_function_->name()].push_back(ident->name()); + caller_to_callee_[current_function_->symbol().value()].push_back( + ident->symbol()); - auto* callee_func = - mod_->FindFunctionBySymbol(mod_->GetSymbol(ident->name())); + auto* callee_func = mod_->FindFunctionBySymbol(ident->symbol()); if (callee_func == nullptr) { - set_error(expr->source(), - "unable to find called function: " + ident->name()); + set_error(expr->source(), "unable to find called function: " + + mod_->SymbolToName(ident->symbol())); return false; } @@ -416,10 +415,10 @@ bool TypeDeterminer::DetermineCall(ast::CallExpression* expr) { } if (!expr->func()->result_type()) { - auto func_name = expr->func()->As()->name(); - set_error( - expr->source(), - "v-0005: function must be declared before use: '" + func_name + "'"); + auto func_sym = expr->func()->As()->symbol(); + set_error(expr->source(), + "v-0005: function must be declared before use: '" + + mod_->SymbolToName(func_sym) + "'"); return false; } @@ -858,7 +857,6 @@ bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) { } bool TypeDeterminer::DetermineIdentifier(ast::IdentifierExpression* expr) { - auto name = expr->name(); auto symbol = expr->symbol(); ast::Variable* var; if (variable_stack_.get(symbol, &var)) { @@ -877,15 +875,16 @@ bool TypeDeterminer::DetermineIdentifier(ast::IdentifierExpression* expr) { return true; } - auto iter = name_to_function_.find(name); - if (iter != name_to_function_.end()) { + auto iter = symbol_to_function_.find(symbol.value()); + if (iter != symbol_to_function_.end()) { expr->set_result_type(iter->second->return_type()); return true; } if (!SetIntrinsicIfNeeded(expr)) { set_error(expr->source(), - "v-0006: identifier must be declared before use: " + name); + "v-0006: identifier must be declared before use: " + + mod_->SymbolToName(symbol)); return false; } return true; diff --git a/src/type_determiner.h b/src/type_determiner.h index 51f6801224..d13b97c50f 100644 --- a/src/type_determiner.h +++ b/src/type_determiner.h @@ -113,7 +113,7 @@ class TypeDeterminer { private: void set_error(const Source& src, const std::string& msg); void set_referenced_from_function_if_needed(ast::Variable* var, bool local); - void set_entry_points(const std::string& fn_name, Symbol ep_sym); + void set_entry_points(const Symbol& fn_sym, Symbol ep_sym); bool DetermineArrayAccessor(ast::ArrayAccessorExpression* expr); bool DetermineBinary(ast::BinaryExpression* expr); @@ -129,11 +129,11 @@ class TypeDeterminer { ast::Module* mod_; std::string error_; ScopeStack variable_stack_; - std::unordered_map name_to_function_; + std::unordered_map symbol_to_function_; ast::Function* current_function_ = nullptr; // Map from caller functions to callee functions. - std::unordered_map> caller_to_callee_; + std::unordered_map> caller_to_callee_; }; } // namespace tint diff --git a/src/validator/validator_impl.cc b/src/validator/validator_impl.cc index 484b3e982f..5aa6f0ae0d 100644 --- a/src/validator/validator_impl.cc +++ b/src/validator/validator_impl.cc @@ -160,14 +160,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: '" + - func->name() + "'"); + module_.SymbolToName(func->symbol()) + "'"); return false; } if (!func->return_type()->Is()) { - add_error( - func->source(), "v-0024", - "Entry point function must return void: '" + func->name() + "'"); + add_error(func->source(), "v-0024", + "Entry point function must return void: '" + + module_.SymbolToName(func->symbol()) + "'"); return false; } auto stage_deco_count = 0; diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index f45ef18f0f..37093fe30f 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -448,7 +448,7 @@ bool Builder::GenerateEntryPoint(ast::Function* func, uint32_t id) { // OperandList operands = {Operand::Int(stage), Operand::Int(id), // Operand::String(func->name())}; OperandList operands = {Operand::Int(stage), Operand::Int(id), - Operand::String(func->name())}; + Operand::String(namer_->NameFor(func->symbol()))}; for (const auto* var : func->referenced_module_variables()) { // For SPIR-V 1.3 we only output Input/output variables. If we update to @@ -593,8 +593,7 @@ bool Builder::GenerateFunction(ast::Function* func) { scope_stack_.pop_scope(); - func_name_to_id_[func->name()] = func_id; - func_name_to_func_[func->name()] = func; + func_symbol_to_id_[func->symbol().value()] = func_id; return true; } @@ -1820,7 +1819,7 @@ uint32_t Builder::GenerateCallExpression(ast::CallExpression* expr) { OperandList ops = {Operand::Int(type_id), result}; - auto func_id = func_name_to_id_[ident->name()]; + auto func_id = func_symbol_to_id_[ident->symbol().value()]; if (func_id == 0) { error_ = "unable to find called function: " + ident->name(); return 0; diff --git a/src/writer/spirv/builder.h b/src/writer/spirv/builder.h index 451c961c46..900af2018d 100644 --- a/src/writer/spirv/builder.h +++ b/src/writer/spirv/builder.h @@ -490,16 +490,6 @@ class Builder { /// automatically. Operand result_op(); - /// Retrives the id for the given function name - /// @param name the function name to search for - /// @returns the id for the given name or 0 on failure - uint32_t id_for_func_name(const std::string& name) { - if (func_name_to_id_.count(name) == 0) { - return 0; - } - return func_name_to_id_[name]; - } - ast::Module* mod_; std::unique_ptr namer_; std::string error_; @@ -517,8 +507,7 @@ class Builder { std::vector functions_; std::unordered_map import_name_to_id_; - std::unordered_map func_name_to_id_; - std::unordered_map func_name_to_func_; + std::unordered_map func_symbol_to_id_; std::unordered_map type_name_to_id_; std::unordered_map const_to_id_; std::unordered_map diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 544033042a..bf70039d46 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -361,7 +361,7 @@ bool GeneratorImpl::EmitFunction(ast::Function* func) { } make_indent(); - out_ << "fn " << func->name() << "("; + out_ << "fn " << module_.SymbolToName(func->symbol()) << "("; bool first = true; for (auto* v : func->params()) {