1 // expressions.cc -- Go frontend expression handling.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #ifndef ENABLE_BUILD_WITH_CXX
20 #include "tree-iterator.h"
25 #ifndef ENABLE_BUILD_WITH_CXX
34 #include "statements.h"
36 #include "expressions.h"
40 Expression::Expression(Expression_classification classification,
41 source_location location)
42 : classification_(classification), location_(location)
46 Expression::~Expression()
50 // If this expression has a constant integer value, return it.
53 Expression::integer_constant_value(bool iota_is_constant, mpz_t val,
57 return this->do_integer_constant_value(iota_is_constant, val, ptype);
60 // If this expression has a constant floating point value, return it.
63 Expression::float_constant_value(mpfr_t val, Type** ptype) const
66 if (this->do_float_constant_value(val, ptype))
72 if (!this->do_integer_constant_value(false, ival, &t))
76 mpfr_set_z(val, ival, GMP_RNDN);
83 // If this expression has a constant complex value, return it.
86 Expression::complex_constant_value(mpfr_t real, mpfr_t imag,
90 if (this->do_complex_constant_value(real, imag, ptype))
93 if (this->float_constant_value(real, &t))
95 mpfr_set_ui(imag, 0, GMP_RNDN);
101 // Traverse the expressions.
104 Expression::traverse(Expression** pexpr, Traverse* traverse)
106 Expression* expr = *pexpr;
107 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
109 int t = traverse->expression(pexpr);
110 if (t == TRAVERSE_EXIT)
111 return TRAVERSE_EXIT;
112 else if (t == TRAVERSE_SKIP_COMPONENTS)
113 return TRAVERSE_CONTINUE;
115 return expr->do_traverse(traverse);
118 // Traverse subexpressions of this expression.
121 Expression::traverse_subexpressions(Traverse* traverse)
123 return this->do_traverse(traverse);
126 // Default implementation for do_traverse for child classes.
129 Expression::do_traverse(Traverse*)
131 return TRAVERSE_CONTINUE;
134 // This virtual function is called by the parser if the value of this
135 // expression is being discarded. By default, we warn. Expressions
136 // with side effects override.
139 Expression::do_discarding_value()
141 this->warn_about_unused_value();
144 // This virtual function is called to export expressions. This will
145 // only be used by expressions which may be constant.
148 Expression::do_export(Export*) const
153 // Warn that the value of the expression is not used.
156 Expression::warn_about_unused_value()
158 warning_at(this->location(), OPT_Wunused_value, "value computed is not used");
161 // Note that this expression is an error. This is called by children
162 // when they discover an error.
165 Expression::set_is_error()
167 this->classification_ = EXPRESSION_ERROR;
170 // For children to call to report an error conveniently.
173 Expression::report_error(const char* msg)
175 error_at(this->location_, "%s", msg);
176 this->set_is_error();
179 // Set types of variables and constants. This is implemented by the
183 Expression::determine_type(const Type_context* context)
185 this->do_determine_type(context);
188 // Set types when there is no context.
191 Expression::determine_type_no_context()
193 Type_context context;
194 this->do_determine_type(&context);
197 // Return a tree handling any conversions which must be done during
201 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
202 Type* rhs_type, tree rhs_tree,
203 source_location location)
205 if (lhs_type == rhs_type)
208 if (lhs_type->is_error_type() || rhs_type->is_error_type())
209 return error_mark_node;
211 if (lhs_type->is_undefined() || rhs_type->is_undefined())
213 // Make sure we report the error.
216 return error_mark_node;
219 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
220 return error_mark_node;
222 Gogo* gogo = context->gogo();
224 tree lhs_type_tree = lhs_type->get_tree(gogo);
225 if (lhs_type_tree == error_mark_node)
226 return error_mark_node;
228 if (lhs_type->interface_type() != NULL)
230 if (rhs_type->interface_type() == NULL)
231 return Expression::convert_type_to_interface(context, lhs_type,
235 return Expression::convert_interface_to_interface(context, lhs_type,
239 else if (rhs_type->interface_type() != NULL)
240 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
242 else if (lhs_type->is_open_array_type()
243 && rhs_type->is_nil_type())
245 // Assigning nil to an open array.
246 gcc_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
248 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
250 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
251 tree field = TYPE_FIELDS(lhs_type_tree);
252 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
255 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
257 elt = VEC_quick_push(constructor_elt, init, NULL);
258 field = DECL_CHAIN(field);
259 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
262 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
264 elt = VEC_quick_push(constructor_elt, init, NULL);
265 field = DECL_CHAIN(field);
266 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
269 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
271 tree val = build_constructor(lhs_type_tree, init);
272 TREE_CONSTANT(val) = 1;
276 else if (rhs_type->is_nil_type())
278 // The left hand side should be a pointer type at the tree
280 gcc_assert(POINTER_TYPE_P(lhs_type_tree));
281 return fold_convert(lhs_type_tree, null_pointer_node);
283 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
285 // No conversion is needed.
288 else if (POINTER_TYPE_P(lhs_type_tree)
289 || INTEGRAL_TYPE_P(lhs_type_tree)
290 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
291 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
292 return fold_convert_loc(location, lhs_type_tree, rhs_tree);
293 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
294 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
296 // This conversion must be permitted by Go, or we wouldn't have
298 gcc_assert(int_size_in_bytes(lhs_type_tree)
299 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
300 return fold_build1_loc(location, VIEW_CONVERT_EXPR, lhs_type_tree,
305 gcc_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
310 // Return a tree for a conversion from a non-interface type to an
314 Expression::convert_type_to_interface(Translate_context* context,
315 Type* lhs_type, Type* rhs_type,
316 tree rhs_tree, source_location location)
318 Gogo* gogo = context->gogo();
319 Interface_type* lhs_interface_type = lhs_type->interface_type();
320 bool lhs_is_empty = lhs_interface_type->is_empty();
322 // Since RHS_TYPE is a static type, we can create the interface
323 // method table at compile time.
325 // When setting an interface to nil, we just set both fields to
327 if (rhs_type->is_nil_type())
328 return lhs_type->get_init_tree(gogo, false);
330 // This should have been checked already.
331 gcc_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
333 tree lhs_type_tree = lhs_type->get_tree(gogo);
334 if (lhs_type_tree == error_mark_node)
335 return error_mark_node;
337 // An interface is a tuple. If LHS_TYPE is an empty interface type,
338 // then the first field is the type descriptor for RHS_TYPE.
339 // Otherwise it is the interface method table for RHS_TYPE.
340 tree first_field_value;
342 first_field_value = rhs_type->type_descriptor_pointer(gogo);
345 // Build the interface method table for this interface and this
346 // object type: a list of function pointers for each interface
348 Named_type* rhs_named_type = rhs_type->named_type();
349 bool is_pointer = false;
350 if (rhs_named_type == NULL)
352 rhs_named_type = rhs_type->deref()->named_type();
356 if (rhs_named_type == NULL)
357 method_table = null_pointer_node;
360 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
362 first_field_value = fold_convert_loc(location, const_ptr_type_node,
366 // Start building a constructor for the value we will return.
368 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
370 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
371 tree field = TYPE_FIELDS(lhs_type_tree);
372 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
373 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
375 elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value);
377 elt = VEC_quick_push(constructor_elt, init, NULL);
378 field = DECL_CHAIN(field);
379 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
382 if (rhs_type->points_to() != NULL)
384 // We are assigning a pointer to the interface; the interface
385 // holds the pointer itself.
386 elt->value = rhs_tree;
387 return build_constructor(lhs_type_tree, init);
390 // We are assigning a non-pointer value to the interface; the
391 // interface gets a copy of the value in the heap.
393 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
395 tree space = gogo->allocate_memory(rhs_type, object_size, location);
396 space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)),
398 space = save_expr(space);
400 tree ref = build_fold_indirect_ref_loc(location, space);
401 TREE_THIS_NOTRAP(ref) = 1;
402 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
405 elt->value = fold_convert_loc(location, TREE_TYPE(field), space);
407 return build2(COMPOUND_EXPR, lhs_type_tree, set,
408 build_constructor(lhs_type_tree, init));
411 // Return a tree for the type descriptor of RHS_TREE, which has
412 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
416 Expression::get_interface_type_descriptor(Translate_context*,
417 Type* rhs_type, tree rhs_tree,
418 source_location location)
420 tree rhs_type_tree = TREE_TYPE(rhs_tree);
421 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
422 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
423 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
425 if (rhs_type->interface_type()->is_empty())
427 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
428 "__type_descriptor") == 0);
432 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
434 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v)));
436 tree v1 = build_fold_indirect_ref_loc(location, v);
437 gcc_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
438 tree f = TYPE_FIELDS(TREE_TYPE(v1));
439 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
441 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
443 tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v,
444 fold_convert_loc(location, TREE_TYPE(v),
446 tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node);
447 return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1),
451 // Return a tree for the conversion of an interface type to an
455 Expression::convert_interface_to_interface(Translate_context* context,
456 Type *lhs_type, Type *rhs_type,
457 tree rhs_tree, bool for_type_guard,
458 source_location location)
460 Gogo* gogo = context->gogo();
461 Interface_type* lhs_interface_type = lhs_type->interface_type();
462 bool lhs_is_empty = lhs_interface_type->is_empty();
464 tree lhs_type_tree = lhs_type->get_tree(gogo);
465 if (lhs_type_tree == error_mark_node)
466 return error_mark_node;
468 // In the general case this requires runtime examination of the type
469 // method table to match it up with the interface methods.
471 // FIXME: If all of the methods in the right hand side interface
472 // also appear in the left hand side interface, then we don't need
473 // to do a runtime check, although we still need to build a new
476 // Get the type descriptor for the right hand side. This will be
477 // NULL for a nil interface.
479 if (!DECL_P(rhs_tree))
480 rhs_tree = save_expr(rhs_tree);
482 tree rhs_type_descriptor =
483 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
486 // The result is going to be a two element constructor.
488 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
490 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
491 tree field = TYPE_FIELDS(lhs_type_tree);
496 // A type assertion fails when converting a nil interface.
497 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
498 static tree assert_interface_decl;
499 tree call = Gogo::call_builtin(&assert_interface_decl,
501 "__go_assert_interface",
504 TREE_TYPE(lhs_type_descriptor),
506 TREE_TYPE(rhs_type_descriptor),
507 rhs_type_descriptor);
508 // This will panic if the interface conversion fails.
509 TREE_NOTHROW(assert_interface_decl) = 0;
510 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
512 else if (lhs_is_empty)
514 // A convertion to an empty interface always succeeds, and the
515 // first field is just the type descriptor of the object.
516 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
517 "__type_descriptor") == 0);
518 gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
519 elt->value = rhs_type_descriptor;
523 // A conversion to a non-empty interface may fail, but unlike a
524 // type assertion converting nil will always succeed.
525 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
527 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
528 static tree convert_interface_decl;
529 tree call = Gogo::call_builtin(&convert_interface_decl,
531 "__go_convert_interface",
534 TREE_TYPE(lhs_type_descriptor),
536 TREE_TYPE(rhs_type_descriptor),
537 rhs_type_descriptor);
538 // This will panic if the interface conversion fails.
539 TREE_NOTHROW(convert_interface_decl) = 0;
540 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
543 // The second field is simply the object pointer.
545 elt = VEC_quick_push(constructor_elt, init, NULL);
546 field = DECL_CHAIN(field);
547 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
550 tree rhs_type_tree = TREE_TYPE(rhs_tree);
551 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
552 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
553 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
554 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
557 return build_constructor(lhs_type_tree, init);
560 // Return a tree for the conversion of an interface type to a
561 // non-interface type.
564 Expression::convert_interface_to_type(Translate_context* context,
565 Type *lhs_type, Type* rhs_type,
566 tree rhs_tree, source_location location)
568 Gogo* gogo = context->gogo();
569 tree rhs_type_tree = TREE_TYPE(rhs_tree);
571 tree lhs_type_tree = lhs_type->get_tree(gogo);
572 if (lhs_type_tree == error_mark_node)
573 return error_mark_node;
575 // Call a function to check that the type is valid. The function
576 // will panic with an appropriate runtime type error if the type is
579 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
581 if (!DECL_P(rhs_tree))
582 rhs_tree = save_expr(rhs_tree);
584 tree rhs_type_descriptor =
585 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
588 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo);
590 static tree check_interface_type_decl;
591 tree call = Gogo::call_builtin(&check_interface_type_decl,
593 "__go_check_interface_type",
596 TREE_TYPE(lhs_type_descriptor),
598 TREE_TYPE(rhs_type_descriptor),
600 TREE_TYPE(rhs_inter_descriptor),
601 rhs_inter_descriptor);
602 // This call will panic if the conversion is invalid.
603 TREE_NOTHROW(check_interface_type_decl) = 0;
605 // If the call succeeds, pull out the value.
606 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
607 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
608 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
609 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
612 // If the value is a pointer, then it is the value we want.
613 // Otherwise it points to the value.
614 if (lhs_type->points_to() == NULL)
616 val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val);
617 val = build_fold_indirect_ref_loc(location, val);
620 return build2(COMPOUND_EXPR, lhs_type_tree, call,
621 fold_convert_loc(location, lhs_type_tree, val));
624 // Convert an expression to a tree. This is implemented by the child
625 // class. Not that it is not in general safe to call this multiple
626 // times for a single expression, but that we don't catch such errors.
629 Expression::get_tree(Translate_context* context)
631 // The child may have marked this expression as having an error.
632 if (this->classification_ == EXPRESSION_ERROR)
633 return error_mark_node;
635 return this->do_get_tree(context);
638 // Return a tree for VAL in TYPE.
641 Expression::integer_constant_tree(mpz_t val, tree type)
643 if (type == error_mark_node)
644 return error_mark_node;
645 else if (TREE_CODE(type) == INTEGER_TYPE)
646 return double_int_to_tree(type,
647 mpz_get_double_int(type, val, true));
648 else if (TREE_CODE(type) == REAL_TYPE)
651 mpfr_init_set_z(fval, val, GMP_RNDN);
652 tree ret = Expression::float_constant_tree(fval, type);
656 else if (TREE_CODE(type) == COMPLEX_TYPE)
659 mpfr_init_set_z(fval, val, GMP_RNDN);
660 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
662 tree imag = build_real_from_int_cst(TREE_TYPE(type),
664 return build_complex(type, real, imag);
670 // Return a tree for VAL in TYPE.
673 Expression::float_constant_tree(mpfr_t val, tree type)
675 if (type == error_mark_node)
676 return error_mark_node;
677 else if (TREE_CODE(type) == INTEGER_TYPE)
681 mpfr_get_z(ival, val, GMP_RNDN);
682 tree ret = Expression::integer_constant_tree(ival, type);
686 else if (TREE_CODE(type) == REAL_TYPE)
689 real_from_mpfr(&r1, val, type, GMP_RNDN);
691 real_convert(&r2, TYPE_MODE(type), &r1);
692 return build_real(type, r2);
694 else if (TREE_CODE(type) == COMPLEX_TYPE)
697 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
699 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
700 tree imag = build_real_from_int_cst(TREE_TYPE(type),
702 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
708 // Return a tree for REAL/IMAG in TYPE.
711 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
713 if (TREE_CODE(type) == COMPLEX_TYPE)
716 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
718 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
721 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
723 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
725 return build_complex(type, build_real(TREE_TYPE(type), r2),
726 build_real(TREE_TYPE(type), r4));
732 // Return a tree which evaluates to true if VAL, of arbitrary integer
733 // type, is negative or is more than the maximum value of BOUND_TYPE.
734 // If SOFAR is not NULL, it is or'red into the result. The return
735 // value may be NULL if SOFAR is NULL.
738 Expression::check_bounds(tree val, tree bound_type, tree sofar,
741 tree val_type = TREE_TYPE(val);
742 tree ret = NULL_TREE;
744 if (!TYPE_UNSIGNED(val_type))
746 ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val,
747 build_int_cst(val_type, 0));
748 if (ret == boolean_false_node)
752 if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type))
753 || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type))
755 tree max = TYPE_MAX_VALUE(bound_type);
756 tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val,
757 fold_convert_loc(loc, val_type, max));
758 if (big == boolean_false_node)
760 else if (ret == NULL_TREE)
763 ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
767 if (ret == NULL_TREE)
769 else if (sofar == NULL_TREE)
772 return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
776 // Error expressions. This are used to avoid cascading errors.
778 class Error_expression : public Expression
781 Error_expression(source_location location)
782 : Expression(EXPRESSION_ERROR, location)
787 do_is_constant() const
791 do_integer_constant_value(bool, mpz_t val, Type**) const
798 do_float_constant_value(mpfr_t val, Type**) const
800 mpfr_set_ui(val, 0, GMP_RNDN);
805 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
807 mpfr_set_ui(real, 0, GMP_RNDN);
808 mpfr_set_ui(imag, 0, GMP_RNDN);
813 do_discarding_value()
818 { return Type::make_error_type(); }
821 do_determine_type(const Type_context*)
829 do_is_addressable() const
833 do_get_tree(Translate_context*)
834 { return error_mark_node; }
838 Expression::make_error(source_location location)
840 return new Error_expression(location);
843 // An expression which is really a type. This is used during parsing.
844 // It is an error if these survive after lowering.
847 Type_expression : public Expression
850 Type_expression(Type* type, source_location location)
851 : Expression(EXPRESSION_TYPE, location),
857 do_traverse(Traverse* traverse)
858 { return Type::traverse(this->type_, traverse); }
862 { return this->type_; }
865 do_determine_type(const Type_context*)
869 do_check_types(Gogo*)
870 { this->report_error(_("invalid use of type")); }
877 do_get_tree(Translate_context*)
878 { gcc_unreachable(); }
881 // The type which we are representing as an expression.
886 Expression::make_type(Type* type, source_location location)
888 return new Type_expression(type, location);
891 // Class Var_expression.
893 // Lower a variable expression. Here we just make sure that the
894 // initialization expression of the variable has been lowered. This
895 // ensures that we will be able to determine the type of the variable
899 Var_expression::do_lower(Gogo* gogo, Named_object* function, int)
901 if (this->variable_->is_variable())
903 Variable* var = this->variable_->var_value();
904 // This is either a local variable or a global variable. A
905 // reference to a variable which is local to an enclosing
906 // function will be a reference to a field in a closure.
907 if (var->is_global())
909 var->lower_init_expression(gogo, function);
914 // Return the name of the variable.
917 Var_expression::name() const
919 return this->variable_->name();
922 // Return the type of a reference to a variable.
925 Var_expression::do_type()
927 if (this->variable_->is_variable())
928 return this->variable_->var_value()->type();
929 else if (this->variable_->is_result_variable())
930 return this->variable_->result_var_value()->type();
935 // Something takes the address of this variable. This means that we
936 // may want to move the variable onto the heap.
939 Var_expression::do_address_taken(bool escapes)
943 else if (this->variable_->is_variable())
944 this->variable_->var_value()->set_address_taken();
945 else if (this->variable_->is_result_variable())
946 this->variable_->result_var_value()->set_address_taken();
951 // Get the tree for a reference to a variable.
954 Var_expression::do_get_tree(Translate_context* context)
956 return this->variable_->get_tree(context->gogo(), context->function());
959 // Make a reference to a variable in an expression.
962 Expression::make_var_reference(Named_object* var, source_location location)
965 return Expression::make_sink(location);
967 // FIXME: Creating a new object for each reference to a variable is
969 return new Var_expression(var, location);
972 // Class Temporary_reference_expression.
977 Temporary_reference_expression::do_type()
979 return this->statement_->type();
982 // Called if something takes the address of this temporary variable.
983 // We never have to move temporary variables to the heap, but we do
984 // need to know that they must live in the stack rather than in a
988 Temporary_reference_expression::do_address_taken(bool)
990 this->statement_->set_is_address_taken();
993 // Get a tree referring to the variable.
996 Temporary_reference_expression::do_get_tree(Translate_context*)
998 return this->statement_->get_decl();
1001 // Make a reference to a temporary variable.
1004 Expression::make_temporary_reference(Temporary_statement* statement,
1005 source_location location)
1007 return new Temporary_reference_expression(statement, location);
1010 // A sink expression--a use of the blank identifier _.
1012 class Sink_expression : public Expression
1015 Sink_expression(source_location location)
1016 : Expression(EXPRESSION_SINK, location),
1017 type_(NULL), var_(NULL_TREE)
1022 do_discarding_value()
1029 do_determine_type(const Type_context*);
1033 { return new Sink_expression(this->location()); }
1036 do_get_tree(Translate_context*);
1039 // The type of this sink variable.
1041 // The temporary variable we generate.
1045 // Return the type of a sink expression.
1048 Sink_expression::do_type()
1050 if (this->type_ == NULL)
1051 return Type::make_sink_type();
1055 // Determine the type of a sink expression.
1058 Sink_expression::do_determine_type(const Type_context* context)
1060 if (context->type != NULL)
1061 this->type_ = context->type;
1064 // Return a temporary variable for a sink expression. This will
1065 // presumably be a write-only variable which the middle-end will drop.
1068 Sink_expression::do_get_tree(Translate_context* context)
1070 if (this->var_ == NULL_TREE)
1072 gcc_assert(this->type_ != NULL && !this->type_->is_sink_type());
1073 this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()),
1079 // Make a sink expression.
1082 Expression::make_sink(source_location location)
1084 return new Sink_expression(location);
1087 // Class Func_expression.
1089 // FIXME: Can a function expression appear in a constant expression?
1090 // The value is unchanging. Initializing a constant to the address of
1091 // a function seems like it could work, though there might be little
1094 // Return the name of the function.
1097 Func_expression::name() const
1099 return this->function_->name();
1105 Func_expression::do_traverse(Traverse* traverse)
1107 return (this->closure_ == NULL
1109 : Expression::traverse(&this->closure_, traverse));
1112 // Return the type of a function expression.
1115 Func_expression::do_type()
1117 if (this->function_->is_function())
1118 return this->function_->func_value()->type();
1119 else if (this->function_->is_function_declaration())
1120 return this->function_->func_declaration_value()->type();
1125 // Get the tree for a function expression without evaluating the
1129 Func_expression::get_tree_without_closure(Gogo* gogo)
1131 Function_type* fntype;
1132 if (this->function_->is_function())
1133 fntype = this->function_->func_value()->type();
1134 else if (this->function_->is_function_declaration())
1135 fntype = this->function_->func_declaration_value()->type();
1139 // Builtin functions are handled specially by Call_expression. We
1140 // can't take their address.
1141 if (fntype->is_builtin())
1143 error_at(this->location(), "invalid use of special builtin function %qs",
1144 this->function_->name().c_str());
1145 return error_mark_node;
1148 Named_object* no = this->function_;
1150 tree id = no->get_id(gogo);
1151 if (id == error_mark_node)
1152 return error_mark_node;
1155 if (no->is_function())
1156 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1157 else if (no->is_function_declaration())
1158 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1162 if (fndecl == error_mark_node)
1163 return error_mark_node;
1165 return build_fold_addr_expr_loc(this->location(), fndecl);
1168 // Get the tree for a function expression. This is used when we take
1169 // the address of a function rather than simply calling it. If the
1170 // function has a closure, we must use a trampoline.
1173 Func_expression::do_get_tree(Translate_context* context)
1175 Gogo* gogo = context->gogo();
1177 tree fnaddr = this->get_tree_without_closure(gogo);
1178 if (fnaddr == error_mark_node)
1179 return error_mark_node;
1181 gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1182 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1183 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1185 // For a normal non-nested function call, that is all we have to do.
1186 if (!this->function_->is_function()
1187 || this->function_->func_value()->enclosing() == NULL)
1189 gcc_assert(this->closure_ == NULL);
1193 // For a nested function call, we have to always allocate a
1194 // trampoline. If we don't always allocate, then closures will not
1195 // be reliably distinct.
1196 Expression* closure = this->closure_;
1198 if (closure == NULL)
1199 closure_tree = null_pointer_node;
1202 // Get the value of the closure. This will be a pointer to
1203 // space allocated on the heap.
1204 closure_tree = closure->get_tree(context);
1205 if (closure_tree == error_mark_node)
1206 return error_mark_node;
1207 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1210 // Now we need to build some code on the heap. This code will load
1211 // the static chain pointer with the closure and then jump to the
1212 // body of the function. The normal gcc approach is to build the
1213 // code on the stack. Unfortunately we can not do that, as Go
1214 // permits us to return the function pointer.
1216 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1219 // Make a reference to a function in an expression.
1222 Expression::make_func_reference(Named_object* function, Expression* closure,
1223 source_location location)
1225 return new Func_expression(function, closure, location);
1228 // Class Unknown_expression.
1230 // Return the name of an unknown expression.
1233 Unknown_expression::name() const
1235 return this->named_object_->name();
1238 // Lower a reference to an unknown name.
1241 Unknown_expression::do_lower(Gogo*, Named_object*, int)
1243 source_location location = this->location();
1244 Named_object* no = this->named_object_;
1245 Named_object* real = no->unknown_value()->real_named_object();
1248 if (this->is_composite_literal_key_)
1250 error_at(location, "reference to undefined name %qs",
1251 this->named_object_->message_name().c_str());
1252 return Expression::make_error(location);
1254 switch (real->classification())
1256 case Named_object::NAMED_OBJECT_CONST:
1257 return Expression::make_const_reference(real, location);
1258 case Named_object::NAMED_OBJECT_TYPE:
1259 return Expression::make_type(real->type_value(), location);
1260 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1261 if (this->is_composite_literal_key_)
1263 error_at(location, "reference to undefined type %qs",
1264 real->message_name().c_str());
1265 return Expression::make_error(location);
1266 case Named_object::NAMED_OBJECT_VAR:
1267 return Expression::make_var_reference(real, location);
1268 case Named_object::NAMED_OBJECT_FUNC:
1269 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1270 return Expression::make_func_reference(real, NULL, location);
1271 case Named_object::NAMED_OBJECT_PACKAGE:
1272 if (this->is_composite_literal_key_)
1274 error_at(location, "unexpected reference to package");
1275 return Expression::make_error(location);
1281 // Make a reference to an unknown name.
1284 Expression::make_unknown_reference(Named_object* no, source_location location)
1286 gcc_assert(no->resolve()->is_unknown());
1287 return new Unknown_expression(no, location);
1290 // A boolean expression.
1292 class Boolean_expression : public Expression
1295 Boolean_expression(bool val, source_location location)
1296 : Expression(EXPRESSION_BOOLEAN, location),
1297 val_(val), type_(NULL)
1305 do_is_constant() const
1312 do_determine_type(const Type_context*);
1319 do_get_tree(Translate_context*)
1320 { return this->val_ ? boolean_true_node : boolean_false_node; }
1323 do_export(Export* exp) const
1324 { exp->write_c_string(this->val_ ? "true" : "false"); }
1329 // The type as determined by context.
1336 Boolean_expression::do_type()
1338 if (this->type_ == NULL)
1339 this->type_ = Type::make_boolean_type();
1343 // Set the type from the context.
1346 Boolean_expression::do_determine_type(const Type_context* context)
1348 if (this->type_ != NULL && !this->type_->is_abstract())
1350 else if (context->type != NULL && context->type->is_boolean_type())
1351 this->type_ = context->type;
1352 else if (!context->may_be_abstract)
1353 this->type_ = Type::lookup_bool_type();
1356 // Import a boolean constant.
1359 Boolean_expression::do_import(Import* imp)
1361 if (imp->peek_char() == 't')
1363 imp->require_c_string("true");
1364 return Expression::make_boolean(true, imp->location());
1368 imp->require_c_string("false");
1369 return Expression::make_boolean(false, imp->location());
1373 // Make a boolean expression.
1376 Expression::make_boolean(bool val, source_location location)
1378 return new Boolean_expression(val, location);
1381 // Class String_expression.
1386 String_expression::do_type()
1388 if (this->type_ == NULL)
1389 this->type_ = Type::make_string_type();
1393 // Set the type from the context.
1396 String_expression::do_determine_type(const Type_context* context)
1398 if (this->type_ != NULL && !this->type_->is_abstract())
1400 else if (context->type != NULL && context->type->is_string_type())
1401 this->type_ = context->type;
1402 else if (!context->may_be_abstract)
1403 this->type_ = Type::lookup_string_type();
1406 // Build a string constant.
1409 String_expression::do_get_tree(Translate_context* context)
1411 return context->gogo()->go_string_constant_tree(this->val_);
1414 // Export a string expression.
1417 String_expression::do_export(Export* exp) const
1420 s.reserve(this->val_.length() * 4 + 2);
1422 for (std::string::const_iterator p = this->val_.begin();
1423 p != this->val_.end();
1426 if (*p == '\\' || *p == '"')
1431 else if (*p >= 0x20 && *p < 0x7f)
1433 else if (*p == '\n')
1435 else if (*p == '\t')
1440 unsigned char c = *p;
1441 unsigned int dig = c >> 4;
1442 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1444 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1448 exp->write_string(s);
1451 // Import a string expression.
1454 String_expression::do_import(Import* imp)
1456 imp->require_c_string("\"");
1460 int c = imp->get_char();
1461 if (c == '"' || c == -1)
1464 val += static_cast<char>(c);
1467 c = imp->get_char();
1468 if (c == '\\' || c == '"')
1469 val += static_cast<char>(c);
1476 c = imp->get_char();
1477 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1478 c = imp->get_char();
1479 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1480 char v = (vh << 4) | vl;
1485 error_at(imp->location(), "bad string constant");
1486 return Expression::make_error(imp->location());
1490 return Expression::make_string(val, imp->location());
1493 // Make a string expression.
1496 Expression::make_string(const std::string& val, source_location location)
1498 return new String_expression(val, location);
1501 // Make an integer expression.
1503 class Integer_expression : public Expression
1506 Integer_expression(const mpz_t* val, Type* type, source_location location)
1507 : Expression(EXPRESSION_INTEGER, location),
1509 { mpz_init_set(this->val_, *val); }
1514 // Return whether VAL fits in the type.
1516 check_constant(mpz_t val, Type*, source_location);
1518 // Write VAL to export data.
1520 export_integer(Export* exp, const mpz_t val);
1524 do_is_constant() const
1528 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1534 do_determine_type(const Type_context* context);
1537 do_check_types(Gogo*);
1540 do_get_tree(Translate_context*);
1544 { return Expression::make_integer(&this->val_, this->type_,
1545 this->location()); }
1548 do_export(Export*) const;
1551 // The integer value.
1557 // Return an integer constant value.
1560 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1563 if (this->type_ != NULL)
1564 *ptype = this->type_;
1565 mpz_set(val, this->val_);
1569 // Return the current type. If we haven't set the type yet, we return
1570 // an abstract integer type.
1573 Integer_expression::do_type()
1575 if (this->type_ == NULL)
1576 this->type_ = Type::make_abstract_integer_type();
1580 // Set the type of the integer value. Here we may switch from an
1581 // abstract type to a real type.
1584 Integer_expression::do_determine_type(const Type_context* context)
1586 if (this->type_ != NULL && !this->type_->is_abstract())
1588 else if (context->type != NULL
1589 && (context->type->integer_type() != NULL
1590 || context->type->float_type() != NULL
1591 || context->type->complex_type() != NULL))
1592 this->type_ = context->type;
1593 else if (!context->may_be_abstract)
1594 this->type_ = Type::lookup_integer_type("int");
1597 // Return true if the integer VAL fits in the range of the type TYPE.
1598 // Otherwise give an error and return false. TYPE may be NULL.
1601 Integer_expression::check_constant(mpz_t val, Type* type,
1602 source_location location)
1606 Integer_type* itype = type->integer_type();
1607 if (itype == NULL || itype->is_abstract())
1610 int bits = mpz_sizeinbase(val, 2);
1612 if (itype->is_unsigned())
1614 // For an unsigned type we can only accept a nonnegative number,
1615 // and we must be able to represent at least BITS.
1616 if (mpz_sgn(val) >= 0
1617 && bits <= itype->bits())
1622 // For a signed type we need an extra bit to indicate the sign.
1623 // We have to handle the most negative integer specially.
1624 if (bits + 1 <= itype->bits()
1625 || (bits <= itype->bits()
1627 && (mpz_scan1(val, 0)
1628 == static_cast<unsigned long>(itype->bits() - 1))
1629 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1633 error_at(location, "integer constant overflow");
1637 // Check the type of an integer constant.
1640 Integer_expression::do_check_types(Gogo*)
1642 if (this->type_ == NULL)
1644 if (!Integer_expression::check_constant(this->val_, this->type_,
1646 this->set_is_error();
1649 // Get a tree for an integer constant.
1652 Integer_expression::do_get_tree(Translate_context* context)
1654 Gogo* gogo = context->gogo();
1656 if (this->type_ != NULL && !this->type_->is_abstract())
1657 type = this->type_->get_tree(gogo);
1658 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1660 // We are converting to an abstract floating point type.
1661 type = Type::lookup_float_type("float64")->get_tree(gogo);
1663 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1665 // We are converting to an abstract complex type.
1666 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
1670 // If we still have an abstract type here, then this is being
1671 // used in a constant expression which didn't get reduced for
1672 // some reason. Use a type which will fit the value. We use <,
1673 // not <=, because we need an extra bit for the sign bit.
1674 int bits = mpz_sizeinbase(this->val_, 2);
1675 if (bits < INT_TYPE_SIZE)
1676 type = Type::lookup_integer_type("int")->get_tree(gogo);
1678 type = Type::lookup_integer_type("int64")->get_tree(gogo);
1680 type = long_long_integer_type_node;
1682 return Expression::integer_constant_tree(this->val_, type);
1685 // Write VAL to export data.
1688 Integer_expression::export_integer(Export* exp, const mpz_t val)
1690 char* s = mpz_get_str(NULL, 10, val);
1691 exp->write_c_string(s);
1695 // Export an integer in a constant expression.
1698 Integer_expression::do_export(Export* exp) const
1700 Integer_expression::export_integer(exp, this->val_);
1701 // A trailing space lets us reliably identify the end of the number.
1702 exp->write_c_string(" ");
1705 // Import an integer, floating point, or complex value. This handles
1706 // all these types because they all start with digits.
1709 Integer_expression::do_import(Import* imp)
1711 std::string num = imp->read_identifier();
1712 imp->require_c_string(" ");
1713 if (!num.empty() && num[num.length() - 1] == 'i')
1716 size_t plus_pos = num.find('+', 1);
1717 size_t minus_pos = num.find('-', 1);
1719 if (plus_pos == std::string::npos)
1721 else if (minus_pos == std::string::npos)
1725 error_at(imp->location(), "bad number in import data: %qs",
1727 return Expression::make_error(imp->location());
1729 if (pos == std::string::npos)
1730 mpfr_set_ui(real, 0, GMP_RNDN);
1733 std::string real_str = num.substr(0, pos);
1734 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1736 error_at(imp->location(), "bad number in import data: %qs",
1738 return Expression::make_error(imp->location());
1742 std::string imag_str;
1743 if (pos == std::string::npos)
1746 imag_str = num.substr(pos);
1747 imag_str = imag_str.substr(0, imag_str.size() - 1);
1749 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1751 error_at(imp->location(), "bad number in import data: %qs",
1753 return Expression::make_error(imp->location());
1755 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1761 else if (num.find('.') == std::string::npos
1762 && num.find('E') == std::string::npos)
1765 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1767 error_at(imp->location(), "bad number in import data: %qs",
1769 return Expression::make_error(imp->location());
1771 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1778 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1780 error_at(imp->location(), "bad number in import data: %qs",
1782 return Expression::make_error(imp->location());
1784 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1790 // Build a new integer value.
1793 Expression::make_integer(const mpz_t* val, Type* type,
1794 source_location location)
1796 return new Integer_expression(val, type, location);
1801 class Float_expression : public Expression
1804 Float_expression(const mpfr_t* val, Type* type, source_location location)
1805 : Expression(EXPRESSION_FLOAT, location),
1808 mpfr_init_set(this->val_, *val, GMP_RNDN);
1811 // Constrain VAL to fit into TYPE.
1813 constrain_float(mpfr_t val, Type* type);
1815 // Return whether VAL fits in the type.
1817 check_constant(mpfr_t val, Type*, source_location);
1819 // Write VAL to export data.
1821 export_float(Export* exp, const mpfr_t val);
1825 do_is_constant() const
1829 do_float_constant_value(mpfr_t val, Type**) const;
1835 do_determine_type(const Type_context*);
1838 do_check_types(Gogo*);
1842 { return Expression::make_float(&this->val_, this->type_,
1843 this->location()); }
1846 do_get_tree(Translate_context*);
1849 do_export(Export*) const;
1852 // The floating point value.
1858 // Constrain VAL to fit into TYPE.
1861 Float_expression::constrain_float(mpfr_t val, Type* type)
1863 Float_type* ftype = type->float_type();
1864 if (ftype != NULL && !ftype->is_abstract())
1866 tree type_tree = ftype->type_tree();
1867 REAL_VALUE_TYPE rvt;
1868 real_from_mpfr(&rvt, val, type_tree, GMP_RNDN);
1869 real_convert(&rvt, TYPE_MODE(type_tree), &rvt);
1870 mpfr_from_real(val, &rvt, GMP_RNDN);
1874 // Return a floating point constant value.
1877 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
1879 if (this->type_ != NULL)
1880 *ptype = this->type_;
1881 mpfr_set(val, this->val_, GMP_RNDN);
1885 // Return the current type. If we haven't set the type yet, we return
1886 // an abstract float type.
1889 Float_expression::do_type()
1891 if (this->type_ == NULL)
1892 this->type_ = Type::make_abstract_float_type();
1896 // Set the type of the float value. Here we may switch from an
1897 // abstract type to a real type.
1900 Float_expression::do_determine_type(const Type_context* context)
1902 if (this->type_ != NULL && !this->type_->is_abstract())
1904 else if (context->type != NULL
1905 && (context->type->integer_type() != NULL
1906 || context->type->float_type() != NULL
1907 || context->type->complex_type() != NULL))
1908 this->type_ = context->type;
1909 else if (!context->may_be_abstract)
1910 this->type_ = Type::lookup_float_type("float");
1913 // Return true if the floating point value VAL fits in the range of
1914 // the type TYPE. Otherwise give an error and return false. TYPE may
1918 Float_expression::check_constant(mpfr_t val, Type* type,
1919 source_location location)
1923 Float_type* ftype = type->float_type();
1924 if (ftype == NULL || ftype->is_abstract())
1927 // A NaN or Infinity always fits in the range of the type.
1928 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
1931 mp_exp_t exp = mpfr_get_exp(val);
1933 switch (ftype->bits())
1946 error_at(location, "floating point constant overflow");
1952 // Check the type of a float value.
1955 Float_expression::do_check_types(Gogo*)
1957 if (this->type_ == NULL)
1960 if (!Float_expression::check_constant(this->val_, this->type_,
1962 this->set_is_error();
1964 Integer_type* integer_type = this->type_->integer_type();
1965 if (integer_type != NULL)
1967 if (!mpfr_integer_p(this->val_))
1968 this->report_error(_("floating point constant truncated to integer"));
1971 gcc_assert(!integer_type->is_abstract());
1974 mpfr_get_z(ival, this->val_, GMP_RNDN);
1975 Integer_expression::check_constant(ival, integer_type,
1982 // Get a tree for a float constant.
1985 Float_expression::do_get_tree(Translate_context* context)
1987 Gogo* gogo = context->gogo();
1989 if (this->type_ != NULL && !this->type_->is_abstract())
1990 type = this->type_->get_tree(gogo);
1991 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
1993 // We have an abstract integer type. We just hope for the best.
1994 type = Type::lookup_integer_type("int")->get_tree(gogo);
1998 // If we still have an abstract type here, then this is being
1999 // used in a constant expression which didn't get reduced. We
2000 // just use float64 and hope for the best.
2001 type = Type::lookup_float_type("float64")->get_tree(gogo);
2003 return Expression::float_constant_tree(this->val_, type);
2006 // Write a floating point number to export data.
2009 Float_expression::export_float(Export *exp, const mpfr_t val)
2012 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2014 exp->write_c_string("-");
2015 exp->write_c_string("0.");
2016 exp->write_c_string(*s == '-' ? s + 1 : s);
2019 snprintf(buf, sizeof buf, "E%ld", exponent);
2020 exp->write_c_string(buf);
2023 // Export a floating point number in a constant expression.
2026 Float_expression::do_export(Export* exp) const
2028 Float_expression::export_float(exp, this->val_);
2029 // A trailing space lets us reliably identify the end of the number.
2030 exp->write_c_string(" ");
2033 // Make a float expression.
2036 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2038 return new Float_expression(val, type, location);
2043 class Complex_expression : public Expression
2046 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2047 source_location location)
2048 : Expression(EXPRESSION_COMPLEX, location),
2051 mpfr_init_set(this->real_, *real, GMP_RNDN);
2052 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2055 // Constrain REAL/IMAG to fit into TYPE.
2057 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2059 // Return whether REAL/IMAG fits in the type.
2061 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2063 // Write REAL/IMAG to export data.
2065 export_complex(Export* exp, const mpfr_t real, const mpfr_t val);
2069 do_is_constant() const
2073 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2079 do_determine_type(const Type_context*);
2082 do_check_types(Gogo*);
2087 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2092 do_get_tree(Translate_context*);
2095 do_export(Export*) const;
2100 // The imaginary part;
2102 // The type if known.
2106 // Constrain REAL/IMAG to fit into TYPE.
2109 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2111 Complex_type* ctype = type->complex_type();
2112 if (ctype != NULL && !ctype->is_abstract())
2114 tree type_tree = ctype->type_tree();
2116 REAL_VALUE_TYPE rvt;
2117 real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN);
2118 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2119 mpfr_from_real(real, &rvt, GMP_RNDN);
2121 real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN);
2122 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2123 mpfr_from_real(imag, &rvt, GMP_RNDN);
2127 // Return a complex constant value.
2130 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2133 if (this->type_ != NULL)
2134 *ptype = this->type_;
2135 mpfr_set(real, this->real_, GMP_RNDN);
2136 mpfr_set(imag, this->imag_, GMP_RNDN);
2140 // Return the current type. If we haven't set the type yet, we return
2141 // an abstract complex type.
2144 Complex_expression::do_type()
2146 if (this->type_ == NULL)
2147 this->type_ = Type::make_abstract_complex_type();
2151 // Set the type of the complex value. Here we may switch from an
2152 // abstract type to a real type.
2155 Complex_expression::do_determine_type(const Type_context* context)
2157 if (this->type_ != NULL && !this->type_->is_abstract())
2159 else if (context->type != NULL
2160 && context->type->complex_type() != NULL)
2161 this->type_ = context->type;
2162 else if (!context->may_be_abstract)
2163 this->type_ = Type::lookup_complex_type("complex");
2166 // Return true if the complex value REAL/IMAG fits in the range of the
2167 // type TYPE. Otherwise give an error and return false. TYPE may be
2171 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2172 source_location location)
2176 Complex_type* ctype = type->complex_type();
2177 if (ctype == NULL || ctype->is_abstract())
2181 switch (ctype->bits())
2193 // A NaN or Infinity always fits in the range of the type.
2194 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2196 if (mpfr_get_exp(real) > max_exp)
2198 error_at(location, "complex real part constant overflow");
2203 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2205 if (mpfr_get_exp(imag) > max_exp)
2207 error_at(location, "complex imaginary part constant overflow");
2215 // Check the type of a complex value.
2218 Complex_expression::do_check_types(Gogo*)
2220 if (this->type_ == NULL)
2223 if (!Complex_expression::check_constant(this->real_, this->imag_,
2224 this->type_, this->location()))
2225 this->set_is_error();
2228 // Get a tree for a complex constant.
2231 Complex_expression::do_get_tree(Translate_context* context)
2233 Gogo* gogo = context->gogo();
2235 if (this->type_ != NULL && !this->type_->is_abstract())
2236 type = this->type_->get_tree(gogo);
2239 // If we still have an abstract type here, this this is being
2240 // used in a constant expression which didn't get reduced. We
2241 // just use complex128 and hope for the best.
2242 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
2244 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2247 // Write REAL/IMAG to export data.
2250 Complex_expression::export_complex(Export* exp, const mpfr_t real,
2253 if (!mpfr_zero_p(real))
2255 Float_expression::export_float(exp, real);
2256 if (mpfr_sgn(imag) > 0)
2257 exp->write_c_string("+");
2259 Float_expression::export_float(exp, imag);
2260 exp->write_c_string("i");
2263 // Export a complex number in a constant expression.
2266 Complex_expression::do_export(Export* exp) const
2268 Complex_expression::export_complex(exp, this->real_, this->imag_);
2269 // A trailing space lets us reliably identify the end of the number.
2270 exp->write_c_string(" ");
2273 // Make a complex expression.
2276 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2277 source_location location)
2279 return new Complex_expression(real, imag, type, location);
2282 // A reference to a const in an expression.
2284 class Const_expression : public Expression
2287 Const_expression(Named_object* constant, source_location location)
2288 : Expression(EXPRESSION_CONST_REFERENCE, location),
2289 constant_(constant), type_(NULL)
2294 { return this->constant_->name(); }
2298 do_lower(Gogo*, Named_object*, int);
2301 do_is_constant() const
2305 do_integer_constant_value(bool, mpz_t val, Type**) const;
2308 do_float_constant_value(mpfr_t val, Type**) const;
2311 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2314 do_string_constant_value(std::string* val) const
2315 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2320 // The type of a const is set by the declaration, not the use.
2322 do_determine_type(const Type_context*);
2325 do_check_types(Gogo*);
2332 do_get_tree(Translate_context* context);
2334 // When exporting a reference to a const as part of a const
2335 // expression, we export the value. We ignore the fact that it has
2338 do_export(Export* exp) const
2339 { this->constant_->const_value()->expr()->export_expression(exp); }
2343 Named_object* constant_;
2344 // The type of this reference. This is used if the constant has an
2349 // Lower a constant expression. This is where we convert the
2350 // predeclared constant iota into an integer value.
2353 Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value)
2355 if (this->constant_->const_value()->expr()->classification()
2358 if (iota_value == -1)
2360 error_at(this->location(),
2361 "iota is only defined in const declarations");
2365 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2366 Expression* ret = Expression::make_integer(&val, NULL,
2372 // Make sure that the constant itself has been lowered.
2373 gogo->lower_constant(this->constant_);
2378 // Return an integer constant value.
2381 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2385 if (this->type_ != NULL)
2386 ctype = this->type_;
2388 ctype = this->constant_->const_value()->type();
2389 if (ctype != NULL && ctype->integer_type() == NULL)
2392 Expression* e = this->constant_->const_value()->expr();
2394 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2398 && !Integer_expression::check_constant(val, ctype, this->location()))
2401 *ptype = ctype != NULL ? ctype : t;
2405 // Return a floating point constant value.
2408 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2411 if (this->type_ != NULL)
2412 ctype = this->type_;
2414 ctype = this->constant_->const_value()->type();
2415 if (ctype != NULL && ctype->float_type() == NULL)
2419 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2421 if (r && ctype != NULL)
2423 if (!Float_expression::check_constant(val, ctype, this->location()))
2425 Float_expression::constrain_float(val, ctype);
2427 *ptype = ctype != NULL ? ctype : t;
2431 // Return a complex constant value.
2434 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2438 if (this->type_ != NULL)
2439 ctype = this->type_;
2441 ctype = this->constant_->const_value()->type();
2442 if (ctype != NULL && ctype->complex_type() == NULL)
2446 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2449 if (r && ctype != NULL)
2451 if (!Complex_expression::check_constant(real, imag, ctype,
2454 Complex_expression::constrain_complex(real, imag, ctype);
2456 *ptype = ctype != NULL ? ctype : t;
2460 // Return the type of the const reference.
2463 Const_expression::do_type()
2465 if (this->type_ != NULL)
2467 Named_constant* nc = this->constant_->const_value();
2468 Type* ret = nc->type();
2471 // During parsing, a named constant may have a NULL type, but we
2472 // must not return a NULL type here.
2473 return nc->expr()->type();
2476 // Set the type of the const reference.
2479 Const_expression::do_determine_type(const Type_context* context)
2481 Type* ctype = this->constant_->const_value()->type();
2482 Type* cetype = (ctype != NULL
2484 : this->constant_->const_value()->expr()->type());
2485 if (ctype != NULL && !ctype->is_abstract())
2487 else if (context->type != NULL
2488 && (context->type->integer_type() != NULL
2489 || context->type->float_type() != NULL
2490 || context->type->complex_type() != NULL)
2491 && (cetype->integer_type() != NULL
2492 || cetype->float_type() != NULL
2493 || cetype->complex_type() != NULL))
2494 this->type_ = context->type;
2495 else if (context->type != NULL
2496 && context->type->is_string_type()
2497 && cetype->is_string_type())
2498 this->type_ = context->type;
2499 else if (context->type != NULL
2500 && context->type->is_boolean_type()
2501 && cetype->is_boolean_type())
2502 this->type_ = context->type;
2503 else if (!context->may_be_abstract)
2505 if (cetype->is_abstract())
2506 cetype = cetype->make_non_abstract_type();
2507 this->type_ = cetype;
2511 // Check types of a const reference.
2514 Const_expression::do_check_types(Gogo*)
2516 if (this->type_ == NULL || this->type_->is_abstract())
2519 // Check for integer overflow.
2520 if (this->type_->integer_type() != NULL)
2525 if (!this->integer_constant_value(true, ival, &dummy))
2529 Expression* cexpr = this->constant_->const_value()->expr();
2530 if (cexpr->float_constant_value(fval, &dummy))
2532 if (!mpfr_integer_p(fval))
2533 this->report_error(_("floating point constant "
2534 "truncated to integer"));
2537 mpfr_get_z(ival, fval, GMP_RNDN);
2538 Integer_expression::check_constant(ival, this->type_,
2548 // Return a tree for the const reference.
2551 Const_expression::do_get_tree(Translate_context* context)
2553 Gogo* gogo = context->gogo();
2555 if (this->type_ == NULL)
2556 type_tree = NULL_TREE;
2559 type_tree = this->type_->get_tree(gogo);
2560 if (type_tree == error_mark_node)
2561 return error_mark_node;
2564 // If the type has been set for this expression, but the underlying
2565 // object is an abstract int or float, we try to get the abstract
2566 // value. Otherwise we may lose something in the conversion.
2567 if (this->type_ != NULL
2568 && this->constant_->const_value()->type()->is_abstract())
2570 Expression* expr = this->constant_->const_value()->expr();
2574 if (expr->integer_constant_value(true, ival, &t))
2576 tree ret = Expression::integer_constant_tree(ival, type_tree);
2584 if (expr->float_constant_value(fval, &t))
2586 tree ret = Expression::float_constant_tree(fval, type_tree);
2593 if (expr->complex_constant_value(fval, imag, &t))
2595 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2604 tree const_tree = this->constant_->get_tree(gogo, context->function());
2605 if (this->type_ == NULL
2606 || const_tree == error_mark_node
2607 || TREE_TYPE(const_tree) == error_mark_node)
2611 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2612 ret = fold_convert(type_tree, const_tree);
2613 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2614 ret = fold(convert_to_integer(type_tree, const_tree));
2615 else if (TREE_CODE(type_tree) == REAL_TYPE)
2616 ret = fold(convert_to_real(type_tree, const_tree));
2617 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2618 ret = fold(convert_to_complex(type_tree, const_tree));
2624 // Make a reference to a constant in an expression.
2627 Expression::make_const_reference(Named_object* constant,
2628 source_location location)
2630 return new Const_expression(constant, location);
2635 class Nil_expression : public Expression
2638 Nil_expression(source_location location)
2639 : Expression(EXPRESSION_NIL, location)
2647 do_is_constant() const
2652 { return Type::make_nil_type(); }
2655 do_determine_type(const Type_context*)
2663 do_get_tree(Translate_context*)
2664 { return null_pointer_node; }
2667 do_export(Export* exp) const
2668 { exp->write_c_string("nil"); }
2671 // Import a nil expression.
2674 Nil_expression::do_import(Import* imp)
2676 imp->require_c_string("nil");
2677 return Expression::make_nil(imp->location());
2680 // Make a nil expression.
2683 Expression::make_nil(source_location location)
2685 return new Nil_expression(location);
2688 // The value of the predeclared constant iota. This is little more
2689 // than a marker. This will be lowered to an integer in
2690 // Const_expression::do_lower, which is where we know the value that
2693 class Iota_expression : public Parser_expression
2696 Iota_expression(source_location location)
2697 : Parser_expression(EXPRESSION_IOTA, location)
2702 do_lower(Gogo*, Named_object*, int)
2703 { gcc_unreachable(); }
2705 // There should only ever be one of these.
2708 { gcc_unreachable(); }
2711 // Make an iota expression. This is only called for one case: the
2712 // value of the predeclared constant iota.
2715 Expression::make_iota()
2717 static Iota_expression iota_expression(UNKNOWN_LOCATION);
2718 return &iota_expression;
2721 // A type conversion expression.
2723 class Type_conversion_expression : public Expression
2726 Type_conversion_expression(Type* type, Expression* expr,
2727 source_location location)
2728 : Expression(EXPRESSION_CONVERSION, location),
2729 type_(type), expr_(expr), may_convert_function_types_(false)
2732 // Return the type to which we are converting.
2735 { return this->type_; }
2737 // Return the expression which we are converting.
2740 { return this->expr_; }
2742 // Permit converting from one function type to another. This is
2743 // used internally for method expressions.
2745 set_may_convert_function_types()
2747 this->may_convert_function_types_ = true;
2750 // Import a type conversion expression.
2756 do_traverse(Traverse* traverse);
2759 do_lower(Gogo*, Named_object*, int);
2762 do_is_constant() const
2763 { return this->expr_->is_constant(); }
2766 do_integer_constant_value(bool, mpz_t, Type**) const;
2769 do_float_constant_value(mpfr_t, Type**) const;
2772 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
2775 do_string_constant_value(std::string*) const;
2779 { return this->type_; }
2782 do_determine_type(const Type_context*)
2784 Type_context subcontext(this->type_, false);
2785 this->expr_->determine_type(&subcontext);
2789 do_check_types(Gogo*);
2794 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2799 do_get_tree(Translate_context* context);
2802 do_export(Export*) const;
2805 // The type to convert to.
2807 // The expression to convert.
2809 // True if this is permitted to convert function types. This is
2810 // used internally for method expressions.
2811 bool may_convert_function_types_;
2817 Type_conversion_expression::do_traverse(Traverse* traverse)
2819 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
2820 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
2821 return TRAVERSE_EXIT;
2822 return TRAVERSE_CONTINUE;
2825 // Convert to a constant at lowering time.
2828 Type_conversion_expression::do_lower(Gogo*, Named_object*, int)
2830 Type* type = this->type_;
2831 Expression* val = this->expr_;
2832 source_location location = this->location();
2834 if (type->integer_type() != NULL)
2839 if (val->integer_constant_value(false, ival, &dummy))
2841 if (!Integer_expression::check_constant(ival, type, location))
2842 mpz_set_ui(ival, 0);
2843 Expression* ret = Expression::make_integer(&ival, type, location);
2850 if (val->float_constant_value(fval, &dummy))
2852 if (!mpfr_integer_p(fval))
2855 "floating point constant truncated to integer");
2856 return Expression::make_error(location);
2858 mpfr_get_z(ival, fval, GMP_RNDN);
2859 if (!Integer_expression::check_constant(ival, type, location))
2860 mpz_set_ui(ival, 0);
2861 Expression* ret = Expression::make_integer(&ival, type, location);
2870 if (type->float_type() != NULL)
2875 if (val->float_constant_value(fval, &dummy))
2877 if (!Float_expression::check_constant(fval, type, location))
2878 mpfr_set_ui(fval, 0, GMP_RNDN);
2879 Float_expression::constrain_float(fval, type);
2880 Expression *ret = Expression::make_float(&fval, type, location);
2887 if (type->complex_type() != NULL)
2894 if (val->complex_constant_value(real, imag, &dummy))
2896 if (!Complex_expression::check_constant(real, imag, type, location))
2898 mpfr_set_ui(real, 0, GMP_RNDN);
2899 mpfr_set_ui(imag, 0, GMP_RNDN);
2901 Complex_expression::constrain_complex(real, imag, type);
2902 Expression* ret = Expression::make_complex(&real, &imag, type,
2912 if (type->is_open_array_type() && type->named_type() == NULL)
2914 Type* element_type = type->array_type()->element_type()->forwarded();
2915 bool is_byte = element_type == Type::lookup_integer_type("uint8");
2916 bool is_int = element_type == Type::lookup_integer_type("int");
2917 if (is_byte || is_int)
2920 if (val->string_constant_value(&s))
2922 Expression_list* vals = new Expression_list();
2925 for (std::string::const_iterator p = s.begin();
2930 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
2931 Expression* v = Expression::make_integer(&val,
2940 const char *p = s.data();
2941 const char *pend = s.data() + s.length();
2945 int adv = Lex::fetch_char(p, &c);
2948 warning_at(this->location(), 0,
2949 "invalid UTF-8 encoding");
2954 mpz_init_set_ui(val, c);
2955 Expression* v = Expression::make_integer(&val,
2963 return Expression::make_slice_composite_literal(type, vals,
2972 // Return the constant integer value if there is one.
2975 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
2979 if (this->type_->integer_type() == NULL)
2985 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
2987 if (!Integer_expression::check_constant(ival, this->type_,
2995 *ptype = this->type_;
3002 if (this->expr_->float_constant_value(fval, &dummy))
3004 mpfr_get_z(val, fval, GMP_RNDN);
3006 if (!Integer_expression::check_constant(val, this->type_,
3009 *ptype = this->type_;
3017 // Return the constant floating point value if there is one.
3020 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3023 if (this->type_->float_type() == NULL)
3029 if (this->expr_->float_constant_value(fval, &dummy))
3031 if (!Float_expression::check_constant(fval, this->type_,
3037 mpfr_set(val, fval, GMP_RNDN);
3039 Float_expression::constrain_float(val, this->type_);
3040 *ptype = this->type_;
3048 // Return the constant complex value if there is one.
3051 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3055 if (this->type_->complex_type() == NULL)
3063 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3065 if (!Complex_expression::check_constant(rval, ival, this->type_,
3072 mpfr_set(real, rval, GMP_RNDN);
3073 mpfr_set(imag, ival, GMP_RNDN);
3076 Complex_expression::constrain_complex(real, imag, this->type_);
3077 *ptype = this->type_;
3086 // Return the constant string value if there is one.
3089 Type_conversion_expression::do_string_constant_value(std::string* val) const
3091 if (this->type_->is_string_type()
3092 && this->expr_->type()->integer_type() != NULL)
3097 if (this->expr_->integer_constant_value(false, ival, &dummy))
3099 unsigned long ulval = mpz_get_ui(ival);
3100 if (mpz_cmp_ui(ival, ulval) == 0)
3102 Lex::append_char(ulval, true, val, this->location());
3110 // FIXME: Could handle conversion from const []int here.
3115 // Check that types are convertible.
3118 Type_conversion_expression::do_check_types(Gogo*)
3120 Type* type = this->type_;
3121 Type* expr_type = this->expr_->type();
3124 if (this->may_convert_function_types_
3125 && type->function_type() != NULL
3126 && expr_type->function_type() != NULL)
3129 if (Type::are_convertible(type, expr_type, &reason))
3132 error_at(this->location(), "%s", reason.c_str());
3133 this->set_is_error();
3136 // Get a tree for a type conversion.
3139 Type_conversion_expression::do_get_tree(Translate_context* context)
3141 Gogo* gogo = context->gogo();
3142 tree type_tree = this->type_->get_tree(gogo);
3143 tree expr_tree = this->expr_->get_tree(context);
3145 if (type_tree == error_mark_node
3146 || expr_tree == error_mark_node
3147 || TREE_TYPE(expr_tree) == error_mark_node)
3148 return error_mark_node;
3150 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3151 return fold_convert(type_tree, expr_tree);
3153 Type* type = this->type_;
3154 Type* expr_type = this->expr_->type();
3156 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3157 ret = Expression::convert_for_assignment(context, type, expr_type,
3158 expr_tree, this->location());
3159 else if (type->integer_type() != NULL)
3161 if (expr_type->integer_type() != NULL
3162 || expr_type->float_type() != NULL
3163 || expr_type->is_unsafe_pointer_type())
3164 ret = fold(convert_to_integer(type_tree, expr_tree));
3168 else if (type->float_type() != NULL)
3170 if (expr_type->integer_type() != NULL
3171 || expr_type->float_type() != NULL)
3172 ret = fold(convert_to_real(type_tree, expr_tree));
3176 else if (type->complex_type() != NULL)
3178 if (expr_type->complex_type() != NULL)
3179 ret = fold(convert_to_complex(type_tree, expr_tree));
3183 else if (type->is_string_type()
3184 && expr_type->integer_type() != NULL)
3186 expr_tree = fold_convert(integer_type_node, expr_tree);
3187 if (host_integerp(expr_tree, 0))
3189 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3191 Lex::append_char(intval, true, &s, this->location());
3192 Expression* se = Expression::make_string(s, this->location());
3193 return se->get_tree(context);
3196 static tree int_to_string_fndecl;
3197 ret = Gogo::call_builtin(&int_to_string_fndecl,
3199 "__go_int_to_string",
3203 fold_convert(integer_type_node, expr_tree));
3205 else if (type->is_string_type()
3206 && (expr_type->array_type() != NULL
3207 || (expr_type->points_to() != NULL
3208 && expr_type->points_to()->array_type() != NULL)))
3210 Type* t = expr_type;
3211 if (t->points_to() != NULL)
3214 expr_tree = build_fold_indirect_ref(expr_tree);
3216 if (!DECL_P(expr_tree))
3217 expr_tree = save_expr(expr_tree);
3218 Array_type* a = t->array_type();
3219 Type* e = a->element_type()->forwarded();
3220 gcc_assert(e->integer_type() != NULL);
3221 tree valptr = fold_convert(const_ptr_type_node,
3222 a->value_pointer_tree(gogo, expr_tree));
3223 tree len = a->length_tree(gogo, expr_tree);
3224 len = fold_convert_loc(this->location(), size_type_node, len);
3225 if (e->integer_type()->is_unsigned()
3226 && e->integer_type()->bits() == 8)
3228 static tree byte_array_to_string_fndecl;
3229 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3231 "__go_byte_array_to_string",
3234 const_ptr_type_node,
3241 gcc_assert(e == Type::lookup_integer_type("int"));
3242 static tree int_array_to_string_fndecl;
3243 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3245 "__go_int_array_to_string",
3248 const_ptr_type_node,
3254 else if (type->is_open_array_type() && expr_type->is_string_type())
3256 Type* e = type->array_type()->element_type()->forwarded();
3257 gcc_assert(e->integer_type() != NULL);
3258 if (e->integer_type()->is_unsigned()
3259 && e->integer_type()->bits() == 8)
3261 static tree string_to_byte_array_fndecl;
3262 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3264 "__go_string_to_byte_array",
3267 TREE_TYPE(expr_tree),
3272 gcc_assert(e == Type::lookup_integer_type("int"));
3273 static tree string_to_int_array_fndecl;
3274 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3276 "__go_string_to_int_array",
3279 TREE_TYPE(expr_tree),
3283 else if ((type->is_unsafe_pointer_type()
3284 && expr_type->points_to() != NULL)
3285 || (expr_type->is_unsafe_pointer_type()
3286 && type->points_to() != NULL))
3287 ret = fold_convert(type_tree, expr_tree);
3288 else if (type->is_unsafe_pointer_type()
3289 && expr_type->integer_type() != NULL)
3290 ret = convert_to_pointer(type_tree, expr_tree);
3291 else if (this->may_convert_function_types_
3292 && type->function_type() != NULL
3293 && expr_type->function_type() != NULL)
3294 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3296 ret = Expression::convert_for_assignment(context, type, expr_type,
3297 expr_tree, this->location());
3302 // Output a type conversion in a constant expression.
3305 Type_conversion_expression::do_export(Export* exp) const
3307 exp->write_c_string("convert(");
3308 exp->write_type(this->type_);
3309 exp->write_c_string(", ");
3310 this->expr_->export_expression(exp);
3311 exp->write_c_string(")");
3314 // Import a type conversion or a struct construction.
3317 Type_conversion_expression::do_import(Import* imp)
3319 imp->require_c_string("convert(");
3320 Type* type = imp->read_type();
3321 imp->require_c_string(", ");
3322 Expression* val = Expression::import_expression(imp);
3323 imp->require_c_string(")");
3324 return Expression::make_cast(type, val, imp->location());
3327 // Make a type cast expression.
3330 Expression::make_cast(Type* type, Expression* val, source_location location)
3332 if (type->is_error_type() || val->is_error_expression())
3333 return Expression::make_error(location);
3334 return new Type_conversion_expression(type, val, location);
3337 // Unary expressions.
3339 class Unary_expression : public Expression
3342 Unary_expression(Operator op, Expression* expr, source_location location)
3343 : Expression(EXPRESSION_UNARY, location),
3344 op_(op), escapes_(true), expr_(expr)
3347 // Return the operator.
3350 { return this->op_; }
3352 // Return the operand.
3355 { return this->expr_; }
3357 // Record that an address expression does not escape.
3359 set_does_not_escape()
3361 gcc_assert(this->op_ == OPERATOR_AND);
3362 this->escapes_ = false;
3365 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3366 // could be done, false if not.
3368 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3371 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3372 // could be done, false if not.
3374 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3376 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3377 // true if this could be done, false if not.
3379 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3387 do_traverse(Traverse* traverse)
3388 { return Expression::traverse(&this->expr_, traverse); }
3391 do_lower(Gogo*, Named_object*, int);
3394 do_is_constant() const;
3397 do_integer_constant_value(bool, mpz_t, Type**) const;
3400 do_float_constant_value(mpfr_t, Type**) const;
3403 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3409 do_determine_type(const Type_context*);
3412 do_check_types(Gogo*);
3417 return Expression::make_unary(this->op_, this->expr_->copy(),
3422 do_is_addressable() const
3423 { return this->op_ == OPERATOR_MULT; }
3426 do_get_tree(Translate_context*);
3429 do_export(Export*) const;
3432 // The unary operator to apply.
3434 // Normally true. False if this is an address expression which does
3435 // not escape the current function.
3441 // If we are taking the address of a composite literal, and the
3442 // contents are not constant, then we want to make a heap composite
3446 Unary_expression::do_lower(Gogo*, Named_object*, int)
3448 source_location loc = this->location();
3449 Operator op = this->op_;
3450 Expression* expr = this->expr_;
3452 if (op == OPERATOR_MULT && expr->is_type_expression())
3453 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3455 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3456 // moving x to the heap. FIXME: Is it worth doing a real escape
3457 // analysis here? This case is found in math/unsafe.go and is
3458 // therefore worth special casing.
3459 if (op == OPERATOR_MULT)
3461 Expression* e = expr;
3462 while (e->classification() == EXPRESSION_CONVERSION)
3464 Type_conversion_expression* te
3465 = static_cast<Type_conversion_expression*>(e);
3469 if (e->classification() == EXPRESSION_UNARY)
3471 Unary_expression* ue = static_cast<Unary_expression*>(e);
3472 if (ue->op_ == OPERATOR_AND)
3479 ue->set_does_not_escape();
3484 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
3485 || op == OPERATOR_NOT || op == OPERATOR_XOR)
3487 Expression* ret = NULL;
3492 if (expr->integer_constant_value(false, eval, &etype))
3496 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
3497 ret = Expression::make_integer(&val, etype, loc);
3504 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)