Commit 80d7d30d by Ian Lance Taylor

compiler: add '$' to names in expression export data

    
    For inlined function bodies we're going to need to refer to variables,
    so change the existing export data to add a '$' to names that look
    like identifiers: true, false, nil, convert.
    
    While we're here drop an unnecessary space character after operators.
    
    Reviewed-on: https://go-review.googlesource.com/c/150067

From-SVN: r266529
parent 01021139
6e0974fc6c9aa6ef19f72fbb5698e4b3734a4220 ce81aad0e3d53215e2c0f1f060c7fd6219e6fb23
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.
...@@ -1610,7 +1610,7 @@ class Boolean_expression : public Expression ...@@ -1610,7 +1610,7 @@ class Boolean_expression : public Expression
void void
do_export(Export_function_body* efb) const do_export(Export_function_body* efb) const
{ efb->write_c_string(this->val_ ? "true" : "false"); } { efb->write_c_string(this->val_ ? "$true" : "$false"); }
void void
do_dump_expression(Ast_dump_context* ast_dump_context) const do_dump_expression(Ast_dump_context* ast_dump_context) const
...@@ -1651,6 +1651,8 @@ Boolean_expression::do_determine_type(const Type_context* context) ...@@ -1651,6 +1651,8 @@ Boolean_expression::do_determine_type(const Type_context* context)
Expression* Expression*
Boolean_expression::do_import(Import_expression* imp, Location loc) Boolean_expression::do_import(Import_expression* imp, Location loc)
{ {
if (imp->version() >= EXPORT_FORMAT_V3)
imp->require_c_string("$");
if (imp->peek_char() == 't') if (imp->peek_char() == 't')
{ {
imp->require_c_string("true"); imp->require_c_string("true");
...@@ -3162,7 +3164,7 @@ class Nil_expression : public Expression ...@@ -3162,7 +3164,7 @@ class Nil_expression : public Expression
void void
do_export(Export_function_body* efb) const do_export(Export_function_body* efb) const
{ efb->write_c_string("nil"); } { efb->write_c_string("$nil"); }
void void
do_dump_expression(Ast_dump_context* ast_dump_context) const do_dump_expression(Ast_dump_context* ast_dump_context) const
...@@ -3174,6 +3176,8 @@ class Nil_expression : public Expression ...@@ -3174,6 +3176,8 @@ class Nil_expression : public Expression
Expression* Expression*
Nil_expression::do_import(Import_expression* imp, Location loc) Nil_expression::do_import(Import_expression* imp, Location loc)
{ {
if (imp->version() >= EXPORT_FORMAT_V3)
imp->require_c_string("$");
imp->require_c_string("nil"); imp->require_c_string("nil");
return Expression::make_nil(loc); return Expression::make_nil(loc);
} }
...@@ -3613,7 +3617,7 @@ Type_conversion_expression::do_get_backend(Translate_context* context) ...@@ -3613,7 +3617,7 @@ Type_conversion_expression::do_get_backend(Translate_context* context)
void void
Type_conversion_expression::do_export(Export_function_body* efb) const Type_conversion_expression::do_export(Export_function_body* efb) const
{ {
efb->write_c_string("convert("); efb->write_c_string("$convert(");
efb->write_type(this->type_); efb->write_type(this->type_);
efb->write_c_string(", "); efb->write_c_string(", ");
this->expr_->export_expression(efb); this->expr_->export_expression(efb);
...@@ -3625,7 +3629,7 @@ Type_conversion_expression::do_export(Export_function_body* efb) const ...@@ -3625,7 +3629,7 @@ Type_conversion_expression::do_export(Export_function_body* efb) const
Expression* Expression*
Type_conversion_expression::do_import(Import_expression* imp, Location loc) Type_conversion_expression::do_import(Import_expression* imp, Location loc)
{ {
imp->require_c_string("convert("); imp->require_c_string("$convert(");
Type* type = imp->read_type(); Type* type = imp->read_type();
imp->require_c_string(", "); imp->require_c_string(", ");
Expression* val = Expression::import_expression(imp, loc); Expression* val = Expression::import_expression(imp, loc);
...@@ -4612,16 +4616,16 @@ Unary_expression::do_export(Export_function_body* efb) const ...@@ -4612,16 +4616,16 @@ Unary_expression::do_export(Export_function_body* efb) const
switch (this->op_) switch (this->op_)
{ {
case OPERATOR_PLUS: case OPERATOR_PLUS:
efb->write_c_string("+ "); efb->write_c_string("+");
break; break;
case OPERATOR_MINUS: case OPERATOR_MINUS:
efb->write_c_string("- "); efb->write_c_string("-");
break; break;
case OPERATOR_NOT: case OPERATOR_NOT:
efb->write_c_string("! "); efb->write_c_string("!");
break; break;
case OPERATOR_XOR: case OPERATOR_XOR:
efb->write_c_string("^ "); efb->write_c_string("^");
break; break;
case OPERATOR_AND: case OPERATOR_AND:
case OPERATOR_MULT: case OPERATOR_MULT:
...@@ -4654,7 +4658,8 @@ Unary_expression::do_import(Import_expression* imp, Location loc) ...@@ -4654,7 +4658,8 @@ Unary_expression::do_import(Import_expression* imp, Location loc)
default: default:
go_unreachable(); go_unreachable();
} }
imp->require_c_string(" "); if (imp->version() < EXPORT_FORMAT_V3)
imp->require_c_string(" ");
Expression* expr = Expression::import_expression(imp, loc); Expression* expr = Expression::import_expression(imp, loc);
return Expression::make_unary(op, expr, loc); return Expression::make_unary(op, expr, loc);
} }
...@@ -12959,7 +12964,7 @@ Struct_construction_expression::do_get_backend(Translate_context* context) ...@@ -12959,7 +12964,7 @@ Struct_construction_expression::do_get_backend(Translate_context* context)
void void
Struct_construction_expression::do_export(Export_function_body* efb) const Struct_construction_expression::do_export(Export_function_body* efb) const
{ {
efb->write_c_string("convert("); efb->write_c_string("$convert(");
efb->write_type(this->type_); efb->write_type(this->type_);
for (Expression_list::const_iterator pv = this->vals()->begin(); for (Expression_list::const_iterator pv = this->vals()->begin();
pv != this->vals()->end(); pv != this->vals()->end();
...@@ -13192,7 +13197,7 @@ Array_construction_expression::get_constructor(Translate_context* context, ...@@ -13192,7 +13197,7 @@ Array_construction_expression::get_constructor(Translate_context* context,
void void
Array_construction_expression::do_export(Export_function_body* efb) const Array_construction_expression::do_export(Export_function_body* efb) const
{ {
efb->write_c_string("convert("); efb->write_c_string("$convert(");
efb->write_type(this->type_); efb->write_type(this->type_);
if (this->vals() != NULL) if (this->vals() != NULL)
{ {
...@@ -13709,7 +13714,7 @@ Map_construction_expression::do_get_backend(Translate_context* context) ...@@ -13709,7 +13714,7 @@ Map_construction_expression::do_get_backend(Translate_context* context)
void void
Map_construction_expression::do_export(Export_function_body* efb) const Map_construction_expression::do_export(Export_function_body* efb) const
{ {
efb->write_c_string("convert("); efb->write_c_string("$convert(");
efb->write_type(this->type_); efb->write_type(this->type_);
for (Expression_list::const_iterator pv = this->vals_->begin(); for (Expression_list::const_iterator pv = this->vals_->begin();
pv != this->vals_->end(); pv != this->vals_->end();
...@@ -16141,14 +16146,15 @@ Expression* ...@@ -16141,14 +16146,15 @@ Expression*
Expression::import_expression(Import_expression* imp, Location loc) Expression::import_expression(Import_expression* imp, Location loc)
{ {
int c = imp->peek_char(); int c = imp->peek_char();
if (imp->match_c_string("- ") if (c == '+' || c == '-' || c == '!' || c == '^')
|| imp->match_c_string("! ")
|| imp->match_c_string("^ "))
return Unary_expression::do_import(imp, loc); return Unary_expression::do_import(imp, loc);
else if (c == '(') else if (c == '(')
return Binary_expression::do_import(imp, loc); return Binary_expression::do_import(imp, loc);
else if (imp->match_c_string("true") else if (imp->match_c_string("$true")
|| imp->match_c_string("false")) || imp->match_c_string("$false")
|| (imp->version() < EXPORT_FORMAT_V3
&& (imp->match_c_string("true")
|| imp->match_c_string("false"))))
return Boolean_expression::do_import(imp, loc); return Boolean_expression::do_import(imp, loc);
else if (c == '"') else if (c == '"')
return String_expression::do_import(imp, loc); return String_expression::do_import(imp, loc);
...@@ -16157,9 +16163,13 @@ Expression::import_expression(Import_expression* imp, Location loc) ...@@ -16157,9 +16163,13 @@ Expression::import_expression(Import_expression* imp, Location loc)
// This handles integers, floats and complex constants. // This handles integers, floats and complex constants.
return Integer_expression::do_import(imp, loc); return Integer_expression::do_import(imp, loc);
} }
else if (imp->match_c_string("nil")) else if (imp->match_c_string("$nil")
|| (imp->version() < EXPORT_FORMAT_V3
&& imp->match_c_string("nil")))
return Nil_expression::do_import(imp, loc); return Nil_expression::do_import(imp, loc);
else if (imp->match_c_string("convert")) else if (imp->match_c_string("$convert")
|| (imp->version() < EXPORT_FORMAT_V3
&& imp->match_c_string("convert")))
return Type_conversion_expression::do_import(imp, loc); return Type_conversion_expression::do_import(imp, loc);
else else
{ {
......
...@@ -71,6 +71,10 @@ class Import_expression ...@@ -71,6 +71,10 @@ class Import_expression
// Read a type. // Read a type.
virtual Type* virtual Type*
read_type() = 0; read_type() = 0;
// Return the version number of the export data we're reading.
virtual Export_data_version
version() const = 0;
}; };
// This class manages importing Go declarations. // This class manages importing Go declarations.
...@@ -253,6 +257,10 @@ class Import : public Import_expression ...@@ -253,6 +257,10 @@ class Import : public Import_expression
advance(size_t skip) advance(size_t skip)
{ this->stream_->advance(skip); } { this->stream_->advance(skip); }
// Return the version number of the export data we're reading.
Export_data_version
version() const { return this->version_; }
// Skip a semicolon if using an older version. // Skip a semicolon if using an older version.
void void
require_semicolon_if_old_version() require_semicolon_if_old_version()
...@@ -381,10 +389,6 @@ class Import : public Import_expression ...@@ -381,10 +389,6 @@ class Import : public Import_expression
return true; return true;
} }
// Return the version number of the export data we're reading.
Export_data_version
version() const { return this->version_; }
// The general IR. // The general IR.
Gogo* gogo_; Gogo* gogo_;
// The stream from which to read import data. // The stream from which to read import data.
...@@ -638,6 +642,10 @@ class Import_function_body : public Import_expression ...@@ -638,6 +642,10 @@ class Import_function_body : public Import_expression
Type* Type*
read_type(); read_type();
Export_data_version
version() const
{ return this->imp_->version(); }
// Implement Import_expression. // Implement Import_expression.
Import_function_body* Import_function_body*
ifb() ifb()
......
...@@ -282,6 +282,15 @@ func (p *parser) parseConversion(pkg *types.Package) (val constant.Value, typ ty ...@@ -282,6 +282,15 @@ func (p *parser) parseConversion(pkg *types.Package) (val constant.Value, typ ty
// ConstValue = string | "false" | "true" | ["-"] (int ["'"] | FloatOrComplex) | Conversion . // ConstValue = string | "false" | "true" | ["-"] (int ["'"] | FloatOrComplex) | Conversion .
// FloatOrComplex = float ["i" | ("+"|"-") float "i"] . // FloatOrComplex = float ["i" | ("+"|"-") float "i"] .
func (p *parser) parseConstValue(pkg *types.Package) (val constant.Value, typ types.Type) { func (p *parser) parseConstValue(pkg *types.Package) (val constant.Value, typ types.Type) {
// v3 changed to $false, $true, $convert, to avoid confusion
// with variable names in inline function bodies.
if p.tok == '$' {
p.next()
if p.tok != scanner.Ident {
p.errorf("expected identifer after '$', got %s (%q)", scanner.TokenString(p.tok), p.lit)
}
}
switch p.tok { switch p.tok {
case scanner.String: case scanner.String:
str := p.parseString() str := p.parseString()
......
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