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"
37 #include "expressions.h"
41 Expression::Expression(Expression_classification classification,
42 source_location location)
43 : classification_(classification), location_(location)
47 Expression::~Expression()
51 // If this expression has a constant integer value, return it.
54 Expression::integer_constant_value(bool iota_is_constant, mpz_t val,
58 return this->do_integer_constant_value(iota_is_constant, val, ptype);
61 // If this expression has a constant floating point value, return it.
64 Expression::float_constant_value(mpfr_t val, Type** ptype) const
67 if (this->do_float_constant_value(val, ptype))
73 if (!this->do_integer_constant_value(false, ival, &t))
77 mpfr_set_z(val, ival, GMP_RNDN);
84 // If this expression has a constant complex value, return it.
87 Expression::complex_constant_value(mpfr_t real, mpfr_t imag,
91 if (this->do_complex_constant_value(real, imag, ptype))
94 if (this->float_constant_value(real, &t))
96 mpfr_set_ui(imag, 0, GMP_RNDN);
102 // Traverse the expressions.
105 Expression::traverse(Expression** pexpr, Traverse* traverse)
107 Expression* expr = *pexpr;
108 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
110 int t = traverse->expression(pexpr);
111 if (t == TRAVERSE_EXIT)
112 return TRAVERSE_EXIT;
113 else if (t == TRAVERSE_SKIP_COMPONENTS)
114 return TRAVERSE_CONTINUE;
116 return expr->do_traverse(traverse);
119 // Traverse subexpressions of this expression.
122 Expression::traverse_subexpressions(Traverse* traverse)
124 return this->do_traverse(traverse);
127 // Default implementation for do_traverse for child classes.
130 Expression::do_traverse(Traverse*)
132 return TRAVERSE_CONTINUE;
135 // This virtual function is called by the parser if the value of this
136 // expression is being discarded. By default, we warn. Expressions
137 // with side effects override.
140 Expression::do_discarding_value()
142 this->warn_about_unused_value();
145 // This virtual function is called to export expressions. This will
146 // only be used by expressions which may be constant.
149 Expression::do_export(Export*) const
154 // Warn that the value of the expression is not used.
157 Expression::warn_about_unused_value()
159 warning_at(this->location(), OPT_Wunused_value, "value computed is not used");
162 // Note that this expression is an error. This is called by children
163 // when they discover an error.
166 Expression::set_is_error()
168 this->classification_ = EXPRESSION_ERROR;
171 // For children to call to report an error conveniently.
174 Expression::report_error(const char* msg)
176 error_at(this->location_, "%s", msg);
177 this->set_is_error();
180 // Set types of variables and constants. This is implemented by the
184 Expression::determine_type(const Type_context* context)
186 this->do_determine_type(context);
189 // Set types when there is no context.
192 Expression::determine_type_no_context()
194 Type_context context;
195 this->do_determine_type(&context);
198 // Return a tree handling any conversions which must be done during
202 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
203 Type* rhs_type, tree rhs_tree,
204 source_location location)
206 if (lhs_type == rhs_type)
209 if (lhs_type->is_error() || rhs_type->is_error())
210 return error_mark_node;
212 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
213 return error_mark_node;
215 Gogo* gogo = context->gogo();
217 tree lhs_type_tree = lhs_type->get_tree(gogo);
218 if (lhs_type_tree == error_mark_node)
219 return error_mark_node;
221 if (lhs_type->interface_type() != NULL)
223 if (rhs_type->interface_type() == NULL)
224 return Expression::convert_type_to_interface(context, lhs_type,
228 return Expression::convert_interface_to_interface(context, lhs_type,
232 else if (rhs_type->interface_type() != NULL)
233 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
235 else if (lhs_type->is_open_array_type()
236 && rhs_type->is_nil_type())
238 // Assigning nil to an open array.
239 gcc_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
241 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
243 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
244 tree field = TYPE_FIELDS(lhs_type_tree);
245 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
248 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
250 elt = VEC_quick_push(constructor_elt, init, NULL);
251 field = DECL_CHAIN(field);
252 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
255 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
257 elt = VEC_quick_push(constructor_elt, init, NULL);
258 field = DECL_CHAIN(field);
259 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
262 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
264 tree val = build_constructor(lhs_type_tree, init);
265 TREE_CONSTANT(val) = 1;
269 else if (rhs_type->is_nil_type())
271 // The left hand side should be a pointer type at the tree
273 gcc_assert(POINTER_TYPE_P(lhs_type_tree));
274 return fold_convert(lhs_type_tree, null_pointer_node);
276 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
278 // No conversion is needed.
281 else if (POINTER_TYPE_P(lhs_type_tree)
282 || INTEGRAL_TYPE_P(lhs_type_tree)
283 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
284 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
285 return fold_convert_loc(location, lhs_type_tree, rhs_tree);
286 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
287 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
289 // This conversion must be permitted by Go, or we wouldn't have
291 gcc_assert(int_size_in_bytes(lhs_type_tree)
292 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
293 return fold_build1_loc(location, VIEW_CONVERT_EXPR, lhs_type_tree,
298 gcc_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
303 // Return a tree for a conversion from a non-interface type to an
307 Expression::convert_type_to_interface(Translate_context* context,
308 Type* lhs_type, Type* rhs_type,
309 tree rhs_tree, source_location location)
311 Gogo* gogo = context->gogo();
312 Interface_type* lhs_interface_type = lhs_type->interface_type();
313 bool lhs_is_empty = lhs_interface_type->is_empty();
315 // Since RHS_TYPE is a static type, we can create the interface
316 // method table at compile time.
318 // When setting an interface to nil, we just set both fields to
320 if (rhs_type->is_nil_type())
321 return lhs_type->get_init_tree(gogo, false);
323 // This should have been checked already.
324 gcc_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
326 tree lhs_type_tree = lhs_type->get_tree(gogo);
327 if (lhs_type_tree == error_mark_node)
328 return error_mark_node;
330 // An interface is a tuple. If LHS_TYPE is an empty interface type,
331 // then the first field is the type descriptor for RHS_TYPE.
332 // Otherwise it is the interface method table for RHS_TYPE.
333 tree first_field_value;
335 first_field_value = rhs_type->type_descriptor_pointer(gogo);
338 // Build the interface method table for this interface and this
339 // object type: a list of function pointers for each interface
341 Named_type* rhs_named_type = rhs_type->named_type();
342 bool is_pointer = false;
343 if (rhs_named_type == NULL)
345 rhs_named_type = rhs_type->deref()->named_type();
349 if (rhs_named_type == NULL)
350 method_table = null_pointer_node;
353 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
355 first_field_value = fold_convert_loc(location, const_ptr_type_node,
358 if (first_field_value == error_mark_node)
359 return error_mark_node;
361 // Start building a constructor for the value we will return.
363 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
365 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
366 tree field = TYPE_FIELDS(lhs_type_tree);
367 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
368 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
370 elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value);
372 elt = VEC_quick_push(constructor_elt, init, NULL);
373 field = DECL_CHAIN(field);
374 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
377 if (rhs_type->points_to() != NULL)
379 // We are assigning a pointer to the interface; the interface
380 // holds the pointer itself.
381 elt->value = rhs_tree;
382 return build_constructor(lhs_type_tree, init);
385 // We are assigning a non-pointer value to the interface; the
386 // interface gets a copy of the value in the heap.
388 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
390 tree space = gogo->allocate_memory(rhs_type, object_size, location);
391 space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)),
393 space = save_expr(space);
395 tree ref = build_fold_indirect_ref_loc(location, space);
396 TREE_THIS_NOTRAP(ref) = 1;
397 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
400 elt->value = fold_convert_loc(location, TREE_TYPE(field), space);
402 return build2(COMPOUND_EXPR, lhs_type_tree, set,
403 build_constructor(lhs_type_tree, init));
406 // Return a tree for the type descriptor of RHS_TREE, which has
407 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
411 Expression::get_interface_type_descriptor(Translate_context*,
412 Type* rhs_type, tree rhs_tree,
413 source_location location)
415 tree rhs_type_tree = TREE_TYPE(rhs_tree);
416 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
417 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
418 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
420 if (rhs_type->interface_type()->is_empty())
422 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
423 "__type_descriptor") == 0);
427 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
429 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v)));
431 tree v1 = build_fold_indirect_ref_loc(location, v);
432 gcc_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
433 tree f = TYPE_FIELDS(TREE_TYPE(v1));
434 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
436 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
438 tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v,
439 fold_convert_loc(location, TREE_TYPE(v),
441 tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node);
442 return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1),
446 // Return a tree for the conversion of an interface type to an
450 Expression::convert_interface_to_interface(Translate_context* context,
451 Type *lhs_type, Type *rhs_type,
452 tree rhs_tree, bool for_type_guard,
453 source_location location)
455 Gogo* gogo = context->gogo();
456 Interface_type* lhs_interface_type = lhs_type->interface_type();
457 bool lhs_is_empty = lhs_interface_type->is_empty();
459 tree lhs_type_tree = lhs_type->get_tree(gogo);
460 if (lhs_type_tree == error_mark_node)
461 return error_mark_node;
463 // In the general case this requires runtime examination of the type
464 // method table to match it up with the interface methods.
466 // FIXME: If all of the methods in the right hand side interface
467 // also appear in the left hand side interface, then we don't need
468 // to do a runtime check, although we still need to build a new
471 // Get the type descriptor for the right hand side. This will be
472 // NULL for a nil interface.
474 if (!DECL_P(rhs_tree))
475 rhs_tree = save_expr(rhs_tree);
477 tree rhs_type_descriptor =
478 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
481 // The result is going to be a two element constructor.
483 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
485 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
486 tree field = TYPE_FIELDS(lhs_type_tree);
491 // A type assertion fails when converting a nil interface.
492 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
493 static tree assert_interface_decl;
494 tree call = Gogo::call_builtin(&assert_interface_decl,
496 "__go_assert_interface",
499 TREE_TYPE(lhs_type_descriptor),
501 TREE_TYPE(rhs_type_descriptor),
502 rhs_type_descriptor);
503 if (call == error_mark_node)
504 return error_mark_node;
505 // This will panic if the interface conversion fails.
506 TREE_NOTHROW(assert_interface_decl) = 0;
507 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
509 else if (lhs_is_empty)
511 // A convertion to an empty interface always succeeds, and the
512 // first field is just the type descriptor of the object.
513 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
514 "__type_descriptor") == 0);
515 gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
516 elt->value = rhs_type_descriptor;
520 // A conversion to a non-empty interface may fail, but unlike a
521 // type assertion converting nil will always succeed.
522 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
524 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
525 static tree convert_interface_decl;
526 tree call = Gogo::call_builtin(&convert_interface_decl,
528 "__go_convert_interface",
531 TREE_TYPE(lhs_type_descriptor),
533 TREE_TYPE(rhs_type_descriptor),
534 rhs_type_descriptor);
535 if (call == error_mark_node)
536 return error_mark_node;
537 // This will panic if the interface conversion fails.
538 TREE_NOTHROW(convert_interface_decl) = 0;
539 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
542 // The second field is simply the object pointer.
544 elt = VEC_quick_push(constructor_elt, init, NULL);
545 field = DECL_CHAIN(field);
546 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
549 tree rhs_type_tree = TREE_TYPE(rhs_tree);
550 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
551 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
552 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
553 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
556 return build_constructor(lhs_type_tree, init);
559 // Return a tree for the conversion of an interface type to a
560 // non-interface type.
563 Expression::convert_interface_to_type(Translate_context* context,
564 Type *lhs_type, Type* rhs_type,
565 tree rhs_tree, source_location location)
567 Gogo* gogo = context->gogo();
568 tree rhs_type_tree = TREE_TYPE(rhs_tree);
570 tree lhs_type_tree = lhs_type->get_tree(gogo);
571 if (lhs_type_tree == error_mark_node)
572 return error_mark_node;
574 // Call a function to check that the type is valid. The function
575 // will panic with an appropriate runtime type error if the type is
578 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
580 if (!DECL_P(rhs_tree))
581 rhs_tree = save_expr(rhs_tree);
583 tree rhs_type_descriptor =
584 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
587 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo);
589 static tree check_interface_type_decl;
590 tree call = Gogo::call_builtin(&check_interface_type_decl,
592 "__go_check_interface_type",
595 TREE_TYPE(lhs_type_descriptor),
597 TREE_TYPE(rhs_type_descriptor),
599 TREE_TYPE(rhs_inter_descriptor),
600 rhs_inter_descriptor);
601 if (call == error_mark_node)
602 return error_mark_node;
603 // This call will panic if the conversion is invalid.
604 TREE_NOTHROW(check_interface_type_decl) = 0;
606 // If the call succeeds, pull out the value.
607 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
608 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
609 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
610 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
613 // If the value is a pointer, then it is the value we want.
614 // Otherwise it points to the value.
615 if (lhs_type->points_to() == NULL)
617 val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val);
618 val = build_fold_indirect_ref_loc(location, val);
621 return build2(COMPOUND_EXPR, lhs_type_tree, call,
622 fold_convert_loc(location, lhs_type_tree, val));
625 // Convert an expression to a tree. This is implemented by the child
626 // class. Not that it is not in general safe to call this multiple
627 // times for a single expression, but that we don't catch such errors.
630 Expression::get_tree(Translate_context* context)
632 // The child may have marked this expression as having an error.
633 if (this->classification_ == EXPRESSION_ERROR)
634 return error_mark_node;
636 return this->do_get_tree(context);
639 // Return a tree for VAL in TYPE.
642 Expression::integer_constant_tree(mpz_t val, tree type)
644 if (type == error_mark_node)
645 return error_mark_node;
646 else if (TREE_CODE(type) == INTEGER_TYPE)
647 return double_int_to_tree(type,
648 mpz_get_double_int(type, val, true));
649 else if (TREE_CODE(type) == REAL_TYPE)
652 mpfr_init_set_z(fval, val, GMP_RNDN);
653 tree ret = Expression::float_constant_tree(fval, type);
657 else if (TREE_CODE(type) == COMPLEX_TYPE)
660 mpfr_init_set_z(fval, val, GMP_RNDN);
661 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
663 tree imag = build_real_from_int_cst(TREE_TYPE(type),
665 return build_complex(type, real, imag);
671 // Return a tree for VAL in TYPE.
674 Expression::float_constant_tree(mpfr_t val, tree type)
676 if (type == error_mark_node)
677 return error_mark_node;
678 else if (TREE_CODE(type) == INTEGER_TYPE)
682 mpfr_get_z(ival, val, GMP_RNDN);
683 tree ret = Expression::integer_constant_tree(ival, type);
687 else if (TREE_CODE(type) == REAL_TYPE)
690 real_from_mpfr(&r1, val, type, GMP_RNDN);
692 real_convert(&r2, TYPE_MODE(type), &r1);
693 return build_real(type, r2);
695 else if (TREE_CODE(type) == COMPLEX_TYPE)
698 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
700 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
701 tree imag = build_real_from_int_cst(TREE_TYPE(type),
703 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
709 // Return a tree for REAL/IMAG in TYPE.
712 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
714 if (type == error_mark_node)
715 return error_mark_node;
716 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
717 return Expression::float_constant_tree(real, type);
718 else if (TREE_CODE(type) == COMPLEX_TYPE)
721 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
723 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
726 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
728 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
730 return build_complex(type, build_real(TREE_TYPE(type), r2),
731 build_real(TREE_TYPE(type), r4));
737 // Return a tree which evaluates to true if VAL, of arbitrary integer
738 // type, is negative or is more than the maximum value of BOUND_TYPE.
739 // If SOFAR is not NULL, it is or'red into the result. The return
740 // value may be NULL if SOFAR is NULL.
743 Expression::check_bounds(tree val, tree bound_type, tree sofar,
746 tree val_type = TREE_TYPE(val);
747 tree ret = NULL_TREE;
749 if (!TYPE_UNSIGNED(val_type))
751 ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val,
752 build_int_cst(val_type, 0));
753 if (ret == boolean_false_node)
757 if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type))
758 || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type))
760 tree max = TYPE_MAX_VALUE(bound_type);
761 tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val,
762 fold_convert_loc(loc, val_type, max));
763 if (big == boolean_false_node)
765 else if (ret == NULL_TREE)
768 ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
772 if (ret == NULL_TREE)
774 else if (sofar == NULL_TREE)
777 return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
781 // Error expressions. This are used to avoid cascading errors.
783 class Error_expression : public Expression
786 Error_expression(source_location location)
787 : Expression(EXPRESSION_ERROR, location)
792 do_is_constant() const
796 do_integer_constant_value(bool, mpz_t val, Type**) const
803 do_float_constant_value(mpfr_t val, Type**) const
805 mpfr_set_ui(val, 0, GMP_RNDN);
810 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
812 mpfr_set_ui(real, 0, GMP_RNDN);
813 mpfr_set_ui(imag, 0, GMP_RNDN);
818 do_discarding_value()
823 { return Type::make_error_type(); }
826 do_determine_type(const Type_context*)
834 do_is_addressable() const
838 do_get_tree(Translate_context*)
839 { return error_mark_node; }
843 Expression::make_error(source_location location)
845 return new Error_expression(location);
848 // An expression which is really a type. This is used during parsing.
849 // It is an error if these survive after lowering.
852 Type_expression : public Expression
855 Type_expression(Type* type, source_location location)
856 : Expression(EXPRESSION_TYPE, location),
862 do_traverse(Traverse* traverse)
863 { return Type::traverse(this->type_, traverse); }
867 { return this->type_; }
870 do_determine_type(const Type_context*)
874 do_check_types(Gogo*)
875 { this->report_error(_("invalid use of type")); }
882 do_get_tree(Translate_context*)
883 { gcc_unreachable(); }
886 // The type which we are representing as an expression.
891 Expression::make_type(Type* type, source_location location)
893 return new Type_expression(type, location);
896 // Class Parser_expression.
899 Parser_expression::do_type()
901 // We should never really ask for the type of a Parser_expression.
902 // However, it can happen, at least when we have an invalid const
903 // whose initializer refers to the const itself. In that case we
904 // may ask for the type when lowering the const itself.
905 gcc_assert(saw_errors());
906 return Type::make_error_type();
909 // Class Var_expression.
911 // Lower a variable expression. Here we just make sure that the
912 // initialization expression of the variable has been lowered. This
913 // ensures that we will be able to determine the type of the variable
917 Var_expression::do_lower(Gogo* gogo, Named_object* function, int)
919 if (this->variable_->is_variable())
921 Variable* var = this->variable_->var_value();
922 // This is either a local variable or a global variable. A
923 // reference to a variable which is local to an enclosing
924 // function will be a reference to a field in a closure.
925 if (var->is_global())
927 var->lower_init_expression(gogo, function);
932 // Return the type of a reference to a variable.
935 Var_expression::do_type()
937 if (this->variable_->is_variable())
938 return this->variable_->var_value()->type();
939 else if (this->variable_->is_result_variable())
940 return this->variable_->result_var_value()->type();
945 // Determine the type of a reference to a variable.
948 Var_expression::do_determine_type(const Type_context*)
950 if (this->variable_->is_variable())
951 this->variable_->var_value()->determine_type();
954 // Something takes the address of this variable. This means that we
955 // may want to move the variable onto the heap.
958 Var_expression::do_address_taken(bool escapes)
962 else if (this->variable_->is_variable())
963 this->variable_->var_value()->set_address_taken();
964 else if (this->variable_->is_result_variable())
965 this->variable_->result_var_value()->set_address_taken();
970 // Get the tree for a reference to a variable.
973 Var_expression::do_get_tree(Translate_context* context)
975 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
976 context->function());
977 tree ret = var_to_tree(bvar);
978 if (ret == error_mark_node)
979 return error_mark_node;
981 if (this->variable_->is_variable())
982 is_in_heap = this->variable_->var_value()->is_in_heap();
983 else if (this->variable_->is_result_variable())
984 is_in_heap = this->variable_->result_var_value()->is_in_heap();
989 ret = build_fold_indirect_ref_loc(this->location(), ret);
990 TREE_THIS_NOTRAP(ret) = 1;
995 // Make a reference to a variable in an expression.
998 Expression::make_var_reference(Named_object* var, source_location location)
1001 return Expression::make_sink(location);
1003 // FIXME: Creating a new object for each reference to a variable is
1005 return new Var_expression(var, location);
1008 // Class Temporary_reference_expression.
1013 Temporary_reference_expression::do_type()
1015 return this->statement_->type();
1018 // Called if something takes the address of this temporary variable.
1019 // We never have to move temporary variables to the heap, but we do
1020 // need to know that they must live in the stack rather than in a
1024 Temporary_reference_expression::do_address_taken(bool)
1026 this->statement_->set_is_address_taken();
1029 // Get a tree referring to the variable.
1032 Temporary_reference_expression::do_get_tree(Translate_context*)
1034 return this->statement_->get_decl();
1037 // Make a reference to a temporary variable.
1040 Expression::make_temporary_reference(Temporary_statement* statement,
1041 source_location location)
1043 return new Temporary_reference_expression(statement, location);
1046 // A sink expression--a use of the blank identifier _.
1048 class Sink_expression : public Expression
1051 Sink_expression(source_location location)
1052 : Expression(EXPRESSION_SINK, location),
1053 type_(NULL), var_(NULL_TREE)
1058 do_discarding_value()
1065 do_determine_type(const Type_context*);
1069 { return new Sink_expression(this->location()); }
1072 do_get_tree(Translate_context*);
1075 // The type of this sink variable.
1077 // The temporary variable we generate.
1081 // Return the type of a sink expression.
1084 Sink_expression::do_type()
1086 if (this->type_ == NULL)
1087 return Type::make_sink_type();
1091 // Determine the type of a sink expression.
1094 Sink_expression::do_determine_type(const Type_context* context)
1096 if (context->type != NULL)
1097 this->type_ = context->type;
1100 // Return a temporary variable for a sink expression. This will
1101 // presumably be a write-only variable which the middle-end will drop.
1104 Sink_expression::do_get_tree(Translate_context* context)
1106 if (this->var_ == NULL_TREE)
1108 gcc_assert(this->type_ != NULL && !this->type_->is_sink_type());
1109 this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()),
1115 // Make a sink expression.
1118 Expression::make_sink(source_location location)
1120 return new Sink_expression(location);
1123 // Class Func_expression.
1125 // FIXME: Can a function expression appear in a constant expression?
1126 // The value is unchanging. Initializing a constant to the address of
1127 // a function seems like it could work, though there might be little
1133 Func_expression::do_traverse(Traverse* traverse)
1135 return (this->closure_ == NULL
1137 : Expression::traverse(&this->closure_, traverse));
1140 // Return the type of a function expression.
1143 Func_expression::do_type()
1145 if (this->function_->is_function())
1146 return this->function_->func_value()->type();
1147 else if (this->function_->is_function_declaration())
1148 return this->function_->func_declaration_value()->type();
1153 // Get the tree for a function expression without evaluating the
1157 Func_expression::get_tree_without_closure(Gogo* gogo)
1159 Function_type* fntype;
1160 if (this->function_->is_function())
1161 fntype = this->function_->func_value()->type();
1162 else if (this->function_->is_function_declaration())
1163 fntype = this->function_->func_declaration_value()->type();
1167 // Builtin functions are handled specially by Call_expression. We
1168 // can't take their address.
1169 if (fntype->is_builtin())
1171 error_at(this->location(), "invalid use of special builtin function %qs",
1172 this->function_->name().c_str());
1173 return error_mark_node;
1176 Named_object* no = this->function_;
1178 tree id = no->get_id(gogo);
1179 if (id == error_mark_node)
1180 return error_mark_node;
1183 if (no->is_function())
1184 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1185 else if (no->is_function_declaration())
1186 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1190 if (fndecl == error_mark_node)
1191 return error_mark_node;
1193 return build_fold_addr_expr_loc(this->location(), fndecl);
1196 // Get the tree for a function expression. This is used when we take
1197 // the address of a function rather than simply calling it. If the
1198 // function has a closure, we must use a trampoline.
1201 Func_expression::do_get_tree(Translate_context* context)
1203 Gogo* gogo = context->gogo();
1205 tree fnaddr = this->get_tree_without_closure(gogo);
1206 if (fnaddr == error_mark_node)
1207 return error_mark_node;
1209 gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1210 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1211 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1213 // For a normal non-nested function call, that is all we have to do.
1214 if (!this->function_->is_function()
1215 || this->function_->func_value()->enclosing() == NULL)
1217 gcc_assert(this->closure_ == NULL);
1221 // For a nested function call, we have to always allocate a
1222 // trampoline. If we don't always allocate, then closures will not
1223 // be reliably distinct.
1224 Expression* closure = this->closure_;
1226 if (closure == NULL)
1227 closure_tree = null_pointer_node;
1230 // Get the value of the closure. This will be a pointer to
1231 // space allocated on the heap.
1232 closure_tree = closure->get_tree(context);
1233 if (closure_tree == error_mark_node)
1234 return error_mark_node;
1235 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1238 // Now we need to build some code on the heap. This code will load
1239 // the static chain pointer with the closure and then jump to the
1240 // body of the function. The normal gcc approach is to build the
1241 // code on the stack. Unfortunately we can not do that, as Go
1242 // permits us to return the function pointer.
1244 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1247 // Make a reference to a function in an expression.
1250 Expression::make_func_reference(Named_object* function, Expression* closure,
1251 source_location location)
1253 return new Func_expression(function, closure, location);
1256 // Class Unknown_expression.
1258 // Return the name of an unknown expression.
1261 Unknown_expression::name() const
1263 return this->named_object_->name();
1266 // Lower a reference to an unknown name.
1269 Unknown_expression::do_lower(Gogo*, Named_object*, int)
1271 source_location location = this->location();
1272 Named_object* no = this->named_object_;
1274 if (!no->is_unknown())
1278 real = no->unknown_value()->real_named_object();
1281 if (this->is_composite_literal_key_)
1283 error_at(location, "reference to undefined name %qs",
1284 this->named_object_->message_name().c_str());
1285 return Expression::make_error(location);
1288 switch (real->classification())
1290 case Named_object::NAMED_OBJECT_CONST:
1291 return Expression::make_const_reference(real, location);
1292 case Named_object::NAMED_OBJECT_TYPE:
1293 return Expression::make_type(real->type_value(), location);
1294 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1295 if (this->is_composite_literal_key_)
1297 error_at(location, "reference to undefined type %qs",
1298 real->message_name().c_str());
1299 return Expression::make_error(location);
1300 case Named_object::NAMED_OBJECT_VAR:
1301 return Expression::make_var_reference(real, location);
1302 case Named_object::NAMED_OBJECT_FUNC:
1303 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1304 return Expression::make_func_reference(real, NULL, location);
1305 case Named_object::NAMED_OBJECT_PACKAGE:
1306 if (this->is_composite_literal_key_)
1308 error_at(location, "unexpected reference to package");
1309 return Expression::make_error(location);
1315 // Make a reference to an unknown name.
1318 Expression::make_unknown_reference(Named_object* no, source_location location)
1320 gcc_assert(no->resolve()->is_unknown());
1321 return new Unknown_expression(no, location);
1324 // A boolean expression.
1326 class Boolean_expression : public Expression
1329 Boolean_expression(bool val, source_location location)
1330 : Expression(EXPRESSION_BOOLEAN, location),
1331 val_(val), type_(NULL)
1339 do_is_constant() const
1346 do_determine_type(const Type_context*);
1353 do_get_tree(Translate_context*)
1354 { return this->val_ ? boolean_true_node : boolean_false_node; }
1357 do_export(Export* exp) const
1358 { exp->write_c_string(this->val_ ? "true" : "false"); }
1363 // The type as determined by context.
1370 Boolean_expression::do_type()
1372 if (this->type_ == NULL)
1373 this->type_ = Type::make_boolean_type();
1377 // Set the type from the context.
1380 Boolean_expression::do_determine_type(const Type_context* context)
1382 if (this->type_ != NULL && !this->type_->is_abstract())
1384 else if (context->type != NULL && context->type->is_boolean_type())
1385 this->type_ = context->type;
1386 else if (!context->may_be_abstract)
1387 this->type_ = Type::lookup_bool_type();
1390 // Import a boolean constant.
1393 Boolean_expression::do_import(Import* imp)
1395 if (imp->peek_char() == 't')
1397 imp->require_c_string("true");
1398 return Expression::make_boolean(true, imp->location());
1402 imp->require_c_string("false");
1403 return Expression::make_boolean(false, imp->location());
1407 // Make a boolean expression.
1410 Expression::make_boolean(bool val, source_location location)
1412 return new Boolean_expression(val, location);
1415 // Class String_expression.
1420 String_expression::do_type()
1422 if (this->type_ == NULL)
1423 this->type_ = Type::make_string_type();
1427 // Set the type from the context.
1430 String_expression::do_determine_type(const Type_context* context)
1432 if (this->type_ != NULL && !this->type_->is_abstract())
1434 else if (context->type != NULL && context->type->is_string_type())
1435 this->type_ = context->type;
1436 else if (!context->may_be_abstract)
1437 this->type_ = Type::lookup_string_type();
1440 // Build a string constant.
1443 String_expression::do_get_tree(Translate_context* context)
1445 return context->gogo()->go_string_constant_tree(this->val_);
1448 // Export a string expression.
1451 String_expression::do_export(Export* exp) const
1454 s.reserve(this->val_.length() * 4 + 2);
1456 for (std::string::const_iterator p = this->val_.begin();
1457 p != this->val_.end();
1460 if (*p == '\\' || *p == '"')
1465 else if (*p >= 0x20 && *p < 0x7f)
1467 else if (*p == '\n')
1469 else if (*p == '\t')
1474 unsigned char c = *p;
1475 unsigned int dig = c >> 4;
1476 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1478 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1482 exp->write_string(s);
1485 // Import a string expression.
1488 String_expression::do_import(Import* imp)
1490 imp->require_c_string("\"");
1494 int c = imp->get_char();
1495 if (c == '"' || c == -1)
1498 val += static_cast<char>(c);
1501 c = imp->get_char();
1502 if (c == '\\' || c == '"')
1503 val += static_cast<char>(c);
1510 c = imp->get_char();
1511 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1512 c = imp->get_char();
1513 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1514 char v = (vh << 4) | vl;
1519 error_at(imp->location(), "bad string constant");
1520 return Expression::make_error(imp->location());
1524 return Expression::make_string(val, imp->location());
1527 // Make a string expression.
1530 Expression::make_string(const std::string& val, source_location location)
1532 return new String_expression(val, location);
1535 // Make an integer expression.
1537 class Integer_expression : public Expression
1540 Integer_expression(const mpz_t* val, Type* type, source_location location)
1541 : Expression(EXPRESSION_INTEGER, location),
1543 { mpz_init_set(this->val_, *val); }
1548 // Return whether VAL fits in the type.
1550 check_constant(mpz_t val, Type*, source_location);
1552 // Write VAL to export data.
1554 export_integer(Export* exp, const mpz_t val);
1558 do_is_constant() const
1562 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1568 do_determine_type(const Type_context* context);
1571 do_check_types(Gogo*);
1574 do_get_tree(Translate_context*);
1578 { return Expression::make_integer(&this->val_, this->type_,
1579 this->location()); }
1582 do_export(Export*) const;
1585 // The integer value.
1591 // Return an integer constant value.
1594 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1597 if (this->type_ != NULL)
1598 *ptype = this->type_;
1599 mpz_set(val, this->val_);
1603 // Return the current type. If we haven't set the type yet, we return
1604 // an abstract integer type.
1607 Integer_expression::do_type()
1609 if (this->type_ == NULL)
1610 this->type_ = Type::make_abstract_integer_type();
1614 // Set the type of the integer value. Here we may switch from an
1615 // abstract type to a real type.
1618 Integer_expression::do_determine_type(const Type_context* context)
1620 if (this->type_ != NULL && !this->type_->is_abstract())
1622 else if (context->type != NULL
1623 && (context->type->integer_type() != NULL
1624 || context->type->float_type() != NULL
1625 || context->type->complex_type() != NULL))
1626 this->type_ = context->type;
1627 else if (!context->may_be_abstract)
1628 this->type_ = Type::lookup_integer_type("int");
1631 // Return true if the integer VAL fits in the range of the type TYPE.
1632 // Otherwise give an error and return false. TYPE may be NULL.
1635 Integer_expression::check_constant(mpz_t val, Type* type,
1636 source_location location)
1640 Integer_type* itype = type->integer_type();
1641 if (itype == NULL || itype->is_abstract())
1644 int bits = mpz_sizeinbase(val, 2);
1646 if (itype->is_unsigned())
1648 // For an unsigned type we can only accept a nonnegative number,
1649 // and we must be able to represent at least BITS.
1650 if (mpz_sgn(val) >= 0
1651 && bits <= itype->bits())
1656 // For a signed type we need an extra bit to indicate the sign.
1657 // We have to handle the most negative integer specially.
1658 if (bits + 1 <= itype->bits()
1659 || (bits <= itype->bits()
1661 && (mpz_scan1(val, 0)
1662 == static_cast<unsigned long>(itype->bits() - 1))
1663 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1667 error_at(location, "integer constant overflow");
1671 // Check the type of an integer constant.
1674 Integer_expression::do_check_types(Gogo*)
1676 if (this->type_ == NULL)
1678 if (!Integer_expression::check_constant(this->val_, this->type_,
1680 this->set_is_error();
1683 // Get a tree for an integer constant.
1686 Integer_expression::do_get_tree(Translate_context* context)
1688 Gogo* gogo = context->gogo();
1690 if (this->type_ != NULL && !this->type_->is_abstract())
1691 type = this->type_->get_tree(gogo);
1692 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1694 // We are converting to an abstract floating point type.
1695 type = Type::lookup_float_type("float64")->get_tree(gogo);
1697 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1699 // We are converting to an abstract complex type.
1700 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
1704 // If we still have an abstract type here, then this is being
1705 // used in a constant expression which didn't get reduced for
1706 // some reason. Use a type which will fit the value. We use <,
1707 // not <=, because we need an extra bit for the sign bit.
1708 int bits = mpz_sizeinbase(this->val_, 2);
1709 if (bits < INT_TYPE_SIZE)
1710 type = Type::lookup_integer_type("int")->get_tree(gogo);
1712 type = Type::lookup_integer_type("int64")->get_tree(gogo);
1714 type = long_long_integer_type_node;
1716 return Expression::integer_constant_tree(this->val_, type);
1719 // Write VAL to export data.
1722 Integer_expression::export_integer(Export* exp, const mpz_t val)
1724 char* s = mpz_get_str(NULL, 10, val);
1725 exp->write_c_string(s);
1729 // Export an integer in a constant expression.
1732 Integer_expression::do_export(Export* exp) const
1734 Integer_expression::export_integer(exp, this->val_);
1735 // A trailing space lets us reliably identify the end of the number.
1736 exp->write_c_string(" ");
1739 // Import an integer, floating point, or complex value. This handles
1740 // all these types because they all start with digits.
1743 Integer_expression::do_import(Import* imp)
1745 std::string num = imp->read_identifier();
1746 imp->require_c_string(" ");
1747 if (!num.empty() && num[num.length() - 1] == 'i')
1750 size_t plus_pos = num.find('+', 1);
1751 size_t minus_pos = num.find('-', 1);
1753 if (plus_pos == std::string::npos)
1755 else if (minus_pos == std::string::npos)
1759 error_at(imp->location(), "bad number in import data: %qs",
1761 return Expression::make_error(imp->location());
1763 if (pos == std::string::npos)
1764 mpfr_set_ui(real, 0, GMP_RNDN);
1767 std::string real_str = num.substr(0, pos);
1768 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1770 error_at(imp->location(), "bad number in import data: %qs",
1772 return Expression::make_error(imp->location());
1776 std::string imag_str;
1777 if (pos == std::string::npos)
1780 imag_str = num.substr(pos);
1781 imag_str = imag_str.substr(0, imag_str.size() - 1);
1783 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1785 error_at(imp->location(), "bad number in import data: %qs",
1787 return Expression::make_error(imp->location());
1789 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1795 else if (num.find('.') == std::string::npos
1796 && num.find('E') == std::string::npos)
1799 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1801 error_at(imp->location(), "bad number in import data: %qs",
1803 return Expression::make_error(imp->location());
1805 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1812 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1814 error_at(imp->location(), "bad number in import data: %qs",
1816 return Expression::make_error(imp->location());
1818 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1824 // Build a new integer value.
1827 Expression::make_integer(const mpz_t* val, Type* type,
1828 source_location location)
1830 return new Integer_expression(val, type, location);
1835 class Float_expression : public Expression
1838 Float_expression(const mpfr_t* val, Type* type, source_location location)
1839 : Expression(EXPRESSION_FLOAT, location),
1842 mpfr_init_set(this->val_, *val, GMP_RNDN);
1845 // Constrain VAL to fit into TYPE.
1847 constrain_float(mpfr_t val, Type* type);
1849 // Return whether VAL fits in the type.
1851 check_constant(mpfr_t val, Type*, source_location);
1853 // Write VAL to export data.
1855 export_float(Export* exp, const mpfr_t val);
1859 do_is_constant() const
1863 do_float_constant_value(mpfr_t val, Type**) const;
1869 do_determine_type(const Type_context*);
1872 do_check_types(Gogo*);
1876 { return Expression::make_float(&this->val_, this->type_,
1877 this->location()); }
1880 do_get_tree(Translate_context*);
1883 do_export(Export*) const;
1886 // The floating point value.
1892 // Constrain VAL to fit into TYPE.
1895 Float_expression::constrain_float(mpfr_t val, Type* type)
1897 Float_type* ftype = type->float_type();
1898 if (ftype != NULL && !ftype->is_abstract())
1900 tree type_tree = ftype->type_tree();
1901 REAL_VALUE_TYPE rvt;
1902 real_from_mpfr(&rvt, val, type_tree, GMP_RNDN);
1903 real_convert(&rvt, TYPE_MODE(type_tree), &rvt);
1904 mpfr_from_real(val, &rvt, GMP_RNDN);
1908 // Return a floating point constant value.
1911 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
1913 if (this->type_ != NULL)
1914 *ptype = this->type_;
1915 mpfr_set(val, this->val_, GMP_RNDN);
1919 // Return the current type. If we haven't set the type yet, we return
1920 // an abstract float type.
1923 Float_expression::do_type()
1925 if (this->type_ == NULL)
1926 this->type_ = Type::make_abstract_float_type();
1930 // Set the type of the float value. Here we may switch from an
1931 // abstract type to a real type.
1934 Float_expression::do_determine_type(const Type_context* context)
1936 if (this->type_ != NULL && !this->type_->is_abstract())
1938 else if (context->type != NULL
1939 && (context->type->integer_type() != NULL
1940 || context->type->float_type() != NULL
1941 || context->type->complex_type() != NULL))
1942 this->type_ = context->type;
1943 else if (!context->may_be_abstract)
1944 this->type_ = Type::lookup_float_type("float64");
1947 // Return true if the floating point value VAL fits in the range of
1948 // the type TYPE. Otherwise give an error and return false. TYPE may
1952 Float_expression::check_constant(mpfr_t val, Type* type,
1953 source_location location)
1957 Float_type* ftype = type->float_type();
1958 if (ftype == NULL || ftype->is_abstract())
1961 // A NaN or Infinity always fits in the range of the type.
1962 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
1965 mp_exp_t exp = mpfr_get_exp(val);
1967 switch (ftype->bits())
1980 error_at(location, "floating point constant overflow");
1986 // Check the type of a float value.
1989 Float_expression::do_check_types(Gogo*)
1991 if (this->type_ == NULL)
1994 if (!Float_expression::check_constant(this->val_, this->type_,
1996 this->set_is_error();
1998 Integer_type* integer_type = this->type_->integer_type();
1999 if (integer_type != NULL)
2001 if (!mpfr_integer_p(this->val_))
2002 this->report_error(_("floating point constant truncated to integer"));
2005 gcc_assert(!integer_type->is_abstract());
2008 mpfr_get_z(ival, this->val_, GMP_RNDN);
2009 Integer_expression::check_constant(ival, integer_type,
2016 // Get a tree for a float constant.
2019 Float_expression::do_get_tree(Translate_context* context)
2021 Gogo* gogo = context->gogo();
2023 if (this->type_ != NULL && !this->type_->is_abstract())
2024 type = this->type_->get_tree(gogo);
2025 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2027 // We have an abstract integer type. We just hope for the best.
2028 type = Type::lookup_integer_type("int")->get_tree(gogo);
2032 // If we still have an abstract type here, then this is being
2033 // used in a constant expression which didn't get reduced. We
2034 // just use float64 and hope for the best.
2035 type = Type::lookup_float_type("float64")->get_tree(gogo);
2037 return Expression::float_constant_tree(this->val_, type);
2040 // Write a floating point number to export data.
2043 Float_expression::export_float(Export *exp, const mpfr_t val)
2046 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2048 exp->write_c_string("-");
2049 exp->write_c_string("0.");
2050 exp->write_c_string(*s == '-' ? s + 1 : s);
2053 snprintf(buf, sizeof buf, "E%ld", exponent);
2054 exp->write_c_string(buf);
2057 // Export a floating point number in a constant expression.
2060 Float_expression::do_export(Export* exp) const
2062 Float_expression::export_float(exp, this->val_);
2063 // A trailing space lets us reliably identify the end of the number.
2064 exp->write_c_string(" ");
2067 // Make a float expression.
2070 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2072 return new Float_expression(val, type, location);
2077 class Complex_expression : public Expression
2080 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2081 source_location location)
2082 : Expression(EXPRESSION_COMPLEX, location),
2085 mpfr_init_set(this->real_, *real, GMP_RNDN);
2086 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2089 // Constrain REAL/IMAG to fit into TYPE.
2091 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2093 // Return whether REAL/IMAG fits in the type.
2095 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2097 // Write REAL/IMAG to export data.
2099 export_complex(Export* exp, const mpfr_t real, const mpfr_t val);
2103 do_is_constant() const
2107 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2113 do_determine_type(const Type_context*);
2116 do_check_types(Gogo*);
2121 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2126 do_get_tree(Translate_context*);
2129 do_export(Export*) const;
2134 // The imaginary part;
2136 // The type if known.
2140 // Constrain REAL/IMAG to fit into TYPE.
2143 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2145 Complex_type* ctype = type->complex_type();
2146 if (ctype != NULL && !ctype->is_abstract())
2148 tree type_tree = ctype->type_tree();
2150 REAL_VALUE_TYPE rvt;
2151 real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN);
2152 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2153 mpfr_from_real(real, &rvt, GMP_RNDN);
2155 real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN);
2156 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2157 mpfr_from_real(imag, &rvt, GMP_RNDN);
2161 // Return a complex constant value.
2164 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2167 if (this->type_ != NULL)
2168 *ptype = this->type_;
2169 mpfr_set(real, this->real_, GMP_RNDN);
2170 mpfr_set(imag, this->imag_, GMP_RNDN);
2174 // Return the current type. If we haven't set the type yet, we return
2175 // an abstract complex type.
2178 Complex_expression::do_type()
2180 if (this->type_ == NULL)
2181 this->type_ = Type::make_abstract_complex_type();
2185 // Set the type of the complex value. Here we may switch from an
2186 // abstract type to a real type.
2189 Complex_expression::do_determine_type(const Type_context* context)
2191 if (this->type_ != NULL && !this->type_->is_abstract())
2193 else if (context->type != NULL
2194 && context->type->complex_type() != NULL)
2195 this->type_ = context->type;
2196 else if (!context->may_be_abstract)
2197 this->type_ = Type::lookup_complex_type("complex128");
2200 // Return true if the complex value REAL/IMAG fits in the range of the
2201 // type TYPE. Otherwise give an error and return false. TYPE may be
2205 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2206 source_location location)
2210 Complex_type* ctype = type->complex_type();
2211 if (ctype == NULL || ctype->is_abstract())
2215 switch (ctype->bits())
2227 // A NaN or Infinity always fits in the range of the type.
2228 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2230 if (mpfr_get_exp(real) > max_exp)
2232 error_at(location, "complex real part constant overflow");
2237 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2239 if (mpfr_get_exp(imag) > max_exp)
2241 error_at(location, "complex imaginary part constant overflow");
2249 // Check the type of a complex value.
2252 Complex_expression::do_check_types(Gogo*)
2254 if (this->type_ == NULL)
2257 if (!Complex_expression::check_constant(this->real_, this->imag_,
2258 this->type_, this->location()))
2259 this->set_is_error();
2262 // Get a tree for a complex constant.
2265 Complex_expression::do_get_tree(Translate_context* context)
2267 Gogo* gogo = context->gogo();
2269 if (this->type_ != NULL && !this->type_->is_abstract())
2270 type = this->type_->get_tree(gogo);
2273 // If we still have an abstract type here, this this is being
2274 // used in a constant expression which didn't get reduced. We
2275 // just use complex128 and hope for the best.
2276 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
2278 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2281 // Write REAL/IMAG to export data.
2284 Complex_expression::export_complex(Export* exp, const mpfr_t real,
2287 if (!mpfr_zero_p(real))
2289 Float_expression::export_float(exp, real);
2290 if (mpfr_sgn(imag) > 0)
2291 exp->write_c_string("+");
2293 Float_expression::export_float(exp, imag);
2294 exp->write_c_string("i");
2297 // Export a complex number in a constant expression.
2300 Complex_expression::do_export(Export* exp) const
2302 Complex_expression::export_complex(exp, this->real_, this->imag_);
2303 // A trailing space lets us reliably identify the end of the number.
2304 exp->write_c_string(" ");
2307 // Make a complex expression.
2310 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2311 source_location location)
2313 return new Complex_expression(real, imag, type, location);
2316 // Find a named object in an expression.
2318 class Find_named_object : public Traverse
2321 Find_named_object(Named_object* no)
2322 : Traverse(traverse_expressions),
2323 no_(no), found_(false)
2326 // Whether we found the object.
2329 { return this->found_; }
2333 expression(Expression**);
2336 // The object we are looking for.
2338 // Whether we found it.
2342 // A reference to a const in an expression.
2344 class Const_expression : public Expression
2347 Const_expression(Named_object* constant, source_location location)
2348 : Expression(EXPRESSION_CONST_REFERENCE, location),
2349 constant_(constant), type_(NULL), seen_(false)
2354 { return this->constant_; }
2356 // Check that the initializer does not refer to the constant itself.
2358 check_for_init_loop();
2362 do_traverse(Traverse*);
2365 do_lower(Gogo*, Named_object*, int);
2368 do_is_constant() const
2372 do_integer_constant_value(bool, mpz_t val, Type**) const;
2375 do_float_constant_value(mpfr_t val, Type**) const;
2378 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2381 do_string_constant_value(std::string* val) const
2382 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2387 // The type of a const is set by the declaration, not the use.
2389 do_determine_type(const Type_context*);
2392 do_check_types(Gogo*);
2399 do_get_tree(Translate_context* context);
2401 // When exporting a reference to a const as part of a const
2402 // expression, we export the value. We ignore the fact that it has
2405 do_export(Export* exp) const
2406 { this->constant_->const_value()->expr()->export_expression(exp); }
2410 Named_object* constant_;
2411 // The type of this reference. This is used if the constant has an
2414 // Used to prevent infinite recursion when a constant incorrectly
2415 // refers to itself.
2422 Const_expression::do_traverse(Traverse* traverse)
2424 if (this->type_ != NULL)
2425 return Type::traverse(this->type_, traverse);
2426 return TRAVERSE_CONTINUE;
2429 // Lower a constant expression. This is where we convert the
2430 // predeclared constant iota into an integer value.
2433 Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value)
2435 if (this->constant_->const_value()->expr()->classification()
2438 if (iota_value == -1)
2440 error_at(this->location(),
2441 "iota is only defined in const declarations");
2445 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2446 Expression* ret = Expression::make_integer(&val, NULL,
2452 // Make sure that the constant itself has been lowered.
2453 gogo->lower_constant(this->constant_);
2458 // Return an integer constant value.
2461 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2468 if (this->type_ != NULL)
2469 ctype = this->type_;
2471 ctype = this->constant_->const_value()->type();
2472 if (ctype != NULL && ctype->integer_type() == NULL)
2475 Expression* e = this->constant_->const_value()->expr();
2480 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2482 this->seen_ = false;
2486 && !Integer_expression::check_constant(val, ctype, this->location()))
2489 *ptype = ctype != NULL ? ctype : t;
2493 // Return a floating point constant value.
2496 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2502 if (this->type_ != NULL)
2503 ctype = this->type_;
2505 ctype = this->constant_->const_value()->type();
2506 if (ctype != NULL && ctype->float_type() == NULL)
2512 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2515 this->seen_ = false;
2517 if (r && ctype != NULL)
2519 if (!Float_expression::check_constant(val, ctype, this->location()))
2521 Float_expression::constrain_float(val, ctype);
2523 *ptype = ctype != NULL ? ctype : t;
2527 // Return a complex constant value.
2530 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2537 if (this->type_ != NULL)
2538 ctype = this->type_;
2540 ctype = this->constant_->const_value()->type();
2541 if (ctype != NULL && ctype->complex_type() == NULL)
2547 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2551 this->seen_ = false;
2553 if (r && ctype != NULL)
2555 if (!Complex_expression::check_constant(real, imag, ctype,
2558 Complex_expression::constrain_complex(real, imag, ctype);
2560 *ptype = ctype != NULL ? ctype : t;
2564 // Return the type of the const reference.
2567 Const_expression::do_type()
2569 if (this->type_ != NULL)
2572 Named_constant* nc = this->constant_->const_value();
2574 if (this->seen_ || nc->lowering())
2576 this->report_error(_("constant refers to itself"));
2577 this->type_ = Type::make_error_type();
2583 Type* ret = nc->type();
2587 this->seen_ = false;
2591 // During parsing, a named constant may have a NULL type, but we
2592 // must not return a NULL type here.
2593 ret = nc->expr()->type();
2595 this->seen_ = false;
2600 // Set the type of the const reference.
2603 Const_expression::do_determine_type(const Type_context* context)
2605 Type* ctype = this->constant_->const_value()->type();
2606 Type* cetype = (ctype != NULL
2608 : this->constant_->const_value()->expr()->type());
2609 if (ctype != NULL && !ctype->is_abstract())
2611 else if (context->type != NULL
2612 && (context->type->integer_type() != NULL
2613 || context->type->float_type() != NULL
2614 || context->type->complex_type() != NULL)
2615 && (cetype->integer_type() != NULL
2616 || cetype->float_type() != NULL
2617 || cetype->complex_type() != NULL))
2618 this->type_ = context->type;
2619 else if (context->type != NULL
2620 && context->type->is_string_type()
2621 && cetype->is_string_type())
2622 this->type_ = context->type;
2623 else if (context->type != NULL
2624 && context->type->is_boolean_type()
2625 && cetype->is_boolean_type())
2626 this->type_ = context->type;
2627 else if (!context->may_be_abstract)
2629 if (cetype->is_abstract())
2630 cetype = cetype->make_non_abstract_type();
2631 this->type_ = cetype;
2635 // Check for a loop in which the initializer of a constant refers to
2636 // the constant itself.
2639 Const_expression::check_for_init_loop()
2641 if (this->type_ != NULL && this->type_->is_error())
2646 this->report_error(_("constant refers to itself"));
2647 this->type_ = Type::make_error_type();
2651 Expression* init = this->constant_->const_value()->expr();
2652 Find_named_object find_named_object(this->constant_);
2655 Expression::traverse(&init, &find_named_object);
2656 this->seen_ = false;
2658 if (find_named_object.found())
2660 if (this->type_ == NULL || !this->type_->is_error())
2662 this->report_error(_("constant refers to itself"));
2663 this->type_ = Type::make_error_type();
2669 // Check types of a const reference.
2672 Const_expression::do_check_types(Gogo*)
2674 if (this->type_ != NULL && this->type_->is_error())
2677 this->check_for_init_loop();
2679 if (this->type_ == NULL || this->type_->is_abstract())
2682 // Check for integer overflow.
2683 if (this->type_->integer_type() != NULL)
2688 if (!this->integer_constant_value(true, ival, &dummy))
2692 Expression* cexpr = this->constant_->const_value()->expr();
2693 if (cexpr->float_constant_value(fval, &dummy))
2695 if (!mpfr_integer_p(fval))
2696 this->report_error(_("floating point constant "
2697 "truncated to integer"));
2700 mpfr_get_z(ival, fval, GMP_RNDN);
2701 Integer_expression::check_constant(ival, this->type_,
2711 // Return a tree for the const reference.
2714 Const_expression::do_get_tree(Translate_context* context)
2716 Gogo* gogo = context->gogo();
2718 if (this->type_ == NULL)
2719 type_tree = NULL_TREE;
2722 type_tree = this->type_->get_tree(gogo);
2723 if (type_tree == error_mark_node)
2724 return error_mark_node;
2727 // If the type has been set for this expression, but the underlying
2728 // object is an abstract int or float, we try to get the abstract
2729 // value. Otherwise we may lose something in the conversion.
2730 if (this->type_ != NULL
2731 && (this->constant_->const_value()->type() == NULL
2732 || this->constant_->const_value()->type()->is_abstract()))
2734 Expression* expr = this->constant_->const_value()->expr();
2738 if (expr->integer_constant_value(true, ival, &t))
2740 tree ret = Expression::integer_constant_tree(ival, type_tree);
2748 if (expr->float_constant_value(fval, &t))
2750 tree ret = Expression::float_constant_tree(fval, type_tree);
2757 if (expr->complex_constant_value(fval, imag, &t))
2759 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2768 tree const_tree = this->constant_->get_tree(gogo, context->function());
2769 if (this->type_ == NULL
2770 || const_tree == error_mark_node
2771 || TREE_TYPE(const_tree) == error_mark_node)
2775 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2776 ret = fold_convert(type_tree, const_tree);
2777 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2778 ret = fold(convert_to_integer(type_tree, const_tree));
2779 else if (TREE_CODE(type_tree) == REAL_TYPE)
2780 ret = fold(convert_to_real(type_tree, const_tree));
2781 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2782 ret = fold(convert_to_complex(type_tree, const_tree));
2788 // Make a reference to a constant in an expression.
2791 Expression::make_const_reference(Named_object* constant,
2792 source_location location)
2794 return new Const_expression(constant, location);
2797 // Find a named object in an expression.
2800 Find_named_object::expression(Expression** pexpr)
2802 switch ((*pexpr)->classification())
2804 case Expression::EXPRESSION_CONST_REFERENCE:
2806 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2807 if (ce->named_object() == this->no_)
2810 // We need to check a constant initializer explicitly, as
2811 // loops here will not be caught by the loop checking for
2812 // variable initializers.
2813 ce->check_for_init_loop();
2815 return TRAVERSE_CONTINUE;
2818 case Expression::EXPRESSION_VAR_REFERENCE:
2819 if ((*pexpr)->var_expression()->named_object() == this->no_)
2821 return TRAVERSE_CONTINUE;
2822 case Expression::EXPRESSION_FUNC_REFERENCE:
2823 if ((*pexpr)->func_expression()->named_object() == this->no_)
2825 return TRAVERSE_CONTINUE;
2827 return TRAVERSE_CONTINUE;
2829 this->found_ = true;
2830 return TRAVERSE_EXIT;
2835 class Nil_expression : public Expression
2838 Nil_expression(source_location location)
2839 : Expression(EXPRESSION_NIL, location)
2847 do_is_constant() const
2852 { return Type::make_nil_type(); }
2855 do_determine_type(const Type_context*)
2863 do_get_tree(Translate_context*)
2864 { return null_pointer_node; }
2867 do_export(Export* exp) const
2868 { exp->write_c_string("nil"); }
2871 // Import a nil expression.
2874 Nil_expression::do_import(Import* imp)
2876 imp->require_c_string("nil");
2877 return Expression::make_nil(imp->location());
2880 // Make a nil expression.
2883 Expression::make_nil(source_location location)
2885 return new Nil_expression(location);
2888 // The value of the predeclared constant iota. This is little more
2889 // than a marker. This will be lowered to an integer in
2890 // Const_expression::do_lower, which is where we know the value that
2893 class Iota_expression : public Parser_expression
2896 Iota_expression(source_location location)
2897 : Parser_expression(EXPRESSION_IOTA, location)
2902 do_lower(Gogo*, Named_object*, int)
2903 { gcc_unreachable(); }
2905 // There should only ever be one of these.
2908 { gcc_unreachable(); }
2911 // Make an iota expression. This is only called for one case: the
2912 // value of the predeclared constant iota.
2915 Expression::make_iota()
2917 static Iota_expression iota_expression(UNKNOWN_LOCATION);
2918 return &iota_expression;
2921 // A type conversion expression.
2923 class Type_conversion_expression : public Expression
2926 Type_conversion_expression(Type* type, Expression* expr,
2927 source_location location)
2928 : Expression(EXPRESSION_CONVERSION, location),
2929 type_(type), expr_(expr), may_convert_function_types_(false)
2932 // Return the type to which we are converting.
2935 { return this->type_; }
2937 // Return the expression which we are converting.
2940 { return this->expr_; }
2942 // Permit converting from one function type to another. This is
2943 // used internally for method expressions.
2945 set_may_convert_function_types()
2947 this->may_convert_function_types_ = true;
2950 // Import a type conversion expression.
2956 do_traverse(Traverse* traverse);
2959 do_lower(Gogo*, Named_object*, int);
2962 do_is_constant() const
2963 { return this->expr_->is_constant(); }
2966 do_integer_constant_value(bool, mpz_t, Type**) const;
2969 do_float_constant_value(mpfr_t, Type**) const;
2972 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
2975 do_string_constant_value(std::string*) const;
2979 { return this->type_; }
2982 do_determine_type(const Type_context*)
2984 Type_context subcontext(this->type_, false);
2985 this->expr_->determine_type(&subcontext);
2989 do_check_types(Gogo*);
2994 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2999 do_get_tree(Translate_context* context);
3002 do_export(Export*) const;
3005 // The type to convert to.
3007 // The expression to convert.
3009 // True if this is permitted to convert function types. This is
3010 // used internally for method expressions.
3011 bool may_convert_function_types_;
3017 Type_conversion_expression::do_traverse(Traverse* traverse)
3019 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3020 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3021 return TRAVERSE_EXIT;
3022 return TRAVERSE_CONTINUE;
3025 // Convert to a constant at lowering time.
3028 Type_conversion_expression::do_lower(Gogo*, Named_object*, int)
3030 Type* type = this->type_;
3031 Expression* val = this->expr_;
3032 source_location location = this->location();
3034 if (type->integer_type() != NULL)
3039 if (val->integer_constant_value(false, ival, &dummy))
3041 if (!Integer_expression::check_constant(ival, type, location))
3042 mpz_set_ui(ival, 0);
3043 Expression* ret = Expression::make_integer(&ival, type, location);
3050 if (val->float_constant_value(fval, &dummy))
3052 if (!mpfr_integer_p(fval))
3055 "floating point constant truncated to integer");
3056 return Expression::make_error(location);
3058 mpfr_get_z(ival, fval, GMP_RNDN);
3059 if (!Integer_expression::check_constant(ival, type, location))
3060 mpz_set_ui(ival, 0);
3061 Expression* ret = Expression::make_integer(&ival, type, location);
3070 if (type->float_type() != NULL)
3075 if (val->float_constant_value(fval, &dummy))
3077 if (!Float_expression::check_constant(fval, type, location))
3078 mpfr_set_ui(fval, 0, GMP_RNDN);
3079 Float_expression::constrain_float(fval, type);
3080 Expression *ret = Expression::make_float(&fval, type, location);
3087 if (type->complex_type() != NULL)
3094 if (val->complex_constant_value(real, imag, &dummy))
3096 if (!Complex_expression::check_constant(real, imag, type, location))
3098 mpfr_set_ui(real, 0, GMP_RNDN);
3099 mpfr_set_ui(imag, 0, GMP_RNDN);
3101 Complex_expression::constrain_complex(real, imag, type);
3102 Expression* ret = Expression::make_complex(&real, &imag, type,
3112 if (type->is_open_array_type() && type->named_type() == NULL)
3114 Type* element_type = type->array_type()->element_type()->forwarded();
3115 bool is_byte = element_type == Type::lookup_integer_type("uint8");
3116 bool is_int = element_type == Type::lookup_integer_type("int");
3117 if (is_byte || is_int)
3120 if (val->string_constant_value(&s))
3122 Expression_list* vals = new Expression_list();
3125 for (std::string::const_iterator p = s.begin();
3130 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3131 Expression* v = Expression::make_integer(&val,
3140 const char *p = s.data();
3141 const char *pend = s.data() + s.length();
3145 int adv = Lex::fetch_char(p, &c);
3148 warning_at(this->location(), 0,
3149 "invalid UTF-8 encoding");
3154 mpz_init_set_ui(val, c);
3155 Expression* v = Expression::make_integer(&val,
3163 return Expression::make_slice_composite_literal(type, vals,
3172 // Return the constant integer value if there is one.
3175 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3179 if (this->type_->integer_type() == NULL)
3185 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3187 if (!Integer_expression::check_constant(ival, this->type_,
3195 *ptype = this->type_;
3202 if (this->expr_->float_constant_value(fval, &dummy))
3204 mpfr_get_z(val, fval, GMP_RNDN);
3206 if (!Integer_expression::check_constant(val, this->type_,
3209 *ptype = this->type_;
3217 // Return the constant floating point value if there is one.
3220 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3223 if (this->type_->float_type() == NULL)
3229 if (this->expr_->float_constant_value(fval, &dummy))
3231 if (!Float_expression::check_constant(fval, this->type_,
3237 mpfr_set(val, fval, GMP_RNDN);
3239 Float_expression::constrain_float(val, this->type_);
3240 *ptype = this->type_;
3248 // Return the constant complex value if there is one.
3251 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3255 if (this->type_->complex_type() == NULL)
3263 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3265 if (!Complex_expression::check_constant(rval, ival, this->type_,
3272 mpfr_set(real, rval, GMP_RNDN);
3273 mpfr_set(imag, ival, GMP_RNDN);
3276 Complex_expression::constrain_complex(real, imag, this->type_);
3277 *ptype = this->type_;
3286 // Return the constant string value if there is one.
3289 Type_conversion_expression::do_string_constant_value(std::string* val) const
3291 if (this->type_->is_string_type()
3292 && this->expr_->type()->integer_type() != NULL)
3297 if (this->expr_->integer_constant_value(false, ival, &dummy))
3299 unsigned long ulval = mpz_get_ui(ival);
3300 if (mpz_cmp_ui(ival, ulval) == 0)
3302 Lex::append_char(ulval, true, val, this->location());
3310 // FIXME: Could handle conversion from const []int here.
3315 // Check that types are convertible.
3318 Type_conversion_expression::do_check_types(Gogo*)
3320 Type* type = this->type_;
3321 Type* expr_type = this->expr_->type();
3324 if (type->is_error() || expr_type->is_error())
3326 this->set_is_error();
3330 if (this->may_convert_function_types_
3331 && type->function_type() != NULL
3332 && expr_type->function_type() != NULL)
3335 if (Type::are_convertible(type, expr_type, &reason))
3338 error_at(this->location(), "%s", reason.c_str());
3339 this->set_is_error();
3342 // Get a tree for a type conversion.
3345 Type_conversion_expression::do_get_tree(Translate_context* context)
3347 Gogo* gogo = context->gogo();
3348 tree type_tree = this->type_->get_tree(gogo);
3349 tree expr_tree = this->expr_->get_tree(context);
3351 if (type_tree == error_mark_node
3352 || expr_tree == error_mark_node
3353 || TREE_TYPE(expr_tree) == error_mark_node)
3354 return error_mark_node;
3356 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3357 return fold_convert(type_tree, expr_tree);
3359 Type* type = this->type_;
3360 Type* expr_type = this->expr_->type();
3362 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3363 ret = Expression::convert_for_assignment(context, type, expr_type,
3364 expr_tree, this->location());
3365 else if (type->integer_type() != NULL)
3367 if (expr_type->integer_type() != NULL
3368 || expr_type->float_type() != NULL
3369 || expr_type->is_unsafe_pointer_type())
3370 ret = fold(convert_to_integer(type_tree, expr_tree));
3374 else if (type->float_type() != NULL)
3376 if (expr_type->integer_type() != NULL
3377 || expr_type->float_type() != NULL)
3378 ret = fold(convert_to_real(type_tree, expr_tree));
3382 else if (type->complex_type() != NULL)
3384 if (expr_type->complex_type() != NULL)
3385 ret = fold(convert_to_complex(type_tree, expr_tree));
3389 else if (type->is_string_type()
3390 && expr_type->integer_type() != NULL)
3392 expr_tree = fold_convert(integer_type_node, expr_tree);
3393 if (host_integerp(expr_tree, 0))
3395 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3397 Lex::append_char(intval, true, &s, this->location());
3398 Expression* se = Expression::make_string(s, this->location());
3399 return se->get_tree(context);
3402 static tree int_to_string_fndecl;
3403 ret = Gogo::call_builtin(&int_to_string_fndecl,
3405 "__go_int_to_string",
3409 fold_convert(integer_type_node, expr_tree));
3411 else if (type->is_string_type()
3412 && (expr_type->array_type() != NULL
3413 || (expr_type->points_to() != NULL
3414 && expr_type->points_to()->array_type() != NULL)))
3416 Type* t = expr_type;
3417 if (t->points_to() != NULL)
3420 expr_tree = build_fold_indirect_ref(expr_tree);
3422 if (!DECL_P(expr_tree))
3423 expr_tree = save_expr(expr_tree);
3424 Array_type* a = t->array_type();
3425 Type* e = a->element_type()->forwarded();
3426 gcc_assert(e->integer_type() != NULL);
3427 tree valptr = fold_convert(const_ptr_type_node,
3428 a->value_pointer_tree(gogo, expr_tree));
3429 tree len = a->length_tree(gogo, expr_tree);
3430 len = fold_convert_loc(this->location(), integer_type_node, len);
3431 if (e->integer_type()->is_unsigned()
3432 && e->integer_type()->bits() == 8)
3434 static tree byte_array_to_string_fndecl;
3435 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3437 "__go_byte_array_to_string",
3440 const_ptr_type_node,
3447 gcc_assert(e == Type::lookup_integer_type("int"));
3448 static tree int_array_to_string_fndecl;
3449 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3451 "__go_int_array_to_string",
3454 const_ptr_type_node,
3460 else if (type->is_open_array_type() && expr_type->is_string_type())
3462 Type* e = type->array_type()->element_type()->forwarded();
3463 gcc_assert(e->integer_type() != NULL);
3464 if (e->integer_type()->is_unsigned()
3465 && e->integer_type()->bits() == 8)
3467 static tree string_to_byte_array_fndecl;
3468 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3470 "__go_string_to_byte_array",
3473 TREE_TYPE(expr_tree),
3478 gcc_assert(e == Type::lookup_integer_type("int"));
3479 static tree string_to_int_array_fndecl;
3480 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3482 "__go_string_to_int_array",
3485 TREE_TYPE(expr_tree),
3489 else if ((type->is_unsafe_pointer_type()
3490 && expr_type->points_to() != NULL)
3491 || (expr_type->is_unsafe_pointer_type()
3492 && type->points_to() != NULL))
3493 ret = fold_convert(type_tree, expr_tree);
3494 else if (type->is_unsafe_pointer_type()
3495 && expr_type->integer_type() != NULL)
3496 ret = convert_to_pointer(type_tree, expr_tree);
3497 else if (this->may_convert_function_types_
3498 && type->function_type() != NULL
3499 && expr_type->function_type() != NULL)
3500 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3502 ret = Expression::convert_for_assignment(context, type, expr_type,
3503 expr_tree, this->location());
3508 // Output a type conversion in a constant expression.
3511 Type_conversion_expression::do_export(Export* exp) const
3513 exp->write_c_string("convert(");
3514 exp->write_type(this->type_);
3515 exp->write_c_string(", ");
3516 this->expr_->export_expression(exp);
3517 exp->write_c_string(")");
3520 // Import a type conversion or a struct construction.
3523 Type_conversion_expression::do_import(Import* imp)
3525 imp->require_c_string("convert(");
3526 Type* type = imp->read_type();
3527 imp->require_c_string(", ");
3528 Expression* val = Expression::import_expression(imp);
3529 imp->require_c_string(")");
3530 return Expression::make_cast(type, val, imp->location());
3533 // Make a type cast expression.
3536 Expression::make_cast(Type* type, Expression* val, source_location location)
3538 if (type->is_error_type() || val->is_error_expression())
3539 return Expression::make_error(location);
3540 return new Type_conversion_expression(type, val, location);
3543 // An unsafe type conversion, used to pass values to builtin functions.
3545 class Unsafe_type_conversion_expression : public Expression
3548 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3549 source_location location)
3550 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3551 type_(type), expr_(expr)
3556 do_traverse(Traverse* traverse);
3560 { return this->type_; }
3563 do_determine_type(const Type_context*)
3569 return new Unsafe_type_conversion_expression(this->type_,
3570 this->expr_->copy(),
3575 do_get_tree(Translate_context*);
3578 // The type to convert to.
3580 // The expression to convert.
3587 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3589 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3590 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3591 return TRAVERSE_EXIT;
3592 return TRAVERSE_CONTINUE;
3595 // Convert to backend representation.
3598 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3600 // We are only called for a limited number of cases.
3602 Type* t = this->type_;
3603 Type* et = this->expr_->type();
3605 tree type_tree = this->type_->get_tree(context->gogo());
3606 tree expr_tree = this->expr_->get_tree(context);
3607 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3608 return error_mark_node;
3610 source_location loc = this->location();
3612 bool use_view_convert = false;
3613 if (t->is_open_array_type())
3615 gcc_assert(et->is_open_array_type());
3616 use_view_convert = true;
3618 else if (t->map_type() != NULL)
3619 gcc_assert(et->map_type() != NULL);
3620 else if (t->channel_type() != NULL)
3621 gcc_assert(et->channel_type() != NULL);
3622 else if (t->points_to() != NULL && t->points_to()->channel_type() != NULL)
3623 gcc_assert((et->points_to() != NULL
3624 && et->points_to()->channel_type() != NULL)
3625 || et->is_nil_type());
3626 else if (t->is_unsafe_pointer_type())
3627 gcc_assert(et->points_to() != NULL || et->is_nil_type());
3628 else if (et->is_unsafe_pointer_type())
3629 gcc_assert(t->points_to() != NULL);
3630 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3632 gcc_assert(et->interface_type() != NULL
3633 && !et->interface_type()->is_empty());
3634 use_view_convert = true;
3636 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3638 gcc_assert(et->interface_type() != NULL
3639 && et->interface_type()->is_empty());
3640 use_view_convert = true;
3642 else if (t->integer_type() != NULL)
3644 gcc_assert(et->is_boolean_type()
3645 || et->integer_type() != NULL
3646 || et->function_type() != NULL
3647 || et->points_to() != NULL
3648 || et->map_type() != NULL
3649 || et->channel_type() != NULL);
3650 return convert_to_integer(type_tree, expr_tree);
3655 if (use_view_convert)
3656 return fold_build1_loc(loc, VIEW_CONVERT_EXPR, type_tree, expr_tree);
3658 return fold_convert_loc(loc, type_tree, expr_tree);
3661 // Make an unsafe type conversion expression.
3664 Expression::make_unsafe_cast(Type* type, Expression* expr,
3665 source_location location)
3667 return new Unsafe_type_conversion_expression(type, expr, location);
3670 // Unary expressions.
3672 class Unary_expression : public Expression
3675 Unary_expression(Operator op, Expression* expr, source_location location)
3676 : Expression(EXPRESSION_UNARY, location),
3677 op_(op), escapes_(true), expr_(expr)
3680 // Return the operator.
3683 { return this->op_; }
3685 // Return the operand.
3688 { return this->expr_; }
3690 // Record that an address expression does not escape.
3692 set_does_not_escape()
3694 gcc_assert(this->op_ == OPERATOR_AND);
3695 this->escapes_ = false;
3698 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3699 // could be done, false if not.
3701 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3704 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3705 // could be done, false if not.
3707 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3709 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3710 // true if this could be done, false if not.
3712 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3720 do_traverse(Traverse* traverse)
3721 { return Expression::traverse(&this->expr_, traverse); }
3724 do_lower(Gogo*, Named_object*, int);
3727 do_is_constant() const;
3730 do_integer_constant_value(bool, mpz_t, Type**) const;
3733 do_float_constant_value(mpfr_t, Type**) const;
3736 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3742 do_determine_type(const Type_context*);
3745 do_check_types(Gogo*);
3750 return Expression::make_unary(this->op_, this->expr_->copy(),
3755 do_is_addressable() const
3756 { return this->op_ == OPERATOR_MULT; }
3759 do_get_tree(Translate_context*);
3762 do_export(Export*) const;
3765 // The unary operator to apply.
3767 // Normally true. False if this is an address expression which does
3768 // not escape the current function.
3774 // If we are taking the address of a composite literal, and the
3775 // contents are not constant, then we want to make a heap composite
3779 Unary_expression::do_lower(Gogo*, Named_object*, int)
3781 source_location loc = this->location();
3782 Operator op = this->op_;
3783 Expression* expr = this->expr_;
3785 if (op == OPERATOR_MULT && expr->is_type_expression())
3786 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3788 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3789 // moving x to the heap. FIXME: Is it worth doing a real escape
3790 // analysis here? This case is found in math/unsafe.go and is
3791 // therefore worth special casing.
3792 if (op == OPERATOR_MULT)
3794 Expression* e = expr;
3795 while (e->classification() == EXPRESSION_CONVERSION)
3797 Type_conversion_expression* te
3798 = static_cast<Type_conversion_expression*>(e);
3802 if (e->classification() == EXPRESSION_UNARY)
3804 Unary_expression* ue = static_cast<Unary_expression*>(e);
3805 if (ue->op_ == OPERATOR_AND)
3812 ue->set_does_not_escape();
3817 // Catching an invalid indirection of unsafe.Pointer here avoid
3818 // having to deal with TYPE_VOID in other places.
3819 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
3821 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3822 return Expression::make_error(this->location());
3825 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
3826 || op == OPERATOR_NOT || op == OPERATOR_XOR)
3828 Expression* ret = NULL;
3833 if (expr->integer_constant_value(false, eval, &etype))
3837 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
3838 ret = Expression::make_integer(&val, etype, loc);
3845 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
3850 if (expr->float_constant_value(fval, &ftype))
3854 if (Unary_expression::eval_float(op, fval, val))
3855 ret = Expression::make_float(&val, ftype, loc);
3866 if (expr->complex_constant_value(fval, ival, &ftype))
3872 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
3873 ret = Expression::make_complex(&real, &imag, ftype, loc);
3887 // Return whether a unary expression is a constant.
3890 Unary_expression::do_is_constant() const
3892 if (this->op_ == OPERATOR_MULT)
3894 // Indirecting through a pointer is only constant if the object
3895 // to which the expression points is constant, but we currently
3896 // have no way to determine that.
3899 else if (this->op_ == OPERATOR_AND)
3901 // Taking the address of a variable is constant if it is a
3902 // global variable, not constant otherwise. In other cases
3903 // taking the address is probably not a constant.
3904 Var_expression* ve = this->expr_->var_expression();
3907 Named_object* no = ve->named_object();
3908 return no->is_variable() && no->var_value()->is_global();
3913 return this->expr_->is_constant();
3916 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3917 // UVAL, if known; it may be NULL. Return true if this could be done,
3921 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3922 source_location location)
3929 case OPERATOR_MINUS:
3931 return Integer_expression::check_constant(val, utype, location);
3933 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3937 || utype->integer_type() == NULL
3938 || utype->integer_type()->is_abstract())
3942 // The number of HOST_WIDE_INTs that it takes to represent
3944 size_t count = ((mpz_sizeinbase(uval, 2)
3945 + HOST_BITS_PER_WIDE_INT
3947 / HOST_BITS_PER_WIDE_INT);
3949 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3950 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3953 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3954 gcc_assert(ecount <= count);
3956 // Trim down to the number of words required by the type.
3957 size_t obits = utype->integer_type()->bits();
3958 if (!utype->integer_type()->is_unsigned())
3960 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3961 / HOST_BITS_PER_WIDE_INT);
3962 gcc_assert(ocount <= count);
3964 for (size_t i = 0; i < ocount; ++i)
3967 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3969 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3972 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3976 return Integer_expression::check_constant(val, utype, location);
3985 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3986 // could be done, false if not.
3989 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
3994 mpfr_set(val, uval, GMP_RNDN);
3996 case OPERATOR_MINUS:
3997 mpfr_neg(val, uval, GMP_RNDN);
4009 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
4010 // if this could be done, false if not.
4013 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
4014 mpfr_t real, mpfr_t imag)
4019 mpfr_set(real, rval, GMP_RNDN);
4020 mpfr_set(imag, ival, GMP_RNDN);
4022 case OPERATOR_MINUS:
4023 mpfr_neg(real, rval, GMP_RNDN);
4024 mpfr_neg(imag, ival, GMP_RNDN);
4036 // Return the integral constant value of a unary expression, if it has one.
4039 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
4045 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
4048 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
4054 // Return the floating point constant value of a unary expression, if
4058 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
4063 if (!this->expr_->float_constant_value(uval, ptype))
4066 ret = Unary_expression::eval_float(this->op_, uval, val);
4071 // Return the complex constant value of a unary expression, if it has
4075 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
4083 if (!this->expr_->complex_constant_value(rval, ival, ptype))
4086 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
4092 // Return the type of a unary expression.
4095 Unary_expression::do_type()
4100 case OPERATOR_MINUS:
4103 return this->expr_->type();
4106 return Type::make_pointer_type(this->expr_->type());
4110 Type* subtype = this->expr_->type();
4111 Type* points_to = subtype->points_to();
4112 if (points_to == NULL)
4113 return Type::make_error_type();
4122 // Determine abstract types for a unary expression.
4125 Unary_expression::do_determine_type(const Type_context* context)
4130 case OPERATOR_MINUS:
4133 this->expr_->determine_type(context);
4137 // Taking the address of something.
4139 Type* subtype = (context->type == NULL
4141 : context->type->points_to());
4142 Type_context subcontext(subtype, false);
4143 this->expr_->determine_type(&subcontext);
4148 // Indirecting through a pointer.
4150 Type* subtype = (context->type == NULL
4152 : Type::make_pointer_type(context->type));
4153 Type_context subcontext(subtype, false);
4154 this->expr_->determine_type(&subcontext);
4163 // Check types for a unary expression.
4166 Unary_expression::do_check_types(Gogo*)
4168 Type* type = this->expr_->type();
4169 if (type->is_error())
4171 this->set_is_error();
4178 case OPERATOR_MINUS:
4179 if (type->integer_type() == NULL
4180 && type->float_type() == NULL
4181 && type->complex_type() == NULL)
4182 this->report_error(_("expected numeric type"));
4187 if (type->integer_type() == NULL
4188 && !type->is_boolean_type())
4189 this->report_error(_("expected integer or boolean type"));
4193 if (!this->expr_->is_addressable())
4194 this->report_error(_("invalid operand for unary %<&%>"));
4196 this->expr_->address_taken(this->escapes_);
4200 // Indirecting through a pointer.
4201 if (type->points_to() == NULL)
4202 this->report_error(_("expected pointer"));
4210 // Get a tree for a unary expression.
4213 Unary_expression::do_get_tree(Translate_context* context)
4215 tree expr = this->expr_->get_tree(context);
4216 if (expr == error_mark_node)
4217 return error_mark_node;
4219 source_location loc = this->location();
4225 case OPERATOR_MINUS:
4227 tree type = TREE_TYPE(expr);
4228 tree compute_type = excess_precision_type(type);
4229 if (compute_type != NULL_TREE)
4230 expr = ::convert(compute_type, expr);
4231 tree ret = fold_build1_loc(loc, NEGATE_EXPR,
4232 (compute_type != NULL_TREE
4236 if (compute_type != NULL_TREE)
4237 ret = ::convert(type, ret);
4242 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4243 return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr);
4245 return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr,
4246 build_int_cst(TREE_TYPE(expr), 0));
4249 return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr);
4252 // We should not see a non-constant constructor here; cases
4253 // where we would see one should have been moved onto the heap
4254 // at parse time. Taking the address of a nonconstant
4255 // constructor will not do what the programmer expects.
4256 gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4257 gcc_assert(TREE_CODE(expr) != ADDR_EXPR);
4259 // Build a decl for a constant constructor.
4260 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4262 tree decl = build_decl(this->location(), VAR_DECL,
4263 create_tmp_var_name("C"), TREE_TYPE(expr));
4264 DECL_EXTERNAL(decl) = 0;
4265 TREE_PUBLIC(decl) = 0;
4266 TREE_READONLY(decl) = 1;
4267 TREE_CONSTANT(decl) = 1;
4268 TREE_STATIC(decl) = 1;
4269 TREE_ADDRESSABLE(decl) = 1;
4270 DECL_ARTIFICIAL(decl) = 1;
4271 DECL_INITIAL(decl) = expr;
4272 rest_of_decl_compilation(decl, 1, 0);
4276 return build_fold_addr_expr_loc(loc, expr);
4280 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4282 // If we are dereferencing the pointer to a large struct, we
4283 // need to check for nil. We don't bother to check for small
4284 // structs because we expect the system to crash on a nil
4285 // pointer dereference.
4286 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4287 if (s == -1 || s >= 4096)
4290 expr = save_expr(expr);
4291 tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node,
4293 fold_convert(TREE_TYPE(expr),
4294 null_pointer_node));
4295 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4297 expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr),
4298 build3(COND_EXPR, void_type_node,
4299 compare, crash, NULL_TREE),
4303 // If the type of EXPR is a recursive pointer type, then we
4304 // need to insert a cast before indirecting.
4305 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4307 Type* pt = this->expr_->type()->points_to();
4308 tree ind = pt->get_tree(context->gogo());
4309 expr = fold_convert_loc(loc, build_pointer_type(ind), expr);
4312 return build_fold_indirect_ref_loc(loc, expr);
4320 // Export a unary expression.
4323 Unary_expression::do_export(Export* exp) const
4328 exp->write_c_string("+ ");
4330 case OPERATOR_MINUS:
4331 exp->write_c_string("- ");
4334 exp->write_c_string("! ");
4337 exp->write_c_string("^ ");
4344 this->expr_->export_expression(exp);
4347 // Import a unary expression.
4350 Unary_expression::do_import(Import* imp)
4353 switch (imp->get_char())
4359 op = OPERATOR_MINUS;
4370 imp->require_c_string(" ");
4371 Expression* expr = Expression::import_expression(imp);
4372 return Expression::make_unary(op, expr, imp->location());
4375 // Make a unary expression.
4378 Expression::make_unary(Operator op, Expression* expr, source_location location)
4380 return new Unary_expression(op, expr, location);
4383 // If this is an indirection through a pointer, return the expression
4384 // being pointed through. Otherwise return this.
4389 if (this->classification_ == EXPRESSION_UNARY)
4391 Unary_expression* ue = static_cast<Unary_expression*>(this);
4392 if (ue->op() == OPERATOR_MULT)
4393 return ue->operand();
4398 // Class Binary_expression.
4403 Binary_expression::do_traverse(Traverse* traverse)
4405 int t = Expression::traverse(&this->left_, traverse);
4406 if (t == TRAVERSE_EXIT)
4407 return TRAVERSE_EXIT;
4408 return Expression::traverse(&this->right_, traverse);
4411 // Compare integer constants according to OP.
4414 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4417 int i = mpz_cmp(left_val, right_val);
4422 case OPERATOR_NOTEQ:
4437 // Compare floating point constants according to OP.
4440 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4445 i = mpfr_cmp(left_val, right_val);
4449 mpfr_init_set(lv, left_val, GMP_RNDN);
4451 mpfr_init_set(rv, right_val, GMP_RNDN);
4452 Float_expression::constrain_float(lv, type);
4453 Float_expression::constrain_float(rv, type);
4454 i = mpfr_cmp(lv, rv);
4462 case OPERATOR_NOTEQ:
4477 // Compare complex constants according to OP. Complex numbers may
4478 // only be compared for equality.
4481 Binary_expression::compare_complex(Operator op, Type* type,
4482 mpfr_t left_real, mpfr_t left_imag,
4483 mpfr_t right_real, mpfr_t right_imag)
4487 is_equal = (mpfr_cmp(left_real, right_real) == 0
4488 && mpfr_cmp(left_imag, right_imag) == 0);
4493 mpfr_init_set(lr, left_real, GMP_RNDN);
4494 mpfr_init_set(li, left_imag, GMP_RNDN);
4497 mpfr_init_set(rr, right_real, GMP_RNDN);
4498 mpfr_init_set(ri, right_imag, GMP_RNDN);
4499 Complex_expression::constrain_complex(lr, li, type);
4500 Complex_expression::constrain_complex(rr, ri, type);
4501 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4511 case OPERATOR_NOTEQ:
4518 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4519 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4520 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4521 // this could be done, false if not.
4524 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4525 Type* right_type, mpz_t right_val,
4526 source_location location, mpz_t val)
4528 bool is_shift_op = false;
4532 case OPERATOR_ANDAND:
4534 case OPERATOR_NOTEQ:
4539 // These return boolean values. We should probably handle them
4540 // anyhow in case a type conversion is used on the result.
4543 mpz_add(val, left_val, right_val);
4545 case OPERATOR_MINUS:
4546 mpz_sub(val, left_val, right_val);
4549 mpz_ior(val, left_val, right_val);
4552 mpz_xor(val, left_val, right_val);
4555 mpz_mul(val, left_val, right_val);
4558 if (mpz_sgn(right_val) != 0)
4559 mpz_tdiv_q(val, left_val, right_val);
4562 error_at(location, "division by zero");
4568 if (mpz_sgn(right_val) != 0)
4569 mpz_tdiv_r(val, left_val, right_val);
4572 error_at(location, "division by zero");
4577 case OPERATOR_LSHIFT:
4579 unsigned long shift = mpz_get_ui(right_val);
4580 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
4582 error_at(location, "shift count overflow");
4586 mpz_mul_2exp(val, left_val, shift);
4591 case OPERATOR_RSHIFT:
4593 unsigned long shift = mpz_get_ui(right_val);
4594 if (mpz_cmp_ui(right_val, shift) != 0)
4596 error_at(location, "shift count overflow");
4600 if (mpz_cmp_ui(left_val, 0) >= 0)
4601 mpz_tdiv_q_2exp(val, left_val, shift);
4603 mpz_fdiv_q_2exp(val, left_val, shift);
4609 mpz_and(val, left_val, right_val);
4611 case OPERATOR_BITCLEAR:
4615 mpz_com(tval, right_val);
4616 mpz_and(val, left_val, tval);
4624 Type* type = left_type;
4629 else if (type != right_type && right_type != NULL)
4631 if (type->is_abstract())
4633 else if (!right_type->is_abstract())
4635 // This look like a type error which should be diagnosed
4636 // elsewhere. Don't do anything here, to avoid an
4637 // unhelpful chain of error messages.
4643 if (type != NULL && !type->is_abstract())
4645 // We have to check the operands too, as we have implicitly
4646 // coerced them to TYPE.
4647 if ((type != left_type
4648 && !Integer_expression::check_constant(left_val, type, location))
4650 && type != right_type
4651 && !Integer_expression::check_constant(right_val, type,
4653 || !Integer_expression::check_constant(val, type, location))
4660 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4661 // Return true if this could be done, false if not.
4664 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4665 Type* right_type, mpfr_t right_val,
4666 mpfr_t val, source_location location)
4671 case OPERATOR_ANDAND:
4673 case OPERATOR_NOTEQ:
4678 // These return boolean values. We should probably handle them
4679 // anyhow in case a type conversion is used on the result.
4682 mpfr_add(val, left_val, right_val, GMP_RNDN);
4684 case OPERATOR_MINUS:
4685 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4690 case OPERATOR_BITCLEAR:
4693 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4696 if (mpfr_zero_p(right_val))
4697 error_at(location, "division by zero");
4698 mpfr_div(val, left_val, right_val, GMP_RNDN);
4702 case OPERATOR_LSHIFT:
4703 case OPERATOR_RSHIFT:
4709 Type* type = left_type;
4712 else if (type != right_type && right_type != NULL)
4714 if (type->is_abstract())
4716 else if (!right_type->is_abstract())
4718 // This looks like a type error which should be diagnosed
4719 // elsewhere. Don't do anything here, to avoid an unhelpful
4720 // chain of error messages.
4725 if (type != NULL && !type->is_abstract())
4727 if ((type != left_type
4728 && !Float_expression::check_constant(left_val, type, location))
4729 || (type != right_type
4730 && !Float_expression::check_constant(right_val, type,
4732 || !Float_expression::check_constant(val, type, location))
4733 mpfr_set_ui(val, 0, GMP_RNDN);
4739 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4740 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4741 // could be done, false if not.
4744 Binary_expression::eval_complex(Operator op, Type* left_type,
4745 mpfr_t left_real, mpfr_t left_imag,
4747 mpfr_t right_real, mpfr_t right_imag,
4748 mpfr_t real, mpfr_t imag,
4749 source_location location)
4754 case OPERATOR_ANDAND:
4756 case OPERATOR_NOTEQ:
4761 // These return boolean values and must be handled differently.
4764 mpfr_add(real, left_real, right_real, GMP_RNDN);
4765 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4767 case OPERATOR_MINUS:
4768 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4769 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4774 case OPERATOR_BITCLEAR:
4778 // You might think that multiplying two complex numbers would
4779 // be simple, and you would be right, until you start to think
4780 // about getting the right answer for infinity. If one
4781 // operand here is infinity and the other is anything other
4782 // than zero or NaN, then we are going to wind up subtracting
4783 // two infinity values. That will give us a NaN, but the
4784 // correct answer is infinity.
4788 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4792 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4796 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4800 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4802 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4803 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4805 // If we get NaN on both sides, check whether it should really
4806 // be infinity. The rule is that if either side of the
4807 // complex number is infinity, then the whole value is
4808 // infinity, even if the other side is NaN. So the only case
4809 // we have to fix is the one in which both sides are NaN.
4810 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4811 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4812 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4814 bool is_infinity = false;
4818 mpfr_init_set(lr, left_real, GMP_RNDN);
4819 mpfr_init_set(li, left_imag, GMP_RNDN);
4823 mpfr_init_set(rr, right_real, GMP_RNDN);
4824 mpfr_init_set(ri, right_imag, GMP_RNDN);
4826 // If the left side is infinity, then the result is
4828 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4830 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4831 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4832 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4833 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4836 mpfr_set_ui(rr, 0, GMP_RNDN);
4837 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4841 mpfr_set_ui(ri, 0, GMP_RNDN);
4842 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4847 // If the right side is infinity, then the result is
4849 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4851 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4852 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4853 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4854 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4857 mpfr_set_ui(lr, 0, GMP_RNDN);
4858 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4862 mpfr_set_ui(li, 0, GMP_RNDN);
4863 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4868 // If we got an overflow in the intermediate computations,
4869 // then the result is infinity.
4871 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4872 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4876 mpfr_set_ui(lr, 0, GMP_RNDN);
4877 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4881 mpfr_set_ui(li, 0, GMP_RNDN);
4882 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4886 mpfr_set_ui(rr, 0, GMP_RNDN);
4887 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4891 mpfr_set_ui(ri, 0, GMP_RNDN);
4892 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4899 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4900 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4901 mpfr_mul(lirr, li, rr, GMP_RNDN);
4902 mpfr_mul(liri, li, ri, GMP_RNDN);
4903 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4904 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4905 mpfr_set_inf(real, mpfr_sgn(real));
4906 mpfr_set_inf(imag, mpfr_sgn(imag));
4923 // For complex division we want to avoid having an
4924 // intermediate overflow turn the whole result in a NaN. We
4925 // scale the values to try to avoid this.
4927 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
4928 error_at(location, "division by zero");
4934 mpfr_abs(rra, right_real, GMP_RNDN);
4935 mpfr_abs(ria, right_imag, GMP_RNDN);
4938 mpfr_max(t, rra, ria, GMP_RNDN);
4942 mpfr_init_set(rr, right_real, GMP_RNDN);
4943 mpfr_init_set(ri, right_imag, GMP_RNDN);
4945 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4947 ilogbw = mpfr_get_exp(t);
4948 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4949 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4954 mpfr_mul(denom, rr, rr, GMP_RNDN);
4955 mpfr_mul(t, ri, ri, GMP_RNDN);
4956 mpfr_add(denom, denom, t, GMP_RNDN);
4958 mpfr_mul(real, left_real, rr, GMP_RNDN);
4959 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4960 mpfr_add(real, real, t, GMP_RNDN);
4961 mpfr_div(real, real, denom, GMP_RNDN);
4962 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4964 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4965 mpfr_mul(t, left_real, ri, GMP_RNDN);
4966 mpfr_sub(imag, imag, t, GMP_RNDN);
4967 mpfr_div(imag, imag, denom, GMP_RNDN);
4968 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4970 // If we wind up with NaN on both sides, check whether we
4971 // should really have infinity. The rule is that if either
4972 // side of the complex number is infinity, then the whole
4973 // value is infinity, even if the other side is NaN. So the
4974 // only case we have to fix is the one in which both sides are
4976 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4977 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4978 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4980 if (mpfr_zero_p(denom))
4982 mpfr_set_inf(real, mpfr_sgn(rr));
4983 mpfr_mul(real, real, left_real, GMP_RNDN);
4984 mpfr_set_inf(imag, mpfr_sgn(rr));
4985 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4987 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4988 && mpfr_number_p(rr) && mpfr_number_p(ri))
4990 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
4991 mpfr_copysign(t, t, left_real, GMP_RNDN);
4994 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
4995 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
4999 mpfr_mul(t3, t, rr, GMP_RNDN);
5003 mpfr_mul(t4, t2, ri, GMP_RNDN);
5005 mpfr_add(t3, t3, t4, GMP_RNDN);
5006 mpfr_set_inf(real, mpfr_sgn(t3));
5008 mpfr_mul(t3, t2, rr, GMP_RNDN);
5009 mpfr_mul(t4, t, ri, GMP_RNDN);
5010 mpfr_sub(t3, t3, t4, GMP_RNDN);
5011 mpfr_set_inf(imag, mpfr_sgn(t3));
5017 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
5018 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
5020 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5021 mpfr_copysign(t, t, rr, GMP_RNDN);
5024 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5025 mpfr_copysign(t2, t2, ri, GMP_RNDN);
5029 mpfr_mul(t3, left_real, t, GMP_RNDN);
5033 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
5035 mpfr_add(t3, t3, t4, GMP_RNDN);
5036 mpfr_set_ui(real, 0, GMP_RNDN);
5037 mpfr_mul(real, real, t3, GMP_RNDN);
5039 mpfr_mul(t3, left_imag, t, GMP_RNDN);
5040 mpfr_mul(t4, left_real, t2, GMP_RNDN);
5041 mpfr_sub(t3, t3, t4, GMP_RNDN);
5042 mpfr_set_ui(imag, 0, GMP_RNDN);
5043 mpfr_mul(imag, imag, t3, GMP_RNDN);
5061 case OPERATOR_LSHIFT:
5062 case OPERATOR_RSHIFT:
5068 Type* type = left_type;
5071 else if (type != right_type && right_type != NULL)
5073 if (type->is_abstract())
5075 else if (!right_type->is_abstract())
5077 // This looks like a type error which should be diagnosed
5078 // elsewhere. Don't do anything here, to avoid an unhelpful
5079 // chain of error messages.
5084 if (type != NULL && !type->is_abstract())
5086 if ((type != left_type
5087 && !Complex_expression::check_constant(left_real, left_imag,
5089 || (type != right_type
5090 && !Complex_expression::check_constant(right_real, right_imag,
5092 || !Complex_expression::check_constant(real, imag, type,
5095 mpfr_set_ui(real, 0, GMP_RNDN);
5096 mpfr_set_ui(imag, 0, GMP_RNDN);
5103 // Lower a binary expression. We have to evaluate constant
5104 // expressions now, in order to implement Go's unlimited precision
5108 Binary_expression::do_lower(Gogo*, Named_object*, int)
5110 source_location location = this->location();
5111 Operator op = this->op_;
5112 Expression* left = this->left_;
5113 Expression* right = this->right_;
5115 const bool is_comparison = (op == OPERATOR_EQEQ
5116 || op == OPERATOR_NOTEQ
5117 || op == OPERATOR_LT
5118 || op == OPERATOR_LE
5119 || op == OPERATOR_GT
5120 || op == OPERATOR_GE);
5122 // Integer constant expressions.
5128 mpz_init(right_val);
5130 if (left->integer_constant_value(false, left_val, &left_type)
5131 && right->integer_constant_value(false, right_val, &right_type))
5133 Expression* ret = NULL;
5134 if (left_type != right_type
5135 && left_type != NULL
5136 && right_type != NULL
5137 && left_type->base() != right_type->base()
5138 && op != OPERATOR_LSHIFT
5139 && op != OPERATOR_RSHIFT)
5141 // May be a type error--let it be diagnosed later.
5143 else if (is_comparison)
5145 bool b = Binary_expression::compare_integer(op, left_val,
5147 ret = Expression::make_cast(Type::lookup_bool_type(),
5148 Expression::make_boolean(b, location),
5156 if (Binary_expression::eval_integer(op, left_type, left_val,
5157 right_type, right_val,
5160 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
5162 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5164 else if (left_type == NULL)
5166 else if (right_type == NULL)
5168 else if (!left_type->is_abstract()
5169 && left_type->named_type() != NULL)
5171 else if (!right_type->is_abstract()
5172 && right_type->named_type() != NULL)
5174 else if (!left_type->is_abstract())
5176 else if (!right_type->is_abstract())
5178 else if (left_type->float_type() != NULL)
5180 else if (right_type->float_type() != NULL)
5182 else if (left_type->complex_type() != NULL)
5184 else if (right_type->complex_type() != NULL)
5188 ret = Expression::make_integer(&val, type, location);
5196 mpz_clear(right_val);
5197 mpz_clear(left_val);
5201 mpz_clear(right_val);
5202 mpz_clear(left_val);
5205 // Floating point constant expressions.
5208 mpfr_init(left_val);
5211 mpfr_init(right_val);
5213 if (left->float_constant_value(left_val, &left_type)
5214 && right->float_constant_value(right_val, &right_type))
5216 Expression* ret = NULL;
5217 if (left_type != right_type
5218 && left_type != NULL
5219 && right_type != NULL
5220 && left_type->base() != right_type->base()
5221 && op != OPERATOR_LSHIFT
5222 && op != OPERATOR_RSHIFT)
5224 // May be a type error--let it be diagnosed later.
5226 else if (is_comparison)
5228 bool b = Binary_expression::compare_float(op,
5232 left_val, right_val);
5233 ret = Expression::make_boolean(b, location);
5240 if (Binary_expression::eval_float(op, left_type, left_val,
5241 right_type, right_val, val,
5244 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5245 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5247 if (left_type == NULL)
5249 else if (right_type == NULL)
5251 else if (!left_type->is_abstract()
5252 && left_type->named_type() != NULL)
5254 else if (!right_type->is_abstract()
5255 && right_type->named_type() != NULL)
5257 else if (!left_type->is_abstract())
5259 else if (!right_type->is_abstract())
5261 else if (left_type->float_type() != NULL)
5263 else if (right_type->float_type() != NULL)
5267 ret = Expression::make_float(&val, type, location);
5275 mpfr_clear(right_val);
5276 mpfr_clear(left_val);
5280 mpfr_clear(right_val);
5281 mpfr_clear(left_val);
5284 // Complex constant expressions.
5288 mpfr_init(left_real);
5289 mpfr_init(left_imag);
5294 mpfr_init(right_real);
5295 mpfr_init(right_imag);
5298 if (left->complex_constant_value(left_real, left_imag, &left_type)
5299 && right->complex_constant_value(right_real, right_imag, &right_type))
5301 Expression* ret = NULL;
5302 if (left_type != right_type
5303 && left_type != NULL
5304 && right_type != NULL
5305 && left_type->base() != right_type->base())
5307 // May be a type error--let it be diagnosed later.
5309 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5311 bool b = Binary_expression::compare_complex(op,
5319 ret = Expression::make_boolean(b, location);
5328 if (Binary_expression::eval_complex(op, left_type,
5329 left_real, left_imag,
5331 right_real, right_imag,
5335 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5336 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5338 if (left_type == NULL)
5340 else if (right_type == NULL)
5342 else if (!left_type->is_abstract()
5343 && left_type->named_type() != NULL)
5345 else if (!right_type->is_abstract()
5346 && right_type->named_type() != NULL)
5348 else if (!left_type->is_abstract())
5350 else if (!right_type->is_abstract())
5352 else if (left_type->complex_type() != NULL)
5354 else if (right_type->complex_type() != NULL)
5358 ret = Expression::make_complex(&real, &imag, type,
5367 mpfr_clear(left_real);
5368 mpfr_clear(left_imag);
5369 mpfr_clear(right_real);
5370 mpfr_clear(right_imag);
5375 mpfr_clear(left_real);
5376 mpfr_clear(left_imag);
5377 mpfr_clear(right_real);
5378 mpfr_clear(right_imag);
5381 // String constant expressions.
5382 if (op == OPERATOR_PLUS
5383 && left->type()->is_string_type()
5384 && right->type()->is_string_type())
5386 std::string left_string;
5387 std::string right_string;
5388 if (left->string_constant_value(&left_string)
5389 && right->string_constant_value(&right_string))
5390 return Expression::make_string(left_string + right_string, location);
5396 // Return the integer constant value, if it has one.
5399 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5405 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5408 mpz_clear(left_val);
5413 mpz_init(right_val);
5415 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5418 mpz_clear(right_val);
5419 mpz_clear(left_val);
5424 if (left_type != right_type
5425 && left_type != NULL
5426 && right_type != NULL
5427 && left_type->base() != right_type->base()
5428 && this->op_ != OPERATOR_RSHIFT
5429 && this->op_ != OPERATOR_LSHIFT)
5432 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5433 right_type, right_val,
5434 this->location(), val);
5436 mpz_clear(right_val);
5437 mpz_clear(left_val);
5445 // Return the floating point constant value, if it has one.
5448 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5451 mpfr_init(left_val);
5453 if (!this->left_->float_constant_value(left_val, &left_type))
5455 mpfr_clear(left_val);
5460 mpfr_init(right_val);
5462 if (!this->right_->float_constant_value(right_val, &right_type))
5464 mpfr_clear(right_val);
5465 mpfr_clear(left_val);
5470 if (left_type != right_type
5471 && left_type != NULL
5472 && right_type != NULL
5473 && left_type->base() != right_type->base())
5476 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5477 right_type, right_val,
5478 val, this->location());
5480 mpfr_clear(left_val);
5481 mpfr_clear(right_val);
5489 // Return the complex constant value, if it has one.
5492 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5497 mpfr_init(left_real);
5498 mpfr_init(left_imag);
5500 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5502 mpfr_clear(left_real);
5503 mpfr_clear(left_imag);
5509 mpfr_init(right_real);
5510 mpfr_init(right_imag);
5512 if (!this->right_->complex_constant_value(right_real, right_imag,
5515 mpfr_clear(left_real);
5516 mpfr_clear(left_imag);
5517 mpfr_clear(right_real);
5518 mpfr_clear(right_imag);
5523 if (left_type != right_type
5524 && left_type != NULL
5525 && right_type != NULL
5526 && left_type->base() != right_type->base())
5529 ret = Binary_expression::eval_complex(this->op_, left_type,
5530 left_real, left_imag,
5532 right_real, right_imag,
5535 mpfr_clear(left_real);
5536 mpfr_clear(left_imag);
5537 mpfr_clear(right_real);
5538 mpfr_clear(right_imag);
5546 // Note that the value is being discarded.
5549 Binary_expression::do_discarding_value()
5551 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5552 this->right_->discarding_value();
5554 this->warn_about_unused_value();
5560 Binary_expression::do_type()
5562 if (this->classification() == EXPRESSION_ERROR)
5563 return Type::make_error_type();
5568 case OPERATOR_ANDAND:
5570 case OPERATOR_NOTEQ:
5575 return Type::lookup_bool_type();
5578 case OPERATOR_MINUS:
5585 case OPERATOR_BITCLEAR:
5587 Type* left_type = this->left_->type();
5588 Type* right_type = this->right_->type();
5589 if (left_type->is_error())
5591 else if (right_type->is_error())
5593 else if (!Type::are_compatible_for_binop(left_type, right_type))
5595 this->report_error(_("incompatible types in binary expression"));
5596 return Type::make_error_type();
5598 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
5600 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5602 else if (!left_type->is_abstract())
5604 else if (!right_type->is_abstract())
5606 else if (left_type->complex_type() != NULL)
5608 else if (right_type->complex_type() != NULL)
5610 else if (left_type->float_type() != NULL)
5612 else if (right_type->float_type() != NULL)
5618 case OPERATOR_LSHIFT:
5619 case OPERATOR_RSHIFT:
5620 return this->left_->type();
5627 // Set type for a binary expression.
5630 Binary_expression::do_determine_type(const Type_context* context)
5632 Type* tleft = this->left_->type();
5633 Type* tright = this->right_->type();
5635 // Both sides should have the same type, except for the shift
5636 // operations. For a comparison, we should ignore the incoming
5639 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5640 || this->op_ == OPERATOR_RSHIFT);
5642 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5643 || this->op_ == OPERATOR_NOTEQ
5644 || this->op_ == OPERATOR_LT
5645 || this->op_ == OPERATOR_LE
5646 || this->op_ == OPERATOR_GT
5647 || this->op_ == OPERATOR_GE);
5649 Type_context subcontext(*context);
5653 // In a comparison, the context does not determine the types of
5655 subcontext.type = NULL;
5658 // Set the context for the left hand operand.
5661 // The right hand operand plays no role in determining the type
5662 // of the left hand operand. A shift of an abstract integer in
5663 // a string context gets special treatment, which may be a
5665 if (subcontext.type != NULL
5666 && subcontext.type->is_string_type()
5667 && tleft->is_abstract())
5668 error_at(this->location(), "shift of non-integer operand");
5670 else if (!tleft->is_abstract())
5671 subcontext.type = tleft;
5672 else if (!tright->is_abstract())
5673 subcontext.type = tright;
5674 else if (subcontext.type == NULL)
5676 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5677 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5678 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5680 // Both sides have an abstract integer, abstract float, or
5681 // abstract complex type. Just let CONTEXT determine
5682 // whether they may remain abstract or not.
5684 else if (tleft->complex_type() != NULL)
5685 subcontext.type = tleft;
5686 else if (tright->complex_type() != NULL)
5687 subcontext.type = tright;
5688 else if (tleft->float_type() != NULL)
5689 subcontext.type = tleft;
5690 else if (tright->float_type() != NULL)
5691 subcontext.type = tright;
5693 subcontext.type = tleft;
5695 if (subcontext.type != NULL && !context->may_be_abstract)
5696 subcontext.type = subcontext.type->make_non_abstract_type();
5699 this->left_->determine_type(&subcontext);
5701 // The context for the right hand operand is the same as for the
5702 // left hand operand, except for a shift operator.
5705 subcontext.type = Type::lookup_integer_type("uint");
5706 subcontext.may_be_abstract = false;
5709 this->right_->determine_type(&subcontext);
5712 // Report an error if the binary operator OP does not support TYPE.
5713 // Return whether the operation is OK. This should not be used for
5717 Binary_expression::check_operator_type(Operator op, Type* type,
5718 source_location location)
5723 case OPERATOR_ANDAND:
5724 if (!type->is_boolean_type())
5726 error_at(location, "expected boolean type");
5732 case OPERATOR_NOTEQ:
5733 if (type->integer_type() == NULL
5734 && type->float_type() == NULL
5735 && type->complex_type() == NULL
5736 && !type->is_string_type()
5737 && type->points_to() == NULL
5738 && !type->is_nil_type()
5739 && !type->is_boolean_type()
5740 && type->interface_type() == NULL
5741 && (type->array_type() == NULL
5742 || type->array_type()->length() != NULL)
5743 && type->map_type() == NULL
5744 && type->channel_type() == NULL
5745 && type->function_type() == NULL)
5748 ("expected integer, floating, complex, string, pointer, "
5749 "boolean, interface, slice, map, channel, "
5750 "or function type"));
5759 if (type->integer_type() == NULL
5760 && type->float_type() == NULL
5761 && !type->is_string_type())
5763 error_at(location, "expected integer, floating, or string type");
5769 case OPERATOR_PLUSEQ:
5770 if (type->integer_type() == NULL
5771 && type->float_type() == NULL
5772 && type->complex_type() == NULL
5773 && !type->is_string_type())
5776 "expected integer, floating, complex, or string type");
5781 case OPERATOR_MINUS:
5782 case OPERATOR_MINUSEQ:
5784 case OPERATOR_MULTEQ:
5786 case OPERATOR_DIVEQ:
5787 if (type->integer_type() == NULL
5788 && type->float_type() == NULL
5789 && type->complex_type() == NULL)
5791 error_at(location, "expected integer, floating, or complex type");
5797 case OPERATOR_MODEQ:
5801 case OPERATOR_ANDEQ:
5803 case OPERATOR_XOREQ:
5804 case OPERATOR_BITCLEAR:
5805 case OPERATOR_BITCLEAREQ:
5806 if (type->integer_type() == NULL)
5808 error_at(location, "expected integer type");
5823 Binary_expression::do_check_types(Gogo*)
5825 if (this->classification() == EXPRESSION_ERROR)
5828 Type* left_type = this->left_->type();
5829 Type* right_type = this->right_->type();
5830 if (left_type->is_error() || right_type->is_error())
5832 this->set_is_error();
5836 if (this->op_ == OPERATOR_EQEQ
5837 || this->op_ == OPERATOR_NOTEQ
5838 || this->op_ == OPERATOR_LT
5839 || this->op_ == OPERATOR_LE
5840 || this->op_ == OPERATOR_GT
5841 || this->op_ == OPERATOR_GE)
5843 if (!Type::are_assignable(left_type, right_type, NULL)
5844 && !Type::are_assignable(right_type, left_type, NULL))
5846 this->report_error(_("incompatible types in binary expression"));
5849 if (!Binary_expression::check_operator_type(this->op_, left_type,
5851 || !Binary_expression::check_operator_type(this->op_, right_type,
5854 this->set_is_error();
5858 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5860 if (!Type::are_compatible_for_binop(left_type, right_type))
5862 this->report_error(_("incompatible types in binary expression"));
5865 if (!Binary_expression::check_operator_type(this->op_, left_type,
5868 this->set_is_error();
5874 if (left_type->integer_type() == NULL)
5875 this->report_error(_("shift of non-integer operand"));
5877 if (!right_type->is_abstract()
5878 && (right_type->integer_type() == NULL
5879 || !right_type->integer_type()->is_unsigned()))
5880 this->report_error(_("shift count not unsigned integer"));
5886 if (this->right_->integer_constant_value(true, val, &type))
5888 if (mpz_sgn(val) < 0)
5890 this->report_error(_("negative shift count"));
5892 source_location rloc = this->right_->location();
5893 this->right_ = Expression::make_integer(&val, right_type,
5902 // Get a tree for a binary expression.
5905 Binary_expression::do_get_tree(Translate_context* context)
5907 tree left = this->left_->get_tree(context);
5908 tree right = this->right_->get_tree(context);
5910 if (left == error_mark_node || right == error_mark_node)
5911 return error_mark_node;
5913 enum tree_code code;
5914 bool use_left_type = true;
5915 bool is_shift_op = false;
5919 case OPERATOR_NOTEQ:
5924 return Expression::comparison_tree(context, this->op_,
5925 this->left_->type(), left,
5926 this->right_->type(), right,
5930 code = TRUTH_ORIF_EXPR;
5931 use_left_type = false;
5933 case OPERATOR_ANDAND:
5934 code = TRUTH_ANDIF_EXPR;
5935 use_left_type = false;
5940 case OPERATOR_MINUS:
5944 code = BIT_IOR_EXPR;
5947 code = BIT_XOR_EXPR;
5954 Type *t = this->left_->type();
5955 if (t->float_type() != NULL || t->complex_type() != NULL)
5958 code = TRUNC_DIV_EXPR;
5962 code = TRUNC_MOD_EXPR;
5964 case OPERATOR_LSHIFT:
5968 case OPERATOR_RSHIFT:
5973 code = BIT_AND_EXPR;
5975 case OPERATOR_BITCLEAR:
5976 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5977 code = BIT_AND_EXPR;
5983 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5985 if (this->left_->type()->is_string_type())
5987 gcc_assert(this->op_ == OPERATOR_PLUS);
5988 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5989 static tree string_plus_decl;
5990 return Gogo::call_builtin(&string_plus_decl,
6001 tree compute_type = excess_precision_type(type);
6002 if (compute_type != NULL_TREE)
6004 left = ::convert(compute_type, left);
6005 right = ::convert(compute_type, right);
6008 tree eval_saved = NULL_TREE;
6011 // Make sure the values are evaluated.
6012 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
6014 left = save_expr(left);
6017 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
6019 right = save_expr(right);
6020 if (eval_saved == NULL_TREE)
6023 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
6024 void_type_node, eval_saved, right);
6028 tree ret = fold_build2_loc(this->location(),
6030 compute_type != NULL_TREE ? compute_type : type,
6033 if (compute_type != NULL_TREE)
6034 ret = ::convert(type, ret);
6036 // In Go, a shift larger than the size of the type is well-defined.
6037 // This is not true in GENERIC, so we need to insert a conditional.
6040 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
6041 gcc_assert(this->left_->type()->integer_type() != NULL);
6042 int bits = TYPE_PRECISION(TREE_TYPE(left));
6044 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
6045 build_int_cst_type(TREE_TYPE(right), bits));
6047 tree overflow_result = fold_convert_loc(this->location(),
6050 if (this->op_ == OPERATOR_RSHIFT
6051 && !this->left_->type()->integer_type()->is_unsigned())
6053 tree neg = fold_build2_loc(this->location(), LT_EXPR,
6054 boolean_type_node, left,
6055 fold_convert_loc(this->location(),
6057 integer_zero_node));
6058 tree neg_one = fold_build2_loc(this->location(),
6059 MINUS_EXPR, TREE_TYPE(left),
6060 fold_convert_loc(this->location(),
6063 fold_convert_loc(this->location(),
6066 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
6067 TREE_TYPE(left), neg, neg_one,
6071 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
6072 compare, ret, overflow_result);
6074 if (eval_saved != NULL_TREE)
6075 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
6076 TREE_TYPE(ret), eval_saved, ret);
6082 // Export a binary expression.
6085 Binary_expression::do_export(Export* exp) const
6087 exp->write_c_string("(");
6088 this->left_->export_expression(exp);
6092 exp->write_c_string(" || ");
6094 case OPERATOR_ANDAND:
6095 exp->write_c_string(" && ");
6098 exp->write_c_string(" == ");
6100 case OPERATOR_NOTEQ:
6101 exp->write_c_string(" != ");
6104 exp->write_c_string(" < ");
6107 exp->write_c_string(" <= ");
6110 exp->write_c_string(" > ");
6113 exp->write_c_string(" >= ");
6116 exp->write_c_string(" + ");
6118 case OPERATOR_MINUS:
6119 exp->write_c_string(" - ");
6122 exp->write_c_string(" | ");
6125 exp->write_c_string(" ^ ");
6128 exp->write_c_string(" * ");
6131 exp->write_c_string(" / ");
6134 exp->write_c_string(" % ");
6136 case OPERATOR_LSHIFT:
6137 exp->write_c_string(" << ");
6139 case OPERATOR_RSHIFT:
6140 exp->write_c_string(" >> ");
6143 exp->write_c_string(" & ");
6145 case OPERATOR_BITCLEAR:
6146 exp->write_c_string(" &^ ");
6151 this->right_->export_expression(exp);
6152 exp->write_c_string(")");
6155 // Import a binary expression.
6158 Binary_expression::do_import(Import* imp)
6160 imp->require_c_string("(");
6162 Expression* left = Expression::import_expression(imp);
6165 if (imp->match_c_string(" || "))
6170 else if (imp->match_c_string(" && "))
6172 op = OPERATOR_ANDAND;
6175 else if (imp->match_c_string(" == "))
6180 else if (imp->match_c_string(" != "))
6182 op = OPERATOR_NOTEQ;
6185 else if (imp->match_c_string(" < "))
6190 else if (imp->match_c_string(" <= "))
6195 else if (imp->match_c_string(" > "))
6200 else if (imp->match_c_string(" >= "))
6205 else if (imp->match_c_string(" + "))
6210 else if (imp->match_c_string(" - "))
6212 op = OPERATOR_MINUS;
6215 else if (imp->match_c_string(" | "))
6220 else if (imp->match_c_string(" ^ "))
6225 else if (imp->match_c_string(" * "))
6230 else if (imp->match_c_string(" / "))
6235 else if (imp->match_c_string(" % "))
6240 else if (imp->match_c_string(" << "))
6242 op = OPERATOR_LSHIFT;
6245 else if (imp->match_c_string(" >> "))
6247 op = OPERATOR_RSHIFT;
6250 else if (imp->match_c_string(" & "))
6255 else if (imp->match_c_string(" &^ "))
6257 op = OPERATOR_BITCLEAR;
6262 error_at(imp->location(), "unrecognized binary operator");
6263 return Expression::make_error(imp->location());
6266 Expression* right = Expression::import_expression(imp);
6268 imp->require_c_string(")");
6270 return Expression::make_binary(op, left, right, imp->location());
6273 // Make a binary expression.
6276 Expression::make_binary(Operator op, Expression* left, Expression* right,
6277 source_location location)
6279 return new Binary_expression(op, left, right, location);
6282 // Implement a comparison.
6285 Expression::comparison_tree(Translate_context* context, Operator op,
6286 Type* left_type, tree left_tree,
6287 Type* right_type, tree right_tree,
6288 source_location location)
6290 enum tree_code code;
6296 case OPERATOR_NOTEQ:
6315 if (left_type->is_string_type() && right_type->is_string_type())
6317 tree string_type = Type::make_string_type()->get_tree(context->gogo());
6318 static tree string_compare_decl;
6319 left_tree = Gogo::call_builtin(&string_compare_decl,
6328 right_tree = build_int_cst_type(integer_type_node, 0);
6330 else if ((left_type->interface_type() != NULL
6331 && right_type->interface_type() == NULL
6332 && !right_type->is_nil_type())
6333 || (left_type->interface_type() == NULL
6334 && !left_type->is_nil_type()
6335 && right_type->interface_type() != NULL))
6337 // Comparing an interface value to a non-interface value.
6338 if (left_type->interface_type() == NULL)
6340 std::swap(left_type, right_type);
6341 std::swap(left_tree, right_tree);
6344 // The right operand is not an interface. We need to take its
6345 // address if it is not a pointer.
6348 if (right_type->points_to() != NULL)
6350 make_tmp = NULL_TREE;
6353 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
6355 make_tmp = NULL_TREE;
6356 arg = build_fold_addr_expr_loc(location, right_tree);
6357 if (DECL_P(right_tree))
6358 TREE_ADDRESSABLE(right_tree) = 1;
6362 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6363 get_name(right_tree));
6364 DECL_IGNORED_P(tmp) = 0;
6365 DECL_INITIAL(tmp) = right_tree;
6366 TREE_ADDRESSABLE(tmp) = 1;
6367 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6368 SET_EXPR_LOCATION(make_tmp, location);
6369 arg = build_fold_addr_expr_loc(location, tmp);
6371 arg = fold_convert_loc(location, ptr_type_node, arg);
6373 tree descriptor = right_type->type_descriptor_pointer(context->gogo());
6375 if (left_type->interface_type()->is_empty())
6377 static tree empty_interface_value_compare_decl;
6378 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6380 "__go_empty_interface_value_compare",
6383 TREE_TYPE(left_tree),
6385 TREE_TYPE(descriptor),
6389 if (left_tree == error_mark_node)
6390 return error_mark_node;
6391 // This can panic if the type is not comparable.
6392 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6396 static tree interface_value_compare_decl;
6397 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6399 "__go_interface_value_compare",
6402 TREE_TYPE(left_tree),
6404 TREE_TYPE(descriptor),
6408 if (left_tree == error_mark_node)
6409 return error_mark_node;
6410 // This can panic if the type is not comparable.
6411 TREE_NOTHROW(interface_value_compare_decl) = 0;
6413 right_tree = build_int_cst_type(integer_type_node, 0);
6415 if (make_tmp != NULL_TREE)
6416 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6419 else if (left_type->interface_type() != NULL
6420 && right_type->interface_type() != NULL)
6422 if (left_type->interface_type()->is_empty()
6423 && right_type->interface_type()->is_empty())
6425 static tree empty_interface_compare_decl;
6426 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6428 "__go_empty_interface_compare",
6431 TREE_TYPE(left_tree),
6433 TREE_TYPE(right_tree),
6435 if (left_tree == error_mark_node)
6436 return error_mark_node;
6437 // This can panic if the type is uncomparable.
6438 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6440 else if (!left_type->interface_type()->is_empty()
6441 && !right_type->interface_type()->is_empty())
6443 static tree interface_compare_decl;
6444 left_tree = Gogo::call_builtin(&interface_compare_decl,
6446 "__go_interface_compare",
6449 TREE_TYPE(left_tree),
6451 TREE_TYPE(right_tree),
6453 if (left_tree == error_mark_node)
6454 return error_mark_node;
6455 // This can panic if the type is uncomparable.
6456 TREE_NOTHROW(interface_compare_decl) = 0;
6460 if (left_type->interface_type()->is_empty())
6462 gcc_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6463 std::swap(left_type, right_type);
6464 std::swap(left_tree, right_tree);
6466 gcc_assert(!left_type->interface_type()->is_empty());
6467 gcc_assert(right_type->interface_type()->is_empty());
6468 static tree interface_empty_compare_decl;
6469 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
6471 "__go_interface_empty_compare",
6474 TREE_TYPE(left_tree),
6476 TREE_TYPE(right_tree),
6478 if (left_tree == error_mark_node)
6479 return error_mark_node;
6480 // This can panic if the type is uncomparable.
6481 TREE_NOTHROW(interface_empty_compare_decl) = 0;
6484 right_tree = build_int_cst_type(integer_type_node, 0);
6487 if (left_type->is_nil_type()
6488 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6490 std::swap(left_type, right_type);
6491 std::swap(left_tree, right_tree);
6494 if (right_type->is_nil_type())
6496 if (left_type->array_type() != NULL
6497 && left_type->array_type()->length() == NULL)
6499 Array_type* at = left_type->array_type();
6500 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6501 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6503 else if (left_type->interface_type() != NULL)
6505 // An interface is nil if the first field is nil.
6506 tree left_type_tree = TREE_TYPE(left_tree);
6507 gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6508 tree field = TYPE_FIELDS(left_type_tree);
6509 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6511 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6515 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6516 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6520 if (left_tree == error_mark_node || right_tree == error_mark_node)
6521 return error_mark_node;
6523 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6524 if (CAN_HAVE_LOCATION_P(ret))
6525 SET_EXPR_LOCATION(ret, location);
6529 // Class Bound_method_expression.
6534 Bound_method_expression::do_traverse(Traverse* traverse)
6536 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
6537 return TRAVERSE_EXIT;
6538 return Expression::traverse(&this->method_, traverse);
6541 // Return the type of a bound method expression. The type of this
6542 // object is really the type of the method with no receiver. We
6543 // should be able to get away with just returning the type of the
6547 Bound_method_expression::do_type()
6549 return this->method_->type();
6552 // Determine the types of a method expression.
6555 Bound_method_expression::do_determine_type(const Type_context*)
6557 this->method_->determine_type_no_context();
6558 Type* mtype = this->method_->type();
6559 Function_type* fntype = mtype == NULL ? NULL : mtype->function_type();
6560 if (fntype == NULL || !fntype->is_method())
6561 this->expr_->determine_type_no_context();
6564 Type_context subcontext(fntype->receiver()->type(), false);
6565 this->expr_->determine_type(&subcontext);
6569 // Check the types of a method expression.
6572 Bound_method_expression::do_check_types(Gogo*)
6574 Type* type = this->method_->type()->deref();
6576 || type->function_type() == NULL
6577 || !type->function_type()->is_method())
6578 this->report_error(_("object is not a method"));
6581 Type* rtype = type->function_type()->receiver()->type()->deref();
6582 Type* etype = (this->expr_type_ != NULL
6584 : this->expr_->type());
6585 etype = etype->deref();
6586 if (!Type::are_identical(rtype, etype, true, NULL))
6587 this->report_error(_("method type does not match object type"));
6591 // Get the tree for a method expression. There is no standard tree
6592 // representation for this. The only places it may currently be used
6593 // are in a Call_expression or a Go_statement, which will take it
6594 // apart directly. So this has nothing to do at present.
6597 Bound_method_expression::do_get_tree(Translate_context*)
6599 error_at(this->location(), "reference to method other than calling it");
6600 return error_mark_node;
6603 // Make a method expression.
6605 Bound_method_expression*
6606 Expression::make_bound_method(Expression* expr, Expression* method,
6607 source_location location)
6609 return new Bound_method_expression(expr, method, location);
6612 // Class Builtin_call_expression. This is used for a call to a
6613 // builtin function.
6615 class Builtin_call_expression : public Call_expression
6618 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6619 bool is_varargs, source_location location);
6622 // This overrides Call_expression::do_lower.
6624 do_lower(Gogo*, Named_object*, int);
6627 do_is_constant() const;
6630 do_integer_constant_value(bool, mpz_t, Type**) const;
6633 do_float_constant_value(mpfr_t, Type**) const;
6636 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6642 do_determine_type(const Type_context*);
6645 do_check_types(Gogo*);
6650 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6651 this->args()->copy(),
6657 do_get_tree(Translate_context*);
6660 do_export(Export*) const;
6663 do_is_recover_call() const;
6666 do_set_recover_arg(Expression*);
6669 // The builtin functions.
6670 enum Builtin_function_code
6674 // Predeclared builtin functions.
6690 // Builtin functions from the unsafe package.
6703 real_imag_type(Type*);
6706 complex_type(Type*);
6708 // A pointer back to the general IR structure. This avoids a global
6709 // variable, or passing it around everywhere.
6711 // The builtin function being called.
6712 Builtin_function_code code_;
6713 // Used to stop endless loops when the length of an array uses len
6714 // or cap of the array itself.
6718 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6720 Expression_list* args,
6722 source_location location)
6723 : Call_expression(fn, args, is_varargs, location),
6724 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
6726 Func_expression* fnexp = this->fn()->func_expression();
6727 gcc_assert(fnexp != NULL);
6728 const std::string& name(fnexp->named_object()->name());
6729 if (name == "append")
6730 this->code_ = BUILTIN_APPEND;
6731 else if (name == "cap")
6732 this->code_ = BUILTIN_CAP;
6733 else if (name == "close")
6734 this->code_ = BUILTIN_CLOSE;
6735 else if (name == "complex")
6736 this->code_ = BUILTIN_COMPLEX;
6737 else if (name == "copy")
6738 this->code_ = BUILTIN_COPY;
6739 else if (name == "imag")
6740 this->code_ = BUILTIN_IMAG;
6741 else if (name == "len")
6742 this->code_ = BUILTIN_LEN;
6743 else if (name == "make")
6744 this->code_ = BUILTIN_MAKE;
6745 else if (name == "new")
6746 this->code_ = BUILTIN_NEW;
6747 else if (name == "panic")
6748 this->code_ = BUILTIN_PANIC;
6749 else if (name == "print")
6750 this->code_ = BUILTIN_PRINT;
6751 else if (name == "println")
6752 this->code_ = BUILTIN_PRINTLN;
6753 else if (name == "real")
6754 this->code_ = BUILTIN_REAL;
6755 else if (name == "recover")
6756 this->code_ = BUILTIN_RECOVER;
6757 else if (name == "Alignof")
6758 this->code_ = BUILTIN_ALIGNOF;
6759 else if (name == "Offsetof")
6760 this->code_ = BUILTIN_OFFSETOF;
6761 else if (name == "Sizeof")
6762 this->code_ = BUILTIN_SIZEOF;
6767 // Return whether this is a call to recover. This is a virtual
6768 // function called from the parent class.
6771 Builtin_call_expression::do_is_recover_call() const
6773 if (this->classification() == EXPRESSION_ERROR)
6775 return this->code_ == BUILTIN_RECOVER;
6778 // Set the argument for a call to recover.
6781 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6783 const Expression_list* args = this->args();
6784 gcc_assert(args == NULL || args->empty());
6785 Expression_list* new_args = new Expression_list();
6786 new_args->push_back(arg);
6787 this->set_args(new_args);
6790 // A traversal class which looks for a call expression.
6792 class Find_call_expression : public Traverse
6795 Find_call_expression()
6796 : Traverse(traverse_expressions),
6801 expression(Expression**);
6805 { return this->found_; }
6812 Find_call_expression::expression(Expression** pexpr)
6814 if ((*pexpr)->call_expression() != NULL)
6816 this->found_ = true;
6817 return TRAVERSE_EXIT;
6819 return TRAVERSE_CONTINUE;
6822 // Lower a builtin call expression. This turns new and make into
6823 // specific expressions. We also convert to a constant if we can.
6826 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int)
6828 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
6830 this->report_error(_("invalid use of %<...%> with builtin function"));
6831 return Expression::make_error(this->location());
6834 if (this->code_ == BUILTIN_NEW)
6836 const Expression_list* args = this->args();
6837 if (args == NULL || args->size() < 1)
6838 this->report_error(_("not enough arguments"));
6839 else if (args->size() > 1)
6840 this->report_error(_("too many arguments"));
6843 Expression* arg = args->front();
6844 if (!arg->is_type_expression())
6846 error_at(arg->location(), "expected type");
6847 this->set_is_error();
6850 return Expression::make_allocation(arg->type(), this->location());
6853 else if (this->code_ == BUILTIN_MAKE)
6855 const Expression_list* args = this->args();
6856 if (args == NULL || args->size() < 1)
6857 this->report_error(_("not enough arguments"));
6860 Expression* arg = args->front();
6861 if (!arg->is_type_expression())
6863 error_at(arg->location(), "expected type");
6864 this->set_is_error();
6868 Expression_list* newargs;
6869 if (args->size() == 1)
6873 newargs = new Expression_list();
6874 Expression_list::const_iterator p = args->begin();
6876 for (; p != args->end(); ++p)
6877 newargs->push_back(*p);
6879 return Expression::make_make(arg->type(), newargs,
6884 else if (this->is_constant())
6886 // We can only lower len and cap if there are no function calls
6887 // in the arguments. Otherwise we have to make the call.
6888 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6890 Expression* arg = this->one_arg();
6891 if (!arg->is_constant())
6893 Find_call_expression find_call;
6894 Expression::traverse(&arg, &find_call);
6895 if (find_call.found())
6903 if (this->integer_constant_value(true, ival, &type))
6905 Expression* ret = Expression::make_integer(&ival, type,
6914 if (this->float_constant_value(rval, &type))
6916 Expression* ret = Expression::make_float(&rval, type,
6924 if (this->complex_constant_value(rval, imag, &type))
6926 Expression* ret = Expression::make_complex(&rval, &imag, type,
6935 else if (this->code_ == BUILTIN_RECOVER)
6937 if (function != NULL)
6938 function->func_value()->set_calls_recover();
6941 // Calling recover outside of a function always returns the
6942 // nil empty interface.
6943 Type* eface = Type::make_interface_type(NULL, this->location());
6944 return Expression::make_cast(eface,
6945 Expression::make_nil(this->location()),
6949 else if (this->code_ == BUILTIN_APPEND)
6951 // Lower the varargs.
6952 const Expression_list* args = this->args();
6953 if (args == NULL || args->empty())
6955 Type* slice_type = args->front()->type();
6956 if (!slice_type->is_open_array_type())
6958 error_at(args->front()->location(), "argument 1 must be a slice");
6959 this->set_is_error();
6962 return this->lower_varargs(gogo, function, slice_type, 2);
6968 // Return the type of the real or imag functions, given the type of
6969 // the argument. We need to map complex to float, complex64 to
6970 // float32, and complex128 to float64, so it has to be done by name.
6971 // This returns NULL if it can't figure out the type.
6974 Builtin_call_expression::real_imag_type(Type* arg_type)
6976 if (arg_type == NULL || arg_type->is_abstract())
6978 Named_type* nt = arg_type->named_type();
6981 while (nt->real_type()->named_type() != NULL)
6982 nt = nt->real_type()->named_type();
6983 if (nt->name() == "complex64")
6984 return Type::lookup_float_type("float32");
6985 else if (nt->name() == "complex128")
6986 return Type::lookup_float_type("float64");
6991 // Return the type of the complex function, given the type of one of the
6992 // argments. Like real_imag_type, we have to map by name.
6995 Builtin_call_expression::complex_type(Type* arg_type)
6997 if (arg_type == NULL || arg_type->is_abstract())
6999 Named_type* nt = arg_type->named_type();
7002 while (nt->real_type()->named_type() != NULL)
7003 nt = nt->real_type()->named_type();
7004 if (nt->name() == "float32")
7005 return Type::lookup_complex_type("complex64");
7006 else if (nt->name() == "float64")
7007 return Type::lookup_complex_type("complex128");
7012 // Return a single argument, or NULL if there isn't one.
7015 Builtin_call_expression::one_arg() const
7017 const Expression_list* args = this->args();
7018 if (args->size() != 1)
7020 return args->front();
7023 // Return whether this is constant: len of a string, or len or cap of
7024 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
7027 Builtin_call_expression::do_is_constant() const
7029 switch (this->code_)
7037 Expression* arg = this->one_arg();
7040 Type* arg_type = arg->type();
7042 if (arg_type->points_to() != NULL
7043 && arg_type->points_to()->array_type() != NULL
7044 && !arg_type->points_to()->is_open_array_type())
7045 arg_type = arg_type->points_to();
7047 if (arg_type->array_type() != NULL
7048 && arg_type->array_type()->length() != NULL)
7051 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7054 bool ret = arg->is_constant();
7055 this->seen_ = false;
7061 case BUILTIN_SIZEOF:
7062 case BUILTIN_ALIGNOF:
7063 return this->one_arg() != NULL;
7065 case BUILTIN_OFFSETOF:
7067 Expression* arg = this->one_arg();
7070 return arg->field_reference_expression() != NULL;
7073 case BUILTIN_COMPLEX:
7075 const Expression_list* args = this->args();
7076 if (args != NULL && args->size() == 2)
7077 return args->front()->is_constant() && args->back()->is_constant();
7084 Expression* arg = this->one_arg();
7085 return arg != NULL && arg->is_constant();
7095 // Return an integer constant value if possible.
7098 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
7102 if (this->code_ == BUILTIN_LEN
7103 || this->code_ == BUILTIN_CAP)
7105 Expression* arg = this->one_arg();
7108 Type* arg_type = arg->type();
7110 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7113 if (arg->string_constant_value(&sval))
7115 mpz_set_ui(val, sval.length());
7116 *ptype = Type::lookup_integer_type("int");
7121 if (arg_type->points_to() != NULL
7122 && arg_type->points_to()->array_type() != NULL
7123 && !arg_type->points_to()->is_open_array_type())
7124 arg_type = arg_type->points_to();
7126 if (arg_type->array_type() != NULL
7127 && arg_type->array_type()->length() != NULL)
7131 Expression* e = arg_type->array_type()->length();
7133 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
7134 this->seen_ = false;
7137 *ptype = Type::lookup_integer_type("int");
7142 else if (this->code_ == BUILTIN_SIZEOF
7143 || this->code_ == BUILTIN_ALIGNOF)
7145 Expression* arg = this->one_arg();
7148 Type* arg_type = arg->type();
7149 if (arg_type->is_error())
7151 if (arg_type->is_abstract())
7153 if (arg_type->named_type() != NULL)
7154 arg_type->named_type()->convert(this->gogo_);
7155 tree arg_type_tree = arg_type->get_tree(this->gogo_);
7156 if (arg_type_tree == error_mark_node)
7158 unsigned long val_long;
7159 if (this->code_ == BUILTIN_SIZEOF)
7161 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
7162 gcc_assert(TREE_CODE(type_size) == INTEGER_CST);
7163 if (TREE_INT_CST_HIGH(type_size) != 0)
7165 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
7166 val_long = static_cast<unsigned long>(val_wide);
7167 if (val_long != val_wide)
7170 else if (this->code_ == BUILTIN_ALIGNOF)
7172 if (arg->field_reference_expression() == NULL)
7173 val_long = go_type_alignment(arg_type_tree);
7176 // Calling unsafe.Alignof(s.f) returns the alignment of
7177 // the type of f when it is used as a field in a struct.
7178 val_long = go_field_alignment(arg_type_tree);
7183 mpz_set_ui(val, val_long);
7187 else if (this->code_ == BUILTIN_OFFSETOF)
7189 Expression* arg = this->one_arg();
7192 Field_reference_expression* farg = arg->field_reference_expression();
7195 Expression* struct_expr = farg->expr();
7196 Type* st = struct_expr->type();
7197 if (st->struct_type() == NULL)
7199 if (st->named_type() != NULL)
7200 st->named_type()->convert(this->gogo_);
7201 tree struct_tree = st->get_tree(this->gogo_);
7202 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
7203 tree field = TYPE_FIELDS(struct_tree);
7204 for (unsigned int index = farg->field_index(); index > 0; --index)
7206 field = DECL_CHAIN(field);
7207 gcc_assert(field != NULL_TREE);
7209 HOST_WIDE_INT offset_wide = int_byte_position (field);
7210 if (offset_wide < 0)
7212 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
7213 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
7215 mpz_set_ui(val, offset_long);
7221 // Return a floating point constant value if possible.
7224 Builtin_call_expression::do_float_constant_value(mpfr_t val,
7227 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
7229 Expression* arg = this->one_arg();
7240 if (arg->complex_constant_value(real, imag, &type))
7242 if (this->code_ == BUILTIN_REAL)
7243 mpfr_set(val, real, GMP_RNDN);
7245 mpfr_set(val, imag, GMP_RNDN);
7246 *ptype = Builtin_call_expression::real_imag_type(type);
7258 // Return a complex constant value if possible.
7261 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
7264 if (this->code_ == BUILTIN_COMPLEX)
7266 const Expression_list* args = this->args();
7267 if (args == NULL || args->size() != 2)
7273 if (!args->front()->float_constant_value(r, &rtype))
7284 if (args->back()->float_constant_value(i, &itype)
7285 && Type::are_identical(rtype, itype, false, NULL))
7287 mpfr_set(real, r, GMP_RNDN);
7288 mpfr_set(imag, i, GMP_RNDN);
7289 *ptype = Builtin_call_expression::complex_type(rtype);
7305 Builtin_call_expression::do_type()
7307 switch (this->code_)
7309 case BUILTIN_INVALID:
7316 const Expression_list* args = this->args();
7317 if (args == NULL || args->empty())
7318 return Type::make_error_type();
7319 return Type::make_pointer_type(args->front()->type());
7325 case BUILTIN_ALIGNOF:
7326 case BUILTIN_OFFSETOF:
7327 case BUILTIN_SIZEOF:
7328 return Type::lookup_integer_type("int");
7333 case BUILTIN_PRINTLN:
7334 return Type::make_void_type();
7336 case BUILTIN_RECOVER:
7337 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
7339 case BUILTIN_APPEND:
7341 const Expression_list* args = this->args();
7342 if (args == NULL || args->empty())
7343 return Type::make_error_type();
7344 return args->front()->type();
7350 Expression* arg = this->one_arg();
7352 return Type::make_error_type();
7353 Type* t = arg->type();
7354 if (t->is_abstract())
7355 t = t->make_non_abstract_type();
7356 t = Builtin_call_expression::real_imag_type(t);
7358 t = Type::make_error_type();
7362 case BUILTIN_COMPLEX:
7364 const Expression_list* args = this->args();
7365 if (args == NULL || args->size() != 2)
7366 return Type::make_error_type();
7367 Type* t = args->front()->type();
7368 if (t->is_abstract())
7370 t = args->back()->type();
7371 if (t->is_abstract())
7372 t = t->make_non_abstract_type();
7374 t = Builtin_call_expression::complex_type(t);
7376 t = Type::make_error_type();
7382 // Determine the type.
7385 Builtin_call_expression::do_determine_type(const Type_context* context)
7387 if (!this->determining_types())
7390 this->fn()->determine_type_no_context();
7392 const Expression_list* args = this->args();
7395 Type* arg_type = NULL;
7396 switch (this->code_)
7399 case BUILTIN_PRINTLN:
7400 // Do not force a large integer constant to "int".
7406 arg_type = Builtin_call_expression::complex_type(context->type);
7410 case BUILTIN_COMPLEX:
7412 // For the complex function the type of one operand can
7413 // determine the type of the other, as in a binary expression.
7414 arg_type = Builtin_call_expression::real_imag_type(context->type);
7415 if (args != NULL && args->size() == 2)
7417 Type* t1 = args->front()->type();
7418 Type* t2 = args->front()->type();
7419 if (!t1->is_abstract())
7421 else if (!t2->is_abstract())
7435 for (Expression_list::const_iterator pa = args->begin();
7439 Type_context subcontext;
7440 subcontext.type = arg_type;
7444 // We want to print large constants, we so can't just
7445 // use the appropriate nonabstract type. Use uint64 for
7446 // an integer if we know it is nonnegative, otherwise
7447 // use int64 for a integer, otherwise use float64 for a
7448 // float or complex128 for a complex.
7449 Type* want_type = NULL;
7450 Type* atype = (*pa)->type();
7451 if (atype->is_abstract())
7453 if (atype->integer_type() != NULL)
7458 if (this->integer_constant_value(true, val, &dummy)
7459 && mpz_sgn(val) >= 0)
7460 want_type = Type::lookup_integer_type("uint64");
7462 want_type = Type::lookup_integer_type("int64");
7465 else if (atype->float_type() != NULL)
7466 want_type = Type::lookup_float_type("float64");
7467 else if (atype->complex_type() != NULL)
7468 want_type = Type::lookup_complex_type("complex128");
7469 else if (atype->is_abstract_string_type())
7470 want_type = Type::lookup_string_type();
7471 else if (atype->is_abstract_boolean_type())
7472 want_type = Type::lookup_bool_type();
7475 subcontext.type = want_type;
7479 (*pa)->determine_type(&subcontext);
7484 // If there is exactly one argument, return true. Otherwise give an
7485 // error message and return false.
7488 Builtin_call_expression::check_one_arg()
7490 const Expression_list* args = this->args();
7491 if (args == NULL || args->size() < 1)
7493 this->report_error(_("not enough arguments"));
7496 else if (args->size() > 1)
7498 this->report_error(_("too many arguments"));
7501 if (args->front()->is_error_expression()
7502 || args->front()->type()->is_error())
7504 this->set_is_error();
7510 // Check argument types for a builtin function.
7513 Builtin_call_expression::do_check_types(Gogo*)
7515 switch (this->code_)
7517 case BUILTIN_INVALID:
7525 // The single argument may be either a string or an array or a
7526 // map or a channel, or a pointer to a closed array.
7527 if (this->check_one_arg())
7529 Type* arg_type = this->one_arg()->type();
7530 if (arg_type->points_to() != NULL
7531 && arg_type->points_to()->array_type() != NULL
7532 && !arg_type->points_to()->is_open_array_type())
7533 arg_type = arg_type->points_to();
7534 if (this->code_ == BUILTIN_CAP)
7536 if (!arg_type->is_error()
7537 && arg_type->array_type() == NULL
7538 && arg_type->channel_type() == NULL)
7539 this->report_error(_("argument must be array or slice "
7544 if (!arg_type->is_error()
7545 && !arg_type->is_string_type()
7546 && arg_type->array_type() == NULL
7547 && arg_type->map_type() == NULL
7548 && arg_type->channel_type() == NULL)
7549 this->report_error(_("argument must be string or "
7550 "array or slice or map or channel"));
7557 case BUILTIN_PRINTLN:
7559 const Expression_list* args = this->args();
7562 if (this->code_ == BUILTIN_PRINT)
7563 warning_at(this->location(), 0,
7564 "no arguments for builtin function %<%s%>",
7565 (this->code_ == BUILTIN_PRINT
7571 for (Expression_list::const_iterator p = args->begin();
7575 Type* type = (*p)->type();
7576 if (type->is_error()
7577 || type->is_string_type()
7578 || type->integer_type() != NULL
7579 || type->float_type() != NULL
7580 || type->complex_type() != NULL
7581 || type->is_boolean_type()
7582 || type->points_to() != NULL
7583 || type->interface_type() != NULL
7584 || type->channel_type() != NULL
7585 || type->map_type() != NULL
7586 || type->function_type() != NULL
7587 || type->is_open_array_type())
7590 this->report_error(_("unsupported argument type to "
7591 "builtin function"));
7598 if (this->check_one_arg())
7600 if (this->one_arg()->type()->channel_type() == NULL)
7601 this->report_error(_("argument must be channel"));
7606 case BUILTIN_SIZEOF:
7607 case BUILTIN_ALIGNOF:
7608 this->check_one_arg();
7611 case BUILTIN_RECOVER:
7612 if (this->args() != NULL && !this->args()->empty())
7613 this->report_error(_("too many arguments"));
7616 case BUILTIN_OFFSETOF:
7617 if (this->check_one_arg())
7619 Expression* arg = this->one_arg();
7620 if (arg->field_reference_expression() == NULL)
7621 this->report_error(_("argument must be a field reference"));
7627 const Expression_list* args = this->args();
7628 if (args == NULL || args->size() < 2)
7630 this->report_error(_("not enough arguments"));
7633 else if (args->size() > 2)
7635 this->report_error(_("too many arguments"));
7638 Type* arg1_type = args->front()->type();
7639 Type* arg2_type = args->back()->type();
7640 if (arg1_type->is_error() || arg2_type->is_error())
7644 if (arg1_type->is_open_array_type())
7645 e1 = arg1_type->array_type()->element_type();
7648 this->report_error(_("left argument must be a slice"));
7653 if (arg2_type->is_open_array_type())
7654 e2 = arg2_type->array_type()->element_type();
7655 else if (arg2_type->is_string_type())
7656 e2 = Type::lookup_integer_type("uint8");
7659 this->report_error(_("right argument must be a slice or a string"));
7663 if (!Type::are_identical(e1, e2, true, NULL))
7664 this->report_error(_("element types must be the same"));
7668 case BUILTIN_APPEND:
7670 const Expression_list* args = this->args();
7671 if (args == NULL || args->size() < 2)
7673 this->report_error(_("not enough arguments"));
7676 if (args->size() > 2)
7678 this->report_error(_("too many arguments"));
7682 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
7686 this->report_error(_("arguments 1 and 2 have different types"));
7689 error_at(this->location(),
7690 "arguments 1 and 2 have different types (%s)",
7692 this->set_is_error();
7700 if (this->check_one_arg())
7702 if (this->one_arg()->type()->complex_type() == NULL)
7703 this->report_error(_("argument must have complex type"));
7707 case BUILTIN_COMPLEX:
7709 const Expression_list* args = this->args();
7710 if (args == NULL || args->size() < 2)
7711 this->report_error(_("not enough arguments"));
7712 else if (args->size() > 2)
7713 this->report_error(_("too many arguments"));
7714 else if (args->front()->is_error_expression()
7715 || args->front()->type()->is_error()
7716 || args->back()->is_error_expression()
7717 || args->back()->type()->is_error())
7718 this->set_is_error();
7719 else if (!Type::are_identical(args->front()->type(),
7720 args->back()->type(), true, NULL))
7721 this->report_error(_("complex arguments must have identical types"));
7722 else if (args->front()->type()->float_type() == NULL)
7723 this->report_error(_("complex arguments must have "
7724 "floating-point type"));
7733 // Return the tree for a builtin function.
7736 Builtin_call_expression::do_get_tree(Translate_context* context)
7738 Gogo* gogo = context->gogo();
7739 source_location location = this->location();
7740 switch (this->code_)
7742 case BUILTIN_INVALID:
7750 const Expression_list* args = this->args();
7751 gcc_assert(args != NULL && args->size() == 1);
7752 Expression* arg = *args->begin();
7753 Type* arg_type = arg->type();
7757 gcc_assert(saw_errors());
7758 return error_mark_node;
7762 tree arg_tree = arg->get_tree(context);
7764 this->seen_ = false;
7766 if (arg_tree == error_mark_node)
7767 return error_mark_node;
7769 if (arg_type->points_to() != NULL)
7771 arg_type = arg_type->points_to();
7772 gcc_assert(arg_type->array_type() != NULL
7773 && !arg_type->is_open_array_type());
7774 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7775 arg_tree = build_fold_indirect_ref(arg_tree);
7779 if (this->code_ == BUILTIN_LEN)
7781 if (arg_type->is_string_type())
7782 val_tree = String_type::length_tree(gogo, arg_tree);
7783 else if (arg_type->array_type() != NULL)
7787 gcc_assert(saw_errors());
7788 return error_mark_node;
7791 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7792 this->seen_ = false;
7794 else if (arg_type->map_type() != NULL)
7796 static tree map_len_fndecl;
7797 val_tree = Gogo::call_builtin(&map_len_fndecl,
7802 arg_type->get_tree(gogo),
7805 else if (arg_type->channel_type() != NULL)
7807 static tree chan_len_fndecl;
7808 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7813 arg_type->get_tree(gogo),
7821 if (arg_type->array_type() != NULL)
7825 gcc_assert(saw_errors());
7826 return error_mark_node;
7829 val_tree = arg_type->array_type()->capacity_tree(gogo,
7831 this->seen_ = false;
7833 else if (arg_type->channel_type() != NULL)
7835 static tree chan_cap_fndecl;
7836 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7841 arg_type->get_tree(gogo),
7848 if (val_tree == error_mark_node)
7849 return error_mark_node;
7851 tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo);
7852 if (type_tree == TREE_TYPE(val_tree))
7855 return fold(convert_to_integer(type_tree, val_tree));
7859 case BUILTIN_PRINTLN:
7861 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7862 tree stmt_list = NULL_TREE;
7864 const Expression_list* call_args = this->args();
7865 if (call_args != NULL)
7867 for (Expression_list::const_iterator p = call_args->begin();
7868 p != call_args->end();
7871 if (is_ln && p != call_args->begin())
7873 static tree print_space_fndecl;
7874 tree call = Gogo::call_builtin(&print_space_fndecl,
7879 if (call == error_mark_node)
7880 return error_mark_node;
7881 append_to_statement_list(call, &stmt_list);
7884 Type* type = (*p)->type();
7886 tree arg = (*p)->get_tree(context);
7887 if (arg == error_mark_node)
7888 return error_mark_node;
7892 if (type->is_string_type())
7894 static tree print_string_fndecl;
7895 pfndecl = &print_string_fndecl;
7896 fnname = "__go_print_string";
7898 else if (type->integer_type() != NULL
7899 && type->integer_type()->is_unsigned())
7901 static tree print_uint64_fndecl;
7902 pfndecl = &print_uint64_fndecl;
7903 fnname = "__go_print_uint64";
7904 Type* itype = Type::lookup_integer_type("uint64");
7905 arg = fold_convert_loc(location, itype->get_tree(gogo),
7908 else if (type->integer_type() != NULL)
7910 static tree print_int64_fndecl;
7911 pfndecl = &print_int64_fndecl;
7912 fnname = "__go_print_int64";
7913 Type* itype = Type::lookup_integer_type("int64");
7914 arg = fold_convert_loc(location, itype->get_tree(gogo),
7917 else if (type->float_type() != NULL)
7919 static tree print_double_fndecl;
7920 pfndecl = &print_double_fndecl;
7921 fnname = "__go_print_double";
7922 arg = fold_convert_loc(location, double_type_node, arg);
7924 else if (type->complex_type() != NULL)
7926 static tree print_complex_fndecl;
7927 pfndecl = &print_complex_fndecl;
7928 fnname = "__go_print_complex";
7929 arg = fold_convert_loc(location, complex_double_type_node,
7932 else if (type->is_boolean_type())
7934 static tree print_bool_fndecl;
7935 pfndecl = &print_bool_fndecl;
7936 fnname = "__go_print_bool";
7938 else if (type->points_to() != NULL
7939 || type->channel_type() != NULL
7940 || type->map_type() != NULL
7941 || type->function_type() != NULL)
7943 static tree print_pointer_fndecl;
7944 pfndecl = &print_pointer_fndecl;
7945 fnname = "__go_print_pointer";
7946 arg = fold_convert_loc(location, ptr_type_node, arg);
7948 else if (type->interface_type() != NULL)
7950 if (type->interface_type()->is_empty())
7952 static tree print_empty_interface_fndecl;
7953 pfndecl = &print_empty_interface_fndecl;
7954 fnname = "__go_print_empty_interface";
7958 static tree print_interface_fndecl;
7959 pfndecl = &print_interface_fndecl;
7960 fnname = "__go_print_interface";
7963 else if (type->is_open_array_type())
7965 static tree print_slice_fndecl;
7966 pfndecl = &print_slice_fndecl;
7967 fnname = "__go_print_slice";
7972 tree call = Gogo::call_builtin(pfndecl,
7979 if (call == error_mark_node)
7980 return error_mark_node;
7981 append_to_statement_list(call, &stmt_list);
7987 static tree print_nl_fndecl;
7988 tree call = Gogo::call_builtin(&print_nl_fndecl,
7993 if (call == error_mark_node)
7994 return error_mark_node;
7995 append_to_statement_list(call, &stmt_list);
8003 const Expression_list* args = this->args();
8004 gcc_assert(args != NULL && args->size() == 1);
8005 Expression* arg = args->front();
8006 tree arg_tree = arg->get_tree(context);
8007 if (arg_tree == error_mark_node)
8008 return error_mark_node;
8009 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
8010 arg_tree = Expression::convert_for_assignment(context, empty,
8012 arg_tree, location);
8013 static tree panic_fndecl;
8014 tree call = Gogo::call_builtin(&panic_fndecl,
8019 TREE_TYPE(arg_tree),
8021 if (call == error_mark_node)
8022 return error_mark_node;
8023 // This function will throw an exception.
8024 TREE_NOTHROW(panic_fndecl) = 0;
8025 // This function will not return.
8026 TREE_THIS_VOLATILE(panic_fndecl) = 1;
8030 case BUILTIN_RECOVER:
8032 // The argument is set when building recover thunks. It's a
8033 // boolean value which is true if we can recover a value now.
8034 const Expression_list* args = this->args();
8035 gcc_assert(args != NULL && args->size() == 1);
8036 Expression* arg = args->front();
8037 tree arg_tree = arg->get_tree(context);
8038 if (arg_tree == error_mark_node)
8039 return error_mark_node;
8041 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
8042 tree empty_tree = empty->get_tree(context->gogo());
8044 Type* nil_type = Type::make_nil_type();
8045 Expression* nil = Expression::make_nil(location);
8046 tree nil_tree = nil->get_tree(context);
8047 tree empty_nil_tree = Expression::convert_for_assignment(context,
8053 // We need to handle a deferred call to recover specially,
8054 // because it changes whether it can recover a panic or not.
8055 // See test7 in test/recover1.go.
8057 if (this->is_deferred())
8059 static tree deferred_recover_fndecl;
8060 call = Gogo::call_builtin(&deferred_recover_fndecl,
8062 "__go_deferred_recover",
8068 static tree recover_fndecl;
8069 call = Gogo::call_builtin(&recover_fndecl,
8075 if (call == error_mark_node)
8076 return error_mark_node;
8077 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
8078 call, empty_nil_tree);
8083 const Expression_list* args = this->args();
8084 gcc_assert(args != NULL && args->size() == 1);
8085 Expression* arg = args->front();
8086 tree arg_tree = arg->get_tree(context);
8087 if (arg_tree == error_mark_node)
8088 return error_mark_node;
8089 static tree close_fndecl;
8090 return Gogo::call_builtin(&close_fndecl,
8092 "__go_builtin_close",
8095 TREE_TYPE(arg_tree),
8099 case BUILTIN_SIZEOF:
8100 case BUILTIN_OFFSETOF:
8101 case BUILTIN_ALIGNOF:
8106 bool b = this->integer_constant_value(true, val, &dummy);
8109 gcc_assert(saw_errors());
8110 return error_mark_node;
8112 tree type = Type::lookup_integer_type("int")->get_tree(gogo);
8113 tree ret = Expression::integer_constant_tree(val, type);
8120 const Expression_list* args = this->args();
8121 gcc_assert(args != NULL && args->size() == 2);
8122 Expression* arg1 = args->front();
8123 Expression* arg2 = args->back();
8125 tree arg1_tree = arg1->get_tree(context);
8126 tree arg2_tree = arg2->get_tree(context);
8127 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8128 return error_mark_node;
8130 Type* arg1_type = arg1->type();
8131 Array_type* at = arg1_type->array_type();
8132 arg1_tree = save_expr(arg1_tree);
8133 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
8134 tree arg1_len = at->length_tree(gogo, arg1_tree);
8135 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
8136 return error_mark_node;
8138 Type* arg2_type = arg2->type();
8141 if (arg2_type->is_open_array_type())
8143 at = arg2_type->array_type();
8144 arg2_tree = save_expr(arg2_tree);
8145 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8146 arg2_len = at->length_tree(gogo, arg2_tree);
8150 arg2_tree = save_expr(arg2_tree);
8151 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8152 arg2_len = String_type::length_tree(gogo, arg2_tree);
8154 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8155 return error_mark_node;
8157 arg1_len = save_expr(arg1_len);
8158 arg2_len = save_expr(arg2_len);
8159 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
8160 fold_build2_loc(location, LT_EXPR,
8162 arg1_len, arg2_len),
8163 arg1_len, arg2_len);
8164 len = save_expr(len);
8166 Type* element_type = at->element_type();
8167 tree element_type_tree = element_type->get_tree(gogo);
8168 if (element_type_tree == error_mark_node)
8169 return error_mark_node;
8170 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8171 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
8173 bytecount = fold_build2_loc(location, MULT_EXPR,
8174 TREE_TYPE(element_size),
8175 bytecount, element_size);
8176 bytecount = fold_convert_loc(location, size_type_node, bytecount);
8178 arg1_val = fold_convert_loc(location, ptr_type_node, arg1_val);
8179 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8181 static tree copy_fndecl;
8182 tree call = Gogo::call_builtin(©_fndecl,
8193 if (call == error_mark_node)
8194 return error_mark_node;
8196 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
8200 case BUILTIN_APPEND:
8202 const Expression_list* args = this->args();
8203 gcc_assert(args != NULL && args->size() == 2);
8204 Expression* arg1 = args->front();
8205 Expression* arg2 = args->back();
8207 tree arg1_tree = arg1->get_tree(context);
8208 tree arg2_tree = arg2->get_tree(context);
8209 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8210 return error_mark_node;
8212 Array_type* at = arg1->type()->array_type();
8213 Type* element_type = at->element_type();
8215 arg2_tree = Expression::convert_for_assignment(context, at,
8219 if (arg2_tree == error_mark_node)
8220 return error_mark_node;
8222 arg2_tree = save_expr(arg2_tree);
8223 tree arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8224 tree arg2_len = at->length_tree(gogo, arg2_tree);
8225 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8226 return error_mark_node;
8227 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8228 arg2_len = fold_convert_loc(location, size_type_node, arg2_len);
8230 tree element_type_tree = element_type->get_tree(gogo);
8231 if (element_type_tree == error_mark_node)
8232 return error_mark_node;
8233 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8234 element_size = fold_convert_loc(location, size_type_node,
8237 // We rebuild the decl each time since the slice types may
8239 tree append_fndecl = NULL_TREE;
8240 return Gogo::call_builtin(&append_fndecl,
8244 TREE_TYPE(arg1_tree),
8245 TREE_TYPE(arg1_tree),
8258 const Expression_list* args = this->args();
8259 gcc_assert(args != NULL && args->size() == 1);
8260 Expression* arg = args->front();
8261 tree arg_tree = arg->get_tree(context);
8262 if (arg_tree == error_mark_node)
8263 return error_mark_node;
8264 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
8265 if (this->code_ == BUILTIN_REAL)
8266 return fold_build1_loc(location, REALPART_EXPR,
8267 TREE_TYPE(TREE_TYPE(arg_tree)),
8270 return fold_build1_loc(location, IMAGPART_EXPR,
8271 TREE_TYPE(TREE_TYPE(arg_tree)),
8275 case BUILTIN_COMPLEX:
8277 const Expression_list* args = this->args();
8278 gcc_assert(args != NULL && args->size() == 2);
8279 tree r = args->front()->get_tree(context);
8280 tree i = args->back()->get_tree(context);
8281 if (r == error_mark_node || i == error_mark_node)
8282 return error_mark_node;
8283 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
8284 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
8285 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8286 return fold_build2_loc(location, COMPLEX_EXPR,
8287 build_complex_type(TREE_TYPE(r)),
8296 // We have to support exporting a builtin call expression, because
8297 // code can set a constant to the result of a builtin expression.
8300 Builtin_call_expression::do_export(Export* exp) const
8307 if (this->integer_constant_value(true, val, &dummy))
8309 Integer_expression::export_integer(exp, val);
8318 if (this->float_constant_value(fval, &dummy))
8320 Float_expression::export_float(exp, fval);
8332 if (this->complex_constant_value(real, imag, &dummy))
8334 Complex_expression::export_complex(exp, real, imag);
8343 error_at(this->location(), "value is not constant");
8347 // A trailing space lets us reliably identify the end of the number.
8348 exp->write_c_string(" ");
8351 // Class Call_expression.
8356 Call_expression::do_traverse(Traverse* traverse)
8358 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8359 return TRAVERSE_EXIT;
8360 if (this->args_ != NULL)
8362 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8363 return TRAVERSE_EXIT;
8365 return TRAVERSE_CONTINUE;
8368 // Lower a call statement.
8371 Call_expression::do_lower(Gogo* gogo, Named_object* function, int)
8373 // A type case can look like a function call.
8374 if (this->fn_->is_type_expression()
8375 && this->args_ != NULL
8376 && this->args_->size() == 1)
8377 return Expression::make_cast(this->fn_->type(), this->args_->front(),
8380 // Recognize a call to a builtin function.
8381 Func_expression* fne = this->fn_->func_expression();
8383 && fne->named_object()->is_function_declaration()
8384 && fne->named_object()->func_declaration_value()->type()->is_builtin())
8385 return new Builtin_call_expression(gogo, this->fn_, this->args_,
8386 this->is_varargs_, this->location());
8388 // Handle an argument which is a call to a function which returns
8389 // multiple results.
8390 if (this->args_ != NULL
8391 && this->args_->size() == 1
8392 && this->args_->front()->call_expression() != NULL
8393 && this->fn_->type()->function_type() != NULL)
8395 Function_type* fntype = this->fn_->type()->function_type();
8396 size_t rc = this->args_->front()->call_expression()->result_count();
8398 && fntype->parameters() != NULL
8399 && (fntype->parameters()->size() == rc
8400 || (fntype->is_varargs()
8401 && fntype->parameters()->size() - 1 <= rc)))
8403 Call_expression* call = this->args_->front()->call_expression();
8404 Expression_list* args = new Expression_list;
8405 for (size_t i = 0; i < rc; ++i)
8406 args->push_back(Expression::make_call_result(call, i));
8407 // We can't return a new call expression here, because this
8408 // one may be referenced by Call_result expressions. We
8409 // also can't delete the old arguments, because we may still
8410 // traverse them somewhere up the call stack. FIXME.
8415 // Handle a call to a varargs function by packaging up the extra
8417 if (this->fn_->type()->function_type() != NULL
8418 && this->fn_->type()->function_type()->is_varargs())
8420 Function_type* fntype = this->fn_->type()->function_type();
8421 const Typed_identifier_list* parameters = fntype->parameters();
8422 gcc_assert(parameters != NULL && !parameters->empty());
8423 Type* varargs_type = parameters->back().type();
8424 return this->lower_varargs(gogo, function, varargs_type,
8425 parameters->size());
8431 // Lower a call to a varargs function. FUNCTION is the function in
8432 // which the call occurs--it's not the function we are calling.
8433 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8434 // PARAM_COUNT is the number of parameters of the function we are
8435 // calling; the last of these parameters will be the varargs
8439 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
8440 Type* varargs_type, size_t param_count)
8442 if (this->varargs_are_lowered_)
8445 source_location loc = this->location();
8447 gcc_assert(param_count > 0);
8448 gcc_assert(varargs_type->is_open_array_type());
8450 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8451 if (arg_count < param_count - 1)
8453 // Not enough arguments; will be caught in check_types.
8457 Expression_list* old_args = this->args_;
8458 Expression_list* new_args = new Expression_list();
8459 bool push_empty_arg = false;
8460 if (old_args == NULL || old_args->empty())
8462 gcc_assert(param_count == 1);
8463 push_empty_arg = true;
8467 Expression_list::const_iterator pa;
8469 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
8471 if (static_cast<size_t>(i) == param_count)
8473 new_args->push_back(*pa);
8476 // We have reached the varargs parameter.
8478 bool issued_error = false;
8479 if (pa == old_args->end())
8480 push_empty_arg = true;
8481 else if (pa + 1 == old_args->end() && this->is_varargs_)
8482 new_args->push_back(*pa);
8483 else if (this->is_varargs_)
8485 this->report_error(_("too many arguments"));
8490 Type* element_type = varargs_type->array_type()->element_type();
8491 Expression_list* vals = new Expression_list;
8492 for (; pa != old_args->end(); ++pa, ++i)
8494 // Check types here so that we get a better message.
8495 Type* patype = (*pa)->type();
8496 source_location paloc = (*pa)->location();
8497 if (!this->check_argument_type(i, element_type, patype,
8498 paloc, issued_error))
8500 vals->push_back(*pa);
8503 Expression::make_slice_composite_literal(varargs_type, vals, loc);
8504 new_args->push_back(val);
8509 new_args->push_back(Expression::make_nil(loc));
8511 // We can't return a new call expression here, because this one may
8512 // be referenced by Call_result expressions. FIXME.
8513 if (old_args != NULL)
8515 this->args_ = new_args;
8516 this->varargs_are_lowered_ = true;
8518 // Lower all the new subexpressions.
8519 Expression* ret = this;
8520 gogo->lower_expression(function, &ret);
8521 gcc_assert(ret == this);
8525 // Get the function type. Returns NULL if we don't know the type. If
8526 // this returns NULL, and if_ERROR is true, issues an error.
8529 Call_expression::get_function_type() const
8531 return this->fn_->type()->function_type();
8534 // Return the number of values which this call will return.
8537 Call_expression::result_count() const
8539 const Function_type* fntype = this->get_function_type();
8542 if (fntype->results() == NULL)
8544 return fntype->results()->size();
8547 // Return whether this is a call to the predeclared function recover.
8550 Call_expression::is_recover_call() const
8552 return this->do_is_recover_call();
8555 // Set the argument to the recover function.
8558 Call_expression::set_recover_arg(Expression* arg)
8560 this->do_set_recover_arg(arg);
8563 // Virtual functions also implemented by Builtin_call_expression.
8566 Call_expression::do_is_recover_call() const
8572 Call_expression::do_set_recover_arg(Expression*)
8580 Call_expression::do_type()
8582 if (this->type_ != NULL)
8586 Function_type* fntype = this->get_function_type();
8588 return Type::make_error_type();
8590 const Typed_identifier_list* results = fntype->results();
8591 if (results == NULL)
8592 ret = Type::make_void_type();
8593 else if (results->size() == 1)
8594 ret = results->begin()->type();
8596 ret = Type::make_call_multiple_result_type(this);
8603 // Determine types for a call expression. We can use the function
8604 // parameter types to set the types of the arguments.
8607 Call_expression::do_determine_type(const Type_context*)
8609 if (!this->determining_types())
8612 this->fn_->determine_type_no_context();
8613 Function_type* fntype = this->get_function_type();
8614 const Typed_identifier_list* parameters = NULL;
8616 parameters = fntype->parameters();
8617 if (this->args_ != NULL)
8619 Typed_identifier_list::const_iterator pt;
8620 if (parameters != NULL)
8621 pt = parameters->begin();
8622 for (Expression_list::const_iterator pa = this->args_->begin();
8623 pa != this->args_->end();
8626 if (parameters != NULL && pt != parameters->end())
8628 Type_context subcontext(pt->type(), false);
8629 (*pa)->determine_type(&subcontext);
8633 (*pa)->determine_type_no_context();
8638 // Called when determining types for a Call_expression. Return true
8639 // if we should go ahead, false if they have already been determined.
8642 Call_expression::determining_types()
8644 if (this->types_are_determined_)
8648 this->types_are_determined_ = true;
8653 // Check types for parameter I.
8656 Call_expression::check_argument_type(int i, const Type* parameter_type,
8657 const Type* argument_type,
8658 source_location argument_location,
8662 if (!Type::are_assignable(parameter_type, argument_type, &reason))
8667 error_at(argument_location, "argument %d has incompatible type", i);
8669 error_at(argument_location,
8670 "argument %d has incompatible type (%s)",
8673 this->set_is_error();
8682 Call_expression::do_check_types(Gogo*)
8684 Function_type* fntype = this->get_function_type();
8687 if (!this->fn_->type()->is_error())
8688 this->report_error(_("expected function"));
8692 if (fntype->is_method())
8694 // We don't support pointers to methods, so the function has to
8695 // be a bound method expression.
8696 Bound_method_expression* bme = this->fn_->bound_method_expression();
8699 this->report_error(_("method call without object"));
8702 Type* first_arg_type = bme->first_argument()->type();
8703 if (first_arg_type->points_to() == NULL)
8705 // When passing a value, we need to check that we are
8706 // permitted to copy it. The language permits copying
8707 // hidden fields for a method receiver.
8709 if (!Type::are_assignable_hidden_ok(fntype->receiver()->type(),
8710 first_arg_type, &reason))
8713 this->report_error(_("incompatible type for receiver"));
8716 error_at(this->location(),
8717 "incompatible type for receiver (%s)",
8719 this->set_is_error();
8725 // Note that varargs was handled by the lower_varargs() method, so
8726 // we don't have to worry about it here.
8728 const Typed_identifier_list* parameters = fntype->parameters();
8729 if (this->args_ == NULL)
8731 if (parameters != NULL && !parameters->empty())
8732 this->report_error(_("not enough arguments"));
8734 else if (parameters == NULL)
8735 this->report_error(_("too many arguments"));
8739 Typed_identifier_list::const_iterator pt = parameters->begin();
8740 for (Expression_list::const_iterator pa = this->args_->begin();
8741 pa != this->args_->end();
8744 if (pt == parameters->end())
8746 this->report_error(_("too many arguments"));
8749 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8750 (*pa)->location(), false);
8752 if (pt != parameters->end())
8753 this->report_error(_("not enough arguments"));
8757 // Return whether we have to use a temporary variable to ensure that
8758 // we evaluate this call expression in order. If the call returns no
8759 // results then it will inevitably be executed last. If the call
8760 // returns more than one result then it will be used with Call_result
8761 // expressions. So we only have to use a temporary variable if the
8762 // call returns exactly one result.
8765 Call_expression::do_must_eval_in_order() const
8767 return this->result_count() == 1;
8770 // Get the function and the first argument to use when calling a bound
8774 Call_expression::bound_method_function(Translate_context* context,
8775 Bound_method_expression* bound_method,
8776 tree* first_arg_ptr)
8778 Expression* first_argument = bound_method->first_argument();
8779 tree first_arg = first_argument->get_tree(context);
8780 if (first_arg == error_mark_node)
8781 return error_mark_node;
8783 // We always pass a pointer to the first argument when calling a
8785 if (first_argument->type()->points_to() == NULL)
8787 tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg));
8788 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg))
8789 || DECL_P(first_arg)
8790 || TREE_CODE(first_arg) == INDIRECT_REF
8791 || TREE_CODE(first_arg) == COMPONENT_REF)
8793 first_arg = build_fold_addr_expr(first_arg);
8794 if (DECL_P(first_arg))
8795 TREE_ADDRESSABLE(first_arg) = 1;
8799 tree tmp = create_tmp_var(TREE_TYPE(first_arg),
8800 get_name(first_arg));
8801 DECL_IGNORED_P(tmp) = 0;
8802 DECL_INITIAL(tmp) = first_arg;
8803 first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type,
8804 build1(DECL_EXPR, void_type_node, tmp),
8805 build_fold_addr_expr(tmp));
8806 TREE_ADDRESSABLE(tmp) = 1;
8808 if (first_arg == error_mark_node)
8809 return error_mark_node;
8812 Type* fatype = bound_method->first_argument_type();
8815 if (fatype->points_to() == NULL)
8816 fatype = Type::make_pointer_type(fatype);
8817 first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg);
8818 if (first_arg == error_mark_node
8819 || TREE_TYPE(first_arg) == error_mark_node)
8820 return error_mark_node;
8823 *first_arg_ptr = first_arg;
8825 return bound_method->method()->get_tree(context);
8828 // Get the function and the first argument to use when calling an
8829 // interface method.
8832 Call_expression::interface_method_function(
8833 Translate_context* context,
8834 Interface_field_reference_expression* interface_method,
8835 tree* first_arg_ptr)
8837 tree expr = interface_method->expr()->get_tree(context);
8838 if (expr == error_mark_node)
8839 return error_mark_node;
8840 expr = save_expr(expr);
8841 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
8842 if (first_arg == error_mark_node)
8843 return error_mark_node;
8844 *first_arg_ptr = first_arg;
8845 return interface_method->get_function_tree(context, expr);
8848 // Build the call expression.
8851 Call_expression::do_get_tree(Translate_context* context)
8853 if (this->tree_ != NULL_TREE)
8856 Function_type* fntype = this->get_function_type();
8858 return error_mark_node;
8860 if (this->fn_->is_error_expression())
8861 return error_mark_node;
8863 Gogo* gogo = context->gogo();
8864 source_location location = this->location();
8866 Func_expression* func = this->fn_->func_expression();
8867 Bound_method_expression* bound_method = this->fn_->bound_method_expression();
8868 Interface_field_reference_expression* interface_method =
8869 this->fn_->interface_field_reference_expression();
8870 const bool has_closure = func != NULL && func->closure() != NULL;
8871 const bool is_method = bound_method != NULL || interface_method != NULL;
8872 gcc_assert(!fntype->is_method() || is_method);
8876 if (this->args_ == NULL || this->args_->empty())
8878 nargs = is_method ? 1 : 0;
8879 args = nargs == 0 ? NULL : new tree[nargs];
8883 const Typed_identifier_list* params = fntype->parameters();
8884 gcc_assert(params != NULL);
8886 nargs = this->args_->size();
8887 int i = is_method ? 1 : 0;
8889 args = new tree[nargs];
8891 Typed_identifier_list::const_iterator pp = params->begin();
8892 Expression_list::const_iterator pe;
8893 for (pe = this->args_->begin();
8894 pe != this->args_->end();
8897 gcc_assert(pp != params->end());
8898 tree arg_val = (*pe)->get_tree(context);
8899 args[i] = Expression::convert_for_assignment(context,
8904 if (args[i] == error_mark_node)
8907 return error_mark_node;
8910 gcc_assert(pp == params->end());
8911 gcc_assert(i == nargs);
8914 tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo)));
8915 if (rettype == error_mark_node)
8918 return error_mark_node;
8923 fn = func->get_tree_without_closure(gogo);
8924 else if (!is_method)
8925 fn = this->fn_->get_tree(context);
8926 else if (bound_method != NULL)
8927 fn = this->bound_method_function(context, bound_method, &args[0]);
8928 else if (interface_method != NULL)
8929 fn = this->interface_method_function(context, interface_method, &args[0]);
8933 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
8936 return error_mark_node;
8940 if (TREE_CODE(fndecl) == ADDR_EXPR)
8941 fndecl = TREE_OPERAND(fndecl, 0);
8943 // Add a type cast in case the type of the function is a recursive
8944 // type which refers to itself.
8945 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
8947 tree fnt = fntype->get_tree(gogo);
8948 if (fnt == error_mark_node)
8949 return error_mark_node;
8950 fn = fold_convert_loc(location, fnt, fn);
8953 // This is to support builtin math functions when using 80387 math.
8954 tree excess_type = NULL_TREE;
8956 && DECL_IS_BUILTIN(fndecl)
8957 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
8959 && ((SCALAR_FLOAT_TYPE_P(rettype)
8960 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
8961 || (COMPLEX_FLOAT_TYPE_P(rettype)
8962 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
8964 excess_type = excess_precision_type(TREE_TYPE(args[0]));
8965 if (excess_type != NULL_TREE)
8967 tree excess_fndecl = mathfn_built_in(excess_type,
8968 DECL_FUNCTION_CODE(fndecl));
8969 if (excess_fndecl == NULL_TREE)
8970 excess_type = NULL_TREE;
8973 fn = build_fold_addr_expr_loc(location, excess_fndecl);
8974 for (int i = 0; i < nargs; ++i)
8975 args[i] = ::convert(excess_type, args[i]);
8980 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
8984 SET_EXPR_LOCATION(ret, location);
8988 tree closure_tree = func->closure()->get_tree(context);
8989 if (closure_tree != error_mark_node)
8990 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
8993 // If this is a recursive function type which returns itself, as in
8995 // we have used ptr_type_node for the return type. Add a cast here
8996 // to the correct type.
8997 if (TREE_TYPE(ret) == ptr_type_node)
8999 tree t = this->type()->base()->get_tree(gogo);
9000 ret = fold_convert_loc(location, t, ret);
9003 if (excess_type != NULL_TREE)
9005 // Calling convert here can undo our excess precision change.
9006 // That may or may not be a bug in convert_to_real.
9007 ret = build1(NOP_EXPR, rettype, ret);
9010 // If there is more than one result, we will refer to the call
9012 if (fntype->results() != NULL && fntype->results()->size() > 1)
9013 ret = save_expr(ret);
9020 // Make a call expression.
9023 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
9024 source_location location)
9026 return new Call_expression(fn, args, is_varargs, location);
9029 // A single result from a call which returns multiple results.
9031 class Call_result_expression : public Expression
9034 Call_result_expression(Call_expression* call, unsigned int index)
9035 : Expression(EXPRESSION_CALL_RESULT, call->location()),
9036 call_(call), index_(index)
9041 do_traverse(Traverse*);
9047 do_determine_type(const Type_context*);
9050 do_check_types(Gogo*);
9055 return new Call_result_expression(this->call_->call_expression(),
9060 do_must_eval_in_order() const
9064 do_get_tree(Translate_context*);
9067 // The underlying call expression.
9069 // Which result we want.
9070 unsigned int index_;
9073 // Traverse a call result.
9076 Call_result_expression::do_traverse(Traverse* traverse)
9078 if (traverse->remember_expression(this->call_))
9080 // We have already traversed the call expression.
9081 return TRAVERSE_CONTINUE;
9083 return Expression::traverse(&this->call_, traverse);
9089 Call_result_expression::do_type()
9091 if (this->classification() == EXPRESSION_ERROR)
9092 return Type::make_error_type();
9094 // THIS->CALL_ can be replaced with a temporary reference due to
9095 // Call_expression::do_must_eval_in_order when there is an error.
9096 Call_expression* ce = this->call_->call_expression();
9099 this->set_is_error();
9100 return Type::make_error_type();
9102 Function_type* fntype = ce->get_function_type();
9105 this->set_is_error();
9106 return Type::make_error_type();
9108 const Typed_identifier_list* results = fntype->results();
9109 if (results == NULL)
9111 this->report_error(_("number of results does not match "
9112 "number of values"));
9113 return Type::make_error_type();
9115 Typed_identifier_list::const_iterator pr = results->begin();
9116 for (unsigned int i = 0; i < this->index_; ++i)
9118 if (pr == results->end())
9122 if (pr == results->end())
9124 this->report_error(_("number of results does not match "
9125 "number of values"));
9126 return Type::make_error_type();
9131 // Check the type. Just make sure that we trigger the warning in
9135 Call_result_expression::do_check_types(Gogo*)
9140 // Determine the type. We have nothing to do here, but the 0 result
9141 // needs to pass down to the caller.
9144 Call_result_expression::do_determine_type(const Type_context*)
9146 this->call_->determine_type_no_context();
9152 Call_result_expression::do_get_tree(Translate_context* context)
9154 tree call_tree = this->call_->get_tree(context);
9155 if (call_tree == error_mark_node)
9156 return error_mark_node;
9157 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9159 gcc_assert(saw_errors());
9160 return error_mark_node;
9162 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9163 for (unsigned int i = 0; i < this->index_; ++i)
9165 gcc_assert(field != NULL_TREE);
9166 field = DECL_CHAIN(field);
9168 gcc_assert(field != NULL_TREE);
9169 return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE);
9172 // Make a reference to a single result of a call which returns
9173 // multiple results.
9176 Expression::make_call_result(Call_expression* call, unsigned int index)
9178 return new Call_result_expression(call, index);
9181 // Class Index_expression.
9186 Index_expression::do_traverse(Traverse* traverse)
9188 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9189 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9190 || (this->end_ != NULL
9191 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9192 return TRAVERSE_EXIT;
9193 return TRAVERSE_CONTINUE;
9196 // Lower an index expression. This converts the generic index
9197 // expression into an array index, a string index, or a map index.
9200 Index_expression::do_lower(Gogo*, Named_object*, int)
9202 source_location location = this->location();
9203 Expression* left = this->left_;
9204 Expression* start = this->start_;
9205 Expression* end = this->end_;
9207 Type* type = left->type();
9208 if (type->is_error())
9209 return Expression::make_error(location);
9210 else if (left->is_type_expression())
9212 error_at(location, "attempt to index type expression");
9213 return Expression::make_error(location);
9215 else if (type->array_type() != NULL)
9216 return Expression::make_array_index(left, start, end, location);
9217 else if (type->points_to() != NULL
9218 && type->points_to()->array_type() != NULL
9219 && !type->points_to()->is_open_array_type())
9221 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9223 return Expression::make_array_index(deref, start, end, location);
9225 else if (type->is_string_type())
9226 return Expression::make_string_index(left, start, end, location);
9227 else if (type->map_type() != NULL)
9231 error_at(location, "invalid slice of map");
9232 return Expression::make_error(location);
9234 Map_index_expression* ret= Expression::make_map_index(left, start,
9236 if (this->is_lvalue_)
9237 ret->set_is_lvalue();
9243 "attempt to index object which is not array, string, or map");
9244 return Expression::make_error(location);
9248 // Make an index expression.
9251 Expression::make_index(Expression* left, Expression* start, Expression* end,
9252 source_location location)
9254 return new Index_expression(left, start, end, location);
9257 // An array index. This is used for both indexing and slicing.
9259 class Array_index_expression : public Expression
9262 Array_index_expression(Expression* array, Expression* start,
9263 Expression* end, source_location location)
9264 : Expression(EXPRESSION_ARRAY_INDEX, location),
9265 array_(array), start_(start), end_(end), type_(NULL)
9270 do_traverse(Traverse*);
9276 do_determine_type(const Type_context*);
9279 do_check_types(Gogo*);
9284 return Expression::make_array_index(this->array_->copy(),
9285 this->start_->copy(),
9288 : this->end_->copy()),
9293 do_is_addressable() const;
9296 do_address_taken(bool escapes)
9297 { this->array_->address_taken(escapes); }
9300 do_get_tree(Translate_context*);
9303 // The array we are getting a value from.
9305 // The start or only index.
9307 // The end index of a slice. This may be NULL for a simple array
9308 // index, or it may be a nil expression for the length of the array.
9310 // The type of the expression.
9314 // Array index traversal.
9317 Array_index_expression::do_traverse(Traverse* traverse)
9319 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9320 return TRAVERSE_EXIT;
9321 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9322 return TRAVERSE_EXIT;
9323 if (this->end_ != NULL)
9325 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9326 return TRAVERSE_EXIT;
9328 return TRAVERSE_CONTINUE;
9331 // Return the type of an array index.
9334 Array_index_expression::do_type()
9336 if (this->type_ == NULL)
9338 Array_type* type = this->array_->type()->array_type();
9340 this->type_ = Type::make_error_type();
9341 else if (this->end_ == NULL)
9342 this->type_ = type->element_type();
9343 else if (type->is_open_array_type())
9345 // A slice of a slice has the same type as the original
9347 this->type_ = this->array_->type()->deref();
9351 // A slice of an array is a slice.
9352 this->type_ = Type::make_array_type(type->element_type(), NULL);
9358 // Set the type of an array index.
9361 Array_index_expression::do_determine_type(const Type_context*)
9363 this->array_->determine_type_no_context();
9364 this->start_->determine_type_no_context();
9365 if (this->end_ != NULL)
9366 this->end_->determine_type_no_context();
9369 // Check types of an array index.
9372 Array_index_expression::do_check_types(Gogo*)
9374 if (this->start_->type()->integer_type() == NULL)
9375 this->report_error(_("index must be integer"));
9376 if (this->end_ != NULL
9377 && this->end_->type()->integer_type() == NULL
9378 && !this->end_->is_nil_expression())
9379 this->report_error(_("slice end must be integer"));
9381 Array_type* array_type = this->array_->type()->array_type();
9382 if (array_type == NULL)
9384 gcc_assert(this->array_->type()->is_error());
9388 unsigned int int_bits =
9389 Type::lookup_integer_type("int")->integer_type()->bits();
9394 bool lval_valid = (array_type->length() != NULL
9395 && array_type->length()->integer_constant_value(true,
9400 if (this->start_->integer_constant_value(true, ival, &dummy))
9402 if (mpz_sgn(ival) < 0
9403 || mpz_sizeinbase(ival, 2) >= int_bits
9405 && (this->end_ == NULL
9406 ? mpz_cmp(ival, lval) >= 0
9407 : mpz_cmp(ival, lval) > 0)))
9409 error_at(this->start_->location(), "array index out of bounds");
9410 this->set_is_error();
9413 if (this->end_ != NULL && !this->end_->is_nil_expression())
9415 if (this->end_->integer_constant_value(true, ival, &dummy))
9417 if (mpz_sgn(ival) < 0
9418 || mpz_sizeinbase(ival, 2) >= int_bits
9419 || (lval_valid && mpz_cmp(ival, lval) > 0))
9421 error_at(this->end_->location(), "array index out of bounds");
9422 this->set_is_error();
9429 // A slice of an array requires an addressable array. A slice of a
9430 // slice is always possible.
9431 if (this->end_ != NULL && !array_type->is_open_array_type())
9433 if (!this->array_->is_addressable())
9434 this->report_error(_("array is not addressable"));
9436 this->array_->address_taken(true);
9440 // Return whether this expression is addressable.
9443 Array_index_expression::do_is_addressable() const
9445 // A slice expression is not addressable.
9446 if (this->end_ != NULL)
9449 // An index into a slice is addressable.
9450 if (this->array_->type()->is_open_array_type())
9453 // An index into an array is addressable if the array is
9455 return this->array_->is_addressable();
9458 // Get a tree for an array index.
9461 Array_index_expression::do_get_tree(Translate_context* context)
9463 Gogo* gogo = context->gogo();
9464 source_location loc = this->location();
9466 Array_type* array_type = this->array_->type()->array_type();
9467 if (array_type == NULL)
9469 gcc_assert(this->array_->type()->is_error());
9470 return error_mark_node;
9473 tree type_tree = array_type->get_tree(gogo);
9474 if (type_tree == error_mark_node)
9475 return error_mark_node;
9477 tree array_tree = this->array_->get_tree(context);
9478 if (array_tree == error_mark_node)
9479 return error_mark_node;
9481 if (array_type->length() == NULL && !DECL_P(array_tree))
9482 array_tree = save_expr(array_tree);
9483 tree length_tree = array_type->length_tree(gogo, array_tree);
9484 if (length_tree == error_mark_node)
9485 return error_mark_node;
9486 length_tree = save_expr(length_tree);
9487 tree length_type = TREE_TYPE(length_tree);
9489 tree bad_index = boolean_false_node;
9491 tree start_tree = this->start_->get_tree(context);
9492 if (start_tree == error_mark_node)
9493 return error_mark_node;
9494 if (!DECL_P(start_tree))
9495 start_tree = save_expr(start_tree);
9496 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9497 start_tree = convert_to_integer(length_type, start_tree);
9499 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9502 start_tree = fold_convert_loc(loc, length_type, start_tree);
9503 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
9504 fold_build2_loc(loc,
9508 boolean_type_node, start_tree,
9511 int code = (array_type->length() != NULL
9512 ? (this->end_ == NULL
9513 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9514 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9515 : (this->end_ == NULL
9516 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9517 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9518 tree crash = Gogo::runtime_error(code, loc);
9520 if (this->end_ == NULL)
9522 // Simple array indexing. This has to return an l-value, so
9523 // wrap the index check into START_TREE.
9524 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9525 build3(COND_EXPR, void_type_node,
9526 bad_index, crash, NULL_TREE),
9528 start_tree = fold_convert_loc(loc, sizetype, start_tree);
9530 if (array_type->length() != NULL)
9533 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9534 start_tree, NULL_TREE, NULL_TREE);
9539 tree values = array_type->value_pointer_tree(gogo, array_tree);
9540 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9541 if (element_type_tree == error_mark_node)
9542 return error_mark_node;
9543 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9544 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9545 start_tree, element_size);
9546 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9547 TREE_TYPE(values), values, offset);
9548 return build_fold_indirect_ref(ptr);
9554 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
9555 if (capacity_tree == error_mark_node)
9556 return error_mark_node;
9557 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
9560 if (this->end_->is_nil_expression())
9561 end_tree = length_tree;
9564 end_tree = this->end_->get_tree(context);
9565 if (end_tree == error_mark_node)
9566 return error_mark_node;
9567 if (!DECL_P(end_tree))
9568 end_tree = save_expr(end_tree);
9569 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9570 end_tree = convert_to_integer(length_type, end_tree);
9572 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9575 end_tree = fold_convert_loc(loc, length_type, end_tree);
9577 capacity_tree = save_expr(capacity_tree);
9578 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9579 fold_build2_loc(loc, LT_EXPR,
9581 end_tree, start_tree),
9582 fold_build2_loc(loc, GT_EXPR,
9584 end_tree, capacity_tree));
9585 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9586 bad_index, bad_end);
9589 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9590 if (element_type_tree == error_mark_node)
9591 return error_mark_node;
9592 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9594 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9595 fold_convert_loc(loc, sizetype, start_tree),
9598 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9599 if (value_pointer == error_mark_node)
9600 return error_mark_node;
9602 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9603 TREE_TYPE(value_pointer),
9604 value_pointer, offset);
9606 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9607 end_tree, start_tree);
9609 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9610 capacity_tree, start_tree);
9612 tree struct_tree = this->type()->get_tree(gogo);
9613 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9615 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9617 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9618 tree field = TYPE_FIELDS(struct_tree);
9619 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9621 elt->value = value_pointer;
9623 elt = VEC_quick_push(constructor_elt, init, NULL);
9624 field = DECL_CHAIN(field);
9625 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9627 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
9629 elt = VEC_quick_push(constructor_elt, init, NULL);
9630 field = DECL_CHAIN(field);
9631 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9633 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
9635 tree constructor = build_constructor(struct_tree, init);
9637 if (TREE_CONSTANT(value_pointer)
9638 && TREE_CONSTANT(result_length_tree)
9639 && TREE_CONSTANT(result_capacity_tree))
9640 TREE_CONSTANT(constructor) = 1;
9642 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
9643 build3(COND_EXPR, void_type_node,
9644 bad_index, crash, NULL_TREE),
9648 // Make an array index expression. END may be NULL.
9651 Expression::make_array_index(Expression* array, Expression* start,
9652 Expression* end, source_location location)
9654 // Taking a slice of a composite literal requires moving the literal
9656 if (end != NULL && array->is_composite_literal())
9658 array = Expression::make_heap_composite(array, location);
9659 array = Expression::make_unary(OPERATOR_MULT, array, location);
9661 return new Array_index_expression(array, start, end, location);
9664 // A string index. This is used for both indexing and slicing.
9666 class String_index_expression : public Expression
9669 String_index_expression(Expression* string, Expression* start,
9670 Expression* end, source_location location)
9671 : Expression(EXPRESSION_STRING_INDEX, location),
9672 string_(string), start_(start), end_(end)
9677 do_traverse(Traverse*);
9683 do_determine_type(const Type_context*);
9686 do_check_types(Gogo*);
9691 return Expression::make_string_index(this->string_->copy(),
9692 this->start_->copy(),
9695 : this->end_->copy()),
9700 do_get_tree(Translate_context*);
9703 // The string we are getting a value from.
9704 Expression* string_;
9705 // The start or only index.
9707 // The end index of a slice. This may be NULL for a single index,
9708 // or it may be a nil expression for the length of the string.
9712 // String index traversal.
9715 String_index_expression::do_traverse(Traverse* traverse)
9717 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
9718 return TRAVERSE_EXIT;
9719 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9720 return TRAVERSE_EXIT;
9721 if (this->end_ != NULL)
9723 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9724 return TRAVERSE_EXIT;
9726 return TRAVERSE_CONTINUE;
9729 // Return the type of a string index.
9732 String_index_expression::do_type()
9734 if (this->end_ == NULL)
9735 return Type::lookup_integer_type("uint8");
9737 return this->string_->type();
9740 // Determine the type of a string index.
9743 String_index_expression::do_determine_type(const Type_context*)
9745 this->string_->determine_type_no_context();
9746 this->start_->determine_type_no_context();
9747 if (this->end_ != NULL)
9748 this->end_->determine_type_no_context();
9751 // Check types of a string index.
9754 String_index_expression::do_check_types(Gogo*)
9756 if (this->start_->type()->integer_type() == NULL)
9757 this->report_error(_("index must be integer"));
9758 if (this->end_ != NULL
9759 && this->end_->type()->integer_type() == NULL
9760 && !this->end_->is_nil_expression())
9761 this->report_error(_("slice end must be integer"));
9764 bool sval_valid = this->string_->string_constant_value(&sval);
9769 if (this->start_->integer_constant_value(true, ival, &dummy))
9771 if (mpz_sgn(ival) < 0
9772 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
9774 error_at(this->start_->location(), "string index out of bounds");
9775 this->set_is_error();
9778 if (this->end_ != NULL && !this->end_->is_nil_expression())
9780 if (this->end_->integer_constant_value(true, ival, &dummy))
9782 if (mpz_sgn(ival) < 0
9783 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
9785 error_at(this->end_->location(), "string index out of bounds");
9786 this->set_is_error();
9793 // Get a tree for a string index.
9796 String_index_expression::do_get_tree(Translate_context* context)
9798 source_location loc = this->location();
9800 tree string_tree = this->string_->get_tree(context);
9801 if (string_tree == error_mark_node)
9802 return error_mark_node;
9804 if (this->string_->type()->points_to() != NULL)
9805 string_tree = build_fold_indirect_ref(string_tree);
9806 if (!DECL_P(string_tree))
9807 string_tree = save_expr(string_tree);
9808 tree string_type = TREE_TYPE(string_tree);
9810 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
9811 length_tree = save_expr(length_tree);
9812 tree length_type = TREE_TYPE(length_tree);
9814 tree bad_index = boolean_false_node;
9816 tree start_tree = this->start_->get_tree(context);
9817 if (start_tree == error_mark_node)
9818 return error_mark_node;
9819 if (!DECL_P(start_tree))
9820 start_tree = save_expr(start_tree);
9821 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9822 start_tree = convert_to_integer(length_type, start_tree);
9824 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9827 start_tree = fold_convert_loc(loc, length_type, start_tree);
9829 int code = (this->end_ == NULL
9830 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9831 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
9832 tree crash = Gogo::runtime_error(code, loc);
9834 if (this->end_ == NULL)
9836 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9838 fold_build2_loc(loc, GE_EXPR,
9840 start_tree, length_tree));
9842 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
9843 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
9845 fold_convert_loc(loc, sizetype, start_tree));
9846 tree index = build_fold_indirect_ref_loc(loc, ptr);
9848 return build2(COMPOUND_EXPR, TREE_TYPE(index),
9849 build3(COND_EXPR, void_type_node,
9850 bad_index, crash, NULL_TREE),
9856 if (this->end_->is_nil_expression())
9857 end_tree = build_int_cst(length_type, -1);
9860 end_tree = this->end_->get_tree(context);
9861 if (end_tree == error_mark_node)
9862 return error_mark_node;
9863 if (!DECL_P(end_tree))
9864 end_tree = save_expr(end_tree);
9865 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9866 end_tree = convert_to_integer(length_type, end_tree);
9868 bad_index = Expression::check_bounds(end_tree, length_type,
9871 end_tree = fold_convert_loc(loc, length_type, end_tree);
9874 static tree strslice_fndecl;
9875 tree ret = Gogo::call_builtin(&strslice_fndecl,
9877 "__go_string_slice",
9886 if (ret == error_mark_node)
9887 return error_mark_node;
9888 // This will panic if the bounds are out of range for the
9890 TREE_NOTHROW(strslice_fndecl) = 0;
9892 if (bad_index == boolean_false_node)
9895 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
9896 build3(COND_EXPR, void_type_node,
9897 bad_index, crash, NULL_TREE),
9902 // Make a string index expression. END may be NULL.
9905 Expression::make_string_index(Expression* string, Expression* start,
9906 Expression* end, source_location location)
9908 return new String_index_expression(string, start, end, location);
9913 // Get the type of the map.
9916 Map_index_expression::get_map_type() const
9918 Map_type* mt = this->map_->type()->deref()->map_type();
9920 gcc_assert(saw_errors());
9924 // Map index traversal.
9927 Map_index_expression::do_traverse(Traverse* traverse)
9929 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
9930 return TRAVERSE_EXIT;
9931 return Expression::traverse(&this->index_, traverse);
9934 // Return the type of a map index.
9937 Map_index_expression::do_type()
9939 Map_type* mt = this->get_map_type();
9941 return Type::make_error_type();
9942 Type* type = mt->val_type();
9943 // If this map index is in a tuple assignment, we actually return a
9944 // pointer to the value type. Tuple_map_assignment_statement is
9945 // responsible for handling this correctly. We need to get the type
9946 // right in case this gets assigned to a temporary variable.
9947 if (this->is_in_tuple_assignment_)
9948 type = Type::make_pointer_type(type);
9952 // Fix the type of a map index.
9955 Map_index_expression::do_determine_type(const Type_context*)
9957 this->map_->determine_type_no_context();
9958 Map_type* mt = this->get_map_type();
9959 Type* key_type = mt == NULL ? NULL : mt->key_type();
9960 Type_context subcontext(key_type, false);
9961 this->index_->determine_type(&subcontext);
9964 // Check types of a map index.
9967 Map_index_expression::do_check_types(Gogo*)
9970 Map_type* mt = this->get_map_type();
9973 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
9976 this->report_error(_("incompatible type for map index"));
9979 error_at(this->location(), "incompatible type for map index (%s)",
9981 this->set_is_error();
9986 // Get a tree for a map index.
9989 Map_index_expression::do_get_tree(Translate_context* context)
9991 Map_type* type = this->get_map_type();
9993 return error_mark_node;
9995 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
9996 if (valptr == error_mark_node)
9997 return error_mark_node;
9998 valptr = save_expr(valptr);
10000 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
10002 if (this->is_lvalue_)
10003 return build_fold_indirect_ref(valptr);
10004 else if (this->is_in_tuple_assignment_)
10006 // Tuple_map_assignment_statement is responsible for using this
10012 return fold_build3(COND_EXPR, val_type_tree,
10013 fold_build2(EQ_EXPR, boolean_type_node, valptr,
10014 fold_convert(TREE_TYPE(valptr),
10015 null_pointer_node)),
10016 type->val_type()->get_init_tree(context->gogo(),
10018 build_fold_indirect_ref(valptr));
10022 // Get a tree for the map index. This returns a tree which evaluates
10023 // to a pointer to a value. The pointer will be NULL if the key is
10027 Map_index_expression::get_value_pointer(Translate_context* context,
10030 Map_type* type = this->get_map_type();
10032 return error_mark_node;
10034 tree map_tree = this->map_->get_tree(context);
10035 tree index_tree = this->index_->get_tree(context);
10036 index_tree = Expression::convert_for_assignment(context, type->key_type(),
10037 this->index_->type(),
10040 if (map_tree == error_mark_node || index_tree == error_mark_node)
10041 return error_mark_node;
10043 if (this->map_->type()->points_to() != NULL)
10044 map_tree = build_fold_indirect_ref(map_tree);
10046 // We need to pass in a pointer to the key, so stuff it into a
10050 if (current_function_decl != NULL)
10052 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
10053 DECL_IGNORED_P(tmp) = 0;
10054 DECL_INITIAL(tmp) = index_tree;
10055 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
10056 TREE_ADDRESSABLE(tmp) = 1;
10060 tmp = build_decl(this->location(), VAR_DECL, create_tmp_var_name("M"),
10061 TREE_TYPE(index_tree));
10062 DECL_EXTERNAL(tmp) = 0;
10063 TREE_PUBLIC(tmp) = 0;
10064 TREE_STATIC(tmp) = 1;
10065 DECL_ARTIFICIAL(tmp) = 1;
10066 if (!TREE_CONSTANT(index_tree))
10067 make_tmp = fold_build2_loc(this->location(), INIT_EXPR, void_type_node,
10071 TREE_READONLY(tmp) = 1;
10072 TREE_CONSTANT(tmp) = 1;
10073 DECL_INITIAL(tmp) = index_tree;
10074 make_tmp = NULL_TREE;
10076 rest_of_decl_compilation(tmp, 1, 0);
10078 tree tmpref = fold_convert_loc(this->location(), const_ptr_type_node,
10079 build_fold_addr_expr_loc(this->location(),
10082 static tree map_index_fndecl;
10083 tree call = Gogo::call_builtin(&map_index_fndecl,
10087 const_ptr_type_node,
10088 TREE_TYPE(map_tree),
10090 const_ptr_type_node,
10094 ? boolean_true_node
10095 : boolean_false_node));
10096 if (call == error_mark_node)
10097 return error_mark_node;
10098 // This can panic on a map of interface type if the interface holds
10099 // an uncomparable or unhashable type.
10100 TREE_NOTHROW(map_index_fndecl) = 0;
10102 tree val_type_tree = type->val_type()->get_tree(context->gogo());
10103 if (val_type_tree == error_mark_node)
10104 return error_mark_node;
10105 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
10107 tree ret = fold_convert_loc(this->location(), ptr_val_type_tree, call);
10108 if (make_tmp != NULL_TREE)
10109 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
10113 // Make a map index expression.
10115 Map_index_expression*
10116 Expression::make_map_index(Expression* map, Expression* index,
10117 source_location location)
10119 return new Map_index_expression(map, index, location);
10122 // Class Field_reference_expression.
10124 // Return the type of a field reference.
10127 Field_reference_expression::do_type()
10129 Type* type = this->expr_->type();
10130 if (type->is_error())
10132 Struct_type* struct_type = type->struct_type();
10133 gcc_assert(struct_type != NULL);
10134 return struct_type->field(this->field_index_)->type();
10137 // Check the types for a field reference.
10140 Field_reference_expression::do_check_types(Gogo*)
10142 Type* type = this->expr_->type();
10143 if (type->is_error())
10145 Struct_type* struct_type = type->struct_type();
10146 gcc_assert(struct_type != NULL);
10147 gcc_assert(struct_type->field(this->field_index_) != NULL);
10150 // Get a tree for a field reference.
10153 Field_reference_expression::do_get_tree(Translate_context* context)
10155 tree struct_tree = this->expr_->get_tree(context);
10156 if (struct_tree == error_mark_node
10157 || TREE_TYPE(struct_tree) == error_mark_node)
10158 return error_mark_node;
10159 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
10160 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
10161 if (field == NULL_TREE)
10163 // This can happen for a type which refers to itself indirectly
10164 // and then turns out to be erroneous.
10165 gcc_assert(saw_errors());
10166 return error_mark_node;
10168 for (unsigned int i = this->field_index_; i > 0; --i)
10170 field = DECL_CHAIN(field);
10171 gcc_assert(field != NULL_TREE);
10173 if (TREE_TYPE(field) == error_mark_node)
10174 return error_mark_node;
10175 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
10179 // Make a reference to a qualified identifier in an expression.
10181 Field_reference_expression*
10182 Expression::make_field_reference(Expression* expr, unsigned int field_index,
10183 source_location location)
10185 return new Field_reference_expression(expr, field_index, location);
10188 // Class Interface_field_reference_expression.
10190 // Return a tree for the pointer to the function to call.
10193 Interface_field_reference_expression::get_function_tree(Translate_context*,
10196 if (this->expr_->type()->points_to() != NULL)
10197 expr = build_fold_indirect_ref(expr);
10199 tree expr_type = TREE_TYPE(expr);
10200 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10202 tree field = TYPE_FIELDS(expr_type);
10203 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
10205 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10206 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table)));
10208 table = build_fold_indirect_ref(table);
10209 gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
10211 std::string name = Gogo::unpack_hidden_name(this->name_);
10212 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
10213 field != NULL_TREE;
10214 field = DECL_CHAIN(field))
10216 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
10219 gcc_assert(field != NULL_TREE);
10221 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
10224 // Return a tree for the first argument to pass to the interface
10228 Interface_field_reference_expression::get_underlying_object_tree(
10229 Translate_context*,
10232 if (this->expr_->type()->points_to() != NULL)
10233 expr = build_fold_indirect_ref(expr);
10235 tree expr_type = TREE_TYPE(expr);
10236 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10238 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
10239 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
10241 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10247 Interface_field_reference_expression::do_traverse(Traverse* traverse)
10249 return Expression::traverse(&this->expr_, traverse);
10252 // Return the type of an interface field reference.
10255 Interface_field_reference_expression::do_type()
10257 Type* expr_type = this->expr_->type();
10259 Type* points_to = expr_type->points_to();
10260 if (points_to != NULL)
10261 expr_type = points_to;
10263 Interface_type* interface_type = expr_type->interface_type();
10264 if (interface_type == NULL)
10265 return Type::make_error_type();
10267 const Typed_identifier* method = interface_type->find_method(this->name_);
10268 if (method == NULL)
10269 return Type::make_error_type();
10271 return method->type();
10274 // Determine types.
10277 Interface_field_reference_expression::do_determine_type(const Type_context*)
10279 this->expr_->determine_type_no_context();
10282 // Check the types for an interface field reference.
10285 Interface_field_reference_expression::do_check_types(Gogo*)
10287 Type* type = this->expr_->type();
10289 Type* points_to = type->points_to();
10290 if (points_to != NULL)
10293 Interface_type* interface_type = type->interface_type();
10294 if (interface_type == NULL)
10296 if (!type->is_error_type())
10297 this->report_error(_("expected interface or pointer to interface"));
10301 const Typed_identifier* method =
10302 interface_type->find_method(this->name_);
10303 if (method == NULL)
10305 error_at(this->location(), "method %qs not in interface",
10306 Gogo::message_name(this->name_).c_str());
10307 this->set_is_error();
10312 // Get a tree for a reference to a field in an interface. There is no
10313 // standard tree type representation for this: it's a function
10314 // attached to its first argument, like a Bound_method_expression.
10315 // The only places it may currently be used are in a Call_expression
10316 // or a Go_statement, which will take it apart directly. So this has
10317 // nothing to do at present.
10320 Interface_field_reference_expression::do_get_tree(Translate_context*)
10325 // Make a reference to a field in an interface.
10328 Expression::make_interface_field_reference(Expression* expr,
10329 const std::string& field,
10330 source_location location)
10332 return new Interface_field_reference_expression(expr, field, location);
10335 // A general selector. This is a Parser_expression for LEFT.NAME. It
10336 // is lowered after we know the type of the left hand side.
10338 class Selector_expression : public Parser_expression
10341 Selector_expression(Expression* left, const std::string& name,
10342 source_location location)
10343 : Parser_expression(EXPRESSION_SELECTOR, location),
10344 left_(left), name_(name)
10349 do_traverse(Traverse* traverse)
10350 { return Expression::traverse(&this->left_, traverse); }
10353 do_lower(Gogo*, Named_object*, int);
10358 return new Selector_expression(this->left_->copy(), this->name_,
10364 lower_method_expression(Gogo*);
10366 // The expression on the left hand side.
10368 // The name on the right hand side.
10372 // Lower a selector expression once we know the real type of the left
10376 Selector_expression::do_lower(Gogo* gogo, Named_object*, int)
10378 Expression* left = this->left_;
10379 if (left->is_type_expression())
10380 return this->lower_method_expression(gogo);
10381 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
10385 // Lower a method expression T.M or (*T).M. We turn this into a
10386 // function literal.
10389 Selector_expression::lower_method_expression(Gogo* gogo)
10391 source_location location = this->location();
10392 Type* type = this->left_->type();
10393 const std::string& name(this->name_);
10396 if (type->points_to() == NULL)
10397 is_pointer = false;
10401 type = type->points_to();
10403 Named_type* nt = type->named_type();
10407 ("method expression requires named type or "
10408 "pointer to named type"));
10409 return Expression::make_error(location);
10413 Method* method = nt->method_function(name, &is_ambiguous);
10414 const Typed_identifier* imethod = NULL;
10415 if (method == NULL && !is_pointer)
10417 Interface_type* it = nt->interface_type();
10419 imethod = it->find_method(name);
10422 if (method == NULL && imethod == NULL)
10425 error_at(location, "type %<%s%s%> has no method %<%s%>",
10426 is_pointer ? "*" : "",
10427 nt->message_name().c_str(),
10428 Gogo::message_name(name).c_str());
10430 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
10431 Gogo::message_name(name).c_str(),
10432 is_pointer ? "*" : "",
10433 nt->message_name().c_str());
10434 return Expression::make_error(location);
10437 if (method != NULL && !is_pointer && !method->is_value_method())
10439 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
10440 nt->message_name().c_str(),
10441 Gogo::message_name(name).c_str());
10442 return Expression::make_error(location);
10445 // Build a new function type in which the receiver becomes the first
10447 Function_type* method_type;
10448 if (method != NULL)
10450 method_type = method->type();
10451 gcc_assert(method_type->is_method());
10455 method_type = imethod->type()->function_type();
10456 gcc_assert(method_type != NULL && !method_type->is_method());
10459 const char* const receiver_name = "$this";
10460 Typed_identifier_list* parameters = new Typed_identifier_list();
10461 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
10464 const Typed_identifier_list* method_parameters = method_type->parameters();
10465 if (method_parameters != NULL)
10467 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10468 p != method_parameters->end();
10470 parameters->push_back(*p);
10473 const Typed_identifier_list* method_results = method_type->results();
10474 Typed_identifier_list* results;
10475 if (method_results == NULL)
10479 results = new Typed_identifier_list();
10480 for (Typed_identifier_list::const_iterator p = method_results->begin();
10481 p != method_results->end();
10483 results->push_back(*p);
10486 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
10488 if (method_type->is_varargs())
10489 fntype->set_is_varargs();
10491 // We generate methods which always takes a pointer to the receiver
10492 // as their first argument. If this is for a pointer type, we can
10493 // simply reuse the existing function. We use an internal hack to
10494 // get the right type.
10496 if (method != NULL && is_pointer)
10498 Named_object* mno = (method->needs_stub_method()
10499 ? method->stub_object()
10500 : method->named_object());
10501 Expression* f = Expression::make_func_reference(mno, NULL, location);
10502 f = Expression::make_cast(fntype, f, location);
10503 Type_conversion_expression* tce =
10504 static_cast<Type_conversion_expression*>(f);
10505 tce->set_may_convert_function_types();
10509 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
10512 Named_object* vno = gogo->lookup(receiver_name, NULL);
10513 gcc_assert(vno != NULL);
10514 Expression* ve = Expression::make_var_reference(vno, location);
10516 if (method != NULL)
10517 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
10519 bm = Expression::make_interface_field_reference(ve, name, location);
10521 // Even though we found the method above, if it has an error type we
10522 // may see an error here.
10523 if (bm->is_error_expression())
10525 gogo->finish_function(location);
10529 Expression_list* args;
10530 if (method_parameters == NULL)
10534 args = new Expression_list();
10535 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10536 p != method_parameters->end();
10539 vno = gogo->lookup(p->name(), NULL);
10540 gcc_assert(vno != NULL);
10541 args->push_back(Expression::make_var_reference(vno, location));
10545 Call_expression* call = Expression::make_call(bm, args,
10546 method_type->is_varargs(),
10549 size_t count = call->result_count();
10552 s = Statement::make_statement(call);
10555 Expression_list* retvals = new Expression_list();
10557 retvals->push_back(call);
10560 for (size_t i = 0; i < count; ++i)
10561 retvals->push_back(Expression::make_call_result(call, i));
10563 s = Statement::make_return_statement(retvals, location);
10565 gogo->add_statement(s);
10567 gogo->finish_function(location);
10569 return Expression::make_func_reference(no, NULL, location);
10572 // Make a selector expression.
10575 Expression::make_selector(Expression* left, const std::string& name,
10576 source_location location)
10578 return new Selector_expression(left, name, location);
10581 // Implement the builtin function new.
10583 class Allocation_expression : public Expression
10586 Allocation_expression(Type* type, source_location location)
10587 : Expression(EXPRESSION_ALLOCATION, location),
10593 do_traverse(Traverse* traverse)
10594 { return Type::traverse(this->type_, traverse); }
10598 { return Type::make_pointer_type(this->type_); }
10601 do_determine_type(const Type_context*)
10605 do_check_types(Gogo*);
10609 { return new Allocation_expression(this->type_, this->location()); }
10612 do_get_tree(Translate_context*);
10615 // The type we are allocating.
10619 // Check the type of an allocation expression.
10622 Allocation_expression::do_check_types(Gogo*)
10624 if (this->type_->function_type() != NULL)
10625 this->report_error(_("invalid new of function type"));
10628 // Return a tree for an allocation expression.
10631 Allocation_expression::do_get_tree(Translate_context* context)
10633 tree type_tree = this->type_->get_tree(context->gogo());
10634 if (type_tree == error_mark_node)
10635 return error_mark_node;
10636 tree size_tree = TYPE_SIZE_UNIT(type_tree);
10637 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
10639 if (space == error_mark_node)
10640 return error_mark_node;
10641 return fold_convert(build_pointer_type(type_tree), space);
10644 // Make an allocation expression.
10647 Expression::make_allocation(Type* type, source_location location)
10649 return new Allocation_expression(type, location);
10652 // Implement the builtin function make.
10654 class Make_expression : public Expression
10657 Make_expression(Type* type, Expression_list* args, source_location location)
10658 : Expression(EXPRESSION_MAKE, location),
10659 type_(type), args_(args)
10664 do_traverse(Traverse* traverse);
10668 { return this->type_; }
10671 do_determine_type(const Type_context*);
10674 do_check_types(Gogo*);
10679 return new Make_expression(this->type_, this->args_->copy(),
10684 do_get_tree(Translate_context*);
10687 // The type we are making.
10689 // The arguments to pass to the make routine.
10690 Expression_list* args_;
10696 Make_expression::do_traverse(Traverse* traverse)
10698 if (this->args_ != NULL
10699 && this->args_->traverse(traverse) == TRAVERSE_EXIT)
10700 return TRAVERSE_EXIT;
10701 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10702 return TRAVERSE_EXIT;
10703 return TRAVERSE_CONTINUE;
10706 // Set types of arguments.
10709 Make_expression::do_determine_type(const Type_context*)
10711 if (this->args_ != NULL)
10713 Type_context context(Type::lookup_integer_type("int"), false);
10714 for (Expression_list::const_iterator pe = this->args_->begin();
10715 pe != this->args_->end();
10717 (*pe)->determine_type(&context);
10721 // Check types for a make expression.
10724 Make_expression::do_check_types(Gogo*)
10726 if (this->type_->channel_type() == NULL
10727 && this->type_->map_type() == NULL
10728 && (this->type_->array_type() == NULL
10729 || this->type_->array_type()->length() != NULL))
10730 this->report_error(_("invalid type for make function"));
10731 else if (!this->type_->check_make_expression(this->args_, this->location()))
10732 this->set_is_error();
10735 // Return a tree for a make expression.
10738 Make_expression::do_get_tree(Translate_context* context)
10740 return this->type_->make_expression_tree(context, this->args_,
10744 // Make a make expression.
10747 Expression::make_make(Type* type, Expression_list* args,
10748 source_location location)
10750 return new Make_expression(type, args, location);
10753 // Construct a struct.
10755 class Struct_construction_expression : public Expression
10758 Struct_construction_expression(Type* type, Expression_list* vals,
10759 source_location location)
10760 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
10761 type_(type), vals_(vals)
10764 // Return whether this is a constant initializer.
10766 is_constant_struct() const;
10770 do_traverse(Traverse* traverse);
10774 { return this->type_; }
10777 do_determine_type(const Type_context*);
10780 do_check_types(Gogo*);
10785 return new Struct_construction_expression(this->type_, this->vals_->copy(),
10790 do_is_addressable() const
10794 do_get_tree(Translate_context*);
10797 do_export(Export*) const;
10800 // The type of the struct to construct.
10802 // The list of values, in order of the fields in the struct. A NULL
10803 // entry means that the field should be zero-initialized.
10804 Expression_list* vals_;
10810 Struct_construction_expression::do_traverse(Traverse* traverse)
10812 if (this->vals_ != NULL
10813 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10814 return TRAVERSE_EXIT;
10815 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10816 return TRAVERSE_EXIT;
10817 return TRAVERSE_CONTINUE;
10820 // Return whether this is a constant initializer.
10823 Struct_construction_expression::is_constant_struct() const
10825 if (this->vals_ == NULL)
10827 for (Expression_list::const_iterator pv = this->vals_->begin();
10828 pv != this->vals_->end();
10832 && !(*pv)->is_constant()
10833 && (!(*pv)->is_composite_literal()
10834 || (*pv)->is_nonconstant_composite_literal()))
10838 const Struct_field_list* fields = this->type_->struct_type()->fields();
10839 for (Struct_field_list::const_iterator pf = fields->begin();
10840 pf != fields->end();
10843 // There are no constant constructors for interfaces.
10844 if (pf->type()->interface_type() != NULL)
10851 // Final type determination.
10854 Struct_construction_expression::do_determine_type(const Type_context*)
10856 if (this->vals_ == NULL)
10858 const Struct_field_list* fields = this->type_->struct_type()->fields();
10859 Expression_list::const_iterator pv = this->vals_->begin();
10860 for (Struct_field_list::const_iterator pf = fields->begin();
10861 pf != fields->end();
10864 if (pv == this->vals_->end())
10868 Type_context subcontext(pf->type(), false);
10869 (*pv)->determine_type(&subcontext);
10872 // Extra values are an error we will report elsewhere; we still want
10873 // to determine the type to avoid knockon errors.
10874 for (; pv != this->vals_->end(); ++pv)
10875 (*pv)->determine_type_no_context();
10881 Struct_construction_expression::do_check_types(Gogo*)
10883 if (this->vals_ == NULL)
10886 Struct_type* st = this->type_->struct_type();
10887 if (this->vals_->size() > st->field_count())
10889 this->report_error(_("too many expressions for struct"));
10893 const Struct_field_list* fields = st->fields();
10894 Expression_list::const_iterator pv = this->vals_->begin();
10896 for (Struct_field_list::const_iterator pf = fields->begin();
10897 pf != fields->end();
10900 if (pv == this->vals_->end())
10902 this->report_error(_("too few expressions for struct"));
10909 std::string reason;
10910 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
10912 if (reason.empty())
10913 error_at((*pv)->location(),
10914 "incompatible type for field %d in struct construction",
10917 error_at((*pv)->location(),
10918 ("incompatible type for field %d in "
10919 "struct construction (%s)"),
10920 i + 1, reason.c_str());
10921 this->set_is_error();
10924 gcc_assert(pv == this->vals_->end());
10927 // Return a tree for constructing a struct.
10930 Struct_construction_expression::do_get_tree(Translate_context* context)
10932 Gogo* gogo = context->gogo();
10934 if (this->vals_ == NULL)
10935 return this->type_->get_init_tree(gogo, false);
10937 tree type_tree = this->type_->get_tree(gogo);
10938 if (type_tree == error_mark_node)
10939 return error_mark_node;
10940 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10942 bool is_constant = true;
10943 const Struct_field_list* fields = this->type_->struct_type()->fields();
10944 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
10946 Struct_field_list::const_iterator pf = fields->begin();
10947 Expression_list::const_iterator pv = this->vals_->begin();
10948 for (tree field = TYPE_FIELDS(type_tree);
10949 field != NULL_TREE;
10950 field = DECL_CHAIN(field), ++pf)
10952 gcc_assert(pf != fields->end());
10955 if (pv == this->vals_->end())
10956 val = pf->type()->get_init_tree(gogo, false);
10957 else if (*pv == NULL)
10959 val = pf->type()->get_init_tree(gogo, false);
10964 val = Expression::convert_for_assignment(context, pf->type(),
10966 (*pv)->get_tree(context),
10971 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
10972 return error_mark_node;
10974 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
10975 elt->index = field;
10977 if (!TREE_CONSTANT(val))
10978 is_constant = false;
10980 gcc_assert(pf == fields->end());
10982 tree ret = build_constructor(type_tree, elts);
10984 TREE_CONSTANT(ret) = 1;
10988 // Export a struct construction.
10991 Struct_construction_expression::do_export(Export* exp) const
10993 exp->write_c_string("convert(");
10994 exp->write_type(this->type_);
10995 for (Expression_list::const_iterator pv = this->vals_->begin();
10996 pv != this->vals_->end();
10999 exp->write_c_string(", ");
11001 (*pv)->export_expression(exp);
11003 exp->write_c_string(")");
11006 // Make a struct composite literal. This used by the thunk code.
11009 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
11010 source_location location)
11012 gcc_assert(type->struct_type() != NULL);
11013 return new Struct_construction_expression(type, vals, location);
11016 // Construct an array. This class is not used directly; instead we
11017 // use the child classes, Fixed_array_construction_expression and
11018 // Open_array_construction_expression.
11020 class Array_construction_expression : public Expression
11023 Array_construction_expression(Expression_classification classification,
11024 Type* type, Expression_list* vals,
11025 source_location location)
11026 : Expression(classification, location),
11027 type_(type), vals_(vals)
11031 // Return whether this is a constant initializer.
11033 is_constant_array() const;
11035 // Return the number of elements.
11037 element_count() const
11038 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
11042 do_traverse(Traverse* traverse);
11046 { return this->type_; }
11049 do_determine_type(const Type_context*);
11052 do_check_types(Gogo*);
11055 do_is_addressable() const
11059 do_export(Export*) const;
11061 // The list of values.
11064 { return this->vals_; }
11066 // Get a constructor tree for the array values.
11068 get_constructor_tree(Translate_context* context, tree type_tree);
11071 // The type of the array to construct.
11073 // The list of values.
11074 Expression_list* vals_;
11080 Array_construction_expression::do_traverse(Traverse* traverse)
11082 if (this->vals_ != NULL
11083 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11084 return TRAVERSE_EXIT;
11085 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11086 return TRAVERSE_EXIT;
11087 return TRAVERSE_CONTINUE;
11090 // Return whether this is a constant initializer.
11093 Array_construction_expression::is_constant_array() const
11095 if (this->vals_ == NULL)
11098 // There are no constant constructors for interfaces.
11099 if (this->type_->array_type()->element_type()->interface_type() != NULL)
11102 for (Expression_list::const_iterator pv = this->vals_->begin();
11103 pv != this->vals_->end();
11107 && !(*pv)->is_constant()
11108 && (!(*pv)->is_composite_literal()
11109 || (*pv)->is_nonconstant_composite_literal()))
11115 // Final type determination.
11118 Array_construction_expression::do_determine_type(const Type_context*)
11120 if (this->vals_ == NULL)
11122 Type_context subcontext(this->type_->array_type()->element_type(), false);
11123 for (Expression_list::const_iterator pv = this->vals_->begin();
11124 pv != this->vals_->end();
11128 (*pv)->determine_type(&subcontext);
11135 Array_construction_expression::do_check_types(Gogo*)
11137 if (this->vals_ == NULL)
11140 Array_type* at = this->type_->array_type();
11142 Type* element_type = at->element_type();
11143 for (Expression_list::const_iterator pv = this->vals_->begin();
11144 pv != this->vals_->end();
11148 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
11150 error_at((*pv)->location(),
11151 "incompatible type for element %d in composite literal",
11153 this->set_is_error();
11157 Expression* length = at->length();
11158 if (length != NULL)
11163 if (at->length()->integer_constant_value(true, val, &type))
11165 if (this->vals_->size() > mpz_get_ui(val))
11166 this->report_error(_("too many elements in composite literal"));
11172 // Get a constructor tree for the array values.
11175 Array_construction_expression::get_constructor_tree(Translate_context* context,
11178 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11179 (this->vals_ == NULL
11181 : this->vals_->size()));
11182 Type* element_type = this->type_->array_type()->element_type();
11183 bool is_constant = true;
11184 if (this->vals_ != NULL)
11187 for (Expression_list::const_iterator pv = this->vals_->begin();
11188 pv != this->vals_->end();
11191 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
11192 elt->index = size_int(i);
11194 elt->value = element_type->get_init_tree(context->gogo(), false);
11197 tree value_tree = (*pv)->get_tree(context);
11198 elt->value = Expression::convert_for_assignment(context,
11204 if (elt->value == error_mark_node)
11205 return error_mark_node;
11206 if (!TREE_CONSTANT(elt->value))
11207 is_constant = false;
11211 tree ret = build_constructor(type_tree, values);
11213 TREE_CONSTANT(ret) = 1;
11217 // Export an array construction.
11220 Array_construction_expression::do_export(Export* exp) const
11222 exp->write_c_string("convert(");
11223 exp->write_type(this->type_);
11224 if (this->vals_ != NULL)
11226 for (Expression_list::const_iterator pv = this->vals_->begin();
11227 pv != this->vals_->end();
11230 exp->write_c_string(", ");
11232 (*pv)->export_expression(exp);
11235 exp->write_c_string(")");
11238 // Construct a fixed array.
11240 class Fixed_array_construction_expression :
11241 public Array_construction_expression
11244 Fixed_array_construction_expression(Type* type, Expression_list* vals,
11245 source_location location)
11246 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
11247 type, vals, location)
11249 gcc_assert(type->array_type() != NULL
11250 && type->array_type()->length() != NULL);
11257 return new Fixed_array_construction_expression(this->type(),
11258 (this->vals() == NULL
11260 : this->vals()->copy()),
11265 do_get_tree(Translate_context*);
11268 // Return a tree for constructing a fixed array.
11271 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
11273 return this->get_constructor_tree(context,
11274 this->type()->get_tree(context->gogo()));
11277 // Construct an open array.
11279 class Open_array_construction_expression : public Array_construction_expression
11282 Open_array_construction_expression(Type* type, Expression_list* vals,
11283 source_location location)
11284 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
11285 type, vals, location)
11287 gcc_assert(type->array_type() != NULL
11288 && type->array_type()->length() == NULL);
11292 // Note that taking the address of an open array literal is invalid.
11297 return new Open_array_construction_expression(this->type(),
11298 (this->vals() == NULL
11300 : this->vals()->copy()),
11305 do_get_tree(Translate_context*);
11308 // Return a tree for constructing an open array.
11311 Open_array_construction_expression::do_get_tree(Translate_context* context)
11313 Array_type* array_type = this->type()->array_type();
11314 if (array_type == NULL)
11316 gcc_assert(this->type()->is_error());
11317 return error_mark_node;
11320 Type* element_type = array_type->element_type();
11321 tree element_type_tree = element_type->get_tree(context->gogo());
11322 if (element_type_tree == error_mark_node)
11323 return error_mark_node;
11327 if (this->vals() == NULL || this->vals()->empty())
11329 // We need to create a unique value.
11330 tree max = size_int(0);
11331 tree constructor_type = build_array_type(element_type_tree,
11332 build_index_type(max));
11333 if (constructor_type == error_mark_node)
11334 return error_mark_node;
11335 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
11336 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
11337 elt->index = size_int(0);
11338 elt->value = element_type->get_init_tree(context->gogo(), false);
11339 values = build_constructor(constructor_type, vec);
11340 if (TREE_CONSTANT(elt->value))
11341 TREE_CONSTANT(values) = 1;
11342 length_tree = size_int(0);
11346 tree max = size_int(this->vals()->size() - 1);
11347 tree constructor_type = build_array_type(element_type_tree,
11348 build_index_type(max));
11349 if (constructor_type == error_mark_node)
11350 return error_mark_node;
11351 values = this->get_constructor_tree(context, constructor_type);
11352 length_tree = size_int(this->vals()->size());
11355 if (values == error_mark_node)
11356 return error_mark_node;
11358 bool is_constant_initializer = TREE_CONSTANT(values);
11360 // We have to copy the initial values into heap memory if we are in
11361 // a function or if the values are not constants. We also have to
11362 // copy them if they may contain pointers in a non-constant context,
11363 // as otherwise the garbage collector won't see them.
11364 bool copy_to_heap = (context->function() != NULL
11365 || !is_constant_initializer
11366 || (element_type->has_pointer()
11367 && !context->is_const()));
11369 if (is_constant_initializer)
11371 tree tmp = build_decl(this->location(), VAR_DECL,
11372 create_tmp_var_name("C"), TREE_TYPE(values));
11373 DECL_EXTERNAL(tmp) = 0;
11374 TREE_PUBLIC(tmp) = 0;
11375 TREE_STATIC(tmp) = 1;
11376 DECL_ARTIFICIAL(tmp) = 1;
11379 // If we are not copying the value to the heap, we will only
11380 // initialize the value once, so we can use this directly
11381 // rather than copying it. In that case we can't make it
11382 // read-only, because the program is permitted to change it.
11383 TREE_READONLY(tmp) = 1;
11384 TREE_CONSTANT(tmp) = 1;
11386 DECL_INITIAL(tmp) = values;
11387 rest_of_decl_compilation(tmp, 1, 0);
11395 // the initializer will only run once.
11396 space = build_fold_addr_expr(values);
11401 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
11402 space = context->gogo()->allocate_memory(element_type, memsize,
11404 space = save_expr(space);
11406 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
11407 tree ref = build_fold_indirect_ref_loc(this->location(), s);
11408 TREE_THIS_NOTRAP(ref) = 1;
11409 set = build2(MODIFY_EXPR, void_type_node, ref, values);
11412 // Build a constructor for the open array.
11414 tree type_tree = this->type()->get_tree(context->gogo());
11415 if (type_tree == error_mark_node)
11416 return error_mark_node;
11417 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11419 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
11421 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
11422 tree field = TYPE_FIELDS(type_tree);
11423 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
11424 elt->index = field;
11425 elt->value = fold_convert(TREE_TYPE(field), space);
11427 elt = VEC_quick_push(constructor_elt, init, NULL);
11428 field = DECL_CHAIN(field);
11429 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
11430 elt->index = field;
11431 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11433 elt = VEC_quick_push(constructor_elt, init, NULL);
11434 field = DECL_CHAIN(field);
11435 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
11436 elt->index = field;
11437 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11439 tree constructor = build_constructor(type_tree, init);
11440 if (constructor == error_mark_node)
11441 return error_mark_node;
11443 TREE_CONSTANT(constructor) = 1;
11445 if (set == NULL_TREE)
11446 return constructor;
11448 return build2(COMPOUND_EXPR, type_tree, set, constructor);
11451 // Make a slice composite literal. This is used by the type
11452 // descriptor code.
11455 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
11456 source_location location)
11458 gcc_assert(type->is_open_array_type());
11459 return new Open_array_construction_expression(type, vals, location);
11462 // Construct a map.
11464 class Map_construction_expression : public Expression
11467 Map_construction_expression(Type* type, Expression_list* vals,
11468 source_location location)
11469 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
11470 type_(type), vals_(vals)
11471 { gcc_assert(vals == NULL || vals->size() % 2 == 0); }
11475 do_traverse(Traverse* traverse);
11479 { return this->type_; }
11482 do_determine_type(const Type_context*);
11485 do_check_types(Gogo*);
11490 return new Map_construction_expression(this->type_, this->vals_->copy(),
11495 do_get_tree(Translate_context*);
11498 do_export(Export*) const;
11501 // The type of the map to construct.
11503 // The list of values.
11504 Expression_list* vals_;
11510 Map_construction_expression::do_traverse(Traverse* traverse)
11512 if (this->vals_ != NULL
11513 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11514 return TRAVERSE_EXIT;
11515 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11516 return TRAVERSE_EXIT;
11517 return TRAVERSE_CONTINUE;
11520 // Final type determination.
11523 Map_construction_expression::do_determine_type(const Type_context*)
11525 if (this->vals_ == NULL)
11528 Map_type* mt = this->type_->map_type();
11529 Type_context key_context(mt->key_type(), false);
11530 Type_context val_context(mt->val_type(), false);
11531 for (Expression_list::const_iterator pv = this->vals_->begin();
11532 pv != this->vals_->end();
11535 (*pv)->determine_type(&key_context);
11537 (*pv)->determine_type(&val_context);
11544 Map_construction_expression::do_check_types(Gogo*)
11546 if (this->vals_ == NULL)
11549 Map_type* mt = this->type_->map_type();
11551 Type* key_type = mt->key_type();
11552 Type* val_type = mt->val_type();
11553 for (Expression_list::const_iterator pv = this->vals_->begin();
11554 pv != this->vals_->end();
11557 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
11559 error_at((*pv)->location(),
11560 "incompatible type for element %d key in map construction",
11562 this->set_is_error();
11565 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
11567 error_at((*pv)->location(),
11568 ("incompatible type for element %d value "
11569 "in map construction"),
11571 this->set_is_error();
11576 // Return a tree for constructing a map.
11579 Map_construction_expression::do_get_tree(Translate_context* context)
11581 Gogo* gogo = context->gogo();
11582 source_location loc = this->location();
11584 Map_type* mt = this->type_->map_type();
11586 // Build a struct to hold the key and value.
11587 tree struct_type = make_node(RECORD_TYPE);
11589 Type* key_type = mt->key_type();
11590 tree id = get_identifier("__key");
11591 tree key_type_tree = key_type->get_tree(gogo);
11592 if (key_type_tree == error_mark_node)
11593 return error_mark_node;
11594 tree key_field = build_decl(loc, FIELD_DECL, id, key_type_tree);
11595 DECL_CONTEXT(key_field) = struct_type;
11596 TYPE_FIELDS(struct_type) = key_field;
11598 Type* val_type = mt->val_type();
11599 id = get_identifier("__val");
11600 tree val_type_tree = val_type->get_tree(gogo);
11601 if (val_type_tree == error_mark_node)
11602 return error_mark_node;
11603 tree val_field = build_decl(loc, FIELD_DECL, id, val_type_tree);
11604 DECL_CONTEXT(val_field) = struct_type;
11605 DECL_CHAIN(key_field) = val_field;
11607 layout_type(struct_type);
11609 bool is_constant = true;
11614 if (this->vals_ == NULL || this->vals_->empty())
11616 valaddr = null_pointer_node;
11617 make_tmp = NULL_TREE;
11621 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11622 this->vals_->size() / 2);
11624 for (Expression_list::const_iterator pv = this->vals_->begin();
11625 pv != this->vals_->end();
11628 bool one_is_constant = true;
11630 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
11632 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
11633 elt->index = key_field;
11634 tree val_tree = (*pv)->get_tree(context);
11635 elt->value = Expression::convert_for_assignment(context, key_type,
11638 if (elt->value == error_mark_node)
11639 return error_mark_node;
11640 if (!TREE_CONSTANT(elt->value))
11641 one_is_constant = false;
11645 elt = VEC_quick_push(constructor_elt, one, NULL);
11646 elt->index = val_field;
11647 val_tree = (*pv)->get_tree(context);
11648 elt->value = Expression::convert_for_assignment(context, val_type,
11651 if (elt->value == error_mark_node)
11652 return error_mark_node;
11653 if (!TREE_CONSTANT(elt->value))
11654 one_is_constant = false;
11656 elt = VEC_quick_push(constructor_elt, values, NULL);
11657 elt->index = size_int(i);
11658 elt->value = build_constructor(struct_type, one);
11659 if (one_is_constant)
11660 TREE_CONSTANT(elt->value) = 1;
11662 is_constant = false;
11665 tree index_type = build_index_type(size_int(i - 1));
11666 tree array_type = build_array_type(struct_type, index_type);
11667 tree init = build_constructor(array_type, values);
11669 TREE_CONSTANT(init) = 1;
11671 if (current_function_decl != NULL)
11673 tmp = create_tmp_var(array_type, get_name(array_type));
11674 DECL_INITIAL(tmp) = init;
11675 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
11676 TREE_ADDRESSABLE(tmp) = 1;
11680 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
11681 DECL_EXTERNAL(tmp) = 0;
11682 TREE_PUBLIC(tmp) = 0;
11683 TREE_STATIC(tmp) = 1;
11684 DECL_ARTIFICIAL(tmp) = 1;
11685 if (!TREE_CONSTANT(init))
11686 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
11690 TREE_READONLY(tmp) = 1;
11691 TREE_CONSTANT(tmp) = 1;
11692 DECL_INITIAL(tmp) = init;
11693 make_tmp = NULL_TREE;
11695 rest_of_decl_compilation(tmp, 1, 0);
11698 valaddr = build_fold_addr_expr(tmp);
11701 tree descriptor = gogo->map_descriptor(mt);
11703 tree type_tree = this->type_->get_tree(gogo);
11704 if (type_tree == error_mark_node)
11705 return error_mark_node;
11707 static tree construct_map_fndecl;
11708 tree call = Gogo::call_builtin(&construct_map_fndecl,
11710 "__go_construct_map",
11713 TREE_TYPE(descriptor),
11718 TYPE_SIZE_UNIT(struct_type),
11720 byte_position(val_field),
11722 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
11723 const_ptr_type_node,
11724 fold_convert(const_ptr_type_node, valaddr));
11725 if (call == error_mark_node)
11726 return error_mark_node;
11729 if (make_tmp == NULL)
11732 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
11736 // Export an array construction.
11739 Map_construction_expression::do_export(Export* exp) const
11741 exp->write_c_string("convert(");
11742 exp->write_type(this->type_);
11743 for (Expression_list::const_iterator pv = this->vals_->begin();
11744 pv != this->vals_->end();
11747 exp->write_c_string(", ");
11748 (*pv)->export_expression(exp);
11750 exp->write_c_string(")");
11753 // A general composite literal. This is lowered to a type specific
11756 class Composite_literal_expression : public Parser_expression
11759 Composite_literal_expression(Type* type, int depth, bool has_keys,
11760 Expression_list* vals, source_location location)
11761 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
11762 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
11767 do_traverse(Traverse* traverse);
11770 do_lower(Gogo*, Named_object*, int);
11775 return new Composite_literal_expression(this->type_, this->depth_,
11777 (this->vals_ == NULL
11779 : this->vals_->copy()),
11785 lower_struct(Gogo*, Type*);
11788 lower_array(Type*);
11791 make_array(Type*, Expression_list*);
11794 lower_map(Gogo*, Named_object*, Type*);
11796 // The type of the composite literal.
11798 // The depth within a list of composite literals within a composite
11799 // literal, when the type is omitted.
11801 // The values to put in the composite literal.
11802 Expression_list* vals_;
11803 // If this is true, then VALS_ is a list of pairs: a key and a
11804 // value. In an array initializer, a missing key will be NULL.
11811 Composite_literal_expression::do_traverse(Traverse* traverse)
11813 if (this->vals_ != NULL
11814 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11815 return TRAVERSE_EXIT;
11816 return Type::traverse(this->type_, traverse);
11819 // Lower a generic composite literal into a specific version based on
11823 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int)
11825 Type* type = this->type_;
11827 for (int depth = this->depth_; depth > 0; --depth)
11829 if (type->array_type() != NULL)
11830 type = type->array_type()->element_type();
11831 else if (type->map_type() != NULL)
11832 type = type->map_type()->val_type();
11835 if (!type->is_error())
11836 error_at(this->location(),
11837 ("may only omit types within composite literals "
11838 "of slice, array, or map type"));
11839 return Expression::make_error(this->location());
11843 if (type->is_error())
11844 return Expression::make_error(this->location());
11845 else if (type->struct_type() != NULL)
11846 return this->lower_struct(gogo, type);
11847 else if (type->array_type() != NULL)
11848 return this->lower_array(type);
11849 else if (type->map_type() != NULL)
11850 return this->lower_map(gogo, function, type);
11853 error_at(this->location(),
11854 ("expected struct, slice, array, or map type "
11855 "for composite literal"));
11856 return Expression::make_error(this->location());
11860 // Lower a struct composite literal.
11863 Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
11865 source_location location = this->location();
11866 Struct_type* st = type->struct_type();
11867 if (this->vals_ == NULL || !this->has_keys_)
11868 return new Struct_construction_expression(type, this->vals_, location);
11870 size_t field_count = st->field_count();
11871 std::vector<Expression*> vals(field_count);
11872 Expression_list::const_iterator p = this->vals_->begin();
11873 while (p != this->vals_->end())
11875 Expression* name_expr = *p;
11878 gcc_assert(p != this->vals_->end());
11879 Expression* val = *p;
11883 if (name_expr == NULL)
11885 error_at(val->location(), "mixture of field and value initializers");
11886 return Expression::make_error(location);
11889 bool bad_key = false;
11891 const Named_object* no = NULL;
11892 switch (name_expr->classification())
11894 case EXPRESSION_UNKNOWN_REFERENCE:
11895 name = name_expr->unknown_expression()->name();
11898 case EXPRESSION_CONST_REFERENCE:
11899 no = static_cast<Const_expression*>(name_expr)->named_object();
11902 case EXPRESSION_TYPE:
11904 Type* t = name_expr->type();
11905 Named_type* nt = t->named_type();
11909 no = nt->named_object();
11913 case EXPRESSION_VAR_REFERENCE:
11914 no = name_expr->var_expression()->named_object();
11917 case EXPRESSION_FUNC_REFERENCE:
11918 no = name_expr->func_expression()->named_object();
11921 case EXPRESSION_UNARY:
11922 // If there is a local variable around with the same name as
11923 // the field, and this occurs in the closure, then the
11924 // parser may turn the field reference into an indirection
11925 // through the closure. FIXME: This is a mess.
11928 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
11929 if (ue->op() == OPERATOR_MULT)
11931 Field_reference_expression* fre =
11932 ue->operand()->field_reference_expression();
11936 fre->expr()->type()->deref()->struct_type();
11939 const Struct_field* sf = st->field(fre->field_index());
11940 name = sf->field_name();
11942 snprintf(buf, sizeof buf, "%u", fre->field_index());
11943 size_t buflen = strlen(buf);
11944 if (name.compare(name.length() - buflen, buflen, buf)
11947 name = name.substr(0, name.length() - buflen);
11962 error_at(name_expr->location(), "expected struct field name");
11963 return Expression::make_error(location);
11970 // A predefined name won't be packed. If it starts with a
11971 // lower case letter we need to check for that case, because
11972 // the field name will be packed.
11973 if (!Gogo::is_hidden_name(name)
11977 Named_object* gno = gogo->lookup_global(name.c_str());
11979 name = gogo->pack_hidden_name(name, false);
11983 unsigned int index;
11984 const Struct_field* sf = st->find_local_field(name, &index);
11987 error_at(name_expr->location(), "unknown field %qs in %qs",
11988 Gogo::message_name(name).c_str(),
11989 (type->named_type() != NULL
11990 ? type->named_type()->message_name().c_str()
11991 : "unnamed struct"));
11992 return Expression::make_error(location);
11994 if (vals[index] != NULL)
11996 error_at(name_expr->location(),
11997 "duplicate value for field %qs in %qs",
11998 Gogo::message_name(name).c_str(),
11999 (type->named_type() != NULL
12000 ? type->named_type()->message_name().c_str()
12001 : "unnamed struct"));
12002 return Expression::make_error(location);
12008 Expression_list* list = new Expression_list;
12009 list->reserve(field_count);
12010 for (size_t i = 0; i < field_count; ++i)
12011 list->push_back(vals[i]);
12013 return new Struct_construction_expression(type, list, location);
12016 // Lower an array composite literal.
12019 Composite_literal_expression::lower_array(Type* type)
12021 source_location location = this->location();
12022 if (this->vals_ == NULL || !this->has_keys_)
12023 return this->make_array(type, this->vals_);
12025 std::vector<Expression*> vals;
12026 vals.reserve(this->vals_->size());
12027 unsigned long index = 0;
12028 Expression_list::const_iterator p = this->vals_->begin();
12029 while (p != this->vals_->end())
12031 Expression* index_expr = *p;
12034 gcc_assert(p != this->vals_->end());
12035 Expression* val = *p;
12039 if (index_expr != NULL)
12045 if (!index_expr->integer_constant_value(true, ival, &dummy))
12048 error_at(index_expr->location(),
12049 "index expression is not integer constant");
12050 return Expression::make_error(location);
12053 if (mpz_sgn(ival) < 0)
12056 error_at(index_expr->location(), "index expression is negative");
12057 return Expression::make_error(location);
12060 index = mpz_get_ui(ival);
12061 if (mpz_cmp_ui(ival, index) != 0)
12064 error_at(index_expr->location(), "index value overflow");
12065 return Expression::make_error(location);
12068 Named_type* ntype = Type::lookup_integer_type("int");
12069 Integer_type* inttype = ntype->integer_type();
12071 mpz_init_set_ui(max, 1);
12072 mpz_mul_2exp(max, max, inttype->bits() - 1);
12073 bool ok = mpz_cmp(ival, max) < 0;
12078 error_at(index_expr->location(), "index value overflow");
12079 return Expression::make_error(location);
12084 // FIXME: Our representation isn't very good; this avoids
12086 if (index > 0x1000000)
12088 error_at(index_expr->location(), "index too large for compiler");
12089 return Expression::make_error(location);
12093 if (index == vals.size())
12094 vals.push_back(val);
12097 if (index > vals.size())
12099 vals.reserve(index + 32);
12100 vals.resize(index + 1, static_cast<Expression*>(NULL));
12102 if (vals[index] != NULL)
12104 error_at((index_expr != NULL
12105 ? index_expr->location()
12106 : val->location()),
12107 "duplicate value for index %lu",
12109 return Expression::make_error(location);
12117 size_t size = vals.size();
12118 Expression_list* list = new Expression_list;
12119 list->reserve(size);
12120 for (size_t i = 0; i < size; ++i)
12121 list->push_back(vals[i]);
12123 return this->make_array(type, list);
12126 // Actually build the array composite literal. This handles
12130 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
12132 source_location location = this->location();
12133 Array_type* at = type->array_type();
12134 if (at->length() != NULL && at->length()->is_nil_expression())
12136 size_t size = vals == NULL ? 0 : vals->size();
12138 mpz_init_set_ui(vlen, size);
12139 Expression* elen = Expression::make_integer(&vlen, NULL, location);
12141 at = Type::make_array_type(at->element_type(), elen);
12144 if (at->length() != NULL)
12145 return new Fixed_array_construction_expression(type, vals, location);
12147 return new Open_array_construction_expression(type, vals, location);
12150 // Lower a map composite literal.
12153 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
12156 source_location location = this->location();
12157 if (this->vals_ != NULL)
12159 if (!this->has_keys_)
12161 error_at(location, "map composite literal must have keys");
12162 return Expression::make_error(location);
12165 for (Expression_list::iterator p = this->vals_->begin();
12166 p != this->vals_->end();
12172 error_at((*p)->location(),
12173 "map composite literal must have keys for every value");
12174 return Expression::make_error(location);
12176 // Make sure we have lowered the key; it may not have been
12177 // lowered in order to handle keys for struct composite
12178 // literals. Lower it now to get the right error message.
12179 if ((*p)->unknown_expression() != NULL)
12181 (*p)->unknown_expression()->clear_is_composite_literal_key();
12182 gogo->lower_expression(function, &*p);
12183 gcc_assert((*p)->is_error_expression());
12184 return Expression::make_error(location);
12189 return new Map_construction_expression(type, this->vals_, location);
12192 // Make a composite literal expression.
12195 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
12196 Expression_list* vals,
12197 source_location location)
12199 return new Composite_literal_expression(type, depth, has_keys, vals,
12203 // Return whether this expression is a composite literal.
12206 Expression::is_composite_literal() const
12208 switch (this->classification_)
12210 case EXPRESSION_COMPOSITE_LITERAL:
12211 case EXPRESSION_STRUCT_CONSTRUCTION:
12212 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12213 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12214 case EXPRESSION_MAP_CONSTRUCTION:
12221 // Return whether this expression is a composite literal which is not
12225 Expression::is_nonconstant_composite_literal() const
12227 switch (this->classification_)
12229 case EXPRESSION_STRUCT_CONSTRUCTION:
12231 const Struct_construction_expression *psce =
12232 static_cast<const Struct_construction_expression*>(this);
12233 return !psce->is_constant_struct();
12235 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12237 const Fixed_array_construction_expression *pace =
12238 static_cast<const Fixed_array_construction_expression*>(this);
12239 return !pace->is_constant_array();
12241 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12243 const Open_array_construction_expression *pace =
12244 static_cast<const Open_array_construction_expression*>(this);
12245 return !pace->is_constant_array();
12247 case EXPRESSION_MAP_CONSTRUCTION:
12254 // Return true if this is a reference to a local variable.
12257 Expression::is_local_variable() const
12259 const Var_expression* ve = this->var_expression();
12262 const Named_object* no = ve->named_object();
12263 return (no->is_result_variable()
12264 || (no->is_variable() && !no->var_value()->is_global()));
12267 // Class Type_guard_expression.
12272 Type_guard_expression::do_traverse(Traverse* traverse)
12274 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
12275 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12276 return TRAVERSE_EXIT;
12277 return TRAVERSE_CONTINUE;
12280 // Check types of a type guard expression. The expression must have
12281 // an interface type, but the actual type conversion is checked at run
12285 Type_guard_expression::do_check_types(Gogo*)
12287 // 6g permits using a type guard with unsafe.pointer; we are
12289 Type* expr_type = this->expr_->type();
12290 if (expr_type->is_unsafe_pointer_type())
12292 if (this->type_->points_to() == NULL
12293 && (this->type_->integer_type() == NULL
12294 || (this->type_->forwarded()
12295 != Type::lookup_integer_type("uintptr"))))
12296 this->report_error(_("invalid unsafe.Pointer conversion"));
12298 else if (this->type_->is_unsafe_pointer_type())
12300 if (expr_type->points_to() == NULL
12301 && (expr_type->integer_type() == NULL
12302 || (expr_type->forwarded()
12303 != Type::lookup_integer_type("uintptr"))))
12304 this->report_error(_("invalid unsafe.Pointer conversion"));
12306 else if (expr_type->interface_type() == NULL)
12308 if (!expr_type->is_error() && !this->type_->is_error())
12309 this->report_error(_("type assertion only valid for interface types"));
12310 this->set_is_error();
12312 else if (this->type_->interface_type() == NULL)
12314 std::string reason;
12315 if (!expr_type->interface_type()->implements_interface(this->type_,
12318 if (!this->type_->is_error())
12320 if (reason.empty())
12321 this->report_error(_("impossible type assertion: "
12322 "type does not implement interface"));
12324 error_at(this->location(),
12325 ("impossible type assertion: "
12326 "type does not implement interface (%s)"),
12329 this->set_is_error();
12334 // Return a tree for a type guard expression.
12337 Type_guard_expression::do_get_tree(Translate_context* context)
12339 Gogo* gogo = context->gogo();
12340 tree expr_tree = this->expr_->get_tree(context);
12341 if (expr_tree == error_mark_node)
12342 return error_mark_node;
12343 Type* expr_type = this->expr_->type();
12344 if ((this->type_->is_unsafe_pointer_type()
12345 && (expr_type->points_to() != NULL
12346 || expr_type->integer_type() != NULL))
12347 || (expr_type->is_unsafe_pointer_type()
12348 && this->type_->points_to() != NULL))
12349 return convert_to_pointer(this->type_->get_tree(gogo), expr_tree);
12350 else if (expr_type->is_unsafe_pointer_type()
12351 && this->type_->integer_type() != NULL)
12352 return convert_to_integer(this->type_->get_tree(gogo), expr_tree);
12353 else if (this->type_->interface_type() != NULL)
12354 return Expression::convert_interface_to_interface(context, this->type_,
12355 this->expr_->type(),
12359 return Expression::convert_for_assignment(context, this->type_,
12360 this->expr_->type(), expr_tree,
12364 // Make a type guard expression.
12367 Expression::make_type_guard(Expression* expr, Type* type,
12368 source_location location)
12370 return new Type_guard_expression(expr, type, location);
12373 // Class Heap_composite_expression.
12375 // When you take the address of a composite literal, it is allocated
12376 // on the heap. This class implements that.
12378 class Heap_composite_expression : public Expression
12381 Heap_composite_expression(Expression* expr, source_location location)
12382 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
12388 do_traverse(Traverse* traverse)
12389 { return Expression::traverse(&this->expr_, traverse); }
12393 { return Type::make_pointer_type(this->expr_->type()); }
12396 do_determine_type(const Type_context*)
12397 { this->expr_->determine_type_no_context(); }
12402 return Expression::make_heap_composite(this->expr_->copy(),
12407 do_get_tree(Translate_context*);
12409 // We only export global objects, and the parser does not generate
12410 // this in global scope.
12412 do_export(Export*) const
12413 { gcc_unreachable(); }
12416 // The composite literal which is being put on the heap.
12420 // Return a tree which allocates a composite literal on the heap.
12423 Heap_composite_expression::do_get_tree(Translate_context* context)
12425 tree expr_tree = this->expr_->get_tree(context);
12426 if (expr_tree == error_mark_node)
12427 return error_mark_node;
12428 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
12429 gcc_assert(TREE_CODE(expr_size) == INTEGER_CST);
12430 tree space = context->gogo()->allocate_memory(this->expr_->type(),
12431 expr_size, this->location());
12432 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
12433 space = save_expr(space);
12434 tree ref = build_fold_indirect_ref_loc(this->location(), space);
12435 TREE_THIS_NOTRAP(ref) = 1;
12436 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
12437 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
12439 SET_EXPR_LOCATION(ret, this->location());
12443 // Allocate a composite literal on the heap.
12446 Expression::make_heap_composite(Expression* expr, source_location location)
12448 return new Heap_composite_expression(expr, location);
12451 // Class Receive_expression.
12453 // Return the type of a receive expression.
12456 Receive_expression::do_type()
12458 Channel_type* channel_type = this->channel_->type()->channel_type();
12459 if (channel_type == NULL)
12460 return Type::make_error_type();
12461 return channel_type->element_type();
12464 // Check types for a receive expression.
12467 Receive_expression::do_check_types(Gogo*)
12469 Type* type = this->channel_->type();
12470 if (type->is_error())
12472 this->set_is_error();
12475 if (type->channel_type() == NULL)
12477 this->report_error(_("expected channel"));
12480 if (!type->channel_type()->may_receive())
12482 this->report_error(_("invalid receive on send-only channel"));
12487 // Get a tree for a receive expression.
12490 Receive_expression::do_get_tree(Translate_context* context)
12492 Channel_type* channel_type = this->channel_->type()->channel_type();
12493 if (channel_type == NULL)
12495 gcc_assert(this->channel_->type()->is_error());
12496 return error_mark_node;
12498 Type* element_type = channel_type->element_type();
12499 tree element_type_tree = element_type->get_tree(context->gogo());
12501 tree channel = this->channel_->get_tree(context);
12502 if (element_type_tree == error_mark_node || channel == error_mark_node)
12503 return error_mark_node;
12505 return Gogo::receive_from_channel(element_type_tree, channel,
12506 this->for_select_, this->location());
12509 // Make a receive expression.
12511 Receive_expression*
12512 Expression::make_receive(Expression* channel, source_location location)
12514 return new Receive_expression(channel, location);
12517 // An expression which evaluates to a pointer to the type descriptor
12520 class Type_descriptor_expression : public Expression
12523 Type_descriptor_expression(Type* type, source_location location)
12524 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
12531 { return Type::make_type_descriptor_ptr_type(); }
12534 do_determine_type(const Type_context*)
12542 do_get_tree(Translate_context* context)
12543 { return this->type_->type_descriptor_pointer(context->gogo()); }
12546 // The type for which this is the descriptor.
12550 // Make a type descriptor expression.
12553 Expression::make_type_descriptor(Type* type, source_location location)
12555 return new Type_descriptor_expression(type, location);
12558 // An expression which evaluates to some characteristic of a type.
12559 // This is only used to initialize fields of a type descriptor. Using
12560 // a new expression class is slightly inefficient but gives us a good
12561 // separation between the frontend and the middle-end with regard to
12562 // how types are laid out.
12564 class Type_info_expression : public Expression
12567 Type_info_expression(Type* type, Type_info type_info)
12568 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
12569 type_(type), type_info_(type_info)
12577 do_determine_type(const Type_context*)
12585 do_get_tree(Translate_context* context);
12588 // The type for which we are getting information.
12590 // What information we want.
12591 Type_info type_info_;
12594 // The type is chosen to match what the type descriptor struct
12598 Type_info_expression::do_type()
12600 switch (this->type_info_)
12602 case TYPE_INFO_SIZE:
12603 return Type::lookup_integer_type("uintptr");
12604 case TYPE_INFO_ALIGNMENT:
12605 case TYPE_INFO_FIELD_ALIGNMENT:
12606 return Type::lookup_integer_type("uint8");
12612 // Return type information in GENERIC.
12615 Type_info_expression::do_get_tree(Translate_context* context)
12617 tree type_tree = this->type_->get_tree(context->gogo());
12618 if (type_tree == error_mark_node)
12619 return error_mark_node;
12621 tree val_type_tree = this->type()->get_tree(context->gogo());
12622 gcc_assert(val_type_tree != error_mark_node);
12624 if (this->type_info_ == TYPE_INFO_SIZE)
12625 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12626 TYPE_SIZE_UNIT(type_tree));
12630 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
12631 val = go_type_alignment(type_tree);
12633 val = go_field_alignment(type_tree);
12634 return build_int_cstu(val_type_tree, val);
12638 // Make a type info expression.
12641 Expression::make_type_info(Type* type, Type_info type_info)
12643 return new Type_info_expression(type, type_info);
12646 // An expression which evaluates to the offset of a field within a
12647 // struct. This, like Type_info_expression, q.v., is only used to
12648 // initialize fields of a type descriptor.
12650 class Struct_field_offset_expression : public Expression
12653 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
12654 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
12655 type_(type), field_(field)
12661 { return Type::lookup_integer_type("uintptr"); }
12664 do_determine_type(const Type_context*)
12672 do_get_tree(Translate_context* context);
12675 // The type of the struct.
12676 Struct_type* type_;
12678 const Struct_field* field_;
12681 // Return a struct field offset in GENERIC.
12684 Struct_field_offset_expression::do_get_tree(Translate_context* context)
12686 tree type_tree = this->type_->get_tree(context->gogo());
12687 if (type_tree == error_mark_node)
12688 return error_mark_node;
12690 tree val_type_tree = this->type()->get_tree(context->gogo());
12691 gcc_assert(val_type_tree != error_mark_node);
12693 const Struct_field_list* fields = this->type_->fields();
12694 tree struct_field_tree = TYPE_FIELDS(type_tree);
12695 Struct_field_list::const_iterator p;
12696 for (p = fields->begin();
12697 p != fields->end();
12698 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
12700 gcc_assert(struct_field_tree != NULL_TREE);
12701 if (&*p == this->field_)
12704 gcc_assert(&*p == this->field_);
12706 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12707 byte_position(struct_field_tree));
12710 // Make an expression for a struct field offset.
12713 Expression::make_struct_field_offset(Struct_type* type,
12714 const Struct_field* field)
12716 return new Struct_field_offset_expression(type, field);
12719 // An expression which evaluates to the address of an unnamed label.
12721 class Label_addr_expression : public Expression
12724 Label_addr_expression(Label* label, source_location location)
12725 : Expression(EXPRESSION_LABEL_ADDR, location),
12732 { return Type::make_pointer_type(Type::make_void_type()); }
12735 do_determine_type(const Type_context*)
12740 { return new Label_addr_expression(this->label_, this->location()); }
12743 do_get_tree(Translate_context* context)
12745 return expr_to_tree(this->label_->get_addr(context, this->location()));
12749 // The label whose address we are taking.
12753 // Make an expression for the address of an unnamed label.
12756 Expression::make_label_addr(Label* label, source_location location)
12758 return new Label_addr_expression(label, location);
12761 // Import an expression. This comes at the end in order to see the
12762 // various class definitions.
12765 Expression::import_expression(Import* imp)
12767 int c = imp->peek_char();
12768 if (imp->match_c_string("- ")
12769 || imp->match_c_string("! ")
12770 || imp->match_c_string("^ "))
12771 return Unary_expression::do_import(imp);
12773 return Binary_expression::do_import(imp);
12774 else if (imp->match_c_string("true")
12775 || imp->match_c_string("false"))
12776 return Boolean_expression::do_import(imp);
12778 return String_expression::do_import(imp);
12779 else if (c == '-' || (c >= '0' && c <= '9'))
12781 // This handles integers, floats and complex constants.
12782 return Integer_expression::do_import(imp);
12784 else if (imp->match_c_string("nil"))
12785 return Nil_expression::do_import(imp);
12786 else if (imp->match_c_string("convert"))
12787 return Type_conversion_expression::do_import(imp);
12790 error_at(imp->location(), "import error: expected expression");
12791 return Expression::make_error(imp->location());
12795 // Class Expression_list.
12797 // Traverse the list.
12800 Expression_list::traverse(Traverse* traverse)
12802 for (Expression_list::iterator p = this->begin();
12808 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12809 return TRAVERSE_EXIT;
12812 return TRAVERSE_CONTINUE;
12818 Expression_list::copy()
12820 Expression_list* ret = new Expression_list();
12821 for (Expression_list::iterator p = this->begin();
12826 ret->push_back(NULL);
12828 ret->push_back((*p)->copy());
12833 // Return whether an expression list has an error expression.
12836 Expression_list::contains_error() const
12838 for (Expression_list::const_iterator p = this->begin();
12841 if (*p != NULL && (*p)->is_error_expression())