compiler: export type for string and bool constants
Also consolidate the identical code for constant type export into a
pair of static methods.
Fixes golang/go#35739
Change-Id: Ifdccd944dbfc032a7bfb29121a5a6e93bcdeb7b7
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/262437
Trust: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/go/expressions.cc b/go/expressions.cc
index 0350e51..e76bc69 100644
--- a/go/expressions.cc
+++ b/go/expressions.cc
@@ -1823,6 +1823,31 @@
return new Unknown_expression(no, location);
}
+// Start exporting a type conversion for a constant, if needed. This
+// returns whether we need to export a closing parenthesis.
+
+bool
+Expression::export_constant_type(Export_function_body* efb, Type* type)
+{
+ if (type == NULL
+ || type->is_abstract()
+ || type == efb->type_context())
+ return false;
+ efb->write_c_string("$convert(");
+ efb->write_type(type);
+ efb->write_c_string(", ");
+ return true;
+}
+
+// Finish a type conversion for a constant.
+
+void
+Expression::finish_export_constant_type(Export_function_body* efb, bool needed)
+{
+ if (needed)
+ efb->write_c_string(")");
+}
+
// A boolean expression.
class Boolean_expression : public Expression
@@ -1878,8 +1903,7 @@
{ return 1; }
void
- do_export(Export_function_body* efb) const
- { efb->write_c_string(this->val_ ? "$true" : "$false"); }
+ do_export(Export_function_body* efb) const;
void
do_dump_expression(Ast_dump_context* ast_dump_context) const
@@ -1926,6 +1950,16 @@
this->type_ = Type::lookup_bool_type();
}
+// Export a boolean constant.
+
+void
+Boolean_expression::do_export(Export_function_body* efb) const
+{
+ bool exported_type = Expression::export_constant_type(efb, this->type_);
+ efb->write_c_string(this->val_ ? "$true" : "$false");
+ Expression::finish_export_constant_type(efb, exported_type);
+}
+
// Import a boolean constant.
Expression*
@@ -2055,7 +2089,9 @@
void
String_expression::do_export(Export_function_body* efb) const
{
+ bool exported_type = Expression::export_constant_type(efb, this->type_);
String_expression::export_string(efb, this);
+ Expression::finish_export_constant_type(efb, exported_type);
}
// Import a string expression.
@@ -2547,16 +2583,7 @@
void
Integer_expression::do_export(Export_function_body* efb) const
{
- bool added_type = false;
- if (this->type_ != NULL
- && !this->type_->is_abstract()
- && this->type_ != efb->type_context())
- {
- efb->write_c_string("$convert(");
- efb->write_type(this->type_);
- efb->write_c_string(", ");
- added_type = true;
- }
+ bool exported_type = Expression::export_constant_type(efb, this->type_);
Integer_expression::export_integer(efb, this->val_);
if (this->is_character_constant_)
@@ -2564,8 +2591,7 @@
// A trailing space lets us reliably identify the end of the number.
efb->write_c_string(" ");
- if (added_type)
- efb->write_c_string(")");
+ Expression::finish_export_constant_type(efb, exported_type);
}
// Import an integer, floating point, or complex value. This handles
@@ -2953,23 +2979,13 @@
void
Float_expression::do_export(Export_function_body* efb) const
{
- bool added_type = false;
- if (this->type_ != NULL
- && !this->type_->is_abstract()
- && this->type_ != efb->type_context())
- {
- efb->write_c_string("$convert(");
- efb->write_type(this->type_);
- efb->write_c_string(", ");
- added_type = true;
- }
+ bool exported_type = Expression::export_constant_type(efb, this->type_);
Float_expression::export_float(efb, this->val_);
// A trailing space lets us reliably identify the end of the number.
efb->write_c_string(" ");
- if (added_type)
- efb->write_c_string(")");
+ Expression::finish_export_constant_type(efb, exported_type);
}
// Dump a floating point number to the dump file.
@@ -3184,23 +3200,13 @@
void
Complex_expression::do_export(Export_function_body* efb) const
{
- bool added_type = false;
- if (this->type_ != NULL
- && !this->type_->is_abstract()
- && this->type_ != efb->type_context())
- {
- efb->write_c_string("$convert(");
- efb->write_type(this->type_);
- efb->write_c_string(", ");
- added_type = true;
- }
+ bool exported_type = Expression::export_constant_type(efb, this->type_);
Complex_expression::export_complex(efb, this->val_);
// A trailing space lets us reliably identify the end of the number.
efb->write_c_string(" ");
- if (added_type)
- efb->write_c_string(")");
+ Expression::finish_export_constant_type(efb, exported_type);
}
// Dump a complex expression to the dump file.
diff --git a/go/expressions.h b/go/expressions.h
index acb2732..d297523 100644
--- a/go/expressions.h
+++ b/go/expressions.h
@@ -1237,6 +1237,14 @@
virtual void
do_dump_expression(Ast_dump_context*) const = 0;
+ // Start exporting a type conversion for a constant, if needed.
+ static bool
+ export_constant_type(Export_function_body*, Type*);
+
+ // Finish exporting a type conversion for a constant.
+ static void
+ finish_export_constant_type(Export_function_body*, bool);
+
// Varargs lowering creates a slice object (unnamed compiler temp)
// to contain the variable length collection of values. The enum
// below tells the lowering routine whether it can mark that temp