Commit 336785ef by Ian Lance Taylor

compiler: change Expression export to use Export_function_body

    
    In preparation for writing expressions to inline function bodies,
    change the existing expression export code to use Export_function_body
    rather than Export.  Adjust existing expression exporters accordingly.
    
    This is a refactoring that doesn't affect compiler output.
    
    Reviewed-on: https://go-review.googlesource.com/c/150063

From-SVN: r266523
parent ba28599c
f551ab95f46c3d7bb7c032711e10b03bfa995ee2 db5240278b3b62a919dd88f857e718a66be50346
The first line of this file holds the git revision number of the last The first line of this file holds the git revision number of the last
merge done from the gofrontend repository. merge done from the gofrontend repository.
...@@ -936,21 +936,41 @@ Export::write_unsigned(unsigned value) ...@@ -936,21 +936,41 @@ Export::write_unsigned(unsigned value)
this->write_c_string(buf); this->write_c_string(buf);
} }
// Export a type. // Return the index of a type.
void int
Export::write_type(const Type* type) Export::type_index(const Type* type)
{ {
type = type->forwarded(); type = type->forwarded();
Type_refs::const_iterator p = type_refs.find(type); Type_refs::const_iterator p = type_refs.find(type);
go_assert(p != type_refs.end()); go_assert(p != type_refs.end());
int index = p->second; int index = p->second;
go_assert(index != 0); go_assert(index != 0);
return index;
}
// Export a type.
void
Export::write_type(const Type* type)
{
int index = this->type_index(type);
char buf[30]; char buf[30];
snprintf(buf, sizeof buf, "<type %d>", index); snprintf(buf, sizeof buf, "<type %d>", index);
this->write_c_string(buf); this->write_c_string(buf);
} }
// Export a type to a function body.
void
Export::write_type_to(const Type* type, Export_function_body* efb)
{
int index = this->type_index(type);
char buf[30];
snprintf(buf, sizeof buf, "<type %d>", index);
efb->write_c_string(buf);
}
// Export escape note. // Export escape note.
void void
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
class Go_sha1_helper; class Go_sha1_helper;
class Gogo; class Gogo;
class Named_object; class Named_object;
class Export_function_body;
class Import_init; class Import_init;
class Named_object; class Named_object;
class Bindings; class Bindings;
...@@ -183,6 +184,10 @@ class Export : public String_dump ...@@ -183,6 +184,10 @@ class Export : public String_dump
void void
write_type(const Type*); write_type(const Type*);
// Write a type to an exported function body.
void
write_type_to(const Type*, Export_function_body*);
// Write the escape note to the export stream. If NOTE is NULL, write // Write the escape note to the export stream. If NOTE is NULL, write
// nothing. // nothing.
void void
...@@ -241,6 +246,10 @@ class Export : public String_dump ...@@ -241,6 +246,10 @@ class Export : public String_dump
void void
register_builtin_type(Gogo*, const char* name, Builtin_code); register_builtin_type(Gogo*, const char* name, Builtin_code);
// Return the index of a type in the export data.
int
type_index(const Type*);
// The stream to which we are writing data. // The stream to which we are writing data.
Stream* stream_; Stream* stream_;
// Index number of next type. // Index number of next type.
...@@ -290,11 +299,11 @@ class Stream_to_string : public Export::Stream ...@@ -290,11 +299,11 @@ class Stream_to_string : public Export::Stream
// to Statements and Expressions. It builds up the export data for // to Statements and Expressions. It builds up the export data for
// the function. // the function.
class Export_function_body class Export_function_body : public String_dump
{ {
public: public:
Export_function_body(int indent) Export_function_body(Export* exp, int indent)
: indent_(indent) : exp_(exp), indent_(indent)
{ } { }
// Write a character to the body. // Write a character to the body.
...@@ -312,6 +321,11 @@ class Export_function_body ...@@ -312,6 +321,11 @@ class Export_function_body
write_string(const std::string& str) write_string(const std::string& str)
{ this->body_.append(str); } { this->body_.append(str); }
// Write a type reference to the body.
void
write_type(const Type* type)
{ this->exp_->write_type_to(type, this); }
// Append as many spaces as the current indentation level. // Append as many spaces as the current indentation level.
void void
indent() indent()
...@@ -336,6 +350,8 @@ class Export_function_body ...@@ -336,6 +350,8 @@ class Export_function_body
{ return this->body_; } { return this->body_; }
private: private:
// The overall export data.
Export* exp_;
// The body we are building. // The body we are building.
std::string body_; std::string body_;
// Current indentation level: the number of spaces before each statement. // Current indentation level: the number of spaces before each statement.
......
...@@ -65,7 +65,7 @@ class Conditional_expression; ...@@ -65,7 +65,7 @@ class Conditional_expression;
class Compound_expression; class Compound_expression;
class Numeric_constant; class Numeric_constant;
class Named_object; class Named_object;
class Export; class Export_function_body;
class Import; class Import;
class Temporary_statement; class Temporary_statement;
class Label; class Label;
...@@ -1009,12 +1009,10 @@ class Expression ...@@ -1009,12 +1009,10 @@ class Expression
backend_numeric_constant_expression(Translate_context*, backend_numeric_constant_expression(Translate_context*,
Numeric_constant* val); Numeric_constant* val);
// Export the expression. This is only used for constants. It will // Export the expression.
// be used for things like values of named constants and sizes of
// arrays.
void void
export_expression(Export* exp) const export_expression(Export_function_body* efb) const
{ this->do_export(exp); } { this->do_export(efb); }
// Import an expression. // Import an expression.
static Expression* static Expression*
...@@ -1130,7 +1128,7 @@ class Expression ...@@ -1130,7 +1128,7 @@ class Expression
// Child class implements export. // Child class implements export.
virtual void virtual void
do_export(Export*) const; do_export(Export_function_body*) const;
// For children to call to give an error for an unused value. // For children to call to give an error for an unused value.
void void
...@@ -1603,7 +1601,7 @@ class String_expression : public Expression ...@@ -1603,7 +1601,7 @@ class String_expression : public Expression
export_string(String_dump* exp, const String_expression* str); export_string(String_dump* exp, const String_expression* str);
void void
do_export(Export*) const; do_export(Export_function_body*) const;
void void
do_dump_expression(Ast_dump_context*) const; do_dump_expression(Ast_dump_context*) const;
...@@ -1687,7 +1685,7 @@ class Type_conversion_expression : public Expression ...@@ -1687,7 +1685,7 @@ class Type_conversion_expression : public Expression
do_get_backend(Translate_context* context); do_get_backend(Translate_context* context);
void void
do_export(Export*) const; do_export(Export_function_body*) const;
void void
do_dump_expression(Ast_dump_context*) const; do_dump_expression(Ast_dump_context*) const;
...@@ -1878,7 +1876,7 @@ class Unary_expression : public Expression ...@@ -1878,7 +1876,7 @@ class Unary_expression : public Expression
do_get_backend(Translate_context*); do_get_backend(Translate_context*);
void void
do_export(Export*) const; do_export(Export_function_body*) const;
void void
do_dump_expression(Ast_dump_context*) const; do_dump_expression(Ast_dump_context*) const;
...@@ -2023,7 +2021,7 @@ class Binary_expression : public Expression ...@@ -2023,7 +2021,7 @@ class Binary_expression : public Expression
do_get_backend(Translate_context*); do_get_backend(Translate_context*);
void void
do_export(Export*) const; do_export(Export_function_body*) const;
void void
do_dump_expression(Ast_dump_context*) const; do_dump_expression(Ast_dump_context*) const;
...@@ -2130,7 +2128,7 @@ class String_concat_expression : public Expression ...@@ -2130,7 +2128,7 @@ class String_concat_expression : public Expression
{ go_unreachable(); } { go_unreachable(); }
void void
do_export(Export*) const do_export(Export_function_body*) const
{ go_unreachable(); } { go_unreachable(); }
void void
...@@ -2456,7 +2454,7 @@ class Builtin_call_expression : public Call_expression ...@@ -2456,7 +2454,7 @@ class Builtin_call_expression : public Call_expression
do_get_backend(Translate_context*); do_get_backend(Translate_context*);
void void
do_export(Export*) const; do_export(Export_function_body*) const;
virtual bool virtual bool
do_is_recover_call() const; do_is_recover_call() const;
...@@ -3551,7 +3549,7 @@ class Struct_construction_expression : public Expression, ...@@ -3551,7 +3549,7 @@ class Struct_construction_expression : public Expression,
do_get_backend(Translate_context*); do_get_backend(Translate_context*);
void void
do_export(Export*) const; do_export(Export_function_body*) const;
void void
do_dump_expression(Ast_dump_context*) const; do_dump_expression(Ast_dump_context*) const;
...@@ -3606,7 +3604,7 @@ protected: ...@@ -3606,7 +3604,7 @@ protected:
do_check_types(Gogo*); do_check_types(Gogo*);
void void
do_export(Export*) const; do_export(Export_function_body*) const;
// The indexes. // The indexes.
const std::vector<unsigned long>* const std::vector<unsigned long>*
...@@ -3744,7 +3742,7 @@ class Map_construction_expression : public Expression ...@@ -3744,7 +3742,7 @@ class Map_construction_expression : public Expression
do_get_backend(Translate_context*); do_get_backend(Translate_context*);
void void
do_export(Export*) const; do_export(Export_function_body*) const;
void void
do_dump_expression(Ast_dump_context*) const; do_dump_expression(Ast_dump_context*) const;
...@@ -3859,7 +3857,7 @@ class Heap_expression : public Expression ...@@ -3859,7 +3857,7 @@ class Heap_expression : public Expression
// We only export global objects, and the parser does not generate // We only export global objects, and the parser does not generate
// this in global scope. // this in global scope.
void void
do_export(Export*) const do_export(Export_function_body*) const
{ go_unreachable(); } { go_unreachable(); }
void void
......
...@@ -5572,7 +5572,7 @@ Function::export_func_with_type(Export* exp, const std::string& name, ...@@ -5572,7 +5572,7 @@ Function::export_func_with_type(Export* exp, const std::string& name,
if (fntype->is_method()) if (fntype->is_method())
indent++; indent++;
Export_function_body efb(indent); Export_function_body efb(exp, indent);
efb.indent(); efb.indent();
efb.write_c_string("// "); efb.write_c_string("// ");
...@@ -7606,7 +7606,11 @@ Named_constant::export_const(Export* exp, const std::string& name) const ...@@ -7606,7 +7606,11 @@ Named_constant::export_const(Export* exp, const std::string& name) const
exp->write_c_string(" "); exp->write_c_string(" ");
} }
exp->write_c_string("= "); exp->write_c_string("= ");
this->expr()->export_expression(exp);
Export_function_body efb(exp, 0);
this->expr()->export_expression(&efb);
exp->write_string(efb.body());
exp->write_c_string("\n"); exp->write_c_string("\n");
} }
......
...@@ -6566,7 +6566,11 @@ Struct_type::do_export(Export* exp) const ...@@ -6566,7 +6566,11 @@ Struct_type::do_export(Export* exp) const
exp->write_c_string(" "); exp->write_c_string(" ");
Expression* expr = Expression* expr =
Expression::make_string(p->tag(), Linemap::predeclared_location()); Expression::make_string(p->tag(), Linemap::predeclared_location());
expr->export_expression(exp);
Export_function_body efb(exp, 0);
expr->export_expression(&efb);
exp->write_string(efb.body());
delete expr; delete expr;
} }
...@@ -7545,7 +7549,11 @@ Array_type::do_export(Export* exp) const ...@@ -7545,7 +7549,11 @@ Array_type::do_export(Export* exp) const
{ {
exp->write_c_string("["); exp->write_c_string("[");
if (this->length_ != NULL) if (this->length_ != NULL)
this->length_->export_expression(exp); {
Export_function_body efb(exp, 0);
this->length_->export_expression(&efb);
exp->write_string(efb.body());
}
exp->write_c_string("] "); exp->write_c_string("] ");
exp->write_type(this->element_type_); exp->write_type(this->element_type_);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment