diff options
author | Ian Lance Taylor <iant@golang.org> | 2023-10-19 19:34:31 -0700 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2023-12-18 17:30:56 -0800 |
commit | c20328e7cad2989bcdc9ff5003d6a16405c31ab5 (patch) | |
tree | 837db6d6d15e763947916017b122788e30ea1e59 /gcc/go/gofrontend/parse.cc | |
parent | 15cb5204e4c5f79d1b7179ae2590bb65e24b745f (diff) | |
download | gcc-c20328e7cad2989bcdc9ff5003d6a16405c31ab5.zip gcc-c20328e7cad2989bcdc9ff5003d6a16405c31ab5.tar.gz gcc-c20328e7cad2989bcdc9ff5003d6a16405c31ab5.tar.bz2 |
compiler: move lowering pass after check types pass
This change moves the lowering pass after the type determination and
the type checking passes. This lets us simplify some of the code that
determines the type of an expression, which previously had to work
correctly both before and after type determination.
I'm doing this to help with future generic support. For example, with
generics, we can see code like
func ident[T any](v T) T { return v }
func F() int32 {
s := int32(1)
return ident(s)
}
Before this change, we would type check return statements in the
lowering pass (see Return_statement::do_lower). With a generic
example like the above, that means we have to determine the type of s,
and use that to infer the type arguments passed to ident, and use that
to determine the result type of ident. That is too much to do at
lowering time. Of course we can change the way that return statements
work, but similar issues arise with index expressions, the types of
closures for function literals, and probably other cases as well.
Rather than try to deal with all those cases, we move the lowering
pass after type checking. This requires a bunch of changes, notably
for determining constant types. We have to add type checking for
various constructs that formerly disappeared in the lowering pass.
So it's a lot of shuffling. Sorry for the size of the patch.
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/536643
Diffstat (limited to 'gcc/go/gofrontend/parse.cc')
-rw-r--r-- | gcc/go/gofrontend/parse.cc | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/gcc/go/gofrontend/parse.cc b/gcc/go/gofrontend/parse.cc index d741058..a4e4ae3 100644 --- a/gcc/go/gofrontend/parse.cc +++ b/gcc/go/gofrontend/parse.cc @@ -2318,6 +2318,7 @@ Parse::simple_var_decl_or_assignment(const std::string& name, go_error_at(id_location, "%qs repeated on left side of %s", Gogo::message_name(id).c_str(), ":="); + id = this->gogo_->pack_hidden_name("_", false); } til.push_back(Typed_identifier(id, NULL, location)); } @@ -2330,10 +2331,12 @@ Parse::simple_var_decl_or_assignment(const std::string& name, id = this->gogo_->pack_hidden_name(id, is_id_exported); ins = uniq_idents.insert(id); + std::string name = id; if (!ins.second && !Gogo::is_sink_name(id)) { dup_name = Gogo::message_name(id); dup_loc = id_location; + id = this->gogo_->pack_hidden_name("_", false); } til.push_back(Typed_identifier(id, NULL, location)); } @@ -3511,9 +3514,9 @@ Parse::id_to_expression(const std::string& name, Location location, if (is_composite_literal_key) { // This is a composite literal key, which means that it - // could just be a struct field name, so avoid confusiong by + // could just be a struct field name, so avoid confusion by // not adding it to the bindings. We'll look up the name - // later during the lowering phase if necessary. + // later during the determine types phase if necessary. return Expression::make_composite_literal_key(name, location); } named_object = this->gogo_->add_unknown_name(name, location); @@ -4496,12 +4499,12 @@ Parse::return_stat() Expression_list* vals = NULL; if (this->expression_may_start_here()) vals = this->expression_list(NULL, false, true); - this->gogo_->add_statement(Statement::make_return_statement(vals, location)); + Named_object* function = this->gogo_->current_function(); + this->gogo_->add_statement(Statement::make_return_statement(function, vals, + location)); - if (vals == NULL - && this->gogo_->current_function()->func_value()->results_are_named()) + if (vals == NULL && function->func_value()->results_are_named()) { - Named_object* function = this->gogo_->current_function(); Function::Results* results = function->func_value()->result_variables(); for (Function::Results::const_iterator p = results->begin(); p != results->end(); |