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 return this->variable_->get_tree(context->gogo(), context->function());
978 // Make a reference to a variable in an expression.
981 Expression::make_var_reference(Named_object* var, source_location location)
984 return Expression::make_sink(location);
986 // FIXME: Creating a new object for each reference to a variable is
988 return new Var_expression(var, location);
991 // Class Temporary_reference_expression.
996 Temporary_reference_expression::do_type()
998 return this->statement_->type();
1001 // Called if something takes the address of this temporary variable.
1002 // We never have to move temporary variables to the heap, but we do
1003 // need to know that they must live in the stack rather than in a
1007 Temporary_reference_expression::do_address_taken(bool)
1009 this->statement_->set_is_address_taken();
1012 // Get a tree referring to the variable.
1015 Temporary_reference_expression::do_get_tree(Translate_context*)
1017 return this->statement_->get_decl();
1020 // Make a reference to a temporary variable.
1023 Expression::make_temporary_reference(Temporary_statement* statement,
1024 source_location location)
1026 return new Temporary_reference_expression(statement, location);
1029 // A sink expression--a use of the blank identifier _.
1031 class Sink_expression : public Expression
1034 Sink_expression(source_location location)
1035 : Expression(EXPRESSION_SINK, location),
1036 type_(NULL), var_(NULL_TREE)
1041 do_discarding_value()
1048 do_determine_type(const Type_context*);
1052 { return new Sink_expression(this->location()); }
1055 do_get_tree(Translate_context*);
1058 // The type of this sink variable.
1060 // The temporary variable we generate.
1064 // Return the type of a sink expression.
1067 Sink_expression::do_type()
1069 if (this->type_ == NULL)
1070 return Type::make_sink_type();
1074 // Determine the type of a sink expression.
1077 Sink_expression::do_determine_type(const Type_context* context)
1079 if (context->type != NULL)
1080 this->type_ = context->type;
1083 // Return a temporary variable for a sink expression. This will
1084 // presumably be a write-only variable which the middle-end will drop.
1087 Sink_expression::do_get_tree(Translate_context* context)
1089 if (this->var_ == NULL_TREE)
1091 gcc_assert(this->type_ != NULL && !this->type_->is_sink_type());
1092 this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()),
1098 // Make a sink expression.
1101 Expression::make_sink(source_location location)
1103 return new Sink_expression(location);
1106 // Class Func_expression.
1108 // FIXME: Can a function expression appear in a constant expression?
1109 // The value is unchanging. Initializing a constant to the address of
1110 // a function seems like it could work, though there might be little
1116 Func_expression::do_traverse(Traverse* traverse)
1118 return (this->closure_ == NULL
1120 : Expression::traverse(&this->closure_, traverse));
1123 // Return the type of a function expression.
1126 Func_expression::do_type()
1128 if (this->function_->is_function())
1129 return this->function_->func_value()->type();
1130 else if (this->function_->is_function_declaration())
1131 return this->function_->func_declaration_value()->type();
1136 // Get the tree for a function expression without evaluating the
1140 Func_expression::get_tree_without_closure(Gogo* gogo)
1142 Function_type* fntype;
1143 if (this->function_->is_function())
1144 fntype = this->function_->func_value()->type();
1145 else if (this->function_->is_function_declaration())
1146 fntype = this->function_->func_declaration_value()->type();
1150 // Builtin functions are handled specially by Call_expression. We
1151 // can't take their address.
1152 if (fntype->is_builtin())
1154 error_at(this->location(), "invalid use of special builtin function %qs",
1155 this->function_->name().c_str());
1156 return error_mark_node;
1159 Named_object* no = this->function_;
1161 tree id = no->get_id(gogo);
1162 if (id == error_mark_node)
1163 return error_mark_node;
1166 if (no->is_function())
1167 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1168 else if (no->is_function_declaration())
1169 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1173 if (fndecl == error_mark_node)
1174 return error_mark_node;
1176 return build_fold_addr_expr_loc(this->location(), fndecl);
1179 // Get the tree for a function expression. This is used when we take
1180 // the address of a function rather than simply calling it. If the
1181 // function has a closure, we must use a trampoline.
1184 Func_expression::do_get_tree(Translate_context* context)
1186 Gogo* gogo = context->gogo();
1188 tree fnaddr = this->get_tree_without_closure(gogo);
1189 if (fnaddr == error_mark_node)
1190 return error_mark_node;
1192 gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1193 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1194 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1196 // For a normal non-nested function call, that is all we have to do.
1197 if (!this->function_->is_function()
1198 || this->function_->func_value()->enclosing() == NULL)
1200 gcc_assert(this->closure_ == NULL);
1204 // For a nested function call, we have to always allocate a
1205 // trampoline. If we don't always allocate, then closures will not
1206 // be reliably distinct.
1207 Expression* closure = this->closure_;
1209 if (closure == NULL)
1210 closure_tree = null_pointer_node;
1213 // Get the value of the closure. This will be a pointer to
1214 // space allocated on the heap.
1215 closure_tree = closure->get_tree(context);
1216 if (closure_tree == error_mark_node)
1217 return error_mark_node;
1218 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1221 // Now we need to build some code on the heap. This code will load
1222 // the static chain pointer with the closure and then jump to the
1223 // body of the function. The normal gcc approach is to build the
1224 // code on the stack. Unfortunately we can not do that, as Go
1225 // permits us to return the function pointer.
1227 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1230 // Make a reference to a function in an expression.
1233 Expression::make_func_reference(Named_object* function, Expression* closure,
1234 source_location location)
1236 return new Func_expression(function, closure, location);
1239 // Class Unknown_expression.
1241 // Return the name of an unknown expression.
1244 Unknown_expression::name() const
1246 return this->named_object_->name();
1249 // Lower a reference to an unknown name.
1252 Unknown_expression::do_lower(Gogo*, Named_object*, int)
1254 source_location location = this->location();
1255 Named_object* no = this->named_object_;
1257 if (!no->is_unknown())
1261 real = no->unknown_value()->real_named_object();
1264 if (this->is_composite_literal_key_)
1266 error_at(location, "reference to undefined name %qs",
1267 this->named_object_->message_name().c_str());
1268 return Expression::make_error(location);
1271 switch (real->classification())
1273 case Named_object::NAMED_OBJECT_CONST:
1274 return Expression::make_const_reference(real, location);
1275 case Named_object::NAMED_OBJECT_TYPE:
1276 return Expression::make_type(real->type_value(), location);
1277 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1278 if (this->is_composite_literal_key_)
1280 error_at(location, "reference to undefined type %qs",
1281 real->message_name().c_str());
1282 return Expression::make_error(location);
1283 case Named_object::NAMED_OBJECT_VAR:
1284 return Expression::make_var_reference(real, location);
1285 case Named_object::NAMED_OBJECT_FUNC:
1286 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1287 return Expression::make_func_reference(real, NULL, location);
1288 case Named_object::NAMED_OBJECT_PACKAGE:
1289 if (this->is_composite_literal_key_)
1291 error_at(location, "unexpected reference to package");
1292 return Expression::make_error(location);
1298 // Make a reference to an unknown name.
1301 Expression::make_unknown_reference(Named_object* no, source_location location)
1303 gcc_assert(no->resolve()->is_unknown());
1304 return new Unknown_expression(no, location);
1307 // A boolean expression.
1309 class Boolean_expression : public Expression
1312 Boolean_expression(bool val, source_location location)
1313 : Expression(EXPRESSION_BOOLEAN, location),
1314 val_(val), type_(NULL)
1322 do_is_constant() const
1329 do_determine_type(const Type_context*);
1336 do_get_tree(Translate_context*)
1337 { return this->val_ ? boolean_true_node : boolean_false_node; }
1340 do_export(Export* exp) const
1341 { exp->write_c_string(this->val_ ? "true" : "false"); }
1346 // The type as determined by context.
1353 Boolean_expression::do_type()
1355 if (this->type_ == NULL)
1356 this->type_ = Type::make_boolean_type();
1360 // Set the type from the context.
1363 Boolean_expression::do_determine_type(const Type_context* context)
1365 if (this->type_ != NULL && !this->type_->is_abstract())
1367 else if (context->type != NULL && context->type->is_boolean_type())
1368 this->type_ = context->type;
1369 else if (!context->may_be_abstract)
1370 this->type_ = Type::lookup_bool_type();
1373 // Import a boolean constant.
1376 Boolean_expression::do_import(Import* imp)
1378 if (imp->peek_char() == 't')
1380 imp->require_c_string("true");
1381 return Expression::make_boolean(true, imp->location());
1385 imp->require_c_string("false");
1386 return Expression::make_boolean(false, imp->location());
1390 // Make a boolean expression.
1393 Expression::make_boolean(bool val, source_location location)
1395 return new Boolean_expression(val, location);
1398 // Class String_expression.
1403 String_expression::do_type()
1405 if (this->type_ == NULL)
1406 this->type_ = Type::make_string_type();
1410 // Set the type from the context.
1413 String_expression::do_determine_type(const Type_context* context)
1415 if (this->type_ != NULL && !this->type_->is_abstract())
1417 else if (context->type != NULL && context->type->is_string_type())
1418 this->type_ = context->type;
1419 else if (!context->may_be_abstract)
1420 this->type_ = Type::lookup_string_type();
1423 // Build a string constant.
1426 String_expression::do_get_tree(Translate_context* context)
1428 return context->gogo()->go_string_constant_tree(this->val_);
1431 // Export a string expression.
1434 String_expression::do_export(Export* exp) const
1437 s.reserve(this->val_.length() * 4 + 2);
1439 for (std::string::const_iterator p = this->val_.begin();
1440 p != this->val_.end();
1443 if (*p == '\\' || *p == '"')
1448 else if (*p >= 0x20 && *p < 0x7f)
1450 else if (*p == '\n')
1452 else if (*p == '\t')
1457 unsigned char c = *p;
1458 unsigned int dig = c >> 4;
1459 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1461 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1465 exp->write_string(s);
1468 // Import a string expression.
1471 String_expression::do_import(Import* imp)
1473 imp->require_c_string("\"");
1477 int c = imp->get_char();
1478 if (c == '"' || c == -1)
1481 val += static_cast<char>(c);
1484 c = imp->get_char();
1485 if (c == '\\' || c == '"')
1486 val += static_cast<char>(c);
1493 c = imp->get_char();
1494 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1495 c = imp->get_char();
1496 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1497 char v = (vh << 4) | vl;
1502 error_at(imp->location(), "bad string constant");
1503 return Expression::make_error(imp->location());
1507 return Expression::make_string(val, imp->location());
1510 // Make a string expression.
1513 Expression::make_string(const std::string& val, source_location location)
1515 return new String_expression(val, location);
1518 // Make an integer expression.
1520 class Integer_expression : public Expression
1523 Integer_expression(const mpz_t* val, Type* type, source_location location)
1524 : Expression(EXPRESSION_INTEGER, location),
1526 { mpz_init_set(this->val_, *val); }
1531 // Return whether VAL fits in the type.
1533 check_constant(mpz_t val, Type*, source_location);
1535 // Write VAL to export data.
1537 export_integer(Export* exp, const mpz_t val);
1541 do_is_constant() const
1545 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1551 do_determine_type(const Type_context* context);
1554 do_check_types(Gogo*);
1557 do_get_tree(Translate_context*);
1561 { return Expression::make_integer(&this->val_, this->type_,
1562 this->location()); }
1565 do_export(Export*) const;
1568 // The integer value.
1574 // Return an integer constant value.
1577 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1580 if (this->type_ != NULL)
1581 *ptype = this->type_;
1582 mpz_set(val, this->val_);
1586 // Return the current type. If we haven't set the type yet, we return
1587 // an abstract integer type.
1590 Integer_expression::do_type()
1592 if (this->type_ == NULL)
1593 this->type_ = Type::make_abstract_integer_type();
1597 // Set the type of the integer value. Here we may switch from an
1598 // abstract type to a real type.
1601 Integer_expression::do_determine_type(const Type_context* context)
1603 if (this->type_ != NULL && !this->type_->is_abstract())
1605 else if (context->type != NULL
1606 && (context->type->integer_type() != NULL
1607 || context->type->float_type() != NULL
1608 || context->type->complex_type() != NULL))
1609 this->type_ = context->type;
1610 else if (!context->may_be_abstract)
1611 this->type_ = Type::lookup_integer_type("int");
1614 // Return true if the integer VAL fits in the range of the type TYPE.
1615 // Otherwise give an error and return false. TYPE may be NULL.
1618 Integer_expression::check_constant(mpz_t val, Type* type,
1619 source_location location)
1623 Integer_type* itype = type->integer_type();
1624 if (itype == NULL || itype->is_abstract())
1627 int bits = mpz_sizeinbase(val, 2);
1629 if (itype->is_unsigned())
1631 // For an unsigned type we can only accept a nonnegative number,
1632 // and we must be able to represent at least BITS.
1633 if (mpz_sgn(val) >= 0
1634 && bits <= itype->bits())
1639 // For a signed type we need an extra bit to indicate the sign.
1640 // We have to handle the most negative integer specially.
1641 if (bits + 1 <= itype->bits()
1642 || (bits <= itype->bits()
1644 && (mpz_scan1(val, 0)
1645 == static_cast<unsigned long>(itype->bits() - 1))
1646 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1650 error_at(location, "integer constant overflow");
1654 // Check the type of an integer constant.
1657 Integer_expression::do_check_types(Gogo*)
1659 if (this->type_ == NULL)
1661 if (!Integer_expression::check_constant(this->val_, this->type_,
1663 this->set_is_error();
1666 // Get a tree for an integer constant.
1669 Integer_expression::do_get_tree(Translate_context* context)
1671 Gogo* gogo = context->gogo();
1673 if (this->type_ != NULL && !this->type_->is_abstract())
1674 type = this->type_->get_tree(gogo);
1675 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1677 // We are converting to an abstract floating point type.
1678 type = Type::lookup_float_type("float64")->get_tree(gogo);
1680 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1682 // We are converting to an abstract complex type.
1683 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
1687 // If we still have an abstract type here, then this is being
1688 // used in a constant expression which didn't get reduced for
1689 // some reason. Use a type which will fit the value. We use <,
1690 // not <=, because we need an extra bit for the sign bit.
1691 int bits = mpz_sizeinbase(this->val_, 2);
1692 if (bits < INT_TYPE_SIZE)
1693 type = Type::lookup_integer_type("int")->get_tree(gogo);
1695 type = Type::lookup_integer_type("int64")->get_tree(gogo);
1697 type = long_long_integer_type_node;
1699 return Expression::integer_constant_tree(this->val_, type);
1702 // Write VAL to export data.
1705 Integer_expression::export_integer(Export* exp, const mpz_t val)
1707 char* s = mpz_get_str(NULL, 10, val);
1708 exp->write_c_string(s);
1712 // Export an integer in a constant expression.
1715 Integer_expression::do_export(Export* exp) const
1717 Integer_expression::export_integer(exp, this->val_);
1718 // A trailing space lets us reliably identify the end of the number.
1719 exp->write_c_string(" ");
1722 // Import an integer, floating point, or complex value. This handles
1723 // all these types because they all start with digits.
1726 Integer_expression::do_import(Import* imp)
1728 std::string num = imp->read_identifier();
1729 imp->require_c_string(" ");
1730 if (!num.empty() && num[num.length() - 1] == 'i')
1733 size_t plus_pos = num.find('+', 1);
1734 size_t minus_pos = num.find('-', 1);
1736 if (plus_pos == std::string::npos)
1738 else if (minus_pos == std::string::npos)
1742 error_at(imp->location(), "bad number in import data: %qs",
1744 return Expression::make_error(imp->location());
1746 if (pos == std::string::npos)
1747 mpfr_set_ui(real, 0, GMP_RNDN);
1750 std::string real_str = num.substr(0, pos);
1751 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1753 error_at(imp->location(), "bad number in import data: %qs",
1755 return Expression::make_error(imp->location());
1759 std::string imag_str;
1760 if (pos == std::string::npos)
1763 imag_str = num.substr(pos);
1764 imag_str = imag_str.substr(0, imag_str.size() - 1);
1766 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1768 error_at(imp->location(), "bad number in import data: %qs",
1770 return Expression::make_error(imp->location());
1772 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1778 else if (num.find('.') == std::string::npos
1779 && num.find('E') == std::string::npos)
1782 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1784 error_at(imp->location(), "bad number in import data: %qs",
1786 return Expression::make_error(imp->location());
1788 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1795 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1797 error_at(imp->location(), "bad number in import data: %qs",
1799 return Expression::make_error(imp->location());
1801 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1807 // Build a new integer value.
1810 Expression::make_integer(const mpz_t* val, Type* type,
1811 source_location location)
1813 return new Integer_expression(val, type, location);
1818 class Float_expression : public Expression
1821 Float_expression(const mpfr_t* val, Type* type, source_location location)
1822 : Expression(EXPRESSION_FLOAT, location),
1825 mpfr_init_set(this->val_, *val, GMP_RNDN);
1828 // Constrain VAL to fit into TYPE.
1830 constrain_float(mpfr_t val, Type* type);
1832 // Return whether VAL fits in the type.
1834 check_constant(mpfr_t val, Type*, source_location);
1836 // Write VAL to export data.
1838 export_float(Export* exp, const mpfr_t val);
1842 do_is_constant() const
1846 do_float_constant_value(mpfr_t val, Type**) const;
1852 do_determine_type(const Type_context*);
1855 do_check_types(Gogo*);
1859 { return Expression::make_float(&this->val_, this->type_,
1860 this->location()); }
1863 do_get_tree(Translate_context*);
1866 do_export(Export*) const;
1869 // The floating point value.
1875 // Constrain VAL to fit into TYPE.
1878 Float_expression::constrain_float(mpfr_t val, Type* type)
1880 Float_type* ftype = type->float_type();
1881 if (ftype != NULL && !ftype->is_abstract())
1883 tree type_tree = ftype->type_tree();
1884 REAL_VALUE_TYPE rvt;
1885 real_from_mpfr(&rvt, val, type_tree, GMP_RNDN);
1886 real_convert(&rvt, TYPE_MODE(type_tree), &rvt);
1887 mpfr_from_real(val, &rvt, GMP_RNDN);
1891 // Return a floating point constant value.
1894 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
1896 if (this->type_ != NULL)
1897 *ptype = this->type_;
1898 mpfr_set(val, this->val_, GMP_RNDN);
1902 // Return the current type. If we haven't set the type yet, we return
1903 // an abstract float type.
1906 Float_expression::do_type()
1908 if (this->type_ == NULL)
1909 this->type_ = Type::make_abstract_float_type();
1913 // Set the type of the float value. Here we may switch from an
1914 // abstract type to a real type.
1917 Float_expression::do_determine_type(const Type_context* context)
1919 if (this->type_ != NULL && !this->type_->is_abstract())
1921 else if (context->type != NULL
1922 && (context->type->integer_type() != NULL
1923 || context->type->float_type() != NULL
1924 || context->type->complex_type() != NULL))
1925 this->type_ = context->type;
1926 else if (!context->may_be_abstract)
1927 this->type_ = Type::lookup_float_type("float64");
1930 // Return true if the floating point value VAL fits in the range of
1931 // the type TYPE. Otherwise give an error and return false. TYPE may
1935 Float_expression::check_constant(mpfr_t val, Type* type,
1936 source_location location)
1940 Float_type* ftype = type->float_type();
1941 if (ftype == NULL || ftype->is_abstract())
1944 // A NaN or Infinity always fits in the range of the type.
1945 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
1948 mp_exp_t exp = mpfr_get_exp(val);
1950 switch (ftype->bits())
1963 error_at(location, "floating point constant overflow");
1969 // Check the type of a float value.
1972 Float_expression::do_check_types(Gogo*)
1974 if (this->type_ == NULL)
1977 if (!Float_expression::check_constant(this->val_, this->type_,
1979 this->set_is_error();
1981 Integer_type* integer_type = this->type_->integer_type();
1982 if (integer_type != NULL)
1984 if (!mpfr_integer_p(this->val_))
1985 this->report_error(_("floating point constant truncated to integer"));
1988 gcc_assert(!integer_type->is_abstract());
1991 mpfr_get_z(ival, this->val_, GMP_RNDN);
1992 Integer_expression::check_constant(ival, integer_type,
1999 // Get a tree for a float constant.
2002 Float_expression::do_get_tree(Translate_context* context)
2004 Gogo* gogo = context->gogo();
2006 if (this->type_ != NULL && !this->type_->is_abstract())
2007 type = this->type_->get_tree(gogo);
2008 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2010 // We have an abstract integer type. We just hope for the best.
2011 type = Type::lookup_integer_type("int")->get_tree(gogo);
2015 // If we still have an abstract type here, then this is being
2016 // used in a constant expression which didn't get reduced. We
2017 // just use float64 and hope for the best.
2018 type = Type::lookup_float_type("float64")->get_tree(gogo);
2020 return Expression::float_constant_tree(this->val_, type);
2023 // Write a floating point number to export data.
2026 Float_expression::export_float(Export *exp, const mpfr_t val)
2029 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2031 exp->write_c_string("-");
2032 exp->write_c_string("0.");
2033 exp->write_c_string(*s == '-' ? s + 1 : s);
2036 snprintf(buf, sizeof buf, "E%ld", exponent);
2037 exp->write_c_string(buf);
2040 // Export a floating point number in a constant expression.
2043 Float_expression::do_export(Export* exp) const
2045 Float_expression::export_float(exp, this->val_);
2046 // A trailing space lets us reliably identify the end of the number.
2047 exp->write_c_string(" ");
2050 // Make a float expression.
2053 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2055 return new Float_expression(val, type, location);
2060 class Complex_expression : public Expression
2063 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2064 source_location location)
2065 : Expression(EXPRESSION_COMPLEX, location),
2068 mpfr_init_set(this->real_, *real, GMP_RNDN);
2069 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2072 // Constrain REAL/IMAG to fit into TYPE.
2074 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2076 // Return whether REAL/IMAG fits in the type.
2078 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2080 // Write REAL/IMAG to export data.
2082 export_complex(Export* exp, const mpfr_t real, const mpfr_t val);
2086 do_is_constant() const
2090 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2096 do_determine_type(const Type_context*);
2099 do_check_types(Gogo*);
2104 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2109 do_get_tree(Translate_context*);
2112 do_export(Export*) const;
2117 // The imaginary part;
2119 // The type if known.
2123 // Constrain REAL/IMAG to fit into TYPE.
2126 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2128 Complex_type* ctype = type->complex_type();
2129 if (ctype != NULL && !ctype->is_abstract())
2131 tree type_tree = ctype->type_tree();
2133 REAL_VALUE_TYPE rvt;
2134 real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN);
2135 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2136 mpfr_from_real(real, &rvt, GMP_RNDN);
2138 real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN);
2139 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2140 mpfr_from_real(imag, &rvt, GMP_RNDN);
2144 // Return a complex constant value.
2147 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2150 if (this->type_ != NULL)
2151 *ptype = this->type_;
2152 mpfr_set(real, this->real_, GMP_RNDN);
2153 mpfr_set(imag, this->imag_, GMP_RNDN);
2157 // Return the current type. If we haven't set the type yet, we return
2158 // an abstract complex type.
2161 Complex_expression::do_type()
2163 if (this->type_ == NULL)
2164 this->type_ = Type::make_abstract_complex_type();
2168 // Set the type of the complex value. Here we may switch from an
2169 // abstract type to a real type.
2172 Complex_expression::do_determine_type(const Type_context* context)
2174 if (this->type_ != NULL && !this->type_->is_abstract())
2176 else if (context->type != NULL
2177 && context->type->complex_type() != NULL)
2178 this->type_ = context->type;
2179 else if (!context->may_be_abstract)
2180 this->type_ = Type::lookup_complex_type("complex128");
2183 // Return true if the complex value REAL/IMAG fits in the range of the
2184 // type TYPE. Otherwise give an error and return false. TYPE may be
2188 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2189 source_location location)
2193 Complex_type* ctype = type->complex_type();
2194 if (ctype == NULL || ctype->is_abstract())
2198 switch (ctype->bits())
2210 // A NaN or Infinity always fits in the range of the type.
2211 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2213 if (mpfr_get_exp(real) > max_exp)
2215 error_at(location, "complex real part constant overflow");
2220 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2222 if (mpfr_get_exp(imag) > max_exp)
2224 error_at(location, "complex imaginary part constant overflow");
2232 // Check the type of a complex value.
2235 Complex_expression::do_check_types(Gogo*)
2237 if (this->type_ == NULL)
2240 if (!Complex_expression::check_constant(this->real_, this->imag_,
2241 this->type_, this->location()))
2242 this->set_is_error();
2245 // Get a tree for a complex constant.
2248 Complex_expression::do_get_tree(Translate_context* context)
2250 Gogo* gogo = context->gogo();
2252 if (this->type_ != NULL && !this->type_->is_abstract())
2253 type = this->type_->get_tree(gogo);
2256 // If we still have an abstract type here, this this is being
2257 // used in a constant expression which didn't get reduced. We
2258 // just use complex128 and hope for the best.
2259 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
2261 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2264 // Write REAL/IMAG to export data.
2267 Complex_expression::export_complex(Export* exp, const mpfr_t real,
2270 if (!mpfr_zero_p(real))
2272 Float_expression::export_float(exp, real);
2273 if (mpfr_sgn(imag) > 0)
2274 exp->write_c_string("+");
2276 Float_expression::export_float(exp, imag);
2277 exp->write_c_string("i");
2280 // Export a complex number in a constant expression.
2283 Complex_expression::do_export(Export* exp) const
2285 Complex_expression::export_complex(exp, this->real_, this->imag_);
2286 // A trailing space lets us reliably identify the end of the number.
2287 exp->write_c_string(" ");
2290 // Make a complex expression.
2293 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2294 source_location location)
2296 return new Complex_expression(real, imag, type, location);
2299 // Find a named object in an expression.
2301 class Find_named_object : public Traverse
2304 Find_named_object(Named_object* no)
2305 : Traverse(traverse_expressions),
2306 no_(no), found_(false)
2309 // Whether we found the object.
2312 { return this->found_; }
2316 expression(Expression**);
2319 // The object we are looking for.
2321 // Whether we found it.
2325 // A reference to a const in an expression.
2327 class Const_expression : public Expression
2330 Const_expression(Named_object* constant, source_location location)
2331 : Expression(EXPRESSION_CONST_REFERENCE, location),
2332 constant_(constant), type_(NULL), seen_(false)
2337 { return this->constant_; }
2339 // Check that the initializer does not refer to the constant itself.
2341 check_for_init_loop();
2345 do_traverse(Traverse*);
2348 do_lower(Gogo*, Named_object*, int);
2351 do_is_constant() const
2355 do_integer_constant_value(bool, mpz_t val, Type**) const;
2358 do_float_constant_value(mpfr_t val, Type**) const;
2361 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2364 do_string_constant_value(std::string* val) const
2365 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2370 // The type of a const is set by the declaration, not the use.
2372 do_determine_type(const Type_context*);
2375 do_check_types(Gogo*);
2382 do_get_tree(Translate_context* context);
2384 // When exporting a reference to a const as part of a const
2385 // expression, we export the value. We ignore the fact that it has
2388 do_export(Export* exp) const
2389 { this->constant_->const_value()->expr()->export_expression(exp); }
2393 Named_object* constant_;
2394 // The type of this reference. This is used if the constant has an
2397 // Used to prevent infinite recursion when a constant incorrectly
2398 // refers to itself.
2405 Const_expression::do_traverse(Traverse* traverse)
2407 if (this->type_ != NULL)
2408 return Type::traverse(this->type_, traverse);
2409 return TRAVERSE_CONTINUE;
2412 // Lower a constant expression. This is where we convert the
2413 // predeclared constant iota into an integer value.
2416 Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value)
2418 if (this->constant_->const_value()->expr()->classification()
2421 if (iota_value == -1)
2423 error_at(this->location(),
2424 "iota is only defined in const declarations");
2428 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2429 Expression* ret = Expression::make_integer(&val, NULL,
2435 // Make sure that the constant itself has been lowered.
2436 gogo->lower_constant(this->constant_);
2441 // Return an integer constant value.
2444 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2451 if (this->type_ != NULL)
2452 ctype = this->type_;
2454 ctype = this->constant_->const_value()->type();
2455 if (ctype != NULL && ctype->integer_type() == NULL)
2458 Expression* e = this->constant_->const_value()->expr();
2463 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2465 this->seen_ = false;
2469 && !Integer_expression::check_constant(val, ctype, this->location()))
2472 *ptype = ctype != NULL ? ctype : t;
2476 // Return a floating point constant value.
2479 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2485 if (this->type_ != NULL)
2486 ctype = this->type_;
2488 ctype = this->constant_->const_value()->type();
2489 if (ctype != NULL && ctype->float_type() == NULL)
2495 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2498 this->seen_ = false;
2500 if (r && ctype != NULL)
2502 if (!Float_expression::check_constant(val, ctype, this->location()))
2504 Float_expression::constrain_float(val, ctype);
2506 *ptype = ctype != NULL ? ctype : t;
2510 // Return a complex constant value.
2513 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2520 if (this->type_ != NULL)
2521 ctype = this->type_;
2523 ctype = this->constant_->const_value()->type();
2524 if (ctype != NULL && ctype->complex_type() == NULL)
2530 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2534 this->seen_ = false;
2536 if (r && ctype != NULL)
2538 if (!Complex_expression::check_constant(real, imag, ctype,
2541 Complex_expression::constrain_complex(real, imag, ctype);
2543 *ptype = ctype != NULL ? ctype : t;
2547 // Return the type of the const reference.
2550 Const_expression::do_type()
2552 if (this->type_ != NULL)
2555 Named_constant* nc = this->constant_->const_value();
2557 if (this->seen_ || nc->lowering())
2559 this->report_error(_("constant refers to itself"));
2560 this->type_ = Type::make_error_type();
2566 Type* ret = nc->type();
2570 this->seen_ = false;
2574 // During parsing, a named constant may have a NULL type, but we
2575 // must not return a NULL type here.
2576 ret = nc->expr()->type();
2578 this->seen_ = false;
2583 // Set the type of the const reference.
2586 Const_expression::do_determine_type(const Type_context* context)
2588 Type* ctype = this->constant_->const_value()->type();
2589 Type* cetype = (ctype != NULL
2591 : this->constant_->const_value()->expr()->type());
2592 if (ctype != NULL && !ctype->is_abstract())
2594 else if (context->type != NULL
2595 && (context->type->integer_type() != NULL
2596 || context->type->float_type() != NULL
2597 || context->type->complex_type() != NULL)
2598 && (cetype->integer_type() != NULL
2599 || cetype->float_type() != NULL
2600 || cetype->complex_type() != NULL))
2601 this->type_ = context->type;
2602 else if (context->type != NULL
2603 && context->type->is_string_type()
2604 && cetype->is_string_type())
2605 this->type_ = context->type;
2606 else if (context->type != NULL
2607 && context->type->is_boolean_type()
2608 && cetype->is_boolean_type())
2609 this->type_ = context->type;
2610 else if (!context->may_be_abstract)
2612 if (cetype->is_abstract())
2613 cetype = cetype->make_non_abstract_type();
2614 this->type_ = cetype;
2618 // Check for a loop in which the initializer of a constant refers to
2619 // the constant itself.
2622 Const_expression::check_for_init_loop()
2624 if (this->type_ != NULL && this->type_->is_error())
2629 this->report_error(_("constant refers to itself"));
2630 this->type_ = Type::make_error_type();
2634 Expression* init = this->constant_->const_value()->expr();
2635 Find_named_object find_named_object(this->constant_);
2638 Expression::traverse(&init, &find_named_object);
2639 this->seen_ = false;
2641 if (find_named_object.found())
2643 if (this->type_ == NULL || !this->type_->is_error())
2645 this->report_error(_("constant refers to itself"));
2646 this->type_ = Type::make_error_type();
2652 // Check types of a const reference.
2655 Const_expression::do_check_types(Gogo*)
2657 if (this->type_ != NULL && this->type_->is_error())
2660 this->check_for_init_loop();
2662 if (this->type_ == NULL || this->type_->is_abstract())
2665 // Check for integer overflow.
2666 if (this->type_->integer_type() != NULL)
2671 if (!this->integer_constant_value(true, ival, &dummy))
2675 Expression* cexpr = this->constant_->const_value()->expr();
2676 if (cexpr->float_constant_value(fval, &dummy))
2678 if (!mpfr_integer_p(fval))
2679 this->report_error(_("floating point constant "
2680 "truncated to integer"));
2683 mpfr_get_z(ival, fval, GMP_RNDN);
2684 Integer_expression::check_constant(ival, this->type_,
2694 // Return a tree for the const reference.
2697 Const_expression::do_get_tree(Translate_context* context)
2699 Gogo* gogo = context->gogo();
2701 if (this->type_ == NULL)
2702 type_tree = NULL_TREE;
2705 type_tree = this->type_->get_tree(gogo);
2706 if (type_tree == error_mark_node)
2707 return error_mark_node;
2710 // If the type has been set for this expression, but the underlying
2711 // object is an abstract int or float, we try to get the abstract
2712 // value. Otherwise we may lose something in the conversion.
2713 if (this->type_ != NULL
2714 && (this->constant_->const_value()->type() == NULL
2715 || this->constant_->const_value()->type()->is_abstract()))
2717 Expression* expr = this->constant_->const_value()->expr();
2721 if (expr->integer_constant_value(true, ival, &t))
2723 tree ret = Expression::integer_constant_tree(ival, type_tree);
2731 if (expr->float_constant_value(fval, &t))
2733 tree ret = Expression::float_constant_tree(fval, type_tree);
2740 if (expr->complex_constant_value(fval, imag, &t))
2742 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2751 tree const_tree = this->constant_->get_tree(gogo, context->function());
2752 if (this->type_ == NULL
2753 || const_tree == error_mark_node
2754 || TREE_TYPE(const_tree) == error_mark_node)
2758 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2759 ret = fold_convert(type_tree, const_tree);
2760 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2761 ret = fold(convert_to_integer(type_tree, const_tree));
2762 else if (TREE_CODE(type_tree) == REAL_TYPE)
2763 ret = fold(convert_to_real(type_tree, const_tree));
2764 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2765 ret = fold(convert_to_complex(type_tree, const_tree));
2771 // Make a reference to a constant in an expression.
2774 Expression::make_const_reference(Named_object* constant,
2775 source_location location)
2777 return new Const_expression(constant, location);
2780 // Find a named object in an expression.
2783 Find_named_object::expression(Expression** pexpr)
2785 switch ((*pexpr)->classification())
2787 case Expression::EXPRESSION_CONST_REFERENCE:
2789 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2790 if (ce->named_object() == this->no_)
2793 // We need to check a constant initializer explicitly, as
2794 // loops here will not be caught by the loop checking for
2795 // variable initializers.
2796 ce->check_for_init_loop();
2798 return TRAVERSE_CONTINUE;
2801 case Expression::EXPRESSION_VAR_REFERENCE:
2802 if ((*pexpr)->var_expression()->named_object() == this->no_)
2804 return TRAVERSE_CONTINUE;
2805 case Expression::EXPRESSION_FUNC_REFERENCE:
2806 if ((*pexpr)->func_expression()->named_object() == this->no_)
2808 return TRAVERSE_CONTINUE;
2810 return TRAVERSE_CONTINUE;
2812 this->found_ = true;
2813 return TRAVERSE_EXIT;
2818 class Nil_expression : public Expression
2821 Nil_expression(source_location location)
2822 : Expression(EXPRESSION_NIL, location)
2830 do_is_constant() const
2835 { return Type::make_nil_type(); }
2838 do_determine_type(const Type_context*)
2846 do_get_tree(Translate_context*)
2847 { return null_pointer_node; }
2850 do_export(Export* exp) const
2851 { exp->write_c_string("nil"); }
2854 // Import a nil expression.
2857 Nil_expression::do_import(Import* imp)
2859 imp->require_c_string("nil");
2860 return Expression::make_nil(imp->location());
2863 // Make a nil expression.
2866 Expression::make_nil(source_location location)
2868 return new Nil_expression(location);
2871 // The value of the predeclared constant iota. This is little more
2872 // than a marker. This will be lowered to an integer in
2873 // Const_expression::do_lower, which is where we know the value that
2876 class Iota_expression : public Parser_expression
2879 Iota_expression(source_location location)
2880 : Parser_expression(EXPRESSION_IOTA, location)
2885 do_lower(Gogo*, Named_object*, int)
2886 { gcc_unreachable(); }
2888 // There should only ever be one of these.
2891 { gcc_unreachable(); }
2894 // Make an iota expression. This is only called for one case: the
2895 // value of the predeclared constant iota.
2898 Expression::make_iota()
2900 static Iota_expression iota_expression(UNKNOWN_LOCATION);
2901 return &iota_expression;
2904 // A type conversion expression.
2906 class Type_conversion_expression : public Expression
2909 Type_conversion_expression(Type* type, Expression* expr,
2910 source_location location)
2911 : Expression(EXPRESSION_CONVERSION, location),
2912 type_(type), expr_(expr), may_convert_function_types_(false)
2915 // Return the type to which we are converting.
2918 { return this->type_; }
2920 // Return the expression which we are converting.
2923 { return this->expr_; }
2925 // Permit converting from one function type to another. This is
2926 // used internally for method expressions.
2928 set_may_convert_function_types()
2930 this->may_convert_function_types_ = true;
2933 // Import a type conversion expression.
2939 do_traverse(Traverse* traverse);
2942 do_lower(Gogo*, Named_object*, int);
2945 do_is_constant() const
2946 { return this->expr_->is_constant(); }
2949 do_integer_constant_value(bool, mpz_t, Type**) const;
2952 do_float_constant_value(mpfr_t, Type**) const;
2955 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
2958 do_string_constant_value(std::string*) const;
2962 { return this->type_; }
2965 do_determine_type(const Type_context*)
2967 Type_context subcontext(this->type_, false);
2968 this->expr_->determine_type(&subcontext);
2972 do_check_types(Gogo*);
2977 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2982 do_get_tree(Translate_context* context);
2985 do_export(Export*) const;
2988 // The type to convert to.
2990 // The expression to convert.
2992 // True if this is permitted to convert function types. This is
2993 // used internally for method expressions.
2994 bool may_convert_function_types_;
3000 Type_conversion_expression::do_traverse(Traverse* traverse)
3002 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3003 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3004 return TRAVERSE_EXIT;
3005 return TRAVERSE_CONTINUE;
3008 // Convert to a constant at lowering time.
3011 Type_conversion_expression::do_lower(Gogo*, Named_object*, int)
3013 Type* type = this->type_;
3014 Expression* val = this->expr_;
3015 source_location location = this->location();
3017 if (type->integer_type() != NULL)
3022 if (val->integer_constant_value(false, ival, &dummy))
3024 if (!Integer_expression::check_constant(ival, type, location))
3025 mpz_set_ui(ival, 0);
3026 Expression* ret = Expression::make_integer(&ival, type, location);
3033 if (val->float_constant_value(fval, &dummy))
3035 if (!mpfr_integer_p(fval))
3038 "floating point constant truncated to integer");
3039 return Expression::make_error(location);
3041 mpfr_get_z(ival, fval, GMP_RNDN);
3042 if (!Integer_expression::check_constant(ival, type, location))
3043 mpz_set_ui(ival, 0);
3044 Expression* ret = Expression::make_integer(&ival, type, location);
3053 if (type->float_type() != NULL)
3058 if (val->float_constant_value(fval, &dummy))
3060 if (!Float_expression::check_constant(fval, type, location))
3061 mpfr_set_ui(fval, 0, GMP_RNDN);
3062 Float_expression::constrain_float(fval, type);
3063 Expression *ret = Expression::make_float(&fval, type, location);
3070 if (type->complex_type() != NULL)
3077 if (val->complex_constant_value(real, imag, &dummy))
3079 if (!Complex_expression::check_constant(real, imag, type, location))
3081 mpfr_set_ui(real, 0, GMP_RNDN);
3082 mpfr_set_ui(imag, 0, GMP_RNDN);
3084 Complex_expression::constrain_complex(real, imag, type);
3085 Expression* ret = Expression::make_complex(&real, &imag, type,
3095 if (type->is_open_array_type() && type->named_type() == NULL)
3097 Type* element_type = type->array_type()->element_type()->forwarded();
3098 bool is_byte = element_type == Type::lookup_integer_type("uint8");
3099 bool is_int = element_type == Type::lookup_integer_type("int");
3100 if (is_byte || is_int)
3103 if (val->string_constant_value(&s))
3105 Expression_list* vals = new Expression_list();
3108 for (std::string::const_iterator p = s.begin();
3113 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3114 Expression* v = Expression::make_integer(&val,
3123 const char *p = s.data();
3124 const char *pend = s.data() + s.length();
3128 int adv = Lex::fetch_char(p, &c);
3131 warning_at(this->location(), 0,
3132 "invalid UTF-8 encoding");
3137 mpz_init_set_ui(val, c);
3138 Expression* v = Expression::make_integer(&val,
3146 return Expression::make_slice_composite_literal(type, vals,
3155 // Return the constant integer value if there is one.
3158 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3162 if (this->type_->integer_type() == NULL)
3168 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3170 if (!Integer_expression::check_constant(ival, this->type_,
3178 *ptype = this->type_;
3185 if (this->expr_->float_constant_value(fval, &dummy))
3187 mpfr_get_z(val, fval, GMP_RNDN);
3189 if (!Integer_expression::check_constant(val, this->type_,
3192 *ptype = this->type_;
3200 // Return the constant floating point value if there is one.
3203 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3206 if (this->type_->float_type() == NULL)
3212 if (this->expr_->float_constant_value(fval, &dummy))
3214 if (!Float_expression::check_constant(fval, this->type_,
3220 mpfr_set(val, fval, GMP_RNDN);
3222 Float_expression::constrain_float(val, this->type_);
3223 *ptype = this->type_;
3231 // Return the constant complex value if there is one.
3234 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3238 if (this->type_->complex_type() == NULL)
3246 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3248 if (!Complex_expression::check_constant(rval, ival, this->type_,
3255 mpfr_set(real, rval, GMP_RNDN);
3256 mpfr_set(imag, ival, GMP_RNDN);
3259 Complex_expression::constrain_complex(real, imag, this->type_);
3260 *ptype = this->type_;
3269 // Return the constant string value if there is one.
3272 Type_conversion_expression::do_string_constant_value(std::string* val) const
3274 if (this->type_->is_string_type()
3275 && this->expr_->type()->integer_type() != NULL)
3280 if (this->expr_->integer_constant_value(false, ival, &dummy))
3282 unsigned long ulval = mpz_get_ui(ival);
3283 if (mpz_cmp_ui(ival, ulval) == 0)
3285 Lex::append_char(ulval, true, val, this->location());
3293 // FIXME: Could handle conversion from const []int here.
3298 // Check that types are convertible.
3301 Type_conversion_expression::do_check_types(Gogo*)
3303 Type* type = this->type_;
3304 Type* expr_type = this->expr_->type();
3307 if (type->is_error() || expr_type->is_error())
3309 this->set_is_error();
3313 if (this->may_convert_function_types_
3314 && type->function_type() != NULL
3315 && expr_type->function_type() != NULL)
3318 if (Type::are_convertible(type, expr_type, &reason))
3321 error_at(this->location(), "%s", reason.c_str());
3322 this->set_is_error();
3325 // Get a tree for a type conversion.
3328 Type_conversion_expression::do_get_tree(Translate_context* context)
3330 Gogo* gogo = context->gogo();
3331 tree type_tree = this->type_->get_tree(gogo);
3332 tree expr_tree = this->expr_->get_tree(context);
3334 if (type_tree == error_mark_node
3335 || expr_tree == error_mark_node
3336 || TREE_TYPE(expr_tree) == error_mark_node)
3337 return error_mark_node;
3339 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3340 return fold_convert(type_tree, expr_tree);
3342 Type* type = this->type_;
3343 Type* expr_type = this->expr_->type();
3345 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3346 ret = Expression::convert_for_assignment(context, type, expr_type,
3347 expr_tree, this->location());
3348 else if (type->integer_type() != NULL)
3350 if (expr_type->integer_type() != NULL
3351 || expr_type->float_type() != NULL
3352 || expr_type->is_unsafe_pointer_type())
3353 ret = fold(convert_to_integer(type_tree, expr_tree));
3357 else if (type->float_type() != NULL)
3359 if (expr_type->integer_type() != NULL
3360 || expr_type->float_type() != NULL)
3361 ret = fold(convert_to_real(type_tree, expr_tree));
3365 else if (type->complex_type() != NULL)
3367 if (expr_type->complex_type() != NULL)
3368 ret = fold(convert_to_complex(type_tree, expr_tree));
3372 else if (type->is_string_type()
3373 && expr_type->integer_type() != NULL)
3375 expr_tree = fold_convert(integer_type_node, expr_tree);
3376 if (host_integerp(expr_tree, 0))
3378 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3380 Lex::append_char(intval, true, &s, this->location());
3381 Expression* se = Expression::make_string(s, this->location());
3382 return se->get_tree(context);
3385 static tree int_to_string_fndecl;
3386 ret = Gogo::call_builtin(&int_to_string_fndecl,
3388 "__go_int_to_string",
3392 fold_convert(integer_type_node, expr_tree));
3394 else if (type->is_string_type()
3395 && (expr_type->array_type() != NULL
3396 || (expr_type->points_to() != NULL
3397 && expr_type->points_to()->array_type() != NULL)))
3399 Type* t = expr_type;
3400 if (t->points_to() != NULL)
3403 expr_tree = build_fold_indirect_ref(expr_tree);
3405 if (!DECL_P(expr_tree))
3406 expr_tree = save_expr(expr_tree);
3407 Array_type* a = t->array_type();
3408 Type* e = a->element_type()->forwarded();
3409 gcc_assert(e->integer_type() != NULL);
3410 tree valptr = fold_convert(const_ptr_type_node,
3411 a->value_pointer_tree(gogo, expr_tree));
3412 tree len = a->length_tree(gogo, expr_tree);
3413 len = fold_convert_loc(this->location(), integer_type_node, len);
3414 if (e->integer_type()->is_unsigned()
3415 && e->integer_type()->bits() == 8)
3417 static tree byte_array_to_string_fndecl;
3418 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3420 "__go_byte_array_to_string",
3423 const_ptr_type_node,
3430 gcc_assert(e == Type::lookup_integer_type("int"));
3431 static tree int_array_to_string_fndecl;
3432 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3434 "__go_int_array_to_string",
3437 const_ptr_type_node,
3443 else if (type->is_open_array_type() && expr_type->is_string_type())
3445 Type* e = type->array_type()->element_type()->forwarded();
3446 gcc_assert(e->integer_type() != NULL);
3447 if (e->integer_type()->is_unsigned()
3448 && e->integer_type()->bits() == 8)
3450 static tree string_to_byte_array_fndecl;
3451 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3453 "__go_string_to_byte_array",
3456 TREE_TYPE(expr_tree),
3461 gcc_assert(e == Type::lookup_integer_type("int"));
3462 static tree string_to_int_array_fndecl;
3463 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3465 "__go_string_to_int_array",
3468 TREE_TYPE(expr_tree),
3472 else if ((type->is_unsafe_pointer_type()
3473 && expr_type->points_to() != NULL)
3474 || (expr_type->is_unsafe_pointer_type()
3475 && type->points_to() != NULL))
3476 ret = fold_convert(type_tree, expr_tree);
3477 else if (type->is_unsafe_pointer_type()
3478 && expr_type->integer_type() != NULL)
3479 ret = convert_to_pointer(type_tree, expr_tree);
3480 else if (this->may_convert_function_types_
3481 && type->function_type() != NULL
3482 && expr_type->function_type() != NULL)
3483 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3485 ret = Expression::convert_for_assignment(context, type, expr_type,
3486 expr_tree, this->location());
3491 // Output a type conversion in a constant expression.
3494 Type_conversion_expression::do_export(Export* exp) const
3496 exp->write_c_string("convert(");
3497 exp->write_type(this->type_);
3498 exp->write_c_string(", ");
3499 this->expr_->export_expression(exp);
3500 exp->write_c_string(")");
3503 // Import a type conversion or a struct construction.
3506 Type_conversion_expression::do_import(Import* imp)
3508 imp->require_c_string("convert(");
3509 Type* type = imp->read_type();
3510 imp->require_c_string(", ");
3511 Expression* val = Expression::import_expression(imp);
3512 imp->require_c_string(")");
3513 return Expression::make_cast(type, val, imp->location());
3516 // Make a type cast expression.
3519 Expression::make_cast(Type* type, Expression* val, source_location location)
3521 if (type->is_error_type() || val->is_error_expression())
3522 return Expression::make_error(location);
3523 return new Type_conversion_expression(type, val, location);
3526 // An unsafe type conversion, used to pass values to builtin functions.
3528 class Unsafe_type_conversion_expression : public Expression
3531 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3532 source_location location)
3533 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3534 type_(type), expr_(expr)
3539 do_traverse(Traverse* traverse);
3543 { return this->type_; }
3546 do_determine_type(const Type_context*)
3552 return new Unsafe_type_conversion_expression(this->type_,
3553 this->expr_->copy(),
3558 do_get_tree(Translate_context*);
3561 // The type to convert to.
3563 // The expression to convert.
3570 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3572 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3573 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3574 return TRAVERSE_EXIT;
3575 return TRAVERSE_CONTINUE;
3578 // Convert to backend representation.
3581 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3583 // We are only called for a limited number of cases.
3585 Type* t = this->type_;
3586 Type* et = this->expr_->type();
3588 tree type_tree = this->type_->get_tree(context->gogo());
3589 tree expr_tree = this->expr_->get_tree(context);
3590 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3591 return error_mark_node;
3593 source_location loc = this->location();
3595 bool use_view_convert = false;
3596 if (t->is_open_array_type())
3598 gcc_assert(et->is_open_array_type());
3599 use_view_convert = true;
3601 else if (t->map_type() != NULL)
3602 gcc_assert(et->map_type() != NULL);
3603 else if (t->channel_type() != NULL)
3604 gcc_assert(et->channel_type() != NULL);
3605 else if (t->points_to() != NULL && t->points_to()->channel_type() != NULL)
3606 gcc_assert(et->points_to() != NULL
3607 && et->points_to()->channel_type() != NULL);
3608 else if (t->is_unsafe_pointer_type())
3609 gcc_assert(et->points_to() != NULL);
3610 else if (et->is_unsafe_pointer_type())
3611 gcc_assert(t->points_to() != NULL);
3612 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3614 gcc_assert(et->interface_type() != NULL
3615 && !et->interface_type()->is_empty());
3616 use_view_convert = true;
3618 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3620 gcc_assert(et->interface_type() != NULL
3621 && et->interface_type()->is_empty());
3622 use_view_convert = true;
3627 if (use_view_convert)
3628 return fold_build1_loc(loc, VIEW_CONVERT_EXPR, type_tree, expr_tree);
3630 return fold_convert_loc(loc, type_tree, expr_tree);
3633 // Make an unsafe type conversion expression.
3636 Expression::make_unsafe_cast(Type* type, Expression* expr,
3637 source_location location)
3639 return new Unsafe_type_conversion_expression(type, expr, location);
3642 // Unary expressions.
3644 class Unary_expression : public Expression
3647 Unary_expression(Operator op, Expression* expr, source_location location)
3648 : Expression(EXPRESSION_UNARY, location),
3649 op_(op), escapes_(true), expr_(expr)
3652 // Return the operator.
3655 { return this->op_; }
3657 // Return the operand.
3660 { return this->expr_; }
3662 // Record that an address expression does not escape.
3664 set_does_not_escape()
3666 gcc_assert(this->op_ == OPERATOR_AND);
3667 this->escapes_ = false;
3670 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3671 // could be done, false if not.
3673 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3676 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3677 // could be done, false if not.
3679 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3681 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3682 // true if this could be done, false if not.
3684 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3692 do_traverse(Traverse* traverse)
3693 { return Expression::traverse(&this->expr_, traverse); }
3696 do_lower(Gogo*, Named_object*, int);
3699 do_is_constant() const;
3702 do_integer_constant_value(bool, mpz_t, Type**) const;
3705 do_float_constant_value(mpfr_t, Type**) const;
3708 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3714 do_determine_type(const Type_context*);
3717 do_check_types(Gogo*);
3722 return Expression::make_unary(this->op_, this->expr_->copy(),
3727 do_is_addressable() const
3728 { return this->op_ == OPERATOR_MULT; }
3731 do_get_tree(Translate_context*);
3734 do_export(Export*) const;
3737 // The unary operator to apply.
3739 // Normally true. False if this is an address expression which does
3740 // not escape the current function.
3746 // If we are taking the address of a composite literal, and the
3747 // contents are not constant, then we want to make a heap composite
3751 Unary_expression::do_lower(Gogo*, Named_object*, int)
3753 source_location loc = this->location();
3754 Operator op = this->op_;
3755 Expression* expr = this->expr_;
3757 if (op == OPERATOR_MULT && expr->is_type_expression())
3758 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3760 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3761 // moving x to the heap. FIXME: Is it worth doing a real escape
3762 // analysis here? This case is found in math/unsafe.go and is
3763 // therefore worth special casing.
3764 if (op == OPERATOR_MULT)
3766 Expression* e = expr;
3767 while (e->classification() == EXPRESSION_CONVERSION)
3769 Type_conversion_expression* te
3770 = static_cast<Type_conversion_expression*>(e);
3774 if (e->classification() == EXPRESSION_UNARY)
3776 Unary_expression* ue = static_cast<Unary_expression*>(e);
3777 if (ue->op_ == OPERATOR_AND)
3784 ue->set_does_not_escape();
3789 // Catching an invalid indirection of unsafe.Pointer here avoid
3790 // having to deal with TYPE_VOID in other places.
3791 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
3793 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3794 return Expression::make_error(this->location());
3797 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
3798 || op == OPERATOR_NOT || op == OPERATOR_XOR)
3800 Expression* ret = NULL;
3805 if (expr->integer_constant_value(false, eval, &etype))
3809 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
3810 ret = Expression::make_integer(&val, etype, loc);
3817 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
3822 if (expr->float_constant_value(fval, &ftype))
3826 if (Unary_expression::eval_float(op, fval, val))
3827 ret = Expression::make_float(&val, ftype, loc);
3838 if (expr->complex_constant_value(fval, ival, &ftype))
3844 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
3845 ret = Expression::make_complex(&real, &imag, ftype, loc);
3859 // Return whether a unary expression is a constant.
3862 Unary_expression::do_is_constant() const
3864 if (this->op_ == OPERATOR_MULT)
3866 // Indirecting through a pointer is only constant if the object
3867 // to which the expression points is constant, but we currently
3868 // have no way to determine that.
3871 else if (this->op_ == OPERATOR_AND)
3873 // Taking the address of a variable is constant if it is a
3874 // global variable, not constant otherwise. In other cases
3875 // taking the address is probably not a constant.
3876 Var_expression* ve = this->expr_->var_expression();
3879 Named_object* no = ve->named_object();
3880 return no->is_variable() && no->var_value()->is_global();
3885 return this->expr_->is_constant();
3888 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3889 // UVAL, if known; it may be NULL. Return true if this could be done,
3893 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3894 source_location location)
3901 case OPERATOR_MINUS:
3903 return Integer_expression::check_constant(val, utype, location);
3905 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3909 || utype->integer_type() == NULL
3910 || utype->integer_type()->is_abstract())
3914 // The number of HOST_WIDE_INTs that it takes to represent
3916 size_t count = ((mpz_sizeinbase(uval, 2)
3917 + HOST_BITS_PER_WIDE_INT
3919 / HOST_BITS_PER_WIDE_INT);
3921 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3922 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3925 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3926 gcc_assert(ecount <= count);
3928 // Trim down to the number of words required by the type.
3929 size_t obits = utype->integer_type()->bits();
3930 if (!utype->integer_type()->is_unsigned())
3932 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3933 / HOST_BITS_PER_WIDE_INT);
3934 gcc_assert(ocount <= count);
3936 for (size_t i = 0; i < ocount; ++i)
3939 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3941 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3944 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3948 return Integer_expression::check_constant(val, utype, location);
3957 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3958 // could be done, false if not.
3961 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
3966 mpfr_set(val, uval, GMP_RNDN);
3968 case OPERATOR_MINUS:
3969 mpfr_neg(val, uval, GMP_RNDN);
3981 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3982 // if this could be done, false if not.
3985 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
3986 mpfr_t real, mpfr_t imag)
3991 mpfr_set(real, rval, GMP_RNDN);
3992 mpfr_set(imag, ival, GMP_RNDN);
3994 case OPERATOR_MINUS:
3995 mpfr_neg(real, rval, GMP_RNDN);
3996 mpfr_neg(imag, ival, GMP_RNDN);
4008 // Return the integral constant value of a unary expression, if it has one.
4011 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
4017 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
4020 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
4026 // Return the floating point constant value of a unary expression, if
4030 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
4035 if (!this->expr_->float_constant_value(uval, ptype))
4038 ret = Unary_expression::eval_float(this->op_, uval, val);
4043 // Return the complex constant value of a unary expression, if it has
4047 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
4055 if (!this->expr_->complex_constant_value(rval, ival, ptype))
4058 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
4064 // Return the type of a unary expression.
4067 Unary_expression::do_type()
4072 case OPERATOR_MINUS:
4075 return this->expr_->type();
4078 return Type::make_pointer_type(this->expr_->type());
4082 Type* subtype = this->expr_->type();
4083 Type* points_to = subtype->points_to();
4084 if (points_to == NULL)
4085 return Type::make_error_type();
4094 // Determine abstract types for a unary expression.
4097 Unary_expression::do_determine_type(const Type_context* context)
4102 case OPERATOR_MINUS:
4105 this->expr_->determine_type(context);
4109 // Taking the address of something.
4111 Type* subtype = (context->type == NULL
4113 : context->type->points_to());
4114 Type_context subcontext(subtype, false);
4115 this->expr_->determine_type(&subcontext);
4120 // Indirecting through a pointer.
4122 Type* subtype = (context->type == NULL
4124 : Type::make_pointer_type(context->type));
4125 Type_context subcontext(subtype, false);
4126 this->expr_->determine_type(&subcontext);
4135 // Check types for a unary expression.
4138 Unary_expression::do_check_types(Gogo*)
4140 Type* type = this->expr_->type();
4141 if (type->is_error())
4143 this->set_is_error();
4150 case OPERATOR_MINUS:
4151 if (type->integer_type() == NULL
4152 && type->float_type() == NULL
4153 && type->complex_type() == NULL)
4154 this->report_error(_("expected numeric type"));
4159 if (type->integer_type() == NULL
4160 && !type->is_boolean_type())
4161 this->report_error(_("expected integer or boolean type"));
4165 if (!this->expr_->is_addressable())
4166 this->report_error(_("invalid operand for unary %<&%>"));
4168 this->expr_->address_taken(this->escapes_);
4172 // Indirecting through a pointer.
4173 if (type->points_to() == NULL)
4174 this->report_error(_("expected pointer"));
4182 // Get a tree for a unary expression.
4185 Unary_expression::do_get_tree(Translate_context* context)
4187 tree expr = this->expr_->get_tree(context);
4188 if (expr == error_mark_node)
4189 return error_mark_node;
4191 source_location loc = this->location();
4197 case OPERATOR_MINUS:
4199 tree type = TREE_TYPE(expr);
4200 tree compute_type = excess_precision_type(type);
4201 if (compute_type != NULL_TREE)
4202 expr = ::convert(compute_type, expr);
4203 tree ret = fold_build1_loc(loc, NEGATE_EXPR,
4204 (compute_type != NULL_TREE
4208 if (compute_type != NULL_TREE)
4209 ret = ::convert(type, ret);
4214 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4215 return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr);
4217 return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr,
4218 build_int_cst(TREE_TYPE(expr), 0));
4221 return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr);
4224 // We should not see a non-constant constructor here; cases
4225 // where we would see one should have been moved onto the heap
4226 // at parse time. Taking the address of a nonconstant
4227 // constructor will not do what the programmer expects.
4228 gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4229 gcc_assert(TREE_CODE(expr) != ADDR_EXPR);
4231 // Build a decl for a constant constructor.
4232 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4234 tree decl = build_decl(this->location(), VAR_DECL,
4235 create_tmp_var_name("C"), TREE_TYPE(expr));
4236 DECL_EXTERNAL(decl) = 0;
4237 TREE_PUBLIC(decl) = 0;
4238 TREE_READONLY(decl) = 1;
4239 TREE_CONSTANT(decl) = 1;
4240 TREE_STATIC(decl) = 1;
4241 TREE_ADDRESSABLE(decl) = 1;
4242 DECL_ARTIFICIAL(decl) = 1;
4243 DECL_INITIAL(decl) = expr;
4244 rest_of_decl_compilation(decl, 1, 0);
4248 return build_fold_addr_expr_loc(loc, expr);
4252 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4254 // If we are dereferencing the pointer to a large struct, we
4255 // need to check for nil. We don't bother to check for small
4256 // structs because we expect the system to crash on a nil
4257 // pointer dereference.
4258 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4259 if (s == -1 || s >= 4096)
4262 expr = save_expr(expr);
4263 tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node,
4265 fold_convert(TREE_TYPE(expr),
4266 null_pointer_node));
4267 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4269 expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr),
4270 build3(COND_EXPR, void_type_node,
4271 compare, crash, NULL_TREE),
4275 // If the type of EXPR is a recursive pointer type, then we
4276 // need to insert a cast before indirecting.
4277 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4279 Type* pt = this->expr_->type()->points_to();
4280 tree ind = pt->get_tree(context->gogo());
4281 expr = fold_convert_loc(loc, build_pointer_type(ind), expr);
4284 return build_fold_indirect_ref_loc(loc, expr);
4292 // Export a unary expression.
4295 Unary_expression::do_export(Export* exp) const
4300 exp->write_c_string("+ ");
4302 case OPERATOR_MINUS:
4303 exp->write_c_string("- ");
4306 exp->write_c_string("! ");
4309 exp->write_c_string("^ ");
4316 this->expr_->export_expression(exp);
4319 // Import a unary expression.
4322 Unary_expression::do_import(Import* imp)
4325 switch (imp->get_char())
4331 op = OPERATOR_MINUS;
4342 imp->require_c_string(" ");
4343 Expression* expr = Expression::import_expression(imp);
4344 return Expression::make_unary(op, expr, imp->location());
4347 // Make a unary expression.
4350 Expression::make_unary(Operator op, Expression* expr, source_location location)
4352 return new Unary_expression(op, expr, location);
4355 // If this is an indirection through a pointer, return the expression
4356 // being pointed through. Otherwise return this.
4361 if (this->classification_ == EXPRESSION_UNARY)
4363 Unary_expression* ue = static_cast<Unary_expression*>(this);
4364 if (ue->op() == OPERATOR_MULT)
4365 return ue->operand();
4370 // Class Binary_expression.
4375 Binary_expression::do_traverse(Traverse* traverse)
4377 int t = Expression::traverse(&this->left_, traverse);
4378 if (t == TRAVERSE_EXIT)
4379 return TRAVERSE_EXIT;
4380 return Expression::traverse(&this->right_, traverse);
4383 // Compare integer constants according to OP.
4386 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4389 int i = mpz_cmp(left_val, right_val);
4394 case OPERATOR_NOTEQ:
4409 // Compare floating point constants according to OP.
4412 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4417 i = mpfr_cmp(left_val, right_val);
4421 mpfr_init_set(lv, left_val, GMP_RNDN);
4423 mpfr_init_set(rv, right_val, GMP_RNDN);
4424 Float_expression::constrain_float(lv, type);
4425 Float_expression::constrain_float(rv, type);
4426 i = mpfr_cmp(lv, rv);
4434 case OPERATOR_NOTEQ:
4449 // Compare complex constants according to OP. Complex numbers may
4450 // only be compared for equality.
4453 Binary_expression::compare_complex(Operator op, Type* type,
4454 mpfr_t left_real, mpfr_t left_imag,
4455 mpfr_t right_real, mpfr_t right_imag)
4459 is_equal = (mpfr_cmp(left_real, right_real) == 0
4460 && mpfr_cmp(left_imag, right_imag) == 0);
4465 mpfr_init_set(lr, left_real, GMP_RNDN);
4466 mpfr_init_set(li, left_imag, GMP_RNDN);
4469 mpfr_init_set(rr, right_real, GMP_RNDN);
4470 mpfr_init_set(ri, right_imag, GMP_RNDN);
4471 Complex_expression::constrain_complex(lr, li, type);
4472 Complex_expression::constrain_complex(rr, ri, type);
4473 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4483 case OPERATOR_NOTEQ:
4490 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4491 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4492 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4493 // this could be done, false if not.
4496 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4497 Type* right_type, mpz_t right_val,
4498 source_location location, mpz_t val)
4500 bool is_shift_op = false;
4504 case OPERATOR_ANDAND:
4506 case OPERATOR_NOTEQ:
4511 // These return boolean values. We should probably handle them
4512 // anyhow in case a type conversion is used on the result.
4515 mpz_add(val, left_val, right_val);
4517 case OPERATOR_MINUS:
4518 mpz_sub(val, left_val, right_val);
4521 mpz_ior(val, left_val, right_val);
4524 mpz_xor(val, left_val, right_val);
4527 mpz_mul(val, left_val, right_val);
4530 if (mpz_sgn(right_val) != 0)
4531 mpz_tdiv_q(val, left_val, right_val);
4534 error_at(location, "division by zero");
4540 if (mpz_sgn(right_val) != 0)
4541 mpz_tdiv_r(val, left_val, right_val);
4544 error_at(location, "division by zero");
4549 case OPERATOR_LSHIFT:
4551 unsigned long shift = mpz_get_ui(right_val);
4552 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
4554 error_at(location, "shift count overflow");
4558 mpz_mul_2exp(val, left_val, shift);
4563 case OPERATOR_RSHIFT:
4565 unsigned long shift = mpz_get_ui(right_val);
4566 if (mpz_cmp_ui(right_val, shift) != 0)
4568 error_at(location, "shift count overflow");
4572 if (mpz_cmp_ui(left_val, 0) >= 0)
4573 mpz_tdiv_q_2exp(val, left_val, shift);
4575 mpz_fdiv_q_2exp(val, left_val, shift);
4581 mpz_and(val, left_val, right_val);
4583 case OPERATOR_BITCLEAR:
4587 mpz_com(tval, right_val);
4588 mpz_and(val, left_val, tval);
4596 Type* type = left_type;
4601 else if (type != right_type && right_type != NULL)
4603 if (type->is_abstract())
4605 else if (!right_type->is_abstract())
4607 // This look like a type error which should be diagnosed
4608 // elsewhere. Don't do anything here, to avoid an
4609 // unhelpful chain of error messages.
4615 if (type != NULL && !type->is_abstract())
4617 // We have to check the operands too, as we have implicitly
4618 // coerced them to TYPE.
4619 if ((type != left_type
4620 && !Integer_expression::check_constant(left_val, type, location))
4622 && type != right_type
4623 && !Integer_expression::check_constant(right_val, type,
4625 || !Integer_expression::check_constant(val, type, location))
4632 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4633 // Return true if this could be done, false if not.
4636 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4637 Type* right_type, mpfr_t right_val,
4638 mpfr_t val, source_location location)
4643 case OPERATOR_ANDAND:
4645 case OPERATOR_NOTEQ:
4650 // These return boolean values. We should probably handle them
4651 // anyhow in case a type conversion is used on the result.
4654 mpfr_add(val, left_val, right_val, GMP_RNDN);
4656 case OPERATOR_MINUS:
4657 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4662 case OPERATOR_BITCLEAR:
4665 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4668 if (mpfr_zero_p(right_val))
4669 error_at(location, "division by zero");
4670 mpfr_div(val, left_val, right_val, GMP_RNDN);
4674 case OPERATOR_LSHIFT:
4675 case OPERATOR_RSHIFT:
4681 Type* type = left_type;
4684 else if (type != right_type && right_type != NULL)
4686 if (type->is_abstract())
4688 else if (!right_type->is_abstract())
4690 // This looks like a type error which should be diagnosed
4691 // elsewhere. Don't do anything here, to avoid an unhelpful
4692 // chain of error messages.
4697 if (type != NULL && !type->is_abstract())
4699 if ((type != left_type
4700 && !Float_expression::check_constant(left_val, type, location))
4701 || (type != right_type
4702 && !Float_expression::check_constant(right_val, type,
4704 || !Float_expression::check_constant(val, type, location))
4705 mpfr_set_ui(val, 0, GMP_RNDN);
4711 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4712 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4713 // could be done, false if not.
4716 Binary_expression::eval_complex(Operator op, Type* left_type,
4717 mpfr_t left_real, mpfr_t left_imag,
4719 mpfr_t right_real, mpfr_t right_imag,
4720 mpfr_t real, mpfr_t imag,
4721 source_location location)
4726 case OPERATOR_ANDAND:
4728 case OPERATOR_NOTEQ:
4733 // These return boolean values and must be handled differently.
4736 mpfr_add(real, left_real, right_real, GMP_RNDN);
4737 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4739 case OPERATOR_MINUS:
4740 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4741 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4746 case OPERATOR_BITCLEAR:
4750 // You might think that multiplying two complex numbers would
4751 // be simple, and you would be right, until you start to think
4752 // about getting the right answer for infinity. If one
4753 // operand here is infinity and the other is anything other
4754 // than zero or NaN, then we are going to wind up subtracting
4755 // two infinity values. That will give us a NaN, but the
4756 // correct answer is infinity.
4760 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4764 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4768 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4772 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4774 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4775 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4777 // If we get NaN on both sides, check whether it should really
4778 // be infinity. The rule is that if either side of the
4779 // complex number is infinity, then the whole value is
4780 // infinity, even if the other side is NaN. So the only case
4781 // we have to fix is the one in which both sides are NaN.
4782 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4783 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4784 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4786 bool is_infinity = false;
4790 mpfr_init_set(lr, left_real, GMP_RNDN);
4791 mpfr_init_set(li, left_imag, GMP_RNDN);
4795 mpfr_init_set(rr, right_real, GMP_RNDN);
4796 mpfr_init_set(ri, right_imag, GMP_RNDN);
4798 // If the left side is infinity, then the result is
4800 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4802 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4803 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4804 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4805 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4808 mpfr_set_ui(rr, 0, GMP_RNDN);
4809 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4813 mpfr_set_ui(ri, 0, GMP_RNDN);
4814 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4819 // If the right side is infinity, then the result is
4821 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4823 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4824 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4825 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4826 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4829 mpfr_set_ui(lr, 0, GMP_RNDN);
4830 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4834 mpfr_set_ui(li, 0, GMP_RNDN);
4835 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4840 // If we got an overflow in the intermediate computations,
4841 // then the result is infinity.
4843 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4844 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4848 mpfr_set_ui(lr, 0, GMP_RNDN);
4849 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4853 mpfr_set_ui(li, 0, GMP_RNDN);
4854 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4858 mpfr_set_ui(rr, 0, GMP_RNDN);
4859 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4863 mpfr_set_ui(ri, 0, GMP_RNDN);
4864 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4871 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4872 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4873 mpfr_mul(lirr, li, rr, GMP_RNDN);
4874 mpfr_mul(liri, li, ri, GMP_RNDN);
4875 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4876 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4877 mpfr_set_inf(real, mpfr_sgn(real));
4878 mpfr_set_inf(imag, mpfr_sgn(imag));
4895 // For complex division we want to avoid having an
4896 // intermediate overflow turn the whole result in a NaN. We
4897 // scale the values to try to avoid this.
4899 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
4900 error_at(location, "division by zero");
4906 mpfr_abs(rra, right_real, GMP_RNDN);
4907 mpfr_abs(ria, right_imag, GMP_RNDN);
4910 mpfr_max(t, rra, ria, GMP_RNDN);
4914 mpfr_init_set(rr, right_real, GMP_RNDN);
4915 mpfr_init_set(ri, right_imag, GMP_RNDN);
4917 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4919 ilogbw = mpfr_get_exp(t);
4920 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4921 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4926 mpfr_mul(denom, rr, rr, GMP_RNDN);
4927 mpfr_mul(t, ri, ri, GMP_RNDN);
4928 mpfr_add(denom, denom, t, GMP_RNDN);
4930 mpfr_mul(real, left_real, rr, GMP_RNDN);
4931 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4932 mpfr_add(real, real, t, GMP_RNDN);
4933 mpfr_div(real, real, denom, GMP_RNDN);
4934 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4936 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4937 mpfr_mul(t, left_real, ri, GMP_RNDN);
4938 mpfr_sub(imag, imag, t, GMP_RNDN);
4939 mpfr_div(imag, imag, denom, GMP_RNDN);
4940 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4942 // If we wind up with NaN on both sides, check whether we
4943 // should really have infinity. The rule is that if either
4944 // side of the complex number is infinity, then the whole
4945 // value is infinity, even if the other side is NaN. So the
4946 // only case we have to fix is the one in which both sides are
4948 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4949 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4950 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4952 if (mpfr_zero_p(denom))
4954 mpfr_set_inf(real, mpfr_sgn(rr));
4955 mpfr_mul(real, real, left_real, GMP_RNDN);
4956 mpfr_set_inf(imag, mpfr_sgn(rr));
4957 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4959 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4960 && mpfr_number_p(rr) && mpfr_number_p(ri))
4962 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
4963 mpfr_copysign(t, t, left_real, GMP_RNDN);
4966 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
4967 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
4971 mpfr_mul(t3, t, rr, GMP_RNDN);
4975 mpfr_mul(t4, t2, ri, GMP_RNDN);
4977 mpfr_add(t3, t3, t4, GMP_RNDN);
4978 mpfr_set_inf(real, mpfr_sgn(t3));
4980 mpfr_mul(t3, t2, rr, GMP_RNDN);
4981 mpfr_mul(t4, t, ri, GMP_RNDN);
4982 mpfr_sub(t3, t3, t4, GMP_RNDN);
4983 mpfr_set_inf(imag, mpfr_sgn(t3));
4989 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
4990 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
4992 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4993 mpfr_copysign(t, t, rr, GMP_RNDN);
4996 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4997 mpfr_copysign(t2, t2, ri, GMP_RNDN);
5001 mpfr_mul(t3, left_real, t, GMP_RNDN);
5005 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
5007 mpfr_add(t3, t3, t4, GMP_RNDN);
5008 mpfr_set_ui(real, 0, GMP_RNDN);
5009 mpfr_mul(real, real, t3, GMP_RNDN);
5011 mpfr_mul(t3, left_imag, t, GMP_RNDN);
5012 mpfr_mul(t4, left_real, t2, GMP_RNDN);
5013 mpfr_sub(t3, t3, t4, GMP_RNDN);
5014 mpfr_set_ui(imag, 0, GMP_RNDN);
5015 mpfr_mul(imag, imag, t3, GMP_RNDN);
5033 case OPERATOR_LSHIFT:
5034 case OPERATOR_RSHIFT:
5040 Type* type = left_type;
5043 else if (type != right_type && right_type != NULL)
5045 if (type->is_abstract())
5047 else if (!right_type->is_abstract())
5049 // This looks like a type error which should be diagnosed
5050 // elsewhere. Don't do anything here, to avoid an unhelpful
5051 // chain of error messages.
5056 if (type != NULL && !type->is_abstract())
5058 if ((type != left_type
5059 && !Complex_expression::check_constant(left_real, left_imag,
5061 || (type != right_type
5062 && !Complex_expression::check_constant(right_real, right_imag,
5064 || !Complex_expression::check_constant(real, imag, type,
5067 mpfr_set_ui(real, 0, GMP_RNDN);
5068 mpfr_set_ui(imag, 0, GMP_RNDN);
5075 // Lower a binary expression. We have to evaluate constant
5076 // expressions now, in order to implement Go's unlimited precision
5080 Binary_expression::do_lower(Gogo*, Named_object*, int)
5082 source_location location = this->location();
5083 Operator op = this->op_;
5084 Expression* left = this->left_;
5085 Expression* right = this->right_;
5087 const bool is_comparison = (op == OPERATOR_EQEQ
5088 || op == OPERATOR_NOTEQ
5089 || op == OPERATOR_LT
5090 || op == OPERATOR_LE
5091 || op == OPERATOR_GT
5092 || op == OPERATOR_GE);
5094 // Integer constant expressions.
5100 mpz_init(right_val);
5102 if (left->integer_constant_value(false, left_val, &left_type)
5103 && right->integer_constant_value(false, right_val, &right_type))
5105 Expression* ret = NULL;
5106 if (left_type != right_type
5107 && left_type != NULL
5108 && right_type != NULL
5109 && left_type->base() != right_type->base()
5110 && op != OPERATOR_LSHIFT
5111 && op != OPERATOR_RSHIFT)
5113 // May be a type error--let it be diagnosed later.
5115 else if (is_comparison)
5117 bool b = Binary_expression::compare_integer(op, left_val,
5119 ret = Expression::make_cast(Type::lookup_bool_type(),
5120 Expression::make_boolean(b, location),
5128 if (Binary_expression::eval_integer(op, left_type, left_val,
5129 right_type, right_val,
5132 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
5134 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5136 else if (left_type == NULL)
5138 else if (right_type == NULL)
5140 else if (!left_type->is_abstract()
5141 && left_type->named_type() != NULL)
5143 else if (!right_type->is_abstract()
5144 && right_type->named_type() != NULL)
5146 else if (!left_type->is_abstract())
5148 else if (!right_type->is_abstract())
5150 else if (left_type->float_type() != NULL)
5152 else if (right_type->float_type() != NULL)
5154 else if (left_type->complex_type() != NULL)
5156 else if (right_type->complex_type() != NULL)
5160 ret = Expression::make_integer(&val, type, location);
5168 mpz_clear(right_val);
5169 mpz_clear(left_val);
5173 mpz_clear(right_val);
5174 mpz_clear(left_val);
5177 // Floating point constant expressions.
5180 mpfr_init(left_val);
5183 mpfr_init(right_val);
5185 if (left->float_constant_value(left_val, &left_type)
5186 && right->float_constant_value(right_val, &right_type))
5188 Expression* ret = NULL;
5189 if (left_type != right_type
5190 && left_type != NULL
5191 && right_type != NULL
5192 && left_type->base() != right_type->base()
5193 && op != OPERATOR_LSHIFT
5194 && op != OPERATOR_RSHIFT)
5196 // May be a type error--let it be diagnosed later.
5198 else if (is_comparison)
5200 bool b = Binary_expression::compare_float(op,
5204 left_val, right_val);
5205 ret = Expression::make_boolean(b, location);
5212 if (Binary_expression::eval_float(op, left_type, left_val,
5213 right_type, right_val, val,
5216 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5217 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5219 if (left_type == NULL)
5221 else if (right_type == NULL)
5223 else if (!left_type->is_abstract()
5224 && left_type->named_type() != NULL)
5226 else if (!right_type->is_abstract()
5227 && right_type->named_type() != NULL)
5229 else if (!left_type->is_abstract())
5231 else if (!right_type->is_abstract())
5233 else if (left_type->float_type() != NULL)
5235 else if (right_type->float_type() != NULL)
5239 ret = Expression::make_float(&val, type, location);
5247 mpfr_clear(right_val);
5248 mpfr_clear(left_val);
5252 mpfr_clear(right_val);
5253 mpfr_clear(left_val);
5256 // Complex constant expressions.
5260 mpfr_init(left_real);
5261 mpfr_init(left_imag);
5266 mpfr_init(right_real);
5267 mpfr_init(right_imag);
5270 if (left->complex_constant_value(left_real, left_imag, &left_type)
5271 && right->complex_constant_value(right_real, right_imag, &right_type))
5273 Expression* ret = NULL;
5274 if (left_type != right_type
5275 && left_type != NULL
5276 && right_type != NULL
5277 && left_type->base() != right_type->base())
5279 // May be a type error--let it be diagnosed later.
5281 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5283 bool b = Binary_expression::compare_complex(op,
5291 ret = Expression::make_boolean(b, location);
5300 if (Binary_expression::eval_complex(op, left_type,
5301 left_real, left_imag,
5303 right_real, right_imag,
5307 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5308 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5310 if (left_type == NULL)
5312 else if (right_type == NULL)
5314 else if (!left_type->is_abstract()
5315 && left_type->named_type() != NULL)
5317 else if (!right_type->is_abstract()
5318 && right_type->named_type() != NULL)
5320 else if (!left_type->is_abstract())
5322 else if (!right_type->is_abstract())
5324 else if (left_type->complex_type() != NULL)
5326 else if (right_type->complex_type() != NULL)
5330 ret = Expression::make_complex(&real, &imag, type,
5339 mpfr_clear(left_real);
5340 mpfr_clear(left_imag);
5341 mpfr_clear(right_real);
5342 mpfr_clear(right_imag);
5347 mpfr_clear(left_real);
5348 mpfr_clear(left_imag);
5349 mpfr_clear(right_real);
5350 mpfr_clear(right_imag);
5353 // String constant expressions.
5354 if (op == OPERATOR_PLUS
5355 && left->type()->is_string_type()
5356 && right->type()->is_string_type())
5358 std::string left_string;
5359 std::string right_string;
5360 if (left->string_constant_value(&left_string)
5361 && right->string_constant_value(&right_string))
5362 return Expression::make_string(left_string + right_string, location);
5368 // Return the integer constant value, if it has one.
5371 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5377 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5380 mpz_clear(left_val);
5385 mpz_init(right_val);
5387 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5390 mpz_clear(right_val);
5391 mpz_clear(left_val);
5396 if (left_type != right_type
5397 && left_type != NULL
5398 && right_type != NULL
5399 && left_type->base() != right_type->base()
5400 && this->op_ != OPERATOR_RSHIFT
5401 && this->op_ != OPERATOR_LSHIFT)
5404 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5405 right_type, right_val,
5406 this->location(), val);
5408 mpz_clear(right_val);
5409 mpz_clear(left_val);
5417 // Return the floating point constant value, if it has one.
5420 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5423 mpfr_init(left_val);
5425 if (!this->left_->float_constant_value(left_val, &left_type))
5427 mpfr_clear(left_val);
5432 mpfr_init(right_val);
5434 if (!this->right_->float_constant_value(right_val, &right_type))
5436 mpfr_clear(right_val);
5437 mpfr_clear(left_val);
5442 if (left_type != right_type
5443 && left_type != NULL
5444 && right_type != NULL
5445 && left_type->base() != right_type->base())
5448 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5449 right_type, right_val,
5450 val, this->location());
5452 mpfr_clear(left_val);
5453 mpfr_clear(right_val);
5461 // Return the complex constant value, if it has one.
5464 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5469 mpfr_init(left_real);
5470 mpfr_init(left_imag);
5472 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5474 mpfr_clear(left_real);
5475 mpfr_clear(left_imag);
5481 mpfr_init(right_real);
5482 mpfr_init(right_imag);
5484 if (!this->right_->complex_constant_value(right_real, right_imag,
5487 mpfr_clear(left_real);
5488 mpfr_clear(left_imag);
5489 mpfr_clear(right_real);
5490 mpfr_clear(right_imag);
5495 if (left_type != right_type
5496 && left_type != NULL
5497 && right_type != NULL
5498 && left_type->base() != right_type->base())
5501 ret = Binary_expression::eval_complex(this->op_, left_type,
5502 left_real, left_imag,
5504 right_real, right_imag,
5507 mpfr_clear(left_real);
5508 mpfr_clear(left_imag);
5509 mpfr_clear(right_real);
5510 mpfr_clear(right_imag);
5518 // Note that the value is being discarded.
5521 Binary_expression::do_discarding_value()
5523 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5524 this->right_->discarding_value();
5526 this->warn_about_unused_value();
5532 Binary_expression::do_type()
5534 if (this->classification() == EXPRESSION_ERROR)
5535 return Type::make_error_type();
5540 case OPERATOR_ANDAND:
5542 case OPERATOR_NOTEQ:
5547 return Type::lookup_bool_type();
5550 case OPERATOR_MINUS:
5557 case OPERATOR_BITCLEAR:
5559 Type* left_type = this->left_->type();
5560 Type* right_type = this->right_->type();
5561 if (left_type->is_error())
5563 else if (right_type->is_error())
5565 else if (!Type::are_compatible_for_binop(left_type, right_type))
5567 this->report_error(_("incompatible types in binary expression"));
5568 return Type::make_error_type();
5570 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
5572 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5574 else if (!left_type->is_abstract())
5576 else if (!right_type->is_abstract())
5578 else if (left_type->complex_type() != NULL)
5580 else if (right_type->complex_type() != NULL)
5582 else if (left_type->float_type() != NULL)
5584 else if (right_type->float_type() != NULL)
5590 case OPERATOR_LSHIFT:
5591 case OPERATOR_RSHIFT:
5592 return this->left_->type();
5599 // Set type for a binary expression.
5602 Binary_expression::do_determine_type(const Type_context* context)
5604 Type* tleft = this->left_->type();
5605 Type* tright = this->right_->type();
5607 // Both sides should have the same type, except for the shift
5608 // operations. For a comparison, we should ignore the incoming
5611 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5612 || this->op_ == OPERATOR_RSHIFT);
5614 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5615 || this->op_ == OPERATOR_NOTEQ
5616 || this->op_ == OPERATOR_LT
5617 || this->op_ == OPERATOR_LE
5618 || this->op_ == OPERATOR_GT
5619 || this->op_ == OPERATOR_GE);
5621 Type_context subcontext(*context);
5625 // In a comparison, the context does not determine the types of
5627 subcontext.type = NULL;
5630 // Set the context for the left hand operand.
5633 // The right hand operand plays no role in determining the type
5634 // of the left hand operand. A shift of an abstract integer in
5635 // a string context gets special treatment, which may be a
5637 if (subcontext.type != NULL
5638 && subcontext.type->is_string_type()
5639 && tleft->is_abstract())
5640 error_at(this->location(), "shift of non-integer operand");
5642 else if (!tleft->is_abstract())
5643 subcontext.type = tleft;
5644 else if (!tright->is_abstract())
5645 subcontext.type = tright;
5646 else if (subcontext.type == NULL)
5648 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5649 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5650 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5652 // Both sides have an abstract integer, abstract float, or
5653 // abstract complex type. Just let CONTEXT determine
5654 // whether they may remain abstract or not.
5656 else if (tleft->complex_type() != NULL)
5657 subcontext.type = tleft;
5658 else if (tright->complex_type() != NULL)
5659 subcontext.type = tright;
5660 else if (tleft->float_type() != NULL)
5661 subcontext.type = tleft;
5662 else if (tright->float_type() != NULL)
5663 subcontext.type = tright;
5665 subcontext.type = tleft;
5667 if (subcontext.type != NULL && !context->may_be_abstract)
5668 subcontext.type = subcontext.type->make_non_abstract_type();
5671 this->left_->determine_type(&subcontext);
5673 // The context for the right hand operand is the same as for the
5674 // left hand operand, except for a shift operator.
5677 subcontext.type = Type::lookup_integer_type("uint");
5678 subcontext.may_be_abstract = false;
5681 this->right_->determine_type(&subcontext);
5684 // Report an error if the binary operator OP does not support TYPE.
5685 // Return whether the operation is OK. This should not be used for
5689 Binary_expression::check_operator_type(Operator op, Type* type,
5690 source_location location)
5695 case OPERATOR_ANDAND:
5696 if (!type->is_boolean_type())
5698 error_at(location, "expected boolean type");
5704 case OPERATOR_NOTEQ:
5705 if (type->integer_type() == NULL
5706 && type->float_type() == NULL
5707 && type->complex_type() == NULL
5708 && !type->is_string_type()
5709 && type->points_to() == NULL
5710 && !type->is_nil_type()
5711 && !type->is_boolean_type()
5712 && type->interface_type() == NULL
5713 && (type->array_type() == NULL
5714 || type->array_type()->length() != NULL)
5715 && type->map_type() == NULL
5716 && type->channel_type() == NULL
5717 && type->function_type() == NULL)
5720 ("expected integer, floating, complex, string, pointer, "
5721 "boolean, interface, slice, map, channel, "
5722 "or function type"));
5731 if (type->integer_type() == NULL
5732 && type->float_type() == NULL
5733 && !type->is_string_type())
5735 error_at(location, "expected integer, floating, or string type");
5741 case OPERATOR_PLUSEQ:
5742 if (type->integer_type() == NULL
5743 && type->float_type() == NULL
5744 && type->complex_type() == NULL
5745 && !type->is_string_type())
5748 "expected integer, floating, complex, or string type");
5753 case OPERATOR_MINUS:
5754 case OPERATOR_MINUSEQ:
5756 case OPERATOR_MULTEQ:
5758 case OPERATOR_DIVEQ:
5759 if (type->integer_type() == NULL
5760 && type->float_type() == NULL
5761 && type->complex_type() == NULL)
5763 error_at(location, "expected integer, floating, or complex type");
5769 case OPERATOR_MODEQ:
5773 case OPERATOR_ANDEQ:
5775 case OPERATOR_XOREQ:
5776 case OPERATOR_BITCLEAR:
5777 case OPERATOR_BITCLEAREQ:
5778 if (type->integer_type() == NULL)
5780 error_at(location, "expected integer type");
5795 Binary_expression::do_check_types(Gogo*)
5797 if (this->classification() == EXPRESSION_ERROR)
5800 Type* left_type = this->left_->type();
5801 Type* right_type = this->right_->type();
5802 if (left_type->is_error() || right_type->is_error())
5804 this->set_is_error();
5808 if (this->op_ == OPERATOR_EQEQ
5809 || this->op_ == OPERATOR_NOTEQ
5810 || this->op_ == OPERATOR_LT
5811 || this->op_ == OPERATOR_LE
5812 || this->op_ == OPERATOR_GT
5813 || this->op_ == OPERATOR_GE)
5815 if (!Type::are_assignable(left_type, right_type, NULL)
5816 && !Type::are_assignable(right_type, left_type, NULL))
5818 this->report_error(_("incompatible types in binary expression"));
5821 if (!Binary_expression::check_operator_type(this->op_, left_type,
5823 || !Binary_expression::check_operator_type(this->op_, right_type,
5826 this->set_is_error();
5830 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5832 if (!Type::are_compatible_for_binop(left_type, right_type))
5834 this->report_error(_("incompatible types in binary expression"));
5837 if (!Binary_expression::check_operator_type(this->op_, left_type,
5840 this->set_is_error();
5846 if (left_type->integer_type() == NULL)
5847 this->report_error(_("shift of non-integer operand"));
5849 if (!right_type->is_abstract()
5850 && (right_type->integer_type() == NULL
5851 || !right_type->integer_type()->is_unsigned()))
5852 this->report_error(_("shift count not unsigned integer"));
5858 if (this->right_->integer_constant_value(true, val, &type))
5860 if (mpz_sgn(val) < 0)
5862 this->report_error(_("negative shift count"));
5864 source_location rloc = this->right_->location();
5865 this->right_ = Expression::make_integer(&val, right_type,
5874 // Get a tree for a binary expression.
5877 Binary_expression::do_get_tree(Translate_context* context)
5879 tree left = this->left_->get_tree(context);
5880 tree right = this->right_->get_tree(context);
5882 if (left == error_mark_node || right == error_mark_node)
5883 return error_mark_node;
5885 enum tree_code code;
5886 bool use_left_type = true;
5887 bool is_shift_op = false;
5891 case OPERATOR_NOTEQ:
5896 return Expression::comparison_tree(context, this->op_,
5897 this->left_->type(), left,
5898 this->right_->type(), right,
5902 code = TRUTH_ORIF_EXPR;
5903 use_left_type = false;
5905 case OPERATOR_ANDAND:
5906 code = TRUTH_ANDIF_EXPR;
5907 use_left_type = false;
5912 case OPERATOR_MINUS:
5916 code = BIT_IOR_EXPR;
5919 code = BIT_XOR_EXPR;
5926 Type *t = this->left_->type();
5927 if (t->float_type() != NULL || t->complex_type() != NULL)
5930 code = TRUNC_DIV_EXPR;
5934 code = TRUNC_MOD_EXPR;
5936 case OPERATOR_LSHIFT:
5940 case OPERATOR_RSHIFT:
5945 code = BIT_AND_EXPR;
5947 case OPERATOR_BITCLEAR:
5948 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5949 code = BIT_AND_EXPR;
5955 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5957 if (this->left_->type()->is_string_type())
5959 gcc_assert(this->op_ == OPERATOR_PLUS);
5960 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5961 static tree string_plus_decl;
5962 return Gogo::call_builtin(&string_plus_decl,
5973 tree compute_type = excess_precision_type(type);
5974 if (compute_type != NULL_TREE)
5976 left = ::convert(compute_type, left);
5977 right = ::convert(compute_type, right);
5980 tree eval_saved = NULL_TREE;
5983 // Make sure the values are evaluated.
5984 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
5986 left = save_expr(left);
5989 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
5991 right = save_expr(right);
5992 if (eval_saved == NULL_TREE)
5995 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
5996 void_type_node, eval_saved, right);
6000 tree ret = fold_build2_loc(this->location(),
6002 compute_type != NULL_TREE ? compute_type : type,
6005 if (compute_type != NULL_TREE)
6006 ret = ::convert(type, ret);
6008 // In Go, a shift larger than the size of the type is well-defined.
6009 // This is not true in GENERIC, so we need to insert a conditional.
6012 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
6013 gcc_assert(this->left_->type()->integer_type() != NULL);
6014 int bits = TYPE_PRECISION(TREE_TYPE(left));
6016 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
6017 build_int_cst_type(TREE_TYPE(right), bits));
6019 tree overflow_result = fold_convert_loc(this->location(),
6022 if (this->op_ == OPERATOR_RSHIFT
6023 && !this->left_->type()->integer_type()->is_unsigned())
6025 tree neg = fold_build2_loc(this->location(), LT_EXPR,
6026 boolean_type_node, left,
6027 fold_convert_loc(this->location(),
6029 integer_zero_node));
6030 tree neg_one = fold_build2_loc(this->location(),
6031 MINUS_EXPR, TREE_TYPE(left),
6032 fold_convert_loc(this->location(),
6035 fold_convert_loc(this->location(),
6038 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
6039 TREE_TYPE(left), neg, neg_one,
6043 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
6044 compare, ret, overflow_result);
6046 if (eval_saved != NULL_TREE)
6047 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
6048 TREE_TYPE(ret), eval_saved, ret);
6054 // Export a binary expression.
6057 Binary_expression::do_export(Export* exp) const
6059 exp->write_c_string("(");
6060 this->left_->export_expression(exp);
6064 exp->write_c_string(" || ");
6066 case OPERATOR_ANDAND:
6067 exp->write_c_string(" && ");
6070 exp->write_c_string(" == ");
6072 case OPERATOR_NOTEQ:
6073 exp->write_c_string(" != ");
6076 exp->write_c_string(" < ");
6079 exp->write_c_string(" <= ");
6082 exp->write_c_string(" > ");
6085 exp->write_c_string(" >= ");
6088 exp->write_c_string(" + ");
6090 case OPERATOR_MINUS:
6091 exp->write_c_string(" - ");
6094 exp->write_c_string(" | ");
6097 exp->write_c_string(" ^ ");
6100 exp->write_c_string(" * ");
6103 exp->write_c_string(" / ");
6106 exp->write_c_string(" % ");
6108 case OPERATOR_LSHIFT:
6109 exp->write_c_string(" << ");
6111 case OPERATOR_RSHIFT:
6112 exp->write_c_string(" >> ");
6115 exp->write_c_string(" & ");
6117 case OPERATOR_BITCLEAR:
6118 exp->write_c_string(" &^ ");
6123 this->right_->export_expression(exp);
6124 exp->write_c_string(")");
6127 // Import a binary expression.
6130 Binary_expression::do_import(Import* imp)
6132 imp->require_c_string("(");
6134 Expression* left = Expression::import_expression(imp);
6137 if (imp->match_c_string(" || "))
6142 else if (imp->match_c_string(" && "))
6144 op = OPERATOR_ANDAND;
6147 else if (imp->match_c_string(" == "))
6152 else if (imp->match_c_string(" != "))
6154 op = OPERATOR_NOTEQ;
6157 else if (imp->match_c_string(" < "))
6162 else if (imp->match_c_string(" <= "))
6167 else if (imp->match_c_string(" > "))
6172 else if (imp->match_c_string(" >= "))
6177 else if (imp->match_c_string(" + "))
6182 else if (imp->match_c_string(" - "))
6184 op = OPERATOR_MINUS;
6187 else if (imp->match_c_string(" | "))
6192 else if (imp->match_c_string(" ^ "))
6197 else if (imp->match_c_string(" * "))
6202 else if (imp->match_c_string(" / "))
6207 else if (imp->match_c_string(" % "))
6212 else if (imp->match_c_string(" << "))
6214 op = OPERATOR_LSHIFT;
6217 else if (imp->match_c_string(" >> "))
6219 op = OPERATOR_RSHIFT;
6222 else if (imp->match_c_string(" & "))
6227 else if (imp->match_c_string(" &^ "))
6229 op = OPERATOR_BITCLEAR;
6234 error_at(imp->location(), "unrecognized binary operator");
6235 return Expression::make_error(imp->location());
6238 Expression* right = Expression::import_expression(imp);
6240 imp->require_c_string(")");
6242 return Expression::make_binary(op, left, right, imp->location());
6245 // Make a binary expression.
6248 Expression::make_binary(Operator op, Expression* left, Expression* right,
6249 source_location location)
6251 return new Binary_expression(op, left, right, location);
6254 // Implement a comparison.
6257 Expression::comparison_tree(Translate_context* context, Operator op,
6258 Type* left_type, tree left_tree,
6259 Type* right_type, tree right_tree,
6260 source_location location)
6262 enum tree_code code;
6268 case OPERATOR_NOTEQ:
6287 if (left_type->is_string_type() && right_type->is_string_type())
6289 tree string_type = Type::make_string_type()->get_tree(context->gogo());
6290 static tree string_compare_decl;
6291 left_tree = Gogo::call_builtin(&string_compare_decl,
6300 right_tree = build_int_cst_type(integer_type_node, 0);
6302 else if ((left_type->interface_type() != NULL
6303 && right_type->interface_type() == NULL
6304 && !right_type->is_nil_type())
6305 || (left_type->interface_type() == NULL
6306 && !left_type->is_nil_type()
6307 && right_type->interface_type() != NULL))
6309 // Comparing an interface value to a non-interface value.
6310 if (left_type->interface_type() == NULL)
6312 std::swap(left_type, right_type);
6313 std::swap(left_tree, right_tree);
6316 // The right operand is not an interface. We need to take its
6317 // address if it is not a pointer.
6320 if (right_type->points_to() != NULL)
6322 make_tmp = NULL_TREE;
6325 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
6327 make_tmp = NULL_TREE;
6328 arg = build_fold_addr_expr_loc(location, right_tree);
6329 if (DECL_P(right_tree))
6330 TREE_ADDRESSABLE(right_tree) = 1;
6334 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6335 get_name(right_tree));
6336 DECL_IGNORED_P(tmp) = 0;
6337 DECL_INITIAL(tmp) = right_tree;
6338 TREE_ADDRESSABLE(tmp) = 1;
6339 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6340 SET_EXPR_LOCATION(make_tmp, location);
6341 arg = build_fold_addr_expr_loc(location, tmp);
6343 arg = fold_convert_loc(location, ptr_type_node, arg);
6345 tree descriptor = right_type->type_descriptor_pointer(context->gogo());
6347 if (left_type->interface_type()->is_empty())
6349 static tree empty_interface_value_compare_decl;
6350 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6352 "__go_empty_interface_value_compare",
6355 TREE_TYPE(left_tree),
6357 TREE_TYPE(descriptor),
6361 if (left_tree == error_mark_node)
6362 return error_mark_node;
6363 // This can panic if the type is not comparable.
6364 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6368 static tree interface_value_compare_decl;
6369 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6371 "__go_interface_value_compare",
6374 TREE_TYPE(left_tree),
6376 TREE_TYPE(descriptor),
6380 if (left_tree == error_mark_node)
6381 return error_mark_node;
6382 // This can panic if the type is not comparable.
6383 TREE_NOTHROW(interface_value_compare_decl) = 0;
6385 right_tree = build_int_cst_type(integer_type_node, 0);
6387 if (make_tmp != NULL_TREE)
6388 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6391 else if (left_type->interface_type() != NULL
6392 && right_type->interface_type() != NULL)
6394 if (left_type->interface_type()->is_empty()
6395 && right_type->interface_type()->is_empty())
6397 static tree empty_interface_compare_decl;
6398 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6400 "__go_empty_interface_compare",
6403 TREE_TYPE(left_tree),
6405 TREE_TYPE(right_tree),
6407 if (left_tree == error_mark_node)
6408 return error_mark_node;
6409 // This can panic if the type is uncomparable.
6410 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6412 else if (!left_type->interface_type()->is_empty()
6413 && !right_type->interface_type()->is_empty())
6415 static tree interface_compare_decl;
6416 left_tree = Gogo::call_builtin(&interface_compare_decl,
6418 "__go_interface_compare",
6421 TREE_TYPE(left_tree),
6423 TREE_TYPE(right_tree),
6425 if (left_tree == error_mark_node)
6426 return error_mark_node;
6427 // This can panic if the type is uncomparable.
6428 TREE_NOTHROW(interface_compare_decl) = 0;
6432 if (left_type->interface_type()->is_empty())
6434 gcc_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6435 std::swap(left_type, right_type);
6436 std::swap(left_tree, right_tree);
6438 gcc_assert(!left_type->interface_type()->is_empty());
6439 gcc_assert(right_type->interface_type()->is_empty());
6440 static tree interface_empty_compare_decl;
6441 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
6443 "__go_interface_empty_compare",
6446 TREE_TYPE(left_tree),
6448 TREE_TYPE(right_tree),
6450 if (left_tree == error_mark_node)
6451 return error_mark_node;
6452 // This can panic if the type is uncomparable.
6453 TREE_NOTHROW(interface_empty_compare_decl) = 0;
6456 right_tree = build_int_cst_type(integer_type_node, 0);
6459 if (left_type->is_nil_type()
6460 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6462 std::swap(left_type, right_type);
6463 std::swap(left_tree, right_tree);
6466 if (right_type->is_nil_type())
6468 if (left_type->array_type() != NULL
6469 && left_type->array_type()->length() == NULL)
6471 Array_type* at = left_type->array_type();
6472 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6473 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6475 else if (left_type->interface_type() != NULL)
6477 // An interface is nil if the first field is nil.
6478 tree left_type_tree = TREE_TYPE(left_tree);
6479 gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6480 tree field = TYPE_FIELDS(left_type_tree);
6481 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6483 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6487 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6488 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6492 if (left_tree == error_mark_node || right_tree == error_mark_node)
6493 return error_mark_node;
6495 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6496 if (CAN_HAVE_LOCATION_P(ret))
6497 SET_EXPR_LOCATION(ret, location);
6501 // Class Bound_method_expression.
6506 Bound_method_expression::do_traverse(Traverse* traverse)
6508 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
6509 return TRAVERSE_EXIT;
6510 return Expression::traverse(&this->method_, traverse);
6513 // Return the type of a bound method expression. The type of this
6514 // object is really the type of the method with no receiver. We
6515 // should be able to get away with just returning the type of the
6519 Bound_method_expression::do_type()
6521 return this->method_->type();
6524 // Determine the types of a method expression.
6527 Bound_method_expression::do_determine_type(const Type_context*)
6529 this->method_->determine_type_no_context();
6530 Type* mtype = this->method_->type();
6531 Function_type* fntype = mtype == NULL ? NULL : mtype->function_type();
6532 if (fntype == NULL || !fntype->is_method())
6533 this->expr_->determine_type_no_context();
6536 Type_context subcontext(fntype->receiver()->type(), false);
6537 this->expr_->determine_type(&subcontext);
6541 // Check the types of a method expression.
6544 Bound_method_expression::do_check_types(Gogo*)
6546 Type* type = this->method_->type()->deref();
6548 || type->function_type() == NULL
6549 || !type->function_type()->is_method())
6550 this->report_error(_("object is not a method"));
6553 Type* rtype = type->function_type()->receiver()->type()->deref();
6554 Type* etype = (this->expr_type_ != NULL
6556 : this->expr_->type());
6557 etype = etype->deref();
6558 if (!Type::are_identical(rtype, etype, true, NULL))
6559 this->report_error(_("method type does not match object type"));
6563 // Get the tree for a method expression. There is no standard tree
6564 // representation for this. The only places it may currently be used
6565 // are in a Call_expression or a Go_statement, which will take it
6566 // apart directly. So this has nothing to do at present.
6569 Bound_method_expression::do_get_tree(Translate_context*)
6571 error_at(this->location(), "reference to method other than calling it");
6572 return error_mark_node;
6575 // Make a method expression.
6577 Bound_method_expression*
6578 Expression::make_bound_method(Expression* expr, Expression* method,
6579 source_location location)
6581 return new Bound_method_expression(expr, method, location);
6584 // Class Builtin_call_expression. This is used for a call to a
6585 // builtin function.
6587 class Builtin_call_expression : public Call_expression
6590 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6591 bool is_varargs, source_location location);
6594 // This overrides Call_expression::do_lower.
6596 do_lower(Gogo*, Named_object*, int);
6599 do_is_constant() const;
6602 do_integer_constant_value(bool, mpz_t, Type**) const;
6605 do_float_constant_value(mpfr_t, Type**) const;
6608 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6614 do_determine_type(const Type_context*);
6617 do_check_types(Gogo*);
6622 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6623 this->args()->copy(),
6629 do_get_tree(Translate_context*);
6632 do_export(Export*) const;
6635 do_is_recover_call() const;
6638 do_set_recover_arg(Expression*);
6641 // The builtin functions.
6642 enum Builtin_function_code
6646 // Predeclared builtin functions.
6662 // Builtin functions from the unsafe package.
6675 real_imag_type(Type*);
6678 complex_type(Type*);
6680 // A pointer back to the general IR structure. This avoids a global
6681 // variable, or passing it around everywhere.
6683 // The builtin function being called.
6684 Builtin_function_code code_;
6685 // Used to stop endless loops when the length of an array uses len
6686 // or cap of the array itself.
6690 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6692 Expression_list* args,
6694 source_location location)
6695 : Call_expression(fn, args, is_varargs, location),
6696 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
6698 Func_expression* fnexp = this->fn()->func_expression();
6699 gcc_assert(fnexp != NULL);
6700 const std::string& name(fnexp->named_object()->name());
6701 if (name == "append")
6702 this->code_ = BUILTIN_APPEND;
6703 else if (name == "cap")
6704 this->code_ = BUILTIN_CAP;
6705 else if (name == "close")
6706 this->code_ = BUILTIN_CLOSE;
6707 else if (name == "complex")
6708 this->code_ = BUILTIN_COMPLEX;
6709 else if (name == "copy")
6710 this->code_ = BUILTIN_COPY;
6711 else if (name == "imag")
6712 this->code_ = BUILTIN_IMAG;
6713 else if (name == "len")
6714 this->code_ = BUILTIN_LEN;
6715 else if (name == "make")
6716 this->code_ = BUILTIN_MAKE;
6717 else if (name == "new")
6718 this->code_ = BUILTIN_NEW;
6719 else if (name == "panic")
6720 this->code_ = BUILTIN_PANIC;
6721 else if (name == "print")
6722 this->code_ = BUILTIN_PRINT;
6723 else if (name == "println")
6724 this->code_ = BUILTIN_PRINTLN;
6725 else if (name == "real")
6726 this->code_ = BUILTIN_REAL;
6727 else if (name == "recover")
6728 this->code_ = BUILTIN_RECOVER;
6729 else if (name == "Alignof")
6730 this->code_ = BUILTIN_ALIGNOF;
6731 else if (name == "Offsetof")
6732 this->code_ = BUILTIN_OFFSETOF;
6733 else if (name == "Sizeof")
6734 this->code_ = BUILTIN_SIZEOF;
6739 // Return whether this is a call to recover. This is a virtual
6740 // function called from the parent class.
6743 Builtin_call_expression::do_is_recover_call() const
6745 if (this->classification() == EXPRESSION_ERROR)
6747 return this->code_ == BUILTIN_RECOVER;
6750 // Set the argument for a call to recover.
6753 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6755 const Expression_list* args = this->args();
6756 gcc_assert(args == NULL || args->empty());
6757 Expression_list* new_args = new Expression_list();
6758 new_args->push_back(arg);
6759 this->set_args(new_args);
6762 // A traversal class which looks for a call expression.
6764 class Find_call_expression : public Traverse
6767 Find_call_expression()
6768 : Traverse(traverse_expressions),
6773 expression(Expression**);
6777 { return this->found_; }
6784 Find_call_expression::expression(Expression** pexpr)
6786 if ((*pexpr)->call_expression() != NULL)
6788 this->found_ = true;
6789 return TRAVERSE_EXIT;
6791 return TRAVERSE_CONTINUE;
6794 // Lower a builtin call expression. This turns new and make into
6795 // specific expressions. We also convert to a constant if we can.
6798 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int)
6800 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
6802 this->report_error(_("invalid use of %<...%> with builtin function"));
6803 return Expression::make_error(this->location());
6806 if (this->code_ == BUILTIN_NEW)
6808 const Expression_list* args = this->args();
6809 if (args == NULL || args->size() < 1)
6810 this->report_error(_("not enough arguments"));
6811 else if (args->size() > 1)
6812 this->report_error(_("too many arguments"));
6815 Expression* arg = args->front();
6816 if (!arg->is_type_expression())
6818 error_at(arg->location(), "expected type");
6819 this->set_is_error();
6822 return Expression::make_allocation(arg->type(), this->location());
6825 else if (this->code_ == BUILTIN_MAKE)
6827 const Expression_list* args = this->args();
6828 if (args == NULL || args->size() < 1)
6829 this->report_error(_("not enough arguments"));
6832 Expression* arg = args->front();
6833 if (!arg->is_type_expression())
6835 error_at(arg->location(), "expected type");
6836 this->set_is_error();
6840 Expression_list* newargs;
6841 if (args->size() == 1)
6845 newargs = new Expression_list();
6846 Expression_list::const_iterator p = args->begin();
6848 for (; p != args->end(); ++p)
6849 newargs->push_back(*p);
6851 return Expression::make_make(arg->type(), newargs,
6856 else if (this->is_constant())
6858 // We can only lower len and cap if there are no function calls
6859 // in the arguments. Otherwise we have to make the call.
6860 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6862 Expression* arg = this->one_arg();
6863 if (!arg->is_constant())
6865 Find_call_expression find_call;
6866 Expression::traverse(&arg, &find_call);
6867 if (find_call.found())
6875 if (this->integer_constant_value(true, ival, &type))
6877 Expression* ret = Expression::make_integer(&ival, type,
6886 if (this->float_constant_value(rval, &type))
6888 Expression* ret = Expression::make_float(&rval, type,
6896 if (this->complex_constant_value(rval, imag, &type))
6898 Expression* ret = Expression::make_complex(&rval, &imag, type,
6907 else if (this->code_ == BUILTIN_RECOVER)
6909 if (function != NULL)
6910 function->func_value()->set_calls_recover();
6913 // Calling recover outside of a function always returns the
6914 // nil empty interface.
6915 Type* eface = Type::make_interface_type(NULL, this->location());
6916 return Expression::make_cast(eface,
6917 Expression::make_nil(this->location()),
6921 else if (this->code_ == BUILTIN_APPEND)
6923 // Lower the varargs.
6924 const Expression_list* args = this->args();
6925 if (args == NULL || args->empty())
6927 Type* slice_type = args->front()->type();
6928 if (!slice_type->is_open_array_type())
6930 error_at(args->front()->location(), "argument 1 must be a slice");
6931 this->set_is_error();
6934 return this->lower_varargs(gogo, function, slice_type, 2);
6940 // Return the type of the real or imag functions, given the type of
6941 // the argument. We need to map complex to float, complex64 to
6942 // float32, and complex128 to float64, so it has to be done by name.
6943 // This returns NULL if it can't figure out the type.
6946 Builtin_call_expression::real_imag_type(Type* arg_type)
6948 if (arg_type == NULL || arg_type->is_abstract())
6950 Named_type* nt = arg_type->named_type();
6953 while (nt->real_type()->named_type() != NULL)
6954 nt = nt->real_type()->named_type();
6955 if (nt->name() == "complex64")
6956 return Type::lookup_float_type("float32");
6957 else if (nt->name() == "complex128")
6958 return Type::lookup_float_type("float64");
6963 // Return the type of the complex function, given the type of one of the
6964 // argments. Like real_imag_type, we have to map by name.
6967 Builtin_call_expression::complex_type(Type* arg_type)
6969 if (arg_type == NULL || arg_type->is_abstract())
6971 Named_type* nt = arg_type->named_type();
6974 while (nt->real_type()->named_type() != NULL)
6975 nt = nt->real_type()->named_type();
6976 if (nt->name() == "float32")
6977 return Type::lookup_complex_type("complex64");
6978 else if (nt->name() == "float64")
6979 return Type::lookup_complex_type("complex128");
6984 // Return a single argument, or NULL if there isn't one.
6987 Builtin_call_expression::one_arg() const
6989 const Expression_list* args = this->args();
6990 if (args->size() != 1)
6992 return args->front();
6995 // Return whether this is constant: len of a string, or len or cap of
6996 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6999 Builtin_call_expression::do_is_constant() const
7001 switch (this->code_)
7009 Expression* arg = this->one_arg();
7012 Type* arg_type = arg->type();
7014 if (arg_type->points_to() != NULL
7015 && arg_type->points_to()->array_type() != NULL
7016 && !arg_type->points_to()->is_open_array_type())
7017 arg_type = arg_type->points_to();
7019 if (arg_type->array_type() != NULL
7020 && arg_type->array_type()->length() != NULL)
7023 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7026 bool ret = arg->is_constant();
7027 this->seen_ = false;
7033 case BUILTIN_SIZEOF:
7034 case BUILTIN_ALIGNOF:
7035 return this->one_arg() != NULL;
7037 case BUILTIN_OFFSETOF:
7039 Expression* arg = this->one_arg();
7042 return arg->field_reference_expression() != NULL;
7045 case BUILTIN_COMPLEX:
7047 const Expression_list* args = this->args();
7048 if (args != NULL && args->size() == 2)
7049 return args->front()->is_constant() && args->back()->is_constant();
7056 Expression* arg = this->one_arg();
7057 return arg != NULL && arg->is_constant();
7067 // Return an integer constant value if possible.
7070 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
7074 if (this->code_ == BUILTIN_LEN
7075 || this->code_ == BUILTIN_CAP)
7077 Expression* arg = this->one_arg();
7080 Type* arg_type = arg->type();
7082 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7085 if (arg->string_constant_value(&sval))
7087 mpz_set_ui(val, sval.length());
7088 *ptype = Type::lookup_integer_type("int");
7093 if (arg_type->points_to() != NULL
7094 && arg_type->points_to()->array_type() != NULL
7095 && !arg_type->points_to()->is_open_array_type())
7096 arg_type = arg_type->points_to();
7098 if (arg_type->array_type() != NULL
7099 && arg_type->array_type()->length() != NULL)
7103 Expression* e = arg_type->array_type()->length();
7105 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
7106 this->seen_ = false;
7109 *ptype = Type::lookup_integer_type("int");
7114 else if (this->code_ == BUILTIN_SIZEOF
7115 || this->code_ == BUILTIN_ALIGNOF)
7117 Expression* arg = this->one_arg();
7120 Type* arg_type = arg->type();
7121 if (arg_type->is_error())
7123 if (arg_type->is_abstract())
7125 if (arg_type->named_type() != NULL)
7126 arg_type->named_type()->convert(this->gogo_);
7127 tree arg_type_tree = arg_type->get_tree(this->gogo_);
7128 if (arg_type_tree == error_mark_node)
7130 unsigned long val_long;
7131 if (this->code_ == BUILTIN_SIZEOF)
7133 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
7134 gcc_assert(TREE_CODE(type_size) == INTEGER_CST);
7135 if (TREE_INT_CST_HIGH(type_size) != 0)
7137 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
7138 val_long = static_cast<unsigned long>(val_wide);
7139 if (val_long != val_wide)
7142 else if (this->code_ == BUILTIN_ALIGNOF)
7144 if (arg->field_reference_expression() == NULL)
7145 val_long = go_type_alignment(arg_type_tree);
7148 // Calling unsafe.Alignof(s.f) returns the alignment of
7149 // the type of f when it is used as a field in a struct.
7150 val_long = go_field_alignment(arg_type_tree);
7155 mpz_set_ui(val, val_long);
7159 else if (this->code_ == BUILTIN_OFFSETOF)
7161 Expression* arg = this->one_arg();
7164 Field_reference_expression* farg = arg->field_reference_expression();
7167 Expression* struct_expr = farg->expr();
7168 Type* st = struct_expr->type();
7169 if (st->struct_type() == NULL)
7171 if (st->named_type() != NULL)
7172 st->named_type()->convert(this->gogo_);
7173 tree struct_tree = st->get_tree(this->gogo_);
7174 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
7175 tree field = TYPE_FIELDS(struct_tree);
7176 for (unsigned int index = farg->field_index(); index > 0; --index)
7178 field = DECL_CHAIN(field);
7179 gcc_assert(field != NULL_TREE);
7181 HOST_WIDE_INT offset_wide = int_byte_position (field);
7182 if (offset_wide < 0)
7184 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
7185 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
7187 mpz_set_ui(val, offset_long);
7193 // Return a floating point constant value if possible.
7196 Builtin_call_expression::do_float_constant_value(mpfr_t val,
7199 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
7201 Expression* arg = this->one_arg();
7212 if (arg->complex_constant_value(real, imag, &type))
7214 if (this->code_ == BUILTIN_REAL)
7215 mpfr_set(val, real, GMP_RNDN);
7217 mpfr_set(val, imag, GMP_RNDN);
7218 *ptype = Builtin_call_expression::real_imag_type(type);
7230 // Return a complex constant value if possible.
7233 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
7236 if (this->code_ == BUILTIN_COMPLEX)
7238 const Expression_list* args = this->args();
7239 if (args == NULL || args->size() != 2)
7245 if (!args->front()->float_constant_value(r, &rtype))
7256 if (args->back()->float_constant_value(i, &itype)
7257 && Type::are_identical(rtype, itype, false, NULL))
7259 mpfr_set(real, r, GMP_RNDN);
7260 mpfr_set(imag, i, GMP_RNDN);
7261 *ptype = Builtin_call_expression::complex_type(rtype);
7277 Builtin_call_expression::do_type()
7279 switch (this->code_)
7281 case BUILTIN_INVALID:
7288 const Expression_list* args = this->args();
7289 if (args == NULL || args->empty())
7290 return Type::make_error_type();
7291 return Type::make_pointer_type(args->front()->type());
7297 case BUILTIN_ALIGNOF:
7298 case BUILTIN_OFFSETOF:
7299 case BUILTIN_SIZEOF:
7300 return Type::lookup_integer_type("int");
7305 case BUILTIN_PRINTLN:
7306 return Type::make_void_type();
7308 case BUILTIN_RECOVER:
7309 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
7311 case BUILTIN_APPEND:
7313 const Expression_list* args = this->args();
7314 if (args == NULL || args->empty())
7315 return Type::make_error_type();
7316 return args->front()->type();
7322 Expression* arg = this->one_arg();
7324 return Type::make_error_type();
7325 Type* t = arg->type();
7326 if (t->is_abstract())
7327 t = t->make_non_abstract_type();
7328 t = Builtin_call_expression::real_imag_type(t);
7330 t = Type::make_error_type();
7334 case BUILTIN_COMPLEX:
7336 const Expression_list* args = this->args();
7337 if (args == NULL || args->size() != 2)
7338 return Type::make_error_type();
7339 Type* t = args->front()->type();
7340 if (t->is_abstract())
7342 t = args->back()->type();
7343 if (t->is_abstract())
7344 t = t->make_non_abstract_type();
7346 t = Builtin_call_expression::complex_type(t);
7348 t = Type::make_error_type();
7354 // Determine the type.
7357 Builtin_call_expression::do_determine_type(const Type_context* context)
7359 if (!this->determining_types())
7362 this->fn()->determine_type_no_context();
7364 const Expression_list* args = this->args();
7367 Type* arg_type = NULL;
7368 switch (this->code_)
7371 case BUILTIN_PRINTLN:
7372 // Do not force a large integer constant to "int".
7378 arg_type = Builtin_call_expression::complex_type(context->type);
7382 case BUILTIN_COMPLEX:
7384 // For the complex function the type of one operand can
7385 // determine the type of the other, as in a binary expression.
7386 arg_type = Builtin_call_expression::real_imag_type(context->type);
7387 if (args != NULL && args->size() == 2)
7389 Type* t1 = args->front()->type();
7390 Type* t2 = args->front()->type();
7391 if (!t1->is_abstract())
7393 else if (!t2->is_abstract())
7407 for (Expression_list::const_iterator pa = args->begin();
7411 Type_context subcontext;
7412 subcontext.type = arg_type;
7416 // We want to print large constants, we so can't just
7417 // use the appropriate nonabstract type. Use uint64 for
7418 // an integer if we know it is nonnegative, otherwise
7419 // use int64 for a integer, otherwise use float64 for a
7420 // float or complex128 for a complex.
7421 Type* want_type = NULL;
7422 Type* atype = (*pa)->type();
7423 if (atype->is_abstract())
7425 if (atype->integer_type() != NULL)
7430 if (this->integer_constant_value(true, val, &dummy)
7431 && mpz_sgn(val) >= 0)
7432 want_type = Type::lookup_integer_type("uint64");
7434 want_type = Type::lookup_integer_type("int64");
7437 else if (atype->float_type() != NULL)
7438 want_type = Type::lookup_float_type("float64");
7439 else if (atype->complex_type() != NULL)
7440 want_type = Type::lookup_complex_type("complex128");
7441 else if (atype->is_abstract_string_type())
7442 want_type = Type::lookup_string_type();
7443 else if (atype->is_abstract_boolean_type())
7444 want_type = Type::lookup_bool_type();
7447 subcontext.type = want_type;
7451 (*pa)->determine_type(&subcontext);
7456 // If there is exactly one argument, return true. Otherwise give an
7457 // error message and return false.
7460 Builtin_call_expression::check_one_arg()
7462 const Expression_list* args = this->args();
7463 if (args == NULL || args->size() < 1)
7465 this->report_error(_("not enough arguments"));
7468 else if (args->size() > 1)
7470 this->report_error(_("too many arguments"));
7473 if (args->front()->is_error_expression()
7474 || args->front()->type()->is_error())
7476 this->set_is_error();
7482 // Check argument types for a builtin function.
7485 Builtin_call_expression::do_check_types(Gogo*)
7487 switch (this->code_)
7489 case BUILTIN_INVALID:
7497 // The single argument may be either a string or an array or a
7498 // map or a channel, or a pointer to a closed array.
7499 if (this->check_one_arg())
7501 Type* arg_type = this->one_arg()->type();
7502 if (arg_type->points_to() != NULL
7503 && arg_type->points_to()->array_type() != NULL
7504 && !arg_type->points_to()->is_open_array_type())
7505 arg_type = arg_type->points_to();
7506 if (this->code_ == BUILTIN_CAP)
7508 if (!arg_type->is_error()
7509 && arg_type->array_type() == NULL
7510 && arg_type->channel_type() == NULL)
7511 this->report_error(_("argument must be array or slice "
7516 if (!arg_type->is_error()
7517 && !arg_type->is_string_type()
7518 && arg_type->array_type() == NULL
7519 && arg_type->map_type() == NULL
7520 && arg_type->channel_type() == NULL)
7521 this->report_error(_("argument must be string or "
7522 "array or slice or map or channel"));
7529 case BUILTIN_PRINTLN:
7531 const Expression_list* args = this->args();
7534 if (this->code_ == BUILTIN_PRINT)
7535 warning_at(this->location(), 0,
7536 "no arguments for builtin function %<%s%>",
7537 (this->code_ == BUILTIN_PRINT
7543 for (Expression_list::const_iterator p = args->begin();
7547 Type* type = (*p)->type();
7548 if (type->is_error()
7549 || type->is_string_type()
7550 || type->integer_type() != NULL
7551 || type->float_type() != NULL
7552 || type->complex_type() != NULL
7553 || type->is_boolean_type()
7554 || type->points_to() != NULL
7555 || type->interface_type() != NULL
7556 || type->channel_type() != NULL
7557 || type->map_type() != NULL
7558 || type->function_type() != NULL
7559 || type->is_open_array_type())
7562 this->report_error(_("unsupported argument type to "
7563 "builtin function"));
7570 if (this->check_one_arg())
7572 if (this->one_arg()->type()->channel_type() == NULL)
7573 this->report_error(_("argument must be channel"));
7578 case BUILTIN_SIZEOF:
7579 case BUILTIN_ALIGNOF:
7580 this->check_one_arg();
7583 case BUILTIN_RECOVER:
7584 if (this->args() != NULL && !this->args()->empty())
7585 this->report_error(_("too many arguments"));
7588 case BUILTIN_OFFSETOF:
7589 if (this->check_one_arg())
7591 Expression* arg = this->one_arg();
7592 if (arg->field_reference_expression() == NULL)
7593 this->report_error(_("argument must be a field reference"));
7599 const Expression_list* args = this->args();
7600 if (args == NULL || args->size() < 2)
7602 this->report_error(_("not enough arguments"));
7605 else if (args->size() > 2)
7607 this->report_error(_("too many arguments"));
7610 Type* arg1_type = args->front()->type();
7611 Type* arg2_type = args->back()->type();
7612 if (arg1_type->is_error() || arg2_type->is_error())
7616 if (arg1_type->is_open_array_type())
7617 e1 = arg1_type->array_type()->element_type();
7620 this->report_error(_("left argument must be a slice"));
7625 if (arg2_type->is_open_array_type())
7626 e2 = arg2_type->array_type()->element_type();
7627 else if (arg2_type->is_string_type())
7628 e2 = Type::lookup_integer_type("uint8");
7631 this->report_error(_("right argument must be a slice or a string"));
7635 if (!Type::are_identical(e1, e2, true, NULL))
7636 this->report_error(_("element types must be the same"));
7640 case BUILTIN_APPEND:
7642 const Expression_list* args = this->args();
7643 if (args == NULL || args->size() < 2)
7645 this->report_error(_("not enough arguments"));
7648 if (args->size() > 2)
7650 this->report_error(_("too many arguments"));
7654 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
7658 this->report_error(_("arguments 1 and 2 have different types"));
7661 error_at(this->location(),
7662 "arguments 1 and 2 have different types (%s)",
7664 this->set_is_error();
7672 if (this->check_one_arg())
7674 if (this->one_arg()->type()->complex_type() == NULL)
7675 this->report_error(_("argument must have complex type"));
7679 case BUILTIN_COMPLEX:
7681 const Expression_list* args = this->args();
7682 if (args == NULL || args->size() < 2)
7683 this->report_error(_("not enough arguments"));
7684 else if (args->size() > 2)
7685 this->report_error(_("too many arguments"));
7686 else if (args->front()->is_error_expression()
7687 || args->front()->type()->is_error()
7688 || args->back()->is_error_expression()
7689 || args->back()->type()->is_error())
7690 this->set_is_error();
7691 else if (!Type::are_identical(args->front()->type(),
7692 args->back()->type(), true, NULL))
7693 this->report_error(_("complex arguments must have identical types"));
7694 else if (args->front()->type()->float_type() == NULL)
7695 this->report_error(_("complex arguments must have "
7696 "floating-point type"));
7705 // Return the tree for a builtin function.
7708 Builtin_call_expression::do_get_tree(Translate_context* context)
7710 Gogo* gogo = context->gogo();
7711 source_location location = this->location();
7712 switch (this->code_)
7714 case BUILTIN_INVALID:
7722 const Expression_list* args = this->args();
7723 gcc_assert(args != NULL && args->size() == 1);
7724 Expression* arg = *args->begin();
7725 Type* arg_type = arg->type();
7729 gcc_assert(saw_errors());
7730 return error_mark_node;
7734 tree arg_tree = arg->get_tree(context);
7736 this->seen_ = false;
7738 if (arg_tree == error_mark_node)
7739 return error_mark_node;
7741 if (arg_type->points_to() != NULL)
7743 arg_type = arg_type->points_to();
7744 gcc_assert(arg_type->array_type() != NULL
7745 && !arg_type->is_open_array_type());
7746 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7747 arg_tree = build_fold_indirect_ref(arg_tree);
7751 if (this->code_ == BUILTIN_LEN)
7753 if (arg_type->is_string_type())
7754 val_tree = String_type::length_tree(gogo, arg_tree);
7755 else if (arg_type->array_type() != NULL)
7759 gcc_assert(saw_errors());
7760 return error_mark_node;
7763 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7764 this->seen_ = false;
7766 else if (arg_type->map_type() != NULL)
7768 static tree map_len_fndecl;
7769 val_tree = Gogo::call_builtin(&map_len_fndecl,
7774 arg_type->get_tree(gogo),
7777 else if (arg_type->channel_type() != NULL)
7779 static tree chan_len_fndecl;
7780 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7785 arg_type->get_tree(gogo),
7793 if (arg_type->array_type() != NULL)
7797 gcc_assert(saw_errors());
7798 return error_mark_node;
7801 val_tree = arg_type->array_type()->capacity_tree(gogo,
7803 this->seen_ = false;
7805 else if (arg_type->channel_type() != NULL)
7807 static tree chan_cap_fndecl;
7808 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7813 arg_type->get_tree(gogo),
7820 if (val_tree == error_mark_node)
7821 return error_mark_node;
7823 tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo);
7824 if (type_tree == TREE_TYPE(val_tree))
7827 return fold(convert_to_integer(type_tree, val_tree));
7831 case BUILTIN_PRINTLN:
7833 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7834 tree stmt_list = NULL_TREE;
7836 const Expression_list* call_args = this->args();
7837 if (call_args != NULL)
7839 for (Expression_list::const_iterator p = call_args->begin();
7840 p != call_args->end();
7843 if (is_ln && p != call_args->begin())
7845 static tree print_space_fndecl;
7846 tree call = Gogo::call_builtin(&print_space_fndecl,
7851 if (call == error_mark_node)
7852 return error_mark_node;
7853 append_to_statement_list(call, &stmt_list);
7856 Type* type = (*p)->type();
7858 tree arg = (*p)->get_tree(context);
7859 if (arg == error_mark_node)
7860 return error_mark_node;
7864 if (type->is_string_type())
7866 static tree print_string_fndecl;
7867 pfndecl = &print_string_fndecl;
7868 fnname = "__go_print_string";
7870 else if (type->integer_type() != NULL
7871 && type->integer_type()->is_unsigned())
7873 static tree print_uint64_fndecl;
7874 pfndecl = &print_uint64_fndecl;
7875 fnname = "__go_print_uint64";
7876 Type* itype = Type::lookup_integer_type("uint64");
7877 arg = fold_convert_loc(location, itype->get_tree(gogo),
7880 else if (type->integer_type() != NULL)
7882 static tree print_int64_fndecl;
7883 pfndecl = &print_int64_fndecl;
7884 fnname = "__go_print_int64";
7885 Type* itype = Type::lookup_integer_type("int64");
7886 arg = fold_convert_loc(location, itype->get_tree(gogo),
7889 else if (type->float_type() != NULL)
7891 static tree print_double_fndecl;
7892 pfndecl = &print_double_fndecl;
7893 fnname = "__go_print_double";
7894 arg = fold_convert_loc(location, double_type_node, arg);
7896 else if (type->complex_type() != NULL)
7898 static tree print_complex_fndecl;
7899 pfndecl = &print_complex_fndecl;
7900 fnname = "__go_print_complex";
7901 arg = fold_convert_loc(location, complex_double_type_node,
7904 else if (type->is_boolean_type())
7906 static tree print_bool_fndecl;
7907 pfndecl = &print_bool_fndecl;
7908 fnname = "__go_print_bool";
7910 else if (type->points_to() != NULL
7911 || type->channel_type() != NULL
7912 || type->map_type() != NULL
7913 || type->function_type() != NULL)
7915 static tree print_pointer_fndecl;
7916 pfndecl = &print_pointer_fndecl;
7917 fnname = "__go_print_pointer";
7918 arg = fold_convert_loc(location, ptr_type_node, arg);
7920 else if (type->interface_type() != NULL)
7922 if (type->interface_type()->is_empty())
7924 static tree print_empty_interface_fndecl;
7925 pfndecl = &print_empty_interface_fndecl;
7926 fnname = "__go_print_empty_interface";
7930 static tree print_interface_fndecl;
7931 pfndecl = &print_interface_fndecl;
7932 fnname = "__go_print_interface";
7935 else if (type->is_open_array_type())
7937 static tree print_slice_fndecl;
7938 pfndecl = &print_slice_fndecl;
7939 fnname = "__go_print_slice";
7944 tree call = Gogo::call_builtin(pfndecl,
7951 if (call == error_mark_node)
7952 return error_mark_node;
7953 append_to_statement_list(call, &stmt_list);
7959 static tree print_nl_fndecl;
7960 tree call = Gogo::call_builtin(&print_nl_fndecl,
7965 if (call == error_mark_node)
7966 return error_mark_node;
7967 append_to_statement_list(call, &stmt_list);
7975 const Expression_list* args = this->args();
7976 gcc_assert(args != NULL && args->size() == 1);
7977 Expression* arg = args->front();
7978 tree arg_tree = arg->get_tree(context);
7979 if (arg_tree == error_mark_node)
7980 return error_mark_node;
7981 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7982 arg_tree = Expression::convert_for_assignment(context, empty,
7984 arg_tree, location);
7985 static tree panic_fndecl;
7986 tree call = Gogo::call_builtin(&panic_fndecl,
7991 TREE_TYPE(arg_tree),
7993 if (call == error_mark_node)
7994 return error_mark_node;
7995 // This function will throw an exception.
7996 TREE_NOTHROW(panic_fndecl) = 0;
7997 // This function will not return.
7998 TREE_THIS_VOLATILE(panic_fndecl) = 1;
8002 case BUILTIN_RECOVER:
8004 // The argument is set when building recover thunks. It's a
8005 // boolean value which is true if we can recover a value now.
8006 const Expression_list* args = this->args();
8007 gcc_assert(args != NULL && args->size() == 1);
8008 Expression* arg = args->front();
8009 tree arg_tree = arg->get_tree(context);
8010 if (arg_tree == error_mark_node)
8011 return error_mark_node;
8013 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
8014 tree empty_tree = empty->get_tree(context->gogo());
8016 Type* nil_type = Type::make_nil_type();
8017 Expression* nil = Expression::make_nil(location);
8018 tree nil_tree = nil->get_tree(context);
8019 tree empty_nil_tree = Expression::convert_for_assignment(context,
8025 // We need to handle a deferred call to recover specially,
8026 // because it changes whether it can recover a panic or not.
8027 // See test7 in test/recover1.go.
8029 if (this->is_deferred())
8031 static tree deferred_recover_fndecl;
8032 call = Gogo::call_builtin(&deferred_recover_fndecl,
8034 "__go_deferred_recover",
8040 static tree recover_fndecl;
8041 call = Gogo::call_builtin(&recover_fndecl,
8047 if (call == error_mark_node)
8048 return error_mark_node;
8049 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
8050 call, empty_nil_tree);
8055 const Expression_list* args = this->args();
8056 gcc_assert(args != NULL && args->size() == 1);
8057 Expression* arg = args->front();
8058 tree arg_tree = arg->get_tree(context);
8059 if (arg_tree == error_mark_node)
8060 return error_mark_node;
8061 static tree close_fndecl;
8062 return Gogo::call_builtin(&close_fndecl,
8064 "__go_builtin_close",
8067 TREE_TYPE(arg_tree),
8071 case BUILTIN_SIZEOF:
8072 case BUILTIN_OFFSETOF:
8073 case BUILTIN_ALIGNOF:
8078 bool b = this->integer_constant_value(true, val, &dummy);
8081 gcc_assert(saw_errors());
8082 return error_mark_node;
8084 tree type = Type::lookup_integer_type("int")->get_tree(gogo);
8085 tree ret = Expression::integer_constant_tree(val, type);
8092 const Expression_list* args = this->args();
8093 gcc_assert(args != NULL && args->size() == 2);
8094 Expression* arg1 = args->front();
8095 Expression* arg2 = args->back();
8097 tree arg1_tree = arg1->get_tree(context);
8098 tree arg2_tree = arg2->get_tree(context);
8099 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8100 return error_mark_node;
8102 Type* arg1_type = arg1->type();
8103 Array_type* at = arg1_type->array_type();
8104 arg1_tree = save_expr(arg1_tree);
8105 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
8106 tree arg1_len = at->length_tree(gogo, arg1_tree);
8107 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
8108 return error_mark_node;
8110 Type* arg2_type = arg2->type();
8113 if (arg2_type->is_open_array_type())
8115 at = arg2_type->array_type();
8116 arg2_tree = save_expr(arg2_tree);
8117 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8118 arg2_len = at->length_tree(gogo, arg2_tree);
8122 arg2_tree = save_expr(arg2_tree);
8123 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8124 arg2_len = String_type::length_tree(gogo, arg2_tree);
8126 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8127 return error_mark_node;
8129 arg1_len = save_expr(arg1_len);
8130 arg2_len = save_expr(arg2_len);
8131 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
8132 fold_build2_loc(location, LT_EXPR,
8134 arg1_len, arg2_len),
8135 arg1_len, arg2_len);
8136 len = save_expr(len);
8138 Type* element_type = at->element_type();
8139 tree element_type_tree = element_type->get_tree(gogo);
8140 if (element_type_tree == error_mark_node)
8141 return error_mark_node;
8142 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8143 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
8145 bytecount = fold_build2_loc(location, MULT_EXPR,
8146 TREE_TYPE(element_size),
8147 bytecount, element_size);
8148 bytecount = fold_convert_loc(location, size_type_node, bytecount);
8150 arg1_val = fold_convert_loc(location, ptr_type_node, arg1_val);
8151 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8153 static tree copy_fndecl;
8154 tree call = Gogo::call_builtin(©_fndecl,
8165 if (call == error_mark_node)
8166 return error_mark_node;
8168 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
8172 case BUILTIN_APPEND:
8174 const Expression_list* args = this->args();
8175 gcc_assert(args != NULL && args->size() == 2);
8176 Expression* arg1 = args->front();
8177 Expression* arg2 = args->back();
8179 tree arg1_tree = arg1->get_tree(context);
8180 tree arg2_tree = arg2->get_tree(context);
8181 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8182 return error_mark_node;
8184 Array_type* at = arg1->type()->array_type();
8185 Type* element_type = at->element_type();
8187 arg2_tree = Expression::convert_for_assignment(context, at,
8191 if (arg2_tree == error_mark_node)
8192 return error_mark_node;
8194 arg2_tree = save_expr(arg2_tree);
8195 tree arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8196 tree arg2_len = at->length_tree(gogo, arg2_tree);
8197 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8198 return error_mark_node;
8199 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8200 arg2_len = fold_convert_loc(location, size_type_node, arg2_len);
8202 tree element_type_tree = element_type->get_tree(gogo);
8203 if (element_type_tree == error_mark_node)
8204 return error_mark_node;
8205 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8206 element_size = fold_convert_loc(location, size_type_node,
8209 // We rebuild the decl each time since the slice types may
8211 tree append_fndecl = NULL_TREE;
8212 return Gogo::call_builtin(&append_fndecl,
8216 TREE_TYPE(arg1_tree),
8217 TREE_TYPE(arg1_tree),
8230 const Expression_list* args = this->args();
8231 gcc_assert(args != NULL && args->size() == 1);
8232 Expression* arg = args->front();
8233 tree arg_tree = arg->get_tree(context);
8234 if (arg_tree == error_mark_node)
8235 return error_mark_node;
8236 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
8237 if (this->code_ == BUILTIN_REAL)
8238 return fold_build1_loc(location, REALPART_EXPR,
8239 TREE_TYPE(TREE_TYPE(arg_tree)),
8242 return fold_build1_loc(location, IMAGPART_EXPR,
8243 TREE_TYPE(TREE_TYPE(arg_tree)),
8247 case BUILTIN_COMPLEX:
8249 const Expression_list* args = this->args();
8250 gcc_assert(args != NULL && args->size() == 2);
8251 tree r = args->front()->get_tree(context);
8252 tree i = args->back()->get_tree(context);
8253 if (r == error_mark_node || i == error_mark_node)
8254 return error_mark_node;
8255 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
8256 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
8257 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8258 return fold_build2_loc(location, COMPLEX_EXPR,
8259 build_complex_type(TREE_TYPE(r)),
8268 // We have to support exporting a builtin call expression, because
8269 // code can set a constant to the result of a builtin expression.
8272 Builtin_call_expression::do_export(Export* exp) const
8279 if (this->integer_constant_value(true, val, &dummy))
8281 Integer_expression::export_integer(exp, val);
8290 if (this->float_constant_value(fval, &dummy))
8292 Float_expression::export_float(exp, fval);
8304 if (this->complex_constant_value(real, imag, &dummy))
8306 Complex_expression::export_complex(exp, real, imag);
8315 error_at(this->location(), "value is not constant");
8319 // A trailing space lets us reliably identify the end of the number.
8320 exp->write_c_string(" ");
8323 // Class Call_expression.
8328 Call_expression::do_traverse(Traverse* traverse)
8330 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8331 return TRAVERSE_EXIT;
8332 if (this->args_ != NULL)
8334 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8335 return TRAVERSE_EXIT;
8337 return TRAVERSE_CONTINUE;
8340 // Lower a call statement.
8343 Call_expression::do_lower(Gogo* gogo, Named_object* function, int)
8345 // A type case can look like a function call.
8346 if (this->fn_->is_type_expression()
8347 && this->args_ != NULL
8348 && this->args_->size() == 1)
8349 return Expression::make_cast(this->fn_->type(), this->args_->front(),
8352 // Recognize a call to a builtin function.
8353 Func_expression* fne = this->fn_->func_expression();
8355 && fne->named_object()->is_function_declaration()
8356 && fne->named_object()->func_declaration_value()->type()->is_builtin())
8357 return new Builtin_call_expression(gogo, this->fn_, this->args_,
8358 this->is_varargs_, this->location());
8360 // Handle an argument which is a call to a function which returns
8361 // multiple results.
8362 if (this->args_ != NULL
8363 && this->args_->size() == 1
8364 && this->args_->front()->call_expression() != NULL
8365 && this->fn_->type()->function_type() != NULL)
8367 Function_type* fntype = this->fn_->type()->function_type();
8368 size_t rc = this->args_->front()->call_expression()->result_count();
8370 && fntype->parameters() != NULL
8371 && (fntype->parameters()->size() == rc
8372 || (fntype->is_varargs()
8373 && fntype->parameters()->size() - 1 <= rc)))
8375 Call_expression* call = this->args_->front()->call_expression();
8376 Expression_list* args = new Expression_list;
8377 for (size_t i = 0; i < rc; ++i)
8378 args->push_back(Expression::make_call_result(call, i));
8379 // We can't return a new call expression here, because this
8380 // one may be referenced by Call_result expressions. We
8381 // also can't delete the old arguments, because we may still
8382 // traverse them somewhere up the call stack. FIXME.
8387 // Handle a call to a varargs function by packaging up the extra
8389 if (this->fn_->type()->function_type() != NULL
8390 && this->fn_->type()->function_type()->is_varargs())
8392 Function_type* fntype = this->fn_->type()->function_type();
8393 const Typed_identifier_list* parameters = fntype->parameters();
8394 gcc_assert(parameters != NULL && !parameters->empty());
8395 Type* varargs_type = parameters->back().type();
8396 return this->lower_varargs(gogo, function, varargs_type,
8397 parameters->size());
8403 // Lower a call to a varargs function. FUNCTION is the function in
8404 // which the call occurs--it's not the function we are calling.
8405 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8406 // PARAM_COUNT is the number of parameters of the function we are
8407 // calling; the last of these parameters will be the varargs
8411 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
8412 Type* varargs_type, size_t param_count)
8414 if (this->varargs_are_lowered_)
8417 source_location loc = this->location();
8419 gcc_assert(param_count > 0);
8420 gcc_assert(varargs_type->is_open_array_type());
8422 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8423 if (arg_count < param_count - 1)
8425 // Not enough arguments; will be caught in check_types.
8429 Expression_list* old_args = this->args_;
8430 Expression_list* new_args = new Expression_list();
8431 bool push_empty_arg = false;
8432 if (old_args == NULL || old_args->empty())
8434 gcc_assert(param_count == 1);
8435 push_empty_arg = true;
8439 Expression_list::const_iterator pa;
8441 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
8443 if (static_cast<size_t>(i) == param_count)
8445 new_args->push_back(*pa);
8448 // We have reached the varargs parameter.
8450 bool issued_error = false;
8451 if (pa == old_args->end())
8452 push_empty_arg = true;
8453 else if (pa + 1 == old_args->end() && this->is_varargs_)
8454 new_args->push_back(*pa);
8455 else if (this->is_varargs_)
8457 this->report_error(_("too many arguments"));
8462 Type* element_type = varargs_type->array_type()->element_type();
8463 Expression_list* vals = new Expression_list;
8464 for (; pa != old_args->end(); ++pa, ++i)
8466 // Check types here so that we get a better message.
8467 Type* patype = (*pa)->type();
8468 source_location paloc = (*pa)->location();
8469 if (!this->check_argument_type(i, element_type, patype,
8470 paloc, issued_error))
8472 vals->push_back(*pa);
8475 Expression::make_slice_composite_literal(varargs_type, vals, loc);
8476 new_args->push_back(val);
8481 new_args->push_back(Expression::make_nil(loc));
8483 // We can't return a new call expression here, because this one may
8484 // be referenced by Call_result expressions. FIXME.
8485 if (old_args != NULL)
8487 this->args_ = new_args;
8488 this->varargs_are_lowered_ = true;
8490 // Lower all the new subexpressions.
8491 Expression* ret = this;
8492 gogo->lower_expression(function, &ret);
8493 gcc_assert(ret == this);
8497 // Get the function type. Returns NULL if we don't know the type. If
8498 // this returns NULL, and if_ERROR is true, issues an error.
8501 Call_expression::get_function_type() const
8503 return this->fn_->type()->function_type();
8506 // Return the number of values which this call will return.
8509 Call_expression::result_count() const
8511 const Function_type* fntype = this->get_function_type();
8514 if (fntype->results() == NULL)
8516 return fntype->results()->size();
8519 // Return whether this is a call to the predeclared function recover.
8522 Call_expression::is_recover_call() const
8524 return this->do_is_recover_call();
8527 // Set the argument to the recover function.
8530 Call_expression::set_recover_arg(Expression* arg)
8532 this->do_set_recover_arg(arg);
8535 // Virtual functions also implemented by Builtin_call_expression.
8538 Call_expression::do_is_recover_call() const
8544 Call_expression::do_set_recover_arg(Expression*)
8552 Call_expression::do_type()
8554 if (this->type_ != NULL)
8558 Function_type* fntype = this->get_function_type();
8560 return Type::make_error_type();
8562 const Typed_identifier_list* results = fntype->results();
8563 if (results == NULL)
8564 ret = Type::make_void_type();
8565 else if (results->size() == 1)
8566 ret = results->begin()->type();
8568 ret = Type::make_call_multiple_result_type(this);
8575 // Determine types for a call expression. We can use the function
8576 // parameter types to set the types of the arguments.
8579 Call_expression::do_determine_type(const Type_context*)
8581 if (!this->determining_types())
8584 this->fn_->determine_type_no_context();
8585 Function_type* fntype = this->get_function_type();
8586 const Typed_identifier_list* parameters = NULL;
8588 parameters = fntype->parameters();
8589 if (this->args_ != NULL)
8591 Typed_identifier_list::const_iterator pt;
8592 if (parameters != NULL)
8593 pt = parameters->begin();
8594 for (Expression_list::const_iterator pa = this->args_->begin();
8595 pa != this->args_->end();
8598 if (parameters != NULL && pt != parameters->end())
8600 Type_context subcontext(pt->type(), false);
8601 (*pa)->determine_type(&subcontext);
8605 (*pa)->determine_type_no_context();
8610 // Called when determining types for a Call_expression. Return true
8611 // if we should go ahead, false if they have already been determined.
8614 Call_expression::determining_types()
8616 if (this->types_are_determined_)
8620 this->types_are_determined_ = true;
8625 // Check types for parameter I.
8628 Call_expression::check_argument_type(int i, const Type* parameter_type,
8629 const Type* argument_type,
8630 source_location argument_location,
8634 if (!Type::are_assignable(parameter_type, argument_type, &reason))
8639 error_at(argument_location, "argument %d has incompatible type", i);
8641 error_at(argument_location,
8642 "argument %d has incompatible type (%s)",
8645 this->set_is_error();
8654 Call_expression::do_check_types(Gogo*)
8656 Function_type* fntype = this->get_function_type();
8659 if (!this->fn_->type()->is_error())
8660 this->report_error(_("expected function"));
8664 if (fntype->is_method())
8666 // We don't support pointers to methods, so the function has to
8667 // be a bound method expression.
8668 Bound_method_expression* bme = this->fn_->bound_method_expression();
8671 this->report_error(_("method call without object"));
8674 Type* first_arg_type = bme->first_argument()->type();
8675 if (first_arg_type->points_to() == NULL)
8677 // When passing a value, we need to check that we are
8678 // permitted to copy it. The language permits copying
8679 // hidden fields for a method receiver.
8681 if (!Type::are_assignable_hidden_ok(fntype->receiver()->type(),
8682 first_arg_type, &reason))
8685 this->report_error(_("incompatible type for receiver"));
8688 error_at(this->location(),
8689 "incompatible type for receiver (%s)",
8691 this->set_is_error();
8697 // Note that varargs was handled by the lower_varargs() method, so
8698 // we don't have to worry about it here.
8700 const Typed_identifier_list* parameters = fntype->parameters();
8701 if (this->args_ == NULL)
8703 if (parameters != NULL && !parameters->empty())
8704 this->report_error(_("not enough arguments"));
8706 else if (parameters == NULL)
8707 this->report_error(_("too many arguments"));
8711 Typed_identifier_list::const_iterator pt = parameters->begin();
8712 for (Expression_list::const_iterator pa = this->args_->begin();
8713 pa != this->args_->end();
8716 if (pt == parameters->end())
8718 this->report_error(_("too many arguments"));
8721 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8722 (*pa)->location(), false);
8724 if (pt != parameters->end())
8725 this->report_error(_("not enough arguments"));
8729 // Return whether we have to use a temporary variable to ensure that
8730 // we evaluate this call expression in order. If the call returns no
8731 // results then it will inevitably be executed last. If the call
8732 // returns more than one result then it will be used with Call_result
8733 // expressions. So we only have to use a temporary variable if the
8734 // call returns exactly one result.
8737 Call_expression::do_must_eval_in_order() const
8739 return this->result_count() == 1;
8742 // Get the function and the first argument to use when calling a bound
8746 Call_expression::bound_method_function(Translate_context* context,
8747 Bound_method_expression* bound_method,
8748 tree* first_arg_ptr)
8750 Expression* first_argument = bound_method->first_argument();
8751 tree first_arg = first_argument->get_tree(context);
8752 if (first_arg == error_mark_node)
8753 return error_mark_node;
8755 // We always pass a pointer to the first argument when calling a
8757 if (first_argument->type()->points_to() == NULL)
8759 tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg));
8760 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg))
8761 || DECL_P(first_arg)
8762 || TREE_CODE(first_arg) == INDIRECT_REF
8763 || TREE_CODE(first_arg) == COMPONENT_REF)
8765 first_arg = build_fold_addr_expr(first_arg);
8766 if (DECL_P(first_arg))
8767 TREE_ADDRESSABLE(first_arg) = 1;
8771 tree tmp = create_tmp_var(TREE_TYPE(first_arg),
8772 get_name(first_arg));
8773 DECL_IGNORED_P(tmp) = 0;
8774 DECL_INITIAL(tmp) = first_arg;
8775 first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type,
8776 build1(DECL_EXPR, void_type_node, tmp),
8777 build_fold_addr_expr(tmp));
8778 TREE_ADDRESSABLE(tmp) = 1;
8780 if (first_arg == error_mark_node)
8781 return error_mark_node;
8784 Type* fatype = bound_method->first_argument_type();
8787 if (fatype->points_to() == NULL)
8788 fatype = Type::make_pointer_type(fatype);
8789 first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg);
8790 if (first_arg == error_mark_node
8791 || TREE_TYPE(first_arg) == error_mark_node)
8792 return error_mark_node;
8795 *first_arg_ptr = first_arg;
8797 return bound_method->method()->get_tree(context);
8800 // Get the function and the first argument to use when calling an
8801 // interface method.
8804 Call_expression::interface_method_function(
8805 Translate_context* context,
8806 Interface_field_reference_expression* interface_method,
8807 tree* first_arg_ptr)
8809 tree expr = interface_method->expr()->get_tree(context);
8810 if (expr == error_mark_node)
8811 return error_mark_node;
8812 expr = save_expr(expr);
8813 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
8814 if (first_arg == error_mark_node)
8815 return error_mark_node;
8816 *first_arg_ptr = first_arg;
8817 return interface_method->get_function_tree(context, expr);
8820 // Build the call expression.
8823 Call_expression::do_get_tree(Translate_context* context)
8825 if (this->tree_ != NULL_TREE)
8828 Function_type* fntype = this->get_function_type();
8830 return error_mark_node;
8832 if (this->fn_->is_error_expression())
8833 return error_mark_node;
8835 Gogo* gogo = context->gogo();
8836 source_location location = this->location();
8838 Func_expression* func = this->fn_->func_expression();
8839 Bound_method_expression* bound_method = this->fn_->bound_method_expression();
8840 Interface_field_reference_expression* interface_method =
8841 this->fn_->interface_field_reference_expression();
8842 const bool has_closure = func != NULL && func->closure() != NULL;
8843 const bool is_method = bound_method != NULL || interface_method != NULL;
8844 gcc_assert(!fntype->is_method() || is_method);
8848 if (this->args_ == NULL || this->args_->empty())
8850 nargs = is_method ? 1 : 0;
8851 args = nargs == 0 ? NULL : new tree[nargs];
8855 const Typed_identifier_list* params = fntype->parameters();
8856 gcc_assert(params != NULL);
8858 nargs = this->args_->size();
8859 int i = is_method ? 1 : 0;
8861 args = new tree[nargs];
8863 Typed_identifier_list::const_iterator pp = params->begin();
8864 Expression_list::const_iterator pe;
8865 for (pe = this->args_->begin();
8866 pe != this->args_->end();
8869 gcc_assert(pp != params->end());
8870 tree arg_val = (*pe)->get_tree(context);
8871 args[i] = Expression::convert_for_assignment(context,
8876 if (args[i] == error_mark_node)
8879 return error_mark_node;
8882 gcc_assert(pp == params->end());
8883 gcc_assert(i == nargs);
8886 tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo)));
8887 if (rettype == error_mark_node)
8890 return error_mark_node;
8895 fn = func->get_tree_without_closure(gogo);
8896 else if (!is_method)
8897 fn = this->fn_->get_tree(context);
8898 else if (bound_method != NULL)
8899 fn = this->bound_method_function(context, bound_method, &args[0]);
8900 else if (interface_method != NULL)
8901 fn = this->interface_method_function(context, interface_method, &args[0]);
8905 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
8908 return error_mark_node;
8912 if (TREE_CODE(fndecl) == ADDR_EXPR)
8913 fndecl = TREE_OPERAND(fndecl, 0);
8915 // Add a type cast in case the type of the function is a recursive
8916 // type which refers to itself.
8917 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
8919 tree fnt = fntype->get_tree(gogo);
8920 if (fnt == error_mark_node)
8921 return error_mark_node;
8922 fn = fold_convert_loc(location, fnt, fn);
8925 // This is to support builtin math functions when using 80387 math.
8926 tree excess_type = NULL_TREE;
8928 && DECL_IS_BUILTIN(fndecl)
8929 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
8931 && ((SCALAR_FLOAT_TYPE_P(rettype)
8932 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
8933 || (COMPLEX_FLOAT_TYPE_P(rettype)
8934 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
8936 excess_type = excess_precision_type(TREE_TYPE(args[0]));
8937 if (excess_type != NULL_TREE)
8939 tree excess_fndecl = mathfn_built_in(excess_type,
8940 DECL_FUNCTION_CODE(fndecl));
8941 if (excess_fndecl == NULL_TREE)
8942 excess_type = NULL_TREE;
8945 fn = build_fold_addr_expr_loc(location, excess_fndecl);
8946 for (int i = 0; i < nargs; ++i)
8947 args[i] = ::convert(excess_type, args[i]);
8952 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
8956 SET_EXPR_LOCATION(ret, location);
8960 tree closure_tree = func->closure()->get_tree(context);
8961 if (closure_tree != error_mark_node)
8962 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
8965 // If this is a recursive function type which returns itself, as in
8967 // we have used ptr_type_node for the return type. Add a cast here
8968 // to the correct type.
8969 if (TREE_TYPE(ret) == ptr_type_node)
8971 tree t = this->type()->base()->get_tree(gogo);
8972 ret = fold_convert_loc(location, t, ret);
8975 if (excess_type != NULL_TREE)
8977 // Calling convert here can undo our excess precision change.
8978 // That may or may not be a bug in convert_to_real.
8979 ret = build1(NOP_EXPR, rettype, ret);
8982 // If there is more than one result, we will refer to the call
8984 if (fntype->results() != NULL && fntype->results()->size() > 1)
8985 ret = save_expr(ret);
8992 // Make a call expression.
8995 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
8996 source_location location)
8998 return new Call_expression(fn, args, is_varargs, location);
9001 // A single result from a call which returns multiple results.
9003 class Call_result_expression : public Expression
9006 Call_result_expression(Call_expression* call, unsigned int index)
9007 : Expression(EXPRESSION_CALL_RESULT, call->location()),
9008 call_(call), index_(index)
9013 do_traverse(Traverse*);
9019 do_determine_type(const Type_context*);
9022 do_check_types(Gogo*);
9027 return new Call_result_expression(this->call_->call_expression(),
9032 do_must_eval_in_order() const
9036 do_get_tree(Translate_context*);
9039 // The underlying call expression.
9041 // Which result we want.
9042 unsigned int index_;
9045 // Traverse a call result.
9048 Call_result_expression::do_traverse(Traverse* traverse)
9050 if (traverse->remember_expression(this->call_))
9052 // We have already traversed the call expression.
9053 return TRAVERSE_CONTINUE;
9055 return Expression::traverse(&this->call_, traverse);
9061 Call_result_expression::do_type()
9063 if (this->classification() == EXPRESSION_ERROR)
9064 return Type::make_error_type();
9066 // THIS->CALL_ can be replaced with a temporary reference due to
9067 // Call_expression::do_must_eval_in_order when there is an error.
9068 Call_expression* ce = this->call_->call_expression();
9071 this->set_is_error();
9072 return Type::make_error_type();
9074 Function_type* fntype = ce->get_function_type();
9077 this->set_is_error();
9078 return Type::make_error_type();
9080 const Typed_identifier_list* results = fntype->results();
9081 if (results == NULL)
9083 this->report_error(_("number of results does not match "
9084 "number of values"));
9085 return Type::make_error_type();
9087 Typed_identifier_list::const_iterator pr = results->begin();
9088 for (unsigned int i = 0; i < this->index_; ++i)
9090 if (pr == results->end())
9094 if (pr == results->end())
9096 this->report_error(_("number of results does not match "
9097 "number of values"));
9098 return Type::make_error_type();
9103 // Check the type. Just make sure that we trigger the warning in
9107 Call_result_expression::do_check_types(Gogo*)
9112 // Determine the type. We have nothing to do here, but the 0 result
9113 // needs to pass down to the caller.
9116 Call_result_expression::do_determine_type(const Type_context*)
9118 this->call_->determine_type_no_context();
9124 Call_result_expression::do_get_tree(Translate_context* context)
9126 tree call_tree = this->call_->get_tree(context);
9127 if (call_tree == error_mark_node)
9128 return error_mark_node;
9129 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9131 gcc_assert(saw_errors());
9132 return error_mark_node;
9134 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9135 for (unsigned int i = 0; i < this->index_; ++i)
9137 gcc_assert(field != NULL_TREE);
9138 field = DECL_CHAIN(field);
9140 gcc_assert(field != NULL_TREE);
9141 return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE);
9144 // Make a reference to a single result of a call which returns
9145 // multiple results.
9148 Expression::make_call_result(Call_expression* call, unsigned int index)
9150 return new Call_result_expression(call, index);
9153 // Class Index_expression.
9158 Index_expression::do_traverse(Traverse* traverse)
9160 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9161 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9162 || (this->end_ != NULL
9163 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9164 return TRAVERSE_EXIT;
9165 return TRAVERSE_CONTINUE;
9168 // Lower an index expression. This converts the generic index
9169 // expression into an array index, a string index, or a map index.
9172 Index_expression::do_lower(Gogo*, Named_object*, int)
9174 source_location location = this->location();
9175 Expression* left = this->left_;
9176 Expression* start = this->start_;
9177 Expression* end = this->end_;
9179 Type* type = left->type();
9180 if (type->is_error())
9181 return Expression::make_error(location);
9182 else if (left->is_type_expression())
9184 error_at(location, "attempt to index type expression");
9185 return Expression::make_error(location);
9187 else if (type->array_type() != NULL)
9188 return Expression::make_array_index(left, start, end, location);
9189 else if (type->points_to() != NULL
9190 && type->points_to()->array_type() != NULL
9191 && !type->points_to()->is_open_array_type())
9193 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9195 return Expression::make_array_index(deref, start, end, location);
9197 else if (type->is_string_type())
9198 return Expression::make_string_index(left, start, end, location);
9199 else if (type->map_type() != NULL)
9203 error_at(location, "invalid slice of map");
9204 return Expression::make_error(location);
9206 Map_index_expression* ret= Expression::make_map_index(left, start,
9208 if (this->is_lvalue_)
9209 ret->set_is_lvalue();
9215 "attempt to index object which is not array, string, or map");
9216 return Expression::make_error(location);
9220 // Make an index expression.
9223 Expression::make_index(Expression* left, Expression* start, Expression* end,
9224 source_location location)
9226 return new Index_expression(left, start, end, location);
9229 // An array index. This is used for both indexing and slicing.
9231 class Array_index_expression : public Expression
9234 Array_index_expression(Expression* array, Expression* start,
9235 Expression* end, source_location location)
9236 : Expression(EXPRESSION_ARRAY_INDEX, location),
9237 array_(array), start_(start), end_(end), type_(NULL)
9242 do_traverse(Traverse*);
9248 do_determine_type(const Type_context*);
9251 do_check_types(Gogo*);
9256 return Expression::make_array_index(this->array_->copy(),
9257 this->start_->copy(),
9260 : this->end_->copy()),
9265 do_is_addressable() const;
9268 do_address_taken(bool escapes)
9269 { this->array_->address_taken(escapes); }
9272 do_get_tree(Translate_context*);
9275 // The array we are getting a value from.
9277 // The start or only index.
9279 // The end index of a slice. This may be NULL for a simple array
9280 // index, or it may be a nil expression for the length of the array.
9282 // The type of the expression.
9286 // Array index traversal.
9289 Array_index_expression::do_traverse(Traverse* traverse)
9291 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9292 return TRAVERSE_EXIT;
9293 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9294 return TRAVERSE_EXIT;
9295 if (this->end_ != NULL)
9297 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9298 return TRAVERSE_EXIT;
9300 return TRAVERSE_CONTINUE;
9303 // Return the type of an array index.
9306 Array_index_expression::do_type()
9308 if (this->type_ == NULL)
9310 Array_type* type = this->array_->type()->array_type();
9312 this->type_ = Type::make_error_type();
9313 else if (this->end_ == NULL)
9314 this->type_ = type->element_type();
9315 else if (type->is_open_array_type())
9317 // A slice of a slice has the same type as the original
9319 this->type_ = this->array_->type()->deref();
9323 // A slice of an array is a slice.
9324 this->type_ = Type::make_array_type(type->element_type(), NULL);
9330 // Set the type of an array index.
9333 Array_index_expression::do_determine_type(const Type_context*)
9335 this->array_->determine_type_no_context();
9336 this->start_->determine_type_no_context();
9337 if (this->end_ != NULL)
9338 this->end_->determine_type_no_context();
9341 // Check types of an array index.
9344 Array_index_expression::do_check_types(Gogo*)
9346 if (this->start_->type()->integer_type() == NULL)
9347 this->report_error(_("index must be integer"));
9348 if (this->end_ != NULL
9349 && this->end_->type()->integer_type() == NULL
9350 && !this->end_->is_nil_expression())
9351 this->report_error(_("slice end must be integer"));
9353 Array_type* array_type = this->array_->type()->array_type();
9354 if (array_type == NULL)
9356 gcc_assert(this->array_->type()->is_error());
9360 unsigned int int_bits =
9361 Type::lookup_integer_type("int")->integer_type()->bits();
9366 bool lval_valid = (array_type->length() != NULL
9367 && array_type->length()->integer_constant_value(true,
9372 if (this->start_->integer_constant_value(true, ival, &dummy))
9374 if (mpz_sgn(ival) < 0
9375 || mpz_sizeinbase(ival, 2) >= int_bits
9377 && (this->end_ == NULL
9378 ? mpz_cmp(ival, lval) >= 0
9379 : mpz_cmp(ival, lval) > 0)))
9381 error_at(this->start_->location(), "array index out of bounds");
9382 this->set_is_error();
9385 if (this->end_ != NULL && !this->end_->is_nil_expression())
9387 if (this->end_->integer_constant_value(true, ival, &dummy))
9389 if (mpz_sgn(ival) < 0
9390 || mpz_sizeinbase(ival, 2) >= int_bits
9391 || (lval_valid && mpz_cmp(ival, lval) > 0))
9393 error_at(this->end_->location(), "array index out of bounds");
9394 this->set_is_error();
9401 // A slice of an array requires an addressable array. A slice of a
9402 // slice is always possible.
9403 if (this->end_ != NULL && !array_type->is_open_array_type())
9405 if (!this->array_->is_addressable())
9406 this->report_error(_("array is not addressable"));
9408 this->array_->address_taken(true);
9412 // Return whether this expression is addressable.
9415 Array_index_expression::do_is_addressable() const
9417 // A slice expression is not addressable.
9418 if (this->end_ != NULL)
9421 // An index into a slice is addressable.
9422 if (this->array_->type()->is_open_array_type())
9425 // An index into an array is addressable if the array is
9427 return this->array_->is_addressable();
9430 // Get a tree for an array index.
9433 Array_index_expression::do_get_tree(Translate_context* context)
9435 Gogo* gogo = context->gogo();
9436 source_location loc = this->location();
9438 Array_type* array_type = this->array_->type()->array_type();
9439 if (array_type == NULL)
9441 gcc_assert(this->array_->type()->is_error());
9442 return error_mark_node;
9445 tree type_tree = array_type->get_tree(gogo);
9446 if (type_tree == error_mark_node)
9447 return error_mark_node;
9449 tree array_tree = this->array_->get_tree(context);
9450 if (array_tree == error_mark_node)
9451 return error_mark_node;
9453 if (array_type->length() == NULL && !DECL_P(array_tree))
9454 array_tree = save_expr(array_tree);
9455 tree length_tree = array_type->length_tree(gogo, array_tree);
9456 if (length_tree == error_mark_node)
9457 return error_mark_node;
9458 length_tree = save_expr(length_tree);
9459 tree length_type = TREE_TYPE(length_tree);
9461 tree bad_index = boolean_false_node;
9463 tree start_tree = this->start_->get_tree(context);
9464 if (start_tree == error_mark_node)
9465 return error_mark_node;
9466 if (!DECL_P(start_tree))
9467 start_tree = save_expr(start_tree);
9468 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9469 start_tree = convert_to_integer(length_type, start_tree);
9471 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9474 start_tree = fold_convert_loc(loc, length_type, start_tree);
9475 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
9476 fold_build2_loc(loc,
9480 boolean_type_node, start_tree,
9483 int code = (array_type->length() != NULL
9484 ? (this->end_ == NULL
9485 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9486 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9487 : (this->end_ == NULL
9488 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9489 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9490 tree crash = Gogo::runtime_error(code, loc);
9492 if (this->end_ == NULL)
9494 // Simple array indexing. This has to return an l-value, so
9495 // wrap the index check into START_TREE.
9496 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9497 build3(COND_EXPR, void_type_node,
9498 bad_index, crash, NULL_TREE),
9500 start_tree = fold_convert_loc(loc, sizetype, start_tree);
9502 if (array_type->length() != NULL)
9505 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9506 start_tree, NULL_TREE, NULL_TREE);
9511 tree values = array_type->value_pointer_tree(gogo, array_tree);
9512 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9513 if (element_type_tree == error_mark_node)
9514 return error_mark_node;
9515 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9516 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9517 start_tree, element_size);
9518 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9519 TREE_TYPE(values), values, offset);
9520 return build_fold_indirect_ref(ptr);
9526 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
9527 if (capacity_tree == error_mark_node)
9528 return error_mark_node;
9529 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
9532 if (this->end_->is_nil_expression())
9533 end_tree = length_tree;
9536 end_tree = this->end_->get_tree(context);
9537 if (end_tree == error_mark_node)
9538 return error_mark_node;
9539 if (!DECL_P(end_tree))
9540 end_tree = save_expr(end_tree);
9541 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9542 end_tree = convert_to_integer(length_type, end_tree);
9544 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9547 end_tree = fold_convert_loc(loc, length_type, end_tree);
9549 capacity_tree = save_expr(capacity_tree);
9550 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9551 fold_build2_loc(loc, LT_EXPR,
9553 end_tree, start_tree),
9554 fold_build2_loc(loc, GT_EXPR,
9556 end_tree, capacity_tree));
9557 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9558 bad_index, bad_end);
9561 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9562 if (element_type_tree == error_mark_node)
9563 return error_mark_node;
9564 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9566 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9567 fold_convert_loc(loc, sizetype, start_tree),
9570 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9571 if (value_pointer == error_mark_node)
9572 return error_mark_node;
9574 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9575 TREE_TYPE(value_pointer),
9576 value_pointer, offset);
9578 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9579 end_tree, start_tree);
9581 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9582 capacity_tree, start_tree);
9584 tree struct_tree = this->type()->get_tree(gogo);
9585 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9587 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9589 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9590 tree field = TYPE_FIELDS(struct_tree);
9591 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9593 elt->value = value_pointer;
9595 elt = VEC_quick_push(constructor_elt, init, NULL);
9596 field = DECL_CHAIN(field);
9597 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9599 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
9601 elt = VEC_quick_push(constructor_elt, init, NULL);
9602 field = DECL_CHAIN(field);
9603 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9605 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
9607 tree constructor = build_constructor(struct_tree, init);
9609 if (TREE_CONSTANT(value_pointer)
9610 && TREE_CONSTANT(result_length_tree)
9611 && TREE_CONSTANT(result_capacity_tree))
9612 TREE_CONSTANT(constructor) = 1;
9614 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
9615 build3(COND_EXPR, void_type_node,
9616 bad_index, crash, NULL_TREE),
9620 // Make an array index expression. END may be NULL.
9623 Expression::make_array_index(Expression* array, Expression* start,
9624 Expression* end, source_location location)
9626 // Taking a slice of a composite literal requires moving the literal
9628 if (end != NULL && array->is_composite_literal())
9630 array = Expression::make_heap_composite(array, location);
9631 array = Expression::make_unary(OPERATOR_MULT, array, location);
9633 return new Array_index_expression(array, start, end, location);
9636 // A string index. This is used for both indexing and slicing.
9638 class String_index_expression : public Expression
9641 String_index_expression(Expression* string, Expression* start,
9642 Expression* end, source_location location)
9643 : Expression(EXPRESSION_STRING_INDEX, location),
9644 string_(string), start_(start), end_(end)
9649 do_traverse(Traverse*);
9655 do_determine_type(const Type_context*);
9658 do_check_types(Gogo*);
9663 return Expression::make_string_index(this->string_->copy(),
9664 this->start_->copy(),
9667 : this->end_->copy()),
9672 do_get_tree(Translate_context*);
9675 // The string we are getting a value from.
9676 Expression* string_;
9677 // The start or only index.
9679 // The end index of a slice. This may be NULL for a single index,
9680 // or it may be a nil expression for the length of the string.
9684 // String index traversal.
9687 String_index_expression::do_traverse(Traverse* traverse)
9689 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
9690 return TRAVERSE_EXIT;
9691 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9692 return TRAVERSE_EXIT;
9693 if (this->end_ != NULL)
9695 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9696 return TRAVERSE_EXIT;
9698 return TRAVERSE_CONTINUE;
9701 // Return the type of a string index.
9704 String_index_expression::do_type()
9706 if (this->end_ == NULL)
9707 return Type::lookup_integer_type("uint8");
9709 return this->string_->type();
9712 // Determine the type of a string index.
9715 String_index_expression::do_determine_type(const Type_context*)
9717 this->string_->determine_type_no_context();
9718 this->start_->determine_type_no_context();
9719 if (this->end_ != NULL)
9720 this->end_->determine_type_no_context();
9723 // Check types of a string index.
9726 String_index_expression::do_check_types(Gogo*)
9728 if (this->start_->type()->integer_type() == NULL)
9729 this->report_error(_("index must be integer"));
9730 if (this->end_ != NULL
9731 && this->end_->type()->integer_type() == NULL
9732 && !this->end_->is_nil_expression())
9733 this->report_error(_("slice end must be integer"));
9736 bool sval_valid = this->string_->string_constant_value(&sval);
9741 if (this->start_->integer_constant_value(true, ival, &dummy))
9743 if (mpz_sgn(ival) < 0
9744 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
9746 error_at(this->start_->location(), "string index out of bounds");
9747 this->set_is_error();
9750 if (this->end_ != NULL && !this->end_->is_nil_expression())
9752 if (this->end_->integer_constant_value(true, ival, &dummy))
9754 if (mpz_sgn(ival) < 0
9755 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
9757 error_at(this->end_->location(), "string index out of bounds");
9758 this->set_is_error();
9765 // Get a tree for a string index.
9768 String_index_expression::do_get_tree(Translate_context* context)
9770 source_location loc = this->location();
9772 tree string_tree = this->string_->get_tree(context);
9773 if (string_tree == error_mark_node)
9774 return error_mark_node;
9776 if (this->string_->type()->points_to() != NULL)
9777 string_tree = build_fold_indirect_ref(string_tree);
9778 if (!DECL_P(string_tree))
9779 string_tree = save_expr(string_tree);
9780 tree string_type = TREE_TYPE(string_tree);
9782 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
9783 length_tree = save_expr(length_tree);
9784 tree length_type = TREE_TYPE(length_tree);
9786 tree bad_index = boolean_false_node;
9788 tree start_tree = this->start_->get_tree(context);
9789 if (start_tree == error_mark_node)
9790 return error_mark_node;
9791 if (!DECL_P(start_tree))
9792 start_tree = save_expr(start_tree);
9793 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9794 start_tree = convert_to_integer(length_type, start_tree);
9796 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9799 start_tree = fold_convert_loc(loc, length_type, start_tree);
9801 int code = (this->end_ == NULL
9802 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9803 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
9804 tree crash = Gogo::runtime_error(code, loc);
9806 if (this->end_ == NULL)
9808 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9810 fold_build2_loc(loc, GE_EXPR,
9812 start_tree, length_tree));
9814 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
9815 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
9817 fold_convert_loc(loc, sizetype, start_tree));
9818 tree index = build_fold_indirect_ref_loc(loc, ptr);
9820 return build2(COMPOUND_EXPR, TREE_TYPE(index),
9821 build3(COND_EXPR, void_type_node,
9822 bad_index, crash, NULL_TREE),
9828 if (this->end_->is_nil_expression())
9829 end_tree = build_int_cst(length_type, -1);
9832 end_tree = this->end_->get_tree(context);
9833 if (end_tree == error_mark_node)
9834 return error_mark_node;
9835 if (!DECL_P(end_tree))
9836 end_tree = save_expr(end_tree);
9837 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9838 end_tree = convert_to_integer(length_type, end_tree);
9840 bad_index = Expression::check_bounds(end_tree, length_type,
9843 end_tree = fold_convert_loc(loc, length_type, end_tree);
9846 static tree strslice_fndecl;
9847 tree ret = Gogo::call_builtin(&strslice_fndecl,
9849 "__go_string_slice",
9858 if (ret == error_mark_node)
9859 return error_mark_node;
9860 // This will panic if the bounds are out of range for the
9862 TREE_NOTHROW(strslice_fndecl) = 0;
9864 if (bad_index == boolean_false_node)
9867 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
9868 build3(COND_EXPR, void_type_node,
9869 bad_index, crash, NULL_TREE),
9874 // Make a string index expression. END may be NULL.
9877 Expression::make_string_index(Expression* string, Expression* start,
9878 Expression* end, source_location location)
9880 return new String_index_expression(string, start, end, location);
9885 // Get the type of the map.
9888 Map_index_expression::get_map_type() const
9890 Map_type* mt = this->map_->type()->deref()->map_type();
9892 gcc_assert(saw_errors());
9896 // Map index traversal.
9899 Map_index_expression::do_traverse(Traverse* traverse)
9901 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
9902 return TRAVERSE_EXIT;
9903 return Expression::traverse(&this->index_, traverse);
9906 // Return the type of a map index.
9909 Map_index_expression::do_type()
9911 Map_type* mt = this->get_map_type();
9913 return Type::make_error_type();
9914 Type* type = mt->val_type();
9915 // If this map index is in a tuple assignment, we actually return a
9916 // pointer to the value type. Tuple_map_assignment_statement is
9917 // responsible for handling this correctly. We need to get the type
9918 // right in case this gets assigned to a temporary variable.
9919 if (this->is_in_tuple_assignment_)
9920 type = Type::make_pointer_type(type);
9924 // Fix the type of a map index.
9927 Map_index_expression::do_determine_type(const Type_context*)
9929 this->map_->determine_type_no_context();
9930 Map_type* mt = this->get_map_type();
9931 Type* key_type = mt == NULL ? NULL : mt->key_type();
9932 Type_context subcontext(key_type, false);
9933 this->index_->determine_type(&subcontext);
9936 // Check types of a map index.
9939 Map_index_expression::do_check_types(Gogo*)
9942 Map_type* mt = this->get_map_type();
9945 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
9948 this->report_error(_("incompatible type for map index"));
9951 error_at(this->location(), "incompatible type for map index (%s)",
9953 this->set_is_error();
9958 // Get a tree for a map index.
9961 Map_index_expression::do_get_tree(Translate_context* context)
9963 Map_type* type = this->get_map_type();
9965 return error_mark_node;
9967 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
9968 if (valptr == error_mark_node)
9969 return error_mark_node;
9970 valptr = save_expr(valptr);
9972 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
9974 if (this->is_lvalue_)
9975 return build_fold_indirect_ref(valptr);
9976 else if (this->is_in_tuple_assignment_)
9978 // Tuple_map_assignment_statement is responsible for using this
9984 return fold_build3(COND_EXPR, val_type_tree,
9985 fold_build2(EQ_EXPR, boolean_type_node, valptr,
9986 fold_convert(TREE_TYPE(valptr),
9987 null_pointer_node)),
9988 type->val_type()->get_init_tree(context->gogo(),
9990 build_fold_indirect_ref(valptr));
9994 // Get a tree for the map index. This returns a tree which evaluates
9995 // to a pointer to a value. The pointer will be NULL if the key is
9999 Map_index_expression::get_value_pointer(Translate_context* context,
10002 Map_type* type = this->get_map_type();
10004 return error_mark_node;
10006 tree map_tree = this->map_->get_tree(context);
10007 tree index_tree = this->index_->get_tree(context);
10008 index_tree = Expression::convert_for_assignment(context, type->key_type(),
10009 this->index_->type(),
10012 if (map_tree == error_mark_node || index_tree == error_mark_node)
10013 return error_mark_node;
10015 if (this->map_->type()->points_to() != NULL)
10016 map_tree = build_fold_indirect_ref(map_tree);
10018 // We need to pass in a pointer to the key, so stuff it into a
10022 if (current_function_decl != NULL)
10024 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
10025 DECL_IGNORED_P(tmp) = 0;
10026 DECL_INITIAL(tmp) = index_tree;
10027 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
10028 TREE_ADDRESSABLE(tmp) = 1;
10032 tmp = build_decl(this->location(), VAR_DECL, create_tmp_var_name("M"),
10033 TREE_TYPE(index_tree));
10034 DECL_EXTERNAL(tmp) = 0;
10035 TREE_PUBLIC(tmp) = 0;
10036 TREE_STATIC(tmp) = 1;
10037 DECL_ARTIFICIAL(tmp) = 1;
10038 if (!TREE_CONSTANT(index_tree))
10039 make_tmp = fold_build2_loc(this->location(), INIT_EXPR, void_type_node,
10043 TREE_READONLY(tmp) = 1;
10044 TREE_CONSTANT(tmp) = 1;
10045 DECL_INITIAL(tmp) = index_tree;
10046 make_tmp = NULL_TREE;
10048 rest_of_decl_compilation(tmp, 1, 0);
10050 tree tmpref = fold_convert_loc(this->location(), const_ptr_type_node,
10051 build_fold_addr_expr_loc(this->location(),
10054 static tree map_index_fndecl;
10055 tree call = Gogo::call_builtin(&map_index_fndecl,
10059 const_ptr_type_node,
10060 TREE_TYPE(map_tree),
10062 const_ptr_type_node,
10066 ? boolean_true_node
10067 : boolean_false_node));
10068 if (call == error_mark_node)
10069 return error_mark_node;
10070 // This can panic on a map of interface type if the interface holds
10071 // an uncomparable or unhashable type.
10072 TREE_NOTHROW(map_index_fndecl) = 0;
10074 tree val_type_tree = type->val_type()->get_tree(context->gogo());
10075 if (val_type_tree == error_mark_node)
10076 return error_mark_node;
10077 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
10079 tree ret = fold_convert_loc(this->location(), ptr_val_type_tree, call);
10080 if (make_tmp != NULL_TREE)
10081 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
10085 // Make a map index expression.
10087 Map_index_expression*
10088 Expression::make_map_index(Expression* map, Expression* index,
10089 source_location location)
10091 return new Map_index_expression(map, index, location);
10094 // Class Field_reference_expression.
10096 // Return the type of a field reference.
10099 Field_reference_expression::do_type()
10101 Type* type = this->expr_->type();
10102 if (type->is_error())
10104 Struct_type* struct_type = type->struct_type();
10105 gcc_assert(struct_type != NULL);
10106 return struct_type->field(this->field_index_)->type();
10109 // Check the types for a field reference.
10112 Field_reference_expression::do_check_types(Gogo*)
10114 Type* type = this->expr_->type();
10115 if (type->is_error())
10117 Struct_type* struct_type = type->struct_type();
10118 gcc_assert(struct_type != NULL);
10119 gcc_assert(struct_type->field(this->field_index_) != NULL);
10122 // Get a tree for a field reference.
10125 Field_reference_expression::do_get_tree(Translate_context* context)
10127 tree struct_tree = this->expr_->get_tree(context);
10128 if (struct_tree == error_mark_node
10129 || TREE_TYPE(struct_tree) == error_mark_node)
10130 return error_mark_node;
10131 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
10132 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
10133 if (field == NULL_TREE)
10135 // This can happen for a type which refers to itself indirectly
10136 // and then turns out to be erroneous.
10137 gcc_assert(saw_errors());
10138 return error_mark_node;
10140 for (unsigned int i = this->field_index_; i > 0; --i)
10142 field = DECL_CHAIN(field);
10143 gcc_assert(field != NULL_TREE);
10145 if (TREE_TYPE(field) == error_mark_node)
10146 return error_mark_node;
10147 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
10151 // Make a reference to a qualified identifier in an expression.
10153 Field_reference_expression*
10154 Expression::make_field_reference(Expression* expr, unsigned int field_index,
10155 source_location location)
10157 return new Field_reference_expression(expr, field_index, location);
10160 // Class Interface_field_reference_expression.
10162 // Return a tree for the pointer to the function to call.
10165 Interface_field_reference_expression::get_function_tree(Translate_context*,
10168 if (this->expr_->type()->points_to() != NULL)
10169 expr = build_fold_indirect_ref(expr);
10171 tree expr_type = TREE_TYPE(expr);
10172 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10174 tree field = TYPE_FIELDS(expr_type);
10175 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
10177 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10178 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table)));
10180 table = build_fold_indirect_ref(table);
10181 gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
10183 std::string name = Gogo::unpack_hidden_name(this->name_);
10184 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
10185 field != NULL_TREE;
10186 field = DECL_CHAIN(field))
10188 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
10191 gcc_assert(field != NULL_TREE);
10193 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
10196 // Return a tree for the first argument to pass to the interface
10200 Interface_field_reference_expression::get_underlying_object_tree(
10201 Translate_context*,
10204 if (this->expr_->type()->points_to() != NULL)
10205 expr = build_fold_indirect_ref(expr);
10207 tree expr_type = TREE_TYPE(expr);
10208 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10210 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
10211 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
10213 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10219 Interface_field_reference_expression::do_traverse(Traverse* traverse)
10221 return Expression::traverse(&this->expr_, traverse);
10224 // Return the type of an interface field reference.
10227 Interface_field_reference_expression::do_type()
10229 Type* expr_type = this->expr_->type();
10231 Type* points_to = expr_type->points_to();
10232 if (points_to != NULL)
10233 expr_type = points_to;
10235 Interface_type* interface_type = expr_type->interface_type();
10236 if (interface_type == NULL)
10237 return Type::make_error_type();
10239 const Typed_identifier* method = interface_type->find_method(this->name_);
10240 if (method == NULL)
10241 return Type::make_error_type();
10243 return method->type();
10246 // Determine types.
10249 Interface_field_reference_expression::do_determine_type(const Type_context*)
10251 this->expr_->determine_type_no_context();
10254 // Check the types for an interface field reference.
10257 Interface_field_reference_expression::do_check_types(Gogo*)
10259 Type* type = this->expr_->type();
10261 Type* points_to = type->points_to();
10262 if (points_to != NULL)
10265 Interface_type* interface_type = type->interface_type();
10266 if (interface_type == NULL)
10268 if (!type->is_error_type())
10269 this->report_error(_("expected interface or pointer to interface"));
10273 const Typed_identifier* method =
10274 interface_type->find_method(this->name_);
10275 if (method == NULL)
10277 error_at(this->location(), "method %qs not in interface",
10278 Gogo::message_name(this->name_).c_str());
10279 this->set_is_error();
10284 // Get a tree for a reference to a field in an interface. There is no
10285 // standard tree type representation for this: it's a function
10286 // attached to its first argument, like a Bound_method_expression.
10287 // The only places it may currently be used are in a Call_expression
10288 // or a Go_statement, which will take it apart directly. So this has
10289 // nothing to do at present.
10292 Interface_field_reference_expression::do_get_tree(Translate_context*)
10297 // Make a reference to a field in an interface.
10300 Expression::make_interface_field_reference(Expression* expr,
10301 const std::string& field,
10302 source_location location)
10304 return new Interface_field_reference_expression(expr, field, location);
10307 // A general selector. This is a Parser_expression for LEFT.NAME. It
10308 // is lowered after we know the type of the left hand side.
10310 class Selector_expression : public Parser_expression
10313 Selector_expression(Expression* left, const std::string& name,
10314 source_location location)
10315 : Parser_expression(EXPRESSION_SELECTOR, location),
10316 left_(left), name_(name)
10321 do_traverse(Traverse* traverse)
10322 { return Expression::traverse(&this->left_, traverse); }
10325 do_lower(Gogo*, Named_object*, int);
10330 return new Selector_expression(this->left_->copy(), this->name_,
10336 lower_method_expression(Gogo*);
10338 // The expression on the left hand side.
10340 // The name on the right hand side.
10344 // Lower a selector expression once we know the real type of the left
10348 Selector_expression::do_lower(Gogo* gogo, Named_object*, int)
10350 Expression* left = this->left_;
10351 if (left->is_type_expression())
10352 return this->lower_method_expression(gogo);
10353 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
10357 // Lower a method expression T.M or (*T).M. We turn this into a
10358 // function literal.
10361 Selector_expression::lower_method_expression(Gogo* gogo)
10363 source_location location = this->location();
10364 Type* type = this->left_->type();
10365 const std::string& name(this->name_);
10368 if (type->points_to() == NULL)
10369 is_pointer = false;
10373 type = type->points_to();
10375 Named_type* nt = type->named_type();
10379 ("method expression requires named type or "
10380 "pointer to named type"));
10381 return Expression::make_error(location);
10385 Method* method = nt->method_function(name, &is_ambiguous);
10386 const Typed_identifier* imethod = NULL;
10387 if (method == NULL && !is_pointer)
10389 Interface_type* it = nt->interface_type();
10391 imethod = it->find_method(name);
10394 if (method == NULL && imethod == NULL)
10397 error_at(location, "type %<%s%s%> has no method %<%s%>",
10398 is_pointer ? "*" : "",
10399 nt->message_name().c_str(),
10400 Gogo::message_name(name).c_str());
10402 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
10403 Gogo::message_name(name).c_str(),
10404 is_pointer ? "*" : "",
10405 nt->message_name().c_str());
10406 return Expression::make_error(location);
10409 if (method != NULL && !is_pointer && !method->is_value_method())
10411 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
10412 nt->message_name().c_str(),
10413 Gogo::message_name(name).c_str());
10414 return Expression::make_error(location);
10417 // Build a new function type in which the receiver becomes the first
10419 Function_type* method_type;
10420 if (method != NULL)
10422 method_type = method->type();
10423 gcc_assert(method_type->is_method());
10427 method_type = imethod->type()->function_type();
10428 gcc_assert(method_type != NULL && !method_type->is_method());
10431 const char* const receiver_name = "$this";
10432 Typed_identifier_list* parameters = new Typed_identifier_list();
10433 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
10436 const Typed_identifier_list* method_parameters = method_type->parameters();
10437 if (method_parameters != NULL)
10439 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10440 p != method_parameters->end();
10442 parameters->push_back(*p);
10445 const Typed_identifier_list* method_results = method_type->results();
10446 Typed_identifier_list* results;
10447 if (method_results == NULL)
10451 results = new Typed_identifier_list();
10452 for (Typed_identifier_list::const_iterator p = method_results->begin();
10453 p != method_results->end();
10455 results->push_back(*p);
10458 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
10460 if (method_type->is_varargs())
10461 fntype->set_is_varargs();
10463 // We generate methods which always takes a pointer to the receiver
10464 // as their first argument. If this is for a pointer type, we can
10465 // simply reuse the existing function. We use an internal hack to
10466 // get the right type.
10468 if (method != NULL && is_pointer)
10470 Named_object* mno = (method->needs_stub_method()
10471 ? method->stub_object()
10472 : method->named_object());
10473 Expression* f = Expression::make_func_reference(mno, NULL, location);
10474 f = Expression::make_cast(fntype, f, location);
10475 Type_conversion_expression* tce =
10476 static_cast<Type_conversion_expression*>(f);
10477 tce->set_may_convert_function_types();
10481 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
10484 Named_object* vno = gogo->lookup(receiver_name, NULL);
10485 gcc_assert(vno != NULL);
10486 Expression* ve = Expression::make_var_reference(vno, location);
10488 if (method != NULL)
10489 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
10491 bm = Expression::make_interface_field_reference(ve, name, location);
10493 // Even though we found the method above, if it has an error type we
10494 // may see an error here.
10495 if (bm->is_error_expression())
10497 gogo->finish_function(location);
10501 Expression_list* args;
10502 if (method_parameters == NULL)
10506 args = new Expression_list();
10507 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10508 p != method_parameters->end();
10511 vno = gogo->lookup(p->name(), NULL);
10512 gcc_assert(vno != NULL);
10513 args->push_back(Expression::make_var_reference(vno, location));
10517 Call_expression* call = Expression::make_call(bm, args,
10518 method_type->is_varargs(),
10521 size_t count = call->result_count();
10524 s = Statement::make_statement(call);
10527 Expression_list* retvals = new Expression_list();
10529 retvals->push_back(call);
10532 for (size_t i = 0; i < count; ++i)
10533 retvals->push_back(Expression::make_call_result(call, i));
10535 s = Statement::make_return_statement(retvals, location);
10537 gogo->add_statement(s);
10539 gogo->finish_function(location);
10541 return Expression::make_func_reference(no, NULL, location);
10544 // Make a selector expression.
10547 Expression::make_selector(Expression* left, const std::string& name,
10548 source_location location)
10550 return new Selector_expression(left, name, location);
10553 // Implement the builtin function new.
10555 class Allocation_expression : public Expression
10558 Allocation_expression(Type* type, source_location location)
10559 : Expression(EXPRESSION_ALLOCATION, location),
10565 do_traverse(Traverse* traverse)
10566 { return Type::traverse(this->type_, traverse); }
10570 { return Type::make_pointer_type(this->type_); }
10573 do_determine_type(const Type_context*)
10577 do_check_types(Gogo*);
10581 { return new Allocation_expression(this->type_, this->location()); }
10584 do_get_tree(Translate_context*);
10587 // The type we are allocating.
10591 // Check the type of an allocation expression.
10594 Allocation_expression::do_check_types(Gogo*)
10596 if (this->type_->function_type() != NULL)
10597 this->report_error(_("invalid new of function type"));
10600 // Return a tree for an allocation expression.
10603 Allocation_expression::do_get_tree(Translate_context* context)
10605 tree type_tree = this->type_->get_tree(context->gogo());
10606 if (type_tree == error_mark_node)
10607 return error_mark_node;
10608 tree size_tree = TYPE_SIZE_UNIT(type_tree);
10609 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
10611 if (space == error_mark_node)
10612 return error_mark_node;
10613 return fold_convert(build_pointer_type(type_tree), space);
10616 // Make an allocation expression.
10619 Expression::make_allocation(Type* type, source_location location)
10621 return new Allocation_expression(type, location);
10624 // Implement the builtin function make.
10626 class Make_expression : public Expression
10629 Make_expression(Type* type, Expression_list* args, source_location location)
10630 : Expression(EXPRESSION_MAKE, location),
10631 type_(type), args_(args)
10636 do_traverse(Traverse* traverse);
10640 { return this->type_; }
10643 do_determine_type(const Type_context*);
10646 do_check_types(Gogo*);
10651 return new Make_expression(this->type_, this->args_->copy(),
10656 do_get_tree(Translate_context*);
10659 // The type we are making.
10661 // The arguments to pass to the make routine.
10662 Expression_list* args_;
10668 Make_expression::do_traverse(Traverse* traverse)
10670 if (this->args_ != NULL
10671 && this->args_->traverse(traverse) == TRAVERSE_EXIT)
10672 return TRAVERSE_EXIT;
10673 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10674 return TRAVERSE_EXIT;
10675 return TRAVERSE_CONTINUE;
10678 // Set types of arguments.
10681 Make_expression::do_determine_type(const Type_context*)
10683 if (this->args_ != NULL)
10685 Type_context context(Type::lookup_integer_type("int"), false);
10686 for (Expression_list::const_iterator pe = this->args_->begin();
10687 pe != this->args_->end();
10689 (*pe)->determine_type(&context);
10693 // Check types for a make expression.
10696 Make_expression::do_check_types(Gogo*)
10698 if (this->type_->channel_type() == NULL
10699 && this->type_->map_type() == NULL
10700 && (this->type_->array_type() == NULL
10701 || this->type_->array_type()->length() != NULL))
10702 this->report_error(_("invalid type for make function"));
10703 else if (!this->type_->check_make_expression(this->args_, this->location()))
10704 this->set_is_error();
10707 // Return a tree for a make expression.
10710 Make_expression::do_get_tree(Translate_context* context)
10712 return this->type_->make_expression_tree(context, this->args_,
10716 // Make a make expression.
10719 Expression::make_make(Type* type, Expression_list* args,
10720 source_location location)
10722 return new Make_expression(type, args, location);
10725 // Construct a struct.
10727 class Struct_construction_expression : public Expression
10730 Struct_construction_expression(Type* type, Expression_list* vals,
10731 source_location location)
10732 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
10733 type_(type), vals_(vals)
10736 // Return whether this is a constant initializer.
10738 is_constant_struct() const;
10742 do_traverse(Traverse* traverse);
10746 { return this->type_; }
10749 do_determine_type(const Type_context*);
10752 do_check_types(Gogo*);
10757 return new Struct_construction_expression(this->type_, this->vals_->copy(),
10762 do_is_addressable() const
10766 do_get_tree(Translate_context*);
10769 do_export(Export*) const;
10772 // The type of the struct to construct.
10774 // The list of values, in order of the fields in the struct. A NULL
10775 // entry means that the field should be zero-initialized.
10776 Expression_list* vals_;
10782 Struct_construction_expression::do_traverse(Traverse* traverse)
10784 if (this->vals_ != NULL
10785 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10786 return TRAVERSE_EXIT;
10787 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10788 return TRAVERSE_EXIT;
10789 return TRAVERSE_CONTINUE;
10792 // Return whether this is a constant initializer.
10795 Struct_construction_expression::is_constant_struct() const
10797 if (this->vals_ == NULL)
10799 for (Expression_list::const_iterator pv = this->vals_->begin();
10800 pv != this->vals_->end();
10804 && !(*pv)->is_constant()
10805 && (!(*pv)->is_composite_literal()
10806 || (*pv)->is_nonconstant_composite_literal()))
10810 const Struct_field_list* fields = this->type_->struct_type()->fields();
10811 for (Struct_field_list::const_iterator pf = fields->begin();
10812 pf != fields->end();
10815 // There are no constant constructors for interfaces.
10816 if (pf->type()->interface_type() != NULL)
10823 // Final type determination.
10826 Struct_construction_expression::do_determine_type(const Type_context*)
10828 if (this->vals_ == NULL)
10830 const Struct_field_list* fields = this->type_->struct_type()->fields();
10831 Expression_list::const_iterator pv = this->vals_->begin();
10832 for (Struct_field_list::const_iterator pf = fields->begin();
10833 pf != fields->end();
10836 if (pv == this->vals_->end())
10840 Type_context subcontext(pf->type(), false);
10841 (*pv)->determine_type(&subcontext);
10844 // Extra values are an error we will report elsewhere; we still want
10845 // to determine the type to avoid knockon errors.
10846 for (; pv != this->vals_->end(); ++pv)
10847 (*pv)->determine_type_no_context();
10853 Struct_construction_expression::do_check_types(Gogo*)
10855 if (this->vals_ == NULL)
10858 Struct_type* st = this->type_->struct_type();
10859 if (this->vals_->size() > st->field_count())
10861 this->report_error(_("too many expressions for struct"));
10865 const Struct_field_list* fields = st->fields();
10866 Expression_list::const_iterator pv = this->vals_->begin();
10868 for (Struct_field_list::const_iterator pf = fields->begin();
10869 pf != fields->end();
10872 if (pv == this->vals_->end())
10874 this->report_error(_("too few expressions for struct"));
10881 std::string reason;
10882 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
10884 if (reason.empty())
10885 error_at((*pv)->location(),
10886 "incompatible type for field %d in struct construction",
10889 error_at((*pv)->location(),
10890 ("incompatible type for field %d in "
10891 "struct construction (%s)"),
10892 i + 1, reason.c_str());
10893 this->set_is_error();
10896 gcc_assert(pv == this->vals_->end());
10899 // Return a tree for constructing a struct.
10902 Struct_construction_expression::do_get_tree(Translate_context* context)
10904 Gogo* gogo = context->gogo();
10906 if (this->vals_ == NULL)
10907 return this->type_->get_init_tree(gogo, false);
10909 tree type_tree = this->type_->get_tree(gogo);
10910 if (type_tree == error_mark_node)
10911 return error_mark_node;
10912 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10914 bool is_constant = true;
10915 const Struct_field_list* fields = this->type_->struct_type()->fields();
10916 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
10918 Struct_field_list::const_iterator pf = fields->begin();
10919 Expression_list::const_iterator pv = this->vals_->begin();
10920 for (tree field = TYPE_FIELDS(type_tree);
10921 field != NULL_TREE;
10922 field = DECL_CHAIN(field), ++pf)
10924 gcc_assert(pf != fields->end());
10927 if (pv == this->vals_->end())
10928 val = pf->type()->get_init_tree(gogo, false);
10929 else if (*pv == NULL)
10931 val = pf->type()->get_init_tree(gogo, false);
10936 val = Expression::convert_for_assignment(context, pf->type(),
10938 (*pv)->get_tree(context),
10943 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
10944 return error_mark_node;
10946 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
10947 elt->index = field;
10949 if (!TREE_CONSTANT(val))
10950 is_constant = false;
10952 gcc_assert(pf == fields->end());
10954 tree ret = build_constructor(type_tree, elts);
10956 TREE_CONSTANT(ret) = 1;
10960 // Export a struct construction.
10963 Struct_construction_expression::do_export(Export* exp) const
10965 exp->write_c_string("convert(");
10966 exp->write_type(this->type_);
10967 for (Expression_list::const_iterator pv = this->vals_->begin();
10968 pv != this->vals_->end();
10971 exp->write_c_string(", ");
10973 (*pv)->export_expression(exp);
10975 exp->write_c_string(")");
10978 // Make a struct composite literal. This used by the thunk code.
10981 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
10982 source_location location)
10984 gcc_assert(type->struct_type() != NULL);
10985 return new Struct_construction_expression(type, vals, location);
10988 // Construct an array. This class is not used directly; instead we
10989 // use the child classes, Fixed_array_construction_expression and
10990 // Open_array_construction_expression.
10992 class Array_construction_expression : public Expression
10995 Array_construction_expression(Expression_classification classification,
10996 Type* type, Expression_list* vals,
10997 source_location location)
10998 : Expression(classification, location),
10999 type_(type), vals_(vals)
11003 // Return whether this is a constant initializer.
11005 is_constant_array() const;
11007 // Return the number of elements.
11009 element_count() const
11010 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
11014 do_traverse(Traverse* traverse);
11018 { return this->type_; }
11021 do_determine_type(const Type_context*);
11024 do_check_types(Gogo*);
11027 do_is_addressable() const
11031 do_export(Export*) const;
11033 // The list of values.
11036 { return this->vals_; }
11038 // Get a constructor tree for the array values.
11040 get_constructor_tree(Translate_context* context, tree type_tree);
11043 // The type of the array to construct.
11045 // The list of values.
11046 Expression_list* vals_;
11052 Array_construction_expression::do_traverse(Traverse* traverse)
11054 if (this->vals_ != NULL
11055 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11056 return TRAVERSE_EXIT;
11057 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11058 return TRAVERSE_EXIT;
11059 return TRAVERSE_CONTINUE;
11062 // Return whether this is a constant initializer.
11065 Array_construction_expression::is_constant_array() const
11067 if (this->vals_ == NULL)
11070 // There are no constant constructors for interfaces.
11071 if (this->type_->array_type()->element_type()->interface_type() != NULL)
11074 for (Expression_list::const_iterator pv = this->vals_->begin();
11075 pv != this->vals_->end();
11079 && !(*pv)->is_constant()
11080 && (!(*pv)->is_composite_literal()
11081 || (*pv)->is_nonconstant_composite_literal()))
11087 // Final type determination.
11090 Array_construction_expression::do_determine_type(const Type_context*)
11092 if (this->vals_ == NULL)
11094 Type_context subcontext(this->type_->array_type()->element_type(), false);
11095 for (Expression_list::const_iterator pv = this->vals_->begin();
11096 pv != this->vals_->end();
11100 (*pv)->determine_type(&subcontext);
11107 Array_construction_expression::do_check_types(Gogo*)
11109 if (this->vals_ == NULL)
11112 Array_type* at = this->type_->array_type();
11114 Type* element_type = at->element_type();
11115 for (Expression_list::const_iterator pv = this->vals_->begin();
11116 pv != this->vals_->end();
11120 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
11122 error_at((*pv)->location(),
11123 "incompatible type for element %d in composite literal",
11125 this->set_is_error();
11129 Expression* length = at->length();
11130 if (length != NULL)
11135 if (at->length()->integer_constant_value(true, val, &type))
11137 if (this->vals_->size() > mpz_get_ui(val))
11138 this->report_error(_("too many elements in composite literal"));
11144 // Get a constructor tree for the array values.
11147 Array_construction_expression::get_constructor_tree(Translate_context* context,
11150 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11151 (this->vals_ == NULL
11153 : this->vals_->size()));
11154 Type* element_type = this->type_->array_type()->element_type();
11155 bool is_constant = true;
11156 if (this->vals_ != NULL)
11159 for (Expression_list::const_iterator pv = this->vals_->begin();
11160 pv != this->vals_->end();
11163 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
11164 elt->index = size_int(i);
11166 elt->value = element_type->get_init_tree(context->gogo(), false);
11169 tree value_tree = (*pv)->get_tree(context);
11170 elt->value = Expression::convert_for_assignment(context,
11176 if (elt->value == error_mark_node)
11177 return error_mark_node;
11178 if (!TREE_CONSTANT(elt->value))
11179 is_constant = false;
11183 tree ret = build_constructor(type_tree, values);
11185 TREE_CONSTANT(ret) = 1;
11189 // Export an array construction.
11192 Array_construction_expression::do_export(Export* exp) const
11194 exp->write_c_string("convert(");
11195 exp->write_type(this->type_);
11196 if (this->vals_ != NULL)
11198 for (Expression_list::const_iterator pv = this->vals_->begin();
11199 pv != this->vals_->end();
11202 exp->write_c_string(", ");
11204 (*pv)->export_expression(exp);
11207 exp->write_c_string(")");
11210 // Construct a fixed array.
11212 class Fixed_array_construction_expression :
11213 public Array_construction_expression
11216 Fixed_array_construction_expression(Type* type, Expression_list* vals,
11217 source_location location)
11218 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
11219 type, vals, location)
11221 gcc_assert(type->array_type() != NULL
11222 && type->array_type()->length() != NULL);
11229 return new Fixed_array_construction_expression(this->type(),
11230 (this->vals() == NULL
11232 : this->vals()->copy()),
11237 do_get_tree(Translate_context*);
11240 // Return a tree for constructing a fixed array.
11243 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
11245 return this->get_constructor_tree(context,
11246 this->type()->get_tree(context->gogo()));
11249 // Construct an open array.
11251 class Open_array_construction_expression : public Array_construction_expression
11254 Open_array_construction_expression(Type* type, Expression_list* vals,
11255 source_location location)
11256 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
11257 type, vals, location)
11259 gcc_assert(type->array_type() != NULL
11260 && type->array_type()->length() == NULL);
11264 // Note that taking the address of an open array literal is invalid.
11269 return new Open_array_construction_expression(this->type(),
11270 (this->vals() == NULL
11272 : this->vals()->copy()),
11277 do_get_tree(Translate_context*);
11280 // Return a tree for constructing an open array.
11283 Open_array_construction_expression::do_get_tree(Translate_context* context)
11285 Array_type* array_type = this->type()->array_type();
11286 if (array_type == NULL)
11288 gcc_assert(this->type()->is_error());
11289 return error_mark_node;
11292 Type* element_type = array_type->element_type();
11293 tree element_type_tree = element_type->get_tree(context->gogo());
11294 if (element_type_tree == error_mark_node)
11295 return error_mark_node;
11299 if (this->vals() == NULL || this->vals()->empty())
11301 // We need to create a unique value.
11302 tree max = size_int(0);
11303 tree constructor_type = build_array_type(element_type_tree,
11304 build_index_type(max));
11305 if (constructor_type == error_mark_node)
11306 return error_mark_node;
11307 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
11308 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
11309 elt->index = size_int(0);
11310 elt->value = element_type->get_init_tree(context->gogo(), false);
11311 values = build_constructor(constructor_type, vec);
11312 if (TREE_CONSTANT(elt->value))
11313 TREE_CONSTANT(values) = 1;
11314 length_tree = size_int(0);
11318 tree max = size_int(this->vals()->size() - 1);
11319 tree constructor_type = build_array_type(element_type_tree,
11320 build_index_type(max));
11321 if (constructor_type == error_mark_node)
11322 return error_mark_node;
11323 values = this->get_constructor_tree(context, constructor_type);
11324 length_tree = size_int(this->vals()->size());
11327 if (values == error_mark_node)
11328 return error_mark_node;
11330 bool is_constant_initializer = TREE_CONSTANT(values);
11332 // We have to copy the initial values into heap memory if we are in
11333 // a function or if the values are not constants. We also have to
11334 // copy them if they may contain pointers in a non-constant context,
11335 // as otherwise the garbage collector won't see them.
11336 bool copy_to_heap = (context->function() != NULL
11337 || !is_constant_initializer
11338 || (element_type->has_pointer()
11339 && !context->is_const()));
11341 if (is_constant_initializer)
11343 tree tmp = build_decl(this->location(), VAR_DECL,
11344 create_tmp_var_name("C"), TREE_TYPE(values));
11345 DECL_EXTERNAL(tmp) = 0;
11346 TREE_PUBLIC(tmp) = 0;
11347 TREE_STATIC(tmp) = 1;
11348 DECL_ARTIFICIAL(tmp) = 1;
11351 // If we are not copying the value to the heap, we will only
11352 // initialize the value once, so we can use this directly
11353 // rather than copying it. In that case we can't make it
11354 // read-only, because the program is permitted to change it.
11355 TREE_READONLY(tmp) = 1;
11356 TREE_CONSTANT(tmp) = 1;
11358 DECL_INITIAL(tmp) = values;
11359 rest_of_decl_compilation(tmp, 1, 0);
11367 // the initializer will only run once.
11368 space = build_fold_addr_expr(values);
11373 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
11374 space = context->gogo()->allocate_memory(element_type, memsize,
11376 space = save_expr(space);
11378 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
11379 tree ref = build_fold_indirect_ref_loc(this->location(), s);
11380 TREE_THIS_NOTRAP(ref) = 1;
11381 set = build2(MODIFY_EXPR, void_type_node, ref, values);
11384 // Build a constructor for the open array.
11386 tree type_tree = this->type()->get_tree(context->gogo());
11387 if (type_tree == error_mark_node)
11388 return error_mark_node;
11389 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11391 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
11393 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
11394 tree field = TYPE_FIELDS(type_tree);
11395 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
11396 elt->index = field;
11397 elt->value = fold_convert(TREE_TYPE(field), space);
11399 elt = VEC_quick_push(constructor_elt, init, NULL);
11400 field = DECL_CHAIN(field);
11401 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
11402 elt->index = field;
11403 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11405 elt = VEC_quick_push(constructor_elt, init, NULL);
11406 field = DECL_CHAIN(field);
11407 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
11408 elt->index = field;
11409 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11411 tree constructor = build_constructor(type_tree, init);
11412 if (constructor == error_mark_node)
11413 return error_mark_node;
11415 TREE_CONSTANT(constructor) = 1;
11417 if (set == NULL_TREE)
11418 return constructor;
11420 return build2(COMPOUND_EXPR, type_tree, set, constructor);
11423 // Make a slice composite literal. This is used by the type
11424 // descriptor code.
11427 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
11428 source_location location)
11430 gcc_assert(type->is_open_array_type());
11431 return new Open_array_construction_expression(type, vals, location);
11434 // Construct a map.
11436 class Map_construction_expression : public Expression
11439 Map_construction_expression(Type* type, Expression_list* vals,
11440 source_location location)
11441 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
11442 type_(type), vals_(vals)
11443 { gcc_assert(vals == NULL || vals->size() % 2 == 0); }
11447 do_traverse(Traverse* traverse);
11451 { return this->type_; }
11454 do_determine_type(const Type_context*);
11457 do_check_types(Gogo*);
11462 return new Map_construction_expression(this->type_, this->vals_->copy(),
11467 do_get_tree(Translate_context*);
11470 do_export(Export*) const;
11473 // The type of the map to construct.
11475 // The list of values.
11476 Expression_list* vals_;
11482 Map_construction_expression::do_traverse(Traverse* traverse)
11484 if (this->vals_ != NULL
11485 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11486 return TRAVERSE_EXIT;
11487 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11488 return TRAVERSE_EXIT;
11489 return TRAVERSE_CONTINUE;
11492 // Final type determination.
11495 Map_construction_expression::do_determine_type(const Type_context*)
11497 if (this->vals_ == NULL)
11500 Map_type* mt = this->type_->map_type();
11501 Type_context key_context(mt->key_type(), false);
11502 Type_context val_context(mt->val_type(), false);
11503 for (Expression_list::const_iterator pv = this->vals_->begin();
11504 pv != this->vals_->end();
11507 (*pv)->determine_type(&key_context);
11509 (*pv)->determine_type(&val_context);
11516 Map_construction_expression::do_check_types(Gogo*)
11518 if (this->vals_ == NULL)
11521 Map_type* mt = this->type_->map_type();
11523 Type* key_type = mt->key_type();
11524 Type* val_type = mt->val_type();
11525 for (Expression_list::const_iterator pv = this->vals_->begin();
11526 pv != this->vals_->end();
11529 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
11531 error_at((*pv)->location(),
11532 "incompatible type for element %d key in map construction",
11534 this->set_is_error();
11537 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
11539 error_at((*pv)->location(),
11540 ("incompatible type for element %d value "
11541 "in map construction"),
11543 this->set_is_error();
11548 // Return a tree for constructing a map.
11551 Map_construction_expression::do_get_tree(Translate_context* context)
11553 Gogo* gogo = context->gogo();
11554 source_location loc = this->location();
11556 Map_type* mt = this->type_->map_type();
11558 // Build a struct to hold the key and value.
11559 tree struct_type = make_node(RECORD_TYPE);
11561 Type* key_type = mt->key_type();
11562 tree id = get_identifier("__key");
11563 tree key_type_tree = key_type->get_tree(gogo);
11564 if (key_type_tree == error_mark_node)
11565 return error_mark_node;
11566 tree key_field = build_decl(loc, FIELD_DECL, id, key_type_tree);
11567 DECL_CONTEXT(key_field) = struct_type;
11568 TYPE_FIELDS(struct_type) = key_field;
11570 Type* val_type = mt->val_type();
11571 id = get_identifier("__val");
11572 tree val_type_tree = val_type->get_tree(gogo);
11573 if (val_type_tree == error_mark_node)
11574 return error_mark_node;
11575 tree val_field = build_decl(loc, FIELD_DECL, id, val_type_tree);
11576 DECL_CONTEXT(val_field) = struct_type;
11577 DECL_CHAIN(key_field) = val_field;
11579 layout_type(struct_type);
11581 bool is_constant = true;
11586 if (this->vals_ == NULL || this->vals_->empty())
11588 valaddr = null_pointer_node;
11589 make_tmp = NULL_TREE;
11593 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11594 this->vals_->size() / 2);
11596 for (Expression_list::const_iterator pv = this->vals_->begin();
11597 pv != this->vals_->end();
11600 bool one_is_constant = true;
11602 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
11604 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
11605 elt->index = key_field;
11606 tree val_tree = (*pv)->get_tree(context);
11607 elt->value = Expression::convert_for_assignment(context, key_type,
11610 if (elt->value == error_mark_node)
11611 return error_mark_node;
11612 if (!TREE_CONSTANT(elt->value))
11613 one_is_constant = false;
11617 elt = VEC_quick_push(constructor_elt, one, NULL);
11618 elt->index = val_field;
11619 val_tree = (*pv)->get_tree(context);
11620 elt->value = Expression::convert_for_assignment(context, val_type,
11623 if (elt->value == error_mark_node)
11624 return error_mark_node;
11625 if (!TREE_CONSTANT(elt->value))
11626 one_is_constant = false;
11628 elt = VEC_quick_push(constructor_elt, values, NULL);
11629 elt->index = size_int(i);
11630 elt->value = build_constructor(struct_type, one);
11631 if (one_is_constant)
11632 TREE_CONSTANT(elt->value) = 1;
11634 is_constant = false;
11637 tree index_type = build_index_type(size_int(i - 1));
11638 tree array_type = build_array_type(struct_type, index_type);
11639 tree init = build_constructor(array_type, values);
11641 TREE_CONSTANT(init) = 1;
11643 if (current_function_decl != NULL)
11645 tmp = create_tmp_var(array_type, get_name(array_type));
11646 DECL_INITIAL(tmp) = init;
11647 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
11648 TREE_ADDRESSABLE(tmp) = 1;
11652 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
11653 DECL_EXTERNAL(tmp) = 0;
11654 TREE_PUBLIC(tmp) = 0;
11655 TREE_STATIC(tmp) = 1;
11656 DECL_ARTIFICIAL(tmp) = 1;
11657 if (!TREE_CONSTANT(init))
11658 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
11662 TREE_READONLY(tmp) = 1;
11663 TREE_CONSTANT(tmp) = 1;
11664 DECL_INITIAL(tmp) = init;
11665 make_tmp = NULL_TREE;
11667 rest_of_decl_compilation(tmp, 1, 0);
11670 valaddr = build_fold_addr_expr(tmp);
11673 tree descriptor = gogo->map_descriptor(mt);
11675 tree type_tree = this->type_->get_tree(gogo);
11676 if (type_tree == error_mark_node)
11677 return error_mark_node;
11679 static tree construct_map_fndecl;
11680 tree call = Gogo::call_builtin(&construct_map_fndecl,
11682 "__go_construct_map",
11685 TREE_TYPE(descriptor),
11690 TYPE_SIZE_UNIT(struct_type),
11692 byte_position(val_field),
11694 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
11695 const_ptr_type_node,
11696 fold_convert(const_ptr_type_node, valaddr));
11697 if (call == error_mark_node)
11698 return error_mark_node;
11701 if (make_tmp == NULL)
11704 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
11708 // Export an array construction.
11711 Map_construction_expression::do_export(Export* exp) const
11713 exp->write_c_string("convert(");
11714 exp->write_type(this->type_);
11715 for (Expression_list::const_iterator pv = this->vals_->begin();
11716 pv != this->vals_->end();
11719 exp->write_c_string(", ");
11720 (*pv)->export_expression(exp);
11722 exp->write_c_string(")");
11725 // A general composite literal. This is lowered to a type specific
11728 class Composite_literal_expression : public Parser_expression
11731 Composite_literal_expression(Type* type, int depth, bool has_keys,
11732 Expression_list* vals, source_location location)
11733 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
11734 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
11739 do_traverse(Traverse* traverse);
11742 do_lower(Gogo*, Named_object*, int);
11747 return new Composite_literal_expression(this->type_, this->depth_,
11749 (this->vals_ == NULL
11751 : this->vals_->copy()),
11757 lower_struct(Gogo*, Type*);
11760 lower_array(Type*);
11763 make_array(Type*, Expression_list*);
11766 lower_map(Gogo*, Named_object*, Type*);
11768 // The type of the composite literal.
11770 // The depth within a list of composite literals within a composite
11771 // literal, when the type is omitted.
11773 // The values to put in the composite literal.
11774 Expression_list* vals_;
11775 // If this is true, then VALS_ is a list of pairs: a key and a
11776 // value. In an array initializer, a missing key will be NULL.
11783 Composite_literal_expression::do_traverse(Traverse* traverse)
11785 if (this->vals_ != NULL
11786 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11787 return TRAVERSE_EXIT;
11788 return Type::traverse(this->type_, traverse);
11791 // Lower a generic composite literal into a specific version based on
11795 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int)
11797 Type* type = this->type_;
11799 for (int depth = this->depth_; depth > 0; --depth)
11801 if (type->array_type() != NULL)
11802 type = type->array_type()->element_type();
11803 else if (type->map_type() != NULL)
11804 type = type->map_type()->val_type();
11807 if (!type->is_error())
11808 error_at(this->location(),
11809 ("may only omit types within composite literals "
11810 "of slice, array, or map type"));
11811 return Expression::make_error(this->location());
11815 if (type->is_error())
11816 return Expression::make_error(this->location());
11817 else if (type->struct_type() != NULL)
11818 return this->lower_struct(gogo, type);
11819 else if (type->array_type() != NULL)
11820 return this->lower_array(type);
11821 else if (type->map_type() != NULL)
11822 return this->lower_map(gogo, function, type);
11825 error_at(this->location(),
11826 ("expected struct, slice, array, or map type "
11827 "for composite literal"));
11828 return Expression::make_error(this->location());
11832 // Lower a struct composite literal.
11835 Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
11837 source_location location = this->location();
11838 Struct_type* st = type->struct_type();
11839 if (this->vals_ == NULL || !this->has_keys_)
11840 return new Struct_construction_expression(type, this->vals_, location);
11842 size_t field_count = st->field_count();
11843 std::vector<Expression*> vals(field_count);
11844 Expression_list::const_iterator p = this->vals_->begin();
11845 while (p != this->vals_->end())
11847 Expression* name_expr = *p;
11850 gcc_assert(p != this->vals_->end());
11851 Expression* val = *p;
11855 if (name_expr == NULL)
11857 error_at(val->location(), "mixture of field and value initializers");
11858 return Expression::make_error(location);
11861 bool bad_key = false;
11863 const Named_object* no = NULL;
11864 switch (name_expr->classification())
11866 case EXPRESSION_UNKNOWN_REFERENCE:
11867 name = name_expr->unknown_expression()->name();
11870 case EXPRESSION_CONST_REFERENCE:
11871 no = static_cast<Const_expression*>(name_expr)->named_object();
11874 case EXPRESSION_TYPE:
11876 Type* t = name_expr->type();
11877 Named_type* nt = t->named_type();
11881 no = nt->named_object();
11885 case EXPRESSION_VAR_REFERENCE:
11886 no = name_expr->var_expression()->named_object();
11889 case EXPRESSION_FUNC_REFERENCE:
11890 no = name_expr->func_expression()->named_object();
11893 case EXPRESSION_UNARY:
11894 // If there is a local variable around with the same name as
11895 // the field, and this occurs in the closure, then the
11896 // parser may turn the field reference into an indirection
11897 // through the closure. FIXME: This is a mess.
11900 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
11901 if (ue->op() == OPERATOR_MULT)
11903 Field_reference_expression* fre =
11904 ue->operand()->field_reference_expression();
11908 fre->expr()->type()->deref()->struct_type();
11911 const Struct_field* sf = st->field(fre->field_index());
11912 name = sf->field_name();
11914 snprintf(buf, sizeof buf, "%u", fre->field_index());
11915 size_t buflen = strlen(buf);
11916 if (name.compare(name.length() - buflen, buflen, buf)
11919 name = name.substr(0, name.length() - buflen);
11934 error_at(name_expr->location(), "expected struct field name");
11935 return Expression::make_error(location);
11942 // A predefined name won't be packed. If it starts with a
11943 // lower case letter we need to check for that case, because
11944 // the field name will be packed.
11945 if (!Gogo::is_hidden_name(name)
11949 Named_object* gno = gogo->lookup_global(name.c_str());
11951 name = gogo->pack_hidden_name(name, false);
11955 unsigned int index;
11956 const Struct_field* sf = st->find_local_field(name, &index);
11959 error_at(name_expr->location(), "unknown field %qs in %qs",
11960 Gogo::message_name(name).c_str(),
11961 (type->named_type() != NULL
11962 ? type->named_type()->message_name().c_str()
11963 : "unnamed struct"));
11964 return Expression::make_error(location);
11966 if (vals[index] != NULL)
11968 error_at(name_expr->location(),
11969 "duplicate value for field %qs in %qs",
11970 Gogo::message_name(name).c_str(),
11971 (type->named_type() != NULL
11972 ? type->named_type()->message_name().c_str()
11973 : "unnamed struct"));
11974 return Expression::make_error(location);
11980 Expression_list* list = new Expression_list;
11981 list->reserve(field_count);
11982 for (size_t i = 0; i < field_count; ++i)
11983 list->push_back(vals[i]);
11985 return new Struct_construction_expression(type, list, location);
11988 // Lower an array composite literal.
11991 Composite_literal_expression::lower_array(Type* type)
11993 source_location location = this->location();
11994 if (this->vals_ == NULL || !this->has_keys_)
11995 return this->make_array(type, this->vals_);
11997 std::vector<Expression*> vals;
11998 vals.reserve(this->vals_->size());
11999 unsigned long index = 0;
12000 Expression_list::const_iterator p = this->vals_->begin();
12001 while (p != this->vals_->end())
12003 Expression* index_expr = *p;
12006 gcc_assert(p != this->vals_->end());
12007 Expression* val = *p;
12011 if (index_expr != NULL)
12017 if (!index_expr->integer_constant_value(true, ival, &dummy))
12020 error_at(index_expr->location(),
12021 "index expression is not integer constant");
12022 return Expression::make_error(location);
12025 if (mpz_sgn(ival) < 0)
12028 error_at(index_expr->location(), "index expression is negative");
12029 return Expression::make_error(location);
12032 index = mpz_get_ui(ival);
12033 if (mpz_cmp_ui(ival, index) != 0)
12036 error_at(index_expr->location(), "index value overflow");
12037 return Expression::make_error(location);
12040 Named_type* ntype = Type::lookup_integer_type("int");
12041 Integer_type* inttype = ntype->integer_type();
12043 mpz_init_set_ui(max, 1);
12044 mpz_mul_2exp(max, max, inttype->bits() - 1);
12045 bool ok = mpz_cmp(ival, max) < 0;
12050 error_at(index_expr->location(), "index value overflow");
12051 return Expression::make_error(location);
12056 // FIXME: Our representation isn't very good; this avoids
12058 if (index > 0x1000000)
12060 error_at(index_expr->location(), "index too large for compiler");
12061 return Expression::make_error(location);
12065 if (index == vals.size())
12066 vals.push_back(val);
12069 if (index > vals.size())
12071 vals.reserve(index + 32);
12072 vals.resize(index + 1, static_cast<Expression*>(NULL));
12074 if (vals[index] != NULL)
12076 error_at((index_expr != NULL
12077 ? index_expr->location()
12078 : val->location()),
12079 "duplicate value for index %lu",
12081 return Expression::make_error(location);
12089 size_t size = vals.size();
12090 Expression_list* list = new Expression_list;
12091 list->reserve(size);
12092 for (size_t i = 0; i < size; ++i)
12093 list->push_back(vals[i]);
12095 return this->make_array(type, list);
12098 // Actually build the array composite literal. This handles
12102 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
12104 source_location location = this->location();
12105 Array_type* at = type->array_type();
12106 if (at->length() != NULL && at->length()->is_nil_expression())
12108 size_t size = vals == NULL ? 0 : vals->size();
12110 mpz_init_set_ui(vlen, size);
12111 Expression* elen = Expression::make_integer(&vlen, NULL, location);
12113 at = Type::make_array_type(at->element_type(), elen);
12116 if (at->length() != NULL)
12117 return new Fixed_array_construction_expression(type, vals, location);
12119 return new Open_array_construction_expression(type, vals, location);
12122 // Lower a map composite literal.
12125 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
12128 source_location location = this->location();
12129 if (this->vals_ != NULL)
12131 if (!this->has_keys_)
12133 error_at(location, "map composite literal must have keys");
12134 return Expression::make_error(location);
12137 for (Expression_list::iterator p = this->vals_->begin();
12138 p != this->vals_->end();
12144 error_at((*p)->location(),
12145 "map composite literal must have keys for every value");
12146 return Expression::make_error(location);
12148 // Make sure we have lowered the key; it may not have been
12149 // lowered in order to handle keys for struct composite
12150 // literals. Lower it now to get the right error message.
12151 if ((*p)->unknown_expression() != NULL)
12153 (*p)->unknown_expression()->clear_is_composite_literal_key();
12154 gogo->lower_expression(function, &*p);
12155 gcc_assert((*p)->is_error_expression());
12156 return Expression::make_error(location);
12161 return new Map_construction_expression(type, this->vals_, location);
12164 // Make a composite literal expression.
12167 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
12168 Expression_list* vals,
12169 source_location location)
12171 return new Composite_literal_expression(type, depth, has_keys, vals,
12175 // Return whether this expression is a composite literal.
12178 Expression::is_composite_literal() const
12180 switch (this->classification_)
12182 case EXPRESSION_COMPOSITE_LITERAL:
12183 case EXPRESSION_STRUCT_CONSTRUCTION:
12184 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12185 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12186 case EXPRESSION_MAP_CONSTRUCTION:
12193 // Return whether this expression is a composite literal which is not
12197 Expression::is_nonconstant_composite_literal() const
12199 switch (this->classification_)
12201 case EXPRESSION_STRUCT_CONSTRUCTION:
12203 const Struct_construction_expression *psce =
12204 static_cast<const Struct_construction_expression*>(this);
12205 return !psce->is_constant_struct();
12207 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12209 const Fixed_array_construction_expression *pace =
12210 static_cast<const Fixed_array_construction_expression*>(this);
12211 return !pace->is_constant_array();
12213 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12215 const Open_array_construction_expression *pace =
12216 static_cast<const Open_array_construction_expression*>(this);
12217 return !pace->is_constant_array();
12219 case EXPRESSION_MAP_CONSTRUCTION:
12226 // Return true if this is a reference to a local variable.
12229 Expression::is_local_variable() const
12231 const Var_expression* ve = this->var_expression();
12234 const Named_object* no = ve->named_object();
12235 return (no->is_result_variable()
12236 || (no->is_variable() && !no->var_value()->is_global()));
12239 // Class Type_guard_expression.
12244 Type_guard_expression::do_traverse(Traverse* traverse)
12246 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
12247 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12248 return TRAVERSE_EXIT;
12249 return TRAVERSE_CONTINUE;
12252 // Check types of a type guard expression. The expression must have
12253 // an interface type, but the actual type conversion is checked at run
12257 Type_guard_expression::do_check_types(Gogo*)
12259 // 6g permits using a type guard with unsafe.pointer; we are
12261 Type* expr_type = this->expr_->type();
12262 if (expr_type->is_unsafe_pointer_type())
12264 if (this->type_->points_to() == NULL
12265 && (this->type_->integer_type() == NULL
12266 || (this->type_->forwarded()
12267 != Type::lookup_integer_type("uintptr"))))
12268 this->report_error(_("invalid unsafe.Pointer conversion"));
12270 else if (this->type_->is_unsafe_pointer_type())
12272 if (expr_type->points_to() == NULL
12273 && (expr_type->integer_type() == NULL
12274 || (expr_type->forwarded()
12275 != Type::lookup_integer_type("uintptr"))))
12276 this->report_error(_("invalid unsafe.Pointer conversion"));
12278 else if (expr_type->interface_type() == NULL)
12280 if (!expr_type->is_error() && !this->type_->is_error())
12281 this->report_error(_("type assertion only valid for interface types"));
12282 this->set_is_error();
12284 else if (this->type_->interface_type() == NULL)
12286 std::string reason;
12287 if (!expr_type->interface_type()->implements_interface(this->type_,
12290 if (!this->type_->is_error())
12292 if (reason.empty())
12293 this->report_error(_("impossible type assertion: "
12294 "type does not implement interface"));
12296 error_at(this->location(),
12297 ("impossible type assertion: "
12298 "type does not implement interface (%s)"),
12301 this->set_is_error();
12306 // Return a tree for a type guard expression.
12309 Type_guard_expression::do_get_tree(Translate_context* context)
12311 Gogo* gogo = context->gogo();
12312 tree expr_tree = this->expr_->get_tree(context);
12313 if (expr_tree == error_mark_node)
12314 return error_mark_node;
12315 Type* expr_type = this->expr_->type();
12316 if ((this->type_->is_unsafe_pointer_type()
12317 && (expr_type->points_to() != NULL
12318 || expr_type->integer_type() != NULL))
12319 || (expr_type->is_unsafe_pointer_type()
12320 && this->type_->points_to() != NULL))
12321 return convert_to_pointer(this->type_->get_tree(gogo), expr_tree);
12322 else if (expr_type->is_unsafe_pointer_type()
12323 && this->type_->integer_type() != NULL)
12324 return convert_to_integer(this->type_->get_tree(gogo), expr_tree);
12325 else if (this->type_->interface_type() != NULL)
12326 return Expression::convert_interface_to_interface(context, this->type_,
12327 this->expr_->type(),
12331 return Expression::convert_for_assignment(context, this->type_,
12332 this->expr_->type(), expr_tree,
12336 // Make a type guard expression.
12339 Expression::make_type_guard(Expression* expr, Type* type,
12340 source_location location)
12342 return new Type_guard_expression(expr, type, location);
12345 // Class Heap_composite_expression.
12347 // When you take the address of a composite literal, it is allocated
12348 // on the heap. This class implements that.
12350 class Heap_composite_expression : public Expression
12353 Heap_composite_expression(Expression* expr, source_location location)
12354 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
12360 do_traverse(Traverse* traverse)
12361 { return Expression::traverse(&this->expr_, traverse); }
12365 { return Type::make_pointer_type(this->expr_->type()); }
12368 do_determine_type(const Type_context*)
12369 { this->expr_->determine_type_no_context(); }
12374 return Expression::make_heap_composite(this->expr_->copy(),
12379 do_get_tree(Translate_context*);
12381 // We only export global objects, and the parser does not generate
12382 // this in global scope.
12384 do_export(Export*) const
12385 { gcc_unreachable(); }
12388 // The composite literal which is being put on the heap.
12392 // Return a tree which allocates a composite literal on the heap.
12395 Heap_composite_expression::do_get_tree(Translate_context* context)
12397 tree expr_tree = this->expr_->get_tree(context);
12398 if (expr_tree == error_mark_node)
12399 return error_mark_node;
12400 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
12401 gcc_assert(TREE_CODE(expr_size) == INTEGER_CST);
12402 tree space = context->gogo()->allocate_memory(this->expr_->type(),
12403 expr_size, this->location());
12404 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
12405 space = save_expr(space);
12406 tree ref = build_fold_indirect_ref_loc(this->location(), space);
12407 TREE_THIS_NOTRAP(ref) = 1;
12408 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
12409 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
12411 SET_EXPR_LOCATION(ret, this->location());
12415 // Allocate a composite literal on the heap.
12418 Expression::make_heap_composite(Expression* expr, source_location location)
12420 return new Heap_composite_expression(expr, location);
12423 // Class Receive_expression.
12425 // Return the type of a receive expression.
12428 Receive_expression::do_type()
12430 Channel_type* channel_type = this->channel_->type()->channel_type();
12431 if (channel_type == NULL)
12432 return Type::make_error_type();
12433 return channel_type->element_type();
12436 // Check types for a receive expression.
12439 Receive_expression::do_check_types(Gogo*)
12441 Type* type = this->channel_->type();
12442 if (type->is_error())
12444 this->set_is_error();
12447 if (type->channel_type() == NULL)
12449 this->report_error(_("expected channel"));
12452 if (!type->channel_type()->may_receive())
12454 this->report_error(_("invalid receive on send-only channel"));
12459 // Get a tree for a receive expression.
12462 Receive_expression::do_get_tree(Translate_context* context)
12464 Channel_type* channel_type = this->channel_->type()->channel_type();
12465 if (channel_type == NULL)
12467 gcc_assert(this->channel_->type()->is_error());
12468 return error_mark_node;
12470 Type* element_type = channel_type->element_type();
12471 tree element_type_tree = element_type->get_tree(context->gogo());
12473 tree channel = this->channel_->get_tree(context);
12474 if (element_type_tree == error_mark_node || channel == error_mark_node)
12475 return error_mark_node;
12477 return Gogo::receive_from_channel(element_type_tree, channel,
12478 this->for_select_, this->location());
12481 // Make a receive expression.
12483 Receive_expression*
12484 Expression::make_receive(Expression* channel, source_location location)
12486 return new Receive_expression(channel, location);
12489 // An expression which evaluates to a pointer to the type descriptor
12492 class Type_descriptor_expression : public Expression
12495 Type_descriptor_expression(Type* type, source_location location)
12496 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
12503 { return Type::make_type_descriptor_ptr_type(); }
12506 do_determine_type(const Type_context*)
12514 do_get_tree(Translate_context* context)
12515 { return this->type_->type_descriptor_pointer(context->gogo()); }
12518 // The type for which this is the descriptor.
12522 // Make a type descriptor expression.
12525 Expression::make_type_descriptor(Type* type, source_location location)
12527 return new Type_descriptor_expression(type, location);
12530 // An expression which evaluates to some characteristic of a type.
12531 // This is only used to initialize fields of a type descriptor. Using
12532 // a new expression class is slightly inefficient but gives us a good
12533 // separation between the frontend and the middle-end with regard to
12534 // how types are laid out.
12536 class Type_info_expression : public Expression
12539 Type_info_expression(Type* type, Type_info type_info)
12540 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
12541 type_(type), type_info_(type_info)
12549 do_determine_type(const Type_context*)
12557 do_get_tree(Translate_context* context);
12560 // The type for which we are getting information.
12562 // What information we want.
12563 Type_info type_info_;
12566 // The type is chosen to match what the type descriptor struct
12570 Type_info_expression::do_type()
12572 switch (this->type_info_)
12574 case TYPE_INFO_SIZE:
12575 return Type::lookup_integer_type("uintptr");
12576 case TYPE_INFO_ALIGNMENT:
12577 case TYPE_INFO_FIELD_ALIGNMENT:
12578 return Type::lookup_integer_type("uint8");
12584 // Return type information in GENERIC.
12587 Type_info_expression::do_get_tree(Translate_context* context)
12589 tree type_tree = this->type_->get_tree(context->gogo());
12590 if (type_tree == error_mark_node)
12591 return error_mark_node;
12593 tree val_type_tree = this->type()->get_tree(context->gogo());
12594 gcc_assert(val_type_tree != error_mark_node);
12596 if (this->type_info_ == TYPE_INFO_SIZE)
12597 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12598 TYPE_SIZE_UNIT(type_tree));
12602 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
12603 val = go_type_alignment(type_tree);
12605 val = go_field_alignment(type_tree);
12606 return build_int_cstu(val_type_tree, val);
12610 // Make a type info expression.
12613 Expression::make_type_info(Type* type, Type_info type_info)
12615 return new Type_info_expression(type, type_info);
12618 // An expression which evaluates to the offset of a field within a
12619 // struct. This, like Type_info_expression, q.v., is only used to
12620 // initialize fields of a type descriptor.
12622 class Struct_field_offset_expression : public Expression
12625 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
12626 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
12627 type_(type), field_(field)
12633 { return Type::lookup_integer_type("uintptr"); }
12636 do_determine_type(const Type_context*)
12644 do_get_tree(Translate_context* context);
12647 // The type of the struct.
12648 Struct_type* type_;
12650 const Struct_field* field_;
12653 // Return a struct field offset in GENERIC.
12656 Struct_field_offset_expression::do_get_tree(Translate_context* context)
12658 tree type_tree = this->type_->get_tree(context->gogo());
12659 if (type_tree == error_mark_node)
12660 return error_mark_node;
12662 tree val_type_tree = this->type()->get_tree(context->gogo());
12663 gcc_assert(val_type_tree != error_mark_node);
12665 const Struct_field_list* fields = this->type_->fields();
12666 tree struct_field_tree = TYPE_FIELDS(type_tree);
12667 Struct_field_list::const_iterator p;
12668 for (p = fields->begin();
12669 p != fields->end();
12670 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
12672 gcc_assert(struct_field_tree != NULL_TREE);
12673 if (&*p == this->field_)
12676 gcc_assert(&*p == this->field_);
12678 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12679 byte_position(struct_field_tree));
12682 // Make an expression for a struct field offset.
12685 Expression::make_struct_field_offset(Struct_type* type,
12686 const Struct_field* field)
12688 return new Struct_field_offset_expression(type, field);
12691 // An expression which evaluates to the address of an unnamed label.
12693 class Label_addr_expression : public Expression
12696 Label_addr_expression(Label* label, source_location location)
12697 : Expression(EXPRESSION_LABEL_ADDR, location),
12704 { return Type::make_pointer_type(Type::make_void_type()); }
12707 do_determine_type(const Type_context*)
12712 { return new Label_addr_expression(this->label_, this->location()); }
12715 do_get_tree(Translate_context* context)
12717 return expr_to_tree(this->label_->get_addr(context, this->location()));
12721 // The label whose address we are taking.
12725 // Make an expression for the address of an unnamed label.
12728 Expression::make_label_addr(Label* label, source_location location)
12730 return new Label_addr_expression(label, location);
12733 // Import an expression. This comes at the end in order to see the
12734 // various class definitions.
12737 Expression::import_expression(Import* imp)
12739 int c = imp->peek_char();
12740 if (imp->match_c_string("- ")
12741 || imp->match_c_string("! ")
12742 || imp->match_c_string("^ "))
12743 return Unary_expression::do_import(imp);
12745 return Binary_expression::do_import(imp);
12746 else if (imp->match_c_string("true")
12747 || imp->match_c_string("false"))
12748 return Boolean_expression::do_import(imp);
12750 return String_expression::do_import(imp);
12751 else if (c == '-' || (c >= '0' && c <= '9'))
12753 // This handles integers, floats and complex constants.
12754 return Integer_expression::do_import(imp);
12756 else if (imp->match_c_string("nil"))
12757 return Nil_expression::do_import(imp);
12758 else if (imp->match_c_string("convert"))
12759 return Type_conversion_expression::do_import(imp);
12762 error_at(imp->location(), "import error: expected expression");
12763 return Expression::make_error(imp->location());
12767 // Class Expression_list.
12769 // Traverse the list.
12772 Expression_list::traverse(Traverse* traverse)
12774 for (Expression_list::iterator p = this->begin();
12780 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12781 return TRAVERSE_EXIT;
12784 return TRAVERSE_CONTINUE;
12790 Expression_list::copy()
12792 Expression_list* ret = new Expression_list();
12793 for (Expression_list::iterator p = this->begin();
12798 ret->push_back(NULL);
12800 ret->push_back((*p)->copy());
12805 // Return whether an expression list has an error expression.
12808 Expression_list::contains_error() const
12810 for (Expression_list::const_iterator p = this->begin();
12813 if (*p != NULL && (*p)->is_error_expression())