From c20328e7cad2989bcdc9ff5003d6a16405c31ab5 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Thu, 19 Oct 2023 19:34:31 -0700 Subject: 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 --- gcc/go/gofrontend/runtime.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'gcc/go/gofrontend/runtime.cc') diff --git a/gcc/go/gofrontend/runtime.cc b/gcc/go/gofrontend/runtime.cc index 274aa4a..e4dbd26 100644 --- a/gcc/go/gofrontend/runtime.cc +++ b/gcc/go/gofrontend/runtime.cc @@ -256,8 +256,8 @@ runtime_function_type(Runtime_function_type bft) // Convert an expression to the type to pass to a runtime function. static Expression* -convert_to_runtime_function_type(Runtime_function_type bft, Expression* e, - Location loc) +convert_to_runtime_function_type(Gogo* gogo, Runtime_function_type bft, + Expression* e, Location loc) { switch (bft) { @@ -284,6 +284,8 @@ convert_to_runtime_function_type(Runtime_function_type bft, Expression* e, case RFT_POINTER: { Type* t = runtime_function_type(bft); + Type_context context(t, false); + e->determine_type(gogo, &context); if (!Type::are_identical(t, e->type(), true, NULL)) e = Expression::make_cast(t, e, loc); return e; @@ -414,7 +416,7 @@ Runtime::runtime_declaration(Function code) // Make a call to a runtime function. Call_expression* -Runtime::make_call(Gogo*, Runtime::Function code, Location loc, +Runtime::make_call(Gogo* gogo, Runtime::Function code, Location loc, int param_count, ...) { go_assert(code < Runtime::NUMBER_OF_FUNCTIONS); @@ -436,7 +438,7 @@ Runtime::make_call(Gogo*, Runtime::Function code, Location loc, { Expression* e = va_arg(ap, Expression*); Runtime_function_type rft = pb->parameter_types[i]; - args->push_back(convert_to_runtime_function_type(rft, e, loc)); + args->push_back(convert_to_runtime_function_type(gogo, rft, e, loc)); } va_end(ap); -- cgit v1.1