Commit 59357059 by Ian Lance Taylor

compiler: pass a Location to import_expression

    
    Separate the Location that import_expression uses when creating a new
    Expression from the Location used to report an error.  This is a step
    toward importing expressions for inlined functions.  This is a pure
    refactoring that does not affect compiler behavior.
    
    Reviewed-on: https://go-review.googlesource.com/c/150064

From-SVN: r266525
parent 98f33efc
db5240278b3b62a919dd88f857e718a66be50346 75d48ff977a2865d12b03857362ea48016a4b885
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.
...@@ -1583,7 +1583,7 @@ class Boolean_expression : public Expression ...@@ -1583,7 +1583,7 @@ class Boolean_expression : public Expression
{ } { }
static Expression* static Expression*
do_import(Import*); do_import(Import*, Location);
protected: protected:
bool bool
...@@ -1649,17 +1649,17 @@ Boolean_expression::do_determine_type(const Type_context* context) ...@@ -1649,17 +1649,17 @@ Boolean_expression::do_determine_type(const Type_context* context)
// Import a boolean constant. // Import a boolean constant.
Expression* Expression*
Boolean_expression::do_import(Import* imp) Boolean_expression::do_import(Import* imp, Location loc)
{ {
if (imp->peek_char() == 't') if (imp->peek_char() == 't')
{ {
imp->require_c_string("true"); imp->require_c_string("true");
return Expression::make_boolean(true, imp->location()); return Expression::make_boolean(true, loc);
} }
else else
{ {
imp->require_c_string("false"); imp->require_c_string("false");
return Expression::make_boolean(false, imp->location()); return Expression::make_boolean(false, loc);
} }
} }
...@@ -1768,7 +1768,7 @@ String_expression::do_export(Export_function_body* efb) const ...@@ -1768,7 +1768,7 @@ String_expression::do_export(Export_function_body* efb) const
// Import a string expression. // Import a string expression.
Expression* Expression*
String_expression::do_import(Import* imp) String_expression::do_import(Import* imp, Location loc)
{ {
imp->require_c_string("\""); imp->require_c_string("\"");
std::string val; std::string val;
...@@ -1800,11 +1800,11 @@ String_expression::do_import(Import* imp) ...@@ -1800,11 +1800,11 @@ String_expression::do_import(Import* imp)
else else
{ {
go_error_at(imp->location(), "bad string constant"); go_error_at(imp->location(), "bad string constant");
return Expression::make_error(imp->location()); return Expression::make_error(loc);
} }
} }
} }
return Expression::make_string(val, imp->location()); return Expression::make_string(val, loc);
} }
// Ast dump for string expression. // Ast dump for string expression.
...@@ -1944,7 +1944,7 @@ class Integer_expression : public Expression ...@@ -1944,7 +1944,7 @@ class Integer_expression : public Expression
{ mpz_init_set(this->val_, *val); } { mpz_init_set(this->val_, *val); }
static Expression* static Expression*
do_import(Import*); do_import(Import*, Location);
// Write VAL to string dump. // Write VAL to string dump.
static void static void
...@@ -2151,7 +2151,7 @@ Integer_expression::do_export(Export_function_body* efb) const ...@@ -2151,7 +2151,7 @@ Integer_expression::do_export(Export_function_body* efb) const
// all these types because they all start with digits. // all these types because they all start with digits.
Expression* Expression*
Integer_expression::do_import(Import* imp) Integer_expression::do_import(Import* imp, Location loc)
{ {
std::string num = imp->read_identifier(); std::string num = imp->read_identifier();
imp->require_c_string(" "); imp->require_c_string(" ");
...@@ -2169,7 +2169,7 @@ Integer_expression::do_import(Import* imp) ...@@ -2169,7 +2169,7 @@ Integer_expression::do_import(Import* imp)
{ {
go_error_at(imp->location(), "bad number in import data: %qs", go_error_at(imp->location(), "bad number in import data: %qs",
num.c_str()); num.c_str());
return Expression::make_error(imp->location()); return Expression::make_error(loc);
} }
if (pos == std::string::npos) if (pos == std::string::npos)
mpfr_set_ui(real, 0, GMP_RNDN); mpfr_set_ui(real, 0, GMP_RNDN);
...@@ -2180,7 +2180,7 @@ Integer_expression::do_import(Import* imp) ...@@ -2180,7 +2180,7 @@ Integer_expression::do_import(Import* imp)
{ {
go_error_at(imp->location(), "bad number in import data: %qs", go_error_at(imp->location(), "bad number in import data: %qs",
real_str.c_str()); real_str.c_str());
return Expression::make_error(imp->location()); return Expression::make_error(loc);
} }
} }
...@@ -2195,14 +2195,14 @@ Integer_expression::do_import(Import* imp) ...@@ -2195,14 +2195,14 @@ Integer_expression::do_import(Import* imp)
{ {
go_error_at(imp->location(), "bad number in import data: %qs", go_error_at(imp->location(), "bad number in import data: %qs",
imag_str.c_str()); imag_str.c_str());
return Expression::make_error(imp->location()); return Expression::make_error(loc);
} }
mpc_t cval; mpc_t cval;
mpc_init2(cval, mpc_precision); mpc_init2(cval, mpc_precision);
mpc_set_fr_fr(cval, real, imag, MPC_RNDNN); mpc_set_fr_fr(cval, real, imag, MPC_RNDNN);
mpfr_clear(real); mpfr_clear(real);
mpfr_clear(imag); mpfr_clear(imag);
Expression* ret = Expression::make_complex(&cval, NULL, imp->location()); Expression* ret = Expression::make_complex(&cval, NULL, loc);
mpc_clear(cval); mpc_clear(cval);
return ret; return ret;
} }
...@@ -2218,13 +2218,13 @@ Integer_expression::do_import(Import* imp) ...@@ -2218,13 +2218,13 @@ Integer_expression::do_import(Import* imp)
{ {
go_error_at(imp->location(), "bad number in import data: %qs", go_error_at(imp->location(), "bad number in import data: %qs",
num.c_str()); num.c_str());
return Expression::make_error(imp->location()); return Expression::make_error(loc);
} }
Expression* ret; Expression* ret;
if (is_character_constant) if (is_character_constant)
ret = Expression::make_character(&val, NULL, imp->location()); ret = Expression::make_character(&val, NULL, loc);
else else
ret = Expression::make_integer_z(&val, NULL, imp->location()); ret = Expression::make_integer_z(&val, NULL, loc);
mpz_clear(val); mpz_clear(val);
return ret; return ret;
} }
...@@ -2235,9 +2235,9 @@ Integer_expression::do_import(Import* imp) ...@@ -2235,9 +2235,9 @@ Integer_expression::do_import(Import* imp)
{ {
go_error_at(imp->location(), "bad number in import data: %qs", go_error_at(imp->location(), "bad number in import data: %qs",
num.c_str()); num.c_str());
return Expression::make_error(imp->location()); return Expression::make_error(loc);
} }
Expression* ret = Expression::make_float(&val, NULL, imp->location()); Expression* ret = Expression::make_float(&val, NULL, loc);
mpfr_clear(val); mpfr_clear(val);
return ret; return ret;
} }
...@@ -3133,7 +3133,7 @@ class Nil_expression : public Expression ...@@ -3133,7 +3133,7 @@ class Nil_expression : public Expression
{ } { }
static Expression* static Expression*
do_import(Import*); do_import(Import*, Location);
protected: protected:
bool bool
...@@ -3172,10 +3172,10 @@ class Nil_expression : public Expression ...@@ -3172,10 +3172,10 @@ class Nil_expression : public Expression
// Import a nil expression. // Import a nil expression.
Expression* Expression*
Nil_expression::do_import(Import* imp) Nil_expression::do_import(Import* imp, Location loc)
{ {
imp->require_c_string("nil"); imp->require_c_string("nil");
return Expression::make_nil(imp->location()); return Expression::make_nil(loc);
} }
// Make a nil expression. // Make a nil expression.
...@@ -3623,14 +3623,14 @@ Type_conversion_expression::do_export(Export_function_body* efb) const ...@@ -3623,14 +3623,14 @@ Type_conversion_expression::do_export(Export_function_body* efb) const
// Import a type conversion or a struct construction. // Import a type conversion or a struct construction.
Expression* Expression*
Type_conversion_expression::do_import(Import* imp) Type_conversion_expression::do_import(Import* 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); Expression* val = Expression::import_expression(imp, loc);
imp->require_c_string(")"); imp->require_c_string(")");
return Expression::make_cast(type, val, imp->location()); return Expression::make_cast(type, val, loc);
} }
// Dump ast representation for a type conversion expression. // Dump ast representation for a type conversion expression.
...@@ -4634,7 +4634,7 @@ Unary_expression::do_export(Export_function_body* efb) const ...@@ -4634,7 +4634,7 @@ Unary_expression::do_export(Export_function_body* efb) const
// Import a unary expression. // Import a unary expression.
Expression* Expression*
Unary_expression::do_import(Import* imp) Unary_expression::do_import(Import* imp, Location loc)
{ {
Operator op; Operator op;
switch (imp->get_char()) switch (imp->get_char())
...@@ -4655,8 +4655,8 @@ Unary_expression::do_import(Import* imp) ...@@ -4655,8 +4655,8 @@ Unary_expression::do_import(Import* imp)
go_unreachable(); go_unreachable();
} }
imp->require_c_string(" "); imp->require_c_string(" ");
Expression* expr = Expression::import_expression(imp); Expression* expr = Expression::import_expression(imp, loc);
return Expression::make_unary(op, expr, imp->location()); return Expression::make_unary(op, expr, loc);
} }
// Dump ast representation of an unary expression. // Dump ast representation of an unary expression.
...@@ -6403,11 +6403,11 @@ Binary_expression::do_export(Export_function_body* efb) const ...@@ -6403,11 +6403,11 @@ Binary_expression::do_export(Export_function_body* efb) const
// Import a binary expression. // Import a binary expression.
Expression* Expression*
Binary_expression::do_import(Import* imp) Binary_expression::do_import(Import* imp, Location loc)
{ {
imp->require_c_string("("); imp->require_c_string("(");
Expression* left = Expression::import_expression(imp); Expression* left = Expression::import_expression(imp, loc);
Operator op; Operator op;
if (imp->match_c_string(" || ")) if (imp->match_c_string(" || "))
...@@ -6508,14 +6508,14 @@ Binary_expression::do_import(Import* imp) ...@@ -6508,14 +6508,14 @@ Binary_expression::do_import(Import* imp)
else else
{ {
go_error_at(imp->location(), "unrecognized binary operator"); go_error_at(imp->location(), "unrecognized binary operator");
return Expression::make_error(imp->location()); return Expression::make_error(loc);
} }
Expression* right = Expression::import_expression(imp); Expression* right = Expression::import_expression(imp, loc);
imp->require_c_string(")"); imp->require_c_string(")");
return Expression::make_binary(op, left, right, imp->location()); return Expression::make_binary(op, left, right, loc);
} }
// Dump ast representation of a binary expression. // Dump ast representation of a binary expression.
...@@ -16138,33 +16138,33 @@ Expression::make_backend(Bexpression* bexpr, Type* type, Location location) ...@@ -16138,33 +16138,33 @@ Expression::make_backend(Bexpression* bexpr, Type* type, Location location)
// various class definitions. // various class definitions.
Expression* Expression*
Expression::import_expression(Import* imp) Expression::import_expression(Import* imp, Location loc)
{ {
int c = imp->peek_char(); int c = imp->peek_char();
if (imp->match_c_string("- ") if (imp->match_c_string("- ")
|| imp->match_c_string("! ") || imp->match_c_string("! ")
|| imp->match_c_string("^ ")) || imp->match_c_string("^ "))
return Unary_expression::do_import(imp); return Unary_expression::do_import(imp, loc);
else if (c == '(') else if (c == '(')
return Binary_expression::do_import(imp); 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"))
return Boolean_expression::do_import(imp); return Boolean_expression::do_import(imp, loc);
else if (c == '"') else if (c == '"')
return String_expression::do_import(imp); return String_expression::do_import(imp, loc);
else if (c == '-' || (c >= '0' && c <= '9')) else if (c == '-' || (c >= '0' && c <= '9'))
{ {
// This handles integers, floats and complex constants. // This handles integers, floats and complex constants.
return Integer_expression::do_import(imp); return Integer_expression::do_import(imp, loc);
} }
else if (imp->match_c_string("nil")) else if (imp->match_c_string("nil"))
return Nil_expression::do_import(imp); return Nil_expression::do_import(imp, loc);
else if (imp->match_c_string("convert")) else if (imp->match_c_string("convert"))
return Type_conversion_expression::do_import(imp); return Type_conversion_expression::do_import(imp, loc);
else else
{ {
go_error_at(imp->location(), "import error: expected expression"); go_error_at(imp->location(), "import error: expected expression");
return Expression::make_error(imp->location()); return Expression::make_error(loc);
} }
} }
......
...@@ -1014,9 +1014,11 @@ class Expression ...@@ -1014,9 +1014,11 @@ class Expression
export_expression(Export_function_body* efb) const export_expression(Export_function_body* efb) const
{ this->do_export(efb); } { this->do_export(efb); }
// Import an expression. // Import an expression. The location should be used for the
// returned expression. Errors should be reported using the
// Import's location method.
static Expression* static Expression*
import_expression(Import*); import_expression(Import*, Location);
// Return an expression which checks that VAL, of arbitrary integer type, // Return an expression which checks that VAL, of arbitrary integer type,
// is non-negative and is not more than the maximum integer value. // is non-negative and is not more than the maximum integer value.
...@@ -1565,7 +1567,7 @@ class String_expression : public Expression ...@@ -1565,7 +1567,7 @@ class String_expression : public Expression
{ return this->val_; } { return this->val_; }
static Expression* static Expression*
do_import(Import*); do_import(Import*, Location);
protected: protected:
bool bool
...@@ -1644,7 +1646,7 @@ class Type_conversion_expression : public Expression ...@@ -1644,7 +1646,7 @@ class Type_conversion_expression : public Expression
// Import a type conversion expression. // Import a type conversion expression.
static Expression* static Expression*
do_import(Import*); do_import(Import*, Location);
protected: protected:
int int
...@@ -1815,7 +1817,7 @@ class Unary_expression : public Expression ...@@ -1815,7 +1817,7 @@ class Unary_expression : public Expression
Location, Numeric_constant* nc, bool *issued_error); Location, Numeric_constant* nc, bool *issued_error);
static Expression* static Expression*
do_import(Import*); do_import(Import*, Location);
// Declare that this deref does or does not require an explicit nil check. // Declare that this deref does or does not require an explicit nil check.
void void
...@@ -1964,7 +1966,7 @@ class Binary_expression : public Expression ...@@ -1964,7 +1966,7 @@ class Binary_expression : public Expression
bool* result); bool* result);
static Expression* static Expression*
do_import(Import*); do_import(Import*, Location);
// Report an error if OP can not be applied to TYPE. Return whether // Report an error if OP can not be applied to TYPE. Return whether
// it can. OTYPE is the type of the other operand. // it can. OTYPE is the type of the other operand.
......
...@@ -7631,7 +7631,7 @@ Named_constant::import_const(Import* imp, std::string* pname, Type** ptype, ...@@ -7631,7 +7631,7 @@ Named_constant::import_const(Import* imp, std::string* pname, Type** ptype,
imp->require_c_string(" "); imp->require_c_string(" ");
} }
imp->require_c_string("= "); imp->require_c_string("= ");
*pexpr = Expression::import_expression(imp); *pexpr = Expression::import_expression(imp, imp->location());
imp->require_semicolon_if_old_version(); imp->require_semicolon_if_old_version();
imp->require_c_string("\n"); imp->require_c_string("\n");
} }
......
...@@ -338,7 +338,9 @@ class Statement ...@@ -338,7 +338,9 @@ class Statement
export_statement(Export_function_body* efb) export_statement(Export_function_body* efb)
{ this->do_export_statement(efb); } { this->do_export_statement(efb); }
// Read a statement from export data. // Read a statement from export data. The location should be used
// for the returned statement. Errors should be reported using the
// Import_function_body's location method.
static Statement* static Statement*
import_statement(Import_function_body*, Location); import_statement(Import_function_body*, Location);
......
...@@ -6606,7 +6606,8 @@ Struct_type::do_import(Import* imp) ...@@ -6606,7 +6606,8 @@ Struct_type::do_import(Import* imp)
if (imp->peek_char() == ' ') if (imp->peek_char() == ' ')
{ {
imp->advance(1); imp->advance(1);
Expression* expr = Expression::import_expression(imp); Expression* expr = Expression::import_expression(imp,
imp->location());
String_expression* sexpr = expr->string_expression(); String_expression* sexpr = expr->string_expression();
go_assert(sexpr != NULL); go_assert(sexpr != NULL);
sf.set_tag(sexpr->val()); sf.set_tag(sexpr->val());
...@@ -7568,7 +7569,7 @@ Array_type::do_import(Import* imp) ...@@ -7568,7 +7569,7 @@ Array_type::do_import(Import* imp)
if (imp->peek_char() == ']') if (imp->peek_char() == ']')
length = NULL; length = NULL;
else else
length = Expression::import_expression(imp); length = Expression::import_expression(imp, imp->location());
imp->require_c_string("] "); imp->require_c_string("] ");
Type* element_type = imp->read_type(); Type* element_type = imp->read_type();
return Type::make_array_type(element_type, length); return Type::make_array_type(element_type, length);
......
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