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 name of the variable.
942 Var_expression::name() const
944 return this->variable_->name();
947 // Return the type of a reference to a variable.
950 Var_expression::do_type()
952 if (this->variable_->is_variable())
953 return this->variable_->var_value()->type();
954 else if (this->variable_->is_result_variable())
955 return this->variable_->result_var_value()->type();
960 // Determine the type of a reference to a variable.
963 Var_expression::do_determine_type(const Type_context*)
965 if (this->variable_->is_variable())
966 this->variable_->var_value()->determine_type();
969 // Something takes the address of this variable. This means that we
970 // may want to move the variable onto the heap.
973 Var_expression::do_address_taken(bool escapes)
977 else if (this->variable_->is_variable())
978 this->variable_->var_value()->set_address_taken();
979 else if (this->variable_->is_result_variable())
980 this->variable_->result_var_value()->set_address_taken();
985 // Get the tree for a reference to a variable.
988 Var_expression::do_get_tree(Translate_context* context)
990 return this->variable_->get_tree(context->gogo(), context->function());
993 // Make a reference to a variable in an expression.
996 Expression::make_var_reference(Named_object* var, source_location location)
999 return Expression::make_sink(location);
1001 // FIXME: Creating a new object for each reference to a variable is
1003 return new Var_expression(var, location);
1006 // Class Temporary_reference_expression.
1011 Temporary_reference_expression::do_type()
1013 return this->statement_->type();
1016 // Called if something takes the address of this temporary variable.
1017 // We never have to move temporary variables to the heap, but we do
1018 // need to know that they must live in the stack rather than in a
1022 Temporary_reference_expression::do_address_taken(bool)
1024 this->statement_->set_is_address_taken();
1027 // Get a tree referring to the variable.
1030 Temporary_reference_expression::do_get_tree(Translate_context*)
1032 return this->statement_->get_decl();
1035 // Make a reference to a temporary variable.
1038 Expression::make_temporary_reference(Temporary_statement* statement,
1039 source_location location)
1041 return new Temporary_reference_expression(statement, location);
1044 // A sink expression--a use of the blank identifier _.
1046 class Sink_expression : public Expression
1049 Sink_expression(source_location location)
1050 : Expression(EXPRESSION_SINK, location),
1051 type_(NULL), var_(NULL_TREE)
1056 do_discarding_value()
1063 do_determine_type(const Type_context*);
1067 { return new Sink_expression(this->location()); }
1070 do_get_tree(Translate_context*);
1073 // The type of this sink variable.
1075 // The temporary variable we generate.
1079 // Return the type of a sink expression.
1082 Sink_expression::do_type()
1084 if (this->type_ == NULL)
1085 return Type::make_sink_type();
1089 // Determine the type of a sink expression.
1092 Sink_expression::do_determine_type(const Type_context* context)
1094 if (context->type != NULL)
1095 this->type_ = context->type;
1098 // Return a temporary variable for a sink expression. This will
1099 // presumably be a write-only variable which the middle-end will drop.
1102 Sink_expression::do_get_tree(Translate_context* context)
1104 if (this->var_ == NULL_TREE)
1106 gcc_assert(this->type_ != NULL && !this->type_->is_sink_type());
1107 this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()),
1113 // Make a sink expression.
1116 Expression::make_sink(source_location location)
1118 return new Sink_expression(location);
1121 // Class Func_expression.
1123 // FIXME: Can a function expression appear in a constant expression?
1124 // The value is unchanging. Initializing a constant to the address of
1125 // a function seems like it could work, though there might be little
1128 // Return the name of the function.
1131 Func_expression::name() const
1133 return this->function_->name();
1139 Func_expression::do_traverse(Traverse* traverse)
1141 return (this->closure_ == NULL
1143 : Expression::traverse(&this->closure_, traverse));
1146 // Return the type of a function expression.
1149 Func_expression::do_type()
1151 if (this->function_->is_function())
1152 return this->function_->func_value()->type();
1153 else if (this->function_->is_function_declaration())
1154 return this->function_->func_declaration_value()->type();
1159 // Get the tree for a function expression without evaluating the
1163 Func_expression::get_tree_without_closure(Gogo* gogo)
1165 Function_type* fntype;
1166 if (this->function_->is_function())
1167 fntype = this->function_->func_value()->type();
1168 else if (this->function_->is_function_declaration())
1169 fntype = this->function_->func_declaration_value()->type();
1173 // Builtin functions are handled specially by Call_expression. We
1174 // can't take their address.
1175 if (fntype->is_builtin())
1177 error_at(this->location(), "invalid use of special builtin function %qs",
1178 this->function_->name().c_str());
1179 return error_mark_node;
1182 Named_object* no = this->function_;
1184 tree id = no->get_id(gogo);
1185 if (id == error_mark_node)
1186 return error_mark_node;
1189 if (no->is_function())
1190 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1191 else if (no->is_function_declaration())
1192 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1196 if (fndecl == error_mark_node)
1197 return error_mark_node;
1199 return build_fold_addr_expr_loc(this->location(), fndecl);
1202 // Get the tree for a function expression. This is used when we take
1203 // the address of a function rather than simply calling it. If the
1204 // function has a closure, we must use a trampoline.
1207 Func_expression::do_get_tree(Translate_context* context)
1209 Gogo* gogo = context->gogo();
1211 tree fnaddr = this->get_tree_without_closure(gogo);
1212 if (fnaddr == error_mark_node)
1213 return error_mark_node;
1215 gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1216 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1217 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1219 // For a normal non-nested function call, that is all we have to do.
1220 if (!this->function_->is_function()
1221 || this->function_->func_value()->enclosing() == NULL)
1223 gcc_assert(this->closure_ == NULL);
1227 // For a nested function call, we have to always allocate a
1228 // trampoline. If we don't always allocate, then closures will not
1229 // be reliably distinct.
1230 Expression* closure = this->closure_;
1232 if (closure == NULL)
1233 closure_tree = null_pointer_node;
1236 // Get the value of the closure. This will be a pointer to
1237 // space allocated on the heap.
1238 closure_tree = closure->get_tree(context);
1239 if (closure_tree == error_mark_node)
1240 return error_mark_node;
1241 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1244 // Now we need to build some code on the heap. This code will load
1245 // the static chain pointer with the closure and then jump to the
1246 // body of the function. The normal gcc approach is to build the
1247 // code on the stack. Unfortunately we can not do that, as Go
1248 // permits us to return the function pointer.
1250 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1253 // Make a reference to a function in an expression.
1256 Expression::make_func_reference(Named_object* function, Expression* closure,
1257 source_location location)
1259 return new Func_expression(function, closure, location);
1262 // Class Unknown_expression.
1264 // Return the name of an unknown expression.
1267 Unknown_expression::name() const
1269 return this->named_object_->name();
1272 // Lower a reference to an unknown name.
1275 Unknown_expression::do_lower(Gogo*, Named_object*, int)
1277 source_location location = this->location();
1278 Named_object* no = this->named_object_;
1280 if (!no->is_unknown())
1284 real = no->unknown_value()->real_named_object();
1287 if (this->is_composite_literal_key_)
1289 error_at(location, "reference to undefined name %qs",
1290 this->named_object_->message_name().c_str());
1291 return Expression::make_error(location);
1294 switch (real->classification())
1296 case Named_object::NAMED_OBJECT_CONST:
1297 return Expression::make_const_reference(real, location);
1298 case Named_object::NAMED_OBJECT_TYPE:
1299 return Expression::make_type(real->type_value(), location);
1300 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1301 if (this->is_composite_literal_key_)
1303 error_at(location, "reference to undefined type %qs",
1304 real->message_name().c_str());
1305 return Expression::make_error(location);
1306 case Named_object::NAMED_OBJECT_VAR:
1307 return Expression::make_var_reference(real, location);
1308 case Named_object::NAMED_OBJECT_FUNC:
1309 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1310 return Expression::make_func_reference(real, NULL, location);
1311 case Named_object::NAMED_OBJECT_PACKAGE:
1312 if (this->is_composite_literal_key_)
1314 error_at(location, "unexpected reference to package");
1315 return Expression::make_error(location);
1321 // Make a reference to an unknown name.
1324 Expression::make_unknown_reference(Named_object* no, source_location location)
1326 gcc_assert(no->resolve()->is_unknown());
1327 return new Unknown_expression(no, location);
1330 // A boolean expression.
1332 class Boolean_expression : public Expression
1335 Boolean_expression(bool val, source_location location)
1336 : Expression(EXPRESSION_BOOLEAN, location),
1337 val_(val), type_(NULL)
1345 do_is_constant() const
1352 do_determine_type(const Type_context*);
1359 do_get_tree(Translate_context*)
1360 { return this->val_ ? boolean_true_node : boolean_false_node; }
1363 do_export(Export* exp) const
1364 { exp->write_c_string(this->val_ ? "true" : "false"); }
1369 // The type as determined by context.
1376 Boolean_expression::do_type()
1378 if (this->type_ == NULL)
1379 this->type_ = Type::make_boolean_type();
1383 // Set the type from the context.
1386 Boolean_expression::do_determine_type(const Type_context* context)
1388 if (this->type_ != NULL && !this->type_->is_abstract())
1390 else if (context->type != NULL && context->type->is_boolean_type())
1391 this->type_ = context->type;
1392 else if (!context->may_be_abstract)
1393 this->type_ = Type::lookup_bool_type();
1396 // Import a boolean constant.
1399 Boolean_expression::do_import(Import* imp)
1401 if (imp->peek_char() == 't')
1403 imp->require_c_string("true");
1404 return Expression::make_boolean(true, imp->location());
1408 imp->require_c_string("false");
1409 return Expression::make_boolean(false, imp->location());
1413 // Make a boolean expression.
1416 Expression::make_boolean(bool val, source_location location)
1418 return new Boolean_expression(val, location);
1421 // Class String_expression.
1426 String_expression::do_type()
1428 if (this->type_ == NULL)
1429 this->type_ = Type::make_string_type();
1433 // Set the type from the context.
1436 String_expression::do_determine_type(const Type_context* context)
1438 if (this->type_ != NULL && !this->type_->is_abstract())
1440 else if (context->type != NULL && context->type->is_string_type())
1441 this->type_ = context->type;
1442 else if (!context->may_be_abstract)
1443 this->type_ = Type::lookup_string_type();
1446 // Build a string constant.
1449 String_expression::do_get_tree(Translate_context* context)
1451 return context->gogo()->go_string_constant_tree(this->val_);
1454 // Export a string expression.
1457 String_expression::do_export(Export* exp) const
1460 s.reserve(this->val_.length() * 4 + 2);
1462 for (std::string::const_iterator p = this->val_.begin();
1463 p != this->val_.end();
1466 if (*p == '\\' || *p == '"')
1471 else if (*p >= 0x20 && *p < 0x7f)
1473 else if (*p == '\n')
1475 else if (*p == '\t')
1480 unsigned char c = *p;
1481 unsigned int dig = c >> 4;
1482 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1484 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1488 exp->write_string(s);
1491 // Import a string expression.
1494 String_expression::do_import(Import* imp)
1496 imp->require_c_string("\"");
1500 int c = imp->get_char();
1501 if (c == '"' || c == -1)
1504 val += static_cast<char>(c);
1507 c = imp->get_char();
1508 if (c == '\\' || c == '"')
1509 val += static_cast<char>(c);
1516 c = imp->get_char();
1517 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1518 c = imp->get_char();
1519 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1520 char v = (vh << 4) | vl;
1525 error_at(imp->location(), "bad string constant");
1526 return Expression::make_error(imp->location());
1530 return Expression::make_string(val, imp->location());
1533 // Make a string expression.
1536 Expression::make_string(const std::string& val, source_location location)
1538 return new String_expression(val, location);
1541 // Make an integer expression.
1543 class Integer_expression : public Expression
1546 Integer_expression(const mpz_t* val, Type* type, source_location location)
1547 : Expression(EXPRESSION_INTEGER, location),
1549 { mpz_init_set(this->val_, *val); }
1554 // Return whether VAL fits in the type.
1556 check_constant(mpz_t val, Type*, source_location);
1558 // Write VAL to export data.
1560 export_integer(Export* exp, const mpz_t val);
1564 do_is_constant() const
1568 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1574 do_determine_type(const Type_context* context);
1577 do_check_types(Gogo*);
1580 do_get_tree(Translate_context*);
1584 { return Expression::make_integer(&this->val_, this->type_,
1585 this->location()); }
1588 do_export(Export*) const;
1591 // The integer value.
1597 // Return an integer constant value.
1600 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1603 if (this->type_ != NULL)
1604 *ptype = this->type_;
1605 mpz_set(val, this->val_);
1609 // Return the current type. If we haven't set the type yet, we return
1610 // an abstract integer type.
1613 Integer_expression::do_type()
1615 if (this->type_ == NULL)
1616 this->type_ = Type::make_abstract_integer_type();
1620 // Set the type of the integer value. Here we may switch from an
1621 // abstract type to a real type.
1624 Integer_expression::do_determine_type(const Type_context* context)
1626 if (this->type_ != NULL && !this->type_->is_abstract())
1628 else if (context->type != NULL
1629 && (context->type->integer_type() != NULL
1630 || context->type->float_type() != NULL
1631 || context->type->complex_type() != NULL))
1632 this->type_ = context->type;
1633 else if (!context->may_be_abstract)
1634 this->type_ = Type::lookup_integer_type("int");
1637 // Return true if the integer VAL fits in the range of the type TYPE.
1638 // Otherwise give an error and return false. TYPE may be NULL.
1641 Integer_expression::check_constant(mpz_t val, Type* type,
1642 source_location location)
1646 Integer_type* itype = type->integer_type();
1647 if (itype == NULL || itype->is_abstract())
1650 int bits = mpz_sizeinbase(val, 2);
1652 if (itype->is_unsigned())
1654 // For an unsigned type we can only accept a nonnegative number,
1655 // and we must be able to represent at least BITS.
1656 if (mpz_sgn(val) >= 0
1657 && bits <= itype->bits())
1662 // For a signed type we need an extra bit to indicate the sign.
1663 // We have to handle the most negative integer specially.
1664 if (bits + 1 <= itype->bits()
1665 || (bits <= itype->bits()
1667 && (mpz_scan1(val, 0)
1668 == static_cast<unsigned long>(itype->bits() - 1))
1669 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1673 error_at(location, "integer constant overflow");
1677 // Check the type of an integer constant.
1680 Integer_expression::do_check_types(Gogo*)
1682 if (this->type_ == NULL)
1684 if (!Integer_expression::check_constant(this->val_, this->type_,
1686 this->set_is_error();
1689 // Get a tree for an integer constant.
1692 Integer_expression::do_get_tree(Translate_context* context)
1694 Gogo* gogo = context->gogo();
1696 if (this->type_ != NULL && !this->type_->is_abstract())
1697 type = this->type_->get_tree(gogo);
1698 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1700 // We are converting to an abstract floating point type.
1701 type = Type::lookup_float_type("float64")->get_tree(gogo);
1703 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1705 // We are converting to an abstract complex type.
1706 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
1710 // If we still have an abstract type here, then this is being
1711 // used in a constant expression which didn't get reduced for
1712 // some reason. Use a type which will fit the value. We use <,
1713 // not <=, because we need an extra bit for the sign bit.
1714 int bits = mpz_sizeinbase(this->val_, 2);
1715 if (bits < INT_TYPE_SIZE)
1716 type = Type::lookup_integer_type("int")->get_tree(gogo);
1718 type = Type::lookup_integer_type("int64")->get_tree(gogo);
1720 type = long_long_integer_type_node;
1722 return Expression::integer_constant_tree(this->val_, type);
1725 // Write VAL to export data.
1728 Integer_expression::export_integer(Export* exp, const mpz_t val)
1730 char* s = mpz_get_str(NULL, 10, val);
1731 exp->write_c_string(s);
1735 // Export an integer in a constant expression.
1738 Integer_expression::do_export(Export* exp) const
1740 Integer_expression::export_integer(exp, this->val_);
1741 // A trailing space lets us reliably identify the end of the number.
1742 exp->write_c_string(" ");
1745 // Import an integer, floating point, or complex value. This handles
1746 // all these types because they all start with digits.
1749 Integer_expression::do_import(Import* imp)
1751 std::string num = imp->read_identifier();
1752 imp->require_c_string(" ");
1753 if (!num.empty() && num[num.length() - 1] == 'i')
1756 size_t plus_pos = num.find('+', 1);
1757 size_t minus_pos = num.find('-', 1);
1759 if (plus_pos == std::string::npos)
1761 else if (minus_pos == std::string::npos)
1765 error_at(imp->location(), "bad number in import data: %qs",
1767 return Expression::make_error(imp->location());
1769 if (pos == std::string::npos)
1770 mpfr_set_ui(real, 0, GMP_RNDN);
1773 std::string real_str = num.substr(0, pos);
1774 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1776 error_at(imp->location(), "bad number in import data: %qs",
1778 return Expression::make_error(imp->location());
1782 std::string imag_str;
1783 if (pos == std::string::npos)
1786 imag_str = num.substr(pos);
1787 imag_str = imag_str.substr(0, imag_str.size() - 1);
1789 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1791 error_at(imp->location(), "bad number in import data: %qs",
1793 return Expression::make_error(imp->location());
1795 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1801 else if (num.find('.') == std::string::npos
1802 && num.find('E') == std::string::npos)
1805 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1807 error_at(imp->location(), "bad number in import data: %qs",
1809 return Expression::make_error(imp->location());
1811 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1818 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1820 error_at(imp->location(), "bad number in import data: %qs",
1822 return Expression::make_error(imp->location());
1824 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1830 // Build a new integer value.
1833 Expression::make_integer(const mpz_t* val, Type* type,
1834 source_location location)
1836 return new Integer_expression(val, type, location);
1841 class Float_expression : public Expression
1844 Float_expression(const mpfr_t* val, Type* type, source_location location)
1845 : Expression(EXPRESSION_FLOAT, location),
1848 mpfr_init_set(this->val_, *val, GMP_RNDN);
1851 // Constrain VAL to fit into TYPE.
1853 constrain_float(mpfr_t val, Type* type);
1855 // Return whether VAL fits in the type.
1857 check_constant(mpfr_t val, Type*, source_location);
1859 // Write VAL to export data.
1861 export_float(Export* exp, const mpfr_t val);
1865 do_is_constant() const
1869 do_float_constant_value(mpfr_t val, Type**) const;
1875 do_determine_type(const Type_context*);
1878 do_check_types(Gogo*);
1882 { return Expression::make_float(&this->val_, this->type_,
1883 this->location()); }
1886 do_get_tree(Translate_context*);
1889 do_export(Export*) const;
1892 // The floating point value.
1898 // Constrain VAL to fit into TYPE.
1901 Float_expression::constrain_float(mpfr_t val, Type* type)
1903 Float_type* ftype = type->float_type();
1904 if (ftype != NULL && !ftype->is_abstract())
1906 tree type_tree = ftype->type_tree();
1907 REAL_VALUE_TYPE rvt;
1908 real_from_mpfr(&rvt, val, type_tree, GMP_RNDN);
1909 real_convert(&rvt, TYPE_MODE(type_tree), &rvt);
1910 mpfr_from_real(val, &rvt, GMP_RNDN);
1914 // Return a floating point constant value.
1917 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
1919 if (this->type_ != NULL)
1920 *ptype = this->type_;
1921 mpfr_set(val, this->val_, GMP_RNDN);
1925 // Return the current type. If we haven't set the type yet, we return
1926 // an abstract float type.
1929 Float_expression::do_type()
1931 if (this->type_ == NULL)
1932 this->type_ = Type::make_abstract_float_type();
1936 // Set the type of the float value. Here we may switch from an
1937 // abstract type to a real type.
1940 Float_expression::do_determine_type(const Type_context* context)
1942 if (this->type_ != NULL && !this->type_->is_abstract())
1944 else if (context->type != NULL
1945 && (context->type->integer_type() != NULL
1946 || context->type->float_type() != NULL
1947 || context->type->complex_type() != NULL))
1948 this->type_ = context->type;
1949 else if (!context->may_be_abstract)
1950 this->type_ = Type::lookup_float_type("float64");
1953 // Return true if the floating point value VAL fits in the range of
1954 // the type TYPE. Otherwise give an error and return false. TYPE may
1958 Float_expression::check_constant(mpfr_t val, Type* type,
1959 source_location location)
1963 Float_type* ftype = type->float_type();
1964 if (ftype == NULL || ftype->is_abstract())
1967 // A NaN or Infinity always fits in the range of the type.
1968 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
1971 mp_exp_t exp = mpfr_get_exp(val);
1973 switch (ftype->bits())
1986 error_at(location, "floating point constant overflow");
1992 // Check the type of a float value.
1995 Float_expression::do_check_types(Gogo*)
1997 if (this->type_ == NULL)
2000 if (!Float_expression::check_constant(this->val_, this->type_,
2002 this->set_is_error();
2004 Integer_type* integer_type = this->type_->integer_type();
2005 if (integer_type != NULL)
2007 if (!mpfr_integer_p(this->val_))
2008 this->report_error(_("floating point constant truncated to integer"));
2011 gcc_assert(!integer_type->is_abstract());
2014 mpfr_get_z(ival, this->val_, GMP_RNDN);
2015 Integer_expression::check_constant(ival, integer_type,
2022 // Get a tree for a float constant.
2025 Float_expression::do_get_tree(Translate_context* context)
2027 Gogo* gogo = context->gogo();
2029 if (this->type_ != NULL && !this->type_->is_abstract())
2030 type = this->type_->get_tree(gogo);
2031 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2033 // We have an abstract integer type. We just hope for the best.
2034 type = Type::lookup_integer_type("int")->get_tree(gogo);
2038 // If we still have an abstract type here, then this is being
2039 // used in a constant expression which didn't get reduced. We
2040 // just use float64 and hope for the best.
2041 type = Type::lookup_float_type("float64")->get_tree(gogo);
2043 return Expression::float_constant_tree(this->val_, type);
2046 // Write a floating point number to export data.
2049 Float_expression::export_float(Export *exp, const mpfr_t val)
2052 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2054 exp->write_c_string("-");
2055 exp->write_c_string("0.");
2056 exp->write_c_string(*s == '-' ? s + 1 : s);
2059 snprintf(buf, sizeof buf, "E%ld", exponent);
2060 exp->write_c_string(buf);
2063 // Export a floating point number in a constant expression.
2066 Float_expression::do_export(Export* exp) const
2068 Float_expression::export_float(exp, this->val_);
2069 // A trailing space lets us reliably identify the end of the number.
2070 exp->write_c_string(" ");
2073 // Make a float expression.
2076 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2078 return new Float_expression(val, type, location);
2083 class Complex_expression : public Expression
2086 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2087 source_location location)
2088 : Expression(EXPRESSION_COMPLEX, location),
2091 mpfr_init_set(this->real_, *real, GMP_RNDN);
2092 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2095 // Constrain REAL/IMAG to fit into TYPE.
2097 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2099 // Return whether REAL/IMAG fits in the type.
2101 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2103 // Write REAL/IMAG to export data.
2105 export_complex(Export* exp, const mpfr_t real, const mpfr_t val);
2109 do_is_constant() const
2113 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2119 do_determine_type(const Type_context*);
2122 do_check_types(Gogo*);
2127 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2132 do_get_tree(Translate_context*);
2135 do_export(Export*) const;
2140 // The imaginary part;
2142 // The type if known.
2146 // Constrain REAL/IMAG to fit into TYPE.
2149 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2151 Complex_type* ctype = type->complex_type();
2152 if (ctype != NULL && !ctype->is_abstract())
2154 tree type_tree = ctype->type_tree();
2156 REAL_VALUE_TYPE rvt;
2157 real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN);
2158 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2159 mpfr_from_real(real, &rvt, GMP_RNDN);
2161 real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN);
2162 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2163 mpfr_from_real(imag, &rvt, GMP_RNDN);
2167 // Return a complex constant value.
2170 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2173 if (this->type_ != NULL)
2174 *ptype = this->type_;
2175 mpfr_set(real, this->real_, GMP_RNDN);
2176 mpfr_set(imag, this->imag_, GMP_RNDN);
2180 // Return the current type. If we haven't set the type yet, we return
2181 // an abstract complex type.
2184 Complex_expression::do_type()
2186 if (this->type_ == NULL)
2187 this->type_ = Type::make_abstract_complex_type();
2191 // Set the type of the complex value. Here we may switch from an
2192 // abstract type to a real type.
2195 Complex_expression::do_determine_type(const Type_context* context)
2197 if (this->type_ != NULL && !this->type_->is_abstract())
2199 else if (context->type != NULL
2200 && context->type->complex_type() != NULL)
2201 this->type_ = context->type;
2202 else if (!context->may_be_abstract)
2203 this->type_ = Type::lookup_complex_type("complex128");
2206 // Return true if the complex value REAL/IMAG fits in the range of the
2207 // type TYPE. Otherwise give an error and return false. TYPE may be
2211 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2212 source_location location)
2216 Complex_type* ctype = type->complex_type();
2217 if (ctype == NULL || ctype->is_abstract())
2221 switch (ctype->bits())
2233 // A NaN or Infinity always fits in the range of the type.
2234 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2236 if (mpfr_get_exp(real) > max_exp)
2238 error_at(location, "complex real part constant overflow");
2243 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2245 if (mpfr_get_exp(imag) > max_exp)
2247 error_at(location, "complex imaginary part constant overflow");
2255 // Check the type of a complex value.
2258 Complex_expression::do_check_types(Gogo*)
2260 if (this->type_ == NULL)
2263 if (!Complex_expression::check_constant(this->real_, this->imag_,
2264 this->type_, this->location()))
2265 this->set_is_error();
2268 // Get a tree for a complex constant.
2271 Complex_expression::do_get_tree(Translate_context* context)
2273 Gogo* gogo = context->gogo();
2275 if (this->type_ != NULL && !this->type_->is_abstract())
2276 type = this->type_->get_tree(gogo);
2279 // If we still have an abstract type here, this this is being
2280 // used in a constant expression which didn't get reduced. We
2281 // just use complex128 and hope for the best.
2282 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
2284 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2287 // Write REAL/IMAG to export data.
2290 Complex_expression::export_complex(Export* exp, const mpfr_t real,
2293 if (!mpfr_zero_p(real))
2295 Float_expression::export_float(exp, real);
2296 if (mpfr_sgn(imag) > 0)
2297 exp->write_c_string("+");
2299 Float_expression::export_float(exp, imag);
2300 exp->write_c_string("i");
2303 // Export a complex number in a constant expression.
2306 Complex_expression::do_export(Export* exp) const
2308 Complex_expression::export_complex(exp, this->real_, this->imag_);
2309 // A trailing space lets us reliably identify the end of the number.
2310 exp->write_c_string(" ");
2313 // Make a complex expression.
2316 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2317 source_location location)
2319 return new Complex_expression(real, imag, type, location);
2322 // Find a named object in an expression.
2324 class Find_named_object : public Traverse
2327 Find_named_object(Named_object* no)
2328 : Traverse(traverse_expressions),
2329 no_(no), found_(false)
2332 // Whether we found the object.
2335 { return this->found_; }
2339 expression(Expression**);
2342 // The object we are looking for.
2344 // Whether we found it.
2348 // A reference to a const in an expression.
2350 class Const_expression : public Expression
2353 Const_expression(Named_object* constant, source_location location)
2354 : Expression(EXPRESSION_CONST_REFERENCE, location),
2355 constant_(constant), type_(NULL), seen_(false)
2360 { return this->constant_; }
2364 { return this->constant_->name(); }
2366 // Check that the initializer does not refer to the constant itself.
2368 check_for_init_loop();
2372 do_traverse(Traverse*);
2375 do_lower(Gogo*, Named_object*, int);
2378 do_is_constant() const
2382 do_integer_constant_value(bool, mpz_t val, Type**) const;
2385 do_float_constant_value(mpfr_t val, Type**) const;
2388 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2391 do_string_constant_value(std::string* val) const
2392 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2397 // The type of a const is set by the declaration, not the use.
2399 do_determine_type(const Type_context*);
2402 do_check_types(Gogo*);
2409 do_get_tree(Translate_context* context);
2411 // When exporting a reference to a const as part of a const
2412 // expression, we export the value. We ignore the fact that it has
2415 do_export(Export* exp) const
2416 { this->constant_->const_value()->expr()->export_expression(exp); }
2420 Named_object* constant_;
2421 // The type of this reference. This is used if the constant has an
2424 // Used to prevent infinite recursion when a constant incorrectly
2425 // refers to itself.
2432 Const_expression::do_traverse(Traverse* traverse)
2434 if (this->type_ != NULL)
2435 return Type::traverse(this->type_, traverse);
2436 return TRAVERSE_CONTINUE;
2439 // Lower a constant expression. This is where we convert the
2440 // predeclared constant iota into an integer value.
2443 Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value)
2445 if (this->constant_->const_value()->expr()->classification()
2448 if (iota_value == -1)
2450 error_at(this->location(),
2451 "iota is only defined in const declarations");
2455 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2456 Expression* ret = Expression::make_integer(&val, NULL,
2462 // Make sure that the constant itself has been lowered.
2463 gogo->lower_constant(this->constant_);
2468 // Return an integer constant value.
2471 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2478 if (this->type_ != NULL)
2479 ctype = this->type_;
2481 ctype = this->constant_->const_value()->type();
2482 if (ctype != NULL && ctype->integer_type() == NULL)
2485 Expression* e = this->constant_->const_value()->expr();
2490 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2492 this->seen_ = false;
2496 && !Integer_expression::check_constant(val, ctype, this->location()))
2499 *ptype = ctype != NULL ? ctype : t;
2503 // Return a floating point constant value.
2506 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2512 if (this->type_ != NULL)
2513 ctype = this->type_;
2515 ctype = this->constant_->const_value()->type();
2516 if (ctype != NULL && ctype->float_type() == NULL)
2522 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2525 this->seen_ = false;
2527 if (r && ctype != NULL)
2529 if (!Float_expression::check_constant(val, ctype, this->location()))
2531 Float_expression::constrain_float(val, ctype);
2533 *ptype = ctype != NULL ? ctype : t;
2537 // Return a complex constant value.
2540 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2547 if (this->type_ != NULL)
2548 ctype = this->type_;
2550 ctype = this->constant_->const_value()->type();
2551 if (ctype != NULL && ctype->complex_type() == NULL)
2557 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2561 this->seen_ = false;
2563 if (r && ctype != NULL)
2565 if (!Complex_expression::check_constant(real, imag, ctype,
2568 Complex_expression::constrain_complex(real, imag, ctype);
2570 *ptype = ctype != NULL ? ctype : t;
2574 // Return the type of the const reference.
2577 Const_expression::do_type()
2579 if (this->type_ != NULL)
2582 Named_constant* nc = this->constant_->const_value();
2584 if (this->seen_ || nc->lowering())
2586 this->report_error(_("constant refers to itself"));
2587 this->type_ = Type::make_error_type();
2593 Type* ret = nc->type();
2597 this->seen_ = false;
2601 // During parsing, a named constant may have a NULL type, but we
2602 // must not return a NULL type here.
2603 ret = nc->expr()->type();
2605 this->seen_ = false;
2610 // Set the type of the const reference.
2613 Const_expression::do_determine_type(const Type_context* context)
2615 Type* ctype = this->constant_->const_value()->type();
2616 Type* cetype = (ctype != NULL
2618 : this->constant_->const_value()->expr()->type());
2619 if (ctype != NULL && !ctype->is_abstract())
2621 else if (context->type != NULL
2622 && (context->type->integer_type() != NULL
2623 || context->type->float_type() != NULL
2624 || context->type->complex_type() != NULL)
2625 && (cetype->integer_type() != NULL
2626 || cetype->float_type() != NULL
2627 || cetype->complex_type() != NULL))
2628 this->type_ = context->type;
2629 else if (context->type != NULL
2630 && context->type->is_string_type()
2631 && cetype->is_string_type())
2632 this->type_ = context->type;
2633 else if (context->type != NULL
2634 && context->type->is_boolean_type()
2635 && cetype->is_boolean_type())
2636 this->type_ = context->type;
2637 else if (!context->may_be_abstract)
2639 if (cetype->is_abstract())
2640 cetype = cetype->make_non_abstract_type();
2641 this->type_ = cetype;
2645 // Check for a loop in which the initializer of a constant refers to
2646 // the constant itself.
2649 Const_expression::check_for_init_loop()
2651 if (this->type_ != NULL && this->type_->is_error_type())
2656 this->report_error(_("constant refers to itself"));
2657 this->type_ = Type::make_error_type();
2661 Expression* init = this->constant_->const_value()->expr();
2662 Find_named_object find_named_object(this->constant_);
2665 Expression::traverse(&init, &find_named_object);
2666 this->seen_ = false;
2668 if (find_named_object.found())
2670 if (this->type_ == NULL || !this->type_->is_error_type())
2672 this->report_error(_("constant refers to itself"));
2673 this->type_ = Type::make_error_type();
2679 // Check types of a const reference.
2682 Const_expression::do_check_types(Gogo*)
2684 if (this->type_ != NULL && this->type_->is_error_type())
2687 this->check_for_init_loop();
2689 if (this->type_ == NULL || this->type_->is_abstract())
2692 // Check for integer overflow.
2693 if (this->type_->integer_type() != NULL)
2698 if (!this->integer_constant_value(true, ival, &dummy))
2702 Expression* cexpr = this->constant_->const_value()->expr();
2703 if (cexpr->float_constant_value(fval, &dummy))
2705 if (!mpfr_integer_p(fval))
2706 this->report_error(_("floating point constant "
2707 "truncated to integer"));
2710 mpfr_get_z(ival, fval, GMP_RNDN);
2711 Integer_expression::check_constant(ival, this->type_,
2721 // Return a tree for the const reference.
2724 Const_expression::do_get_tree(Translate_context* context)
2726 Gogo* gogo = context->gogo();
2728 if (this->type_ == NULL)
2729 type_tree = NULL_TREE;
2732 type_tree = this->type_->get_tree(gogo);
2733 if (type_tree == error_mark_node)
2734 return error_mark_node;
2737 // If the type has been set for this expression, but the underlying
2738 // object is an abstract int or float, we try to get the abstract
2739 // value. Otherwise we may lose something in the conversion.
2740 if (this->type_ != NULL
2741 && (this->constant_->const_value()->type() == NULL
2742 || this->constant_->const_value()->type()->is_abstract()))
2744 Expression* expr = this->constant_->const_value()->expr();
2748 if (expr->integer_constant_value(true, ival, &t))
2750 tree ret = Expression::integer_constant_tree(ival, type_tree);
2758 if (expr->float_constant_value(fval, &t))
2760 tree ret = Expression::float_constant_tree(fval, type_tree);
2767 if (expr->complex_constant_value(fval, imag, &t))
2769 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2778 tree const_tree = this->constant_->get_tree(gogo, context->function());
2779 if (this->type_ == NULL
2780 || const_tree == error_mark_node
2781 || TREE_TYPE(const_tree) == error_mark_node)
2785 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2786 ret = fold_convert(type_tree, const_tree);
2787 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2788 ret = fold(convert_to_integer(type_tree, const_tree));
2789 else if (TREE_CODE(type_tree) == REAL_TYPE)
2790 ret = fold(convert_to_real(type_tree, const_tree));
2791 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2792 ret = fold(convert_to_complex(type_tree, const_tree));
2798 // Make a reference to a constant in an expression.
2801 Expression::make_const_reference(Named_object* constant,
2802 source_location location)
2804 return new Const_expression(constant, location);
2807 // Find a named object in an expression.
2810 Find_named_object::expression(Expression** pexpr)
2812 switch ((*pexpr)->classification())
2814 case Expression::EXPRESSION_CONST_REFERENCE:
2816 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2817 if (ce->named_object() == this->no_)
2820 // We need to check a constant initializer explicitly, as
2821 // loops here will not be caught by the loop checking for
2822 // variable initializers.
2823 ce->check_for_init_loop();
2825 return TRAVERSE_CONTINUE;
2828 case Expression::EXPRESSION_VAR_REFERENCE:
2829 if ((*pexpr)->var_expression()->named_object() == this->no_)
2831 return TRAVERSE_CONTINUE;
2832 case Expression::EXPRESSION_FUNC_REFERENCE:
2833 if ((*pexpr)->func_expression()->named_object() == this->no_)
2835 return TRAVERSE_CONTINUE;
2837 return TRAVERSE_CONTINUE;
2839 this->found_ = true;
2840 return TRAVERSE_EXIT;
2845 class Nil_expression : public Expression
2848 Nil_expression(source_location location)
2849 : Expression(EXPRESSION_NIL, location)
2857 do_is_constant() const
2862 { return Type::make_nil_type(); }
2865 do_determine_type(const Type_context*)
2873 do_get_tree(Translate_context*)
2874 { return null_pointer_node; }
2877 do_export(Export* exp) const
2878 { exp->write_c_string("nil"); }
2881 // Import a nil expression.
2884 Nil_expression::do_import(Import* imp)
2886 imp->require_c_string("nil");
2887 return Expression::make_nil(imp->location());
2890 // Make a nil expression.
2893 Expression::make_nil(source_location location)
2895 return new Nil_expression(location);
2898 // The value of the predeclared constant iota. This is little more
2899 // than a marker. This will be lowered to an integer in
2900 // Const_expression::do_lower, which is where we know the value that
2903 class Iota_expression : public Parser_expression
2906 Iota_expression(source_location location)
2907 : Parser_expression(EXPRESSION_IOTA, location)
2912 do_lower(Gogo*, Named_object*, int)
2913 { gcc_unreachable(); }
2915 // There should only ever be one of these.
2918 { gcc_unreachable(); }
2921 // Make an iota expression. This is only called for one case: the
2922 // value of the predeclared constant iota.
2925 Expression::make_iota()
2927 static Iota_expression iota_expression(UNKNOWN_LOCATION);
2928 return &iota_expression;
2931 // A type conversion expression.
2933 class Type_conversion_expression : public Expression
2936 Type_conversion_expression(Type* type, Expression* expr,
2937 source_location location)
2938 : Expression(EXPRESSION_CONVERSION, location),
2939 type_(type), expr_(expr), may_convert_function_types_(false)
2942 // Return the type to which we are converting.
2945 { return this->type_; }
2947 // Return the expression which we are converting.
2950 { return this->expr_; }
2952 // Permit converting from one function type to another. This is
2953 // used internally for method expressions.
2955 set_may_convert_function_types()
2957 this->may_convert_function_types_ = true;
2960 // Import a type conversion expression.
2966 do_traverse(Traverse* traverse);
2969 do_lower(Gogo*, Named_object*, int);
2972 do_is_constant() const
2973 { return this->expr_->is_constant(); }
2976 do_integer_constant_value(bool, mpz_t, Type**) const;
2979 do_float_constant_value(mpfr_t, Type**) const;
2982 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
2985 do_string_constant_value(std::string*) const;
2989 { return this->type_; }
2992 do_determine_type(const Type_context*)
2994 Type_context subcontext(this->type_, false);
2995 this->expr_->determine_type(&subcontext);
2999 do_check_types(Gogo*);
3004 return new Type_conversion_expression(this->type_, this->expr_->copy(),
3009 do_get_tree(Translate_context* context);
3012 do_export(Export*) const;
3015 // The type to convert to.
3017 // The expression to convert.
3019 // True if this is permitted to convert function types. This is
3020 // used internally for method expressions.
3021 bool may_convert_function_types_;
3027 Type_conversion_expression::do_traverse(Traverse* traverse)
3029 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3030 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3031 return TRAVERSE_EXIT;
3032 return TRAVERSE_CONTINUE;
3035 // Convert to a constant at lowering time.
3038 Type_conversion_expression::do_lower(Gogo*, Named_object*, int)
3040 Type* type = this->type_;
3041 Expression* val = this->expr_;
3042 source_location location = this->location();
3044 if (type->integer_type() != NULL)
3049 if (val->integer_constant_value(false, ival, &dummy))
3051 if (!Integer_expression::check_constant(ival, type, location))
3052 mpz_set_ui(ival, 0);
3053 Expression* ret = Expression::make_integer(&ival, type, location);
3060 if (val->float_constant_value(fval, &dummy))
3062 if (!mpfr_integer_p(fval))
3065 "floating point constant truncated to integer");
3066 return Expression::make_error(location);
3068 mpfr_get_z(ival, fval, GMP_RNDN);
3069 if (!Integer_expression::check_constant(ival, type, location))
3070 mpz_set_ui(ival, 0);
3071 Expression* ret = Expression::make_integer(&ival, type, location);
3080 if (type->float_type() != NULL)
3085 if (val->float_constant_value(fval, &dummy))
3087 if (!Float_expression::check_constant(fval, type, location))
3088 mpfr_set_ui(fval, 0, GMP_RNDN);
3089 Float_expression::constrain_float(fval, type);
3090 Expression *ret = Expression::make_float(&fval, type, location);
3097 if (type->complex_type() != NULL)
3104 if (val->complex_constant_value(real, imag, &dummy))
3106 if (!Complex_expression::check_constant(real, imag, type, location))
3108 mpfr_set_ui(real, 0, GMP_RNDN);
3109 mpfr_set_ui(imag, 0, GMP_RNDN);
3111 Complex_expression::constrain_complex(real, imag, type);
3112 Expression* ret = Expression::make_complex(&real, &imag, type,
3122 if (type->is_open_array_type() && type->named_type() == NULL)
3124 Type* element_type = type->array_type()->element_type()->forwarded();
3125 bool is_byte = element_type == Type::lookup_integer_type("uint8");
3126 bool is_int = element_type == Type::lookup_integer_type("int");
3127 if (is_byte || is_int)
3130 if (val->string_constant_value(&s))
3132 Expression_list* vals = new Expression_list();
3135 for (std::string::const_iterator p = s.begin();
3140 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3141 Expression* v = Expression::make_integer(&val,
3150 const char *p = s.data();
3151 const char *pend = s.data() + s.length();
3155 int adv = Lex::fetch_char(p, &c);
3158 warning_at(this->location(), 0,
3159 "invalid UTF-8 encoding");
3164 mpz_init_set_ui(val, c);
3165 Expression* v = Expression::make_integer(&val,
3173 return Expression::make_slice_composite_literal(type, vals,
3182 // Return the constant integer value if there is one.
3185 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3189 if (this->type_->integer_type() == NULL)
3195 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3197 if (!Integer_expression::check_constant(ival, this->type_,
3205 *ptype = this->type_;
3212 if (this->expr_->float_constant_value(fval, &dummy))
3214 mpfr_get_z(val, fval, GMP_RNDN);
3216 if (!Integer_expression::check_constant(val, this->type_,
3219 *ptype = this->type_;
3227 // Return the constant floating point value if there is one.
3230 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3233 if (this->type_->float_type() == NULL)
3239 if (this->expr_->float_constant_value(fval, &dummy))
3241 if (!Float_expression::check_constant(fval, this->type_,
3247 mpfr_set(val, fval, GMP_RNDN);
3249 Float_expression::constrain_float(val, this->type_);
3250 *ptype = this->type_;
3258 // Return the constant complex value if there is one.
3261 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3265 if (this->type_->complex_type() == NULL)
3273 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3275 if (!Complex_expression::check_constant(rval, ival, this->type_,
3282 mpfr_set(real, rval, GMP_RNDN);
3283 mpfr_set(imag, ival, GMP_RNDN);
3286 Complex_expression::constrain_complex(real, imag, this->type_);
3287 *ptype = this->type_;
3296 // Return the constant string value if there is one.
3299 Type_conversion_expression::do_string_constant_value(std::string* val) const
3301 if (this->type_->is_string_type()
3302 && this->expr_->type()->integer_type() != NULL)
3307 if (this->expr_->integer_constant_value(false, ival, &dummy))
3309 unsigned long ulval = mpz_get_ui(ival);
3310 if (mpz_cmp_ui(ival, ulval) == 0)
3312 Lex::append_char(ulval, true, val, this->location());
3320 // FIXME: Could handle conversion from const []int here.
3325 // Check that types are convertible.
3328 Type_conversion_expression::do_check_types(Gogo*)
3330 Type* type = this->type_;
3331 Type* expr_type = this->expr_->type();
3334 if (type->is_error_type()
3335 || type->is_undefined()
3336 || expr_type->is_error_type()
3337 || expr_type->is_undefined())
3339 // Make sure we emit an error for an undefined type.
3342 this->set_is_error();
3346 if (this->may_convert_function_types_
3347 && type->function_type() != NULL
3348 && expr_type->function_type() != NULL)
3351 if (Type::are_convertible(type, expr_type, &reason))
3354 error_at(this->location(), "%s", reason.c_str());
3355 this->set_is_error();
3358 // Get a tree for a type conversion.
3361 Type_conversion_expression::do_get_tree(Translate_context* context)
3363 Gogo* gogo = context->gogo();
3364 tree type_tree = this->type_->get_tree(gogo);
3365 tree expr_tree = this->expr_->get_tree(context);
3367 if (type_tree == error_mark_node
3368 || expr_tree == error_mark_node
3369 || TREE_TYPE(expr_tree) == error_mark_node)
3370 return error_mark_node;
3372 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3373 return fold_convert(type_tree, expr_tree);
3375 Type* type = this->type_;
3376 Type* expr_type = this->expr_->type();
3378 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3379 ret = Expression::convert_for_assignment(context, type, expr_type,
3380 expr_tree, this->location());
3381 else if (type->integer_type() != NULL)
3383 if (expr_type->integer_type() != NULL
3384 || expr_type->float_type() != NULL
3385 || expr_type->is_unsafe_pointer_type())
3386 ret = fold(convert_to_integer(type_tree, expr_tree));
3390 else if (type->float_type() != NULL)
3392 if (expr_type->integer_type() != NULL
3393 || expr_type->float_type() != NULL)
3394 ret = fold(convert_to_real(type_tree, expr_tree));
3398 else if (type->complex_type() != NULL)
3400 if (expr_type->complex_type() != NULL)
3401 ret = fold(convert_to_complex(type_tree, expr_tree));
3405 else if (type->is_string_type()
3406 && expr_type->integer_type() != NULL)
3408 expr_tree = fold_convert(integer_type_node, expr_tree);
3409 if (host_integerp(expr_tree, 0))
3411 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3413 Lex::append_char(intval, true, &s, this->location());
3414 Expression* se = Expression::make_string(s, this->location());
3415 return se->get_tree(context);
3418 static tree int_to_string_fndecl;
3419 ret = Gogo::call_builtin(&int_to_string_fndecl,
3421 "__go_int_to_string",
3425 fold_convert(integer_type_node, expr_tree));
3427 else if (type->is_string_type()
3428 && (expr_type->array_type() != NULL
3429 || (expr_type->points_to() != NULL
3430 && expr_type->points_to()->array_type() != NULL)))
3432 Type* t = expr_type;
3433 if (t->points_to() != NULL)
3436 expr_tree = build_fold_indirect_ref(expr_tree);
3438 if (!DECL_P(expr_tree))
3439 expr_tree = save_expr(expr_tree);
3440 Array_type* a = t->array_type();
3441 Type* e = a->element_type()->forwarded();
3442 gcc_assert(e->integer_type() != NULL);
3443 tree valptr = fold_convert(const_ptr_type_node,
3444 a->value_pointer_tree(gogo, expr_tree));
3445 tree len = a->length_tree(gogo, expr_tree);
3446 len = fold_convert_loc(this->location(), size_type_node, len);
3447 if (e->integer_type()->is_unsigned()
3448 && e->integer_type()->bits() == 8)
3450 static tree byte_array_to_string_fndecl;
3451 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3453 "__go_byte_array_to_string",
3456 const_ptr_type_node,
3463 gcc_assert(e == Type::lookup_integer_type("int"));
3464 static tree int_array_to_string_fndecl;
3465 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3467 "__go_int_array_to_string",
3470 const_ptr_type_node,
3476 else if (type->is_open_array_type() && expr_type->is_string_type())
3478 Type* e = type->array_type()->element_type()->forwarded();
3479 gcc_assert(e->integer_type() != NULL);
3480 if (e->integer_type()->is_unsigned()
3481 && e->integer_type()->bits() == 8)
3483 static tree string_to_byte_array_fndecl;
3484 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3486 "__go_string_to_byte_array",
3489 TREE_TYPE(expr_tree),
3494 gcc_assert(e == Type::lookup_integer_type("int"));
3495 static tree string_to_int_array_fndecl;
3496 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3498 "__go_string_to_int_array",
3501 TREE_TYPE(expr_tree),
3505 else if ((type->is_unsafe_pointer_type()
3506 && expr_type->points_to() != NULL)
3507 || (expr_type->is_unsafe_pointer_type()
3508 && type->points_to() != NULL))
3509 ret = fold_convert(type_tree, expr_tree);
3510 else if (type->is_unsafe_pointer_type()
3511 && expr_type->integer_type() != NULL)
3512 ret = convert_to_pointer(type_tree, expr_tree);
3513 else if (this->may_convert_function_types_
3514 && type->function_type() != NULL
3515 && expr_type->function_type() != NULL)
3516 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3518 ret = Expression::convert_for_assignment(context, type, expr_type,
3519 expr_tree, this->location());
3524 // Output a type conversion in a constant expression.
3527 Type_conversion_expression::do_export(Export* exp) const
3529 exp->write_c_string("convert(");
3530 exp->write_type(this->type_);
3531 exp->write_c_string(", ");
3532 this->expr_->export_expression(exp);
3533 exp->write_c_string(")");
3536 // Import a type conversion or a struct construction.
3539 Type_conversion_expression::do_import(Import* imp)
3541 imp->require_c_string("convert(");
3542 Type* type = imp->read_type();
3543 imp->require_c_string(", ");
3544 Expression* val = Expression::import_expression(imp);
3545 imp->require_c_string(")");
3546 return Expression::make_cast(type, val, imp->location());
3549 // Make a type cast expression.
3552 Expression::make_cast(Type* type, Expression* val, source_location location)
3554 if (type->is_error_type() || val->is_error_expression())
3555 return Expression::make_error(location);
3556 return new Type_conversion_expression(type, val, location);
3559 // Unary expressions.
3561 class Unary_expression : public Expression
3564 Unary_expression(Operator op, Expression* expr, source_location location)
3565 : Expression(EXPRESSION_UNARY, location),
3566 op_(op), escapes_(true), expr_(expr)
3569 // Return the operator.
3572 { return this->op_; }
3574 // Return the operand.
3577 { return this->expr_; }
3579 // Record that an address expression does not escape.
3581 set_does_not_escape()
3583 gcc_assert(this->op_ == OPERATOR_AND);
3584 this->escapes_ = false;
3587 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3588 // could be done, false if not.
3590 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3593 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3594 // could be done, false if not.
3596 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3598 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3599 // true if this could be done, false if not.
3601 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3609 do_traverse(Traverse* traverse)
3610 { return Expression::traverse(&this->expr_, traverse); }
3613 do_lower(Gogo*, Named_object*, int);
3616 do_is_constant() const;
3619 do_integer_constant_value(bool, mpz_t, Type**) const;
3622 do_float_constant_value(mpfr_t, Type**) const;
3625 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3631 do_determine_type(const Type_context*);
3634 do_check_types(Gogo*);
3639 return Expression::make_unary(this->op_, this->expr_->copy(),
3644 do_is_addressable() const
3645 { return this->op_ == OPERATOR_MULT; }
3648 do_get_tree(Translate_context*);
3651 do_export(Export*) const;
3654 // The unary operator to apply.
3656 // Normally true. False if this is an address expression which does
3657 // not escape the current function.
3663 // If we are taking the address of a composite literal, and the
3664 // contents are not constant, then we want to make a heap composite
3668 Unary_expression::do_lower(Gogo*, Named_object*, int)
3670 source_location loc = this->location();
3671 Operator op = this->op_;
3672 Expression* expr = this->expr_;
3674 if (op == OPERATOR_MULT && expr->is_type_expression())
3675 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3677 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3678 // moving x to the heap. FIXME: Is it worth doing a real escape
3679 // analysis here? This case is found in math/unsafe.go and is
3680 // therefore worth special casing.
3681 if (op == OPERATOR_MULT)
3683 Expression* e = expr;
3684 while (e->classification() == EXPRESSION_CONVERSION)
3686 Type_conversion_expression* te
3687 = static_cast<Type_conversion_expression*>(e);
3691 if (e->classification() == EXPRESSION_UNARY)
3693 Unary_expression* ue = static_cast<Unary_expression*>(e);
3694 if (ue->op_ == OPERATOR_AND)
3701 ue->set_does_not_escape();
3706 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
3707 || op == OPERATOR_NOT || op == OPERATOR_XOR)
3709 Expression* ret = NULL;
3714 if (expr->integer_constant_value(false, eval, &etype))
3718 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
3719 ret = Expression::make_integer(&val, etype, loc);
3726 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
3731 if (expr->float_constant_value(fval, &ftype))
3735 if (Unary_expression::eval_float(op, fval, val))
3736 ret = Expression::make_float(&val, ftype, loc);
3747 if (expr->complex_constant_value(fval, ival, &ftype))
3753 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
3754 ret = Expression::make_complex(&real, &imag, ftype, loc);
3768 // Return whether a unary expression is a constant.
3771 Unary_expression::do_is_constant() const
3773 if (this->op_ == OPERATOR_MULT)
3775 // Indirecting through a pointer is only constant if the object
3776 // to which the expression points is constant, but we currently
3777 // have no way to determine that.
3780 else if (this->op_ == OPERATOR_AND)
3782 // Taking the address of a variable is constant if it is a
3783 // global variable, not constant otherwise. In other cases
3784 // taking the address is probably not a constant.
3785 Var_expression* ve = this->expr_->var_expression();
3788 Named_object* no = ve->named_object();
3789 return no->is_variable() && no->var_value()->is_global();
3794 return this->expr_->is_constant();
3797 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3798 // UVAL, if known; it may be NULL. Return true if this could be done,
3802 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3803 source_location location)
3810 case OPERATOR_MINUS:
3812 return Integer_expression::check_constant(val, utype, location);
3814 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3818 || utype->integer_type() == NULL
3819 || utype->integer_type()->is_abstract())
3823 // The number of HOST_WIDE_INTs that it takes to represent
3825 size_t count = ((mpz_sizeinbase(uval, 2)
3826 + HOST_BITS_PER_WIDE_INT
3828 / HOST_BITS_PER_WIDE_INT);
3830 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3831 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3834 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3835 gcc_assert(ecount <= count);
3837 // Trim down to the number of words required by the type.
3838 size_t obits = utype->integer_type()->bits();
3839 if (!utype->integer_type()->is_unsigned())
3841 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3842 / HOST_BITS_PER_WIDE_INT);
3843 gcc_assert(ocount <= ocount);
3845 for (size_t i = 0; i < ocount; ++i)
3848 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3850 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3853 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3857 return Integer_expression::check_constant(val, utype, location);
3866 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3867 // could be done, false if not.
3870 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
3875 mpfr_set(val, uval, GMP_RNDN);
3877 case OPERATOR_MINUS:
3878 mpfr_neg(val, uval, GMP_RNDN);
3890 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3891 // if this could be done, false if not.
3894 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
3895 mpfr_t real, mpfr_t imag)
3900 mpfr_set(real, rval, GMP_RNDN);
3901 mpfr_set(imag, ival, GMP_RNDN);
3903 case OPERATOR_MINUS:
3904 mpfr_neg(real, rval, GMP_RNDN);
3905 mpfr_neg(imag, ival, GMP_RNDN);
3917 // Return the integral constant value of a unary expression, if it has one.
3920 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
3926 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
3929 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
3935 // Return the floating point constant value of a unary expression, if
3939 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
3944 if (!this->expr_->float_constant_value(uval, ptype))
3947 ret = Unary_expression::eval_float(this->op_, uval, val);
3952 // Return the complex constant value of a unary expression, if it has
3956 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
3964 if (!this->expr_->complex_constant_value(rval, ival, ptype))
3967 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
3973 // Return the type of a unary expression.
3976 Unary_expression::do_type()
3981 case OPERATOR_MINUS:
3984 return this->expr_->type();
3987 return Type::make_pointer_type(this->expr_->type());
3991 Type* subtype = this->expr_->type();
3992 Type* points_to = subtype->points_to();
3993 if (points_to == NULL)
3994 return Type::make_error_type();
4003 // Determine abstract types for a unary expression.
4006 Unary_expression::do_determine_type(const Type_context* context)
4011 case OPERATOR_MINUS:
4014 this->expr_->determine_type(context);
4018 // Taking the address of something.
4020 Type* subtype = (context->type == NULL
4022 : context->type->points_to());
4023 Type_context subcontext(subtype, false);
4024 this->expr_->determine_type(&subcontext);
4029 // Indirecting through a pointer.
4031 Type* subtype = (context->type == NULL
4033 : Type::make_pointer_type(context->type));
4034 Type_context subcontext(subtype, false);
4035 this->expr_->determine_type(&subcontext);
4044 // Check types for a unary expression.
4047 Unary_expression::do_check_types(Gogo*)
4049 Type* type = this->expr_->type();
4050 if (type->is_error_type())
4052 this->set_is_error();
4059 case OPERATOR_MINUS:
4060 if (type->integer_type() == NULL
4061 && type->float_type() == NULL
4062 && type->complex_type() == NULL)
4063 this->report_error(_("expected numeric type"));
4068 if (type->integer_type() == NULL
4069 && !type->is_boolean_type())
4070 this->report_error(_("expected integer or boolean type"));
4074 if (!this->expr_->is_addressable())
4075 this->report_error(_("invalid operand for unary %<&%>"));
4077 this->expr_->address_taken(this->escapes_);
4081 // Indirecting through a pointer.
4082 if (type->points_to() == NULL)
4083 this->report_error(_("expected pointer"));
4091 // Get a tree for a unary expression.
4094 Unary_expression::do_get_tree(Translate_context* context)
4096 tree expr = this->expr_->get_tree(context);
4097 if (expr == error_mark_node)
4098 return error_mark_node;
4100 source_location loc = this->location();
4106 case OPERATOR_MINUS:
4108 tree type = TREE_TYPE(expr);
4109 tree compute_type = excess_precision_type(type);
4110 if (compute_type != NULL_TREE)
4111 expr = ::convert(compute_type, expr);
4112 tree ret = fold_build1_loc(loc, NEGATE_EXPR,
4113 (compute_type != NULL_TREE
4117 if (compute_type != NULL_TREE)
4118 ret = ::convert(type, ret);
4123 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4124 return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr);
4126 return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr,
4127 build_int_cst(TREE_TYPE(expr), 0));
4130 return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr);
4133 // We should not see a non-constant constructor here; cases
4134 // where we would see one should have been moved onto the heap
4135 // at parse time. Taking the address of a nonconstant
4136 // constructor will not do what the programmer expects.
4137 gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4138 gcc_assert(TREE_CODE(expr) != ADDR_EXPR);
4140 // Build a decl for a constant constructor.
4141 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4143 tree decl = build_decl(this->location(), VAR_DECL,
4144 create_tmp_var_name("C"), TREE_TYPE(expr));
4145 DECL_EXTERNAL(decl) = 0;
4146 TREE_PUBLIC(decl) = 0;
4147 TREE_READONLY(decl) = 1;
4148 TREE_CONSTANT(decl) = 1;
4149 TREE_STATIC(decl) = 1;
4150 TREE_ADDRESSABLE(decl) = 1;
4151 DECL_ARTIFICIAL(decl) = 1;
4152 DECL_INITIAL(decl) = expr;
4153 rest_of_decl_compilation(decl, 1, 0);
4157 return build_fold_addr_expr_loc(loc, expr);
4161 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4163 // If we are dereferencing the pointer to a large struct, we
4164 // need to check for nil. We don't bother to check for small
4165 // structs because we expect the system to crash on a nil
4166 // pointer dereference.
4167 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4168 if (s == -1 || s >= 4096)
4171 expr = save_expr(expr);
4172 tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node,
4174 fold_convert(TREE_TYPE(expr),
4175 null_pointer_node));
4176 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4178 expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr),
4179 build3(COND_EXPR, void_type_node,
4180 compare, crash, NULL_TREE),
4184 // If the type of EXPR is a recursive pointer type, then we
4185 // need to insert a cast before indirecting.
4186 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4188 Type* pt = this->expr_->type()->points_to();
4189 tree ind = pt->get_tree(context->gogo());
4190 expr = fold_convert_loc(loc, build_pointer_type(ind), expr);
4193 return build_fold_indirect_ref_loc(loc, expr);
4201 // Export a unary expression.
4204 Unary_expression::do_export(Export* exp) const
4209 exp->write_c_string("+ ");
4211 case OPERATOR_MINUS:
4212 exp->write_c_string("- ");
4215 exp->write_c_string("! ");
4218 exp->write_c_string("^ ");
4225 this->expr_->export_expression(exp);
4228 // Import a unary expression.
4231 Unary_expression::do_import(Import* imp)
4234 switch (imp->get_char())
4240 op = OPERATOR_MINUS;
4251 imp->require_c_string(" ");
4252 Expression* expr = Expression::import_expression(imp);
4253 return Expression::make_unary(op, expr, imp->location());
4256 // Make a unary expression.
4259 Expression::make_unary(Operator op, Expression* expr, source_location location)
4261 return new Unary_expression(op, expr, location);
4264 // If this is an indirection through a pointer, return the expression
4265 // being pointed through. Otherwise return this.
4270 if (this->classification_ == EXPRESSION_UNARY)
4272 Unary_expression* ue = static_cast<Unary_expression*>(this);
4273 if (ue->op() == OPERATOR_MULT)
4274 return ue->operand();
4279 // Class Binary_expression.
4284 Binary_expression::do_traverse(Traverse* traverse)
4286 int t = Expression::traverse(&this->left_, traverse);
4287 if (t == TRAVERSE_EXIT)
4288 return TRAVERSE_EXIT;
4289 return Expression::traverse(&this->right_, traverse);
4292 // Compare integer constants according to OP.
4295 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4298 int i = mpz_cmp(left_val, right_val);
4303 case OPERATOR_NOTEQ:
4318 // Compare floating point constants according to OP.
4321 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4326 i = mpfr_cmp(left_val, right_val);
4330 mpfr_init_set(lv, left_val, GMP_RNDN);
4332 mpfr_init_set(rv, right_val, GMP_RNDN);
4333 Float_expression::constrain_float(lv, type);
4334 Float_expression::constrain_float(rv, type);
4335 i = mpfr_cmp(lv, rv);
4343 case OPERATOR_NOTEQ:
4358 // Compare complex constants according to OP. Complex numbers may
4359 // only be compared for equality.
4362 Binary_expression::compare_complex(Operator op, Type* type,
4363 mpfr_t left_real, mpfr_t left_imag,
4364 mpfr_t right_real, mpfr_t right_imag)
4368 is_equal = (mpfr_cmp(left_real, right_real) == 0
4369 && mpfr_cmp(left_imag, right_imag) == 0);
4374 mpfr_init_set(lr, left_real, GMP_RNDN);
4375 mpfr_init_set(li, left_imag, GMP_RNDN);
4378 mpfr_init_set(rr, right_real, GMP_RNDN);
4379 mpfr_init_set(ri, right_imag, GMP_RNDN);
4380 Complex_expression::constrain_complex(lr, li, type);
4381 Complex_expression::constrain_complex(rr, ri, type);
4382 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4392 case OPERATOR_NOTEQ:
4399 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4400 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4401 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4402 // this could be done, false if not.
4405 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4406 Type* right_type, mpz_t right_val,
4407 source_location location, mpz_t val)
4409 bool is_shift_op = false;
4413 case OPERATOR_ANDAND:
4415 case OPERATOR_NOTEQ:
4420 // These return boolean values. We should probably handle them
4421 // anyhow in case a type conversion is used on the result.
4424 mpz_add(val, left_val, right_val);
4426 case OPERATOR_MINUS:
4427 mpz_sub(val, left_val, right_val);
4430 mpz_ior(val, left_val, right_val);
4433 mpz_xor(val, left_val, right_val);
4436 mpz_mul(val, left_val, right_val);
4439 if (mpz_sgn(right_val) != 0)
4440 mpz_tdiv_q(val, left_val, right_val);
4443 error_at(location, "division by zero");
4449 if (mpz_sgn(right_val) != 0)
4450 mpz_tdiv_r(val, left_val, right_val);
4453 error_at(location, "division by zero");
4458 case OPERATOR_LSHIFT:
4460 unsigned long shift = mpz_get_ui(right_val);
4461 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
4463 error_at(location, "shift count overflow");
4467 mpz_mul_2exp(val, left_val, shift);
4472 case OPERATOR_RSHIFT:
4474 unsigned long shift = mpz_get_ui(right_val);
4475 if (mpz_cmp_ui(right_val, shift) != 0)
4477 error_at(location, "shift count overflow");
4481 if (mpz_cmp_ui(left_val, 0) >= 0)
4482 mpz_tdiv_q_2exp(val, left_val, shift);
4484 mpz_fdiv_q_2exp(val, left_val, shift);
4490 mpz_and(val, left_val, right_val);
4492 case OPERATOR_BITCLEAR:
4496 mpz_com(tval, right_val);
4497 mpz_and(val, left_val, tval);
4505 Type* type = left_type;
4510 else if (type != right_type && right_type != NULL)
4512 if (type->is_abstract())
4514 else if (!right_type->is_abstract())
4516 // This look like a type error which should be diagnosed
4517 // elsewhere. Don't do anything here, to avoid an
4518 // unhelpful chain of error messages.
4524 if (type != NULL && !type->is_abstract())
4526 // We have to check the operands too, as we have implicitly
4527 // coerced them to TYPE.
4528 if ((type != left_type
4529 && !Integer_expression::check_constant(left_val, type, location))
4531 && type != right_type
4532 && !Integer_expression::check_constant(right_val, type,
4534 || !Integer_expression::check_constant(val, type, location))
4541 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4542 // Return true if this could be done, false if not.
4545 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4546 Type* right_type, mpfr_t right_val,
4547 mpfr_t val, source_location location)
4552 case OPERATOR_ANDAND:
4554 case OPERATOR_NOTEQ:
4559 // These return boolean values. We should probably handle them
4560 // anyhow in case a type conversion is used on the result.
4563 mpfr_add(val, left_val, right_val, GMP_RNDN);
4565 case OPERATOR_MINUS:
4566 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4571 case OPERATOR_BITCLEAR:
4574 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4577 if (mpfr_zero_p(right_val))
4578 error_at(location, "division by zero");
4579 mpfr_div(val, left_val, right_val, GMP_RNDN);
4583 case OPERATOR_LSHIFT:
4584 case OPERATOR_RSHIFT:
4590 Type* type = left_type;
4593 else if (type != right_type && right_type != NULL)
4595 if (type->is_abstract())
4597 else if (!right_type->is_abstract())
4599 // This looks like a type error which should be diagnosed
4600 // elsewhere. Don't do anything here, to avoid an unhelpful
4601 // chain of error messages.
4606 if (type != NULL && !type->is_abstract())
4608 if ((type != left_type
4609 && !Float_expression::check_constant(left_val, type, location))
4610 || (type != right_type
4611 && !Float_expression::check_constant(right_val, type,
4613 || !Float_expression::check_constant(val, type, location))
4614 mpfr_set_ui(val, 0, GMP_RNDN);
4620 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4621 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4622 // could be done, false if not.
4625 Binary_expression::eval_complex(Operator op, Type* left_type,
4626 mpfr_t left_real, mpfr_t left_imag,
4628 mpfr_t right_real, mpfr_t right_imag,
4629 mpfr_t real, mpfr_t imag,
4630 source_location location)
4635 case OPERATOR_ANDAND:
4637 case OPERATOR_NOTEQ:
4642 // These return boolean values and must be handled differently.
4645 mpfr_add(real, left_real, right_real, GMP_RNDN);
4646 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4648 case OPERATOR_MINUS:
4649 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4650 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4655 case OPERATOR_BITCLEAR:
4659 // You might think that multiplying two complex numbers would
4660 // be simple, and you would be right, until you start to think
4661 // about getting the right answer for infinity. If one
4662 // operand here is infinity and the other is anything other
4663 // than zero or NaN, then we are going to wind up subtracting
4664 // two infinity values. That will give us a NaN, but the
4665 // correct answer is infinity.
4669 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4673 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4677 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4681 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4683 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4684 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4686 // If we get NaN on both sides, check whether it should really
4687 // be infinity. The rule is that if either side of the
4688 // complex number is infinity, then the whole value is
4689 // infinity, even if the other side is NaN. So the only case
4690 // we have to fix is the one in which both sides are NaN.
4691 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4692 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4693 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4695 bool is_infinity = false;
4699 mpfr_init_set(lr, left_real, GMP_RNDN);
4700 mpfr_init_set(li, left_imag, GMP_RNDN);
4704 mpfr_init_set(rr, right_real, GMP_RNDN);
4705 mpfr_init_set(ri, right_imag, GMP_RNDN);
4707 // If the left side is infinity, then the result is
4709 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4711 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4712 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4713 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4714 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4717 mpfr_set_ui(rr, 0, GMP_RNDN);
4718 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4722 mpfr_set_ui(ri, 0, GMP_RNDN);
4723 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4728 // If the right side is infinity, then the result is
4730 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4732 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4733 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4734 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4735 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4738 mpfr_set_ui(lr, 0, GMP_RNDN);
4739 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4743 mpfr_set_ui(li, 0, GMP_RNDN);
4744 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4749 // If we got an overflow in the intermediate computations,
4750 // then the result is infinity.
4752 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4753 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4757 mpfr_set_ui(lr, 0, GMP_RNDN);
4758 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4762 mpfr_set_ui(li, 0, GMP_RNDN);
4763 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4767 mpfr_set_ui(rr, 0, GMP_RNDN);
4768 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4772 mpfr_set_ui(ri, 0, GMP_RNDN);
4773 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4780 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4781 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4782 mpfr_mul(lirr, li, rr, GMP_RNDN);
4783 mpfr_mul(liri, li, ri, GMP_RNDN);
4784 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4785 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4786 mpfr_set_inf(real, mpfr_sgn(real));
4787 mpfr_set_inf(imag, mpfr_sgn(imag));
4804 // For complex division we want to avoid having an
4805 // intermediate overflow turn the whole result in a NaN. We
4806 // scale the values to try to avoid this.
4808 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
4809 error_at(location, "division by zero");
4815 mpfr_abs(rra, right_real, GMP_RNDN);
4816 mpfr_abs(ria, right_imag, GMP_RNDN);
4819 mpfr_max(t, rra, ria, GMP_RNDN);
4823 mpfr_init_set(rr, right_real, GMP_RNDN);
4824 mpfr_init_set(ri, right_imag, GMP_RNDN);
4826 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4828 ilogbw = mpfr_get_exp(t);
4829 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4830 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4835 mpfr_mul(denom, rr, rr, GMP_RNDN);
4836 mpfr_mul(t, ri, ri, GMP_RNDN);
4837 mpfr_add(denom, denom, t, GMP_RNDN);
4839 mpfr_mul(real, left_real, rr, GMP_RNDN);
4840 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4841 mpfr_add(real, real, t, GMP_RNDN);
4842 mpfr_div(real, real, denom, GMP_RNDN);
4843 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4845 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4846 mpfr_mul(t, left_real, ri, GMP_RNDN);
4847 mpfr_sub(imag, imag, t, GMP_RNDN);
4848 mpfr_div(imag, imag, denom, GMP_RNDN);
4849 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4851 // If we wind up with NaN on both sides, check whether we
4852 // should really have infinity. The rule is that if either
4853 // side of the complex number is infinity, then the whole
4854 // value is infinity, even if the other side is NaN. So the
4855 // only case we have to fix is the one in which both sides are
4857 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4858 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4859 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4861 if (mpfr_zero_p(denom))
4863 mpfr_set_inf(real, mpfr_sgn(rr));
4864 mpfr_mul(real, real, left_real, GMP_RNDN);
4865 mpfr_set_inf(imag, mpfr_sgn(rr));
4866 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4868 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4869 && mpfr_number_p(rr) && mpfr_number_p(ri))
4871 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
4872 mpfr_copysign(t, t, left_real, GMP_RNDN);
4875 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
4876 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
4880 mpfr_mul(t3, t, rr, GMP_RNDN);
4884 mpfr_mul(t4, t2, ri, GMP_RNDN);
4886 mpfr_add(t3, t3, t4, GMP_RNDN);
4887 mpfr_set_inf(real, mpfr_sgn(t3));
4889 mpfr_mul(t3, t2, rr, GMP_RNDN);
4890 mpfr_mul(t4, t, ri, GMP_RNDN);
4891 mpfr_sub(t3, t3, t4, GMP_RNDN);
4892 mpfr_set_inf(imag, mpfr_sgn(t3));
4898 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
4899 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
4901 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4902 mpfr_copysign(t, t, rr, GMP_RNDN);
4905 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4906 mpfr_copysign(t2, t2, ri, GMP_RNDN);
4910 mpfr_mul(t3, left_real, t, GMP_RNDN);
4914 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
4916 mpfr_add(t3, t3, t4, GMP_RNDN);
4917 mpfr_set_ui(real, 0, GMP_RNDN);
4918 mpfr_mul(real, real, t3, GMP_RNDN);
4920 mpfr_mul(t3, left_imag, t, GMP_RNDN);
4921 mpfr_mul(t4, left_real, t2, GMP_RNDN);
4922 mpfr_sub(t3, t3, t4, GMP_RNDN);
4923 mpfr_set_ui(imag, 0, GMP_RNDN);
4924 mpfr_mul(imag, imag, t3, GMP_RNDN);
4942 case OPERATOR_LSHIFT:
4943 case OPERATOR_RSHIFT:
4949 Type* type = left_type;
4952 else if (type != right_type && right_type != NULL)
4954 if (type->is_abstract())
4956 else if (!right_type->is_abstract())
4958 // This looks like a type error which should be diagnosed
4959 // elsewhere. Don't do anything here, to avoid an unhelpful
4960 // chain of error messages.
4965 if (type != NULL && !type->is_abstract())
4967 if ((type != left_type
4968 && !Complex_expression::check_constant(left_real, left_imag,
4970 || (type != right_type
4971 && !Complex_expression::check_constant(right_real, right_imag,
4973 || !Complex_expression::check_constant(real, imag, type,
4976 mpfr_set_ui(real, 0, GMP_RNDN);
4977 mpfr_set_ui(imag, 0, GMP_RNDN);
4984 // Lower a binary expression. We have to evaluate constant
4985 // expressions now, in order to implement Go's unlimited precision
4989 Binary_expression::do_lower(Gogo*, Named_object*, int)
4991 source_location location = this->location();
4992 Operator op = this->op_;
4993 Expression* left = this->left_;
4994 Expression* right = this->right_;
4996 const bool is_comparison = (op == OPERATOR_EQEQ
4997 || op == OPERATOR_NOTEQ
4998 || op == OPERATOR_LT
4999 || op == OPERATOR_LE
5000 || op == OPERATOR_GT
5001 || op == OPERATOR_GE);
5003 // Integer constant expressions.
5009 mpz_init(right_val);
5011 if (left->integer_constant_value(false, left_val, &left_type)
5012 && right->integer_constant_value(false, right_val, &right_type))
5014 Expression* ret = NULL;
5015 if (left_type != right_type
5016 && left_type != NULL
5017 && right_type != NULL
5018 && left_type->base() != right_type->base()
5019 && op != OPERATOR_LSHIFT
5020 && op != OPERATOR_RSHIFT)
5022 // May be a type error--let it be diagnosed later.
5024 else if (is_comparison)
5026 bool b = Binary_expression::compare_integer(op, left_val,
5028 ret = Expression::make_cast(Type::lookup_bool_type(),
5029 Expression::make_boolean(b, location),
5037 if (Binary_expression::eval_integer(op, left_type, left_val,
5038 right_type, right_val,
5041 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
5043 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5045 else if (left_type == NULL)
5047 else if (right_type == NULL)
5049 else if (!left_type->is_abstract()
5050 && left_type->named_type() != NULL)
5052 else if (!right_type->is_abstract()
5053 && right_type->named_type() != NULL)
5055 else if (!left_type->is_abstract())
5057 else if (!right_type->is_abstract())
5059 else if (left_type->float_type() != NULL)
5061 else if (right_type->float_type() != NULL)
5063 else if (left_type->complex_type() != NULL)
5065 else if (right_type->complex_type() != NULL)
5069 ret = Expression::make_integer(&val, type, location);
5077 mpz_clear(right_val);
5078 mpz_clear(left_val);
5082 mpz_clear(right_val);
5083 mpz_clear(left_val);
5086 // Floating point constant expressions.
5089 mpfr_init(left_val);
5092 mpfr_init(right_val);
5094 if (left->float_constant_value(left_val, &left_type)
5095 && right->float_constant_value(right_val, &right_type))
5097 Expression* ret = NULL;
5098 if (left_type != right_type
5099 && left_type != NULL
5100 && right_type != NULL
5101 && left_type->base() != right_type->base()
5102 && op != OPERATOR_LSHIFT
5103 && op != OPERATOR_RSHIFT)
5105 // May be a type error--let it be diagnosed later.
5107 else if (is_comparison)
5109 bool b = Binary_expression::compare_float(op,
5113 left_val, right_val);
5114 ret = Expression::make_boolean(b, location);
5121 if (Binary_expression::eval_float(op, left_type, left_val,
5122 right_type, right_val, val,
5125 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5126 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5128 if (left_type == NULL)
5130 else if (right_type == NULL)
5132 else if (!left_type->is_abstract()
5133 && left_type->named_type() != NULL)
5135 else if (!right_type->is_abstract()
5136 && right_type->named_type() != NULL)
5138 else if (!left_type->is_abstract())
5140 else if (!right_type->is_abstract())
5142 else if (left_type->float_type() != NULL)
5144 else if (right_type->float_type() != NULL)
5148 ret = Expression::make_float(&val, type, location);
5156 mpfr_clear(right_val);
5157 mpfr_clear(left_val);
5161 mpfr_clear(right_val);
5162 mpfr_clear(left_val);
5165 // Complex constant expressions.
5169 mpfr_init(left_real);
5170 mpfr_init(left_imag);
5175 mpfr_init(right_real);
5176 mpfr_init(right_imag);
5179 if (left->complex_constant_value(left_real, left_imag, &left_type)
5180 && right->complex_constant_value(right_real, right_imag, &right_type))
5182 Expression* ret = NULL;
5183 if (left_type != right_type
5184 && left_type != NULL
5185 && right_type != NULL
5186 && left_type->base() != right_type->base())
5188 // May be a type error--let it be diagnosed later.
5190 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5192 bool b = Binary_expression::compare_complex(op,
5200 ret = Expression::make_boolean(b, location);
5209 if (Binary_expression::eval_complex(op, left_type,
5210 left_real, left_imag,
5212 right_real, right_imag,
5216 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5217 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5219 if (left_type == NULL)
5221 else if (right_type == NULL)
5223 else if (!left_type->is_abstract()
5224 && left_type->named_type() != NULL)
5226 else if (!right_type->is_abstract()
5227 && right_type->named_type() != NULL)
5229 else if (!left_type->is_abstract())
5231 else if (!right_type->is_abstract())
5233 else if (left_type->complex_type() != NULL)
5235 else if (right_type->complex_type() != NULL)
5239 ret = Expression::make_complex(&real, &imag, type,
5248 mpfr_clear(left_real);
5249 mpfr_clear(left_imag);
5250 mpfr_clear(right_real);
5251 mpfr_clear(right_imag);
5256 mpfr_clear(left_real);
5257 mpfr_clear(left_imag);
5258 mpfr_clear(right_real);
5259 mpfr_clear(right_imag);
5262 // String constant expressions.
5263 if (op == OPERATOR_PLUS
5264 && left->type()->is_string_type()
5265 && right->type()->is_string_type())
5267 std::string left_string;
5268 std::string right_string;
5269 if (left->string_constant_value(&left_string)
5270 && right->string_constant_value(&right_string))
5271 return Expression::make_string(left_string + right_string, location);
5277 // Return the integer constant value, if it has one.
5280 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5286 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5289 mpz_clear(left_val);
5294 mpz_init(right_val);
5296 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5299 mpz_clear(right_val);
5300 mpz_clear(left_val);
5305 if (left_type != right_type
5306 && left_type != NULL
5307 && right_type != NULL
5308 && left_type->base() != right_type->base()
5309 && this->op_ != OPERATOR_RSHIFT
5310 && this->op_ != OPERATOR_LSHIFT)
5313 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5314 right_type, right_val,
5315 this->location(), val);
5317 mpz_clear(right_val);
5318 mpz_clear(left_val);
5326 // Return the floating point constant value, if it has one.
5329 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5332 mpfr_init(left_val);
5334 if (!this->left_->float_constant_value(left_val, &left_type))
5336 mpfr_clear(left_val);
5341 mpfr_init(right_val);
5343 if (!this->right_->float_constant_value(right_val, &right_type))
5345 mpfr_clear(right_val);
5346 mpfr_clear(left_val);
5351 if (left_type != right_type
5352 && left_type != NULL
5353 && right_type != NULL
5354 && left_type->base() != right_type->base())
5357 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5358 right_type, right_val,
5359 val, this->location());
5361 mpfr_clear(left_val);
5362 mpfr_clear(right_val);
5370 // Return the complex constant value, if it has one.
5373 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5378 mpfr_init(left_real);
5379 mpfr_init(left_imag);
5381 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5383 mpfr_clear(left_real);
5384 mpfr_clear(left_imag);
5390 mpfr_init(right_real);
5391 mpfr_init(right_imag);
5393 if (!this->right_->complex_constant_value(right_real, right_imag,
5396 mpfr_clear(left_real);
5397 mpfr_clear(left_imag);
5398 mpfr_clear(right_real);
5399 mpfr_clear(right_imag);
5404 if (left_type != right_type
5405 && left_type != NULL
5406 && right_type != NULL
5407 && left_type->base() != right_type->base())
5410 ret = Binary_expression::eval_complex(this->op_, left_type,
5411 left_real, left_imag,
5413 right_real, right_imag,
5416 mpfr_clear(left_real);
5417 mpfr_clear(left_imag);
5418 mpfr_clear(right_real);
5419 mpfr_clear(right_imag);
5427 // Note that the value is being discarded.
5430 Binary_expression::do_discarding_value()
5432 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5433 this->right_->discarding_value();
5435 this->warn_about_unused_value();
5441 Binary_expression::do_type()
5443 if (this->classification() == EXPRESSION_ERROR)
5444 return Type::make_error_type();
5449 case OPERATOR_ANDAND:
5451 case OPERATOR_NOTEQ:
5456 return Type::lookup_bool_type();
5459 case OPERATOR_MINUS:
5466 case OPERATOR_BITCLEAR:
5468 Type* left_type = this->left_->type();
5469 Type* right_type = this->right_->type();
5470 if (left_type->is_error_type())
5472 else if (right_type->is_error_type())
5474 else if (!Type::are_compatible_for_binop(left_type, right_type))
5476 this->report_error(_("incompatible types in binary expression"));
5477 return Type::make_error_type();
5479 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
5481 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5483 else if (!left_type->is_abstract())
5485 else if (!right_type->is_abstract())
5487 else if (left_type->complex_type() != NULL)
5489 else if (right_type->complex_type() != NULL)
5491 else if (left_type->float_type() != NULL)
5493 else if (right_type->float_type() != NULL)
5499 case OPERATOR_LSHIFT:
5500 case OPERATOR_RSHIFT:
5501 return this->left_->type();
5508 // Set type for a binary expression.
5511 Binary_expression::do_determine_type(const Type_context* context)
5513 Type* tleft = this->left_->type();
5514 Type* tright = this->right_->type();
5516 // Both sides should have the same type, except for the shift
5517 // operations. For a comparison, we should ignore the incoming
5520 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5521 || this->op_ == OPERATOR_RSHIFT);
5523 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5524 || this->op_ == OPERATOR_NOTEQ
5525 || this->op_ == OPERATOR_LT
5526 || this->op_ == OPERATOR_LE
5527 || this->op_ == OPERATOR_GT
5528 || this->op_ == OPERATOR_GE);
5530 Type_context subcontext(*context);
5534 // In a comparison, the context does not determine the types of
5536 subcontext.type = NULL;
5539 // Set the context for the left hand operand.
5542 // The right hand operand plays no role in determining the type
5543 // of the left hand operand. A shift of an abstract integer in
5544 // a string context gets special treatment, which may be a
5546 if (subcontext.type != NULL
5547 && subcontext.type->is_string_type()
5548 && tleft->is_abstract())
5549 error_at(this->location(), "shift of non-integer operand");
5551 else if (!tleft->is_abstract())
5552 subcontext.type = tleft;
5553 else if (!tright->is_abstract())
5554 subcontext.type = tright;
5555 else if (subcontext.type == NULL)
5557 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5558 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5559 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5561 // Both sides have an abstract integer, abstract float, or
5562 // abstract complex type. Just let CONTEXT determine
5563 // whether they may remain abstract or not.
5565 else if (tleft->complex_type() != NULL)
5566 subcontext.type = tleft;
5567 else if (tright->complex_type() != NULL)
5568 subcontext.type = tright;
5569 else if (tleft->float_type() != NULL)
5570 subcontext.type = tleft;
5571 else if (tright->float_type() != NULL)
5572 subcontext.type = tright;
5574 subcontext.type = tleft;
5576 if (subcontext.type != NULL && !context->may_be_abstract)
5577 subcontext.type = subcontext.type->make_non_abstract_type();
5580 this->left_->determine_type(&subcontext);
5582 // The context for the right hand operand is the same as for the
5583 // left hand operand, except for a shift operator.
5586 subcontext.type = Type::lookup_integer_type("uint");
5587 subcontext.may_be_abstract = false;
5590 this->right_->determine_type(&subcontext);
5593 // Report an error if the binary operator OP does not support TYPE.
5594 // Return whether the operation is OK. This should not be used for
5598 Binary_expression::check_operator_type(Operator op, Type* type,
5599 source_location location)
5604 case OPERATOR_ANDAND:
5605 if (!type->is_boolean_type())
5607 error_at(location, "expected boolean type");
5613 case OPERATOR_NOTEQ:
5614 if (type->integer_type() == NULL
5615 && type->float_type() == NULL
5616 && type->complex_type() == NULL
5617 && !type->is_string_type()
5618 && type->points_to() == NULL
5619 && !type->is_nil_type()
5620 && !type->is_boolean_type()
5621 && type->interface_type() == NULL
5622 && (type->array_type() == NULL
5623 || type->array_type()->length() != NULL)
5624 && type->map_type() == NULL
5625 && type->channel_type() == NULL
5626 && type->function_type() == NULL)
5629 ("expected integer, floating, complex, string, pointer, "
5630 "boolean, interface, slice, map, channel, "
5631 "or function type"));
5640 if (type->integer_type() == NULL
5641 && type->float_type() == NULL
5642 && !type->is_string_type())
5644 error_at(location, "expected integer, floating, or string type");
5650 case OPERATOR_PLUSEQ:
5651 if (type->integer_type() == NULL
5652 && type->float_type() == NULL
5653 && type->complex_type() == NULL
5654 && !type->is_string_type())
5657 "expected integer, floating, complex, or string type");
5662 case OPERATOR_MINUS:
5663 case OPERATOR_MINUSEQ:
5665 case OPERATOR_MULTEQ:
5667 case OPERATOR_DIVEQ:
5668 if (type->integer_type() == NULL
5669 && type->float_type() == NULL
5670 && type->complex_type() == NULL)
5672 error_at(location, "expected integer, floating, or complex type");
5678 case OPERATOR_MODEQ:
5682 case OPERATOR_ANDEQ:
5684 case OPERATOR_XOREQ:
5685 case OPERATOR_BITCLEAR:
5686 case OPERATOR_BITCLEAREQ:
5687 if (type->integer_type() == NULL)
5689 error_at(location, "expected integer type");
5704 Binary_expression::do_check_types(Gogo*)
5706 if (this->classification() == EXPRESSION_ERROR)
5709 Type* left_type = this->left_->type();
5710 Type* right_type = this->right_->type();
5711 if (left_type->is_error_type() || right_type->is_error_type())
5713 this->set_is_error();
5717 if (this->op_ == OPERATOR_EQEQ
5718 || this->op_ == OPERATOR_NOTEQ
5719 || this->op_ == OPERATOR_LT
5720 || this->op_ == OPERATOR_LE
5721 || this->op_ == OPERATOR_GT
5722 || this->op_ == OPERATOR_GE)
5724 if (!Type::are_assignable(left_type, right_type, NULL)
5725 && !Type::are_assignable(right_type, left_type, NULL))
5727 this->report_error(_("incompatible types in binary expression"));
5730 if (!Binary_expression::check_operator_type(this->op_, left_type,
5732 || !Binary_expression::check_operator_type(this->op_, right_type,
5735 this->set_is_error();
5739 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5741 if (!Type::are_compatible_for_binop(left_type, right_type))
5743 this->report_error(_("incompatible types in binary expression"));
5746 if (!Binary_expression::check_operator_type(this->op_, left_type,
5749 this->set_is_error();
5755 if (left_type->integer_type() == NULL)
5756 this->report_error(_("shift of non-integer operand"));
5758 if (!right_type->is_abstract()
5759 && (right_type->integer_type() == NULL
5760 || !right_type->integer_type()->is_unsigned()))
5761 this->report_error(_("shift count not unsigned integer"));
5767 if (this->right_->integer_constant_value(true, val, &type))
5769 if (mpz_sgn(val) < 0)
5770 this->report_error(_("negative shift count"));
5777 // Get a tree for a binary expression.
5780 Binary_expression::do_get_tree(Translate_context* context)
5782 tree left = this->left_->get_tree(context);
5783 tree right = this->right_->get_tree(context);
5785 if (left == error_mark_node || right == error_mark_node)
5786 return error_mark_node;
5788 enum tree_code code;
5789 bool use_left_type = true;
5790 bool is_shift_op = false;
5794 case OPERATOR_NOTEQ:
5799 return Expression::comparison_tree(context, this->op_,
5800 this->left_->type(), left,
5801 this->right_->type(), right,
5805 code = TRUTH_ORIF_EXPR;
5806 use_left_type = false;
5808 case OPERATOR_ANDAND:
5809 code = TRUTH_ANDIF_EXPR;
5810 use_left_type = false;
5815 case OPERATOR_MINUS:
5819 code = BIT_IOR_EXPR;
5822 code = BIT_XOR_EXPR;
5829 Type *t = this->left_->type();
5830 if (t->float_type() != NULL || t->complex_type() != NULL)
5833 code = TRUNC_DIV_EXPR;
5837 code = TRUNC_MOD_EXPR;
5839 case OPERATOR_LSHIFT:
5843 case OPERATOR_RSHIFT:
5848 code = BIT_AND_EXPR;
5850 case OPERATOR_BITCLEAR:
5851 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5852 code = BIT_AND_EXPR;
5858 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5860 if (this->left_->type()->is_string_type())
5862 gcc_assert(this->op_ == OPERATOR_PLUS);
5863 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5864 static tree string_plus_decl;
5865 return Gogo::call_builtin(&string_plus_decl,
5876 tree compute_type = excess_precision_type(type);
5877 if (compute_type != NULL_TREE)
5879 left = ::convert(compute_type, left);
5880 right = ::convert(compute_type, right);
5883 tree eval_saved = NULL_TREE;
5886 // Make sure the values are evaluated.
5887 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
5889 left = save_expr(left);
5892 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
5894 right = save_expr(right);
5895 if (eval_saved == NULL_TREE)
5898 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
5899 void_type_node, eval_saved, right);
5903 tree ret = fold_build2_loc(this->location(),
5905 compute_type != NULL_TREE ? compute_type : type,
5908 if (compute_type != NULL_TREE)
5909 ret = ::convert(type, ret);
5911 // In Go, a shift larger than the size of the type is well-defined.
5912 // This is not true in GENERIC, so we need to insert a conditional.
5915 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
5916 gcc_assert(this->left_->type()->integer_type() != NULL);
5917 int bits = TYPE_PRECISION(TREE_TYPE(left));
5919 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
5920 build_int_cst_type(TREE_TYPE(right), bits));
5922 tree overflow_result = fold_convert_loc(this->location(),
5925 if (this->op_ == OPERATOR_RSHIFT
5926 && !this->left_->type()->integer_type()->is_unsigned())
5928 tree neg = fold_build2_loc(this->location(), LT_EXPR,
5929 boolean_type_node, left,
5930 fold_convert_loc(this->location(),
5932 integer_zero_node));
5933 tree neg_one = fold_build2_loc(this->location(),
5934 MINUS_EXPR, TREE_TYPE(left),
5935 fold_convert_loc(this->location(),
5938 fold_convert_loc(this->location(),
5941 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
5942 TREE_TYPE(left), neg, neg_one,
5946 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
5947 compare, ret, overflow_result);
5949 if (eval_saved != NULL_TREE)
5950 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
5951 TREE_TYPE(ret), eval_saved, ret);
5957 // Export a binary expression.
5960 Binary_expression::do_export(Export* exp) const
5962 exp->write_c_string("(");
5963 this->left_->export_expression(exp);
5967 exp->write_c_string(" || ");
5969 case OPERATOR_ANDAND:
5970 exp->write_c_string(" && ");
5973 exp->write_c_string(" == ");
5975 case OPERATOR_NOTEQ:
5976 exp->write_c_string(" != ");
5979 exp->write_c_string(" < ");
5982 exp->write_c_string(" <= ");
5985 exp->write_c_string(" > ");
5988 exp->write_c_string(" >= ");
5991 exp->write_c_string(" + ");
5993 case OPERATOR_MINUS:
5994 exp->write_c_string(" - ");
5997 exp->write_c_string(" | ");
6000 exp->write_c_string(" ^ ");
6003 exp->write_c_string(" * ");
6006 exp->write_c_string(" / ");
6009 exp->write_c_string(" % ");
6011 case OPERATOR_LSHIFT:
6012 exp->write_c_string(" << ");
6014 case OPERATOR_RSHIFT:
6015 exp->write_c_string(" >> ");
6018 exp->write_c_string(" & ");
6020 case OPERATOR_BITCLEAR:
6021 exp->write_c_string(" &^ ");
6026 this->right_->export_expression(exp);
6027 exp->write_c_string(")");
6030 // Import a binary expression.
6033 Binary_expression::do_import(Import* imp)
6035 imp->require_c_string("(");
6037 Expression* left = Expression::import_expression(imp);
6040 if (imp->match_c_string(" || "))
6045 else if (imp->match_c_string(" && "))
6047 op = OPERATOR_ANDAND;
6050 else if (imp->match_c_string(" == "))
6055 else if (imp->match_c_string(" != "))
6057 op = OPERATOR_NOTEQ;
6060 else if (imp->match_c_string(" < "))
6065 else if (imp->match_c_string(" <= "))
6070 else if (imp->match_c_string(" > "))
6075 else if (imp->match_c_string(" >= "))
6080 else if (imp->match_c_string(" + "))
6085 else if (imp->match_c_string(" - "))
6087 op = OPERATOR_MINUS;
6090 else if (imp->match_c_string(" | "))
6095 else if (imp->match_c_string(" ^ "))
6100 else if (imp->match_c_string(" * "))
6105 else if (imp->match_c_string(" / "))
6110 else if (imp->match_c_string(" % "))
6115 else if (imp->match_c_string(" << "))
6117 op = OPERATOR_LSHIFT;
6120 else if (imp->match_c_string(" >> "))
6122 op = OPERATOR_RSHIFT;
6125 else if (imp->match_c_string(" & "))
6130 else if (imp->match_c_string(" &^ "))
6132 op = OPERATOR_BITCLEAR;
6137 error_at(imp->location(), "unrecognized binary operator");
6138 return Expression::make_error(imp->location());
6141 Expression* right = Expression::import_expression(imp);
6143 imp->require_c_string(")");
6145 return Expression::make_binary(op, left, right, imp->location());
6148 // Make a binary expression.
6151 Expression::make_binary(Operator op, Expression* left, Expression* right,
6152 source_location location)
6154 return new Binary_expression(op, left, right, location);
6157 // Implement a comparison.
6160 Expression::comparison_tree(Translate_context* context, Operator op,
6161 Type* left_type, tree left_tree,
6162 Type* right_type, tree right_tree,
6163 source_location location)
6165 enum tree_code code;
6171 case OPERATOR_NOTEQ:
6190 if (left_type->is_string_type() && right_type->is_string_type())
6192 tree string_type = Type::make_string_type()->get_tree(context->gogo());
6193 static tree string_compare_decl;
6194 left_tree = Gogo::call_builtin(&string_compare_decl,
6203 right_tree = build_int_cst_type(integer_type_node, 0);
6205 else if ((left_type->interface_type() != NULL
6206 && right_type->interface_type() == NULL
6207 && !right_type->is_nil_type())
6208 || (left_type->interface_type() == NULL
6209 && !left_type->is_nil_type()
6210 && right_type->interface_type() != NULL))
6212 // Comparing an interface value to a non-interface value.
6213 if (left_type->interface_type() == NULL)
6215 std::swap(left_type, right_type);
6216 std::swap(left_tree, right_tree);
6219 // The right operand is not an interface. We need to take its
6220 // address if it is not a pointer.
6223 if (right_type->points_to() != NULL)
6225 make_tmp = NULL_TREE;
6228 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
6230 make_tmp = NULL_TREE;
6231 arg = build_fold_addr_expr_loc(location, right_tree);
6232 if (DECL_P(right_tree))
6233 TREE_ADDRESSABLE(right_tree) = 1;
6237 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6238 get_name(right_tree));
6239 DECL_IGNORED_P(tmp) = 0;
6240 DECL_INITIAL(tmp) = right_tree;
6241 TREE_ADDRESSABLE(tmp) = 1;
6242 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6243 SET_EXPR_LOCATION(make_tmp, location);
6244 arg = build_fold_addr_expr_loc(location, tmp);
6246 arg = fold_convert_loc(location, ptr_type_node, arg);
6248 tree descriptor = right_type->type_descriptor_pointer(context->gogo());
6250 if (left_type->interface_type()->is_empty())
6252 static tree empty_interface_value_compare_decl;
6253 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6255 "__go_empty_interface_value_compare",
6258 TREE_TYPE(left_tree),
6260 TREE_TYPE(descriptor),
6264 if (left_tree == error_mark_node)
6265 return error_mark_node;
6266 // This can panic if the type is not comparable.
6267 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6271 static tree interface_value_compare_decl;
6272 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6274 "__go_interface_value_compare",
6277 TREE_TYPE(left_tree),
6279 TREE_TYPE(descriptor),
6283 if (left_tree == error_mark_node)
6284 return error_mark_node;
6285 // This can panic if the type is not comparable.
6286 TREE_NOTHROW(interface_value_compare_decl) = 0;
6288 right_tree = build_int_cst_type(integer_type_node, 0);
6290 if (make_tmp != NULL_TREE)
6291 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6294 else if (left_type->interface_type() != NULL
6295 && right_type->interface_type() != NULL)
6297 if (left_type->interface_type()->is_empty()
6298 && right_type->interface_type()->is_empty())
6300 static tree empty_interface_compare_decl;
6301 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6303 "__go_empty_interface_compare",
6306 TREE_TYPE(left_tree),
6308 TREE_TYPE(right_tree),
6310 if (left_tree == error_mark_node)
6311 return error_mark_node;
6312 // This can panic if the type is uncomparable.
6313 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6315 else if (!left_type->interface_type()->is_empty()
6316 && !right_type->interface_type()->is_empty())
6318 static tree interface_compare_decl;
6319 left_tree = Gogo::call_builtin(&interface_compare_decl,
6321 "__go_interface_compare",
6324 TREE_TYPE(left_tree),
6326 TREE_TYPE(right_tree),
6328 if (left_tree == error_mark_node)
6329 return error_mark_node;
6330 // This can panic if the type is uncomparable.
6331 TREE_NOTHROW(interface_compare_decl) = 0;
6335 if (left_type->interface_type()->is_empty())
6337 gcc_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6338 std::swap(left_type, right_type);
6339 std::swap(left_tree, right_tree);
6341 gcc_assert(!left_type->interface_type()->is_empty());
6342 gcc_assert(right_type->interface_type()->is_empty());
6343 static tree interface_empty_compare_decl;
6344 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
6346 "__go_interface_empty_compare",
6349 TREE_TYPE(left_tree),
6351 TREE_TYPE(right_tree),
6353 if (left_tree == error_mark_node)
6354 return error_mark_node;
6355 // This can panic if the type is uncomparable.
6356 TREE_NOTHROW(interface_empty_compare_decl) = 0;
6359 right_tree = build_int_cst_type(integer_type_node, 0);
6362 if (left_type->is_nil_type()
6363 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6365 std::swap(left_type, right_type);
6366 std::swap(left_tree, right_tree);
6369 if (right_type->is_nil_type())
6371 if (left_type->array_type() != NULL
6372 && left_type->array_type()->length() == NULL)
6374 Array_type* at = left_type->array_type();
6375 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6376 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6378 else if (left_type->interface_type() != NULL)
6380 // An interface is nil if the first field is nil.
6381 tree left_type_tree = TREE_TYPE(left_tree);
6382 gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6383 tree field = TYPE_FIELDS(left_type_tree);
6384 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6386 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6390 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6391 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6395 if (left_tree == error_mark_node || right_tree == error_mark_node)
6396 return error_mark_node;
6398 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6399 if (CAN_HAVE_LOCATION_P(ret))
6400 SET_EXPR_LOCATION(ret, location);
6404 // Class Bound_method_expression.
6409 Bound_method_expression::do_traverse(Traverse* traverse)
6411 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
6412 return TRAVERSE_EXIT;
6413 return Expression::traverse(&this->method_, traverse);
6416 // Return the type of a bound method expression. The type of this
6417 // object is really the type of the method with no receiver. We
6418 // should be able to get away with just returning the type of the
6422 Bound_method_expression::do_type()
6424 return this->method_->type();
6427 // Determine the types of a method expression.
6430 Bound_method_expression::do_determine_type(const Type_context*)
6432 this->method_->determine_type_no_context();
6433 Type* mtype = this->method_->type();
6434 Function_type* fntype = mtype == NULL ? NULL : mtype->function_type();
6435 if (fntype == NULL || !fntype->is_method())
6436 this->expr_->determine_type_no_context();
6439 Type_context subcontext(fntype->receiver()->type(), false);
6440 this->expr_->determine_type(&subcontext);
6444 // Check the types of a method expression.
6447 Bound_method_expression::do_check_types(Gogo*)
6449 Type* type = this->method_->type()->deref();
6451 || type->function_type() == NULL
6452 || !type->function_type()->is_method())
6453 this->report_error(_("object is not a method"));
6456 Type* rtype = type->function_type()->receiver()->type()->deref();
6457 Type* etype = (this->expr_type_ != NULL
6459 : this->expr_->type());
6460 etype = etype->deref();
6461 if (!Type::are_identical(rtype, etype, true, NULL))
6462 this->report_error(_("method type does not match object type"));
6466 // Get the tree for a method expression. There is no standard tree
6467 // representation for this. The only places it may currently be used
6468 // are in a Call_expression or a Go_statement, which will take it
6469 // apart directly. So this has nothing to do at present.
6472 Bound_method_expression::do_get_tree(Translate_context*)
6474 error_at(this->location(), "reference to method other than calling it");
6475 return error_mark_node;
6478 // Make a method expression.
6480 Bound_method_expression*
6481 Expression::make_bound_method(Expression* expr, Expression* method,
6482 source_location location)
6484 return new Bound_method_expression(expr, method, location);
6487 // Class Builtin_call_expression. This is used for a call to a
6488 // builtin function.
6490 class Builtin_call_expression : public Call_expression
6493 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6494 bool is_varargs, source_location location);
6497 // This overrides Call_expression::do_lower.
6499 do_lower(Gogo*, Named_object*, int);
6502 do_is_constant() const;
6505 do_integer_constant_value(bool, mpz_t, Type**) const;
6508 do_float_constant_value(mpfr_t, Type**) const;
6511 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6517 do_determine_type(const Type_context*);
6520 do_check_types(Gogo*);
6525 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6526 this->args()->copy(),
6532 do_get_tree(Translate_context*);
6535 do_export(Export*) const;
6538 do_is_recover_call() const;
6541 do_set_recover_arg(Expression*);
6544 // The builtin functions.
6545 enum Builtin_function_code
6549 // Predeclared builtin functions.
6566 // Builtin functions from the unsafe package.
6579 real_imag_type(Type*);
6582 complex_type(Type*);
6584 // A pointer back to the general IR structure. This avoids a global
6585 // variable, or passing it around everywhere.
6587 // The builtin function being called.
6588 Builtin_function_code code_;
6589 // Used to stop endless loops when the length of an array uses len
6590 // or cap of the array itself.
6594 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6596 Expression_list* args,
6598 source_location location)
6599 : Call_expression(fn, args, is_varargs, location),
6600 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
6602 Func_expression* fnexp = this->fn()->func_expression();
6603 gcc_assert(fnexp != NULL);
6604 const std::string& name(fnexp->named_object()->name());
6605 if (name == "append")
6606 this->code_ = BUILTIN_APPEND;
6607 else if (name == "cap")
6608 this->code_ = BUILTIN_CAP;
6609 else if (name == "close")
6610 this->code_ = BUILTIN_CLOSE;
6611 else if (name == "closed")
6612 this->code_ = BUILTIN_CLOSED;
6613 else if (name == "complex")
6614 this->code_ = BUILTIN_COMPLEX;
6615 else if (name == "copy")
6616 this->code_ = BUILTIN_COPY;
6617 else if (name == "imag")
6618 this->code_ = BUILTIN_IMAG;
6619 else if (name == "len")
6620 this->code_ = BUILTIN_LEN;
6621 else if (name == "make")
6622 this->code_ = BUILTIN_MAKE;
6623 else if (name == "new")
6624 this->code_ = BUILTIN_NEW;
6625 else if (name == "panic")
6626 this->code_ = BUILTIN_PANIC;
6627 else if (name == "print")
6628 this->code_ = BUILTIN_PRINT;
6629 else if (name == "println")
6630 this->code_ = BUILTIN_PRINTLN;
6631 else if (name == "real")
6632 this->code_ = BUILTIN_REAL;
6633 else if (name == "recover")
6634 this->code_ = BUILTIN_RECOVER;
6635 else if (name == "Alignof")
6636 this->code_ = BUILTIN_ALIGNOF;
6637 else if (name == "Offsetof")
6638 this->code_ = BUILTIN_OFFSETOF;
6639 else if (name == "Sizeof")
6640 this->code_ = BUILTIN_SIZEOF;
6645 // Return whether this is a call to recover. This is a virtual
6646 // function called from the parent class.
6649 Builtin_call_expression::do_is_recover_call() const
6651 if (this->classification() == EXPRESSION_ERROR)
6653 return this->code_ == BUILTIN_RECOVER;
6656 // Set the argument for a call to recover.
6659 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6661 const Expression_list* args = this->args();
6662 gcc_assert(args == NULL || args->empty());
6663 Expression_list* new_args = new Expression_list();
6664 new_args->push_back(arg);
6665 this->set_args(new_args);
6668 // A traversal class which looks for a call expression.
6670 class Find_call_expression : public Traverse
6673 Find_call_expression()
6674 : Traverse(traverse_expressions),
6679 expression(Expression**);
6683 { return this->found_; }
6690 Find_call_expression::expression(Expression** pexpr)
6692 if ((*pexpr)->call_expression() != NULL)
6694 this->found_ = true;
6695 return TRAVERSE_EXIT;
6697 return TRAVERSE_CONTINUE;
6700 // Lower a builtin call expression. This turns new and make into
6701 // specific expressions. We also convert to a constant if we can.
6704 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int)
6706 if (this->code_ == BUILTIN_NEW)
6708 const Expression_list* args = this->args();
6709 if (args == NULL || args->size() < 1)
6710 this->report_error(_("not enough arguments"));
6711 else if (args->size() > 1)
6712 this->report_error(_("too many arguments"));
6715 Expression* arg = args->front();
6716 if (!arg->is_type_expression())
6718 error_at(arg->location(), "expected type");
6719 this->set_is_error();
6722 return Expression::make_allocation(arg->type(), this->location());
6725 else if (this->code_ == BUILTIN_MAKE)
6727 const Expression_list* args = this->args();
6728 if (args == NULL || args->size() < 1)
6729 this->report_error(_("not enough arguments"));
6732 Expression* arg = args->front();
6733 if (!arg->is_type_expression())
6735 error_at(arg->location(), "expected type");
6736 this->set_is_error();
6740 Expression_list* newargs;
6741 if (args->size() == 1)
6745 newargs = new Expression_list();
6746 Expression_list::const_iterator p = args->begin();
6748 for (; p != args->end(); ++p)
6749 newargs->push_back(*p);
6751 return Expression::make_make(arg->type(), newargs,
6756 else if (this->is_constant())
6758 // We can only lower len and cap if there are no function calls
6759 // in the arguments. Otherwise we have to make the call.
6760 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6762 Expression* arg = this->one_arg();
6763 if (!arg->is_constant())
6765 Find_call_expression find_call;
6766 Expression::traverse(&arg, &find_call);
6767 if (find_call.found())
6775 if (this->integer_constant_value(true, ival, &type))
6777 Expression* ret = Expression::make_integer(&ival, type,
6786 if (this->float_constant_value(rval, &type))
6788 Expression* ret = Expression::make_float(&rval, type,
6796 if (this->complex_constant_value(rval, imag, &type))
6798 Expression* ret = Expression::make_complex(&rval, &imag, type,
6807 else if (this->code_ == BUILTIN_RECOVER)
6809 if (function != NULL)
6810 function->func_value()->set_calls_recover();
6813 // Calling recover outside of a function always returns the
6814 // nil empty interface.
6815 Type* eface = Type::make_interface_type(NULL, this->location());
6816 return Expression::make_cast(eface,
6817 Expression::make_nil(this->location()),
6821 else if (this->code_ == BUILTIN_APPEND)
6823 // Lower the varargs.
6824 const Expression_list* args = this->args();
6825 if (args == NULL || args->empty())
6827 Type* slice_type = args->front()->type();
6828 if (!slice_type->is_open_array_type())
6830 error_at(args->front()->location(), "argument 1 must be a slice");
6831 this->set_is_error();
6834 return this->lower_varargs(gogo, function, slice_type, 2);
6840 // Return the type of the real or imag functions, given the type of
6841 // the argument. We need to map complex to float, complex64 to
6842 // float32, and complex128 to float64, so it has to be done by name.
6843 // This returns NULL if it can't figure out the type.
6846 Builtin_call_expression::real_imag_type(Type* arg_type)
6848 if (arg_type == NULL || arg_type->is_abstract())
6850 Named_type* nt = arg_type->named_type();
6853 while (nt->real_type()->named_type() != NULL)
6854 nt = nt->real_type()->named_type();
6855 if (nt->name() == "complex64")
6856 return Type::lookup_float_type("float32");
6857 else if (nt->name() == "complex128")
6858 return Type::lookup_float_type("float64");
6863 // Return the type of the complex function, given the type of one of the
6864 // argments. Like real_imag_type, we have to map by name.
6867 Builtin_call_expression::complex_type(Type* arg_type)
6869 if (arg_type == NULL || arg_type->is_abstract())
6871 Named_type* nt = arg_type->named_type();
6874 while (nt->real_type()->named_type() != NULL)
6875 nt = nt->real_type()->named_type();
6876 if (nt->name() == "float32")
6877 return Type::lookup_complex_type("complex64");
6878 else if (nt->name() == "float64")
6879 return Type::lookup_complex_type("complex128");
6884 // Return a single argument, or NULL if there isn't one.
6887 Builtin_call_expression::one_arg() const
6889 const Expression_list* args = this->args();
6890 if (args->size() != 1)
6892 return args->front();
6895 // Return whether this is constant: len of a string, or len or cap of
6896 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6899 Builtin_call_expression::do_is_constant() const
6901 switch (this->code_)
6909 Expression* arg = this->one_arg();
6912 Type* arg_type = arg->type();
6914 if (arg_type->points_to() != NULL
6915 && arg_type->points_to()->array_type() != NULL
6916 && !arg_type->points_to()->is_open_array_type())
6917 arg_type = arg_type->points_to();
6919 if (arg_type->array_type() != NULL
6920 && arg_type->array_type()->length() != NULL)
6923 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6926 bool ret = arg->is_constant();
6927 this->seen_ = false;
6933 case BUILTIN_SIZEOF:
6934 case BUILTIN_ALIGNOF:
6935 return this->one_arg() != NULL;
6937 case BUILTIN_OFFSETOF:
6939 Expression* arg = this->one_arg();
6942 return arg->field_reference_expression() != NULL;
6945 case BUILTIN_COMPLEX:
6947 const Expression_list* args = this->args();
6948 if (args != NULL && args->size() == 2)
6949 return args->front()->is_constant() && args->back()->is_constant();
6956 Expression* arg = this->one_arg();
6957 return arg != NULL && arg->is_constant();
6967 // Return an integer constant value if possible.
6970 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
6974 if (this->code_ == BUILTIN_LEN
6975 || this->code_ == BUILTIN_CAP)
6977 Expression* arg = this->one_arg();
6980 Type* arg_type = arg->type();
6982 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6985 if (arg->string_constant_value(&sval))
6987 mpz_set_ui(val, sval.length());
6988 *ptype = Type::lookup_integer_type("int");
6993 if (arg_type->points_to() != NULL
6994 && arg_type->points_to()->array_type() != NULL
6995 && !arg_type->points_to()->is_open_array_type())
6996 arg_type = arg_type->points_to();
6998 if (arg_type->array_type() != NULL
6999 && arg_type->array_type()->length() != NULL)
7003 Expression* e = arg_type->array_type()->length();
7005 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
7006 this->seen_ = false;
7009 *ptype = Type::lookup_integer_type("int");
7014 else if (this->code_ == BUILTIN_SIZEOF
7015 || this->code_ == BUILTIN_ALIGNOF)
7017 Expression* arg = this->one_arg();
7020 Type* arg_type = arg->type();
7021 if (arg_type->is_error_type() || arg_type->is_undefined())
7023 if (arg_type->is_abstract())
7025 if (arg_type->named_type() != NULL)
7026 arg_type->named_type()->convert(this->gogo_);
7027 tree arg_type_tree = arg_type->get_tree(this->gogo_);
7028 if (arg_type_tree == error_mark_node)
7030 unsigned long val_long;
7031 if (this->code_ == BUILTIN_SIZEOF)
7033 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
7034 gcc_assert(TREE_CODE(type_size) == INTEGER_CST);
7035 if (TREE_INT_CST_HIGH(type_size) != 0)
7037 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
7038 val_long = static_cast<unsigned long>(val_wide);
7039 if (val_long != val_wide)
7042 else if (this->code_ == BUILTIN_ALIGNOF)
7044 if (arg->field_reference_expression() == NULL)
7045 val_long = go_type_alignment(arg_type_tree);
7048 // Calling unsafe.Alignof(s.f) returns the alignment of
7049 // the type of f when it is used as a field in a struct.
7050 val_long = go_field_alignment(arg_type_tree);
7055 mpz_set_ui(val, val_long);
7059 else if (this->code_ == BUILTIN_OFFSETOF)
7061 Expression* arg = this->one_arg();
7064 Field_reference_expression* farg = arg->field_reference_expression();
7067 Expression* struct_expr = farg->expr();
7068 Type* st = struct_expr->type();
7069 if (st->struct_type() == NULL)
7071 if (st->named_type() != NULL)
7072 st->named_type()->convert(this->gogo_);
7073 tree struct_tree = st->get_tree(this->gogo_);
7074 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
7075 tree field = TYPE_FIELDS(struct_tree);
7076 for (unsigned int index = farg->field_index(); index > 0; --index)
7078 field = DECL_CHAIN(field);
7079 gcc_assert(field != NULL_TREE);
7081 HOST_WIDE_INT offset_wide = int_byte_position (field);
7082 if (offset_wide < 0)
7084 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
7085 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
7087 mpz_set_ui(val, offset_long);
7093 // Return a floating point constant value if possible.
7096 Builtin_call_expression::do_float_constant_value(mpfr_t val,
7099 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
7101 Expression* arg = this->one_arg();
7112 if (arg->complex_constant_value(real, imag, &type))
7114 if (this->code_ == BUILTIN_REAL)
7115 mpfr_set(val, real, GMP_RNDN);
7117 mpfr_set(val, imag, GMP_RNDN);
7118 *ptype = Builtin_call_expression::real_imag_type(type);
7130 // Return a complex constant value if possible.
7133 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
7136 if (this->code_ == BUILTIN_COMPLEX)
7138 const Expression_list* args = this->args();
7139 if (args == NULL || args->size() != 2)
7145 if (!args->front()->float_constant_value(r, &rtype))
7156 if (args->back()->float_constant_value(i, &itype)
7157 && Type::are_identical(rtype, itype, false, NULL))
7159 mpfr_set(real, r, GMP_RNDN);
7160 mpfr_set(imag, i, GMP_RNDN);
7161 *ptype = Builtin_call_expression::complex_type(rtype);
7177 Builtin_call_expression::do_type()
7179 switch (this->code_)
7181 case BUILTIN_INVALID:
7188 const Expression_list* args = this->args();
7189 if (args == NULL || args->empty())
7190 return Type::make_error_type();
7191 return Type::make_pointer_type(args->front()->type());
7197 case BUILTIN_ALIGNOF:
7198 case BUILTIN_OFFSETOF:
7199 case BUILTIN_SIZEOF:
7200 return Type::lookup_integer_type("int");
7205 case BUILTIN_PRINTLN:
7206 return Type::make_void_type();
7208 case BUILTIN_CLOSED:
7209 return Type::lookup_bool_type();
7211 case BUILTIN_RECOVER:
7212 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
7214 case BUILTIN_APPEND:
7216 const Expression_list* args = this->args();
7217 if (args == NULL || args->empty())
7218 return Type::make_error_type();
7219 return args->front()->type();
7225 Expression* arg = this->one_arg();
7227 return Type::make_error_type();
7228 Type* t = arg->type();
7229 if (t->is_abstract())
7230 t = t->make_non_abstract_type();
7231 t = Builtin_call_expression::real_imag_type(t);
7233 t = Type::make_error_type();
7237 case BUILTIN_COMPLEX:
7239 const Expression_list* args = this->args();
7240 if (args == NULL || args->size() != 2)
7241 return Type::make_error_type();
7242 Type* t = args->front()->type();
7243 if (t->is_abstract())
7245 t = args->back()->type();
7246 if (t->is_abstract())
7247 t = t->make_non_abstract_type();
7249 t = Builtin_call_expression::complex_type(t);
7251 t = Type::make_error_type();
7257 // Determine the type.
7260 Builtin_call_expression::do_determine_type(const Type_context* context)
7262 if (!this->determining_types())
7265 this->fn()->determine_type_no_context();
7267 const Expression_list* args = this->args();
7270 Type* arg_type = NULL;
7271 switch (this->code_)
7274 case BUILTIN_PRINTLN:
7275 // Do not force a large integer constant to "int".
7281 arg_type = Builtin_call_expression::complex_type(context->type);
7285 case BUILTIN_COMPLEX:
7287 // For the complex function the type of one operand can
7288 // determine the type of the other, as in a binary expression.
7289 arg_type = Builtin_call_expression::real_imag_type(context->type);
7290 if (args != NULL && args->size() == 2)
7292 Type* t1 = args->front()->type();
7293 Type* t2 = args->front()->type();
7294 if (!t1->is_abstract())
7296 else if (!t2->is_abstract())
7310 for (Expression_list::const_iterator pa = args->begin();
7314 Type_context subcontext;
7315 subcontext.type = arg_type;
7319 // We want to print large constants, we so can't just
7320 // use the appropriate nonabstract type. Use uint64 for
7321 // an integer if we know it is nonnegative, otherwise
7322 // use int64 for a integer, otherwise use float64 for a
7323 // float or complex128 for a complex.
7324 Type* want_type = NULL;
7325 Type* atype = (*pa)->type();
7326 if (atype->is_abstract())
7328 if (atype->integer_type() != NULL)
7333 if (this->integer_constant_value(true, val, &dummy)
7334 && mpz_sgn(val) >= 0)
7335 want_type = Type::lookup_integer_type("uint64");
7337 want_type = Type::lookup_integer_type("int64");
7340 else if (atype->float_type() != NULL)
7341 want_type = Type::lookup_float_type("float64");
7342 else if (atype->complex_type() != NULL)
7343 want_type = Type::lookup_complex_type("complex128");
7344 else if (atype->is_abstract_string_type())
7345 want_type = Type::lookup_string_type();
7346 else if (atype->is_abstract_boolean_type())
7347 want_type = Type::lookup_bool_type();
7350 subcontext.type = want_type;
7354 (*pa)->determine_type(&subcontext);
7359 // If there is exactly one argument, return true. Otherwise give an
7360 // error message and return false.
7363 Builtin_call_expression::check_one_arg()
7365 const Expression_list* args = this->args();
7366 if (args == NULL || args->size() < 1)
7368 this->report_error(_("not enough arguments"));
7371 else if (args->size() > 1)
7373 this->report_error(_("too many arguments"));
7376 if (args->front()->is_error_expression()
7377 || args->front()->type()->is_error_type()
7378 || args->front()->type()->is_undefined())
7380 this->set_is_error();
7386 // Check argument types for a builtin function.
7389 Builtin_call_expression::do_check_types(Gogo*)
7391 switch (this->code_)
7393 case BUILTIN_INVALID:
7401 // The single argument may be either a string or an array or a
7402 // map or a channel, or a pointer to a closed array.
7403 if (this->check_one_arg())
7405 Type* arg_type = this->one_arg()->type();
7406 if (arg_type->points_to() != NULL
7407 && arg_type->points_to()->array_type() != NULL
7408 && !arg_type->points_to()->is_open_array_type())
7409 arg_type = arg_type->points_to();
7410 if (this->code_ == BUILTIN_CAP)
7412 if (!arg_type->is_error_type()
7413 && arg_type->array_type() == NULL
7414 && arg_type->channel_type() == NULL)
7415 this->report_error(_("argument must be array or slice "
7420 if (!arg_type->is_error_type()
7421 && !arg_type->is_string_type()
7422 && arg_type->array_type() == NULL
7423 && arg_type->map_type() == NULL
7424 && arg_type->channel_type() == NULL)
7425 this->report_error(_("argument must be string or "
7426 "array or slice or map or channel"));
7433 case BUILTIN_PRINTLN:
7435 const Expression_list* args = this->args();
7438 if (this->code_ == BUILTIN_PRINT)
7439 warning_at(this->location(), 0,
7440 "no arguments for builtin function %<%s%>",
7441 (this->code_ == BUILTIN_PRINT
7447 for (Expression_list::const_iterator p = args->begin();
7451 Type* type = (*p)->type();
7452 if (type->is_error_type()
7453 || type->is_string_type()
7454 || type->integer_type() != NULL
7455 || type->float_type() != NULL
7456 || type->complex_type() != NULL
7457 || type->is_boolean_type()
7458 || type->points_to() != NULL
7459 || type->interface_type() != NULL
7460 || type->channel_type() != NULL
7461 || type->map_type() != NULL
7462 || type->function_type() != NULL
7463 || type->is_open_array_type())
7466 this->report_error(_("unsupported argument type to "
7467 "builtin function"));
7474 case BUILTIN_CLOSED:
7475 if (this->check_one_arg())
7477 if (this->one_arg()->type()->channel_type() == NULL)
7478 this->report_error(_("argument must be channel"));
7483 case BUILTIN_SIZEOF:
7484 case BUILTIN_ALIGNOF:
7485 this->check_one_arg();
7488 case BUILTIN_RECOVER:
7489 if (this->args() != NULL && !this->args()->empty())
7490 this->report_error(_("too many arguments"));
7493 case BUILTIN_OFFSETOF:
7494 if (this->check_one_arg())
7496 Expression* arg = this->one_arg();
7497 if (arg->field_reference_expression() == NULL)
7498 this->report_error(_("argument must be a field reference"));
7504 const Expression_list* args = this->args();
7505 if (args == NULL || args->size() < 2)
7507 this->report_error(_("not enough arguments"));
7510 else if (args->size() > 2)
7512 this->report_error(_("too many arguments"));
7515 Type* arg1_type = args->front()->type();
7516 Type* arg2_type = args->back()->type();
7517 if (arg1_type->is_error_type() || arg2_type->is_error_type())
7521 if (arg1_type->is_open_array_type())
7522 e1 = arg1_type->array_type()->element_type();
7525 this->report_error(_("left argument must be a slice"));
7530 if (arg2_type->is_open_array_type())
7531 e2 = arg2_type->array_type()->element_type();
7532 else if (arg2_type->is_string_type())
7533 e2 = Type::lookup_integer_type("uint8");
7536 this->report_error(_("right argument must be a slice or a string"));
7540 if (!Type::are_identical(e1, e2, true, NULL))
7541 this->report_error(_("element types must be the same"));
7545 case BUILTIN_APPEND:
7547 const Expression_list* args = this->args();
7548 if (args == NULL || args->size() < 2)
7550 this->report_error(_("not enough arguments"));
7553 if (args->size() > 2)
7555 this->report_error(_("too many arguments"));
7559 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
7563 this->report_error(_("arguments 1 and 2 have different types"));
7566 error_at(this->location(),
7567 "arguments 1 and 2 have different types (%s)",
7569 this->set_is_error();
7577 if (this->check_one_arg())
7579 if (this->one_arg()->type()->complex_type() == NULL)
7580 this->report_error(_("argument must have complex type"));
7584 case BUILTIN_COMPLEX:
7586 const Expression_list* args = this->args();
7587 if (args == NULL || args->size() < 2)
7588 this->report_error(_("not enough arguments"));
7589 else if (args->size() > 2)
7590 this->report_error(_("too many arguments"));
7591 else if (args->front()->is_error_expression()
7592 || args->front()->type()->is_error_type()
7593 || args->back()->is_error_expression()
7594 || args->back()->type()->is_error_type())
7595 this->set_is_error();
7596 else if (!Type::are_identical(args->front()->type(),
7597 args->back()->type(), true, NULL))
7598 this->report_error(_("complex arguments must have identical types"));
7599 else if (args->front()->type()->float_type() == NULL)
7600 this->report_error(_("complex arguments must have "
7601 "floating-point type"));
7610 // Return the tree for a builtin function.
7613 Builtin_call_expression::do_get_tree(Translate_context* context)
7615 Gogo* gogo = context->gogo();
7616 source_location location = this->location();
7617 switch (this->code_)
7619 case BUILTIN_INVALID:
7627 const Expression_list* args = this->args();
7628 gcc_assert(args != NULL && args->size() == 1);
7629 Expression* arg = *args->begin();
7630 Type* arg_type = arg->type();
7634 gcc_assert(saw_errors());
7635 return error_mark_node;
7639 tree arg_tree = arg->get_tree(context);
7641 this->seen_ = false;
7643 if (arg_tree == error_mark_node)
7644 return error_mark_node;
7646 if (arg_type->points_to() != NULL)
7648 arg_type = arg_type->points_to();
7649 gcc_assert(arg_type->array_type() != NULL
7650 && !arg_type->is_open_array_type());
7651 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7652 arg_tree = build_fold_indirect_ref(arg_tree);
7656 if (this->code_ == BUILTIN_LEN)
7658 if (arg_type->is_string_type())
7659 val_tree = String_type::length_tree(gogo, arg_tree);
7660 else if (arg_type->array_type() != NULL)
7664 gcc_assert(saw_errors());
7665 return error_mark_node;
7668 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7669 this->seen_ = false;
7671 else if (arg_type->map_type() != NULL)
7673 static tree map_len_fndecl;
7674 val_tree = Gogo::call_builtin(&map_len_fndecl,
7679 arg_type->get_tree(gogo),
7682 else if (arg_type->channel_type() != NULL)
7684 static tree chan_len_fndecl;
7685 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7690 arg_type->get_tree(gogo),
7698 if (arg_type->array_type() != NULL)
7702 gcc_assert(saw_errors());
7703 return error_mark_node;
7706 val_tree = arg_type->array_type()->capacity_tree(gogo,
7708 this->seen_ = false;
7710 else if (arg_type->channel_type() != NULL)
7712 static tree chan_cap_fndecl;
7713 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7718 arg_type->get_tree(gogo),
7725 if (val_tree == error_mark_node)
7726 return error_mark_node;
7728 tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo);
7729 if (type_tree == TREE_TYPE(val_tree))
7732 return fold(convert_to_integer(type_tree, val_tree));
7736 case BUILTIN_PRINTLN:
7738 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7739 tree stmt_list = NULL_TREE;
7741 const Expression_list* call_args = this->args();
7742 if (call_args != NULL)
7744 for (Expression_list::const_iterator p = call_args->begin();
7745 p != call_args->end();
7748 if (is_ln && p != call_args->begin())
7750 static tree print_space_fndecl;
7751 tree call = Gogo::call_builtin(&print_space_fndecl,
7756 if (call == error_mark_node)
7757 return error_mark_node;
7758 append_to_statement_list(call, &stmt_list);
7761 Type* type = (*p)->type();
7763 tree arg = (*p)->get_tree(context);
7764 if (arg == error_mark_node)
7765 return error_mark_node;
7769 if (type->is_string_type())
7771 static tree print_string_fndecl;
7772 pfndecl = &print_string_fndecl;
7773 fnname = "__go_print_string";
7775 else if (type->integer_type() != NULL
7776 && type->integer_type()->is_unsigned())
7778 static tree print_uint64_fndecl;
7779 pfndecl = &print_uint64_fndecl;
7780 fnname = "__go_print_uint64";
7781 Type* itype = Type::lookup_integer_type("uint64");
7782 arg = fold_convert_loc(location, itype->get_tree(gogo),
7785 else if (type->integer_type() != NULL)
7787 static tree print_int64_fndecl;
7788 pfndecl = &print_int64_fndecl;
7789 fnname = "__go_print_int64";
7790 Type* itype = Type::lookup_integer_type("int64");
7791 arg = fold_convert_loc(location, itype->get_tree(gogo),
7794 else if (type->float_type() != NULL)
7796 static tree print_double_fndecl;
7797 pfndecl = &print_double_fndecl;
7798 fnname = "__go_print_double";
7799 arg = fold_convert_loc(location, double_type_node, arg);
7801 else if (type->complex_type() != NULL)
7803 static tree print_complex_fndecl;
7804 pfndecl = &print_complex_fndecl;
7805 fnname = "__go_print_complex";
7806 arg = fold_convert_loc(location, complex_double_type_node,
7809 else if (type->is_boolean_type())
7811 static tree print_bool_fndecl;
7812 pfndecl = &print_bool_fndecl;
7813 fnname = "__go_print_bool";
7815 else if (type->points_to() != NULL
7816 || type->channel_type() != NULL
7817 || type->map_type() != NULL
7818 || type->function_type() != NULL)
7820 static tree print_pointer_fndecl;
7821 pfndecl = &print_pointer_fndecl;
7822 fnname = "__go_print_pointer";
7823 arg = fold_convert_loc(location, ptr_type_node, arg);
7825 else if (type->interface_type() != NULL)
7827 if (type->interface_type()->is_empty())
7829 static tree print_empty_interface_fndecl;
7830 pfndecl = &print_empty_interface_fndecl;
7831 fnname = "__go_print_empty_interface";
7835 static tree print_interface_fndecl;
7836 pfndecl = &print_interface_fndecl;
7837 fnname = "__go_print_interface";
7840 else if (type->is_open_array_type())
7842 static tree print_slice_fndecl;
7843 pfndecl = &print_slice_fndecl;
7844 fnname = "__go_print_slice";
7849 tree call = Gogo::call_builtin(pfndecl,
7856 if (call == error_mark_node)
7857 return error_mark_node;
7858 append_to_statement_list(call, &stmt_list);
7864 static tree print_nl_fndecl;
7865 tree call = Gogo::call_builtin(&print_nl_fndecl,
7870 if (call == error_mark_node)
7871 return error_mark_node;
7872 append_to_statement_list(call, &stmt_list);
7880 const Expression_list* args = this->args();
7881 gcc_assert(args != NULL && args->size() == 1);
7882 Expression* arg = args->front();
7883 tree arg_tree = arg->get_tree(context);
7884 if (arg_tree == error_mark_node)
7885 return error_mark_node;
7886 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7887 arg_tree = Expression::convert_for_assignment(context, empty,
7889 arg_tree, location);
7890 static tree panic_fndecl;
7891 tree call = Gogo::call_builtin(&panic_fndecl,
7896 TREE_TYPE(arg_tree),
7898 if (call == error_mark_node)
7899 return error_mark_node;
7900 // This function will throw an exception.
7901 TREE_NOTHROW(panic_fndecl) = 0;
7902 // This function will not return.
7903 TREE_THIS_VOLATILE(panic_fndecl) = 1;
7907 case BUILTIN_RECOVER:
7909 // The argument is set when building recover thunks. It's a
7910 // boolean value which is true if we can recover a value now.
7911 const Expression_list* args = this->args();
7912 gcc_assert(args != NULL && args->size() == 1);
7913 Expression* arg = args->front();
7914 tree arg_tree = arg->get_tree(context);
7915 if (arg_tree == error_mark_node)
7916 return error_mark_node;
7918 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7919 tree empty_tree = empty->get_tree(context->gogo());
7921 Type* nil_type = Type::make_nil_type();
7922 Expression* nil = Expression::make_nil(location);
7923 tree nil_tree = nil->get_tree(context);
7924 tree empty_nil_tree = Expression::convert_for_assignment(context,
7930 // We need to handle a deferred call to recover specially,
7931 // because it changes whether it can recover a panic or not.
7932 // See test7 in test/recover1.go.
7934 if (this->is_deferred())
7936 static tree deferred_recover_fndecl;
7937 call = Gogo::call_builtin(&deferred_recover_fndecl,
7939 "__go_deferred_recover",
7945 static tree recover_fndecl;
7946 call = Gogo::call_builtin(&recover_fndecl,
7952 if (call == error_mark_node)
7953 return error_mark_node;
7954 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
7955 call, empty_nil_tree);
7959 case BUILTIN_CLOSED:
7961 const Expression_list* args = this->args();
7962 gcc_assert(args != NULL && args->size() == 1);
7963 Expression* arg = args->front();
7964 tree arg_tree = arg->get_tree(context);
7965 if (arg_tree == error_mark_node)
7966 return error_mark_node;
7967 if (this->code_ == BUILTIN_CLOSE)
7969 static tree close_fndecl;
7970 return Gogo::call_builtin(&close_fndecl,
7972 "__go_builtin_close",
7975 TREE_TYPE(arg_tree),
7980 static tree closed_fndecl;
7981 return Gogo::call_builtin(&closed_fndecl,
7983 "__go_builtin_closed",
7986 TREE_TYPE(arg_tree),
7991 case BUILTIN_SIZEOF:
7992 case BUILTIN_OFFSETOF:
7993 case BUILTIN_ALIGNOF:
7998 bool b = this->integer_constant_value(true, val, &dummy);
8001 gcc_assert(saw_errors());
8002 return error_mark_node;
8004 tree type = Type::lookup_integer_type("int")->get_tree(gogo);
8005 tree ret = Expression::integer_constant_tree(val, type);
8012 const Expression_list* args = this->args();
8013 gcc_assert(args != NULL && args->size() == 2);
8014 Expression* arg1 = args->front();
8015 Expression* arg2 = args->back();
8017 tree arg1_tree = arg1->get_tree(context);
8018 tree arg2_tree = arg2->get_tree(context);
8019 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8020 return error_mark_node;
8022 Type* arg1_type = arg1->type();
8023 Array_type* at = arg1_type->array_type();
8024 arg1_tree = save_expr(arg1_tree);
8025 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
8026 tree arg1_len = at->length_tree(gogo, arg1_tree);
8027 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
8028 return error_mark_node;
8030 Type* arg2_type = arg2->type();
8033 if (arg2_type->is_open_array_type())
8035 at = arg2_type->array_type();
8036 arg2_tree = save_expr(arg2_tree);
8037 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8038 arg2_len = at->length_tree(gogo, arg2_tree);
8042 arg2_tree = save_expr(arg2_tree);
8043 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8044 arg2_len = String_type::length_tree(gogo, arg2_tree);
8046 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8047 return error_mark_node;
8049 arg1_len = save_expr(arg1_len);
8050 arg2_len = save_expr(arg2_len);
8051 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
8052 fold_build2_loc(location, LT_EXPR,
8054 arg1_len, arg2_len),
8055 arg1_len, arg2_len);
8056 len = save_expr(len);
8058 Type* element_type = at->element_type();
8059 tree element_type_tree = element_type->get_tree(gogo);
8060 if (element_type_tree == error_mark_node)
8061 return error_mark_node;
8062 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8063 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
8065 bytecount = fold_build2_loc(location, MULT_EXPR,
8066 TREE_TYPE(element_size),
8067 bytecount, element_size);
8068 bytecount = fold_convert_loc(location, size_type_node, bytecount);
8070 arg1_val = fold_convert_loc(location, ptr_type_node, arg1_val);
8071 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8073 static tree copy_fndecl;
8074 tree call = Gogo::call_builtin(©_fndecl,
8085 if (call == error_mark_node)
8086 return error_mark_node;
8088 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
8092 case BUILTIN_APPEND:
8094 const Expression_list* args = this->args();
8095 gcc_assert(args != NULL && args->size() == 2);
8096 Expression* arg1 = args->front();
8097 Expression* arg2 = args->back();
8099 tree arg1_tree = arg1->get_tree(context);
8100 tree arg2_tree = arg2->get_tree(context);
8101 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8102 return error_mark_node;
8104 Array_type* at = arg1->type()->array_type();
8105 Type* element_type = at->element_type();
8107 arg2_tree = Expression::convert_for_assignment(context, at,
8111 if (arg2_tree == error_mark_node)
8112 return error_mark_node;
8114 arg2_tree = save_expr(arg2_tree);
8115 tree arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8116 tree arg2_len = at->length_tree(gogo, arg2_tree);
8117 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8118 return error_mark_node;
8119 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8120 arg2_len = fold_convert_loc(location, size_type_node, arg2_len);
8122 tree element_type_tree = element_type->get_tree(gogo);
8123 if (element_type_tree == error_mark_node)
8124 return error_mark_node;
8125 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8126 element_size = fold_convert_loc(location, size_type_node,
8129 // We rebuild the decl each time since the slice types may
8131 tree append_fndecl = NULL_TREE;
8132 return Gogo::call_builtin(&append_fndecl,
8136 TREE_TYPE(arg1_tree),
8137 TREE_TYPE(arg1_tree),
8150 const Expression_list* args = this->args();
8151 gcc_assert(args != NULL && args->size() == 1);
8152 Expression* arg = args->front();
8153 tree arg_tree = arg->get_tree(context);
8154 if (arg_tree == error_mark_node)
8155 return error_mark_node;
8156 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
8157 if (this->code_ == BUILTIN_REAL)
8158 return fold_build1_loc(location, REALPART_EXPR,
8159 TREE_TYPE(TREE_TYPE(arg_tree)),
8162 return fold_build1_loc(location, IMAGPART_EXPR,
8163 TREE_TYPE(TREE_TYPE(arg_tree)),
8167 case BUILTIN_COMPLEX:
8169 const Expression_list* args = this->args();
8170 gcc_assert(args != NULL && args->size() == 2);
8171 tree r = args->front()->get_tree(context);
8172 tree i = args->back()->get_tree(context);
8173 if (r == error_mark_node || i == error_mark_node)
8174 return error_mark_node;
8175 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
8176 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
8177 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8178 return fold_build2_loc(location, COMPLEX_EXPR,
8179 build_complex_type(TREE_TYPE(r)),
8188 // We have to support exporting a builtin call expression, because
8189 // code can set a constant to the result of a builtin expression.
8192 Builtin_call_expression::do_export(Export* exp) const
8199 if (this->integer_constant_value(true, val, &dummy))
8201 Integer_expression::export_integer(exp, val);
8210 if (this->float_constant_value(fval, &dummy))
8212 Float_expression::export_float(exp, fval);
8224 if (this->complex_constant_value(real, imag, &dummy))
8226 Complex_expression::export_complex(exp, real, imag);
8235 error_at(this->location(), "value is not constant");
8239 // A trailing space lets us reliably identify the end of the number.
8240 exp->write_c_string(" ");
8243 // Class Call_expression.
8248 Call_expression::do_traverse(Traverse* traverse)
8250 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8251 return TRAVERSE_EXIT;
8252 if (this->args_ != NULL)
8254 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8255 return TRAVERSE_EXIT;
8257 return TRAVERSE_CONTINUE;
8260 // Lower a call statement.
8263 Call_expression::do_lower(Gogo* gogo, Named_object* function, int)
8265 // A type case can look like a function call.
8266 if (this->fn_->is_type_expression()
8267 && this->args_ != NULL
8268 && this->args_->size() == 1)
8269 return Expression::make_cast(this->fn_->type(), this->args_->front(),
8272 // Recognize a call to a builtin function.
8273 Func_expression* fne = this->fn_->func_expression();
8275 && fne->named_object()->is_function_declaration()
8276 && fne->named_object()->func_declaration_value()->type()->is_builtin())
8277 return new Builtin_call_expression(gogo, this->fn_, this->args_,
8278 this->is_varargs_, this->location());
8280 // Handle an argument which is a call to a function which returns
8281 // multiple results.
8282 if (this->args_ != NULL
8283 && this->args_->size() == 1
8284 && this->args_->front()->call_expression() != NULL
8285 && this->fn_->type()->function_type() != NULL)
8287 Function_type* fntype = this->fn_->type()->function_type();
8288 size_t rc = this->args_->front()->call_expression()->result_count();
8290 && fntype->parameters() != NULL
8291 && (fntype->parameters()->size() == rc
8292 || (fntype->is_varargs()
8293 && fntype->parameters()->size() - 1 <= rc)))
8295 Call_expression* call = this->args_->front()->call_expression();
8296 Expression_list* args = new Expression_list;
8297 for (size_t i = 0; i < rc; ++i)
8298 args->push_back(Expression::make_call_result(call, i));
8299 // We can't return a new call expression here, because this
8300 // one may be referenced by Call_result expressions. We
8301 // also can't delete the old arguments, because we may still
8302 // traverse them somewhere up the call stack. FIXME.
8307 // Handle a call to a varargs function by packaging up the extra
8309 if (this->fn_->type()->function_type() != NULL
8310 && this->fn_->type()->function_type()->is_varargs())
8312 Function_type* fntype = this->fn_->type()->function_type();
8313 const Typed_identifier_list* parameters = fntype->parameters();
8314 gcc_assert(parameters != NULL && !parameters->empty());
8315 Type* varargs_type = parameters->back().type();
8316 return this->lower_varargs(gogo, function, varargs_type,
8317 parameters->size());
8323 // Lower a call to a varargs function. FUNCTION is the function in
8324 // which the call occurs--it's not the function we are calling.
8325 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8326 // PARAM_COUNT is the number of parameters of the function we are
8327 // calling; the last of these parameters will be the varargs
8331 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
8332 Type* varargs_type, size_t param_count)
8334 if (this->varargs_are_lowered_)
8337 source_location loc = this->location();
8339 gcc_assert(param_count > 0);
8340 gcc_assert(varargs_type->is_open_array_type());
8342 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8343 if (arg_count < param_count - 1)
8345 // Not enough arguments; will be caught in check_types.
8349 Expression_list* old_args = this->args_;
8350 Expression_list* new_args = new Expression_list();
8351 bool push_empty_arg = false;
8352 if (old_args == NULL || old_args->empty())
8354 gcc_assert(param_count == 1);
8355 push_empty_arg = true;
8359 Expression_list::const_iterator pa;
8361 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
8363 if (static_cast<size_t>(i) == param_count)
8365 new_args->push_back(*pa);
8368 // We have reached the varargs parameter.
8370 bool issued_error = false;
8371 if (pa == old_args->end())
8372 push_empty_arg = true;
8373 else if (pa + 1 == old_args->end() && this->is_varargs_)
8374 new_args->push_back(*pa);
8375 else if (this->is_varargs_)
8377 this->report_error(_("too many arguments"));
8382 Type* element_type = varargs_type->array_type()->element_type();
8383 Expression_list* vals = new Expression_list;
8384 for (; pa != old_args->end(); ++pa, ++i)
8386 // Check types here so that we get a better message.
8387 Type* patype = (*pa)->type();
8388 source_location paloc = (*pa)->location();
8389 if (!this->check_argument_type(i, element_type, patype,
8390 paloc, issued_error))
8392 vals->push_back(*pa);
8395 Expression::make_slice_composite_literal(varargs_type, vals, loc);
8396 new_args->push_back(val);
8401 new_args->push_back(Expression::make_nil(loc));
8403 // We can't return a new call expression here, because this one may
8404 // be referenced by Call_result expressions. FIXME.
8405 if (old_args != NULL)
8407 this->args_ = new_args;
8408 this->varargs_are_lowered_ = true;
8410 // Lower all the new subexpressions.
8411 Expression* ret = this;
8412 gogo->lower_expression(function, &ret);
8413 gcc_assert(ret == this);
8417 // Get the function type. Returns NULL if we don't know the type. If
8418 // this returns NULL, and if_ERROR is true, issues an error.
8421 Call_expression::get_function_type() const
8423 return this->fn_->type()->function_type();
8426 // Return the number of values which this call will return.
8429 Call_expression::result_count() const
8431 const Function_type* fntype = this->get_function_type();
8434 if (fntype->results() == NULL)
8436 return fntype->results()->size();
8439 // Return whether this is a call to the predeclared function recover.
8442 Call_expression::is_recover_call() const
8444 return this->do_is_recover_call();
8447 // Set the argument to the recover function.
8450 Call_expression::set_recover_arg(Expression* arg)
8452 this->do_set_recover_arg(arg);
8455 // Virtual functions also implemented by Builtin_call_expression.
8458 Call_expression::do_is_recover_call() const
8464 Call_expression::do_set_recover_arg(Expression*)
8472 Call_expression::do_type()
8474 if (this->type_ != NULL)
8478 Function_type* fntype = this->get_function_type();
8480 return Type::make_error_type();
8482 const Typed_identifier_list* results = fntype->results();
8483 if (results == NULL)
8484 ret = Type::make_void_type();
8485 else if (results->size() == 1)
8486 ret = results->begin()->type();
8488 ret = Type::make_call_multiple_result_type(this);
8495 // Determine types for a call expression. We can use the function
8496 // parameter types to set the types of the arguments.
8499 Call_expression::do_determine_type(const Type_context*)
8501 if (!this->determining_types())
8504 this->fn_->determine_type_no_context();
8505 Function_type* fntype = this->get_function_type();
8506 const Typed_identifier_list* parameters = NULL;
8508 parameters = fntype->parameters();
8509 if (this->args_ != NULL)
8511 Typed_identifier_list::const_iterator pt;
8512 if (parameters != NULL)
8513 pt = parameters->begin();
8514 for (Expression_list::const_iterator pa = this->args_->begin();
8515 pa != this->args_->end();
8518 if (parameters != NULL && pt != parameters->end())
8520 Type_context subcontext(pt->type(), false);
8521 (*pa)->determine_type(&subcontext);
8525 (*pa)->determine_type_no_context();
8530 // Called when determining types for a Call_expression. Return true
8531 // if we should go ahead, false if they have already been determined.
8534 Call_expression::determining_types()
8536 if (this->types_are_determined_)
8540 this->types_are_determined_ = true;
8545 // Check types for parameter I.
8548 Call_expression::check_argument_type(int i, const Type* parameter_type,
8549 const Type* argument_type,
8550 source_location argument_location,
8554 if (!Type::are_assignable(parameter_type, argument_type, &reason))
8559 error_at(argument_location, "argument %d has incompatible type", i);
8561 error_at(argument_location,
8562 "argument %d has incompatible type (%s)",
8565 this->set_is_error();
8574 Call_expression::do_check_types(Gogo*)
8576 Function_type* fntype = this->get_function_type();
8579 if (!this->fn_->type()->is_error_type())
8580 this->report_error(_("expected function"));
8584 if (fntype->is_method())
8586 // We don't support pointers to methods, so the function has to
8587 // be a bound method expression.
8588 Bound_method_expression* bme = this->fn_->bound_method_expression();
8591 this->report_error(_("method call without object"));
8594 Type* first_arg_type = bme->first_argument()->type();
8595 if (first_arg_type->points_to() == NULL)
8597 // When passing a value, we need to check that we are
8598 // permitted to copy it.
8600 if (!Type::are_assignable(fntype->receiver()->type(),
8601 first_arg_type, &reason))
8604 this->report_error(_("incompatible type for receiver"));
8607 error_at(this->location(),
8608 "incompatible type for receiver (%s)",
8610 this->set_is_error();
8616 // Note that varargs was handled by the lower_varargs() method, so
8617 // we don't have to worry about it here.
8619 const Typed_identifier_list* parameters = fntype->parameters();
8620 if (this->args_ == NULL)
8622 if (parameters != NULL && !parameters->empty())
8623 this->report_error(_("not enough arguments"));
8625 else if (parameters == NULL)
8626 this->report_error(_("too many arguments"));
8630 Typed_identifier_list::const_iterator pt = parameters->begin();
8631 for (Expression_list::const_iterator pa = this->args_->begin();
8632 pa != this->args_->end();
8635 if (pt == parameters->end())
8637 this->report_error(_("too many arguments"));
8640 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8641 (*pa)->location(), false);
8643 if (pt != parameters->end())
8644 this->report_error(_("not enough arguments"));
8648 // Return whether we have to use a temporary variable to ensure that
8649 // we evaluate this call expression in order. If the call returns no
8650 // results then it will inevitably be executed last. If the call
8651 // returns more than one result then it will be used with Call_result
8652 // expressions. So we only have to use a temporary variable if the
8653 // call returns exactly one result.
8656 Call_expression::do_must_eval_in_order() const
8658 return this->result_count() == 1;
8661 // Get the function and the first argument to use when calling a bound
8665 Call_expression::bound_method_function(Translate_context* context,
8666 Bound_method_expression* bound_method,
8667 tree* first_arg_ptr)
8669 Expression* first_argument = bound_method->first_argument();
8670 tree first_arg = first_argument->get_tree(context);
8671 if (first_arg == error_mark_node)
8672 return error_mark_node;
8674 // We always pass a pointer to the first argument when calling a
8676 if (first_argument->type()->points_to() == NULL)
8678 tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg));
8679 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg))
8680 || DECL_P(first_arg)
8681 || TREE_CODE(first_arg) == INDIRECT_REF
8682 || TREE_CODE(first_arg) == COMPONENT_REF)
8684 first_arg = build_fold_addr_expr(first_arg);
8685 if (DECL_P(first_arg))
8686 TREE_ADDRESSABLE(first_arg) = 1;
8690 tree tmp = create_tmp_var(TREE_TYPE(first_arg),
8691 get_name(first_arg));
8692 DECL_IGNORED_P(tmp) = 0;
8693 DECL_INITIAL(tmp) = first_arg;
8694 first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type,
8695 build1(DECL_EXPR, void_type_node, tmp),
8696 build_fold_addr_expr(tmp));
8697 TREE_ADDRESSABLE(tmp) = 1;
8699 if (first_arg == error_mark_node)
8700 return error_mark_node;
8703 Type* fatype = bound_method->first_argument_type();
8706 if (fatype->points_to() == NULL)
8707 fatype = Type::make_pointer_type(fatype);
8708 first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg);
8709 if (first_arg == error_mark_node
8710 || TREE_TYPE(first_arg) == error_mark_node)
8711 return error_mark_node;
8714 *first_arg_ptr = first_arg;
8716 return bound_method->method()->get_tree(context);
8719 // Get the function and the first argument to use when calling an
8720 // interface method.
8723 Call_expression::interface_method_function(
8724 Translate_context* context,
8725 Interface_field_reference_expression* interface_method,
8726 tree* first_arg_ptr)
8728 tree expr = interface_method->expr()->get_tree(context);
8729 if (expr == error_mark_node)
8730 return error_mark_node;
8731 expr = save_expr(expr);
8732 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
8733 if (first_arg == error_mark_node)
8734 return error_mark_node;
8735 *first_arg_ptr = first_arg;
8736 return interface_method->get_function_tree(context, expr);
8739 // Build the call expression.
8742 Call_expression::do_get_tree(Translate_context* context)
8744 if (this->tree_ != NULL_TREE)
8747 Function_type* fntype = this->get_function_type();
8749 return error_mark_node;
8751 if (this->fn_->is_error_expression())
8752 return error_mark_node;
8754 Gogo* gogo = context->gogo();
8755 source_location location = this->location();
8757 Func_expression* func = this->fn_->func_expression();
8758 Bound_method_expression* bound_method = this->fn_->bound_method_expression();
8759 Interface_field_reference_expression* interface_method =
8760 this->fn_->interface_field_reference_expression();
8761 const bool has_closure = func != NULL && func->closure() != NULL;
8762 const bool is_method = bound_method != NULL || interface_method != NULL;
8763 gcc_assert(!fntype->is_method() || is_method);
8767 if (this->args_ == NULL || this->args_->empty())
8769 nargs = is_method ? 1 : 0;
8770 args = nargs == 0 ? NULL : new tree[nargs];
8774 const Typed_identifier_list* params = fntype->parameters();
8775 gcc_assert(params != NULL);
8777 nargs = this->args_->size();
8778 int i = is_method ? 1 : 0;
8780 args = new tree[nargs];
8782 Typed_identifier_list::const_iterator pp = params->begin();
8783 Expression_list::const_iterator pe;
8784 for (pe = this->args_->begin();
8785 pe != this->args_->end();
8788 gcc_assert(pp != params->end());
8789 tree arg_val = (*pe)->get_tree(context);
8790 args[i] = Expression::convert_for_assignment(context,
8795 if (args[i] == error_mark_node)
8798 return error_mark_node;
8801 gcc_assert(pp == params->end());
8802 gcc_assert(i == nargs);
8805 tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo)));
8806 if (rettype == error_mark_node)
8809 return error_mark_node;
8814 fn = func->get_tree_without_closure(gogo);
8815 else if (!is_method)
8816 fn = this->fn_->get_tree(context);
8817 else if (bound_method != NULL)
8818 fn = this->bound_method_function(context, bound_method, &args[0]);
8819 else if (interface_method != NULL)
8820 fn = this->interface_method_function(context, interface_method, &args[0]);
8824 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
8827 return error_mark_node;
8831 if (TREE_CODE(fndecl) == ADDR_EXPR)
8832 fndecl = TREE_OPERAND(fndecl, 0);
8834 // Add a type cast in case the type of the function is a recursive
8835 // type which refers to itself.
8836 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
8838 tree fnt = fntype->get_tree(gogo);
8839 if (fnt == error_mark_node)
8840 return error_mark_node;
8841 fn = fold_convert_loc(location, fnt, fn);
8844 // This is to support builtin math functions when using 80387 math.
8845 tree excess_type = NULL_TREE;
8847 && DECL_IS_BUILTIN(fndecl)
8848 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
8850 && ((SCALAR_FLOAT_TYPE_P(rettype)
8851 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
8852 || (COMPLEX_FLOAT_TYPE_P(rettype)
8853 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
8855 excess_type = excess_precision_type(TREE_TYPE(args[0]));
8856 if (excess_type != NULL_TREE)
8858 tree excess_fndecl = mathfn_built_in(excess_type,
8859 DECL_FUNCTION_CODE(fndecl));
8860 if (excess_fndecl == NULL_TREE)
8861 excess_type = NULL_TREE;
8864 fn = build_fold_addr_expr_loc(location, excess_fndecl);
8865 for (int i = 0; i < nargs; ++i)
8866 args[i] = ::convert(excess_type, args[i]);
8871 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
8875 SET_EXPR_LOCATION(ret, location);
8879 tree closure_tree = func->closure()->get_tree(context);
8880 if (closure_tree != error_mark_node)
8881 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
8884 // If this is a recursive function type which returns itself, as in
8886 // we have used ptr_type_node for the return type. Add a cast here
8887 // to the correct type.
8888 if (TREE_TYPE(ret) == ptr_type_node)
8890 tree t = this->type()->base()->get_tree(gogo);
8891 ret = fold_convert_loc(location, t, ret);
8894 if (excess_type != NULL_TREE)
8896 // Calling convert here can undo our excess precision change.
8897 // That may or may not be a bug in convert_to_real.
8898 ret = build1(NOP_EXPR, rettype, ret);
8901 // If there is more than one result, we will refer to the call
8903 if (fntype->results() != NULL && fntype->results()->size() > 1)
8904 ret = save_expr(ret);
8911 // Make a call expression.
8914 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
8915 source_location location)
8917 return new Call_expression(fn, args, is_varargs, location);
8920 // A single result from a call which returns multiple results.
8922 class Call_result_expression : public Expression
8925 Call_result_expression(Call_expression* call, unsigned int index)
8926 : Expression(EXPRESSION_CALL_RESULT, call->location()),
8927 call_(call), index_(index)
8932 do_traverse(Traverse*);
8938 do_determine_type(const Type_context*);
8941 do_check_types(Gogo*);
8946 return new Call_result_expression(this->call_->call_expression(),
8951 do_must_eval_in_order() const
8955 do_get_tree(Translate_context*);
8958 // The underlying call expression.
8960 // Which result we want.
8961 unsigned int index_;
8964 // Traverse a call result.
8967 Call_result_expression::do_traverse(Traverse* traverse)
8969 if (traverse->remember_expression(this->call_))
8971 // We have already traversed the call expression.
8972 return TRAVERSE_CONTINUE;
8974 return Expression::traverse(&this->call_, traverse);
8980 Call_result_expression::do_type()
8982 if (this->classification() == EXPRESSION_ERROR)
8983 return Type::make_error_type();
8985 // THIS->CALL_ can be replaced with a temporary reference due to
8986 // Call_expression::do_must_eval_in_order when there is an error.
8987 Call_expression* ce = this->call_->call_expression();
8990 this->set_is_error();
8991 return Type::make_error_type();
8993 Function_type* fntype = ce->get_function_type();
8996 this->set_is_error();
8997 return Type::make_error_type();
8999 const Typed_identifier_list* results = fntype->results();
9000 if (results == NULL)
9002 this->report_error(_("number of results does not match "
9003 "number of values"));
9004 return Type::make_error_type();
9006 Typed_identifier_list::const_iterator pr = results->begin();
9007 for (unsigned int i = 0; i < this->index_; ++i)
9009 if (pr == results->end())
9013 if (pr == results->end())
9015 this->report_error(_("number of results does not match "
9016 "number of values"));
9017 return Type::make_error_type();
9022 // Check the type. Just make sure that we trigger the warning in
9026 Call_result_expression::do_check_types(Gogo*)
9031 // Determine the type. We have nothing to do here, but the 0 result
9032 // needs to pass down to the caller.
9035 Call_result_expression::do_determine_type(const Type_context*)
9037 this->call_->determine_type_no_context();
9043 Call_result_expression::do_get_tree(Translate_context* context)
9045 tree call_tree = this->call_->get_tree(context);
9046 if (call_tree == error_mark_node)
9047 return error_mark_node;
9048 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9050 gcc_assert(saw_errors());
9051 return error_mark_node;
9053 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9054 for (unsigned int i = 0; i < this->index_; ++i)
9056 gcc_assert(field != NULL_TREE);
9057 field = DECL_CHAIN(field);
9059 gcc_assert(field != NULL_TREE);
9060 return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE);
9063 // Make a reference to a single result of a call which returns
9064 // multiple results.
9067 Expression::make_call_result(Call_expression* call, unsigned int index)
9069 return new Call_result_expression(call, index);
9072 // Class Index_expression.
9077 Index_expression::do_traverse(Traverse* traverse)
9079 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9080 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9081 || (this->end_ != NULL
9082 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9083 return TRAVERSE_EXIT;
9084 return TRAVERSE_CONTINUE;
9087 // Lower an index expression. This converts the generic index
9088 // expression into an array index, a string index, or a map index.
9091 Index_expression::do_lower(Gogo*, Named_object*, int)
9093 source_location location = this->location();
9094 Expression* left = this->left_;
9095 Expression* start = this->start_;
9096 Expression* end = this->end_;
9098 Type* type = left->type();
9099 if (type->is_error_type())
9100 return Expression::make_error(location);
9101 else if (left->is_type_expression())
9103 error_at(location, "attempt to index type expression");
9104 return Expression::make_error(location);
9106 else if (type->array_type() != NULL)
9107 return Expression::make_array_index(left, start, end, location);
9108 else if (type->points_to() != NULL
9109 && type->points_to()->array_type() != NULL
9110 && !type->points_to()->is_open_array_type())
9112 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9114 return Expression::make_array_index(deref, start, end, location);
9116 else if (type->is_string_type())
9117 return Expression::make_string_index(left, start, end, location);
9118 else if (type->map_type() != NULL)
9122 error_at(location, "invalid slice of map");
9123 return Expression::make_error(location);
9125 Map_index_expression* ret= Expression::make_map_index(left, start,
9127 if (this->is_lvalue_)
9128 ret->set_is_lvalue();
9134 "attempt to index object which is not array, string, or map");
9135 return Expression::make_error(location);
9139 // Make an index expression.
9142 Expression::make_index(Expression* left, Expression* start, Expression* end,
9143 source_location location)
9145 return new Index_expression(left, start, end, location);
9148 // An array index. This is used for both indexing and slicing.
9150 class Array_index_expression : public Expression
9153 Array_index_expression(Expression* array, Expression* start,
9154 Expression* end, source_location location)
9155 : Expression(EXPRESSION_ARRAY_INDEX, location),
9156 array_(array), start_(start), end_(end), type_(NULL)
9161 do_traverse(Traverse*);
9167 do_determine_type(const Type_context*);
9170 do_check_types(Gogo*);
9175 return Expression::make_array_index(this->array_->copy(),
9176 this->start_->copy(),
9179 : this->end_->copy()),
9184 do_is_addressable() const;
9187 do_address_taken(bool escapes)
9188 { this->array_->address_taken(escapes); }
9191 do_get_tree(Translate_context*);
9194 // The array we are getting a value from.
9196 // The start or only index.
9198 // The end index of a slice. This may be NULL for a simple array
9199 // index, or it may be a nil expression for the length of the array.
9201 // The type of the expression.
9205 // Array index traversal.
9208 Array_index_expression::do_traverse(Traverse* traverse)
9210 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9211 return TRAVERSE_EXIT;
9212 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9213 return TRAVERSE_EXIT;
9214 if (this->end_ != NULL)
9216 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9217 return TRAVERSE_EXIT;
9219 return TRAVERSE_CONTINUE;
9222 // Return the type of an array index.
9225 Array_index_expression::do_type()
9227 if (this->type_ == NULL)
9229 Array_type* type = this->array_->type()->array_type();
9231 this->type_ = Type::make_error_type();
9232 else if (this->end_ == NULL)
9233 this->type_ = type->element_type();
9234 else if (type->is_open_array_type())
9236 // A slice of a slice has the same type as the original
9238 this->type_ = this->array_->type()->deref();
9242 // A slice of an array is a slice.
9243 this->type_ = Type::make_array_type(type->element_type(), NULL);
9249 // Set the type of an array index.
9252 Array_index_expression::do_determine_type(const Type_context*)
9254 this->array_->determine_type_no_context();
9255 this->start_->determine_type_no_context();
9256 if (this->end_ != NULL)
9257 this->end_->determine_type_no_context();
9260 // Check types of an array index.
9263 Array_index_expression::do_check_types(Gogo*)
9265 if (this->start_->type()->integer_type() == NULL)
9266 this->report_error(_("index must be integer"));
9267 if (this->end_ != NULL
9268 && this->end_->type()->integer_type() == NULL
9269 && !this->end_->is_nil_expression())
9270 this->report_error(_("slice end must be integer"));
9272 Array_type* array_type = this->array_->type()->array_type();
9273 if (array_type == NULL)
9275 gcc_assert(this->array_->type()->is_error_type());
9279 unsigned int int_bits =
9280 Type::lookup_integer_type("int")->integer_type()->bits();
9285 bool lval_valid = (array_type->length() != NULL
9286 && array_type->length()->integer_constant_value(true,
9291 if (this->start_->integer_constant_value(true, ival, &dummy))
9293 if (mpz_sgn(ival) < 0
9294 || mpz_sizeinbase(ival, 2) >= int_bits
9296 && (this->end_ == NULL
9297 ? mpz_cmp(ival, lval) >= 0
9298 : mpz_cmp(ival, lval) > 0)))
9300 error_at(this->start_->location(), "array index out of bounds");
9301 this->set_is_error();
9304 if (this->end_ != NULL && !this->end_->is_nil_expression())
9306 if (this->end_->integer_constant_value(true, ival, &dummy))
9308 if (mpz_sgn(ival) < 0
9309 || mpz_sizeinbase(ival, 2) >= int_bits
9310 || (lval_valid && mpz_cmp(ival, lval) > 0))
9312 error_at(this->end_->location(), "array index out of bounds");
9313 this->set_is_error();
9320 // A slice of an array requires an addressable array. A slice of a
9321 // slice is always possible.
9322 if (this->end_ != NULL
9323 && !array_type->is_open_array_type()
9324 && !this->array_->is_addressable())
9325 this->report_error(_("array is not addressable"));
9328 // Return whether this expression is addressable.
9331 Array_index_expression::do_is_addressable() const
9333 // A slice expression is not addressable.
9334 if (this->end_ != NULL)
9337 // An index into a slice is addressable.
9338 if (this->array_->type()->is_open_array_type())
9341 // An index into an array is addressable if the array is
9343 return this->array_->is_addressable();
9346 // Get a tree for an array index.
9349 Array_index_expression::do_get_tree(Translate_context* context)
9351 Gogo* gogo = context->gogo();
9352 source_location loc = this->location();
9354 Array_type* array_type = this->array_->type()->array_type();
9355 if (array_type == NULL)
9357 gcc_assert(this->array_->type()->is_error_type());
9358 return error_mark_node;
9361 tree type_tree = array_type->get_tree(gogo);
9362 if (type_tree == error_mark_node)
9363 return error_mark_node;
9365 tree array_tree = this->array_->get_tree(context);
9366 if (array_tree == error_mark_node)
9367 return error_mark_node;
9369 if (array_type->length() == NULL && !DECL_P(array_tree))
9370 array_tree = save_expr(array_tree);
9371 tree length_tree = array_type->length_tree(gogo, array_tree);
9372 if (length_tree == error_mark_node)
9373 return error_mark_node;
9374 length_tree = save_expr(length_tree);
9375 tree length_type = TREE_TYPE(length_tree);
9377 tree bad_index = boolean_false_node;
9379 tree start_tree = this->start_->get_tree(context);
9380 if (start_tree == error_mark_node)
9381 return error_mark_node;
9382 if (!DECL_P(start_tree))
9383 start_tree = save_expr(start_tree);
9384 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9385 start_tree = convert_to_integer(length_type, start_tree);
9387 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9390 start_tree = fold_convert_loc(loc, length_type, start_tree);
9391 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
9392 fold_build2_loc(loc,
9396 boolean_type_node, start_tree,
9399 int code = (array_type->length() != NULL
9400 ? (this->end_ == NULL
9401 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9402 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9403 : (this->end_ == NULL
9404 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9405 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9406 tree crash = Gogo::runtime_error(code, loc);
9408 if (this->end_ == NULL)
9410 // Simple array indexing. This has to return an l-value, so
9411 // wrap the index check into START_TREE.
9412 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9413 build3(COND_EXPR, void_type_node,
9414 bad_index, crash, NULL_TREE),
9416 start_tree = fold_convert_loc(loc, sizetype, start_tree);
9418 if (array_type->length() != NULL)
9421 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9422 start_tree, NULL_TREE, NULL_TREE);
9427 tree values = array_type->value_pointer_tree(gogo, array_tree);
9428 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9429 if (element_type_tree == error_mark_node)
9430 return error_mark_node;
9431 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9432 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9433 start_tree, element_size);
9434 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9435 TREE_TYPE(values), values, offset);
9436 return build_fold_indirect_ref(ptr);
9442 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
9443 if (capacity_tree == error_mark_node)
9444 return error_mark_node;
9445 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
9448 if (this->end_->is_nil_expression())
9449 end_tree = length_tree;
9452 end_tree = this->end_->get_tree(context);
9453 if (end_tree == error_mark_node)
9454 return error_mark_node;
9455 if (!DECL_P(end_tree))
9456 end_tree = save_expr(end_tree);
9457 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9458 end_tree = convert_to_integer(length_type, end_tree);
9460 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9463 end_tree = fold_convert_loc(loc, length_type, end_tree);
9465 capacity_tree = save_expr(capacity_tree);
9466 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9467 fold_build2_loc(loc, LT_EXPR,
9469 end_tree, start_tree),
9470 fold_build2_loc(loc, GT_EXPR,
9472 end_tree, capacity_tree));
9473 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9474 bad_index, bad_end);
9477 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9478 if (element_type_tree == error_mark_node)
9479 return error_mark_node;
9480 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9482 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9483 fold_convert_loc(loc, sizetype, start_tree),
9486 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9487 if (value_pointer == error_mark_node)
9488 return error_mark_node;
9490 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9491 TREE_TYPE(value_pointer),
9492 value_pointer, offset);
9494 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9495 end_tree, start_tree);
9497 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9498 capacity_tree, start_tree);
9500 tree struct_tree = this->type()->get_tree(gogo);
9501 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9503 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9505 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9506 tree field = TYPE_FIELDS(struct_tree);
9507 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9509 elt->value = value_pointer;
9511 elt = VEC_quick_push(constructor_elt, init, NULL);
9512 field = DECL_CHAIN(field);
9513 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9515 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
9517 elt = VEC_quick_push(constructor_elt, init, NULL);
9518 field = DECL_CHAIN(field);
9519 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9521 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
9523 tree constructor = build_constructor(struct_tree, init);
9525 if (TREE_CONSTANT(value_pointer)
9526 && TREE_CONSTANT(result_length_tree)
9527 && TREE_CONSTANT(result_capacity_tree))
9528 TREE_CONSTANT(constructor) = 1;
9530 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
9531 build3(COND_EXPR, void_type_node,
9532 bad_index, crash, NULL_TREE),
9536 // Make an array index expression. END may be NULL.
9539 Expression::make_array_index(Expression* array, Expression* start,
9540 Expression* end, source_location location)
9542 // Taking a slice of a composite literal requires moving the literal
9544 if (end != NULL && array->is_composite_literal())
9546 array = Expression::make_heap_composite(array, location);
9547 array = Expression::make_unary(OPERATOR_MULT, array, location);
9549 return new Array_index_expression(array, start, end, location);
9552 // A string index. This is used for both indexing and slicing.
9554 class String_index_expression : public Expression
9557 String_index_expression(Expression* string, Expression* start,
9558 Expression* end, source_location location)
9559 : Expression(EXPRESSION_STRING_INDEX, location),
9560 string_(string), start_(start), end_(end)
9565 do_traverse(Traverse*);
9571 do_determine_type(const Type_context*);
9574 do_check_types(Gogo*);
9579 return Expression::make_string_index(this->string_->copy(),
9580 this->start_->copy(),
9583 : this->end_->copy()),
9588 do_get_tree(Translate_context*);
9591 // The string we are getting a value from.
9592 Expression* string_;
9593 // The start or only index.
9595 // The end index of a slice. This may be NULL for a single index,
9596 // or it may be a nil expression for the length of the string.
9600 // String index traversal.
9603 String_index_expression::do_traverse(Traverse* traverse)
9605 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
9606 return TRAVERSE_EXIT;
9607 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9608 return TRAVERSE_EXIT;
9609 if (this->end_ != NULL)
9611 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9612 return TRAVERSE_EXIT;
9614 return TRAVERSE_CONTINUE;
9617 // Return the type of a string index.
9620 String_index_expression::do_type()
9622 if (this->end_ == NULL)
9623 return Type::lookup_integer_type("uint8");
9625 return this->string_->type();
9628 // Determine the type of a string index.
9631 String_index_expression::do_determine_type(const Type_context*)
9633 this->string_->determine_type_no_context();
9634 this->start_->determine_type_no_context();
9635 if (this->end_ != NULL)
9636 this->end_->determine_type_no_context();
9639 // Check types of a string index.
9642 String_index_expression::do_check_types(Gogo*)
9644 if (this->start_->type()->integer_type() == NULL)
9645 this->report_error(_("index must be integer"));
9646 if (this->end_ != NULL
9647 && this->end_->type()->integer_type() == NULL
9648 && !this->end_->is_nil_expression())
9649 this->report_error(_("slice end must be integer"));
9652 bool sval_valid = this->string_->string_constant_value(&sval);
9657 if (this->start_->integer_constant_value(true, ival, &dummy))
9659 if (mpz_sgn(ival) < 0
9660 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
9662 error_at(this->start_->location(), "string index out of bounds");
9663 this->set_is_error();
9666 if (this->end_ != NULL && !this->end_->is_nil_expression())
9668 if (this->end_->integer_constant_value(true, ival, &dummy))
9670 if (mpz_sgn(ival) < 0
9671 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
9673 error_at(this->end_->location(), "string index out of bounds");
9674 this->set_is_error();
9681 // Get a tree for a string index.
9684 String_index_expression::do_get_tree(Translate_context* context)
9686 source_location loc = this->location();
9688 tree string_tree = this->string_->get_tree(context);
9689 if (string_tree == error_mark_node)
9690 return error_mark_node;
9692 if (this->string_->type()->points_to() != NULL)
9693 string_tree = build_fold_indirect_ref(string_tree);
9694 if (!DECL_P(string_tree))
9695 string_tree = save_expr(string_tree);
9696 tree string_type = TREE_TYPE(string_tree);
9698 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
9699 length_tree = save_expr(length_tree);
9700 tree length_type = TREE_TYPE(length_tree);
9702 tree bad_index = boolean_false_node;
9704 tree start_tree = this->start_->get_tree(context);
9705 if (start_tree == error_mark_node)
9706 return error_mark_node;
9707 if (!DECL_P(start_tree))
9708 start_tree = save_expr(start_tree);
9709 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9710 start_tree = convert_to_integer(length_type, start_tree);
9712 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9715 start_tree = fold_convert_loc(loc, length_type, start_tree);
9717 int code = (this->end_ == NULL
9718 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9719 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
9720 tree crash = Gogo::runtime_error(code, loc);
9722 if (this->end_ == NULL)
9724 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9726 fold_build2_loc(loc, GE_EXPR,
9728 start_tree, length_tree));
9730 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
9731 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
9733 fold_convert_loc(loc, sizetype, start_tree));
9734 tree index = build_fold_indirect_ref_loc(loc, ptr);
9736 return build2(COMPOUND_EXPR, TREE_TYPE(index),
9737 build3(COND_EXPR, void_type_node,
9738 bad_index, crash, NULL_TREE),
9744 if (this->end_->is_nil_expression())
9745 end_tree = build_int_cst(length_type, -1);
9748 end_tree = this->end_->get_tree(context);
9749 if (end_tree == error_mark_node)
9750 return error_mark_node;
9751 if (!DECL_P(end_tree))
9752 end_tree = save_expr(end_tree);
9753 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9754 end_tree = convert_to_integer(length_type, end_tree);
9756 bad_index = Expression::check_bounds(end_tree, length_type,
9759 end_tree = fold_convert_loc(loc, length_type, end_tree);
9762 static tree strslice_fndecl;
9763 tree ret = Gogo::call_builtin(&strslice_fndecl,
9765 "__go_string_slice",
9774 if (ret == error_mark_node)
9775 return error_mark_node;
9776 // This will panic if the bounds are out of range for the
9778 TREE_NOTHROW(strslice_fndecl) = 0;
9780 if (bad_index == boolean_false_node)
9783 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
9784 build3(COND_EXPR, void_type_node,
9785 bad_index, crash, NULL_TREE),
9790 // Make a string index expression. END may be NULL.
9793 Expression::make_string_index(Expression* string, Expression* start,
9794 Expression* end, source_location location)
9796 return new String_index_expression(string, start, end, location);
9801 // Get the type of the map.
9804 Map_index_expression::get_map_type() const
9806 Map_type* mt = this->map_->type()->deref()->map_type();
9808 gcc_assert(saw_errors());
9812 // Map index traversal.
9815 Map_index_expression::do_traverse(Traverse* traverse)
9817 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
9818 return TRAVERSE_EXIT;
9819 return Expression::traverse(&this->index_, traverse);
9822 // Return the type of a map index.
9825 Map_index_expression::do_type()
9827 Map_type* mt = this->get_map_type();
9829 return Type::make_error_type();
9830 Type* type = mt->val_type();
9831 // If this map index is in a tuple assignment, we actually return a
9832 // pointer to the value type. Tuple_map_assignment_statement is
9833 // responsible for handling this correctly. We need to get the type
9834 // right in case this gets assigned to a temporary variable.
9835 if (this->is_in_tuple_assignment_)
9836 type = Type::make_pointer_type(type);
9840 // Fix the type of a map index.
9843 Map_index_expression::do_determine_type(const Type_context*)
9845 this->map_->determine_type_no_context();
9846 Map_type* mt = this->get_map_type();
9847 Type* key_type = mt == NULL ? NULL : mt->key_type();
9848 Type_context subcontext(key_type, false);
9849 this->index_->determine_type(&subcontext);
9852 // Check types of a map index.
9855 Map_index_expression::do_check_types(Gogo*)
9858 Map_type* mt = this->get_map_type();
9861 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
9864 this->report_error(_("incompatible type for map index"));
9867 error_at(this->location(), "incompatible type for map index (%s)",
9869 this->set_is_error();
9874 // Get a tree for a map index.
9877 Map_index_expression::do_get_tree(Translate_context* context)
9879 Map_type* type = this->get_map_type();
9881 return error_mark_node;
9883 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
9884 if (valptr == error_mark_node)
9885 return error_mark_node;
9886 valptr = save_expr(valptr);
9888 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
9890 if (this->is_lvalue_)
9891 return build_fold_indirect_ref(valptr);
9892 else if (this->is_in_tuple_assignment_)
9894 // Tuple_map_assignment_statement is responsible for using this
9900 return fold_build3(COND_EXPR, val_type_tree,
9901 fold_build2(EQ_EXPR, boolean_type_node, valptr,
9902 fold_convert(TREE_TYPE(valptr),
9903 null_pointer_node)),
9904 type->val_type()->get_init_tree(context->gogo(),
9906 build_fold_indirect_ref(valptr));
9910 // Get a tree for the map index. This returns a tree which evaluates
9911 // to a pointer to a value. The pointer will be NULL if the key is
9915 Map_index_expression::get_value_pointer(Translate_context* context,
9918 Map_type* type = this->get_map_type();
9920 return error_mark_node;
9922 tree map_tree = this->map_->get_tree(context);
9923 tree index_tree = this->index_->get_tree(context);
9924 index_tree = Expression::convert_for_assignment(context, type->key_type(),
9925 this->index_->type(),
9928 if (map_tree == error_mark_node || index_tree == error_mark_node)
9929 return error_mark_node;
9931 if (this->map_->type()->points_to() != NULL)
9932 map_tree = build_fold_indirect_ref(map_tree);
9934 // We need to pass in a pointer to the key, so stuff it into a
9938 if (current_function_decl != NULL)
9940 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
9941 DECL_IGNORED_P(tmp) = 0;
9942 DECL_INITIAL(tmp) = index_tree;
9943 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
9944 TREE_ADDRESSABLE(tmp) = 1;
9948 tmp = build_decl(this->location(), VAR_DECL, create_tmp_var_name("M"),
9949 TREE_TYPE(index_tree));
9950 DECL_EXTERNAL(tmp) = 0;
9951 TREE_PUBLIC(tmp) = 0;
9952 TREE_STATIC(tmp) = 1;
9953 DECL_ARTIFICIAL(tmp) = 1;
9954 if (!TREE_CONSTANT(index_tree))
9955 make_tmp = fold_build2_loc(this->location(), INIT_EXPR, void_type_node,
9959 TREE_READONLY(tmp) = 1;
9960 TREE_CONSTANT(tmp) = 1;
9961 DECL_INITIAL(tmp) = index_tree;
9962 make_tmp = NULL_TREE;
9964 rest_of_decl_compilation(tmp, 1, 0);
9966 tree tmpref = fold_convert_loc(this->location(), const_ptr_type_node,
9967 build_fold_addr_expr_loc(this->location(),
9970 static tree map_index_fndecl;
9971 tree call = Gogo::call_builtin(&map_index_fndecl,
9975 const_ptr_type_node,
9976 TREE_TYPE(map_tree),
9978 const_ptr_type_node,
9983 : boolean_false_node));
9984 if (call == error_mark_node)
9985 return error_mark_node;
9986 // This can panic on a map of interface type if the interface holds
9987 // an uncomparable or unhashable type.
9988 TREE_NOTHROW(map_index_fndecl) = 0;
9990 tree val_type_tree = type->val_type()->get_tree(context->gogo());
9991 if (val_type_tree == error_mark_node)
9992 return error_mark_node;
9993 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
9995 tree ret = fold_convert_loc(this->location(), ptr_val_type_tree, call);
9996 if (make_tmp != NULL_TREE)
9997 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
10001 // Make a map index expression.
10003 Map_index_expression*
10004 Expression::make_map_index(Expression* map, Expression* index,
10005 source_location location)
10007 return new Map_index_expression(map, index, location);
10010 // Class Field_reference_expression.
10012 // Return the type of a field reference.
10015 Field_reference_expression::do_type()
10017 Type* type = this->expr_->type();
10018 if (type->is_error_type())
10020 Struct_type* struct_type = type->struct_type();
10021 gcc_assert(struct_type != NULL);
10022 return struct_type->field(this->field_index_)->type();
10025 // Check the types for a field reference.
10028 Field_reference_expression::do_check_types(Gogo*)
10030 Type* type = this->expr_->type();
10031 if (type->is_error_type())
10033 Struct_type* struct_type = type->struct_type();
10034 gcc_assert(struct_type != NULL);
10035 gcc_assert(struct_type->field(this->field_index_) != NULL);
10038 // Get a tree for a field reference.
10041 Field_reference_expression::do_get_tree(Translate_context* context)
10043 tree struct_tree = this->expr_->get_tree(context);
10044 if (struct_tree == error_mark_node
10045 || TREE_TYPE(struct_tree) == error_mark_node)
10046 return error_mark_node;
10047 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
10048 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
10049 if (field == NULL_TREE)
10051 // This can happen for a type which refers to itself indirectly
10052 // and then turns out to be erroneous.
10053 gcc_assert(saw_errors());
10054 return error_mark_node;
10056 for (unsigned int i = this->field_index_; i > 0; --i)
10058 field = DECL_CHAIN(field);
10059 gcc_assert(field != NULL_TREE);
10061 if (TREE_TYPE(field) == error_mark_node)
10062 return error_mark_node;
10063 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
10067 // Make a reference to a qualified identifier in an expression.
10069 Field_reference_expression*
10070 Expression::make_field_reference(Expression* expr, unsigned int field_index,
10071 source_location location)
10073 return new Field_reference_expression(expr, field_index, location);
10076 // Class Interface_field_reference_expression.
10078 // Return a tree for the pointer to the function to call.
10081 Interface_field_reference_expression::get_function_tree(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 = TYPE_FIELDS(expr_type);
10091 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
10093 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10094 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table)));
10096 table = build_fold_indirect_ref(table);
10097 gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
10099 std::string name = Gogo::unpack_hidden_name(this->name_);
10100 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
10101 field != NULL_TREE;
10102 field = DECL_CHAIN(field))
10104 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
10107 gcc_assert(field != NULL_TREE);
10109 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
10112 // Return a tree for the first argument to pass to the interface
10116 Interface_field_reference_expression::get_underlying_object_tree(
10117 Translate_context*,
10120 if (this->expr_->type()->points_to() != NULL)
10121 expr = build_fold_indirect_ref(expr);
10123 tree expr_type = TREE_TYPE(expr);
10124 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10126 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
10127 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
10129 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10135 Interface_field_reference_expression::do_traverse(Traverse* traverse)
10137 return Expression::traverse(&this->expr_, traverse);
10140 // Return the type of an interface field reference.
10143 Interface_field_reference_expression::do_type()
10145 Type* expr_type = this->expr_->type();
10147 Type* points_to = expr_type->points_to();
10148 if (points_to != NULL)
10149 expr_type = points_to;
10151 Interface_type* interface_type = expr_type->interface_type();
10152 if (interface_type == NULL)
10153 return Type::make_error_type();
10155 const Typed_identifier* method = interface_type->find_method(this->name_);
10156 if (method == NULL)
10157 return Type::make_error_type();
10159 return method->type();
10162 // Determine types.
10165 Interface_field_reference_expression::do_determine_type(const Type_context*)
10167 this->expr_->determine_type_no_context();
10170 // Check the types for an interface field reference.
10173 Interface_field_reference_expression::do_check_types(Gogo*)
10175 Type* type = this->expr_->type();
10177 Type* points_to = type->points_to();
10178 if (points_to != NULL)
10181 Interface_type* interface_type = type->interface_type();
10182 if (interface_type == NULL)
10183 this->report_error(_("expected interface or pointer to interface"));
10186 const Typed_identifier* method =
10187 interface_type->find_method(this->name_);
10188 if (method == NULL)
10190 error_at(this->location(), "method %qs not in interface",
10191 Gogo::message_name(this->name_).c_str());
10192 this->set_is_error();
10197 // Get a tree for a reference to a field in an interface. There is no
10198 // standard tree type representation for this: it's a function
10199 // attached to its first argument, like a Bound_method_expression.
10200 // The only places it may currently be used are in a Call_expression
10201 // or a Go_statement, which will take it apart directly. So this has
10202 // nothing to do at present.
10205 Interface_field_reference_expression::do_get_tree(Translate_context*)
10210 // Make a reference to a field in an interface.
10213 Expression::make_interface_field_reference(Expression* expr,
10214 const std::string& field,
10215 source_location location)
10217 return new Interface_field_reference_expression(expr, field, location);
10220 // A general selector. This is a Parser_expression for LEFT.NAME. It
10221 // is lowered after we know the type of the left hand side.
10223 class Selector_expression : public Parser_expression
10226 Selector_expression(Expression* left, const std::string& name,
10227 source_location location)
10228 : Parser_expression(EXPRESSION_SELECTOR, location),
10229 left_(left), name_(name)
10234 do_traverse(Traverse* traverse)
10235 { return Expression::traverse(&this->left_, traverse); }
10238 do_lower(Gogo*, Named_object*, int);
10243 return new Selector_expression(this->left_->copy(), this->name_,
10249 lower_method_expression(Gogo*);
10251 // The expression on the left hand side.
10253 // The name on the right hand side.
10257 // Lower a selector expression once we know the real type of the left
10261 Selector_expression::do_lower(Gogo* gogo, Named_object*, int)
10263 Expression* left = this->left_;
10264 if (left->is_type_expression())
10265 return this->lower_method_expression(gogo);
10266 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
10270 // Lower a method expression T.M or (*T).M. We turn this into a
10271 // function literal.
10274 Selector_expression::lower_method_expression(Gogo* gogo)
10276 source_location location = this->location();
10277 Type* type = this->left_->type();
10278 const std::string& name(this->name_);
10281 if (type->points_to() == NULL)
10282 is_pointer = false;
10286 type = type->points_to();
10288 Named_type* nt = type->named_type();
10292 ("method expression requires named type or "
10293 "pointer to named type"));
10294 return Expression::make_error(location);
10298 Method* method = nt->method_function(name, &is_ambiguous);
10299 if (method == NULL)
10302 error_at(location, "type %<%s%> has no method %<%s%>",
10303 nt->message_name().c_str(),
10304 Gogo::message_name(name).c_str());
10306 error_at(location, "method %<%s%> is ambiguous in type %<%s%>",
10307 Gogo::message_name(name).c_str(),
10308 nt->message_name().c_str());
10309 return Expression::make_error(location);
10312 if (!is_pointer && !method->is_value_method())
10314 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
10315 nt->message_name().c_str(),
10316 Gogo::message_name(name).c_str());
10317 return Expression::make_error(location);
10320 // Build a new function type in which the receiver becomes the first
10322 Function_type* method_type = method->type();
10323 gcc_assert(method_type->is_method());
10325 const char* const receiver_name = "$this";
10326 Typed_identifier_list* parameters = new Typed_identifier_list();
10327 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
10330 const Typed_identifier_list* method_parameters = method_type->parameters();
10331 if (method_parameters != NULL)
10333 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10334 p != method_parameters->end();
10336 parameters->push_back(*p);
10339 const Typed_identifier_list* method_results = method_type->results();
10340 Typed_identifier_list* results;
10341 if (method_results == NULL)
10345 results = new Typed_identifier_list();
10346 for (Typed_identifier_list::const_iterator p = method_results->begin();
10347 p != method_results->end();
10349 results->push_back(*p);
10352 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
10354 if (method_type->is_varargs())
10355 fntype->set_is_varargs();
10357 // We generate methods which always takes a pointer to the receiver
10358 // as their first argument. If this is for a pointer type, we can
10359 // simply reuse the existing function. We use an internal hack to
10360 // get the right type.
10364 Named_object* mno = (method->needs_stub_method()
10365 ? method->stub_object()
10366 : method->named_object());
10367 Expression* f = Expression::make_func_reference(mno, NULL, location);
10368 f = Expression::make_cast(fntype, f, location);
10369 Type_conversion_expression* tce =
10370 static_cast<Type_conversion_expression*>(f);
10371 tce->set_may_convert_function_types();
10375 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
10378 Named_object* vno = gogo->lookup(receiver_name, NULL);
10379 gcc_assert(vno != NULL);
10380 Expression* ve = Expression::make_var_reference(vno, location);
10381 Expression* bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
10383 // Even though we found the method above, if it has an error type we
10384 // may see an error here.
10385 if (bm->is_error_expression())
10387 gogo->finish_function(location);
10391 Expression_list* args;
10392 if (method_parameters == NULL)
10396 args = new Expression_list();
10397 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10398 p != method_parameters->end();
10401 vno = gogo->lookup(p->name(), NULL);
10402 gcc_assert(vno != NULL);
10403 args->push_back(Expression::make_var_reference(vno, location));
10407 Call_expression* call = Expression::make_call(bm, args,
10408 method_type->is_varargs(),
10411 size_t count = call->result_count();
10414 s = Statement::make_statement(call);
10417 Expression_list* retvals = new Expression_list();
10419 retvals->push_back(call);
10422 for (size_t i = 0; i < count; ++i)
10423 retvals->push_back(Expression::make_call_result(call, i));
10425 s = Statement::make_return_statement(no->func_value()->type()->results(),
10426 retvals, location);
10428 gogo->add_statement(s);
10430 gogo->finish_function(location);
10432 return Expression::make_func_reference(no, NULL, location);
10435 // Make a selector expression.
10438 Expression::make_selector(Expression* left, const std::string& name,
10439 source_location location)
10441 return new Selector_expression(left, name, location);
10444 // Implement the builtin function new.
10446 class Allocation_expression : public Expression
10449 Allocation_expression(Type* type, source_location location)
10450 : Expression(EXPRESSION_ALLOCATION, location),
10456 do_traverse(Traverse* traverse)
10457 { return Type::traverse(this->type_, traverse); }
10461 { return Type::make_pointer_type(this->type_); }
10464 do_determine_type(const Type_context*)
10468 do_check_types(Gogo*);
10472 { return new Allocation_expression(this->type_, this->location()); }
10475 do_get_tree(Translate_context*);
10478 // The type we are allocating.
10482 // Check the type of an allocation expression.
10485 Allocation_expression::do_check_types(Gogo*)
10487 if (this->type_->function_type() != NULL)
10488 this->report_error(_("invalid new of function type"));
10491 // Return a tree for an allocation expression.
10494 Allocation_expression::do_get_tree(Translate_context* context)
10496 tree type_tree = this->type_->get_tree(context->gogo());
10497 if (type_tree == error_mark_node)
10498 return error_mark_node;
10499 tree size_tree = TYPE_SIZE_UNIT(type_tree);
10500 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
10502 if (space == error_mark_node)
10503 return error_mark_node;
10504 return fold_convert(build_pointer_type(type_tree), space);
10507 // Make an allocation expression.
10510 Expression::make_allocation(Type* type, source_location location)
10512 return new Allocation_expression(type, location);
10515 // Implement the builtin function make.
10517 class Make_expression : public Expression
10520 Make_expression(Type* type, Expression_list* args, source_location location)
10521 : Expression(EXPRESSION_MAKE, location),
10522 type_(type), args_(args)
10527 do_traverse(Traverse* traverse);
10531 { return this->type_; }
10534 do_determine_type(const Type_context*);
10537 do_check_types(Gogo*);
10542 return new Make_expression(this->type_, this->args_->copy(),
10547 do_get_tree(Translate_context*);
10550 // The type we are making.
10552 // The arguments to pass to the make routine.
10553 Expression_list* args_;
10559 Make_expression::do_traverse(Traverse* traverse)
10561 if (this->args_ != NULL
10562 && this->args_->traverse(traverse) == TRAVERSE_EXIT)
10563 return TRAVERSE_EXIT;
10564 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10565 return TRAVERSE_EXIT;
10566 return TRAVERSE_CONTINUE;
10569 // Set types of arguments.
10572 Make_expression::do_determine_type(const Type_context*)
10574 if (this->args_ != NULL)
10576 Type_context context(Type::lookup_integer_type("int"), false);
10577 for (Expression_list::const_iterator pe = this->args_->begin();
10578 pe != this->args_->end();
10580 (*pe)->determine_type(&context);
10584 // Check types for a make expression.
10587 Make_expression::do_check_types(Gogo*)
10589 if (this->type_->channel_type() == NULL
10590 && this->type_->map_type() == NULL
10591 && (this->type_->array_type() == NULL
10592 || this->type_->array_type()->length() != NULL))
10593 this->report_error(_("invalid type for make function"));
10594 else if (!this->type_->check_make_expression(this->args_, this->location()))
10595 this->set_is_error();
10598 // Return a tree for a make expression.
10601 Make_expression::do_get_tree(Translate_context* context)
10603 return this->type_->make_expression_tree(context, this->args_,
10607 // Make a make expression.
10610 Expression::make_make(Type* type, Expression_list* args,
10611 source_location location)
10613 return new Make_expression(type, args, location);
10616 // Construct a struct.
10618 class Struct_construction_expression : public Expression
10621 Struct_construction_expression(Type* type, Expression_list* vals,
10622 source_location location)
10623 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
10624 type_(type), vals_(vals)
10627 // Return whether this is a constant initializer.
10629 is_constant_struct() const;
10633 do_traverse(Traverse* traverse);
10637 { return this->type_; }
10640 do_determine_type(const Type_context*);
10643 do_check_types(Gogo*);
10648 return new Struct_construction_expression(this->type_, this->vals_->copy(),
10653 do_is_addressable() const
10657 do_get_tree(Translate_context*);
10660 do_export(Export*) const;
10663 // The type of the struct to construct.
10665 // The list of values, in order of the fields in the struct. A NULL
10666 // entry means that the field should be zero-initialized.
10667 Expression_list* vals_;
10673 Struct_construction_expression::do_traverse(Traverse* traverse)
10675 if (this->vals_ != NULL
10676 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10677 return TRAVERSE_EXIT;
10678 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10679 return TRAVERSE_EXIT;
10680 return TRAVERSE_CONTINUE;
10683 // Return whether this is a constant initializer.
10686 Struct_construction_expression::is_constant_struct() const
10688 if (this->vals_ == NULL)
10690 for (Expression_list::const_iterator pv = this->vals_->begin();
10691 pv != this->vals_->end();
10695 && !(*pv)->is_constant()
10696 && (!(*pv)->is_composite_literal()
10697 || (*pv)->is_nonconstant_composite_literal()))
10701 const Struct_field_list* fields = this->type_->struct_type()->fields();
10702 for (Struct_field_list::const_iterator pf = fields->begin();
10703 pf != fields->end();
10706 // There are no constant constructors for interfaces.
10707 if (pf->type()->interface_type() != NULL)
10714 // Final type determination.
10717 Struct_construction_expression::do_determine_type(const Type_context*)
10719 if (this->vals_ == NULL)
10721 const Struct_field_list* fields = this->type_->struct_type()->fields();
10722 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())
10731 Type_context subcontext(pf->type(), false);
10732 (*pv)->determine_type(&subcontext);
10735 // Extra values are an error we will report elsewhere; we still want
10736 // to determine the type to avoid knockon errors.
10737 for (; pv != this->vals_->end(); ++pv)
10738 (*pv)->determine_type_no_context();
10744 Struct_construction_expression::do_check_types(Gogo*)
10746 if (this->vals_ == NULL)
10749 Struct_type* st = this->type_->struct_type();
10750 if (this->vals_->size() > st->field_count())
10752 this->report_error(_("too many expressions for struct"));
10756 const Struct_field_list* fields = st->fields();
10757 Expression_list::const_iterator pv = this->vals_->begin();
10759 for (Struct_field_list::const_iterator pf = fields->begin();
10760 pf != fields->end();
10763 if (pv == this->vals_->end())
10765 this->report_error(_("too few expressions for struct"));
10772 std::string reason;
10773 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
10775 if (reason.empty())
10776 error_at((*pv)->location(),
10777 "incompatible type for field %d in struct construction",
10780 error_at((*pv)->location(),
10781 ("incompatible type for field %d in "
10782 "struct construction (%s)"),
10783 i + 1, reason.c_str());
10784 this->set_is_error();
10787 gcc_assert(pv == this->vals_->end());
10790 // Return a tree for constructing a struct.
10793 Struct_construction_expression::do_get_tree(Translate_context* context)
10795 Gogo* gogo = context->gogo();
10797 if (this->vals_ == NULL)
10798 return this->type_->get_init_tree(gogo, false);
10800 tree type_tree = this->type_->get_tree(gogo);
10801 if (type_tree == error_mark_node)
10802 return error_mark_node;
10803 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10805 bool is_constant = true;
10806 const Struct_field_list* fields = this->type_->struct_type()->fields();
10807 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
10809 Struct_field_list::const_iterator pf = fields->begin();
10810 Expression_list::const_iterator pv = this->vals_->begin();
10811 for (tree field = TYPE_FIELDS(type_tree);
10812 field != NULL_TREE;
10813 field = DECL_CHAIN(field), ++pf)
10815 gcc_assert(pf != fields->end());
10818 if (pv == this->vals_->end())
10819 val = pf->type()->get_init_tree(gogo, false);
10820 else if (*pv == NULL)
10822 val = pf->type()->get_init_tree(gogo, false);
10827 val = Expression::convert_for_assignment(context, pf->type(),
10829 (*pv)->get_tree(context),
10834 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
10835 return error_mark_node;
10837 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
10838 elt->index = field;
10840 if (!TREE_CONSTANT(val))
10841 is_constant = false;
10843 gcc_assert(pf == fields->end());
10845 tree ret = build_constructor(type_tree, elts);
10847 TREE_CONSTANT(ret) = 1;
10851 // Export a struct construction.
10854 Struct_construction_expression::do_export(Export* exp) const
10856 exp->write_c_string("convert(");
10857 exp->write_type(this->type_);
10858 for (Expression_list::const_iterator pv = this->vals_->begin();
10859 pv != this->vals_->end();
10862 exp->write_c_string(", ");
10864 (*pv)->export_expression(exp);
10866 exp->write_c_string(")");
10869 // Make a struct composite literal. This used by the thunk code.
10872 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
10873 source_location location)
10875 gcc_assert(type->struct_type() != NULL);
10876 return new Struct_construction_expression(type, vals, location);
10879 // Construct an array. This class is not used directly; instead we
10880 // use the child classes, Fixed_array_construction_expression and
10881 // Open_array_construction_expression.
10883 class Array_construction_expression : public Expression
10886 Array_construction_expression(Expression_classification classification,
10887 Type* type, Expression_list* vals,
10888 source_location location)
10889 : Expression(classification, location),
10890 type_(type), vals_(vals)
10894 // Return whether this is a constant initializer.
10896 is_constant_array() const;
10898 // Return the number of elements.
10900 element_count() const
10901 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
10905 do_traverse(Traverse* traverse);
10909 { return this->type_; }
10912 do_determine_type(const Type_context*);
10915 do_check_types(Gogo*);
10918 do_is_addressable() const
10922 do_export(Export*) const;
10924 // The list of values.
10927 { return this->vals_; }
10929 // Get a constructor tree for the array values.
10931 get_constructor_tree(Translate_context* context, tree type_tree);
10934 // The type of the array to construct.
10936 // The list of values.
10937 Expression_list* vals_;
10943 Array_construction_expression::do_traverse(Traverse* traverse)
10945 if (this->vals_ != NULL
10946 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10947 return TRAVERSE_EXIT;
10948 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10949 return TRAVERSE_EXIT;
10950 return TRAVERSE_CONTINUE;
10953 // Return whether this is a constant initializer.
10956 Array_construction_expression::is_constant_array() const
10958 if (this->vals_ == NULL)
10961 // There are no constant constructors for interfaces.
10962 if (this->type_->array_type()->element_type()->interface_type() != NULL)
10965 for (Expression_list::const_iterator pv = this->vals_->begin();
10966 pv != this->vals_->end();
10970 && !(*pv)->is_constant()
10971 && (!(*pv)->is_composite_literal()
10972 || (*pv)->is_nonconstant_composite_literal()))
10978 // Final type determination.
10981 Array_construction_expression::do_determine_type(const Type_context*)
10983 if (this->vals_ == NULL)
10985 Type_context subcontext(this->type_->array_type()->element_type(), false);
10986 for (Expression_list::const_iterator pv = this->vals_->begin();
10987 pv != this->vals_->end();
10991 (*pv)->determine_type(&subcontext);
10998 Array_construction_expression::do_check_types(Gogo*)
11000 if (this->vals_ == NULL)
11003 Array_type* at = this->type_->array_type();
11005 Type* element_type = at->element_type();
11006 for (Expression_list::const_iterator pv = this->vals_->begin();
11007 pv != this->vals_->end();
11011 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
11013 error_at((*pv)->location(),
11014 "incompatible type for element %d in composite literal",
11016 this->set_is_error();
11020 Expression* length = at->length();
11021 if (length != NULL)
11026 if (at->length()->integer_constant_value(true, val, &type))
11028 if (this->vals_->size() > mpz_get_ui(val))
11029 this->report_error(_("too many elements in composite literal"));
11035 // Get a constructor tree for the array values.
11038 Array_construction_expression::get_constructor_tree(Translate_context* context,
11041 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11042 (this->vals_ == NULL
11044 : this->vals_->size()));
11045 Type* element_type = this->type_->array_type()->element_type();
11046 bool is_constant = true;
11047 if (this->vals_ != NULL)
11050 for (Expression_list::const_iterator pv = this->vals_->begin();
11051 pv != this->vals_->end();
11054 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
11055 elt->index = size_int(i);
11057 elt->value = element_type->get_init_tree(context->gogo(), false);
11060 tree value_tree = (*pv)->get_tree(context);
11061 elt->value = Expression::convert_for_assignment(context,
11067 if (elt->value == error_mark_node)
11068 return error_mark_node;
11069 if (!TREE_CONSTANT(elt->value))
11070 is_constant = false;
11074 tree ret = build_constructor(type_tree, values);
11076 TREE_CONSTANT(ret) = 1;
11080 // Export an array construction.
11083 Array_construction_expression::do_export(Export* exp) const
11085 exp->write_c_string("convert(");
11086 exp->write_type(this->type_);
11087 if (this->vals_ != NULL)
11089 for (Expression_list::const_iterator pv = this->vals_->begin();
11090 pv != this->vals_->end();
11093 exp->write_c_string(", ");
11095 (*pv)->export_expression(exp);
11098 exp->write_c_string(")");
11101 // Construct a fixed array.
11103 class Fixed_array_construction_expression :
11104 public Array_construction_expression
11107 Fixed_array_construction_expression(Type* type, Expression_list* vals,
11108 source_location location)
11109 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
11110 type, vals, location)
11112 gcc_assert(type->array_type() != NULL
11113 && type->array_type()->length() != NULL);
11120 return new Fixed_array_construction_expression(this->type(),
11121 (this->vals() == NULL
11123 : this->vals()->copy()),
11128 do_get_tree(Translate_context*);
11131 // Return a tree for constructing a fixed array.
11134 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
11136 return this->get_constructor_tree(context,
11137 this->type()->get_tree(context->gogo()));
11140 // Construct an open array.
11142 class Open_array_construction_expression : public Array_construction_expression
11145 Open_array_construction_expression(Type* type, Expression_list* vals,
11146 source_location location)
11147 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
11148 type, vals, location)
11150 gcc_assert(type->array_type() != NULL
11151 && type->array_type()->length() == NULL);
11155 // Note that taking the address of an open array literal is invalid.
11160 return new Open_array_construction_expression(this->type(),
11161 (this->vals() == NULL
11163 : this->vals()->copy()),
11168 do_get_tree(Translate_context*);
11171 // Return a tree for constructing an open array.
11174 Open_array_construction_expression::do_get_tree(Translate_context* context)
11176 Array_type* array_type = this->type()->array_type();
11177 if (array_type == NULL)
11179 gcc_assert(this->type()->is_error_type());
11180 return error_mark_node;
11183 Type* element_type = array_type->element_type();
11184 tree element_type_tree = element_type->get_tree(context->gogo());
11185 if (element_type_tree == error_mark_node)
11186 return error_mark_node;
11190 if (this->vals() == NULL || this->vals()->empty())
11192 // We need to create a unique value.
11193 tree max = size_int(0);
11194 tree constructor_type = build_array_type(element_type_tree,
11195 build_index_type(max));
11196 if (constructor_type == error_mark_node)
11197 return error_mark_node;
11198 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
11199 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
11200 elt->index = size_int(0);
11201 elt->value = element_type->get_init_tree(context->gogo(), false);
11202 values = build_constructor(constructor_type, vec);
11203 if (TREE_CONSTANT(elt->value))
11204 TREE_CONSTANT(values) = 1;
11205 length_tree = size_int(0);
11209 tree max = size_int(this->vals()->size() - 1);
11210 tree constructor_type = build_array_type(element_type_tree,
11211 build_index_type(max));
11212 if (constructor_type == error_mark_node)
11213 return error_mark_node;
11214 values = this->get_constructor_tree(context, constructor_type);
11215 length_tree = size_int(this->vals()->size());
11218 if (values == error_mark_node)
11219 return error_mark_node;
11221 bool is_constant_initializer = TREE_CONSTANT(values);
11223 // We have to copy the initial values into heap memory if we are in
11224 // a function or if the values are not constants. We also have to
11225 // copy them if they may contain pointers in a non-constant context,
11226 // as otherwise the garbage collector won't see them.
11227 bool copy_to_heap = (context->function() != NULL
11228 || !is_constant_initializer
11229 || (element_type->has_pointer()
11230 && !context->is_const()));
11232 if (is_constant_initializer)
11234 tree tmp = build_decl(this->location(), VAR_DECL,
11235 create_tmp_var_name("C"), TREE_TYPE(values));
11236 DECL_EXTERNAL(tmp) = 0;
11237 TREE_PUBLIC(tmp) = 0;
11238 TREE_STATIC(tmp) = 1;
11239 DECL_ARTIFICIAL(tmp) = 1;
11242 // If we are not copying the value to the heap, we will only
11243 // initialize the value once, so we can use this directly
11244 // rather than copying it. In that case we can't make it
11245 // read-only, because the program is permitted to change it.
11246 TREE_READONLY(tmp) = 1;
11247 TREE_CONSTANT(tmp) = 1;
11249 DECL_INITIAL(tmp) = values;
11250 rest_of_decl_compilation(tmp, 1, 0);
11258 // the initializer will only run once.
11259 space = build_fold_addr_expr(values);
11264 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
11265 space = context->gogo()->allocate_memory(element_type, memsize,
11267 space = save_expr(space);
11269 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
11270 tree ref = build_fold_indirect_ref_loc(this->location(), s);
11271 TREE_THIS_NOTRAP(ref) = 1;
11272 set = build2(MODIFY_EXPR, void_type_node, ref, values);
11275 // Build a constructor for the open array.
11277 tree type_tree = this->type()->get_tree(context->gogo());
11278 if (type_tree == error_mark_node)
11279 return error_mark_node;
11280 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11282 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
11284 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
11285 tree field = TYPE_FIELDS(type_tree);
11286 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
11287 elt->index = field;
11288 elt->value = fold_convert(TREE_TYPE(field), space);
11290 elt = VEC_quick_push(constructor_elt, init, NULL);
11291 field = DECL_CHAIN(field);
11292 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
11293 elt->index = field;
11294 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11296 elt = VEC_quick_push(constructor_elt, init, NULL);
11297 field = DECL_CHAIN(field);
11298 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
11299 elt->index = field;
11300 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11302 tree constructor = build_constructor(type_tree, init);
11303 if (constructor == error_mark_node)
11304 return error_mark_node;
11306 TREE_CONSTANT(constructor) = 1;
11308 if (set == NULL_TREE)
11309 return constructor;
11311 return build2(COMPOUND_EXPR, type_tree, set, constructor);
11314 // Make a slice composite literal. This is used by the type
11315 // descriptor code.
11318 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
11319 source_location location)
11321 gcc_assert(type->is_open_array_type());
11322 return new Open_array_construction_expression(type, vals, location);
11325 // Construct a map.
11327 class Map_construction_expression : public Expression
11330 Map_construction_expression(Type* type, Expression_list* vals,
11331 source_location location)
11332 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
11333 type_(type), vals_(vals)
11334 { gcc_assert(vals == NULL || vals->size() % 2 == 0); }
11338 do_traverse(Traverse* traverse);
11342 { return this->type_; }
11345 do_determine_type(const Type_context*);
11348 do_check_types(Gogo*);
11353 return new Map_construction_expression(this->type_, this->vals_->copy(),
11358 do_get_tree(Translate_context*);
11361 do_export(Export*) const;
11364 // The type of the map to construct.
11366 // The list of values.
11367 Expression_list* vals_;
11373 Map_construction_expression::do_traverse(Traverse* traverse)
11375 if (this->vals_ != NULL
11376 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11377 return TRAVERSE_EXIT;
11378 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11379 return TRAVERSE_EXIT;
11380 return TRAVERSE_CONTINUE;
11383 // Final type determination.
11386 Map_construction_expression::do_determine_type(const Type_context*)
11388 if (this->vals_ == NULL)
11391 Map_type* mt = this->type_->map_type();
11392 Type_context key_context(mt->key_type(), false);
11393 Type_context val_context(mt->val_type(), false);
11394 for (Expression_list::const_iterator pv = this->vals_->begin();
11395 pv != this->vals_->end();
11398 (*pv)->determine_type(&key_context);
11400 (*pv)->determine_type(&val_context);
11407 Map_construction_expression::do_check_types(Gogo*)
11409 if (this->vals_ == NULL)
11412 Map_type* mt = this->type_->map_type();
11414 Type* key_type = mt->key_type();
11415 Type* val_type = mt->val_type();
11416 for (Expression_list::const_iterator pv = this->vals_->begin();
11417 pv != this->vals_->end();
11420 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
11422 error_at((*pv)->location(),
11423 "incompatible type for element %d key in map construction",
11425 this->set_is_error();
11428 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
11430 error_at((*pv)->location(),
11431 ("incompatible type for element %d value "
11432 "in map construction"),
11434 this->set_is_error();
11439 // Return a tree for constructing a map.
11442 Map_construction_expression::do_get_tree(Translate_context* context)
11444 Gogo* gogo = context->gogo();
11445 source_location loc = this->location();
11447 Map_type* mt = this->type_->map_type();
11449 // Build a struct to hold the key and value.
11450 tree struct_type = make_node(RECORD_TYPE);
11452 Type* key_type = mt->key_type();
11453 tree id = get_identifier("__key");
11454 tree key_type_tree = key_type->get_tree(gogo);
11455 if (key_type_tree == error_mark_node)
11456 return error_mark_node;
11457 tree key_field = build_decl(loc, FIELD_DECL, id, key_type_tree);
11458 DECL_CONTEXT(key_field) = struct_type;
11459 TYPE_FIELDS(struct_type) = key_field;
11461 Type* val_type = mt->val_type();
11462 id = get_identifier("__val");
11463 tree val_type_tree = val_type->get_tree(gogo);
11464 if (val_type_tree == error_mark_node)
11465 return error_mark_node;
11466 tree val_field = build_decl(loc, FIELD_DECL, id, val_type_tree);
11467 DECL_CONTEXT(val_field) = struct_type;
11468 DECL_CHAIN(key_field) = val_field;
11470 layout_type(struct_type);
11472 bool is_constant = true;
11477 if (this->vals_ == NULL || this->vals_->empty())
11479 valaddr = null_pointer_node;
11480 make_tmp = NULL_TREE;
11484 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11485 this->vals_->size() / 2);
11487 for (Expression_list::const_iterator pv = this->vals_->begin();
11488 pv != this->vals_->end();
11491 bool one_is_constant = true;
11493 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
11495 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
11496 elt->index = key_field;
11497 tree val_tree = (*pv)->get_tree(context);
11498 elt->value = Expression::convert_for_assignment(context, key_type,
11501 if (elt->value == error_mark_node)
11502 return error_mark_node;
11503 if (!TREE_CONSTANT(elt->value))
11504 one_is_constant = false;
11508 elt = VEC_quick_push(constructor_elt, one, NULL);
11509 elt->index = val_field;
11510 val_tree = (*pv)->get_tree(context);
11511 elt->value = Expression::convert_for_assignment(context, val_type,
11514 if (elt->value == error_mark_node)
11515 return error_mark_node;
11516 if (!TREE_CONSTANT(elt->value))
11517 one_is_constant = false;
11519 elt = VEC_quick_push(constructor_elt, values, NULL);
11520 elt->index = size_int(i);
11521 elt->value = build_constructor(struct_type, one);
11522 if (one_is_constant)
11523 TREE_CONSTANT(elt->value) = 1;
11525 is_constant = false;
11528 tree index_type = build_index_type(size_int(i - 1));
11529 tree array_type = build_array_type(struct_type, index_type);
11530 tree init = build_constructor(array_type, values);
11532 TREE_CONSTANT(init) = 1;
11534 if (current_function_decl != NULL)
11536 tmp = create_tmp_var(array_type, get_name(array_type));
11537 DECL_INITIAL(tmp) = init;
11538 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
11539 TREE_ADDRESSABLE(tmp) = 1;
11543 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
11544 DECL_EXTERNAL(tmp) = 0;
11545 TREE_PUBLIC(tmp) = 0;
11546 TREE_STATIC(tmp) = 1;
11547 DECL_ARTIFICIAL(tmp) = 1;
11548 if (!TREE_CONSTANT(init))
11549 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
11553 TREE_READONLY(tmp) = 1;
11554 TREE_CONSTANT(tmp) = 1;
11555 DECL_INITIAL(tmp) = init;
11556 make_tmp = NULL_TREE;
11558 rest_of_decl_compilation(tmp, 1, 0);
11561 valaddr = build_fold_addr_expr(tmp);
11564 tree descriptor = gogo->map_descriptor(mt);
11566 tree type_tree = this->type_->get_tree(gogo);
11567 if (type_tree == error_mark_node)
11568 return error_mark_node;
11570 static tree construct_map_fndecl;
11571 tree call = Gogo::call_builtin(&construct_map_fndecl,
11573 "__go_construct_map",
11576 TREE_TYPE(descriptor),
11581 TYPE_SIZE_UNIT(struct_type),
11583 byte_position(val_field),
11585 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
11586 const_ptr_type_node,
11587 fold_convert(const_ptr_type_node, valaddr));
11588 if (call == error_mark_node)
11589 return error_mark_node;
11592 if (make_tmp == NULL)
11595 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
11599 // Export an array construction.
11602 Map_construction_expression::do_export(Export* exp) const
11604 exp->write_c_string("convert(");
11605 exp->write_type(this->type_);
11606 for (Expression_list::const_iterator pv = this->vals_->begin();
11607 pv != this->vals_->end();
11610 exp->write_c_string(", ");
11611 (*pv)->export_expression(exp);
11613 exp->write_c_string(")");
11616 // A general composite literal. This is lowered to a type specific
11619 class Composite_literal_expression : public Parser_expression
11622 Composite_literal_expression(Type* type, int depth, bool has_keys,
11623 Expression_list* vals, source_location location)
11624 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
11625 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
11630 do_traverse(Traverse* traverse);
11633 do_lower(Gogo*, Named_object*, int);
11638 return new Composite_literal_expression(this->type_, this->depth_,
11640 (this->vals_ == NULL
11642 : this->vals_->copy()),
11648 lower_struct(Type*);
11651 lower_array(Type*);
11654 make_array(Type*, Expression_list*);
11657 lower_map(Gogo*, Named_object*, Type*);
11659 // The type of the composite literal.
11661 // The depth within a list of composite literals within a composite
11662 // literal, when the type is omitted.
11664 // The values to put in the composite literal.
11665 Expression_list* vals_;
11666 // If this is true, then VALS_ is a list of pairs: a key and a
11667 // value. In an array initializer, a missing key will be NULL.
11674 Composite_literal_expression::do_traverse(Traverse* traverse)
11676 if (this->vals_ != NULL
11677 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11678 return TRAVERSE_EXIT;
11679 return Type::traverse(this->type_, traverse);
11682 // Lower a generic composite literal into a specific version based on
11686 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int)
11688 Type* type = this->type_;
11690 for (int depth = this->depth_; depth > 0; --depth)
11692 if (type->array_type() != NULL)
11693 type = type->array_type()->element_type();
11694 else if (type->map_type() != NULL)
11695 type = type->map_type()->val_type();
11698 if (!type->is_error_type())
11699 error_at(this->location(),
11700 ("may only omit types within composite literals "
11701 "of slice, array, or map type"));
11702 return Expression::make_error(this->location());
11706 if (type->is_error_type())
11707 return Expression::make_error(this->location());
11708 else if (type->struct_type() != NULL)
11709 return this->lower_struct(type);
11710 else if (type->array_type() != NULL)
11711 return this->lower_array(type);
11712 else if (type->map_type() != NULL)
11713 return this->lower_map(gogo, function, type);
11716 error_at(this->location(),
11717 ("expected struct, slice, array, or map type "
11718 "for composite literal"));
11719 return Expression::make_error(this->location());
11723 // Lower a struct composite literal.
11726 Composite_literal_expression::lower_struct(Type* type)
11728 source_location location = this->location();
11729 Struct_type* st = type->struct_type();
11730 if (this->vals_ == NULL || !this->has_keys_)
11731 return new Struct_construction_expression(type, this->vals_, location);
11733 size_t field_count = st->field_count();
11734 std::vector<Expression*> vals(field_count);
11735 Expression_list::const_iterator p = this->vals_->begin();
11736 while (p != this->vals_->end())
11738 Expression* name_expr = *p;
11741 gcc_assert(p != this->vals_->end());
11742 Expression* val = *p;
11746 if (name_expr == NULL)
11748 error_at(val->location(), "mixture of field and value initializers");
11749 return Expression::make_error(location);
11752 bool bad_key = false;
11754 switch (name_expr->classification())
11756 case EXPRESSION_UNKNOWN_REFERENCE:
11757 name = name_expr->unknown_expression()->name();
11760 case EXPRESSION_CONST_REFERENCE:
11761 name = static_cast<Const_expression*>(name_expr)->name();
11764 case EXPRESSION_TYPE:
11766 Type* t = name_expr->type();
11767 Named_type* nt = t->named_type();
11775 case EXPRESSION_VAR_REFERENCE:
11776 name = name_expr->var_expression()->name();
11779 case EXPRESSION_FUNC_REFERENCE:
11780 name = name_expr->func_expression()->name();
11783 case EXPRESSION_UNARY:
11784 // If there is a local variable around with the same name as
11785 // the field, and this occurs in the closure, then the
11786 // parser may turn the field reference into an indirection
11787 // through the closure. FIXME: This is a mess.
11790 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
11791 if (ue->op() == OPERATOR_MULT)
11793 Field_reference_expression* fre =
11794 ue->operand()->field_reference_expression();
11798 fre->expr()->type()->deref()->struct_type();
11801 const Struct_field* sf = st->field(fre->field_index());
11802 name = sf->field_name();
11804 snprintf(buf, sizeof buf, "%u", fre->field_index());
11805 size_t buflen = strlen(buf);
11806 if (name.compare(name.length() - buflen, buflen, buf)
11809 name = name.substr(0, name.length() - buflen);
11824 error_at(name_expr->location(), "expected struct field name");
11825 return Expression::make_error(location);
11828 unsigned int index;
11829 const Struct_field* sf = st->find_local_field(name, &index);
11832 error_at(name_expr->location(), "unknown field %qs in %qs",
11833 Gogo::message_name(name).c_str(),
11834 (type->named_type() != NULL
11835 ? type->named_type()->message_name().c_str()
11836 : "unnamed struct"));
11837 return Expression::make_error(location);
11839 if (vals[index] != NULL)
11841 error_at(name_expr->location(),
11842 "duplicate value for field %qs in %qs",
11843 Gogo::message_name(name).c_str(),
11844 (type->named_type() != NULL
11845 ? type->named_type()->message_name().c_str()
11846 : "unnamed struct"));
11847 return Expression::make_error(location);
11853 Expression_list* list = new Expression_list;
11854 list->reserve(field_count);
11855 for (size_t i = 0; i < field_count; ++i)
11856 list->push_back(vals[i]);
11858 return new Struct_construction_expression(type, list, location);
11861 // Lower an array composite literal.
11864 Composite_literal_expression::lower_array(Type* type)
11866 source_location location = this->location();
11867 if (this->vals_ == NULL || !this->has_keys_)
11868 return this->make_array(type, this->vals_);
11870 std::vector<Expression*> vals;
11871 vals.reserve(this->vals_->size());
11872 unsigned long index = 0;
11873 Expression_list::const_iterator p = this->vals_->begin();
11874 while (p != this->vals_->end())
11876 Expression* index_expr = *p;
11879 gcc_assert(p != this->vals_->end());
11880 Expression* val = *p;
11884 if (index_expr != NULL)
11890 if (!index_expr->integer_constant_value(true, ival, &dummy))
11893 error_at(index_expr->location(),
11894 "index expression is not integer constant");
11895 return Expression::make_error(location);
11898 if (mpz_sgn(ival) < 0)
11901 error_at(index_expr->location(), "index expression is negative");
11902 return Expression::make_error(location);
11905 index = mpz_get_ui(ival);
11906 if (mpz_cmp_ui(ival, index) != 0)
11909 error_at(index_expr->location(), "index value overflow");
11910 return Expression::make_error(location);
11913 Named_type* ntype = Type::lookup_integer_type("int");
11914 Integer_type* inttype = ntype->integer_type();
11916 mpz_init_set_ui(max, 1);
11917 mpz_mul_2exp(max, max, inttype->bits() - 1);
11918 bool ok = mpz_cmp(ival, max) < 0;
11923 error_at(index_expr->location(), "index value overflow");
11924 return Expression::make_error(location);
11929 // FIXME: Our representation isn't very good; this avoids
11931 if (index > 0x1000000)
11933 error_at(index_expr->location(), "index too large for compiler");
11934 return Expression::make_error(location);
11938 if (index == vals.size())
11939 vals.push_back(val);
11942 if (index > vals.size())
11944 vals.reserve(index + 32);
11945 vals.resize(index + 1, static_cast<Expression*>(NULL));
11947 if (vals[index] != NULL)
11949 error_at((index_expr != NULL
11950 ? index_expr->location()
11951 : val->location()),
11952 "duplicate value for index %lu",
11954 return Expression::make_error(location);
11962 size_t size = vals.size();
11963 Expression_list* list = new Expression_list;
11964 list->reserve(size);
11965 for (size_t i = 0; i < size; ++i)
11966 list->push_back(vals[i]);
11968 return this->make_array(type, list);
11971 // Actually build the array composite literal. This handles
11975 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
11977 source_location location = this->location();
11978 Array_type* at = type->array_type();
11979 if (at->length() != NULL && at->length()->is_nil_expression())
11981 size_t size = vals == NULL ? 0 : vals->size();
11983 mpz_init_set_ui(vlen, size);
11984 Expression* elen = Expression::make_integer(&vlen, NULL, location);
11986 at = Type::make_array_type(at->element_type(), elen);
11989 if (at->length() != NULL)
11990 return new Fixed_array_construction_expression(type, vals, location);
11992 return new Open_array_construction_expression(type, vals, location);
11995 // Lower a map composite literal.
11998 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
12001 source_location location = this->location();
12002 if (this->vals_ != NULL)
12004 if (!this->has_keys_)
12006 error_at(location, "map composite literal must have keys");
12007 return Expression::make_error(location);
12010 for (Expression_list::iterator p = this->vals_->begin();
12011 p != this->vals_->end();
12017 error_at((*p)->location(),
12018 "map composite literal must have keys for every value");
12019 return Expression::make_error(location);
12021 // Make sure we have lowered the key; it may not have been
12022 // lowered in order to handle keys for struct composite
12023 // literals. Lower it now to get the right error message.
12024 if ((*p)->unknown_expression() != NULL)
12026 (*p)->unknown_expression()->clear_is_composite_literal_key();
12027 gogo->lower_expression(function, &*p);
12028 gcc_assert((*p)->is_error_expression());
12029 return Expression::make_error(location);
12034 return new Map_construction_expression(type, this->vals_, location);
12037 // Make a composite literal expression.
12040 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
12041 Expression_list* vals,
12042 source_location location)
12044 return new Composite_literal_expression(type, depth, has_keys, vals,
12048 // Return whether this expression is a composite literal.
12051 Expression::is_composite_literal() const
12053 switch (this->classification_)
12055 case EXPRESSION_COMPOSITE_LITERAL:
12056 case EXPRESSION_STRUCT_CONSTRUCTION:
12057 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12058 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12059 case EXPRESSION_MAP_CONSTRUCTION:
12066 // Return whether this expression is a composite literal which is not
12070 Expression::is_nonconstant_composite_literal() const
12072 switch (this->classification_)
12074 case EXPRESSION_STRUCT_CONSTRUCTION:
12076 const Struct_construction_expression *psce =
12077 static_cast<const Struct_construction_expression*>(this);
12078 return !psce->is_constant_struct();
12080 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12082 const Fixed_array_construction_expression *pace =
12083 static_cast<const Fixed_array_construction_expression*>(this);
12084 return !pace->is_constant_array();
12086 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12088 const Open_array_construction_expression *pace =
12089 static_cast<const Open_array_construction_expression*>(this);
12090 return !pace->is_constant_array();
12092 case EXPRESSION_MAP_CONSTRUCTION:
12099 // Return true if this is a reference to a local variable.
12102 Expression::is_local_variable() const
12104 const Var_expression* ve = this->var_expression();
12107 const Named_object* no = ve->named_object();
12108 return (no->is_result_variable()
12109 || (no->is_variable() && !no->var_value()->is_global()));
12112 // Class Type_guard_expression.
12117 Type_guard_expression::do_traverse(Traverse* traverse)
12119 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
12120 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12121 return TRAVERSE_EXIT;
12122 return TRAVERSE_CONTINUE;
12125 // Check types of a type guard expression. The expression must have
12126 // an interface type, but the actual type conversion is checked at run
12130 Type_guard_expression::do_check_types(Gogo*)
12132 // 6g permits using a type guard with unsafe.pointer; we are
12134 Type* expr_type = this->expr_->type();
12135 if (expr_type->is_unsafe_pointer_type())
12137 if (this->type_->points_to() == NULL
12138 && (this->type_->integer_type() == NULL
12139 || (this->type_->forwarded()
12140 != Type::lookup_integer_type("uintptr"))))
12141 this->report_error(_("invalid unsafe.Pointer conversion"));
12143 else if (this->type_->is_unsafe_pointer_type())
12145 if (expr_type->points_to() == NULL
12146 && (expr_type->integer_type() == NULL
12147 || (expr_type->forwarded()
12148 != Type::lookup_integer_type("uintptr"))))
12149 this->report_error(_("invalid unsafe.Pointer conversion"));
12151 else if (expr_type->interface_type() == NULL)
12153 if (!expr_type->is_error_type() && !this->type_->is_error_type())
12154 this->report_error(_("type assertion only valid for interface types"));
12155 this->set_is_error();
12157 else if (this->type_->interface_type() == NULL)
12159 std::string reason;
12160 if (!expr_type->interface_type()->implements_interface(this->type_,
12163 if (!this->type_->is_error_type())
12165 if (reason.empty())
12166 this->report_error(_("impossible type assertion: "
12167 "type does not implement interface"));
12169 error_at(this->location(),
12170 ("impossible type assertion: "
12171 "type does not implement interface (%s)"),
12174 this->set_is_error();
12179 // Return a tree for a type guard expression.
12182 Type_guard_expression::do_get_tree(Translate_context* context)
12184 Gogo* gogo = context->gogo();
12185 tree expr_tree = this->expr_->get_tree(context);
12186 if (expr_tree == error_mark_node)
12187 return error_mark_node;
12188 Type* expr_type = this->expr_->type();
12189 if ((this->type_->is_unsafe_pointer_type()
12190 && (expr_type->points_to() != NULL
12191 || expr_type->integer_type() != NULL))
12192 || (expr_type->is_unsafe_pointer_type()
12193 && this->type_->points_to() != NULL))
12194 return convert_to_pointer(this->type_->get_tree(gogo), expr_tree);
12195 else if (expr_type->is_unsafe_pointer_type()
12196 && this->type_->integer_type() != NULL)
12197 return convert_to_integer(this->type_->get_tree(gogo), expr_tree);
12198 else if (this->type_->interface_type() != NULL)
12199 return Expression::convert_interface_to_interface(context, this->type_,
12200 this->expr_->type(),
12204 return Expression::convert_for_assignment(context, this->type_,
12205 this->expr_->type(), expr_tree,
12209 // Make a type guard expression.
12212 Expression::make_type_guard(Expression* expr, Type* type,
12213 source_location location)
12215 return new Type_guard_expression(expr, type, location);
12218 // Class Heap_composite_expression.
12220 // When you take the address of a composite literal, it is allocated
12221 // on the heap. This class implements that.
12223 class Heap_composite_expression : public Expression
12226 Heap_composite_expression(Expression* expr, source_location location)
12227 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
12233 do_traverse(Traverse* traverse)
12234 { return Expression::traverse(&this->expr_, traverse); }
12238 { return Type::make_pointer_type(this->expr_->type()); }
12241 do_determine_type(const Type_context*)
12242 { this->expr_->determine_type_no_context(); }
12247 return Expression::make_heap_composite(this->expr_->copy(),
12252 do_get_tree(Translate_context*);
12254 // We only export global objects, and the parser does not generate
12255 // this in global scope.
12257 do_export(Export*) const
12258 { gcc_unreachable(); }
12261 // The composite literal which is being put on the heap.
12265 // Return a tree which allocates a composite literal on the heap.
12268 Heap_composite_expression::do_get_tree(Translate_context* context)
12270 tree expr_tree = this->expr_->get_tree(context);
12271 if (expr_tree == error_mark_node)
12272 return error_mark_node;
12273 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
12274 gcc_assert(TREE_CODE(expr_size) == INTEGER_CST);
12275 tree space = context->gogo()->allocate_memory(this->expr_->type(),
12276 expr_size, this->location());
12277 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
12278 space = save_expr(space);
12279 tree ref = build_fold_indirect_ref_loc(this->location(), space);
12280 TREE_THIS_NOTRAP(ref) = 1;
12281 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
12282 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
12284 SET_EXPR_LOCATION(ret, this->location());
12288 // Allocate a composite literal on the heap.
12291 Expression::make_heap_composite(Expression* expr, source_location location)
12293 return new Heap_composite_expression(expr, location);
12296 // Class Receive_expression.
12298 // Return the type of a receive expression.
12301 Receive_expression::do_type()
12303 Channel_type* channel_type = this->channel_->type()->channel_type();
12304 if (channel_type == NULL)
12305 return Type::make_error_type();
12306 return channel_type->element_type();
12309 // Check types for a receive expression.
12312 Receive_expression::do_check_types(Gogo*)
12314 Type* type = this->channel_->type();
12315 if (type->is_error_type())
12317 this->set_is_error();
12320 if (type->channel_type() == NULL)
12322 this->report_error(_("expected channel"));
12325 if (!type->channel_type()->may_receive())
12327 this->report_error(_("invalid receive on send-only channel"));
12332 // Get a tree for a receive expression.
12335 Receive_expression::do_get_tree(Translate_context* context)
12337 Channel_type* channel_type = this->channel_->type()->channel_type();
12338 if (channel_type == NULL)
12340 gcc_assert(this->channel_->type()->is_error_type());
12341 return error_mark_node;
12343 Type* element_type = channel_type->element_type();
12344 tree element_type_tree = element_type->get_tree(context->gogo());
12346 tree channel = this->channel_->get_tree(context);
12347 if (element_type_tree == error_mark_node || channel == error_mark_node)
12348 return error_mark_node;
12350 return Gogo::receive_from_channel(element_type_tree, channel,
12351 this->for_select_, this->location());
12354 // Make a receive expression.
12356 Receive_expression*
12357 Expression::make_receive(Expression* channel, source_location location)
12359 return new Receive_expression(channel, location);
12362 // Class Send_expression.
12367 Send_expression::do_traverse(Traverse* traverse)
12369 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
12370 return TRAVERSE_EXIT;
12371 return Expression::traverse(&this->val_, traverse);
12377 Send_expression::do_type()
12379 if (this->is_value_discarded_)
12380 return Type::make_void_type();
12382 return Type::lookup_bool_type();
12388 Send_expression::do_determine_type(const Type_context*)
12390 this->channel_->determine_type_no_context();
12392 Type* type = this->channel_->type();
12393 Type_context subcontext;
12394 if (type->channel_type() != NULL)
12395 subcontext.type = type->channel_type()->element_type();
12396 this->val_->determine_type(&subcontext);
12402 Send_expression::do_check_types(Gogo*)
12404 Type* type = this->channel_->type();
12405 if (type->is_error_type())
12407 this->set_is_error();
12410 Channel_type* channel_type = type->channel_type();
12411 if (channel_type == NULL)
12413 error_at(this->location(), "left operand of %<<-%> must be channel");
12414 this->set_is_error();
12417 Type* element_type = channel_type->element_type();
12418 if (element_type != NULL
12419 && !Type::are_assignable(element_type, this->val_->type(), NULL))
12421 this->report_error(_("incompatible types in send"));
12424 if (!channel_type->may_send())
12426 this->report_error(_("invalid send on receive-only channel"));
12431 // Get a tree for a send expression.
12434 Send_expression::do_get_tree(Translate_context* context)
12436 tree channel = this->channel_->get_tree(context);
12437 tree val = this->val_->get_tree(context);
12438 if (channel == error_mark_node || val == error_mark_node)
12439 return error_mark_node;
12440 Channel_type* channel_type = this->channel_->type()->channel_type();
12441 val = Expression::convert_for_assignment(context,
12442 channel_type->element_type(),
12443 this->val_->type(),
12446 return Gogo::send_on_channel(channel, val, this->is_value_discarded_,
12447 this->for_select_, this->location());
12450 // Make a send expression
12453 Expression::make_send(Expression* channel, Expression* val,
12454 source_location location)
12456 return new Send_expression(channel, val, location);
12459 // An expression which evaluates to a pointer to the type descriptor
12462 class Type_descriptor_expression : public Expression
12465 Type_descriptor_expression(Type* type, source_location location)
12466 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
12473 { return Type::make_type_descriptor_ptr_type(); }
12476 do_determine_type(const Type_context*)
12484 do_get_tree(Translate_context* context)
12485 { return this->type_->type_descriptor_pointer(context->gogo()); }
12488 // The type for which this is the descriptor.
12492 // Make a type descriptor expression.
12495 Expression::make_type_descriptor(Type* type, source_location location)
12497 return new Type_descriptor_expression(type, location);
12500 // An expression which evaluates to some characteristic of a type.
12501 // This is only used to initialize fields of a type descriptor. Using
12502 // a new expression class is slightly inefficient but gives us a good
12503 // separation between the frontend and the middle-end with regard to
12504 // how types are laid out.
12506 class Type_info_expression : public Expression
12509 Type_info_expression(Type* type, Type_info type_info)
12510 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
12511 type_(type), type_info_(type_info)
12519 do_determine_type(const Type_context*)
12527 do_get_tree(Translate_context* context);
12530 // The type for which we are getting information.
12532 // What information we want.
12533 Type_info type_info_;
12536 // The type is chosen to match what the type descriptor struct
12540 Type_info_expression::do_type()
12542 switch (this->type_info_)
12544 case TYPE_INFO_SIZE:
12545 return Type::lookup_integer_type("uintptr");
12546 case TYPE_INFO_ALIGNMENT:
12547 case TYPE_INFO_FIELD_ALIGNMENT:
12548 return Type::lookup_integer_type("uint8");
12554 // Return type information in GENERIC.
12557 Type_info_expression::do_get_tree(Translate_context* context)
12559 tree type_tree = this->type_->get_tree(context->gogo());
12560 if (type_tree == error_mark_node)
12561 return error_mark_node;
12563 tree val_type_tree = this->type()->get_tree(context->gogo());
12564 gcc_assert(val_type_tree != error_mark_node);
12566 if (this->type_info_ == TYPE_INFO_SIZE)
12567 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12568 TYPE_SIZE_UNIT(type_tree));
12572 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
12573 val = go_type_alignment(type_tree);
12575 val = go_field_alignment(type_tree);
12576 return build_int_cstu(val_type_tree, val);
12580 // Make a type info expression.
12583 Expression::make_type_info(Type* type, Type_info type_info)
12585 return new Type_info_expression(type, type_info);
12588 // An expression which evaluates to the offset of a field within a
12589 // struct. This, like Type_info_expression, q.v., is only used to
12590 // initialize fields of a type descriptor.
12592 class Struct_field_offset_expression : public Expression
12595 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
12596 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
12597 type_(type), field_(field)
12603 { return Type::lookup_integer_type("uintptr"); }
12606 do_determine_type(const Type_context*)
12614 do_get_tree(Translate_context* context);
12617 // The type of the struct.
12618 Struct_type* type_;
12620 const Struct_field* field_;
12623 // Return a struct field offset in GENERIC.
12626 Struct_field_offset_expression::do_get_tree(Translate_context* context)
12628 tree type_tree = this->type_->get_tree(context->gogo());
12629 if (type_tree == error_mark_node)
12630 return error_mark_node;
12632 tree val_type_tree = this->type()->get_tree(context->gogo());
12633 gcc_assert(val_type_tree != error_mark_node);
12635 const Struct_field_list* fields = this->type_->fields();
12636 tree struct_field_tree = TYPE_FIELDS(type_tree);
12637 Struct_field_list::const_iterator p;
12638 for (p = fields->begin();
12639 p != fields->end();
12640 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
12642 gcc_assert(struct_field_tree != NULL_TREE);
12643 if (&*p == this->field_)
12646 gcc_assert(&*p == this->field_);
12648 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12649 byte_position(struct_field_tree));
12652 // Make an expression for a struct field offset.
12655 Expression::make_struct_field_offset(Struct_type* type,
12656 const Struct_field* field)
12658 return new Struct_field_offset_expression(type, field);
12661 // An expression which evaluates to the address of an unnamed label.
12663 class Label_addr_expression : public Expression
12666 Label_addr_expression(Label* label, source_location location)
12667 : Expression(EXPRESSION_LABEL_ADDR, location),
12674 { return Type::make_pointer_type(Type::make_void_type()); }
12677 do_determine_type(const Type_context*)
12682 { return new Label_addr_expression(this->label_, this->location()); }
12685 do_get_tree(Translate_context*)
12686 { return this->label_->get_addr(this->location()); }
12689 // The label whose address we are taking.
12693 // Make an expression for the address of an unnamed label.
12696 Expression::make_label_addr(Label* label, source_location location)
12698 return new Label_addr_expression(label, location);
12701 // Import an expression. This comes at the end in order to see the
12702 // various class definitions.
12705 Expression::import_expression(Import* imp)
12707 int c = imp->peek_char();
12708 if (imp->match_c_string("- ")
12709 || imp->match_c_string("! ")
12710 || imp->match_c_string("^ "))
12711 return Unary_expression::do_import(imp);
12713 return Binary_expression::do_import(imp);
12714 else if (imp->match_c_string("true")
12715 || imp->match_c_string("false"))
12716 return Boolean_expression::do_import(imp);
12718 return String_expression::do_import(imp);
12719 else if (c == '-' || (c >= '0' && c <= '9'))
12721 // This handles integers, floats and complex constants.
12722 return Integer_expression::do_import(imp);
12724 else if (imp->match_c_string("nil"))
12725 return Nil_expression::do_import(imp);
12726 else if (imp->match_c_string("convert"))
12727 return Type_conversion_expression::do_import(imp);
12730 error_at(imp->location(), "import error: expected expression");
12731 return Expression::make_error(imp->location());
12735 // Class Expression_list.
12737 // Traverse the list.
12740 Expression_list::traverse(Traverse* traverse)
12742 for (Expression_list::iterator p = this->begin();
12748 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12749 return TRAVERSE_EXIT;
12752 return TRAVERSE_CONTINUE;
12758 Expression_list::copy()
12760 Expression_list* ret = new Expression_list();
12761 for (Expression_list::iterator p = this->begin();
12766 ret->push_back(NULL);
12768 ret->push_back((*p)->copy());
12773 // Return whether an expression list has an error expression.
12776 Expression_list::contains_error() const
12778 for (Expression_list::const_iterator p = this->begin();
12781 if (*p != NULL && (*p)->is_error_expression())