From: ian Date: Wed, 28 Mar 2012 21:27:28 +0000 (+0000) Subject: compiler: Rewrite handling of untyped numeric constants. X-Git-Tag: upstream/4.9.2~13538 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0c77715ba591f1ea7596395122e5240130bdd178;p=platform%2Fupstream%2Flinaro-gcc.git compiler: Rewrite handling of untyped numeric constants. Fixes various bugs when, e.g., using float or complex constants in integer contexts. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@185925 138bc75d-0d04-0410-961f-82ee72b054a4 --- diff --git a/gcc/go/gofrontend/expressions.cc b/gcc/go/gofrontend/expressions.cc index 90cf6f3..4c493f8 100644 --- a/gcc/go/gofrontend/expressions.cc +++ b/gcc/go/gofrontend/expressions.cc @@ -50,57 +50,6 @@ Expression::~Expression() { } -// If this expression has a constant integer value, return it. - -bool -Expression::integer_constant_value(bool iota_is_constant, mpz_t val, - Type** ptype) const -{ - *ptype = NULL; - return this->do_integer_constant_value(iota_is_constant, val, ptype); -} - -// If this expression has a constant floating point value, return it. - -bool -Expression::float_constant_value(mpfr_t val, Type** ptype) const -{ - *ptype = NULL; - if (this->do_float_constant_value(val, ptype)) - return true; - mpz_t ival; - mpz_init(ival); - Type* t; - bool ret; - if (!this->do_integer_constant_value(false, ival, &t)) - ret = false; - else - { - mpfr_set_z(val, ival, GMP_RNDN); - ret = true; - } - mpz_clear(ival); - return ret; -} - -// If this expression has a constant complex value, return it. - -bool -Expression::complex_constant_value(mpfr_t real, mpfr_t imag, - Type** ptype) const -{ - *ptype = NULL; - if (this->do_complex_constant_value(real, imag, ptype)) - return true; - Type *t; - if (this->float_constant_value(real, &t)) - { - mpfr_set_ui(imag, 0, GMP_RNDN); - return true; - } - return false; -} - // Traverse the expressions. int @@ -824,24 +773,9 @@ class Error_expression : public Expression { return true; } bool - do_integer_constant_value(bool, mpz_t val, Type**) const - { - mpz_set_ui(val, 0); - return true; - } - - bool - do_float_constant_value(mpfr_t val, Type**) const - { - mpfr_set_ui(val, 0, GMP_RNDN); - return true; - } - - bool - do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const + do_numeric_constant_value(Numeric_constant* nc) const { - mpfr_set_ui(real, 0, GMP_RNDN); - mpfr_set_ui(imag, 0, GMP_RNDN); + nc->set_unsigned_long(NULL, 0); return true; } @@ -1759,10 +1693,6 @@ class Integer_expression : public Expression static Expression* do_import(Import*); - // Return whether VAL fits in the type. - static bool - check_constant(mpz_t val, Type*, Location); - // Write VAL to string dump. static void export_integer(String_dump* exp, const mpz_t val); @@ -1777,7 +1707,7 @@ class Integer_expression : public Expression { return true; } bool - do_integer_constant_value(bool, mpz_t val, Type** ptype) const; + do_numeric_constant_value(Numeric_constant* nc) const; Type* do_type(); @@ -1817,15 +1747,16 @@ class Integer_expression : public Expression bool is_character_constant_; }; -// Return an integer constant value. +// Return a numeric constant for this expression. We have to mark +// this as a character when appropriate. bool -Integer_expression::do_integer_constant_value(bool, mpz_t val, - Type** ptype) const +Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const { - if (this->type_ != NULL) - *ptype = this->type_; - mpz_set(val, this->val_); + if (this->is_character_constant_) + nc->set_rune(this->type_, this->val_); + else + nc->set_int(this->type_, this->val_); return true; } @@ -1853,10 +1784,7 @@ Integer_expression::do_determine_type(const Type_context* context) { if (this->type_ != NULL && !this->type_->is_abstract()) ; - else if (context->type != NULL - && (context->type->integer_type() != NULL - || context->type->float_type() != NULL - || context->type->complex_type() != NULL)) + else if (context->type != NULL && context->type->is_numeric_type()) this->type_ = context->type; else if (!context->may_be_abstract) { @@ -1867,55 +1795,20 @@ Integer_expression::do_determine_type(const Type_context* context) } } -// Return true if the integer VAL fits in the range of the type TYPE. -// Otherwise give an error and return false. TYPE may be NULL. - -bool -Integer_expression::check_constant(mpz_t val, Type* type, - Location location) -{ - if (type == NULL) - return true; - Integer_type* itype = type->integer_type(); - if (itype == NULL || itype->is_abstract()) - return true; - - int bits = mpz_sizeinbase(val, 2); - - if (itype->is_unsigned()) - { - // For an unsigned type we can only accept a nonnegative number, - // and we must be able to represent at least BITS. - if (mpz_sgn(val) >= 0 - && bits <= itype->bits()) - return true; - } - else - { - // For a signed type we need an extra bit to indicate the sign. - // We have to handle the most negative integer specially. - if (bits + 1 <= itype->bits() - || (bits <= itype->bits() - && mpz_sgn(val) < 0 - && (mpz_scan1(val, 0) - == static_cast(itype->bits() - 1)) - && mpz_scan0(val, itype->bits()) == ULONG_MAX)) - return true; - } - - error_at(location, "integer constant overflow"); - return false; -} - // Check the type of an integer constant. void Integer_expression::do_check_types(Gogo*) { - if (this->type_ == NULL) + Type* type = this->type_; + if (type == NULL) return; - if (!Integer_expression::check_constant(this->val_, this->type_, - this->location())) + Numeric_constant nc; + if (this->is_character_constant_) + nc.set_rune(NULL, this->val_); + else + nc.set_int(NULL, this->val_); + if (!nc.set_type(type, true, this->location())) this->set_is_error(); } @@ -2117,14 +2010,6 @@ class Float_expression : public Expression mpfr_init_set(this->val_, *val, GMP_RNDN); } - // Constrain VAL to fit into TYPE. - static void - constrain_float(mpfr_t val, Type* type); - - // Return whether VAL fits in the type. - static bool - check_constant(mpfr_t val, Type*, Location); - // Write VAL to export data. static void export_float(String_dump* exp, const mpfr_t val); @@ -2139,7 +2024,11 @@ class Float_expression : public Expression { return true; } bool - do_float_constant_value(mpfr_t val, Type**) const; + do_numeric_constant_value(Numeric_constant* nc) const + { + nc->set_float(this->type_, this->val_); + return true; + } Type* do_type(); @@ -2171,27 +2060,6 @@ class Float_expression : public Expression Type* type_; }; -// Constrain VAL to fit into TYPE. - -void -Float_expression::constrain_float(mpfr_t val, Type* type) -{ - Float_type* ftype = type->float_type(); - if (ftype != NULL && !ftype->is_abstract()) - mpfr_prec_round(val, ftype->bits(), GMP_RNDN); -} - -// Return a floating point constant value. - -bool -Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const -{ - if (this->type_ != NULL) - *ptype = this->type_; - mpfr_set(val, this->val_, GMP_RNDN); - return true; -} - // Return the current type. If we haven't set the type yet, we return // an abstract float type. @@ -2220,73 +2088,18 @@ Float_expression::do_determine_type(const Type_context* context) this->type_ = Type::lookup_float_type("float64"); } -// Return true if the floating point value VAL fits in the range of -// the type TYPE. Otherwise give an error and return false. TYPE may -// be NULL. - -bool -Float_expression::check_constant(mpfr_t val, Type* type, - Location location) -{ - if (type == NULL) - return true; - Float_type* ftype = type->float_type(); - if (ftype == NULL || ftype->is_abstract()) - return true; - - // A NaN or Infinity always fits in the range of the type. - if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) - return true; - - mp_exp_t exp = mpfr_get_exp(val); - mp_exp_t max_exp; - switch (ftype->bits()) - { - case 32: - max_exp = 128; - break; - case 64: - max_exp = 1024; - break; - default: - go_unreachable(); - } - if (exp > max_exp) - { - error_at(location, "floating point constant overflow"); - return false; - } - return true; -} - // Check the type of a float value. void Float_expression::do_check_types(Gogo*) { - if (this->type_ == NULL) + Type* type = this->type_; + if (type == NULL) return; - - if (!Float_expression::check_constant(this->val_, this->type_, - this->location())) + Numeric_constant nc; + nc.set_float(NULL, this->val_); + if (!nc.set_type(this->type_, true, this->location())) this->set_is_error(); - - Integer_type* integer_type = this->type_->integer_type(); - if (integer_type != NULL) - { - if (!mpfr_integer_p(this->val_)) - this->report_error(_("floating point constant truncated to integer")); - else - { - go_assert(!integer_type->is_abstract()); - mpz_t ival; - mpz_init(ival); - mpfr_get_z(ival, this->val_, GMP_RNDN); - Integer_expression::check_constant(ival, integer_type, - this->location()); - mpz_clear(ival); - } - } } // Get a tree for a float constant. @@ -2371,14 +2184,6 @@ class Complex_expression : public Expression mpfr_init_set(this->imag_, *imag, GMP_RNDN); } - // Constrain REAL/IMAG to fit into TYPE. - static void - constrain_complex(mpfr_t real, mpfr_t imag, Type* type); - - // Return whether REAL/IMAG fits in the type. - static bool - check_constant(mpfr_t real, mpfr_t imag, Type*, Location); - // Write REAL/IMAG to string dump. static void export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val); @@ -2394,7 +2199,11 @@ class Complex_expression : public Expression { return true; } bool - do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const; + do_numeric_constant_value(Numeric_constant* nc) const + { + nc->set_complex(this->type_, this->real_, this->imag_); + return true; + } Type* do_type(); @@ -2430,32 +2239,6 @@ class Complex_expression : public Expression Type* type_; }; -// Constrain REAL/IMAG to fit into TYPE. - -void -Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type) -{ - Complex_type* ctype = type->complex_type(); - if (ctype != NULL && !ctype->is_abstract()) - { - mpfr_prec_round(real, ctype->bits() / 2, GMP_RNDN); - mpfr_prec_round(imag, ctype->bits() / 2, GMP_RNDN); - } -} - -// Return a complex constant value. - -bool -Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, - Type** ptype) const -{ - if (this->type_ != NULL) - *ptype = this->type_; - mpfr_set(real, this->real_, GMP_RNDN); - mpfr_set(imag, this->imag_, GMP_RNDN); - return true; -} - // Return the current type. If we haven't set the type yet, we return // an abstract complex type. @@ -2482,65 +2265,17 @@ Complex_expression::do_determine_type(const Type_context* context) this->type_ = Type::lookup_complex_type("complex128"); } -// Return true if the complex value REAL/IMAG fits in the range of the -// type TYPE. Otherwise give an error and return false. TYPE may be -// NULL. - -bool -Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type, - Location location) -{ - if (type == NULL) - return true; - Complex_type* ctype = type->complex_type(); - if (ctype == NULL || ctype->is_abstract()) - return true; - - mp_exp_t max_exp; - switch (ctype->bits()) - { - case 64: - max_exp = 128; - break; - case 128: - max_exp = 1024; - break; - default: - go_unreachable(); - } - - // A NaN or Infinity always fits in the range of the type. - if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real)) - { - if (mpfr_get_exp(real) > max_exp) - { - error_at(location, "complex real part constant overflow"); - return false; - } - } - - if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag)) - { - if (mpfr_get_exp(imag) > max_exp) - { - error_at(location, "complex imaginary part constant overflow"); - return false; - } - } - - return true; -} - // Check the type of a complex value. void Complex_expression::do_check_types(Gogo*) { - if (this->type_ == NULL) + Type* type = this->type_; + if (type == NULL) return; - - if (!Complex_expression::check_constant(this->real_, this->imag_, - this->type_, this->location())) + Numeric_constant nc; + nc.set_complex(NULL, this->real_, this->imag_); + if (!nc.set_type(this->type_, true, this->location())) this->set_is_error(); } @@ -2665,13 +2400,7 @@ class Const_expression : public Expression { return true; } bool - do_integer_constant_value(bool, mpz_t val, Type**) const; - - bool - do_float_constant_value(mpfr_t val, Type**) const; - - bool - do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const; + do_numeric_constant_value(Numeric_constant* nc) const; bool do_string_constant_value(std::string* val) const @@ -2755,109 +2484,33 @@ Const_expression::do_lower(Gogo* gogo, Named_object*, return this; } -// Return an integer constant value. +// Return a numeric constant value. bool -Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, - Type** ptype) const +Const_expression::do_numeric_constant_value(Numeric_constant* nc) const { if (this->seen_) return false; - Type* ctype; - if (this->type_ != NULL) - ctype = this->type_; - else - ctype = this->constant_->const_value()->type(); - if (ctype != NULL && ctype->integer_type() == NULL) - return false; - Expression* e = this->constant_->const_value()->expr(); - + this->seen_ = true; - Type* t; - bool r = e->integer_constant_value(iota_is_constant, val, &t); + bool r = e->numeric_constant_value(nc); this->seen_ = false; - if (r - && ctype != NULL - && !Integer_expression::check_constant(val, ctype, this->location())) - return false; - - *ptype = ctype != NULL ? ctype : t; - return r; -} - -// Return a floating point constant value. - -bool -Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const -{ - if (this->seen_) - return false; - Type* ctype; if (this->type_ != NULL) ctype = this->type_; else ctype = this->constant_->const_value()->type(); - if (ctype != NULL && ctype->float_type() == NULL) - return false; - - this->seen_ = true; - - Type* t; - bool r = this->constant_->const_value()->expr()->float_constant_value(val, - &t); - - this->seen_ = false; - if (r && ctype != NULL) { - if (!Float_expression::check_constant(val, ctype, this->location())) + if (!nc->set_type(ctype, false, this->location())) return false; - Float_expression::constrain_float(val, ctype); } - *ptype = ctype != NULL ? ctype : t; - return r; -} - -// Return a complex constant value. -bool -Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, - Type **ptype) const -{ - if (this->seen_) - return false; - - Type* ctype; - if (this->type_ != NULL) - ctype = this->type_; - else - ctype = this->constant_->const_value()->type(); - if (ctype != NULL && ctype->complex_type() == NULL) - return false; - - this->seen_ = true; - - Type *t; - bool r = this->constant_->const_value()->expr()->complex_constant_value(real, - imag, - &t); - - this->seen_ = false; - - if (r && ctype != NULL) - { - if (!Complex_expression::check_constant(real, imag, ctype, - this->location())) - return false; - Complex_expression::constrain_complex(real, imag, ctype); - } - *ptype = ctype != NULL ? ctype : t; return r; } @@ -2909,12 +2562,8 @@ Const_expression::do_determine_type(const Type_context* context) if (ctype != NULL && !ctype->is_abstract()) ; else if (context->type != NULL - && (context->type->integer_type() != NULL - || context->type->float_type() != NULL - || context->type->complex_type() != NULL) - && (cetype->integer_type() != NULL - || cetype->float_type() != NULL - || cetype->complex_type() != NULL)) + && context->type->is_numeric_type() + && cetype->is_numeric_type()) this->type_ = context->type; else if (context->type != NULL && context->type->is_string_type() @@ -2976,35 +2625,15 @@ Const_expression::do_check_types(Gogo*) this->check_for_init_loop(); - if (this->type_ == NULL || this->type_->is_abstract()) - return; - - // Check for integer overflow. - if (this->type_->integer_type() != NULL) + // Check that numeric constant fits in type. + if (this->type_ != NULL && this->type_->is_numeric_type()) { - mpz_t ival; - mpz_init(ival); - Type* dummy; - if (!this->integer_constant_value(true, ival, &dummy)) + Numeric_constant nc; + if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) { - mpfr_t fval; - mpfr_init(fval); - Expression* cexpr = this->constant_->const_value()->expr(); - if (cexpr->float_constant_value(fval, &dummy)) - { - if (!mpfr_integer_p(fval)) - this->report_error(_("floating point constant " - "truncated to integer")); - else - { - mpfr_get_z(ival, fval, GMP_RNDN); - Integer_expression::check_constant(ival, this->type_, - this->location()); - } - } - mpfr_clear(fval); + if (!nc.set_type(this->type_, true, this->location())) + this->set_is_error(); } - mpz_clear(ival); } } @@ -3028,41 +2657,18 @@ Const_expression::do_get_tree(Translate_context* context) // object is an abstract int or float, we try to get the abstract // value. Otherwise we may lose something in the conversion. if (this->type_ != NULL + && this->type_->is_numeric_type() && (this->constant_->const_value()->type() == NULL || this->constant_->const_value()->type()->is_abstract())) { Expression* expr = this->constant_->const_value()->expr(); - mpz_t ival; - mpz_init(ival); - Type* t; - if (expr->integer_constant_value(true, ival, &t)) - { - tree ret = Expression::integer_constant_tree(ival, type_tree); - mpz_clear(ival); - return ret; - } - mpz_clear(ival); - - mpfr_t fval; - mpfr_init(fval); - if (expr->float_constant_value(fval, &t)) - { - tree ret = Expression::float_constant_tree(fval, type_tree); - mpfr_clear(fval); - return ret; - } - - mpfr_t imag; - mpfr_init(imag); - if (expr->complex_constant_value(fval, imag, &t)) + Numeric_constant nc; + if (expr->numeric_constant_value(&nc) + && nc.set_type(this->type_, false, this->location())) { - tree ret = Expression::complex_constant_tree(fval, imag, type_tree); - mpfr_clear(fval); - mpfr_clear(imag); - return ret; + Expression* e = nc.expression(this->location()); + return e->get_tree(context); } - mpfr_clear(imag); - mpfr_clear(fval); } tree const_tree = this->constant_->get_tree(gogo, context->function()); @@ -3279,13 +2885,7 @@ class Type_conversion_expression : public Expression { return this->expr_->is_constant(); } bool - do_integer_constant_value(bool, mpz_t, Type**) const; - - bool - do_float_constant_value(mpfr_t, Type**) const; - - bool - do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; + do_numeric_constant_value(Numeric_constant*) const; bool do_string_constant_value(std::string*) const; @@ -3351,82 +2951,15 @@ Type_conversion_expression::do_lower(Gogo*, Named_object*, Expression* val = this->expr_; Location location = this->location(); - if (type->integer_type() != NULL) - { - mpz_t ival; - mpz_init(ival); - Type* dummy; - if (val->integer_constant_value(false, ival, &dummy)) - { - if (!Integer_expression::check_constant(ival, type, location)) - mpz_set_ui(ival, 0); - Expression* ret = Expression::make_integer(&ival, type, location); - mpz_clear(ival); - return ret; - } - - mpfr_t fval; - mpfr_init(fval); - if (val->float_constant_value(fval, &dummy)) - { - if (!mpfr_integer_p(fval)) - { - error_at(location, - "floating point constant truncated to integer"); - return Expression::make_error(location); - } - mpfr_get_z(ival, fval, GMP_RNDN); - if (!Integer_expression::check_constant(ival, type, location)) - mpz_set_ui(ival, 0); - Expression* ret = Expression::make_integer(&ival, type, location); - mpfr_clear(fval); - mpz_clear(ival); - return ret; - } - mpfr_clear(fval); - mpz_clear(ival); - } - - if (type->float_type() != NULL) - { - mpfr_t fval; - mpfr_init(fval); - Type* dummy; - if (val->float_constant_value(fval, &dummy)) - { - if (!Float_expression::check_constant(fval, type, location)) - mpfr_set_ui(fval, 0, GMP_RNDN); - Float_expression::constrain_float(fval, type); - Expression *ret = Expression::make_float(&fval, type, location); - mpfr_clear(fval); - return ret; - } - mpfr_clear(fval); - } - - if (type->complex_type() != NULL) + if (type->is_numeric_type()) { - mpfr_t real; - mpfr_t imag; - mpfr_init(real); - mpfr_init(imag); - Type* dummy; - if (val->complex_constant_value(real, imag, &dummy)) + Numeric_constant nc; + if (val->numeric_constant_value(&nc)) { - if (!Complex_expression::check_constant(real, imag, type, location)) - { - mpfr_set_ui(real, 0, GMP_RNDN); - mpfr_set_ui(imag, 0, GMP_RNDN); - } - Complex_expression::constrain_complex(real, imag, type); - Expression* ret = Expression::make_complex(&real, &imag, type, - location); - mpfr_clear(real); - mpfr_clear(imag); - return ret; + if (!nc.set_type(type, true, location)) + return Expression::make_error(location); + return nc.expression(location); } - mpfr_clear(real); - mpfr_clear(imag); } if (type->is_slice_type()) @@ -3491,118 +3024,17 @@ Type_conversion_expression::do_lower(Gogo*, Named_object*, return this; } -// Return the constant integer value if there is one. - -bool -Type_conversion_expression::do_integer_constant_value(bool iota_is_constant, - mpz_t val, - Type** ptype) const -{ - if (this->type_->integer_type() == NULL) - return false; - - mpz_t ival; - mpz_init(ival); - Type* dummy; - if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy)) - { - if (!Integer_expression::check_constant(ival, this->type_, - this->location())) - { - mpz_clear(ival); - return false; - } - mpz_set(val, ival); - mpz_clear(ival); - *ptype = this->type_; - return true; - } - mpz_clear(ival); - - mpfr_t fval; - mpfr_init(fval); - if (this->expr_->float_constant_value(fval, &dummy)) - { - mpfr_get_z(val, fval, GMP_RNDN); - mpfr_clear(fval); - if (!Integer_expression::check_constant(val, this->type_, - this->location())) - return false; - *ptype = this->type_; - return true; - } - mpfr_clear(fval); - - return false; -} - -// Return the constant floating point value if there is one. +// Return the constant numeric value if there is one. bool -Type_conversion_expression::do_float_constant_value(mpfr_t val, - Type** ptype) const +Type_conversion_expression::do_numeric_constant_value( + Numeric_constant* nc) const { - if (this->type_->float_type() == NULL) + if (!this->type_->is_numeric_type()) return false; - - mpfr_t fval; - mpfr_init(fval); - Type* dummy; - if (this->expr_->float_constant_value(fval, &dummy)) - { - if (!Float_expression::check_constant(fval, this->type_, - this->location())) - { - mpfr_clear(fval); - return false; - } - mpfr_set(val, fval, GMP_RNDN); - mpfr_clear(fval); - Float_expression::constrain_float(val, this->type_); - *ptype = this->type_; - return true; - } - mpfr_clear(fval); - - return false; -} - -// Return the constant complex value if there is one. - -bool -Type_conversion_expression::do_complex_constant_value(mpfr_t real, - mpfr_t imag, - Type **ptype) const -{ - if (this->type_->complex_type() == NULL) + if (!this->expr_->numeric_constant_value(nc)) return false; - - mpfr_t rval; - mpfr_t ival; - mpfr_init(rval); - mpfr_init(ival); - Type* dummy; - if (this->expr_->complex_constant_value(rval, ival, &dummy)) - { - if (!Complex_expression::check_constant(rval, ival, this->type_, - this->location())) - { - mpfr_clear(rval); - mpfr_clear(ival); - return false; - } - mpfr_set(real, rval, GMP_RNDN); - mpfr_set(imag, ival, GMP_RNDN); - mpfr_clear(rval); - mpfr_clear(ival); - Complex_expression::constrain_complex(real, imag, this->type_); - *ptype = this->type_; - return true; - } - mpfr_clear(rval); - mpfr_clear(ival); - - return false; + return nc->set_type(this->type_, false, this->location()); } // Return the constant string value if there is one. @@ -3613,20 +3045,17 @@ Type_conversion_expression::do_string_constant_value(std::string* val) const if (this->type_->is_string_type() && this->expr_->type()->integer_type() != NULL) { - mpz_t ival; - mpz_init(ival); - Type* dummy; - if (this->expr_->integer_constant_value(false, ival, &dummy)) + Numeric_constant nc; + if (this->expr_->numeric_constant_value(&nc)) { - unsigned long ulval = mpz_get_ui(ival); - if (mpz_cmp_ui(ival, ulval) == 0) + unsigned long ival; + if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) { - Lex::append_char(ulval, true, val, this->location()); - mpz_clear(ival); + val->clear(); + Lex::append_char(ival, true, val, this->location()); return true; } } - mpz_clear(ival); } // FIXME: Could handle conversion from const []int here. @@ -4041,22 +3470,11 @@ class Unary_expression : public Expression this->create_temp_ = true; } - // Apply unary opcode OP to UVAL, setting VAL. Return true if this - // could be done, false if not. + // Apply unary opcode OP to UNC, setting NC. Return true if this + // could be done, false if not. Issue errors for overflow. static bool - eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val, - Location); - - // Apply unary opcode OP to UVAL, setting VAL. Return true if this - // could be done, false if not. - static bool - eval_float(Operator op, mpfr_t uval, mpfr_t val); - - // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return - // true if this could be done, false if not. - static bool - eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real, - mpfr_t imag); + eval_constant(Operator op, const Numeric_constant* unc, + Location, Numeric_constant* nc); static Expression* do_import(Import*); @@ -4073,13 +3491,7 @@ class Unary_expression : public Expression do_is_constant() const; bool - do_integer_constant_value(bool, mpz_t, Type**) const; - - bool - do_float_constant_value(mpfr_t, Type**) const; - - bool - do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; + do_numeric_constant_value(Numeric_constant*) const; Type* do_type(); @@ -4181,59 +3593,12 @@ Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_NOT || op == OPERATOR_XOR) { - Expression* ret = NULL; - - mpz_t eval; - mpz_init(eval); - Type* etype; - if (expr->integer_constant_value(false, eval, &etype)) - { - mpz_t val; - mpz_init(val); - if (Unary_expression::eval_integer(op, etype, eval, val, loc)) - ret = Expression::make_integer(&val, etype, loc); - mpz_clear(val); - } - mpz_clear(eval); - if (ret != NULL) - return ret; - - if (op == OPERATOR_PLUS || op == OPERATOR_MINUS) + Numeric_constant nc; + if (expr->numeric_constant_value(&nc)) { - mpfr_t fval; - mpfr_init(fval); - Type* ftype; - if (expr->float_constant_value(fval, &ftype)) - { - mpfr_t val; - mpfr_init(val); - if (Unary_expression::eval_float(op, fval, val)) - ret = Expression::make_float(&val, ftype, loc); - mpfr_clear(val); - } - if (ret != NULL) - { - mpfr_clear(fval); - return ret; - } - - mpfr_t ival; - mpfr_init(ival); - if (expr->complex_constant_value(fval, ival, &ftype)) - { - mpfr_t real; - mpfr_t imag; - mpfr_init(real); - mpfr_init(imag); - if (Unary_expression::eval_complex(op, fval, ival, real, imag)) - ret = Expression::make_complex(&real, &imag, ftype, loc); - mpfr_clear(real); - mpfr_clear(imag); - } - mpfr_clear(ival); - mpfr_clear(fval); - if (ret != NULL) - return ret; + Numeric_constant result; + if (Unary_expression::eval_constant(op, &nc, loc, &result)) + return result.expression(loc); } } @@ -4269,199 +3634,171 @@ Unary_expression::do_is_constant() const return this->expr_->is_constant(); } -// Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of -// UVAL, if known; it may be NULL. Return true if this could be done, -// false if not. +// Apply unary opcode OP to UNC, setting NC. Return true if this +// could be done, false if not. Issue errors for overflow. bool -Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val, - Location location) +Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, + Location location, Numeric_constant* nc) { switch (op) { case OPERATOR_PLUS: - mpz_set(val, uval); + *nc = *unc; return true; + case OPERATOR_MINUS: - mpz_neg(val, uval); - return Integer_expression::check_constant(val, utype, location); - case OPERATOR_NOT: - mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); - return true; - case OPERATOR_XOR: - if (utype == NULL - || utype->integer_type() == NULL - || utype->integer_type()->is_abstract()) - mpz_com(val, uval); + if (unc->is_int() || unc->is_rune()) + break; + else if (unc->is_float()) + { + mpfr_t uval; + unc->get_float(&uval); + mpfr_t val; + mpfr_init(val); + mpfr_neg(val, uval, GMP_RNDN); + nc->set_float(unc->type(), val); + mpfr_clear(uval); + mpfr_clear(val); + return true; + } + else if (unc->is_complex()) + { + mpfr_t ureal, uimag; + unc->get_complex(&ureal, &uimag); + mpfr_t real, imag; + mpfr_init(real); + mpfr_init(imag); + mpfr_neg(real, ureal, GMP_RNDN); + mpfr_neg(imag, uimag, GMP_RNDN); + nc->set_complex(unc->type(), real, imag); + mpfr_clear(ureal); + mpfr_clear(uimag); + mpfr_clear(real); + mpfr_clear(imag); + return true; + } else - { - // The number of HOST_WIDE_INTs that it takes to represent - // UVAL. - size_t count = ((mpz_sizeinbase(uval, 2) - + HOST_BITS_PER_WIDE_INT - - 1) - / HOST_BITS_PER_WIDE_INT); - - unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; - memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); - - size_t obits = utype->integer_type()->bits(); - - if (!utype->integer_type()->is_unsigned() - && mpz_sgn(uval) < 0) - { - mpz_t adj; - mpz_init_set_ui(adj, 1); - mpz_mul_2exp(adj, adj, obits); - mpz_add(uval, uval, adj); - mpz_clear(adj); - } - - size_t ecount; - mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); - go_assert(ecount <= count); - - // Trim down to the number of words required by the type. - size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) - / HOST_BITS_PER_WIDE_INT); - go_assert(ocount <= count); - - for (size_t i = 0; i < ocount; ++i) - phwi[i] = ~phwi[i]; - - size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; - if (clearbits != 0) - phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) - >> clearbits); - - mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); + go_unreachable(); - if (!utype->integer_type()->is_unsigned() - && mpz_tstbit(val, obits - 1)) - { - mpz_t adj; - mpz_init_set_ui(adj, 1); - mpz_mul_2exp(adj, adj, obits); - mpz_sub(val, val, adj); - mpz_clear(adj); - } + case OPERATOR_NOT: + case OPERATOR_XOR: + break; - delete[] phwi; - } - return Integer_expression::check_constant(val, utype, location); case OPERATOR_AND: case OPERATOR_MULT: return false; + default: go_unreachable(); } -} -// Apply unary opcode OP to UVAL, setting VAL. Return true if this -// could be done, false if not. + if (!unc->is_int() && !unc->is_rune()) + return false; + + mpz_t uval; + unc->get_int(&uval); + mpz_t val; + mpz_init(val); -bool -Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val) -{ switch (op) { - case OPERATOR_PLUS: - mpfr_set(val, uval, GMP_RNDN); - return true; case OPERATOR_MINUS: - mpfr_neg(val, uval, GMP_RNDN); - return true; + mpz_neg(val, uval); + break; + case OPERATOR_NOT: + mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); + break; + case OPERATOR_XOR: - case OPERATOR_AND: - case OPERATOR_MULT: - return false; - default: - go_unreachable(); - } -} + { + Type* utype = unc->type(); + if (utype->integer_type() == NULL + || utype->integer_type()->is_abstract()) + mpz_com(val, uval); + else + { + // The number of HOST_WIDE_INTs that it takes to represent + // UVAL. + size_t count = ((mpz_sizeinbase(uval, 2) + + HOST_BITS_PER_WIDE_INT + - 1) + / HOST_BITS_PER_WIDE_INT); -// Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true -// if this could be done, false if not. + unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; + memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); + + size_t obits = utype->integer_type()->bits(); + + if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) + { + mpz_t adj; + mpz_init_set_ui(adj, 1); + mpz_mul_2exp(adj, adj, obits); + mpz_add(uval, uval, adj); + mpz_clear(adj); + } + + size_t ecount; + mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); + go_assert(ecount <= count); + + // Trim down to the number of words required by the type. + size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) + / HOST_BITS_PER_WIDE_INT); + go_assert(ocount <= count); + + for (size_t i = 0; i < ocount; ++i) + phwi[i] = ~phwi[i]; + + size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; + if (clearbits != 0) + phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) + >> clearbits); + + mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); + + if (!utype->integer_type()->is_unsigned() + && mpz_tstbit(val, obits - 1)) + { + mpz_t adj; + mpz_init_set_ui(adj, 1); + mpz_mul_2exp(adj, adj, obits); + mpz_sub(val, val, adj); + mpz_clear(adj); + } + + delete[] phwi; + } + } + break; -bool -Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival, - mpfr_t real, mpfr_t imag) -{ - switch (op) - { - case OPERATOR_PLUS: - mpfr_set(real, rval, GMP_RNDN); - mpfr_set(imag, ival, GMP_RNDN); - return true; - case OPERATOR_MINUS: - mpfr_neg(real, rval, GMP_RNDN); - mpfr_neg(imag, ival, GMP_RNDN); - return true; - case OPERATOR_NOT: - case OPERATOR_XOR: - case OPERATOR_AND: - case OPERATOR_MULT: - return false; default: go_unreachable(); } -} - -// Return the integral constant value of a unary expression, if it has one. -bool -Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, - Type** ptype) const -{ - mpz_t uval; - mpz_init(uval); - bool ret; - if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype)) - ret = false; + if (unc->is_rune()) + nc->set_rune(NULL, val); else - ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val, - this->location()); - mpz_clear(uval); - return ret; -} + nc->set_int(NULL, val); -// Return the floating point constant value of a unary expression, if -// it has one. + mpz_clear(uval); + mpz_clear(val); -bool -Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const -{ - mpfr_t uval; - mpfr_init(uval); - bool ret; - if (!this->expr_->float_constant_value(uval, ptype)) - ret = false; - else - ret = Unary_expression::eval_float(this->op_, uval, val); - mpfr_clear(uval); - return ret; + return nc->set_type(unc->type(), true, location); } -// Return the complex constant value of a unary expression, if it has -// one. +// Return the integral constant value of a unary expression, if it has one. bool -Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, - Type** ptype) const +Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const { - mpfr_t rval; - mpfr_t ival; - mpfr_init(rval); - mpfr_init(ival); - bool ret; - if (!this->expr_->complex_constant_value(rval, ival, ptype)) - ret = false; - else - ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag); - mpfr_clear(rval); - mpfr_clear(ival); - return ret; + Numeric_constant unc; + if (!this->expr_->numeric_constant_value(&unc)) + return false; + return Unary_expression::eval_constant(this->op_, &unc, this->location(), + nc); } // Return the type of a unary expression. @@ -4854,124 +4191,239 @@ Binary_expression::do_traverse(Traverse* traverse) return Expression::traverse(&this->right_, traverse); } -// Compare integer constants according to OP. +// Return the type to use for a binary operation on operands of +// LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as +// such may be NULL or abstract. + +bool +Binary_expression::operation_type(Operator op, Type* left_type, + Type* right_type, Type** result_type) +{ + if (left_type != right_type + && !left_type->is_abstract() + && !right_type->is_abstract() + && left_type->base() != right_type->base() + && op != OPERATOR_LSHIFT + && op != OPERATOR_RSHIFT) + { + // May be a type error--let it be diagnosed elsewhere. + return false; + } + + if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) + { + if (left_type->integer_type() != NULL) + *result_type = left_type; + else + *result_type = Type::make_abstract_integer_type(); + } + else if (!left_type->is_abstract() && left_type->named_type() != NULL) + *result_type = left_type; + else if (!right_type->is_abstract() && right_type->named_type() != NULL) + *result_type = right_type; + else if (!left_type->is_abstract()) + *result_type = left_type; + else if (!right_type->is_abstract()) + *result_type = right_type; + else if (left_type->complex_type() != NULL) + *result_type = left_type; + else if (right_type->complex_type() != NULL) + *result_type = right_type; + else if (left_type->float_type() != NULL) + *result_type = left_type; + else if (right_type->float_type() != NULL) + *result_type = right_type; + else if (left_type->integer_type() != NULL + && left_type->integer_type()->is_rune()) + *result_type = left_type; + else if (right_type->integer_type() != NULL + && right_type->integer_type()->is_rune()) + *result_type = right_type; + else + *result_type = left_type; + + return true; +} + +// Convert an integer comparison code and an operator to a boolean +// value. bool -Binary_expression::compare_integer(Operator op, mpz_t left_val, - mpz_t right_val) +Binary_expression::cmp_to_bool(Operator op, int cmp) { - int i = mpz_cmp(left_val, right_val); switch (op) { case OPERATOR_EQEQ: - return i == 0; + return cmp == 0; + break; case OPERATOR_NOTEQ: - return i != 0; + return cmp != 0; + break; case OPERATOR_LT: - return i < 0; + return cmp < 0; + break; case OPERATOR_LE: - return i <= 0; + return cmp <= 0; case OPERATOR_GT: - return i > 0; + return cmp > 0; case OPERATOR_GE: - return i >= 0; + return cmp >= 0; default: go_unreachable(); } } -// Compare floating point constants according to OP. +// Compare constants according to OP. bool -Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val, - mpfr_t right_val) +Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, + Numeric_constant* right_nc, + Location location, bool* result) { - int i; - if (type == NULL) - i = mpfr_cmp(left_val, right_val); + Type* left_type = left_nc->type(); + Type* right_type = right_nc->type(); + + Type* type; + if (!Binary_expression::operation_type(op, left_type, right_type, &type)) + return false; + + // When comparing an untyped operand to a typed operand, we are + // effectively coercing the untyped operand to the other operand's + // type, so make sure that is valid. + if (!left_nc->set_type(type, true, location) + || !right_nc->set_type(type, true, location)) + return false; + + bool ret; + int cmp; + if (type->complex_type() != NULL) + { + if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) + return false; + ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); + } + else if (type->float_type() != NULL) + ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); else + ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); + + if (ret) + *result = Binary_expression::cmp_to_bool(op, cmp); + + return ret; +} + +// Compare integer constants. + +bool +Binary_expression::compare_integer(const Numeric_constant* left_nc, + const Numeric_constant* right_nc, + int* cmp) +{ + mpz_t left_val; + if (!left_nc->to_int(&left_val)) + return false; + mpz_t right_val; + if (!right_nc->to_int(&right_val)) { - mpfr_t lv; - mpfr_init_set(lv, left_val, GMP_RNDN); - mpfr_t rv; - mpfr_init_set(rv, right_val, GMP_RNDN); - Float_expression::constrain_float(lv, type); - Float_expression::constrain_float(rv, type); - i = mpfr_cmp(lv, rv); - mpfr_clear(lv); - mpfr_clear(rv); + mpz_clear(left_val); + return false; } - switch (op) + + *cmp = mpz_cmp(left_val, right_val); + + mpz_clear(left_val); + mpz_clear(right_val); + + return true; +} + +// Compare floating point constants. + +bool +Binary_expression::compare_float(const Numeric_constant* left_nc, + const Numeric_constant* right_nc, + int* cmp) +{ + mpfr_t left_val; + if (!left_nc->to_float(&left_val)) + return false; + mpfr_t right_val; + if (!right_nc->to_float(&right_val)) { - case OPERATOR_EQEQ: - return i == 0; - case OPERATOR_NOTEQ: - return i != 0; - case OPERATOR_LT: - return i < 0; - case OPERATOR_LE: - return i <= 0; - case OPERATOR_GT: - return i > 0; - case OPERATOR_GE: - return i >= 0; - default: - go_unreachable(); + mpfr_clear(left_val); + return false; + } + + // We already coerced both operands to the same type. If that type + // is not an abstract type, we need to round the values accordingly. + Type* type = left_nc->type(); + if (!type->is_abstract() && type->float_type() != NULL) + { + int bits = type->float_type()->bits(); + mpfr_prec_round(left_val, bits, GMP_RNDN); + mpfr_prec_round(right_val, bits, GMP_RNDN); } + + *cmp = mpfr_cmp(left_val, right_val); + + mpfr_clear(left_val); + mpfr_clear(right_val); + + return true; } -// Compare complex constants according to OP. Complex numbers may -// only be compared for equality. +// Compare complex constants. Complex numbers may only be compared +// for equality. bool -Binary_expression::compare_complex(Operator op, Type* type, - mpfr_t left_real, mpfr_t left_imag, - mpfr_t right_real, mpfr_t right_imag) +Binary_expression::compare_complex(const Numeric_constant* left_nc, + const Numeric_constant* right_nc, + int* cmp) { - bool is_equal; - if (type == NULL) - is_equal = (mpfr_cmp(left_real, right_real) == 0 - && mpfr_cmp(left_imag, right_imag) == 0); - else + mpfr_t left_real, left_imag; + if (!left_nc->to_complex(&left_real, &left_imag)) + return false; + mpfr_t right_real, right_imag; + if (!right_nc->to_complex(&right_real, &right_imag)) { - mpfr_t lr; - mpfr_t li; - mpfr_init_set(lr, left_real, GMP_RNDN); - mpfr_init_set(li, left_imag, GMP_RNDN); - mpfr_t rr; - mpfr_t ri; - mpfr_init_set(rr, right_real, GMP_RNDN); - mpfr_init_set(ri, right_imag, GMP_RNDN); - Complex_expression::constrain_complex(lr, li, type); - Complex_expression::constrain_complex(rr, ri, type); - is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0; - mpfr_clear(lr); - mpfr_clear(li); - mpfr_clear(rr); - mpfr_clear(ri); + mpfr_clear(left_real); + mpfr_clear(left_imag); + return false; } - switch (op) + + // We already coerced both operands to the same type. If that type + // is not an abstract type, we need to round the values accordingly. + Type* type = left_nc->type(); + if (!type->is_abstract() && type->complex_type() != NULL) { - case OPERATOR_EQEQ: - return is_equal; - case OPERATOR_NOTEQ: - return !is_equal; - default: - go_unreachable(); + int bits = type->complex_type()->bits(); + mpfr_prec_round(left_real, bits / 2, GMP_RNDN); + mpfr_prec_round(left_imag, bits / 2, GMP_RNDN); + mpfr_prec_round(right_real, bits / 2, GMP_RNDN); + mpfr_prec_round(right_imag, bits / 2, GMP_RNDN); } + + *cmp = (mpfr_cmp(left_real, right_real) != 0 + || mpfr_cmp(left_imag, right_imag) != 0); + + mpfr_clear(left_real); + mpfr_clear(left_imag); + mpfr_clear(right_real); + mpfr_clear(right_imag); + + return true; } -// Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL. -// LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of -// RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if -// this could be done, false if not. +// 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. bool -Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val, - Type* right_type, mpz_t right_val, - Location location, mpz_t val) +Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, + Numeric_constant* right_nc, + Location location, Numeric_constant* nc) { - bool is_shift_op = false; switch (op) { case OPERATOR_OROR: @@ -4982,9 +4434,68 @@ Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val, case OPERATOR_LE: case OPERATOR_GT: case OPERATOR_GE: - // These return boolean values. We should probably handle them - // anyhow in case a type conversion is used on the result. + // These return boolean values and as such must be handled + // elsewhere. + go_unreachable(); + default: + break; + } + + Type* left_type = left_nc->type(); + Type* right_type = right_nc->type(); + + Type* type; + if (!Binary_expression::operation_type(op, left_type, right_type, &type)) + return false; + + bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; + + // When combining an untyped operand with a typed operand, we are + // effectively coercing the untyped operand to the other operand's + // type, so make sure that is valid. + if (!left_nc->set_type(type, true, location)) + return false; + if (!is_shift && !right_nc->set_type(type, true, location)) + return false; + + bool r; + if (type->complex_type() != NULL) + r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); + else if (type->float_type() != NULL) + r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); + else + r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); + + if (r) + r = nc->set_type(type, true, location); + + return r; +} + +// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using +// integer operations. Return true if this could be done, false if +// not. + +bool +Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, + const Numeric_constant* right_nc, + Location location, Numeric_constant* nc) +{ + mpz_t left_val; + if (!left_nc->to_int(&left_val)) + return false; + mpz_t right_val; + if (!right_nc->to_int(&right_val)) + { + mpz_clear(left_val); return false; + } + + mpz_t val; + mpz_init(val); + + switch (op) + { case OPERATOR_PLUS: mpz_add(val, left_val, right_val); break; @@ -5007,7 +4518,6 @@ Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val, { error_at(location, "division by zero"); mpz_set_ui(val, 0); - return true; } break; case OPERATOR_MOD: @@ -5017,20 +4527,18 @@ Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val, { error_at(location, "division by zero"); mpz_set_ui(val, 0); - return true; } break; case OPERATOR_LSHIFT: { unsigned long shift = mpz_get_ui(right_val); - if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000) + if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) + mpz_mul_2exp(val, left_val, shift); + else { error_at(location, "shift count overflow"); mpz_set_ui(val, 0); - return true; } - mpz_mul_2exp(val, left_val, shift); - is_shift_op = true; break; } break; @@ -5041,13 +4549,14 @@ Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val, { error_at(location, "shift count overflow"); mpz_set_ui(val, 0); - return true; } - if (mpz_cmp_ui(left_val, 0) >= 0) - mpz_tdiv_q_2exp(val, left_val, shift); else - mpz_fdiv_q_2exp(val, left_val, shift); - is_shift_op = true; + { + if (mpz_cmp_ui(left_val, 0) >= 0) + mpz_tdiv_q_2exp(val, left_val, shift); + else + mpz_fdiv_q_2exp(val, left_val, shift); + } break; } break; @@ -5067,63 +4576,47 @@ Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val, go_unreachable(); } - Type* type = left_type; - if (!is_shift_op) - { - if (type == NULL) - type = right_type; - else if (type != right_type && right_type != NULL) - { - if (type->is_abstract()) - type = right_type; - else if (!right_type->is_abstract()) - { - // This look like a type error which should be diagnosed - // elsewhere. Don't do anything here, to avoid an - // unhelpful chain of error messages. - return true; - } - } - } + mpz_clear(left_val); + mpz_clear(right_val); - if (type != NULL && !type->is_abstract()) - { - // We have to check the operands too, as we have implicitly - // coerced them to TYPE. - if ((type != left_type - && !Integer_expression::check_constant(left_val, type, location)) - || (!is_shift_op - && type != right_type - && !Integer_expression::check_constant(right_val, type, - location)) - || !Integer_expression::check_constant(val, type, location)) - mpz_set_ui(val, 0); - } + if (left_nc->is_rune() + || (op != OPERATOR_LSHIFT + && op != OPERATOR_RSHIFT + && right_nc->is_rune())) + nc->set_rune(NULL, val); + else + nc->set_int(NULL, val); + + mpz_clear(val); return true; } -// Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL. -// Return true if this could be done, false if not. +// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using +// floating point operations. Return true if this could be done, +// false if not. bool -Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val, - Type* right_type, mpfr_t right_val, - mpfr_t val, Location location) +Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, + const Numeric_constant* right_nc, + Location location, Numeric_constant* nc) { - switch (op) + mpfr_t left_val; + if (!left_nc->to_float(&left_val)) + return false; + mpfr_t right_val; + if (!right_nc->to_float(&right_val)) { - case OPERATOR_OROR: - case OPERATOR_ANDAND: - case OPERATOR_EQEQ: - case OPERATOR_NOTEQ: - case OPERATOR_LT: - case OPERATOR_LE: - case OPERATOR_GT: - case OPERATOR_GE: - // These return boolean values. We should probably handle them - // anyhow in case a type conversion is used on the result. + mpfr_clear(left_val); return false; + } + + mpfr_t val; + mpfr_init(val); + + bool ret = true; + switch (op) + { case OPERATOR_PLUS: mpfr_add(val, left_val, right_val, GMP_RNDN); break; @@ -5134,78 +4627,64 @@ Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val, case OPERATOR_XOR: case OPERATOR_AND: case OPERATOR_BITCLEAR: - return false; + case OPERATOR_MOD: + case OPERATOR_LSHIFT: + case OPERATOR_RSHIFT: + mpfr_set_ui(val, 0, GMP_RNDN); + ret = false; + break; case OPERATOR_MULT: mpfr_mul(val, left_val, right_val, GMP_RNDN); break; case OPERATOR_DIV: - if (mpfr_zero_p(right_val)) - error_at(location, "division by zero"); - mpfr_div(val, left_val, right_val, GMP_RNDN); + if (!mpfr_zero_p(right_val)) + mpfr_div(val, left_val, right_val, GMP_RNDN); + else + { + error_at(location, "division by zero"); + mpfr_set_ui(val, 0, GMP_RNDN); + } break; - case OPERATOR_MOD: - return false; - case OPERATOR_LSHIFT: - case OPERATOR_RSHIFT: - return false; default: go_unreachable(); } - Type* type = left_type; - if (type == NULL) - type = right_type; - else if (type != right_type && right_type != NULL) - { - if (type->is_abstract()) - type = right_type; - else if (!right_type->is_abstract()) - { - // This looks like a type error which should be diagnosed - // elsewhere. Don't do anything here, to avoid an unhelpful - // chain of error messages. - return true; - } - } + mpfr_clear(left_val); + mpfr_clear(right_val); - if (type != NULL && !type->is_abstract()) - { - if ((type != left_type - && !Float_expression::check_constant(left_val, type, location)) - || (type != right_type - && !Float_expression::check_constant(right_val, type, - location)) - || !Float_expression::check_constant(val, type, location)) - mpfr_set_ui(val, 0, GMP_RNDN); - } + nc->set_float(NULL, val); + mpfr_clear(val); - return true; + return ret; } -// Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and -// RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this -// could be done, false if not. +// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using +// complex operations. Return true if this could be done, false if +// not. bool -Binary_expression::eval_complex(Operator op, Type* left_type, - mpfr_t left_real, mpfr_t left_imag, - Type *right_type, - mpfr_t right_real, mpfr_t right_imag, - mpfr_t real, mpfr_t imag, - Location location) +Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, + const Numeric_constant* right_nc, + Location location, Numeric_constant* nc) { - switch (op) + mpfr_t left_real, left_imag; + if (!left_nc->to_complex(&left_real, &left_imag)) + return false; + mpfr_t right_real, right_imag; + if (!right_nc->to_complex(&right_real, &right_imag)) { - case OPERATOR_OROR: - case OPERATOR_ANDAND: - case OPERATOR_EQEQ: - case OPERATOR_NOTEQ: - case OPERATOR_LT: - case OPERATOR_LE: - case OPERATOR_GT: - case OPERATOR_GE: - // These return boolean values and must be handled differently. + mpfr_clear(left_real); + mpfr_clear(left_imag); return false; + } + + mpfr_t real, imag; + mpfr_init(real); + mpfr_init(imag); + + bool ret = true; + switch (op) + { case OPERATOR_PLUS: mpfr_add(real, left_real, right_real, GMP_RNDN); mpfr_add(imag, left_imag, right_imag, GMP_RNDN); @@ -5218,7 +4697,13 @@ Binary_expression::eval_complex(Operator op, Type* left_type, case OPERATOR_XOR: case OPERATOR_AND: case OPERATOR_BITCLEAR: - return false; + case OPERATOR_MOD: + case OPERATOR_LSHIFT: + case OPERATOR_RSHIFT: + mpfr_set_ui(real, 0, GMP_RNDN); + mpfr_set_ui(imag, 0, GMP_RNDN); + ret = false; + break; case OPERATOR_MULT: { // You might think that multiplying two complex numbers would @@ -5371,7 +4856,12 @@ Binary_expression::eval_complex(Operator op, Type* left_type, // scale the values to try to avoid this. if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag)) - error_at(location, "division by zero"); + { + error_at(location, "division by zero"); + mpfr_set_ui(real, 0, GMP_RNDN); + mpfr_set_ui(imag, 0, GMP_RNDN); + break; + } mpfr_t rra; mpfr_t ria; @@ -5502,48 +4992,20 @@ Binary_expression::eval_complex(Operator op, Type* left_type, mpfr_clear(ria); } break; - case OPERATOR_MOD: - return false; - case OPERATOR_LSHIFT: - case OPERATOR_RSHIFT: - return false; default: go_unreachable(); } - Type* type = left_type; - if (type == NULL) - type = right_type; - else if (type != right_type && right_type != NULL) - { - if (type->is_abstract()) - type = right_type; - else if (!right_type->is_abstract()) - { - // This looks like a type error which should be diagnosed - // elsewhere. Don't do anything here, to avoid an unhelpful - // chain of error messages. - return true; - } - } + mpfr_clear(left_real); + mpfr_clear(left_imag); + mpfr_clear(right_real); + mpfr_clear(right_imag); - if (type != NULL && !type->is_abstract()) - { - if ((type != left_type - && !Complex_expression::check_constant(left_real, left_imag, - type, location)) - || (type != right_type - && !Complex_expression::check_constant(right_real, right_imag, - type, location)) - || !Complex_expression::check_constant(real, imag, type, - location)) - { - mpfr_set_ui(real, 0, GMP_RNDN); - mpfr_set_ui(imag, 0, GMP_RNDN); - } - } + nc->set_complex(NULL, real, imag); + mpfr_clear(real); + mpfr_clear(imag); - return true; + return ret; } // Lower a binary expression. We have to evaluate constant @@ -5566,288 +5028,34 @@ Binary_expression::do_lower(Gogo* gogo, Named_object*, || op == OPERATOR_GT || op == OPERATOR_GE); - // Integer constant expressions. + // Numeric constant expressions. { - mpz_t left_val; - mpz_init(left_val); - Type* left_type; - mpz_t right_val; - mpz_init(right_val); - Type* right_type; - if (left->integer_constant_value(false, left_val, &left_type) - && right->integer_constant_value(false, right_val, &right_type)) + Numeric_constant left_nc; + Numeric_constant right_nc; + if (left->numeric_constant_value(&left_nc) + && right->numeric_constant_value(&right_nc)) { - Expression* ret = NULL; - if (left_type != right_type - && left_type != NULL - && !left_type->is_abstract() - && right_type != NULL - && !right_type->is_abstract() - && left_type->base() != right_type->base() - && op != OPERATOR_LSHIFT - && op != OPERATOR_RSHIFT) - { - // May be a type error--let it be diagnosed later. - return this; - } - else if (is_comparison) + if (is_comparison) { - bool b = Binary_expression::compare_integer(op, left_val, - right_val); - ret = Expression::make_cast(Type::lookup_bool_type(), - Expression::make_boolean(b, location), - location); + bool result; + if (!Binary_expression::compare_constant(op, &left_nc, + &right_nc, location, + &result)) + return this; + return Expression::make_cast(Type::lookup_bool_type(), + Expression::make_boolean(result, + location), + location); } else { - mpz_t val; - mpz_init(val); - - if (Binary_expression::eval_integer(op, left_type, left_val, - right_type, right_val, - location, val)) - { - go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND); - Type* type; - if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) - type = left_type; - else if (left_type == NULL) - type = right_type; - else if (right_type == NULL) - type = left_type; - else if (!left_type->is_abstract() - && left_type->named_type() != NULL) - type = left_type; - else if (!right_type->is_abstract() - && right_type->named_type() != NULL) - type = right_type; - else if (!left_type->is_abstract()) - type = left_type; - else if (!right_type->is_abstract()) - type = right_type; - else if (left_type->float_type() != NULL) - type = left_type; - else if (right_type->float_type() != NULL) - type = right_type; - else if (left_type->complex_type() != NULL) - type = left_type; - else if (right_type->complex_type() != NULL) - type = right_type; - else - type = left_type; - - bool is_character = false; - if (type == NULL) - { - Type* t = this->left_->type(); - if (t->integer_type() != NULL - && t->integer_type()->is_rune()) - is_character = true; - else if (op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT) - { - t = this->right_->type(); - if (t->integer_type() != NULL - && t->integer_type()->is_rune()) - is_character = true; - } - } - - if (is_character) - ret = Expression::make_character(&val, type, location); - else - ret = Expression::make_integer(&val, type, location); - } - - mpz_clear(val); - } - - if (ret != NULL) - { - mpz_clear(right_val); - mpz_clear(left_val); - return ret; + Numeric_constant nc; + if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, + location, &nc)) + return this; + return nc.expression(location); } } - mpz_clear(right_val); - mpz_clear(left_val); - } - - // Floating point constant expressions. - { - mpfr_t left_val; - mpfr_init(left_val); - Type* left_type; - mpfr_t right_val; - mpfr_init(right_val); - Type* right_type; - if (left->float_constant_value(left_val, &left_type) - && right->float_constant_value(right_val, &right_type)) - { - Expression* ret = NULL; - if (left_type != right_type - && left_type != NULL - && right_type != NULL - && left_type->base() != right_type->base() - && op != OPERATOR_LSHIFT - && op != OPERATOR_RSHIFT) - { - // May be a type error--let it be diagnosed later. - return this; - } - else if (is_comparison) - { - bool b = Binary_expression::compare_float(op, - (left_type != NULL - ? left_type - : right_type), - left_val, right_val); - ret = Expression::make_boolean(b, location); - } - else - { - mpfr_t val; - mpfr_init(val); - - if (Binary_expression::eval_float(op, left_type, left_val, - right_type, right_val, val, - location)) - { - go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND - && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT); - Type* type; - if (left_type == NULL) - type = right_type; - else if (right_type == NULL) - type = left_type; - else if (!left_type->is_abstract() - && left_type->named_type() != NULL) - type = left_type; - else if (!right_type->is_abstract() - && right_type->named_type() != NULL) - type = right_type; - else if (!left_type->is_abstract()) - type = left_type; - else if (!right_type->is_abstract()) - type = right_type; - else if (left_type->float_type() != NULL) - type = left_type; - else if (right_type->float_type() != NULL) - type = right_type; - else - type = left_type; - ret = Expression::make_float(&val, type, location); - } - - mpfr_clear(val); - } - - if (ret != NULL) - { - mpfr_clear(right_val); - mpfr_clear(left_val); - return ret; - } - } - mpfr_clear(right_val); - mpfr_clear(left_val); - } - - // Complex constant expressions. - { - mpfr_t left_real; - mpfr_t left_imag; - mpfr_init(left_real); - mpfr_init(left_imag); - Type* left_type; - - mpfr_t right_real; - mpfr_t right_imag; - mpfr_init(right_real); - mpfr_init(right_imag); - Type* right_type; - - if (left->complex_constant_value(left_real, left_imag, &left_type) - && right->complex_constant_value(right_real, right_imag, &right_type)) - { - Expression* ret = NULL; - if (left_type != right_type - && left_type != NULL - && right_type != NULL - && left_type->base() != right_type->base()) - { - // May be a type error--let it be diagnosed later. - return this; - } - else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) - { - bool b = Binary_expression::compare_complex(op, - (left_type != NULL - ? left_type - : right_type), - left_real, - left_imag, - right_real, - right_imag); - ret = Expression::make_boolean(b, location); - } - else - { - mpfr_t real; - mpfr_t imag; - mpfr_init(real); - mpfr_init(imag); - - if (Binary_expression::eval_complex(op, left_type, - left_real, left_imag, - right_type, - right_real, right_imag, - real, imag, - location)) - { - go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND - && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT); - Type* type; - if (left_type == NULL) - type = right_type; - else if (right_type == NULL) - type = left_type; - else if (!left_type->is_abstract() - && left_type->named_type() != NULL) - type = left_type; - else if (!right_type->is_abstract() - && right_type->named_type() != NULL) - type = right_type; - else if (!left_type->is_abstract()) - type = left_type; - else if (!right_type->is_abstract()) - type = right_type; - else if (left_type->complex_type() != NULL) - type = left_type; - else if (right_type->complex_type() != NULL) - type = right_type; - else - type = left_type; - ret = Expression::make_complex(&real, &imag, type, - location); - } - mpfr_clear(real); - mpfr_clear(imag); - } - - if (ret != NULL) - { - mpfr_clear(left_real); - mpfr_clear(left_imag); - mpfr_clear(right_real); - mpfr_clear(right_imag); - return ret; - } - } - - mpfr_clear(left_real); - mpfr_clear(left_imag); - mpfr_clear(right_real); - mpfr_clear(right_imag); } // String constant expressions. @@ -5864,77 +5072,13 @@ Binary_expression::do_lower(Gogo* gogo, Named_object*, else if (is_comparison) { int cmp = left_string.compare(right_string); - bool r; - switch (op) - { - case OPERATOR_EQEQ: - r = cmp == 0; - break; - case OPERATOR_NOTEQ: - r = cmp != 0; - break; - case OPERATOR_LT: - r = cmp < 0; - break; - case OPERATOR_LE: - r = cmp <= 0; - break; - case OPERATOR_GT: - r = cmp > 0; - break; - case OPERATOR_GE: - r = cmp >= 0; - break; - default: - go_unreachable(); - } - return Expression::make_boolean(r, location); - } - } - } - - // Special case for shift of a floating point constant. - if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) - { - mpfr_t left_val; - mpfr_init(left_val); - Type* left_type; - mpz_t right_val; - mpz_init(right_val); - Type* right_type; - if (left->float_constant_value(left_val, &left_type) - && right->integer_constant_value(false, right_val, &right_type) - && mpfr_integer_p(left_val) - && (left_type == NULL - || left_type->is_abstract() - || left_type->integer_type() != NULL)) - { - mpz_t left_int; - mpz_init(left_int); - mpfr_get_z(left_int, left_val, GMP_RNDN); - - mpz_t val; - mpz_init(val); - - Expression* ret = NULL; - if (Binary_expression::eval_integer(op, left_type, left_int, - right_type, right_val, - location, val)) - ret = Expression::make_integer(&val, left_type, location); - - mpz_clear(left_int); - mpz_clear(val); - - if (ret != NULL) - { - mpfr_clear(left_val); - mpz_clear(right_val); - return ret; + bool r = Binary_expression::cmp_to_bool(op, cmp); + return Expression::make_cast(Type::lookup_bool_type(), + Expression::make_boolean(r, + location), + location); } } - - mpfr_clear(left_val); - mpz_clear(right_val); } // Lower struct and array comparisons. @@ -6114,154 +5258,30 @@ Binary_expression::operand_address(Statement_inserter* inserter, return Expression::make_cast(unsafe_pointer_type, expr, loc); } -// Return the integer constant value, if it has one. - -bool -Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, - Type** ptype) const -{ - mpz_t left_val; - mpz_init(left_val); - Type* left_type; - if (!this->left_->integer_constant_value(iota_is_constant, left_val, - &left_type)) - { - mpz_clear(left_val); - return false; - } - - mpz_t right_val; - mpz_init(right_val); - Type* right_type; - if (!this->right_->integer_constant_value(iota_is_constant, right_val, - &right_type)) - { - mpz_clear(right_val); - mpz_clear(left_val); - return false; - } - - bool ret; - if (left_type != right_type - && left_type != NULL - && right_type != NULL - && left_type->base() != right_type->base() - && this->op_ != OPERATOR_RSHIFT - && this->op_ != OPERATOR_LSHIFT) - ret = false; - else - ret = Binary_expression::eval_integer(this->op_, left_type, left_val, - right_type, right_val, - this->location(), val); - - mpz_clear(right_val); - mpz_clear(left_val); - - if (ret) - *ptype = left_type; - - return ret; -} - -// Return the floating point constant value, if it has one. - -bool -Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const -{ - mpfr_t left_val; - mpfr_init(left_val); - Type* left_type; - if (!this->left_->float_constant_value(left_val, &left_type)) - { - mpfr_clear(left_val); - return false; - } - - mpfr_t right_val; - mpfr_init(right_val); - Type* right_type; - if (!this->right_->float_constant_value(right_val, &right_type)) - { - mpfr_clear(right_val); - mpfr_clear(left_val); - return false; - } - - bool ret; - if (left_type != right_type - && left_type != NULL - && right_type != NULL - && left_type->base() != right_type->base()) - ret = false; - else - ret = Binary_expression::eval_float(this->op_, left_type, left_val, - right_type, right_val, - val, this->location()); - - mpfr_clear(left_val); - mpfr_clear(right_val); - - if (ret) - *ptype = left_type; - - return ret; -} - -// Return the complex constant value, if it has one. +// Return the numeric constant value, if it has one. bool -Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, - Type** ptype) const +Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const { - mpfr_t left_real; - mpfr_t left_imag; - mpfr_init(left_real); - mpfr_init(left_imag); - Type* left_type; - if (!this->left_->complex_constant_value(left_real, left_imag, &left_type)) - { - mpfr_clear(left_real); - mpfr_clear(left_imag); - return false; - } - - mpfr_t right_real; - mpfr_t right_imag; - mpfr_init(right_real); - mpfr_init(right_imag); - Type* right_type; - if (!this->right_->complex_constant_value(right_real, right_imag, - &right_type)) - { - mpfr_clear(left_real); - mpfr_clear(left_imag); - mpfr_clear(right_real); - mpfr_clear(right_imag); - return false; - } + Operator op = this->op_; - bool ret; - if (left_type != right_type - && left_type != NULL - && right_type != NULL - && left_type->base() != right_type->base()) - ret = false; - else - ret = Binary_expression::eval_complex(this->op_, left_type, - left_real, left_imag, - right_type, - right_real, right_imag, - real, imag, - this->location()); - mpfr_clear(left_real); - mpfr_clear(left_imag); - mpfr_clear(right_real); - mpfr_clear(right_imag); + if (op == OPERATOR_EQEQ + || op == OPERATOR_NOTEQ + || op == OPERATOR_LT + || op == OPERATOR_LE + || op == OPERATOR_GT + || op == OPERATOR_GE) + return false; - if (ret) - *ptype = left_type; + Numeric_constant left_nc; + if (!this->left_->numeric_constant_value(&left_nc)) + return false; + Numeric_constant right_nc; + if (!this->right_->numeric_constant_value(&right_nc)) + return false; - return ret; + return Binary_expression::eval_constant(op, &left_nc, &right_nc, + this->location(), nc); } // Note that the value is being discarded. @@ -6305,41 +5325,13 @@ Binary_expression::do_type() case OPERATOR_AND: case OPERATOR_BITCLEAR: { - Type* left_type = this->left_->type(); - Type* right_type = this->right_->type(); - if (left_type->is_error()) - return left_type; - else if (right_type->is_error()) - return right_type; - else if (!Type::are_compatible_for_binop(left_type, right_type)) - { - this->report_error(_("incompatible types in binary expression")); - return Type::make_error_type(); - } - else if (!left_type->is_abstract() && left_type->named_type() != NULL) - return left_type; - else if (!right_type->is_abstract() && right_type->named_type() != NULL) - return right_type; - else if (!left_type->is_abstract()) - return left_type; - else if (!right_type->is_abstract()) - return right_type; - else if (left_type->complex_type() != NULL) - return left_type; - else if (right_type->complex_type() != NULL) - return right_type; - else if (left_type->float_type() != NULL) - return left_type; - else if (right_type->float_type() != NULL) - return right_type; - else if (left_type->integer_type() != NULL - && left_type->integer_type()->is_rune()) - return left_type; - else if (right_type->integer_type() != NULL - && right_type->integer_type()->is_rune()) - return right_type; - else - return left_type; + Type* type; + if (!Binary_expression::operation_type(this->op_, + this->left_->type(), + this->right_->type(), + &type)) + return Type::make_error_type(); + return type; } case OPERATOR_LSHIFT: @@ -6604,21 +5596,25 @@ Binary_expression::do_check_types(Gogo*) this->report_error(_("shift count not unsigned integer")); else { - mpz_t val; - mpz_init(val); - Type* type; - if (this->right_->integer_constant_value(true, val, &type)) + Numeric_constant nc; + if (this->right_->numeric_constant_value(&nc)) { - if (mpz_sgn(val) < 0) + mpz_t val; + if (!nc.to_int(&val)) + this->report_error(_("shift count not unsigned integer")); + else { - this->report_error(_("negative shift count")); - mpz_set_ui(val, 0); - Location rloc = this->right_->location(); - this->right_ = Expression::make_integer(&val, right_type, - rloc); + if (mpz_sgn(val) < 0) + { + this->report_error(_("negative shift count")); + mpz_set_ui(val, 0); + Location rloc = this->right_->location(); + this->right_ = Expression::make_integer(&val, right_type, + rloc); + } + mpz_clear(val); } } - mpz_clear(val); } } } @@ -7390,13 +6386,7 @@ class Builtin_call_expression : public Call_expression do_is_constant() const; bool - do_integer_constant_value(bool, mpz_t, Type**) const; - - bool - do_float_constant_value(mpfr_t, Type**) const; - - bool - do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; + do_numeric_constant_value(Numeric_constant*) const; void do_discarding_value(); @@ -7628,37 +6618,9 @@ Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, } } - mpz_t ival; - mpz_init(ival); - Type* type; - if (this->integer_constant_value(true, ival, &type)) - { - Expression* ret = Expression::make_integer(&ival, type, loc); - mpz_clear(ival); - return ret; - } - mpz_clear(ival); - - mpfr_t rval; - mpfr_init(rval); - if (this->float_constant_value(rval, &type)) - { - Expression* ret = Expression::make_float(&rval, type, loc); - mpfr_clear(rval); - return ret; - } - - mpfr_t imag; - mpfr_init(imag); - if (this->complex_constant_value(rval, imag, &type)) - { - Expression* ret = Expression::make_complex(&rval, &imag, type, loc); - mpfr_clear(rval); - mpfr_clear(imag); - return ret; - } - mpfr_clear(rval); - mpfr_clear(imag); + Numeric_constant nc; + if (this->numeric_constant_value(&nc)) + return nc.expression(loc); } switch (this->code_) @@ -7906,43 +6868,14 @@ Builtin_call_expression::check_int_value(Expression* e) return true; // Check for a floating point constant with integer value. - mpfr_t fval; - mpfr_init(fval); - - Type* dummy; - if (e->float_constant_value(fval, &dummy) && mpfr_integer_p(fval)) + Numeric_constant nc; + mpz_t ival; + if (e->numeric_constant_value(&nc) && nc.to_int(&ival)) { - mpz_t ival; - mpz_init(ival); - - bool ok = false; - - mpfr_clear_overflow(); - mpfr_clear_erangeflag(); - mpfr_get_z(ival, fval, GMP_RNDN); - if (!mpfr_overflow_p() - && !mpfr_erangeflag_p() - && mpz_sgn(ival) >= 0) - { - Named_type* ntype = Type::lookup_integer_type("int"); - Integer_type* inttype = ntype->integer_type(); - mpz_t max; - mpz_init_set_ui(max, 1); - mpz_mul_2exp(max, max, inttype->bits() - 1); - ok = mpz_cmp(ival, max) < 0; - mpz_clear(max); - } mpz_clear(ival); - - if (ok) - { - mpfr_clear(fval); - return true; - } + return true; } - mpfr_clear(fval); - return false; } @@ -8073,12 +7006,10 @@ Builtin_call_expression::do_is_constant() const return false; } -// Return an integer constant value if possible. +// Return a numeric constant if possible. bool -Builtin_call_expression::do_integer_constant_value(bool iota_is_constant, - mpz_t val, - Type** ptype) const +Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const { if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP) @@ -8093,8 +7024,8 @@ Builtin_call_expression::do_integer_constant_value(bool iota_is_constant, std::string sval; if (arg->string_constant_value(&sval)) { - mpz_set_ui(val, sval.length()); - *ptype = Type::lookup_integer_type("int"); + nc->set_unsigned_long(Type::lookup_integer_type("int"), + sval.length()); return true; } } @@ -8111,13 +7042,15 @@ Builtin_call_expression::do_integer_constant_value(bool iota_is_constant, return false; Expression* e = arg_type->array_type()->length(); this->seen_ = true; - bool r = e->integer_constant_value(iota_is_constant, val, ptype); + bool r = e->numeric_constant_value(nc); this->seen_ = false; if (r) { - *ptype = Type::lookup_integer_type("int"); - return true; + if (!nc->set_type(Type::lookup_integer_type("int"), false, + this->location())) + r = false; } + return r; } } else if (this->code_ == BUILTIN_SIZEOF @@ -8158,8 +7091,7 @@ Builtin_call_expression::do_integer_constant_value(bool iota_is_constant, else go_unreachable(); - mpz_set_ui(val, ret); - *ptype = NULL; + nc->set_unsigned_long(NULL, static_cast(ret)); return true; } else if (this->code_ == BUILTIN_OFFSETOF) @@ -8181,88 +7113,72 @@ Builtin_call_expression::do_integer_constant_value(bool iota_is_constant, farg->field_index(), &offset)) return false; - mpz_set_ui(val, offset); + nc->set_unsigned_long(NULL, static_cast(offset)); return true; } - return false; -} - -// Return a floating point constant value if possible. - -bool -Builtin_call_expression::do_float_constant_value(mpfr_t val, - Type** ptype) const -{ - if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) + else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) { Expression* arg = this->one_arg(); if (arg == NULL) return false; + Numeric_constant argnc; + if (!arg->numeric_constant_value(&argnc)) + return false; + mpfr_t real; mpfr_t imag; - mpfr_init(real); - mpfr_init(imag); - - bool ret = false; - Type* type; - if (arg->complex_constant_value(real, imag, &type)) - { - if (this->code_ == BUILTIN_REAL) - mpfr_set(val, real, GMP_RNDN); - else - mpfr_set(val, imag, GMP_RNDN); - *ptype = Builtin_call_expression::real_imag_type(type); - ret = true; - } + if (!argnc.to_complex(&real, &imag)) + return false; - mpfr_clear(real); - mpfr_clear(imag); - return ret; + Type* type = Builtin_call_expression::real_imag_type(argnc.type()); + if (this->code_ == BUILTIN_REAL) + nc->set_float(type, real); + else + nc->set_float(type, imag); + return true; } - - return false; -} - -// Return a complex constant value if possible. - -bool -Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, - Type** ptype) const -{ - if (this->code_ == BUILTIN_COMPLEX) + else if (this->code_ == BUILTIN_COMPLEX) { const Expression_list* args = this->args(); if (args == NULL || args->size() != 2) return false; + Numeric_constant rnc; + if (!args->front()->numeric_constant_value(&rnc)) + return false; + Numeric_constant inc; + if (!args->back()->numeric_constant_value(&inc)) + return false; + + if (rnc.type() != NULL + && !rnc.type()->is_abstract() + && inc.type() != NULL + && !inc.type()->is_abstract() + && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) + return false; + mpfr_t r; - mpfr_init(r); - Type* rtype; - if (!args->front()->float_constant_value(r, &rtype)) + if (!rnc.to_float(&r)) + return false; + mpfr_t i; + if (!inc.to_float(&i)) { mpfr_clear(r); return false; } - mpfr_t i; - mpfr_init(i); + Type* arg_type = rnc.type(); + if (arg_type == NULL || arg_type->is_abstract()) + arg_type = inc.type(); - bool ret = false; - Type* itype; - if (args->back()->float_constant_value(i, &itype) - && Type::are_identical(rtype, itype, false, NULL)) - { - mpfr_set(real, r, GMP_RNDN); - mpfr_set(imag, i, GMP_RNDN); - *ptype = Builtin_call_expression::complex_type(rtype); - ret = true; - } + Type* type = Builtin_call_expression::complex_type(arg_type); + nc->set_complex(type, r, i); mpfr_clear(r); mpfr_clear(i); - return ret; + return true; } return false; @@ -8461,15 +7377,19 @@ Builtin_call_expression::do_determine_type(const Type_context* context) { if (atype->integer_type() != NULL) { - mpz_t val; - mpz_init(val); - Type* dummy; - if (this->integer_constant_value(true, val, &dummy) - && mpz_sgn(val) >= 0) - want_type = Type::lookup_integer_type("uint64"); - else + Numeric_constant nc; + if (this->numeric_constant_value(&nc)) + { + mpz_t val; + if (nc.to_int(&val)) + { + if (mpz_sgn(val) >= 0) + want_type = Type::lookup_integer_type("uint64"); + mpz_clear(val); + } + } + if (want_type == NULL) want_type = Type::lookup_integer_type("int64"); - mpz_clear(val); } else if (atype->float_type() != NULL) want_type = Type::lookup_float_type("float64"); @@ -9148,20 +8068,17 @@ Builtin_call_expression::do_get_tree(Translate_context* context) case BUILTIN_OFFSETOF: case BUILTIN_ALIGNOF: { - mpz_t val; - mpz_init(val); - Type* dummy; - bool b = this->integer_constant_value(true, val, &dummy); - if (!b) + Numeric_constant nc; + unsigned long val; + if (!this->numeric_constant_value(&nc) + || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) { go_assert(saw_errors()); return error_mark_node; } Type* int_type = Type::lookup_integer_type("int"); tree type = type_to_tree(int_type->get_backend(gogo)); - tree ret = Expression::integer_constant_tree(val, type); - mpz_clear(val); - return ret; + return build_int_cst(type, val); } case BUILTIN_COPY: @@ -9376,50 +8293,37 @@ Builtin_call_expression::do_get_tree(Translate_context* context) void Builtin_call_expression::do_export(Export* exp) const { - bool ok = false; + Numeric_constant nc; + if (!this->numeric_constant_value(&nc)) + { + error_at(this->location(), "value is not constant"); + return; + } - mpz_t val; - mpz_init(val); - Type* dummy; - if (this->integer_constant_value(true, val, &dummy)) + if (nc.is_int()) { + mpz_t val; + nc.get_int(&val); Integer_expression::export_integer(exp, val); - ok = true; + mpz_clear(val); } - mpz_clear(val); - - if (!ok) + else if (nc.is_float()) { mpfr_t fval; - mpfr_init(fval); - if (this->float_constant_value(fval, &dummy)) - { - Float_expression::export_float(exp, fval); - ok = true; - } + nc.get_float(&fval); + Float_expression::export_float(exp, fval); mpfr_clear(fval); } - - if (!ok) + else if (nc.is_complex()) { mpfr_t real; mpfr_t imag; - mpfr_init(real); - mpfr_init(imag); - if (this->complex_constant_value(real, imag, &dummy)) - { - Complex_expression::export_complex(exp, real, imag); - ok = true; - } + Complex_expression::export_complex(exp, real, imag); mpfr_clear(real); mpfr_clear(imag); } - - if (!ok) - { - error_at(this->location(), "value is not constant"); - return; - } + else + go_unreachable(); // A trailing space lets us reliably identify the end of the number. exp->write_c_string(" "); @@ -10660,16 +9564,14 @@ Array_index_expression::do_check_types(Gogo*) unsigned int int_bits = Type::lookup_integer_type("int")->integer_type()->bits(); - Type* dummy; + Numeric_constant lvalnc; mpz_t lval; - mpz_init(lval); bool lval_valid = (array_type->length() != NULL - && array_type->length()->integer_constant_value(true, - lval, - &dummy)); + && array_type->length()->numeric_constant_value(&lvalnc) + && lvalnc.to_int(&lval)); + Numeric_constant inc; mpz_t ival; - mpz_init(ival); - if (this->start_->integer_constant_value(true, ival, &dummy)) + if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) { if (mpz_sgn(ival) < 0 || mpz_sizeinbase(ival, 2) >= int_bits @@ -10681,22 +9583,26 @@ Array_index_expression::do_check_types(Gogo*) error_at(this->start_->location(), "array index out of bounds"); this->set_is_error(); } + mpz_clear(ival); } if (this->end_ != NULL && !this->end_->is_nil_expression()) { - if (this->end_->integer_constant_value(true, ival, &dummy)) + Numeric_constant enc; + mpz_t eval; + if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) { - if (mpz_sgn(ival) < 0 - || mpz_sizeinbase(ival, 2) >= int_bits - || (lval_valid && mpz_cmp(ival, lval) > 0)) + if (mpz_sgn(eval) < 0 + || mpz_sizeinbase(eval, 2) >= int_bits + || (lval_valid && mpz_cmp(eval, lval) > 0)) { error_at(this->end_->location(), "array index out of bounds"); this->set_is_error(); } + mpz_clear(eval); } } - mpz_clear(ival); - mpz_clear(lval); + if (lval_valid) + mpz_clear(lval); // A slice of an array requires an addressable array. A slice of a // slice is always possible. @@ -11071,10 +9977,9 @@ String_index_expression::do_check_types(Gogo*) std::string sval; bool sval_valid = this->string_->string_constant_value(&sval); + Numeric_constant inc; mpz_t ival; - mpz_init(ival); - Type* dummy; - if (this->start_->integer_constant_value(true, ival, &dummy)) + if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) { if (mpz_sgn(ival) < 0 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0)) @@ -11082,20 +9987,23 @@ String_index_expression::do_check_types(Gogo*) error_at(this->start_->location(), "string index out of bounds"); this->set_is_error(); } + mpz_clear(ival); } if (this->end_ != NULL && !this->end_->is_nil_expression()) { - if (this->end_->integer_constant_value(true, ival, &dummy)) + Numeric_constant enc; + mpz_t eval; + if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) { - if (mpz_sgn(ival) < 0 - || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0)) + if (mpz_sgn(eval) < 0 + || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) { error_at(this->end_->location(), "string index out of bounds"); this->set_is_error(); } + mpz_clear(eval); } } - mpz_clear(ival); } // Get a tree for a string index. @@ -12464,17 +11372,15 @@ Array_construction_expression::do_check_types(Gogo*) } Expression* length = at->length(); - if (length != NULL && !length->is_error_expression()) + Numeric_constant nc; + unsigned long val; + if (length != NULL + && !length->is_error_expression() + && length->numeric_constant_value(&nc) + && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) { - mpz_t val; - mpz_init(val); - Type* type; - if (at->length()->integer_constant_value(true, val, &type)) - { - if (this->vals_->size() > mpz_get_ui(val)) - this->report_error(_("too many elements in composite literal")); - } - mpz_clear(val); + if (this->vals_->size() > val) + this->report_error(_("too many elements in composite literal")); } } @@ -13472,49 +12378,41 @@ Composite_literal_expression::lower_array(Type* type) if (index_expr != NULL) { - mpz_t ival; - mpz_init(ival); - - Type* dummy; - if (!index_expr->integer_constant_value(true, ival, &dummy)) + Numeric_constant nc; + if (!index_expr->numeric_constant_value(&nc)) { - mpz_clear(ival); error_at(index_expr->location(), "index expression is not integer constant"); return Expression::make_error(location); } - if (mpz_sgn(ival) < 0) + switch (nc.to_unsigned_long(&index)) { - mpz_clear(ival); + case Numeric_constant::NC_UL_VALID: + break; + case Numeric_constant::NC_UL_NOTINT: + error_at(index_expr->location(), + "index expression is not integer constant"); + return Expression::make_error(location); + case Numeric_constant::NC_UL_NEGATIVE: error_at(index_expr->location(), "index expression is negative"); return Expression::make_error(location); - } - - index = mpz_get_ui(ival); - if (mpz_cmp_ui(ival, index) != 0) - { - mpz_clear(ival); + case Numeric_constant::NC_UL_BIG: error_at(index_expr->location(), "index value overflow"); return Expression::make_error(location); + default: + go_unreachable(); } Named_type* ntype = Type::lookup_integer_type("int"); Integer_type* inttype = ntype->integer_type(); - mpz_t max; - mpz_init_set_ui(max, 1); - mpz_mul_2exp(max, max, inttype->bits() - 1); - bool ok = mpz_cmp(ival, max) < 0; - mpz_clear(max); - if (!ok) + if (sizeof(index) <= static_cast(inttype->bits() * 8) + && index >> (inttype->bits() - 1) != 0) { - mpz_clear(ival); error_at(index_expr->location(), "index value overflow"); return Expression::make_error(location); } - mpz_clear(ival); - // FIXME: Our representation isn't very good; this avoids // thrashing. if (index > 0x1000000) @@ -14449,3 +13347,594 @@ Expression_list::contains_error() const return true; return false; } + +// Class Numeric_constant. + +// Destructor. + +Numeric_constant::~Numeric_constant() +{ + this->clear(); +} + +// Copy constructor. + +Numeric_constant::Numeric_constant(const Numeric_constant& a) + : classification_(a.classification_), type_(a.type_) +{ + switch (a.classification_) + { + case NC_INVALID: + break; + case NC_INT: + case NC_RUNE: + mpz_init_set(this->u_.int_val, a.u_.int_val); + break; + case NC_FLOAT: + mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); + break; + case NC_COMPLEX: + mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real, + GMP_RNDN); + mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag, + GMP_RNDN); + break; + default: + go_unreachable(); + } +} + +// Assignment operator. + +Numeric_constant& +Numeric_constant::operator=(const Numeric_constant& a) +{ + this->clear(); + this->classification_ = a.classification_; + this->type_ = a.type_; + switch (a.classification_) + { + case NC_INVALID: + break; + case NC_INT: + case NC_RUNE: + mpz_init_set(this->u_.int_val, a.u_.int_val); + break; + case NC_FLOAT: + mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); + break; + case NC_COMPLEX: + mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real, + GMP_RNDN); + mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag, + GMP_RNDN); + break; + default: + go_unreachable(); + } + return *this; +} + +// Clear the contents. + +void +Numeric_constant::clear() +{ + switch (this->classification_) + { + case NC_INVALID: + break; + case NC_INT: + case NC_RUNE: + mpz_clear(this->u_.int_val); + break; + case NC_FLOAT: + mpfr_clear(this->u_.float_val); + break; + case NC_COMPLEX: + mpfr_clear(this->u_.complex_val.real); + mpfr_clear(this->u_.complex_val.imag); + break; + default: + go_unreachable(); + } + this->classification_ = NC_INVALID; +} + +// Set to an unsigned long value. + +void +Numeric_constant::set_unsigned_long(Type* type, unsigned long val) +{ + this->clear(); + this->classification_ = NC_INT; + this->type_ = type; + mpz_init_set_ui(this->u_.int_val, val); +} + +// Set to an integer value. + +void +Numeric_constant::set_int(Type* type, const mpz_t val) +{ + this->clear(); + this->classification_ = NC_INT; + this->type_ = type; + mpz_init_set(this->u_.int_val, val); +} + +// Set to a rune value. + +void +Numeric_constant::set_rune(Type* type, const mpz_t val) +{ + this->clear(); + this->classification_ = NC_RUNE; + this->type_ = type; + mpz_init_set(this->u_.int_val, val); +} + +// Set to a floating point value. + +void +Numeric_constant::set_float(Type* type, const mpfr_t val) +{ + this->clear(); + this->classification_ = NC_FLOAT; + this->type_ = type; + mpfr_init_set(this->u_.float_val, val, GMP_RNDN); +} + +// Set to a complex value. + +void +Numeric_constant::set_complex(Type* type, const mpfr_t real, const mpfr_t imag) +{ + this->clear(); + this->classification_ = NC_COMPLEX; + this->type_ = type; + mpfr_init_set(this->u_.complex_val.real, real, GMP_RNDN); + mpfr_init_set(this->u_.complex_val.imag, imag, GMP_RNDN); +} + +// Get an int value. + +void +Numeric_constant::get_int(mpz_t* val) const +{ + go_assert(this->is_int()); + mpz_init_set(*val, this->u_.int_val); +} + +// Get a rune value. + +void +Numeric_constant::get_rune(mpz_t* val) const +{ + go_assert(this->is_rune()); + mpz_init_set(*val, this->u_.int_val); +} + +// Get a floating point value. + +void +Numeric_constant::get_float(mpfr_t* val) const +{ + go_assert(this->is_float()); + mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); +} + +// Get a complex value. + +void +Numeric_constant::get_complex(mpfr_t* real, mpfr_t* imag) const +{ + go_assert(this->is_complex()); + mpfr_init_set(*real, this->u_.complex_val.real, GMP_RNDN); + mpfr_init_set(*imag, this->u_.complex_val.imag, GMP_RNDN); +} + +// Express value as unsigned long if possible. + +Numeric_constant::To_unsigned_long +Numeric_constant::to_unsigned_long(unsigned long* val) const +{ + switch (this->classification_) + { + case NC_INT: + case NC_RUNE: + return this->mpz_to_unsigned_long(this->u_.int_val, val); + case NC_FLOAT: + return this->mpfr_to_unsigned_long(this->u_.float_val, val); + case NC_COMPLEX: + if (!mpfr_zero_p(this->u_.complex_val.imag)) + return NC_UL_NOTINT; + return this->mpfr_to_unsigned_long(this->u_.complex_val.real, val); + default: + go_unreachable(); + } +} + +// Express integer value as unsigned long if possible. + +Numeric_constant::To_unsigned_long +Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, + unsigned long *val) const +{ + if (mpz_sgn(ival) < 0) + return NC_UL_NEGATIVE; + unsigned long ui = mpz_get_ui(ival); + if (mpz_cmp_ui(ival, ui) != 0) + return NC_UL_BIG; + *val = ui; + return NC_UL_VALID; +} + +// Express floating point value as unsigned long if possible. + +Numeric_constant::To_unsigned_long +Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, + unsigned long *val) const +{ + if (!mpfr_integer_p(fval)) + return NC_UL_NOTINT; + mpz_t ival; + mpz_init(ival); + mpfr_get_z(ival, fval, GMP_RNDN); + To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); + mpz_clear(ival); + return ret; +} + +// Convert value to integer if possible. + +bool +Numeric_constant::to_int(mpz_t* val) const +{ + switch (this->classification_) + { + case NC_INT: + case NC_RUNE: + mpz_init_set(*val, this->u_.int_val); + return true; + case NC_FLOAT: + if (!mpfr_integer_p(this->u_.float_val)) + return false; + mpz_init(*val); + mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); + return true; + case NC_COMPLEX: + if (!mpfr_zero_p(this->u_.complex_val.imag) + || !mpfr_integer_p(this->u_.complex_val.real)) + return false; + mpz_init(*val); + mpfr_get_z(*val, this->u_.complex_val.real, GMP_RNDN); + return true; + default: + go_unreachable(); + } +} + +// Convert value to floating point if possible. + +bool +Numeric_constant::to_float(mpfr_t* val) const +{ + switch (this->classification_) + { + case NC_INT: + case NC_RUNE: + mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); + return true; + case NC_FLOAT: + mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); + return true; + case NC_COMPLEX: + if (!mpfr_zero_p(this->u_.complex_val.imag)) + return false; + mpfr_init_set(*val, this->u_.complex_val.real, GMP_RNDN); + return true; + default: + go_unreachable(); + } +} + +// Convert value to complex. + +bool +Numeric_constant::to_complex(mpfr_t* vr, mpfr_t* vi) const +{ + switch (this->classification_) + { + case NC_INT: + case NC_RUNE: + mpfr_init_set_z(*vr, this->u_.int_val, GMP_RNDN); + mpfr_init_set_ui(*vi, 0, GMP_RNDN); + return true; + case NC_FLOAT: + mpfr_init_set(*vr, this->u_.float_val, GMP_RNDN); + mpfr_init_set_ui(*vi, 0, GMP_RNDN); + return true; + case NC_COMPLEX: + mpfr_init_set(*vr, this->u_.complex_val.real, GMP_RNDN); + mpfr_init_set(*vi, this->u_.complex_val.imag, GMP_RNDN); + return true; + default: + go_unreachable(); + } +} + +// Get the type. + +Type* +Numeric_constant::type() const +{ + if (this->type_ != NULL) + return this->type_; + switch (this->classification_) + { + case NC_INT: + return Type::make_abstract_integer_type(); + case NC_RUNE: + return Type::make_abstract_character_type(); + case NC_FLOAT: + return Type::make_abstract_float_type(); + case NC_COMPLEX: + return Type::make_abstract_complex_type(); + default: + go_unreachable(); + } +} + +// If the constant can be expressed in TYPE, then set the type of the +// constant to TYPE and return true. Otherwise return false, and, if +// ISSUE_ERROR is true, report an appropriate error message. + +bool +Numeric_constant::set_type(Type* type, bool issue_error, Location loc) +{ + bool ret; + if (type == NULL) + ret = true; + else if (type->integer_type() != NULL) + ret = this->check_int_type(type->integer_type(), issue_error, loc); + else if (type->float_type() != NULL) + ret = this->check_float_type(type->float_type(), issue_error, loc); + else if (type->complex_type() != NULL) + ret = this->check_complex_type(type->complex_type(), issue_error, loc); + else + go_unreachable(); + if (ret) + this->type_ = type; + return ret; +} + +// Check whether the constant can be expressed in an integer type. + +bool +Numeric_constant::check_int_type(Integer_type* type, bool issue_error, + Location location) const +{ + mpz_t val; + switch (this->classification_) + { + case NC_INT: + case NC_RUNE: + mpz_init_set(val, this->u_.int_val); + break; + + case NC_FLOAT: + if (!mpfr_integer_p(this->u_.float_val)) + { + if (issue_error) + error_at(location, "floating point constant truncated to integer"); + return false; + } + mpz_init(val); + mpfr_get_z(val, this->u_.float_val, GMP_RNDN); + break; + + case NC_COMPLEX: + if (!mpfr_integer_p(this->u_.complex_val.real) + || !mpfr_zero_p(this->u_.complex_val.imag)) + { + if (issue_error) + error_at(location, "complex constant truncated to integer"); + return false; + } + mpz_init(val); + mpfr_get_z(val, this->u_.complex_val.real, GMP_RNDN); + break; + + default: + go_unreachable(); + } + + bool ret; + if (type->is_abstract()) + ret = true; + else + { + int bits = mpz_sizeinbase(val, 2); + if (type->is_unsigned()) + { + // For an unsigned type we can only accept a nonnegative + // number, and we must be able to represents at least BITS. + ret = mpz_sgn(val) >= 0 && bits <= type->bits(); + } + else + { + // For a signed type we need an extra bit to indicate the + // sign. We have to handle the most negative integer + // specially. + ret = (bits + 1 <= type->bits() + || (bits <= type->bits() + && mpz_sgn(val) < 0 + && (mpz_scan1(val, 0) + == static_cast(type->bits() - 1)) + && mpz_scan0(val, type->bits()) == ULONG_MAX)); + } + } + + if (!ret && issue_error) + error_at(location, "integer constant overflow"); + + return ret; +} + +// Check whether the constant can be expressed in a floating point +// type. + +bool +Numeric_constant::check_float_type(Float_type* type, bool issue_error, + Location location) const +{ + mpfr_t val; + switch (this->classification_) + { + case NC_INT: + case NC_RUNE: + mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); + break; + + case NC_FLOAT: + mpfr_init_set(val, this->u_.float_val, GMP_RNDN); + break; + + case NC_COMPLEX: + if (!mpfr_zero_p(this->u_.complex_val.imag)) + { + if (issue_error) + error_at(location, "complex constant truncated to float"); + return false; + } + mpfr_init_set(val, this->u_.complex_val.real, GMP_RNDN); + break; + + default: + go_unreachable(); + } + + bool ret; + if (type->is_abstract()) + ret = true; + else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) + { + // A NaN or Infinity always fits in the range of the type. + ret = true; + } + else + { + mp_exp_t exp = mpfr_get_exp(val); + mp_exp_t max_exp; + switch (type->bits()) + { + case 32: + max_exp = 128; + break; + case 64: + max_exp = 1024; + break; + default: + go_unreachable(); + } + + ret = exp <= max_exp; + } + + mpfr_clear(val); + + if (!ret && issue_error) + error_at(location, "floating point constant overflow"); + + return ret; +} + +// Check whether the constant can be expressed in a complex type. + +bool +Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, + Location location) const +{ + if (type->is_abstract()) + return true; + + mp_exp_t max_exp; + switch (type->bits()) + { + case 64: + max_exp = 128; + break; + case 128: + max_exp = 1024; + break; + default: + go_unreachable(); + } + + mpfr_t real; + switch (this->classification_) + { + case NC_INT: + case NC_RUNE: + mpfr_init_set_z(real, this->u_.int_val, GMP_RNDN); + break; + + case NC_FLOAT: + mpfr_init_set(real, this->u_.float_val, GMP_RNDN); + break; + + case NC_COMPLEX: + if (!mpfr_nan_p(this->u_.complex_val.imag) + && !mpfr_inf_p(this->u_.complex_val.imag) + && !mpfr_zero_p(this->u_.complex_val.imag)) + { + if (mpfr_get_exp(this->u_.complex_val.imag) > max_exp) + { + if (issue_error) + error_at(location, "complex imaginary part overflow"); + return false; + } + } + mpfr_init_set(real, this->u_.complex_val.real, GMP_RNDN); + break; + + default: + go_unreachable(); + } + + bool ret; + if (mpfr_nan_p(real) || mpfr_inf_p(real) || mpfr_zero_p(real)) + ret = true; + else + ret = mpfr_get_exp(real) <= max_exp; + + mpfr_clear(real); + + if (!ret && issue_error) + error_at(location, "complex real part overflow"); + + return ret; +} + +// Return an Expression for this value. + +Expression* +Numeric_constant::expression(Location loc) const +{ + switch (this->classification_) + { + case NC_INT: + return Expression::make_integer(&this->u_.int_val, this->type_, loc); + case NC_RUNE: + return Expression::make_character(&this->u_.int_val, this->type_, loc); + case NC_FLOAT: + return Expression::make_float(&this->u_.float_val, this->type_, loc); + case NC_COMPLEX: + return Expression::make_complex(&this->u_.complex_val.real, + &this->u_.complex_val.imag, + this->type_, loc); + default: + go_unreachable(); + } +} diff --git a/gcc/go/gofrontend/expressions.h b/gcc/go/gofrontend/expressions.h index 0a3f205..090e893 100644 --- a/gcc/go/gofrontend/expressions.h +++ b/gcc/go/gofrontend/expressions.h @@ -18,6 +18,9 @@ class Traverse; class Statement_inserter; class Type; struct Type_context; +class Integer_type; +class Float_type; +class Complex_type; class Function_type; class Map_type; class Struct_type; @@ -38,6 +41,7 @@ class Field_reference_expression; class Interface_field_reference_expression; class Type_guard_expression; class Receive_expression; +class Numeric_constant; class Named_object; class Export; class Import; @@ -342,30 +346,11 @@ class Expression is_constant() const { return this->do_is_constant(); } - // If this is not a constant expression with integral type, return - // false. If it is one, return true, and set VAL to the value. VAL - // should already be initialized. If this returns true, it sets - // *PTYPE to the type of the value, or NULL for an abstract type. - // If IOTA_IS_CONSTANT is true, then an iota expression is assumed - // to have its final value. + // If this is not a numeric constant, return false. If it is one, + // return true, and set VAL to hold the value. bool - integer_constant_value(bool iota_is_constant, mpz_t val, Type** ptype) const; - - // If this is not a constant expression with floating point type, - // return false. If it is one, return true, and set VAL to the - // value. VAL should already be initialized. If this returns true, - // it sets *PTYPE to the type of the value, or NULL for an abstract - // type. - bool - float_constant_value(mpfr_t val, Type** ptype) const; - - // If this is not a constant expression with complex type, return - // false. If it is one, return true, and set REAL and IMAG to the - // value. REAL and IMAG should already be initialized. If this - // return strue, it sets *PTYPE to the type of the value, or NULL - // for an abstract type. - bool - complex_constant_value(mpfr_t real, mpfr_t imag, Type** ptype) const; + numeric_constant_value(Numeric_constant* val) const + { return this->do_numeric_constant_value(val); } // If this is not a constant expression with string type, return // false. If it is one, return true, and set VAL to the value. @@ -691,22 +676,10 @@ class Expression do_is_constant() const { return false; } - // Return whether this is a constant expression of integral type, - // and set VAL to the value. - virtual bool - do_integer_constant_value(bool, mpz_t, Type**) const - { return false; } - - // Return whether this is a constant expression of floating point - // type, and set VAL to the value. + // Return whether this is a constant expression of numeric type, and + // set the Numeric_constant to the value. virtual bool - do_float_constant_value(mpfr_t, Type**) const - { return false; } - - // Return whether this is a constant expression of complex type, and - // set REAL and IMAGE to the value. - virtual bool - do_complex_constant_value(mpfr_t, mpfr_t, Type**) const + do_numeric_constant_value(Numeric_constant*) const { return false; } // Return whether this is a constant expression of string type, and @@ -1189,42 +1162,21 @@ class Binary_expression : public Expression right() { return this->right_; } - // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL. - // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of - // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true - // if this could be done, false if not. + // 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. static bool - eval_integer(Operator op, Type* left_type, mpz_t left_val, - Type* right_type, mpz_t right_val, Location, - mpz_t val); + eval_constant(Operator op, Numeric_constant* left_nc, + Numeric_constant* right_nc, Location location, + Numeric_constant* nc); - // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL. - // Return true if this could be done, false if not. + // Compare constants LEFT_NC and RIGHT_NC according to OP, setting + // *RESULT. Return true if this could be done, false if not. Issue + // errors at LOCATION as appropriate. static bool - eval_float(Operator op, Type* left_type, mpfr_t left_val, - Type* right_type, mpfr_t right_val, mpfr_t val, - Location); - - // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and - // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this - // could be done, false if not. - static bool - eval_complex(Operator op, Type* left_type, mpfr_t left_real, - mpfr_t left_imag, Type* right_type, mpfr_t right_real, - mpfr_t right_imag, mpfr_t real, mpfr_t imag, Location); - - // Compare integer constants according to OP. - static bool - compare_integer(Operator op, mpz_t left_val, mpz_t right_val); - - // Compare floating point constants according to OP. - static bool - compare_float(Operator op, Type* type, mpfr_t left_val, mpfr_t right_val); - - // Compare complex constants according to OP. - static bool - compare_complex(Operator op, Type* type, mpfr_t left_real, mpfr_t left_imag, - mpfr_t right_val, mpfr_t right_imag); + compare_constant(Operator op, Numeric_constant* left_nc, + Numeric_constant* right_nc, Location location, + bool* result); static Expression* do_import(Import*); @@ -1246,13 +1198,7 @@ class Binary_expression : public Expression { return this->left_->is_constant() && this->right_->is_constant(); } bool - do_integer_constant_value(bool, mpz_t val, Type**) const; - - bool - do_float_constant_value(mpfr_t val, Type**) const; - - bool - do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const; + do_numeric_constant_value(Numeric_constant*) const; void do_discarding_value(); @@ -1283,6 +1229,34 @@ class Binary_expression : public Expression do_dump_expression(Ast_dump_context*) const; private: + static bool + operation_type(Operator op, Type* left_type, Type* right_type, + Type** result_type); + + static bool + cmp_to_bool(Operator op, int cmp); + + static bool + eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*, + Location, Numeric_constant*); + + static bool + eval_float(Operator op, const Numeric_constant*, const Numeric_constant*, + Location, Numeric_constant*); + + static bool + eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*, + Location, Numeric_constant*); + + static bool + compare_integer(const Numeric_constant*, const Numeric_constant*, int*); + + static bool + compare_float(const Numeric_constant*, const Numeric_constant *, int*); + + static bool + compare_complex(const Numeric_constant*, const Numeric_constant*, int*); + Expression* lower_struct_comparison(Gogo*, Statement_inserter*); @@ -2101,4 +2075,173 @@ class Receive_expression : public Expression Expression* channel_; }; +// A numeric constant. This is used both for untyped constants and +// for constants that have a type. + +class Numeric_constant +{ + public: + Numeric_constant() + : classification_(NC_INVALID), type_(NULL) + { } + + ~Numeric_constant(); + + Numeric_constant(const Numeric_constant&); + + Numeric_constant& operator=(const Numeric_constant&); + + // Set to an unsigned long value. + void + set_unsigned_long(Type*, unsigned long); + + // Set to an integer value. + void + set_int(Type*, const mpz_t); + + // Set to a rune value. + void + set_rune(Type*, const mpz_t); + + // Set to a floating point value. + void + set_float(Type*, const mpfr_t); + + // Set to a complex value. + void + set_complex(Type*, const mpfr_t, const mpfr_t); + + // Classifiers. + bool + is_int() const + { return this->classification_ == Numeric_constant::NC_INT; } + + bool + is_rune() const + { return this->classification_ == Numeric_constant::NC_RUNE; } + + bool + is_float() const + { return this->classification_ == Numeric_constant::NC_FLOAT; } + + bool + is_complex() const + { return this->classification_ == Numeric_constant::NC_COMPLEX; } + + // Value retrievers. These will initialize the values as well as + // set them. GET_INT is only valid if IS_INT returns true, and + // likewise respectively. + void + get_int(mpz_t*) const; + + void + get_rune(mpz_t*) const; + + void + get_float(mpfr_t*) const; + + void + get_complex(mpfr_t*, mpfr_t*) const; + + // Codes returned by to_unsigned_long. + enum To_unsigned_long + { + // Value is integer and fits in unsigned long. + NC_UL_VALID, + // Value is not integer. + NC_UL_NOTINT, + // Value is integer but is negative. + NC_UL_NEGATIVE, + // Value is non-negative integer but does not fit in unsigned + // long. + NC_UL_BIG + }; + + // If the value can be expressed as an integer that fits in an + // unsigned long, set *VAL and return NC_UL_VALID. Otherwise return + // one of the other To_unsigned_long codes. + To_unsigned_long + to_unsigned_long(unsigned long* val) const; + + // If the value can be expressed as an int, return true and + // initialize and set VAL. This will return false for a value with + // an explicit float or complex type, even if the value is integral. + bool + to_int(mpz_t* val) const; + + // If the value can be expressed as a float, return true and + // initialize and set VAL. + bool + to_float(mpfr_t* val) const; + + // If the value can be expressed as a complex, return true and + // initialize and set VR and VI. + bool + to_complex(mpfr_t* vr, mpfr_t* vi) const; + + // Get the type. + Type* + type() const; + + // If the constant can be expressed in TYPE, then set the type of + // the constant to TYPE and return true. Otherwise return false, + // and, if ISSUE_ERROR is true, issue an error message. LOCATION is + // the location to use for the error. + bool + set_type(Type* type, bool issue_error, Location location); + + // Return an Expression for this value. + Expression* + expression(Location) const; + + private: + void + clear(); + + To_unsigned_long + mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const; + + To_unsigned_long + mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const; + + bool + check_int_type(Integer_type*, bool, Location) const; + + bool + check_float_type(Float_type*, bool, Location) const; + + bool + check_complex_type(Complex_type*, bool, Location) const; + + // The kinds of constants. + enum Classification + { + NC_INVALID, + NC_RUNE, + NC_INT, + NC_FLOAT, + NC_COMPLEX + }; + + // The kind of constant. + Classification classification_; + // The value. + union + { + // If NC_INT or NC_RUNE. + mpz_t int_val; + // If NC_FLOAT. + mpfr_t float_val; + // If NC_COMPLEX. + struct + { + mpfr_t real; + mpfr_t imag; + } complex_val; + } u_; + // The type if there is one. This will be NULL for an untyped + // constant. + Type* type_; +}; + #endif // !defined(GO_EXPRESSIONS_H) diff --git a/gcc/go/gofrontend/statements.cc b/gcc/go/gofrontend/statements.cc index 65c64c5..7f91e55 100644 --- a/gcc/go/gofrontend/statements.cc +++ b/gcc/go/gofrontend/statements.cc @@ -3214,10 +3214,9 @@ class Case_clauses::Hash_integer_value size_t Case_clauses::Hash_integer_value::operator()(Expression* pe) const { - Type* itype; + Numeric_constant nc; mpz_t ival; - mpz_init(ival); - if (!pe->integer_constant_value(true, ival, &itype)) + if (!pe->numeric_constant_value(&nc) || !nc.to_int(&ival)) go_unreachable(); size_t ret = mpz_get_ui(ival); mpz_clear(ival); @@ -3236,14 +3235,14 @@ class Case_clauses::Eq_integer_value bool Case_clauses::Eq_integer_value::operator()(Expression* a, Expression* b) const { - Type* atype; - Type* btype; + Numeric_constant anc; mpz_t aval; + Numeric_constant bnc; mpz_t bval; - mpz_init(aval); - mpz_init(bval); - if (!a->integer_constant_value(true, aval, &atype) - || !b->integer_constant_value(true, bval, &btype)) + if (!a->numeric_constant_value(&anc) + || !anc.to_int(&aval) + || !b->numeric_constant_value(&bnc) + || !bnc.to_int(&bval)) go_unreachable(); bool ret = mpz_cmp(aval, bval) == 0; mpz_clear(aval); @@ -3431,18 +3430,17 @@ Case_clauses::Case_clause::get_backend(Translate_context* context, Expression* e = *p; if (e->classification() != Expression::EXPRESSION_INTEGER) { - Type* itype; + Numeric_constant nc; mpz_t ival; - mpz_init(ival); - if (!(*p)->integer_constant_value(true, ival, &itype)) + if (!(*p)->numeric_constant_value(&nc) || !nc.to_int(&ival)) { // Something went wrong. This can happen with a // negative constant and an unsigned switch value. go_assert(saw_errors()); continue; } - go_assert(itype != NULL); - e = Expression::make_integer(&ival, itype, e->location()); + go_assert(nc.type() != NULL); + e = Expression::make_integer(&ival, nc.type(), e->location()); mpz_clear(ival); } diff --git a/gcc/go/gofrontend/types.cc b/gcc/go/gofrontend/types.cc index 03f1b3e..9e64a6a 100644 --- a/gcc/go/gofrontend/types.cc +++ b/gcc/go/gofrontend/types.cc @@ -2230,15 +2230,13 @@ Type::is_backend_type_size_known(Gogo* gogo) return true; else { + Numeric_constant nc; + if (!at->length()->numeric_constant_value(&nc)) + return false; mpz_t ival; - mpz_init(ival); - Type* dummy; - bool length_known = at->length()->integer_constant_value(true, - ival, - &dummy); - mpz_clear(ival); - if (!length_known) + if (!nc.to_int(&ival)) return false; + mpz_clear(ival); return at->element_type()->is_backend_type_size_known(gogo); } } @@ -5106,17 +5104,22 @@ Array_type::is_identical(const Array_type* t, bool errors_are_identical) const // Try to determine the lengths. If we can't, assume the arrays // are not identical. bool ret = false; - mpz_t v1; - mpz_init(v1); - Type* type1; - mpz_t v2; - mpz_init(v2); - Type* type2; - if (l1->integer_constant_value(true, v1, &type1) - && l2->integer_constant_value(true, v2, &type2)) - ret = mpz_cmp(v1, v2) == 0; - mpz_clear(v1); - mpz_clear(v2); + Numeric_constant nc1, nc2; + if (l1->numeric_constant_value(&nc1) + && l2->numeric_constant_value(&nc2)) + { + mpz_t v1; + if (nc1.to_int(&v1)) + { + mpz_t v2; + if (nc2.to_int(&v2)) + { + ret = mpz_cmp(v1, v2) == 0; + mpz_clear(v2); + } + mpz_clear(v1); + } + } return ret; } @@ -5154,58 +5157,44 @@ Array_type::verify_length() return false; } - mpz_t val; - mpz_init(val); - Type* vt; - if (!this->length_->integer_constant_value(true, val, &vt)) + Numeric_constant nc; + if (!this->length_->numeric_constant_value(&nc)) { - mpfr_t fval; - mpfr_init(fval); - if (!this->length_->float_constant_value(fval, &vt)) - { - if (this->length_->type()->integer_type() != NULL - || this->length_->type()->float_type() != NULL) - error_at(this->length_->location(), - "array bound is not constant"); - else - error_at(this->length_->location(), - "array bound is not numeric"); - mpfr_clear(fval); - mpz_clear(val); - return false; - } - if (!mpfr_integer_p(fval)) - { - error_at(this->length_->location(), - "array bound truncated to integer"); - mpfr_clear(fval); - mpz_clear(val); - return false; - } - mpz_init(val); - mpfr_get_z(val, fval, GMP_RNDN); - mpfr_clear(fval); + if (this->length_->type()->integer_type() != NULL + || this->length_->type()->float_type() != NULL) + error_at(this->length_->location(), "array bound is not constant"); + else + error_at(this->length_->location(), "array bound is not numeric"); + return false; } - if (mpz_sgn(val) < 0) + unsigned long val; + switch (nc.to_unsigned_long(&val)) { + case Numeric_constant::NC_UL_VALID: + break; + case Numeric_constant::NC_UL_NOTINT: + error_at(this->length_->location(), "array bound truncated to integer"); + return false; + case Numeric_constant::NC_UL_NEGATIVE: error_at(this->length_->location(), "negative array bound"); - mpz_clear(val); return false; + case Numeric_constant::NC_UL_BIG: + error_at(this->length_->location(), "array bound overflows"); + return false; + default: + go_unreachable(); } Type* int_type = Type::lookup_integer_type("int"); - int tbits = int_type->integer_type()->bits(); - int vbits = mpz_sizeinbase(val, 2); - if (vbits + 1 > tbits) + unsigned int tbits = int_type->integer_type()->bits(); + if (sizeof(val) <= tbits * 8 + && val >> (tbits - 1) != 0) { error_at(this->length_->location(), "array bound overflows"); - mpz_clear(val); return false; } - mpz_clear(val); - return true; } @@ -5457,11 +5446,11 @@ Array_type::get_length_tree(Gogo* gogo) go_assert(this->length_ != NULL); if (this->length_tree_ == NULL_TREE) { + Numeric_constant nc; mpz_t val; - mpz_init(val); - Type* t; - if (this->length_->integer_constant_value(true, val, &t)) + if (this->length_->numeric_constant_value(&nc) && nc.to_int(&val)) { + Type* t = nc.type(); if (t == NULL) t = Type::lookup_integer_type("int"); else if (t->is_abstract()) @@ -5472,8 +5461,6 @@ Array_type::get_length_tree(Gogo* gogo) } else { - mpz_clear(val); - // Make up a translation context for the array length // expression. FIXME: This won't work in general. Translate_context context(gogo, NULL, NULL, NULL); @@ -5824,23 +5811,17 @@ Array_type::do_reflection(Gogo* gogo, std::string* ret) const ret->push_back('['); if (this->length_ != NULL) { - mpz_t val; - mpz_init(val); - Type* type; - if (!this->length_->integer_constant_value(true, val, &type)) - error_at(this->length_->location(), - "array length must be integer constant expression"); - else if (mpz_cmp_si(val, 0) < 0) - error_at(this->length_->location(), "array length is negative"); - else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0) - error_at(this->length_->location(), "array length is too large"); + Numeric_constant nc; + unsigned long val; + if (!this->length_->numeric_constant_value(&nc) + || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) + error_at(this->length_->location(), "invalid array length"); else { char buf[50]; - snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val)); + snprintf(buf, sizeof buf, "%lu", val); ret->append(buf); } - mpz_clear(val); } ret->push_back(']'); @@ -5856,23 +5837,17 @@ Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const this->append_mangled_name(this->element_type_, gogo, ret); if (this->length_ != NULL) { - mpz_t val; - mpz_init(val); - Type* type; - if (!this->length_->integer_constant_value(true, val, &type)) - error_at(this->length_->location(), - "array length must be integer constant expression"); - else if (mpz_cmp_si(val, 0) < 0) - error_at(this->length_->location(), "array length is negative"); - else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0) - error_at(this->length_->location(), "array size is too large"); + Numeric_constant nc; + unsigned long val; + if (!this->length_->numeric_constant_value(&nc) + || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) + error_at(this->length_->location(), "invalid array length"); else { char buf[50]; - snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val)); + snprintf(buf, sizeof buf, "%lu", val); ret->append(buf); } - mpz_clear(val); } ret->push_back('e'); } diff --git a/gcc/go/gofrontend/types.h b/gcc/go/gofrontend/types.h index 3fe8048..edc46b7 100644 --- a/gcc/go/gofrontend/types.h +++ b/gcc/go/gofrontend/types.h @@ -680,6 +680,14 @@ class Type complex_type() const { return this->convert(); } + // Return whether this is a numeric type. + bool + is_numeric_type() const + { + Type_classification tc = this->base()->classification_; + return tc == TYPE_INTEGER || tc == TYPE_FLOAT || tc == TYPE_COMPLEX; + } + // Return true if this is a boolean type. bool is_boolean_type() const