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,
365 if (first_field_value == error_mark_node)
366 return error_mark_node;
368 // Start building a constructor for the value we will return.
370 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
372 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
373 tree field = TYPE_FIELDS(lhs_type_tree);
374 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
375 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
377 elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value);
379 elt = VEC_quick_push(constructor_elt, init, NULL);
380 field = DECL_CHAIN(field);
381 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
384 if (rhs_type->points_to() != NULL)
386 // We are assigning a pointer to the interface; the interface
387 // holds the pointer itself.
388 elt->value = rhs_tree;
389 return build_constructor(lhs_type_tree, init);
392 // We are assigning a non-pointer value to the interface; the
393 // interface gets a copy of the value in the heap.
395 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
397 tree space = gogo->allocate_memory(rhs_type, object_size, location);
398 space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)),
400 space = save_expr(space);
402 tree ref = build_fold_indirect_ref_loc(location, space);
403 TREE_THIS_NOTRAP(ref) = 1;
404 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
407 elt->value = fold_convert_loc(location, TREE_TYPE(field), space);
409 return build2(COMPOUND_EXPR, lhs_type_tree, set,
410 build_constructor(lhs_type_tree, init));
413 // Return a tree for the type descriptor of RHS_TREE, which has
414 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
418 Expression::get_interface_type_descriptor(Translate_context*,
419 Type* rhs_type, tree rhs_tree,
420 source_location location)
422 tree rhs_type_tree = TREE_TYPE(rhs_tree);
423 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
424 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
425 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
427 if (rhs_type->interface_type()->is_empty())
429 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
430 "__type_descriptor") == 0);
434 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
436 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v)));
438 tree v1 = build_fold_indirect_ref_loc(location, v);
439 gcc_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
440 tree f = TYPE_FIELDS(TREE_TYPE(v1));
441 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
443 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
445 tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v,
446 fold_convert_loc(location, TREE_TYPE(v),
448 tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node);
449 return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1),
453 // Return a tree for the conversion of an interface type to an
457 Expression::convert_interface_to_interface(Translate_context* context,
458 Type *lhs_type, Type *rhs_type,
459 tree rhs_tree, bool for_type_guard,
460 source_location location)
462 Gogo* gogo = context->gogo();
463 Interface_type* lhs_interface_type = lhs_type->interface_type();
464 bool lhs_is_empty = lhs_interface_type->is_empty();
466 tree lhs_type_tree = lhs_type->get_tree(gogo);
467 if (lhs_type_tree == error_mark_node)
468 return error_mark_node;
470 // In the general case this requires runtime examination of the type
471 // method table to match it up with the interface methods.
473 // FIXME: If all of the methods in the right hand side interface
474 // also appear in the left hand side interface, then we don't need
475 // to do a runtime check, although we still need to build a new
478 // Get the type descriptor for the right hand side. This will be
479 // NULL for a nil interface.
481 if (!DECL_P(rhs_tree))
482 rhs_tree = save_expr(rhs_tree);
484 tree rhs_type_descriptor =
485 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
488 // The result is going to be a two element constructor.
490 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
492 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
493 tree field = TYPE_FIELDS(lhs_type_tree);
498 // A type assertion fails when converting a nil interface.
499 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
500 static tree assert_interface_decl;
501 tree call = Gogo::call_builtin(&assert_interface_decl,
503 "__go_assert_interface",
506 TREE_TYPE(lhs_type_descriptor),
508 TREE_TYPE(rhs_type_descriptor),
509 rhs_type_descriptor);
510 if (call == error_mark_node)
511 return error_mark_node;
512 // This will panic if the interface conversion fails.
513 TREE_NOTHROW(assert_interface_decl) = 0;
514 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
516 else if (lhs_is_empty)
518 // A convertion to an empty interface always succeeds, and the
519 // first field is just the type descriptor of the object.
520 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
521 "__type_descriptor") == 0);
522 gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
523 elt->value = rhs_type_descriptor;
527 // A conversion to a non-empty interface may fail, but unlike a
528 // type assertion converting nil will always succeed.
529 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
531 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
532 static tree convert_interface_decl;
533 tree call = Gogo::call_builtin(&convert_interface_decl,
535 "__go_convert_interface",
538 TREE_TYPE(lhs_type_descriptor),
540 TREE_TYPE(rhs_type_descriptor),
541 rhs_type_descriptor);
542 if (call == error_mark_node)
543 return error_mark_node;
544 // This will panic if the interface conversion fails.
545 TREE_NOTHROW(convert_interface_decl) = 0;
546 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
549 // The second field is simply the object pointer.
551 elt = VEC_quick_push(constructor_elt, init, NULL);
552 field = DECL_CHAIN(field);
553 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
556 tree rhs_type_tree = TREE_TYPE(rhs_tree);
557 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
558 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
559 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
560 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
563 return build_constructor(lhs_type_tree, init);
566 // Return a tree for the conversion of an interface type to a
567 // non-interface type.
570 Expression::convert_interface_to_type(Translate_context* context,
571 Type *lhs_type, Type* rhs_type,
572 tree rhs_tree, source_location location)
574 Gogo* gogo = context->gogo();
575 tree rhs_type_tree = TREE_TYPE(rhs_tree);
577 tree lhs_type_tree = lhs_type->get_tree(gogo);
578 if (lhs_type_tree == error_mark_node)
579 return error_mark_node;
581 // Call a function to check that the type is valid. The function
582 // will panic with an appropriate runtime type error if the type is
585 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
587 if (!DECL_P(rhs_tree))
588 rhs_tree = save_expr(rhs_tree);
590 tree rhs_type_descriptor =
591 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
594 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo);
596 static tree check_interface_type_decl;
597 tree call = Gogo::call_builtin(&check_interface_type_decl,
599 "__go_check_interface_type",
602 TREE_TYPE(lhs_type_descriptor),
604 TREE_TYPE(rhs_type_descriptor),
606 TREE_TYPE(rhs_inter_descriptor),
607 rhs_inter_descriptor);
608 if (call == error_mark_node)
609 return error_mark_node;
610 // This call will panic if the conversion is invalid.
611 TREE_NOTHROW(check_interface_type_decl) = 0;
613 // If the call succeeds, pull out the value.
614 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
615 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
616 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
617 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
620 // If the value is a pointer, then it is the value we want.
621 // Otherwise it points to the value.
622 if (lhs_type->points_to() == NULL)
624 val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val);
625 val = build_fold_indirect_ref_loc(location, val);
628 return build2(COMPOUND_EXPR, lhs_type_tree, call,
629 fold_convert_loc(location, lhs_type_tree, val));
632 // Convert an expression to a tree. This is implemented by the child
633 // class. Not that it is not in general safe to call this multiple
634 // times for a single expression, but that we don't catch such errors.
637 Expression::get_tree(Translate_context* context)
639 // The child may have marked this expression as having an error.
640 if (this->classification_ == EXPRESSION_ERROR)
641 return error_mark_node;
643 return this->do_get_tree(context);
646 // Return a tree for VAL in TYPE.
649 Expression::integer_constant_tree(mpz_t val, tree type)
651 if (type == error_mark_node)
652 return error_mark_node;
653 else if (TREE_CODE(type) == INTEGER_TYPE)
654 return double_int_to_tree(type,
655 mpz_get_double_int(type, val, true));
656 else if (TREE_CODE(type) == REAL_TYPE)
659 mpfr_init_set_z(fval, val, GMP_RNDN);
660 tree ret = Expression::float_constant_tree(fval, type);
664 else if (TREE_CODE(type) == COMPLEX_TYPE)
667 mpfr_init_set_z(fval, val, GMP_RNDN);
668 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
670 tree imag = build_real_from_int_cst(TREE_TYPE(type),
672 return build_complex(type, real, imag);
678 // Return a tree for VAL in TYPE.
681 Expression::float_constant_tree(mpfr_t val, tree type)
683 if (type == error_mark_node)
684 return error_mark_node;
685 else if (TREE_CODE(type) == INTEGER_TYPE)
689 mpfr_get_z(ival, val, GMP_RNDN);
690 tree ret = Expression::integer_constant_tree(ival, type);
694 else if (TREE_CODE(type) == REAL_TYPE)
697 real_from_mpfr(&r1, val, type, GMP_RNDN);
699 real_convert(&r2, TYPE_MODE(type), &r1);
700 return build_real(type, r2);
702 else if (TREE_CODE(type) == COMPLEX_TYPE)
705 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
707 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
708 tree imag = build_real_from_int_cst(TREE_TYPE(type),
710 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
716 // Return a tree for REAL/IMAG in TYPE.
719 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
721 if (type == error_mark_node)
722 return error_mark_node;
723 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
724 return Expression::float_constant_tree(real, type);
725 else if (TREE_CODE(type) == COMPLEX_TYPE)
728 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
730 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
733 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
735 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
737 return build_complex(type, build_real(TREE_TYPE(type), r2),
738 build_real(TREE_TYPE(type), r4));
744 // Return a tree which evaluates to true if VAL, of arbitrary integer
745 // type, is negative or is more than the maximum value of BOUND_TYPE.
746 // If SOFAR is not NULL, it is or'red into the result. The return
747 // value may be NULL if SOFAR is NULL.
750 Expression::check_bounds(tree val, tree bound_type, tree sofar,
753 tree val_type = TREE_TYPE(val);
754 tree ret = NULL_TREE;
756 if (!TYPE_UNSIGNED(val_type))
758 ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val,
759 build_int_cst(val_type, 0));
760 if (ret == boolean_false_node)
764 if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type))
765 || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type))
767 tree max = TYPE_MAX_VALUE(bound_type);
768 tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val,
769 fold_convert_loc(loc, val_type, max));
770 if (big == boolean_false_node)
772 else if (ret == NULL_TREE)
775 ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
779 if (ret == NULL_TREE)
781 else if (sofar == NULL_TREE)
784 return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
788 // Error expressions. This are used to avoid cascading errors.
790 class Error_expression : public Expression
793 Error_expression(source_location location)
794 : Expression(EXPRESSION_ERROR, location)
799 do_is_constant() const
803 do_integer_constant_value(bool, mpz_t val, Type**) const
810 do_float_constant_value(mpfr_t val, Type**) const
812 mpfr_set_ui(val, 0, GMP_RNDN);
817 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
819 mpfr_set_ui(real, 0, GMP_RNDN);
820 mpfr_set_ui(imag, 0, GMP_RNDN);
825 do_discarding_value()
830 { return Type::make_error_type(); }
833 do_determine_type(const Type_context*)
841 do_is_addressable() const
845 do_get_tree(Translate_context*)
846 { return error_mark_node; }
850 Expression::make_error(source_location location)
852 return new Error_expression(location);
855 // An expression which is really a type. This is used during parsing.
856 // It is an error if these survive after lowering.
859 Type_expression : public Expression
862 Type_expression(Type* type, source_location location)
863 : Expression(EXPRESSION_TYPE, location),
869 do_traverse(Traverse* traverse)
870 { return Type::traverse(this->type_, traverse); }
874 { return this->type_; }
877 do_determine_type(const Type_context*)
881 do_check_types(Gogo*)
882 { this->report_error(_("invalid use of type")); }
889 do_get_tree(Translate_context*)
890 { gcc_unreachable(); }
893 // The type which we are representing as an expression.
898 Expression::make_type(Type* type, source_location location)
900 return new Type_expression(type, location);
903 // Class Parser_expression.
906 Parser_expression::do_type()
908 // We should never really ask for the type of a Parser_expression.
909 // However, it can happen, at least when we have an invalid const
910 // whose initializer refers to the const itself. In that case we
911 // may ask for the type when lowering the const itself.
912 gcc_assert(saw_errors());
913 return Type::make_error_type();
916 // Class Var_expression.
918 // Lower a variable expression. Here we just make sure that the
919 // initialization expression of the variable has been lowered. This
920 // ensures that we will be able to determine the type of the variable
924 Var_expression::do_lower(Gogo* gogo, Named_object* function, int)
926 if (this->variable_->is_variable())
928 Variable* var = this->variable_->var_value();
929 // This is either a local variable or a global variable. A
930 // reference to a variable which is local to an enclosing
931 // function will be a reference to a field in a closure.
932 if (var->is_global())
934 var->lower_init_expression(gogo, function);
939 // Return the type of a reference to a variable.
942 Var_expression::do_type()
944 if (this->variable_->is_variable())
945 return this->variable_->var_value()->type();
946 else if (this->variable_->is_result_variable())
947 return this->variable_->result_var_value()->type();
952 // Determine the type of a reference to a variable.
955 Var_expression::do_determine_type(const Type_context*)
957 if (this->variable_->is_variable())
958 this->variable_->var_value()->determine_type();
961 // Something takes the address of this variable. This means that we
962 // may want to move the variable onto the heap.
965 Var_expression::do_address_taken(bool escapes)
969 else if (this->variable_->is_variable())
970 this->variable_->var_value()->set_address_taken();
971 else if (this->variable_->is_result_variable())
972 this->variable_->result_var_value()->set_address_taken();
977 // Get the tree for a reference to a variable.
980 Var_expression::do_get_tree(Translate_context* context)
982 return this->variable_->get_tree(context->gogo(), context->function());
985 // Make a reference to a variable in an expression.
988 Expression::make_var_reference(Named_object* var, source_location location)
991 return Expression::make_sink(location);
993 // FIXME: Creating a new object for each reference to a variable is
995 return new Var_expression(var, location);
998 // Class Temporary_reference_expression.
1003 Temporary_reference_expression::do_type()
1005 return this->statement_->type();
1008 // Called if something takes the address of this temporary variable.
1009 // We never have to move temporary variables to the heap, but we do
1010 // need to know that they must live in the stack rather than in a
1014 Temporary_reference_expression::do_address_taken(bool)
1016 this->statement_->set_is_address_taken();
1019 // Get a tree referring to the variable.
1022 Temporary_reference_expression::do_get_tree(Translate_context*)
1024 return this->statement_->get_decl();
1027 // Make a reference to a temporary variable.
1030 Expression::make_temporary_reference(Temporary_statement* statement,
1031 source_location location)
1033 return new Temporary_reference_expression(statement, location);
1036 // A sink expression--a use of the blank identifier _.
1038 class Sink_expression : public Expression
1041 Sink_expression(source_location location)
1042 : Expression(EXPRESSION_SINK, location),
1043 type_(NULL), var_(NULL_TREE)
1048 do_discarding_value()
1055 do_determine_type(const Type_context*);
1059 { return new Sink_expression(this->location()); }
1062 do_get_tree(Translate_context*);
1065 // The type of this sink variable.
1067 // The temporary variable we generate.
1071 // Return the type of a sink expression.
1074 Sink_expression::do_type()
1076 if (this->type_ == NULL)
1077 return Type::make_sink_type();
1081 // Determine the type of a sink expression.
1084 Sink_expression::do_determine_type(const Type_context* context)
1086 if (context->type != NULL)
1087 this->type_ = context->type;
1090 // Return a temporary variable for a sink expression. This will
1091 // presumably be a write-only variable which the middle-end will drop.
1094 Sink_expression::do_get_tree(Translate_context* context)
1096 if (this->var_ == NULL_TREE)
1098 gcc_assert(this->type_ != NULL && !this->type_->is_sink_type());
1099 this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()),
1105 // Make a sink expression.
1108 Expression::make_sink(source_location location)
1110 return new Sink_expression(location);
1113 // Class Func_expression.
1115 // FIXME: Can a function expression appear in a constant expression?
1116 // The value is unchanging. Initializing a constant to the address of
1117 // a function seems like it could work, though there might be little
1123 Func_expression::do_traverse(Traverse* traverse)
1125 return (this->closure_ == NULL
1127 : Expression::traverse(&this->closure_, traverse));
1130 // Return the type of a function expression.
1133 Func_expression::do_type()
1135 if (this->function_->is_function())
1136 return this->function_->func_value()->type();
1137 else if (this->function_->is_function_declaration())
1138 return this->function_->func_declaration_value()->type();
1143 // Get the tree for a function expression without evaluating the
1147 Func_expression::get_tree_without_closure(Gogo* gogo)
1149 Function_type* fntype;
1150 if (this->function_->is_function())
1151 fntype = this->function_->func_value()->type();
1152 else if (this->function_->is_function_declaration())
1153 fntype = this->function_->func_declaration_value()->type();
1157 // Builtin functions are handled specially by Call_expression. We
1158 // can't take their address.
1159 if (fntype->is_builtin())
1161 error_at(this->location(), "invalid use of special builtin function %qs",
1162 this->function_->name().c_str());
1163 return error_mark_node;
1166 Named_object* no = this->function_;
1168 tree id = no->get_id(gogo);
1169 if (id == error_mark_node)
1170 return error_mark_node;
1173 if (no->is_function())
1174 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1175 else if (no->is_function_declaration())
1176 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1180 if (fndecl == error_mark_node)
1181 return error_mark_node;
1183 return build_fold_addr_expr_loc(this->location(), fndecl);
1186 // Get the tree for a function expression. This is used when we take
1187 // the address of a function rather than simply calling it. If the
1188 // function has a closure, we must use a trampoline.
1191 Func_expression::do_get_tree(Translate_context* context)
1193 Gogo* gogo = context->gogo();
1195 tree fnaddr = this->get_tree_without_closure(gogo);
1196 if (fnaddr == error_mark_node)
1197 return error_mark_node;
1199 gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1200 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1201 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1203 // For a normal non-nested function call, that is all we have to do.
1204 if (!this->function_->is_function()
1205 || this->function_->func_value()->enclosing() == NULL)
1207 gcc_assert(this->closure_ == NULL);
1211 // For a nested function call, we have to always allocate a
1212 // trampoline. If we don't always allocate, then closures will not
1213 // be reliably distinct.
1214 Expression* closure = this->closure_;
1216 if (closure == NULL)
1217 closure_tree = null_pointer_node;
1220 // Get the value of the closure. This will be a pointer to
1221 // space allocated on the heap.
1222 closure_tree = closure->get_tree(context);
1223 if (closure_tree == error_mark_node)
1224 return error_mark_node;
1225 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1228 // Now we need to build some code on the heap. This code will load
1229 // the static chain pointer with the closure and then jump to the
1230 // body of the function. The normal gcc approach is to build the
1231 // code on the stack. Unfortunately we can not do that, as Go
1232 // permits us to return the function pointer.
1234 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1237 // Make a reference to a function in an expression.
1240 Expression::make_func_reference(Named_object* function, Expression* closure,
1241 source_location location)
1243 return new Func_expression(function, closure, location);
1246 // Class Unknown_expression.
1248 // Return the name of an unknown expression.
1251 Unknown_expression::name() const
1253 return this->named_object_->name();
1256 // Lower a reference to an unknown name.
1259 Unknown_expression::do_lower(Gogo*, Named_object*, int)
1261 source_location location = this->location();
1262 Named_object* no = this->named_object_;
1264 if (!no->is_unknown())
1268 real = no->unknown_value()->real_named_object();
1271 if (this->is_composite_literal_key_)
1273 error_at(location, "reference to undefined name %qs",
1274 this->named_object_->message_name().c_str());
1275 return Expression::make_error(location);
1278 switch (real->classification())
1280 case Named_object::NAMED_OBJECT_CONST:
1281 return Expression::make_const_reference(real, location);
1282 case Named_object::NAMED_OBJECT_TYPE:
1283 return Expression::make_type(real->type_value(), location);
1284 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1285 if (this->is_composite_literal_key_)
1287 error_at(location, "reference to undefined type %qs",
1288 real->message_name().c_str());
1289 return Expression::make_error(location);
1290 case Named_object::NAMED_OBJECT_VAR:
1291 return Expression::make_var_reference(real, location);
1292 case Named_object::NAMED_OBJECT_FUNC:
1293 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1294 return Expression::make_func_reference(real, NULL, location);
1295 case Named_object::NAMED_OBJECT_PACKAGE:
1296 if (this->is_composite_literal_key_)
1298 error_at(location, "unexpected reference to package");
1299 return Expression::make_error(location);
1305 // Make a reference to an unknown name.
1308 Expression::make_unknown_reference(Named_object* no, source_location location)
1310 gcc_assert(no->resolve()->is_unknown());
1311 return new Unknown_expression(no, location);
1314 // A boolean expression.
1316 class Boolean_expression : public Expression
1319 Boolean_expression(bool val, source_location location)
1320 : Expression(EXPRESSION_BOOLEAN, location),
1321 val_(val), type_(NULL)
1329 do_is_constant() const
1336 do_determine_type(const Type_context*);
1343 do_get_tree(Translate_context*)
1344 { return this->val_ ? boolean_true_node : boolean_false_node; }
1347 do_export(Export* exp) const
1348 { exp->write_c_string(this->val_ ? "true" : "false"); }
1353 // The type as determined by context.
1360 Boolean_expression::do_type()
1362 if (this->type_ == NULL)
1363 this->type_ = Type::make_boolean_type();
1367 // Set the type from the context.
1370 Boolean_expression::do_determine_type(const Type_context* context)
1372 if (this->type_ != NULL && !this->type_->is_abstract())
1374 else if (context->type != NULL && context->type->is_boolean_type())
1375 this->type_ = context->type;
1376 else if (!context->may_be_abstract)
1377 this->type_ = Type::lookup_bool_type();
1380 // Import a boolean constant.
1383 Boolean_expression::do_import(Import* imp)
1385 if (imp->peek_char() == 't')
1387 imp->require_c_string("true");
1388 return Expression::make_boolean(true, imp->location());
1392 imp->require_c_string("false");
1393 return Expression::make_boolean(false, imp->location());
1397 // Make a boolean expression.
1400 Expression::make_boolean(bool val, source_location location)
1402 return new Boolean_expression(val, location);
1405 // Class String_expression.
1410 String_expression::do_type()
1412 if (this->type_ == NULL)
1413 this->type_ = Type::make_string_type();
1417 // Set the type from the context.
1420 String_expression::do_determine_type(const Type_context* context)
1422 if (this->type_ != NULL && !this->type_->is_abstract())
1424 else if (context->type != NULL && context->type->is_string_type())
1425 this->type_ = context->type;
1426 else if (!context->may_be_abstract)
1427 this->type_ = Type::lookup_string_type();
1430 // Build a string constant.
1433 String_expression::do_get_tree(Translate_context* context)
1435 return context->gogo()->go_string_constant_tree(this->val_);
1438 // Export a string expression.
1441 String_expression::do_export(Export* exp) const
1444 s.reserve(this->val_.length() * 4 + 2);
1446 for (std::string::const_iterator p = this->val_.begin();
1447 p != this->val_.end();
1450 if (*p == '\\' || *p == '"')
1455 else if (*p >= 0x20 && *p < 0x7f)
1457 else if (*p == '\n')
1459 else if (*p == '\t')
1464 unsigned char c = *p;
1465 unsigned int dig = c >> 4;
1466 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1468 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1472 exp->write_string(s);
1475 // Import a string expression.
1478 String_expression::do_import(Import* imp)
1480 imp->require_c_string("\"");
1484 int c = imp->get_char();
1485 if (c == '"' || c == -1)
1488 val += static_cast<char>(c);
1491 c = imp->get_char();
1492 if (c == '\\' || c == '"')
1493 val += static_cast<char>(c);
1500 c = imp->get_char();
1501 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1502 c = imp->get_char();
1503 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1504 char v = (vh << 4) | vl;
1509 error_at(imp->location(), "bad string constant");
1510 return Expression::make_error(imp->location());
1514 return Expression::make_string(val, imp->location());
1517 // Make a string expression.
1520 Expression::make_string(const std::string& val, source_location location)
1522 return new String_expression(val, location);
1525 // Make an integer expression.
1527 class Integer_expression : public Expression
1530 Integer_expression(const mpz_t* val, Type* type, source_location location)
1531 : Expression(EXPRESSION_INTEGER, location),
1533 { mpz_init_set(this->val_, *val); }
1538 // Return whether VAL fits in the type.
1540 check_constant(mpz_t val, Type*, source_location);
1542 // Write VAL to export data.
1544 export_integer(Export* exp, const mpz_t val);
1548 do_is_constant() const
1552 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1558 do_determine_type(const Type_context* context);
1561 do_check_types(Gogo*);
1564 do_get_tree(Translate_context*);
1568 { return Expression::make_integer(&this->val_, this->type_,
1569 this->location()); }
1572 do_export(Export*) const;
1575 // The integer value.
1581 // Return an integer constant value.
1584 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1587 if (this->type_ != NULL)
1588 *ptype = this->type_;
1589 mpz_set(val, this->val_);
1593 // Return the current type. If we haven't set the type yet, we return
1594 // an abstract integer type.
1597 Integer_expression::do_type()
1599 if (this->type_ == NULL)
1600 this->type_ = Type::make_abstract_integer_type();
1604 // Set the type of the integer value. Here we may switch from an
1605 // abstract type to a real type.
1608 Integer_expression::do_determine_type(const Type_context* context)
1610 if (this->type_ != NULL && !this->type_->is_abstract())
1612 else if (context->type != NULL
1613 && (context->type->integer_type() != NULL
1614 || context->type->float_type() != NULL
1615 || context->type->complex_type() != NULL))
1616 this->type_ = context->type;
1617 else if (!context->may_be_abstract)
1618 this->type_ = Type::lookup_integer_type("int");
1621 // Return true if the integer VAL fits in the range of the type TYPE.
1622 // Otherwise give an error and return false. TYPE may be NULL.
1625 Integer_expression::check_constant(mpz_t val, Type* type,
1626 source_location location)
1630 Integer_type* itype = type->integer_type();
1631 if (itype == NULL || itype->is_abstract())
1634 int bits = mpz_sizeinbase(val, 2);
1636 if (itype->is_unsigned())
1638 // For an unsigned type we can only accept a nonnegative number,
1639 // and we must be able to represent at least BITS.
1640 if (mpz_sgn(val) >= 0
1641 && bits <= itype->bits())
1646 // For a signed type we need an extra bit to indicate the sign.
1647 // We have to handle the most negative integer specially.
1648 if (bits + 1 <= itype->bits()
1649 || (bits <= itype->bits()
1651 && (mpz_scan1(val, 0)
1652 == static_cast<unsigned long>(itype->bits() - 1))
1653 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1657 error_at(location, "integer constant overflow");
1661 // Check the type of an integer constant.
1664 Integer_expression::do_check_types(Gogo*)
1666 if (this->type_ == NULL)
1668 if (!Integer_expression::check_constant(this->val_, this->type_,
1670 this->set_is_error();
1673 // Get a tree for an integer constant.
1676 Integer_expression::do_get_tree(Translate_context* context)
1678 Gogo* gogo = context->gogo();
1680 if (this->type_ != NULL && !this->type_->is_abstract())
1681 type = this->type_->get_tree(gogo);
1682 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1684 // We are converting to an abstract floating point type.
1685 type = Type::lookup_float_type("float64")->get_tree(gogo);
1687 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1689 // We are converting to an abstract complex type.
1690 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
1694 // If we still have an abstract type here, then this is being
1695 // used in a constant expression which didn't get reduced for
1696 // some reason. Use a type which will fit the value. We use <,
1697 // not <=, because we need an extra bit for the sign bit.
1698 int bits = mpz_sizeinbase(this->val_, 2);
1699 if (bits < INT_TYPE_SIZE)
1700 type = Type::lookup_integer_type("int")->get_tree(gogo);
1702 type = Type::lookup_integer_type("int64")->get_tree(gogo);
1704 type = long_long_integer_type_node;
1706 return Expression::integer_constant_tree(this->val_, type);
1709 // Write VAL to export data.
1712 Integer_expression::export_integer(Export* exp, const mpz_t val)
1714 char* s = mpz_get_str(NULL, 10, val);
1715 exp->write_c_string(s);
1719 // Export an integer in a constant expression.
1722 Integer_expression::do_export(Export* exp) const
1724 Integer_expression::export_integer(exp, this->val_);
1725 // A trailing space lets us reliably identify the end of the number.
1726 exp->write_c_string(" ");
1729 // Import an integer, floating point, or complex value. This handles
1730 // all these types because they all start with digits.
1733 Integer_expression::do_import(Import* imp)
1735 std::string num = imp->read_identifier();
1736 imp->require_c_string(" ");
1737 if (!num.empty() && num[num.length() - 1] == 'i')
1740 size_t plus_pos = num.find('+', 1);
1741 size_t minus_pos = num.find('-', 1);
1743 if (plus_pos == std::string::npos)
1745 else if (minus_pos == std::string::npos)
1749 error_at(imp->location(), "bad number in import data: %qs",
1751 return Expression::make_error(imp->location());
1753 if (pos == std::string::npos)
1754 mpfr_set_ui(real, 0, GMP_RNDN);
1757 std::string real_str = num.substr(0, pos);
1758 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1760 error_at(imp->location(), "bad number in import data: %qs",
1762 return Expression::make_error(imp->location());
1766 std::string imag_str;
1767 if (pos == std::string::npos)
1770 imag_str = num.substr(pos);
1771 imag_str = imag_str.substr(0, imag_str.size() - 1);
1773 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1775 error_at(imp->location(), "bad number in import data: %qs",
1777 return Expression::make_error(imp->location());
1779 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1785 else if (num.find('.') == std::string::npos
1786 && num.find('E') == std::string::npos)
1789 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1791 error_at(imp->location(), "bad number in import data: %qs",
1793 return Expression::make_error(imp->location());
1795 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1802 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1804 error_at(imp->location(), "bad number in import data: %qs",
1806 return Expression::make_error(imp->location());
1808 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1814 // Build a new integer value.
1817 Expression::make_integer(const mpz_t* val, Type* type,
1818 source_location location)
1820 return new Integer_expression(val, type, location);
1825 class Float_expression : public Expression
1828 Float_expression(const mpfr_t* val, Type* type, source_location location)
1829 : Expression(EXPRESSION_FLOAT, location),
1832 mpfr_init_set(this->val_, *val, GMP_RNDN);
1835 // Constrain VAL to fit into TYPE.
1837 constrain_float(mpfr_t val, Type* type);
1839 // Return whether VAL fits in the type.
1841 check_constant(mpfr_t val, Type*, source_location);
1843 // Write VAL to export data.
1845 export_float(Export* exp, const mpfr_t val);
1849 do_is_constant() const
1853 do_float_constant_value(mpfr_t val, Type**) const;
1859 do_determine_type(const Type_context*);
1862 do_check_types(Gogo*);
1866 { return Expression::make_float(&this->val_, this->type_,
1867 this->location()); }
1870 do_get_tree(Translate_context*);
1873 do_export(Export*) const;
1876 // The floating point value.
1882 // Constrain VAL to fit into TYPE.
1885 Float_expression::constrain_float(mpfr_t val, Type* type)
1887 Float_type* ftype = type->float_type();
1888 if (ftype != NULL && !ftype->is_abstract())
1890 tree type_tree = ftype->type_tree();
1891 REAL_VALUE_TYPE rvt;
1892 real_from_mpfr(&rvt, val, type_tree, GMP_RNDN);
1893 real_convert(&rvt, TYPE_MODE(type_tree), &rvt);
1894 mpfr_from_real(val, &rvt, GMP_RNDN);
1898 // Return a floating point constant value.
1901 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
1903 if (this->type_ != NULL)
1904 *ptype = this->type_;
1905 mpfr_set(val, this->val_, GMP_RNDN);
1909 // Return the current type. If we haven't set the type yet, we return
1910 // an abstract float type.
1913 Float_expression::do_type()
1915 if (this->type_ == NULL)
1916 this->type_ = Type::make_abstract_float_type();
1920 // Set the type of the float value. Here we may switch from an
1921 // abstract type to a real type.
1924 Float_expression::do_determine_type(const Type_context* context)
1926 if (this->type_ != NULL && !this->type_->is_abstract())
1928 else if (context->type != NULL
1929 && (context->type->integer_type() != NULL
1930 || context->type->float_type() != NULL
1931 || context->type->complex_type() != NULL))
1932 this->type_ = context->type;
1933 else if (!context->may_be_abstract)
1934 this->type_ = Type::lookup_float_type("float64");
1937 // Return true if the floating point value VAL fits in the range of
1938 // the type TYPE. Otherwise give an error and return false. TYPE may
1942 Float_expression::check_constant(mpfr_t val, Type* type,
1943 source_location location)
1947 Float_type* ftype = type->float_type();
1948 if (ftype == NULL || ftype->is_abstract())
1951 // A NaN or Infinity always fits in the range of the type.
1952 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
1955 mp_exp_t exp = mpfr_get_exp(val);
1957 switch (ftype->bits())
1970 error_at(location, "floating point constant overflow");
1976 // Check the type of a float value.
1979 Float_expression::do_check_types(Gogo*)
1981 if (this->type_ == NULL)
1984 if (!Float_expression::check_constant(this->val_, this->type_,
1986 this->set_is_error();
1988 Integer_type* integer_type = this->type_->integer_type();
1989 if (integer_type != NULL)
1991 if (!mpfr_integer_p(this->val_))
1992 this->report_error(_("floating point constant truncated to integer"));
1995 gcc_assert(!integer_type->is_abstract());
1998 mpfr_get_z(ival, this->val_, GMP_RNDN);
1999 Integer_expression::check_constant(ival, integer_type,
2006 // Get a tree for a float constant.
2009 Float_expression::do_get_tree(Translate_context* context)
2011 Gogo* gogo = context->gogo();
2013 if (this->type_ != NULL && !this->type_->is_abstract())
2014 type = this->type_->get_tree(gogo);
2015 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2017 // We have an abstract integer type. We just hope for the best.
2018 type = Type::lookup_integer_type("int")->get_tree(gogo);
2022 // If we still have an abstract type here, then this is being
2023 // used in a constant expression which didn't get reduced. We
2024 // just use float64 and hope for the best.
2025 type = Type::lookup_float_type("float64")->get_tree(gogo);
2027 return Expression::float_constant_tree(this->val_, type);
2030 // Write a floating point number to export data.
2033 Float_expression::export_float(Export *exp, const mpfr_t val)
2036 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2038 exp->write_c_string("-");
2039 exp->write_c_string("0.");
2040 exp->write_c_string(*s == '-' ? s + 1 : s);
2043 snprintf(buf, sizeof buf, "E%ld", exponent);
2044 exp->write_c_string(buf);
2047 // Export a floating point number in a constant expression.
2050 Float_expression::do_export(Export* exp) const
2052 Float_expression::export_float(exp, this->val_);
2053 // A trailing space lets us reliably identify the end of the number.
2054 exp->write_c_string(" ");
2057 // Make a float expression.
2060 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2062 return new Float_expression(val, type, location);
2067 class Complex_expression : public Expression
2070 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2071 source_location location)
2072 : Expression(EXPRESSION_COMPLEX, location),
2075 mpfr_init_set(this->real_, *real, GMP_RNDN);
2076 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2079 // Constrain REAL/IMAG to fit into TYPE.
2081 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2083 // Return whether REAL/IMAG fits in the type.
2085 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2087 // Write REAL/IMAG to export data.
2089 export_complex(Export* exp, const mpfr_t real, const mpfr_t val);
2093 do_is_constant() const
2097 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2103 do_determine_type(const Type_context*);
2106 do_check_types(Gogo*);
2111 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2116 do_get_tree(Translate_context*);
2119 do_export(Export*) const;
2124 // The imaginary part;
2126 // The type if known.
2130 // Constrain REAL/IMAG to fit into TYPE.
2133 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2135 Complex_type* ctype = type->complex_type();
2136 if (ctype != NULL && !ctype->is_abstract())
2138 tree type_tree = ctype->type_tree();
2140 REAL_VALUE_TYPE rvt;
2141 real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN);
2142 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2143 mpfr_from_real(real, &rvt, GMP_RNDN);
2145 real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN);
2146 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2147 mpfr_from_real(imag, &rvt, GMP_RNDN);
2151 // Return a complex constant value.
2154 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2157 if (this->type_ != NULL)
2158 *ptype = this->type_;
2159 mpfr_set(real, this->real_, GMP_RNDN);
2160 mpfr_set(imag, this->imag_, GMP_RNDN);
2164 // Return the current type. If we haven't set the type yet, we return
2165 // an abstract complex type.
2168 Complex_expression::do_type()
2170 if (this->type_ == NULL)
2171 this->type_ = Type::make_abstract_complex_type();
2175 // Set the type of the complex value. Here we may switch from an
2176 // abstract type to a real type.
2179 Complex_expression::do_determine_type(const Type_context* context)
2181 if (this->type_ != NULL && !this->type_->is_abstract())
2183 else if (context->type != NULL
2184 && context->type->complex_type() != NULL)
2185 this->type_ = context->type;
2186 else if (!context->may_be_abstract)
2187 this->type_ = Type::lookup_complex_type("complex128");
2190 // Return true if the complex value REAL/IMAG fits in the range of the
2191 // type TYPE. Otherwise give an error and return false. TYPE may be
2195 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2196 source_location location)
2200 Complex_type* ctype = type->complex_type();
2201 if (ctype == NULL || ctype->is_abstract())
2205 switch (ctype->bits())
2217 // A NaN or Infinity always fits in the range of the type.
2218 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2220 if (mpfr_get_exp(real) > max_exp)
2222 error_at(location, "complex real part constant overflow");
2227 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2229 if (mpfr_get_exp(imag) > max_exp)
2231 error_at(location, "complex imaginary part constant overflow");
2239 // Check the type of a complex value.
2242 Complex_expression::do_check_types(Gogo*)
2244 if (this->type_ == NULL)
2247 if (!Complex_expression::check_constant(this->real_, this->imag_,
2248 this->type_, this->location()))
2249 this->set_is_error();
2252 // Get a tree for a complex constant.
2255 Complex_expression::do_get_tree(Translate_context* context)
2257 Gogo* gogo = context->gogo();
2259 if (this->type_ != NULL && !this->type_->is_abstract())
2260 type = this->type_->get_tree(gogo);
2263 // If we still have an abstract type here, this this is being
2264 // used in a constant expression which didn't get reduced. We
2265 // just use complex128 and hope for the best.
2266 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
2268 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2271 // Write REAL/IMAG to export data.
2274 Complex_expression::export_complex(Export* exp, const mpfr_t real,
2277 if (!mpfr_zero_p(real))
2279 Float_expression::export_float(exp, real);
2280 if (mpfr_sgn(imag) > 0)
2281 exp->write_c_string("+");
2283 Float_expression::export_float(exp, imag);
2284 exp->write_c_string("i");
2287 // Export a complex number in a constant expression.
2290 Complex_expression::do_export(Export* exp) const
2292 Complex_expression::export_complex(exp, this->real_, this->imag_);
2293 // A trailing space lets us reliably identify the end of the number.
2294 exp->write_c_string(" ");
2297 // Make a complex expression.
2300 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2301 source_location location)
2303 return new Complex_expression(real, imag, type, location);
2306 // Find a named object in an expression.
2308 class Find_named_object : public Traverse
2311 Find_named_object(Named_object* no)
2312 : Traverse(traverse_expressions),
2313 no_(no), found_(false)
2316 // Whether we found the object.
2319 { return this->found_; }
2323 expression(Expression**);
2326 // The object we are looking for.
2328 // Whether we found it.
2332 // A reference to a const in an expression.
2334 class Const_expression : public Expression
2337 Const_expression(Named_object* constant, source_location location)
2338 : Expression(EXPRESSION_CONST_REFERENCE, location),
2339 constant_(constant), type_(NULL), seen_(false)
2344 { return this->constant_; }
2346 // Check that the initializer does not refer to the constant itself.
2348 check_for_init_loop();
2352 do_traverse(Traverse*);
2355 do_lower(Gogo*, Named_object*, int);
2358 do_is_constant() const
2362 do_integer_constant_value(bool, mpz_t val, Type**) const;
2365 do_float_constant_value(mpfr_t val, Type**) const;
2368 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2371 do_string_constant_value(std::string* val) const
2372 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2377 // The type of a const is set by the declaration, not the use.
2379 do_determine_type(const Type_context*);
2382 do_check_types(Gogo*);
2389 do_get_tree(Translate_context* context);
2391 // When exporting a reference to a const as part of a const
2392 // expression, we export the value. We ignore the fact that it has
2395 do_export(Export* exp) const
2396 { this->constant_->const_value()->expr()->export_expression(exp); }
2400 Named_object* constant_;
2401 // The type of this reference. This is used if the constant has an
2404 // Used to prevent infinite recursion when a constant incorrectly
2405 // refers to itself.
2412 Const_expression::do_traverse(Traverse* traverse)
2414 if (this->type_ != NULL)
2415 return Type::traverse(this->type_, traverse);
2416 return TRAVERSE_CONTINUE;
2419 // Lower a constant expression. This is where we convert the
2420 // predeclared constant iota into an integer value.
2423 Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value)
2425 if (this->constant_->const_value()->expr()->classification()
2428 if (iota_value == -1)
2430 error_at(this->location(),
2431 "iota is only defined in const declarations");
2435 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2436 Expression* ret = Expression::make_integer(&val, NULL,
2442 // Make sure that the constant itself has been lowered.
2443 gogo->lower_constant(this->constant_);
2448 // Return an integer constant value.
2451 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2458 if (this->type_ != NULL)
2459 ctype = this->type_;
2461 ctype = this->constant_->const_value()->type();
2462 if (ctype != NULL && ctype->integer_type() == NULL)
2465 Expression* e = this->constant_->const_value()->expr();
2470 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2472 this->seen_ = false;
2476 && !Integer_expression::check_constant(val, ctype, this->location()))
2479 *ptype = ctype != NULL ? ctype : t;
2483 // Return a floating point constant value.
2486 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2492 if (this->type_ != NULL)
2493 ctype = this->type_;
2495 ctype = this->constant_->const_value()->type();
2496 if (ctype != NULL && ctype->float_type() == NULL)
2502 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2505 this->seen_ = false;
2507 if (r && ctype != NULL)
2509 if (!Float_expression::check_constant(val, ctype, this->location()))
2511 Float_expression::constrain_float(val, ctype);
2513 *ptype = ctype != NULL ? ctype : t;
2517 // Return a complex constant value.
2520 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2527 if (this->type_ != NULL)
2528 ctype = this->type_;
2530 ctype = this->constant_->const_value()->type();
2531 if (ctype != NULL && ctype->complex_type() == NULL)
2537 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2541 this->seen_ = false;
2543 if (r && ctype != NULL)
2545 if (!Complex_expression::check_constant(real, imag, ctype,
2548 Complex_expression::constrain_complex(real, imag, ctype);
2550 *ptype = ctype != NULL ? ctype : t;
2554 // Return the type of the const reference.
2557 Const_expression::do_type()
2559 if (this->type_ != NULL)
2562 Named_constant* nc = this->constant_->const_value();
2564 if (this->seen_ || nc->lowering())
2566 this->report_error(_("constant refers to itself"));
2567 this->type_ = Type::make_error_type();
2573 Type* ret = nc->type();
2577 this->seen_ = false;
2581 // During parsing, a named constant may have a NULL type, but we
2582 // must not return a NULL type here.
2583 ret = nc->expr()->type();
2585 this->seen_ = false;
2590 // Set the type of the const reference.
2593 Const_expression::do_determine_type(const Type_context* context)
2595 Type* ctype = this->constant_->const_value()->type();
2596 Type* cetype = (ctype != NULL
2598 : this->constant_->const_value()->expr()->type());
2599 if (ctype != NULL && !ctype->is_abstract())
2601 else if (context->type != NULL
2602 && (context->type->integer_type() != NULL
2603 || context->type->float_type() != NULL
2604 || context->type->complex_type() != NULL)
2605 && (cetype->integer_type() != NULL
2606 || cetype->float_type() != NULL
2607 || cetype->complex_type() != NULL))
2608 this->type_ = context->type;
2609 else if (context->type != NULL
2610 && context->type->is_string_type()
2611 && cetype->is_string_type())
2612 this->type_ = context->type;
2613 else if (context->type != NULL
2614 && context->type->is_boolean_type()
2615 && cetype->is_boolean_type())
2616 this->type_ = context->type;
2617 else if (!context->may_be_abstract)
2619 if (cetype->is_abstract())
2620 cetype = cetype->make_non_abstract_type();
2621 this->type_ = cetype;
2625 // Check for a loop in which the initializer of a constant refers to
2626 // the constant itself.
2629 Const_expression::check_for_init_loop()
2631 if (this->type_ != NULL && this->type_->is_error_type())
2636 this->report_error(_("constant refers to itself"));
2637 this->type_ = Type::make_error_type();
2641 Expression* init = this->constant_->const_value()->expr();
2642 Find_named_object find_named_object(this->constant_);
2645 Expression::traverse(&init, &find_named_object);
2646 this->seen_ = false;
2648 if (find_named_object.found())
2650 if (this->type_ == NULL || !this->type_->is_error_type())
2652 this->report_error(_("constant refers to itself"));
2653 this->type_ = Type::make_error_type();
2659 // Check types of a const reference.
2662 Const_expression::do_check_types(Gogo*)
2664 if (this->type_ != NULL && this->type_->is_error_type())
2667 this->check_for_init_loop();
2669 if (this->type_ == NULL || this->type_->is_abstract())
2672 // Check for integer overflow.
2673 if (this->type_->integer_type() != NULL)
2678 if (!this->integer_constant_value(true, ival, &dummy))
2682 Expression* cexpr = this->constant_->const_value()->expr();
2683 if (cexpr->float_constant_value(fval, &dummy))
2685 if (!mpfr_integer_p(fval))
2686 this->report_error(_("floating point constant "
2687 "truncated to integer"));
2690 mpfr_get_z(ival, fval, GMP_RNDN);
2691 Integer_expression::check_constant(ival, this->type_,
2701 // Return a tree for the const reference.
2704 Const_expression::do_get_tree(Translate_context* context)
2706 Gogo* gogo = context->gogo();
2708 if (this->type_ == NULL)
2709 type_tree = NULL_TREE;
2712 type_tree = this->type_->get_tree(gogo);
2713 if (type_tree == error_mark_node)
2714 return error_mark_node;
2717 // If the type has been set for this expression, but the underlying
2718 // object is an abstract int or float, we try to get the abstract
2719 // value. Otherwise we may lose something in the conversion.
2720 if (this->type_ != NULL
2721 && (this->constant_->const_value()->type() == NULL
2722 || this->constant_->const_value()->type()->is_abstract()))
2724 Expression* expr = this->constant_->const_value()->expr();
2728 if (expr->integer_constant_value(true, ival, &t))
2730 tree ret = Expression::integer_constant_tree(ival, type_tree);
2738 if (expr->float_constant_value(fval, &t))
2740 tree ret = Expression::float_constant_tree(fval, type_tree);
2747 if (expr->complex_constant_value(fval, imag, &t))
2749 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2758 tree const_tree = this->constant_->get_tree(gogo, context->function());
2759 if (this->type_ == NULL
2760 || const_tree == error_mark_node
2761 || TREE_TYPE(const_tree) == error_mark_node)
2765 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2766 ret = fold_convert(type_tree, const_tree);
2767 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2768 ret = fold(convert_to_integer(type_tree, const_tree));
2769 else if (TREE_CODE(type_tree) == REAL_TYPE)
2770 ret = fold(convert_to_real(type_tree, const_tree));
2771 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2772 ret = fold(convert_to_complex(type_tree, const_tree));
2778 // Make a reference to a constant in an expression.
2781 Expression::make_const_reference(Named_object* constant,
2782 source_location location)
2784 return new Const_expression(constant, location);
2787 // Find a named object in an expression.
2790 Find_named_object::expression(Expression** pexpr)
2792 switch ((*pexpr)->classification())
2794 case Expression::EXPRESSION_CONST_REFERENCE:
2796 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2797 if (ce->named_object() == this->no_)
2800 // We need to check a constant initializer explicitly, as
2801 // loops here will not be caught by the loop checking for
2802 // variable initializers.
2803 ce->check_for_init_loop();
2805 return TRAVERSE_CONTINUE;
2808 case Expression::EXPRESSION_VAR_REFERENCE:
2809 if ((*pexpr)->var_expression()->named_object() == this->no_)
2811 return TRAVERSE_CONTINUE;
2812 case Expression::EXPRESSION_FUNC_REFERENCE:
2813 if ((*pexpr)->func_expression()->named_object() == this->no_)
2815 return TRAVERSE_CONTINUE;
2817 return TRAVERSE_CONTINUE;
2819 this->found_ = true;
2820 return TRAVERSE_EXIT;
2825 class Nil_expression : public Expression
2828 Nil_expression(source_location location)
2829 : Expression(EXPRESSION_NIL, location)
2837 do_is_constant() const
2842 { return Type::make_nil_type(); }
2845 do_determine_type(const Type_context*)
2853 do_get_tree(Translate_context*)
2854 { return null_pointer_node; }
2857 do_export(Export* exp) const
2858 { exp->write_c_string("nil"); }
2861 // Import a nil expression.
2864 Nil_expression::do_import(Import* imp)
2866 imp->require_c_string("nil");
2867 return Expression::make_nil(imp->location());
2870 // Make a nil expression.
2873 Expression::make_nil(source_location location)
2875 return new Nil_expression(location);
2878 // The value of the predeclared constant iota. This is little more
2879 // than a marker. This will be lowered to an integer in
2880 // Const_expression::do_lower, which is where we know the value that
2883 class Iota_expression : public Parser_expression
2886 Iota_expression(source_location location)
2887 : Parser_expression(EXPRESSION_IOTA, location)
2892 do_lower(Gogo*, Named_object*, int)
2893 { gcc_unreachable(); }
2895 // There should only ever be one of these.
2898 { gcc_unreachable(); }
2901 // Make an iota expression. This is only called for one case: the
2902 // value of the predeclared constant iota.
2905 Expression::make_iota()
2907 static Iota_expression iota_expression(UNKNOWN_LOCATION);
2908 return &iota_expression;
2911 // A type conversion expression.
2913 class Type_conversion_expression : public Expression
2916 Type_conversion_expression(Type* type, Expression* expr,
2917 source_location location)
2918 : Expression(EXPRESSION_CONVERSION, location),
2919 type_(type), expr_(expr), may_convert_function_types_(false)
2922 // Return the type to which we are converting.
2925 { return this->type_; }
2927 // Return the expression which we are converting.
2930 { return this->expr_; }
2932 // Permit converting from one function type to another. This is
2933 // used internally for method expressions.
2935 set_may_convert_function_types()
2937 this->may_convert_function_types_ = true;
2940 // Import a type conversion expression.
2946 do_traverse(Traverse* traverse);
2949 do_lower(Gogo*, Named_object*, int);
2952 do_is_constant() const
2953 { return this->expr_->is_constant(); }
2956 do_integer_constant_value(bool, mpz_t, Type**) const;
2959 do_float_constant_value(mpfr_t, Type**) const;
2962 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
2965 do_string_constant_value(std::string*) const;
2969 { return this->type_; }
2972 do_determine_type(const Type_context*)
2974 Type_context subcontext(this->type_, false);
2975 this->expr_->determine_type(&subcontext);
2979 do_check_types(Gogo*);
2984 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2989 do_get_tree(Translate_context* context);
2992 do_export(Export*) const;
2995 // The type to convert to.
2997 // The expression to convert.
2999 // True if this is permitted to convert function types. This is
3000 // used internally for method expressions.
3001 bool may_convert_function_types_;
3007 Type_conversion_expression::do_traverse(Traverse* traverse)
3009 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3010 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3011 return TRAVERSE_EXIT;
3012 return TRAVERSE_CONTINUE;
3015 // Convert to a constant at lowering time.
3018 Type_conversion_expression::do_lower(Gogo*, Named_object*, int)
3020 Type* type = this->type_;
3021 Expression* val = this->expr_;
3022 source_location location = this->location();
3024 if (type->integer_type() != NULL)
3029 if (val->integer_constant_value(false, ival, &dummy))
3031 if (!Integer_expression::check_constant(ival, type, location))
3032 mpz_set_ui(ival, 0);
3033 Expression* ret = Expression::make_integer(&ival, type, location);
3040 if (val->float_constant_value(fval, &dummy))
3042 if (!mpfr_integer_p(fval))
3045 "floating point constant truncated to integer");
3046 return Expression::make_error(location);
3048 mpfr_get_z(ival, fval, GMP_RNDN);
3049 if (!Integer_expression::check_constant(ival, type, location))
3050 mpz_set_ui(ival, 0);
3051 Expression* ret = Expression::make_integer(&ival, type, location);
3060 if (type->float_type() != NULL)
3065 if (val->float_constant_value(fval, &dummy))
3067 if (!Float_expression::check_constant(fval, type, location))
3068 mpfr_set_ui(fval, 0, GMP_RNDN);
3069 Float_expression::constrain_float(fval, type);
3070 Expression *ret = Expression::make_float(&fval, type, location);
3077 if (type->complex_type() != NULL)
3084 if (val->complex_constant_value(real, imag, &dummy))
3086 if (!Complex_expression::check_constant(real, imag, type, location))
3088 mpfr_set_ui(real, 0, GMP_RNDN);
3089 mpfr_set_ui(imag, 0, GMP_RNDN);
3091 Complex_expression::constrain_complex(real, imag, type);
3092 Expression* ret = Expression::make_complex(&real, &imag, type,
3102 if (type->is_open_array_type() && type->named_type() == NULL)
3104 Type* element_type = type->array_type()->element_type()->forwarded();
3105 bool is_byte = element_type == Type::lookup_integer_type("uint8");
3106 bool is_int = element_type == Type::lookup_integer_type("int");
3107 if (is_byte || is_int)
3110 if (val->string_constant_value(&s))
3112 Expression_list* vals = new Expression_list();
3115 for (std::string::const_iterator p = s.begin();
3120 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3121 Expression* v = Expression::make_integer(&val,
3130 const char *p = s.data();
3131 const char *pend = s.data() + s.length();
3135 int adv = Lex::fetch_char(p, &c);
3138 warning_at(this->location(), 0,
3139 "invalid UTF-8 encoding");
3144 mpz_init_set_ui(val, c);
3145 Expression* v = Expression::make_integer(&val,
3153 return Expression::make_slice_composite_literal(type, vals,
3162 // Return the constant integer value if there is one.
3165 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3169 if (this->type_->integer_type() == NULL)
3175 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3177 if (!Integer_expression::check_constant(ival, this->type_,
3185 *ptype = this->type_;
3192 if (this->expr_->float_constant_value(fval, &dummy))
3194 mpfr_get_z(val, fval, GMP_RNDN);
3196 if (!Integer_expression::check_constant(val, this->type_,
3199 *ptype = this->type_;
3207 // Return the constant floating point value if there is one.
3210 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3213 if (this->type_->float_type() == NULL)
3219 if (this->expr_->float_constant_value(fval, &dummy))
3221 if (!Float_expression::check_constant(fval, this->type_,
3227 mpfr_set(val, fval, GMP_RNDN);
3229 Float_expression::constrain_float(val, this->type_);
3230 *ptype = this->type_;
3238 // Return the constant complex value if there is one.
3241 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3245 if (this->type_->complex_type() == NULL)
3253 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3255 if (!Complex_expression::check_constant(rval, ival, this->type_,
3262 mpfr_set(real, rval, GMP_RNDN);
3263 mpfr_set(imag, ival, GMP_RNDN);
3266 Complex_expression::constrain_complex(real, imag, this->type_);
3267 *ptype = this->type_;
3276 // Return the constant string value if there is one.
3279 Type_conversion_expression::do_string_constant_value(std::string* val) const
3281 if (this->type_->is_string_type()
3282 && this->expr_->type()->integer_type() != NULL)
3287 if (this->expr_->integer_constant_value(false, ival, &dummy))
3289 unsigned long ulval = mpz_get_ui(ival);
3290 if (mpz_cmp_ui(ival, ulval) == 0)
3292 Lex::append_char(ulval, true, val, this->location());
3300 // FIXME: Could handle conversion from const []int here.
3305 // Check that types are convertible.
3308 Type_conversion_expression::do_check_types(Gogo*)
3310 Type* type = this->type_;
3311 Type* expr_type = this->expr_->type();
3314 if (type->is_error_type()
3315 || type->is_undefined()
3316 || expr_type->is_error_type()
3317 || expr_type->is_undefined())
3319 // Make sure we emit an error for an undefined type.
3322 this->set_is_error();
3326 if (this->may_convert_function_types_
3327 && type->function_type() != NULL
3328 && expr_type->function_type() != NULL)
3331 if (Type::are_convertible(type, expr_type, &reason))
3334 error_at(this->location(), "%s", reason.c_str());
3335 this->set_is_error();
3338 // Get a tree for a type conversion.
3341 Type_conversion_expression::do_get_tree(Translate_context* context)
3343 Gogo* gogo = context->gogo();
3344 tree type_tree = this->type_->get_tree(gogo);
3345 tree expr_tree = this->expr_->get_tree(context);
3347 if (type_tree == error_mark_node
3348 || expr_tree == error_mark_node
3349 || TREE_TYPE(expr_tree) == error_mark_node)
3350 return error_mark_node;
3352 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3353 return fold_convert(type_tree, expr_tree);
3355 Type* type = this->type_;
3356 Type* expr_type = this->expr_->type();
3358 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3359 ret = Expression::convert_for_assignment(context, type, expr_type,
3360 expr_tree, this->location());
3361 else if (type->integer_type() != NULL)
3363 if (expr_type->integer_type() != NULL
3364 || expr_type->float_type() != NULL
3365 || expr_type->is_unsafe_pointer_type())
3366 ret = fold(convert_to_integer(type_tree, expr_tree));
3370 else if (type->float_type() != NULL)
3372 if (expr_type->integer_type() != NULL
3373 || expr_type->float_type() != NULL)
3374 ret = fold(convert_to_real(type_tree, expr_tree));
3378 else if (type->complex_type() != NULL)
3380 if (expr_type->complex_type() != NULL)
3381 ret = fold(convert_to_complex(type_tree, expr_tree));
3385 else if (type->is_string_type()
3386 && expr_type->integer_type() != NULL)
3388 expr_tree = fold_convert(integer_type_node, expr_tree);
3389 if (host_integerp(expr_tree, 0))
3391 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3393 Lex::append_char(intval, true, &s, this->location());
3394 Expression* se = Expression::make_string(s, this->location());
3395 return se->get_tree(context);
3398 static tree int_to_string_fndecl;
3399 ret = Gogo::call_builtin(&int_to_string_fndecl,
3401 "__go_int_to_string",
3405 fold_convert(integer_type_node, expr_tree));
3407 else if (type->is_string_type()
3408 && (expr_type->array_type() != NULL
3409 || (expr_type->points_to() != NULL
3410 && expr_type->points_to()->array_type() != NULL)))
3412 Type* t = expr_type;
3413 if (t->points_to() != NULL)
3416 expr_tree = build_fold_indirect_ref(expr_tree);
3418 if (!DECL_P(expr_tree))
3419 expr_tree = save_expr(expr_tree);
3420 Array_type* a = t->array_type();
3421 Type* e = a->element_type()->forwarded();
3422 gcc_assert(e->integer_type() != NULL);
3423 tree valptr = fold_convert(const_ptr_type_node,
3424 a->value_pointer_tree(gogo, expr_tree));
3425 tree len = a->length_tree(gogo, expr_tree);
3426 len = fold_convert_loc(this->location(), size_type_node, len);
3427 if (e->integer_type()->is_unsigned()
3428 && e->integer_type()->bits() == 8)
3430 static tree byte_array_to_string_fndecl;
3431 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3433 "__go_byte_array_to_string",
3436 const_ptr_type_node,
3443 gcc_assert(e == Type::lookup_integer_type("int"));
3444 static tree int_array_to_string_fndecl;
3445 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3447 "__go_int_array_to_string",
3450 const_ptr_type_node,
3456 else if (type->is_open_array_type() && expr_type->is_string_type())
3458 Type* e = type->array_type()->element_type()->forwarded();
3459 gcc_assert(e->integer_type() != NULL);
3460 if (e->integer_type()->is_unsigned()
3461 && e->integer_type()->bits() == 8)
3463 static tree string_to_byte_array_fndecl;
3464 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3466 "__go_string_to_byte_array",
3469 TREE_TYPE(expr_tree),
3474 gcc_assert(e == Type::lookup_integer_type("int"));
3475 static tree string_to_int_array_fndecl;
3476 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3478 "__go_string_to_int_array",
3481 TREE_TYPE(expr_tree),
3485 else if ((type->is_unsafe_pointer_type()
3486 && expr_type->points_to() != NULL)
3487 || (expr_type->is_unsafe_pointer_type()
3488 && type->points_to() != NULL))
3489 ret = fold_convert(type_tree, expr_tree);
3490 else if (type->is_unsafe_pointer_type()
3491 && expr_type->integer_type() != NULL)
3492 ret = convert_to_pointer(type_tree, expr_tree);
3493 else if (this->may_convert_function_types_
3494 && type->function_type() != NULL
3495 && expr_type->function_type() != NULL)
3496 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3498 ret = Expression::convert_for_assignment(context, type, expr_type,
3499 expr_tree, this->location());
3504 // Output a type conversion in a constant expression.
3507 Type_conversion_expression::do_export(Export* exp) const
3509 exp->write_c_string("convert(");
3510 exp->write_type(this->type_);
3511 exp->write_c_string(", ");
3512 this->expr_->export_expression(exp);
3513 exp->write_c_string(")");
3516 // Import a type conversion or a struct construction.
3519 Type_conversion_expression::do_import(Import* imp)
3521 imp->require_c_string("convert(");
3522 Type* type = imp->read_type();
3523 imp->require_c_string(", ");
3524 Expression* val = Expression::import_expression(imp);
3525 imp->require_c_string(")");
3526 return Expression::make_cast(type, val, imp->location());
3529 // Make a type cast expression.
3532 Expression::make_cast(Type* type, Expression* val, source_location location)
3534 if (type->is_error_type() || val->is_error_expression())
3535 return Expression::make_error(location);
3536 return new Type_conversion_expression(type, val, location);
3539 // Unary expressions.
3541 class Unary_expression : public Expression
3544 Unary_expression(Operator op, Expression* expr, source_location location)
3545 : Expression(EXPRESSION_UNARY, location),
3546 op_(op), escapes_(true), expr_(expr)
3549 // Return the operator.
3552 { return this->op_; }
3554 // Return the operand.
3557 { return this->expr_; }
3559 // Record that an address expression does not escape.
3561 set_does_not_escape()
3563 gcc_assert(this->op_ == OPERATOR_AND);
3564 this->escapes_ = false;
3567 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3568 // could be done, false if not.
3570 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3573 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3574 // could be done, false if not.
3576 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3578 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3579 // true if this could be done, false if not.
3581 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3589 do_traverse(Traverse* traverse)
3590 { return Expression::traverse(&this->expr_, traverse); }
3593 do_lower(Gogo*, Named_object*, int);
3596 do_is_constant() const;
3599 do_integer_constant_value(bool, mpz_t, Type**) const;
3602 do_float_constant_value(mpfr_t, Type**) const;
3605 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3611 do_determine_type(const Type_context*);
3614 do_check_types(Gogo*);
3619 return Expression::make_unary(this->op_, this->expr_->copy(),
3624 do_is_addressable() const
3625 { return this->op_ == OPERATOR_MULT; }
3628 do_get_tree(Translate_context*);
3631 do_export(Export*) const;
3634 // The unary operator to apply.
3636 // Normally true. False if this is an address expression which does
3637 // not escape the current function.
3643 // If we are taking the address of a composite literal, and the
3644 // contents are not constant, then we want to make a heap composite
3648 Unary_expression::do_lower(Gogo*, Named_object*, int)
3650 source_location loc = this->location();
3651 Operator op = this->op_;
3652 Expression* expr = this->expr_;
3654 if (op == OPERATOR_MULT && expr->is_type_expression())
3655 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3657 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3658 // moving x to the heap. FIXME: Is it worth doing a real escape
3659 // analysis here? This case is found in math/unsafe.go and is
3660 // therefore worth special casing.
3661 if (op == OPERATOR_MULT)
3663 Expression* e = expr;
3664 while (e->classification() == EXPRESSION_CONVERSION)
3666 Type_conversion_expression* te
3667 = static_cast<Type_conversion_expression*>(e);
3671 if (e->classification() == EXPRESSION_UNARY)
3673 Unary_expression* ue = static_cast<Unary_expression*>(e);
3674 if (ue->op_ == OPERATOR_AND)
3681 ue->set_does_not_escape();
3686 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
3687 || op == OPERATOR_NOT || op == OPERATOR_XOR)
3689 Expression* ret = NULL;
3694 if (expr->integer_constant_value(false, eval, &etype))
3698 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
3699 ret = Expression::make_integer(&val, etype, loc);
3706 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
3711 if (expr->float_constant_value(fval, &ftype))
3715 if (Unary_expression::eval_float(op, fval, val))
3716 ret = Expression::make_float(&val, ftype, loc);
3727 if (expr->complex_constant_value(fval, ival, &ftype))
3733 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
3734 ret = Expression::make_complex(&real, &imag, ftype, loc);
3748 // Return whether a unary expression is a constant.
3751 Unary_expression::do_is_constant() const
3753 if (this->op_ == OPERATOR_MULT)
3755 // Indirecting through a pointer is only constant if the object
3756 // to which the expression points is constant, but we currently
3757 // have no way to determine that.
3760 else if (this->op_ == OPERATOR_AND)
3762 // Taking the address of a variable is constant if it is a
3763 // global variable, not constant otherwise. In other cases
3764 // taking the address is probably not a constant.
3765 Var_expression* ve = this->expr_->var_expression();
3768 Named_object* no = ve->named_object();
3769 return no->is_variable() && no->var_value()->is_global();
3774 return this->expr_->is_constant();
3777 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3778 // UVAL, if known; it may be NULL. Return true if this could be done,
3782 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3783 source_location location)
3790 case OPERATOR_MINUS:
3792 return Integer_expression::check_constant(val, utype, location);
3794 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3798 || utype->integer_type() == NULL
3799 || utype->integer_type()->is_abstract())
3803 // The number of HOST_WIDE_INTs that it takes to represent
3805 size_t count = ((mpz_sizeinbase(uval, 2)
3806 + HOST_BITS_PER_WIDE_INT
3808 / HOST_BITS_PER_WIDE_INT);
3810 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3811 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3814 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3815 gcc_assert(ecount <= count);
3817 // Trim down to the number of words required by the type.
3818 size_t obits = utype->integer_type()->bits();
3819 if (!utype->integer_type()->is_unsigned())
3821 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3822 / HOST_BITS_PER_WIDE_INT);
3823 gcc_assert(ocount <= ocount);
3825 for (size_t i = 0; i < ocount; ++i)
3828 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3830 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3833 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3837 return Integer_expression::check_constant(val, utype, location);
3846 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3847 // could be done, false if not.
3850 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
3855 mpfr_set(val, uval, GMP_RNDN);
3857 case OPERATOR_MINUS:
3858 mpfr_neg(val, uval, GMP_RNDN);
3870 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3871 // if this could be done, false if not.
3874 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
3875 mpfr_t real, mpfr_t imag)
3880 mpfr_set(real, rval, GMP_RNDN);
3881 mpfr_set(imag, ival, GMP_RNDN);
3883 case OPERATOR_MINUS:
3884 mpfr_neg(real, rval, GMP_RNDN);
3885 mpfr_neg(imag, ival, GMP_RNDN);
3897 // Return the integral constant value of a unary expression, if it has one.
3900 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
3906 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
3909 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
3915 // Return the floating point constant value of a unary expression, if
3919 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
3924 if (!this->expr_->float_constant_value(uval, ptype))
3927 ret = Unary_expression::eval_float(this->op_, uval, val);
3932 // Return the complex constant value of a unary expression, if it has
3936 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
3944 if (!this->expr_->complex_constant_value(rval, ival, ptype))
3947 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
3953 // Return the type of a unary expression.
3956 Unary_expression::do_type()
3961 case OPERATOR_MINUS:
3964 return this->expr_->type();
3967 return Type::make_pointer_type(this->expr_->type());
3971 Type* subtype = this->expr_->type();
3972 Type* points_to = subtype->points_to();
3973 if (points_to == NULL)
3974 return Type::make_error_type();
3983 // Determine abstract types for a unary expression.
3986 Unary_expression::do_determine_type(const Type_context* context)
3991 case OPERATOR_MINUS:
3994 this->expr_->determine_type(context);
3998 // Taking the address of something.
4000 Type* subtype = (context->type == NULL
4002 : context->type->points_to());
4003 Type_context subcontext(subtype, false);
4004 this->expr_->determine_type(&subcontext);
4009 // Indirecting through a pointer.
4011 Type* subtype = (context->type == NULL
4013 : Type::make_pointer_type(context->type));
4014 Type_context subcontext(subtype, false);
4015 this->expr_->determine_type(&subcontext);
4024 // Check types for a unary expression.
4027 Unary_expression::do_check_types(Gogo*)
4029 Type* type = this->expr_->type();
4030 if (type->is_error_type())
4032 this->set_is_error();
4039 case OPERATOR_MINUS:
4040 if (type->integer_type() == NULL
4041 && type->float_type() == NULL
4042 && type->complex_type() == NULL)
4043 this->report_error(_("expected numeric type"));
4048 if (type->integer_type() == NULL
4049 && !type->is_boolean_type())
4050 this->report_error(_("expected integer or boolean type"));
4054 if (!this->expr_->is_addressable())
4055 this->report_error(_("invalid operand for unary %<&%>"));
4057 this->expr_->address_taken(this->escapes_);
4061 // Indirecting through a pointer.
4062 if (type->points_to() == NULL)
4063 this->report_error(_("expected pointer"));
4071 // Get a tree for a unary expression.
4074 Unary_expression::do_get_tree(Translate_context* context)
4076 tree expr = this->expr_->get_tree(context);
4077 if (expr == error_mark_node)
4078 return error_mark_node;
4080 source_location loc = this->location();
4086 case OPERATOR_MINUS:
4088 tree type = TREE_TYPE(expr);
4089 tree compute_type = excess_precision_type(type);
4090 if (compute_type != NULL_TREE)
4091 expr = ::convert(compute_type, expr);
4092 tree ret = fold_build1_loc(loc, NEGATE_EXPR,
4093 (compute_type != NULL_TREE
4097 if (compute_type != NULL_TREE)
4098 ret = ::convert(type, ret);
4103 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4104 return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr);
4106 return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr,
4107 build_int_cst(TREE_TYPE(expr), 0));
4110 return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr);
4113 // We should not see a non-constant constructor here; cases
4114 // where we would see one should have been moved onto the heap
4115 // at parse time. Taking the address of a nonconstant
4116 // constructor will not do what the programmer expects.
4117 gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4118 gcc_assert(TREE_CODE(expr) != ADDR_EXPR);
4120 // Build a decl for a constant constructor.
4121 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4123 tree decl = build_decl(this->location(), VAR_DECL,
4124 create_tmp_var_name("C"), TREE_TYPE(expr));
4125 DECL_EXTERNAL(decl) = 0;
4126 TREE_PUBLIC(decl) = 0;
4127 TREE_READONLY(decl) = 1;
4128 TREE_CONSTANT(decl) = 1;
4129 TREE_STATIC(decl) = 1;
4130 TREE_ADDRESSABLE(decl) = 1;
4131 DECL_ARTIFICIAL(decl) = 1;
4132 DECL_INITIAL(decl) = expr;
4133 rest_of_decl_compilation(decl, 1, 0);
4137 return build_fold_addr_expr_loc(loc, expr);
4141 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4143 // If we are dereferencing the pointer to a large struct, we
4144 // need to check for nil. We don't bother to check for small
4145 // structs because we expect the system to crash on a nil
4146 // pointer dereference.
4147 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4148 if (s == -1 || s >= 4096)
4151 expr = save_expr(expr);
4152 tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node,
4154 fold_convert(TREE_TYPE(expr),
4155 null_pointer_node));
4156 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4158 expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr),
4159 build3(COND_EXPR, void_type_node,
4160 compare, crash, NULL_TREE),
4164 // If the type of EXPR is a recursive pointer type, then we
4165 // need to insert a cast before indirecting.
4166 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4168 Type* pt = this->expr_->type()->points_to();
4169 tree ind = pt->get_tree(context->gogo());
4170 expr = fold_convert_loc(loc, build_pointer_type(ind), expr);
4173 return build_fold_indirect_ref_loc(loc, expr);
4181 // Export a unary expression.
4184 Unary_expression::do_export(Export* exp) const
4189 exp->write_c_string("+ ");
4191 case OPERATOR_MINUS:
4192 exp->write_c_string("- ");
4195 exp->write_c_string("! ");
4198 exp->write_c_string("^ ");
4205 this->expr_->export_expression(exp);
4208 // Import a unary expression.
4211 Unary_expression::do_import(Import* imp)
4214 switch (imp->get_char())
4220 op = OPERATOR_MINUS;
4231 imp->require_c_string(" ");
4232 Expression* expr = Expression::import_expression(imp);
4233 return Expression::make_unary(op, expr, imp->location());
4236 // Make a unary expression.
4239 Expression::make_unary(Operator op, Expression* expr, source_location location)
4241 return new Unary_expression(op, expr, location);
4244 // If this is an indirection through a pointer, return the expression
4245 // being pointed through. Otherwise return this.
4250 if (this->classification_ == EXPRESSION_UNARY)
4252 Unary_expression* ue = static_cast<Unary_expression*>(this);
4253 if (ue->op() == OPERATOR_MULT)
4254 return ue->operand();
4259 // Class Binary_expression.
4264 Binary_expression::do_traverse(Traverse* traverse)
4266 int t = Expression::traverse(&this->left_, traverse);
4267 if (t == TRAVERSE_EXIT)
4268 return TRAVERSE_EXIT;
4269 return Expression::traverse(&this->right_, traverse);
4272 // Compare integer constants according to OP.
4275 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4278 int i = mpz_cmp(left_val, right_val);
4283 case OPERATOR_NOTEQ:
4298 // Compare floating point constants according to OP.
4301 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4306 i = mpfr_cmp(left_val, right_val);
4310 mpfr_init_set(lv, left_val, GMP_RNDN);
4312 mpfr_init_set(rv, right_val, GMP_RNDN);
4313 Float_expression::constrain_float(lv, type);
4314 Float_expression::constrain_float(rv, type);
4315 i = mpfr_cmp(lv, rv);
4323 case OPERATOR_NOTEQ:
4338 // Compare complex constants according to OP. Complex numbers may
4339 // only be compared for equality.
4342 Binary_expression::compare_complex(Operator op, Type* type,
4343 mpfr_t left_real, mpfr_t left_imag,
4344 mpfr_t right_real, mpfr_t right_imag)
4348 is_equal = (mpfr_cmp(left_real, right_real) == 0
4349 && mpfr_cmp(left_imag, right_imag) == 0);
4354 mpfr_init_set(lr, left_real, GMP_RNDN);
4355 mpfr_init_set(li, left_imag, GMP_RNDN);
4358 mpfr_init_set(rr, right_real, GMP_RNDN);
4359 mpfr_init_set(ri, right_imag, GMP_RNDN);
4360 Complex_expression::constrain_complex(lr, li, type);
4361 Complex_expression::constrain_complex(rr, ri, type);
4362 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4372 case OPERATOR_NOTEQ:
4379 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4380 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4381 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4382 // this could be done, false if not.
4385 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4386 Type* right_type, mpz_t right_val,
4387 source_location location, mpz_t val)
4389 bool is_shift_op = false;
4393 case OPERATOR_ANDAND:
4395 case OPERATOR_NOTEQ:
4400 // These return boolean values. We should probably handle them
4401 // anyhow in case a type conversion is used on the result.
4404 mpz_add(val, left_val, right_val);
4406 case OPERATOR_MINUS:
4407 mpz_sub(val, left_val, right_val);
4410 mpz_ior(val, left_val, right_val);
4413 mpz_xor(val, left_val, right_val);
4416 mpz_mul(val, left_val, right_val);
4419 if (mpz_sgn(right_val) != 0)
4420 mpz_tdiv_q(val, left_val, right_val);
4423 error_at(location, "division by zero");
4429 if (mpz_sgn(right_val) != 0)
4430 mpz_tdiv_r(val, left_val, right_val);
4433 error_at(location, "division by zero");
4438 case OPERATOR_LSHIFT:
4440 unsigned long shift = mpz_get_ui(right_val);
4441 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
4443 error_at(location, "shift count overflow");
4447 mpz_mul_2exp(val, left_val, shift);
4452 case OPERATOR_RSHIFT:
4454 unsigned long shift = mpz_get_ui(right_val);
4455 if (mpz_cmp_ui(right_val, shift) != 0)
4457 error_at(location, "shift count overflow");
4461 if (mpz_cmp_ui(left_val, 0) >= 0)
4462 mpz_tdiv_q_2exp(val, left_val, shift);
4464 mpz_fdiv_q_2exp(val, left_val, shift);
4470 mpz_and(val, left_val, right_val);
4472 case OPERATOR_BITCLEAR:
4476 mpz_com(tval, right_val);
4477 mpz_and(val, left_val, tval);
4485 Type* type = left_type;
4490 else if (type != right_type && right_type != NULL)
4492 if (type->is_abstract())
4494 else if (!right_type->is_abstract())
4496 // This look like a type error which should be diagnosed
4497 // elsewhere. Don't do anything here, to avoid an
4498 // unhelpful chain of error messages.
4504 if (type != NULL && !type->is_abstract())
4506 // We have to check the operands too, as we have implicitly
4507 // coerced them to TYPE.
4508 if ((type != left_type
4509 && !Integer_expression::check_constant(left_val, type, location))
4511 && type != right_type
4512 && !Integer_expression::check_constant(right_val, type,
4514 || !Integer_expression::check_constant(val, type, location))
4521 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4522 // Return true if this could be done, false if not.
4525 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4526 Type* right_type, mpfr_t right_val,
4527 mpfr_t val, source_location location)
4532 case OPERATOR_ANDAND:
4534 case OPERATOR_NOTEQ:
4539 // These return boolean values. We should probably handle them
4540 // anyhow in case a type conversion is used on the result.
4543 mpfr_add(val, left_val, right_val, GMP_RNDN);
4545 case OPERATOR_MINUS:
4546 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4551 case OPERATOR_BITCLEAR:
4554 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4557 if (mpfr_zero_p(right_val))
4558 error_at(location, "division by zero");
4559 mpfr_div(val, left_val, right_val, GMP_RNDN);
4563 case OPERATOR_LSHIFT:
4564 case OPERATOR_RSHIFT:
4570 Type* type = left_type;
4573 else if (type != right_type && right_type != NULL)
4575 if (type->is_abstract())
4577 else if (!right_type->is_abstract())
4579 // This looks like a type error which should be diagnosed
4580 // elsewhere. Don't do anything here, to avoid an unhelpful
4581 // chain of error messages.
4586 if (type != NULL && !type->is_abstract())
4588 if ((type != left_type
4589 && !Float_expression::check_constant(left_val, type, location))
4590 || (type != right_type
4591 && !Float_expression::check_constant(right_val, type,
4593 || !Float_expression::check_constant(val, type, location))
4594 mpfr_set_ui(val, 0, GMP_RNDN);
4600 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4601 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4602 // could be done, false if not.
4605 Binary_expression::eval_complex(Operator op, Type* left_type,
4606 mpfr_t left_real, mpfr_t left_imag,
4608 mpfr_t right_real, mpfr_t right_imag,
4609 mpfr_t real, mpfr_t imag,
4610 source_location location)
4615 case OPERATOR_ANDAND:
4617 case OPERATOR_NOTEQ:
4622 // These return boolean values and must be handled differently.
4625 mpfr_add(real, left_real, right_real, GMP_RNDN);
4626 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4628 case OPERATOR_MINUS:
4629 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4630 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4635 case OPERATOR_BITCLEAR:
4639 // You might think that multiplying two complex numbers would
4640 // be simple, and you would be right, until you start to think
4641 // about getting the right answer for infinity. If one
4642 // operand here is infinity and the other is anything other
4643 // than zero or NaN, then we are going to wind up subtracting
4644 // two infinity values. That will give us a NaN, but the
4645 // correct answer is infinity.
4649 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4653 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4657 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4661 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4663 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4664 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4666 // If we get NaN on both sides, check whether it should really
4667 // be infinity. The rule is that if either side of the
4668 // complex number is infinity, then the whole value is
4669 // infinity, even if the other side is NaN. So the only case
4670 // we have to fix is the one in which both sides are NaN.
4671 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4672 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4673 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4675 bool is_infinity = false;
4679 mpfr_init_set(lr, left_real, GMP_RNDN);
4680 mpfr_init_set(li, left_imag, GMP_RNDN);
4684 mpfr_init_set(rr, right_real, GMP_RNDN);
4685 mpfr_init_set(ri, right_imag, GMP_RNDN);
4687 // If the left side is infinity, then the result is
4689 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4691 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4692 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4693 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4694 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4697 mpfr_set_ui(rr, 0, GMP_RNDN);
4698 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4702 mpfr_set_ui(ri, 0, GMP_RNDN);
4703 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4708 // If the right side is infinity, then the result is
4710 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4712 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4713 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4714 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4715 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4718 mpfr_set_ui(lr, 0, GMP_RNDN);
4719 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4723 mpfr_set_ui(li, 0, GMP_RNDN);
4724 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4729 // If we got an overflow in the intermediate computations,
4730 // then the result is infinity.
4732 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4733 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4737 mpfr_set_ui(lr, 0, GMP_RNDN);
4738 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4742 mpfr_set_ui(li, 0, GMP_RNDN);
4743 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4747 mpfr_set_ui(rr, 0, GMP_RNDN);
4748 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4752 mpfr_set_ui(ri, 0, GMP_RNDN);
4753 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4760 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4761 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4762 mpfr_mul(lirr, li, rr, GMP_RNDN);
4763 mpfr_mul(liri, li, ri, GMP_RNDN);
4764 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4765 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4766 mpfr_set_inf(real, mpfr_sgn(real));
4767 mpfr_set_inf(imag, mpfr_sgn(imag));
4784 // For complex division we want to avoid having an
4785 // intermediate overflow turn the whole result in a NaN. We
4786 // scale the values to try to avoid this.
4788 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
4789 error_at(location, "division by zero");
4795 mpfr_abs(rra, right_real, GMP_RNDN);
4796 mpfr_abs(ria, right_imag, GMP_RNDN);
4799 mpfr_max(t, rra, ria, GMP_RNDN);
4803 mpfr_init_set(rr, right_real, GMP_RNDN);
4804 mpfr_init_set(ri, right_imag, GMP_RNDN);
4806 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4808 ilogbw = mpfr_get_exp(t);
4809 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4810 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4815 mpfr_mul(denom, rr, rr, GMP_RNDN);
4816 mpfr_mul(t, ri, ri, GMP_RNDN);
4817 mpfr_add(denom, denom, t, GMP_RNDN);
4819 mpfr_mul(real, left_real, rr, GMP_RNDN);
4820 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4821 mpfr_add(real, real, t, GMP_RNDN);
4822 mpfr_div(real, real, denom, GMP_RNDN);
4823 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4825 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4826 mpfr_mul(t, left_real, ri, GMP_RNDN);
4827 mpfr_sub(imag, imag, t, GMP_RNDN);
4828 mpfr_div(imag, imag, denom, GMP_RNDN);
4829 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4831 // If we wind up with NaN on both sides, check whether we
4832 // should really have infinity. The rule is that if either
4833 // side of the complex number is infinity, then the whole
4834 // value is infinity, even if the other side is NaN. So the
4835 // only case we have to fix is the one in which both sides are
4837 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4838 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4839 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4841 if (mpfr_zero_p(denom))
4843 mpfr_set_inf(real, mpfr_sgn(rr));
4844 mpfr_mul(real, real, left_real, GMP_RNDN);
4845 mpfr_set_inf(imag, mpfr_sgn(rr));
4846 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4848 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4849 && mpfr_number_p(rr) && mpfr_number_p(ri))
4851 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
4852 mpfr_copysign(t, t, left_real, GMP_RNDN);
4855 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
4856 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
4860 mpfr_mul(t3, t, rr, GMP_RNDN);
4864 mpfr_mul(t4, t2, ri, GMP_RNDN);
4866 mpfr_add(t3, t3, t4, GMP_RNDN);
4867 mpfr_set_inf(real, mpfr_sgn(t3));
4869 mpfr_mul(t3, t2, rr, GMP_RNDN);
4870 mpfr_mul(t4, t, ri, GMP_RNDN);
4871 mpfr_sub(t3, t3, t4, GMP_RNDN);
4872 mpfr_set_inf(imag, mpfr_sgn(t3));
4878 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
4879 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
4881 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4882 mpfr_copysign(t, t, rr, GMP_RNDN);
4885 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4886 mpfr_copysign(t2, t2, ri, GMP_RNDN);
4890 mpfr_mul(t3, left_real, t, GMP_RNDN);
4894 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
4896 mpfr_add(t3, t3, t4, GMP_RNDN);
4897 mpfr_set_ui(real, 0, GMP_RNDN);
4898 mpfr_mul(real, real, t3, GMP_RNDN);
4900 mpfr_mul(t3, left_imag, t, GMP_RNDN);
4901 mpfr_mul(t4, left_real, t2, GMP_RNDN);
4902 mpfr_sub(t3, t3, t4, GMP_RNDN);
4903 mpfr_set_ui(imag, 0, GMP_RNDN);
4904 mpfr_mul(imag, imag, t3, GMP_RNDN);
4922 case OPERATOR_LSHIFT:
4923 case OPERATOR_RSHIFT:
4929 Type* type = left_type;
4932 else if (type != right_type && right_type != NULL)
4934 if (type->is_abstract())
4936 else if (!right_type->is_abstract())
4938 // This looks like a type error which should be diagnosed
4939 // elsewhere. Don't do anything here, to avoid an unhelpful
4940 // chain of error messages.
4945 if (type != NULL && !type->is_abstract())
4947 if ((type != left_type
4948 && !Complex_expression::check_constant(left_real, left_imag,
4950 || (type != right_type
4951 && !Complex_expression::check_constant(right_real, right_imag,
4953 || !Complex_expression::check_constant(real, imag, type,
4956 mpfr_set_ui(real, 0, GMP_RNDN);
4957 mpfr_set_ui(imag, 0, GMP_RNDN);
4964 // Lower a binary expression. We have to evaluate constant
4965 // expressions now, in order to implement Go's unlimited precision
4969 Binary_expression::do_lower(Gogo*, Named_object*, int)
4971 source_location location = this->location();
4972 Operator op = this->op_;
4973 Expression* left = this->left_;
4974 Expression* right = this->right_;
4976 const bool is_comparison = (op == OPERATOR_EQEQ
4977 || op == OPERATOR_NOTEQ
4978 || op == OPERATOR_LT
4979 || op == OPERATOR_LE
4980 || op == OPERATOR_GT
4981 || op == OPERATOR_GE);
4983 // Integer constant expressions.
4989 mpz_init(right_val);
4991 if (left->integer_constant_value(false, left_val, &left_type)
4992 && right->integer_constant_value(false, right_val, &right_type))
4994 Expression* ret = NULL;
4995 if (left_type != right_type
4996 && left_type != NULL
4997 && right_type != NULL
4998 && left_type->base() != right_type->base()
4999 && op != OPERATOR_LSHIFT
5000 && op != OPERATOR_RSHIFT)
5002 // May be a type error--let it be diagnosed later.
5004 else if (is_comparison)
5006 bool b = Binary_expression::compare_integer(op, left_val,
5008 ret = Expression::make_cast(Type::lookup_bool_type(),
5009 Expression::make_boolean(b, location),
5017 if (Binary_expression::eval_integer(op, left_type, left_val,
5018 right_type, right_val,
5021 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
5023 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5025 else if (left_type == NULL)
5027 else if (right_type == NULL)
5029 else if (!left_type->is_abstract()
5030 && left_type->named_type() != NULL)
5032 else if (!right_type->is_abstract()
5033 && right_type->named_type() != NULL)
5035 else if (!left_type->is_abstract())
5037 else if (!right_type->is_abstract())
5039 else if (left_type->float_type() != NULL)
5041 else if (right_type->float_type() != NULL)
5043 else if (left_type->complex_type() != NULL)
5045 else if (right_type->complex_type() != NULL)
5049 ret = Expression::make_integer(&val, type, location);
5057 mpz_clear(right_val);
5058 mpz_clear(left_val);
5062 mpz_clear(right_val);
5063 mpz_clear(left_val);
5066 // Floating point constant expressions.
5069 mpfr_init(left_val);
5072 mpfr_init(right_val);
5074 if (left->float_constant_value(left_val, &left_type)
5075 && right->float_constant_value(right_val, &right_type))
5077 Expression* ret = NULL;
5078 if (left_type != right_type
5079 && left_type != NULL
5080 && right_type != NULL
5081 && left_type->base() != right_type->base()
5082 && op != OPERATOR_LSHIFT
5083 && op != OPERATOR_RSHIFT)
5085 // May be a type error--let it be diagnosed later.
5087 else if (is_comparison)
5089 bool b = Binary_expression::compare_float(op,
5093 left_val, right_val);
5094 ret = Expression::make_boolean(b, location);
5101 if (Binary_expression::eval_float(op, left_type, left_val,
5102 right_type, right_val, val,
5105 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5106 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5108 if (left_type == NULL)
5110 else if (right_type == NULL)
5112 else if (!left_type->is_abstract()
5113 && left_type->named_type() != NULL)
5115 else if (!right_type->is_abstract()
5116 && right_type->named_type() != NULL)
5118 else if (!left_type->is_abstract())
5120 else if (!right_type->is_abstract())
5122 else if (left_type->float_type() != NULL)
5124 else if (right_type->float_type() != NULL)
5128 ret = Expression::make_float(&val, type, location);
5136 mpfr_clear(right_val);
5137 mpfr_clear(left_val);
5141 mpfr_clear(right_val);
5142 mpfr_clear(left_val);
5145 // Complex constant expressions.
5149 mpfr_init(left_real);
5150 mpfr_init(left_imag);
5155 mpfr_init(right_real);
5156 mpfr_init(right_imag);
5159 if (left->complex_constant_value(left_real, left_imag, &left_type)
5160 && right->complex_constant_value(right_real, right_imag, &right_type))
5162 Expression* ret = NULL;
5163 if (left_type != right_type
5164 && left_type != NULL
5165 && right_type != NULL
5166 && left_type->base() != right_type->base())
5168 // May be a type error--let it be diagnosed later.
5170 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5172 bool b = Binary_expression::compare_complex(op,
5180 ret = Expression::make_boolean(b, location);
5189 if (Binary_expression::eval_complex(op, left_type,
5190 left_real, left_imag,
5192 right_real, right_imag,
5196 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5197 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5199 if (left_type == NULL)
5201 else if (right_type == NULL)
5203 else if (!left_type->is_abstract()
5204 && left_type->named_type() != NULL)
5206 else if (!right_type->is_abstract()
5207 && right_type->named_type() != NULL)
5209 else if (!left_type->is_abstract())
5211 else if (!right_type->is_abstract())
5213 else if (left_type->complex_type() != NULL)
5215 else if (right_type->complex_type() != NULL)
5219 ret = Expression::make_complex(&real, &imag, type,
5228 mpfr_clear(left_real);
5229 mpfr_clear(left_imag);
5230 mpfr_clear(right_real);
5231 mpfr_clear(right_imag);
5236 mpfr_clear(left_real);
5237 mpfr_clear(left_imag);
5238 mpfr_clear(right_real);
5239 mpfr_clear(right_imag);
5242 // String constant expressions.
5243 if (op == OPERATOR_PLUS
5244 && left->type()->is_string_type()
5245 && right->type()->is_string_type())
5247 std::string left_string;
5248 std::string right_string;
5249 if (left->string_constant_value(&left_string)
5250 && right->string_constant_value(&right_string))
5251 return Expression::make_string(left_string + right_string, location);
5257 // Return the integer constant value, if it has one.
5260 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5266 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5269 mpz_clear(left_val);
5274 mpz_init(right_val);
5276 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5279 mpz_clear(right_val);
5280 mpz_clear(left_val);
5285 if (left_type != right_type
5286 && left_type != NULL
5287 && right_type != NULL
5288 && left_type->base() != right_type->base()
5289 && this->op_ != OPERATOR_RSHIFT
5290 && this->op_ != OPERATOR_LSHIFT)
5293 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5294 right_type, right_val,
5295 this->location(), val);
5297 mpz_clear(right_val);
5298 mpz_clear(left_val);
5306 // Return the floating point constant value, if it has one.
5309 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5312 mpfr_init(left_val);
5314 if (!this->left_->float_constant_value(left_val, &left_type))
5316 mpfr_clear(left_val);
5321 mpfr_init(right_val);
5323 if (!this->right_->float_constant_value(right_val, &right_type))
5325 mpfr_clear(right_val);
5326 mpfr_clear(left_val);
5331 if (left_type != right_type
5332 && left_type != NULL
5333 && right_type != NULL
5334 && left_type->base() != right_type->base())
5337 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5338 right_type, right_val,
5339 val, this->location());
5341 mpfr_clear(left_val);
5342 mpfr_clear(right_val);
5350 // Return the complex constant value, if it has one.
5353 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5358 mpfr_init(left_real);
5359 mpfr_init(left_imag);
5361 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5363 mpfr_clear(left_real);
5364 mpfr_clear(left_imag);
5370 mpfr_init(right_real);
5371 mpfr_init(right_imag);
5373 if (!this->right_->complex_constant_value(right_real, right_imag,
5376 mpfr_clear(left_real);
5377 mpfr_clear(left_imag);
5378 mpfr_clear(right_real);
5379 mpfr_clear(right_imag);
5384 if (left_type != right_type
5385 && left_type != NULL
5386 && right_type != NULL
5387 && left_type->base() != right_type->base())
5390 ret = Binary_expression::eval_complex(this->op_, left_type,
5391 left_real, left_imag,
5393 right_real, right_imag,
5396 mpfr_clear(left_real);
5397 mpfr_clear(left_imag);
5398 mpfr_clear(right_real);
5399 mpfr_clear(right_imag);
5407 // Note that the value is being discarded.
5410 Binary_expression::do_discarding_value()
5412 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5413 this->right_->discarding_value();
5415 this->warn_about_unused_value();
5421 Binary_expression::do_type()
5423 if (this->classification() == EXPRESSION_ERROR)
5424 return Type::make_error_type();
5429 case OPERATOR_ANDAND:
5431 case OPERATOR_NOTEQ:
5436 return Type::lookup_bool_type();
5439 case OPERATOR_MINUS:
5446 case OPERATOR_BITCLEAR:
5448 Type* left_type = this->left_->type();
5449 Type* right_type = this->right_->type();
5450 if (left_type->is_error_type())
5452 else if (right_type->is_error_type())
5454 else if (!Type::are_compatible_for_binop(left_type, right_type))
5456 this->report_error(_("incompatible types in binary expression"));
5457 return Type::make_error_type();
5459 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
5461 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5463 else if (!left_type->is_abstract())
5465 else if (!right_type->is_abstract())
5467 else if (left_type->complex_type() != NULL)
5469 else if (right_type->complex_type() != NULL)
5471 else if (left_type->float_type() != NULL)
5473 else if (right_type->float_type() != NULL)
5479 case OPERATOR_LSHIFT:
5480 case OPERATOR_RSHIFT:
5481 return this->left_->type();
5488 // Set type for a binary expression.
5491 Binary_expression::do_determine_type(const Type_context* context)
5493 Type* tleft = this->left_->type();
5494 Type* tright = this->right_->type();
5496 // Both sides should have the same type, except for the shift
5497 // operations. For a comparison, we should ignore the incoming
5500 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5501 || this->op_ == OPERATOR_RSHIFT);
5503 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5504 || this->op_ == OPERATOR_NOTEQ
5505 || this->op_ == OPERATOR_LT
5506 || this->op_ == OPERATOR_LE
5507 || this->op_ == OPERATOR_GT
5508 || this->op_ == OPERATOR_GE);
5510 Type_context subcontext(*context);
5514 // In a comparison, the context does not determine the types of
5516 subcontext.type = NULL;
5519 // Set the context for the left hand operand.
5522 // The right hand operand plays no role in determining the type
5523 // of the left hand operand. A shift of an abstract integer in
5524 // a string context gets special treatment, which may be a
5526 if (subcontext.type != NULL
5527 && subcontext.type->is_string_type()
5528 && tleft->is_abstract())
5529 error_at(this->location(), "shift of non-integer operand");
5531 else if (!tleft->is_abstract())
5532 subcontext.type = tleft;
5533 else if (!tright->is_abstract())
5534 subcontext.type = tright;
5535 else if (subcontext.type == NULL)
5537 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5538 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5539 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5541 // Both sides have an abstract integer, abstract float, or
5542 // abstract complex type. Just let CONTEXT determine
5543 // whether they may remain abstract or not.
5545 else if (tleft->complex_type() != NULL)
5546 subcontext.type = tleft;
5547 else if (tright->complex_type() != NULL)
5548 subcontext.type = tright;
5549 else if (tleft->float_type() != NULL)
5550 subcontext.type = tleft;
5551 else if (tright->float_type() != NULL)
5552 subcontext.type = tright;
5554 subcontext.type = tleft;
5556 if (subcontext.type != NULL && !context->may_be_abstract)
5557 subcontext.type = subcontext.type->make_non_abstract_type();
5560 this->left_->determine_type(&subcontext);
5562 // The context for the right hand operand is the same as for the
5563 // left hand operand, except for a shift operator.
5566 subcontext.type = Type::lookup_integer_type("uint");
5567 subcontext.may_be_abstract = false;
5570 this->right_->determine_type(&subcontext);
5573 // Report an error if the binary operator OP does not support TYPE.
5574 // Return whether the operation is OK. This should not be used for
5578 Binary_expression::check_operator_type(Operator op, Type* type,
5579 source_location location)
5584 case OPERATOR_ANDAND:
5585 if (!type->is_boolean_type())
5587 error_at(location, "expected boolean type");
5593 case OPERATOR_NOTEQ:
5594 if (type->integer_type() == NULL
5595 && type->float_type() == NULL
5596 && type->complex_type() == NULL
5597 && !type->is_string_type()
5598 && type->points_to() == NULL
5599 && !type->is_nil_type()
5600 && !type->is_boolean_type()
5601 && type->interface_type() == NULL
5602 && (type->array_type() == NULL
5603 || type->array_type()->length() != NULL)
5604 && type->map_type() == NULL
5605 && type->channel_type() == NULL
5606 && type->function_type() == NULL)
5609 ("expected integer, floating, complex, string, pointer, "
5610 "boolean, interface, slice, map, channel, "
5611 "or function type"));
5620 if (type->integer_type() == NULL
5621 && type->float_type() == NULL
5622 && !type->is_string_type())
5624 error_at(location, "expected integer, floating, or string type");
5630 case OPERATOR_PLUSEQ:
5631 if (type->integer_type() == NULL
5632 && type->float_type() == NULL
5633 && type->complex_type() == NULL
5634 && !type->is_string_type())
5637 "expected integer, floating, complex, or string type");
5642 case OPERATOR_MINUS:
5643 case OPERATOR_MINUSEQ:
5645 case OPERATOR_MULTEQ:
5647 case OPERATOR_DIVEQ:
5648 if (type->integer_type() == NULL
5649 && type->float_type() == NULL
5650 && type->complex_type() == NULL)
5652 error_at(location, "expected integer, floating, or complex type");
5658 case OPERATOR_MODEQ:
5662 case OPERATOR_ANDEQ:
5664 case OPERATOR_XOREQ:
5665 case OPERATOR_BITCLEAR:
5666 case OPERATOR_BITCLEAREQ:
5667 if (type->integer_type() == NULL)
5669 error_at(location, "expected integer type");
5684 Binary_expression::do_check_types(Gogo*)
5686 if (this->classification() == EXPRESSION_ERROR)
5689 Type* left_type = this->left_->type();
5690 Type* right_type = this->right_->type();
5691 if (left_type->is_error_type() || right_type->is_error_type())
5693 this->set_is_error();
5697 if (this->op_ == OPERATOR_EQEQ
5698 || this->op_ == OPERATOR_NOTEQ
5699 || this->op_ == OPERATOR_LT
5700 || this->op_ == OPERATOR_LE
5701 || this->op_ == OPERATOR_GT
5702 || this->op_ == OPERATOR_GE)
5704 if (!Type::are_assignable(left_type, right_type, NULL)
5705 && !Type::are_assignable(right_type, left_type, NULL))
5707 this->report_error(_("incompatible types in binary expression"));
5710 if (!Binary_expression::check_operator_type(this->op_, left_type,
5712 || !Binary_expression::check_operator_type(this->op_, right_type,
5715 this->set_is_error();
5719 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5721 if (!Type::are_compatible_for_binop(left_type, right_type))
5723 this->report_error(_("incompatible types in binary expression"));
5726 if (!Binary_expression::check_operator_type(this->op_, left_type,
5729 this->set_is_error();
5735 if (left_type->integer_type() == NULL)
5736 this->report_error(_("shift of non-integer operand"));
5738 if (!right_type->is_abstract()
5739 && (right_type->integer_type() == NULL
5740 || !right_type->integer_type()->is_unsigned()))
5741 this->report_error(_("shift count not unsigned integer"));
5747 if (this->right_->integer_constant_value(true, val, &type))
5749 if (mpz_sgn(val) < 0)
5751 this->report_error(_("negative shift count"));
5753 source_location rloc = this->right_->location();
5754 this->right_ = Expression::make_integer(&val, right_type,
5763 // Get a tree for a binary expression.
5766 Binary_expression::do_get_tree(Translate_context* context)
5768 tree left = this->left_->get_tree(context);
5769 tree right = this->right_->get_tree(context);
5771 if (left == error_mark_node || right == error_mark_node)
5772 return error_mark_node;
5774 enum tree_code code;
5775 bool use_left_type = true;
5776 bool is_shift_op = false;
5780 case OPERATOR_NOTEQ:
5785 return Expression::comparison_tree(context, this->op_,
5786 this->left_->type(), left,
5787 this->right_->type(), right,
5791 code = TRUTH_ORIF_EXPR;
5792 use_left_type = false;
5794 case OPERATOR_ANDAND:
5795 code = TRUTH_ANDIF_EXPR;
5796 use_left_type = false;
5801 case OPERATOR_MINUS:
5805 code = BIT_IOR_EXPR;
5808 code = BIT_XOR_EXPR;
5815 Type *t = this->left_->type();
5816 if (t->float_type() != NULL || t->complex_type() != NULL)
5819 code = TRUNC_DIV_EXPR;
5823 code = TRUNC_MOD_EXPR;
5825 case OPERATOR_LSHIFT:
5829 case OPERATOR_RSHIFT:
5834 code = BIT_AND_EXPR;
5836 case OPERATOR_BITCLEAR:
5837 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5838 code = BIT_AND_EXPR;
5844 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5846 if (this->left_->type()->is_string_type())
5848 gcc_assert(this->op_ == OPERATOR_PLUS);
5849 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5850 static tree string_plus_decl;
5851 return Gogo::call_builtin(&string_plus_decl,
5862 tree compute_type = excess_precision_type(type);
5863 if (compute_type != NULL_TREE)
5865 left = ::convert(compute_type, left);
5866 right = ::convert(compute_type, right);
5869 tree eval_saved = NULL_TREE;
5872 // Make sure the values are evaluated.
5873 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
5875 left = save_expr(left);
5878 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
5880 right = save_expr(right);
5881 if (eval_saved == NULL_TREE)
5884 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
5885 void_type_node, eval_saved, right);
5889 tree ret = fold_build2_loc(this->location(),
5891 compute_type != NULL_TREE ? compute_type : type,
5894 if (compute_type != NULL_TREE)
5895 ret = ::convert(type, ret);
5897 // In Go, a shift larger than the size of the type is well-defined.
5898 // This is not true in GENERIC, so we need to insert a conditional.
5901 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
5902 gcc_assert(this->left_->type()->integer_type() != NULL);
5903 int bits = TYPE_PRECISION(TREE_TYPE(left));
5905 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
5906 build_int_cst_type(TREE_TYPE(right), bits));
5908 tree overflow_result = fold_convert_loc(this->location(),
5911 if (this->op_ == OPERATOR_RSHIFT
5912 && !this->left_->type()->integer_type()->is_unsigned())
5914 tree neg = fold_build2_loc(this->location(), LT_EXPR,
5915 boolean_type_node, left,
5916 fold_convert_loc(this->location(),
5918 integer_zero_node));
5919 tree neg_one = fold_build2_loc(this->location(),
5920 MINUS_EXPR, TREE_TYPE(left),
5921 fold_convert_loc(this->location(),
5924 fold_convert_loc(this->location(),
5927 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
5928 TREE_TYPE(left), neg, neg_one,
5932 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
5933 compare, ret, overflow_result);
5935 if (eval_saved != NULL_TREE)
5936 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
5937 TREE_TYPE(ret), eval_saved, ret);
5943 // Export a binary expression.
5946 Binary_expression::do_export(Export* exp) const
5948 exp->write_c_string("(");
5949 this->left_->export_expression(exp);
5953 exp->write_c_string(" || ");
5955 case OPERATOR_ANDAND:
5956 exp->write_c_string(" && ");
5959 exp->write_c_string(" == ");
5961 case OPERATOR_NOTEQ:
5962 exp->write_c_string(" != ");
5965 exp->write_c_string(" < ");
5968 exp->write_c_string(" <= ");
5971 exp->write_c_string(" > ");
5974 exp->write_c_string(" >= ");
5977 exp->write_c_string(" + ");
5979 case OPERATOR_MINUS:
5980 exp->write_c_string(" - ");
5983 exp->write_c_string(" | ");
5986 exp->write_c_string(" ^ ");
5989 exp->write_c_string(" * ");
5992 exp->write_c_string(" / ");
5995 exp->write_c_string(" % ");
5997 case OPERATOR_LSHIFT:
5998 exp->write_c_string(" << ");
6000 case OPERATOR_RSHIFT:
6001 exp->write_c_string(" >> ");
6004 exp->write_c_string(" & ");
6006 case OPERATOR_BITCLEAR:
6007 exp->write_c_string(" &^ ");
6012 this->right_->export_expression(exp);
6013 exp->write_c_string(")");
6016 // Import a binary expression.
6019 Binary_expression::do_import(Import* imp)
6021 imp->require_c_string("(");
6023 Expression* left = Expression::import_expression(imp);
6026 if (imp->match_c_string(" || "))
6031 else if (imp->match_c_string(" && "))
6033 op = OPERATOR_ANDAND;
6036 else if (imp->match_c_string(" == "))
6041 else if (imp->match_c_string(" != "))
6043 op = OPERATOR_NOTEQ;
6046 else if (imp->match_c_string(" < "))
6051 else if (imp->match_c_string(" <= "))
6056 else if (imp->match_c_string(" > "))
6061 else if (imp->match_c_string(" >= "))
6066 else if (imp->match_c_string(" + "))
6071 else if (imp->match_c_string(" - "))
6073 op = OPERATOR_MINUS;
6076 else if (imp->match_c_string(" | "))
6081 else if (imp->match_c_string(" ^ "))
6086 else if (imp->match_c_string(" * "))
6091 else if (imp->match_c_string(" / "))
6096 else if (imp->match_c_string(" % "))
6101 else if (imp->match_c_string(" << "))
6103 op = OPERATOR_LSHIFT;
6106 else if (imp->match_c_string(" >> "))
6108 op = OPERATOR_RSHIFT;
6111 else if (imp->match_c_string(" & "))
6116 else if (imp->match_c_string(" &^ "))
6118 op = OPERATOR_BITCLEAR;
6123 error_at(imp->location(), "unrecognized binary operator");
6124 return Expression::make_error(imp->location());
6127 Expression* right = Expression::import_expression(imp);
6129 imp->require_c_string(")");
6131 return Expression::make_binary(op, left, right, imp->location());
6134 // Make a binary expression.
6137 Expression::make_binary(Operator op, Expression* left, Expression* right,
6138 source_location location)
6140 return new Binary_expression(op, left, right, location);
6143 // Implement a comparison.
6146 Expression::comparison_tree(Translate_context* context, Operator op,
6147 Type* left_type, tree left_tree,
6148 Type* right_type, tree right_tree,
6149 source_location location)
6151 enum tree_code code;
6157 case OPERATOR_NOTEQ:
6176 if (left_type->is_string_type() && right_type->is_string_type())
6178 tree string_type = Type::make_string_type()->get_tree(context->gogo());
6179 static tree string_compare_decl;
6180 left_tree = Gogo::call_builtin(&string_compare_decl,
6189 right_tree = build_int_cst_type(integer_type_node, 0);
6191 else if ((left_type->interface_type() != NULL
6192 && right_type->interface_type() == NULL
6193 && !right_type->is_nil_type())
6194 || (left_type->interface_type() == NULL
6195 && !left_type->is_nil_type()
6196 && right_type->interface_type() != NULL))
6198 // Comparing an interface value to a non-interface value.
6199 if (left_type->interface_type() == NULL)
6201 std::swap(left_type, right_type);
6202 std::swap(left_tree, right_tree);
6205 // The right operand is not an interface. We need to take its
6206 // address if it is not a pointer.
6209 if (right_type->points_to() != NULL)
6211 make_tmp = NULL_TREE;
6214 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
6216 make_tmp = NULL_TREE;
6217 arg = build_fold_addr_expr_loc(location, right_tree);
6218 if (DECL_P(right_tree))
6219 TREE_ADDRESSABLE(right_tree) = 1;
6223 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6224 get_name(right_tree));
6225 DECL_IGNORED_P(tmp) = 0;
6226 DECL_INITIAL(tmp) = right_tree;
6227 TREE_ADDRESSABLE(tmp) = 1;
6228 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6229 SET_EXPR_LOCATION(make_tmp, location);
6230 arg = build_fold_addr_expr_loc(location, tmp);
6232 arg = fold_convert_loc(location, ptr_type_node, arg);
6234 tree descriptor = right_type->type_descriptor_pointer(context->gogo());
6236 if (left_type->interface_type()->is_empty())
6238 static tree empty_interface_value_compare_decl;
6239 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6241 "__go_empty_interface_value_compare",
6244 TREE_TYPE(left_tree),
6246 TREE_TYPE(descriptor),
6250 if (left_tree == error_mark_node)
6251 return error_mark_node;
6252 // This can panic if the type is not comparable.
6253 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6257 static tree interface_value_compare_decl;
6258 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6260 "__go_interface_value_compare",
6263 TREE_TYPE(left_tree),
6265 TREE_TYPE(descriptor),
6269 if (left_tree == error_mark_node)
6270 return error_mark_node;
6271 // This can panic if the type is not comparable.
6272 TREE_NOTHROW(interface_value_compare_decl) = 0;
6274 right_tree = build_int_cst_type(integer_type_node, 0);
6276 if (make_tmp != NULL_TREE)
6277 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6280 else if (left_type->interface_type() != NULL
6281 && right_type->interface_type() != NULL)
6283 if (left_type->interface_type()->is_empty()
6284 && right_type->interface_type()->is_empty())
6286 static tree empty_interface_compare_decl;
6287 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6289 "__go_empty_interface_compare",
6292 TREE_TYPE(left_tree),
6294 TREE_TYPE(right_tree),
6296 if (left_tree == error_mark_node)
6297 return error_mark_node;
6298 // This can panic if the type is uncomparable.
6299 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6301 else if (!left_type->interface_type()->is_empty()
6302 && !right_type->interface_type()->is_empty())
6304 static tree interface_compare_decl;
6305 left_tree = Gogo::call_builtin(&interface_compare_decl,
6307 "__go_interface_compare",
6310 TREE_TYPE(left_tree),
6312 TREE_TYPE(right_tree),
6314 if (left_tree == error_mark_node)
6315 return error_mark_node;
6316 // This can panic if the type is uncomparable.
6317 TREE_NOTHROW(interface_compare_decl) = 0;
6321 if (left_type->interface_type()->is_empty())
6323 gcc_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6324 std::swap(left_type, right_type);
6325 std::swap(left_tree, right_tree);
6327 gcc_assert(!left_type->interface_type()->is_empty());
6328 gcc_assert(right_type->interface_type()->is_empty());
6329 static tree interface_empty_compare_decl;
6330 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
6332 "__go_interface_empty_compare",
6335 TREE_TYPE(left_tree),
6337 TREE_TYPE(right_tree),
6339 if (left_tree == error_mark_node)
6340 return error_mark_node;
6341 // This can panic if the type is uncomparable.
6342 TREE_NOTHROW(interface_empty_compare_decl) = 0;
6345 right_tree = build_int_cst_type(integer_type_node, 0);
6348 if (left_type->is_nil_type()
6349 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6351 std::swap(left_type, right_type);
6352 std::swap(left_tree, right_tree);
6355 if (right_type->is_nil_type())
6357 if (left_type->array_type() != NULL
6358 && left_type->array_type()->length() == NULL)
6360 Array_type* at = left_type->array_type();
6361 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6362 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6364 else if (left_type->interface_type() != NULL)
6366 // An interface is nil if the first field is nil.
6367 tree left_type_tree = TREE_TYPE(left_tree);
6368 gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6369 tree field = TYPE_FIELDS(left_type_tree);
6370 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6372 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6376 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6377 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6381 if (left_tree == error_mark_node || right_tree == error_mark_node)
6382 return error_mark_node;
6384 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6385 if (CAN_HAVE_LOCATION_P(ret))
6386 SET_EXPR_LOCATION(ret, location);
6390 // Class Bound_method_expression.
6395 Bound_method_expression::do_traverse(Traverse* traverse)
6397 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
6398 return TRAVERSE_EXIT;
6399 return Expression::traverse(&this->method_, traverse);
6402 // Return the type of a bound method expression. The type of this
6403 // object is really the type of the method with no receiver. We
6404 // should be able to get away with just returning the type of the
6408 Bound_method_expression::do_type()
6410 return this->method_->type();
6413 // Determine the types of a method expression.
6416 Bound_method_expression::do_determine_type(const Type_context*)
6418 this->method_->determine_type_no_context();
6419 Type* mtype = this->method_->type();
6420 Function_type* fntype = mtype == NULL ? NULL : mtype->function_type();
6421 if (fntype == NULL || !fntype->is_method())
6422 this->expr_->determine_type_no_context();
6425 Type_context subcontext(fntype->receiver()->type(), false);
6426 this->expr_->determine_type(&subcontext);
6430 // Check the types of a method expression.
6433 Bound_method_expression::do_check_types(Gogo*)
6435 Type* type = this->method_->type()->deref();
6437 || type->function_type() == NULL
6438 || !type->function_type()->is_method())
6439 this->report_error(_("object is not a method"));
6442 Type* rtype = type->function_type()->receiver()->type()->deref();
6443 Type* etype = (this->expr_type_ != NULL
6445 : this->expr_->type());
6446 etype = etype->deref();
6447 if (!Type::are_identical(rtype, etype, true, NULL))
6448 this->report_error(_("method type does not match object type"));
6452 // Get the tree for a method expression. There is no standard tree
6453 // representation for this. The only places it may currently be used
6454 // are in a Call_expression or a Go_statement, which will take it
6455 // apart directly. So this has nothing to do at present.
6458 Bound_method_expression::do_get_tree(Translate_context*)
6460 error_at(this->location(), "reference to method other than calling it");
6461 return error_mark_node;
6464 // Make a method expression.
6466 Bound_method_expression*
6467 Expression::make_bound_method(Expression* expr, Expression* method,
6468 source_location location)
6470 return new Bound_method_expression(expr, method, location);
6473 // Class Builtin_call_expression. This is used for a call to a
6474 // builtin function.
6476 class Builtin_call_expression : public Call_expression
6479 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6480 bool is_varargs, source_location location);
6483 // This overrides Call_expression::do_lower.
6485 do_lower(Gogo*, Named_object*, int);
6488 do_is_constant() const;
6491 do_integer_constant_value(bool, mpz_t, Type**) const;
6494 do_float_constant_value(mpfr_t, Type**) const;
6497 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6503 do_determine_type(const Type_context*);
6506 do_check_types(Gogo*);
6511 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6512 this->args()->copy(),
6518 do_get_tree(Translate_context*);
6521 do_export(Export*) const;
6524 do_is_recover_call() const;
6527 do_set_recover_arg(Expression*);
6530 // The builtin functions.
6531 enum Builtin_function_code
6535 // Predeclared builtin functions.
6551 // Builtin functions from the unsafe package.
6564 real_imag_type(Type*);
6567 complex_type(Type*);
6569 // A pointer back to the general IR structure. This avoids a global
6570 // variable, or passing it around everywhere.
6572 // The builtin function being called.
6573 Builtin_function_code code_;
6574 // Used to stop endless loops when the length of an array uses len
6575 // or cap of the array itself.
6579 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6581 Expression_list* args,
6583 source_location location)
6584 : Call_expression(fn, args, is_varargs, location),
6585 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
6587 Func_expression* fnexp = this->fn()->func_expression();
6588 gcc_assert(fnexp != NULL);
6589 const std::string& name(fnexp->named_object()->name());
6590 if (name == "append")
6591 this->code_ = BUILTIN_APPEND;
6592 else if (name == "cap")
6593 this->code_ = BUILTIN_CAP;
6594 else if (name == "close")
6595 this->code_ = BUILTIN_CLOSE;
6596 else if (name == "complex")
6597 this->code_ = BUILTIN_COMPLEX;
6598 else if (name == "copy")
6599 this->code_ = BUILTIN_COPY;
6600 else if (name == "imag")
6601 this->code_ = BUILTIN_IMAG;
6602 else if (name == "len")
6603 this->code_ = BUILTIN_LEN;
6604 else if (name == "make")
6605 this->code_ = BUILTIN_MAKE;
6606 else if (name == "new")
6607 this->code_ = BUILTIN_NEW;
6608 else if (name == "panic")
6609 this->code_ = BUILTIN_PANIC;
6610 else if (name == "print")
6611 this->code_ = BUILTIN_PRINT;
6612 else if (name == "println")
6613 this->code_ = BUILTIN_PRINTLN;
6614 else if (name == "real")
6615 this->code_ = BUILTIN_REAL;
6616 else if (name == "recover")
6617 this->code_ = BUILTIN_RECOVER;
6618 else if (name == "Alignof")
6619 this->code_ = BUILTIN_ALIGNOF;
6620 else if (name == "Offsetof")
6621 this->code_ = BUILTIN_OFFSETOF;
6622 else if (name == "Sizeof")
6623 this->code_ = BUILTIN_SIZEOF;
6628 // Return whether this is a call to recover. This is a virtual
6629 // function called from the parent class.
6632 Builtin_call_expression::do_is_recover_call() const
6634 if (this->classification() == EXPRESSION_ERROR)
6636 return this->code_ == BUILTIN_RECOVER;
6639 // Set the argument for a call to recover.
6642 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6644 const Expression_list* args = this->args();
6645 gcc_assert(args == NULL || args->empty());
6646 Expression_list* new_args = new Expression_list();
6647 new_args->push_back(arg);
6648 this->set_args(new_args);
6651 // A traversal class which looks for a call expression.
6653 class Find_call_expression : public Traverse
6656 Find_call_expression()
6657 : Traverse(traverse_expressions),
6662 expression(Expression**);
6666 { return this->found_; }
6673 Find_call_expression::expression(Expression** pexpr)
6675 if ((*pexpr)->call_expression() != NULL)
6677 this->found_ = true;
6678 return TRAVERSE_EXIT;
6680 return TRAVERSE_CONTINUE;
6683 // Lower a builtin call expression. This turns new and make into
6684 // specific expressions. We also convert to a constant if we can.
6687 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int)
6689 if (this->code_ == BUILTIN_NEW)
6691 const Expression_list* args = this->args();
6692 if (args == NULL || args->size() < 1)
6693 this->report_error(_("not enough arguments"));
6694 else if (args->size() > 1)
6695 this->report_error(_("too many arguments"));
6698 Expression* arg = args->front();
6699 if (!arg->is_type_expression())
6701 error_at(arg->location(), "expected type");
6702 this->set_is_error();
6705 return Expression::make_allocation(arg->type(), this->location());
6708 else if (this->code_ == BUILTIN_MAKE)
6710 const Expression_list* args = this->args();
6711 if (args == NULL || args->size() < 1)
6712 this->report_error(_("not enough arguments"));
6715 Expression* arg = args->front();
6716 if (!arg->is_type_expression())
6718 error_at(arg->location(), "expected type");
6719 this->set_is_error();
6723 Expression_list* newargs;
6724 if (args->size() == 1)
6728 newargs = new Expression_list();
6729 Expression_list::const_iterator p = args->begin();
6731 for (; p != args->end(); ++p)
6732 newargs->push_back(*p);
6734 return Expression::make_make(arg->type(), newargs,
6739 else if (this->is_constant())
6741 // We can only lower len and cap if there are no function calls
6742 // in the arguments. Otherwise we have to make the call.
6743 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6745 Expression* arg = this->one_arg();
6746 if (!arg->is_constant())
6748 Find_call_expression find_call;
6749 Expression::traverse(&arg, &find_call);
6750 if (find_call.found())
6758 if (this->integer_constant_value(true, ival, &type))
6760 Expression* ret = Expression::make_integer(&ival, type,
6769 if (this->float_constant_value(rval, &type))
6771 Expression* ret = Expression::make_float(&rval, type,
6779 if (this->complex_constant_value(rval, imag, &type))
6781 Expression* ret = Expression::make_complex(&rval, &imag, type,
6790 else if (this->code_ == BUILTIN_RECOVER)
6792 if (function != NULL)
6793 function->func_value()->set_calls_recover();
6796 // Calling recover outside of a function always returns the
6797 // nil empty interface.
6798 Type* eface = Type::make_interface_type(NULL, this->location());
6799 return Expression::make_cast(eface,
6800 Expression::make_nil(this->location()),
6804 else if (this->code_ == BUILTIN_APPEND)
6806 // Lower the varargs.
6807 const Expression_list* args = this->args();
6808 if (args == NULL || args->empty())
6810 Type* slice_type = args->front()->type();
6811 if (!slice_type->is_open_array_type())
6813 error_at(args->front()->location(), "argument 1 must be a slice");
6814 this->set_is_error();
6817 return this->lower_varargs(gogo, function, slice_type, 2);
6823 // Return the type of the real or imag functions, given the type of
6824 // the argument. We need to map complex to float, complex64 to
6825 // float32, and complex128 to float64, so it has to be done by name.
6826 // This returns NULL if it can't figure out the type.
6829 Builtin_call_expression::real_imag_type(Type* arg_type)
6831 if (arg_type == NULL || arg_type->is_abstract())
6833 Named_type* nt = arg_type->named_type();
6836 while (nt->real_type()->named_type() != NULL)
6837 nt = nt->real_type()->named_type();
6838 if (nt->name() == "complex64")
6839 return Type::lookup_float_type("float32");
6840 else if (nt->name() == "complex128")
6841 return Type::lookup_float_type("float64");
6846 // Return the type of the complex function, given the type of one of the
6847 // argments. Like real_imag_type, we have to map by name.
6850 Builtin_call_expression::complex_type(Type* arg_type)
6852 if (arg_type == NULL || arg_type->is_abstract())
6854 Named_type* nt = arg_type->named_type();
6857 while (nt->real_type()->named_type() != NULL)
6858 nt = nt->real_type()->named_type();
6859 if (nt->name() == "float32")
6860 return Type::lookup_complex_type("complex64");
6861 else if (nt->name() == "float64")
6862 return Type::lookup_complex_type("complex128");
6867 // Return a single argument, or NULL if there isn't one.
6870 Builtin_call_expression::one_arg() const
6872 const Expression_list* args = this->args();
6873 if (args->size() != 1)
6875 return args->front();
6878 // Return whether this is constant: len of a string, or len or cap of
6879 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6882 Builtin_call_expression::do_is_constant() const
6884 switch (this->code_)
6892 Expression* arg = this->one_arg();
6895 Type* arg_type = arg->type();
6897 if (arg_type->points_to() != NULL
6898 && arg_type->points_to()->array_type() != NULL
6899 && !arg_type->points_to()->is_open_array_type())
6900 arg_type = arg_type->points_to();
6902 if (arg_type->array_type() != NULL
6903 && arg_type->array_type()->length() != NULL)
6906 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6909 bool ret = arg->is_constant();
6910 this->seen_ = false;
6916 case BUILTIN_SIZEOF:
6917 case BUILTIN_ALIGNOF:
6918 return this->one_arg() != NULL;
6920 case BUILTIN_OFFSETOF:
6922 Expression* arg = this->one_arg();
6925 return arg->field_reference_expression() != NULL;
6928 case BUILTIN_COMPLEX:
6930 const Expression_list* args = this->args();
6931 if (args != NULL && args->size() == 2)
6932 return args->front()->is_constant() && args->back()->is_constant();
6939 Expression* arg = this->one_arg();
6940 return arg != NULL && arg->is_constant();
6950 // Return an integer constant value if possible.
6953 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
6957 if (this->code_ == BUILTIN_LEN
6958 || this->code_ == BUILTIN_CAP)
6960 Expression* arg = this->one_arg();
6963 Type* arg_type = arg->type();
6965 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6968 if (arg->string_constant_value(&sval))
6970 mpz_set_ui(val, sval.length());
6971 *ptype = Type::lookup_integer_type("int");
6976 if (arg_type->points_to() != NULL
6977 && arg_type->points_to()->array_type() != NULL
6978 && !arg_type->points_to()->is_open_array_type())
6979 arg_type = arg_type->points_to();
6981 if (arg_type->array_type() != NULL
6982 && arg_type->array_type()->length() != NULL)
6986 Expression* e = arg_type->array_type()->length();
6988 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
6989 this->seen_ = false;
6992 *ptype = Type::lookup_integer_type("int");
6997 else if (this->code_ == BUILTIN_SIZEOF
6998 || this->code_ == BUILTIN_ALIGNOF)
7000 Expression* arg = this->one_arg();
7003 Type* arg_type = arg->type();
7004 if (arg_type->is_error_type() || arg_type->is_undefined())
7006 if (arg_type->is_abstract())
7008 if (arg_type->named_type() != NULL)
7009 arg_type->named_type()->convert(this->gogo_);
7010 tree arg_type_tree = arg_type->get_tree(this->gogo_);
7011 if (arg_type_tree == error_mark_node)
7013 unsigned long val_long;
7014 if (this->code_ == BUILTIN_SIZEOF)
7016 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
7017 gcc_assert(TREE_CODE(type_size) == INTEGER_CST);
7018 if (TREE_INT_CST_HIGH(type_size) != 0)
7020 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
7021 val_long = static_cast<unsigned long>(val_wide);
7022 if (val_long != val_wide)
7025 else if (this->code_ == BUILTIN_ALIGNOF)
7027 if (arg->field_reference_expression() == NULL)
7028 val_long = go_type_alignment(arg_type_tree);
7031 // Calling unsafe.Alignof(s.f) returns the alignment of
7032 // the type of f when it is used as a field in a struct.
7033 val_long = go_field_alignment(arg_type_tree);
7038 mpz_set_ui(val, val_long);
7042 else if (this->code_ == BUILTIN_OFFSETOF)
7044 Expression* arg = this->one_arg();
7047 Field_reference_expression* farg = arg->field_reference_expression();
7050 Expression* struct_expr = farg->expr();
7051 Type* st = struct_expr->type();
7052 if (st->struct_type() == NULL)
7054 if (st->named_type() != NULL)
7055 st->named_type()->convert(this->gogo_);
7056 tree struct_tree = st->get_tree(this->gogo_);
7057 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
7058 tree field = TYPE_FIELDS(struct_tree);
7059 for (unsigned int index = farg->field_index(); index > 0; --index)
7061 field = DECL_CHAIN(field);
7062 gcc_assert(field != NULL_TREE);
7064 HOST_WIDE_INT offset_wide = int_byte_position (field);
7065 if (offset_wide < 0)
7067 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
7068 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
7070 mpz_set_ui(val, offset_long);
7076 // Return a floating point constant value if possible.
7079 Builtin_call_expression::do_float_constant_value(mpfr_t val,
7082 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
7084 Expression* arg = this->one_arg();
7095 if (arg->complex_constant_value(real, imag, &type))
7097 if (this->code_ == BUILTIN_REAL)
7098 mpfr_set(val, real, GMP_RNDN);
7100 mpfr_set(val, imag, GMP_RNDN);
7101 *ptype = Builtin_call_expression::real_imag_type(type);
7113 // Return a complex constant value if possible.
7116 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
7119 if (this->code_ == BUILTIN_COMPLEX)
7121 const Expression_list* args = this->args();
7122 if (args == NULL || args->size() != 2)
7128 if (!args->front()->float_constant_value(r, &rtype))
7139 if (args->back()->float_constant_value(i, &itype)
7140 && Type::are_identical(rtype, itype, false, NULL))
7142 mpfr_set(real, r, GMP_RNDN);
7143 mpfr_set(imag, i, GMP_RNDN);
7144 *ptype = Builtin_call_expression::complex_type(rtype);
7160 Builtin_call_expression::do_type()
7162 switch (this->code_)
7164 case BUILTIN_INVALID:
7171 const Expression_list* args = this->args();
7172 if (args == NULL || args->empty())
7173 return Type::make_error_type();
7174 return Type::make_pointer_type(args->front()->type());
7180 case BUILTIN_ALIGNOF:
7181 case BUILTIN_OFFSETOF:
7182 case BUILTIN_SIZEOF:
7183 return Type::lookup_integer_type("int");
7188 case BUILTIN_PRINTLN:
7189 return Type::make_void_type();
7191 case BUILTIN_RECOVER:
7192 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
7194 case BUILTIN_APPEND:
7196 const Expression_list* args = this->args();
7197 if (args == NULL || args->empty())
7198 return Type::make_error_type();
7199 return args->front()->type();
7205 Expression* arg = this->one_arg();
7207 return Type::make_error_type();
7208 Type* t = arg->type();
7209 if (t->is_abstract())
7210 t = t->make_non_abstract_type();
7211 t = Builtin_call_expression::real_imag_type(t);
7213 t = Type::make_error_type();
7217 case BUILTIN_COMPLEX:
7219 const Expression_list* args = this->args();
7220 if (args == NULL || args->size() != 2)
7221 return Type::make_error_type();
7222 Type* t = args->front()->type();
7223 if (t->is_abstract())
7225 t = args->back()->type();
7226 if (t->is_abstract())
7227 t = t->make_non_abstract_type();
7229 t = Builtin_call_expression::complex_type(t);
7231 t = Type::make_error_type();
7237 // Determine the type.
7240 Builtin_call_expression::do_determine_type(const Type_context* context)
7242 if (!this->determining_types())
7245 this->fn()->determine_type_no_context();
7247 const Expression_list* args = this->args();
7250 Type* arg_type = NULL;
7251 switch (this->code_)
7254 case BUILTIN_PRINTLN:
7255 // Do not force a large integer constant to "int".
7261 arg_type = Builtin_call_expression::complex_type(context->type);
7265 case BUILTIN_COMPLEX:
7267 // For the complex function the type of one operand can
7268 // determine the type of the other, as in a binary expression.
7269 arg_type = Builtin_call_expression::real_imag_type(context->type);
7270 if (args != NULL && args->size() == 2)
7272 Type* t1 = args->front()->type();
7273 Type* t2 = args->front()->type();
7274 if (!t1->is_abstract())
7276 else if (!t2->is_abstract())
7290 for (Expression_list::const_iterator pa = args->begin();
7294 Type_context subcontext;
7295 subcontext.type = arg_type;
7299 // We want to print large constants, we so can't just
7300 // use the appropriate nonabstract type. Use uint64 for
7301 // an integer if we know it is nonnegative, otherwise
7302 // use int64 for a integer, otherwise use float64 for a
7303 // float or complex128 for a complex.
7304 Type* want_type = NULL;
7305 Type* atype = (*pa)->type();
7306 if (atype->is_abstract())
7308 if (atype->integer_type() != NULL)
7313 if (this->integer_constant_value(true, val, &dummy)
7314 && mpz_sgn(val) >= 0)
7315 want_type = Type::lookup_integer_type("uint64");
7317 want_type = Type::lookup_integer_type("int64");
7320 else if (atype->float_type() != NULL)
7321 want_type = Type::lookup_float_type("float64");
7322 else if (atype->complex_type() != NULL)
7323 want_type = Type::lookup_complex_type("complex128");
7324 else if (atype->is_abstract_string_type())
7325 want_type = Type::lookup_string_type();
7326 else if (atype->is_abstract_boolean_type())
7327 want_type = Type::lookup_bool_type();
7330 subcontext.type = want_type;
7334 (*pa)->determine_type(&subcontext);
7339 // If there is exactly one argument, return true. Otherwise give an
7340 // error message and return false.
7343 Builtin_call_expression::check_one_arg()
7345 const Expression_list* args = this->args();
7346 if (args == NULL || args->size() < 1)
7348 this->report_error(_("not enough arguments"));
7351 else if (args->size() > 1)
7353 this->report_error(_("too many arguments"));
7356 if (args->front()->is_error_expression()
7357 || args->front()->type()->is_error_type()
7358 || args->front()->type()->is_undefined())
7360 this->set_is_error();
7366 // Check argument types for a builtin function.
7369 Builtin_call_expression::do_check_types(Gogo*)
7371 switch (this->code_)
7373 case BUILTIN_INVALID:
7381 // The single argument may be either a string or an array or a
7382 // map or a channel, or a pointer to a closed array.
7383 if (this->check_one_arg())
7385 Type* arg_type = this->one_arg()->type();
7386 if (arg_type->points_to() != NULL
7387 && arg_type->points_to()->array_type() != NULL
7388 && !arg_type->points_to()->is_open_array_type())
7389 arg_type = arg_type->points_to();
7390 if (this->code_ == BUILTIN_CAP)
7392 if (!arg_type->is_error_type()
7393 && arg_type->array_type() == NULL
7394 && arg_type->channel_type() == NULL)
7395 this->report_error(_("argument must be array or slice "
7400 if (!arg_type->is_error_type()
7401 && !arg_type->is_string_type()
7402 && arg_type->array_type() == NULL
7403 && arg_type->map_type() == NULL
7404 && arg_type->channel_type() == NULL)
7405 this->report_error(_("argument must be string or "
7406 "array or slice or map or channel"));
7413 case BUILTIN_PRINTLN:
7415 const Expression_list* args = this->args();
7418 if (this->code_ == BUILTIN_PRINT)
7419 warning_at(this->location(), 0,
7420 "no arguments for builtin function %<%s%>",
7421 (this->code_ == BUILTIN_PRINT
7427 for (Expression_list::const_iterator p = args->begin();
7431 Type* type = (*p)->type();
7432 if (type->is_error_type()
7433 || type->is_string_type()
7434 || type->integer_type() != NULL
7435 || type->float_type() != NULL
7436 || type->complex_type() != NULL
7437 || type->is_boolean_type()
7438 || type->points_to() != NULL
7439 || type->interface_type() != NULL
7440 || type->channel_type() != NULL
7441 || type->map_type() != NULL
7442 || type->function_type() != NULL
7443 || type->is_open_array_type())
7446 this->report_error(_("unsupported argument type to "
7447 "builtin function"));
7454 if (this->check_one_arg())
7456 if (this->one_arg()->type()->channel_type() == NULL)
7457 this->report_error(_("argument must be channel"));
7462 case BUILTIN_SIZEOF:
7463 case BUILTIN_ALIGNOF:
7464 this->check_one_arg();
7467 case BUILTIN_RECOVER:
7468 if (this->args() != NULL && !this->args()->empty())
7469 this->report_error(_("too many arguments"));
7472 case BUILTIN_OFFSETOF:
7473 if (this->check_one_arg())
7475 Expression* arg = this->one_arg();
7476 if (arg->field_reference_expression() == NULL)
7477 this->report_error(_("argument must be a field reference"));
7483 const Expression_list* args = this->args();
7484 if (args == NULL || args->size() < 2)
7486 this->report_error(_("not enough arguments"));
7489 else if (args->size() > 2)
7491 this->report_error(_("too many arguments"));
7494 Type* arg1_type = args->front()->type();
7495 Type* arg2_type = args->back()->type();
7496 if (arg1_type->is_error_type() || arg2_type->is_error_type())
7500 if (arg1_type->is_open_array_type())
7501 e1 = arg1_type->array_type()->element_type();
7504 this->report_error(_("left argument must be a slice"));
7509 if (arg2_type->is_open_array_type())
7510 e2 = arg2_type->array_type()->element_type();
7511 else if (arg2_type->is_string_type())
7512 e2 = Type::lookup_integer_type("uint8");
7515 this->report_error(_("right argument must be a slice or a string"));
7519 if (!Type::are_identical(e1, e2, true, NULL))
7520 this->report_error(_("element types must be the same"));
7524 case BUILTIN_APPEND:
7526 const Expression_list* args = this->args();
7527 if (args == NULL || args->size() < 2)
7529 this->report_error(_("not enough arguments"));
7532 if (args->size() > 2)
7534 this->report_error(_("too many arguments"));
7538 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
7542 this->report_error(_("arguments 1 and 2 have different types"));
7545 error_at(this->location(),
7546 "arguments 1 and 2 have different types (%s)",
7548 this->set_is_error();
7556 if (this->check_one_arg())
7558 if (this->one_arg()->type()->complex_type() == NULL)
7559 this->report_error(_("argument must have complex type"));
7563 case BUILTIN_COMPLEX:
7565 const Expression_list* args = this->args();
7566 if (args == NULL || args->size() < 2)
7567 this->report_error(_("not enough arguments"));
7568 else if (args->size() > 2)
7569 this->report_error(_("too many arguments"));
7570 else if (args->front()->is_error_expression()
7571 || args->front()->type()->is_error_type()
7572 || args->back()->is_error_expression()
7573 || args->back()->type()->is_error_type())
7574 this->set_is_error();
7575 else if (!Type::are_identical(args->front()->type(),
7576 args->back()->type(), true, NULL))
7577 this->report_error(_("complex arguments must have identical types"));
7578 else if (args->front()->type()->float_type() == NULL)
7579 this->report_error(_("complex arguments must have "
7580 "floating-point type"));
7589 // Return the tree for a builtin function.
7592 Builtin_call_expression::do_get_tree(Translate_context* context)
7594 Gogo* gogo = context->gogo();
7595 source_location location = this->location();
7596 switch (this->code_)
7598 case BUILTIN_INVALID:
7606 const Expression_list* args = this->args();
7607 gcc_assert(args != NULL && args->size() == 1);
7608 Expression* arg = *args->begin();
7609 Type* arg_type = arg->type();
7613 gcc_assert(saw_errors());
7614 return error_mark_node;
7618 tree arg_tree = arg->get_tree(context);
7620 this->seen_ = false;
7622 if (arg_tree == error_mark_node)
7623 return error_mark_node;
7625 if (arg_type->points_to() != NULL)
7627 arg_type = arg_type->points_to();
7628 gcc_assert(arg_type->array_type() != NULL
7629 && !arg_type->is_open_array_type());
7630 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7631 arg_tree = build_fold_indirect_ref(arg_tree);
7635 if (this->code_ == BUILTIN_LEN)
7637 if (arg_type->is_string_type())
7638 val_tree = String_type::length_tree(gogo, arg_tree);
7639 else if (arg_type->array_type() != NULL)
7643 gcc_assert(saw_errors());
7644 return error_mark_node;
7647 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7648 this->seen_ = false;
7650 else if (arg_type->map_type() != NULL)
7652 static tree map_len_fndecl;
7653 val_tree = Gogo::call_builtin(&map_len_fndecl,
7658 arg_type->get_tree(gogo),
7661 else if (arg_type->channel_type() != NULL)
7663 static tree chan_len_fndecl;
7664 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7669 arg_type->get_tree(gogo),
7677 if (arg_type->array_type() != NULL)
7681 gcc_assert(saw_errors());
7682 return error_mark_node;
7685 val_tree = arg_type->array_type()->capacity_tree(gogo,
7687 this->seen_ = false;
7689 else if (arg_type->channel_type() != NULL)
7691 static tree chan_cap_fndecl;
7692 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7697 arg_type->get_tree(gogo),
7704 if (val_tree == error_mark_node)
7705 return error_mark_node;
7707 tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo);
7708 if (type_tree == TREE_TYPE(val_tree))
7711 return fold(convert_to_integer(type_tree, val_tree));
7715 case BUILTIN_PRINTLN:
7717 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7718 tree stmt_list = NULL_TREE;
7720 const Expression_list* call_args = this->args();
7721 if (call_args != NULL)
7723 for (Expression_list::const_iterator p = call_args->begin();
7724 p != call_args->end();
7727 if (is_ln && p != call_args->begin())
7729 static tree print_space_fndecl;
7730 tree call = Gogo::call_builtin(&print_space_fndecl,
7735 if (call == error_mark_node)
7736 return error_mark_node;
7737 append_to_statement_list(call, &stmt_list);
7740 Type* type = (*p)->type();
7742 tree arg = (*p)->get_tree(context);
7743 if (arg == error_mark_node)
7744 return error_mark_node;
7748 if (type->is_string_type())
7750 static tree print_string_fndecl;
7751 pfndecl = &print_string_fndecl;
7752 fnname = "__go_print_string";
7754 else if (type->integer_type() != NULL
7755 && type->integer_type()->is_unsigned())
7757 static tree print_uint64_fndecl;
7758 pfndecl = &print_uint64_fndecl;
7759 fnname = "__go_print_uint64";
7760 Type* itype = Type::lookup_integer_type("uint64");
7761 arg = fold_convert_loc(location, itype->get_tree(gogo),
7764 else if (type->integer_type() != NULL)
7766 static tree print_int64_fndecl;
7767 pfndecl = &print_int64_fndecl;
7768 fnname = "__go_print_int64";
7769 Type* itype = Type::lookup_integer_type("int64");
7770 arg = fold_convert_loc(location, itype->get_tree(gogo),
7773 else if (type->float_type() != NULL)
7775 static tree print_double_fndecl;
7776 pfndecl = &print_double_fndecl;
7777 fnname = "__go_print_double";
7778 arg = fold_convert_loc(location, double_type_node, arg);
7780 else if (type->complex_type() != NULL)
7782 static tree print_complex_fndecl;
7783 pfndecl = &print_complex_fndecl;
7784 fnname = "__go_print_complex";
7785 arg = fold_convert_loc(location, complex_double_type_node,
7788 else if (type->is_boolean_type())
7790 static tree print_bool_fndecl;
7791 pfndecl = &print_bool_fndecl;
7792 fnname = "__go_print_bool";
7794 else if (type->points_to() != NULL
7795 || type->channel_type() != NULL
7796 || type->map_type() != NULL
7797 || type->function_type() != NULL)
7799 static tree print_pointer_fndecl;
7800 pfndecl = &print_pointer_fndecl;
7801 fnname = "__go_print_pointer";
7802 arg = fold_convert_loc(location, ptr_type_node, arg);
7804 else if (type->interface_type() != NULL)
7806 if (type->interface_type()->is_empty())
7808 static tree print_empty_interface_fndecl;
7809 pfndecl = &print_empty_interface_fndecl;
7810 fnname = "__go_print_empty_interface";
7814 static tree print_interface_fndecl;
7815 pfndecl = &print_interface_fndecl;
7816 fnname = "__go_print_interface";
7819 else if (type->is_open_array_type())
7821 static tree print_slice_fndecl;
7822 pfndecl = &print_slice_fndecl;
7823 fnname = "__go_print_slice";
7828 tree call = Gogo::call_builtin(pfndecl,
7835 if (call == error_mark_node)
7836 return error_mark_node;
7837 append_to_statement_list(call, &stmt_list);
7843 static tree print_nl_fndecl;
7844 tree call = Gogo::call_builtin(&print_nl_fndecl,
7849 if (call == error_mark_node)
7850 return error_mark_node;
7851 append_to_statement_list(call, &stmt_list);
7859 const Expression_list* args = this->args();
7860 gcc_assert(args != NULL && args->size() == 1);
7861 Expression* arg = args->front();
7862 tree arg_tree = arg->get_tree(context);
7863 if (arg_tree == error_mark_node)
7864 return error_mark_node;
7865 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7866 arg_tree = Expression::convert_for_assignment(context, empty,
7868 arg_tree, location);
7869 static tree panic_fndecl;
7870 tree call = Gogo::call_builtin(&panic_fndecl,
7875 TREE_TYPE(arg_tree),
7877 if (call == error_mark_node)
7878 return error_mark_node;
7879 // This function will throw an exception.
7880 TREE_NOTHROW(panic_fndecl) = 0;
7881 // This function will not return.
7882 TREE_THIS_VOLATILE(panic_fndecl) = 1;
7886 case BUILTIN_RECOVER:
7888 // The argument is set when building recover thunks. It's a
7889 // boolean value which is true if we can recover a value now.
7890 const Expression_list* args = this->args();
7891 gcc_assert(args != NULL && args->size() == 1);
7892 Expression* arg = args->front();
7893 tree arg_tree = arg->get_tree(context);
7894 if (arg_tree == error_mark_node)
7895 return error_mark_node;
7897 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7898 tree empty_tree = empty->get_tree(context->gogo());
7900 Type* nil_type = Type::make_nil_type();
7901 Expression* nil = Expression::make_nil(location);
7902 tree nil_tree = nil->get_tree(context);
7903 tree empty_nil_tree = Expression::convert_for_assignment(context,
7909 // We need to handle a deferred call to recover specially,
7910 // because it changes whether it can recover a panic or not.
7911 // See test7 in test/recover1.go.
7913 if (this->is_deferred())
7915 static tree deferred_recover_fndecl;
7916 call = Gogo::call_builtin(&deferred_recover_fndecl,
7918 "__go_deferred_recover",
7924 static tree recover_fndecl;
7925 call = Gogo::call_builtin(&recover_fndecl,
7931 if (call == error_mark_node)
7932 return error_mark_node;
7933 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
7934 call, empty_nil_tree);
7939 const Expression_list* args = this->args();
7940 gcc_assert(args != NULL && args->size() == 1);
7941 Expression* arg = args->front();
7942 tree arg_tree = arg->get_tree(context);
7943 if (arg_tree == error_mark_node)
7944 return error_mark_node;
7945 static tree close_fndecl;
7946 return Gogo::call_builtin(&close_fndecl,
7948 "__go_builtin_close",
7951 TREE_TYPE(arg_tree),
7955 case BUILTIN_SIZEOF:
7956 case BUILTIN_OFFSETOF:
7957 case BUILTIN_ALIGNOF:
7962 bool b = this->integer_constant_value(true, val, &dummy);
7965 gcc_assert(saw_errors());
7966 return error_mark_node;
7968 tree type = Type::lookup_integer_type("int")->get_tree(gogo);
7969 tree ret = Expression::integer_constant_tree(val, type);
7976 const Expression_list* args = this->args();
7977 gcc_assert(args != NULL && args->size() == 2);
7978 Expression* arg1 = args->front();
7979 Expression* arg2 = args->back();
7981 tree arg1_tree = arg1->get_tree(context);
7982 tree arg2_tree = arg2->get_tree(context);
7983 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
7984 return error_mark_node;
7986 Type* arg1_type = arg1->type();
7987 Array_type* at = arg1_type->array_type();
7988 arg1_tree = save_expr(arg1_tree);
7989 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
7990 tree arg1_len = at->length_tree(gogo, arg1_tree);
7991 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
7992 return error_mark_node;
7994 Type* arg2_type = arg2->type();
7997 if (arg2_type->is_open_array_type())
7999 at = arg2_type->array_type();
8000 arg2_tree = save_expr(arg2_tree);
8001 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8002 arg2_len = at->length_tree(gogo, arg2_tree);
8006 arg2_tree = save_expr(arg2_tree);
8007 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8008 arg2_len = String_type::length_tree(gogo, arg2_tree);
8010 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8011 return error_mark_node;
8013 arg1_len = save_expr(arg1_len);
8014 arg2_len = save_expr(arg2_len);
8015 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
8016 fold_build2_loc(location, LT_EXPR,
8018 arg1_len, arg2_len),
8019 arg1_len, arg2_len);
8020 len = save_expr(len);
8022 Type* element_type = at->element_type();
8023 tree element_type_tree = element_type->get_tree(gogo);
8024 if (element_type_tree == error_mark_node)
8025 return error_mark_node;
8026 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8027 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
8029 bytecount = fold_build2_loc(location, MULT_EXPR,
8030 TREE_TYPE(element_size),
8031 bytecount, element_size);
8032 bytecount = fold_convert_loc(location, size_type_node, bytecount);
8034 arg1_val = fold_convert_loc(location, ptr_type_node, arg1_val);
8035 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8037 static tree copy_fndecl;
8038 tree call = Gogo::call_builtin(©_fndecl,
8049 if (call == error_mark_node)
8050 return error_mark_node;
8052 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
8056 case BUILTIN_APPEND:
8058 const Expression_list* args = this->args();
8059 gcc_assert(args != NULL && args->size() == 2);
8060 Expression* arg1 = args->front();
8061 Expression* arg2 = args->back();
8063 tree arg1_tree = arg1->get_tree(context);
8064 tree arg2_tree = arg2->get_tree(context);
8065 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8066 return error_mark_node;
8068 Array_type* at = arg1->type()->array_type();
8069 Type* element_type = at->element_type();
8071 arg2_tree = Expression::convert_for_assignment(context, at,
8075 if (arg2_tree == error_mark_node)
8076 return error_mark_node;
8078 arg2_tree = save_expr(arg2_tree);
8079 tree arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8080 tree arg2_len = at->length_tree(gogo, arg2_tree);
8081 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8082 return error_mark_node;
8083 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8084 arg2_len = fold_convert_loc(location, size_type_node, arg2_len);
8086 tree element_type_tree = element_type->get_tree(gogo);
8087 if (element_type_tree == error_mark_node)
8088 return error_mark_node;
8089 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8090 element_size = fold_convert_loc(location, size_type_node,
8093 // We rebuild the decl each time since the slice types may
8095 tree append_fndecl = NULL_TREE;
8096 return Gogo::call_builtin(&append_fndecl,
8100 TREE_TYPE(arg1_tree),
8101 TREE_TYPE(arg1_tree),
8114 const Expression_list* args = this->args();
8115 gcc_assert(args != NULL && args->size() == 1);
8116 Expression* arg = args->front();
8117 tree arg_tree = arg->get_tree(context);
8118 if (arg_tree == error_mark_node)
8119 return error_mark_node;
8120 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
8121 if (this->code_ == BUILTIN_REAL)
8122 return fold_build1_loc(location, REALPART_EXPR,
8123 TREE_TYPE(TREE_TYPE(arg_tree)),
8126 return fold_build1_loc(location, IMAGPART_EXPR,
8127 TREE_TYPE(TREE_TYPE(arg_tree)),
8131 case BUILTIN_COMPLEX:
8133 const Expression_list* args = this->args();
8134 gcc_assert(args != NULL && args->size() == 2);
8135 tree r = args->front()->get_tree(context);
8136 tree i = args->back()->get_tree(context);
8137 if (r == error_mark_node || i == error_mark_node)
8138 return error_mark_node;
8139 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
8140 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
8141 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8142 return fold_build2_loc(location, COMPLEX_EXPR,
8143 build_complex_type(TREE_TYPE(r)),
8152 // We have to support exporting a builtin call expression, because
8153 // code can set a constant to the result of a builtin expression.
8156 Builtin_call_expression::do_export(Export* exp) const
8163 if (this->integer_constant_value(true, val, &dummy))
8165 Integer_expression::export_integer(exp, val);
8174 if (this->float_constant_value(fval, &dummy))
8176 Float_expression::export_float(exp, fval);
8188 if (this->complex_constant_value(real, imag, &dummy))
8190 Complex_expression::export_complex(exp, real, imag);
8199 error_at(this->location(), "value is not constant");
8203 // A trailing space lets us reliably identify the end of the number.
8204 exp->write_c_string(" ");
8207 // Class Call_expression.
8212 Call_expression::do_traverse(Traverse* traverse)
8214 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8215 return TRAVERSE_EXIT;
8216 if (this->args_ != NULL)
8218 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8219 return TRAVERSE_EXIT;
8221 return TRAVERSE_CONTINUE;
8224 // Lower a call statement.
8227 Call_expression::do_lower(Gogo* gogo, Named_object* function, int)
8229 // A type case can look like a function call.
8230 if (this->fn_->is_type_expression()
8231 && this->args_ != NULL
8232 && this->args_->size() == 1)
8233 return Expression::make_cast(this->fn_->type(), this->args_->front(),
8236 // Recognize a call to a builtin function.
8237 Func_expression* fne = this->fn_->func_expression();
8239 && fne->named_object()->is_function_declaration()
8240 && fne->named_object()->func_declaration_value()->type()->is_builtin())
8241 return new Builtin_call_expression(gogo, this->fn_, this->args_,
8242 this->is_varargs_, this->location());
8244 // Handle an argument which is a call to a function which returns
8245 // multiple results.
8246 if (this->args_ != NULL
8247 && this->args_->size() == 1
8248 && this->args_->front()->call_expression() != NULL
8249 && this->fn_->type()->function_type() != NULL)
8251 Function_type* fntype = this->fn_->type()->function_type();
8252 size_t rc = this->args_->front()->call_expression()->result_count();
8254 && fntype->parameters() != NULL
8255 && (fntype->parameters()->size() == rc
8256 || (fntype->is_varargs()
8257 && fntype->parameters()->size() - 1 <= rc)))
8259 Call_expression* call = this->args_->front()->call_expression();
8260 Expression_list* args = new Expression_list;
8261 for (size_t i = 0; i < rc; ++i)
8262 args->push_back(Expression::make_call_result(call, i));
8263 // We can't return a new call expression here, because this
8264 // one may be referenced by Call_result expressions. We
8265 // also can't delete the old arguments, because we may still
8266 // traverse them somewhere up the call stack. FIXME.
8271 // Handle a call to a varargs function by packaging up the extra
8273 if (this->fn_->type()->function_type() != NULL
8274 && this->fn_->type()->function_type()->is_varargs())
8276 Function_type* fntype = this->fn_->type()->function_type();
8277 const Typed_identifier_list* parameters = fntype->parameters();
8278 gcc_assert(parameters != NULL && !parameters->empty());
8279 Type* varargs_type = parameters->back().type();
8280 return this->lower_varargs(gogo, function, varargs_type,
8281 parameters->size());
8287 // Lower a call to a varargs function. FUNCTION is the function in
8288 // which the call occurs--it's not the function we are calling.
8289 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8290 // PARAM_COUNT is the number of parameters of the function we are
8291 // calling; the last of these parameters will be the varargs
8295 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
8296 Type* varargs_type, size_t param_count)
8298 if (this->varargs_are_lowered_)
8301 source_location loc = this->location();
8303 gcc_assert(param_count > 0);
8304 gcc_assert(varargs_type->is_open_array_type());
8306 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8307 if (arg_count < param_count - 1)
8309 // Not enough arguments; will be caught in check_types.
8313 Expression_list* old_args = this->args_;
8314 Expression_list* new_args = new Expression_list();
8315 bool push_empty_arg = false;
8316 if (old_args == NULL || old_args->empty())
8318 gcc_assert(param_count == 1);
8319 push_empty_arg = true;
8323 Expression_list::const_iterator pa;
8325 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
8327 if (static_cast<size_t>(i) == param_count)
8329 new_args->push_back(*pa);
8332 // We have reached the varargs parameter.
8334 bool issued_error = false;
8335 if (pa == old_args->end())
8336 push_empty_arg = true;
8337 else if (pa + 1 == old_args->end() && this->is_varargs_)
8338 new_args->push_back(*pa);
8339 else if (this->is_varargs_)
8341 this->report_error(_("too many arguments"));
8346 Type* element_type = varargs_type->array_type()->element_type();
8347 Expression_list* vals = new Expression_list;
8348 for (; pa != old_args->end(); ++pa, ++i)
8350 // Check types here so that we get a better message.
8351 Type* patype = (*pa)->type();
8352 source_location paloc = (*pa)->location();
8353 if (!this->check_argument_type(i, element_type, patype,
8354 paloc, issued_error))
8356 vals->push_back(*pa);
8359 Expression::make_slice_composite_literal(varargs_type, vals, loc);
8360 new_args->push_back(val);
8365 new_args->push_back(Expression::make_nil(loc));
8367 // We can't return a new call expression here, because this one may
8368 // be referenced by Call_result expressions. FIXME.
8369 if (old_args != NULL)
8371 this->args_ = new_args;
8372 this->varargs_are_lowered_ = true;
8374 // Lower all the new subexpressions.
8375 Expression* ret = this;
8376 gogo->lower_expression(function, &ret);
8377 gcc_assert(ret == this);
8381 // Get the function type. Returns NULL if we don't know the type. If
8382 // this returns NULL, and if_ERROR is true, issues an error.
8385 Call_expression::get_function_type() const
8387 return this->fn_->type()->function_type();
8390 // Return the number of values which this call will return.
8393 Call_expression::result_count() const
8395 const Function_type* fntype = this->get_function_type();
8398 if (fntype->results() == NULL)
8400 return fntype->results()->size();
8403 // Return whether this is a call to the predeclared function recover.
8406 Call_expression::is_recover_call() const
8408 return this->do_is_recover_call();
8411 // Set the argument to the recover function.
8414 Call_expression::set_recover_arg(Expression* arg)
8416 this->do_set_recover_arg(arg);
8419 // Virtual functions also implemented by Builtin_call_expression.
8422 Call_expression::do_is_recover_call() const
8428 Call_expression::do_set_recover_arg(Expression*)
8436 Call_expression::do_type()
8438 if (this->type_ != NULL)
8442 Function_type* fntype = this->get_function_type();
8444 return Type::make_error_type();
8446 const Typed_identifier_list* results = fntype->results();
8447 if (results == NULL)
8448 ret = Type::make_void_type();
8449 else if (results->size() == 1)
8450 ret = results->begin()->type();
8452 ret = Type::make_call_multiple_result_type(this);
8459 // Determine types for a call expression. We can use the function
8460 // parameter types to set the types of the arguments.
8463 Call_expression::do_determine_type(const Type_context*)
8465 if (!this->determining_types())
8468 this->fn_->determine_type_no_context();
8469 Function_type* fntype = this->get_function_type();
8470 const Typed_identifier_list* parameters = NULL;
8472 parameters = fntype->parameters();
8473 if (this->args_ != NULL)
8475 Typed_identifier_list::const_iterator pt;
8476 if (parameters != NULL)
8477 pt = parameters->begin();
8478 for (Expression_list::const_iterator pa = this->args_->begin();
8479 pa != this->args_->end();
8482 if (parameters != NULL && pt != parameters->end())
8484 Type_context subcontext(pt->type(), false);
8485 (*pa)->determine_type(&subcontext);
8489 (*pa)->determine_type_no_context();
8494 // Called when determining types for a Call_expression. Return true
8495 // if we should go ahead, false if they have already been determined.
8498 Call_expression::determining_types()
8500 if (this->types_are_determined_)
8504 this->types_are_determined_ = true;
8509 // Check types for parameter I.
8512 Call_expression::check_argument_type(int i, const Type* parameter_type,
8513 const Type* argument_type,
8514 source_location argument_location,
8518 if (!Type::are_assignable(parameter_type, argument_type, &reason))
8523 error_at(argument_location, "argument %d has incompatible type", i);
8525 error_at(argument_location,
8526 "argument %d has incompatible type (%s)",
8529 this->set_is_error();
8538 Call_expression::do_check_types(Gogo*)
8540 Function_type* fntype = this->get_function_type();
8543 if (!this->fn_->type()->is_error_type())
8544 this->report_error(_("expected function"));
8548 if (fntype->is_method())
8550 // We don't support pointers to methods, so the function has to
8551 // be a bound method expression.
8552 Bound_method_expression* bme = this->fn_->bound_method_expression();
8555 this->report_error(_("method call without object"));
8558 Type* first_arg_type = bme->first_argument()->type();
8559 if (first_arg_type->points_to() == NULL)
8561 // When passing a value, we need to check that we are
8562 // permitted to copy it.
8564 if (!Type::are_assignable(fntype->receiver()->type(),
8565 first_arg_type, &reason))
8568 this->report_error(_("incompatible type for receiver"));
8571 error_at(this->location(),
8572 "incompatible type for receiver (%s)",
8574 this->set_is_error();
8580 // Note that varargs was handled by the lower_varargs() method, so
8581 // we don't have to worry about it here.
8583 const Typed_identifier_list* parameters = fntype->parameters();
8584 if (this->args_ == NULL)
8586 if (parameters != NULL && !parameters->empty())
8587 this->report_error(_("not enough arguments"));
8589 else if (parameters == NULL)
8590 this->report_error(_("too many arguments"));
8594 Typed_identifier_list::const_iterator pt = parameters->begin();
8595 for (Expression_list::const_iterator pa = this->args_->begin();
8596 pa != this->args_->end();
8599 if (pt == parameters->end())
8601 this->report_error(_("too many arguments"));
8604 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8605 (*pa)->location(), false);
8607 if (pt != parameters->end())
8608 this->report_error(_("not enough arguments"));
8612 // Return whether we have to use a temporary variable to ensure that
8613 // we evaluate this call expression in order. If the call returns no
8614 // results then it will inevitably be executed last. If the call
8615 // returns more than one result then it will be used with Call_result
8616 // expressions. So we only have to use a temporary variable if the
8617 // call returns exactly one result.
8620 Call_expression::do_must_eval_in_order() const
8622 return this->result_count() == 1;
8625 // Get the function and the first argument to use when calling a bound
8629 Call_expression::bound_method_function(Translate_context* context,
8630 Bound_method_expression* bound_method,
8631 tree* first_arg_ptr)
8633 Expression* first_argument = bound_method->first_argument();
8634 tree first_arg = first_argument->get_tree(context);
8635 if (first_arg == error_mark_node)
8636 return error_mark_node;
8638 // We always pass a pointer to the first argument when calling a
8640 if (first_argument->type()->points_to() == NULL)
8642 tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg));
8643 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg))
8644 || DECL_P(first_arg)
8645 || TREE_CODE(first_arg) == INDIRECT_REF
8646 || TREE_CODE(first_arg) == COMPONENT_REF)
8648 first_arg = build_fold_addr_expr(first_arg);
8649 if (DECL_P(first_arg))
8650 TREE_ADDRESSABLE(first_arg) = 1;
8654 tree tmp = create_tmp_var(TREE_TYPE(first_arg),
8655 get_name(first_arg));
8656 DECL_IGNORED_P(tmp) = 0;
8657 DECL_INITIAL(tmp) = first_arg;
8658 first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type,
8659 build1(DECL_EXPR, void_type_node, tmp),
8660 build_fold_addr_expr(tmp));
8661 TREE_ADDRESSABLE(tmp) = 1;
8663 if (first_arg == error_mark_node)
8664 return error_mark_node;
8667 Type* fatype = bound_method->first_argument_type();
8670 if (fatype->points_to() == NULL)
8671 fatype = Type::make_pointer_type(fatype);
8672 first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg);
8673 if (first_arg == error_mark_node
8674 || TREE_TYPE(first_arg) == error_mark_node)
8675 return error_mark_node;
8678 *first_arg_ptr = first_arg;
8680 return bound_method->method()->get_tree(context);
8683 // Get the function and the first argument to use when calling an
8684 // interface method.
8687 Call_expression::interface_method_function(
8688 Translate_context* context,
8689 Interface_field_reference_expression* interface_method,
8690 tree* first_arg_ptr)
8692 tree expr = interface_method->expr()->get_tree(context);
8693 if (expr == error_mark_node)
8694 return error_mark_node;
8695 expr = save_expr(expr);
8696 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
8697 if (first_arg == error_mark_node)
8698 return error_mark_node;
8699 *first_arg_ptr = first_arg;
8700 return interface_method->get_function_tree(context, expr);
8703 // Build the call expression.
8706 Call_expression::do_get_tree(Translate_context* context)
8708 if (this->tree_ != NULL_TREE)
8711 Function_type* fntype = this->get_function_type();
8713 return error_mark_node;
8715 if (this->fn_->is_error_expression())
8716 return error_mark_node;
8718 Gogo* gogo = context->gogo();
8719 source_location location = this->location();
8721 Func_expression* func = this->fn_->func_expression();
8722 Bound_method_expression* bound_method = this->fn_->bound_method_expression();
8723 Interface_field_reference_expression* interface_method =
8724 this->fn_->interface_field_reference_expression();
8725 const bool has_closure = func != NULL && func->closure() != NULL;
8726 const bool is_method = bound_method != NULL || interface_method != NULL;
8727 gcc_assert(!fntype->is_method() || is_method);
8731 if (this->args_ == NULL || this->args_->empty())
8733 nargs = is_method ? 1 : 0;
8734 args = nargs == 0 ? NULL : new tree[nargs];
8738 const Typed_identifier_list* params = fntype->parameters();
8739 gcc_assert(params != NULL);
8741 nargs = this->args_->size();
8742 int i = is_method ? 1 : 0;
8744 args = new tree[nargs];
8746 Typed_identifier_list::const_iterator pp = params->begin();
8747 Expression_list::const_iterator pe;
8748 for (pe = this->args_->begin();
8749 pe != this->args_->end();
8752 gcc_assert(pp != params->end());
8753 tree arg_val = (*pe)->get_tree(context);
8754 args[i] = Expression::convert_for_assignment(context,
8759 if (args[i] == error_mark_node)
8762 return error_mark_node;
8765 gcc_assert(pp == params->end());
8766 gcc_assert(i == nargs);
8769 tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo)));
8770 if (rettype == error_mark_node)
8773 return error_mark_node;
8778 fn = func->get_tree_without_closure(gogo);
8779 else if (!is_method)
8780 fn = this->fn_->get_tree(context);
8781 else if (bound_method != NULL)
8782 fn = this->bound_method_function(context, bound_method, &args[0]);
8783 else if (interface_method != NULL)
8784 fn = this->interface_method_function(context, interface_method, &args[0]);
8788 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
8791 return error_mark_node;
8795 if (TREE_CODE(fndecl) == ADDR_EXPR)
8796 fndecl = TREE_OPERAND(fndecl, 0);
8798 // Add a type cast in case the type of the function is a recursive
8799 // type which refers to itself.
8800 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
8802 tree fnt = fntype->get_tree(gogo);
8803 if (fnt == error_mark_node)
8804 return error_mark_node;
8805 fn = fold_convert_loc(location, fnt, fn);
8808 // This is to support builtin math functions when using 80387 math.
8809 tree excess_type = NULL_TREE;
8811 && DECL_IS_BUILTIN(fndecl)
8812 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
8814 && ((SCALAR_FLOAT_TYPE_P(rettype)
8815 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
8816 || (COMPLEX_FLOAT_TYPE_P(rettype)
8817 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
8819 excess_type = excess_precision_type(TREE_TYPE(args[0]));
8820 if (excess_type != NULL_TREE)
8822 tree excess_fndecl = mathfn_built_in(excess_type,
8823 DECL_FUNCTION_CODE(fndecl));
8824 if (excess_fndecl == NULL_TREE)
8825 excess_type = NULL_TREE;
8828 fn = build_fold_addr_expr_loc(location, excess_fndecl);
8829 for (int i = 0; i < nargs; ++i)
8830 args[i] = ::convert(excess_type, args[i]);
8835 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
8839 SET_EXPR_LOCATION(ret, location);
8843 tree closure_tree = func->closure()->get_tree(context);
8844 if (closure_tree != error_mark_node)
8845 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
8848 // If this is a recursive function type which returns itself, as in
8850 // we have used ptr_type_node for the return type. Add a cast here
8851 // to the correct type.
8852 if (TREE_TYPE(ret) == ptr_type_node)
8854 tree t = this->type()->base()->get_tree(gogo);
8855 ret = fold_convert_loc(location, t, ret);
8858 if (excess_type != NULL_TREE)
8860 // Calling convert here can undo our excess precision change.
8861 // That may or may not be a bug in convert_to_real.
8862 ret = build1(NOP_EXPR, rettype, ret);
8865 // If there is more than one result, we will refer to the call
8867 if (fntype->results() != NULL && fntype->results()->size() > 1)
8868 ret = save_expr(ret);
8875 // Make a call expression.
8878 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
8879 source_location location)
8881 return new Call_expression(fn, args, is_varargs, location);
8884 // A single result from a call which returns multiple results.
8886 class Call_result_expression : public Expression
8889 Call_result_expression(Call_expression* call, unsigned int index)
8890 : Expression(EXPRESSION_CALL_RESULT, call->location()),
8891 call_(call), index_(index)
8896 do_traverse(Traverse*);
8902 do_determine_type(const Type_context*);
8905 do_check_types(Gogo*);
8910 return new Call_result_expression(this->call_->call_expression(),
8915 do_must_eval_in_order() const
8919 do_get_tree(Translate_context*);
8922 // The underlying call expression.
8924 // Which result we want.
8925 unsigned int index_;
8928 // Traverse a call result.
8931 Call_result_expression::do_traverse(Traverse* traverse)
8933 if (traverse->remember_expression(this->call_))
8935 // We have already traversed the call expression.
8936 return TRAVERSE_CONTINUE;
8938 return Expression::traverse(&this->call_, traverse);
8944 Call_result_expression::do_type()
8946 if (this->classification() == EXPRESSION_ERROR)
8947 return Type::make_error_type();
8949 // THIS->CALL_ can be replaced with a temporary reference due to
8950 // Call_expression::do_must_eval_in_order when there is an error.
8951 Call_expression* ce = this->call_->call_expression();
8954 this->set_is_error();
8955 return Type::make_error_type();
8957 Function_type* fntype = ce->get_function_type();
8960 this->set_is_error();
8961 return Type::make_error_type();
8963 const Typed_identifier_list* results = fntype->results();
8964 if (results == NULL)
8966 this->report_error(_("number of results does not match "
8967 "number of values"));
8968 return Type::make_error_type();
8970 Typed_identifier_list::const_iterator pr = results->begin();
8971 for (unsigned int i = 0; i < this->index_; ++i)
8973 if (pr == results->end())
8977 if (pr == results->end())
8979 this->report_error(_("number of results does not match "
8980 "number of values"));
8981 return Type::make_error_type();
8986 // Check the type. Just make sure that we trigger the warning in
8990 Call_result_expression::do_check_types(Gogo*)
8995 // Determine the type. We have nothing to do here, but the 0 result
8996 // needs to pass down to the caller.
8999 Call_result_expression::do_determine_type(const Type_context*)
9001 this->call_->determine_type_no_context();
9007 Call_result_expression::do_get_tree(Translate_context* context)
9009 tree call_tree = this->call_->get_tree(context);
9010 if (call_tree == error_mark_node)
9011 return error_mark_node;
9012 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9014 gcc_assert(saw_errors());
9015 return error_mark_node;
9017 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9018 for (unsigned int i = 0; i < this->index_; ++i)
9020 gcc_assert(field != NULL_TREE);
9021 field = DECL_CHAIN(field);
9023 gcc_assert(field != NULL_TREE);
9024 return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE);
9027 // Make a reference to a single result of a call which returns
9028 // multiple results.
9031 Expression::make_call_result(Call_expression* call, unsigned int index)
9033 return new Call_result_expression(call, index);
9036 // Class Index_expression.
9041 Index_expression::do_traverse(Traverse* traverse)
9043 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9044 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9045 || (this->end_ != NULL
9046 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9047 return TRAVERSE_EXIT;
9048 return TRAVERSE_CONTINUE;
9051 // Lower an index expression. This converts the generic index
9052 // expression into an array index, a string index, or a map index.
9055 Index_expression::do_lower(Gogo*, Named_object*, int)
9057 source_location location = this->location();
9058 Expression* left = this->left_;
9059 Expression* start = this->start_;
9060 Expression* end = this->end_;
9062 Type* type = left->type();
9063 if (type->is_error_type())
9064 return Expression::make_error(location);
9065 else if (left->is_type_expression())
9067 error_at(location, "attempt to index type expression");
9068 return Expression::make_error(location);
9070 else if (type->array_type() != NULL)
9071 return Expression::make_array_index(left, start, end, location);
9072 else if (type->points_to() != NULL
9073 && type->points_to()->array_type() != NULL
9074 && !type->points_to()->is_open_array_type())
9076 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9078 return Expression::make_array_index(deref, start, end, location);
9080 else if (type->is_string_type())
9081 return Expression::make_string_index(left, start, end, location);
9082 else if (type->map_type() != NULL)
9086 error_at(location, "invalid slice of map");
9087 return Expression::make_error(location);
9089 Map_index_expression* ret= Expression::make_map_index(left, start,
9091 if (this->is_lvalue_)
9092 ret->set_is_lvalue();
9098 "attempt to index object which is not array, string, or map");
9099 return Expression::make_error(location);
9103 // Make an index expression.
9106 Expression::make_index(Expression* left, Expression* start, Expression* end,
9107 source_location location)
9109 return new Index_expression(left, start, end, location);
9112 // An array index. This is used for both indexing and slicing.
9114 class Array_index_expression : public Expression
9117 Array_index_expression(Expression* array, Expression* start,
9118 Expression* end, source_location location)
9119 : Expression(EXPRESSION_ARRAY_INDEX, location),
9120 array_(array), start_(start), end_(end), type_(NULL)
9125 do_traverse(Traverse*);
9131 do_determine_type(const Type_context*);
9134 do_check_types(Gogo*);
9139 return Expression::make_array_index(this->array_->copy(),
9140 this->start_->copy(),
9143 : this->end_->copy()),
9148 do_is_addressable() const;
9151 do_address_taken(bool escapes)
9152 { this->array_->address_taken(escapes); }
9155 do_get_tree(Translate_context*);
9158 // The array we are getting a value from.
9160 // The start or only index.
9162 // The end index of a slice. This may be NULL for a simple array
9163 // index, or it may be a nil expression for the length of the array.
9165 // The type of the expression.
9169 // Array index traversal.
9172 Array_index_expression::do_traverse(Traverse* traverse)
9174 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9175 return TRAVERSE_EXIT;
9176 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9177 return TRAVERSE_EXIT;
9178 if (this->end_ != NULL)
9180 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9181 return TRAVERSE_EXIT;
9183 return TRAVERSE_CONTINUE;
9186 // Return the type of an array index.
9189 Array_index_expression::do_type()
9191 if (this->type_ == NULL)
9193 Array_type* type = this->array_->type()->array_type();
9195 this->type_ = Type::make_error_type();
9196 else if (this->end_ == NULL)
9197 this->type_ = type->element_type();
9198 else if (type->is_open_array_type())
9200 // A slice of a slice has the same type as the original
9202 this->type_ = this->array_->type()->deref();
9206 // A slice of an array is a slice.
9207 this->type_ = Type::make_array_type(type->element_type(), NULL);
9213 // Set the type of an array index.
9216 Array_index_expression::do_determine_type(const Type_context*)
9218 this->array_->determine_type_no_context();
9219 this->start_->determine_type_no_context();
9220 if (this->end_ != NULL)
9221 this->end_->determine_type_no_context();
9224 // Check types of an array index.
9227 Array_index_expression::do_check_types(Gogo*)
9229 if (this->start_->type()->integer_type() == NULL)
9230 this->report_error(_("index must be integer"));
9231 if (this->end_ != NULL
9232 && this->end_->type()->integer_type() == NULL
9233 && !this->end_->is_nil_expression())
9234 this->report_error(_("slice end must be integer"));
9236 Array_type* array_type = this->array_->type()->array_type();
9237 if (array_type == NULL)
9239 gcc_assert(this->array_->type()->is_error_type());
9243 unsigned int int_bits =
9244 Type::lookup_integer_type("int")->integer_type()->bits();
9249 bool lval_valid = (array_type->length() != NULL
9250 && array_type->length()->integer_constant_value(true,
9255 if (this->start_->integer_constant_value(true, ival, &dummy))
9257 if (mpz_sgn(ival) < 0
9258 || mpz_sizeinbase(ival, 2) >= int_bits
9260 && (this->end_ == NULL
9261 ? mpz_cmp(ival, lval) >= 0
9262 : mpz_cmp(ival, lval) > 0)))
9264 error_at(this->start_->location(), "array index out of bounds");
9265 this->set_is_error();
9268 if (this->end_ != NULL && !this->end_->is_nil_expression())
9270 if (this->end_->integer_constant_value(true, ival, &dummy))
9272 if (mpz_sgn(ival) < 0
9273 || mpz_sizeinbase(ival, 2) >= int_bits
9274 || (lval_valid && mpz_cmp(ival, lval) > 0))
9276 error_at(this->end_->location(), "array index out of bounds");
9277 this->set_is_error();
9284 // A slice of an array requires an addressable array. A slice of a
9285 // slice is always possible.
9286 if (this->end_ != NULL
9287 && !array_type->is_open_array_type()
9288 && !this->array_->is_addressable())
9289 this->report_error(_("array is not addressable"));
9292 // Return whether this expression is addressable.
9295 Array_index_expression::do_is_addressable() const
9297 // A slice expression is not addressable.
9298 if (this->end_ != NULL)
9301 // An index into a slice is addressable.
9302 if (this->array_->type()->is_open_array_type())
9305 // An index into an array is addressable if the array is
9307 return this->array_->is_addressable();
9310 // Get a tree for an array index.
9313 Array_index_expression::do_get_tree(Translate_context* context)
9315 Gogo* gogo = context->gogo();
9316 source_location loc = this->location();
9318 Array_type* array_type = this->array_->type()->array_type();
9319 if (array_type == NULL)
9321 gcc_assert(this->array_->type()->is_error_type());
9322 return error_mark_node;
9325 tree type_tree = array_type->get_tree(gogo);
9326 if (type_tree == error_mark_node)
9327 return error_mark_node;
9329 tree array_tree = this->array_->get_tree(context);
9330 if (array_tree == error_mark_node)
9331 return error_mark_node;
9333 if (array_type->length() == NULL && !DECL_P(array_tree))
9334 array_tree = save_expr(array_tree);
9335 tree length_tree = array_type->length_tree(gogo, array_tree);
9336 if (length_tree == error_mark_node)
9337 return error_mark_node;
9338 length_tree = save_expr(length_tree);
9339 tree length_type = TREE_TYPE(length_tree);
9341 tree bad_index = boolean_false_node;
9343 tree start_tree = this->start_->get_tree(context);
9344 if (start_tree == error_mark_node)
9345 return error_mark_node;
9346 if (!DECL_P(start_tree))
9347 start_tree = save_expr(start_tree);
9348 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9349 start_tree = convert_to_integer(length_type, start_tree);
9351 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9354 start_tree = fold_convert_loc(loc, length_type, start_tree);
9355 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
9356 fold_build2_loc(loc,
9360 boolean_type_node, start_tree,
9363 int code = (array_type->length() != NULL
9364 ? (this->end_ == NULL
9365 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9366 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9367 : (this->end_ == NULL
9368 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9369 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9370 tree crash = Gogo::runtime_error(code, loc);
9372 if (this->end_ == NULL)
9374 // Simple array indexing. This has to return an l-value, so
9375 // wrap the index check into START_TREE.
9376 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9377 build3(COND_EXPR, void_type_node,
9378 bad_index, crash, NULL_TREE),
9380 start_tree = fold_convert_loc(loc, sizetype, start_tree);
9382 if (array_type->length() != NULL)
9385 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9386 start_tree, NULL_TREE, NULL_TREE);
9391 tree values = array_type->value_pointer_tree(gogo, array_tree);
9392 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9393 if (element_type_tree == error_mark_node)
9394 return error_mark_node;
9395 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9396 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9397 start_tree, element_size);
9398 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9399 TREE_TYPE(values), values, offset);
9400 return build_fold_indirect_ref(ptr);
9406 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
9407 if (capacity_tree == error_mark_node)
9408 return error_mark_node;
9409 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
9412 if (this->end_->is_nil_expression())
9413 end_tree = length_tree;
9416 end_tree = this->end_->get_tree(context);
9417 if (end_tree == error_mark_node)
9418 return error_mark_node;
9419 if (!DECL_P(end_tree))
9420 end_tree = save_expr(end_tree);
9421 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9422 end_tree = convert_to_integer(length_type, end_tree);
9424 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9427 end_tree = fold_convert_loc(loc, length_type, end_tree);
9429 capacity_tree = save_expr(capacity_tree);
9430 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9431 fold_build2_loc(loc, LT_EXPR,
9433 end_tree, start_tree),
9434 fold_build2_loc(loc, GT_EXPR,
9436 end_tree, capacity_tree));
9437 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9438 bad_index, bad_end);
9441 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9442 if (element_type_tree == error_mark_node)
9443 return error_mark_node;
9444 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9446 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9447 fold_convert_loc(loc, sizetype, start_tree),
9450 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9451 if (value_pointer == error_mark_node)
9452 return error_mark_node;
9454 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9455 TREE_TYPE(value_pointer),
9456 value_pointer, offset);
9458 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9459 end_tree, start_tree);
9461 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9462 capacity_tree, start_tree);
9464 tree struct_tree = this->type()->get_tree(gogo);
9465 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9467 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9469 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9470 tree field = TYPE_FIELDS(struct_tree);
9471 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9473 elt->value = value_pointer;
9475 elt = VEC_quick_push(constructor_elt, init, NULL);
9476 field = DECL_CHAIN(field);
9477 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9479 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
9481 elt = VEC_quick_push(constructor_elt, init, NULL);
9482 field = DECL_CHAIN(field);
9483 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9485 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
9487 tree constructor = build_constructor(struct_tree, init);
9489 if (TREE_CONSTANT(value_pointer)
9490 && TREE_CONSTANT(result_length_tree)
9491 && TREE_CONSTANT(result_capacity_tree))
9492 TREE_CONSTANT(constructor) = 1;
9494 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
9495 build3(COND_EXPR, void_type_node,
9496 bad_index, crash, NULL_TREE),
9500 // Make an array index expression. END may be NULL.
9503 Expression::make_array_index(Expression* array, Expression* start,
9504 Expression* end, source_location location)
9506 // Taking a slice of a composite literal requires moving the literal
9508 if (end != NULL && array->is_composite_literal())
9510 array = Expression::make_heap_composite(array, location);
9511 array = Expression::make_unary(OPERATOR_MULT, array, location);
9513 return new Array_index_expression(array, start, end, location);
9516 // A string index. This is used for both indexing and slicing.
9518 class String_index_expression : public Expression
9521 String_index_expression(Expression* string, Expression* start,
9522 Expression* end, source_location location)
9523 : Expression(EXPRESSION_STRING_INDEX, location),
9524 string_(string), start_(start), end_(end)
9529 do_traverse(Traverse*);
9535 do_determine_type(const Type_context*);
9538 do_check_types(Gogo*);
9543 return Expression::make_string_index(this->string_->copy(),
9544 this->start_->copy(),
9547 : this->end_->copy()),
9552 do_get_tree(Translate_context*);
9555 // The string we are getting a value from.
9556 Expression* string_;
9557 // The start or only index.
9559 // The end index of a slice. This may be NULL for a single index,
9560 // or it may be a nil expression for the length of the string.
9564 // String index traversal.
9567 String_index_expression::do_traverse(Traverse* traverse)
9569 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
9570 return TRAVERSE_EXIT;
9571 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9572 return TRAVERSE_EXIT;
9573 if (this->end_ != NULL)
9575 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9576 return TRAVERSE_EXIT;
9578 return TRAVERSE_CONTINUE;
9581 // Return the type of a string index.
9584 String_index_expression::do_type()
9586 if (this->end_ == NULL)
9587 return Type::lookup_integer_type("uint8");
9589 return this->string_->type();
9592 // Determine the type of a string index.
9595 String_index_expression::do_determine_type(const Type_context*)
9597 this->string_->determine_type_no_context();
9598 this->start_->determine_type_no_context();
9599 if (this->end_ != NULL)
9600 this->end_->determine_type_no_context();
9603 // Check types of a string index.
9606 String_index_expression::do_check_types(Gogo*)
9608 if (this->start_->type()->integer_type() == NULL)
9609 this->report_error(_("index must be integer"));
9610 if (this->end_ != NULL
9611 && this->end_->type()->integer_type() == NULL
9612 && !this->end_->is_nil_expression())
9613 this->report_error(_("slice end must be integer"));
9616 bool sval_valid = this->string_->string_constant_value(&sval);
9621 if (this->start_->integer_constant_value(true, ival, &dummy))
9623 if (mpz_sgn(ival) < 0
9624 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
9626 error_at(this->start_->location(), "string index out of bounds");
9627 this->set_is_error();
9630 if (this->end_ != NULL && !this->end_->is_nil_expression())
9632 if (this->end_->integer_constant_value(true, ival, &dummy))
9634 if (mpz_sgn(ival) < 0
9635 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
9637 error_at(this->end_->location(), "string index out of bounds");
9638 this->set_is_error();
9645 // Get a tree for a string index.
9648 String_index_expression::do_get_tree(Translate_context* context)
9650 source_location loc = this->location();
9652 tree string_tree = this->string_->get_tree(context);
9653 if (string_tree == error_mark_node)
9654 return error_mark_node;
9656 if (this->string_->type()->points_to() != NULL)
9657 string_tree = build_fold_indirect_ref(string_tree);
9658 if (!DECL_P(string_tree))
9659 string_tree = save_expr(string_tree);
9660 tree string_type = TREE_TYPE(string_tree);
9662 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
9663 length_tree = save_expr(length_tree);
9664 tree length_type = TREE_TYPE(length_tree);
9666 tree bad_index = boolean_false_node;
9668 tree start_tree = this->start_->get_tree(context);
9669 if (start_tree == error_mark_node)
9670 return error_mark_node;
9671 if (!DECL_P(start_tree))
9672 start_tree = save_expr(start_tree);
9673 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9674 start_tree = convert_to_integer(length_type, start_tree);
9676 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9679 start_tree = fold_convert_loc(loc, length_type, start_tree);
9681 int code = (this->end_ == NULL
9682 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9683 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
9684 tree crash = Gogo::runtime_error(code, loc);
9686 if (this->end_ == NULL)
9688 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9690 fold_build2_loc(loc, GE_EXPR,
9692 start_tree, length_tree));
9694 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
9695 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
9697 fold_convert_loc(loc, sizetype, start_tree));
9698 tree index = build_fold_indirect_ref_loc(loc, ptr);
9700 return build2(COMPOUND_EXPR, TREE_TYPE(index),
9701 build3(COND_EXPR, void_type_node,
9702 bad_index, crash, NULL_TREE),
9708 if (this->end_->is_nil_expression())
9709 end_tree = build_int_cst(length_type, -1);
9712 end_tree = this->end_->get_tree(context);
9713 if (end_tree == error_mark_node)
9714 return error_mark_node;
9715 if (!DECL_P(end_tree))
9716 end_tree = save_expr(end_tree);
9717 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9718 end_tree = convert_to_integer(length_type, end_tree);
9720 bad_index = Expression::check_bounds(end_tree, length_type,
9723 end_tree = fold_convert_loc(loc, length_type, end_tree);
9726 static tree strslice_fndecl;
9727 tree ret = Gogo::call_builtin(&strslice_fndecl,
9729 "__go_string_slice",
9738 if (ret == error_mark_node)
9739 return error_mark_node;
9740 // This will panic if the bounds are out of range for the
9742 TREE_NOTHROW(strslice_fndecl) = 0;
9744 if (bad_index == boolean_false_node)
9747 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
9748 build3(COND_EXPR, void_type_node,
9749 bad_index, crash, NULL_TREE),
9754 // Make a string index expression. END may be NULL.
9757 Expression::make_string_index(Expression* string, Expression* start,
9758 Expression* end, source_location location)
9760 return new String_index_expression(string, start, end, location);
9765 // Get the type of the map.
9768 Map_index_expression::get_map_type() const
9770 Map_type* mt = this->map_->type()->deref()->map_type();
9772 gcc_assert(saw_errors());
9776 // Map index traversal.
9779 Map_index_expression::do_traverse(Traverse* traverse)
9781 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
9782 return TRAVERSE_EXIT;
9783 return Expression::traverse(&this->index_, traverse);
9786 // Return the type of a map index.
9789 Map_index_expression::do_type()
9791 Map_type* mt = this->get_map_type();
9793 return Type::make_error_type();
9794 Type* type = mt->val_type();
9795 // If this map index is in a tuple assignment, we actually return a
9796 // pointer to the value type. Tuple_map_assignment_statement is
9797 // responsible for handling this correctly. We need to get the type
9798 // right in case this gets assigned to a temporary variable.
9799 if (this->is_in_tuple_assignment_)
9800 type = Type::make_pointer_type(type);
9804 // Fix the type of a map index.
9807 Map_index_expression::do_determine_type(const Type_context*)
9809 this->map_->determine_type_no_context();
9810 Map_type* mt = this->get_map_type();
9811 Type* key_type = mt == NULL ? NULL : mt->key_type();
9812 Type_context subcontext(key_type, false);
9813 this->index_->determine_type(&subcontext);
9816 // Check types of a map index.
9819 Map_index_expression::do_check_types(Gogo*)
9822 Map_type* mt = this->get_map_type();
9825 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
9828 this->report_error(_("incompatible type for map index"));
9831 error_at(this->location(), "incompatible type for map index (%s)",
9833 this->set_is_error();
9838 // Get a tree for a map index.
9841 Map_index_expression::do_get_tree(Translate_context* context)
9843 Map_type* type = this->get_map_type();
9845 return error_mark_node;
9847 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
9848 if (valptr == error_mark_node)
9849 return error_mark_node;
9850 valptr = save_expr(valptr);
9852 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
9854 if (this->is_lvalue_)
9855 return build_fold_indirect_ref(valptr);
9856 else if (this->is_in_tuple_assignment_)
9858 // Tuple_map_assignment_statement is responsible for using this
9864 return fold_build3(COND_EXPR, val_type_tree,
9865 fold_build2(EQ_EXPR, boolean_type_node, valptr,
9866 fold_convert(TREE_TYPE(valptr),
9867 null_pointer_node)),
9868 type->val_type()->get_init_tree(context->gogo(),
9870 build_fold_indirect_ref(valptr));
9874 // Get a tree for the map index. This returns a tree which evaluates
9875 // to a pointer to a value. The pointer will be NULL if the key is
9879 Map_index_expression::get_value_pointer(Translate_context* context,
9882 Map_type* type = this->get_map_type();
9884 return error_mark_node;
9886 tree map_tree = this->map_->get_tree(context);
9887 tree index_tree = this->index_->get_tree(context);
9888 index_tree = Expression::convert_for_assignment(context, type->key_type(),
9889 this->index_->type(),
9892 if (map_tree == error_mark_node || index_tree == error_mark_node)
9893 return error_mark_node;
9895 if (this->map_->type()->points_to() != NULL)
9896 map_tree = build_fold_indirect_ref(map_tree);
9898 // We need to pass in a pointer to the key, so stuff it into a
9902 if (current_function_decl != NULL)
9904 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
9905 DECL_IGNORED_P(tmp) = 0;
9906 DECL_INITIAL(tmp) = index_tree;
9907 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
9908 TREE_ADDRESSABLE(tmp) = 1;
9912 tmp = build_decl(this->location(), VAR_DECL, create_tmp_var_name("M"),
9913 TREE_TYPE(index_tree));
9914 DECL_EXTERNAL(tmp) = 0;
9915 TREE_PUBLIC(tmp) = 0;
9916 TREE_STATIC(tmp) = 1;
9917 DECL_ARTIFICIAL(tmp) = 1;
9918 if (!TREE_CONSTANT(index_tree))
9919 make_tmp = fold_build2_loc(this->location(), INIT_EXPR, void_type_node,
9923 TREE_READONLY(tmp) = 1;
9924 TREE_CONSTANT(tmp) = 1;
9925 DECL_INITIAL(tmp) = index_tree;
9926 make_tmp = NULL_TREE;
9928 rest_of_decl_compilation(tmp, 1, 0);
9930 tree tmpref = fold_convert_loc(this->location(), const_ptr_type_node,
9931 build_fold_addr_expr_loc(this->location(),
9934 static tree map_index_fndecl;
9935 tree call = Gogo::call_builtin(&map_index_fndecl,
9939 const_ptr_type_node,
9940 TREE_TYPE(map_tree),
9942 const_ptr_type_node,
9947 : boolean_false_node));
9948 if (call == error_mark_node)
9949 return error_mark_node;
9950 // This can panic on a map of interface type if the interface holds
9951 // an uncomparable or unhashable type.
9952 TREE_NOTHROW(map_index_fndecl) = 0;
9954 tree val_type_tree = type->val_type()->get_tree(context->gogo());
9955 if (val_type_tree == error_mark_node)
9956 return error_mark_node;
9957 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
9959 tree ret = fold_convert_loc(this->location(), ptr_val_type_tree, call);
9960 if (make_tmp != NULL_TREE)
9961 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
9965 // Make a map index expression.
9967 Map_index_expression*
9968 Expression::make_map_index(Expression* map, Expression* index,
9969 source_location location)
9971 return new Map_index_expression(map, index, location);
9974 // Class Field_reference_expression.
9976 // Return the type of a field reference.
9979 Field_reference_expression::do_type()
9981 Type* type = this->expr_->type();
9982 if (type->is_error_type())
9984 Struct_type* struct_type = type->struct_type();
9985 gcc_assert(struct_type != NULL);
9986 return struct_type->field(this->field_index_)->type();
9989 // Check the types for a field reference.
9992 Field_reference_expression::do_check_types(Gogo*)
9994 Type* type = this->expr_->type();
9995 if (type->is_error_type())
9997 Struct_type* struct_type = type->struct_type();
9998 gcc_assert(struct_type != NULL);
9999 gcc_assert(struct_type->field(this->field_index_) != NULL);
10002 // Get a tree for a field reference.
10005 Field_reference_expression::do_get_tree(Translate_context* context)
10007 tree struct_tree = this->expr_->get_tree(context);
10008 if (struct_tree == error_mark_node
10009 || TREE_TYPE(struct_tree) == error_mark_node)
10010 return error_mark_node;
10011 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
10012 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
10013 if (field == NULL_TREE)
10015 // This can happen for a type which refers to itself indirectly
10016 // and then turns out to be erroneous.
10017 gcc_assert(saw_errors());
10018 return error_mark_node;
10020 for (unsigned int i = this->field_index_; i > 0; --i)
10022 field = DECL_CHAIN(field);
10023 gcc_assert(field != NULL_TREE);
10025 if (TREE_TYPE(field) == error_mark_node)
10026 return error_mark_node;
10027 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
10031 // Make a reference to a qualified identifier in an expression.
10033 Field_reference_expression*
10034 Expression::make_field_reference(Expression* expr, unsigned int field_index,
10035 source_location location)
10037 return new Field_reference_expression(expr, field_index, location);
10040 // Class Interface_field_reference_expression.
10042 // Return a tree for the pointer to the function to call.
10045 Interface_field_reference_expression::get_function_tree(Translate_context*,
10048 if (this->expr_->type()->points_to() != NULL)
10049 expr = build_fold_indirect_ref(expr);
10051 tree expr_type = TREE_TYPE(expr);
10052 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10054 tree field = TYPE_FIELDS(expr_type);
10055 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
10057 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10058 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table)));
10060 table = build_fold_indirect_ref(table);
10061 gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
10063 std::string name = Gogo::unpack_hidden_name(this->name_);
10064 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
10065 field != NULL_TREE;
10066 field = DECL_CHAIN(field))
10068 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
10071 gcc_assert(field != NULL_TREE);
10073 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
10076 // Return a tree for the first argument to pass to the interface
10080 Interface_field_reference_expression::get_underlying_object_tree(
10081 Translate_context*,
10084 if (this->expr_->type()->points_to() != NULL)
10085 expr = build_fold_indirect_ref(expr);
10087 tree expr_type = TREE_TYPE(expr);
10088 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10090 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
10091 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
10093 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10099 Interface_field_reference_expression::do_traverse(Traverse* traverse)
10101 return Expression::traverse(&this->expr_, traverse);
10104 // Return the type of an interface field reference.
10107 Interface_field_reference_expression::do_type()
10109 Type* expr_type = this->expr_->type();
10111 Type* points_to = expr_type->points_to();
10112 if (points_to != NULL)
10113 expr_type = points_to;
10115 Interface_type* interface_type = expr_type->interface_type();
10116 if (interface_type == NULL)
10117 return Type::make_error_type();
10119 const Typed_identifier* method = interface_type->find_method(this->name_);
10120 if (method == NULL)
10121 return Type::make_error_type();
10123 return method->type();
10126 // Determine types.
10129 Interface_field_reference_expression::do_determine_type(const Type_context*)
10131 this->expr_->determine_type_no_context();
10134 // Check the types for an interface field reference.
10137 Interface_field_reference_expression::do_check_types(Gogo*)
10139 Type* type = this->expr_->type();
10141 Type* points_to = type->points_to();
10142 if (points_to != NULL)
10145 Interface_type* interface_type = type->interface_type();
10146 if (interface_type == NULL)
10147 this->report_error(_("expected interface or pointer to interface"));
10150 const Typed_identifier* method =
10151 interface_type->find_method(this->name_);
10152 if (method == NULL)
10154 error_at(this->location(), "method %qs not in interface",
10155 Gogo::message_name(this->name_).c_str());
10156 this->set_is_error();
10161 // Get a tree for a reference to a field in an interface. There is no
10162 // standard tree type representation for this: it's a function
10163 // attached to its first argument, like a Bound_method_expression.
10164 // The only places it may currently be used are in a Call_expression
10165 // or a Go_statement, which will take it apart directly. So this has
10166 // nothing to do at present.
10169 Interface_field_reference_expression::do_get_tree(Translate_context*)
10174 // Make a reference to a field in an interface.
10177 Expression::make_interface_field_reference(Expression* expr,
10178 const std::string& field,
10179 source_location location)
10181 return new Interface_field_reference_expression(expr, field, location);
10184 // A general selector. This is a Parser_expression for LEFT.NAME. It
10185 // is lowered after we know the type of the left hand side.
10187 class Selector_expression : public Parser_expression
10190 Selector_expression(Expression* left, const std::string& name,
10191 source_location location)
10192 : Parser_expression(EXPRESSION_SELECTOR, location),
10193 left_(left), name_(name)
10198 do_traverse(Traverse* traverse)
10199 { return Expression::traverse(&this->left_, traverse); }
10202 do_lower(Gogo*, Named_object*, int);
10207 return new Selector_expression(this->left_->copy(), this->name_,
10213 lower_method_expression(Gogo*);
10215 // The expression on the left hand side.
10217 // The name on the right hand side.
10221 // Lower a selector expression once we know the real type of the left
10225 Selector_expression::do_lower(Gogo* gogo, Named_object*, int)
10227 Expression* left = this->left_;
10228 if (left->is_type_expression())
10229 return this->lower_method_expression(gogo);
10230 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
10234 // Lower a method expression T.M or (*T).M. We turn this into a
10235 // function literal.
10238 Selector_expression::lower_method_expression(Gogo* gogo)
10240 source_location location = this->location();
10241 Type* type = this->left_->type();
10242 const std::string& name(this->name_);
10245 if (type->points_to() == NULL)
10246 is_pointer = false;
10250 type = type->points_to();
10252 Named_type* nt = type->named_type();
10256 ("method expression requires named type or "
10257 "pointer to named type"));
10258 return Expression::make_error(location);
10262 Method* method = nt->method_function(name, &is_ambiguous);
10263 if (method == NULL)
10266 error_at(location, "type %<%s%> has no method %<%s%>",
10267 nt->message_name().c_str(),
10268 Gogo::message_name(name).c_str());
10270 error_at(location, "method %<%s%> is ambiguous in type %<%s%>",
10271 Gogo::message_name(name).c_str(),
10272 nt->message_name().c_str());
10273 return Expression::make_error(location);
10276 if (!is_pointer && !method->is_value_method())
10278 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
10279 nt->message_name().c_str(),
10280 Gogo::message_name(name).c_str());
10281 return Expression::make_error(location);
10284 // Build a new function type in which the receiver becomes the first
10286 Function_type* method_type = method->type();
10287 gcc_assert(method_type->is_method());
10289 const char* const receiver_name = "$this";
10290 Typed_identifier_list* parameters = new Typed_identifier_list();
10291 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
10294 const Typed_identifier_list* method_parameters = method_type->parameters();
10295 if (method_parameters != NULL)
10297 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10298 p != method_parameters->end();
10300 parameters->push_back(*p);
10303 const Typed_identifier_list* method_results = method_type->results();
10304 Typed_identifier_list* results;
10305 if (method_results == NULL)
10309 results = new Typed_identifier_list();
10310 for (Typed_identifier_list::const_iterator p = method_results->begin();
10311 p != method_results->end();
10313 results->push_back(*p);
10316 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
10318 if (method_type->is_varargs())
10319 fntype->set_is_varargs();
10321 // We generate methods which always takes a pointer to the receiver
10322 // as their first argument. If this is for a pointer type, we can
10323 // simply reuse the existing function. We use an internal hack to
10324 // get the right type.
10328 Named_object* mno = (method->needs_stub_method()
10329 ? method->stub_object()
10330 : method->named_object());
10331 Expression* f = Expression::make_func_reference(mno, NULL, location);
10332 f = Expression::make_cast(fntype, f, location);
10333 Type_conversion_expression* tce =
10334 static_cast<Type_conversion_expression*>(f);
10335 tce->set_may_convert_function_types();
10339 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
10342 Named_object* vno = gogo->lookup(receiver_name, NULL);
10343 gcc_assert(vno != NULL);
10344 Expression* ve = Expression::make_var_reference(vno, location);
10345 Expression* bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
10347 // Even though we found the method above, if it has an error type we
10348 // may see an error here.
10349 if (bm->is_error_expression())
10351 gogo->finish_function(location);
10355 Expression_list* args;
10356 if (method_parameters == NULL)
10360 args = new Expression_list();
10361 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10362 p != method_parameters->end();
10365 vno = gogo->lookup(p->name(), NULL);
10366 gcc_assert(vno != NULL);
10367 args->push_back(Expression::make_var_reference(vno, location));
10371 Call_expression* call = Expression::make_call(bm, args,
10372 method_type->is_varargs(),
10375 size_t count = call->result_count();
10378 s = Statement::make_statement(call);
10381 Expression_list* retvals = new Expression_list();
10383 retvals->push_back(call);
10386 for (size_t i = 0; i < count; ++i)
10387 retvals->push_back(Expression::make_call_result(call, i));
10389 s = Statement::make_return_statement(no->func_value()->type()->results(),
10390 retvals, location);
10392 gogo->add_statement(s);
10394 gogo->finish_function(location);
10396 return Expression::make_func_reference(no, NULL, location);
10399 // Make a selector expression.
10402 Expression::make_selector(Expression* left, const std::string& name,
10403 source_location location)
10405 return new Selector_expression(left, name, location);
10408 // Implement the builtin function new.
10410 class Allocation_expression : public Expression
10413 Allocation_expression(Type* type, source_location location)
10414 : Expression(EXPRESSION_ALLOCATION, location),
10420 do_traverse(Traverse* traverse)
10421 { return Type::traverse(this->type_, traverse); }
10425 { return Type::make_pointer_type(this->type_); }
10428 do_determine_type(const Type_context*)
10432 do_check_types(Gogo*);
10436 { return new Allocation_expression(this->type_, this->location()); }
10439 do_get_tree(Translate_context*);
10442 // The type we are allocating.
10446 // Check the type of an allocation expression.
10449 Allocation_expression::do_check_types(Gogo*)
10451 if (this->type_->function_type() != NULL)
10452 this->report_error(_("invalid new of function type"));
10455 // Return a tree for an allocation expression.
10458 Allocation_expression::do_get_tree(Translate_context* context)
10460 tree type_tree = this->type_->get_tree(context->gogo());
10461 if (type_tree == error_mark_node)
10462 return error_mark_node;
10463 tree size_tree = TYPE_SIZE_UNIT(type_tree);
10464 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
10466 if (space == error_mark_node)
10467 return error_mark_node;
10468 return fold_convert(build_pointer_type(type_tree), space);
10471 // Make an allocation expression.
10474 Expression::make_allocation(Type* type, source_location location)
10476 return new Allocation_expression(type, location);
10479 // Implement the builtin function make.
10481 class Make_expression : public Expression
10484 Make_expression(Type* type, Expression_list* args, source_location location)
10485 : Expression(EXPRESSION_MAKE, location),
10486 type_(type), args_(args)
10491 do_traverse(Traverse* traverse);
10495 { return this->type_; }
10498 do_determine_type(const Type_context*);
10501 do_check_types(Gogo*);
10506 return new Make_expression(this->type_, this->args_->copy(),
10511 do_get_tree(Translate_context*);
10514 // The type we are making.
10516 // The arguments to pass to the make routine.
10517 Expression_list* args_;
10523 Make_expression::do_traverse(Traverse* traverse)
10525 if (this->args_ != NULL
10526 && this->args_->traverse(traverse) == TRAVERSE_EXIT)
10527 return TRAVERSE_EXIT;
10528 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10529 return TRAVERSE_EXIT;
10530 return TRAVERSE_CONTINUE;
10533 // Set types of arguments.
10536 Make_expression::do_determine_type(const Type_context*)
10538 if (this->args_ != NULL)
10540 Type_context context(Type::lookup_integer_type("int"), false);
10541 for (Expression_list::const_iterator pe = this->args_->begin();
10542 pe != this->args_->end();
10544 (*pe)->determine_type(&context);
10548 // Check types for a make expression.
10551 Make_expression::do_check_types(Gogo*)
10553 if (this->type_->channel_type() == NULL
10554 && this->type_->map_type() == NULL
10555 && (this->type_->array_type() == NULL
10556 || this->type_->array_type()->length() != NULL))
10557 this->report_error(_("invalid type for make function"));
10558 else if (!this->type_->check_make_expression(this->args_, this->location()))
10559 this->set_is_error();
10562 // Return a tree for a make expression.
10565 Make_expression::do_get_tree(Translate_context* context)
10567 return this->type_->make_expression_tree(context, this->args_,
10571 // Make a make expression.
10574 Expression::make_make(Type* type, Expression_list* args,
10575 source_location location)
10577 return new Make_expression(type, args, location);
10580 // Construct a struct.
10582 class Struct_construction_expression : public Expression
10585 Struct_construction_expression(Type* type, Expression_list* vals,
10586 source_location location)
10587 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
10588 type_(type), vals_(vals)
10591 // Return whether this is a constant initializer.
10593 is_constant_struct() const;
10597 do_traverse(Traverse* traverse);
10601 { return this->type_; }
10604 do_determine_type(const Type_context*);
10607 do_check_types(Gogo*);
10612 return new Struct_construction_expression(this->type_, this->vals_->copy(),
10617 do_is_addressable() const
10621 do_get_tree(Translate_context*);
10624 do_export(Export*) const;
10627 // The type of the struct to construct.
10629 // The list of values, in order of the fields in the struct. A NULL
10630 // entry means that the field should be zero-initialized.
10631 Expression_list* vals_;
10637 Struct_construction_expression::do_traverse(Traverse* traverse)
10639 if (this->vals_ != NULL
10640 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10641 return TRAVERSE_EXIT;
10642 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10643 return TRAVERSE_EXIT;
10644 return TRAVERSE_CONTINUE;
10647 // Return whether this is a constant initializer.
10650 Struct_construction_expression::is_constant_struct() const
10652 if (this->vals_ == NULL)
10654 for (Expression_list::const_iterator pv = this->vals_->begin();
10655 pv != this->vals_->end();
10659 && !(*pv)->is_constant()
10660 && (!(*pv)->is_composite_literal()
10661 || (*pv)->is_nonconstant_composite_literal()))
10665 const Struct_field_list* fields = this->type_->struct_type()->fields();
10666 for (Struct_field_list::const_iterator pf = fields->begin();
10667 pf != fields->end();
10670 // There are no constant constructors for interfaces.
10671 if (pf->type()->interface_type() != NULL)
10678 // Final type determination.
10681 Struct_construction_expression::do_determine_type(const Type_context*)
10683 if (this->vals_ == NULL)
10685 const Struct_field_list* fields = this->type_->struct_type()->fields();
10686 Expression_list::const_iterator pv = this->vals_->begin();
10687 for (Struct_field_list::const_iterator pf = fields->begin();
10688 pf != fields->end();
10691 if (pv == this->vals_->end())
10695 Type_context subcontext(pf->type(), false);
10696 (*pv)->determine_type(&subcontext);
10699 // Extra values are an error we will report elsewhere; we still want
10700 // to determine the type to avoid knockon errors.
10701 for (; pv != this->vals_->end(); ++pv)
10702 (*pv)->determine_type_no_context();
10708 Struct_construction_expression::do_check_types(Gogo*)
10710 if (this->vals_ == NULL)
10713 Struct_type* st = this->type_->struct_type();
10714 if (this->vals_->size() > st->field_count())
10716 this->report_error(_("too many expressions for struct"));
10720 const Struct_field_list* fields = st->fields();
10721 Expression_list::const_iterator pv = this->vals_->begin();
10723 for (Struct_field_list::const_iterator pf = fields->begin();
10724 pf != fields->end();
10727 if (pv == this->vals_->end())
10729 this->report_error(_("too few expressions for struct"));
10736 std::string reason;
10737 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
10739 if (reason.empty())
10740 error_at((*pv)->location(),
10741 "incompatible type for field %d in struct construction",
10744 error_at((*pv)->location(),
10745 ("incompatible type for field %d in "
10746 "struct construction (%s)"),
10747 i + 1, reason.c_str());
10748 this->set_is_error();
10751 gcc_assert(pv == this->vals_->end());
10754 // Return a tree for constructing a struct.
10757 Struct_construction_expression::do_get_tree(Translate_context* context)
10759 Gogo* gogo = context->gogo();
10761 if (this->vals_ == NULL)
10762 return this->type_->get_init_tree(gogo, false);
10764 tree type_tree = this->type_->get_tree(gogo);
10765 if (type_tree == error_mark_node)
10766 return error_mark_node;
10767 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10769 bool is_constant = true;
10770 const Struct_field_list* fields = this->type_->struct_type()->fields();
10771 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
10773 Struct_field_list::const_iterator pf = fields->begin();
10774 Expression_list::const_iterator pv = this->vals_->begin();
10775 for (tree field = TYPE_FIELDS(type_tree);
10776 field != NULL_TREE;
10777 field = DECL_CHAIN(field), ++pf)
10779 gcc_assert(pf != fields->end());
10782 if (pv == this->vals_->end())
10783 val = pf->type()->get_init_tree(gogo, false);
10784 else if (*pv == NULL)
10786 val = pf->type()->get_init_tree(gogo, false);
10791 val = Expression::convert_for_assignment(context, pf->type(),
10793 (*pv)->get_tree(context),
10798 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
10799 return error_mark_node;
10801 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
10802 elt->index = field;
10804 if (!TREE_CONSTANT(val))
10805 is_constant = false;
10807 gcc_assert(pf == fields->end());
10809 tree ret = build_constructor(type_tree, elts);
10811 TREE_CONSTANT(ret) = 1;
10815 // Export a struct construction.
10818 Struct_construction_expression::do_export(Export* exp) const
10820 exp->write_c_string("convert(");
10821 exp->write_type(this->type_);
10822 for (Expression_list::const_iterator pv = this->vals_->begin();
10823 pv != this->vals_->end();
10826 exp->write_c_string(", ");
10828 (*pv)->export_expression(exp);
10830 exp->write_c_string(")");
10833 // Make a struct composite literal. This used by the thunk code.
10836 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
10837 source_location location)
10839 gcc_assert(type->struct_type() != NULL);
10840 return new Struct_construction_expression(type, vals, location);
10843 // Construct an array. This class is not used directly; instead we
10844 // use the child classes, Fixed_array_construction_expression and
10845 // Open_array_construction_expression.
10847 class Array_construction_expression : public Expression
10850 Array_construction_expression(Expression_classification classification,
10851 Type* type, Expression_list* vals,
10852 source_location location)
10853 : Expression(classification, location),
10854 type_(type), vals_(vals)
10858 // Return whether this is a constant initializer.
10860 is_constant_array() const;
10862 // Return the number of elements.
10864 element_count() const
10865 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
10869 do_traverse(Traverse* traverse);
10873 { return this->type_; }
10876 do_determine_type(const Type_context*);
10879 do_check_types(Gogo*);
10882 do_is_addressable() const
10886 do_export(Export*) const;
10888 // The list of values.
10891 { return this->vals_; }
10893 // Get a constructor tree for the array values.
10895 get_constructor_tree(Translate_context* context, tree type_tree);
10898 // The type of the array to construct.
10900 // The list of values.
10901 Expression_list* vals_;
10907 Array_construction_expression::do_traverse(Traverse* traverse)
10909 if (this->vals_ != NULL
10910 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10911 return TRAVERSE_EXIT;
10912 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10913 return TRAVERSE_EXIT;
10914 return TRAVERSE_CONTINUE;
10917 // Return whether this is a constant initializer.
10920 Array_construction_expression::is_constant_array() const
10922 if (this->vals_ == NULL)
10925 // There are no constant constructors for interfaces.
10926 if (this->type_->array_type()->element_type()->interface_type() != NULL)
10929 for (Expression_list::const_iterator pv = this->vals_->begin();
10930 pv != this->vals_->end();
10934 && !(*pv)->is_constant()
10935 && (!(*pv)->is_composite_literal()
10936 || (*pv)->is_nonconstant_composite_literal()))
10942 // Final type determination.
10945 Array_construction_expression::do_determine_type(const Type_context*)
10947 if (this->vals_ == NULL)
10949 Type_context subcontext(this->type_->array_type()->element_type(), false);
10950 for (Expression_list::const_iterator pv = this->vals_->begin();
10951 pv != this->vals_->end();
10955 (*pv)->determine_type(&subcontext);
10962 Array_construction_expression::do_check_types(Gogo*)
10964 if (this->vals_ == NULL)
10967 Array_type* at = this->type_->array_type();
10969 Type* element_type = at->element_type();
10970 for (Expression_list::const_iterator pv = this->vals_->begin();
10971 pv != this->vals_->end();
10975 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
10977 error_at((*pv)->location(),
10978 "incompatible type for element %d in composite literal",
10980 this->set_is_error();
10984 Expression* length = at->length();
10985 if (length != NULL)
10990 if (at->length()->integer_constant_value(true, val, &type))
10992 if (this->vals_->size() > mpz_get_ui(val))
10993 this->report_error(_("too many elements in composite literal"));
10999 // Get a constructor tree for the array values.
11002 Array_construction_expression::get_constructor_tree(Translate_context* context,
11005 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11006 (this->vals_ == NULL
11008 : this->vals_->size()));
11009 Type* element_type = this->type_->array_type()->element_type();
11010 bool is_constant = true;
11011 if (this->vals_ != NULL)
11014 for (Expression_list::const_iterator pv = this->vals_->begin();
11015 pv != this->vals_->end();
11018 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
11019 elt->index = size_int(i);
11021 elt->value = element_type->get_init_tree(context->gogo(), false);
11024 tree value_tree = (*pv)->get_tree(context);
11025 elt->value = Expression::convert_for_assignment(context,
11031 if (elt->value == error_mark_node)
11032 return error_mark_node;
11033 if (!TREE_CONSTANT(elt->value))
11034 is_constant = false;
11038 tree ret = build_constructor(type_tree, values);
11040 TREE_CONSTANT(ret) = 1;
11044 // Export an array construction.
11047 Array_construction_expression::do_export(Export* exp) const
11049 exp->write_c_string("convert(");
11050 exp->write_type(this->type_);
11051 if (this->vals_ != NULL)
11053 for (Expression_list::const_iterator pv = this->vals_->begin();
11054 pv != this->vals_->end();
11057 exp->write_c_string(", ");
11059 (*pv)->export_expression(exp);
11062 exp->write_c_string(")");
11065 // Construct a fixed array.
11067 class Fixed_array_construction_expression :
11068 public Array_construction_expression
11071 Fixed_array_construction_expression(Type* type, Expression_list* vals,
11072 source_location location)
11073 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
11074 type, vals, location)
11076 gcc_assert(type->array_type() != NULL
11077 && type->array_type()->length() != NULL);
11084 return new Fixed_array_construction_expression(this->type(),
11085 (this->vals() == NULL
11087 : this->vals()->copy()),
11092 do_get_tree(Translate_context*);
11095 // Return a tree for constructing a fixed array.
11098 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
11100 return this->get_constructor_tree(context,
11101 this->type()->get_tree(context->gogo()));
11104 // Construct an open array.
11106 class Open_array_construction_expression : public Array_construction_expression
11109 Open_array_construction_expression(Type* type, Expression_list* vals,
11110 source_location location)
11111 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
11112 type, vals, location)
11114 gcc_assert(type->array_type() != NULL
11115 && type->array_type()->length() == NULL);
11119 // Note that taking the address of an open array literal is invalid.
11124 return new Open_array_construction_expression(this->type(),
11125 (this->vals() == NULL
11127 : this->vals()->copy()),
11132 do_get_tree(Translate_context*);
11135 // Return a tree for constructing an open array.
11138 Open_array_construction_expression::do_get_tree(Translate_context* context)
11140 Array_type* array_type = this->type()->array_type();
11141 if (array_type == NULL)
11143 gcc_assert(this->type()->is_error_type());
11144 return error_mark_node;
11147 Type* element_type = array_type->element_type();
11148 tree element_type_tree = element_type->get_tree(context->gogo());
11149 if (element_type_tree == error_mark_node)
11150 return error_mark_node;
11154 if (this->vals() == NULL || this->vals()->empty())
11156 // We need to create a unique value.
11157 tree max = size_int(0);
11158 tree constructor_type = build_array_type(element_type_tree,
11159 build_index_type(max));
11160 if (constructor_type == error_mark_node)
11161 return error_mark_node;
11162 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
11163 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
11164 elt->index = size_int(0);
11165 elt->value = element_type->get_init_tree(context->gogo(), false);
11166 values = build_constructor(constructor_type, vec);
11167 if (TREE_CONSTANT(elt->value))
11168 TREE_CONSTANT(values) = 1;
11169 length_tree = size_int(0);
11173 tree max = size_int(this->vals()->size() - 1);
11174 tree constructor_type = build_array_type(element_type_tree,
11175 build_index_type(max));
11176 if (constructor_type == error_mark_node)
11177 return error_mark_node;
11178 values = this->get_constructor_tree(context, constructor_type);
11179 length_tree = size_int(this->vals()->size());
11182 if (values == error_mark_node)
11183 return error_mark_node;
11185 bool is_constant_initializer = TREE_CONSTANT(values);
11187 // We have to copy the initial values into heap memory if we are in
11188 // a function or if the values are not constants. We also have to
11189 // copy them if they may contain pointers in a non-constant context,
11190 // as otherwise the garbage collector won't see them.
11191 bool copy_to_heap = (context->function() != NULL
11192 || !is_constant_initializer
11193 || (element_type->has_pointer()
11194 && !context->is_const()));
11196 if (is_constant_initializer)
11198 tree tmp = build_decl(this->location(), VAR_DECL,
11199 create_tmp_var_name("C"), TREE_TYPE(values));
11200 DECL_EXTERNAL(tmp) = 0;
11201 TREE_PUBLIC(tmp) = 0;
11202 TREE_STATIC(tmp) = 1;
11203 DECL_ARTIFICIAL(tmp) = 1;
11206 // If we are not copying the value to the heap, we will only
11207 // initialize the value once, so we can use this directly
11208 // rather than copying it. In that case we can't make it
11209 // read-only, because the program is permitted to change it.
11210 TREE_READONLY(tmp) = 1;
11211 TREE_CONSTANT(tmp) = 1;
11213 DECL_INITIAL(tmp) = values;
11214 rest_of_decl_compilation(tmp, 1, 0);
11222 // the initializer will only run once.
11223 space = build_fold_addr_expr(values);
11228 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
11229 space = context->gogo()->allocate_memory(element_type, memsize,
11231 space = save_expr(space);
11233 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
11234 tree ref = build_fold_indirect_ref_loc(this->location(), s);
11235 TREE_THIS_NOTRAP(ref) = 1;
11236 set = build2(MODIFY_EXPR, void_type_node, ref, values);
11239 // Build a constructor for the open array.
11241 tree type_tree = this->type()->get_tree(context->gogo());
11242 if (type_tree == error_mark_node)
11243 return error_mark_node;
11244 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11246 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
11248 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
11249 tree field = TYPE_FIELDS(type_tree);
11250 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
11251 elt->index = field;
11252 elt->value = fold_convert(TREE_TYPE(field), space);
11254 elt = VEC_quick_push(constructor_elt, init, NULL);
11255 field = DECL_CHAIN(field);
11256 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
11257 elt->index = field;
11258 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11260 elt = VEC_quick_push(constructor_elt, init, NULL);
11261 field = DECL_CHAIN(field);
11262 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
11263 elt->index = field;
11264 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11266 tree constructor = build_constructor(type_tree, init);
11267 if (constructor == error_mark_node)
11268 return error_mark_node;
11270 TREE_CONSTANT(constructor) = 1;
11272 if (set == NULL_TREE)
11273 return constructor;
11275 return build2(COMPOUND_EXPR, type_tree, set, constructor);
11278 // Make a slice composite literal. This is used by the type
11279 // descriptor code.
11282 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
11283 source_location location)
11285 gcc_assert(type->is_open_array_type());
11286 return new Open_array_construction_expression(type, vals, location);
11289 // Construct a map.
11291 class Map_construction_expression : public Expression
11294 Map_construction_expression(Type* type, Expression_list* vals,
11295 source_location location)
11296 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
11297 type_(type), vals_(vals)
11298 { gcc_assert(vals == NULL || vals->size() % 2 == 0); }
11302 do_traverse(Traverse* traverse);
11306 { return this->type_; }
11309 do_determine_type(const Type_context*);
11312 do_check_types(Gogo*);
11317 return new Map_construction_expression(this->type_, this->vals_->copy(),
11322 do_get_tree(Translate_context*);
11325 do_export(Export*) const;
11328 // The type of the map to construct.
11330 // The list of values.
11331 Expression_list* vals_;
11337 Map_construction_expression::do_traverse(Traverse* traverse)
11339 if (this->vals_ != NULL
11340 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11341 return TRAVERSE_EXIT;
11342 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11343 return TRAVERSE_EXIT;
11344 return TRAVERSE_CONTINUE;
11347 // Final type determination.
11350 Map_construction_expression::do_determine_type(const Type_context*)
11352 if (this->vals_ == NULL)
11355 Map_type* mt = this->type_->map_type();
11356 Type_context key_context(mt->key_type(), false);
11357 Type_context val_context(mt->val_type(), false);
11358 for (Expression_list::const_iterator pv = this->vals_->begin();
11359 pv != this->vals_->end();
11362 (*pv)->determine_type(&key_context);
11364 (*pv)->determine_type(&val_context);
11371 Map_construction_expression::do_check_types(Gogo*)
11373 if (this->vals_ == NULL)
11376 Map_type* mt = this->type_->map_type();
11378 Type* key_type = mt->key_type();
11379 Type* val_type = mt->val_type();
11380 for (Expression_list::const_iterator pv = this->vals_->begin();
11381 pv != this->vals_->end();
11384 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
11386 error_at((*pv)->location(),
11387 "incompatible type for element %d key in map construction",
11389 this->set_is_error();
11392 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
11394 error_at((*pv)->location(),
11395 ("incompatible type for element %d value "
11396 "in map construction"),
11398 this->set_is_error();
11403 // Return a tree for constructing a map.
11406 Map_construction_expression::do_get_tree(Translate_context* context)
11408 Gogo* gogo = context->gogo();
11409 source_location loc = this->location();
11411 Map_type* mt = this->type_->map_type();
11413 // Build a struct to hold the key and value.
11414 tree struct_type = make_node(RECORD_TYPE);
11416 Type* key_type = mt->key_type();
11417 tree id = get_identifier("__key");
11418 tree key_type_tree = key_type->get_tree(gogo);
11419 if (key_type_tree == error_mark_node)
11420 return error_mark_node;
11421 tree key_field = build_decl(loc, FIELD_DECL, id, key_type_tree);
11422 DECL_CONTEXT(key_field) = struct_type;
11423 TYPE_FIELDS(struct_type) = key_field;
11425 Type* val_type = mt->val_type();
11426 id = get_identifier("__val");
11427 tree val_type_tree = val_type->get_tree(gogo);
11428 if (val_type_tree == error_mark_node)
11429 return error_mark_node;
11430 tree val_field = build_decl(loc, FIELD_DECL, id, val_type_tree);
11431 DECL_CONTEXT(val_field) = struct_type;
11432 DECL_CHAIN(key_field) = val_field;
11434 layout_type(struct_type);
11436 bool is_constant = true;
11441 if (this->vals_ == NULL || this->vals_->empty())
11443 valaddr = null_pointer_node;
11444 make_tmp = NULL_TREE;
11448 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11449 this->vals_->size() / 2);
11451 for (Expression_list::const_iterator pv = this->vals_->begin();
11452 pv != this->vals_->end();
11455 bool one_is_constant = true;
11457 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
11459 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
11460 elt->index = key_field;
11461 tree val_tree = (*pv)->get_tree(context);
11462 elt->value = Expression::convert_for_assignment(context, key_type,
11465 if (elt->value == error_mark_node)
11466 return error_mark_node;
11467 if (!TREE_CONSTANT(elt->value))
11468 one_is_constant = false;
11472 elt = VEC_quick_push(constructor_elt, one, NULL);
11473 elt->index = val_field;
11474 val_tree = (*pv)->get_tree(context);
11475 elt->value = Expression::convert_for_assignment(context, val_type,
11478 if (elt->value == error_mark_node)
11479 return error_mark_node;
11480 if (!TREE_CONSTANT(elt->value))
11481 one_is_constant = false;
11483 elt = VEC_quick_push(constructor_elt, values, NULL);
11484 elt->index = size_int(i);
11485 elt->value = build_constructor(struct_type, one);
11486 if (one_is_constant)
11487 TREE_CONSTANT(elt->value) = 1;
11489 is_constant = false;
11492 tree index_type = build_index_type(size_int(i - 1));
11493 tree array_type = build_array_type(struct_type, index_type);
11494 tree init = build_constructor(array_type, values);
11496 TREE_CONSTANT(init) = 1;
11498 if (current_function_decl != NULL)
11500 tmp = create_tmp_var(array_type, get_name(array_type));
11501 DECL_INITIAL(tmp) = init;
11502 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
11503 TREE_ADDRESSABLE(tmp) = 1;
11507 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
11508 DECL_EXTERNAL(tmp) = 0;
11509 TREE_PUBLIC(tmp) = 0;
11510 TREE_STATIC(tmp) = 1;
11511 DECL_ARTIFICIAL(tmp) = 1;
11512 if (!TREE_CONSTANT(init))
11513 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
11517 TREE_READONLY(tmp) = 1;
11518 TREE_CONSTANT(tmp) = 1;
11519 DECL_INITIAL(tmp) = init;
11520 make_tmp = NULL_TREE;
11522 rest_of_decl_compilation(tmp, 1, 0);
11525 valaddr = build_fold_addr_expr(tmp);
11528 tree descriptor = gogo->map_descriptor(mt);
11530 tree type_tree = this->type_->get_tree(gogo);
11531 if (type_tree == error_mark_node)
11532 return error_mark_node;
11534 static tree construct_map_fndecl;
11535 tree call = Gogo::call_builtin(&construct_map_fndecl,
11537 "__go_construct_map",
11540 TREE_TYPE(descriptor),
11545 TYPE_SIZE_UNIT(struct_type),
11547 byte_position(val_field),
11549 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
11550 const_ptr_type_node,
11551 fold_convert(const_ptr_type_node, valaddr));
11552 if (call == error_mark_node)
11553 return error_mark_node;
11556 if (make_tmp == NULL)
11559 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
11563 // Export an array construction.
11566 Map_construction_expression::do_export(Export* exp) const
11568 exp->write_c_string("convert(");
11569 exp->write_type(this->type_);
11570 for (Expression_list::const_iterator pv = this->vals_->begin();
11571 pv != this->vals_->end();
11574 exp->write_c_string(", ");
11575 (*pv)->export_expression(exp);
11577 exp->write_c_string(")");
11580 // A general composite literal. This is lowered to a type specific
11583 class Composite_literal_expression : public Parser_expression
11586 Composite_literal_expression(Type* type, int depth, bool has_keys,
11587 Expression_list* vals, source_location location)
11588 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
11589 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
11594 do_traverse(Traverse* traverse);
11597 do_lower(Gogo*, Named_object*, int);
11602 return new Composite_literal_expression(this->type_, this->depth_,
11604 (this->vals_ == NULL
11606 : this->vals_->copy()),
11612 lower_struct(Gogo*, Type*);
11615 lower_array(Type*);
11618 make_array(Type*, Expression_list*);
11621 lower_map(Gogo*, Named_object*, Type*);
11623 // The type of the composite literal.
11625 // The depth within a list of composite literals within a composite
11626 // literal, when the type is omitted.
11628 // The values to put in the composite literal.
11629 Expression_list* vals_;
11630 // If this is true, then VALS_ is a list of pairs: a key and a
11631 // value. In an array initializer, a missing key will be NULL.
11638 Composite_literal_expression::do_traverse(Traverse* traverse)
11640 if (this->vals_ != NULL
11641 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11642 return TRAVERSE_EXIT;
11643 return Type::traverse(this->type_, traverse);
11646 // Lower a generic composite literal into a specific version based on
11650 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int)
11652 Type* type = this->type_;
11654 for (int depth = this->depth_; depth > 0; --depth)
11656 if (type->array_type() != NULL)
11657 type = type->array_type()->element_type();
11658 else if (type->map_type() != NULL)
11659 type = type->map_type()->val_type();
11662 if (!type->is_error_type())
11663 error_at(this->location(),
11664 ("may only omit types within composite literals "
11665 "of slice, array, or map type"));
11666 return Expression::make_error(this->location());
11670 if (type->is_error_type())
11671 return Expression::make_error(this->location());
11672 else if (type->struct_type() != NULL)
11673 return this->lower_struct(gogo, type);
11674 else if (type->array_type() != NULL)
11675 return this->lower_array(type);
11676 else if (type->map_type() != NULL)
11677 return this->lower_map(gogo, function, type);
11680 error_at(this->location(),
11681 ("expected struct, slice, array, or map type "
11682 "for composite literal"));
11683 return Expression::make_error(this->location());
11687 // Lower a struct composite literal.
11690 Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
11692 source_location location = this->location();
11693 Struct_type* st = type->struct_type();
11694 if (this->vals_ == NULL || !this->has_keys_)
11695 return new Struct_construction_expression(type, this->vals_, location);
11697 size_t field_count = st->field_count();
11698 std::vector<Expression*> vals(field_count);
11699 Expression_list::const_iterator p = this->vals_->begin();
11700 while (p != this->vals_->end())
11702 Expression* name_expr = *p;
11705 gcc_assert(p != this->vals_->end());
11706 Expression* val = *p;
11710 if (name_expr == NULL)
11712 error_at(val->location(), "mixture of field and value initializers");
11713 return Expression::make_error(location);
11716 bool bad_key = false;
11718 const Named_object* no = NULL;
11719 switch (name_expr->classification())
11721 case EXPRESSION_UNKNOWN_REFERENCE:
11722 name = name_expr->unknown_expression()->name();
11725 case EXPRESSION_CONST_REFERENCE:
11726 no = static_cast<Const_expression*>(name_expr)->named_object();
11729 case EXPRESSION_TYPE:
11731 Type* t = name_expr->type();
11732 Named_type* nt = t->named_type();
11736 no = nt->named_object();
11740 case EXPRESSION_VAR_REFERENCE:
11741 no = name_expr->var_expression()->named_object();
11744 case EXPRESSION_FUNC_REFERENCE:
11745 no = name_expr->func_expression()->named_object();
11748 case EXPRESSION_UNARY:
11749 // If there is a local variable around with the same name as
11750 // the field, and this occurs in the closure, then the
11751 // parser may turn the field reference into an indirection
11752 // through the closure. FIXME: This is a mess.
11755 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
11756 if (ue->op() == OPERATOR_MULT)
11758 Field_reference_expression* fre =
11759 ue->operand()->field_reference_expression();
11763 fre->expr()->type()->deref()->struct_type();
11766 const Struct_field* sf = st->field(fre->field_index());
11767 name = sf->field_name();
11769 snprintf(buf, sizeof buf, "%u", fre->field_index());
11770 size_t buflen = strlen(buf);
11771 if (name.compare(name.length() - buflen, buflen, buf)
11774 name = name.substr(0, name.length() - buflen);
11789 error_at(name_expr->location(), "expected struct field name");
11790 return Expression::make_error(location);
11797 // A predefined name won't be packed. If it starts with a
11798 // lower case letter we need to check for that case, because
11799 // the field name will be packed.
11800 if (!Gogo::is_hidden_name(name)
11804 Named_object* gno = gogo->lookup_global(name.c_str());
11806 name = gogo->pack_hidden_name(name, false);
11810 unsigned int index;
11811 const Struct_field* sf = st->find_local_field(name, &index);
11814 error_at(name_expr->location(), "unknown field %qs in %qs",
11815 Gogo::message_name(name).c_str(),
11816 (type->named_type() != NULL
11817 ? type->named_type()->message_name().c_str()
11818 : "unnamed struct"));
11819 return Expression::make_error(location);
11821 if (vals[index] != NULL)
11823 error_at(name_expr->location(),
11824 "duplicate value for field %qs in %qs",
11825 Gogo::message_name(name).c_str(),
11826 (type->named_type() != NULL
11827 ? type->named_type()->message_name().c_str()
11828 : "unnamed struct"));
11829 return Expression::make_error(location);
11835 Expression_list* list = new Expression_list;
11836 list->reserve(field_count);
11837 for (size_t i = 0; i < field_count; ++i)
11838 list->push_back(vals[i]);
11840 return new Struct_construction_expression(type, list, location);
11843 // Lower an array composite literal.
11846 Composite_literal_expression::lower_array(Type* type)
11848 source_location location = this->location();
11849 if (this->vals_ == NULL || !this->has_keys_)
11850 return this->make_array(type, this->vals_);
11852 std::vector<Expression*> vals;
11853 vals.reserve(this->vals_->size());
11854 unsigned long index = 0;
11855 Expression_list::const_iterator p = this->vals_->begin();
11856 while (p != this->vals_->end())
11858 Expression* index_expr = *p;
11861 gcc_assert(p != this->vals_->end());
11862 Expression* val = *p;
11866 if (index_expr != NULL)
11872 if (!index_expr->integer_constant_value(true, ival, &dummy))
11875 error_at(index_expr->location(),
11876 "index expression is not integer constant");
11877 return Expression::make_error(location);
11880 if (mpz_sgn(ival) < 0)
11883 error_at(index_expr->location(), "index expression is negative");
11884 return Expression::make_error(location);
11887 index = mpz_get_ui(ival);
11888 if (mpz_cmp_ui(ival, index) != 0)
11891 error_at(index_expr->location(), "index value overflow");
11892 return Expression::make_error(location);
11895 Named_type* ntype = Type::lookup_integer_type("int");
11896 Integer_type* inttype = ntype->integer_type();
11898 mpz_init_set_ui(max, 1);
11899 mpz_mul_2exp(max, max, inttype->bits() - 1);
11900 bool ok = mpz_cmp(ival, max) < 0;
11905 error_at(index_expr->location(), "index value overflow");
11906 return Expression::make_error(location);
11911 // FIXME: Our representation isn't very good; this avoids
11913 if (index > 0x1000000)
11915 error_at(index_expr->location(), "index too large for compiler");
11916 return Expression::make_error(location);
11920 if (index == vals.size())
11921 vals.push_back(val);
11924 if (index > vals.size())
11926 vals.reserve(index + 32);
11927 vals.resize(index + 1, static_cast<Expression*>(NULL));
11929 if (vals[index] != NULL)
11931 error_at((index_expr != NULL
11932 ? index_expr->location()
11933 : val->location()),
11934 "duplicate value for index %lu",
11936 return Expression::make_error(location);
11944 size_t size = vals.size();
11945 Expression_list* list = new Expression_list;
11946 list->reserve(size);
11947 for (size_t i = 0; i < size; ++i)
11948 list->push_back(vals[i]);
11950 return this->make_array(type, list);
11953 // Actually build the array composite literal. This handles
11957 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
11959 source_location location = this->location();
11960 Array_type* at = type->array_type();
11961 if (at->length() != NULL && at->length()->is_nil_expression())
11963 size_t size = vals == NULL ? 0 : vals->size();
11965 mpz_init_set_ui(vlen, size);
11966 Expression* elen = Expression::make_integer(&vlen, NULL, location);
11968 at = Type::make_array_type(at->element_type(), elen);
11971 if (at->length() != NULL)
11972 return new Fixed_array_construction_expression(type, vals, location);
11974 return new Open_array_construction_expression(type, vals, location);
11977 // Lower a map composite literal.
11980 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
11983 source_location location = this->location();
11984 if (this->vals_ != NULL)
11986 if (!this->has_keys_)
11988 error_at(location, "map composite literal must have keys");
11989 return Expression::make_error(location);
11992 for (Expression_list::iterator p = this->vals_->begin();
11993 p != this->vals_->end();
11999 error_at((*p)->location(),
12000 "map composite literal must have keys for every value");
12001 return Expression::make_error(location);
12003 // Make sure we have lowered the key; it may not have been
12004 // lowered in order to handle keys for struct composite
12005 // literals. Lower it now to get the right error message.
12006 if ((*p)->unknown_expression() != NULL)
12008 (*p)->unknown_expression()->clear_is_composite_literal_key();
12009 gogo->lower_expression(function, &*p);
12010 gcc_assert((*p)->is_error_expression());
12011 return Expression::make_error(location);
12016 return new Map_construction_expression(type, this->vals_, location);
12019 // Make a composite literal expression.
12022 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
12023 Expression_list* vals,
12024 source_location location)
12026 return new Composite_literal_expression(type, depth, has_keys, vals,
12030 // Return whether this expression is a composite literal.
12033 Expression::is_composite_literal() const
12035 switch (this->classification_)
12037 case EXPRESSION_COMPOSITE_LITERAL:
12038 case EXPRESSION_STRUCT_CONSTRUCTION:
12039 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12040 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12041 case EXPRESSION_MAP_CONSTRUCTION:
12048 // Return whether this expression is a composite literal which is not
12052 Expression::is_nonconstant_composite_literal() const
12054 switch (this->classification_)
12056 case EXPRESSION_STRUCT_CONSTRUCTION:
12058 const Struct_construction_expression *psce =
12059 static_cast<const Struct_construction_expression*>(this);
12060 return !psce->is_constant_struct();
12062 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12064 const Fixed_array_construction_expression *pace =
12065 static_cast<const Fixed_array_construction_expression*>(this);
12066 return !pace->is_constant_array();
12068 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12070 const Open_array_construction_expression *pace =
12071 static_cast<const Open_array_construction_expression*>(this);
12072 return !pace->is_constant_array();
12074 case EXPRESSION_MAP_CONSTRUCTION:
12081 // Return true if this is a reference to a local variable.
12084 Expression::is_local_variable() const
12086 const Var_expression* ve = this->var_expression();
12089 const Named_object* no = ve->named_object();
12090 return (no->is_result_variable()
12091 || (no->is_variable() && !no->var_value()->is_global()));
12094 // Class Type_guard_expression.
12099 Type_guard_expression::do_traverse(Traverse* traverse)
12101 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
12102 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12103 return TRAVERSE_EXIT;
12104 return TRAVERSE_CONTINUE;
12107 // Check types of a type guard expression. The expression must have
12108 // an interface type, but the actual type conversion is checked at run
12112 Type_guard_expression::do_check_types(Gogo*)
12114 // 6g permits using a type guard with unsafe.pointer; we are
12116 Type* expr_type = this->expr_->type();
12117 if (expr_type->is_unsafe_pointer_type())
12119 if (this->type_->points_to() == NULL
12120 && (this->type_->integer_type() == NULL
12121 || (this->type_->forwarded()
12122 != Type::lookup_integer_type("uintptr"))))
12123 this->report_error(_("invalid unsafe.Pointer conversion"));
12125 else if (this->type_->is_unsafe_pointer_type())
12127 if (expr_type->points_to() == NULL
12128 && (expr_type->integer_type() == NULL
12129 || (expr_type->forwarded()
12130 != Type::lookup_integer_type("uintptr"))))
12131 this->report_error(_("invalid unsafe.Pointer conversion"));
12133 else if (expr_type->interface_type() == NULL)
12135 if (!expr_type->is_error_type() && !this->type_->is_error_type())
12136 this->report_error(_("type assertion only valid for interface types"));
12137 this->set_is_error();
12139 else if (this->type_->interface_type() == NULL)
12141 std::string reason;
12142 if (!expr_type->interface_type()->implements_interface(this->type_,
12145 if (!this->type_->is_error_type())
12147 if (reason.empty())
12148 this->report_error(_("impossible type assertion: "
12149 "type does not implement interface"));
12151 error_at(this->location(),
12152 ("impossible type assertion: "
12153 "type does not implement interface (%s)"),
12156 this->set_is_error();
12161 // Return a tree for a type guard expression.
12164 Type_guard_expression::do_get_tree(Translate_context* context)
12166 Gogo* gogo = context->gogo();
12167 tree expr_tree = this->expr_->get_tree(context);
12168 if (expr_tree == error_mark_node)
12169 return error_mark_node;
12170 Type* expr_type = this->expr_->type();
12171 if ((this->type_->is_unsafe_pointer_type()
12172 && (expr_type->points_to() != NULL
12173 || expr_type->integer_type() != NULL))
12174 || (expr_type->is_unsafe_pointer_type()
12175 && this->type_->points_to() != NULL))
12176 return convert_to_pointer(this->type_->get_tree(gogo), expr_tree);
12177 else if (expr_type->is_unsafe_pointer_type()
12178 && this->type_->integer_type() != NULL)
12179 return convert_to_integer(this->type_->get_tree(gogo), expr_tree);
12180 else if (this->type_->interface_type() != NULL)
12181 return Expression::convert_interface_to_interface(context, this->type_,
12182 this->expr_->type(),
12186 return Expression::convert_for_assignment(context, this->type_,
12187 this->expr_->type(), expr_tree,
12191 // Make a type guard expression.
12194 Expression::make_type_guard(Expression* expr, Type* type,
12195 source_location location)
12197 return new Type_guard_expression(expr, type, location);
12200 // Class Heap_composite_expression.
12202 // When you take the address of a composite literal, it is allocated
12203 // on the heap. This class implements that.
12205 class Heap_composite_expression : public Expression
12208 Heap_composite_expression(Expression* expr, source_location location)
12209 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
12215 do_traverse(Traverse* traverse)
12216 { return Expression::traverse(&this->expr_, traverse); }
12220 { return Type::make_pointer_type(this->expr_->type()); }
12223 do_determine_type(const Type_context*)
12224 { this->expr_->determine_type_no_context(); }
12229 return Expression::make_heap_composite(this->expr_->copy(),
12234 do_get_tree(Translate_context*);
12236 // We only export global objects, and the parser does not generate
12237 // this in global scope.
12239 do_export(Export*) const
12240 { gcc_unreachable(); }
12243 // The composite literal which is being put on the heap.
12247 // Return a tree which allocates a composite literal on the heap.
12250 Heap_composite_expression::do_get_tree(Translate_context* context)
12252 tree expr_tree = this->expr_->get_tree(context);
12253 if (expr_tree == error_mark_node)
12254 return error_mark_node;
12255 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
12256 gcc_assert(TREE_CODE(expr_size) == INTEGER_CST);
12257 tree space = context->gogo()->allocate_memory(this->expr_->type(),
12258 expr_size, this->location());
12259 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
12260 space = save_expr(space);
12261 tree ref = build_fold_indirect_ref_loc(this->location(), space);
12262 TREE_THIS_NOTRAP(ref) = 1;
12263 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
12264 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
12266 SET_EXPR_LOCATION(ret, this->location());
12270 // Allocate a composite literal on the heap.
12273 Expression::make_heap_composite(Expression* expr, source_location location)
12275 return new Heap_composite_expression(expr, location);
12278 // Class Receive_expression.
12280 // Return the type of a receive expression.
12283 Receive_expression::do_type()
12285 Channel_type* channel_type = this->channel_->type()->channel_type();
12286 if (channel_type == NULL)
12287 return Type::make_error_type();
12288 return channel_type->element_type();
12291 // Check types for a receive expression.
12294 Receive_expression::do_check_types(Gogo*)
12296 Type* type = this->channel_->type();
12297 if (type->is_error_type())
12299 this->set_is_error();
12302 if (type->channel_type() == NULL)
12304 this->report_error(_("expected channel"));
12307 if (!type->channel_type()->may_receive())
12309 this->report_error(_("invalid receive on send-only channel"));
12314 // Get a tree for a receive expression.
12317 Receive_expression::do_get_tree(Translate_context* context)
12319 Channel_type* channel_type = this->channel_->type()->channel_type();
12320 if (channel_type == NULL)
12322 gcc_assert(this->channel_->type()->is_error_type());
12323 return error_mark_node;
12325 Type* element_type = channel_type->element_type();
12326 tree element_type_tree = element_type->get_tree(context->gogo());
12328 tree channel = this->channel_->get_tree(context);
12329 if (element_type_tree == error_mark_node || channel == error_mark_node)
12330 return error_mark_node;
12332 return Gogo::receive_from_channel(element_type_tree, channel,
12333 this->for_select_, this->location());
12336 // Make a receive expression.
12338 Receive_expression*
12339 Expression::make_receive(Expression* channel, source_location location)
12341 return new Receive_expression(channel, location);
12344 // An expression which evaluates to a pointer to the type descriptor
12347 class Type_descriptor_expression : public Expression
12350 Type_descriptor_expression(Type* type, source_location location)
12351 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
12358 { return Type::make_type_descriptor_ptr_type(); }
12361 do_determine_type(const Type_context*)
12369 do_get_tree(Translate_context* context)
12370 { return this->type_->type_descriptor_pointer(context->gogo()); }
12373 // The type for which this is the descriptor.
12377 // Make a type descriptor expression.
12380 Expression::make_type_descriptor(Type* type, source_location location)
12382 return new Type_descriptor_expression(type, location);
12385 // An expression which evaluates to some characteristic of a type.
12386 // This is only used to initialize fields of a type descriptor. Using
12387 // a new expression class is slightly inefficient but gives us a good
12388 // separation between the frontend and the middle-end with regard to
12389 // how types are laid out.
12391 class Type_info_expression : public Expression
12394 Type_info_expression(Type* type, Type_info type_info)
12395 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
12396 type_(type), type_info_(type_info)
12404 do_determine_type(const Type_context*)
12412 do_get_tree(Translate_context* context);
12415 // The type for which we are getting information.
12417 // What information we want.
12418 Type_info type_info_;
12421 // The type is chosen to match what the type descriptor struct
12425 Type_info_expression::do_type()
12427 switch (this->type_info_)
12429 case TYPE_INFO_SIZE:
12430 return Type::lookup_integer_type("uintptr");
12431 case TYPE_INFO_ALIGNMENT:
12432 case TYPE_INFO_FIELD_ALIGNMENT:
12433 return Type::lookup_integer_type("uint8");
12439 // Return type information in GENERIC.
12442 Type_info_expression::do_get_tree(Translate_context* context)
12444 tree type_tree = this->type_->get_tree(context->gogo());
12445 if (type_tree == error_mark_node)
12446 return error_mark_node;
12448 tree val_type_tree = this->type()->get_tree(context->gogo());
12449 gcc_assert(val_type_tree != error_mark_node);
12451 if (this->type_info_ == TYPE_INFO_SIZE)
12452 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12453 TYPE_SIZE_UNIT(type_tree));
12457 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
12458 val = go_type_alignment(type_tree);
12460 val = go_field_alignment(type_tree);
12461 return build_int_cstu(val_type_tree, val);
12465 // Make a type info expression.
12468 Expression::make_type_info(Type* type, Type_info type_info)
12470 return new Type_info_expression(type, type_info);
12473 // An expression which evaluates to the offset of a field within a
12474 // struct. This, like Type_info_expression, q.v., is only used to
12475 // initialize fields of a type descriptor.
12477 class Struct_field_offset_expression : public Expression
12480 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
12481 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
12482 type_(type), field_(field)
12488 { return Type::lookup_integer_type("uintptr"); }
12491 do_determine_type(const Type_context*)
12499 do_get_tree(Translate_context* context);
12502 // The type of the struct.
12503 Struct_type* type_;
12505 const Struct_field* field_;
12508 // Return a struct field offset in GENERIC.
12511 Struct_field_offset_expression::do_get_tree(Translate_context* context)
12513 tree type_tree = this->type_->get_tree(context->gogo());
12514 if (type_tree == error_mark_node)
12515 return error_mark_node;
12517 tree val_type_tree = this->type()->get_tree(context->gogo());
12518 gcc_assert(val_type_tree != error_mark_node);
12520 const Struct_field_list* fields = this->type_->fields();
12521 tree struct_field_tree = TYPE_FIELDS(type_tree);
12522 Struct_field_list::const_iterator p;
12523 for (p = fields->begin();
12524 p != fields->end();
12525 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
12527 gcc_assert(struct_field_tree != NULL_TREE);
12528 if (&*p == this->field_)
12531 gcc_assert(&*p == this->field_);
12533 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12534 byte_position(struct_field_tree));
12537 // Make an expression for a struct field offset.
12540 Expression::make_struct_field_offset(Struct_type* type,
12541 const Struct_field* field)
12543 return new Struct_field_offset_expression(type, field);
12546 // An expression which evaluates to the address of an unnamed label.
12548 class Label_addr_expression : public Expression
12551 Label_addr_expression(Label* label, source_location location)
12552 : Expression(EXPRESSION_LABEL_ADDR, location),
12559 { return Type::make_pointer_type(Type::make_void_type()); }
12562 do_determine_type(const Type_context*)
12567 { return new Label_addr_expression(this->label_, this->location()); }
12570 do_get_tree(Translate_context*)
12571 { return this->label_->get_addr(this->location()); }
12574 // The label whose address we are taking.
12578 // Make an expression for the address of an unnamed label.
12581 Expression::make_label_addr(Label* label, source_location location)
12583 return new Label_addr_expression(label, location);
12586 // Import an expression. This comes at the end in order to see the
12587 // various class definitions.
12590 Expression::import_expression(Import* imp)
12592 int c = imp->peek_char();
12593 if (imp->match_c_string("- ")
12594 || imp->match_c_string("! ")
12595 || imp->match_c_string("^ "))
12596 return Unary_expression::do_import(imp);
12598 return Binary_expression::do_import(imp);
12599 else if (imp->match_c_string("true")
12600 || imp->match_c_string("false"))
12601 return Boolean_expression::do_import(imp);
12603 return String_expression::do_import(imp);
12604 else if (c == '-' || (c >= '0' && c <= '9'))
12606 // This handles integers, floats and complex constants.
12607 return Integer_expression::do_import(imp);
12609 else if (imp->match_c_string("nil"))
12610 return Nil_expression::do_import(imp);
12611 else if (imp->match_c_string("convert"))
12612 return Type_conversion_expression::do_import(imp);
12615 error_at(imp->location(), "import error: expected expression");
12616 return Expression::make_error(imp->location());
12620 // Class Expression_list.
12622 // Traverse the list.
12625 Expression_list::traverse(Traverse* traverse)
12627 for (Expression_list::iterator p = this->begin();
12633 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12634 return TRAVERSE_EXIT;
12637 return TRAVERSE_CONTINUE;
12643 Expression_list::copy()
12645 Expression_list* ret = new Expression_list();
12646 for (Expression_list::iterator p = this->begin();
12651 ret->push_back(NULL);
12653 ret->push_back((*p)->copy());
12658 // Return whether an expression list has an error expression.
12661 Expression_list::contains_error() const
12663 for (Expression_list::const_iterator p = this->begin();
12666 if (*p != NULL && (*p)->is_error_expression())