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() || rhs_type->is_error())
209 return error_mark_node;
211 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
212 return error_mark_node;
214 Gogo* gogo = context->gogo();
216 tree lhs_type_tree = lhs_type->get_tree(gogo);
217 if (lhs_type_tree == error_mark_node)
218 return error_mark_node;
220 if (lhs_type->interface_type() != NULL)
222 if (rhs_type->interface_type() == NULL)
223 return Expression::convert_type_to_interface(context, lhs_type,
227 return Expression::convert_interface_to_interface(context, lhs_type,
231 else if (rhs_type->interface_type() != NULL)
232 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
234 else if (lhs_type->is_open_array_type()
235 && rhs_type->is_nil_type())
237 // Assigning nil to an open array.
238 gcc_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
240 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
242 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
243 tree field = TYPE_FIELDS(lhs_type_tree);
244 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
247 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
249 elt = VEC_quick_push(constructor_elt, init, NULL);
250 field = DECL_CHAIN(field);
251 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
254 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
256 elt = VEC_quick_push(constructor_elt, init, NULL);
257 field = DECL_CHAIN(field);
258 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
261 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
263 tree val = build_constructor(lhs_type_tree, init);
264 TREE_CONSTANT(val) = 1;
268 else if (rhs_type->is_nil_type())
270 // The left hand side should be a pointer type at the tree
272 gcc_assert(POINTER_TYPE_P(lhs_type_tree));
273 return fold_convert(lhs_type_tree, null_pointer_node);
275 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
277 // No conversion is needed.
280 else if (POINTER_TYPE_P(lhs_type_tree)
281 || INTEGRAL_TYPE_P(lhs_type_tree)
282 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
283 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
284 return fold_convert_loc(location, lhs_type_tree, rhs_tree);
285 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
286 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
288 // This conversion must be permitted by Go, or we wouldn't have
290 gcc_assert(int_size_in_bytes(lhs_type_tree)
291 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
292 return fold_build1_loc(location, VIEW_CONVERT_EXPR, lhs_type_tree,
297 gcc_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
302 // Return a tree for a conversion from a non-interface type to an
306 Expression::convert_type_to_interface(Translate_context* context,
307 Type* lhs_type, Type* rhs_type,
308 tree rhs_tree, source_location location)
310 Gogo* gogo = context->gogo();
311 Interface_type* lhs_interface_type = lhs_type->interface_type();
312 bool lhs_is_empty = lhs_interface_type->is_empty();
314 // Since RHS_TYPE is a static type, we can create the interface
315 // method table at compile time.
317 // When setting an interface to nil, we just set both fields to
319 if (rhs_type->is_nil_type())
320 return lhs_type->get_init_tree(gogo, false);
322 // This should have been checked already.
323 gcc_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
325 tree lhs_type_tree = lhs_type->get_tree(gogo);
326 if (lhs_type_tree == error_mark_node)
327 return error_mark_node;
329 // An interface is a tuple. If LHS_TYPE is an empty interface type,
330 // then the first field is the type descriptor for RHS_TYPE.
331 // Otherwise it is the interface method table for RHS_TYPE.
332 tree first_field_value;
334 first_field_value = rhs_type->type_descriptor_pointer(gogo);
337 // Build the interface method table for this interface and this
338 // object type: a list of function pointers for each interface
340 Named_type* rhs_named_type = rhs_type->named_type();
341 bool is_pointer = false;
342 if (rhs_named_type == NULL)
344 rhs_named_type = rhs_type->deref()->named_type();
348 if (rhs_named_type == NULL)
349 method_table = null_pointer_node;
352 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
354 first_field_value = fold_convert_loc(location, const_ptr_type_node,
357 if (first_field_value == error_mark_node)
358 return error_mark_node;
360 // Start building a constructor for the value we will return.
362 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
364 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
365 tree field = TYPE_FIELDS(lhs_type_tree);
366 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
367 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
369 elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value);
371 elt = VEC_quick_push(constructor_elt, init, NULL);
372 field = DECL_CHAIN(field);
373 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
376 if (rhs_type->points_to() != NULL)
378 // We are assigning a pointer to the interface; the interface
379 // holds the pointer itself.
380 elt->value = rhs_tree;
381 return build_constructor(lhs_type_tree, init);
384 // We are assigning a non-pointer value to the interface; the
385 // interface gets a copy of the value in the heap.
387 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
389 tree space = gogo->allocate_memory(rhs_type, object_size, location);
390 space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)),
392 space = save_expr(space);
394 tree ref = build_fold_indirect_ref_loc(location, space);
395 TREE_THIS_NOTRAP(ref) = 1;
396 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
399 elt->value = fold_convert_loc(location, TREE_TYPE(field), space);
401 return build2(COMPOUND_EXPR, lhs_type_tree, set,
402 build_constructor(lhs_type_tree, init));
405 // Return a tree for the type descriptor of RHS_TREE, which has
406 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
410 Expression::get_interface_type_descriptor(Translate_context*,
411 Type* rhs_type, tree rhs_tree,
412 source_location location)
414 tree rhs_type_tree = TREE_TYPE(rhs_tree);
415 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
416 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
417 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
419 if (rhs_type->interface_type()->is_empty())
421 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
422 "__type_descriptor") == 0);
426 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
428 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v)));
430 tree v1 = build_fold_indirect_ref_loc(location, v);
431 gcc_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
432 tree f = TYPE_FIELDS(TREE_TYPE(v1));
433 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
435 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
437 tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v,
438 fold_convert_loc(location, TREE_TYPE(v),
440 tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node);
441 return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1),
445 // Return a tree for the conversion of an interface type to an
449 Expression::convert_interface_to_interface(Translate_context* context,
450 Type *lhs_type, Type *rhs_type,
451 tree rhs_tree, bool for_type_guard,
452 source_location location)
454 Gogo* gogo = context->gogo();
455 Interface_type* lhs_interface_type = lhs_type->interface_type();
456 bool lhs_is_empty = lhs_interface_type->is_empty();
458 tree lhs_type_tree = lhs_type->get_tree(gogo);
459 if (lhs_type_tree == error_mark_node)
460 return error_mark_node;
462 // In the general case this requires runtime examination of the type
463 // method table to match it up with the interface methods.
465 // FIXME: If all of the methods in the right hand side interface
466 // also appear in the left hand side interface, then we don't need
467 // to do a runtime check, although we still need to build a new
470 // Get the type descriptor for the right hand side. This will be
471 // NULL for a nil interface.
473 if (!DECL_P(rhs_tree))
474 rhs_tree = save_expr(rhs_tree);
476 tree rhs_type_descriptor =
477 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
480 // The result is going to be a two element constructor.
482 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
484 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
485 tree field = TYPE_FIELDS(lhs_type_tree);
490 // A type assertion fails when converting a nil interface.
491 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
492 static tree assert_interface_decl;
493 tree call = Gogo::call_builtin(&assert_interface_decl,
495 "__go_assert_interface",
498 TREE_TYPE(lhs_type_descriptor),
500 TREE_TYPE(rhs_type_descriptor),
501 rhs_type_descriptor);
502 if (call == error_mark_node)
503 return error_mark_node;
504 // This will panic if the interface conversion fails.
505 TREE_NOTHROW(assert_interface_decl) = 0;
506 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
508 else if (lhs_is_empty)
510 // A convertion to an empty interface always succeeds, and the
511 // first field is just the type descriptor of the object.
512 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
513 "__type_descriptor") == 0);
514 gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
515 elt->value = rhs_type_descriptor;
519 // A conversion to a non-empty interface may fail, but unlike a
520 // type assertion converting nil will always succeed.
521 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
523 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
524 static tree convert_interface_decl;
525 tree call = Gogo::call_builtin(&convert_interface_decl,
527 "__go_convert_interface",
530 TREE_TYPE(lhs_type_descriptor),
532 TREE_TYPE(rhs_type_descriptor),
533 rhs_type_descriptor);
534 if (call == error_mark_node)
535 return error_mark_node;
536 // This will panic if the interface conversion fails.
537 TREE_NOTHROW(convert_interface_decl) = 0;
538 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
541 // The second field is simply the object pointer.
543 elt = VEC_quick_push(constructor_elt, init, NULL);
544 field = DECL_CHAIN(field);
545 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
548 tree rhs_type_tree = TREE_TYPE(rhs_tree);
549 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
550 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
551 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
552 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
555 return build_constructor(lhs_type_tree, init);
558 // Return a tree for the conversion of an interface type to a
559 // non-interface type.
562 Expression::convert_interface_to_type(Translate_context* context,
563 Type *lhs_type, Type* rhs_type,
564 tree rhs_tree, source_location location)
566 Gogo* gogo = context->gogo();
567 tree rhs_type_tree = TREE_TYPE(rhs_tree);
569 tree lhs_type_tree = lhs_type->get_tree(gogo);
570 if (lhs_type_tree == error_mark_node)
571 return error_mark_node;
573 // Call a function to check that the type is valid. The function
574 // will panic with an appropriate runtime type error if the type is
577 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
579 if (!DECL_P(rhs_tree))
580 rhs_tree = save_expr(rhs_tree);
582 tree rhs_type_descriptor =
583 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
586 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo);
588 static tree check_interface_type_decl;
589 tree call = Gogo::call_builtin(&check_interface_type_decl,
591 "__go_check_interface_type",
594 TREE_TYPE(lhs_type_descriptor),
596 TREE_TYPE(rhs_type_descriptor),
598 TREE_TYPE(rhs_inter_descriptor),
599 rhs_inter_descriptor);
600 if (call == error_mark_node)
601 return error_mark_node;
602 // This call will panic if the conversion is invalid.
603 TREE_NOTHROW(check_interface_type_decl) = 0;
605 // If the call succeeds, pull out the value.
606 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
607 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
608 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
609 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
612 // If the value is a pointer, then it is the value we want.
613 // Otherwise it points to the value.
614 if (lhs_type->points_to() == NULL)
616 val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val);
617 val = build_fold_indirect_ref_loc(location, val);
620 return build2(COMPOUND_EXPR, lhs_type_tree, call,
621 fold_convert_loc(location, lhs_type_tree, val));
624 // Convert an expression to a tree. This is implemented by the child
625 // class. Not that it is not in general safe to call this multiple
626 // times for a single expression, but that we don't catch such errors.
629 Expression::get_tree(Translate_context* context)
631 // The child may have marked this expression as having an error.
632 if (this->classification_ == EXPRESSION_ERROR)
633 return error_mark_node;
635 return this->do_get_tree(context);
638 // Return a tree for VAL in TYPE.
641 Expression::integer_constant_tree(mpz_t val, tree type)
643 if (type == error_mark_node)
644 return error_mark_node;
645 else if (TREE_CODE(type) == INTEGER_TYPE)
646 return double_int_to_tree(type,
647 mpz_get_double_int(type, val, true));
648 else if (TREE_CODE(type) == REAL_TYPE)
651 mpfr_init_set_z(fval, val, GMP_RNDN);
652 tree ret = Expression::float_constant_tree(fval, type);
656 else if (TREE_CODE(type) == COMPLEX_TYPE)
659 mpfr_init_set_z(fval, val, GMP_RNDN);
660 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
662 tree imag = build_real_from_int_cst(TREE_TYPE(type),
664 return build_complex(type, real, imag);
670 // Return a tree for VAL in TYPE.
673 Expression::float_constant_tree(mpfr_t val, tree type)
675 if (type == error_mark_node)
676 return error_mark_node;
677 else if (TREE_CODE(type) == INTEGER_TYPE)
681 mpfr_get_z(ival, val, GMP_RNDN);
682 tree ret = Expression::integer_constant_tree(ival, type);
686 else if (TREE_CODE(type) == REAL_TYPE)
689 real_from_mpfr(&r1, val, type, GMP_RNDN);
691 real_convert(&r2, TYPE_MODE(type), &r1);
692 return build_real(type, r2);
694 else if (TREE_CODE(type) == COMPLEX_TYPE)
697 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
699 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
700 tree imag = build_real_from_int_cst(TREE_TYPE(type),
702 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
708 // Return a tree for REAL/IMAG in TYPE.
711 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
713 if (type == error_mark_node)
714 return error_mark_node;
715 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
716 return Expression::float_constant_tree(real, type);
717 else if (TREE_CODE(type) == COMPLEX_TYPE)
720 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
722 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
725 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
727 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
729 return build_complex(type, build_real(TREE_TYPE(type), r2),
730 build_real(TREE_TYPE(type), r4));
736 // Return a tree which evaluates to true if VAL, of arbitrary integer
737 // type, is negative or is more than the maximum value of BOUND_TYPE.
738 // If SOFAR is not NULL, it is or'red into the result. The return
739 // value may be NULL if SOFAR is NULL.
742 Expression::check_bounds(tree val, tree bound_type, tree sofar,
745 tree val_type = TREE_TYPE(val);
746 tree ret = NULL_TREE;
748 if (!TYPE_UNSIGNED(val_type))
750 ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val,
751 build_int_cst(val_type, 0));
752 if (ret == boolean_false_node)
756 if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type))
757 || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type))
759 tree max = TYPE_MAX_VALUE(bound_type);
760 tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val,
761 fold_convert_loc(loc, val_type, max));
762 if (big == boolean_false_node)
764 else if (ret == NULL_TREE)
767 ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
771 if (ret == NULL_TREE)
773 else if (sofar == NULL_TREE)
776 return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
780 // Error expressions. This are used to avoid cascading errors.
782 class Error_expression : public Expression
785 Error_expression(source_location location)
786 : Expression(EXPRESSION_ERROR, location)
791 do_is_constant() const
795 do_integer_constant_value(bool, mpz_t val, Type**) const
802 do_float_constant_value(mpfr_t val, Type**) const
804 mpfr_set_ui(val, 0, GMP_RNDN);
809 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
811 mpfr_set_ui(real, 0, GMP_RNDN);
812 mpfr_set_ui(imag, 0, GMP_RNDN);
817 do_discarding_value()
822 { return Type::make_error_type(); }
825 do_determine_type(const Type_context*)
833 do_is_addressable() const
837 do_get_tree(Translate_context*)
838 { return error_mark_node; }
842 Expression::make_error(source_location location)
844 return new Error_expression(location);
847 // An expression which is really a type. This is used during parsing.
848 // It is an error if these survive after lowering.
851 Type_expression : public Expression
854 Type_expression(Type* type, source_location location)
855 : Expression(EXPRESSION_TYPE, location),
861 do_traverse(Traverse* traverse)
862 { return Type::traverse(this->type_, traverse); }
866 { return this->type_; }
869 do_determine_type(const Type_context*)
873 do_check_types(Gogo*)
874 { this->report_error(_("invalid use of type")); }
881 do_get_tree(Translate_context*)
882 { gcc_unreachable(); }
885 // The type which we are representing as an expression.
890 Expression::make_type(Type* type, source_location location)
892 return new Type_expression(type, location);
895 // Class Parser_expression.
898 Parser_expression::do_type()
900 // We should never really ask for the type of a Parser_expression.
901 // However, it can happen, at least when we have an invalid const
902 // whose initializer refers to the const itself. In that case we
903 // may ask for the type when lowering the const itself.
904 gcc_assert(saw_errors());
905 return Type::make_error_type();
908 // Class Var_expression.
910 // Lower a variable expression. Here we just make sure that the
911 // initialization expression of the variable has been lowered. This
912 // ensures that we will be able to determine the type of the variable
916 Var_expression::do_lower(Gogo* gogo, Named_object* function, int)
918 if (this->variable_->is_variable())
920 Variable* var = this->variable_->var_value();
921 // This is either a local variable or a global variable. A
922 // reference to a variable which is local to an enclosing
923 // function will be a reference to a field in a closure.
924 if (var->is_global())
926 var->lower_init_expression(gogo, function);
931 // Return the type of a reference to a variable.
934 Var_expression::do_type()
936 if (this->variable_->is_variable())
937 return this->variable_->var_value()->type();
938 else if (this->variable_->is_result_variable())
939 return this->variable_->result_var_value()->type();
944 // Determine the type of a reference to a variable.
947 Var_expression::do_determine_type(const Type_context*)
949 if (this->variable_->is_variable())
950 this->variable_->var_value()->determine_type();
953 // Something takes the address of this variable. This means that we
954 // may want to move the variable onto the heap.
957 Var_expression::do_address_taken(bool escapes)
961 else if (this->variable_->is_variable())
962 this->variable_->var_value()->set_address_taken();
963 else if (this->variable_->is_result_variable())
964 this->variable_->result_var_value()->set_address_taken();
969 // Get the tree for a reference to a variable.
972 Var_expression::do_get_tree(Translate_context* context)
974 return this->variable_->get_tree(context->gogo(), context->function());
977 // Make a reference to a variable in an expression.
980 Expression::make_var_reference(Named_object* var, source_location location)
983 return Expression::make_sink(location);
985 // FIXME: Creating a new object for each reference to a variable is
987 return new Var_expression(var, location);
990 // Class Temporary_reference_expression.
995 Temporary_reference_expression::do_type()
997 return this->statement_->type();
1000 // Called if something takes the address of this temporary variable.
1001 // We never have to move temporary variables to the heap, but we do
1002 // need to know that they must live in the stack rather than in a
1006 Temporary_reference_expression::do_address_taken(bool)
1008 this->statement_->set_is_address_taken();
1011 // Get a tree referring to the variable.
1014 Temporary_reference_expression::do_get_tree(Translate_context*)
1016 return this->statement_->get_decl();
1019 // Make a reference to a temporary variable.
1022 Expression::make_temporary_reference(Temporary_statement* statement,
1023 source_location location)
1025 return new Temporary_reference_expression(statement, location);
1028 // A sink expression--a use of the blank identifier _.
1030 class Sink_expression : public Expression
1033 Sink_expression(source_location location)
1034 : Expression(EXPRESSION_SINK, location),
1035 type_(NULL), var_(NULL_TREE)
1040 do_discarding_value()
1047 do_determine_type(const Type_context*);
1051 { return new Sink_expression(this->location()); }
1054 do_get_tree(Translate_context*);
1057 // The type of this sink variable.
1059 // The temporary variable we generate.
1063 // Return the type of a sink expression.
1066 Sink_expression::do_type()
1068 if (this->type_ == NULL)
1069 return Type::make_sink_type();
1073 // Determine the type of a sink expression.
1076 Sink_expression::do_determine_type(const Type_context* context)
1078 if (context->type != NULL)
1079 this->type_ = context->type;
1082 // Return a temporary variable for a sink expression. This will
1083 // presumably be a write-only variable which the middle-end will drop.
1086 Sink_expression::do_get_tree(Translate_context* context)
1088 if (this->var_ == NULL_TREE)
1090 gcc_assert(this->type_ != NULL && !this->type_->is_sink_type());
1091 this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()),
1097 // Make a sink expression.
1100 Expression::make_sink(source_location location)
1102 return new Sink_expression(location);
1105 // Class Func_expression.
1107 // FIXME: Can a function expression appear in a constant expression?
1108 // The value is unchanging. Initializing a constant to the address of
1109 // a function seems like it could work, though there might be little
1115 Func_expression::do_traverse(Traverse* traverse)
1117 return (this->closure_ == NULL
1119 : Expression::traverse(&this->closure_, traverse));
1122 // Return the type of a function expression.
1125 Func_expression::do_type()
1127 if (this->function_->is_function())
1128 return this->function_->func_value()->type();
1129 else if (this->function_->is_function_declaration())
1130 return this->function_->func_declaration_value()->type();
1135 // Get the tree for a function expression without evaluating the
1139 Func_expression::get_tree_without_closure(Gogo* gogo)
1141 Function_type* fntype;
1142 if (this->function_->is_function())
1143 fntype = this->function_->func_value()->type();
1144 else if (this->function_->is_function_declaration())
1145 fntype = this->function_->func_declaration_value()->type();
1149 // Builtin functions are handled specially by Call_expression. We
1150 // can't take their address.
1151 if (fntype->is_builtin())
1153 error_at(this->location(), "invalid use of special builtin function %qs",
1154 this->function_->name().c_str());
1155 return error_mark_node;
1158 Named_object* no = this->function_;
1160 tree id = no->get_id(gogo);
1161 if (id == error_mark_node)
1162 return error_mark_node;
1165 if (no->is_function())
1166 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1167 else if (no->is_function_declaration())
1168 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1172 if (fndecl == error_mark_node)
1173 return error_mark_node;
1175 return build_fold_addr_expr_loc(this->location(), fndecl);
1178 // Get the tree for a function expression. This is used when we take
1179 // the address of a function rather than simply calling it. If the
1180 // function has a closure, we must use a trampoline.
1183 Func_expression::do_get_tree(Translate_context* context)
1185 Gogo* gogo = context->gogo();
1187 tree fnaddr = this->get_tree_without_closure(gogo);
1188 if (fnaddr == error_mark_node)
1189 return error_mark_node;
1191 gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1192 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1193 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1195 // For a normal non-nested function call, that is all we have to do.
1196 if (!this->function_->is_function()
1197 || this->function_->func_value()->enclosing() == NULL)
1199 gcc_assert(this->closure_ == NULL);
1203 // For a nested function call, we have to always allocate a
1204 // trampoline. If we don't always allocate, then closures will not
1205 // be reliably distinct.
1206 Expression* closure = this->closure_;
1208 if (closure == NULL)
1209 closure_tree = null_pointer_node;
1212 // Get the value of the closure. This will be a pointer to
1213 // space allocated on the heap.
1214 closure_tree = closure->get_tree(context);
1215 if (closure_tree == error_mark_node)
1216 return error_mark_node;
1217 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1220 // Now we need to build some code on the heap. This code will load
1221 // the static chain pointer with the closure and then jump to the
1222 // body of the function. The normal gcc approach is to build the
1223 // code on the stack. Unfortunately we can not do that, as Go
1224 // permits us to return the function pointer.
1226 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1229 // Make a reference to a function in an expression.
1232 Expression::make_func_reference(Named_object* function, Expression* closure,
1233 source_location location)
1235 return new Func_expression(function, closure, location);
1238 // Class Unknown_expression.
1240 // Return the name of an unknown expression.
1243 Unknown_expression::name() const
1245 return this->named_object_->name();
1248 // Lower a reference to an unknown name.
1251 Unknown_expression::do_lower(Gogo*, Named_object*, int)
1253 source_location location = this->location();
1254 Named_object* no = this->named_object_;
1256 if (!no->is_unknown())
1260 real = no->unknown_value()->real_named_object();
1263 if (this->is_composite_literal_key_)
1265 error_at(location, "reference to undefined name %qs",
1266 this->named_object_->message_name().c_str());
1267 return Expression::make_error(location);
1270 switch (real->classification())
1272 case Named_object::NAMED_OBJECT_CONST:
1273 return Expression::make_const_reference(real, location);
1274 case Named_object::NAMED_OBJECT_TYPE:
1275 return Expression::make_type(real->type_value(), location);
1276 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1277 if (this->is_composite_literal_key_)
1279 error_at(location, "reference to undefined type %qs",
1280 real->message_name().c_str());
1281 return Expression::make_error(location);
1282 case Named_object::NAMED_OBJECT_VAR:
1283 return Expression::make_var_reference(real, location);
1284 case Named_object::NAMED_OBJECT_FUNC:
1285 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1286 return Expression::make_func_reference(real, NULL, location);
1287 case Named_object::NAMED_OBJECT_PACKAGE:
1288 if (this->is_composite_literal_key_)
1290 error_at(location, "unexpected reference to package");
1291 return Expression::make_error(location);
1297 // Make a reference to an unknown name.
1300 Expression::make_unknown_reference(Named_object* no, source_location location)
1302 gcc_assert(no->resolve()->is_unknown());
1303 return new Unknown_expression(no, location);
1306 // A boolean expression.
1308 class Boolean_expression : public Expression
1311 Boolean_expression(bool val, source_location location)
1312 : Expression(EXPRESSION_BOOLEAN, location),
1313 val_(val), type_(NULL)
1321 do_is_constant() const
1328 do_determine_type(const Type_context*);
1335 do_get_tree(Translate_context*)
1336 { return this->val_ ? boolean_true_node : boolean_false_node; }
1339 do_export(Export* exp) const
1340 { exp->write_c_string(this->val_ ? "true" : "false"); }
1345 // The type as determined by context.
1352 Boolean_expression::do_type()
1354 if (this->type_ == NULL)
1355 this->type_ = Type::make_boolean_type();
1359 // Set the type from the context.
1362 Boolean_expression::do_determine_type(const Type_context* context)
1364 if (this->type_ != NULL && !this->type_->is_abstract())
1366 else if (context->type != NULL && context->type->is_boolean_type())
1367 this->type_ = context->type;
1368 else if (!context->may_be_abstract)
1369 this->type_ = Type::lookup_bool_type();
1372 // Import a boolean constant.
1375 Boolean_expression::do_import(Import* imp)
1377 if (imp->peek_char() == 't')
1379 imp->require_c_string("true");
1380 return Expression::make_boolean(true, imp->location());
1384 imp->require_c_string("false");
1385 return Expression::make_boolean(false, imp->location());
1389 // Make a boolean expression.
1392 Expression::make_boolean(bool val, source_location location)
1394 return new Boolean_expression(val, location);
1397 // Class String_expression.
1402 String_expression::do_type()
1404 if (this->type_ == NULL)
1405 this->type_ = Type::make_string_type();
1409 // Set the type from the context.
1412 String_expression::do_determine_type(const Type_context* context)
1414 if (this->type_ != NULL && !this->type_->is_abstract())
1416 else if (context->type != NULL && context->type->is_string_type())
1417 this->type_ = context->type;
1418 else if (!context->may_be_abstract)
1419 this->type_ = Type::lookup_string_type();
1422 // Build a string constant.
1425 String_expression::do_get_tree(Translate_context* context)
1427 return context->gogo()->go_string_constant_tree(this->val_);
1430 // Export a string expression.
1433 String_expression::do_export(Export* exp) const
1436 s.reserve(this->val_.length() * 4 + 2);
1438 for (std::string::const_iterator p = this->val_.begin();
1439 p != this->val_.end();
1442 if (*p == '\\' || *p == '"')
1447 else if (*p >= 0x20 && *p < 0x7f)
1449 else if (*p == '\n')
1451 else if (*p == '\t')
1456 unsigned char c = *p;
1457 unsigned int dig = c >> 4;
1458 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1460 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1464 exp->write_string(s);
1467 // Import a string expression.
1470 String_expression::do_import(Import* imp)
1472 imp->require_c_string("\"");
1476 int c = imp->get_char();
1477 if (c == '"' || c == -1)
1480 val += static_cast<char>(c);
1483 c = imp->get_char();
1484 if (c == '\\' || c == '"')
1485 val += static_cast<char>(c);
1492 c = imp->get_char();
1493 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1494 c = imp->get_char();
1495 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1496 char v = (vh << 4) | vl;
1501 error_at(imp->location(), "bad string constant");
1502 return Expression::make_error(imp->location());
1506 return Expression::make_string(val, imp->location());
1509 // Make a string expression.
1512 Expression::make_string(const std::string& val, source_location location)
1514 return new String_expression(val, location);
1517 // Make an integer expression.
1519 class Integer_expression : public Expression
1522 Integer_expression(const mpz_t* val, Type* type, source_location location)
1523 : Expression(EXPRESSION_INTEGER, location),
1525 { mpz_init_set(this->val_, *val); }
1530 // Return whether VAL fits in the type.
1532 check_constant(mpz_t val, Type*, source_location);
1534 // Write VAL to export data.
1536 export_integer(Export* exp, const mpz_t val);
1540 do_is_constant() const
1544 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1550 do_determine_type(const Type_context* context);
1553 do_check_types(Gogo*);
1556 do_get_tree(Translate_context*);
1560 { return Expression::make_integer(&this->val_, this->type_,
1561 this->location()); }
1564 do_export(Export*) const;
1567 // The integer value.
1573 // Return an integer constant value.
1576 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1579 if (this->type_ != NULL)
1580 *ptype = this->type_;
1581 mpz_set(val, this->val_);
1585 // Return the current type. If we haven't set the type yet, we return
1586 // an abstract integer type.
1589 Integer_expression::do_type()
1591 if (this->type_ == NULL)
1592 this->type_ = Type::make_abstract_integer_type();
1596 // Set the type of the integer value. Here we may switch from an
1597 // abstract type to a real type.
1600 Integer_expression::do_determine_type(const Type_context* context)
1602 if (this->type_ != NULL && !this->type_->is_abstract())
1604 else if (context->type != NULL
1605 && (context->type->integer_type() != NULL
1606 || context->type->float_type() != NULL
1607 || context->type->complex_type() != NULL))
1608 this->type_ = context->type;
1609 else if (!context->may_be_abstract)
1610 this->type_ = Type::lookup_integer_type("int");
1613 // Return true if the integer VAL fits in the range of the type TYPE.
1614 // Otherwise give an error and return false. TYPE may be NULL.
1617 Integer_expression::check_constant(mpz_t val, Type* type,
1618 source_location location)
1622 Integer_type* itype = type->integer_type();
1623 if (itype == NULL || itype->is_abstract())
1626 int bits = mpz_sizeinbase(val, 2);
1628 if (itype->is_unsigned())
1630 // For an unsigned type we can only accept a nonnegative number,
1631 // and we must be able to represent at least BITS.
1632 if (mpz_sgn(val) >= 0
1633 && bits <= itype->bits())
1638 // For a signed type we need an extra bit to indicate the sign.
1639 // We have to handle the most negative integer specially.
1640 if (bits + 1 <= itype->bits()
1641 || (bits <= itype->bits()
1643 && (mpz_scan1(val, 0)
1644 == static_cast<unsigned long>(itype->bits() - 1))
1645 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1649 error_at(location, "integer constant overflow");
1653 // Check the type of an integer constant.
1656 Integer_expression::do_check_types(Gogo*)
1658 if (this->type_ == NULL)
1660 if (!Integer_expression::check_constant(this->val_, this->type_,
1662 this->set_is_error();
1665 // Get a tree for an integer constant.
1668 Integer_expression::do_get_tree(Translate_context* context)
1670 Gogo* gogo = context->gogo();
1672 if (this->type_ != NULL && !this->type_->is_abstract())
1673 type = this->type_->get_tree(gogo);
1674 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1676 // We are converting to an abstract floating point type.
1677 type = Type::lookup_float_type("float64")->get_tree(gogo);
1679 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1681 // We are converting to an abstract complex type.
1682 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
1686 // If we still have an abstract type here, then this is being
1687 // used in a constant expression which didn't get reduced for
1688 // some reason. Use a type which will fit the value. We use <,
1689 // not <=, because we need an extra bit for the sign bit.
1690 int bits = mpz_sizeinbase(this->val_, 2);
1691 if (bits < INT_TYPE_SIZE)
1692 type = Type::lookup_integer_type("int")->get_tree(gogo);
1694 type = Type::lookup_integer_type("int64")->get_tree(gogo);
1696 type = long_long_integer_type_node;
1698 return Expression::integer_constant_tree(this->val_, type);
1701 // Write VAL to export data.
1704 Integer_expression::export_integer(Export* exp, const mpz_t val)
1706 char* s = mpz_get_str(NULL, 10, val);
1707 exp->write_c_string(s);
1711 // Export an integer in a constant expression.
1714 Integer_expression::do_export(Export* exp) const
1716 Integer_expression::export_integer(exp, this->val_);
1717 // A trailing space lets us reliably identify the end of the number.
1718 exp->write_c_string(" ");
1721 // Import an integer, floating point, or complex value. This handles
1722 // all these types because they all start with digits.
1725 Integer_expression::do_import(Import* imp)
1727 std::string num = imp->read_identifier();
1728 imp->require_c_string(" ");
1729 if (!num.empty() && num[num.length() - 1] == 'i')
1732 size_t plus_pos = num.find('+', 1);
1733 size_t minus_pos = num.find('-', 1);
1735 if (plus_pos == std::string::npos)
1737 else if (minus_pos == std::string::npos)
1741 error_at(imp->location(), "bad number in import data: %qs",
1743 return Expression::make_error(imp->location());
1745 if (pos == std::string::npos)
1746 mpfr_set_ui(real, 0, GMP_RNDN);
1749 std::string real_str = num.substr(0, pos);
1750 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1752 error_at(imp->location(), "bad number in import data: %qs",
1754 return Expression::make_error(imp->location());
1758 std::string imag_str;
1759 if (pos == std::string::npos)
1762 imag_str = num.substr(pos);
1763 imag_str = imag_str.substr(0, imag_str.size() - 1);
1765 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1767 error_at(imp->location(), "bad number in import data: %qs",
1769 return Expression::make_error(imp->location());
1771 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1777 else if (num.find('.') == std::string::npos
1778 && num.find('E') == std::string::npos)
1781 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1783 error_at(imp->location(), "bad number in import data: %qs",
1785 return Expression::make_error(imp->location());
1787 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1794 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1796 error_at(imp->location(), "bad number in import data: %qs",
1798 return Expression::make_error(imp->location());
1800 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1806 // Build a new integer value.
1809 Expression::make_integer(const mpz_t* val, Type* type,
1810 source_location location)
1812 return new Integer_expression(val, type, location);
1817 class Float_expression : public Expression
1820 Float_expression(const mpfr_t* val, Type* type, source_location location)
1821 : Expression(EXPRESSION_FLOAT, location),
1824 mpfr_init_set(this->val_, *val, GMP_RNDN);
1827 // Constrain VAL to fit into TYPE.
1829 constrain_float(mpfr_t val, Type* type);
1831 // Return whether VAL fits in the type.
1833 check_constant(mpfr_t val, Type*, source_location);
1835 // Write VAL to export data.
1837 export_float(Export* exp, const mpfr_t val);
1841 do_is_constant() const
1845 do_float_constant_value(mpfr_t val, Type**) const;
1851 do_determine_type(const Type_context*);
1854 do_check_types(Gogo*);
1858 { return Expression::make_float(&this->val_, this->type_,
1859 this->location()); }
1862 do_get_tree(Translate_context*);
1865 do_export(Export*) const;
1868 // The floating point value.
1874 // Constrain VAL to fit into TYPE.
1877 Float_expression::constrain_float(mpfr_t val, Type* type)
1879 Float_type* ftype = type->float_type();
1880 if (ftype != NULL && !ftype->is_abstract())
1882 tree type_tree = ftype->type_tree();
1883 REAL_VALUE_TYPE rvt;
1884 real_from_mpfr(&rvt, val, type_tree, GMP_RNDN);
1885 real_convert(&rvt, TYPE_MODE(type_tree), &rvt);
1886 mpfr_from_real(val, &rvt, GMP_RNDN);
1890 // Return a floating point constant value.
1893 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
1895 if (this->type_ != NULL)
1896 *ptype = this->type_;
1897 mpfr_set(val, this->val_, GMP_RNDN);
1901 // Return the current type. If we haven't set the type yet, we return
1902 // an abstract float type.
1905 Float_expression::do_type()
1907 if (this->type_ == NULL)
1908 this->type_ = Type::make_abstract_float_type();
1912 // Set the type of the float value. Here we may switch from an
1913 // abstract type to a real type.
1916 Float_expression::do_determine_type(const Type_context* context)
1918 if (this->type_ != NULL && !this->type_->is_abstract())
1920 else if (context->type != NULL
1921 && (context->type->integer_type() != NULL
1922 || context->type->float_type() != NULL
1923 || context->type->complex_type() != NULL))
1924 this->type_ = context->type;
1925 else if (!context->may_be_abstract)
1926 this->type_ = Type::lookup_float_type("float64");
1929 // Return true if the floating point value VAL fits in the range of
1930 // the type TYPE. Otherwise give an error and return false. TYPE may
1934 Float_expression::check_constant(mpfr_t val, Type* type,
1935 source_location location)
1939 Float_type* ftype = type->float_type();
1940 if (ftype == NULL || ftype->is_abstract())
1943 // A NaN or Infinity always fits in the range of the type.
1944 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
1947 mp_exp_t exp = mpfr_get_exp(val);
1949 switch (ftype->bits())
1962 error_at(location, "floating point constant overflow");
1968 // Check the type of a float value.
1971 Float_expression::do_check_types(Gogo*)
1973 if (this->type_ == NULL)
1976 if (!Float_expression::check_constant(this->val_, this->type_,
1978 this->set_is_error();
1980 Integer_type* integer_type = this->type_->integer_type();
1981 if (integer_type != NULL)
1983 if (!mpfr_integer_p(this->val_))
1984 this->report_error(_("floating point constant truncated to integer"));
1987 gcc_assert(!integer_type->is_abstract());
1990 mpfr_get_z(ival, this->val_, GMP_RNDN);
1991 Integer_expression::check_constant(ival, integer_type,
1998 // Get a tree for a float constant.
2001 Float_expression::do_get_tree(Translate_context* context)
2003 Gogo* gogo = context->gogo();
2005 if (this->type_ != NULL && !this->type_->is_abstract())
2006 type = this->type_->get_tree(gogo);
2007 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2009 // We have an abstract integer type. We just hope for the best.
2010 type = Type::lookup_integer_type("int")->get_tree(gogo);
2014 // If we still have an abstract type here, then this is being
2015 // used in a constant expression which didn't get reduced. We
2016 // just use float64 and hope for the best.
2017 type = Type::lookup_float_type("float64")->get_tree(gogo);
2019 return Expression::float_constant_tree(this->val_, type);
2022 // Write a floating point number to export data.
2025 Float_expression::export_float(Export *exp, const mpfr_t val)
2028 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2030 exp->write_c_string("-");
2031 exp->write_c_string("0.");
2032 exp->write_c_string(*s == '-' ? s + 1 : s);
2035 snprintf(buf, sizeof buf, "E%ld", exponent);
2036 exp->write_c_string(buf);
2039 // Export a floating point number in a constant expression.
2042 Float_expression::do_export(Export* exp) const
2044 Float_expression::export_float(exp, this->val_);
2045 // A trailing space lets us reliably identify the end of the number.
2046 exp->write_c_string(" ");
2049 // Make a float expression.
2052 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2054 return new Float_expression(val, type, location);
2059 class Complex_expression : public Expression
2062 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2063 source_location location)
2064 : Expression(EXPRESSION_COMPLEX, location),
2067 mpfr_init_set(this->real_, *real, GMP_RNDN);
2068 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2071 // Constrain REAL/IMAG to fit into TYPE.
2073 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2075 // Return whether REAL/IMAG fits in the type.
2077 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2079 // Write REAL/IMAG to export data.
2081 export_complex(Export* exp, const mpfr_t real, const mpfr_t val);
2085 do_is_constant() const
2089 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2095 do_determine_type(const Type_context*);
2098 do_check_types(Gogo*);
2103 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2108 do_get_tree(Translate_context*);
2111 do_export(Export*) const;
2116 // The imaginary part;
2118 // The type if known.
2122 // Constrain REAL/IMAG to fit into TYPE.
2125 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2127 Complex_type* ctype = type->complex_type();
2128 if (ctype != NULL && !ctype->is_abstract())
2130 tree type_tree = ctype->type_tree();
2132 REAL_VALUE_TYPE rvt;
2133 real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN);
2134 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2135 mpfr_from_real(real, &rvt, GMP_RNDN);
2137 real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN);
2138 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2139 mpfr_from_real(imag, &rvt, GMP_RNDN);
2143 // Return a complex constant value.
2146 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2149 if (this->type_ != NULL)
2150 *ptype = this->type_;
2151 mpfr_set(real, this->real_, GMP_RNDN);
2152 mpfr_set(imag, this->imag_, GMP_RNDN);
2156 // Return the current type. If we haven't set the type yet, we return
2157 // an abstract complex type.
2160 Complex_expression::do_type()
2162 if (this->type_ == NULL)
2163 this->type_ = Type::make_abstract_complex_type();
2167 // Set the type of the complex value. Here we may switch from an
2168 // abstract type to a real type.
2171 Complex_expression::do_determine_type(const Type_context* context)
2173 if (this->type_ != NULL && !this->type_->is_abstract())
2175 else if (context->type != NULL
2176 && context->type->complex_type() != NULL)
2177 this->type_ = context->type;
2178 else if (!context->may_be_abstract)
2179 this->type_ = Type::lookup_complex_type("complex128");
2182 // Return true if the complex value REAL/IMAG fits in the range of the
2183 // type TYPE. Otherwise give an error and return false. TYPE may be
2187 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2188 source_location location)
2192 Complex_type* ctype = type->complex_type();
2193 if (ctype == NULL || ctype->is_abstract())
2197 switch (ctype->bits())
2209 // A NaN or Infinity always fits in the range of the type.
2210 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2212 if (mpfr_get_exp(real) > max_exp)
2214 error_at(location, "complex real part constant overflow");
2219 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2221 if (mpfr_get_exp(imag) > max_exp)
2223 error_at(location, "complex imaginary part constant overflow");
2231 // Check the type of a complex value.
2234 Complex_expression::do_check_types(Gogo*)
2236 if (this->type_ == NULL)
2239 if (!Complex_expression::check_constant(this->real_, this->imag_,
2240 this->type_, this->location()))
2241 this->set_is_error();
2244 // Get a tree for a complex constant.
2247 Complex_expression::do_get_tree(Translate_context* context)
2249 Gogo* gogo = context->gogo();
2251 if (this->type_ != NULL && !this->type_->is_abstract())
2252 type = this->type_->get_tree(gogo);
2255 // If we still have an abstract type here, this this is being
2256 // used in a constant expression which didn't get reduced. We
2257 // just use complex128 and hope for the best.
2258 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
2260 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2263 // Write REAL/IMAG to export data.
2266 Complex_expression::export_complex(Export* exp, const mpfr_t real,
2269 if (!mpfr_zero_p(real))
2271 Float_expression::export_float(exp, real);
2272 if (mpfr_sgn(imag) > 0)
2273 exp->write_c_string("+");
2275 Float_expression::export_float(exp, imag);
2276 exp->write_c_string("i");
2279 // Export a complex number in a constant expression.
2282 Complex_expression::do_export(Export* exp) const
2284 Complex_expression::export_complex(exp, this->real_, this->imag_);
2285 // A trailing space lets us reliably identify the end of the number.
2286 exp->write_c_string(" ");
2289 // Make a complex expression.
2292 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2293 source_location location)
2295 return new Complex_expression(real, imag, type, location);
2298 // Find a named object in an expression.
2300 class Find_named_object : public Traverse
2303 Find_named_object(Named_object* no)
2304 : Traverse(traverse_expressions),
2305 no_(no), found_(false)
2308 // Whether we found the object.
2311 { return this->found_; }
2315 expression(Expression**);
2318 // The object we are looking for.
2320 // Whether we found it.
2324 // A reference to a const in an expression.
2326 class Const_expression : public Expression
2329 Const_expression(Named_object* constant, source_location location)
2330 : Expression(EXPRESSION_CONST_REFERENCE, location),
2331 constant_(constant), type_(NULL), seen_(false)
2336 { return this->constant_; }
2338 // Check that the initializer does not refer to the constant itself.
2340 check_for_init_loop();
2344 do_traverse(Traverse*);
2347 do_lower(Gogo*, Named_object*, int);
2350 do_is_constant() const
2354 do_integer_constant_value(bool, mpz_t val, Type**) const;
2357 do_float_constant_value(mpfr_t val, Type**) const;
2360 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2363 do_string_constant_value(std::string* val) const
2364 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2369 // The type of a const is set by the declaration, not the use.
2371 do_determine_type(const Type_context*);
2374 do_check_types(Gogo*);
2381 do_get_tree(Translate_context* context);
2383 // When exporting a reference to a const as part of a const
2384 // expression, we export the value. We ignore the fact that it has
2387 do_export(Export* exp) const
2388 { this->constant_->const_value()->expr()->export_expression(exp); }
2392 Named_object* constant_;
2393 // The type of this reference. This is used if the constant has an
2396 // Used to prevent infinite recursion when a constant incorrectly
2397 // refers to itself.
2404 Const_expression::do_traverse(Traverse* traverse)
2406 if (this->type_ != NULL)
2407 return Type::traverse(this->type_, traverse);
2408 return TRAVERSE_CONTINUE;
2411 // Lower a constant expression. This is where we convert the
2412 // predeclared constant iota into an integer value.
2415 Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value)
2417 if (this->constant_->const_value()->expr()->classification()
2420 if (iota_value == -1)
2422 error_at(this->location(),
2423 "iota is only defined in const declarations");
2427 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2428 Expression* ret = Expression::make_integer(&val, NULL,
2434 // Make sure that the constant itself has been lowered.
2435 gogo->lower_constant(this->constant_);
2440 // Return an integer constant value.
2443 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2450 if (this->type_ != NULL)
2451 ctype = this->type_;
2453 ctype = this->constant_->const_value()->type();
2454 if (ctype != NULL && ctype->integer_type() == NULL)
2457 Expression* e = this->constant_->const_value()->expr();
2462 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2464 this->seen_ = false;
2468 && !Integer_expression::check_constant(val, ctype, this->location()))
2471 *ptype = ctype != NULL ? ctype : t;
2475 // Return a floating point constant value.
2478 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2484 if (this->type_ != NULL)
2485 ctype = this->type_;
2487 ctype = this->constant_->const_value()->type();
2488 if (ctype != NULL && ctype->float_type() == NULL)
2494 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2497 this->seen_ = false;
2499 if (r && ctype != NULL)
2501 if (!Float_expression::check_constant(val, ctype, this->location()))
2503 Float_expression::constrain_float(val, ctype);
2505 *ptype = ctype != NULL ? ctype : t;
2509 // Return a complex constant value.
2512 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2519 if (this->type_ != NULL)
2520 ctype = this->type_;
2522 ctype = this->constant_->const_value()->type();
2523 if (ctype != NULL && ctype->complex_type() == NULL)
2529 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2533 this->seen_ = false;
2535 if (r && ctype != NULL)
2537 if (!Complex_expression::check_constant(real, imag, ctype,
2540 Complex_expression::constrain_complex(real, imag, ctype);
2542 *ptype = ctype != NULL ? ctype : t;
2546 // Return the type of the const reference.
2549 Const_expression::do_type()
2551 if (this->type_ != NULL)
2554 Named_constant* nc = this->constant_->const_value();
2556 if (this->seen_ || nc->lowering())
2558 this->report_error(_("constant refers to itself"));
2559 this->type_ = Type::make_error_type();
2565 Type* ret = nc->type();
2569 this->seen_ = false;
2573 // During parsing, a named constant may have a NULL type, but we
2574 // must not return a NULL type here.
2575 ret = nc->expr()->type();
2577 this->seen_ = false;
2582 // Set the type of the const reference.
2585 Const_expression::do_determine_type(const Type_context* context)
2587 Type* ctype = this->constant_->const_value()->type();
2588 Type* cetype = (ctype != NULL
2590 : this->constant_->const_value()->expr()->type());
2591 if (ctype != NULL && !ctype->is_abstract())
2593 else if (context->type != NULL
2594 && (context->type->integer_type() != NULL
2595 || context->type->float_type() != NULL
2596 || context->type->complex_type() != NULL)
2597 && (cetype->integer_type() != NULL
2598 || cetype->float_type() != NULL
2599 || cetype->complex_type() != NULL))
2600 this->type_ = context->type;
2601 else if (context->type != NULL
2602 && context->type->is_string_type()
2603 && cetype->is_string_type())
2604 this->type_ = context->type;
2605 else if (context->type != NULL
2606 && context->type->is_boolean_type()
2607 && cetype->is_boolean_type())
2608 this->type_ = context->type;
2609 else if (!context->may_be_abstract)
2611 if (cetype->is_abstract())
2612 cetype = cetype->make_non_abstract_type();
2613 this->type_ = cetype;
2617 // Check for a loop in which the initializer of a constant refers to
2618 // the constant itself.
2621 Const_expression::check_for_init_loop()
2623 if (this->type_ != NULL && this->type_->is_error())
2628 this->report_error(_("constant refers to itself"));
2629 this->type_ = Type::make_error_type();
2633 Expression* init = this->constant_->const_value()->expr();
2634 Find_named_object find_named_object(this->constant_);
2637 Expression::traverse(&init, &find_named_object);
2638 this->seen_ = false;
2640 if (find_named_object.found())
2642 if (this->type_ == NULL || !this->type_->is_error())
2644 this->report_error(_("constant refers to itself"));
2645 this->type_ = Type::make_error_type();
2651 // Check types of a const reference.
2654 Const_expression::do_check_types(Gogo*)
2656 if (this->type_ != NULL && this->type_->is_error())
2659 this->check_for_init_loop();
2661 if (this->type_ == NULL || this->type_->is_abstract())
2664 // Check for integer overflow.
2665 if (this->type_->integer_type() != NULL)
2670 if (!this->integer_constant_value(true, ival, &dummy))
2674 Expression* cexpr = this->constant_->const_value()->expr();
2675 if (cexpr->float_constant_value(fval, &dummy))
2677 if (!mpfr_integer_p(fval))
2678 this->report_error(_("floating point constant "
2679 "truncated to integer"));
2682 mpfr_get_z(ival, fval, GMP_RNDN);
2683 Integer_expression::check_constant(ival, this->type_,
2693 // Return a tree for the const reference.
2696 Const_expression::do_get_tree(Translate_context* context)
2698 Gogo* gogo = context->gogo();
2700 if (this->type_ == NULL)
2701 type_tree = NULL_TREE;
2704 type_tree = this->type_->get_tree(gogo);
2705 if (type_tree == error_mark_node)
2706 return error_mark_node;
2709 // If the type has been set for this expression, but the underlying
2710 // object is an abstract int or float, we try to get the abstract
2711 // value. Otherwise we may lose something in the conversion.
2712 if (this->type_ != NULL
2713 && (this->constant_->const_value()->type() == NULL
2714 || this->constant_->const_value()->type()->is_abstract()))
2716 Expression* expr = this->constant_->const_value()->expr();
2720 if (expr->integer_constant_value(true, ival, &t))
2722 tree ret = Expression::integer_constant_tree(ival, type_tree);
2730 if (expr->float_constant_value(fval, &t))
2732 tree ret = Expression::float_constant_tree(fval, type_tree);
2739 if (expr->complex_constant_value(fval, imag, &t))
2741 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2750 tree const_tree = this->constant_->get_tree(gogo, context->function());
2751 if (this->type_ == NULL
2752 || const_tree == error_mark_node
2753 || TREE_TYPE(const_tree) == error_mark_node)
2757 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2758 ret = fold_convert(type_tree, const_tree);
2759 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2760 ret = fold(convert_to_integer(type_tree, const_tree));
2761 else if (TREE_CODE(type_tree) == REAL_TYPE)
2762 ret = fold(convert_to_real(type_tree, const_tree));
2763 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2764 ret = fold(convert_to_complex(type_tree, const_tree));
2770 // Make a reference to a constant in an expression.
2773 Expression::make_const_reference(Named_object* constant,
2774 source_location location)
2776 return new Const_expression(constant, location);
2779 // Find a named object in an expression.
2782 Find_named_object::expression(Expression** pexpr)
2784 switch ((*pexpr)->classification())
2786 case Expression::EXPRESSION_CONST_REFERENCE:
2788 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2789 if (ce->named_object() == this->no_)
2792 // We need to check a constant initializer explicitly, as
2793 // loops here will not be caught by the loop checking for
2794 // variable initializers.
2795 ce->check_for_init_loop();
2797 return TRAVERSE_CONTINUE;
2800 case Expression::EXPRESSION_VAR_REFERENCE:
2801 if ((*pexpr)->var_expression()->named_object() == this->no_)
2803 return TRAVERSE_CONTINUE;
2804 case Expression::EXPRESSION_FUNC_REFERENCE:
2805 if ((*pexpr)->func_expression()->named_object() == this->no_)
2807 return TRAVERSE_CONTINUE;
2809 return TRAVERSE_CONTINUE;
2811 this->found_ = true;
2812 return TRAVERSE_EXIT;
2817 class Nil_expression : public Expression
2820 Nil_expression(source_location location)
2821 : Expression(EXPRESSION_NIL, location)
2829 do_is_constant() const
2834 { return Type::make_nil_type(); }
2837 do_determine_type(const Type_context*)
2845 do_get_tree(Translate_context*)
2846 { return null_pointer_node; }
2849 do_export(Export* exp) const
2850 { exp->write_c_string("nil"); }
2853 // Import a nil expression.
2856 Nil_expression::do_import(Import* imp)
2858 imp->require_c_string("nil");
2859 return Expression::make_nil(imp->location());
2862 // Make a nil expression.
2865 Expression::make_nil(source_location location)
2867 return new Nil_expression(location);
2870 // The value of the predeclared constant iota. This is little more
2871 // than a marker. This will be lowered to an integer in
2872 // Const_expression::do_lower, which is where we know the value that
2875 class Iota_expression : public Parser_expression
2878 Iota_expression(source_location location)
2879 : Parser_expression(EXPRESSION_IOTA, location)
2884 do_lower(Gogo*, Named_object*, int)
2885 { gcc_unreachable(); }
2887 // There should only ever be one of these.
2890 { gcc_unreachable(); }
2893 // Make an iota expression. This is only called for one case: the
2894 // value of the predeclared constant iota.
2897 Expression::make_iota()
2899 static Iota_expression iota_expression(UNKNOWN_LOCATION);
2900 return &iota_expression;
2903 // A type conversion expression.
2905 class Type_conversion_expression : public Expression
2908 Type_conversion_expression(Type* type, Expression* expr,
2909 source_location location)
2910 : Expression(EXPRESSION_CONVERSION, location),
2911 type_(type), expr_(expr), may_convert_function_types_(false)
2914 // Return the type to which we are converting.
2917 { return this->type_; }
2919 // Return the expression which we are converting.
2922 { return this->expr_; }
2924 // Permit converting from one function type to another. This is
2925 // used internally for method expressions.
2927 set_may_convert_function_types()
2929 this->may_convert_function_types_ = true;
2932 // Import a type conversion expression.
2938 do_traverse(Traverse* traverse);
2941 do_lower(Gogo*, Named_object*, int);
2944 do_is_constant() const
2945 { return this->expr_->is_constant(); }
2948 do_integer_constant_value(bool, mpz_t, Type**) const;
2951 do_float_constant_value(mpfr_t, Type**) const;
2954 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
2957 do_string_constant_value(std::string*) const;
2961 { return this->type_; }
2964 do_determine_type(const Type_context*)
2966 Type_context subcontext(this->type_, false);
2967 this->expr_->determine_type(&subcontext);
2971 do_check_types(Gogo*);
2976 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2981 do_get_tree(Translate_context* context);
2984 do_export(Export*) const;
2987 // The type to convert to.
2989 // The expression to convert.
2991 // True if this is permitted to convert function types. This is
2992 // used internally for method expressions.
2993 bool may_convert_function_types_;
2999 Type_conversion_expression::do_traverse(Traverse* traverse)
3001 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3002 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3003 return TRAVERSE_EXIT;
3004 return TRAVERSE_CONTINUE;
3007 // Convert to a constant at lowering time.
3010 Type_conversion_expression::do_lower(Gogo*, Named_object*, int)
3012 Type* type = this->type_;
3013 Expression* val = this->expr_;
3014 source_location location = this->location();
3016 if (type->integer_type() != NULL)
3021 if (val->integer_constant_value(false, ival, &dummy))
3023 if (!Integer_expression::check_constant(ival, type, location))
3024 mpz_set_ui(ival, 0);
3025 Expression* ret = Expression::make_integer(&ival, type, location);
3032 if (val->float_constant_value(fval, &dummy))
3034 if (!mpfr_integer_p(fval))
3037 "floating point constant truncated to integer");
3038 return Expression::make_error(location);
3040 mpfr_get_z(ival, fval, GMP_RNDN);
3041 if (!Integer_expression::check_constant(ival, type, location))
3042 mpz_set_ui(ival, 0);
3043 Expression* ret = Expression::make_integer(&ival, type, location);
3052 if (type->float_type() != NULL)
3057 if (val->float_constant_value(fval, &dummy))
3059 if (!Float_expression::check_constant(fval, type, location))
3060 mpfr_set_ui(fval, 0, GMP_RNDN);
3061 Float_expression::constrain_float(fval, type);
3062 Expression *ret = Expression::make_float(&fval, type, location);
3069 if (type->complex_type() != NULL)
3076 if (val->complex_constant_value(real, imag, &dummy))
3078 if (!Complex_expression::check_constant(real, imag, type, location))
3080 mpfr_set_ui(real, 0, GMP_RNDN);
3081 mpfr_set_ui(imag, 0, GMP_RNDN);
3083 Complex_expression::constrain_complex(real, imag, type);
3084 Expression* ret = Expression::make_complex(&real, &imag, type,
3094 if (type->is_open_array_type() && type->named_type() == NULL)
3096 Type* element_type = type->array_type()->element_type()->forwarded();
3097 bool is_byte = element_type == Type::lookup_integer_type("uint8");
3098 bool is_int = element_type == Type::lookup_integer_type("int");
3099 if (is_byte || is_int)
3102 if (val->string_constant_value(&s))
3104 Expression_list* vals = new Expression_list();
3107 for (std::string::const_iterator p = s.begin();
3112 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3113 Expression* v = Expression::make_integer(&val,
3122 const char *p = s.data();
3123 const char *pend = s.data() + s.length();
3127 int adv = Lex::fetch_char(p, &c);
3130 warning_at(this->location(), 0,
3131 "invalid UTF-8 encoding");
3136 mpz_init_set_ui(val, c);
3137 Expression* v = Expression::make_integer(&val,
3145 return Expression::make_slice_composite_literal(type, vals,
3154 // Return the constant integer value if there is one.
3157 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3161 if (this->type_->integer_type() == NULL)
3167 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3169 if (!Integer_expression::check_constant(ival, this->type_,
3177 *ptype = this->type_;
3184 if (this->expr_->float_constant_value(fval, &dummy))
3186 mpfr_get_z(val, fval, GMP_RNDN);
3188 if (!Integer_expression::check_constant(val, this->type_,
3191 *ptype = this->type_;
3199 // Return the constant floating point value if there is one.
3202 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3205 if (this->type_->float_type() == NULL)
3211 if (this->expr_->float_constant_value(fval, &dummy))
3213 if (!Float_expression::check_constant(fval, this->type_,
3219 mpfr_set(val, fval, GMP_RNDN);
3221 Float_expression::constrain_float(val, this->type_);
3222 *ptype = this->type_;
3230 // Return the constant complex value if there is one.
3233 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3237 if (this->type_->complex_type() == NULL)
3245 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3247 if (!Complex_expression::check_constant(rval, ival, this->type_,
3254 mpfr_set(real, rval, GMP_RNDN);
3255 mpfr_set(imag, ival, GMP_RNDN);
3258 Complex_expression::constrain_complex(real, imag, this->type_);
3259 *ptype = this->type_;
3268 // Return the constant string value if there is one.
3271 Type_conversion_expression::do_string_constant_value(std::string* val) const
3273 if (this->type_->is_string_type()
3274 && this->expr_->type()->integer_type() != NULL)
3279 if (this->expr_->integer_constant_value(false, ival, &dummy))
3281 unsigned long ulval = mpz_get_ui(ival);
3282 if (mpz_cmp_ui(ival, ulval) == 0)
3284 Lex::append_char(ulval, true, val, this->location());
3292 // FIXME: Could handle conversion from const []int here.
3297 // Check that types are convertible.
3300 Type_conversion_expression::do_check_types(Gogo*)
3302 Type* type = this->type_;
3303 Type* expr_type = this->expr_->type();
3306 if (type->is_error() || expr_type->is_error())
3308 this->set_is_error();
3312 if (this->may_convert_function_types_
3313 && type->function_type() != NULL
3314 && expr_type->function_type() != NULL)
3317 if (Type::are_convertible(type, expr_type, &reason))
3320 error_at(this->location(), "%s", reason.c_str());
3321 this->set_is_error();
3324 // Get a tree for a type conversion.
3327 Type_conversion_expression::do_get_tree(Translate_context* context)
3329 Gogo* gogo = context->gogo();
3330 tree type_tree = this->type_->get_tree(gogo);
3331 tree expr_tree = this->expr_->get_tree(context);
3333 if (type_tree == error_mark_node
3334 || expr_tree == error_mark_node
3335 || TREE_TYPE(expr_tree) == error_mark_node)
3336 return error_mark_node;
3338 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3339 return fold_convert(type_tree, expr_tree);
3341 Type* type = this->type_;
3342 Type* expr_type = this->expr_->type();
3344 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3345 ret = Expression::convert_for_assignment(context, type, expr_type,
3346 expr_tree, this->location());
3347 else if (type->integer_type() != NULL)
3349 if (expr_type->integer_type() != NULL
3350 || expr_type->float_type() != NULL
3351 || expr_type->is_unsafe_pointer_type())
3352 ret = fold(convert_to_integer(type_tree, expr_tree));
3356 else if (type->float_type() != NULL)
3358 if (expr_type->integer_type() != NULL
3359 || expr_type->float_type() != NULL)
3360 ret = fold(convert_to_real(type_tree, expr_tree));
3364 else if (type->complex_type() != NULL)
3366 if (expr_type->complex_type() != NULL)
3367 ret = fold(convert_to_complex(type_tree, expr_tree));
3371 else if (type->is_string_type()
3372 && expr_type->integer_type() != NULL)
3374 expr_tree = fold_convert(integer_type_node, expr_tree);
3375 if (host_integerp(expr_tree, 0))
3377 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3379 Lex::append_char(intval, true, &s, this->location());
3380 Expression* se = Expression::make_string(s, this->location());
3381 return se->get_tree(context);
3384 static tree int_to_string_fndecl;
3385 ret = Gogo::call_builtin(&int_to_string_fndecl,
3387 "__go_int_to_string",
3391 fold_convert(integer_type_node, expr_tree));
3393 else if (type->is_string_type()
3394 && (expr_type->array_type() != NULL
3395 || (expr_type->points_to() != NULL
3396 && expr_type->points_to()->array_type() != NULL)))
3398 Type* t = expr_type;
3399 if (t->points_to() != NULL)
3402 expr_tree = build_fold_indirect_ref(expr_tree);
3404 if (!DECL_P(expr_tree))
3405 expr_tree = save_expr(expr_tree);
3406 Array_type* a = t->array_type();
3407 Type* e = a->element_type()->forwarded();
3408 gcc_assert(e->integer_type() != NULL);
3409 tree valptr = fold_convert(const_ptr_type_node,
3410 a->value_pointer_tree(gogo, expr_tree));
3411 tree len = a->length_tree(gogo, expr_tree);
3412 len = fold_convert_loc(this->location(), size_type_node, len);
3413 if (e->integer_type()->is_unsigned()
3414 && e->integer_type()->bits() == 8)
3416 static tree byte_array_to_string_fndecl;
3417 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3419 "__go_byte_array_to_string",
3422 const_ptr_type_node,
3429 gcc_assert(e == Type::lookup_integer_type("int"));
3430 static tree int_array_to_string_fndecl;
3431 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3433 "__go_int_array_to_string",
3436 const_ptr_type_node,
3442 else if (type->is_open_array_type() && expr_type->is_string_type())
3444 Type* e = type->array_type()->element_type()->forwarded();
3445 gcc_assert(e->integer_type() != NULL);
3446 if (e->integer_type()->is_unsigned()
3447 && e->integer_type()->bits() == 8)
3449 static tree string_to_byte_array_fndecl;
3450 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3452 "__go_string_to_byte_array",
3455 TREE_TYPE(expr_tree),
3460 gcc_assert(e == Type::lookup_integer_type("int"));
3461 static tree string_to_int_array_fndecl;
3462 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3464 "__go_string_to_int_array",
3467 TREE_TYPE(expr_tree),
3471 else if ((type->is_unsafe_pointer_type()
3472 && expr_type->points_to() != NULL)
3473 || (expr_type->is_unsafe_pointer_type()
3474 && type->points_to() != NULL))
3475 ret = fold_convert(type_tree, expr_tree);
3476 else if (type->is_unsafe_pointer_type()
3477 && expr_type->integer_type() != NULL)
3478 ret = convert_to_pointer(type_tree, expr_tree);
3479 else if (this->may_convert_function_types_
3480 && type->function_type() != NULL
3481 && expr_type->function_type() != NULL)
3482 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3484 ret = Expression::convert_for_assignment(context, type, expr_type,
3485 expr_tree, this->location());
3490 // Output a type conversion in a constant expression.
3493 Type_conversion_expression::do_export(Export* exp) const
3495 exp->write_c_string("convert(");
3496 exp->write_type(this->type_);
3497 exp->write_c_string(", ");
3498 this->expr_->export_expression(exp);
3499 exp->write_c_string(")");
3502 // Import a type conversion or a struct construction.
3505 Type_conversion_expression::do_import(Import* imp)
3507 imp->require_c_string("convert(");
3508 Type* type = imp->read_type();
3509 imp->require_c_string(", ");
3510 Expression* val = Expression::import_expression(imp);
3511 imp->require_c_string(")");
3512 return Expression::make_cast(type, val, imp->location());
3515 // Make a type cast expression.
3518 Expression::make_cast(Type* type, Expression* val, source_location location)
3520 if (type->is_error_type() || val->is_error_expression())
3521 return Expression::make_error(location);
3522 return new Type_conversion_expression(type, val, location);
3525 // Unary expressions.
3527 class Unary_expression : public Expression
3530 Unary_expression(Operator op, Expression* expr, source_location location)
3531 : Expression(EXPRESSION_UNARY, location),
3532 op_(op), escapes_(true), expr_(expr)
3535 // Return the operator.
3538 { return this->op_; }
3540 // Return the operand.
3543 { return this->expr_; }
3545 // Record that an address expression does not escape.
3547 set_does_not_escape()
3549 gcc_assert(this->op_ == OPERATOR_AND);
3550 this->escapes_ = false;
3553 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3554 // could be done, false if not.
3556 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3559 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3560 // could be done, false if not.
3562 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3564 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3565 // true if this could be done, false if not.
3567 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3575 do_traverse(Traverse* traverse)
3576 { return Expression::traverse(&this->expr_, traverse); }
3579 do_lower(Gogo*, Named_object*, int);
3582 do_is_constant() const;
3585 do_integer_constant_value(bool, mpz_t, Type**) const;
3588 do_float_constant_value(mpfr_t, Type**) const;
3591 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3597 do_determine_type(const Type_context*);
3600 do_check_types(Gogo*);
3605 return Expression::make_unary(this->op_, this->expr_->copy(),
3610 do_is_addressable() const
3611 { return this->op_ == OPERATOR_MULT; }
3614 do_get_tree(Translate_context*);
3617 do_export(Export*) const;
3620 // The unary operator to apply.
3622 // Normally true. False if this is an address expression which does
3623 // not escape the current function.
3629 // If we are taking the address of a composite literal, and the
3630 // contents are not constant, then we want to make a heap composite
3634 Unary_expression::do_lower(Gogo*, Named_object*, int)
3636 source_location loc = this->location();
3637 Operator op = this->op_;
3638 Expression* expr = this->expr_;
3640 if (op == OPERATOR_MULT && expr->is_type_expression())
3641 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3643 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3644 // moving x to the heap. FIXME: Is it worth doing a real escape
3645 // analysis here? This case is found in math/unsafe.go and is
3646 // therefore worth special casing.
3647 if (op == OPERATOR_MULT)
3649 Expression* e = expr;
3650 while (e->classification() == EXPRESSION_CONVERSION)
3652 Type_conversion_expression* te
3653 = static_cast<Type_conversion_expression*>(e);
3657 if (e->classification() == EXPRESSION_UNARY)
3659 Unary_expression* ue = static_cast<Unary_expression*>(e);
3660 if (ue->op_ == OPERATOR_AND)
3667 ue->set_does_not_escape();
3672 // Catching an invalid indirection of unsafe.Pointer here avoid
3673 // having to deal with TYPE_VOID in other places.
3674 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
3676 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3677 return Expression::make_error(this->location());
3680 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
3681 || op == OPERATOR_NOT || op == OPERATOR_XOR)
3683 Expression* ret = NULL;
3688 if (expr->integer_constant_value(false, eval, &etype))
3692 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
3693 ret = Expression::make_integer(&val, etype, loc);
3700 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
3705 if (expr->float_constant_value(fval, &ftype))
3709 if (Unary_expression::eval_float(op, fval, val))
3710 ret = Expression::make_float(&val, ftype, loc);
3721 if (expr->complex_constant_value(fval, ival, &ftype))
3727 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
3728 ret = Expression::make_complex(&real, &imag, ftype, loc);
3742 // Return whether a unary expression is a constant.
3745 Unary_expression::do_is_constant() const
3747 if (this->op_ == OPERATOR_MULT)
3749 // Indirecting through a pointer is only constant if the object
3750 // to which the expression points is constant, but we currently
3751 // have no way to determine that.
3754 else if (this->op_ == OPERATOR_AND)
3756 // Taking the address of a variable is constant if it is a
3757 // global variable, not constant otherwise. In other cases
3758 // taking the address is probably not a constant.
3759 Var_expression* ve = this->expr_->var_expression();
3762 Named_object* no = ve->named_object();
3763 return no->is_variable() && no->var_value()->is_global();
3768 return this->expr_->is_constant();
3771 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3772 // UVAL, if known; it may be NULL. Return true if this could be done,
3776 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3777 source_location location)
3784 case OPERATOR_MINUS:
3786 return Integer_expression::check_constant(val, utype, location);
3788 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3792 || utype->integer_type() == NULL
3793 || utype->integer_type()->is_abstract())
3797 // The number of HOST_WIDE_INTs that it takes to represent
3799 size_t count = ((mpz_sizeinbase(uval, 2)
3800 + HOST_BITS_PER_WIDE_INT
3802 / HOST_BITS_PER_WIDE_INT);
3804 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3805 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3808 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3809 gcc_assert(ecount <= count);
3811 // Trim down to the number of words required by the type.
3812 size_t obits = utype->integer_type()->bits();
3813 if (!utype->integer_type()->is_unsigned())
3815 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3816 / HOST_BITS_PER_WIDE_INT);
3817 gcc_assert(ocount <= count);
3819 for (size_t i = 0; i < ocount; ++i)
3822 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3824 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3827 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3831 return Integer_expression::check_constant(val, utype, location);
3840 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3841 // could be done, false if not.
3844 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
3849 mpfr_set(val, uval, GMP_RNDN);
3851 case OPERATOR_MINUS:
3852 mpfr_neg(val, uval, GMP_RNDN);
3864 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3865 // if this could be done, false if not.
3868 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
3869 mpfr_t real, mpfr_t imag)
3874 mpfr_set(real, rval, GMP_RNDN);
3875 mpfr_set(imag, ival, GMP_RNDN);
3877 case OPERATOR_MINUS:
3878 mpfr_neg(real, rval, GMP_RNDN);
3879 mpfr_neg(imag, ival, GMP_RNDN);
3891 // Return the integral constant value of a unary expression, if it has one.
3894 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
3900 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
3903 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
3909 // Return the floating point constant value of a unary expression, if
3913 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
3918 if (!this->expr_->float_constant_value(uval, ptype))
3921 ret = Unary_expression::eval_float(this->op_, uval, val);
3926 // Return the complex constant value of a unary expression, if it has
3930 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
3938 if (!this->expr_->complex_constant_value(rval, ival, ptype))
3941 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
3947 // Return the type of a unary expression.
3950 Unary_expression::do_type()
3955 case OPERATOR_MINUS:
3958 return this->expr_->type();
3961 return Type::make_pointer_type(this->expr_->type());
3965 Type* subtype = this->expr_->type();
3966 Type* points_to = subtype->points_to();
3967 if (points_to == NULL)
3968 return Type::make_error_type();
3977 // Determine abstract types for a unary expression.
3980 Unary_expression::do_determine_type(const Type_context* context)
3985 case OPERATOR_MINUS:
3988 this->expr_->determine_type(context);
3992 // Taking the address of something.
3994 Type* subtype = (context->type == NULL
3996 : context->type->points_to());
3997 Type_context subcontext(subtype, false);
3998 this->expr_->determine_type(&subcontext);
4003 // Indirecting through a pointer.
4005 Type* subtype = (context->type == NULL
4007 : Type::make_pointer_type(context->type));
4008 Type_context subcontext(subtype, false);
4009 this->expr_->determine_type(&subcontext);
4018 // Check types for a unary expression.
4021 Unary_expression::do_check_types(Gogo*)
4023 Type* type = this->expr_->type();
4024 if (type->is_error())
4026 this->set_is_error();
4033 case OPERATOR_MINUS:
4034 if (type->integer_type() == NULL
4035 && type->float_type() == NULL
4036 && type->complex_type() == NULL)
4037 this->report_error(_("expected numeric type"));
4042 if (type->integer_type() == NULL
4043 && !type->is_boolean_type())
4044 this->report_error(_("expected integer or boolean type"));
4048 if (!this->expr_->is_addressable())
4049 this->report_error(_("invalid operand for unary %<&%>"));
4051 this->expr_->address_taken(this->escapes_);
4055 // Indirecting through a pointer.
4056 if (type->points_to() == NULL)
4057 this->report_error(_("expected pointer"));
4065 // Get a tree for a unary expression.
4068 Unary_expression::do_get_tree(Translate_context* context)
4070 tree expr = this->expr_->get_tree(context);
4071 if (expr == error_mark_node)
4072 return error_mark_node;
4074 source_location loc = this->location();
4080 case OPERATOR_MINUS:
4082 tree type = TREE_TYPE(expr);
4083 tree compute_type = excess_precision_type(type);
4084 if (compute_type != NULL_TREE)
4085 expr = ::convert(compute_type, expr);
4086 tree ret = fold_build1_loc(loc, NEGATE_EXPR,
4087 (compute_type != NULL_TREE
4091 if (compute_type != NULL_TREE)
4092 ret = ::convert(type, ret);
4097 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4098 return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr);
4100 return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr,
4101 build_int_cst(TREE_TYPE(expr), 0));
4104 return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr);
4107 // We should not see a non-constant constructor here; cases
4108 // where we would see one should have been moved onto the heap
4109 // at parse time. Taking the address of a nonconstant
4110 // constructor will not do what the programmer expects.
4111 gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4112 gcc_assert(TREE_CODE(expr) != ADDR_EXPR);
4114 // Build a decl for a constant constructor.
4115 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4117 tree decl = build_decl(this->location(), VAR_DECL,
4118 create_tmp_var_name("C"), TREE_TYPE(expr));
4119 DECL_EXTERNAL(decl) = 0;
4120 TREE_PUBLIC(decl) = 0;
4121 TREE_READONLY(decl) = 1;
4122 TREE_CONSTANT(decl) = 1;
4123 TREE_STATIC(decl) = 1;
4124 TREE_ADDRESSABLE(decl) = 1;
4125 DECL_ARTIFICIAL(decl) = 1;
4126 DECL_INITIAL(decl) = expr;
4127 rest_of_decl_compilation(decl, 1, 0);
4131 return build_fold_addr_expr_loc(loc, expr);
4135 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4137 // If we are dereferencing the pointer to a large struct, we
4138 // need to check for nil. We don't bother to check for small
4139 // structs because we expect the system to crash on a nil
4140 // pointer dereference.
4141 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4142 if (s == -1 || s >= 4096)
4145 expr = save_expr(expr);
4146 tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node,
4148 fold_convert(TREE_TYPE(expr),
4149 null_pointer_node));
4150 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4152 expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr),
4153 build3(COND_EXPR, void_type_node,
4154 compare, crash, NULL_TREE),
4158 // If the type of EXPR is a recursive pointer type, then we
4159 // need to insert a cast before indirecting.
4160 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4162 Type* pt = this->expr_->type()->points_to();
4163 tree ind = pt->get_tree(context->gogo());
4164 expr = fold_convert_loc(loc, build_pointer_type(ind), expr);
4167 return build_fold_indirect_ref_loc(loc, expr);
4175 // Export a unary expression.
4178 Unary_expression::do_export(Export* exp) const
4183 exp->write_c_string("+ ");
4185 case OPERATOR_MINUS:
4186 exp->write_c_string("- ");
4189 exp->write_c_string("! ");
4192 exp->write_c_string("^ ");
4199 this->expr_->export_expression(exp);
4202 // Import a unary expression.
4205 Unary_expression::do_import(Import* imp)
4208 switch (imp->get_char())
4214 op = OPERATOR_MINUS;
4225 imp->require_c_string(" ");
4226 Expression* expr = Expression::import_expression(imp);
4227 return Expression::make_unary(op, expr, imp->location());
4230 // Make a unary expression.
4233 Expression::make_unary(Operator op, Expression* expr, source_location location)
4235 return new Unary_expression(op, expr, location);
4238 // If this is an indirection through a pointer, return the expression
4239 // being pointed through. Otherwise return this.
4244 if (this->classification_ == EXPRESSION_UNARY)
4246 Unary_expression* ue = static_cast<Unary_expression*>(this);
4247 if (ue->op() == OPERATOR_MULT)
4248 return ue->operand();
4253 // Class Binary_expression.
4258 Binary_expression::do_traverse(Traverse* traverse)
4260 int t = Expression::traverse(&this->left_, traverse);
4261 if (t == TRAVERSE_EXIT)
4262 return TRAVERSE_EXIT;
4263 return Expression::traverse(&this->right_, traverse);
4266 // Compare integer constants according to OP.
4269 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4272 int i = mpz_cmp(left_val, right_val);
4277 case OPERATOR_NOTEQ:
4292 // Compare floating point constants according to OP.
4295 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4300 i = mpfr_cmp(left_val, right_val);
4304 mpfr_init_set(lv, left_val, GMP_RNDN);
4306 mpfr_init_set(rv, right_val, GMP_RNDN);
4307 Float_expression::constrain_float(lv, type);
4308 Float_expression::constrain_float(rv, type);
4309 i = mpfr_cmp(lv, rv);
4317 case OPERATOR_NOTEQ:
4332 // Compare complex constants according to OP. Complex numbers may
4333 // only be compared for equality.
4336 Binary_expression::compare_complex(Operator op, Type* type,
4337 mpfr_t left_real, mpfr_t left_imag,
4338 mpfr_t right_real, mpfr_t right_imag)
4342 is_equal = (mpfr_cmp(left_real, right_real) == 0
4343 && mpfr_cmp(left_imag, right_imag) == 0);
4348 mpfr_init_set(lr, left_real, GMP_RNDN);
4349 mpfr_init_set(li, left_imag, GMP_RNDN);
4352 mpfr_init_set(rr, right_real, GMP_RNDN);
4353 mpfr_init_set(ri, right_imag, GMP_RNDN);
4354 Complex_expression::constrain_complex(lr, li, type);
4355 Complex_expression::constrain_complex(rr, ri, type);
4356 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4366 case OPERATOR_NOTEQ:
4373 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4374 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4375 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4376 // this could be done, false if not.
4379 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4380 Type* right_type, mpz_t right_val,
4381 source_location location, mpz_t val)
4383 bool is_shift_op = false;
4387 case OPERATOR_ANDAND:
4389 case OPERATOR_NOTEQ:
4394 // These return boolean values. We should probably handle them
4395 // anyhow in case a type conversion is used on the result.
4398 mpz_add(val, left_val, right_val);
4400 case OPERATOR_MINUS:
4401 mpz_sub(val, left_val, right_val);
4404 mpz_ior(val, left_val, right_val);
4407 mpz_xor(val, left_val, right_val);
4410 mpz_mul(val, left_val, right_val);
4413 if (mpz_sgn(right_val) != 0)
4414 mpz_tdiv_q(val, left_val, right_val);
4417 error_at(location, "division by zero");
4423 if (mpz_sgn(right_val) != 0)
4424 mpz_tdiv_r(val, left_val, right_val);
4427 error_at(location, "division by zero");
4432 case OPERATOR_LSHIFT:
4434 unsigned long shift = mpz_get_ui(right_val);
4435 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
4437 error_at(location, "shift count overflow");
4441 mpz_mul_2exp(val, left_val, shift);
4446 case OPERATOR_RSHIFT:
4448 unsigned long shift = mpz_get_ui(right_val);
4449 if (mpz_cmp_ui(right_val, shift) != 0)
4451 error_at(location, "shift count overflow");
4455 if (mpz_cmp_ui(left_val, 0) >= 0)
4456 mpz_tdiv_q_2exp(val, left_val, shift);
4458 mpz_fdiv_q_2exp(val, left_val, shift);
4464 mpz_and(val, left_val, right_val);
4466 case OPERATOR_BITCLEAR:
4470 mpz_com(tval, right_val);
4471 mpz_and(val, left_val, tval);
4479 Type* type = left_type;
4484 else if (type != right_type && right_type != NULL)
4486 if (type->is_abstract())
4488 else if (!right_type->is_abstract())
4490 // This look like a type error which should be diagnosed
4491 // elsewhere. Don't do anything here, to avoid an
4492 // unhelpful chain of error messages.
4498 if (type != NULL && !type->is_abstract())
4500 // We have to check the operands too, as we have implicitly
4501 // coerced them to TYPE.
4502 if ((type != left_type
4503 && !Integer_expression::check_constant(left_val, type, location))
4505 && type != right_type
4506 && !Integer_expression::check_constant(right_val, type,
4508 || !Integer_expression::check_constant(val, type, location))
4515 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4516 // Return true if this could be done, false if not.
4519 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4520 Type* right_type, mpfr_t right_val,
4521 mpfr_t val, source_location location)
4526 case OPERATOR_ANDAND:
4528 case OPERATOR_NOTEQ:
4533 // These return boolean values. We should probably handle them
4534 // anyhow in case a type conversion is used on the result.
4537 mpfr_add(val, left_val, right_val, GMP_RNDN);
4539 case OPERATOR_MINUS:
4540 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4545 case OPERATOR_BITCLEAR:
4548 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4551 if (mpfr_zero_p(right_val))
4552 error_at(location, "division by zero");
4553 mpfr_div(val, left_val, right_val, GMP_RNDN);
4557 case OPERATOR_LSHIFT:
4558 case OPERATOR_RSHIFT:
4564 Type* type = left_type;
4567 else if (type != right_type && right_type != NULL)
4569 if (type->is_abstract())
4571 else if (!right_type->is_abstract())
4573 // This looks like a type error which should be diagnosed
4574 // elsewhere. Don't do anything here, to avoid an unhelpful
4575 // chain of error messages.
4580 if (type != NULL && !type->is_abstract())
4582 if ((type != left_type
4583 && !Float_expression::check_constant(left_val, type, location))
4584 || (type != right_type
4585 && !Float_expression::check_constant(right_val, type,
4587 || !Float_expression::check_constant(val, type, location))
4588 mpfr_set_ui(val, 0, GMP_RNDN);
4594 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4595 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4596 // could be done, false if not.
4599 Binary_expression::eval_complex(Operator op, Type* left_type,
4600 mpfr_t left_real, mpfr_t left_imag,
4602 mpfr_t right_real, mpfr_t right_imag,
4603 mpfr_t real, mpfr_t imag,
4604 source_location location)
4609 case OPERATOR_ANDAND:
4611 case OPERATOR_NOTEQ:
4616 // These return boolean values and must be handled differently.
4619 mpfr_add(real, left_real, right_real, GMP_RNDN);
4620 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4622 case OPERATOR_MINUS:
4623 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4624 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4629 case OPERATOR_BITCLEAR:
4633 // You might think that multiplying two complex numbers would
4634 // be simple, and you would be right, until you start to think
4635 // about getting the right answer for infinity. If one
4636 // operand here is infinity and the other is anything other
4637 // than zero or NaN, then we are going to wind up subtracting
4638 // two infinity values. That will give us a NaN, but the
4639 // correct answer is infinity.
4643 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4647 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4651 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4655 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4657 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4658 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4660 // If we get NaN on both sides, check whether it should really
4661 // be infinity. The rule is that if either side of the
4662 // complex number is infinity, then the whole value is
4663 // infinity, even if the other side is NaN. So the only case
4664 // we have to fix is the one in which both sides are NaN.
4665 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4666 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4667 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4669 bool is_infinity = false;
4673 mpfr_init_set(lr, left_real, GMP_RNDN);
4674 mpfr_init_set(li, left_imag, GMP_RNDN);
4678 mpfr_init_set(rr, right_real, GMP_RNDN);
4679 mpfr_init_set(ri, right_imag, GMP_RNDN);
4681 // If the left side is infinity, then the result is
4683 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4685 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4686 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4687 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4688 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4691 mpfr_set_ui(rr, 0, GMP_RNDN);
4692 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4696 mpfr_set_ui(ri, 0, GMP_RNDN);
4697 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4702 // If the right side is infinity, then the result is
4704 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4706 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4707 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4708 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4709 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4712 mpfr_set_ui(lr, 0, GMP_RNDN);
4713 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4717 mpfr_set_ui(li, 0, GMP_RNDN);
4718 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4723 // If we got an overflow in the intermediate computations,
4724 // then the result is infinity.
4726 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4727 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4731 mpfr_set_ui(lr, 0, GMP_RNDN);
4732 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4736 mpfr_set_ui(li, 0, GMP_RNDN);
4737 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4741 mpfr_set_ui(rr, 0, GMP_RNDN);
4742 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4746 mpfr_set_ui(ri, 0, GMP_RNDN);
4747 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4754 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4755 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4756 mpfr_mul(lirr, li, rr, GMP_RNDN);
4757 mpfr_mul(liri, li, ri, GMP_RNDN);
4758 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4759 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4760 mpfr_set_inf(real, mpfr_sgn(real));
4761 mpfr_set_inf(imag, mpfr_sgn(imag));
4778 // For complex division we want to avoid having an
4779 // intermediate overflow turn the whole result in a NaN. We
4780 // scale the values to try to avoid this.
4782 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
4783 error_at(location, "division by zero");
4789 mpfr_abs(rra, right_real, GMP_RNDN);
4790 mpfr_abs(ria, right_imag, GMP_RNDN);
4793 mpfr_max(t, rra, ria, GMP_RNDN);
4797 mpfr_init_set(rr, right_real, GMP_RNDN);
4798 mpfr_init_set(ri, right_imag, GMP_RNDN);
4800 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4802 ilogbw = mpfr_get_exp(t);
4803 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4804 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4809 mpfr_mul(denom, rr, rr, GMP_RNDN);
4810 mpfr_mul(t, ri, ri, GMP_RNDN);
4811 mpfr_add(denom, denom, t, GMP_RNDN);
4813 mpfr_mul(real, left_real, rr, GMP_RNDN);
4814 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4815 mpfr_add(real, real, t, GMP_RNDN);
4816 mpfr_div(real, real, denom, GMP_RNDN);
4817 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4819 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4820 mpfr_mul(t, left_real, ri, GMP_RNDN);
4821 mpfr_sub(imag, imag, t, GMP_RNDN);
4822 mpfr_div(imag, imag, denom, GMP_RNDN);
4823 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4825 // If we wind up with NaN on both sides, check whether we
4826 // should really have infinity. The rule is that if either
4827 // side of the complex number is infinity, then the whole
4828 // value is infinity, even if the other side is NaN. So the
4829 // only case we have to fix is the one in which both sides are
4831 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4832 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4833 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4835 if (mpfr_zero_p(denom))
4837 mpfr_set_inf(real, mpfr_sgn(rr));
4838 mpfr_mul(real, real, left_real, GMP_RNDN);
4839 mpfr_set_inf(imag, mpfr_sgn(rr));
4840 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4842 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4843 && mpfr_number_p(rr) && mpfr_number_p(ri))
4845 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
4846 mpfr_copysign(t, t, left_real, GMP_RNDN);
4849 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
4850 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
4854 mpfr_mul(t3, t, rr, GMP_RNDN);
4858 mpfr_mul(t4, t2, ri, GMP_RNDN);
4860 mpfr_add(t3, t3, t4, GMP_RNDN);
4861 mpfr_set_inf(real, mpfr_sgn(t3));
4863 mpfr_mul(t3, t2, rr, GMP_RNDN);
4864 mpfr_mul(t4, t, ri, GMP_RNDN);
4865 mpfr_sub(t3, t3, t4, GMP_RNDN);
4866 mpfr_set_inf(imag, mpfr_sgn(t3));
4872 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
4873 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
4875 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4876 mpfr_copysign(t, t, rr, GMP_RNDN);
4879 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4880 mpfr_copysign(t2, t2, ri, GMP_RNDN);
4884 mpfr_mul(t3, left_real, t, GMP_RNDN);
4888 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
4890 mpfr_add(t3, t3, t4, GMP_RNDN);
4891 mpfr_set_ui(real, 0, GMP_RNDN);
4892 mpfr_mul(real, real, t3, GMP_RNDN);
4894 mpfr_mul(t3, left_imag, t, GMP_RNDN);
4895 mpfr_mul(t4, left_real, t2, GMP_RNDN);
4896 mpfr_sub(t3, t3, t4, GMP_RNDN);
4897 mpfr_set_ui(imag, 0, GMP_RNDN);
4898 mpfr_mul(imag, imag, t3, GMP_RNDN);
4916 case OPERATOR_LSHIFT:
4917 case OPERATOR_RSHIFT:
4923 Type* type = left_type;
4926 else if (type != right_type && right_type != NULL)
4928 if (type->is_abstract())
4930 else if (!right_type->is_abstract())
4932 // This looks like a type error which should be diagnosed
4933 // elsewhere. Don't do anything here, to avoid an unhelpful
4934 // chain of error messages.
4939 if (type != NULL && !type->is_abstract())
4941 if ((type != left_type
4942 && !Complex_expression::check_constant(left_real, left_imag,
4944 || (type != right_type
4945 && !Complex_expression::check_constant(right_real, right_imag,
4947 || !Complex_expression::check_constant(real, imag, type,
4950 mpfr_set_ui(real, 0, GMP_RNDN);
4951 mpfr_set_ui(imag, 0, GMP_RNDN);
4958 // Lower a binary expression. We have to evaluate constant
4959 // expressions now, in order to implement Go's unlimited precision
4963 Binary_expression::do_lower(Gogo*, Named_object*, int)
4965 source_location location = this->location();
4966 Operator op = this->op_;
4967 Expression* left = this->left_;
4968 Expression* right = this->right_;
4970 const bool is_comparison = (op == OPERATOR_EQEQ
4971 || op == OPERATOR_NOTEQ
4972 || op == OPERATOR_LT
4973 || op == OPERATOR_LE
4974 || op == OPERATOR_GT
4975 || op == OPERATOR_GE);
4977 // Integer constant expressions.
4983 mpz_init(right_val);
4985 if (left->integer_constant_value(false, left_val, &left_type)
4986 && right->integer_constant_value(false, right_val, &right_type))
4988 Expression* ret = NULL;
4989 if (left_type != right_type
4990 && left_type != NULL
4991 && right_type != NULL
4992 && left_type->base() != right_type->base()
4993 && op != OPERATOR_LSHIFT
4994 && op != OPERATOR_RSHIFT)
4996 // May be a type error--let it be diagnosed later.
4998 else if (is_comparison)
5000 bool b = Binary_expression::compare_integer(op, left_val,
5002 ret = Expression::make_cast(Type::lookup_bool_type(),
5003 Expression::make_boolean(b, location),
5011 if (Binary_expression::eval_integer(op, left_type, left_val,
5012 right_type, right_val,
5015 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
5017 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5019 else if (left_type == NULL)
5021 else if (right_type == NULL)
5023 else if (!left_type->is_abstract()
5024 && left_type->named_type() != NULL)
5026 else if (!right_type->is_abstract()
5027 && right_type->named_type() != NULL)
5029 else if (!left_type->is_abstract())
5031 else if (!right_type->is_abstract())
5033 else if (left_type->float_type() != NULL)
5035 else if (right_type->float_type() != NULL)
5037 else if (left_type->complex_type() != NULL)
5039 else if (right_type->complex_type() != NULL)
5043 ret = Expression::make_integer(&val, type, location);
5051 mpz_clear(right_val);
5052 mpz_clear(left_val);
5056 mpz_clear(right_val);
5057 mpz_clear(left_val);
5060 // Floating point constant expressions.
5063 mpfr_init(left_val);
5066 mpfr_init(right_val);
5068 if (left->float_constant_value(left_val, &left_type)
5069 && right->float_constant_value(right_val, &right_type))
5071 Expression* ret = NULL;
5072 if (left_type != right_type
5073 && left_type != NULL
5074 && right_type != NULL
5075 && left_type->base() != right_type->base()
5076 && op != OPERATOR_LSHIFT
5077 && op != OPERATOR_RSHIFT)
5079 // May be a type error--let it be diagnosed later.
5081 else if (is_comparison)
5083 bool b = Binary_expression::compare_float(op,
5087 left_val, right_val);
5088 ret = Expression::make_boolean(b, location);
5095 if (Binary_expression::eval_float(op, left_type, left_val,
5096 right_type, right_val, val,
5099 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5100 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5102 if (left_type == NULL)
5104 else if (right_type == NULL)
5106 else if (!left_type->is_abstract()
5107 && left_type->named_type() != NULL)
5109 else if (!right_type->is_abstract()
5110 && right_type->named_type() != NULL)
5112 else if (!left_type->is_abstract())
5114 else if (!right_type->is_abstract())
5116 else if (left_type->float_type() != NULL)
5118 else if (right_type->float_type() != NULL)
5122 ret = Expression::make_float(&val, type, location);
5130 mpfr_clear(right_val);
5131 mpfr_clear(left_val);
5135 mpfr_clear(right_val);
5136 mpfr_clear(left_val);
5139 // Complex constant expressions.
5143 mpfr_init(left_real);
5144 mpfr_init(left_imag);
5149 mpfr_init(right_real);
5150 mpfr_init(right_imag);
5153 if (left->complex_constant_value(left_real, left_imag, &left_type)
5154 && right->complex_constant_value(right_real, right_imag, &right_type))
5156 Expression* ret = NULL;
5157 if (left_type != right_type
5158 && left_type != NULL
5159 && right_type != NULL
5160 && left_type->base() != right_type->base())
5162 // May be a type error--let it be diagnosed later.
5164 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5166 bool b = Binary_expression::compare_complex(op,
5174 ret = Expression::make_boolean(b, location);
5183 if (Binary_expression::eval_complex(op, left_type,
5184 left_real, left_imag,
5186 right_real, right_imag,
5190 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5191 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5193 if (left_type == NULL)
5195 else if (right_type == NULL)
5197 else if (!left_type->is_abstract()
5198 && left_type->named_type() != NULL)
5200 else if (!right_type->is_abstract()
5201 && right_type->named_type() != NULL)
5203 else if (!left_type->is_abstract())
5205 else if (!right_type->is_abstract())
5207 else if (left_type->complex_type() != NULL)
5209 else if (right_type->complex_type() != NULL)
5213 ret = Expression::make_complex(&real, &imag, type,
5222 mpfr_clear(left_real);
5223 mpfr_clear(left_imag);
5224 mpfr_clear(right_real);
5225 mpfr_clear(right_imag);
5230 mpfr_clear(left_real);
5231 mpfr_clear(left_imag);
5232 mpfr_clear(right_real);
5233 mpfr_clear(right_imag);
5236 // String constant expressions.
5237 if (op == OPERATOR_PLUS
5238 && left->type()->is_string_type()
5239 && right->type()->is_string_type())
5241 std::string left_string;
5242 std::string right_string;
5243 if (left->string_constant_value(&left_string)
5244 && right->string_constant_value(&right_string))
5245 return Expression::make_string(left_string + right_string, location);
5251 // Return the integer constant value, if it has one.
5254 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5260 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5263 mpz_clear(left_val);
5268 mpz_init(right_val);
5270 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5273 mpz_clear(right_val);
5274 mpz_clear(left_val);
5279 if (left_type != right_type
5280 && left_type != NULL
5281 && right_type != NULL
5282 && left_type->base() != right_type->base()
5283 && this->op_ != OPERATOR_RSHIFT
5284 && this->op_ != OPERATOR_LSHIFT)
5287 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5288 right_type, right_val,
5289 this->location(), val);
5291 mpz_clear(right_val);
5292 mpz_clear(left_val);
5300 // Return the floating point constant value, if it has one.
5303 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5306 mpfr_init(left_val);
5308 if (!this->left_->float_constant_value(left_val, &left_type))
5310 mpfr_clear(left_val);
5315 mpfr_init(right_val);
5317 if (!this->right_->float_constant_value(right_val, &right_type))
5319 mpfr_clear(right_val);
5320 mpfr_clear(left_val);
5325 if (left_type != right_type
5326 && left_type != NULL
5327 && right_type != NULL
5328 && left_type->base() != right_type->base())
5331 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5332 right_type, right_val,
5333 val, this->location());
5335 mpfr_clear(left_val);
5336 mpfr_clear(right_val);
5344 // Return the complex constant value, if it has one.
5347 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5352 mpfr_init(left_real);
5353 mpfr_init(left_imag);
5355 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5357 mpfr_clear(left_real);
5358 mpfr_clear(left_imag);
5364 mpfr_init(right_real);
5365 mpfr_init(right_imag);
5367 if (!this->right_->complex_constant_value(right_real, right_imag,
5370 mpfr_clear(left_real);
5371 mpfr_clear(left_imag);
5372 mpfr_clear(right_real);
5373 mpfr_clear(right_imag);
5378 if (left_type != right_type
5379 && left_type != NULL
5380 && right_type != NULL
5381 && left_type->base() != right_type->base())
5384 ret = Binary_expression::eval_complex(this->op_, left_type,
5385 left_real, left_imag,
5387 right_real, right_imag,
5390 mpfr_clear(left_real);
5391 mpfr_clear(left_imag);
5392 mpfr_clear(right_real);
5393 mpfr_clear(right_imag);
5401 // Note that the value is being discarded.
5404 Binary_expression::do_discarding_value()
5406 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5407 this->right_->discarding_value();
5409 this->warn_about_unused_value();
5415 Binary_expression::do_type()
5417 if (this->classification() == EXPRESSION_ERROR)
5418 return Type::make_error_type();
5423 case OPERATOR_ANDAND:
5425 case OPERATOR_NOTEQ:
5430 return Type::lookup_bool_type();
5433 case OPERATOR_MINUS:
5440 case OPERATOR_BITCLEAR:
5442 Type* left_type = this->left_->type();
5443 Type* right_type = this->right_->type();
5444 if (left_type->is_error())
5446 else if (right_type->is_error())
5448 else if (!Type::are_compatible_for_binop(left_type, right_type))
5450 this->report_error(_("incompatible types in binary expression"));
5451 return Type::make_error_type();
5453 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
5455 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5457 else if (!left_type->is_abstract())
5459 else if (!right_type->is_abstract())
5461 else if (left_type->complex_type() != NULL)
5463 else if (right_type->complex_type() != NULL)
5465 else if (left_type->float_type() != NULL)
5467 else if (right_type->float_type() != NULL)
5473 case OPERATOR_LSHIFT:
5474 case OPERATOR_RSHIFT:
5475 return this->left_->type();
5482 // Set type for a binary expression.
5485 Binary_expression::do_determine_type(const Type_context* context)
5487 Type* tleft = this->left_->type();
5488 Type* tright = this->right_->type();
5490 // Both sides should have the same type, except for the shift
5491 // operations. For a comparison, we should ignore the incoming
5494 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5495 || this->op_ == OPERATOR_RSHIFT);
5497 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5498 || this->op_ == OPERATOR_NOTEQ
5499 || this->op_ == OPERATOR_LT
5500 || this->op_ == OPERATOR_LE
5501 || this->op_ == OPERATOR_GT
5502 || this->op_ == OPERATOR_GE);
5504 Type_context subcontext(*context);
5508 // In a comparison, the context does not determine the types of
5510 subcontext.type = NULL;
5513 // Set the context for the left hand operand.
5516 // The right hand operand plays no role in determining the type
5517 // of the left hand operand. A shift of an abstract integer in
5518 // a string context gets special treatment, which may be a
5520 if (subcontext.type != NULL
5521 && subcontext.type->is_string_type()
5522 && tleft->is_abstract())
5523 error_at(this->location(), "shift of non-integer operand");
5525 else if (!tleft->is_abstract())
5526 subcontext.type = tleft;
5527 else if (!tright->is_abstract())
5528 subcontext.type = tright;
5529 else if (subcontext.type == NULL)
5531 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5532 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5533 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5535 // Both sides have an abstract integer, abstract float, or
5536 // abstract complex type. Just let CONTEXT determine
5537 // whether they may remain abstract or not.
5539 else if (tleft->complex_type() != NULL)
5540 subcontext.type = tleft;
5541 else if (tright->complex_type() != NULL)
5542 subcontext.type = tright;
5543 else if (tleft->float_type() != NULL)
5544 subcontext.type = tleft;
5545 else if (tright->float_type() != NULL)
5546 subcontext.type = tright;
5548 subcontext.type = tleft;
5550 if (subcontext.type != NULL && !context->may_be_abstract)
5551 subcontext.type = subcontext.type->make_non_abstract_type();
5554 this->left_->determine_type(&subcontext);
5556 // The context for the right hand operand is the same as for the
5557 // left hand operand, except for a shift operator.
5560 subcontext.type = Type::lookup_integer_type("uint");
5561 subcontext.may_be_abstract = false;
5564 this->right_->determine_type(&subcontext);
5567 // Report an error if the binary operator OP does not support TYPE.
5568 // Return whether the operation is OK. This should not be used for
5572 Binary_expression::check_operator_type(Operator op, Type* type,
5573 source_location location)
5578 case OPERATOR_ANDAND:
5579 if (!type->is_boolean_type())
5581 error_at(location, "expected boolean type");
5587 case OPERATOR_NOTEQ:
5588 if (type->integer_type() == NULL
5589 && type->float_type() == NULL
5590 && type->complex_type() == NULL
5591 && !type->is_string_type()
5592 && type->points_to() == NULL
5593 && !type->is_nil_type()
5594 && !type->is_boolean_type()
5595 && type->interface_type() == NULL
5596 && (type->array_type() == NULL
5597 || type->array_type()->length() != NULL)
5598 && type->map_type() == NULL
5599 && type->channel_type() == NULL
5600 && type->function_type() == NULL)
5603 ("expected integer, floating, complex, string, pointer, "
5604 "boolean, interface, slice, map, channel, "
5605 "or function type"));
5614 if (type->integer_type() == NULL
5615 && type->float_type() == NULL
5616 && !type->is_string_type())
5618 error_at(location, "expected integer, floating, or string type");
5624 case OPERATOR_PLUSEQ:
5625 if (type->integer_type() == NULL
5626 && type->float_type() == NULL
5627 && type->complex_type() == NULL
5628 && !type->is_string_type())
5631 "expected integer, floating, complex, or string type");
5636 case OPERATOR_MINUS:
5637 case OPERATOR_MINUSEQ:
5639 case OPERATOR_MULTEQ:
5641 case OPERATOR_DIVEQ:
5642 if (type->integer_type() == NULL
5643 && type->float_type() == NULL
5644 && type->complex_type() == NULL)
5646 error_at(location, "expected integer, floating, or complex type");
5652 case OPERATOR_MODEQ:
5656 case OPERATOR_ANDEQ:
5658 case OPERATOR_XOREQ:
5659 case OPERATOR_BITCLEAR:
5660 case OPERATOR_BITCLEAREQ:
5661 if (type->integer_type() == NULL)
5663 error_at(location, "expected integer type");
5678 Binary_expression::do_check_types(Gogo*)
5680 if (this->classification() == EXPRESSION_ERROR)
5683 Type* left_type = this->left_->type();
5684 Type* right_type = this->right_->type();
5685 if (left_type->is_error() || right_type->is_error())
5687 this->set_is_error();
5691 if (this->op_ == OPERATOR_EQEQ
5692 || this->op_ == OPERATOR_NOTEQ
5693 || this->op_ == OPERATOR_LT
5694 || this->op_ == OPERATOR_LE
5695 || this->op_ == OPERATOR_GT
5696 || this->op_ == OPERATOR_GE)
5698 if (!Type::are_assignable(left_type, right_type, NULL)
5699 && !Type::are_assignable(right_type, left_type, NULL))
5701 this->report_error(_("incompatible types in binary expression"));
5704 if (!Binary_expression::check_operator_type(this->op_, left_type,
5706 || !Binary_expression::check_operator_type(this->op_, right_type,
5709 this->set_is_error();
5713 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5715 if (!Type::are_compatible_for_binop(left_type, right_type))
5717 this->report_error(_("incompatible types in binary expression"));
5720 if (!Binary_expression::check_operator_type(this->op_, left_type,
5723 this->set_is_error();
5729 if (left_type->integer_type() == NULL)
5730 this->report_error(_("shift of non-integer operand"));
5732 if (!right_type->is_abstract()
5733 && (right_type->integer_type() == NULL
5734 || !right_type->integer_type()->is_unsigned()))
5735 this->report_error(_("shift count not unsigned integer"));
5741 if (this->right_->integer_constant_value(true, val, &type))
5743 if (mpz_sgn(val) < 0)
5745 this->report_error(_("negative shift count"));
5747 source_location rloc = this->right_->location();
5748 this->right_ = Expression::make_integer(&val, right_type,
5757 // Get a tree for a binary expression.
5760 Binary_expression::do_get_tree(Translate_context* context)
5762 tree left = this->left_->get_tree(context);
5763 tree right = this->right_->get_tree(context);
5765 if (left == error_mark_node || right == error_mark_node)
5766 return error_mark_node;
5768 enum tree_code code;
5769 bool use_left_type = true;
5770 bool is_shift_op = false;
5774 case OPERATOR_NOTEQ:
5779 return Expression::comparison_tree(context, this->op_,
5780 this->left_->type(), left,
5781 this->right_->type(), right,
5785 code = TRUTH_ORIF_EXPR;
5786 use_left_type = false;
5788 case OPERATOR_ANDAND:
5789 code = TRUTH_ANDIF_EXPR;
5790 use_left_type = false;
5795 case OPERATOR_MINUS:
5799 code = BIT_IOR_EXPR;
5802 code = BIT_XOR_EXPR;
5809 Type *t = this->left_->type();
5810 if (t->float_type() != NULL || t->complex_type() != NULL)
5813 code = TRUNC_DIV_EXPR;
5817 code = TRUNC_MOD_EXPR;
5819 case OPERATOR_LSHIFT:
5823 case OPERATOR_RSHIFT:
5828 code = BIT_AND_EXPR;
5830 case OPERATOR_BITCLEAR:
5831 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5832 code = BIT_AND_EXPR;
5838 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5840 if (this->left_->type()->is_string_type())
5842 gcc_assert(this->op_ == OPERATOR_PLUS);
5843 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5844 static tree string_plus_decl;
5845 return Gogo::call_builtin(&string_plus_decl,
5856 tree compute_type = excess_precision_type(type);
5857 if (compute_type != NULL_TREE)
5859 left = ::convert(compute_type, left);
5860 right = ::convert(compute_type, right);
5863 tree eval_saved = NULL_TREE;
5866 // Make sure the values are evaluated.
5867 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
5869 left = save_expr(left);
5872 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
5874 right = save_expr(right);
5875 if (eval_saved == NULL_TREE)
5878 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
5879 void_type_node, eval_saved, right);
5883 tree ret = fold_build2_loc(this->location(),
5885 compute_type != NULL_TREE ? compute_type : type,
5888 if (compute_type != NULL_TREE)
5889 ret = ::convert(type, ret);
5891 // In Go, a shift larger than the size of the type is well-defined.
5892 // This is not true in GENERIC, so we need to insert a conditional.
5895 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
5896 gcc_assert(this->left_->type()->integer_type() != NULL);
5897 int bits = TYPE_PRECISION(TREE_TYPE(left));
5899 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
5900 build_int_cst_type(TREE_TYPE(right), bits));
5902 tree overflow_result = fold_convert_loc(this->location(),
5905 if (this->op_ == OPERATOR_RSHIFT
5906 && !this->left_->type()->integer_type()->is_unsigned())
5908 tree neg = fold_build2_loc(this->location(), LT_EXPR,
5909 boolean_type_node, left,
5910 fold_convert_loc(this->location(),
5912 integer_zero_node));
5913 tree neg_one = fold_build2_loc(this->location(),
5914 MINUS_EXPR, TREE_TYPE(left),
5915 fold_convert_loc(this->location(),
5918 fold_convert_loc(this->location(),
5921 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
5922 TREE_TYPE(left), neg, neg_one,
5926 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
5927 compare, ret, overflow_result);
5929 if (eval_saved != NULL_TREE)
5930 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
5931 TREE_TYPE(ret), eval_saved, ret);
5937 // Export a binary expression.
5940 Binary_expression::do_export(Export* exp) const
5942 exp->write_c_string("(");
5943 this->left_->export_expression(exp);
5947 exp->write_c_string(" || ");
5949 case OPERATOR_ANDAND:
5950 exp->write_c_string(" && ");
5953 exp->write_c_string(" == ");
5955 case OPERATOR_NOTEQ:
5956 exp->write_c_string(" != ");
5959 exp->write_c_string(" < ");
5962 exp->write_c_string(" <= ");
5965 exp->write_c_string(" > ");
5968 exp->write_c_string(" >= ");
5971 exp->write_c_string(" + ");
5973 case OPERATOR_MINUS:
5974 exp->write_c_string(" - ");
5977 exp->write_c_string(" | ");
5980 exp->write_c_string(" ^ ");
5983 exp->write_c_string(" * ");
5986 exp->write_c_string(" / ");
5989 exp->write_c_string(" % ");
5991 case OPERATOR_LSHIFT:
5992 exp->write_c_string(" << ");
5994 case OPERATOR_RSHIFT:
5995 exp->write_c_string(" >> ");
5998 exp->write_c_string(" & ");
6000 case OPERATOR_BITCLEAR:
6001 exp->write_c_string(" &^ ");
6006 this->right_->export_expression(exp);
6007 exp->write_c_string(")");
6010 // Import a binary expression.
6013 Binary_expression::do_import(Import* imp)
6015 imp->require_c_string("(");
6017 Expression* left = Expression::import_expression(imp);
6020 if (imp->match_c_string(" || "))
6025 else if (imp->match_c_string(" && "))
6027 op = OPERATOR_ANDAND;
6030 else if (imp->match_c_string(" == "))
6035 else if (imp->match_c_string(" != "))
6037 op = OPERATOR_NOTEQ;
6040 else if (imp->match_c_string(" < "))
6045 else if (imp->match_c_string(" <= "))
6050 else if (imp->match_c_string(" > "))
6055 else if (imp->match_c_string(" >= "))
6060 else if (imp->match_c_string(" + "))
6065 else if (imp->match_c_string(" - "))
6067 op = OPERATOR_MINUS;
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(" / "))
6090 else if (imp->match_c_string(" % "))
6095 else if (imp->match_c_string(" << "))
6097 op = OPERATOR_LSHIFT;
6100 else if (imp->match_c_string(" >> "))
6102 op = OPERATOR_RSHIFT;
6105 else if (imp->match_c_string(" & "))
6110 else if (imp->match_c_string(" &^ "))
6112 op = OPERATOR_BITCLEAR;
6117 error_at(imp->location(), "unrecognized binary operator");
6118 return Expression::make_error(imp->location());
6121 Expression* right = Expression::import_expression(imp);
6123 imp->require_c_string(")");
6125 return Expression::make_binary(op, left, right, imp->location());
6128 // Make a binary expression.
6131 Expression::make_binary(Operator op, Expression* left, Expression* right,
6132 source_location location)
6134 return new Binary_expression(op, left, right, location);
6137 // Implement a comparison.
6140 Expression::comparison_tree(Translate_context* context, Operator op,
6141 Type* left_type, tree left_tree,
6142 Type* right_type, tree right_tree,
6143 source_location location)
6145 enum tree_code code;
6151 case OPERATOR_NOTEQ:
6170 if (left_type->is_string_type() && right_type->is_string_type())
6172 tree string_type = Type::make_string_type()->get_tree(context->gogo());
6173 static tree string_compare_decl;
6174 left_tree = Gogo::call_builtin(&string_compare_decl,
6183 right_tree = build_int_cst_type(integer_type_node, 0);
6185 else if ((left_type->interface_type() != NULL
6186 && right_type->interface_type() == NULL
6187 && !right_type->is_nil_type())
6188 || (left_type->interface_type() == NULL
6189 && !left_type->is_nil_type()
6190 && right_type->interface_type() != NULL))
6192 // Comparing an interface value to a non-interface value.
6193 if (left_type->interface_type() == NULL)
6195 std::swap(left_type, right_type);
6196 std::swap(left_tree, right_tree);
6199 // The right operand is not an interface. We need to take its
6200 // address if it is not a pointer.
6203 if (right_type->points_to() != NULL)
6205 make_tmp = NULL_TREE;
6208 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
6210 make_tmp = NULL_TREE;
6211 arg = build_fold_addr_expr_loc(location, right_tree);
6212 if (DECL_P(right_tree))
6213 TREE_ADDRESSABLE(right_tree) = 1;
6217 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6218 get_name(right_tree));
6219 DECL_IGNORED_P(tmp) = 0;
6220 DECL_INITIAL(tmp) = right_tree;
6221 TREE_ADDRESSABLE(tmp) = 1;
6222 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6223 SET_EXPR_LOCATION(make_tmp, location);
6224 arg = build_fold_addr_expr_loc(location, tmp);
6226 arg = fold_convert_loc(location, ptr_type_node, arg);
6228 tree descriptor = right_type->type_descriptor_pointer(context->gogo());
6230 if (left_type->interface_type()->is_empty())
6232 static tree empty_interface_value_compare_decl;
6233 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6235 "__go_empty_interface_value_compare",
6238 TREE_TYPE(left_tree),
6240 TREE_TYPE(descriptor),
6244 if (left_tree == error_mark_node)
6245 return error_mark_node;
6246 // This can panic if the type is not comparable.
6247 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6251 static tree interface_value_compare_decl;
6252 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6254 "__go_interface_value_compare",
6257 TREE_TYPE(left_tree),
6259 TREE_TYPE(descriptor),
6263 if (left_tree == error_mark_node)
6264 return error_mark_node;
6265 // This can panic if the type is not comparable.
6266 TREE_NOTHROW(interface_value_compare_decl) = 0;
6268 right_tree = build_int_cst_type(integer_type_node, 0);
6270 if (make_tmp != NULL_TREE)
6271 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6274 else if (left_type->interface_type() != NULL
6275 && right_type->interface_type() != NULL)
6277 if (left_type->interface_type()->is_empty()
6278 && right_type->interface_type()->is_empty())
6280 static tree empty_interface_compare_decl;
6281 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6283 "__go_empty_interface_compare",
6286 TREE_TYPE(left_tree),
6288 TREE_TYPE(right_tree),
6290 if (left_tree == error_mark_node)
6291 return error_mark_node;
6292 // This can panic if the type is uncomparable.
6293 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6295 else if (!left_type->interface_type()->is_empty()
6296 && !right_type->interface_type()->is_empty())
6298 static tree interface_compare_decl;
6299 left_tree = Gogo::call_builtin(&interface_compare_decl,
6301 "__go_interface_compare",
6304 TREE_TYPE(left_tree),
6306 TREE_TYPE(right_tree),
6308 if (left_tree == error_mark_node)
6309 return error_mark_node;
6310 // This can panic if the type is uncomparable.
6311 TREE_NOTHROW(interface_compare_decl) = 0;
6315 if (left_type->interface_type()->is_empty())
6317 gcc_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6318 std::swap(left_type, right_type);
6319 std::swap(left_tree, right_tree);
6321 gcc_assert(!left_type->interface_type()->is_empty());
6322 gcc_assert(right_type->interface_type()->is_empty());
6323 static tree interface_empty_compare_decl;
6324 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
6326 "__go_interface_empty_compare",
6329 TREE_TYPE(left_tree),
6331 TREE_TYPE(right_tree),
6333 if (left_tree == error_mark_node)
6334 return error_mark_node;
6335 // This can panic if the type is uncomparable.
6336 TREE_NOTHROW(interface_empty_compare_decl) = 0;
6339 right_tree = build_int_cst_type(integer_type_node, 0);
6342 if (left_type->is_nil_type()
6343 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6345 std::swap(left_type, right_type);
6346 std::swap(left_tree, right_tree);
6349 if (right_type->is_nil_type())
6351 if (left_type->array_type() != NULL
6352 && left_type->array_type()->length() == NULL)
6354 Array_type* at = left_type->array_type();
6355 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6356 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6358 else if (left_type->interface_type() != NULL)
6360 // An interface is nil if the first field is nil.
6361 tree left_type_tree = TREE_TYPE(left_tree);
6362 gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6363 tree field = TYPE_FIELDS(left_type_tree);
6364 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6366 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6370 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6371 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6375 if (left_tree == error_mark_node || right_tree == error_mark_node)
6376 return error_mark_node;
6378 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6379 if (CAN_HAVE_LOCATION_P(ret))
6380 SET_EXPR_LOCATION(ret, location);
6384 // Class Bound_method_expression.
6389 Bound_method_expression::do_traverse(Traverse* traverse)
6391 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
6392 return TRAVERSE_EXIT;
6393 return Expression::traverse(&this->method_, traverse);
6396 // Return the type of a bound method expression. The type of this
6397 // object is really the type of the method with no receiver. We
6398 // should be able to get away with just returning the type of the
6402 Bound_method_expression::do_type()
6404 return this->method_->type();
6407 // Determine the types of a method expression.
6410 Bound_method_expression::do_determine_type(const Type_context*)
6412 this->method_->determine_type_no_context();
6413 Type* mtype = this->method_->type();
6414 Function_type* fntype = mtype == NULL ? NULL : mtype->function_type();
6415 if (fntype == NULL || !fntype->is_method())
6416 this->expr_->determine_type_no_context();
6419 Type_context subcontext(fntype->receiver()->type(), false);
6420 this->expr_->determine_type(&subcontext);
6424 // Check the types of a method expression.
6427 Bound_method_expression::do_check_types(Gogo*)
6429 Type* type = this->method_->type()->deref();
6431 || type->function_type() == NULL
6432 || !type->function_type()->is_method())
6433 this->report_error(_("object is not a method"));
6436 Type* rtype = type->function_type()->receiver()->type()->deref();
6437 Type* etype = (this->expr_type_ != NULL
6439 : this->expr_->type());
6440 etype = etype->deref();
6441 if (!Type::are_identical(rtype, etype, true, NULL))
6442 this->report_error(_("method type does not match object type"));
6446 // Get the tree for a method expression. There is no standard tree
6447 // representation for this. The only places it may currently be used
6448 // are in a Call_expression or a Go_statement, which will take it
6449 // apart directly. So this has nothing to do at present.
6452 Bound_method_expression::do_get_tree(Translate_context*)
6454 error_at(this->location(), "reference to method other than calling it");
6455 return error_mark_node;
6458 // Make a method expression.
6460 Bound_method_expression*
6461 Expression::make_bound_method(Expression* expr, Expression* method,
6462 source_location location)
6464 return new Bound_method_expression(expr, method, location);
6467 // Class Builtin_call_expression. This is used for a call to a
6468 // builtin function.
6470 class Builtin_call_expression : public Call_expression
6473 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6474 bool is_varargs, source_location location);
6477 // This overrides Call_expression::do_lower.
6479 do_lower(Gogo*, Named_object*, int);
6482 do_is_constant() const;
6485 do_integer_constant_value(bool, mpz_t, Type**) const;
6488 do_float_constant_value(mpfr_t, Type**) const;
6491 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6497 do_determine_type(const Type_context*);
6500 do_check_types(Gogo*);
6505 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6506 this->args()->copy(),
6512 do_get_tree(Translate_context*);
6515 do_export(Export*) const;
6518 do_is_recover_call() const;
6521 do_set_recover_arg(Expression*);
6524 // The builtin functions.
6525 enum Builtin_function_code
6529 // Predeclared builtin functions.
6545 // Builtin functions from the unsafe package.
6558 real_imag_type(Type*);
6561 complex_type(Type*);
6563 // A pointer back to the general IR structure. This avoids a global
6564 // variable, or passing it around everywhere.
6566 // The builtin function being called.
6567 Builtin_function_code code_;
6568 // Used to stop endless loops when the length of an array uses len
6569 // or cap of the array itself.
6573 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6575 Expression_list* args,
6577 source_location location)
6578 : Call_expression(fn, args, is_varargs, location),
6579 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
6581 Func_expression* fnexp = this->fn()->func_expression();
6582 gcc_assert(fnexp != NULL);
6583 const std::string& name(fnexp->named_object()->name());
6584 if (name == "append")
6585 this->code_ = BUILTIN_APPEND;
6586 else if (name == "cap")
6587 this->code_ = BUILTIN_CAP;
6588 else if (name == "close")
6589 this->code_ = BUILTIN_CLOSE;
6590 else if (name == "complex")
6591 this->code_ = BUILTIN_COMPLEX;
6592 else if (name == "copy")
6593 this->code_ = BUILTIN_COPY;
6594 else if (name == "imag")
6595 this->code_ = BUILTIN_IMAG;
6596 else if (name == "len")
6597 this->code_ = BUILTIN_LEN;
6598 else if (name == "make")
6599 this->code_ = BUILTIN_MAKE;
6600 else if (name == "new")
6601 this->code_ = BUILTIN_NEW;
6602 else if (name == "panic")
6603 this->code_ = BUILTIN_PANIC;
6604 else if (name == "print")
6605 this->code_ = BUILTIN_PRINT;
6606 else if (name == "println")
6607 this->code_ = BUILTIN_PRINTLN;
6608 else if (name == "real")
6609 this->code_ = BUILTIN_REAL;
6610 else if (name == "recover")
6611 this->code_ = BUILTIN_RECOVER;
6612 else if (name == "Alignof")
6613 this->code_ = BUILTIN_ALIGNOF;
6614 else if (name == "Offsetof")
6615 this->code_ = BUILTIN_OFFSETOF;
6616 else if (name == "Sizeof")
6617 this->code_ = BUILTIN_SIZEOF;
6622 // Return whether this is a call to recover. This is a virtual
6623 // function called from the parent class.
6626 Builtin_call_expression::do_is_recover_call() const
6628 if (this->classification() == EXPRESSION_ERROR)
6630 return this->code_ == BUILTIN_RECOVER;
6633 // Set the argument for a call to recover.
6636 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6638 const Expression_list* args = this->args();
6639 gcc_assert(args == NULL || args->empty());
6640 Expression_list* new_args = new Expression_list();
6641 new_args->push_back(arg);
6642 this->set_args(new_args);
6645 // A traversal class which looks for a call expression.
6647 class Find_call_expression : public Traverse
6650 Find_call_expression()
6651 : Traverse(traverse_expressions),
6656 expression(Expression**);
6660 { return this->found_; }
6667 Find_call_expression::expression(Expression** pexpr)
6669 if ((*pexpr)->call_expression() != NULL)
6671 this->found_ = true;
6672 return TRAVERSE_EXIT;
6674 return TRAVERSE_CONTINUE;
6677 // Lower a builtin call expression. This turns new and make into
6678 // specific expressions. We also convert to a constant if we can.
6681 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int)
6683 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
6685 this->report_error(_("invalid use of %<...%> with builtin function"));
6686 return Expression::make_error(this->location());
6689 if (this->code_ == BUILTIN_NEW)
6691 const Expression_list* args = this->args();
6692 if (args == NULL || args->size() < 1)
6693 this->report_error(_("not enough arguments"));
6694 else if (args->size() > 1)
6695 this->report_error(_("too many arguments"));
6698 Expression* arg = args->front();
6699 if (!arg->is_type_expression())
6701 error_at(arg->location(), "expected type");
6702 this->set_is_error();
6705 return Expression::make_allocation(arg->type(), this->location());
6708 else if (this->code_ == BUILTIN_MAKE)
6710 const Expression_list* args = this->args();
6711 if (args == NULL || args->size() < 1)
6712 this->report_error(_("not enough arguments"));
6715 Expression* arg = args->front();
6716 if (!arg->is_type_expression())
6718 error_at(arg->location(), "expected type");
6719 this->set_is_error();
6723 Expression_list* newargs;
6724 if (args->size() == 1)
6728 newargs = new Expression_list();
6729 Expression_list::const_iterator p = args->begin();
6731 for (; p != args->end(); ++p)
6732 newargs->push_back(*p);
6734 return Expression::make_make(arg->type(), newargs,
6739 else if (this->is_constant())
6741 // We can only lower len and cap if there are no function calls
6742 // in the arguments. Otherwise we have to make the call.
6743 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6745 Expression* arg = this->one_arg();
6746 if (!arg->is_constant())
6748 Find_call_expression find_call;
6749 Expression::traverse(&arg, &find_call);
6750 if (find_call.found())
6758 if (this->integer_constant_value(true, ival, &type))
6760 Expression* ret = Expression::make_integer(&ival, type,
6769 if (this->float_constant_value(rval, &type))
6771 Expression* ret = Expression::make_float(&rval, type,
6779 if (this->complex_constant_value(rval, imag, &type))
6781 Expression* ret = Expression::make_complex(&rval, &imag, type,
6790 else if (this->code_ == BUILTIN_RECOVER)
6792 if (function != NULL)
6793 function->func_value()->set_calls_recover();
6796 // Calling recover outside of a function always returns the
6797 // nil empty interface.
6798 Type* eface = Type::make_interface_type(NULL, this->location());
6799 return Expression::make_cast(eface,
6800 Expression::make_nil(this->location()),
6804 else if (this->code_ == BUILTIN_APPEND)
6806 // Lower the varargs.
6807 const Expression_list* args = this->args();
6808 if (args == NULL || args->empty())
6810 Type* slice_type = args->front()->type();
6811 if (!slice_type->is_open_array_type())
6813 error_at(args->front()->location(), "argument 1 must be a slice");
6814 this->set_is_error();
6817 return this->lower_varargs(gogo, function, slice_type, 2);
6823 // Return the type of the real or imag functions, given the type of
6824 // the argument. We need to map complex to float, complex64 to
6825 // float32, and complex128 to float64, so it has to be done by name.
6826 // This returns NULL if it can't figure out the type.
6829 Builtin_call_expression::real_imag_type(Type* arg_type)
6831 if (arg_type == NULL || arg_type->is_abstract())
6833 Named_type* nt = arg_type->named_type();
6836 while (nt->real_type()->named_type() != NULL)
6837 nt = nt->real_type()->named_type();
6838 if (nt->name() == "complex64")
6839 return Type::lookup_float_type("float32");
6840 else if (nt->name() == "complex128")
6841 return Type::lookup_float_type("float64");
6846 // Return the type of the complex function, given the type of one of the
6847 // argments. Like real_imag_type, we have to map by name.
6850 Builtin_call_expression::complex_type(Type* arg_type)
6852 if (arg_type == NULL || arg_type->is_abstract())
6854 Named_type* nt = arg_type->named_type();
6857 while (nt->real_type()->named_type() != NULL)
6858 nt = nt->real_type()->named_type();
6859 if (nt->name() == "float32")
6860 return Type::lookup_complex_type("complex64");
6861 else if (nt->name() == "float64")
6862 return Type::lookup_complex_type("complex128");
6867 // Return a single argument, or NULL if there isn't one.
6870 Builtin_call_expression::one_arg() const
6872 const Expression_list* args = this->args();
6873 if (args->size() != 1)
6875 return args->front();
6878 // Return whether this is constant: len of a string, or len or cap of
6879 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6882 Builtin_call_expression::do_is_constant() const
6884 switch (this->code_)
6892 Expression* arg = this->one_arg();
6895 Type* arg_type = arg->type();
6897 if (arg_type->points_to() != NULL
6898 && arg_type->points_to()->array_type() != NULL
6899 && !arg_type->points_to()->is_open_array_type())
6900 arg_type = arg_type->points_to();
6902 if (arg_type->array_type() != NULL
6903 && arg_type->array_type()->length() != NULL)
6906 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6909 bool ret = arg->is_constant();
6910 this->seen_ = false;
6916 case BUILTIN_SIZEOF:
6917 case BUILTIN_ALIGNOF:
6918 return this->one_arg() != NULL;
6920 case BUILTIN_OFFSETOF:
6922 Expression* arg = this->one_arg();
6925 return arg->field_reference_expression() != NULL;
6928 case BUILTIN_COMPLEX:
6930 const Expression_list* args = this->args();
6931 if (args != NULL && args->size() == 2)
6932 return args->front()->is_constant() && args->back()->is_constant();
6939 Expression* arg = this->one_arg();
6940 return arg != NULL && arg->is_constant();
6950 // Return an integer constant value if possible.
6953 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
6957 if (this->code_ == BUILTIN_LEN
6958 || this->code_ == BUILTIN_CAP)
6960 Expression* arg = this->one_arg();
6963 Type* arg_type = arg->type();
6965 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6968 if (arg->string_constant_value(&sval))
6970 mpz_set_ui(val, sval.length());
6971 *ptype = Type::lookup_integer_type("int");
6976 if (arg_type->points_to() != NULL
6977 && arg_type->points_to()->array_type() != NULL
6978 && !arg_type->points_to()->is_open_array_type())
6979 arg_type = arg_type->points_to();
6981 if (arg_type->array_type() != NULL
6982 && arg_type->array_type()->length() != NULL)
6986 Expression* e = arg_type->array_type()->length();
6988 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
6989 this->seen_ = false;
6992 *ptype = Type::lookup_integer_type("int");
6997 else if (this->code_ == BUILTIN_SIZEOF
6998 || this->code_ == BUILTIN_ALIGNOF)
7000 Expression* arg = this->one_arg();
7003 Type* arg_type = arg->type();
7004 if (arg_type->is_error())
7006 if (arg_type->is_abstract())
7008 if (arg_type->named_type() != NULL)
7009 arg_type->named_type()->convert(this->gogo_);
7010 tree arg_type_tree = arg_type->get_tree(this->gogo_);
7011 if (arg_type_tree == error_mark_node)
7013 unsigned long val_long;
7014 if (this->code_ == BUILTIN_SIZEOF)
7016 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
7017 gcc_assert(TREE_CODE(type_size) == INTEGER_CST);
7018 if (TREE_INT_CST_HIGH(type_size) != 0)
7020 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
7021 val_long = static_cast<unsigned long>(val_wide);
7022 if (val_long != val_wide)
7025 else if (this->code_ == BUILTIN_ALIGNOF)
7027 if (arg->field_reference_expression() == NULL)
7028 val_long = go_type_alignment(arg_type_tree);
7031 // Calling unsafe.Alignof(s.f) returns the alignment of
7032 // the type of f when it is used as a field in a struct.
7033 val_long = go_field_alignment(arg_type_tree);
7038 mpz_set_ui(val, val_long);
7042 else if (this->code_ == BUILTIN_OFFSETOF)
7044 Expression* arg = this->one_arg();
7047 Field_reference_expression* farg = arg->field_reference_expression();
7050 Expression* struct_expr = farg->expr();
7051 Type* st = struct_expr->type();
7052 if (st->struct_type() == NULL)
7054 if (st->named_type() != NULL)
7055 st->named_type()->convert(this->gogo_);
7056 tree struct_tree = st->get_tree(this->gogo_);
7057 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
7058 tree field = TYPE_FIELDS(struct_tree);
7059 for (unsigned int index = farg->field_index(); index > 0; --index)
7061 field = DECL_CHAIN(field);
7062 gcc_assert(field != NULL_TREE);
7064 HOST_WIDE_INT offset_wide = int_byte_position (field);
7065 if (offset_wide < 0)
7067 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
7068 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
7070 mpz_set_ui(val, offset_long);
7076 // Return a floating point constant value if possible.
7079 Builtin_call_expression::do_float_constant_value(mpfr_t val,
7082 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
7084 Expression* arg = this->one_arg();
7095 if (arg->complex_constant_value(real, imag, &type))
7097 if (this->code_ == BUILTIN_REAL)
7098 mpfr_set(val, real, GMP_RNDN);
7100 mpfr_set(val, imag, GMP_RNDN);
7101 *ptype = Builtin_call_expression::real_imag_type(type);
7113 // Return a complex constant value if possible.
7116 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
7119 if (this->code_ == BUILTIN_COMPLEX)
7121 const Expression_list* args = this->args();
7122 if (args == NULL || args->size() != 2)
7128 if (!args->front()->float_constant_value(r, &rtype))
7139 if (args->back()->float_constant_value(i, &itype)
7140 && Type::are_identical(rtype, itype, false, NULL))
7142 mpfr_set(real, r, GMP_RNDN);
7143 mpfr_set(imag, i, GMP_RNDN);
7144 *ptype = Builtin_call_expression::complex_type(rtype);
7160 Builtin_call_expression::do_type()
7162 switch (this->code_)
7164 case BUILTIN_INVALID:
7171 const Expression_list* args = this->args();
7172 if (args == NULL || args->empty())
7173 return Type::make_error_type();
7174 return Type::make_pointer_type(args->front()->type());
7180 case BUILTIN_ALIGNOF:
7181 case BUILTIN_OFFSETOF:
7182 case BUILTIN_SIZEOF:
7183 return Type::lookup_integer_type("int");
7188 case BUILTIN_PRINTLN:
7189 return Type::make_void_type();
7191 case BUILTIN_RECOVER:
7192 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
7194 case BUILTIN_APPEND:
7196 const Expression_list* args = this->args();
7197 if (args == NULL || args->empty())
7198 return Type::make_error_type();
7199 return args->front()->type();
7205 Expression* arg = this->one_arg();
7207 return Type::make_error_type();
7208 Type* t = arg->type();
7209 if (t->is_abstract())
7210 t = t->make_non_abstract_type();
7211 t = Builtin_call_expression::real_imag_type(t);
7213 t = Type::make_error_type();
7217 case BUILTIN_COMPLEX:
7219 const Expression_list* args = this->args();
7220 if (args == NULL || args->size() != 2)
7221 return Type::make_error_type();
7222 Type* t = args->front()->type();
7223 if (t->is_abstract())
7225 t = args->back()->type();
7226 if (t->is_abstract())
7227 t = t->make_non_abstract_type();
7229 t = Builtin_call_expression::complex_type(t);
7231 t = Type::make_error_type();
7237 // Determine the type.
7240 Builtin_call_expression::do_determine_type(const Type_context* context)
7242 if (!this->determining_types())
7245 this->fn()->determine_type_no_context();
7247 const Expression_list* args = this->args();
7250 Type* arg_type = NULL;
7251 switch (this->code_)
7254 case BUILTIN_PRINTLN:
7255 // Do not force a large integer constant to "int".
7261 arg_type = Builtin_call_expression::complex_type(context->type);
7265 case BUILTIN_COMPLEX:
7267 // For the complex function the type of one operand can
7268 // determine the type of the other, as in a binary expression.
7269 arg_type = Builtin_call_expression::real_imag_type(context->type);
7270 if (args != NULL && args->size() == 2)
7272 Type* t1 = args->front()->type();
7273 Type* t2 = args->front()->type();
7274 if (!t1->is_abstract())
7276 else if (!t2->is_abstract())
7290 for (Expression_list::const_iterator pa = args->begin();
7294 Type_context subcontext;
7295 subcontext.type = arg_type;
7299 // We want to print large constants, we so can't just
7300 // use the appropriate nonabstract type. Use uint64 for
7301 // an integer if we know it is nonnegative, otherwise
7302 // use int64 for a integer, otherwise use float64 for a
7303 // float or complex128 for a complex.
7304 Type* want_type = NULL;
7305 Type* atype = (*pa)->type();
7306 if (atype->is_abstract())
7308 if (atype->integer_type() != NULL)
7313 if (this->integer_constant_value(true, val, &dummy)
7314 && mpz_sgn(val) >= 0)
7315 want_type = Type::lookup_integer_type("uint64");
7317 want_type = Type::lookup_integer_type("int64");
7320 else if (atype->float_type() != NULL)
7321 want_type = Type::lookup_float_type("float64");
7322 else if (atype->complex_type() != NULL)
7323 want_type = Type::lookup_complex_type("complex128");
7324 else if (atype->is_abstract_string_type())
7325 want_type = Type::lookup_string_type();
7326 else if (atype->is_abstract_boolean_type())
7327 want_type = Type::lookup_bool_type();
7330 subcontext.type = want_type;
7334 (*pa)->determine_type(&subcontext);
7339 // If there is exactly one argument, return true. Otherwise give an
7340 // error message and return false.
7343 Builtin_call_expression::check_one_arg()
7345 const Expression_list* args = this->args();
7346 if (args == NULL || args->size() < 1)
7348 this->report_error(_("not enough arguments"));
7351 else if (args->size() > 1)
7353 this->report_error(_("too many arguments"));
7356 if (args->front()->is_error_expression()
7357 || args->front()->type()->is_error())
7359 this->set_is_error();
7365 // Check argument types for a builtin function.
7368 Builtin_call_expression::do_check_types(Gogo*)
7370 switch (this->code_)
7372 case BUILTIN_INVALID:
7380 // The single argument may be either a string or an array or a
7381 // map or a channel, or a pointer to a closed array.
7382 if (this->check_one_arg())
7384 Type* arg_type = this->one_arg()->type();
7385 if (arg_type->points_to() != NULL
7386 && arg_type->points_to()->array_type() != NULL
7387 && !arg_type->points_to()->is_open_array_type())
7388 arg_type = arg_type->points_to();
7389 if (this->code_ == BUILTIN_CAP)
7391 if (!arg_type->is_error()
7392 && arg_type->array_type() == NULL
7393 && arg_type->channel_type() == NULL)
7394 this->report_error(_("argument must be array or slice "
7399 if (!arg_type->is_error()
7400 && !arg_type->is_string_type()
7401 && arg_type->array_type() == NULL
7402 && arg_type->map_type() == NULL
7403 && arg_type->channel_type() == NULL)
7404 this->report_error(_("argument must be string or "
7405 "array or slice or map or channel"));
7412 case BUILTIN_PRINTLN:
7414 const Expression_list* args = this->args();
7417 if (this->code_ == BUILTIN_PRINT)
7418 warning_at(this->location(), 0,
7419 "no arguments for builtin function %<%s%>",
7420 (this->code_ == BUILTIN_PRINT
7426 for (Expression_list::const_iterator p = args->begin();
7430 Type* type = (*p)->type();
7431 if (type->is_error()
7432 || type->is_string_type()
7433 || type->integer_type() != NULL
7434 || type->float_type() != NULL
7435 || type->complex_type() != NULL
7436 || type->is_boolean_type()
7437 || type->points_to() != NULL
7438 || type->interface_type() != NULL
7439 || type->channel_type() != NULL
7440 || type->map_type() != NULL
7441 || type->function_type() != NULL
7442 || type->is_open_array_type())
7445 this->report_error(_("unsupported argument type to "
7446 "builtin function"));
7453 if (this->check_one_arg())
7455 if (this->one_arg()->type()->channel_type() == NULL)
7456 this->report_error(_("argument must be channel"));
7461 case BUILTIN_SIZEOF:
7462 case BUILTIN_ALIGNOF:
7463 this->check_one_arg();
7466 case BUILTIN_RECOVER:
7467 if (this->args() != NULL && !this->args()->empty())
7468 this->report_error(_("too many arguments"));
7471 case BUILTIN_OFFSETOF:
7472 if (this->check_one_arg())
7474 Expression* arg = this->one_arg();
7475 if (arg->field_reference_expression() == NULL)
7476 this->report_error(_("argument must be a field reference"));
7482 const Expression_list* args = this->args();
7483 if (args == NULL || args->size() < 2)
7485 this->report_error(_("not enough arguments"));
7488 else if (args->size() > 2)
7490 this->report_error(_("too many arguments"));
7493 Type* arg1_type = args->front()->type();
7494 Type* arg2_type = args->back()->type();
7495 if (arg1_type->is_error() || arg2_type->is_error())
7499 if (arg1_type->is_open_array_type())
7500 e1 = arg1_type->array_type()->element_type();
7503 this->report_error(_("left argument must be a slice"));
7508 if (arg2_type->is_open_array_type())
7509 e2 = arg2_type->array_type()->element_type();
7510 else if (arg2_type->is_string_type())
7511 e2 = Type::lookup_integer_type("uint8");
7514 this->report_error(_("right argument must be a slice or a string"));
7518 if (!Type::are_identical(e1, e2, true, NULL))
7519 this->report_error(_("element types must be the same"));
7523 case BUILTIN_APPEND:
7525 const Expression_list* args = this->args();
7526 if (args == NULL || args->size() < 2)
7528 this->report_error(_("not enough arguments"));
7531 if (args->size() > 2)
7533 this->report_error(_("too many arguments"));
7537 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
7541 this->report_error(_("arguments 1 and 2 have different types"));
7544 error_at(this->location(),
7545 "arguments 1 and 2 have different types (%s)",
7547 this->set_is_error();
7555 if (this->check_one_arg())
7557 if (this->one_arg()->type()->complex_type() == NULL)
7558 this->report_error(_("argument must have complex type"));
7562 case BUILTIN_COMPLEX:
7564 const Expression_list* args = this->args();
7565 if (args == NULL || args->size() < 2)
7566 this->report_error(_("not enough arguments"));
7567 else if (args->size() > 2)
7568 this->report_error(_("too many arguments"));
7569 else if (args->front()->is_error_expression()
7570 || args->front()->type()->is_error()
7571 || args->back()->is_error_expression()
7572 || args->back()->type()->is_error())
7573 this->set_is_error();
7574 else if (!Type::are_identical(args->front()->type(),
7575 args->back()->type(), true, NULL))
7576 this->report_error(_("complex arguments must have identical types"));
7577 else if (args->front()->type()->float_type() == NULL)
7578 this->report_error(_("complex arguments must have "
7579 "floating-point type"));
7588 // Return the tree for a builtin function.
7591 Builtin_call_expression::do_get_tree(Translate_context* context)
7593 Gogo* gogo = context->gogo();
7594 source_location location = this->location();
7595 switch (this->code_)
7597 case BUILTIN_INVALID:
7605 const Expression_list* args = this->args();
7606 gcc_assert(args != NULL && args->size() == 1);
7607 Expression* arg = *args->begin();
7608 Type* arg_type = arg->type();
7612 gcc_assert(saw_errors());
7613 return error_mark_node;
7617 tree arg_tree = arg->get_tree(context);
7619 this->seen_ = false;
7621 if (arg_tree == error_mark_node)
7622 return error_mark_node;
7624 if (arg_type->points_to() != NULL)
7626 arg_type = arg_type->points_to();
7627 gcc_assert(arg_type->array_type() != NULL
7628 && !arg_type->is_open_array_type());
7629 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7630 arg_tree = build_fold_indirect_ref(arg_tree);
7634 if (this->code_ == BUILTIN_LEN)
7636 if (arg_type->is_string_type())
7637 val_tree = String_type::length_tree(gogo, arg_tree);
7638 else if (arg_type->array_type() != NULL)
7642 gcc_assert(saw_errors());
7643 return error_mark_node;
7646 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7647 this->seen_ = false;
7649 else if (arg_type->map_type() != NULL)
7651 static tree map_len_fndecl;
7652 val_tree = Gogo::call_builtin(&map_len_fndecl,
7657 arg_type->get_tree(gogo),
7660 else if (arg_type->channel_type() != NULL)
7662 static tree chan_len_fndecl;
7663 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7668 arg_type->get_tree(gogo),
7676 if (arg_type->array_type() != NULL)
7680 gcc_assert(saw_errors());
7681 return error_mark_node;
7684 val_tree = arg_type->array_type()->capacity_tree(gogo,
7686 this->seen_ = false;
7688 else if (arg_type->channel_type() != NULL)
7690 static tree chan_cap_fndecl;
7691 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7696 arg_type->get_tree(gogo),
7703 if (val_tree == error_mark_node)
7704 return error_mark_node;
7706 tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo);
7707 if (type_tree == TREE_TYPE(val_tree))
7710 return fold(convert_to_integer(type_tree, val_tree));
7714 case BUILTIN_PRINTLN:
7716 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7717 tree stmt_list = NULL_TREE;
7719 const Expression_list* call_args = this->args();
7720 if (call_args != NULL)
7722 for (Expression_list::const_iterator p = call_args->begin();
7723 p != call_args->end();
7726 if (is_ln && p != call_args->begin())
7728 static tree print_space_fndecl;
7729 tree call = Gogo::call_builtin(&print_space_fndecl,
7734 if (call == error_mark_node)
7735 return error_mark_node;
7736 append_to_statement_list(call, &stmt_list);
7739 Type* type = (*p)->type();
7741 tree arg = (*p)->get_tree(context);
7742 if (arg == error_mark_node)
7743 return error_mark_node;
7747 if (type->is_string_type())
7749 static tree print_string_fndecl;
7750 pfndecl = &print_string_fndecl;
7751 fnname = "__go_print_string";
7753 else if (type->integer_type() != NULL
7754 && type->integer_type()->is_unsigned())
7756 static tree print_uint64_fndecl;
7757 pfndecl = &print_uint64_fndecl;
7758 fnname = "__go_print_uint64";
7759 Type* itype = Type::lookup_integer_type("uint64");
7760 arg = fold_convert_loc(location, itype->get_tree(gogo),
7763 else if (type->integer_type() != NULL)
7765 static tree print_int64_fndecl;
7766 pfndecl = &print_int64_fndecl;
7767 fnname = "__go_print_int64";
7768 Type* itype = Type::lookup_integer_type("int64");
7769 arg = fold_convert_loc(location, itype->get_tree(gogo),
7772 else if (type->float_type() != NULL)
7774 static tree print_double_fndecl;
7775 pfndecl = &print_double_fndecl;
7776 fnname = "__go_print_double";
7777 arg = fold_convert_loc(location, double_type_node, arg);
7779 else if (type->complex_type() != NULL)
7781 static tree print_complex_fndecl;
7782 pfndecl = &print_complex_fndecl;
7783 fnname = "__go_print_complex";
7784 arg = fold_convert_loc(location, complex_double_type_node,
7787 else if (type->is_boolean_type())
7789 static tree print_bool_fndecl;
7790 pfndecl = &print_bool_fndecl;
7791 fnname = "__go_print_bool";
7793 else if (type->points_to() != NULL
7794 || type->channel_type() != NULL
7795 || type->map_type() != NULL
7796 || type->function_type() != NULL)
7798 static tree print_pointer_fndecl;
7799 pfndecl = &print_pointer_fndecl;
7800 fnname = "__go_print_pointer";
7801 arg = fold_convert_loc(location, ptr_type_node, arg);
7803 else if (type->interface_type() != NULL)
7805 if (type->interface_type()->is_empty())
7807 static tree print_empty_interface_fndecl;
7808 pfndecl = &print_empty_interface_fndecl;
7809 fnname = "__go_print_empty_interface";
7813 static tree print_interface_fndecl;
7814 pfndecl = &print_interface_fndecl;
7815 fnname = "__go_print_interface";
7818 else if (type->is_open_array_type())
7820 static tree print_slice_fndecl;
7821 pfndecl = &print_slice_fndecl;
7822 fnname = "__go_print_slice";
7827 tree call = Gogo::call_builtin(pfndecl,
7834 if (call == error_mark_node)
7835 return error_mark_node;
7836 append_to_statement_list(call, &stmt_list);
7842 static tree print_nl_fndecl;
7843 tree call = Gogo::call_builtin(&print_nl_fndecl,
7848 if (call == error_mark_node)
7849 return error_mark_node;
7850 append_to_statement_list(call, &stmt_list);
7858 const Expression_list* args = this->args();
7859 gcc_assert(args != NULL && args->size() == 1);
7860 Expression* arg = args->front();
7861 tree arg_tree = arg->get_tree(context);
7862 if (arg_tree == error_mark_node)
7863 return error_mark_node;
7864 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7865 arg_tree = Expression::convert_for_assignment(context, empty,
7867 arg_tree, location);
7868 static tree panic_fndecl;
7869 tree call = Gogo::call_builtin(&panic_fndecl,
7874 TREE_TYPE(arg_tree),
7876 if (call == error_mark_node)
7877 return error_mark_node;
7878 // This function will throw an exception.
7879 TREE_NOTHROW(panic_fndecl) = 0;
7880 // This function will not return.
7881 TREE_THIS_VOLATILE(panic_fndecl) = 1;
7885 case BUILTIN_RECOVER:
7887 // The argument is set when building recover thunks. It's a
7888 // boolean value which is true if we can recover a value now.
7889 const Expression_list* args = this->args();
7890 gcc_assert(args != NULL && args->size() == 1);
7891 Expression* arg = args->front();
7892 tree arg_tree = arg->get_tree(context);
7893 if (arg_tree == error_mark_node)
7894 return error_mark_node;
7896 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7897 tree empty_tree = empty->get_tree(context->gogo());
7899 Type* nil_type = Type::make_nil_type();
7900 Expression* nil = Expression::make_nil(location);
7901 tree nil_tree = nil->get_tree(context);
7902 tree empty_nil_tree = Expression::convert_for_assignment(context,
7908 // We need to handle a deferred call to recover specially,
7909 // because it changes whether it can recover a panic or not.
7910 // See test7 in test/recover1.go.
7912 if (this->is_deferred())
7914 static tree deferred_recover_fndecl;
7915 call = Gogo::call_builtin(&deferred_recover_fndecl,
7917 "__go_deferred_recover",
7923 static tree recover_fndecl;
7924 call = Gogo::call_builtin(&recover_fndecl,
7930 if (call == error_mark_node)
7931 return error_mark_node;
7932 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
7933 call, empty_nil_tree);
7938 const Expression_list* args = this->args();
7939 gcc_assert(args != NULL && args->size() == 1);
7940 Expression* arg = args->front();
7941 tree arg_tree = arg->get_tree(context);
7942 if (arg_tree == error_mark_node)
7943 return error_mark_node;
7944 static tree close_fndecl;
7945 return Gogo::call_builtin(&close_fndecl,
7947 "__go_builtin_close",
7950 TREE_TYPE(arg_tree),
7954 case BUILTIN_SIZEOF:
7955 case BUILTIN_OFFSETOF:
7956 case BUILTIN_ALIGNOF:
7961 bool b = this->integer_constant_value(true, val, &dummy);
7964 gcc_assert(saw_errors());
7965 return error_mark_node;
7967 tree type = Type::lookup_integer_type("int")->get_tree(gogo);
7968 tree ret = Expression::integer_constant_tree(val, type);
7975 const Expression_list* args = this->args();
7976 gcc_assert(args != NULL && args->size() == 2);
7977 Expression* arg1 = args->front();
7978 Expression* arg2 = args->back();
7980 tree arg1_tree = arg1->get_tree(context);
7981 tree arg2_tree = arg2->get_tree(context);
7982 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
7983 return error_mark_node;
7985 Type* arg1_type = arg1->type();
7986 Array_type* at = arg1_type->array_type();
7987 arg1_tree = save_expr(arg1_tree);
7988 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
7989 tree arg1_len = at->length_tree(gogo, arg1_tree);
7990 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
7991 return error_mark_node;
7993 Type* arg2_type = arg2->type();
7996 if (arg2_type->is_open_array_type())
7998 at = arg2_type->array_type();
7999 arg2_tree = save_expr(arg2_tree);
8000 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8001 arg2_len = at->length_tree(gogo, arg2_tree);
8005 arg2_tree = save_expr(arg2_tree);
8006 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8007 arg2_len = String_type::length_tree(gogo, arg2_tree);
8009 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8010 return error_mark_node;
8012 arg1_len = save_expr(arg1_len);
8013 arg2_len = save_expr(arg2_len);
8014 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
8015 fold_build2_loc(location, LT_EXPR,
8017 arg1_len, arg2_len),
8018 arg1_len, arg2_len);
8019 len = save_expr(len);
8021 Type* element_type = at->element_type();
8022 tree element_type_tree = element_type->get_tree(gogo);
8023 if (element_type_tree == error_mark_node)
8024 return error_mark_node;
8025 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8026 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
8028 bytecount = fold_build2_loc(location, MULT_EXPR,
8029 TREE_TYPE(element_size),
8030 bytecount, element_size);
8031 bytecount = fold_convert_loc(location, size_type_node, bytecount);
8033 arg1_val = fold_convert_loc(location, ptr_type_node, arg1_val);
8034 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8036 static tree copy_fndecl;
8037 tree call = Gogo::call_builtin(©_fndecl,
8048 if (call == error_mark_node)
8049 return error_mark_node;
8051 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
8055 case BUILTIN_APPEND:
8057 const Expression_list* args = this->args();
8058 gcc_assert(args != NULL && args->size() == 2);
8059 Expression* arg1 = args->front();
8060 Expression* arg2 = args->back();
8062 tree arg1_tree = arg1->get_tree(context);
8063 tree arg2_tree = arg2->get_tree(context);
8064 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8065 return error_mark_node;
8067 Array_type* at = arg1->type()->array_type();
8068 Type* element_type = at->element_type();
8070 arg2_tree = Expression::convert_for_assignment(context, at,
8074 if (arg2_tree == error_mark_node)
8075 return error_mark_node;
8077 arg2_tree = save_expr(arg2_tree);
8078 tree arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8079 tree arg2_len = at->length_tree(gogo, arg2_tree);
8080 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8081 return error_mark_node;
8082 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8083 arg2_len = fold_convert_loc(location, size_type_node, arg2_len);
8085 tree element_type_tree = element_type->get_tree(gogo);
8086 if (element_type_tree == error_mark_node)
8087 return error_mark_node;
8088 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8089 element_size = fold_convert_loc(location, size_type_node,
8092 // We rebuild the decl each time since the slice types may
8094 tree append_fndecl = NULL_TREE;
8095 return Gogo::call_builtin(&append_fndecl,
8099 TREE_TYPE(arg1_tree),
8100 TREE_TYPE(arg1_tree),
8113 const Expression_list* args = this->args();
8114 gcc_assert(args != NULL && args->size() == 1);
8115 Expression* arg = args->front();
8116 tree arg_tree = arg->get_tree(context);
8117 if (arg_tree == error_mark_node)
8118 return error_mark_node;
8119 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
8120 if (this->code_ == BUILTIN_REAL)
8121 return fold_build1_loc(location, REALPART_EXPR,
8122 TREE_TYPE(TREE_TYPE(arg_tree)),
8125 return fold_build1_loc(location, IMAGPART_EXPR,
8126 TREE_TYPE(TREE_TYPE(arg_tree)),
8130 case BUILTIN_COMPLEX:
8132 const Expression_list* args = this->args();
8133 gcc_assert(args != NULL && args->size() == 2);
8134 tree r = args->front()->get_tree(context);
8135 tree i = args->back()->get_tree(context);
8136 if (r == error_mark_node || i == error_mark_node)
8137 return error_mark_node;
8138 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
8139 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
8140 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8141 return fold_build2_loc(location, COMPLEX_EXPR,
8142 build_complex_type(TREE_TYPE(r)),
8151 // We have to support exporting a builtin call expression, because
8152 // code can set a constant to the result of a builtin expression.
8155 Builtin_call_expression::do_export(Export* exp) const
8162 if (this->integer_constant_value(true, val, &dummy))
8164 Integer_expression::export_integer(exp, val);
8173 if (this->float_constant_value(fval, &dummy))
8175 Float_expression::export_float(exp, fval);
8187 if (this->complex_constant_value(real, imag, &dummy))
8189 Complex_expression::export_complex(exp, real, imag);
8198 error_at(this->location(), "value is not constant");
8202 // A trailing space lets us reliably identify the end of the number.
8203 exp->write_c_string(" ");
8206 // Class Call_expression.
8211 Call_expression::do_traverse(Traverse* traverse)
8213 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8214 return TRAVERSE_EXIT;
8215 if (this->args_ != NULL)
8217 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8218 return TRAVERSE_EXIT;
8220 return TRAVERSE_CONTINUE;
8223 // Lower a call statement.
8226 Call_expression::do_lower(Gogo* gogo, Named_object* function, int)
8228 // A type case can look like a function call.
8229 if (this->fn_->is_type_expression()
8230 && this->args_ != NULL
8231 && this->args_->size() == 1)
8232 return Expression::make_cast(this->fn_->type(), this->args_->front(),
8235 // Recognize a call to a builtin function.
8236 Func_expression* fne = this->fn_->func_expression();
8238 && fne->named_object()->is_function_declaration()
8239 && fne->named_object()->func_declaration_value()->type()->is_builtin())
8240 return new Builtin_call_expression(gogo, this->fn_, this->args_,
8241 this->is_varargs_, this->location());
8243 // Handle an argument which is a call to a function which returns
8244 // multiple results.
8245 if (this->args_ != NULL
8246 && this->args_->size() == 1
8247 && this->args_->front()->call_expression() != NULL
8248 && this->fn_->type()->function_type() != NULL)
8250 Function_type* fntype = this->fn_->type()->function_type();
8251 size_t rc = this->args_->front()->call_expression()->result_count();
8253 && fntype->parameters() != NULL
8254 && (fntype->parameters()->size() == rc
8255 || (fntype->is_varargs()
8256 && fntype->parameters()->size() - 1 <= rc)))
8258 Call_expression* call = this->args_->front()->call_expression();
8259 Expression_list* args = new Expression_list;
8260 for (size_t i = 0; i < rc; ++i)
8261 args->push_back(Expression::make_call_result(call, i));
8262 // We can't return a new call expression here, because this
8263 // one may be referenced by Call_result expressions. We
8264 // also can't delete the old arguments, because we may still
8265 // traverse them somewhere up the call stack. FIXME.
8270 // Handle a call to a varargs function by packaging up the extra
8272 if (this->fn_->type()->function_type() != NULL
8273 && this->fn_->type()->function_type()->is_varargs())
8275 Function_type* fntype = this->fn_->type()->function_type();
8276 const Typed_identifier_list* parameters = fntype->parameters();
8277 gcc_assert(parameters != NULL && !parameters->empty());
8278 Type* varargs_type = parameters->back().type();
8279 return this->lower_varargs(gogo, function, varargs_type,
8280 parameters->size());
8286 // Lower a call to a varargs function. FUNCTION is the function in
8287 // which the call occurs--it's not the function we are calling.
8288 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8289 // PARAM_COUNT is the number of parameters of the function we are
8290 // calling; the last of these parameters will be the varargs
8294 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
8295 Type* varargs_type, size_t param_count)
8297 if (this->varargs_are_lowered_)
8300 source_location loc = this->location();
8302 gcc_assert(param_count > 0);
8303 gcc_assert(varargs_type->is_open_array_type());
8305 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8306 if (arg_count < param_count - 1)
8308 // Not enough arguments; will be caught in check_types.
8312 Expression_list* old_args = this->args_;
8313 Expression_list* new_args = new Expression_list();
8314 bool push_empty_arg = false;
8315 if (old_args == NULL || old_args->empty())
8317 gcc_assert(param_count == 1);
8318 push_empty_arg = true;
8322 Expression_list::const_iterator pa;
8324 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
8326 if (static_cast<size_t>(i) == param_count)
8328 new_args->push_back(*pa);
8331 // We have reached the varargs parameter.
8333 bool issued_error = false;
8334 if (pa == old_args->end())
8335 push_empty_arg = true;
8336 else if (pa + 1 == old_args->end() && this->is_varargs_)
8337 new_args->push_back(*pa);
8338 else if (this->is_varargs_)
8340 this->report_error(_("too many arguments"));
8345 Type* element_type = varargs_type->array_type()->element_type();
8346 Expression_list* vals = new Expression_list;
8347 for (; pa != old_args->end(); ++pa, ++i)
8349 // Check types here so that we get a better message.
8350 Type* patype = (*pa)->type();
8351 source_location paloc = (*pa)->location();
8352 if (!this->check_argument_type(i, element_type, patype,
8353 paloc, issued_error))
8355 vals->push_back(*pa);
8358 Expression::make_slice_composite_literal(varargs_type, vals, loc);
8359 new_args->push_back(val);
8364 new_args->push_back(Expression::make_nil(loc));
8366 // We can't return a new call expression here, because this one may
8367 // be referenced by Call_result expressions. FIXME.
8368 if (old_args != NULL)
8370 this->args_ = new_args;
8371 this->varargs_are_lowered_ = true;
8373 // Lower all the new subexpressions.
8374 Expression* ret = this;
8375 gogo->lower_expression(function, &ret);
8376 gcc_assert(ret == this);
8380 // Get the function type. Returns NULL if we don't know the type. If
8381 // this returns NULL, and if_ERROR is true, issues an error.
8384 Call_expression::get_function_type() const
8386 return this->fn_->type()->function_type();
8389 // Return the number of values which this call will return.
8392 Call_expression::result_count() const
8394 const Function_type* fntype = this->get_function_type();
8397 if (fntype->results() == NULL)
8399 return fntype->results()->size();
8402 // Return whether this is a call to the predeclared function recover.
8405 Call_expression::is_recover_call() const
8407 return this->do_is_recover_call();
8410 // Set the argument to the recover function.
8413 Call_expression::set_recover_arg(Expression* arg)
8415 this->do_set_recover_arg(arg);
8418 // Virtual functions also implemented by Builtin_call_expression.
8421 Call_expression::do_is_recover_call() const
8427 Call_expression::do_set_recover_arg(Expression*)
8435 Call_expression::do_type()
8437 if (this->type_ != NULL)
8441 Function_type* fntype = this->get_function_type();
8443 return Type::make_error_type();
8445 const Typed_identifier_list* results = fntype->results();
8446 if (results == NULL)
8447 ret = Type::make_void_type();
8448 else if (results->size() == 1)
8449 ret = results->begin()->type();
8451 ret = Type::make_call_multiple_result_type(this);
8458 // Determine types for a call expression. We can use the function
8459 // parameter types to set the types of the arguments.
8462 Call_expression::do_determine_type(const Type_context*)
8464 if (!this->determining_types())
8467 this->fn_->determine_type_no_context();
8468 Function_type* fntype = this->get_function_type();
8469 const Typed_identifier_list* parameters = NULL;
8471 parameters = fntype->parameters();
8472 if (this->args_ != NULL)
8474 Typed_identifier_list::const_iterator pt;
8475 if (parameters != NULL)
8476 pt = parameters->begin();
8477 for (Expression_list::const_iterator pa = this->args_->begin();
8478 pa != this->args_->end();
8481 if (parameters != NULL && pt != parameters->end())
8483 Type_context subcontext(pt->type(), false);
8484 (*pa)->determine_type(&subcontext);
8488 (*pa)->determine_type_no_context();
8493 // Called when determining types for a Call_expression. Return true
8494 // if we should go ahead, false if they have already been determined.
8497 Call_expression::determining_types()
8499 if (this->types_are_determined_)
8503 this->types_are_determined_ = true;
8508 // Check types for parameter I.
8511 Call_expression::check_argument_type(int i, const Type* parameter_type,
8512 const Type* argument_type,
8513 source_location argument_location,
8517 if (!Type::are_assignable(parameter_type, argument_type, &reason))
8522 error_at(argument_location, "argument %d has incompatible type", i);
8524 error_at(argument_location,
8525 "argument %d has incompatible type (%s)",
8528 this->set_is_error();
8537 Call_expression::do_check_types(Gogo*)
8539 Function_type* fntype = this->get_function_type();
8542 if (!this->fn_->type()->is_error())
8543 this->report_error(_("expected function"));
8547 if (fntype->is_method())
8549 // We don't support pointers to methods, so the function has to
8550 // be a bound method expression.
8551 Bound_method_expression* bme = this->fn_->bound_method_expression();
8554 this->report_error(_("method call without object"));
8557 Type* first_arg_type = bme->first_argument()->type();
8558 if (first_arg_type->points_to() == NULL)
8560 // When passing a value, we need to check that we are
8561 // permitted to copy it. The language permits copying
8562 // hidden fields for a method receiver.
8564 if (!Type::are_assignable_hidden_ok(fntype->receiver()->type(),
8565 first_arg_type, &reason))
8568 this->report_error(_("incompatible type for receiver"));
8571 error_at(this->location(),
8572 "incompatible type for receiver (%s)",
8574 this->set_is_error();
8580 // Note that varargs was handled by the lower_varargs() method, so
8581 // we don't have to worry about it here.
8583 const Typed_identifier_list* parameters = fntype->parameters();
8584 if (this->args_ == NULL)
8586 if (parameters != NULL && !parameters->empty())
8587 this->report_error(_("not enough arguments"));
8589 else if (parameters == NULL)
8590 this->report_error(_("too many arguments"));
8594 Typed_identifier_list::const_iterator pt = parameters->begin();
8595 for (Expression_list::const_iterator pa = this->args_->begin();
8596 pa != this->args_->end();
8599 if (pt == parameters->end())
8601 this->report_error(_("too many arguments"));
8604 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8605 (*pa)->location(), false);
8607 if (pt != parameters->end())
8608 this->report_error(_("not enough arguments"));
8612 // Return whether we have to use a temporary variable to ensure that
8613 // we evaluate this call expression in order. If the call returns no
8614 // results then it will inevitably be executed last. If the call
8615 // returns more than one result then it will be used with Call_result
8616 // expressions. So we only have to use a temporary variable if the
8617 // call returns exactly one result.
8620 Call_expression::do_must_eval_in_order() const
8622 return this->result_count() == 1;
8625 // Get the function and the first argument to use when calling a bound
8629 Call_expression::bound_method_function(Translate_context* context,
8630 Bound_method_expression* bound_method,
8631 tree* first_arg_ptr)
8633 Expression* first_argument = bound_method->first_argument();
8634 tree first_arg = first_argument->get_tree(context);
8635 if (first_arg == error_mark_node)
8636 return error_mark_node;
8638 // We always pass a pointer to the first argument when calling a
8640 if (first_argument->type()->points_to() == NULL)
8642 tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg));
8643 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg))
8644 || DECL_P(first_arg)
8645 || TREE_CODE(first_arg) == INDIRECT_REF
8646 || TREE_CODE(first_arg) == COMPONENT_REF)
8648 first_arg = build_fold_addr_expr(first_arg);
8649 if (DECL_P(first_arg))
8650 TREE_ADDRESSABLE(first_arg) = 1;
8654 tree tmp = create_tmp_var(TREE_TYPE(first_arg),
8655 get_name(first_arg));
8656 DECL_IGNORED_P(tmp) = 0;
8657 DECL_INITIAL(tmp) = first_arg;
8658 first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type,
8659 build1(DECL_EXPR, void_type_node, tmp),
8660 build_fold_addr_expr(tmp));
8661 TREE_ADDRESSABLE(tmp) = 1;
8663 if (first_arg == error_mark_node)
8664 return error_mark_node;
8667 Type* fatype = bound_method->first_argument_type();
8670 if (fatype->points_to() == NULL)
8671 fatype = Type::make_pointer_type(fatype);
8672 first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg);
8673 if (first_arg == error_mark_node
8674 || TREE_TYPE(first_arg) == error_mark_node)
8675 return error_mark_node;
8678 *first_arg_ptr = first_arg;
8680 return bound_method->method()->get_tree(context);
8683 // Get the function and the first argument to use when calling an
8684 // interface method.
8687 Call_expression::interface_method_function(
8688 Translate_context* context,
8689 Interface_field_reference_expression* interface_method,
8690 tree* first_arg_ptr)
8692 tree expr = interface_method->expr()->get_tree(context);
8693 if (expr == error_mark_node)
8694 return error_mark_node;
8695 expr = save_expr(expr);
8696 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
8697 if (first_arg == error_mark_node)
8698 return error_mark_node;
8699 *first_arg_ptr = first_arg;
8700 return interface_method->get_function_tree(context, expr);
8703 // Build the call expression.
8706 Call_expression::do_get_tree(Translate_context* context)
8708 if (this->tree_ != NULL_TREE)
8711 Function_type* fntype = this->get_function_type();
8713 return error_mark_node;
8715 if (this->fn_->is_error_expression())
8716 return error_mark_node;
8718 Gogo* gogo = context->gogo();
8719 source_location location = this->location();
8721 Func_expression* func = this->fn_->func_expression();
8722 Bound_method_expression* bound_method = this->fn_->bound_method_expression();
8723 Interface_field_reference_expression* interface_method =
8724 this->fn_->interface_field_reference_expression();
8725 const bool has_closure = func != NULL && func->closure() != NULL;
8726 const bool is_method = bound_method != NULL || interface_method != NULL;
8727 gcc_assert(!fntype->is_method() || is_method);
8731 if (this->args_ == NULL || this->args_->empty())
8733 nargs = is_method ? 1 : 0;
8734 args = nargs == 0 ? NULL : new tree[nargs];
8738 const Typed_identifier_list* params = fntype->parameters();
8739 gcc_assert(params != NULL);
8741 nargs = this->args_->size();
8742 int i = is_method ? 1 : 0;
8744 args = new tree[nargs];
8746 Typed_identifier_list::const_iterator pp = params->begin();
8747 Expression_list::const_iterator pe;
8748 for (pe = this->args_->begin();
8749 pe != this->args_->end();
8752 gcc_assert(pp != params->end());
8753 tree arg_val = (*pe)->get_tree(context);
8754 args[i] = Expression::convert_for_assignment(context,
8759 if (args[i] == error_mark_node)
8762 return error_mark_node;
8765 gcc_assert(pp == params->end());
8766 gcc_assert(i == nargs);
8769 tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo)));
8770 if (rettype == error_mark_node)
8773 return error_mark_node;
8778 fn = func->get_tree_without_closure(gogo);
8779 else if (!is_method)
8780 fn = this->fn_->get_tree(context);
8781 else if (bound_method != NULL)
8782 fn = this->bound_method_function(context, bound_method, &args[0]);
8783 else if (interface_method != NULL)
8784 fn = this->interface_method_function(context, interface_method, &args[0]);
8788 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
8791 return error_mark_node;
8795 if (TREE_CODE(fndecl) == ADDR_EXPR)
8796 fndecl = TREE_OPERAND(fndecl, 0);
8798 // Add a type cast in case the type of the function is a recursive
8799 // type which refers to itself.
8800 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
8802 tree fnt = fntype->get_tree(gogo);
8803 if (fnt == error_mark_node)
8804 return error_mark_node;
8805 fn = fold_convert_loc(location, fnt, fn);
8808 // This is to support builtin math functions when using 80387 math.
8809 tree excess_type = NULL_TREE;
8811 && DECL_IS_BUILTIN(fndecl)
8812 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
8814 && ((SCALAR_FLOAT_TYPE_P(rettype)
8815 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
8816 || (COMPLEX_FLOAT_TYPE_P(rettype)
8817 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
8819 excess_type = excess_precision_type(TREE_TYPE(args[0]));
8820 if (excess_type != NULL_TREE)
8822 tree excess_fndecl = mathfn_built_in(excess_type,
8823 DECL_FUNCTION_CODE(fndecl));
8824 if (excess_fndecl == NULL_TREE)
8825 excess_type = NULL_TREE;
8828 fn = build_fold_addr_expr_loc(location, excess_fndecl);
8829 for (int i = 0; i < nargs; ++i)
8830 args[i] = ::convert(excess_type, args[i]);
8835 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
8839 SET_EXPR_LOCATION(ret, location);
8843 tree closure_tree = func->closure()->get_tree(context);
8844 if (closure_tree != error_mark_node)
8845 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
8848 // If this is a recursive function type which returns itself, as in
8850 // we have used ptr_type_node for the return type. Add a cast here
8851 // to the correct type.
8852 if (TREE_TYPE(ret) == ptr_type_node)
8854 tree t = this->type()->base()->get_tree(gogo);
8855 ret = fold_convert_loc(location, t, ret);
8858 if (excess_type != NULL_TREE)
8860 // Calling convert here can undo our excess precision change.
8861 // That may or may not be a bug in convert_to_real.
8862 ret = build1(NOP_EXPR, rettype, ret);
8865 // If there is more than one result, we will refer to the call
8867 if (fntype->results() != NULL && fntype->results()->size() > 1)
8868 ret = save_expr(ret);
8875 // Make a call expression.
8878 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
8879 source_location location)
8881 return new Call_expression(fn, args, is_varargs, location);
8884 // A single result from a call which returns multiple results.
8886 class Call_result_expression : public Expression
8889 Call_result_expression(Call_expression* call, unsigned int index)
8890 : Expression(EXPRESSION_CALL_RESULT, call->location()),
8891 call_(call), index_(index)
8896 do_traverse(Traverse*);
8902 do_determine_type(const Type_context*);
8905 do_check_types(Gogo*);
8910 return new Call_result_expression(this->call_->call_expression(),
8915 do_must_eval_in_order() const
8919 do_get_tree(Translate_context*);
8922 // The underlying call expression.
8924 // Which result we want.
8925 unsigned int index_;
8928 // Traverse a call result.
8931 Call_result_expression::do_traverse(Traverse* traverse)
8933 if (traverse->remember_expression(this->call_))
8935 // We have already traversed the call expression.
8936 return TRAVERSE_CONTINUE;
8938 return Expression::traverse(&this->call_, traverse);
8944 Call_result_expression::do_type()
8946 if (this->classification() == EXPRESSION_ERROR)
8947 return Type::make_error_type();
8949 // THIS->CALL_ can be replaced with a temporary reference due to
8950 // Call_expression::do_must_eval_in_order when there is an error.
8951 Call_expression* ce = this->call_->call_expression();
8954 this->set_is_error();
8955 return Type::make_error_type();
8957 Function_type* fntype = ce->get_function_type();
8960 this->set_is_error();
8961 return Type::make_error_type();
8963 const Typed_identifier_list* results = fntype->results();
8964 if (results == NULL)
8966 this->report_error(_("number of results does not match "
8967 "number of values"));
8968 return Type::make_error_type();
8970 Typed_identifier_list::const_iterator pr = results->begin();
8971 for (unsigned int i = 0; i < this->index_; ++i)
8973 if (pr == results->end())
8977 if (pr == results->end())
8979 this->report_error(_("number of results does not match "
8980 "number of values"));
8981 return Type::make_error_type();
8986 // Check the type. Just make sure that we trigger the warning in
8990 Call_result_expression::do_check_types(Gogo*)
8995 // Determine the type. We have nothing to do here, but the 0 result
8996 // needs to pass down to the caller.
8999 Call_result_expression::do_determine_type(const Type_context*)
9001 this->call_->determine_type_no_context();
9007 Call_result_expression::do_get_tree(Translate_context* context)
9009 tree call_tree = this->call_->get_tree(context);
9010 if (call_tree == error_mark_node)
9011 return error_mark_node;
9012 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9014 gcc_assert(saw_errors());
9015 return error_mark_node;
9017 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9018 for (unsigned int i = 0; i < this->index_; ++i)
9020 gcc_assert(field != NULL_TREE);
9021 field = DECL_CHAIN(field);
9023 gcc_assert(field != NULL_TREE);
9024 return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE);
9027 // Make a reference to a single result of a call which returns
9028 // multiple results.
9031 Expression::make_call_result(Call_expression* call, unsigned int index)
9033 return new Call_result_expression(call, index);
9036 // Class Index_expression.
9041 Index_expression::do_traverse(Traverse* traverse)
9043 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9044 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9045 || (this->end_ != NULL
9046 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9047 return TRAVERSE_EXIT;
9048 return TRAVERSE_CONTINUE;
9051 // Lower an index expression. This converts the generic index
9052 // expression into an array index, a string index, or a map index.
9055 Index_expression::do_lower(Gogo*, Named_object*, int)
9057 source_location location = this->location();
9058 Expression* left = this->left_;
9059 Expression* start = this->start_;
9060 Expression* end = this->end_;
9062 Type* type = left->type();
9063 if (type->is_error())
9064 return Expression::make_error(location);
9065 else if (left->is_type_expression())
9067 error_at(location, "attempt to index type expression");
9068 return Expression::make_error(location);
9070 else if (type->array_type() != NULL)
9071 return Expression::make_array_index(left, start, end, location);
9072 else if (type->points_to() != NULL
9073 && type->points_to()->array_type() != NULL
9074 && !type->points_to()->is_open_array_type())
9076 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9078 return Expression::make_array_index(deref, start, end, location);
9080 else if (type->is_string_type())
9081 return Expression::make_string_index(left, start, end, location);
9082 else if (type->map_type() != NULL)
9086 error_at(location, "invalid slice of map");
9087 return Expression::make_error(location);
9089 Map_index_expression* ret= Expression::make_map_index(left, start,
9091 if (this->is_lvalue_)
9092 ret->set_is_lvalue();
9098 "attempt to index object which is not array, string, or map");
9099 return Expression::make_error(location);
9103 // Make an index expression.
9106 Expression::make_index(Expression* left, Expression* start, Expression* end,
9107 source_location location)
9109 return new Index_expression(left, start, end, location);
9112 // An array index. This is used for both indexing and slicing.
9114 class Array_index_expression : public Expression
9117 Array_index_expression(Expression* array, Expression* start,
9118 Expression* end, source_location location)
9119 : Expression(EXPRESSION_ARRAY_INDEX, location),
9120 array_(array), start_(start), end_(end), type_(NULL)
9125 do_traverse(Traverse*);
9131 do_determine_type(const Type_context*);
9134 do_check_types(Gogo*);
9139 return Expression::make_array_index(this->array_->copy(),
9140 this->start_->copy(),
9143 : this->end_->copy()),
9148 do_is_addressable() const;
9151 do_address_taken(bool escapes)
9152 { this->array_->address_taken(escapes); }
9155 do_get_tree(Translate_context*);
9158 // The array we are getting a value from.
9160 // The start or only index.
9162 // The end index of a slice. This may be NULL for a simple array
9163 // index, or it may be a nil expression for the length of the array.
9165 // The type of the expression.
9169 // Array index traversal.
9172 Array_index_expression::do_traverse(Traverse* traverse)
9174 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9175 return TRAVERSE_EXIT;
9176 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9177 return TRAVERSE_EXIT;
9178 if (this->end_ != NULL)
9180 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9181 return TRAVERSE_EXIT;
9183 return TRAVERSE_CONTINUE;
9186 // Return the type of an array index.
9189 Array_index_expression::do_type()
9191 if (this->type_ == NULL)
9193 Array_type* type = this->array_->type()->array_type();
9195 this->type_ = Type::make_error_type();
9196 else if (this->end_ == NULL)
9197 this->type_ = type->element_type();
9198 else if (type->is_open_array_type())
9200 // A slice of a slice has the same type as the original
9202 this->type_ = this->array_->type()->deref();
9206 // A slice of an array is a slice.
9207 this->type_ = Type::make_array_type(type->element_type(), NULL);
9213 // Set the type of an array index.
9216 Array_index_expression::do_determine_type(const Type_context*)
9218 this->array_->determine_type_no_context();
9219 this->start_->determine_type_no_context();
9220 if (this->end_ != NULL)
9221 this->end_->determine_type_no_context();
9224 // Check types of an array index.
9227 Array_index_expression::do_check_types(Gogo*)
9229 if (this->start_->type()->integer_type() == NULL)
9230 this->report_error(_("index must be integer"));
9231 if (this->end_ != NULL
9232 && this->end_->type()->integer_type() == NULL
9233 && !this->end_->is_nil_expression())
9234 this->report_error(_("slice end must be integer"));
9236 Array_type* array_type = this->array_->type()->array_type();
9237 if (array_type == NULL)
9239 gcc_assert(this->array_->type()->is_error());
9243 unsigned int int_bits =
9244 Type::lookup_integer_type("int")->integer_type()->bits();
9249 bool lval_valid = (array_type->length() != NULL
9250 && array_type->length()->integer_constant_value(true,
9255 if (this->start_->integer_constant_value(true, ival, &dummy))
9257 if (mpz_sgn(ival) < 0
9258 || mpz_sizeinbase(ival, 2) >= int_bits
9260 && (this->end_ == NULL
9261 ? mpz_cmp(ival, lval) >= 0
9262 : mpz_cmp(ival, lval) > 0)))
9264 error_at(this->start_->location(), "array index out of bounds");
9265 this->set_is_error();
9268 if (this->end_ != NULL && !this->end_->is_nil_expression())
9270 if (this->end_->integer_constant_value(true, ival, &dummy))
9272 if (mpz_sgn(ival) < 0
9273 || mpz_sizeinbase(ival, 2) >= int_bits
9274 || (lval_valid && mpz_cmp(ival, lval) > 0))
9276 error_at(this->end_->location(), "array index out of bounds");
9277 this->set_is_error();
9284 // A slice of an array requires an addressable array. A slice of a
9285 // slice is always possible.
9286 if (this->end_ != NULL
9287 && !array_type->is_open_array_type()
9288 && !this->array_->is_addressable())
9289 this->report_error(_("array is not addressable"));
9292 // Return whether this expression is addressable.
9295 Array_index_expression::do_is_addressable() const
9297 // A slice expression is not addressable.
9298 if (this->end_ != NULL)
9301 // An index into a slice is addressable.
9302 if (this->array_->type()->is_open_array_type())
9305 // An index into an array is addressable if the array is
9307 return this->array_->is_addressable();
9310 // Get a tree for an array index.
9313 Array_index_expression::do_get_tree(Translate_context* context)
9315 Gogo* gogo = context->gogo();
9316 source_location loc = this->location();
9318 Array_type* array_type = this->array_->type()->array_type();
9319 if (array_type == NULL)
9321 gcc_assert(this->array_->type()->is_error());
9322 return error_mark_node;
9325 tree type_tree = array_type->get_tree(gogo);
9326 if (type_tree == error_mark_node)
9327 return error_mark_node;
9329 tree array_tree = this->array_->get_tree(context);
9330 if (array_tree == error_mark_node)
9331 return error_mark_node;
9333 if (array_type->length() == NULL && !DECL_P(array_tree))
9334 array_tree = save_expr(array_tree);
9335 tree length_tree = array_type->length_tree(gogo, array_tree);
9336 if (length_tree == error_mark_node)
9337 return error_mark_node;
9338 length_tree = save_expr(length_tree);
9339 tree length_type = TREE_TYPE(length_tree);
9341 tree bad_index = boolean_false_node;
9343 tree start_tree = this->start_->get_tree(context);
9344 if (start_tree == error_mark_node)
9345 return error_mark_node;
9346 if (!DECL_P(start_tree))
9347 start_tree = save_expr(start_tree);
9348 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9349 start_tree = convert_to_integer(length_type, start_tree);
9351 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9354 start_tree = fold_convert_loc(loc, length_type, start_tree);
9355 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
9356 fold_build2_loc(loc,
9360 boolean_type_node, start_tree,
9363 int code = (array_type->length() != NULL
9364 ? (this->end_ == NULL
9365 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9366 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9367 : (this->end_ == NULL
9368 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9369 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9370 tree crash = Gogo::runtime_error(code, loc);
9372 if (this->end_ == NULL)
9374 // Simple array indexing. This has to return an l-value, so
9375 // wrap the index check into START_TREE.
9376 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9377 build3(COND_EXPR, void_type_node,
9378 bad_index, crash, NULL_TREE),
9380 start_tree = fold_convert_loc(loc, sizetype, start_tree);
9382 if (array_type->length() != NULL)
9385 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9386 start_tree, NULL_TREE, NULL_TREE);
9391 tree values = array_type->value_pointer_tree(gogo, array_tree);
9392 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9393 if (element_type_tree == error_mark_node)
9394 return error_mark_node;
9395 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9396 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9397 start_tree, element_size);
9398 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9399 TREE_TYPE(values), values, offset);
9400 return build_fold_indirect_ref(ptr);
9406 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
9407 if (capacity_tree == error_mark_node)
9408 return error_mark_node;
9409 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
9412 if (this->end_->is_nil_expression())
9413 end_tree = length_tree;
9416 end_tree = this->end_->get_tree(context);
9417 if (end_tree == error_mark_node)
9418 return error_mark_node;
9419 if (!DECL_P(end_tree))
9420 end_tree = save_expr(end_tree);
9421 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9422 end_tree = convert_to_integer(length_type, end_tree);
9424 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9427 end_tree = fold_convert_loc(loc, length_type, end_tree);
9429 capacity_tree = save_expr(capacity_tree);
9430 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9431 fold_build2_loc(loc, LT_EXPR,
9433 end_tree, start_tree),
9434 fold_build2_loc(loc, GT_EXPR,
9436 end_tree, capacity_tree));
9437 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9438 bad_index, bad_end);
9441 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9442 if (element_type_tree == error_mark_node)
9443 return error_mark_node;
9444 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9446 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9447 fold_convert_loc(loc, sizetype, start_tree),
9450 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9451 if (value_pointer == error_mark_node)
9452 return error_mark_node;
9454 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9455 TREE_TYPE(value_pointer),
9456 value_pointer, offset);
9458 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9459 end_tree, start_tree);
9461 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9462 capacity_tree, start_tree);
9464 tree struct_tree = this->type()->get_tree(gogo);
9465 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9467 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9469 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9470 tree field = TYPE_FIELDS(struct_tree);
9471 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9473 elt->value = value_pointer;
9475 elt = VEC_quick_push(constructor_elt, init, NULL);
9476 field = DECL_CHAIN(field);
9477 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9479 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
9481 elt = VEC_quick_push(constructor_elt, init, NULL);
9482 field = DECL_CHAIN(field);
9483 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9485 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
9487 tree constructor = build_constructor(struct_tree, init);
9489 if (TREE_CONSTANT(value_pointer)
9490 && TREE_CONSTANT(result_length_tree)
9491 && TREE_CONSTANT(result_capacity_tree))
9492 TREE_CONSTANT(constructor) = 1;
9494 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
9495 build3(COND_EXPR, void_type_node,
9496 bad_index, crash, NULL_TREE),
9500 // Make an array index expression. END may be NULL.
9503 Expression::make_array_index(Expression* array, Expression* start,
9504 Expression* end, source_location location)
9506 // Taking a slice of a composite literal requires moving the literal
9508 if (end != NULL && array->is_composite_literal())
9510 array = Expression::make_heap_composite(array, location);
9511 array = Expression::make_unary(OPERATOR_MULT, array, location);
9513 return new Array_index_expression(array, start, end, location);
9516 // A string index. This is used for both indexing and slicing.
9518 class String_index_expression : public Expression
9521 String_index_expression(Expression* string, Expression* start,
9522 Expression* end, source_location location)
9523 : Expression(EXPRESSION_STRING_INDEX, location),
9524 string_(string), start_(start), end_(end)
9529 do_traverse(Traverse*);
9535 do_determine_type(const Type_context*);
9538 do_check_types(Gogo*);
9543 return Expression::make_string_index(this->string_->copy(),
9544 this->start_->copy(),
9547 : this->end_->copy()),
9552 do_get_tree(Translate_context*);
9555 // The string we are getting a value from.
9556 Expression* string_;
9557 // The start or only index.
9559 // The end index of a slice. This may be NULL for a single index,
9560 // or it may be a nil expression for the length of the string.
9564 // String index traversal.
9567 String_index_expression::do_traverse(Traverse* traverse)
9569 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
9570 return TRAVERSE_EXIT;
9571 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9572 return TRAVERSE_EXIT;
9573 if (this->end_ != NULL)
9575 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9576 return TRAVERSE_EXIT;
9578 return TRAVERSE_CONTINUE;
9581 // Return the type of a string index.
9584 String_index_expression::do_type()
9586 if (this->end_ == NULL)
9587 return Type::lookup_integer_type("uint8");
9589 return this->string_->type();
9592 // Determine the type of a string index.
9595 String_index_expression::do_determine_type(const Type_context*)
9597 this->string_->determine_type_no_context();
9598 this->start_->determine_type_no_context();
9599 if (this->end_ != NULL)
9600 this->end_->determine_type_no_context();
9603 // Check types of a string index.
9606 String_index_expression::do_check_types(Gogo*)
9608 if (this->start_->type()->integer_type() == NULL)
9609 this->report_error(_("index must be integer"));
9610 if (this->end_ != NULL
9611 && this->end_->type()->integer_type() == NULL
9612 && !this->end_->is_nil_expression())
9613 this->report_error(_("slice end must be integer"));
9616 bool sval_valid = this->string_->string_constant_value(&sval);
9621 if (this->start_->integer_constant_value(true, ival, &dummy))
9623 if (mpz_sgn(ival) < 0
9624 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
9626 error_at(this->start_->location(), "string index out of bounds");
9627 this->set_is_error();
9630 if (this->end_ != NULL && !this->end_->is_nil_expression())
9632 if (this->end_->integer_constant_value(true, ival, &dummy))
9634 if (mpz_sgn(ival) < 0
9635 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
9637 error_at(this->end_->location(), "string index out of bounds");
9638 this->set_is_error();
9645 // Get a tree for a string index.
9648 String_index_expression::do_get_tree(Translate_context* context)
9650 source_location loc = this->location();
9652 tree string_tree = this->string_->get_tree(context);
9653 if (string_tree == error_mark_node)
9654 return error_mark_node;
9656 if (this->string_->type()->points_to() != NULL)
9657 string_tree = build_fold_indirect_ref(string_tree);
9658 if (!DECL_P(string_tree))
9659 string_tree = save_expr(string_tree);
9660 tree string_type = TREE_TYPE(string_tree);
9662 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
9663 length_tree = save_expr(length_tree);
9664 tree length_type = TREE_TYPE(length_tree);
9666 tree bad_index = boolean_false_node;
9668 tree start_tree = this->start_->get_tree(context);
9669 if (start_tree == error_mark_node)
9670 return error_mark_node;
9671 if (!DECL_P(start_tree))
9672 start_tree = save_expr(start_tree);
9673 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9674 start_tree = convert_to_integer(length_type, start_tree);
9676 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9679 start_tree = fold_convert_loc(loc, length_type, start_tree);
9681 int code = (this->end_ == NULL
9682 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9683 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
9684 tree crash = Gogo::runtime_error(code, loc);
9686 if (this->end_ == NULL)
9688 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9690 fold_build2_loc(loc, GE_EXPR,
9692 start_tree, length_tree));
9694 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
9695 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
9697 fold_convert_loc(loc, sizetype, start_tree));
9698 tree index = build_fold_indirect_ref_loc(loc, ptr);
9700 return build2(COMPOUND_EXPR, TREE_TYPE(index),
9701 build3(COND_EXPR, void_type_node,
9702 bad_index, crash, NULL_TREE),
9708 if (this->end_->is_nil_expression())
9709 end_tree = build_int_cst(length_type, -1);
9712 end_tree = this->end_->get_tree(context);
9713 if (end_tree == error_mark_node)
9714 return error_mark_node;
9715 if (!DECL_P(end_tree))
9716 end_tree = save_expr(end_tree);
9717 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9718 end_tree = convert_to_integer(length_type, end_tree);
9720 bad_index = Expression::check_bounds(end_tree, length_type,
9723 end_tree = fold_convert_loc(loc, length_type, end_tree);
9726 static tree strslice_fndecl;
9727 tree ret = Gogo::call_builtin(&strslice_fndecl,
9729 "__go_string_slice",
9738 if (ret == error_mark_node)
9739 return error_mark_node;
9740 // This will panic if the bounds are out of range for the
9742 TREE_NOTHROW(strslice_fndecl) = 0;
9744 if (bad_index == boolean_false_node)
9747 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
9748 build3(COND_EXPR, void_type_node,
9749 bad_index, crash, NULL_TREE),
9754 // Make a string index expression. END may be NULL.
9757 Expression::make_string_index(Expression* string, Expression* start,
9758 Expression* end, source_location location)
9760 return new String_index_expression(string, start, end, location);
9765 // Get the type of the map.
9768 Map_index_expression::get_map_type() const
9770 Map_type* mt = this->map_->type()->deref()->map_type();
9772 gcc_assert(saw_errors());
9776 // Map index traversal.
9779 Map_index_expression::do_traverse(Traverse* traverse)
9781 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
9782 return TRAVERSE_EXIT;
9783 return Expression::traverse(&this->index_, traverse);
9786 // Return the type of a map index.
9789 Map_index_expression::do_type()
9791 Map_type* mt = this->get_map_type();
9793 return Type::make_error_type();
9794 Type* type = mt->val_type();
9795 // If this map index is in a tuple assignment, we actually return a
9796 // pointer to the value type. Tuple_map_assignment_statement is
9797 // responsible for handling this correctly. We need to get the type
9798 // right in case this gets assigned to a temporary variable.
9799 if (this->is_in_tuple_assignment_)
9800 type = Type::make_pointer_type(type);
9804 // Fix the type of a map index.
9807 Map_index_expression::do_determine_type(const Type_context*)
9809 this->map_->determine_type_no_context();
9810 Map_type* mt = this->get_map_type();
9811 Type* key_type = mt == NULL ? NULL : mt->key_type();
9812 Type_context subcontext(key_type, false);
9813 this->index_->determine_type(&subcontext);
9816 // Check types of a map index.
9819 Map_index_expression::do_check_types(Gogo*)
9822 Map_type* mt = this->get_map_type();
9825 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
9828 this->report_error(_("incompatible type for map index"));
9831 error_at(this->location(), "incompatible type for map index (%s)",
9833 this->set_is_error();
9838 // Get a tree for a map index.
9841 Map_index_expression::do_get_tree(Translate_context* context)
9843 Map_type* type = this->get_map_type();
9845 return error_mark_node;
9847 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
9848 if (valptr == error_mark_node)
9849 return error_mark_node;
9850 valptr = save_expr(valptr);
9852 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
9854 if (this->is_lvalue_)
9855 return build_fold_indirect_ref(valptr);
9856 else if (this->is_in_tuple_assignment_)
9858 // Tuple_map_assignment_statement is responsible for using this
9864 return fold_build3(COND_EXPR, val_type_tree,
9865 fold_build2(EQ_EXPR, boolean_type_node, valptr,
9866 fold_convert(TREE_TYPE(valptr),
9867 null_pointer_node)),
9868 type->val_type()->get_init_tree(context->gogo(),
9870 build_fold_indirect_ref(valptr));
9874 // Get a tree for the map index. This returns a tree which evaluates
9875 // to a pointer to a value. The pointer will be NULL if the key is
9879 Map_index_expression::get_value_pointer(Translate_context* context,
9882 Map_type* type = this->get_map_type();
9884 return error_mark_node;
9886 tree map_tree = this->map_->get_tree(context);
9887 tree index_tree = this->index_->get_tree(context);
9888 index_tree = Expression::convert_for_assignment(context, type->key_type(),
9889 this->index_->type(),
9892 if (map_tree == error_mark_node || index_tree == error_mark_node)
9893 return error_mark_node;
9895 if (this->map_->type()->points_to() != NULL)
9896 map_tree = build_fold_indirect_ref(map_tree);
9898 // We need to pass in a pointer to the key, so stuff it into a
9902 if (current_function_decl != NULL)
9904 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
9905 DECL_IGNORED_P(tmp) = 0;
9906 DECL_INITIAL(tmp) = index_tree;
9907 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
9908 TREE_ADDRESSABLE(tmp) = 1;
9912 tmp = build_decl(this->location(), VAR_DECL, create_tmp_var_name("M"),
9913 TREE_TYPE(index_tree));
9914 DECL_EXTERNAL(tmp) = 0;
9915 TREE_PUBLIC(tmp) = 0;
9916 TREE_STATIC(tmp) = 1;
9917 DECL_ARTIFICIAL(tmp) = 1;
9918 if (!TREE_CONSTANT(index_tree))
9919 make_tmp = fold_build2_loc(this->location(), INIT_EXPR, void_type_node,
9923 TREE_READONLY(tmp) = 1;
9924 TREE_CONSTANT(tmp) = 1;
9925 DECL_INITIAL(tmp) = index_tree;
9926 make_tmp = NULL_TREE;
9928 rest_of_decl_compilation(tmp, 1, 0);
9930 tree tmpref = fold_convert_loc(this->location(), const_ptr_type_node,
9931 build_fold_addr_expr_loc(this->location(),
9934 static tree map_index_fndecl;
9935 tree call = Gogo::call_builtin(&map_index_fndecl,
9939 const_ptr_type_node,
9940 TREE_TYPE(map_tree),
9942 const_ptr_type_node,
9947 : boolean_false_node));
9948 if (call == error_mark_node)
9949 return error_mark_node;
9950 // This can panic on a map of interface type if the interface holds
9951 // an uncomparable or unhashable type.
9952 TREE_NOTHROW(map_index_fndecl) = 0;
9954 tree val_type_tree = type->val_type()->get_tree(context->gogo());
9955 if (val_type_tree == error_mark_node)
9956 return error_mark_node;
9957 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
9959 tree ret = fold_convert_loc(this->location(), ptr_val_type_tree, call);
9960 if (make_tmp != NULL_TREE)
9961 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
9965 // Make a map index expression.
9967 Map_index_expression*
9968 Expression::make_map_index(Expression* map, Expression* index,
9969 source_location location)
9971 return new Map_index_expression(map, index, location);
9974 // Class Field_reference_expression.
9976 // Return the type of a field reference.
9979 Field_reference_expression::do_type()
9981 Type* type = this->expr_->type();
9982 if (type->is_error())
9984 Struct_type* struct_type = type->struct_type();
9985 gcc_assert(struct_type != NULL);
9986 return struct_type->field(this->field_index_)->type();
9989 // Check the types for a field reference.
9992 Field_reference_expression::do_check_types(Gogo*)
9994 Type* type = this->expr_->type();
9995 if (type->is_error())
9997 Struct_type* struct_type = type->struct_type();
9998 gcc_assert(struct_type != NULL);
9999 gcc_assert(struct_type->field(this->field_index_) != NULL);
10002 // Get a tree for a field reference.
10005 Field_reference_expression::do_get_tree(Translate_context* context)
10007 tree struct_tree = this->expr_->get_tree(context);
10008 if (struct_tree == error_mark_node
10009 || TREE_TYPE(struct_tree) == error_mark_node)
10010 return error_mark_node;
10011 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
10012 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
10013 if (field == NULL_TREE)
10015 // This can happen for a type which refers to itself indirectly
10016 // and then turns out to be erroneous.
10017 gcc_assert(saw_errors());
10018 return error_mark_node;
10020 for (unsigned int i = this->field_index_; i > 0; --i)
10022 field = DECL_CHAIN(field);
10023 gcc_assert(field != NULL_TREE);
10025 if (TREE_TYPE(field) == error_mark_node)
10026 return error_mark_node;
10027 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
10031 // Make a reference to a qualified identifier in an expression.
10033 Field_reference_expression*
10034 Expression::make_field_reference(Expression* expr, unsigned int field_index,
10035 source_location location)
10037 return new Field_reference_expression(expr, field_index, location);
10040 // Class Interface_field_reference_expression.
10042 // Return a tree for the pointer to the function to call.
10045 Interface_field_reference_expression::get_function_tree(Translate_context*,
10048 if (this->expr_->type()->points_to() != NULL)
10049 expr = build_fold_indirect_ref(expr);
10051 tree expr_type = TREE_TYPE(expr);
10052 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10054 tree field = TYPE_FIELDS(expr_type);
10055 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
10057 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10058 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table)));
10060 table = build_fold_indirect_ref(table);
10061 gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
10063 std::string name = Gogo::unpack_hidden_name(this->name_);
10064 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
10065 field != NULL_TREE;
10066 field = DECL_CHAIN(field))
10068 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
10071 gcc_assert(field != NULL_TREE);
10073 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
10076 // Return a tree for the first argument to pass to the interface
10080 Interface_field_reference_expression::get_underlying_object_tree(
10081 Translate_context*,
10084 if (this->expr_->type()->points_to() != NULL)
10085 expr = build_fold_indirect_ref(expr);
10087 tree expr_type = TREE_TYPE(expr);
10088 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10090 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
10091 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
10093 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10099 Interface_field_reference_expression::do_traverse(Traverse* traverse)
10101 return Expression::traverse(&this->expr_, traverse);
10104 // Return the type of an interface field reference.
10107 Interface_field_reference_expression::do_type()
10109 Type* expr_type = this->expr_->type();
10111 Type* points_to = expr_type->points_to();
10112 if (points_to != NULL)
10113 expr_type = points_to;
10115 Interface_type* interface_type = expr_type->interface_type();
10116 if (interface_type == NULL)
10117 return Type::make_error_type();
10119 const Typed_identifier* method = interface_type->find_method(this->name_);
10120 if (method == NULL)
10121 return Type::make_error_type();
10123 return method->type();
10126 // Determine types.
10129 Interface_field_reference_expression::do_determine_type(const Type_context*)
10131 this->expr_->determine_type_no_context();
10134 // Check the types for an interface field reference.
10137 Interface_field_reference_expression::do_check_types(Gogo*)
10139 Type* type = this->expr_->type();
10141 Type* points_to = type->points_to();
10142 if (points_to != NULL)
10145 Interface_type* interface_type = type->interface_type();
10146 if (interface_type == NULL)
10148 if (!type->is_error_type())
10149 this->report_error(_("expected interface or pointer to interface"));
10153 const Typed_identifier* method =
10154 interface_type->find_method(this->name_);
10155 if (method == NULL)
10157 error_at(this->location(), "method %qs not in interface",
10158 Gogo::message_name(this->name_).c_str());
10159 this->set_is_error();
10164 // Get a tree for a reference to a field in an interface. There is no
10165 // standard tree type representation for this: it's a function
10166 // attached to its first argument, like a Bound_method_expression.
10167 // The only places it may currently be used are in a Call_expression
10168 // or a Go_statement, which will take it apart directly. So this has
10169 // nothing to do at present.
10172 Interface_field_reference_expression::do_get_tree(Translate_context*)
10177 // Make a reference to a field in an interface.
10180 Expression::make_interface_field_reference(Expression* expr,
10181 const std::string& field,
10182 source_location location)
10184 return new Interface_field_reference_expression(expr, field, location);
10187 // A general selector. This is a Parser_expression for LEFT.NAME. It
10188 // is lowered after we know the type of the left hand side.
10190 class Selector_expression : public Parser_expression
10193 Selector_expression(Expression* left, const std::string& name,
10194 source_location location)
10195 : Parser_expression(EXPRESSION_SELECTOR, location),
10196 left_(left), name_(name)
10201 do_traverse(Traverse* traverse)
10202 { return Expression::traverse(&this->left_, traverse); }
10205 do_lower(Gogo*, Named_object*, int);
10210 return new Selector_expression(this->left_->copy(), this->name_,
10216 lower_method_expression(Gogo*);
10218 // The expression on the left hand side.
10220 // The name on the right hand side.
10224 // Lower a selector expression once we know the real type of the left
10228 Selector_expression::do_lower(Gogo* gogo, Named_object*, int)
10230 Expression* left = this->left_;
10231 if (left->is_type_expression())
10232 return this->lower_method_expression(gogo);
10233 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
10237 // Lower a method expression T.M or (*T).M. We turn this into a
10238 // function literal.
10241 Selector_expression::lower_method_expression(Gogo* gogo)
10243 source_location location = this->location();
10244 Type* type = this->left_->type();
10245 const std::string& name(this->name_);
10248 if (type->points_to() == NULL)
10249 is_pointer = false;
10253 type = type->points_to();
10255 Named_type* nt = type->named_type();
10259 ("method expression requires named type or "
10260 "pointer to named type"));
10261 return Expression::make_error(location);
10265 Method* method = nt->method_function(name, &is_ambiguous);
10266 const Typed_identifier* imethod = NULL;
10267 if (method == NULL && !is_pointer)
10269 Interface_type* it = nt->interface_type();
10271 imethod = it->find_method(name);
10274 if (method == NULL && imethod == NULL)
10277 error_at(location, "type %<%s%s%> has no method %<%s%>",
10278 is_pointer ? "*" : "",
10279 nt->message_name().c_str(),
10280 Gogo::message_name(name).c_str());
10282 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
10283 Gogo::message_name(name).c_str(),
10284 is_pointer ? "*" : "",
10285 nt->message_name().c_str());
10286 return Expression::make_error(location);
10289 if (method != NULL && !is_pointer && !method->is_value_method())
10291 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
10292 nt->message_name().c_str(),
10293 Gogo::message_name(name).c_str());
10294 return Expression::make_error(location);
10297 // Build a new function type in which the receiver becomes the first
10299 Function_type* method_type;
10300 if (method != NULL)
10302 method_type = method->type();
10303 gcc_assert(method_type->is_method());
10307 method_type = imethod->type()->function_type();
10308 gcc_assert(method_type != NULL && !method_type->is_method());
10311 const char* const receiver_name = "$this";
10312 Typed_identifier_list* parameters = new Typed_identifier_list();
10313 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
10316 const Typed_identifier_list* method_parameters = method_type->parameters();
10317 if (method_parameters != NULL)
10319 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10320 p != method_parameters->end();
10322 parameters->push_back(*p);
10325 const Typed_identifier_list* method_results = method_type->results();
10326 Typed_identifier_list* results;
10327 if (method_results == NULL)
10331 results = new Typed_identifier_list();
10332 for (Typed_identifier_list::const_iterator p = method_results->begin();
10333 p != method_results->end();
10335 results->push_back(*p);
10338 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
10340 if (method_type->is_varargs())
10341 fntype->set_is_varargs();
10343 // We generate methods which always takes a pointer to the receiver
10344 // as their first argument. If this is for a pointer type, we can
10345 // simply reuse the existing function. We use an internal hack to
10346 // get the right type.
10348 if (method != NULL && is_pointer)
10350 Named_object* mno = (method->needs_stub_method()
10351 ? method->stub_object()
10352 : method->named_object());
10353 Expression* f = Expression::make_func_reference(mno, NULL, location);
10354 f = Expression::make_cast(fntype, f, location);
10355 Type_conversion_expression* tce =
10356 static_cast<Type_conversion_expression*>(f);
10357 tce->set_may_convert_function_types();
10361 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
10364 Named_object* vno = gogo->lookup(receiver_name, NULL);
10365 gcc_assert(vno != NULL);
10366 Expression* ve = Expression::make_var_reference(vno, location);
10368 if (method != NULL)
10369 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
10371 bm = Expression::make_interface_field_reference(ve, name, location);
10373 // Even though we found the method above, if it has an error type we
10374 // may see an error here.
10375 if (bm->is_error_expression())
10377 gogo->finish_function(location);
10381 Expression_list* args;
10382 if (method_parameters == NULL)
10386 args = new Expression_list();
10387 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10388 p != method_parameters->end();
10391 vno = gogo->lookup(p->name(), NULL);
10392 gcc_assert(vno != NULL);
10393 args->push_back(Expression::make_var_reference(vno, location));
10397 Call_expression* call = Expression::make_call(bm, args,
10398 method_type->is_varargs(),
10401 size_t count = call->result_count();
10404 s = Statement::make_statement(call);
10407 Expression_list* retvals = new Expression_list();
10409 retvals->push_back(call);
10412 for (size_t i = 0; i < count; ++i)
10413 retvals->push_back(Expression::make_call_result(call, i));
10415 s = Statement::make_return_statement(no->func_value()->type()->results(),
10416 retvals, location);
10418 gogo->add_statement(s);
10420 gogo->finish_function(location);
10422 return Expression::make_func_reference(no, NULL, location);
10425 // Make a selector expression.
10428 Expression::make_selector(Expression* left, const std::string& name,
10429 source_location location)
10431 return new Selector_expression(left, name, location);
10434 // Implement the builtin function new.
10436 class Allocation_expression : public Expression
10439 Allocation_expression(Type* type, source_location location)
10440 : Expression(EXPRESSION_ALLOCATION, location),
10446 do_traverse(Traverse* traverse)
10447 { return Type::traverse(this->type_, traverse); }
10451 { return Type::make_pointer_type(this->type_); }
10454 do_determine_type(const Type_context*)
10458 do_check_types(Gogo*);
10462 { return new Allocation_expression(this->type_, this->location()); }
10465 do_get_tree(Translate_context*);
10468 // The type we are allocating.
10472 // Check the type of an allocation expression.
10475 Allocation_expression::do_check_types(Gogo*)
10477 if (this->type_->function_type() != NULL)
10478 this->report_error(_("invalid new of function type"));
10481 // Return a tree for an allocation expression.
10484 Allocation_expression::do_get_tree(Translate_context* context)
10486 tree type_tree = this->type_->get_tree(context->gogo());
10487 if (type_tree == error_mark_node)
10488 return error_mark_node;
10489 tree size_tree = TYPE_SIZE_UNIT(type_tree);
10490 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
10492 if (space == error_mark_node)
10493 return error_mark_node;
10494 return fold_convert(build_pointer_type(type_tree), space);
10497 // Make an allocation expression.
10500 Expression::make_allocation(Type* type, source_location location)
10502 return new Allocation_expression(type, location);
10505 // Implement the builtin function make.
10507 class Make_expression : public Expression
10510 Make_expression(Type* type, Expression_list* args, source_location location)
10511 : Expression(EXPRESSION_MAKE, location),
10512 type_(type), args_(args)
10517 do_traverse(Traverse* traverse);
10521 { return this->type_; }
10524 do_determine_type(const Type_context*);
10527 do_check_types(Gogo*);
10532 return new Make_expression(this->type_, this->args_->copy(),
10537 do_get_tree(Translate_context*);
10540 // The type we are making.
10542 // The arguments to pass to the make routine.
10543 Expression_list* args_;
10549 Make_expression::do_traverse(Traverse* traverse)
10551 if (this->args_ != NULL
10552 && this->args_->traverse(traverse) == TRAVERSE_EXIT)
10553 return TRAVERSE_EXIT;
10554 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10555 return TRAVERSE_EXIT;
10556 return TRAVERSE_CONTINUE;
10559 // Set types of arguments.
10562 Make_expression::do_determine_type(const Type_context*)
10564 if (this->args_ != NULL)
10566 Type_context context(Type::lookup_integer_type("int"), false);
10567 for (Expression_list::const_iterator pe = this->args_->begin();
10568 pe != this->args_->end();
10570 (*pe)->determine_type(&context);
10574 // Check types for a make expression.
10577 Make_expression::do_check_types(Gogo*)
10579 if (this->type_->channel_type() == NULL
10580 && this->type_->map_type() == NULL
10581 && (this->type_->array_type() == NULL
10582 || this->type_->array_type()->length() != NULL))
10583 this->report_error(_("invalid type for make function"));
10584 else if (!this->type_->check_make_expression(this->args_, this->location()))
10585 this->set_is_error();
10588 // Return a tree for a make expression.
10591 Make_expression::do_get_tree(Translate_context* context)
10593 return this->type_->make_expression_tree(context, this->args_,
10597 // Make a make expression.
10600 Expression::make_make(Type* type, Expression_list* args,
10601 source_location location)
10603 return new Make_expression(type, args, location);
10606 // Construct a struct.
10608 class Struct_construction_expression : public Expression
10611 Struct_construction_expression(Type* type, Expression_list* vals,
10612 source_location location)
10613 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
10614 type_(type), vals_(vals)
10617 // Return whether this is a constant initializer.
10619 is_constant_struct() const;
10623 do_traverse(Traverse* traverse);
10627 { return this->type_; }
10630 do_determine_type(const Type_context*);
10633 do_check_types(Gogo*);
10638 return new Struct_construction_expression(this->type_, this->vals_->copy(),
10643 do_is_addressable() const
10647 do_get_tree(Translate_context*);
10650 do_export(Export*) const;
10653 // The type of the struct to construct.
10655 // The list of values, in order of the fields in the struct. A NULL
10656 // entry means that the field should be zero-initialized.
10657 Expression_list* vals_;
10663 Struct_construction_expression::do_traverse(Traverse* traverse)
10665 if (this->vals_ != NULL
10666 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10667 return TRAVERSE_EXIT;
10668 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10669 return TRAVERSE_EXIT;
10670 return TRAVERSE_CONTINUE;
10673 // Return whether this is a constant initializer.
10676 Struct_construction_expression::is_constant_struct() const
10678 if (this->vals_ == NULL)
10680 for (Expression_list::const_iterator pv = this->vals_->begin();
10681 pv != this->vals_->end();
10685 && !(*pv)->is_constant()
10686 && (!(*pv)->is_composite_literal()
10687 || (*pv)->is_nonconstant_composite_literal()))
10691 const Struct_field_list* fields = this->type_->struct_type()->fields();
10692 for (Struct_field_list::const_iterator pf = fields->begin();
10693 pf != fields->end();
10696 // There are no constant constructors for interfaces.
10697 if (pf->type()->interface_type() != NULL)
10704 // Final type determination.
10707 Struct_construction_expression::do_determine_type(const Type_context*)
10709 if (this->vals_ == NULL)
10711 const Struct_field_list* fields = this->type_->struct_type()->fields();
10712 Expression_list::const_iterator pv = this->vals_->begin();
10713 for (Struct_field_list::const_iterator pf = fields->begin();
10714 pf != fields->end();
10717 if (pv == this->vals_->end())
10721 Type_context subcontext(pf->type(), false);
10722 (*pv)->determine_type(&subcontext);
10725 // Extra values are an error we will report elsewhere; we still want
10726 // to determine the type to avoid knockon errors.
10727 for (; pv != this->vals_->end(); ++pv)
10728 (*pv)->determine_type_no_context();
10734 Struct_construction_expression::do_check_types(Gogo*)
10736 if (this->vals_ == NULL)
10739 Struct_type* st = this->type_->struct_type();
10740 if (this->vals_->size() > st->field_count())
10742 this->report_error(_("too many expressions for struct"));
10746 const Struct_field_list* fields = st->fields();
10747 Expression_list::const_iterator pv = this->vals_->begin();
10749 for (Struct_field_list::const_iterator pf = fields->begin();
10750 pf != fields->end();
10753 if (pv == this->vals_->end())
10755 this->report_error(_("too few expressions for struct"));
10762 std::string reason;
10763 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
10765 if (reason.empty())
10766 error_at((*pv)->location(),
10767 "incompatible type for field %d in struct construction",
10770 error_at((*pv)->location(),
10771 ("incompatible type for field %d in "
10772 "struct construction (%s)"),
10773 i + 1, reason.c_str());
10774 this->set_is_error();
10777 gcc_assert(pv == this->vals_->end());
10780 // Return a tree for constructing a struct.
10783 Struct_construction_expression::do_get_tree(Translate_context* context)
10785 Gogo* gogo = context->gogo();
10787 if (this->vals_ == NULL)
10788 return this->type_->get_init_tree(gogo, false);
10790 tree type_tree = this->type_->get_tree(gogo);
10791 if (type_tree == error_mark_node)
10792 return error_mark_node;
10793 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10795 bool is_constant = true;
10796 const Struct_field_list* fields = this->type_->struct_type()->fields();
10797 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
10799 Struct_field_list::const_iterator pf = fields->begin();
10800 Expression_list::const_iterator pv = this->vals_->begin();
10801 for (tree field = TYPE_FIELDS(type_tree);
10802 field != NULL_TREE;
10803 field = DECL_CHAIN(field), ++pf)
10805 gcc_assert(pf != fields->end());
10808 if (pv == this->vals_->end())
10809 val = pf->type()->get_init_tree(gogo, false);
10810 else if (*pv == NULL)
10812 val = pf->type()->get_init_tree(gogo, false);
10817 val = Expression::convert_for_assignment(context, pf->type(),
10819 (*pv)->get_tree(context),
10824 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
10825 return error_mark_node;
10827 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
10828 elt->index = field;
10830 if (!TREE_CONSTANT(val))
10831 is_constant = false;
10833 gcc_assert(pf == fields->end());
10835 tree ret = build_constructor(type_tree, elts);
10837 TREE_CONSTANT(ret) = 1;
10841 // Export a struct construction.
10844 Struct_construction_expression::do_export(Export* exp) const
10846 exp->write_c_string("convert(");
10847 exp->write_type(this->type_);
10848 for (Expression_list::const_iterator pv = this->vals_->begin();
10849 pv != this->vals_->end();
10852 exp->write_c_string(", ");
10854 (*pv)->export_expression(exp);
10856 exp->write_c_string(")");
10859 // Make a struct composite literal. This used by the thunk code.
10862 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
10863 source_location location)
10865 gcc_assert(type->struct_type() != NULL);
10866 return new Struct_construction_expression(type, vals, location);
10869 // Construct an array. This class is not used directly; instead we
10870 // use the child classes, Fixed_array_construction_expression and
10871 // Open_array_construction_expression.
10873 class Array_construction_expression : public Expression
10876 Array_construction_expression(Expression_classification classification,
10877 Type* type, Expression_list* vals,
10878 source_location location)
10879 : Expression(classification, location),
10880 type_(type), vals_(vals)
10884 // Return whether this is a constant initializer.
10886 is_constant_array() const;
10888 // Return the number of elements.
10890 element_count() const
10891 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
10895 do_traverse(Traverse* traverse);
10899 { return this->type_; }
10902 do_determine_type(const Type_context*);
10905 do_check_types(Gogo*);
10908 do_is_addressable() const
10912 do_export(Export*) const;
10914 // The list of values.
10917 { return this->vals_; }
10919 // Get a constructor tree for the array values.
10921 get_constructor_tree(Translate_context* context, tree type_tree);
10924 // The type of the array to construct.
10926 // The list of values.
10927 Expression_list* vals_;
10933 Array_construction_expression::do_traverse(Traverse* traverse)
10935 if (this->vals_ != NULL
10936 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10937 return TRAVERSE_EXIT;
10938 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10939 return TRAVERSE_EXIT;
10940 return TRAVERSE_CONTINUE;
10943 // Return whether this is a constant initializer.
10946 Array_construction_expression::is_constant_array() const
10948 if (this->vals_ == NULL)
10951 // There are no constant constructors for interfaces.
10952 if (this->type_->array_type()->element_type()->interface_type() != NULL)
10955 for (Expression_list::const_iterator pv = this->vals_->begin();
10956 pv != this->vals_->end();
10960 && !(*pv)->is_constant()
10961 && (!(*pv)->is_composite_literal()
10962 || (*pv)->is_nonconstant_composite_literal()))
10968 // Final type determination.
10971 Array_construction_expression::do_determine_type(const Type_context*)
10973 if (this->vals_ == NULL)
10975 Type_context subcontext(this->type_->array_type()->element_type(), false);
10976 for (Expression_list::const_iterator pv = this->vals_->begin();
10977 pv != this->vals_->end();
10981 (*pv)->determine_type(&subcontext);
10988 Array_construction_expression::do_check_types(Gogo*)
10990 if (this->vals_ == NULL)
10993 Array_type* at = this->type_->array_type();
10995 Type* element_type = at->element_type();
10996 for (Expression_list::const_iterator pv = this->vals_->begin();
10997 pv != this->vals_->end();
11001 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
11003 error_at((*pv)->location(),
11004 "incompatible type for element %d in composite literal",
11006 this->set_is_error();
11010 Expression* length = at->length();
11011 if (length != NULL)
11016 if (at->length()->integer_constant_value(true, val, &type))
11018 if (this->vals_->size() > mpz_get_ui(val))
11019 this->report_error(_("too many elements in composite literal"));
11025 // Get a constructor tree for the array values.
11028 Array_construction_expression::get_constructor_tree(Translate_context* context,
11031 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11032 (this->vals_ == NULL
11034 : this->vals_->size()));
11035 Type* element_type = this->type_->array_type()->element_type();
11036 bool is_constant = true;
11037 if (this->vals_ != NULL)
11040 for (Expression_list::const_iterator pv = this->vals_->begin();
11041 pv != this->vals_->end();
11044 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
11045 elt->index = size_int(i);
11047 elt->value = element_type->get_init_tree(context->gogo(), false);
11050 tree value_tree = (*pv)->get_tree(context);
11051 elt->value = Expression::convert_for_assignment(context,
11057 if (elt->value == error_mark_node)
11058 return error_mark_node;
11059 if (!TREE_CONSTANT(elt->value))
11060 is_constant = false;
11064 tree ret = build_constructor(type_tree, values);
11066 TREE_CONSTANT(ret) = 1;
11070 // Export an array construction.
11073 Array_construction_expression::do_export(Export* exp) const
11075 exp->write_c_string("convert(");
11076 exp->write_type(this->type_);
11077 if (this->vals_ != NULL)
11079 for (Expression_list::const_iterator pv = this->vals_->begin();
11080 pv != this->vals_->end();
11083 exp->write_c_string(", ");
11085 (*pv)->export_expression(exp);
11088 exp->write_c_string(")");
11091 // Construct a fixed array.
11093 class Fixed_array_construction_expression :
11094 public Array_construction_expression
11097 Fixed_array_construction_expression(Type* type, Expression_list* vals,
11098 source_location location)
11099 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
11100 type, vals, location)
11102 gcc_assert(type->array_type() != NULL
11103 && type->array_type()->length() != NULL);
11110 return new Fixed_array_construction_expression(this->type(),
11111 (this->vals() == NULL
11113 : this->vals()->copy()),
11118 do_get_tree(Translate_context*);
11121 // Return a tree for constructing a fixed array.
11124 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
11126 return this->get_constructor_tree(context,
11127 this->type()->get_tree(context->gogo()));
11130 // Construct an open array.
11132 class Open_array_construction_expression : public Array_construction_expression
11135 Open_array_construction_expression(Type* type, Expression_list* vals,
11136 source_location location)
11137 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
11138 type, vals, location)
11140 gcc_assert(type->array_type() != NULL
11141 && type->array_type()->length() == NULL);
11145 // Note that taking the address of an open array literal is invalid.
11150 return new Open_array_construction_expression(this->type(),
11151 (this->vals() == NULL
11153 : this->vals()->copy()),
11158 do_get_tree(Translate_context*);
11161 // Return a tree for constructing an open array.
11164 Open_array_construction_expression::do_get_tree(Translate_context* context)
11166 Array_type* array_type = this->type()->array_type();
11167 if (array_type == NULL)
11169 gcc_assert(this->type()->is_error());
11170 return error_mark_node;
11173 Type* element_type = array_type->element_type();
11174 tree element_type_tree = element_type->get_tree(context->gogo());
11175 if (element_type_tree == error_mark_node)
11176 return error_mark_node;
11180 if (this->vals() == NULL || this->vals()->empty())
11182 // We need to create a unique value.
11183 tree max = size_int(0);
11184 tree constructor_type = build_array_type(element_type_tree,
11185 build_index_type(max));
11186 if (constructor_type == error_mark_node)
11187 return error_mark_node;
11188 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
11189 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
11190 elt->index = size_int(0);
11191 elt->value = element_type->get_init_tree(context->gogo(), false);
11192 values = build_constructor(constructor_type, vec);
11193 if (TREE_CONSTANT(elt->value))
11194 TREE_CONSTANT(values) = 1;
11195 length_tree = size_int(0);
11199 tree max = size_int(this->vals()->size() - 1);
11200 tree constructor_type = build_array_type(element_type_tree,
11201 build_index_type(max));
11202 if (constructor_type == error_mark_node)
11203 return error_mark_node;
11204 values = this->get_constructor_tree(context, constructor_type);
11205 length_tree = size_int(this->vals()->size());
11208 if (values == error_mark_node)
11209 return error_mark_node;
11211 bool is_constant_initializer = TREE_CONSTANT(values);
11213 // We have to copy the initial values into heap memory if we are in
11214 // a function or if the values are not constants. We also have to
11215 // copy them if they may contain pointers in a non-constant context,
11216 // as otherwise the garbage collector won't see them.
11217 bool copy_to_heap = (context->function() != NULL
11218 || !is_constant_initializer
11219 || (element_type->has_pointer()
11220 && !context->is_const()));
11222 if (is_constant_initializer)
11224 tree tmp = build_decl(this->location(), VAR_DECL,
11225 create_tmp_var_name("C"), TREE_TYPE(values));
11226 DECL_EXTERNAL(tmp) = 0;
11227 TREE_PUBLIC(tmp) = 0;
11228 TREE_STATIC(tmp) = 1;
11229 DECL_ARTIFICIAL(tmp) = 1;
11232 // If we are not copying the value to the heap, we will only
11233 // initialize the value once, so we can use this directly
11234 // rather than copying it. In that case we can't make it
11235 // read-only, because the program is permitted to change it.
11236 TREE_READONLY(tmp) = 1;
11237 TREE_CONSTANT(tmp) = 1;
11239 DECL_INITIAL(tmp) = values;
11240 rest_of_decl_compilation(tmp, 1, 0);
11248 // the initializer will only run once.
11249 space = build_fold_addr_expr(values);
11254 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
11255 space = context->gogo()->allocate_memory(element_type, memsize,
11257 space = save_expr(space);
11259 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
11260 tree ref = build_fold_indirect_ref_loc(this->location(), s);
11261 TREE_THIS_NOTRAP(ref) = 1;
11262 set = build2(MODIFY_EXPR, void_type_node, ref, values);
11265 // Build a constructor for the open array.
11267 tree type_tree = this->type()->get_tree(context->gogo());
11268 if (type_tree == error_mark_node)
11269 return error_mark_node;
11270 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11272 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
11274 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
11275 tree field = TYPE_FIELDS(type_tree);
11276 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
11277 elt->index = field;
11278 elt->value = fold_convert(TREE_TYPE(field), space);
11280 elt = VEC_quick_push(constructor_elt, init, NULL);
11281 field = DECL_CHAIN(field);
11282 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
11283 elt->index = field;
11284 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11286 elt = VEC_quick_push(constructor_elt, init, NULL);
11287 field = DECL_CHAIN(field);
11288 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
11289 elt->index = field;
11290 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11292 tree constructor = build_constructor(type_tree, init);
11293 if (constructor == error_mark_node)
11294 return error_mark_node;
11296 TREE_CONSTANT(constructor) = 1;
11298 if (set == NULL_TREE)
11299 return constructor;
11301 return build2(COMPOUND_EXPR, type_tree, set, constructor);
11304 // Make a slice composite literal. This is used by the type
11305 // descriptor code.
11308 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
11309 source_location location)
11311 gcc_assert(type->is_open_array_type());
11312 return new Open_array_construction_expression(type, vals, location);
11315 // Construct a map.
11317 class Map_construction_expression : public Expression
11320 Map_construction_expression(Type* type, Expression_list* vals,
11321 source_location location)
11322 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
11323 type_(type), vals_(vals)
11324 { gcc_assert(vals == NULL || vals->size() % 2 == 0); }
11328 do_traverse(Traverse* traverse);
11332 { return this->type_; }
11335 do_determine_type(const Type_context*);
11338 do_check_types(Gogo*);
11343 return new Map_construction_expression(this->type_, this->vals_->copy(),
11348 do_get_tree(Translate_context*);
11351 do_export(Export*) const;
11354 // The type of the map to construct.
11356 // The list of values.
11357 Expression_list* vals_;
11363 Map_construction_expression::do_traverse(Traverse* traverse)
11365 if (this->vals_ != NULL
11366 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11367 return TRAVERSE_EXIT;
11368 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11369 return TRAVERSE_EXIT;
11370 return TRAVERSE_CONTINUE;
11373 // Final type determination.
11376 Map_construction_expression::do_determine_type(const Type_context*)
11378 if (this->vals_ == NULL)
11381 Map_type* mt = this->type_->map_type();
11382 Type_context key_context(mt->key_type(), false);
11383 Type_context val_context(mt->val_type(), false);
11384 for (Expression_list::const_iterator pv = this->vals_->begin();
11385 pv != this->vals_->end();
11388 (*pv)->determine_type(&key_context);
11390 (*pv)->determine_type(&val_context);
11397 Map_construction_expression::do_check_types(Gogo*)
11399 if (this->vals_ == NULL)
11402 Map_type* mt = this->type_->map_type();
11404 Type* key_type = mt->key_type();
11405 Type* val_type = mt->val_type();
11406 for (Expression_list::const_iterator pv = this->vals_->begin();
11407 pv != this->vals_->end();
11410 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
11412 error_at((*pv)->location(),
11413 "incompatible type for element %d key in map construction",
11415 this->set_is_error();
11418 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
11420 error_at((*pv)->location(),
11421 ("incompatible type for element %d value "
11422 "in map construction"),
11424 this->set_is_error();
11429 // Return a tree for constructing a map.
11432 Map_construction_expression::do_get_tree(Translate_context* context)
11434 Gogo* gogo = context->gogo();
11435 source_location loc = this->location();
11437 Map_type* mt = this->type_->map_type();
11439 // Build a struct to hold the key and value.
11440 tree struct_type = make_node(RECORD_TYPE);
11442 Type* key_type = mt->key_type();
11443 tree id = get_identifier("__key");
11444 tree key_type_tree = key_type->get_tree(gogo);
11445 if (key_type_tree == error_mark_node)
11446 return error_mark_node;
11447 tree key_field = build_decl(loc, FIELD_DECL, id, key_type_tree);
11448 DECL_CONTEXT(key_field) = struct_type;
11449 TYPE_FIELDS(struct_type) = key_field;
11451 Type* val_type = mt->val_type();
11452 id = get_identifier("__val");
11453 tree val_type_tree = val_type->get_tree(gogo);
11454 if (val_type_tree == error_mark_node)
11455 return error_mark_node;
11456 tree val_field = build_decl(loc, FIELD_DECL, id, val_type_tree);
11457 DECL_CONTEXT(val_field) = struct_type;
11458 DECL_CHAIN(key_field) = val_field;
11460 layout_type(struct_type);
11462 bool is_constant = true;
11467 if (this->vals_ == NULL || this->vals_->empty())
11469 valaddr = null_pointer_node;
11470 make_tmp = NULL_TREE;
11474 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11475 this->vals_->size() / 2);
11477 for (Expression_list::const_iterator pv = this->vals_->begin();
11478 pv != this->vals_->end();
11481 bool one_is_constant = true;
11483 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
11485 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
11486 elt->index = key_field;
11487 tree val_tree = (*pv)->get_tree(context);
11488 elt->value = Expression::convert_for_assignment(context, key_type,
11491 if (elt->value == error_mark_node)
11492 return error_mark_node;
11493 if (!TREE_CONSTANT(elt->value))
11494 one_is_constant = false;
11498 elt = VEC_quick_push(constructor_elt, one, NULL);
11499 elt->index = val_field;
11500 val_tree = (*pv)->get_tree(context);
11501 elt->value = Expression::convert_for_assignment(context, val_type,
11504 if (elt->value == error_mark_node)
11505 return error_mark_node;
11506 if (!TREE_CONSTANT(elt->value))
11507 one_is_constant = false;
11509 elt = VEC_quick_push(constructor_elt, values, NULL);
11510 elt->index = size_int(i);
11511 elt->value = build_constructor(struct_type, one);
11512 if (one_is_constant)
11513 TREE_CONSTANT(elt->value) = 1;
11515 is_constant = false;
11518 tree index_type = build_index_type(size_int(i - 1));
11519 tree array_type = build_array_type(struct_type, index_type);
11520 tree init = build_constructor(array_type, values);
11522 TREE_CONSTANT(init) = 1;
11524 if (current_function_decl != NULL)
11526 tmp = create_tmp_var(array_type, get_name(array_type));
11527 DECL_INITIAL(tmp) = init;
11528 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
11529 TREE_ADDRESSABLE(tmp) = 1;
11533 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
11534 DECL_EXTERNAL(tmp) = 0;
11535 TREE_PUBLIC(tmp) = 0;
11536 TREE_STATIC(tmp) = 1;
11537 DECL_ARTIFICIAL(tmp) = 1;
11538 if (!TREE_CONSTANT(init))
11539 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
11543 TREE_READONLY(tmp) = 1;
11544 TREE_CONSTANT(tmp) = 1;
11545 DECL_INITIAL(tmp) = init;
11546 make_tmp = NULL_TREE;
11548 rest_of_decl_compilation(tmp, 1, 0);
11551 valaddr = build_fold_addr_expr(tmp);
11554 tree descriptor = gogo->map_descriptor(mt);
11556 tree type_tree = this->type_->get_tree(gogo);
11557 if (type_tree == error_mark_node)
11558 return error_mark_node;
11560 static tree construct_map_fndecl;
11561 tree call = Gogo::call_builtin(&construct_map_fndecl,
11563 "__go_construct_map",
11566 TREE_TYPE(descriptor),
11571 TYPE_SIZE_UNIT(struct_type),
11573 byte_position(val_field),
11575 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
11576 const_ptr_type_node,
11577 fold_convert(const_ptr_type_node, valaddr));
11578 if (call == error_mark_node)
11579 return error_mark_node;
11582 if (make_tmp == NULL)
11585 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
11589 // Export an array construction.
11592 Map_construction_expression::do_export(Export* exp) const
11594 exp->write_c_string("convert(");
11595 exp->write_type(this->type_);
11596 for (Expression_list::const_iterator pv = this->vals_->begin();
11597 pv != this->vals_->end();
11600 exp->write_c_string(", ");
11601 (*pv)->export_expression(exp);
11603 exp->write_c_string(")");
11606 // A general composite literal. This is lowered to a type specific
11609 class Composite_literal_expression : public Parser_expression
11612 Composite_literal_expression(Type* type, int depth, bool has_keys,
11613 Expression_list* vals, source_location location)
11614 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
11615 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
11620 do_traverse(Traverse* traverse);
11623 do_lower(Gogo*, Named_object*, int);
11628 return new Composite_literal_expression(this->type_, this->depth_,
11630 (this->vals_ == NULL
11632 : this->vals_->copy()),
11638 lower_struct(Gogo*, Type*);
11641 lower_array(Type*);
11644 make_array(Type*, Expression_list*);
11647 lower_map(Gogo*, Named_object*, Type*);
11649 // The type of the composite literal.
11651 // The depth within a list of composite literals within a composite
11652 // literal, when the type is omitted.
11654 // The values to put in the composite literal.
11655 Expression_list* vals_;
11656 // If this is true, then VALS_ is a list of pairs: a key and a
11657 // value. In an array initializer, a missing key will be NULL.
11664 Composite_literal_expression::do_traverse(Traverse* traverse)
11666 if (this->vals_ != NULL
11667 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11668 return TRAVERSE_EXIT;
11669 return Type::traverse(this->type_, traverse);
11672 // Lower a generic composite literal into a specific version based on
11676 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int)
11678 Type* type = this->type_;
11680 for (int depth = this->depth_; depth > 0; --depth)
11682 if (type->array_type() != NULL)
11683 type = type->array_type()->element_type();
11684 else if (type->map_type() != NULL)
11685 type = type->map_type()->val_type();
11688 if (!type->is_error())
11689 error_at(this->location(),
11690 ("may only omit types within composite literals "
11691 "of slice, array, or map type"));
11692 return Expression::make_error(this->location());
11696 if (type->is_error())
11697 return Expression::make_error(this->location());
11698 else if (type->struct_type() != NULL)
11699 return this->lower_struct(gogo, type);
11700 else if (type->array_type() != NULL)
11701 return this->lower_array(type);
11702 else if (type->map_type() != NULL)
11703 return this->lower_map(gogo, function, type);
11706 error_at(this->location(),
11707 ("expected struct, slice, array, or map type "
11708 "for composite literal"));
11709 return Expression::make_error(this->location());
11713 // Lower a struct composite literal.
11716 Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
11718 source_location location = this->location();
11719 Struct_type* st = type->struct_type();
11720 if (this->vals_ == NULL || !this->has_keys_)
11721 return new Struct_construction_expression(type, this->vals_, location);
11723 size_t field_count = st->field_count();
11724 std::vector<Expression*> vals(field_count);
11725 Expression_list::const_iterator p = this->vals_->begin();
11726 while (p != this->vals_->end())
11728 Expression* name_expr = *p;
11731 gcc_assert(p != this->vals_->end());
11732 Expression* val = *p;
11736 if (name_expr == NULL)
11738 error_at(val->location(), "mixture of field and value initializers");
11739 return Expression::make_error(location);
11742 bool bad_key = false;
11744 const Named_object* no = NULL;
11745 switch (name_expr->classification())
11747 case EXPRESSION_UNKNOWN_REFERENCE:
11748 name = name_expr->unknown_expression()->name();
11751 case EXPRESSION_CONST_REFERENCE:
11752 no = static_cast<Const_expression*>(name_expr)->named_object();
11755 case EXPRESSION_TYPE:
11757 Type* t = name_expr->type();
11758 Named_type* nt = t->named_type();
11762 no = nt->named_object();
11766 case EXPRESSION_VAR_REFERENCE:
11767 no = name_expr->var_expression()->named_object();
11770 case EXPRESSION_FUNC_REFERENCE:
11771 no = name_expr->func_expression()->named_object();
11774 case EXPRESSION_UNARY:
11775 // If there is a local variable around with the same name as
11776 // the field, and this occurs in the closure, then the
11777 // parser may turn the field reference into an indirection
11778 // through the closure. FIXME: This is a mess.
11781 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
11782 if (ue->op() == OPERATOR_MULT)
11784 Field_reference_expression* fre =
11785 ue->operand()->field_reference_expression();
11789 fre->expr()->type()->deref()->struct_type();
11792 const Struct_field* sf = st->field(fre->field_index());
11793 name = sf->field_name();
11795 snprintf(buf, sizeof buf, "%u", fre->field_index());
11796 size_t buflen = strlen(buf);
11797 if (name.compare(name.length() - buflen, buflen, buf)
11800 name = name.substr(0, name.length() - buflen);
11815 error_at(name_expr->location(), "expected struct field name");
11816 return Expression::make_error(location);
11823 // A predefined name won't be packed. If it starts with a
11824 // lower case letter we need to check for that case, because
11825 // the field name will be packed.
11826 if (!Gogo::is_hidden_name(name)
11830 Named_object* gno = gogo->lookup_global(name.c_str());
11832 name = gogo->pack_hidden_name(name, false);
11836 unsigned int index;
11837 const Struct_field* sf = st->find_local_field(name, &index);
11840 error_at(name_expr->location(), "unknown field %qs in %qs",
11841 Gogo::message_name(name).c_str(),
11842 (type->named_type() != NULL
11843 ? type->named_type()->message_name().c_str()
11844 : "unnamed struct"));
11845 return Expression::make_error(location);
11847 if (vals[index] != NULL)
11849 error_at(name_expr->location(),
11850 "duplicate value for field %qs in %qs",
11851 Gogo::message_name(name).c_str(),
11852 (type->named_type() != NULL
11853 ? type->named_type()->message_name().c_str()
11854 : "unnamed struct"));
11855 return Expression::make_error(location);
11861 Expression_list* list = new Expression_list;
11862 list->reserve(field_count);
11863 for (size_t i = 0; i < field_count; ++i)
11864 list->push_back(vals[i]);
11866 return new Struct_construction_expression(type, list, location);
11869 // Lower an array composite literal.
11872 Composite_literal_expression::lower_array(Type* type)
11874 source_location location = this->location();
11875 if (this->vals_ == NULL || !this->has_keys_)
11876 return this->make_array(type, this->vals_);
11878 std::vector<Expression*> vals;
11879 vals.reserve(this->vals_->size());
11880 unsigned long index = 0;
11881 Expression_list::const_iterator p = this->vals_->begin();
11882 while (p != this->vals_->end())
11884 Expression* index_expr = *p;
11887 gcc_assert(p != this->vals_->end());
11888 Expression* val = *p;
11892 if (index_expr != NULL)
11898 if (!index_expr->integer_constant_value(true, ival, &dummy))
11901 error_at(index_expr->location(),
11902 "index expression is not integer constant");
11903 return Expression::make_error(location);
11906 if (mpz_sgn(ival) < 0)
11909 error_at(index_expr->location(), "index expression is negative");
11910 return Expression::make_error(location);
11913 index = mpz_get_ui(ival);
11914 if (mpz_cmp_ui(ival, index) != 0)
11917 error_at(index_expr->location(), "index value overflow");
11918 return Expression::make_error(location);
11921 Named_type* ntype = Type::lookup_integer_type("int");
11922 Integer_type* inttype = ntype->integer_type();
11924 mpz_init_set_ui(max, 1);
11925 mpz_mul_2exp(max, max, inttype->bits() - 1);
11926 bool ok = mpz_cmp(ival, max) < 0;
11931 error_at(index_expr->location(), "index value overflow");
11932 return Expression::make_error(location);
11937 // FIXME: Our representation isn't very good; this avoids
11939 if (index > 0x1000000)
11941 error_at(index_expr->location(), "index too large for compiler");
11942 return Expression::make_error(location);
11946 if (index == vals.size())
11947 vals.push_back(val);
11950 if (index > vals.size())
11952 vals.reserve(index + 32);
11953 vals.resize(index + 1, static_cast<Expression*>(NULL));
11955 if (vals[index] != NULL)
11957 error_at((index_expr != NULL
11958 ? index_expr->location()
11959 : val->location()),
11960 "duplicate value for index %lu",
11962 return Expression::make_error(location);
11970 size_t size = vals.size();
11971 Expression_list* list = new Expression_list;
11972 list->reserve(size);
11973 for (size_t i = 0; i < size; ++i)
11974 list->push_back(vals[i]);
11976 return this->make_array(type, list);
11979 // Actually build the array composite literal. This handles
11983 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
11985 source_location location = this->location();
11986 Array_type* at = type->array_type();
11987 if (at->length() != NULL && at->length()->is_nil_expression())
11989 size_t size = vals == NULL ? 0 : vals->size();
11991 mpz_init_set_ui(vlen, size);
11992 Expression* elen = Expression::make_integer(&vlen, NULL, location);
11994 at = Type::make_array_type(at->element_type(), elen);
11997 if (at->length() != NULL)
11998 return new Fixed_array_construction_expression(type, vals, location);
12000 return new Open_array_construction_expression(type, vals, location);
12003 // Lower a map composite literal.
12006 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
12009 source_location location = this->location();
12010 if (this->vals_ != NULL)
12012 if (!this->has_keys_)
12014 error_at(location, "map composite literal must have keys");
12015 return Expression::make_error(location);
12018 for (Expression_list::iterator p = this->vals_->begin();
12019 p != this->vals_->end();
12025 error_at((*p)->location(),
12026 "map composite literal must have keys for every value");
12027 return Expression::make_error(location);
12029 // Make sure we have lowered the key; it may not have been
12030 // lowered in order to handle keys for struct composite
12031 // literals. Lower it now to get the right error message.
12032 if ((*p)->unknown_expression() != NULL)
12034 (*p)->unknown_expression()->clear_is_composite_literal_key();
12035 gogo->lower_expression(function, &*p);
12036 gcc_assert((*p)->is_error_expression());
12037 return Expression::make_error(location);
12042 return new Map_construction_expression(type, this->vals_, location);
12045 // Make a composite literal expression.
12048 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
12049 Expression_list* vals,
12050 source_location location)
12052 return new Composite_literal_expression(type, depth, has_keys, vals,
12056 // Return whether this expression is a composite literal.
12059 Expression::is_composite_literal() const
12061 switch (this->classification_)
12063 case EXPRESSION_COMPOSITE_LITERAL:
12064 case EXPRESSION_STRUCT_CONSTRUCTION:
12065 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12066 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12067 case EXPRESSION_MAP_CONSTRUCTION:
12074 // Return whether this expression is a composite literal which is not
12078 Expression::is_nonconstant_composite_literal() const
12080 switch (this->classification_)
12082 case EXPRESSION_STRUCT_CONSTRUCTION:
12084 const Struct_construction_expression *psce =
12085 static_cast<const Struct_construction_expression*>(this);
12086 return !psce->is_constant_struct();
12088 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12090 const Fixed_array_construction_expression *pace =
12091 static_cast<const Fixed_array_construction_expression*>(this);
12092 return !pace->is_constant_array();
12094 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12096 const Open_array_construction_expression *pace =
12097 static_cast<const Open_array_construction_expression*>(this);
12098 return !pace->is_constant_array();
12100 case EXPRESSION_MAP_CONSTRUCTION:
12107 // Return true if this is a reference to a local variable.
12110 Expression::is_local_variable() const
12112 const Var_expression* ve = this->var_expression();
12115 const Named_object* no = ve->named_object();
12116 return (no->is_result_variable()
12117 || (no->is_variable() && !no->var_value()->is_global()));
12120 // Class Type_guard_expression.
12125 Type_guard_expression::do_traverse(Traverse* traverse)
12127 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
12128 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12129 return TRAVERSE_EXIT;
12130 return TRAVERSE_CONTINUE;
12133 // Check types of a type guard expression. The expression must have
12134 // an interface type, but the actual type conversion is checked at run
12138 Type_guard_expression::do_check_types(Gogo*)
12140 // 6g permits using a type guard with unsafe.pointer; we are
12142 Type* expr_type = this->expr_->type();
12143 if (expr_type->is_unsafe_pointer_type())
12145 if (this->type_->points_to() == NULL
12146 && (this->type_->integer_type() == NULL
12147 || (this->type_->forwarded()
12148 != Type::lookup_integer_type("uintptr"))))
12149 this->report_error(_("invalid unsafe.Pointer conversion"));
12151 else if (this->type_->is_unsafe_pointer_type())
12153 if (expr_type->points_to() == NULL
12154 && (expr_type->integer_type() == NULL
12155 || (expr_type->forwarded()
12156 != Type::lookup_integer_type("uintptr"))))
12157 this->report_error(_("invalid unsafe.Pointer conversion"));
12159 else if (expr_type->interface_type() == NULL)
12161 if (!expr_type->is_error() && !this->type_->is_error())
12162 this->report_error(_("type assertion only valid for interface types"));
12163 this->set_is_error();
12165 else if (this->type_->interface_type() == NULL)
12167 std::string reason;
12168 if (!expr_type->interface_type()->implements_interface(this->type_,
12171 if (!this->type_->is_error())
12173 if (reason.empty())
12174 this->report_error(_("impossible type assertion: "
12175 "type does not implement interface"));
12177 error_at(this->location(),
12178 ("impossible type assertion: "
12179 "type does not implement interface (%s)"),
12182 this->set_is_error();
12187 // Return a tree for a type guard expression.
12190 Type_guard_expression::do_get_tree(Translate_context* context)
12192 Gogo* gogo = context->gogo();
12193 tree expr_tree = this->expr_->get_tree(context);
12194 if (expr_tree == error_mark_node)
12195 return error_mark_node;
12196 Type* expr_type = this->expr_->type();
12197 if ((this->type_->is_unsafe_pointer_type()
12198 && (expr_type->points_to() != NULL
12199 || expr_type->integer_type() != NULL))
12200 || (expr_type->is_unsafe_pointer_type()
12201 && this->type_->points_to() != NULL))
12202 return convert_to_pointer(this->type_->get_tree(gogo), expr_tree);
12203 else if (expr_type->is_unsafe_pointer_type()
12204 && this->type_->integer_type() != NULL)
12205 return convert_to_integer(this->type_->get_tree(gogo), expr_tree);
12206 else if (this->type_->interface_type() != NULL)
12207 return Expression::convert_interface_to_interface(context, this->type_,
12208 this->expr_->type(),
12212 return Expression::convert_for_assignment(context, this->type_,
12213 this->expr_->type(), expr_tree,
12217 // Make a type guard expression.
12220 Expression::make_type_guard(Expression* expr, Type* type,
12221 source_location location)
12223 return new Type_guard_expression(expr, type, location);
12226 // Class Heap_composite_expression.
12228 // When you take the address of a composite literal, it is allocated
12229 // on the heap. This class implements that.
12231 class Heap_composite_expression : public Expression
12234 Heap_composite_expression(Expression* expr, source_location location)
12235 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
12241 do_traverse(Traverse* traverse)
12242 { return Expression::traverse(&this->expr_, traverse); }
12246 { return Type::make_pointer_type(this->expr_->type()); }
12249 do_determine_type(const Type_context*)
12250 { this->expr_->determine_type_no_context(); }
12255 return Expression::make_heap_composite(this->expr_->copy(),
12260 do_get_tree(Translate_context*);
12262 // We only export global objects, and the parser does not generate
12263 // this in global scope.
12265 do_export(Export*) const
12266 { gcc_unreachable(); }
12269 // The composite literal which is being put on the heap.
12273 // Return a tree which allocates a composite literal on the heap.
12276 Heap_composite_expression::do_get_tree(Translate_context* context)
12278 tree expr_tree = this->expr_->get_tree(context);
12279 if (expr_tree == error_mark_node)
12280 return error_mark_node;
12281 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
12282 gcc_assert(TREE_CODE(expr_size) == INTEGER_CST);
12283 tree space = context->gogo()->allocate_memory(this->expr_->type(),
12284 expr_size, this->location());
12285 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
12286 space = save_expr(space);
12287 tree ref = build_fold_indirect_ref_loc(this->location(), space);
12288 TREE_THIS_NOTRAP(ref) = 1;
12289 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
12290 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
12292 SET_EXPR_LOCATION(ret, this->location());
12296 // Allocate a composite literal on the heap.
12299 Expression::make_heap_composite(Expression* expr, source_location location)
12301 return new Heap_composite_expression(expr, location);
12304 // Class Receive_expression.
12306 // Return the type of a receive expression.
12309 Receive_expression::do_type()
12311 Channel_type* channel_type = this->channel_->type()->channel_type();
12312 if (channel_type == NULL)
12313 return Type::make_error_type();
12314 return channel_type->element_type();
12317 // Check types for a receive expression.
12320 Receive_expression::do_check_types(Gogo*)
12322 Type* type = this->channel_->type();
12323 if (type->is_error())
12325 this->set_is_error();
12328 if (type->channel_type() == NULL)
12330 this->report_error(_("expected channel"));
12333 if (!type->channel_type()->may_receive())
12335 this->report_error(_("invalid receive on send-only channel"));
12340 // Get a tree for a receive expression.
12343 Receive_expression::do_get_tree(Translate_context* context)
12345 Channel_type* channel_type = this->channel_->type()->channel_type();
12346 if (channel_type == NULL)
12348 gcc_assert(this->channel_->type()->is_error());
12349 return error_mark_node;
12351 Type* element_type = channel_type->element_type();
12352 tree element_type_tree = element_type->get_tree(context->gogo());
12354 tree channel = this->channel_->get_tree(context);
12355 if (element_type_tree == error_mark_node || channel == error_mark_node)
12356 return error_mark_node;
12358 return Gogo::receive_from_channel(element_type_tree, channel,
12359 this->for_select_, this->location());
12362 // Make a receive expression.
12364 Receive_expression*
12365 Expression::make_receive(Expression* channel, source_location location)
12367 return new Receive_expression(channel, location);
12370 // An expression which evaluates to a pointer to the type descriptor
12373 class Type_descriptor_expression : public Expression
12376 Type_descriptor_expression(Type* type, source_location location)
12377 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
12384 { return Type::make_type_descriptor_ptr_type(); }
12387 do_determine_type(const Type_context*)
12395 do_get_tree(Translate_context* context)
12396 { return this->type_->type_descriptor_pointer(context->gogo()); }
12399 // The type for which this is the descriptor.
12403 // Make a type descriptor expression.
12406 Expression::make_type_descriptor(Type* type, source_location location)
12408 return new Type_descriptor_expression(type, location);
12411 // An expression which evaluates to some characteristic of a type.
12412 // This is only used to initialize fields of a type descriptor. Using
12413 // a new expression class is slightly inefficient but gives us a good
12414 // separation between the frontend and the middle-end with regard to
12415 // how types are laid out.
12417 class Type_info_expression : public Expression
12420 Type_info_expression(Type* type, Type_info type_info)
12421 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
12422 type_(type), type_info_(type_info)
12430 do_determine_type(const Type_context*)
12438 do_get_tree(Translate_context* context);
12441 // The type for which we are getting information.
12443 // What information we want.
12444 Type_info type_info_;
12447 // The type is chosen to match what the type descriptor struct
12451 Type_info_expression::do_type()
12453 switch (this->type_info_)
12455 case TYPE_INFO_SIZE:
12456 return Type::lookup_integer_type("uintptr");
12457 case TYPE_INFO_ALIGNMENT:
12458 case TYPE_INFO_FIELD_ALIGNMENT:
12459 return Type::lookup_integer_type("uint8");
12465 // Return type information in GENERIC.
12468 Type_info_expression::do_get_tree(Translate_context* context)
12470 tree type_tree = this->type_->get_tree(context->gogo());
12471 if (type_tree == error_mark_node)
12472 return error_mark_node;
12474 tree val_type_tree = this->type()->get_tree(context->gogo());
12475 gcc_assert(val_type_tree != error_mark_node);
12477 if (this->type_info_ == TYPE_INFO_SIZE)
12478 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12479 TYPE_SIZE_UNIT(type_tree));
12483 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
12484 val = go_type_alignment(type_tree);
12486 val = go_field_alignment(type_tree);
12487 return build_int_cstu(val_type_tree, val);
12491 // Make a type info expression.
12494 Expression::make_type_info(Type* type, Type_info type_info)
12496 return new Type_info_expression(type, type_info);
12499 // An expression which evaluates to the offset of a field within a
12500 // struct. This, like Type_info_expression, q.v., is only used to
12501 // initialize fields of a type descriptor.
12503 class Struct_field_offset_expression : public Expression
12506 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
12507 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
12508 type_(type), field_(field)
12514 { return Type::lookup_integer_type("uintptr"); }
12517 do_determine_type(const Type_context*)
12525 do_get_tree(Translate_context* context);
12528 // The type of the struct.
12529 Struct_type* type_;
12531 const Struct_field* field_;
12534 // Return a struct field offset in GENERIC.
12537 Struct_field_offset_expression::do_get_tree(Translate_context* context)
12539 tree type_tree = this->type_->get_tree(context->gogo());
12540 if (type_tree == error_mark_node)
12541 return error_mark_node;
12543 tree val_type_tree = this->type()->get_tree(context->gogo());
12544 gcc_assert(val_type_tree != error_mark_node);
12546 const Struct_field_list* fields = this->type_->fields();
12547 tree struct_field_tree = TYPE_FIELDS(type_tree);
12548 Struct_field_list::const_iterator p;
12549 for (p = fields->begin();
12550 p != fields->end();
12551 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
12553 gcc_assert(struct_field_tree != NULL_TREE);
12554 if (&*p == this->field_)
12557 gcc_assert(&*p == this->field_);
12559 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12560 byte_position(struct_field_tree));
12563 // Make an expression for a struct field offset.
12566 Expression::make_struct_field_offset(Struct_type* type,
12567 const Struct_field* field)
12569 return new Struct_field_offset_expression(type, field);
12572 // An expression which evaluates to the address of an unnamed label.
12574 class Label_addr_expression : public Expression
12577 Label_addr_expression(Label* label, source_location location)
12578 : Expression(EXPRESSION_LABEL_ADDR, location),
12585 { return Type::make_pointer_type(Type::make_void_type()); }
12588 do_determine_type(const Type_context*)
12593 { return new Label_addr_expression(this->label_, this->location()); }
12596 do_get_tree(Translate_context*)
12597 { return this->label_->get_addr(this->location()); }
12600 // The label whose address we are taking.
12604 // Make an expression for the address of an unnamed label.
12607 Expression::make_label_addr(Label* label, source_location location)
12609 return new Label_addr_expression(label, location);
12612 // Import an expression. This comes at the end in order to see the
12613 // various class definitions.
12616 Expression::import_expression(Import* imp)
12618 int c = imp->peek_char();
12619 if (imp->match_c_string("- ")
12620 || imp->match_c_string("! ")
12621 || imp->match_c_string("^ "))
12622 return Unary_expression::do_import(imp);
12624 return Binary_expression::do_import(imp);
12625 else if (imp->match_c_string("true")
12626 || imp->match_c_string("false"))
12627 return Boolean_expression::do_import(imp);
12629 return String_expression::do_import(imp);
12630 else if (c == '-' || (c >= '0' && c <= '9'))
12632 // This handles integers, floats and complex constants.
12633 return Integer_expression::do_import(imp);
12635 else if (imp->match_c_string("nil"))
12636 return Nil_expression::do_import(imp);
12637 else if (imp->match_c_string("convert"))
12638 return Type_conversion_expression::do_import(imp);
12641 error_at(imp->location(), "import error: expected expression");
12642 return Expression::make_error(imp->location());
12646 // Class Expression_list.
12648 // Traverse the list.
12651 Expression_list::traverse(Traverse* traverse)
12653 for (Expression_list::iterator p = this->begin();
12659 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12660 return TRAVERSE_EXIT;
12663 return TRAVERSE_CONTINUE;
12669 Expression_list::copy()
12671 Expression_list* ret = new Expression_list();
12672 for (Expression_list::iterator p = this->begin();
12677 ret->push_back(NULL);
12679 ret->push_back((*p)->copy());
12684 // Return whether an expression list has an error expression.
12687 Expression_list::contains_error() const
12689 for (Expression_list::const_iterator p = this->begin();
12692 if (*p != NULL && (*p)->is_error_expression())