summaryrefslogtreecommitdiff
path: root/gcc/go/gofrontend/expressions.cc
diff options
context:
space:
mode:
authorian <ian@138bc75d-0d04-0410-961f-82ee72b054a4>2016-12-15 22:47:43 +0000
committerian <ian@138bc75d-0d04-0410-961f-82ee72b054a4>2016-12-15 22:47:43 +0000
commitaf7a52747c01a7359a22ef67febeb5fb39d9ff84 (patch)
tree59845f6dec919b79bac4beebf830fee954ad3678 /gcc/go/gofrontend/expressions.cc
parent6fdca905a149eeec90f0e23373c3d941d02be59e (diff)
downloadgcc-af7a52747c01a7359a22ef67febeb5fb39d9ff84.tar.gz
PR go/78763
compiler: call determine_types even for constant expressions We need to call determine_types even for constant expressions, since a constant expression may include code like unsafe.Sizeof(0). Something needs to determine the type of the untyped 0, and that should be the determine_types pass. Implementing that triggered a compiler crash on test/const1.go because it permitted some erroneous constants to make it all the way to the backend. Catch that case by checking whether we get a constant overflow error, and marking the expression invalid if we do. This is a good change in any case, as previously we reported the same constant overflow error multiple times, and now we only report it once. Fixes GCC PR 78763. Reviewed-on: https://go-review.googlesource.com/34496 git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@243729 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/go/gofrontend/expressions.cc')
-rw-r--r--gcc/go/gofrontend/expressions.cc59
1 files changed, 44 insertions, 15 deletions
diff --git a/gcc/go/gofrontend/expressions.cc b/gcc/go/gofrontend/expressions.cc
index a9337bda6b1..2022c4749b3 100644
--- a/gcc/go/gofrontend/expressions.cc
+++ b/gcc/go/gofrontend/expressions.cc
@@ -3738,8 +3738,12 @@ Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
if (expr->numeric_constant_value(&nc))
{
Numeric_constant result;
- if (Unary_expression::eval_constant(op, &nc, loc, &result))
+ bool issued_error;
+ if (Unary_expression::eval_constant(op, &nc, loc, &result,
+ &issued_error))
return result.expression(loc);
+ else if (issued_error)
+ return Expression::make_error(this->location());
}
}
@@ -3900,12 +3904,15 @@ Unary_expression::do_is_static_initializer() const
}
// Apply unary opcode OP to UNC, setting NC. Return true if this
-// could be done, false if not. Issue errors for overflow.
+// could be done, false if not. On overflow, issues an error and sets
+// *ISSUED_ERROR.
bool
Unary_expression::eval_constant(Operator op, const Numeric_constant* unc,
- Location location, Numeric_constant* nc)
+ Location location, Numeric_constant* nc,
+ bool* issued_error)
{
+ *issued_error = false;
switch (op)
{
case OPERATOR_PLUS:
@@ -4050,7 +4057,12 @@ Unary_expression::eval_constant(Operator op, const Numeric_constant* unc,
mpz_clear(uval);
mpz_clear(val);
- return nc->set_type(unc->type(), true, location);
+ if (!nc->set_type(unc->type(), true, location))
+ {
+ *issued_error = true;
+ return false;
+ }
+ return true;
}
// Return the integral constant value of a unary expression, if it has one.
@@ -4061,8 +4073,9 @@ Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const
Numeric_constant unc;
if (!this->expr_->numeric_constant_value(&unc))
return false;
+ bool issued_error;
return Unary_expression::eval_constant(this->op_, &unc, this->location(),
- nc);
+ nc, &issued_error);
}
// Return the type of a unary expression.
@@ -4737,13 +4750,15 @@ Binary_expression::compare_complex(const Numeric_constant* left_nc,
// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
// true if this could be done, false if not. Issue errors at LOCATION
-// as appropriate.
+// as appropriate, and sets *ISSUED_ERROR if it did.
bool
Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc,
Numeric_constant* right_nc,
- Location location, Numeric_constant* nc)
+ Location location, Numeric_constant* nc,
+ bool* issued_error)
{
+ *issued_error = false;
switch (op)
{
case OPERATOR_OROR:
@@ -4792,7 +4807,11 @@ Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc,
r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc);
if (r)
- r = nc->set_type(type, true, location);
+ {
+ r = nc->set_type(type, true, location);
+ if (!r)
+ *issued_error = true;
+ }
return r;
}
@@ -5115,9 +5134,15 @@ Binary_expression::do_lower(Gogo* gogo, Named_object*,
else
{
Numeric_constant nc;
+ bool issued_error;
if (!Binary_expression::eval_constant(op, &left_nc, &right_nc,
- location, &nc))
+ location, &nc,
+ &issued_error))
+ {
+ if (issued_error)
+ return Expression::make_error(location);
return this;
+ }
return nc.expression(location);
}
}
@@ -5458,8 +5483,9 @@ Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const
Numeric_constant right_nc;
if (!this->right_->numeric_constant_value(&right_nc))
return false;
+ bool issued_error;
return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc,
- this->location(), nc);
+ this->location(), nc, &issued_error);
}
// Note that the value is being discarded.
@@ -5558,7 +5584,12 @@ Binary_expression::do_determine_type(const Type_context* context)
Type_context subcontext(*context);
- if (is_comparison)
+ if (is_constant_expr)
+ {
+ subcontext.type = NULL;
+ subcontext.may_be_abstract = true;
+ }
+ else if (is_comparison)
{
// In a comparison, the context does not determine the types of
// the operands.
@@ -5600,8 +5631,7 @@ Binary_expression::do_determine_type(const Type_context* context)
subcontext.type = subcontext.type->make_non_abstract_type();
}
- if (!is_constant_expr)
- this->left_->determine_type(&subcontext);
+ this->left_->determine_type(&subcontext);
if (is_shift_op)
{
@@ -5621,8 +5651,7 @@ Binary_expression::do_determine_type(const Type_context* context)
subcontext.may_be_abstract = false;
}
- if (!is_constant_expr)
- this->right_->determine_type(&subcontext);
+ this->right_->determine_type(&subcontext);
if (is_comparison)
{