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 = type_to_tree(lhs_type->get_backend(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 go_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 go_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 go_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 go_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 go_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 go_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 go_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 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
326 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(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 go_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 go_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 go_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 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
423 "__type_descriptor") == 0);
427 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
429 go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
431 tree v1 = build_fold_indirect_ref_loc(location, v);
432 go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
433 tree f = TYPE_FIELDS(TREE_TYPE(v1));
434 go_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 = type_to_tree(lhs_type->get_backend(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 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
514 "__type_descriptor") == 0);
515 go_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 go_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 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
549 tree rhs_type_tree = TREE_TYPE(rhs_tree);
550 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
551 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
552 go_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 = type_to_tree(lhs_type->get_backend(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 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
608 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
609 go_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 { go_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 go_assert(saw_errors());
906 return Type::make_error_type();
909 // Class Var_expression.
911 // Lower a variable expression. Here we just make sure that the
912 // initialization expression of the variable has been lowered. This
913 // ensures that we will be able to determine the type of the variable
917 Var_expression::do_lower(Gogo* gogo, Named_object* function, int)
919 if (this->variable_->is_variable())
921 Variable* var = this->variable_->var_value();
922 // This is either a local variable or a global variable. A
923 // reference to a variable which is local to an enclosing
924 // function will be a reference to a field in a closure.
925 if (var->is_global())
927 var->lower_init_expression(gogo, function);
932 // Return the type of a reference to a variable.
935 Var_expression::do_type()
937 if (this->variable_->is_variable())
938 return this->variable_->var_value()->type();
939 else if (this->variable_->is_result_variable())
940 return this->variable_->result_var_value()->type();
945 // Determine the type of a reference to a variable.
948 Var_expression::do_determine_type(const Type_context*)
950 if (this->variable_->is_variable())
951 this->variable_->var_value()->determine_type();
954 // Something takes the address of this variable. This means that we
955 // may want to move the variable onto the heap.
958 Var_expression::do_address_taken(bool escapes)
962 else if (this->variable_->is_variable())
963 this->variable_->var_value()->set_address_taken();
964 else if (this->variable_->is_result_variable())
965 this->variable_->result_var_value()->set_address_taken();
970 // Get the tree for a reference to a variable.
973 Var_expression::do_get_tree(Translate_context* context)
975 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
976 context->function());
977 tree ret = var_to_tree(bvar);
978 if (ret == error_mark_node)
979 return error_mark_node;
981 if (this->variable_->is_variable())
982 is_in_heap = this->variable_->var_value()->is_in_heap();
983 else if (this->variable_->is_result_variable())
984 is_in_heap = this->variable_->result_var_value()->is_in_heap();
989 ret = build_fold_indirect_ref_loc(this->location(), ret);
990 TREE_THIS_NOTRAP(ret) = 1;
995 // Make a reference to a variable in an expression.
998 Expression::make_var_reference(Named_object* var, source_location location)
1001 return Expression::make_sink(location);
1003 // FIXME: Creating a new object for each reference to a variable is
1005 return new Var_expression(var, location);
1008 // Class Temporary_reference_expression.
1013 Temporary_reference_expression::do_type()
1015 return this->statement_->type();
1018 // Called if something takes the address of this temporary variable.
1019 // We never have to move temporary variables to the heap, but we do
1020 // need to know that they must live in the stack rather than in a
1024 Temporary_reference_expression::do_address_taken(bool)
1026 this->statement_->set_is_address_taken();
1029 // Get a tree referring to the variable.
1032 Temporary_reference_expression::do_get_tree(Translate_context* context)
1034 Bvariable* bvar = this->statement_->get_backend_variable(context);
1036 // The gcc backend can't represent the same set of recursive types
1037 // that the Go frontend can. In some cases this means that a
1038 // temporary variable won't have the right backend type. Correct
1039 // that here by adding a type cast. We need to use base() to push
1040 // the circularity down one level.
1041 tree ret = var_to_tree(bvar);
1042 if (POINTER_TYPE_P(TREE_TYPE(ret)) && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
1044 Btype* type_btype = this->type()->base()->get_backend(context->gogo());
1045 tree type_tree = type_to_tree(type_btype);
1046 ret = fold_convert_loc(this->location(), type_tree, ret);
1051 // Make a reference to a temporary variable.
1054 Expression::make_temporary_reference(Temporary_statement* statement,
1055 source_location location)
1057 return new Temporary_reference_expression(statement, location);
1060 // A sink expression--a use of the blank identifier _.
1062 class Sink_expression : public Expression
1065 Sink_expression(source_location location)
1066 : Expression(EXPRESSION_SINK, location),
1067 type_(NULL), var_(NULL_TREE)
1072 do_discarding_value()
1079 do_determine_type(const Type_context*);
1083 { return new Sink_expression(this->location()); }
1086 do_get_tree(Translate_context*);
1089 // The type of this sink variable.
1091 // The temporary variable we generate.
1095 // Return the type of a sink expression.
1098 Sink_expression::do_type()
1100 if (this->type_ == NULL)
1101 return Type::make_sink_type();
1105 // Determine the type of a sink expression.
1108 Sink_expression::do_determine_type(const Type_context* context)
1110 if (context->type != NULL)
1111 this->type_ = context->type;
1114 // Return a temporary variable for a sink expression. This will
1115 // presumably be a write-only variable which the middle-end will drop.
1118 Sink_expression::do_get_tree(Translate_context* context)
1120 if (this->var_ == NULL_TREE)
1122 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
1123 Btype* bt = this->type_->get_backend(context->gogo());
1124 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
1129 // Make a sink expression.
1132 Expression::make_sink(source_location location)
1134 return new Sink_expression(location);
1137 // Class Func_expression.
1139 // FIXME: Can a function expression appear in a constant expression?
1140 // The value is unchanging. Initializing a constant to the address of
1141 // a function seems like it could work, though there might be little
1147 Func_expression::do_traverse(Traverse* traverse)
1149 return (this->closure_ == NULL
1151 : Expression::traverse(&this->closure_, traverse));
1154 // Return the type of a function expression.
1157 Func_expression::do_type()
1159 if (this->function_->is_function())
1160 return this->function_->func_value()->type();
1161 else if (this->function_->is_function_declaration())
1162 return this->function_->func_declaration_value()->type();
1167 // Get the tree for a function expression without evaluating the
1171 Func_expression::get_tree_without_closure(Gogo* gogo)
1173 Function_type* fntype;
1174 if (this->function_->is_function())
1175 fntype = this->function_->func_value()->type();
1176 else if (this->function_->is_function_declaration())
1177 fntype = this->function_->func_declaration_value()->type();
1181 // Builtin functions are handled specially by Call_expression. We
1182 // can't take their address.
1183 if (fntype->is_builtin())
1185 error_at(this->location(), "invalid use of special builtin function %qs",
1186 this->function_->name().c_str());
1187 return error_mark_node;
1190 Named_object* no = this->function_;
1192 tree id = no->get_id(gogo);
1193 if (id == error_mark_node)
1194 return error_mark_node;
1197 if (no->is_function())
1198 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1199 else if (no->is_function_declaration())
1200 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1204 if (fndecl == error_mark_node)
1205 return error_mark_node;
1207 return build_fold_addr_expr_loc(this->location(), fndecl);
1210 // Get the tree for a function expression. This is used when we take
1211 // the address of a function rather than simply calling it. If the
1212 // function has a closure, we must use a trampoline.
1215 Func_expression::do_get_tree(Translate_context* context)
1217 Gogo* gogo = context->gogo();
1219 tree fnaddr = this->get_tree_without_closure(gogo);
1220 if (fnaddr == error_mark_node)
1221 return error_mark_node;
1223 go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1224 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1225 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1227 // For a normal non-nested function call, that is all we have to do.
1228 if (!this->function_->is_function()
1229 || this->function_->func_value()->enclosing() == NULL)
1231 go_assert(this->closure_ == NULL);
1235 // For a nested function call, we have to always allocate a
1236 // trampoline. If we don't always allocate, then closures will not
1237 // be reliably distinct.
1238 Expression* closure = this->closure_;
1240 if (closure == NULL)
1241 closure_tree = null_pointer_node;
1244 // Get the value of the closure. This will be a pointer to
1245 // space allocated on the heap.
1246 closure_tree = closure->get_tree(context);
1247 if (closure_tree == error_mark_node)
1248 return error_mark_node;
1249 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1252 // Now we need to build some code on the heap. This code will load
1253 // the static chain pointer with the closure and then jump to the
1254 // body of the function. The normal gcc approach is to build the
1255 // code on the stack. Unfortunately we can not do that, as Go
1256 // permits us to return the function pointer.
1258 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1261 // Make a reference to a function in an expression.
1264 Expression::make_func_reference(Named_object* function, Expression* closure,
1265 source_location location)
1267 return new Func_expression(function, closure, location);
1270 // Class Unknown_expression.
1272 // Return the name of an unknown expression.
1275 Unknown_expression::name() const
1277 return this->named_object_->name();
1280 // Lower a reference to an unknown name.
1283 Unknown_expression::do_lower(Gogo*, Named_object*, int)
1285 source_location location = this->location();
1286 Named_object* no = this->named_object_;
1288 if (!no->is_unknown())
1292 real = no->unknown_value()->real_named_object();
1295 if (this->is_composite_literal_key_)
1297 error_at(location, "reference to undefined name %qs",
1298 this->named_object_->message_name().c_str());
1299 return Expression::make_error(location);
1302 switch (real->classification())
1304 case Named_object::NAMED_OBJECT_CONST:
1305 return Expression::make_const_reference(real, location);
1306 case Named_object::NAMED_OBJECT_TYPE:
1307 return Expression::make_type(real->type_value(), location);
1308 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1309 if (this->is_composite_literal_key_)
1311 error_at(location, "reference to undefined type %qs",
1312 real->message_name().c_str());
1313 return Expression::make_error(location);
1314 case Named_object::NAMED_OBJECT_VAR:
1315 return Expression::make_var_reference(real, location);
1316 case Named_object::NAMED_OBJECT_FUNC:
1317 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1318 return Expression::make_func_reference(real, NULL, location);
1319 case Named_object::NAMED_OBJECT_PACKAGE:
1320 if (this->is_composite_literal_key_)
1322 error_at(location, "unexpected reference to package");
1323 return Expression::make_error(location);
1329 // Make a reference to an unknown name.
1332 Expression::make_unknown_reference(Named_object* no, source_location location)
1334 go_assert(no->resolve()->is_unknown());
1335 return new Unknown_expression(no, location);
1338 // A boolean expression.
1340 class Boolean_expression : public Expression
1343 Boolean_expression(bool val, source_location location)
1344 : Expression(EXPRESSION_BOOLEAN, location),
1345 val_(val), type_(NULL)
1353 do_is_constant() const
1360 do_determine_type(const Type_context*);
1367 do_get_tree(Translate_context*)
1368 { return this->val_ ? boolean_true_node : boolean_false_node; }
1371 do_export(Export* exp) const
1372 { exp->write_c_string(this->val_ ? "true" : "false"); }
1377 // The type as determined by context.
1384 Boolean_expression::do_type()
1386 if (this->type_ == NULL)
1387 this->type_ = Type::make_boolean_type();
1391 // Set the type from the context.
1394 Boolean_expression::do_determine_type(const Type_context* context)
1396 if (this->type_ != NULL && !this->type_->is_abstract())
1398 else if (context->type != NULL && context->type->is_boolean_type())
1399 this->type_ = context->type;
1400 else if (!context->may_be_abstract)
1401 this->type_ = Type::lookup_bool_type();
1404 // Import a boolean constant.
1407 Boolean_expression::do_import(Import* imp)
1409 if (imp->peek_char() == 't')
1411 imp->require_c_string("true");
1412 return Expression::make_boolean(true, imp->location());
1416 imp->require_c_string("false");
1417 return Expression::make_boolean(false, imp->location());
1421 // Make a boolean expression.
1424 Expression::make_boolean(bool val, source_location location)
1426 return new Boolean_expression(val, location);
1429 // Class String_expression.
1434 String_expression::do_type()
1436 if (this->type_ == NULL)
1437 this->type_ = Type::make_string_type();
1441 // Set the type from the context.
1444 String_expression::do_determine_type(const Type_context* context)
1446 if (this->type_ != NULL && !this->type_->is_abstract())
1448 else if (context->type != NULL && context->type->is_string_type())
1449 this->type_ = context->type;
1450 else if (!context->may_be_abstract)
1451 this->type_ = Type::lookup_string_type();
1454 // Build a string constant.
1457 String_expression::do_get_tree(Translate_context* context)
1459 return context->gogo()->go_string_constant_tree(this->val_);
1462 // Export a string expression.
1465 String_expression::do_export(Export* exp) const
1468 s.reserve(this->val_.length() * 4 + 2);
1470 for (std::string::const_iterator p = this->val_.begin();
1471 p != this->val_.end();
1474 if (*p == '\\' || *p == '"')
1479 else if (*p >= 0x20 && *p < 0x7f)
1481 else if (*p == '\n')
1483 else if (*p == '\t')
1488 unsigned char c = *p;
1489 unsigned int dig = c >> 4;
1490 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1492 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1496 exp->write_string(s);
1499 // Import a string expression.
1502 String_expression::do_import(Import* imp)
1504 imp->require_c_string("\"");
1508 int c = imp->get_char();
1509 if (c == '"' || c == -1)
1512 val += static_cast<char>(c);
1515 c = imp->get_char();
1516 if (c == '\\' || c == '"')
1517 val += static_cast<char>(c);
1524 c = imp->get_char();
1525 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1526 c = imp->get_char();
1527 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1528 char v = (vh << 4) | vl;
1533 error_at(imp->location(), "bad string constant");
1534 return Expression::make_error(imp->location());
1538 return Expression::make_string(val, imp->location());
1541 // Make a string expression.
1544 Expression::make_string(const std::string& val, source_location location)
1546 return new String_expression(val, location);
1549 // Make an integer expression.
1551 class Integer_expression : public Expression
1554 Integer_expression(const mpz_t* val, Type* type, source_location location)
1555 : Expression(EXPRESSION_INTEGER, location),
1557 { mpz_init_set(this->val_, *val); }
1562 // Return whether VAL fits in the type.
1564 check_constant(mpz_t val, Type*, source_location);
1566 // Write VAL to export data.
1568 export_integer(Export* exp, const mpz_t val);
1572 do_is_constant() const
1576 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1582 do_determine_type(const Type_context* context);
1585 do_check_types(Gogo*);
1588 do_get_tree(Translate_context*);
1592 { return Expression::make_integer(&this->val_, this->type_,
1593 this->location()); }
1596 do_export(Export*) const;
1599 // The integer value.
1605 // Return an integer constant value.
1608 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1611 if (this->type_ != NULL)
1612 *ptype = this->type_;
1613 mpz_set(val, this->val_);
1617 // Return the current type. If we haven't set the type yet, we return
1618 // an abstract integer type.
1621 Integer_expression::do_type()
1623 if (this->type_ == NULL)
1624 this->type_ = Type::make_abstract_integer_type();
1628 // Set the type of the integer value. Here we may switch from an
1629 // abstract type to a real type.
1632 Integer_expression::do_determine_type(const Type_context* context)
1634 if (this->type_ != NULL && !this->type_->is_abstract())
1636 else if (context->type != NULL
1637 && (context->type->integer_type() != NULL
1638 || context->type->float_type() != NULL
1639 || context->type->complex_type() != NULL))
1640 this->type_ = context->type;
1641 else if (!context->may_be_abstract)
1642 this->type_ = Type::lookup_integer_type("int");
1645 // Return true if the integer VAL fits in the range of the type TYPE.
1646 // Otherwise give an error and return false. TYPE may be NULL.
1649 Integer_expression::check_constant(mpz_t val, Type* type,
1650 source_location location)
1654 Integer_type* itype = type->integer_type();
1655 if (itype == NULL || itype->is_abstract())
1658 int bits = mpz_sizeinbase(val, 2);
1660 if (itype->is_unsigned())
1662 // For an unsigned type we can only accept a nonnegative number,
1663 // and we must be able to represent at least BITS.
1664 if (mpz_sgn(val) >= 0
1665 && bits <= itype->bits())
1670 // For a signed type we need an extra bit to indicate the sign.
1671 // We have to handle the most negative integer specially.
1672 if (bits + 1 <= itype->bits()
1673 || (bits <= itype->bits()
1675 && (mpz_scan1(val, 0)
1676 == static_cast<unsigned long>(itype->bits() - 1))
1677 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1681 error_at(location, "integer constant overflow");
1685 // Check the type of an integer constant.
1688 Integer_expression::do_check_types(Gogo*)
1690 if (this->type_ == NULL)
1692 if (!Integer_expression::check_constant(this->val_, this->type_,
1694 this->set_is_error();
1697 // Get a tree for an integer constant.
1700 Integer_expression::do_get_tree(Translate_context* context)
1702 Gogo* gogo = context->gogo();
1704 if (this->type_ != NULL && !this->type_->is_abstract())
1705 type = type_to_tree(this->type_->get_backend(gogo));
1706 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1708 // We are converting to an abstract floating point type.
1709 Type* ftype = Type::lookup_float_type("float64");
1710 type = type_to_tree(ftype->get_backend(gogo));
1712 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1714 // We are converting to an abstract complex type.
1715 Type* ctype = Type::lookup_complex_type("complex128");
1716 type = type_to_tree(ctype->get_backend(gogo));
1720 // If we still have an abstract type here, then this is being
1721 // used in a constant expression which didn't get reduced for
1722 // some reason. Use a type which will fit the value. We use <,
1723 // not <=, because we need an extra bit for the sign bit.
1724 int bits = mpz_sizeinbase(this->val_, 2);
1725 if (bits < INT_TYPE_SIZE)
1727 Type* t = Type::lookup_integer_type("int");
1728 type = type_to_tree(t->get_backend(gogo));
1732 Type* t = Type::lookup_integer_type("int64");
1733 type = type_to_tree(t->get_backend(gogo));
1736 type = long_long_integer_type_node;
1738 return Expression::integer_constant_tree(this->val_, type);
1741 // Write VAL to export data.
1744 Integer_expression::export_integer(Export* exp, const mpz_t val)
1746 char* s = mpz_get_str(NULL, 10, val);
1747 exp->write_c_string(s);
1751 // Export an integer in a constant expression.
1754 Integer_expression::do_export(Export* exp) const
1756 Integer_expression::export_integer(exp, this->val_);
1757 // A trailing space lets us reliably identify the end of the number.
1758 exp->write_c_string(" ");
1761 // Import an integer, floating point, or complex value. This handles
1762 // all these types because they all start with digits.
1765 Integer_expression::do_import(Import* imp)
1767 std::string num = imp->read_identifier();
1768 imp->require_c_string(" ");
1769 if (!num.empty() && num[num.length() - 1] == 'i')
1772 size_t plus_pos = num.find('+', 1);
1773 size_t minus_pos = num.find('-', 1);
1775 if (plus_pos == std::string::npos)
1777 else if (minus_pos == std::string::npos)
1781 error_at(imp->location(), "bad number in import data: %qs",
1783 return Expression::make_error(imp->location());
1785 if (pos == std::string::npos)
1786 mpfr_set_ui(real, 0, GMP_RNDN);
1789 std::string real_str = num.substr(0, pos);
1790 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1792 error_at(imp->location(), "bad number in import data: %qs",
1794 return Expression::make_error(imp->location());
1798 std::string imag_str;
1799 if (pos == std::string::npos)
1802 imag_str = num.substr(pos);
1803 imag_str = imag_str.substr(0, imag_str.size() - 1);
1805 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1807 error_at(imp->location(), "bad number in import data: %qs",
1809 return Expression::make_error(imp->location());
1811 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1817 else if (num.find('.') == std::string::npos
1818 && num.find('E') == std::string::npos)
1821 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1823 error_at(imp->location(), "bad number in import data: %qs",
1825 return Expression::make_error(imp->location());
1827 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1834 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1836 error_at(imp->location(), "bad number in import data: %qs",
1838 return Expression::make_error(imp->location());
1840 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1846 // Build a new integer value.
1849 Expression::make_integer(const mpz_t* val, Type* type,
1850 source_location location)
1852 return new Integer_expression(val, type, location);
1857 class Float_expression : public Expression
1860 Float_expression(const mpfr_t* val, Type* type, source_location location)
1861 : Expression(EXPRESSION_FLOAT, location),
1864 mpfr_init_set(this->val_, *val, GMP_RNDN);
1867 // Constrain VAL to fit into TYPE.
1869 constrain_float(mpfr_t val, Type* type);
1871 // Return whether VAL fits in the type.
1873 check_constant(mpfr_t val, Type*, source_location);
1875 // Write VAL to export data.
1877 export_float(Export* exp, const mpfr_t val);
1881 do_is_constant() const
1885 do_float_constant_value(mpfr_t val, Type**) const;
1891 do_determine_type(const Type_context*);
1894 do_check_types(Gogo*);
1898 { return Expression::make_float(&this->val_, this->type_,
1899 this->location()); }
1902 do_get_tree(Translate_context*);
1905 do_export(Export*) const;
1908 // The floating point value.
1914 // Constrain VAL to fit into TYPE.
1917 Float_expression::constrain_float(mpfr_t val, Type* type)
1919 Float_type* ftype = type->float_type();
1920 if (ftype != NULL && !ftype->is_abstract())
1921 mpfr_prec_round(val, ftype->bits(), GMP_RNDN);
1924 // Return a floating point constant value.
1927 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
1929 if (this->type_ != NULL)
1930 *ptype = this->type_;
1931 mpfr_set(val, this->val_, GMP_RNDN);
1935 // Return the current type. If we haven't set the type yet, we return
1936 // an abstract float type.
1939 Float_expression::do_type()
1941 if (this->type_ == NULL)
1942 this->type_ = Type::make_abstract_float_type();
1946 // Set the type of the float value. Here we may switch from an
1947 // abstract type to a real type.
1950 Float_expression::do_determine_type(const Type_context* context)
1952 if (this->type_ != NULL && !this->type_->is_abstract())
1954 else if (context->type != NULL
1955 && (context->type->integer_type() != NULL
1956 || context->type->float_type() != NULL
1957 || context->type->complex_type() != NULL))
1958 this->type_ = context->type;
1959 else if (!context->may_be_abstract)
1960 this->type_ = Type::lookup_float_type("float64");
1963 // Return true if the floating point value VAL fits in the range of
1964 // the type TYPE. Otherwise give an error and return false. TYPE may
1968 Float_expression::check_constant(mpfr_t val, Type* type,
1969 source_location location)
1973 Float_type* ftype = type->float_type();
1974 if (ftype == NULL || ftype->is_abstract())
1977 // A NaN or Infinity always fits in the range of the type.
1978 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
1981 mp_exp_t exp = mpfr_get_exp(val);
1983 switch (ftype->bits())
1996 error_at(location, "floating point constant overflow");
2002 // Check the type of a float value.
2005 Float_expression::do_check_types(Gogo*)
2007 if (this->type_ == NULL)
2010 if (!Float_expression::check_constant(this->val_, this->type_,
2012 this->set_is_error();
2014 Integer_type* integer_type = this->type_->integer_type();
2015 if (integer_type != NULL)
2017 if (!mpfr_integer_p(this->val_))
2018 this->report_error(_("floating point constant truncated to integer"));
2021 go_assert(!integer_type->is_abstract());
2024 mpfr_get_z(ival, this->val_, GMP_RNDN);
2025 Integer_expression::check_constant(ival, integer_type,
2032 // Get a tree for a float constant.
2035 Float_expression::do_get_tree(Translate_context* context)
2037 Gogo* gogo = context->gogo();
2039 if (this->type_ != NULL && !this->type_->is_abstract())
2040 type = type_to_tree(this->type_->get_backend(gogo));
2041 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2043 // We have an abstract integer type. We just hope for the best.
2044 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
2048 // If we still have an abstract type here, then this is being
2049 // used in a constant expression which didn't get reduced. We
2050 // just use float64 and hope for the best.
2051 Type* ft = Type::lookup_float_type("float64");
2052 type = type_to_tree(ft->get_backend(gogo));
2054 return Expression::float_constant_tree(this->val_, type);
2057 // Write a floating point number to export data.
2060 Float_expression::export_float(Export *exp, const mpfr_t val)
2063 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2065 exp->write_c_string("-");
2066 exp->write_c_string("0.");
2067 exp->write_c_string(*s == '-' ? s + 1 : s);
2070 snprintf(buf, sizeof buf, "E%ld", exponent);
2071 exp->write_c_string(buf);
2074 // Export a floating point number in a constant expression.
2077 Float_expression::do_export(Export* exp) const
2079 Float_expression::export_float(exp, this->val_);
2080 // A trailing space lets us reliably identify the end of the number.
2081 exp->write_c_string(" ");
2084 // Make a float expression.
2087 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2089 return new Float_expression(val, type, location);
2094 class Complex_expression : public Expression
2097 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2098 source_location location)
2099 : Expression(EXPRESSION_COMPLEX, location),
2102 mpfr_init_set(this->real_, *real, GMP_RNDN);
2103 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2106 // Constrain REAL/IMAG to fit into TYPE.
2108 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2110 // Return whether REAL/IMAG fits in the type.
2112 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2114 // Write REAL/IMAG to export data.
2116 export_complex(Export* exp, const mpfr_t real, const mpfr_t val);
2120 do_is_constant() const
2124 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2130 do_determine_type(const Type_context*);
2133 do_check_types(Gogo*);
2138 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2143 do_get_tree(Translate_context*);
2146 do_export(Export*) const;
2151 // The imaginary part;
2153 // The type if known.
2157 // Constrain REAL/IMAG to fit into TYPE.
2160 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2162 Complex_type* ctype = type->complex_type();
2163 if (ctype != NULL && !ctype->is_abstract())
2165 mpfr_prec_round(real, ctype->bits() / 2, GMP_RNDN);
2166 mpfr_prec_round(imag, ctype->bits() / 2, GMP_RNDN);
2170 // Return a complex constant value.
2173 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2176 if (this->type_ != NULL)
2177 *ptype = this->type_;
2178 mpfr_set(real, this->real_, GMP_RNDN);
2179 mpfr_set(imag, this->imag_, GMP_RNDN);
2183 // Return the current type. If we haven't set the type yet, we return
2184 // an abstract complex type.
2187 Complex_expression::do_type()
2189 if (this->type_ == NULL)
2190 this->type_ = Type::make_abstract_complex_type();
2194 // Set the type of the complex value. Here we may switch from an
2195 // abstract type to a real type.
2198 Complex_expression::do_determine_type(const Type_context* context)
2200 if (this->type_ != NULL && !this->type_->is_abstract())
2202 else if (context->type != NULL
2203 && context->type->complex_type() != NULL)
2204 this->type_ = context->type;
2205 else if (!context->may_be_abstract)
2206 this->type_ = Type::lookup_complex_type("complex128");
2209 // Return true if the complex value REAL/IMAG fits in the range of the
2210 // type TYPE. Otherwise give an error and return false. TYPE may be
2214 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2215 source_location location)
2219 Complex_type* ctype = type->complex_type();
2220 if (ctype == NULL || ctype->is_abstract())
2224 switch (ctype->bits())
2236 // A NaN or Infinity always fits in the range of the type.
2237 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2239 if (mpfr_get_exp(real) > max_exp)
2241 error_at(location, "complex real part constant overflow");
2246 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2248 if (mpfr_get_exp(imag) > max_exp)
2250 error_at(location, "complex imaginary part constant overflow");
2258 // Check the type of a complex value.
2261 Complex_expression::do_check_types(Gogo*)
2263 if (this->type_ == NULL)
2266 if (!Complex_expression::check_constant(this->real_, this->imag_,
2267 this->type_, this->location()))
2268 this->set_is_error();
2271 // Get a tree for a complex constant.
2274 Complex_expression::do_get_tree(Translate_context* context)
2276 Gogo* gogo = context->gogo();
2278 if (this->type_ != NULL && !this->type_->is_abstract())
2279 type = type_to_tree(this->type_->get_backend(gogo));
2282 // If we still have an abstract type here, this this is being
2283 // used in a constant expression which didn't get reduced. We
2284 // just use complex128 and hope for the best.
2285 Type* ct = Type::lookup_complex_type("complex128");
2286 type = type_to_tree(ct->get_backend(gogo));
2288 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2291 // Write REAL/IMAG to export data.
2294 Complex_expression::export_complex(Export* exp, const mpfr_t real,
2297 if (!mpfr_zero_p(real))
2299 Float_expression::export_float(exp, real);
2300 if (mpfr_sgn(imag) > 0)
2301 exp->write_c_string("+");
2303 Float_expression::export_float(exp, imag);
2304 exp->write_c_string("i");
2307 // Export a complex number in a constant expression.
2310 Complex_expression::do_export(Export* exp) const
2312 Complex_expression::export_complex(exp, this->real_, this->imag_);
2313 // A trailing space lets us reliably identify the end of the number.
2314 exp->write_c_string(" ");
2317 // Make a complex expression.
2320 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2321 source_location location)
2323 return new Complex_expression(real, imag, type, location);
2326 // Find a named object in an expression.
2328 class Find_named_object : public Traverse
2331 Find_named_object(Named_object* no)
2332 : Traverse(traverse_expressions),
2333 no_(no), found_(false)
2336 // Whether we found the object.
2339 { return this->found_; }
2343 expression(Expression**);
2346 // The object we are looking for.
2348 // Whether we found it.
2352 // A reference to a const in an expression.
2354 class Const_expression : public Expression
2357 Const_expression(Named_object* constant, source_location location)
2358 : Expression(EXPRESSION_CONST_REFERENCE, location),
2359 constant_(constant), type_(NULL), seen_(false)
2364 { return this->constant_; }
2366 // Check that the initializer does not refer to the constant itself.
2368 check_for_init_loop();
2372 do_traverse(Traverse*);
2375 do_lower(Gogo*, Named_object*, int);
2378 do_is_constant() const
2382 do_integer_constant_value(bool, mpz_t val, Type**) const;
2385 do_float_constant_value(mpfr_t val, Type**) const;
2388 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2391 do_string_constant_value(std::string* val) const
2392 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2397 // The type of a const is set by the declaration, not the use.
2399 do_determine_type(const Type_context*);
2402 do_check_types(Gogo*);
2409 do_get_tree(Translate_context* context);
2411 // When exporting a reference to a const as part of a const
2412 // expression, we export the value. We ignore the fact that it has
2415 do_export(Export* exp) const
2416 { this->constant_->const_value()->expr()->export_expression(exp); }
2420 Named_object* constant_;
2421 // The type of this reference. This is used if the constant has an
2424 // Used to prevent infinite recursion when a constant incorrectly
2425 // refers to itself.
2432 Const_expression::do_traverse(Traverse* traverse)
2434 if (this->type_ != NULL)
2435 return Type::traverse(this->type_, traverse);
2436 return TRAVERSE_CONTINUE;
2439 // Lower a constant expression. This is where we convert the
2440 // predeclared constant iota into an integer value.
2443 Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value)
2445 if (this->constant_->const_value()->expr()->classification()
2448 if (iota_value == -1)
2450 error_at(this->location(),
2451 "iota is only defined in const declarations");
2455 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2456 Expression* ret = Expression::make_integer(&val, NULL,
2462 // Make sure that the constant itself has been lowered.
2463 gogo->lower_constant(this->constant_);
2468 // Return an integer constant value.
2471 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2478 if (this->type_ != NULL)
2479 ctype = this->type_;
2481 ctype = this->constant_->const_value()->type();
2482 if (ctype != NULL && ctype->integer_type() == NULL)
2485 Expression* e = this->constant_->const_value()->expr();
2490 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2492 this->seen_ = false;
2496 && !Integer_expression::check_constant(val, ctype, this->location()))
2499 *ptype = ctype != NULL ? ctype : t;
2503 // Return a floating point constant value.
2506 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2512 if (this->type_ != NULL)
2513 ctype = this->type_;
2515 ctype = this->constant_->const_value()->type();
2516 if (ctype != NULL && ctype->float_type() == NULL)
2522 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2525 this->seen_ = false;
2527 if (r && ctype != NULL)
2529 if (!Float_expression::check_constant(val, ctype, this->location()))
2531 Float_expression::constrain_float(val, ctype);
2533 *ptype = ctype != NULL ? ctype : t;
2537 // Return a complex constant value.
2540 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2547 if (this->type_ != NULL)
2548 ctype = this->type_;
2550 ctype = this->constant_->const_value()->type();
2551 if (ctype != NULL && ctype->complex_type() == NULL)
2557 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2561 this->seen_ = false;
2563 if (r && ctype != NULL)
2565 if (!Complex_expression::check_constant(real, imag, ctype,
2568 Complex_expression::constrain_complex(real, imag, ctype);
2570 *ptype = ctype != NULL ? ctype : t;
2574 // Return the type of the const reference.
2577 Const_expression::do_type()
2579 if (this->type_ != NULL)
2582 Named_constant* nc = this->constant_->const_value();
2584 if (this->seen_ || nc->lowering())
2586 this->report_error(_("constant refers to itself"));
2587 this->type_ = Type::make_error_type();
2593 Type* ret = nc->type();
2597 this->seen_ = false;
2601 // During parsing, a named constant may have a NULL type, but we
2602 // must not return a NULL type here.
2603 ret = nc->expr()->type();
2605 this->seen_ = false;
2610 // Set the type of the const reference.
2613 Const_expression::do_determine_type(const Type_context* context)
2615 Type* ctype = this->constant_->const_value()->type();
2616 Type* cetype = (ctype != NULL
2618 : this->constant_->const_value()->expr()->type());
2619 if (ctype != NULL && !ctype->is_abstract())
2621 else if (context->type != NULL
2622 && (context->type->integer_type() != NULL
2623 || context->type->float_type() != NULL
2624 || context->type->complex_type() != NULL)
2625 && (cetype->integer_type() != NULL
2626 || cetype->float_type() != NULL
2627 || cetype->complex_type() != NULL))
2628 this->type_ = context->type;
2629 else if (context->type != NULL
2630 && context->type->is_string_type()
2631 && cetype->is_string_type())
2632 this->type_ = context->type;
2633 else if (context->type != NULL
2634 && context->type->is_boolean_type()
2635 && cetype->is_boolean_type())
2636 this->type_ = context->type;
2637 else if (!context->may_be_abstract)
2639 if (cetype->is_abstract())
2640 cetype = cetype->make_non_abstract_type();
2641 this->type_ = cetype;
2645 // Check for a loop in which the initializer of a constant refers to
2646 // the constant itself.
2649 Const_expression::check_for_init_loop()
2651 if (this->type_ != NULL && this->type_->is_error())
2656 this->report_error(_("constant refers to itself"));
2657 this->type_ = Type::make_error_type();
2661 Expression* init = this->constant_->const_value()->expr();
2662 Find_named_object find_named_object(this->constant_);
2665 Expression::traverse(&init, &find_named_object);
2666 this->seen_ = false;
2668 if (find_named_object.found())
2670 if (this->type_ == NULL || !this->type_->is_error())
2672 this->report_error(_("constant refers to itself"));
2673 this->type_ = Type::make_error_type();
2679 // Check types of a const reference.
2682 Const_expression::do_check_types(Gogo*)
2684 if (this->type_ != NULL && this->type_->is_error())
2687 this->check_for_init_loop();
2689 if (this->type_ == NULL || this->type_->is_abstract())
2692 // Check for integer overflow.
2693 if (this->type_->integer_type() != NULL)
2698 if (!this->integer_constant_value(true, ival, &dummy))
2702 Expression* cexpr = this->constant_->const_value()->expr();
2703 if (cexpr->float_constant_value(fval, &dummy))
2705 if (!mpfr_integer_p(fval))
2706 this->report_error(_("floating point constant "
2707 "truncated to integer"));
2710 mpfr_get_z(ival, fval, GMP_RNDN);
2711 Integer_expression::check_constant(ival, this->type_,
2721 // Return a tree for the const reference.
2724 Const_expression::do_get_tree(Translate_context* context)
2726 Gogo* gogo = context->gogo();
2728 if (this->type_ == NULL)
2729 type_tree = NULL_TREE;
2732 type_tree = type_to_tree(this->type_->get_backend(gogo));
2733 if (type_tree == error_mark_node)
2734 return error_mark_node;
2737 // If the type has been set for this expression, but the underlying
2738 // object is an abstract int or float, we try to get the abstract
2739 // value. Otherwise we may lose something in the conversion.
2740 if (this->type_ != NULL
2741 && (this->constant_->const_value()->type() == NULL
2742 || this->constant_->const_value()->type()->is_abstract()))
2744 Expression* expr = this->constant_->const_value()->expr();
2748 if (expr->integer_constant_value(true, ival, &t))
2750 tree ret = Expression::integer_constant_tree(ival, type_tree);
2758 if (expr->float_constant_value(fval, &t))
2760 tree ret = Expression::float_constant_tree(fval, type_tree);
2767 if (expr->complex_constant_value(fval, imag, &t))
2769 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2778 tree const_tree = this->constant_->get_tree(gogo, context->function());
2779 if (this->type_ == NULL
2780 || const_tree == error_mark_node
2781 || TREE_TYPE(const_tree) == error_mark_node)
2785 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2786 ret = fold_convert(type_tree, const_tree);
2787 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2788 ret = fold(convert_to_integer(type_tree, const_tree));
2789 else if (TREE_CODE(type_tree) == REAL_TYPE)
2790 ret = fold(convert_to_real(type_tree, const_tree));
2791 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2792 ret = fold(convert_to_complex(type_tree, const_tree));
2798 // Make a reference to a constant in an expression.
2801 Expression::make_const_reference(Named_object* constant,
2802 source_location location)
2804 return new Const_expression(constant, location);
2807 // Find a named object in an expression.
2810 Find_named_object::expression(Expression** pexpr)
2812 switch ((*pexpr)->classification())
2814 case Expression::EXPRESSION_CONST_REFERENCE:
2816 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2817 if (ce->named_object() == this->no_)
2820 // We need to check a constant initializer explicitly, as
2821 // loops here will not be caught by the loop checking for
2822 // variable initializers.
2823 ce->check_for_init_loop();
2825 return TRAVERSE_CONTINUE;
2828 case Expression::EXPRESSION_VAR_REFERENCE:
2829 if ((*pexpr)->var_expression()->named_object() == this->no_)
2831 return TRAVERSE_CONTINUE;
2832 case Expression::EXPRESSION_FUNC_REFERENCE:
2833 if ((*pexpr)->func_expression()->named_object() == this->no_)
2835 return TRAVERSE_CONTINUE;
2837 return TRAVERSE_CONTINUE;
2839 this->found_ = true;
2840 return TRAVERSE_EXIT;
2845 class Nil_expression : public Expression
2848 Nil_expression(source_location location)
2849 : Expression(EXPRESSION_NIL, location)
2857 do_is_constant() const
2862 { return Type::make_nil_type(); }
2865 do_determine_type(const Type_context*)
2873 do_get_tree(Translate_context*)
2874 { return null_pointer_node; }
2877 do_export(Export* exp) const
2878 { exp->write_c_string("nil"); }
2881 // Import a nil expression.
2884 Nil_expression::do_import(Import* imp)
2886 imp->require_c_string("nil");
2887 return Expression::make_nil(imp->location());
2890 // Make a nil expression.
2893 Expression::make_nil(source_location location)
2895 return new Nil_expression(location);
2898 // The value of the predeclared constant iota. This is little more
2899 // than a marker. This will be lowered to an integer in
2900 // Const_expression::do_lower, which is where we know the value that
2903 class Iota_expression : public Parser_expression
2906 Iota_expression(source_location location)
2907 : Parser_expression(EXPRESSION_IOTA, location)
2912 do_lower(Gogo*, Named_object*, int)
2913 { go_unreachable(); }
2915 // There should only ever be one of these.
2918 { go_unreachable(); }
2921 // Make an iota expression. This is only called for one case: the
2922 // value of the predeclared constant iota.
2925 Expression::make_iota()
2927 static Iota_expression iota_expression(UNKNOWN_LOCATION);
2928 return &iota_expression;
2931 // A type conversion expression.
2933 class Type_conversion_expression : public Expression
2936 Type_conversion_expression(Type* type, Expression* expr,
2937 source_location location)
2938 : Expression(EXPRESSION_CONVERSION, location),
2939 type_(type), expr_(expr), may_convert_function_types_(false)
2942 // Return the type to which we are converting.
2945 { return this->type_; }
2947 // Return the expression which we are converting.
2950 { return this->expr_; }
2952 // Permit converting from one function type to another. This is
2953 // used internally for method expressions.
2955 set_may_convert_function_types()
2957 this->may_convert_function_types_ = true;
2960 // Import a type conversion expression.
2966 do_traverse(Traverse* traverse);
2969 do_lower(Gogo*, Named_object*, int);
2972 do_is_constant() const
2973 { return this->expr_->is_constant(); }
2976 do_integer_constant_value(bool, mpz_t, Type**) const;
2979 do_float_constant_value(mpfr_t, Type**) const;
2982 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
2985 do_string_constant_value(std::string*) const;
2989 { return this->type_; }
2992 do_determine_type(const Type_context*)
2994 Type_context subcontext(this->type_, false);
2995 this->expr_->determine_type(&subcontext);
2999 do_check_types(Gogo*);
3004 return new Type_conversion_expression(this->type_, this->expr_->copy(),
3009 do_get_tree(Translate_context* context);
3012 do_export(Export*) const;
3015 // The type to convert to.
3017 // The expression to convert.
3019 // True if this is permitted to convert function types. This is
3020 // used internally for method expressions.
3021 bool may_convert_function_types_;
3027 Type_conversion_expression::do_traverse(Traverse* traverse)
3029 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3030 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3031 return TRAVERSE_EXIT;
3032 return TRAVERSE_CONTINUE;
3035 // Convert to a constant at lowering time.
3038 Type_conversion_expression::do_lower(Gogo*, Named_object*, int)
3040 Type* type = this->type_;
3041 Expression* val = this->expr_;
3042 source_location location = this->location();
3044 if (type->integer_type() != NULL)
3049 if (val->integer_constant_value(false, ival, &dummy))
3051 if (!Integer_expression::check_constant(ival, type, location))
3052 mpz_set_ui(ival, 0);
3053 Expression* ret = Expression::make_integer(&ival, type, location);
3060 if (val->float_constant_value(fval, &dummy))
3062 if (!mpfr_integer_p(fval))
3065 "floating point constant truncated to integer");
3066 return Expression::make_error(location);
3068 mpfr_get_z(ival, fval, GMP_RNDN);
3069 if (!Integer_expression::check_constant(ival, type, location))
3070 mpz_set_ui(ival, 0);
3071 Expression* ret = Expression::make_integer(&ival, type, location);
3080 if (type->float_type() != NULL)
3085 if (val->float_constant_value(fval, &dummy))
3087 if (!Float_expression::check_constant(fval, type, location))
3088 mpfr_set_ui(fval, 0, GMP_RNDN);
3089 Float_expression::constrain_float(fval, type);
3090 Expression *ret = Expression::make_float(&fval, type, location);
3097 if (type->complex_type() != NULL)
3104 if (val->complex_constant_value(real, imag, &dummy))
3106 if (!Complex_expression::check_constant(real, imag, type, location))
3108 mpfr_set_ui(real, 0, GMP_RNDN);
3109 mpfr_set_ui(imag, 0, GMP_RNDN);
3111 Complex_expression::constrain_complex(real, imag, type);
3112 Expression* ret = Expression::make_complex(&real, &imag, type,
3122 if (type->is_open_array_type() && type->named_type() == NULL)
3124 Type* element_type = type->array_type()->element_type()->forwarded();
3125 bool is_byte = element_type == Type::lookup_integer_type("uint8");
3126 bool is_int = element_type == Type::lookup_integer_type("int");
3127 if (is_byte || is_int)
3130 if (val->string_constant_value(&s))
3132 Expression_list* vals = new Expression_list();
3135 for (std::string::const_iterator p = s.begin();
3140 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3141 Expression* v = Expression::make_integer(&val,
3150 const char *p = s.data();
3151 const char *pend = s.data() + s.length();
3155 int adv = Lex::fetch_char(p, &c);
3158 warning_at(this->location(), 0,
3159 "invalid UTF-8 encoding");
3164 mpz_init_set_ui(val, c);
3165 Expression* v = Expression::make_integer(&val,
3173 return Expression::make_slice_composite_literal(type, vals,
3182 // Return the constant integer value if there is one.
3185 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3189 if (this->type_->integer_type() == NULL)
3195 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3197 if (!Integer_expression::check_constant(ival, this->type_,
3205 *ptype = this->type_;
3212 if (this->expr_->float_constant_value(fval, &dummy))
3214 mpfr_get_z(val, fval, GMP_RNDN);
3216 if (!Integer_expression::check_constant(val, this->type_,
3219 *ptype = this->type_;
3227 // Return the constant floating point value if there is one.
3230 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3233 if (this->type_->float_type() == NULL)
3239 if (this->expr_->float_constant_value(fval, &dummy))
3241 if (!Float_expression::check_constant(fval, this->type_,
3247 mpfr_set(val, fval, GMP_RNDN);
3249 Float_expression::constrain_float(val, this->type_);
3250 *ptype = this->type_;
3258 // Return the constant complex value if there is one.
3261 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3265 if (this->type_->complex_type() == NULL)
3273 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3275 if (!Complex_expression::check_constant(rval, ival, this->type_,
3282 mpfr_set(real, rval, GMP_RNDN);
3283 mpfr_set(imag, ival, GMP_RNDN);
3286 Complex_expression::constrain_complex(real, imag, this->type_);
3287 *ptype = this->type_;
3296 // Return the constant string value if there is one.
3299 Type_conversion_expression::do_string_constant_value(std::string* val) const
3301 if (this->type_->is_string_type()
3302 && this->expr_->type()->integer_type() != NULL)
3307 if (this->expr_->integer_constant_value(false, ival, &dummy))
3309 unsigned long ulval = mpz_get_ui(ival);
3310 if (mpz_cmp_ui(ival, ulval) == 0)
3312 Lex::append_char(ulval, true, val, this->location());
3320 // FIXME: Could handle conversion from const []int here.
3325 // Check that types are convertible.
3328 Type_conversion_expression::do_check_types(Gogo*)
3330 Type* type = this->type_;
3331 Type* expr_type = this->expr_->type();
3334 if (type->is_error() || expr_type->is_error())
3336 this->set_is_error();
3340 if (this->may_convert_function_types_
3341 && type->function_type() != NULL
3342 && expr_type->function_type() != NULL)
3345 if (Type::are_convertible(type, expr_type, &reason))
3348 error_at(this->location(), "%s", reason.c_str());
3349 this->set_is_error();
3352 // Get a tree for a type conversion.
3355 Type_conversion_expression::do_get_tree(Translate_context* context)
3357 Gogo* gogo = context->gogo();
3358 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
3359 tree expr_tree = this->expr_->get_tree(context);
3361 if (type_tree == error_mark_node
3362 || expr_tree == error_mark_node
3363 || TREE_TYPE(expr_tree) == error_mark_node)
3364 return error_mark_node;
3366 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3367 return fold_convert(type_tree, expr_tree);
3369 Type* type = this->type_;
3370 Type* expr_type = this->expr_->type();
3372 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3373 ret = Expression::convert_for_assignment(context, type, expr_type,
3374 expr_tree, this->location());
3375 else if (type->integer_type() != NULL)
3377 if (expr_type->integer_type() != NULL
3378 || expr_type->float_type() != NULL
3379 || expr_type->is_unsafe_pointer_type())
3380 ret = fold(convert_to_integer(type_tree, expr_tree));
3384 else if (type->float_type() != NULL)
3386 if (expr_type->integer_type() != NULL
3387 || expr_type->float_type() != NULL)
3388 ret = fold(convert_to_real(type_tree, expr_tree));
3392 else if (type->complex_type() != NULL)
3394 if (expr_type->complex_type() != NULL)
3395 ret = fold(convert_to_complex(type_tree, expr_tree));
3399 else if (type->is_string_type()
3400 && expr_type->integer_type() != NULL)
3402 expr_tree = fold_convert(integer_type_node, expr_tree);
3403 if (host_integerp(expr_tree, 0))
3405 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3407 Lex::append_char(intval, true, &s, this->location());
3408 Expression* se = Expression::make_string(s, this->location());
3409 return se->get_tree(context);
3412 static tree int_to_string_fndecl;
3413 ret = Gogo::call_builtin(&int_to_string_fndecl,
3415 "__go_int_to_string",
3419 fold_convert(integer_type_node, expr_tree));
3421 else if (type->is_string_type()
3422 && (expr_type->array_type() != NULL
3423 || (expr_type->points_to() != NULL
3424 && expr_type->points_to()->array_type() != NULL)))
3426 Type* t = expr_type;
3427 if (t->points_to() != NULL)
3430 expr_tree = build_fold_indirect_ref(expr_tree);
3432 if (!DECL_P(expr_tree))
3433 expr_tree = save_expr(expr_tree);
3434 Array_type* a = t->array_type();
3435 Type* e = a->element_type()->forwarded();
3436 go_assert(e->integer_type() != NULL);
3437 tree valptr = fold_convert(const_ptr_type_node,
3438 a->value_pointer_tree(gogo, expr_tree));
3439 tree len = a->length_tree(gogo, expr_tree);
3440 len = fold_convert_loc(this->location(), integer_type_node, len);
3441 if (e->integer_type()->is_unsigned()
3442 && e->integer_type()->bits() == 8)
3444 static tree byte_array_to_string_fndecl;
3445 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3447 "__go_byte_array_to_string",
3450 const_ptr_type_node,
3457 go_assert(e == Type::lookup_integer_type("int"));
3458 static tree int_array_to_string_fndecl;
3459 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3461 "__go_int_array_to_string",
3464 const_ptr_type_node,
3470 else if (type->is_open_array_type() && expr_type->is_string_type())
3472 Type* e = type->array_type()->element_type()->forwarded();
3473 go_assert(e->integer_type() != NULL);
3474 if (e->integer_type()->is_unsigned()
3475 && e->integer_type()->bits() == 8)
3477 static tree string_to_byte_array_fndecl;
3478 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3480 "__go_string_to_byte_array",
3483 TREE_TYPE(expr_tree),
3488 go_assert(e == Type::lookup_integer_type("int"));
3489 static tree string_to_int_array_fndecl;
3490 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3492 "__go_string_to_int_array",
3495 TREE_TYPE(expr_tree),
3499 else if ((type->is_unsafe_pointer_type()
3500 && expr_type->points_to() != NULL)
3501 || (expr_type->is_unsafe_pointer_type()
3502 && type->points_to() != NULL))
3503 ret = fold_convert(type_tree, expr_tree);
3504 else if (type->is_unsafe_pointer_type()
3505 && expr_type->integer_type() != NULL)
3506 ret = convert_to_pointer(type_tree, expr_tree);
3507 else if (this->may_convert_function_types_
3508 && type->function_type() != NULL
3509 && expr_type->function_type() != NULL)
3510 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3512 ret = Expression::convert_for_assignment(context, type, expr_type,
3513 expr_tree, this->location());
3518 // Output a type conversion in a constant expression.
3521 Type_conversion_expression::do_export(Export* exp) const
3523 exp->write_c_string("convert(");
3524 exp->write_type(this->type_);
3525 exp->write_c_string(", ");
3526 this->expr_->export_expression(exp);
3527 exp->write_c_string(")");
3530 // Import a type conversion or a struct construction.
3533 Type_conversion_expression::do_import(Import* imp)
3535 imp->require_c_string("convert(");
3536 Type* type = imp->read_type();
3537 imp->require_c_string(", ");
3538 Expression* val = Expression::import_expression(imp);
3539 imp->require_c_string(")");
3540 return Expression::make_cast(type, val, imp->location());
3543 // Make a type cast expression.
3546 Expression::make_cast(Type* type, Expression* val, source_location location)
3548 if (type->is_error_type() || val->is_error_expression())
3549 return Expression::make_error(location);
3550 return new Type_conversion_expression(type, val, location);
3553 // An unsafe type conversion, used to pass values to builtin functions.
3555 class Unsafe_type_conversion_expression : public Expression
3558 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3559 source_location location)
3560 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3561 type_(type), expr_(expr)
3566 do_traverse(Traverse* traverse);
3570 { return this->type_; }
3573 do_determine_type(const Type_context*)
3579 return new Unsafe_type_conversion_expression(this->type_,
3580 this->expr_->copy(),
3585 do_get_tree(Translate_context*);
3588 // The type to convert to.
3590 // The expression to convert.
3597 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3599 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3600 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3601 return TRAVERSE_EXIT;
3602 return TRAVERSE_CONTINUE;
3605 // Convert to backend representation.
3608 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3610 // We are only called for a limited number of cases.
3612 Type* t = this->type_;
3613 Type* et = this->expr_->type();
3615 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
3616 tree expr_tree = this->expr_->get_tree(context);
3617 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3618 return error_mark_node;
3620 source_location loc = this->location();
3622 bool use_view_convert = false;
3623 if (t->is_open_array_type())
3625 go_assert(et->is_open_array_type());
3626 use_view_convert = true;
3628 else if (t->map_type() != NULL)
3629 go_assert(et->map_type() != NULL);
3630 else if (t->channel_type() != NULL)
3631 go_assert(et->channel_type() != NULL);
3632 else if (t->points_to() != NULL && t->points_to()->channel_type() != NULL)
3633 go_assert((et->points_to() != NULL
3634 && et->points_to()->channel_type() != NULL)
3635 || et->is_nil_type());
3636 else if (t->is_unsafe_pointer_type())
3637 go_assert(et->points_to() != NULL || et->is_nil_type());
3638 else if (et->is_unsafe_pointer_type())
3639 go_assert(t->points_to() != NULL);
3640 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3642 go_assert(et->interface_type() != NULL
3643 && !et->interface_type()->is_empty());
3644 use_view_convert = true;
3646 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3648 go_assert(et->interface_type() != NULL
3649 && et->interface_type()->is_empty());
3650 use_view_convert = true;
3652 else if (t->integer_type() != NULL)
3654 go_assert(et->is_boolean_type()
3655 || et->integer_type() != NULL
3656 || et->function_type() != NULL
3657 || et->points_to() != NULL
3658 || et->map_type() != NULL
3659 || et->channel_type() != NULL);
3660 return convert_to_integer(type_tree, expr_tree);
3665 if (use_view_convert)
3666 return fold_build1_loc(loc, VIEW_CONVERT_EXPR, type_tree, expr_tree);
3668 return fold_convert_loc(loc, type_tree, expr_tree);
3671 // Make an unsafe type conversion expression.
3674 Expression::make_unsafe_cast(Type* type, Expression* expr,
3675 source_location location)
3677 return new Unsafe_type_conversion_expression(type, expr, location);
3680 // Unary expressions.
3682 class Unary_expression : public Expression
3685 Unary_expression(Operator op, Expression* expr, source_location location)
3686 : Expression(EXPRESSION_UNARY, location),
3687 op_(op), escapes_(true), expr_(expr)
3690 // Return the operator.
3693 { return this->op_; }
3695 // Return the operand.
3698 { return this->expr_; }
3700 // Record that an address expression does not escape.
3702 set_does_not_escape()
3704 go_assert(this->op_ == OPERATOR_AND);
3705 this->escapes_ = false;
3708 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3709 // could be done, false if not.
3711 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3714 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3715 // could be done, false if not.
3717 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3719 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3720 // true if this could be done, false if not.
3722 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3730 do_traverse(Traverse* traverse)
3731 { return Expression::traverse(&this->expr_, traverse); }
3734 do_lower(Gogo*, Named_object*, int);
3737 do_is_constant() const;
3740 do_integer_constant_value(bool, mpz_t, Type**) const;
3743 do_float_constant_value(mpfr_t, Type**) const;
3746 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3752 do_determine_type(const Type_context*);
3755 do_check_types(Gogo*);
3760 return Expression::make_unary(this->op_, this->expr_->copy(),
3765 do_is_addressable() const
3766 { return this->op_ == OPERATOR_MULT; }
3769 do_get_tree(Translate_context*);
3772 do_export(Export*) const;
3775 // The unary operator to apply.
3777 // Normally true. False if this is an address expression which does
3778 // not escape the current function.
3784 // If we are taking the address of a composite literal, and the
3785 // contents are not constant, then we want to make a heap composite
3789 Unary_expression::do_lower(Gogo*, Named_object*, int)
3791 source_location loc = this->location();
3792 Operator op = this->op_;
3793 Expression* expr = this->expr_;
3795 if (op == OPERATOR_MULT && expr->is_type_expression())
3796 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3798 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3799 // moving x to the heap. FIXME: Is it worth doing a real escape
3800 // analysis here? This case is found in math/unsafe.go and is
3801 // therefore worth special casing.
3802 if (op == OPERATOR_MULT)
3804 Expression* e = expr;
3805 while (e->classification() == EXPRESSION_CONVERSION)
3807 Type_conversion_expression* te
3808 = static_cast<Type_conversion_expression*>(e);
3812 if (e->classification() == EXPRESSION_UNARY)
3814 Unary_expression* ue = static_cast<Unary_expression*>(e);
3815 if (ue->op_ == OPERATOR_AND)
3822 ue->set_does_not_escape();
3827 // Catching an invalid indirection of unsafe.Pointer here avoid
3828 // having to deal with TYPE_VOID in other places.
3829 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
3831 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3832 return Expression::make_error(this->location());
3835 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
3836 || op == OPERATOR_NOT || op == OPERATOR_XOR)
3838 Expression* ret = NULL;
3843 if (expr->integer_constant_value(false, eval, &etype))
3847 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
3848 ret = Expression::make_integer(&val, etype, loc);
3855 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
3860 if (expr->float_constant_value(fval, &ftype))
3864 if (Unary_expression::eval_float(op, fval, val))
3865 ret = Expression::make_float(&val, ftype, loc);
3876 if (expr->complex_constant_value(fval, ival, &ftype))
3882 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
3883 ret = Expression::make_complex(&real, &imag, ftype, loc);
3897 // Return whether a unary expression is a constant.
3900 Unary_expression::do_is_constant() const
3902 if (this->op_ == OPERATOR_MULT)
3904 // Indirecting through a pointer is only constant if the object
3905 // to which the expression points is constant, but we currently
3906 // have no way to determine that.
3909 else if (this->op_ == OPERATOR_AND)
3911 // Taking the address of a variable is constant if it is a
3912 // global variable, not constant otherwise. In other cases
3913 // taking the address is probably not a constant.
3914 Var_expression* ve = this->expr_->var_expression();
3917 Named_object* no = ve->named_object();
3918 return no->is_variable() && no->var_value()->is_global();
3923 return this->expr_->is_constant();
3926 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3927 // UVAL, if known; it may be NULL. Return true if this could be done,
3931 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3932 source_location location)
3939 case OPERATOR_MINUS:
3941 return Integer_expression::check_constant(val, utype, location);
3943 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3947 || utype->integer_type() == NULL
3948 || utype->integer_type()->is_abstract())
3952 // The number of HOST_WIDE_INTs that it takes to represent
3954 size_t count = ((mpz_sizeinbase(uval, 2)
3955 + HOST_BITS_PER_WIDE_INT
3957 / HOST_BITS_PER_WIDE_INT);
3959 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3960 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3963 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3964 go_assert(ecount <= count);
3966 // Trim down to the number of words required by the type.
3967 size_t obits = utype->integer_type()->bits();
3968 if (!utype->integer_type()->is_unsigned())
3970 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3971 / HOST_BITS_PER_WIDE_INT);
3972 go_assert(ocount <= count);
3974 for (size_t i = 0; i < ocount; ++i)
3977 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3979 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3982 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3986 return Integer_expression::check_constant(val, utype, location);
3995 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3996 // could be done, false if not.
3999 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
4004 mpfr_set(val, uval, GMP_RNDN);
4006 case OPERATOR_MINUS:
4007 mpfr_neg(val, uval, GMP_RNDN);
4019 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
4020 // if this could be done, false if not.
4023 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
4024 mpfr_t real, mpfr_t imag)
4029 mpfr_set(real, rval, GMP_RNDN);
4030 mpfr_set(imag, ival, GMP_RNDN);
4032 case OPERATOR_MINUS:
4033 mpfr_neg(real, rval, GMP_RNDN);
4034 mpfr_neg(imag, ival, GMP_RNDN);
4046 // Return the integral constant value of a unary expression, if it has one.
4049 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
4055 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
4058 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
4064 // Return the floating point constant value of a unary expression, if
4068 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
4073 if (!this->expr_->float_constant_value(uval, ptype))
4076 ret = Unary_expression::eval_float(this->op_, uval, val);
4081 // Return the complex constant value of a unary expression, if it has
4085 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
4093 if (!this->expr_->complex_constant_value(rval, ival, ptype))
4096 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
4102 // Return the type of a unary expression.
4105 Unary_expression::do_type()
4110 case OPERATOR_MINUS:
4113 return this->expr_->type();
4116 return Type::make_pointer_type(this->expr_->type());
4120 Type* subtype = this->expr_->type();
4121 Type* points_to = subtype->points_to();
4122 if (points_to == NULL)
4123 return Type::make_error_type();
4132 // Determine abstract types for a unary expression.
4135 Unary_expression::do_determine_type(const Type_context* context)
4140 case OPERATOR_MINUS:
4143 this->expr_->determine_type(context);
4147 // Taking the address of something.
4149 Type* subtype = (context->type == NULL
4151 : context->type->points_to());
4152 Type_context subcontext(subtype, false);
4153 this->expr_->determine_type(&subcontext);
4158 // Indirecting through a pointer.
4160 Type* subtype = (context->type == NULL
4162 : Type::make_pointer_type(context->type));
4163 Type_context subcontext(subtype, false);
4164 this->expr_->determine_type(&subcontext);
4173 // Check types for a unary expression.
4176 Unary_expression::do_check_types(Gogo*)
4178 Type* type = this->expr_->type();
4179 if (type->is_error())
4181 this->set_is_error();
4188 case OPERATOR_MINUS:
4189 if (type->integer_type() == NULL
4190 && type->float_type() == NULL
4191 && type->complex_type() == NULL)
4192 this->report_error(_("expected numeric type"));
4197 if (type->integer_type() == NULL
4198 && !type->is_boolean_type())
4199 this->report_error(_("expected integer or boolean type"));
4203 if (!this->expr_->is_addressable())
4204 this->report_error(_("invalid operand for unary %<&%>"));
4206 this->expr_->address_taken(this->escapes_);
4210 // Indirecting through a pointer.
4211 if (type->points_to() == NULL)
4212 this->report_error(_("expected pointer"));
4220 // Get a tree for a unary expression.
4223 Unary_expression::do_get_tree(Translate_context* context)
4225 tree expr = this->expr_->get_tree(context);
4226 if (expr == error_mark_node)
4227 return error_mark_node;
4229 source_location loc = this->location();
4235 case OPERATOR_MINUS:
4237 tree type = TREE_TYPE(expr);
4238 tree compute_type = excess_precision_type(type);
4239 if (compute_type != NULL_TREE)
4240 expr = ::convert(compute_type, expr);
4241 tree ret = fold_build1_loc(loc, NEGATE_EXPR,
4242 (compute_type != NULL_TREE
4246 if (compute_type != NULL_TREE)
4247 ret = ::convert(type, ret);
4252 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4253 return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr);
4255 return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr,
4256 build_int_cst(TREE_TYPE(expr), 0));
4259 return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr);
4262 // We should not see a non-constant constructor here; cases
4263 // where we would see one should have been moved onto the heap
4264 // at parse time. Taking the address of a nonconstant
4265 // constructor will not do what the programmer expects.
4266 go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4267 go_assert(TREE_CODE(expr) != ADDR_EXPR);
4269 // Build a decl for a constant constructor.
4270 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4272 tree decl = build_decl(this->location(), VAR_DECL,
4273 create_tmp_var_name("C"), TREE_TYPE(expr));
4274 DECL_EXTERNAL(decl) = 0;
4275 TREE_PUBLIC(decl) = 0;
4276 TREE_READONLY(decl) = 1;
4277 TREE_CONSTANT(decl) = 1;
4278 TREE_STATIC(decl) = 1;
4279 TREE_ADDRESSABLE(decl) = 1;
4280 DECL_ARTIFICIAL(decl) = 1;
4281 DECL_INITIAL(decl) = expr;
4282 rest_of_decl_compilation(decl, 1, 0);
4286 return build_fold_addr_expr_loc(loc, expr);
4290 go_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4292 // If we are dereferencing the pointer to a large struct, we
4293 // need to check for nil. We don't bother to check for small
4294 // structs because we expect the system to crash on a nil
4295 // pointer dereference.
4296 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4297 if (s == -1 || s >= 4096)
4300 expr = save_expr(expr);
4301 tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node,
4303 fold_convert(TREE_TYPE(expr),
4304 null_pointer_node));
4305 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4307 expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr),
4308 build3(COND_EXPR, void_type_node,
4309 compare, crash, NULL_TREE),
4313 // If the type of EXPR is a recursive pointer type, then we
4314 // need to insert a cast before indirecting.
4315 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4317 Type* pt = this->expr_->type()->points_to();
4318 tree ind = type_to_tree(pt->get_backend(context->gogo()));
4319 expr = fold_convert_loc(loc, build_pointer_type(ind), expr);
4322 return build_fold_indirect_ref_loc(loc, expr);
4330 // Export a unary expression.
4333 Unary_expression::do_export(Export* exp) const
4338 exp->write_c_string("+ ");
4340 case OPERATOR_MINUS:
4341 exp->write_c_string("- ");
4344 exp->write_c_string("! ");
4347 exp->write_c_string("^ ");
4354 this->expr_->export_expression(exp);
4357 // Import a unary expression.
4360 Unary_expression::do_import(Import* imp)
4363 switch (imp->get_char())
4369 op = OPERATOR_MINUS;
4380 imp->require_c_string(" ");
4381 Expression* expr = Expression::import_expression(imp);
4382 return Expression::make_unary(op, expr, imp->location());
4385 // Make a unary expression.
4388 Expression::make_unary(Operator op, Expression* expr, source_location location)
4390 return new Unary_expression(op, expr, location);
4393 // If this is an indirection through a pointer, return the expression
4394 // being pointed through. Otherwise return this.
4399 if (this->classification_ == EXPRESSION_UNARY)
4401 Unary_expression* ue = static_cast<Unary_expression*>(this);
4402 if (ue->op() == OPERATOR_MULT)
4403 return ue->operand();
4408 // Class Binary_expression.
4413 Binary_expression::do_traverse(Traverse* traverse)
4415 int t = Expression::traverse(&this->left_, traverse);
4416 if (t == TRAVERSE_EXIT)
4417 return TRAVERSE_EXIT;
4418 return Expression::traverse(&this->right_, traverse);
4421 // Compare integer constants according to OP.
4424 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4427 int i = mpz_cmp(left_val, right_val);
4432 case OPERATOR_NOTEQ:
4447 // Compare floating point constants according to OP.
4450 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4455 i = mpfr_cmp(left_val, right_val);
4459 mpfr_init_set(lv, left_val, GMP_RNDN);
4461 mpfr_init_set(rv, right_val, GMP_RNDN);
4462 Float_expression::constrain_float(lv, type);
4463 Float_expression::constrain_float(rv, type);
4464 i = mpfr_cmp(lv, rv);
4472 case OPERATOR_NOTEQ:
4487 // Compare complex constants according to OP. Complex numbers may
4488 // only be compared for equality.
4491 Binary_expression::compare_complex(Operator op, Type* type,
4492 mpfr_t left_real, mpfr_t left_imag,
4493 mpfr_t right_real, mpfr_t right_imag)
4497 is_equal = (mpfr_cmp(left_real, right_real) == 0
4498 && mpfr_cmp(left_imag, right_imag) == 0);
4503 mpfr_init_set(lr, left_real, GMP_RNDN);
4504 mpfr_init_set(li, left_imag, GMP_RNDN);
4507 mpfr_init_set(rr, right_real, GMP_RNDN);
4508 mpfr_init_set(ri, right_imag, GMP_RNDN);
4509 Complex_expression::constrain_complex(lr, li, type);
4510 Complex_expression::constrain_complex(rr, ri, type);
4511 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4521 case OPERATOR_NOTEQ:
4528 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4529 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4530 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4531 // this could be done, false if not.
4534 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4535 Type* right_type, mpz_t right_val,
4536 source_location location, mpz_t val)
4538 bool is_shift_op = false;
4542 case OPERATOR_ANDAND:
4544 case OPERATOR_NOTEQ:
4549 // These return boolean values. We should probably handle them
4550 // anyhow in case a type conversion is used on the result.
4553 mpz_add(val, left_val, right_val);
4555 case OPERATOR_MINUS:
4556 mpz_sub(val, left_val, right_val);
4559 mpz_ior(val, left_val, right_val);
4562 mpz_xor(val, left_val, right_val);
4565 mpz_mul(val, left_val, right_val);
4568 if (mpz_sgn(right_val) != 0)
4569 mpz_tdiv_q(val, left_val, right_val);
4572 error_at(location, "division by zero");
4578 if (mpz_sgn(right_val) != 0)
4579 mpz_tdiv_r(val, left_val, right_val);
4582 error_at(location, "division by zero");
4587 case OPERATOR_LSHIFT:
4589 unsigned long shift = mpz_get_ui(right_val);
4590 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
4592 error_at(location, "shift count overflow");
4596 mpz_mul_2exp(val, left_val, shift);
4601 case OPERATOR_RSHIFT:
4603 unsigned long shift = mpz_get_ui(right_val);
4604 if (mpz_cmp_ui(right_val, shift) != 0)
4606 error_at(location, "shift count overflow");
4610 if (mpz_cmp_ui(left_val, 0) >= 0)
4611 mpz_tdiv_q_2exp(val, left_val, shift);
4613 mpz_fdiv_q_2exp(val, left_val, shift);
4619 mpz_and(val, left_val, right_val);
4621 case OPERATOR_BITCLEAR:
4625 mpz_com(tval, right_val);
4626 mpz_and(val, left_val, tval);
4634 Type* type = left_type;
4639 else if (type != right_type && right_type != NULL)
4641 if (type->is_abstract())
4643 else if (!right_type->is_abstract())
4645 // This look like a type error which should be diagnosed
4646 // elsewhere. Don't do anything here, to avoid an
4647 // unhelpful chain of error messages.
4653 if (type != NULL && !type->is_abstract())
4655 // We have to check the operands too, as we have implicitly
4656 // coerced them to TYPE.
4657 if ((type != left_type
4658 && !Integer_expression::check_constant(left_val, type, location))
4660 && type != right_type
4661 && !Integer_expression::check_constant(right_val, type,
4663 || !Integer_expression::check_constant(val, type, location))
4670 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4671 // Return true if this could be done, false if not.
4674 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4675 Type* right_type, mpfr_t right_val,
4676 mpfr_t val, source_location location)
4681 case OPERATOR_ANDAND:
4683 case OPERATOR_NOTEQ:
4688 // These return boolean values. We should probably handle them
4689 // anyhow in case a type conversion is used on the result.
4692 mpfr_add(val, left_val, right_val, GMP_RNDN);
4694 case OPERATOR_MINUS:
4695 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4700 case OPERATOR_BITCLEAR:
4703 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4706 if (mpfr_zero_p(right_val))
4707 error_at(location, "division by zero");
4708 mpfr_div(val, left_val, right_val, GMP_RNDN);
4712 case OPERATOR_LSHIFT:
4713 case OPERATOR_RSHIFT:
4719 Type* type = left_type;
4722 else if (type != right_type && right_type != NULL)
4724 if (type->is_abstract())
4726 else if (!right_type->is_abstract())
4728 // This looks like a type error which should be diagnosed
4729 // elsewhere. Don't do anything here, to avoid an unhelpful
4730 // chain of error messages.
4735 if (type != NULL && !type->is_abstract())
4737 if ((type != left_type
4738 && !Float_expression::check_constant(left_val, type, location))
4739 || (type != right_type
4740 && !Float_expression::check_constant(right_val, type,
4742 || !Float_expression::check_constant(val, type, location))
4743 mpfr_set_ui(val, 0, GMP_RNDN);
4749 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4750 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4751 // could be done, false if not.
4754 Binary_expression::eval_complex(Operator op, Type* left_type,
4755 mpfr_t left_real, mpfr_t left_imag,
4757 mpfr_t right_real, mpfr_t right_imag,
4758 mpfr_t real, mpfr_t imag,
4759 source_location location)
4764 case OPERATOR_ANDAND:
4766 case OPERATOR_NOTEQ:
4771 // These return boolean values and must be handled differently.
4774 mpfr_add(real, left_real, right_real, GMP_RNDN);
4775 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4777 case OPERATOR_MINUS:
4778 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4779 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4784 case OPERATOR_BITCLEAR:
4788 // You might think that multiplying two complex numbers would
4789 // be simple, and you would be right, until you start to think
4790 // about getting the right answer for infinity. If one
4791 // operand here is infinity and the other is anything other
4792 // than zero or NaN, then we are going to wind up subtracting
4793 // two infinity values. That will give us a NaN, but the
4794 // correct answer is infinity.
4798 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4802 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4806 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4810 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4812 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4813 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4815 // If we get NaN on both sides, check whether it should really
4816 // be infinity. The rule is that if either side of the
4817 // complex number is infinity, then the whole value is
4818 // infinity, even if the other side is NaN. So the only case
4819 // we have to fix is the one in which both sides are NaN.
4820 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4821 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4822 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4824 bool is_infinity = false;
4828 mpfr_init_set(lr, left_real, GMP_RNDN);
4829 mpfr_init_set(li, left_imag, GMP_RNDN);
4833 mpfr_init_set(rr, right_real, GMP_RNDN);
4834 mpfr_init_set(ri, right_imag, GMP_RNDN);
4836 // If the left side is infinity, then the result is
4838 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4840 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4841 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4842 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4843 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4846 mpfr_set_ui(rr, 0, GMP_RNDN);
4847 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4851 mpfr_set_ui(ri, 0, GMP_RNDN);
4852 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4857 // If the right side is infinity, then the result is
4859 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4861 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4862 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4863 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4864 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4867 mpfr_set_ui(lr, 0, GMP_RNDN);
4868 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4872 mpfr_set_ui(li, 0, GMP_RNDN);
4873 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4878 // If we got an overflow in the intermediate computations,
4879 // then the result is infinity.
4881 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4882 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4886 mpfr_set_ui(lr, 0, GMP_RNDN);
4887 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4891 mpfr_set_ui(li, 0, GMP_RNDN);
4892 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4896 mpfr_set_ui(rr, 0, GMP_RNDN);
4897 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4901 mpfr_set_ui(ri, 0, GMP_RNDN);
4902 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4909 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4910 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4911 mpfr_mul(lirr, li, rr, GMP_RNDN);
4912 mpfr_mul(liri, li, ri, GMP_RNDN);
4913 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4914 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4915 mpfr_set_inf(real, mpfr_sgn(real));
4916 mpfr_set_inf(imag, mpfr_sgn(imag));
4933 // For complex division we want to avoid having an
4934 // intermediate overflow turn the whole result in a NaN. We
4935 // scale the values to try to avoid this.
4937 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
4938 error_at(location, "division by zero");
4944 mpfr_abs(rra, right_real, GMP_RNDN);
4945 mpfr_abs(ria, right_imag, GMP_RNDN);
4948 mpfr_max(t, rra, ria, GMP_RNDN);
4952 mpfr_init_set(rr, right_real, GMP_RNDN);
4953 mpfr_init_set(ri, right_imag, GMP_RNDN);
4955 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4957 ilogbw = mpfr_get_exp(t);
4958 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4959 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4964 mpfr_mul(denom, rr, rr, GMP_RNDN);
4965 mpfr_mul(t, ri, ri, GMP_RNDN);
4966 mpfr_add(denom, denom, t, GMP_RNDN);
4968 mpfr_mul(real, left_real, rr, GMP_RNDN);
4969 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4970 mpfr_add(real, real, t, GMP_RNDN);
4971 mpfr_div(real, real, denom, GMP_RNDN);
4972 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4974 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4975 mpfr_mul(t, left_real, ri, GMP_RNDN);
4976 mpfr_sub(imag, imag, t, GMP_RNDN);
4977 mpfr_div(imag, imag, denom, GMP_RNDN);
4978 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4980 // If we wind up with NaN on both sides, check whether we
4981 // should really have infinity. The rule is that if either
4982 // side of the complex number is infinity, then the whole
4983 // value is infinity, even if the other side is NaN. So the
4984 // only case we have to fix is the one in which both sides are
4986 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4987 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4988 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4990 if (mpfr_zero_p(denom))
4992 mpfr_set_inf(real, mpfr_sgn(rr));
4993 mpfr_mul(real, real, left_real, GMP_RNDN);
4994 mpfr_set_inf(imag, mpfr_sgn(rr));
4995 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4997 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4998 && mpfr_number_p(rr) && mpfr_number_p(ri))
5000 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
5001 mpfr_copysign(t, t, left_real, GMP_RNDN);
5004 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
5005 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
5009 mpfr_mul(t3, t, rr, GMP_RNDN);
5013 mpfr_mul(t4, t2, ri, GMP_RNDN);
5015 mpfr_add(t3, t3, t4, GMP_RNDN);
5016 mpfr_set_inf(real, mpfr_sgn(t3));
5018 mpfr_mul(t3, t2, rr, GMP_RNDN);
5019 mpfr_mul(t4, t, ri, GMP_RNDN);
5020 mpfr_sub(t3, t3, t4, GMP_RNDN);
5021 mpfr_set_inf(imag, mpfr_sgn(t3));
5027 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
5028 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
5030 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5031 mpfr_copysign(t, t, rr, GMP_RNDN);
5034 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5035 mpfr_copysign(t2, t2, ri, GMP_RNDN);
5039 mpfr_mul(t3, left_real, t, GMP_RNDN);
5043 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
5045 mpfr_add(t3, t3, t4, GMP_RNDN);
5046 mpfr_set_ui(real, 0, GMP_RNDN);
5047 mpfr_mul(real, real, t3, GMP_RNDN);
5049 mpfr_mul(t3, left_imag, t, GMP_RNDN);
5050 mpfr_mul(t4, left_real, t2, GMP_RNDN);
5051 mpfr_sub(t3, t3, t4, GMP_RNDN);
5052 mpfr_set_ui(imag, 0, GMP_RNDN);
5053 mpfr_mul(imag, imag, t3, GMP_RNDN);
5071 case OPERATOR_LSHIFT:
5072 case OPERATOR_RSHIFT:
5078 Type* type = left_type;
5081 else if (type != right_type && right_type != NULL)
5083 if (type->is_abstract())
5085 else if (!right_type->is_abstract())
5087 // This looks like a type error which should be diagnosed
5088 // elsewhere. Don't do anything here, to avoid an unhelpful
5089 // chain of error messages.
5094 if (type != NULL && !type->is_abstract())
5096 if ((type != left_type
5097 && !Complex_expression::check_constant(left_real, left_imag,
5099 || (type != right_type
5100 && !Complex_expression::check_constant(right_real, right_imag,
5102 || !Complex_expression::check_constant(real, imag, type,
5105 mpfr_set_ui(real, 0, GMP_RNDN);
5106 mpfr_set_ui(imag, 0, GMP_RNDN);
5113 // Lower a binary expression. We have to evaluate constant
5114 // expressions now, in order to implement Go's unlimited precision
5118 Binary_expression::do_lower(Gogo*, Named_object*, int)
5120 source_location location = this->location();
5121 Operator op = this->op_;
5122 Expression* left = this->left_;
5123 Expression* right = this->right_;
5125 const bool is_comparison = (op == OPERATOR_EQEQ
5126 || op == OPERATOR_NOTEQ
5127 || op == OPERATOR_LT
5128 || op == OPERATOR_LE
5129 || op == OPERATOR_GT
5130 || op == OPERATOR_GE);
5132 // Integer constant expressions.
5138 mpz_init(right_val);
5140 if (left->integer_constant_value(false, left_val, &left_type)
5141 && right->integer_constant_value(false, right_val, &right_type))
5143 Expression* ret = NULL;
5144 if (left_type != right_type
5145 && left_type != NULL
5146 && right_type != NULL
5147 && left_type->base() != right_type->base()
5148 && op != OPERATOR_LSHIFT
5149 && op != OPERATOR_RSHIFT)
5151 // May be a type error--let it be diagnosed later.
5153 else if (is_comparison)
5155 bool b = Binary_expression::compare_integer(op, left_val,
5157 ret = Expression::make_cast(Type::lookup_bool_type(),
5158 Expression::make_boolean(b, location),
5166 if (Binary_expression::eval_integer(op, left_type, left_val,
5167 right_type, right_val,
5170 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
5172 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5174 else if (left_type == NULL)
5176 else if (right_type == NULL)
5178 else if (!left_type->is_abstract()
5179 && left_type->named_type() != NULL)
5181 else if (!right_type->is_abstract()
5182 && right_type->named_type() != NULL)
5184 else if (!left_type->is_abstract())
5186 else if (!right_type->is_abstract())
5188 else if (left_type->float_type() != NULL)
5190 else if (right_type->float_type() != NULL)
5192 else if (left_type->complex_type() != NULL)
5194 else if (right_type->complex_type() != NULL)
5198 ret = Expression::make_integer(&val, type, location);
5206 mpz_clear(right_val);
5207 mpz_clear(left_val);
5211 mpz_clear(right_val);
5212 mpz_clear(left_val);
5215 // Floating point constant expressions.
5218 mpfr_init(left_val);
5221 mpfr_init(right_val);
5223 if (left->float_constant_value(left_val, &left_type)
5224 && right->float_constant_value(right_val, &right_type))
5226 Expression* ret = NULL;
5227 if (left_type != right_type
5228 && left_type != NULL
5229 && right_type != NULL
5230 && left_type->base() != right_type->base()
5231 && op != OPERATOR_LSHIFT
5232 && op != OPERATOR_RSHIFT)
5234 // May be a type error--let it be diagnosed later.
5236 else if (is_comparison)
5238 bool b = Binary_expression::compare_float(op,
5242 left_val, right_val);
5243 ret = Expression::make_boolean(b, location);
5250 if (Binary_expression::eval_float(op, left_type, left_val,
5251 right_type, right_val, val,
5254 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5255 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5257 if (left_type == NULL)
5259 else if (right_type == NULL)
5261 else if (!left_type->is_abstract()
5262 && left_type->named_type() != NULL)
5264 else if (!right_type->is_abstract()
5265 && right_type->named_type() != NULL)
5267 else if (!left_type->is_abstract())
5269 else if (!right_type->is_abstract())
5271 else if (left_type->float_type() != NULL)
5273 else if (right_type->float_type() != NULL)
5277 ret = Expression::make_float(&val, type, location);
5285 mpfr_clear(right_val);
5286 mpfr_clear(left_val);
5290 mpfr_clear(right_val);
5291 mpfr_clear(left_val);
5294 // Complex constant expressions.
5298 mpfr_init(left_real);
5299 mpfr_init(left_imag);
5304 mpfr_init(right_real);
5305 mpfr_init(right_imag);
5308 if (left->complex_constant_value(left_real, left_imag, &left_type)
5309 && right->complex_constant_value(right_real, right_imag, &right_type))
5311 Expression* ret = NULL;
5312 if (left_type != right_type
5313 && left_type != NULL
5314 && right_type != NULL
5315 && left_type->base() != right_type->base())
5317 // May be a type error--let it be diagnosed later.
5319 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5321 bool b = Binary_expression::compare_complex(op,
5329 ret = Expression::make_boolean(b, location);
5338 if (Binary_expression::eval_complex(op, left_type,
5339 left_real, left_imag,
5341 right_real, right_imag,
5345 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5346 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5348 if (left_type == NULL)
5350 else if (right_type == NULL)
5352 else if (!left_type->is_abstract()
5353 && left_type->named_type() != NULL)
5355 else if (!right_type->is_abstract()
5356 && right_type->named_type() != NULL)
5358 else if (!left_type->is_abstract())
5360 else if (!right_type->is_abstract())
5362 else if (left_type->complex_type() != NULL)
5364 else if (right_type->complex_type() != NULL)
5368 ret = Expression::make_complex(&real, &imag, type,
5377 mpfr_clear(left_real);
5378 mpfr_clear(left_imag);
5379 mpfr_clear(right_real);
5380 mpfr_clear(right_imag);
5385 mpfr_clear(left_real);
5386 mpfr_clear(left_imag);
5387 mpfr_clear(right_real);
5388 mpfr_clear(right_imag);
5391 // String constant expressions.
5392 if (op == OPERATOR_PLUS
5393 && left->type()->is_string_type()
5394 && right->type()->is_string_type())
5396 std::string left_string;
5397 std::string right_string;
5398 if (left->string_constant_value(&left_string)
5399 && right->string_constant_value(&right_string))
5400 return Expression::make_string(left_string + right_string, location);
5406 // Return the integer constant value, if it has one.
5409 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5415 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5418 mpz_clear(left_val);
5423 mpz_init(right_val);
5425 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5428 mpz_clear(right_val);
5429 mpz_clear(left_val);
5434 if (left_type != right_type
5435 && left_type != NULL
5436 && right_type != NULL
5437 && left_type->base() != right_type->base()
5438 && this->op_ != OPERATOR_RSHIFT
5439 && this->op_ != OPERATOR_LSHIFT)
5442 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5443 right_type, right_val,
5444 this->location(), val);
5446 mpz_clear(right_val);
5447 mpz_clear(left_val);
5455 // Return the floating point constant value, if it has one.
5458 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5461 mpfr_init(left_val);
5463 if (!this->left_->float_constant_value(left_val, &left_type))
5465 mpfr_clear(left_val);
5470 mpfr_init(right_val);
5472 if (!this->right_->float_constant_value(right_val, &right_type))
5474 mpfr_clear(right_val);
5475 mpfr_clear(left_val);
5480 if (left_type != right_type
5481 && left_type != NULL
5482 && right_type != NULL
5483 && left_type->base() != right_type->base())
5486 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5487 right_type, right_val,
5488 val, this->location());
5490 mpfr_clear(left_val);
5491 mpfr_clear(right_val);
5499 // Return the complex constant value, if it has one.
5502 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5507 mpfr_init(left_real);
5508 mpfr_init(left_imag);
5510 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5512 mpfr_clear(left_real);
5513 mpfr_clear(left_imag);
5519 mpfr_init(right_real);
5520 mpfr_init(right_imag);
5522 if (!this->right_->complex_constant_value(right_real, right_imag,
5525 mpfr_clear(left_real);
5526 mpfr_clear(left_imag);
5527 mpfr_clear(right_real);
5528 mpfr_clear(right_imag);
5533 if (left_type != right_type
5534 && left_type != NULL
5535 && right_type != NULL
5536 && left_type->base() != right_type->base())
5539 ret = Binary_expression::eval_complex(this->op_, left_type,
5540 left_real, left_imag,
5542 right_real, right_imag,
5545 mpfr_clear(left_real);
5546 mpfr_clear(left_imag);
5547 mpfr_clear(right_real);
5548 mpfr_clear(right_imag);
5556 // Note that the value is being discarded.
5559 Binary_expression::do_discarding_value()
5561 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5562 this->right_->discarding_value();
5564 this->warn_about_unused_value();
5570 Binary_expression::do_type()
5572 if (this->classification() == EXPRESSION_ERROR)
5573 return Type::make_error_type();
5578 case OPERATOR_ANDAND:
5580 case OPERATOR_NOTEQ:
5585 return Type::lookup_bool_type();
5588 case OPERATOR_MINUS:
5595 case OPERATOR_BITCLEAR:
5597 Type* left_type = this->left_->type();
5598 Type* right_type = this->right_->type();
5599 if (left_type->is_error())
5601 else if (right_type->is_error())
5603 else if (!Type::are_compatible_for_binop(left_type, right_type))
5605 this->report_error(_("incompatible types in binary expression"));
5606 return Type::make_error_type();
5608 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
5610 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5612 else if (!left_type->is_abstract())
5614 else if (!right_type->is_abstract())
5616 else if (left_type->complex_type() != NULL)
5618 else if (right_type->complex_type() != NULL)
5620 else if (left_type->float_type() != NULL)
5622 else if (right_type->float_type() != NULL)
5628 case OPERATOR_LSHIFT:
5629 case OPERATOR_RSHIFT:
5630 return this->left_->type();
5637 // Set type for a binary expression.
5640 Binary_expression::do_determine_type(const Type_context* context)
5642 Type* tleft = this->left_->type();
5643 Type* tright = this->right_->type();
5645 // Both sides should have the same type, except for the shift
5646 // operations. For a comparison, we should ignore the incoming
5649 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5650 || this->op_ == OPERATOR_RSHIFT);
5652 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5653 || this->op_ == OPERATOR_NOTEQ
5654 || this->op_ == OPERATOR_LT
5655 || this->op_ == OPERATOR_LE
5656 || this->op_ == OPERATOR_GT
5657 || this->op_ == OPERATOR_GE);
5659 Type_context subcontext(*context);
5663 // In a comparison, the context does not determine the types of
5665 subcontext.type = NULL;
5668 // Set the context for the left hand operand.
5671 // The right hand operand plays no role in determining the type
5672 // of the left hand operand. A shift of an abstract integer in
5673 // a string context gets special treatment, which may be a
5675 if (subcontext.type != NULL
5676 && subcontext.type->is_string_type()
5677 && tleft->is_abstract())
5678 error_at(this->location(), "shift of non-integer operand");
5680 else if (!tleft->is_abstract())
5681 subcontext.type = tleft;
5682 else if (!tright->is_abstract())
5683 subcontext.type = tright;
5684 else if (subcontext.type == NULL)
5686 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5687 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5688 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5690 // Both sides have an abstract integer, abstract float, or
5691 // abstract complex type. Just let CONTEXT determine
5692 // whether they may remain abstract or not.
5694 else if (tleft->complex_type() != NULL)
5695 subcontext.type = tleft;
5696 else if (tright->complex_type() != NULL)
5697 subcontext.type = tright;
5698 else if (tleft->float_type() != NULL)
5699 subcontext.type = tleft;
5700 else if (tright->float_type() != NULL)
5701 subcontext.type = tright;
5703 subcontext.type = tleft;
5705 if (subcontext.type != NULL && !context->may_be_abstract)
5706 subcontext.type = subcontext.type->make_non_abstract_type();
5709 this->left_->determine_type(&subcontext);
5711 // The context for the right hand operand is the same as for the
5712 // left hand operand, except for a shift operator.
5715 subcontext.type = Type::lookup_integer_type("uint");
5716 subcontext.may_be_abstract = false;
5719 this->right_->determine_type(&subcontext);
5722 // Report an error if the binary operator OP does not support TYPE.
5723 // Return whether the operation is OK. This should not be used for
5727 Binary_expression::check_operator_type(Operator op, Type* type,
5728 source_location location)
5733 case OPERATOR_ANDAND:
5734 if (!type->is_boolean_type())
5736 error_at(location, "expected boolean type");
5742 case OPERATOR_NOTEQ:
5743 if (type->integer_type() == NULL
5744 && type->float_type() == NULL
5745 && type->complex_type() == NULL
5746 && !type->is_string_type()
5747 && type->points_to() == NULL
5748 && !type->is_nil_type()
5749 && !type->is_boolean_type()
5750 && type->interface_type() == NULL
5751 && (type->array_type() == NULL
5752 || type->array_type()->length() != NULL)
5753 && type->map_type() == NULL
5754 && type->channel_type() == NULL
5755 && type->function_type() == NULL)
5758 ("expected integer, floating, complex, string, pointer, "
5759 "boolean, interface, slice, map, channel, "
5760 "or function type"));
5769 if (type->integer_type() == NULL
5770 && type->float_type() == NULL
5771 && !type->is_string_type())
5773 error_at(location, "expected integer, floating, or string type");
5779 case OPERATOR_PLUSEQ:
5780 if (type->integer_type() == NULL
5781 && type->float_type() == NULL
5782 && type->complex_type() == NULL
5783 && !type->is_string_type())
5786 "expected integer, floating, complex, or string type");
5791 case OPERATOR_MINUS:
5792 case OPERATOR_MINUSEQ:
5794 case OPERATOR_MULTEQ:
5796 case OPERATOR_DIVEQ:
5797 if (type->integer_type() == NULL
5798 && type->float_type() == NULL
5799 && type->complex_type() == NULL)
5801 error_at(location, "expected integer, floating, or complex type");
5807 case OPERATOR_MODEQ:
5811 case OPERATOR_ANDEQ:
5813 case OPERATOR_XOREQ:
5814 case OPERATOR_BITCLEAR:
5815 case OPERATOR_BITCLEAREQ:
5816 if (type->integer_type() == NULL)
5818 error_at(location, "expected integer type");
5833 Binary_expression::do_check_types(Gogo*)
5835 if (this->classification() == EXPRESSION_ERROR)
5838 Type* left_type = this->left_->type();
5839 Type* right_type = this->right_->type();
5840 if (left_type->is_error() || right_type->is_error())
5842 this->set_is_error();
5846 if (this->op_ == OPERATOR_EQEQ
5847 || this->op_ == OPERATOR_NOTEQ
5848 || this->op_ == OPERATOR_LT
5849 || this->op_ == OPERATOR_LE
5850 || this->op_ == OPERATOR_GT
5851 || this->op_ == OPERATOR_GE)
5853 if (!Type::are_assignable(left_type, right_type, NULL)
5854 && !Type::are_assignable(right_type, left_type, NULL))
5856 this->report_error(_("incompatible types in binary expression"));
5859 if (!Binary_expression::check_operator_type(this->op_, left_type,
5861 || !Binary_expression::check_operator_type(this->op_, right_type,
5864 this->set_is_error();
5868 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5870 if (!Type::are_compatible_for_binop(left_type, right_type))
5872 this->report_error(_("incompatible types in binary expression"));
5875 if (!Binary_expression::check_operator_type(this->op_, left_type,
5878 this->set_is_error();
5884 if (left_type->integer_type() == NULL)
5885 this->report_error(_("shift of non-integer operand"));
5887 if (!right_type->is_abstract()
5888 && (right_type->integer_type() == NULL
5889 || !right_type->integer_type()->is_unsigned()))
5890 this->report_error(_("shift count not unsigned integer"));
5896 if (this->right_->integer_constant_value(true, val, &type))
5898 if (mpz_sgn(val) < 0)
5900 this->report_error(_("negative shift count"));
5902 source_location rloc = this->right_->location();
5903 this->right_ = Expression::make_integer(&val, right_type,
5912 // Get a tree for a binary expression.
5915 Binary_expression::do_get_tree(Translate_context* context)
5917 tree left = this->left_->get_tree(context);
5918 tree right = this->right_->get_tree(context);
5920 if (left == error_mark_node || right == error_mark_node)
5921 return error_mark_node;
5923 enum tree_code code;
5924 bool use_left_type = true;
5925 bool is_shift_op = false;
5929 case OPERATOR_NOTEQ:
5934 return Expression::comparison_tree(context, this->op_,
5935 this->left_->type(), left,
5936 this->right_->type(), right,
5940 code = TRUTH_ORIF_EXPR;
5941 use_left_type = false;
5943 case OPERATOR_ANDAND:
5944 code = TRUTH_ANDIF_EXPR;
5945 use_left_type = false;
5950 case OPERATOR_MINUS:
5954 code = BIT_IOR_EXPR;
5957 code = BIT_XOR_EXPR;
5964 Type *t = this->left_->type();
5965 if (t->float_type() != NULL || t->complex_type() != NULL)
5968 code = TRUNC_DIV_EXPR;
5972 code = TRUNC_MOD_EXPR;
5974 case OPERATOR_LSHIFT:
5978 case OPERATOR_RSHIFT:
5983 code = BIT_AND_EXPR;
5985 case OPERATOR_BITCLEAR:
5986 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5987 code = BIT_AND_EXPR;
5993 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5995 if (this->left_->type()->is_string_type())
5997 go_assert(this->op_ == OPERATOR_PLUS);
5998 Type* st = Type::make_string_type();
5999 tree string_type = type_to_tree(st->get_backend(context->gogo()));
6000 static tree string_plus_decl;
6001 return Gogo::call_builtin(&string_plus_decl,
6012 tree compute_type = excess_precision_type(type);
6013 if (compute_type != NULL_TREE)
6015 left = ::convert(compute_type, left);
6016 right = ::convert(compute_type, right);
6019 tree eval_saved = NULL_TREE;
6022 // Make sure the values are evaluated.
6023 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
6025 left = save_expr(left);
6028 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
6030 right = save_expr(right);
6031 if (eval_saved == NULL_TREE)
6034 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
6035 void_type_node, eval_saved, right);
6039 tree ret = fold_build2_loc(this->location(),
6041 compute_type != NULL_TREE ? compute_type : type,
6044 if (compute_type != NULL_TREE)
6045 ret = ::convert(type, ret);
6047 // In Go, a shift larger than the size of the type is well-defined.
6048 // This is not true in GENERIC, so we need to insert a conditional.
6051 go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
6052 go_assert(this->left_->type()->integer_type() != NULL);
6053 int bits = TYPE_PRECISION(TREE_TYPE(left));
6055 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
6056 build_int_cst_type(TREE_TYPE(right), bits));
6058 tree overflow_result = fold_convert_loc(this->location(),
6061 if (this->op_ == OPERATOR_RSHIFT
6062 && !this->left_->type()->integer_type()->is_unsigned())
6064 tree neg = fold_build2_loc(this->location(), LT_EXPR,
6065 boolean_type_node, left,
6066 fold_convert_loc(this->location(),
6068 integer_zero_node));
6069 tree neg_one = fold_build2_loc(this->location(),
6070 MINUS_EXPR, TREE_TYPE(left),
6071 fold_convert_loc(this->location(),
6074 fold_convert_loc(this->location(),
6077 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
6078 TREE_TYPE(left), neg, neg_one,
6082 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
6083 compare, ret, overflow_result);
6085 if (eval_saved != NULL_TREE)
6086 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
6087 TREE_TYPE(ret), eval_saved, ret);
6093 // Export a binary expression.
6096 Binary_expression::do_export(Export* exp) const
6098 exp->write_c_string("(");
6099 this->left_->export_expression(exp);
6103 exp->write_c_string(" || ");
6105 case OPERATOR_ANDAND:
6106 exp->write_c_string(" && ");
6109 exp->write_c_string(" == ");
6111 case OPERATOR_NOTEQ:
6112 exp->write_c_string(" != ");
6115 exp->write_c_string(" < ");
6118 exp->write_c_string(" <= ");
6121 exp->write_c_string(" > ");
6124 exp->write_c_string(" >= ");
6127 exp->write_c_string(" + ");
6129 case OPERATOR_MINUS:
6130 exp->write_c_string(" - ");
6133 exp->write_c_string(" | ");
6136 exp->write_c_string(" ^ ");
6139 exp->write_c_string(" * ");
6142 exp->write_c_string(" / ");
6145 exp->write_c_string(" % ");
6147 case OPERATOR_LSHIFT:
6148 exp->write_c_string(" << ");
6150 case OPERATOR_RSHIFT:
6151 exp->write_c_string(" >> ");
6154 exp->write_c_string(" & ");
6156 case OPERATOR_BITCLEAR:
6157 exp->write_c_string(" &^ ");
6162 this->right_->export_expression(exp);
6163 exp->write_c_string(")");
6166 // Import a binary expression.
6169 Binary_expression::do_import(Import* imp)
6171 imp->require_c_string("(");
6173 Expression* left = Expression::import_expression(imp);
6176 if (imp->match_c_string(" || "))
6181 else if (imp->match_c_string(" && "))
6183 op = OPERATOR_ANDAND;
6186 else if (imp->match_c_string(" == "))
6191 else if (imp->match_c_string(" != "))
6193 op = OPERATOR_NOTEQ;
6196 else if (imp->match_c_string(" < "))
6201 else if (imp->match_c_string(" <= "))
6206 else if (imp->match_c_string(" > "))
6211 else if (imp->match_c_string(" >= "))
6216 else if (imp->match_c_string(" + "))
6221 else if (imp->match_c_string(" - "))
6223 op = OPERATOR_MINUS;
6226 else if (imp->match_c_string(" | "))
6231 else if (imp->match_c_string(" ^ "))
6236 else if (imp->match_c_string(" * "))
6241 else if (imp->match_c_string(" / "))
6246 else if (imp->match_c_string(" % "))
6251 else if (imp->match_c_string(" << "))
6253 op = OPERATOR_LSHIFT;
6256 else if (imp->match_c_string(" >> "))
6258 op = OPERATOR_RSHIFT;
6261 else if (imp->match_c_string(" & "))
6266 else if (imp->match_c_string(" &^ "))
6268 op = OPERATOR_BITCLEAR;
6273 error_at(imp->location(), "unrecognized binary operator");
6274 return Expression::make_error(imp->location());
6277 Expression* right = Expression::import_expression(imp);
6279 imp->require_c_string(")");
6281 return Expression::make_binary(op, left, right, imp->location());
6284 // Make a binary expression.
6287 Expression::make_binary(Operator op, Expression* left, Expression* right,
6288 source_location location)
6290 return new Binary_expression(op, left, right, location);
6293 // Implement a comparison.
6296 Expression::comparison_tree(Translate_context* context, Operator op,
6297 Type* left_type, tree left_tree,
6298 Type* right_type, tree right_tree,
6299 source_location location)
6301 enum tree_code code;
6307 case OPERATOR_NOTEQ:
6326 if (left_type->is_string_type() && right_type->is_string_type())
6328 Type* st = Type::make_string_type();
6329 tree string_type = type_to_tree(st->get_backend(context->gogo()));
6330 static tree string_compare_decl;
6331 left_tree = Gogo::call_builtin(&string_compare_decl,
6340 right_tree = build_int_cst_type(integer_type_node, 0);
6342 else if ((left_type->interface_type() != NULL
6343 && right_type->interface_type() == NULL
6344 && !right_type->is_nil_type())
6345 || (left_type->interface_type() == NULL
6346 && !left_type->is_nil_type()
6347 && right_type->interface_type() != NULL))
6349 // Comparing an interface value to a non-interface value.
6350 if (left_type->interface_type() == NULL)
6352 std::swap(left_type, right_type);
6353 std::swap(left_tree, right_tree);
6356 // The right operand is not an interface. We need to take its
6357 // address if it is not a pointer.
6360 if (right_type->points_to() != NULL)
6362 make_tmp = NULL_TREE;
6365 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
6367 make_tmp = NULL_TREE;
6368 arg = build_fold_addr_expr_loc(location, right_tree);
6369 if (DECL_P(right_tree))
6370 TREE_ADDRESSABLE(right_tree) = 1;
6374 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6375 get_name(right_tree));
6376 DECL_IGNORED_P(tmp) = 0;
6377 DECL_INITIAL(tmp) = right_tree;
6378 TREE_ADDRESSABLE(tmp) = 1;
6379 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6380 SET_EXPR_LOCATION(make_tmp, location);
6381 arg = build_fold_addr_expr_loc(location, tmp);
6383 arg = fold_convert_loc(location, ptr_type_node, arg);
6385 tree descriptor = right_type->type_descriptor_pointer(context->gogo());
6387 if (left_type->interface_type()->is_empty())
6389 static tree empty_interface_value_compare_decl;
6390 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6392 "__go_empty_interface_value_compare",
6395 TREE_TYPE(left_tree),
6397 TREE_TYPE(descriptor),
6401 if (left_tree == error_mark_node)
6402 return error_mark_node;
6403 // This can panic if the type is not comparable.
6404 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6408 static tree interface_value_compare_decl;
6409 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6411 "__go_interface_value_compare",
6414 TREE_TYPE(left_tree),
6416 TREE_TYPE(descriptor),
6420 if (left_tree == error_mark_node)
6421 return error_mark_node;
6422 // This can panic if the type is not comparable.
6423 TREE_NOTHROW(interface_value_compare_decl) = 0;
6425 right_tree = build_int_cst_type(integer_type_node, 0);
6427 if (make_tmp != NULL_TREE)
6428 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6431 else if (left_type->interface_type() != NULL
6432 && right_type->interface_type() != NULL)
6434 if (left_type->interface_type()->is_empty()
6435 && right_type->interface_type()->is_empty())
6437 static tree empty_interface_compare_decl;
6438 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6440 "__go_empty_interface_compare",
6443 TREE_TYPE(left_tree),
6445 TREE_TYPE(right_tree),
6447 if (left_tree == error_mark_node)
6448 return error_mark_node;
6449 // This can panic if the type is uncomparable.
6450 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6452 else if (!left_type->interface_type()->is_empty()
6453 && !right_type->interface_type()->is_empty())
6455 static tree interface_compare_decl;
6456 left_tree = Gogo::call_builtin(&interface_compare_decl,
6458 "__go_interface_compare",
6461 TREE_TYPE(left_tree),
6463 TREE_TYPE(right_tree),
6465 if (left_tree == error_mark_node)
6466 return error_mark_node;
6467 // This can panic if the type is uncomparable.
6468 TREE_NOTHROW(interface_compare_decl) = 0;
6472 if (left_type->interface_type()->is_empty())
6474 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6475 std::swap(left_type, right_type);
6476 std::swap(left_tree, right_tree);
6478 go_assert(!left_type->interface_type()->is_empty());
6479 go_assert(right_type->interface_type()->is_empty());
6480 static tree interface_empty_compare_decl;
6481 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
6483 "__go_interface_empty_compare",
6486 TREE_TYPE(left_tree),
6488 TREE_TYPE(right_tree),
6490 if (left_tree == error_mark_node)
6491 return error_mark_node;
6492 // This can panic if the type is uncomparable.
6493 TREE_NOTHROW(interface_empty_compare_decl) = 0;
6496 right_tree = build_int_cst_type(integer_type_node, 0);
6499 if (left_type->is_nil_type()
6500 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6502 std::swap(left_type, right_type);
6503 std::swap(left_tree, right_tree);
6506 if (right_type->is_nil_type())
6508 if (left_type->array_type() != NULL
6509 && left_type->array_type()->length() == NULL)
6511 Array_type* at = left_type->array_type();
6512 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6513 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6515 else if (left_type->interface_type() != NULL)
6517 // An interface is nil if the first field is nil.
6518 tree left_type_tree = TREE_TYPE(left_tree);
6519 go_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6520 tree field = TYPE_FIELDS(left_type_tree);
6521 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6523 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6527 go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6528 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6532 if (left_tree == error_mark_node || right_tree == error_mark_node)
6533 return error_mark_node;
6535 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6536 if (CAN_HAVE_LOCATION_P(ret))
6537 SET_EXPR_LOCATION(ret, location);
6541 // Class Bound_method_expression.
6546 Bound_method_expression::do_traverse(Traverse* traverse)
6548 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
6549 return TRAVERSE_EXIT;
6550 return Expression::traverse(&this->method_, traverse);
6553 // Return the type of a bound method expression. The type of this
6554 // object is really the type of the method with no receiver. We
6555 // should be able to get away with just returning the type of the
6559 Bound_method_expression::do_type()
6561 return this->method_->type();
6564 // Determine the types of a method expression.
6567 Bound_method_expression::do_determine_type(const Type_context*)
6569 this->method_->determine_type_no_context();
6570 Type* mtype = this->method_->type();
6571 Function_type* fntype = mtype == NULL ? NULL : mtype->function_type();
6572 if (fntype == NULL || !fntype->is_method())
6573 this->expr_->determine_type_no_context();
6576 Type_context subcontext(fntype->receiver()->type(), false);
6577 this->expr_->determine_type(&subcontext);
6581 // Check the types of a method expression.
6584 Bound_method_expression::do_check_types(Gogo*)
6586 Type* type = this->method_->type()->deref();
6588 || type->function_type() == NULL
6589 || !type->function_type()->is_method())
6590 this->report_error(_("object is not a method"));
6593 Type* rtype = type->function_type()->receiver()->type()->deref();
6594 Type* etype = (this->expr_type_ != NULL
6596 : this->expr_->type());
6597 etype = etype->deref();
6598 if (!Type::are_identical(rtype, etype, true, NULL))
6599 this->report_error(_("method type does not match object type"));
6603 // Get the tree for a method expression. There is no standard tree
6604 // representation for this. The only places it may currently be used
6605 // are in a Call_expression or a Go_statement, which will take it
6606 // apart directly. So this has nothing to do at present.
6609 Bound_method_expression::do_get_tree(Translate_context*)
6611 error_at(this->location(), "reference to method other than calling it");
6612 return error_mark_node;
6615 // Make a method expression.
6617 Bound_method_expression*
6618 Expression::make_bound_method(Expression* expr, Expression* method,
6619 source_location location)
6621 return new Bound_method_expression(expr, method, location);
6624 // Class Builtin_call_expression. This is used for a call to a
6625 // builtin function.
6627 class Builtin_call_expression : public Call_expression
6630 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6631 bool is_varargs, source_location location);
6634 // This overrides Call_expression::do_lower.
6636 do_lower(Gogo*, Named_object*, int);
6639 do_is_constant() const;
6642 do_integer_constant_value(bool, mpz_t, Type**) const;
6645 do_float_constant_value(mpfr_t, Type**) const;
6648 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6654 do_determine_type(const Type_context*);
6657 do_check_types(Gogo*);
6662 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6663 this->args()->copy(),
6669 do_get_tree(Translate_context*);
6672 do_export(Export*) const;
6675 do_is_recover_call() const;
6678 do_set_recover_arg(Expression*);
6681 // The builtin functions.
6682 enum Builtin_function_code
6686 // Predeclared builtin functions.
6702 // Builtin functions from the unsafe package.
6715 real_imag_type(Type*);
6718 complex_type(Type*);
6720 // A pointer back to the general IR structure. This avoids a global
6721 // variable, or passing it around everywhere.
6723 // The builtin function being called.
6724 Builtin_function_code code_;
6725 // Used to stop endless loops when the length of an array uses len
6726 // or cap of the array itself.
6730 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6732 Expression_list* args,
6734 source_location location)
6735 : Call_expression(fn, args, is_varargs, location),
6736 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
6738 Func_expression* fnexp = this->fn()->func_expression();
6739 go_assert(fnexp != NULL);
6740 const std::string& name(fnexp->named_object()->name());
6741 if (name == "append")
6742 this->code_ = BUILTIN_APPEND;
6743 else if (name == "cap")
6744 this->code_ = BUILTIN_CAP;
6745 else if (name == "close")
6746 this->code_ = BUILTIN_CLOSE;
6747 else if (name == "complex")
6748 this->code_ = BUILTIN_COMPLEX;
6749 else if (name == "copy")
6750 this->code_ = BUILTIN_COPY;
6751 else if (name == "imag")
6752 this->code_ = BUILTIN_IMAG;
6753 else if (name == "len")
6754 this->code_ = BUILTIN_LEN;
6755 else if (name == "make")
6756 this->code_ = BUILTIN_MAKE;
6757 else if (name == "new")
6758 this->code_ = BUILTIN_NEW;
6759 else if (name == "panic")
6760 this->code_ = BUILTIN_PANIC;
6761 else if (name == "print")
6762 this->code_ = BUILTIN_PRINT;
6763 else if (name == "println")
6764 this->code_ = BUILTIN_PRINTLN;
6765 else if (name == "real")
6766 this->code_ = BUILTIN_REAL;
6767 else if (name == "recover")
6768 this->code_ = BUILTIN_RECOVER;
6769 else if (name == "Alignof")
6770 this->code_ = BUILTIN_ALIGNOF;
6771 else if (name == "Offsetof")
6772 this->code_ = BUILTIN_OFFSETOF;
6773 else if (name == "Sizeof")
6774 this->code_ = BUILTIN_SIZEOF;
6779 // Return whether this is a call to recover. This is a virtual
6780 // function called from the parent class.
6783 Builtin_call_expression::do_is_recover_call() const
6785 if (this->classification() == EXPRESSION_ERROR)
6787 return this->code_ == BUILTIN_RECOVER;
6790 // Set the argument for a call to recover.
6793 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6795 const Expression_list* args = this->args();
6796 go_assert(args == NULL || args->empty());
6797 Expression_list* new_args = new Expression_list();
6798 new_args->push_back(arg);
6799 this->set_args(new_args);
6802 // A traversal class which looks for a call expression.
6804 class Find_call_expression : public Traverse
6807 Find_call_expression()
6808 : Traverse(traverse_expressions),
6813 expression(Expression**);
6817 { return this->found_; }
6824 Find_call_expression::expression(Expression** pexpr)
6826 if ((*pexpr)->call_expression() != NULL)
6828 this->found_ = true;
6829 return TRAVERSE_EXIT;
6831 return TRAVERSE_CONTINUE;
6834 // Lower a builtin call expression. This turns new and make into
6835 // specific expressions. We also convert to a constant if we can.
6838 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int)
6840 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
6842 this->report_error(_("invalid use of %<...%> with builtin function"));
6843 return Expression::make_error(this->location());
6846 if (this->code_ == BUILTIN_NEW)
6848 const Expression_list* args = this->args();
6849 if (args == NULL || args->size() < 1)
6850 this->report_error(_("not enough arguments"));
6851 else if (args->size() > 1)
6852 this->report_error(_("too many arguments"));
6855 Expression* arg = args->front();
6856 if (!arg->is_type_expression())
6858 error_at(arg->location(), "expected type");
6859 this->set_is_error();
6862 return Expression::make_allocation(arg->type(), this->location());
6865 else if (this->code_ == BUILTIN_MAKE)
6867 const Expression_list* args = this->args();
6868 if (args == NULL || args->size() < 1)
6869 this->report_error(_("not enough arguments"));
6872 Expression* arg = args->front();
6873 if (!arg->is_type_expression())
6875 error_at(arg->location(), "expected type");
6876 this->set_is_error();
6880 Expression_list* newargs;
6881 if (args->size() == 1)
6885 newargs = new Expression_list();
6886 Expression_list::const_iterator p = args->begin();
6888 for (; p != args->end(); ++p)
6889 newargs->push_back(*p);
6891 return Expression::make_make(arg->type(), newargs,
6896 else if (this->is_constant())
6898 // We can only lower len and cap if there are no function calls
6899 // in the arguments. Otherwise we have to make the call.
6900 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6902 Expression* arg = this->one_arg();
6903 if (!arg->is_constant())
6905 Find_call_expression find_call;
6906 Expression::traverse(&arg, &find_call);
6907 if (find_call.found())
6915 if (this->integer_constant_value(true, ival, &type))
6917 Expression* ret = Expression::make_integer(&ival, type,
6926 if (this->float_constant_value(rval, &type))
6928 Expression* ret = Expression::make_float(&rval, type,
6936 if (this->complex_constant_value(rval, imag, &type))
6938 Expression* ret = Expression::make_complex(&rval, &imag, type,
6947 else if (this->code_ == BUILTIN_RECOVER)
6949 if (function != NULL)
6950 function->func_value()->set_calls_recover();
6953 // Calling recover outside of a function always returns the
6954 // nil empty interface.
6955 Type* eface = Type::make_interface_type(NULL, this->location());
6956 return Expression::make_cast(eface,
6957 Expression::make_nil(this->location()),
6961 else if (this->code_ == BUILTIN_APPEND)
6963 // Lower the varargs.
6964 const Expression_list* args = this->args();
6965 if (args == NULL || args->empty())
6967 Type* slice_type = args->front()->type();
6968 if (!slice_type->is_open_array_type())
6970 error_at(args->front()->location(), "argument 1 must be a slice");
6971 this->set_is_error();
6974 return this->lower_varargs(gogo, function, slice_type, 2);
6980 // Return the type of the real or imag functions, given the type of
6981 // the argument. We need to map complex to float, complex64 to
6982 // float32, and complex128 to float64, so it has to be done by name.
6983 // This returns NULL if it can't figure out the type.
6986 Builtin_call_expression::real_imag_type(Type* arg_type)
6988 if (arg_type == NULL || arg_type->is_abstract())
6990 Named_type* nt = arg_type->named_type();
6993 while (nt->real_type()->named_type() != NULL)
6994 nt = nt->real_type()->named_type();
6995 if (nt->name() == "complex64")
6996 return Type::lookup_float_type("float32");
6997 else if (nt->name() == "complex128")
6998 return Type::lookup_float_type("float64");
7003 // Return the type of the complex function, given the type of one of the
7004 // argments. Like real_imag_type, we have to map by name.
7007 Builtin_call_expression::complex_type(Type* arg_type)
7009 if (arg_type == NULL || arg_type->is_abstract())
7011 Named_type* nt = arg_type->named_type();
7014 while (nt->real_type()->named_type() != NULL)
7015 nt = nt->real_type()->named_type();
7016 if (nt->name() == "float32")
7017 return Type::lookup_complex_type("complex64");
7018 else if (nt->name() == "float64")
7019 return Type::lookup_complex_type("complex128");
7024 // Return a single argument, or NULL if there isn't one.
7027 Builtin_call_expression::one_arg() const
7029 const Expression_list* args = this->args();
7030 if (args->size() != 1)
7032 return args->front();
7035 // Return whether this is constant: len of a string, or len or cap of
7036 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
7039 Builtin_call_expression::do_is_constant() const
7041 switch (this->code_)
7049 Expression* arg = this->one_arg();
7052 Type* arg_type = arg->type();
7054 if (arg_type->points_to() != NULL
7055 && arg_type->points_to()->array_type() != NULL
7056 && !arg_type->points_to()->is_open_array_type())
7057 arg_type = arg_type->points_to();
7059 if (arg_type->array_type() != NULL
7060 && arg_type->array_type()->length() != NULL)
7063 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7066 bool ret = arg->is_constant();
7067 this->seen_ = false;
7073 case BUILTIN_SIZEOF:
7074 case BUILTIN_ALIGNOF:
7075 return this->one_arg() != NULL;
7077 case BUILTIN_OFFSETOF:
7079 Expression* arg = this->one_arg();
7082 return arg->field_reference_expression() != NULL;
7085 case BUILTIN_COMPLEX:
7087 const Expression_list* args = this->args();
7088 if (args != NULL && args->size() == 2)
7089 return args->front()->is_constant() && args->back()->is_constant();
7096 Expression* arg = this->one_arg();
7097 return arg != NULL && arg->is_constant();
7107 // Return an integer constant value if possible.
7110 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
7114 if (this->code_ == BUILTIN_LEN
7115 || this->code_ == BUILTIN_CAP)
7117 Expression* arg = this->one_arg();
7120 Type* arg_type = arg->type();
7122 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7125 if (arg->string_constant_value(&sval))
7127 mpz_set_ui(val, sval.length());
7128 *ptype = Type::lookup_integer_type("int");
7133 if (arg_type->points_to() != NULL
7134 && arg_type->points_to()->array_type() != NULL
7135 && !arg_type->points_to()->is_open_array_type())
7136 arg_type = arg_type->points_to();
7138 if (arg_type->array_type() != NULL
7139 && arg_type->array_type()->length() != NULL)
7143 Expression* e = arg_type->array_type()->length();
7145 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
7146 this->seen_ = false;
7149 *ptype = Type::lookup_integer_type("int");
7154 else if (this->code_ == BUILTIN_SIZEOF
7155 || this->code_ == BUILTIN_ALIGNOF)
7157 Expression* arg = this->one_arg();
7160 Type* arg_type = arg->type();
7161 if (arg_type->is_error())
7163 if (arg_type->is_abstract())
7165 if (arg_type->named_type() != NULL)
7166 arg_type->named_type()->convert(this->gogo_);
7167 tree arg_type_tree = type_to_tree(arg_type->get_backend(this->gogo_));
7168 if (arg_type_tree == error_mark_node)
7170 unsigned long val_long;
7171 if (this->code_ == BUILTIN_SIZEOF)
7173 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
7174 go_assert(TREE_CODE(type_size) == INTEGER_CST);
7175 if (TREE_INT_CST_HIGH(type_size) != 0)
7177 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
7178 val_long = static_cast<unsigned long>(val_wide);
7179 if (val_long != val_wide)
7182 else if (this->code_ == BUILTIN_ALIGNOF)
7184 if (arg->field_reference_expression() == NULL)
7185 val_long = go_type_alignment(arg_type_tree);
7188 // Calling unsafe.Alignof(s.f) returns the alignment of
7189 // the type of f when it is used as a field in a struct.
7190 val_long = go_field_alignment(arg_type_tree);
7195 mpz_set_ui(val, val_long);
7199 else if (this->code_ == BUILTIN_OFFSETOF)
7201 Expression* arg = this->one_arg();
7204 Field_reference_expression* farg = arg->field_reference_expression();
7207 Expression* struct_expr = farg->expr();
7208 Type* st = struct_expr->type();
7209 if (st->struct_type() == NULL)
7211 if (st->named_type() != NULL)
7212 st->named_type()->convert(this->gogo_);
7213 tree struct_tree = type_to_tree(st->get_backend(this->gogo_));
7214 go_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
7215 tree field = TYPE_FIELDS(struct_tree);
7216 for (unsigned int index = farg->field_index(); index > 0; --index)
7218 field = DECL_CHAIN(field);
7219 go_assert(field != NULL_TREE);
7221 HOST_WIDE_INT offset_wide = int_byte_position (field);
7222 if (offset_wide < 0)
7224 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
7225 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
7227 mpz_set_ui(val, offset_long);
7233 // Return a floating point constant value if possible.
7236 Builtin_call_expression::do_float_constant_value(mpfr_t val,
7239 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
7241 Expression* arg = this->one_arg();
7252 if (arg->complex_constant_value(real, imag, &type))
7254 if (this->code_ == BUILTIN_REAL)
7255 mpfr_set(val, real, GMP_RNDN);
7257 mpfr_set(val, imag, GMP_RNDN);
7258 *ptype = Builtin_call_expression::real_imag_type(type);
7270 // Return a complex constant value if possible.
7273 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
7276 if (this->code_ == BUILTIN_COMPLEX)
7278 const Expression_list* args = this->args();
7279 if (args == NULL || args->size() != 2)
7285 if (!args->front()->float_constant_value(r, &rtype))
7296 if (args->back()->float_constant_value(i, &itype)
7297 && Type::are_identical(rtype, itype, false, NULL))
7299 mpfr_set(real, r, GMP_RNDN);
7300 mpfr_set(imag, i, GMP_RNDN);
7301 *ptype = Builtin_call_expression::complex_type(rtype);
7317 Builtin_call_expression::do_type()
7319 switch (this->code_)
7321 case BUILTIN_INVALID:
7328 const Expression_list* args = this->args();
7329 if (args == NULL || args->empty())
7330 return Type::make_error_type();
7331 return Type::make_pointer_type(args->front()->type());
7337 case BUILTIN_ALIGNOF:
7338 case BUILTIN_OFFSETOF:
7339 case BUILTIN_SIZEOF:
7340 return Type::lookup_integer_type("int");
7345 case BUILTIN_PRINTLN:
7346 return Type::make_void_type();
7348 case BUILTIN_RECOVER:
7349 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
7351 case BUILTIN_APPEND:
7353 const Expression_list* args = this->args();
7354 if (args == NULL || args->empty())
7355 return Type::make_error_type();
7356 return args->front()->type();
7362 Expression* arg = this->one_arg();
7364 return Type::make_error_type();
7365 Type* t = arg->type();
7366 if (t->is_abstract())
7367 t = t->make_non_abstract_type();
7368 t = Builtin_call_expression::real_imag_type(t);
7370 t = Type::make_error_type();
7374 case BUILTIN_COMPLEX:
7376 const Expression_list* args = this->args();
7377 if (args == NULL || args->size() != 2)
7378 return Type::make_error_type();
7379 Type* t = args->front()->type();
7380 if (t->is_abstract())
7382 t = args->back()->type();
7383 if (t->is_abstract())
7384 t = t->make_non_abstract_type();
7386 t = Builtin_call_expression::complex_type(t);
7388 t = Type::make_error_type();
7394 // Determine the type.
7397 Builtin_call_expression::do_determine_type(const Type_context* context)
7399 if (!this->determining_types())
7402 this->fn()->determine_type_no_context();
7404 const Expression_list* args = this->args();
7407 Type* arg_type = NULL;
7408 switch (this->code_)
7411 case BUILTIN_PRINTLN:
7412 // Do not force a large integer constant to "int".
7418 arg_type = Builtin_call_expression::complex_type(context->type);
7422 case BUILTIN_COMPLEX:
7424 // For the complex function the type of one operand can
7425 // determine the type of the other, as in a binary expression.
7426 arg_type = Builtin_call_expression::real_imag_type(context->type);
7427 if (args != NULL && args->size() == 2)
7429 Type* t1 = args->front()->type();
7430 Type* t2 = args->front()->type();
7431 if (!t1->is_abstract())
7433 else if (!t2->is_abstract())
7447 for (Expression_list::const_iterator pa = args->begin();
7451 Type_context subcontext;
7452 subcontext.type = arg_type;
7456 // We want to print large constants, we so can't just
7457 // use the appropriate nonabstract type. Use uint64 for
7458 // an integer if we know it is nonnegative, otherwise
7459 // use int64 for a integer, otherwise use float64 for a
7460 // float or complex128 for a complex.
7461 Type* want_type = NULL;
7462 Type* atype = (*pa)->type();
7463 if (atype->is_abstract())
7465 if (atype->integer_type() != NULL)
7470 if (this->integer_constant_value(true, val, &dummy)
7471 && mpz_sgn(val) >= 0)
7472 want_type = Type::lookup_integer_type("uint64");
7474 want_type = Type::lookup_integer_type("int64");
7477 else if (atype->float_type() != NULL)
7478 want_type = Type::lookup_float_type("float64");
7479 else if (atype->complex_type() != NULL)
7480 want_type = Type::lookup_complex_type("complex128");
7481 else if (atype->is_abstract_string_type())
7482 want_type = Type::lookup_string_type();
7483 else if (atype->is_abstract_boolean_type())
7484 want_type = Type::lookup_bool_type();
7487 subcontext.type = want_type;
7491 (*pa)->determine_type(&subcontext);
7496 // If there is exactly one argument, return true. Otherwise give an
7497 // error message and return false.
7500 Builtin_call_expression::check_one_arg()
7502 const Expression_list* args = this->args();
7503 if (args == NULL || args->size() < 1)
7505 this->report_error(_("not enough arguments"));
7508 else if (args->size() > 1)
7510 this->report_error(_("too many arguments"));
7513 if (args->front()->is_error_expression()
7514 || args->front()->type()->is_error())
7516 this->set_is_error();
7522 // Check argument types for a builtin function.
7525 Builtin_call_expression::do_check_types(Gogo*)
7527 switch (this->code_)
7529 case BUILTIN_INVALID:
7537 // The single argument may be either a string or an array or a
7538 // map or a channel, or a pointer to a closed array.
7539 if (this->check_one_arg())
7541 Type* arg_type = this->one_arg()->type();
7542 if (arg_type->points_to() != NULL
7543 && arg_type->points_to()->array_type() != NULL
7544 && !arg_type->points_to()->is_open_array_type())
7545 arg_type = arg_type->points_to();
7546 if (this->code_ == BUILTIN_CAP)
7548 if (!arg_type->is_error()
7549 && arg_type->array_type() == NULL
7550 && arg_type->channel_type() == NULL)
7551 this->report_error(_("argument must be array or slice "
7556 if (!arg_type->is_error()
7557 && !arg_type->is_string_type()
7558 && arg_type->array_type() == NULL
7559 && arg_type->map_type() == NULL
7560 && arg_type->channel_type() == NULL)
7561 this->report_error(_("argument must be string or "
7562 "array or slice or map or channel"));
7569 case BUILTIN_PRINTLN:
7571 const Expression_list* args = this->args();
7574 if (this->code_ == BUILTIN_PRINT)
7575 warning_at(this->location(), 0,
7576 "no arguments for builtin function %<%s%>",
7577 (this->code_ == BUILTIN_PRINT
7583 for (Expression_list::const_iterator p = args->begin();
7587 Type* type = (*p)->type();
7588 if (type->is_error()
7589 || type->is_string_type()
7590 || type->integer_type() != NULL
7591 || type->float_type() != NULL
7592 || type->complex_type() != NULL
7593 || type->is_boolean_type()
7594 || type->points_to() != NULL
7595 || type->interface_type() != NULL
7596 || type->channel_type() != NULL
7597 || type->map_type() != NULL
7598 || type->function_type() != NULL
7599 || type->is_open_array_type())
7602 this->report_error(_("unsupported argument type to "
7603 "builtin function"));
7610 if (this->check_one_arg())
7612 if (this->one_arg()->type()->channel_type() == NULL)
7613 this->report_error(_("argument must be channel"));
7618 case BUILTIN_SIZEOF:
7619 case BUILTIN_ALIGNOF:
7620 this->check_one_arg();
7623 case BUILTIN_RECOVER:
7624 if (this->args() != NULL && !this->args()->empty())
7625 this->report_error(_("too many arguments"));
7628 case BUILTIN_OFFSETOF:
7629 if (this->check_one_arg())
7631 Expression* arg = this->one_arg();
7632 if (arg->field_reference_expression() == NULL)
7633 this->report_error(_("argument must be a field reference"));
7639 const Expression_list* args = this->args();
7640 if (args == NULL || args->size() < 2)
7642 this->report_error(_("not enough arguments"));
7645 else if (args->size() > 2)
7647 this->report_error(_("too many arguments"));
7650 Type* arg1_type = args->front()->type();
7651 Type* arg2_type = args->back()->type();
7652 if (arg1_type->is_error() || arg2_type->is_error())
7656 if (arg1_type->is_open_array_type())
7657 e1 = arg1_type->array_type()->element_type();
7660 this->report_error(_("left argument must be a slice"));
7665 if (arg2_type->is_open_array_type())
7666 e2 = arg2_type->array_type()->element_type();
7667 else if (arg2_type->is_string_type())
7668 e2 = Type::lookup_integer_type("uint8");
7671 this->report_error(_("right argument must be a slice or a string"));
7675 if (!Type::are_identical(e1, e2, true, NULL))
7676 this->report_error(_("element types must be the same"));
7680 case BUILTIN_APPEND:
7682 const Expression_list* args = this->args();
7683 if (args == NULL || args->size() < 2)
7685 this->report_error(_("not enough arguments"));
7688 if (args->size() > 2)
7690 this->report_error(_("too many arguments"));
7694 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
7698 this->report_error(_("arguments 1 and 2 have different types"));
7701 error_at(this->location(),
7702 "arguments 1 and 2 have different types (%s)",
7704 this->set_is_error();
7712 if (this->check_one_arg())
7714 if (this->one_arg()->type()->complex_type() == NULL)
7715 this->report_error(_("argument must have complex type"));
7719 case BUILTIN_COMPLEX:
7721 const Expression_list* args = this->args();
7722 if (args == NULL || args->size() < 2)
7723 this->report_error(_("not enough arguments"));
7724 else if (args->size() > 2)
7725 this->report_error(_("too many arguments"));
7726 else if (args->front()->is_error_expression()
7727 || args->front()->type()->is_error()
7728 || args->back()->is_error_expression()
7729 || args->back()->type()->is_error())
7730 this->set_is_error();
7731 else if (!Type::are_identical(args->front()->type(),
7732 args->back()->type(), true, NULL))
7733 this->report_error(_("complex arguments must have identical types"));
7734 else if (args->front()->type()->float_type() == NULL)
7735 this->report_error(_("complex arguments must have "
7736 "floating-point type"));
7745 // Return the tree for a builtin function.
7748 Builtin_call_expression::do_get_tree(Translate_context* context)
7750 Gogo* gogo = context->gogo();
7751 source_location location = this->location();
7752 switch (this->code_)
7754 case BUILTIN_INVALID:
7762 const Expression_list* args = this->args();
7763 go_assert(args != NULL && args->size() == 1);
7764 Expression* arg = *args->begin();
7765 Type* arg_type = arg->type();
7769 go_assert(saw_errors());
7770 return error_mark_node;
7774 tree arg_tree = arg->get_tree(context);
7776 this->seen_ = false;
7778 if (arg_tree == error_mark_node)
7779 return error_mark_node;
7781 if (arg_type->points_to() != NULL)
7783 arg_type = arg_type->points_to();
7784 go_assert(arg_type->array_type() != NULL
7785 && !arg_type->is_open_array_type());
7786 go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7787 arg_tree = build_fold_indirect_ref(arg_tree);
7791 if (this->code_ == BUILTIN_LEN)
7793 if (arg_type->is_string_type())
7794 val_tree = String_type::length_tree(gogo, arg_tree);
7795 else if (arg_type->array_type() != NULL)
7799 go_assert(saw_errors());
7800 return error_mark_node;
7803 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7804 this->seen_ = false;
7806 else if (arg_type->map_type() != NULL)
7808 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
7809 static tree map_len_fndecl;
7810 val_tree = Gogo::call_builtin(&map_len_fndecl,
7818 else if (arg_type->channel_type() != NULL)
7820 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
7821 static tree chan_len_fndecl;
7822 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7835 if (arg_type->array_type() != NULL)
7839 go_assert(saw_errors());
7840 return error_mark_node;
7843 val_tree = arg_type->array_type()->capacity_tree(gogo,
7845 this->seen_ = false;
7847 else if (arg_type->channel_type() != NULL)
7849 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
7850 static tree chan_cap_fndecl;
7851 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7863 if (val_tree == error_mark_node)
7864 return error_mark_node;
7866 Type* int_type = Type::lookup_integer_type("int");
7867 tree type_tree = type_to_tree(int_type->get_backend(gogo));
7868 if (type_tree == TREE_TYPE(val_tree))
7871 return fold(convert_to_integer(type_tree, val_tree));
7875 case BUILTIN_PRINTLN:
7877 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7878 tree stmt_list = NULL_TREE;
7880 const Expression_list* call_args = this->args();
7881 if (call_args != NULL)
7883 for (Expression_list::const_iterator p = call_args->begin();
7884 p != call_args->end();
7887 if (is_ln && p != call_args->begin())
7889 static tree print_space_fndecl;
7890 tree call = Gogo::call_builtin(&print_space_fndecl,
7895 if (call == error_mark_node)
7896 return error_mark_node;
7897 append_to_statement_list(call, &stmt_list);
7900 Type* type = (*p)->type();
7902 tree arg = (*p)->get_tree(context);
7903 if (arg == error_mark_node)
7904 return error_mark_node;
7908 if (type->is_string_type())
7910 static tree print_string_fndecl;
7911 pfndecl = &print_string_fndecl;
7912 fnname = "__go_print_string";
7914 else if (type->integer_type() != NULL
7915 && type->integer_type()->is_unsigned())
7917 static tree print_uint64_fndecl;
7918 pfndecl = &print_uint64_fndecl;
7919 fnname = "__go_print_uint64";
7920 Type* itype = Type::lookup_integer_type("uint64");
7921 Btype* bitype = itype->get_backend(gogo);
7922 arg = fold_convert_loc(location, type_to_tree(bitype), arg);
7924 else if (type->integer_type() != NULL)
7926 static tree print_int64_fndecl;
7927 pfndecl = &print_int64_fndecl;
7928 fnname = "__go_print_int64";
7929 Type* itype = Type::lookup_integer_type("int64");
7930 Btype* bitype = itype->get_backend(gogo);
7931 arg = fold_convert_loc(location, type_to_tree(bitype), arg);
7933 else if (type->float_type() != NULL)
7935 static tree print_double_fndecl;
7936 pfndecl = &print_double_fndecl;
7937 fnname = "__go_print_double";
7938 arg = fold_convert_loc(location, double_type_node, arg);
7940 else if (type->complex_type() != NULL)
7942 static tree print_complex_fndecl;
7943 pfndecl = &print_complex_fndecl;
7944 fnname = "__go_print_complex";
7945 arg = fold_convert_loc(location, complex_double_type_node,
7948 else if (type->is_boolean_type())
7950 static tree print_bool_fndecl;
7951 pfndecl = &print_bool_fndecl;
7952 fnname = "__go_print_bool";
7954 else if (type->points_to() != NULL
7955 || type->channel_type() != NULL
7956 || type->map_type() != NULL
7957 || type->function_type() != NULL)
7959 static tree print_pointer_fndecl;
7960 pfndecl = &print_pointer_fndecl;
7961 fnname = "__go_print_pointer";
7962 arg = fold_convert_loc(location, ptr_type_node, arg);
7964 else if (type->interface_type() != NULL)
7966 if (type->interface_type()->is_empty())
7968 static tree print_empty_interface_fndecl;
7969 pfndecl = &print_empty_interface_fndecl;
7970 fnname = "__go_print_empty_interface";
7974 static tree print_interface_fndecl;
7975 pfndecl = &print_interface_fndecl;
7976 fnname = "__go_print_interface";
7979 else if (type->is_open_array_type())
7981 static tree print_slice_fndecl;
7982 pfndecl = &print_slice_fndecl;
7983 fnname = "__go_print_slice";
7988 tree call = Gogo::call_builtin(pfndecl,
7995 if (call == error_mark_node)
7996 return error_mark_node;
7997 append_to_statement_list(call, &stmt_list);
8003 static tree print_nl_fndecl;
8004 tree call = Gogo::call_builtin(&print_nl_fndecl,
8009 if (call == error_mark_node)
8010 return error_mark_node;
8011 append_to_statement_list(call, &stmt_list);
8019 const Expression_list* args = this->args();
8020 go_assert(args != NULL && args->size() == 1);
8021 Expression* arg = args->front();
8022 tree arg_tree = arg->get_tree(context);
8023 if (arg_tree == error_mark_node)
8024 return error_mark_node;
8025 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
8026 arg_tree = Expression::convert_for_assignment(context, empty,
8028 arg_tree, location);
8029 static tree panic_fndecl;
8030 tree call = Gogo::call_builtin(&panic_fndecl,
8035 TREE_TYPE(arg_tree),
8037 if (call == error_mark_node)
8038 return error_mark_node;
8039 // This function will throw an exception.
8040 TREE_NOTHROW(panic_fndecl) = 0;
8041 // This function will not return.
8042 TREE_THIS_VOLATILE(panic_fndecl) = 1;
8046 case BUILTIN_RECOVER:
8048 // The argument is set when building recover thunks. It's a
8049 // boolean value which is true if we can recover a value now.
8050 const Expression_list* args = this->args();
8051 go_assert(args != NULL && args->size() == 1);
8052 Expression* arg = args->front();
8053 tree arg_tree = arg->get_tree(context);
8054 if (arg_tree == error_mark_node)
8055 return error_mark_node;
8057 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
8058 tree empty_tree = type_to_tree(empty->get_backend(context->gogo()));
8060 Type* nil_type = Type::make_nil_type();
8061 Expression* nil = Expression::make_nil(location);
8062 tree nil_tree = nil->get_tree(context);
8063 tree empty_nil_tree = Expression::convert_for_assignment(context,
8069 // We need to handle a deferred call to recover specially,
8070 // because it changes whether it can recover a panic or not.
8071 // See test7 in test/recover1.go.
8073 if (this->is_deferred())
8075 static tree deferred_recover_fndecl;
8076 call = Gogo::call_builtin(&deferred_recover_fndecl,
8078 "__go_deferred_recover",
8084 static tree recover_fndecl;
8085 call = Gogo::call_builtin(&recover_fndecl,
8091 if (call == error_mark_node)
8092 return error_mark_node;
8093 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
8094 call, empty_nil_tree);
8099 const Expression_list* args = this->args();
8100 go_assert(args != NULL && args->size() == 1);
8101 Expression* arg = args->front();
8102 tree arg_tree = arg->get_tree(context);
8103 if (arg_tree == error_mark_node)
8104 return error_mark_node;
8105 static tree close_fndecl;
8106 return Gogo::call_builtin(&close_fndecl,
8108 "__go_builtin_close",
8111 TREE_TYPE(arg_tree),
8115 case BUILTIN_SIZEOF:
8116 case BUILTIN_OFFSETOF:
8117 case BUILTIN_ALIGNOF:
8122 bool b = this->integer_constant_value(true, val, &dummy);
8125 go_assert(saw_errors());
8126 return error_mark_node;
8128 Type* int_type = Type::lookup_integer_type("int");
8129 tree type = type_to_tree(int_type->get_backend(gogo));
8130 tree ret = Expression::integer_constant_tree(val, type);
8137 const Expression_list* args = this->args();
8138 go_assert(args != NULL && args->size() == 2);
8139 Expression* arg1 = args->front();
8140 Expression* arg2 = args->back();
8142 tree arg1_tree = arg1->get_tree(context);
8143 tree arg2_tree = arg2->get_tree(context);
8144 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8145 return error_mark_node;
8147 Type* arg1_type = arg1->type();
8148 Array_type* at = arg1_type->array_type();
8149 arg1_tree = save_expr(arg1_tree);
8150 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
8151 tree arg1_len = at->length_tree(gogo, arg1_tree);
8152 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
8153 return error_mark_node;
8155 Type* arg2_type = arg2->type();
8158 if (arg2_type->is_open_array_type())
8160 at = arg2_type->array_type();
8161 arg2_tree = save_expr(arg2_tree);
8162 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8163 arg2_len = at->length_tree(gogo, arg2_tree);
8167 arg2_tree = save_expr(arg2_tree);
8168 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8169 arg2_len = String_type::length_tree(gogo, arg2_tree);
8171 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8172 return error_mark_node;
8174 arg1_len = save_expr(arg1_len);
8175 arg2_len = save_expr(arg2_len);
8176 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
8177 fold_build2_loc(location, LT_EXPR,
8179 arg1_len, arg2_len),
8180 arg1_len, arg2_len);
8181 len = save_expr(len);
8183 Type* element_type = at->element_type();
8184 Btype* element_btype = element_type->get_backend(gogo);
8185 tree element_type_tree = type_to_tree(element_btype);
8186 if (element_type_tree == error_mark_node)
8187 return error_mark_node;
8188 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8189 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
8191 bytecount = fold_build2_loc(location, MULT_EXPR,
8192 TREE_TYPE(element_size),
8193 bytecount, element_size);
8194 bytecount = fold_convert_loc(location, size_type_node, bytecount);
8196 arg1_val = fold_convert_loc(location, ptr_type_node, arg1_val);
8197 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8199 static tree copy_fndecl;
8200 tree call = Gogo::call_builtin(©_fndecl,
8211 if (call == error_mark_node)
8212 return error_mark_node;
8214 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
8218 case BUILTIN_APPEND:
8220 const Expression_list* args = this->args();
8221 go_assert(args != NULL && args->size() == 2);
8222 Expression* arg1 = args->front();
8223 Expression* arg2 = args->back();
8225 tree arg1_tree = arg1->get_tree(context);
8226 tree arg2_tree = arg2->get_tree(context);
8227 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8228 return error_mark_node;
8230 Array_type* at = arg1->type()->array_type();
8231 Type* element_type = at->element_type();
8233 arg2_tree = Expression::convert_for_assignment(context, at,
8237 if (arg2_tree == error_mark_node)
8238 return error_mark_node;
8240 arg2_tree = save_expr(arg2_tree);
8241 tree arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8242 tree arg2_len = at->length_tree(gogo, arg2_tree);
8243 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8244 return error_mark_node;
8245 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8246 arg2_len = fold_convert_loc(location, size_type_node, arg2_len);
8248 tree element_type_tree = type_to_tree(element_type->get_backend(gogo));
8249 if (element_type_tree == error_mark_node)
8250 return error_mark_node;
8251 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8252 element_size = fold_convert_loc(location, size_type_node,
8255 // We rebuild the decl each time since the slice types may
8257 tree append_fndecl = NULL_TREE;
8258 return Gogo::call_builtin(&append_fndecl,
8262 TREE_TYPE(arg1_tree),
8263 TREE_TYPE(arg1_tree),
8276 const Expression_list* args = this->args();
8277 go_assert(args != NULL && args->size() == 1);
8278 Expression* arg = args->front();
8279 tree arg_tree = arg->get_tree(context);
8280 if (arg_tree == error_mark_node)
8281 return error_mark_node;
8282 go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
8283 if (this->code_ == BUILTIN_REAL)
8284 return fold_build1_loc(location, REALPART_EXPR,
8285 TREE_TYPE(TREE_TYPE(arg_tree)),
8288 return fold_build1_loc(location, IMAGPART_EXPR,
8289 TREE_TYPE(TREE_TYPE(arg_tree)),
8293 case BUILTIN_COMPLEX:
8295 const Expression_list* args = this->args();
8296 go_assert(args != NULL && args->size() == 2);
8297 tree r = args->front()->get_tree(context);
8298 tree i = args->back()->get_tree(context);
8299 if (r == error_mark_node || i == error_mark_node)
8300 return error_mark_node;
8301 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
8302 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
8303 go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8304 return fold_build2_loc(location, COMPLEX_EXPR,
8305 build_complex_type(TREE_TYPE(r)),
8314 // We have to support exporting a builtin call expression, because
8315 // code can set a constant to the result of a builtin expression.
8318 Builtin_call_expression::do_export(Export* exp) const
8325 if (this->integer_constant_value(true, val, &dummy))
8327 Integer_expression::export_integer(exp, val);
8336 if (this->float_constant_value(fval, &dummy))
8338 Float_expression::export_float(exp, fval);
8350 if (this->complex_constant_value(real, imag, &dummy))
8352 Complex_expression::export_complex(exp, real, imag);
8361 error_at(this->location(), "value is not constant");
8365 // A trailing space lets us reliably identify the end of the number.
8366 exp->write_c_string(" ");
8369 // Class Call_expression.
8374 Call_expression::do_traverse(Traverse* traverse)
8376 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8377 return TRAVERSE_EXIT;
8378 if (this->args_ != NULL)
8380 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8381 return TRAVERSE_EXIT;
8383 return TRAVERSE_CONTINUE;
8386 // Lower a call statement.
8389 Call_expression::do_lower(Gogo* gogo, Named_object* function, int)
8391 // A type case can look like a function call.
8392 if (this->fn_->is_type_expression()
8393 && this->args_ != NULL
8394 && this->args_->size() == 1)
8395 return Expression::make_cast(this->fn_->type(), this->args_->front(),
8398 // Recognize a call to a builtin function.
8399 Func_expression* fne = this->fn_->func_expression();
8401 && fne->named_object()->is_function_declaration()
8402 && fne->named_object()->func_declaration_value()->type()->is_builtin())
8403 return new Builtin_call_expression(gogo, this->fn_, this->args_,
8404 this->is_varargs_, this->location());
8406 // Handle an argument which is a call to a function which returns
8407 // multiple results.
8408 if (this->args_ != NULL
8409 && this->args_->size() == 1
8410 && this->args_->front()->call_expression() != NULL
8411 && this->fn_->type()->function_type() != NULL)
8413 Function_type* fntype = this->fn_->type()->function_type();
8414 size_t rc = this->args_->front()->call_expression()->result_count();
8416 && fntype->parameters() != NULL
8417 && (fntype->parameters()->size() == rc
8418 || (fntype->is_varargs()
8419 && fntype->parameters()->size() - 1 <= rc)))
8421 Call_expression* call = this->args_->front()->call_expression();
8422 Expression_list* args = new Expression_list;
8423 for (size_t i = 0; i < rc; ++i)
8424 args->push_back(Expression::make_call_result(call, i));
8425 // We can't return a new call expression here, because this
8426 // one may be referenced by Call_result expressions. We
8427 // also can't delete the old arguments, because we may still
8428 // traverse them somewhere up the call stack. FIXME.
8433 // Handle a call to a varargs function by packaging up the extra
8435 if (this->fn_->type()->function_type() != NULL
8436 && this->fn_->type()->function_type()->is_varargs())
8438 Function_type* fntype = this->fn_->type()->function_type();
8439 const Typed_identifier_list* parameters = fntype->parameters();
8440 go_assert(parameters != NULL && !parameters->empty());
8441 Type* varargs_type = parameters->back().type();
8442 return this->lower_varargs(gogo, function, varargs_type,
8443 parameters->size());
8449 // Lower a call to a varargs function. FUNCTION is the function in
8450 // which the call occurs--it's not the function we are calling.
8451 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8452 // PARAM_COUNT is the number of parameters of the function we are
8453 // calling; the last of these parameters will be the varargs
8457 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
8458 Type* varargs_type, size_t param_count)
8460 if (this->varargs_are_lowered_)
8463 source_location loc = this->location();
8465 go_assert(param_count > 0);
8466 go_assert(varargs_type->is_open_array_type());
8468 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8469 if (arg_count < param_count - 1)
8471 // Not enough arguments; will be caught in check_types.
8475 Expression_list* old_args = this->args_;
8476 Expression_list* new_args = new Expression_list();
8477 bool push_empty_arg = false;
8478 if (old_args == NULL || old_args->empty())
8480 go_assert(param_count == 1);
8481 push_empty_arg = true;
8485 Expression_list::const_iterator pa;
8487 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
8489 if (static_cast<size_t>(i) == param_count)
8491 new_args->push_back(*pa);
8494 // We have reached the varargs parameter.
8496 bool issued_error = false;
8497 if (pa == old_args->end())
8498 push_empty_arg = true;
8499 else if (pa + 1 == old_args->end() && this->is_varargs_)
8500 new_args->push_back(*pa);
8501 else if (this->is_varargs_)
8503 this->report_error(_("too many arguments"));
8508 Type* element_type = varargs_type->array_type()->element_type();
8509 Expression_list* vals = new Expression_list;
8510 for (; pa != old_args->end(); ++pa, ++i)
8512 // Check types here so that we get a better message.
8513 Type* patype = (*pa)->type();
8514 source_location paloc = (*pa)->location();
8515 if (!this->check_argument_type(i, element_type, patype,
8516 paloc, issued_error))
8518 vals->push_back(*pa);
8521 Expression::make_slice_composite_literal(varargs_type, vals, loc);
8522 new_args->push_back(val);
8527 new_args->push_back(Expression::make_nil(loc));
8529 // We can't return a new call expression here, because this one may
8530 // be referenced by Call_result expressions. FIXME.
8531 if (old_args != NULL)
8533 this->args_ = new_args;
8534 this->varargs_are_lowered_ = true;
8536 // Lower all the new subexpressions.
8537 Expression* ret = this;
8538 gogo->lower_expression(function, &ret);
8539 go_assert(ret == this);
8543 // Get the function type. Returns NULL if we don't know the type. If
8544 // this returns NULL, and if_ERROR is true, issues an error.
8547 Call_expression::get_function_type() const
8549 return this->fn_->type()->function_type();
8552 // Return the number of values which this call will return.
8555 Call_expression::result_count() const
8557 const Function_type* fntype = this->get_function_type();
8560 if (fntype->results() == NULL)
8562 return fntype->results()->size();
8565 // Return whether this is a call to the predeclared function recover.
8568 Call_expression::is_recover_call() const
8570 return this->do_is_recover_call();
8573 // Set the argument to the recover function.
8576 Call_expression::set_recover_arg(Expression* arg)
8578 this->do_set_recover_arg(arg);
8581 // Virtual functions also implemented by Builtin_call_expression.
8584 Call_expression::do_is_recover_call() const
8590 Call_expression::do_set_recover_arg(Expression*)
8598 Call_expression::do_type()
8600 if (this->type_ != NULL)
8604 Function_type* fntype = this->get_function_type();
8606 return Type::make_error_type();
8608 const Typed_identifier_list* results = fntype->results();
8609 if (results == NULL)
8610 ret = Type::make_void_type();
8611 else if (results->size() == 1)
8612 ret = results->begin()->type();
8614 ret = Type::make_call_multiple_result_type(this);
8621 // Determine types for a call expression. We can use the function
8622 // parameter types to set the types of the arguments.
8625 Call_expression::do_determine_type(const Type_context*)
8627 if (!this->determining_types())
8630 this->fn_->determine_type_no_context();
8631 Function_type* fntype = this->get_function_type();
8632 const Typed_identifier_list* parameters = NULL;
8634 parameters = fntype->parameters();
8635 if (this->args_ != NULL)
8637 Typed_identifier_list::const_iterator pt;
8638 if (parameters != NULL)
8639 pt = parameters->begin();
8640 for (Expression_list::const_iterator pa = this->args_->begin();
8641 pa != this->args_->end();
8644 if (parameters != NULL && pt != parameters->end())
8646 Type_context subcontext(pt->type(), false);
8647 (*pa)->determine_type(&subcontext);
8651 (*pa)->determine_type_no_context();
8656 // Called when determining types for a Call_expression. Return true
8657 // if we should go ahead, false if they have already been determined.
8660 Call_expression::determining_types()
8662 if (this->types_are_determined_)
8666 this->types_are_determined_ = true;
8671 // Check types for parameter I.
8674 Call_expression::check_argument_type(int i, const Type* parameter_type,
8675 const Type* argument_type,
8676 source_location argument_location,
8680 if (!Type::are_assignable(parameter_type, argument_type, &reason))
8685 error_at(argument_location, "argument %d has incompatible type", i);
8687 error_at(argument_location,
8688 "argument %d has incompatible type (%s)",
8691 this->set_is_error();
8700 Call_expression::do_check_types(Gogo*)
8702 Function_type* fntype = this->get_function_type();
8705 if (!this->fn_->type()->is_error())
8706 this->report_error(_("expected function"));
8710 if (fntype->is_method())
8712 // We don't support pointers to methods, so the function has to
8713 // be a bound method expression.
8714 Bound_method_expression* bme = this->fn_->bound_method_expression();
8717 this->report_error(_("method call without object"));
8720 Type* first_arg_type = bme->first_argument()->type();
8721 if (first_arg_type->points_to() == NULL)
8723 // When passing a value, we need to check that we are
8724 // permitted to copy it. The language permits copying
8725 // hidden fields for a method receiver.
8727 if (!Type::are_assignable_hidden_ok(fntype->receiver()->type(),
8728 first_arg_type, &reason))
8731 this->report_error(_("incompatible type for receiver"));
8734 error_at(this->location(),
8735 "incompatible type for receiver (%s)",
8737 this->set_is_error();
8743 // Note that varargs was handled by the lower_varargs() method, so
8744 // we don't have to worry about it here.
8746 const Typed_identifier_list* parameters = fntype->parameters();
8747 if (this->args_ == NULL)
8749 if (parameters != NULL && !parameters->empty())
8750 this->report_error(_("not enough arguments"));
8752 else if (parameters == NULL)
8753 this->report_error(_("too many arguments"));
8757 Typed_identifier_list::const_iterator pt = parameters->begin();
8758 for (Expression_list::const_iterator pa = this->args_->begin();
8759 pa != this->args_->end();
8762 if (pt == parameters->end())
8764 this->report_error(_("too many arguments"));
8767 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8768 (*pa)->location(), false);
8770 if (pt != parameters->end())
8771 this->report_error(_("not enough arguments"));
8775 // Return whether we have to use a temporary variable to ensure that
8776 // we evaluate this call expression in order. If the call returns no
8777 // results then it will inevitably be executed last. If the call
8778 // returns more than one result then it will be used with Call_result
8779 // expressions. So we only have to use a temporary variable if the
8780 // call returns exactly one result.
8783 Call_expression::do_must_eval_in_order() const
8785 return this->result_count() == 1;
8788 // Get the function and the first argument to use when calling a bound
8792 Call_expression::bound_method_function(Translate_context* context,
8793 Bound_method_expression* bound_method,
8794 tree* first_arg_ptr)
8796 Expression* first_argument = bound_method->first_argument();
8797 tree first_arg = first_argument->get_tree(context);
8798 if (first_arg == error_mark_node)
8799 return error_mark_node;
8801 // We always pass a pointer to the first argument when calling a
8803 if (first_argument->type()->points_to() == NULL)
8805 tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg));
8806 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg))
8807 || DECL_P(first_arg)
8808 || TREE_CODE(first_arg) == INDIRECT_REF
8809 || TREE_CODE(first_arg) == COMPONENT_REF)
8811 first_arg = build_fold_addr_expr(first_arg);
8812 if (DECL_P(first_arg))
8813 TREE_ADDRESSABLE(first_arg) = 1;
8817 tree tmp = create_tmp_var(TREE_TYPE(first_arg),
8818 get_name(first_arg));
8819 DECL_IGNORED_P(tmp) = 0;
8820 DECL_INITIAL(tmp) = first_arg;
8821 first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type,
8822 build1(DECL_EXPR, void_type_node, tmp),
8823 build_fold_addr_expr(tmp));
8824 TREE_ADDRESSABLE(tmp) = 1;
8826 if (first_arg == error_mark_node)
8827 return error_mark_node;
8830 Type* fatype = bound_method->first_argument_type();
8833 if (fatype->points_to() == NULL)
8834 fatype = Type::make_pointer_type(fatype);
8835 Btype* bfatype = fatype->get_backend(context->gogo());
8836 first_arg = fold_convert(type_to_tree(bfatype), first_arg);
8837 if (first_arg == error_mark_node
8838 || TREE_TYPE(first_arg) == error_mark_node)
8839 return error_mark_node;
8842 *first_arg_ptr = first_arg;
8844 return bound_method->method()->get_tree(context);
8847 // Get the function and the first argument to use when calling an
8848 // interface method.
8851 Call_expression::interface_method_function(
8852 Translate_context* context,
8853 Interface_field_reference_expression* interface_method,
8854 tree* first_arg_ptr)
8856 tree expr = interface_method->expr()->get_tree(context);
8857 if (expr == error_mark_node)
8858 return error_mark_node;
8859 expr = save_expr(expr);
8860 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
8861 if (first_arg == error_mark_node)
8862 return error_mark_node;
8863 *first_arg_ptr = first_arg;
8864 return interface_method->get_function_tree(context, expr);
8867 // Build the call expression.
8870 Call_expression::do_get_tree(Translate_context* context)
8872 if (this->tree_ != NULL_TREE)
8875 Function_type* fntype = this->get_function_type();
8877 return error_mark_node;
8879 if (this->fn_->is_error_expression())
8880 return error_mark_node;
8882 Gogo* gogo = context->gogo();
8883 source_location location = this->location();
8885 Func_expression* func = this->fn_->func_expression();
8886 Bound_method_expression* bound_method = this->fn_->bound_method_expression();
8887 Interface_field_reference_expression* interface_method =
8888 this->fn_->interface_field_reference_expression();
8889 const bool has_closure = func != NULL && func->closure() != NULL;
8890 const bool is_method = bound_method != NULL || interface_method != NULL;
8891 go_assert(!fntype->is_method() || is_method);
8895 if (this->args_ == NULL || this->args_->empty())
8897 nargs = is_method ? 1 : 0;
8898 args = nargs == 0 ? NULL : new tree[nargs];
8902 const Typed_identifier_list* params = fntype->parameters();
8903 go_assert(params != NULL);
8905 nargs = this->args_->size();
8906 int i = is_method ? 1 : 0;
8908 args = new tree[nargs];
8910 Typed_identifier_list::const_iterator pp = params->begin();
8911 Expression_list::const_iterator pe;
8912 for (pe = this->args_->begin();
8913 pe != this->args_->end();
8916 go_assert(pp != params->end());
8917 tree arg_val = (*pe)->get_tree(context);
8918 args[i] = Expression::convert_for_assignment(context,
8923 if (args[i] == error_mark_node)
8926 return error_mark_node;
8929 go_assert(pp == params->end());
8930 go_assert(i == nargs);
8933 tree rettype = TREE_TYPE(TREE_TYPE(type_to_tree(fntype->get_backend(gogo))));
8934 if (rettype == error_mark_node)
8937 return error_mark_node;
8942 fn = func->get_tree_without_closure(gogo);
8943 else if (!is_method)
8944 fn = this->fn_->get_tree(context);
8945 else if (bound_method != NULL)
8946 fn = this->bound_method_function(context, bound_method, &args[0]);
8947 else if (interface_method != NULL)
8948 fn = this->interface_method_function(context, interface_method, &args[0]);
8952 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
8955 return error_mark_node;
8959 if (TREE_CODE(fndecl) == ADDR_EXPR)
8960 fndecl = TREE_OPERAND(fndecl, 0);
8962 // Add a type cast in case the type of the function is a recursive
8963 // type which refers to itself.
8964 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
8966 tree fnt = type_to_tree(fntype->get_backend(gogo));
8967 if (fnt == error_mark_node)
8968 return error_mark_node;
8969 fn = fold_convert_loc(location, fnt, fn);
8972 // This is to support builtin math functions when using 80387 math.
8973 tree excess_type = NULL_TREE;
8974 if (TREE_CODE(fndecl) == FUNCTION_DECL
8975 && DECL_IS_BUILTIN(fndecl)
8976 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
8978 && ((SCALAR_FLOAT_TYPE_P(rettype)
8979 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
8980 || (COMPLEX_FLOAT_TYPE_P(rettype)
8981 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
8983 excess_type = excess_precision_type(TREE_TYPE(args[0]));
8984 if (excess_type != NULL_TREE)
8986 tree excess_fndecl = mathfn_built_in(excess_type,
8987 DECL_FUNCTION_CODE(fndecl));
8988 if (excess_fndecl == NULL_TREE)
8989 excess_type = NULL_TREE;
8992 fn = build_fold_addr_expr_loc(location, excess_fndecl);
8993 for (int i = 0; i < nargs; ++i)
8994 args[i] = ::convert(excess_type, args[i]);
8999 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
9003 SET_EXPR_LOCATION(ret, location);
9007 tree closure_tree = func->closure()->get_tree(context);
9008 if (closure_tree != error_mark_node)
9009 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
9012 // If this is a recursive function type which returns itself, as in
9014 // we have used ptr_type_node for the return type. Add a cast here
9015 // to the correct type.
9016 if (TREE_TYPE(ret) == ptr_type_node)
9018 tree t = type_to_tree(this->type()->base()->get_backend(gogo));
9019 ret = fold_convert_loc(location, t, ret);
9022 if (excess_type != NULL_TREE)
9024 // Calling convert here can undo our excess precision change.
9025 // That may or may not be a bug in convert_to_real.
9026 ret = build1(NOP_EXPR, rettype, ret);
9029 // If there is more than one result, we will refer to the call
9031 if (fntype->results() != NULL && fntype->results()->size() > 1)
9032 ret = save_expr(ret);
9039 // Make a call expression.
9042 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
9043 source_location location)
9045 return new Call_expression(fn, args, is_varargs, location);
9048 // A single result from a call which returns multiple results.
9050 class Call_result_expression : public Expression
9053 Call_result_expression(Call_expression* call, unsigned int index)
9054 : Expression(EXPRESSION_CALL_RESULT, call->location()),
9055 call_(call), index_(index)
9060 do_traverse(Traverse*);
9066 do_determine_type(const Type_context*);
9069 do_check_types(Gogo*);
9074 return new Call_result_expression(this->call_->call_expression(),
9079 do_must_eval_in_order() const
9083 do_get_tree(Translate_context*);
9086 // The underlying call expression.
9088 // Which result we want.
9089 unsigned int index_;
9092 // Traverse a call result.
9095 Call_result_expression::do_traverse(Traverse* traverse)
9097 if (traverse->remember_expression(this->call_))
9099 // We have already traversed the call expression.
9100 return TRAVERSE_CONTINUE;
9102 return Expression::traverse(&this->call_, traverse);
9108 Call_result_expression::do_type()
9110 if (this->classification() == EXPRESSION_ERROR)
9111 return Type::make_error_type();
9113 // THIS->CALL_ can be replaced with a temporary reference due to
9114 // Call_expression::do_must_eval_in_order when there is an error.
9115 Call_expression* ce = this->call_->call_expression();
9118 this->set_is_error();
9119 return Type::make_error_type();
9121 Function_type* fntype = ce->get_function_type();
9124 this->set_is_error();
9125 return Type::make_error_type();
9127 const Typed_identifier_list* results = fntype->results();
9128 if (results == NULL)
9130 this->report_error(_("number of results does not match "
9131 "number of values"));
9132 return Type::make_error_type();
9134 Typed_identifier_list::const_iterator pr = results->begin();
9135 for (unsigned int i = 0; i < this->index_; ++i)
9137 if (pr == results->end())
9141 if (pr == results->end())
9143 this->report_error(_("number of results does not match "
9144 "number of values"));
9145 return Type::make_error_type();
9150 // Check the type. Just make sure that we trigger the warning in
9154 Call_result_expression::do_check_types(Gogo*)
9159 // Determine the type. We have nothing to do here, but the 0 result
9160 // needs to pass down to the caller.
9163 Call_result_expression::do_determine_type(const Type_context*)
9165 this->call_->determine_type_no_context();
9171 Call_result_expression::do_get_tree(Translate_context* context)
9173 tree call_tree = this->call_->get_tree(context);
9174 if (call_tree == error_mark_node)
9175 return error_mark_node;
9176 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9178 go_assert(saw_errors());
9179 return error_mark_node;
9181 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9182 for (unsigned int i = 0; i < this->index_; ++i)
9184 go_assert(field != NULL_TREE);
9185 field = DECL_CHAIN(field);
9187 go_assert(field != NULL_TREE);
9188 return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE);
9191 // Make a reference to a single result of a call which returns
9192 // multiple results.
9195 Expression::make_call_result(Call_expression* call, unsigned int index)
9197 return new Call_result_expression(call, index);
9200 // Class Index_expression.
9205 Index_expression::do_traverse(Traverse* traverse)
9207 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9208 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9209 || (this->end_ != NULL
9210 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9211 return TRAVERSE_EXIT;
9212 return TRAVERSE_CONTINUE;
9215 // Lower an index expression. This converts the generic index
9216 // expression into an array index, a string index, or a map index.
9219 Index_expression::do_lower(Gogo*, Named_object*, int)
9221 source_location location = this->location();
9222 Expression* left = this->left_;
9223 Expression* start = this->start_;
9224 Expression* end = this->end_;
9226 Type* type = left->type();
9227 if (type->is_error())
9228 return Expression::make_error(location);
9229 else if (left->is_type_expression())
9231 error_at(location, "attempt to index type expression");
9232 return Expression::make_error(location);
9234 else if (type->array_type() != NULL)
9235 return Expression::make_array_index(left, start, end, location);
9236 else if (type->points_to() != NULL
9237 && type->points_to()->array_type() != NULL
9238 && !type->points_to()->is_open_array_type())
9240 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9242 return Expression::make_array_index(deref, start, end, location);
9244 else if (type->is_string_type())
9245 return Expression::make_string_index(left, start, end, location);
9246 else if (type->map_type() != NULL)
9250 error_at(location, "invalid slice of map");
9251 return Expression::make_error(location);
9253 Map_index_expression* ret= Expression::make_map_index(left, start,
9255 if (this->is_lvalue_)
9256 ret->set_is_lvalue();
9262 "attempt to index object which is not array, string, or map");
9263 return Expression::make_error(location);
9267 // Make an index expression.
9270 Expression::make_index(Expression* left, Expression* start, Expression* end,
9271 source_location location)
9273 return new Index_expression(left, start, end, location);
9276 // An array index. This is used for both indexing and slicing.
9278 class Array_index_expression : public Expression
9281 Array_index_expression(Expression* array, Expression* start,
9282 Expression* end, source_location location)
9283 : Expression(EXPRESSION_ARRAY_INDEX, location),
9284 array_(array), start_(start), end_(end), type_(NULL)
9289 do_traverse(Traverse*);
9295 do_determine_type(const Type_context*);
9298 do_check_types(Gogo*);
9303 return Expression::make_array_index(this->array_->copy(),
9304 this->start_->copy(),
9307 : this->end_->copy()),
9312 do_is_addressable() const;
9315 do_address_taken(bool escapes)
9316 { this->array_->address_taken(escapes); }
9319 do_get_tree(Translate_context*);
9322 // The array we are getting a value from.
9324 // The start or only index.
9326 // The end index of a slice. This may be NULL for a simple array
9327 // index, or it may be a nil expression for the length of the array.
9329 // The type of the expression.
9333 // Array index traversal.
9336 Array_index_expression::do_traverse(Traverse* traverse)
9338 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9339 return TRAVERSE_EXIT;
9340 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9341 return TRAVERSE_EXIT;
9342 if (this->end_ != NULL)
9344 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9345 return TRAVERSE_EXIT;
9347 return TRAVERSE_CONTINUE;
9350 // Return the type of an array index.
9353 Array_index_expression::do_type()
9355 if (this->type_ == NULL)
9357 Array_type* type = this->array_->type()->array_type();
9359 this->type_ = Type::make_error_type();
9360 else if (this->end_ == NULL)
9361 this->type_ = type->element_type();
9362 else if (type->is_open_array_type())
9364 // A slice of a slice has the same type as the original
9366 this->type_ = this->array_->type()->deref();
9370 // A slice of an array is a slice.
9371 this->type_ = Type::make_array_type(type->element_type(), NULL);
9377 // Set the type of an array index.
9380 Array_index_expression::do_determine_type(const Type_context*)
9382 this->array_->determine_type_no_context();
9383 this->start_->determine_type_no_context();
9384 if (this->end_ != NULL)
9385 this->end_->determine_type_no_context();
9388 // Check types of an array index.
9391 Array_index_expression::do_check_types(Gogo*)
9393 if (this->start_->type()->integer_type() == NULL)
9394 this->report_error(_("index must be integer"));
9395 if (this->end_ != NULL
9396 && this->end_->type()->integer_type() == NULL
9397 && !this->end_->is_nil_expression())
9398 this->report_error(_("slice end must be integer"));
9400 Array_type* array_type = this->array_->type()->array_type();
9401 if (array_type == NULL)
9403 go_assert(this->array_->type()->is_error());
9407 unsigned int int_bits =
9408 Type::lookup_integer_type("int")->integer_type()->bits();
9413 bool lval_valid = (array_type->length() != NULL
9414 && array_type->length()->integer_constant_value(true,
9419 if (this->start_->integer_constant_value(true, ival, &dummy))
9421 if (mpz_sgn(ival) < 0
9422 || mpz_sizeinbase(ival, 2) >= int_bits
9424 && (this->end_ == NULL
9425 ? mpz_cmp(ival, lval) >= 0
9426 : mpz_cmp(ival, lval) > 0)))
9428 error_at(this->start_->location(), "array index out of bounds");
9429 this->set_is_error();
9432 if (this->end_ != NULL && !this->end_->is_nil_expression())
9434 if (this->end_->integer_constant_value(true, ival, &dummy))
9436 if (mpz_sgn(ival) < 0
9437 || mpz_sizeinbase(ival, 2) >= int_bits
9438 || (lval_valid && mpz_cmp(ival, lval) > 0))
9440 error_at(this->end_->location(), "array index out of bounds");
9441 this->set_is_error();
9448 // A slice of an array requires an addressable array. A slice of a
9449 // slice is always possible.
9450 if (this->end_ != NULL && !array_type->is_open_array_type())
9452 if (!this->array_->is_addressable())
9453 this->report_error(_("array is not addressable"));
9455 this->array_->address_taken(true);
9459 // Return whether this expression is addressable.
9462 Array_index_expression::do_is_addressable() const
9464 // A slice expression is not addressable.
9465 if (this->end_ != NULL)
9468 // An index into a slice is addressable.
9469 if (this->array_->type()->is_open_array_type())
9472 // An index into an array is addressable if the array is
9474 return this->array_->is_addressable();
9477 // Get a tree for an array index.
9480 Array_index_expression::do_get_tree(Translate_context* context)
9482 Gogo* gogo = context->gogo();
9483 source_location loc = this->location();
9485 Array_type* array_type = this->array_->type()->array_type();
9486 if (array_type == NULL)
9488 go_assert(this->array_->type()->is_error());
9489 return error_mark_node;
9492 tree type_tree = type_to_tree(array_type->get_backend(gogo));
9493 if (type_tree == error_mark_node)
9494 return error_mark_node;
9496 tree array_tree = this->array_->get_tree(context);
9497 if (array_tree == error_mark_node)
9498 return error_mark_node;
9500 if (array_type->length() == NULL && !DECL_P(array_tree))
9501 array_tree = save_expr(array_tree);
9502 tree length_tree = array_type->length_tree(gogo, array_tree);
9503 if (length_tree == error_mark_node)
9504 return error_mark_node;
9505 length_tree = save_expr(length_tree);
9506 tree length_type = TREE_TYPE(length_tree);
9508 tree bad_index = boolean_false_node;
9510 tree start_tree = this->start_->get_tree(context);
9511 if (start_tree == error_mark_node)
9512 return error_mark_node;
9513 if (!DECL_P(start_tree))
9514 start_tree = save_expr(start_tree);
9515 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9516 start_tree = convert_to_integer(length_type, start_tree);
9518 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9521 start_tree = fold_convert_loc(loc, length_type, start_tree);
9522 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
9523 fold_build2_loc(loc,
9527 boolean_type_node, start_tree,
9530 int code = (array_type->length() != NULL
9531 ? (this->end_ == NULL
9532 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9533 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9534 : (this->end_ == NULL
9535 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9536 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9537 tree crash = Gogo::runtime_error(code, loc);
9539 if (this->end_ == NULL)
9541 // Simple array indexing. This has to return an l-value, so
9542 // wrap the index check into START_TREE.
9543 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9544 build3(COND_EXPR, void_type_node,
9545 bad_index, crash, NULL_TREE),
9547 start_tree = fold_convert_loc(loc, sizetype, start_tree);
9549 if (array_type->length() != NULL)
9552 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9553 start_tree, NULL_TREE, NULL_TREE);
9558 tree values = array_type->value_pointer_tree(gogo, array_tree);
9559 Type* element_type = array_type->element_type();
9560 Btype* belement_type = element_type->get_backend(gogo);
9561 tree element_type_tree = type_to_tree(belement_type);
9562 if (element_type_tree == error_mark_node)
9563 return error_mark_node;
9564 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9565 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9566 start_tree, element_size);
9567 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9568 TREE_TYPE(values), values, offset);
9569 return build_fold_indirect_ref(ptr);
9575 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
9576 if (capacity_tree == error_mark_node)
9577 return error_mark_node;
9578 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
9581 if (this->end_->is_nil_expression())
9582 end_tree = length_tree;
9585 end_tree = this->end_->get_tree(context);
9586 if (end_tree == error_mark_node)
9587 return error_mark_node;
9588 if (!DECL_P(end_tree))
9589 end_tree = save_expr(end_tree);
9590 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9591 end_tree = convert_to_integer(length_type, end_tree);
9593 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9596 end_tree = fold_convert_loc(loc, length_type, end_tree);
9598 capacity_tree = save_expr(capacity_tree);
9599 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9600 fold_build2_loc(loc, LT_EXPR,
9602 end_tree, start_tree),
9603 fold_build2_loc(loc, GT_EXPR,
9605 end_tree, capacity_tree));
9606 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9607 bad_index, bad_end);
9610 Type* element_type = array_type->element_type();
9611 tree element_type_tree = type_to_tree(element_type->get_backend(gogo));
9612 if (element_type_tree == error_mark_node)
9613 return error_mark_node;
9614 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9616 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9617 fold_convert_loc(loc, sizetype, start_tree),
9620 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9621 if (value_pointer == error_mark_node)
9622 return error_mark_node;
9624 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9625 TREE_TYPE(value_pointer),
9626 value_pointer, offset);
9628 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9629 end_tree, start_tree);
9631 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9632 capacity_tree, start_tree);
9634 tree struct_tree = type_to_tree(this->type()->get_backend(gogo));
9635 go_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9637 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9639 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9640 tree field = TYPE_FIELDS(struct_tree);
9641 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9643 elt->value = value_pointer;
9645 elt = VEC_quick_push(constructor_elt, init, NULL);
9646 field = DECL_CHAIN(field);
9647 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9649 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
9651 elt = VEC_quick_push(constructor_elt, init, NULL);
9652 field = DECL_CHAIN(field);
9653 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9655 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
9657 tree constructor = build_constructor(struct_tree, init);
9659 if (TREE_CONSTANT(value_pointer)
9660 && TREE_CONSTANT(result_length_tree)
9661 && TREE_CONSTANT(result_capacity_tree))
9662 TREE_CONSTANT(constructor) = 1;
9664 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
9665 build3(COND_EXPR, void_type_node,
9666 bad_index, crash, NULL_TREE),
9670 // Make an array index expression. END may be NULL.
9673 Expression::make_array_index(Expression* array, Expression* start,
9674 Expression* end, source_location location)
9676 // Taking a slice of a composite literal requires moving the literal
9678 if (end != NULL && array->is_composite_literal())
9680 array = Expression::make_heap_composite(array, location);
9681 array = Expression::make_unary(OPERATOR_MULT, array, location);
9683 return new Array_index_expression(array, start, end, location);
9686 // A string index. This is used for both indexing and slicing.
9688 class String_index_expression : public Expression
9691 String_index_expression(Expression* string, Expression* start,
9692 Expression* end, source_location location)
9693 : Expression(EXPRESSION_STRING_INDEX, location),
9694 string_(string), start_(start), end_(end)
9699 do_traverse(Traverse*);
9705 do_determine_type(const Type_context*);
9708 do_check_types(Gogo*);
9713 return Expression::make_string_index(this->string_->copy(),
9714 this->start_->copy(),
9717 : this->end_->copy()),
9722 do_get_tree(Translate_context*);
9725 // The string we are getting a value from.
9726 Expression* string_;
9727 // The start or only index.
9729 // The end index of a slice. This may be NULL for a single index,
9730 // or it may be a nil expression for the length of the string.
9734 // String index traversal.
9737 String_index_expression::do_traverse(Traverse* traverse)
9739 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
9740 return TRAVERSE_EXIT;
9741 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9742 return TRAVERSE_EXIT;
9743 if (this->end_ != NULL)
9745 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9746 return TRAVERSE_EXIT;
9748 return TRAVERSE_CONTINUE;
9751 // Return the type of a string index.
9754 String_index_expression::do_type()
9756 if (this->end_ == NULL)
9757 return Type::lookup_integer_type("uint8");
9759 return this->string_->type();
9762 // Determine the type of a string index.
9765 String_index_expression::do_determine_type(const Type_context*)
9767 this->string_->determine_type_no_context();
9768 this->start_->determine_type_no_context();
9769 if (this->end_ != NULL)
9770 this->end_->determine_type_no_context();
9773 // Check types of a string index.
9776 String_index_expression::do_check_types(Gogo*)
9778 if (this->start_->type()->integer_type() == NULL)
9779 this->report_error(_("index must be integer"));
9780 if (this->end_ != NULL
9781 && this->end_->type()->integer_type() == NULL
9782 && !this->end_->is_nil_expression())
9783 this->report_error(_("slice end must be integer"));
9786 bool sval_valid = this->string_->string_constant_value(&sval);
9791 if (this->start_->integer_constant_value(true, ival, &dummy))
9793 if (mpz_sgn(ival) < 0
9794 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
9796 error_at(this->start_->location(), "string index out of bounds");
9797 this->set_is_error();
9800 if (this->end_ != NULL && !this->end_->is_nil_expression())
9802 if (this->end_->integer_constant_value(true, ival, &dummy))
9804 if (mpz_sgn(ival) < 0
9805 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
9807 error_at(this->end_->location(), "string index out of bounds");
9808 this->set_is_error();
9815 // Get a tree for a string index.
9818 String_index_expression::do_get_tree(Translate_context* context)
9820 source_location loc = this->location();
9822 tree string_tree = this->string_->get_tree(context);
9823 if (string_tree == error_mark_node)
9824 return error_mark_node;
9826 if (this->string_->type()->points_to() != NULL)
9827 string_tree = build_fold_indirect_ref(string_tree);
9828 if (!DECL_P(string_tree))
9829 string_tree = save_expr(string_tree);
9830 tree string_type = TREE_TYPE(string_tree);
9832 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
9833 length_tree = save_expr(length_tree);
9834 tree length_type = TREE_TYPE(length_tree);
9836 tree bad_index = boolean_false_node;
9838 tree start_tree = this->start_->get_tree(context);
9839 if (start_tree == error_mark_node)
9840 return error_mark_node;
9841 if (!DECL_P(start_tree))
9842 start_tree = save_expr(start_tree);
9843 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9844 start_tree = convert_to_integer(length_type, start_tree);
9846 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9849 start_tree = fold_convert_loc(loc, length_type, start_tree);
9851 int code = (this->end_ == NULL
9852 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9853 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
9854 tree crash = Gogo::runtime_error(code, loc);
9856 if (this->end_ == NULL)
9858 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9860 fold_build2_loc(loc, GE_EXPR,
9862 start_tree, length_tree));
9864 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
9865 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
9867 fold_convert_loc(loc, sizetype, start_tree));
9868 tree index = build_fold_indirect_ref_loc(loc, ptr);
9870 return build2(COMPOUND_EXPR, TREE_TYPE(index),
9871 build3(COND_EXPR, void_type_node,
9872 bad_index, crash, NULL_TREE),
9878 if (this->end_->is_nil_expression())
9879 end_tree = build_int_cst(length_type, -1);
9882 end_tree = this->end_->get_tree(context);
9883 if (end_tree == error_mark_node)
9884 return error_mark_node;
9885 if (!DECL_P(end_tree))
9886 end_tree = save_expr(end_tree);
9887 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9888 end_tree = convert_to_integer(length_type, end_tree);
9890 bad_index = Expression::check_bounds(end_tree, length_type,
9893 end_tree = fold_convert_loc(loc, length_type, end_tree);
9896 static tree strslice_fndecl;
9897 tree ret = Gogo::call_builtin(&strslice_fndecl,
9899 "__go_string_slice",
9908 if (ret == error_mark_node)
9909 return error_mark_node;
9910 // This will panic if the bounds are out of range for the
9912 TREE_NOTHROW(strslice_fndecl) = 0;
9914 if (bad_index == boolean_false_node)
9917 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
9918 build3(COND_EXPR, void_type_node,
9919 bad_index, crash, NULL_TREE),
9924 // Make a string index expression. END may be NULL.
9927 Expression::make_string_index(Expression* string, Expression* start,
9928 Expression* end, source_location location)
9930 return new String_index_expression(string, start, end, location);
9935 // Get the type of the map.
9938 Map_index_expression::get_map_type() const
9940 Map_type* mt = this->map_->type()->deref()->map_type();
9942 go_assert(saw_errors());
9946 // Map index traversal.
9949 Map_index_expression::do_traverse(Traverse* traverse)
9951 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
9952 return TRAVERSE_EXIT;
9953 return Expression::traverse(&this->index_, traverse);
9956 // Return the type of a map index.
9959 Map_index_expression::do_type()
9961 Map_type* mt = this->get_map_type();
9963 return Type::make_error_type();
9964 Type* type = mt->val_type();
9965 // If this map index is in a tuple assignment, we actually return a
9966 // pointer to the value type. Tuple_map_assignment_statement is
9967 // responsible for handling this correctly. We need to get the type
9968 // right in case this gets assigned to a temporary variable.
9969 if (this->is_in_tuple_assignment_)
9970 type = Type::make_pointer_type(type);
9974 // Fix the type of a map index.
9977 Map_index_expression::do_determine_type(const Type_context*)
9979 this->map_->determine_type_no_context();
9980 Map_type* mt = this->get_map_type();
9981 Type* key_type = mt == NULL ? NULL : mt->key_type();
9982 Type_context subcontext(key_type, false);
9983 this->index_->determine_type(&subcontext);
9986 // Check types of a map index.
9989 Map_index_expression::do_check_types(Gogo*)
9992 Map_type* mt = this->get_map_type();
9995 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
9998 this->report_error(_("incompatible type for map index"));
10001 error_at(this->location(), "incompatible type for map index (%s)",
10003 this->set_is_error();
10008 // Get a tree for a map index.
10011 Map_index_expression::do_get_tree(Translate_context* context)
10013 Map_type* type = this->get_map_type();
10015 return error_mark_node;
10017 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
10018 if (valptr == error_mark_node)
10019 return error_mark_node;
10020 valptr = save_expr(valptr);
10022 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
10024 if (this->is_lvalue_)
10025 return build_fold_indirect_ref(valptr);
10026 else if (this->is_in_tuple_assignment_)
10028 // Tuple_map_assignment_statement is responsible for using this
10034 return fold_build3(COND_EXPR, val_type_tree,
10035 fold_build2(EQ_EXPR, boolean_type_node, valptr,
10036 fold_convert(TREE_TYPE(valptr),
10037 null_pointer_node)),
10038 type->val_type()->get_init_tree(context->gogo(),
10040 build_fold_indirect_ref(valptr));
10044 // Get a tree for the map index. This returns a tree which evaluates
10045 // to a pointer to a value. The pointer will be NULL if the key is
10049 Map_index_expression::get_value_pointer(Translate_context* context,
10052 Map_type* type = this->get_map_type();
10054 return error_mark_node;
10056 tree map_tree = this->map_->get_tree(context);
10057 tree index_tree = this->index_->get_tree(context);
10058 index_tree = Expression::convert_for_assignment(context, type->key_type(),
10059 this->index_->type(),
10062 if (map_tree == error_mark_node || index_tree == error_mark_node)
10063 return error_mark_node;
10065 if (this->map_->type()->points_to() != NULL)
10066 map_tree = build_fold_indirect_ref(map_tree);
10068 // We need to pass in a pointer to the key, so stuff it into a
10072 if (current_function_decl != NULL)
10074 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
10075 DECL_IGNORED_P(tmp) = 0;
10076 DECL_INITIAL(tmp) = index_tree;
10077 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
10078 TREE_ADDRESSABLE(tmp) = 1;
10082 tmp = build_decl(this->location(), VAR_DECL, create_tmp_var_name("M"),
10083 TREE_TYPE(index_tree));
10084 DECL_EXTERNAL(tmp) = 0;
10085 TREE_PUBLIC(tmp) = 0;
10086 TREE_STATIC(tmp) = 1;
10087 DECL_ARTIFICIAL(tmp) = 1;
10088 if (!TREE_CONSTANT(index_tree))
10089 make_tmp = fold_build2_loc(this->location(), INIT_EXPR, void_type_node,
10093 TREE_READONLY(tmp) = 1;
10094 TREE_CONSTANT(tmp) = 1;
10095 DECL_INITIAL(tmp) = index_tree;
10096 make_tmp = NULL_TREE;
10098 rest_of_decl_compilation(tmp, 1, 0);
10100 tree tmpref = fold_convert_loc(this->location(), const_ptr_type_node,
10101 build_fold_addr_expr_loc(this->location(),
10104 static tree map_index_fndecl;
10105 tree call = Gogo::call_builtin(&map_index_fndecl,
10109 const_ptr_type_node,
10110 TREE_TYPE(map_tree),
10112 const_ptr_type_node,
10116 ? boolean_true_node
10117 : boolean_false_node));
10118 if (call == error_mark_node)
10119 return error_mark_node;
10120 // This can panic on a map of interface type if the interface holds
10121 // an uncomparable or unhashable type.
10122 TREE_NOTHROW(map_index_fndecl) = 0;
10124 Type* val_type = type->val_type();
10125 tree val_type_tree = type_to_tree(val_type->get_backend(context->gogo()));
10126 if (val_type_tree == error_mark_node)
10127 return error_mark_node;
10128 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
10130 tree ret = fold_convert_loc(this->location(), ptr_val_type_tree, call);
10131 if (make_tmp != NULL_TREE)
10132 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
10136 // Make a map index expression.
10138 Map_index_expression*
10139 Expression::make_map_index(Expression* map, Expression* index,
10140 source_location location)
10142 return new Map_index_expression(map, index, location);
10145 // Class Field_reference_expression.
10147 // Return the type of a field reference.
10150 Field_reference_expression::do_type()
10152 Type* type = this->expr_->type();
10153 if (type->is_error())
10155 Struct_type* struct_type = type->struct_type();
10156 go_assert(struct_type != NULL);
10157 return struct_type->field(this->field_index_)->type();
10160 // Check the types for a field reference.
10163 Field_reference_expression::do_check_types(Gogo*)
10165 Type* type = this->expr_->type();
10166 if (type->is_error())
10168 Struct_type* struct_type = type->struct_type();
10169 go_assert(struct_type != NULL);
10170 go_assert(struct_type->field(this->field_index_) != NULL);
10173 // Get a tree for a field reference.
10176 Field_reference_expression::do_get_tree(Translate_context* context)
10178 tree struct_tree = this->expr_->get_tree(context);
10179 if (struct_tree == error_mark_node
10180 || TREE_TYPE(struct_tree) == error_mark_node)
10181 return error_mark_node;
10182 go_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
10183 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
10184 if (field == NULL_TREE)
10186 // This can happen for a type which refers to itself indirectly
10187 // and then turns out to be erroneous.
10188 go_assert(saw_errors());
10189 return error_mark_node;
10191 for (unsigned int i = this->field_index_; i > 0; --i)
10193 field = DECL_CHAIN(field);
10194 go_assert(field != NULL_TREE);
10196 if (TREE_TYPE(field) == error_mark_node)
10197 return error_mark_node;
10198 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
10202 // Make a reference to a qualified identifier in an expression.
10204 Field_reference_expression*
10205 Expression::make_field_reference(Expression* expr, unsigned int field_index,
10206 source_location location)
10208 return new Field_reference_expression(expr, field_index, location);
10211 // Class Interface_field_reference_expression.
10213 // Return a tree for the pointer to the function to call.
10216 Interface_field_reference_expression::get_function_tree(Translate_context*,
10219 if (this->expr_->type()->points_to() != NULL)
10220 expr = build_fold_indirect_ref(expr);
10222 tree expr_type = TREE_TYPE(expr);
10223 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10225 tree field = TYPE_FIELDS(expr_type);
10226 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
10228 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10229 go_assert(POINTER_TYPE_P(TREE_TYPE(table)));
10231 table = build_fold_indirect_ref(table);
10232 go_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
10234 std::string name = Gogo::unpack_hidden_name(this->name_);
10235 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
10236 field != NULL_TREE;
10237 field = DECL_CHAIN(field))
10239 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
10242 go_assert(field != NULL_TREE);
10244 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
10247 // Return a tree for the first argument to pass to the interface
10251 Interface_field_reference_expression::get_underlying_object_tree(
10252 Translate_context*,
10255 if (this->expr_->type()->points_to() != NULL)
10256 expr = build_fold_indirect_ref(expr);
10258 tree expr_type = TREE_TYPE(expr);
10259 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10261 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
10262 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
10264 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10270 Interface_field_reference_expression::do_traverse(Traverse* traverse)
10272 return Expression::traverse(&this->expr_, traverse);
10275 // Return the type of an interface field reference.
10278 Interface_field_reference_expression::do_type()
10280 Type* expr_type = this->expr_->type();
10282 Type* points_to = expr_type->points_to();
10283 if (points_to != NULL)
10284 expr_type = points_to;
10286 Interface_type* interface_type = expr_type->interface_type();
10287 if (interface_type == NULL)
10288 return Type::make_error_type();
10290 const Typed_identifier* method = interface_type->find_method(this->name_);
10291 if (method == NULL)
10292 return Type::make_error_type();
10294 return method->type();
10297 // Determine types.
10300 Interface_field_reference_expression::do_determine_type(const Type_context*)
10302 this->expr_->determine_type_no_context();
10305 // Check the types for an interface field reference.
10308 Interface_field_reference_expression::do_check_types(Gogo*)
10310 Type* type = this->expr_->type();
10312 Type* points_to = type->points_to();
10313 if (points_to != NULL)
10316 Interface_type* interface_type = type->interface_type();
10317 if (interface_type == NULL)
10319 if (!type->is_error_type())
10320 this->report_error(_("expected interface or pointer to interface"));
10324 const Typed_identifier* method =
10325 interface_type->find_method(this->name_);
10326 if (method == NULL)
10328 error_at(this->location(), "method %qs not in interface",
10329 Gogo::message_name(this->name_).c_str());
10330 this->set_is_error();
10335 // Get a tree for a reference to a field in an interface. There is no
10336 // standard tree type representation for this: it's a function
10337 // attached to its first argument, like a Bound_method_expression.
10338 // The only places it may currently be used are in a Call_expression
10339 // or a Go_statement, which will take it apart directly. So this has
10340 // nothing to do at present.
10343 Interface_field_reference_expression::do_get_tree(Translate_context*)
10348 // Make a reference to a field in an interface.
10351 Expression::make_interface_field_reference(Expression* expr,
10352 const std::string& field,
10353 source_location location)
10355 return new Interface_field_reference_expression(expr, field, location);
10358 // A general selector. This is a Parser_expression for LEFT.NAME. It
10359 // is lowered after we know the type of the left hand side.
10361 class Selector_expression : public Parser_expression
10364 Selector_expression(Expression* left, const std::string& name,
10365 source_location location)
10366 : Parser_expression(EXPRESSION_SELECTOR, location),
10367 left_(left), name_(name)
10372 do_traverse(Traverse* traverse)
10373 { return Expression::traverse(&this->left_, traverse); }
10376 do_lower(Gogo*, Named_object*, int);
10381 return new Selector_expression(this->left_->copy(), this->name_,
10387 lower_method_expression(Gogo*);
10389 // The expression on the left hand side.
10391 // The name on the right hand side.
10395 // Lower a selector expression once we know the real type of the left
10399 Selector_expression::do_lower(Gogo* gogo, Named_object*, int)
10401 Expression* left = this->left_;
10402 if (left->is_type_expression())
10403 return this->lower_method_expression(gogo);
10404 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
10408 // Lower a method expression T.M or (*T).M. We turn this into a
10409 // function literal.
10412 Selector_expression::lower_method_expression(Gogo* gogo)
10414 source_location location = this->location();
10415 Type* type = this->left_->type();
10416 const std::string& name(this->name_);
10419 if (type->points_to() == NULL)
10420 is_pointer = false;
10424 type = type->points_to();
10426 Named_type* nt = type->named_type();
10430 ("method expression requires named type or "
10431 "pointer to named type"));
10432 return Expression::make_error(location);
10436 Method* method = nt->method_function(name, &is_ambiguous);
10437 const Typed_identifier* imethod = NULL;
10438 if (method == NULL && !is_pointer)
10440 Interface_type* it = nt->interface_type();
10442 imethod = it->find_method(name);
10445 if (method == NULL && imethod == NULL)
10448 error_at(location, "type %<%s%s%> has no method %<%s%>",
10449 is_pointer ? "*" : "",
10450 nt->message_name().c_str(),
10451 Gogo::message_name(name).c_str());
10453 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
10454 Gogo::message_name(name).c_str(),
10455 is_pointer ? "*" : "",
10456 nt->message_name().c_str());
10457 return Expression::make_error(location);
10460 if (method != NULL && !is_pointer && !method->is_value_method())
10462 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
10463 nt->message_name().c_str(),
10464 Gogo::message_name(name).c_str());
10465 return Expression::make_error(location);
10468 // Build a new function type in which the receiver becomes the first
10470 Function_type* method_type;
10471 if (method != NULL)
10473 method_type = method->type();
10474 go_assert(method_type->is_method());
10478 method_type = imethod->type()->function_type();
10479 go_assert(method_type != NULL && !method_type->is_method());
10482 const char* const receiver_name = "$this";
10483 Typed_identifier_list* parameters = new Typed_identifier_list();
10484 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
10487 const Typed_identifier_list* method_parameters = method_type->parameters();
10488 if (method_parameters != NULL)
10490 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10491 p != method_parameters->end();
10493 parameters->push_back(*p);
10496 const Typed_identifier_list* method_results = method_type->results();
10497 Typed_identifier_list* results;
10498 if (method_results == NULL)
10502 results = new Typed_identifier_list();
10503 for (Typed_identifier_list::const_iterator p = method_results->begin();
10504 p != method_results->end();
10506 results->push_back(*p);
10509 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
10511 if (method_type->is_varargs())
10512 fntype->set_is_varargs();
10514 // We generate methods which always takes a pointer to the receiver
10515 // as their first argument. If this is for a pointer type, we can
10516 // simply reuse the existing function. We use an internal hack to
10517 // get the right type.
10519 if (method != NULL && is_pointer)
10521 Named_object* mno = (method->needs_stub_method()
10522 ? method->stub_object()
10523 : method->named_object());
10524 Expression* f = Expression::make_func_reference(mno, NULL, location);
10525 f = Expression::make_cast(fntype, f, location);
10526 Type_conversion_expression* tce =
10527 static_cast<Type_conversion_expression*>(f);
10528 tce->set_may_convert_function_types();
10532 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
10535 Named_object* vno = gogo->lookup(receiver_name, NULL);
10536 go_assert(vno != NULL);
10537 Expression* ve = Expression::make_var_reference(vno, location);
10539 if (method != NULL)
10540 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
10542 bm = Expression::make_interface_field_reference(ve, name, location);
10544 // Even though we found the method above, if it has an error type we
10545 // may see an error here.
10546 if (bm->is_error_expression())
10548 gogo->finish_function(location);
10552 Expression_list* args;
10553 if (method_parameters == NULL)
10557 args = new Expression_list();
10558 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10559 p != method_parameters->end();
10562 vno = gogo->lookup(p->name(), NULL);
10563 go_assert(vno != NULL);
10564 args->push_back(Expression::make_var_reference(vno, location));
10568 Call_expression* call = Expression::make_call(bm, args,
10569 method_type->is_varargs(),
10572 size_t count = call->result_count();
10575 s = Statement::make_statement(call);
10578 Expression_list* retvals = new Expression_list();
10580 retvals->push_back(call);
10583 for (size_t i = 0; i < count; ++i)
10584 retvals->push_back(Expression::make_call_result(call, i));
10586 s = Statement::make_return_statement(retvals, location);
10588 gogo->add_statement(s);
10590 gogo->finish_function(location);
10592 return Expression::make_func_reference(no, NULL, location);
10595 // Make a selector expression.
10598 Expression::make_selector(Expression* left, const std::string& name,
10599 source_location location)
10601 return new Selector_expression(left, name, location);
10604 // Implement the builtin function new.
10606 class Allocation_expression : public Expression
10609 Allocation_expression(Type* type, source_location location)
10610 : Expression(EXPRESSION_ALLOCATION, location),
10616 do_traverse(Traverse* traverse)
10617 { return Type::traverse(this->type_, traverse); }
10621 { return Type::make_pointer_type(this->type_); }
10624 do_determine_type(const Type_context*)
10628 do_check_types(Gogo*);
10632 { return new Allocation_expression(this->type_, this->location()); }
10635 do_get_tree(Translate_context*);
10638 // The type we are allocating.
10642 // Check the type of an allocation expression.
10645 Allocation_expression::do_check_types(Gogo*)
10647 if (this->type_->function_type() != NULL)
10648 this->report_error(_("invalid new of function type"));
10651 // Return a tree for an allocation expression.
10654 Allocation_expression::do_get_tree(Translate_context* context)
10656 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
10657 if (type_tree == error_mark_node)
10658 return error_mark_node;
10659 tree size_tree = TYPE_SIZE_UNIT(type_tree);
10660 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
10662 if (space == error_mark_node)
10663 return error_mark_node;
10664 return fold_convert(build_pointer_type(type_tree), space);
10667 // Make an allocation expression.
10670 Expression::make_allocation(Type* type, source_location location)
10672 return new Allocation_expression(type, location);
10675 // Implement the builtin function make.
10677 class Make_expression : public Expression
10680 Make_expression(Type* type, Expression_list* args, source_location location)
10681 : Expression(EXPRESSION_MAKE, location),
10682 type_(type), args_(args)
10687 do_traverse(Traverse* traverse);
10691 { return this->type_; }
10694 do_determine_type(const Type_context*);
10697 do_check_types(Gogo*);
10702 return new Make_expression(this->type_, this->args_->copy(),
10707 do_get_tree(Translate_context*);
10710 // The type we are making.
10712 // The arguments to pass to the make routine.
10713 Expression_list* args_;
10719 Make_expression::do_traverse(Traverse* traverse)
10721 if (this->args_ != NULL
10722 && this->args_->traverse(traverse) == TRAVERSE_EXIT)
10723 return TRAVERSE_EXIT;
10724 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10725 return TRAVERSE_EXIT;
10726 return TRAVERSE_CONTINUE;
10729 // Set types of arguments.
10732 Make_expression::do_determine_type(const Type_context*)
10734 if (this->args_ != NULL)
10736 Type_context context(Type::lookup_integer_type("int"), false);
10737 for (Expression_list::const_iterator pe = this->args_->begin();
10738 pe != this->args_->end();
10740 (*pe)->determine_type(&context);
10744 // Check types for a make expression.
10747 Make_expression::do_check_types(Gogo*)
10749 if (this->type_->channel_type() == NULL
10750 && this->type_->map_type() == NULL
10751 && (this->type_->array_type() == NULL
10752 || this->type_->array_type()->length() != NULL))
10753 this->report_error(_("invalid type for make function"));
10754 else if (!this->type_->check_make_expression(this->args_, this->location()))
10755 this->set_is_error();
10758 // Return a tree for a make expression.
10761 Make_expression::do_get_tree(Translate_context* context)
10763 return this->type_->make_expression_tree(context, this->args_,
10767 // Make a make expression.
10770 Expression::make_make(Type* type, Expression_list* args,
10771 source_location location)
10773 return new Make_expression(type, args, location);
10776 // Construct a struct.
10778 class Struct_construction_expression : public Expression
10781 Struct_construction_expression(Type* type, Expression_list* vals,
10782 source_location location)
10783 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
10784 type_(type), vals_(vals)
10787 // Return whether this is a constant initializer.
10789 is_constant_struct() const;
10793 do_traverse(Traverse* traverse);
10797 { return this->type_; }
10800 do_determine_type(const Type_context*);
10803 do_check_types(Gogo*);
10808 return new Struct_construction_expression(this->type_, this->vals_->copy(),
10813 do_is_addressable() const
10817 do_get_tree(Translate_context*);
10820 do_export(Export*) const;
10823 // The type of the struct to construct.
10825 // The list of values, in order of the fields in the struct. A NULL
10826 // entry means that the field should be zero-initialized.
10827 Expression_list* vals_;
10833 Struct_construction_expression::do_traverse(Traverse* traverse)
10835 if (this->vals_ != NULL
10836 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10837 return TRAVERSE_EXIT;
10838 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10839 return TRAVERSE_EXIT;
10840 return TRAVERSE_CONTINUE;
10843 // Return whether this is a constant initializer.
10846 Struct_construction_expression::is_constant_struct() const
10848 if (this->vals_ == NULL)
10850 for (Expression_list::const_iterator pv = this->vals_->begin();
10851 pv != this->vals_->end();
10855 && !(*pv)->is_constant()
10856 && (!(*pv)->is_composite_literal()
10857 || (*pv)->is_nonconstant_composite_literal()))
10861 const Struct_field_list* fields = this->type_->struct_type()->fields();
10862 for (Struct_field_list::const_iterator pf = fields->begin();
10863 pf != fields->end();
10866 // There are no constant constructors for interfaces.
10867 if (pf->type()->interface_type() != NULL)
10874 // Final type determination.
10877 Struct_construction_expression::do_determine_type(const Type_context*)
10879 if (this->vals_ == NULL)
10881 const Struct_field_list* fields = this->type_->struct_type()->fields();
10882 Expression_list::const_iterator pv = this->vals_->begin();
10883 for (Struct_field_list::const_iterator pf = fields->begin();
10884 pf != fields->end();
10887 if (pv == this->vals_->end())
10891 Type_context subcontext(pf->type(), false);
10892 (*pv)->determine_type(&subcontext);
10895 // Extra values are an error we will report elsewhere; we still want
10896 // to determine the type to avoid knockon errors.
10897 for (; pv != this->vals_->end(); ++pv)
10898 (*pv)->determine_type_no_context();
10904 Struct_construction_expression::do_check_types(Gogo*)
10906 if (this->vals_ == NULL)
10909 Struct_type* st = this->type_->struct_type();
10910 if (this->vals_->size() > st->field_count())
10912 this->report_error(_("too many expressions for struct"));
10916 const Struct_field_list* fields = st->fields();
10917 Expression_list::const_iterator pv = this->vals_->begin();
10919 for (Struct_field_list::const_iterator pf = fields->begin();
10920 pf != fields->end();
10923 if (pv == this->vals_->end())
10925 this->report_error(_("too few expressions for struct"));
10932 std::string reason;
10933 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
10935 if (reason.empty())
10936 error_at((*pv)->location(),
10937 "incompatible type for field %d in struct construction",
10940 error_at((*pv)->location(),
10941 ("incompatible type for field %d in "
10942 "struct construction (%s)"),
10943 i + 1, reason.c_str());
10944 this->set_is_error();
10947 go_assert(pv == this->vals_->end());
10950 // Return a tree for constructing a struct.
10953 Struct_construction_expression::do_get_tree(Translate_context* context)
10955 Gogo* gogo = context->gogo();
10957 if (this->vals_ == NULL)
10958 return this->type_->get_init_tree(gogo, false);
10960 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
10961 if (type_tree == error_mark_node)
10962 return error_mark_node;
10963 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10965 bool is_constant = true;
10966 const Struct_field_list* fields = this->type_->struct_type()->fields();
10967 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
10969 Struct_field_list::const_iterator pf = fields->begin();
10970 Expression_list::const_iterator pv = this->vals_->begin();
10971 for (tree field = TYPE_FIELDS(type_tree);
10972 field != NULL_TREE;
10973 field = DECL_CHAIN(field), ++pf)
10975 go_assert(pf != fields->end());
10978 if (pv == this->vals_->end())
10979 val = pf->type()->get_init_tree(gogo, false);
10980 else if (*pv == NULL)
10982 val = pf->type()->get_init_tree(gogo, false);
10987 val = Expression::convert_for_assignment(context, pf->type(),
10989 (*pv)->get_tree(context),
10994 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
10995 return error_mark_node;
10997 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
10998 elt->index = field;
11000 if (!TREE_CONSTANT(val))
11001 is_constant = false;
11003 go_assert(pf == fields->end());
11005 tree ret = build_constructor(type_tree, elts);
11007 TREE_CONSTANT(ret) = 1;
11011 // Export a struct construction.
11014 Struct_construction_expression::do_export(Export* exp) const
11016 exp->write_c_string("convert(");
11017 exp->write_type(this->type_);
11018 for (Expression_list::const_iterator pv = this->vals_->begin();
11019 pv != this->vals_->end();
11022 exp->write_c_string(", ");
11024 (*pv)->export_expression(exp);
11026 exp->write_c_string(")");
11029 // Make a struct composite literal. This used by the thunk code.
11032 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
11033 source_location location)
11035 go_assert(type->struct_type() != NULL);
11036 return new Struct_construction_expression(type, vals, location);
11039 // Construct an array. This class is not used directly; instead we
11040 // use the child classes, Fixed_array_construction_expression and
11041 // Open_array_construction_expression.
11043 class Array_construction_expression : public Expression
11046 Array_construction_expression(Expression_classification classification,
11047 Type* type, Expression_list* vals,
11048 source_location location)
11049 : Expression(classification, location),
11050 type_(type), vals_(vals)
11054 // Return whether this is a constant initializer.
11056 is_constant_array() const;
11058 // Return the number of elements.
11060 element_count() const
11061 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
11065 do_traverse(Traverse* traverse);
11069 { return this->type_; }
11072 do_determine_type(const Type_context*);
11075 do_check_types(Gogo*);
11078 do_is_addressable() const
11082 do_export(Export*) const;
11084 // The list of values.
11087 { return this->vals_; }
11089 // Get a constructor tree for the array values.
11091 get_constructor_tree(Translate_context* context, tree type_tree);
11094 // The type of the array to construct.
11096 // The list of values.
11097 Expression_list* vals_;
11103 Array_construction_expression::do_traverse(Traverse* traverse)
11105 if (this->vals_ != NULL
11106 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11107 return TRAVERSE_EXIT;
11108 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11109 return TRAVERSE_EXIT;
11110 return TRAVERSE_CONTINUE;
11113 // Return whether this is a constant initializer.
11116 Array_construction_expression::is_constant_array() const
11118 if (this->vals_ == NULL)
11121 // There are no constant constructors for interfaces.
11122 if (this->type_->array_type()->element_type()->interface_type() != NULL)
11125 for (Expression_list::const_iterator pv = this->vals_->begin();
11126 pv != this->vals_->end();
11130 && !(*pv)->is_constant()
11131 && (!(*pv)->is_composite_literal()
11132 || (*pv)->is_nonconstant_composite_literal()))
11138 // Final type determination.
11141 Array_construction_expression::do_determine_type(const Type_context*)
11143 if (this->vals_ == NULL)
11145 Type_context subcontext(this->type_->array_type()->element_type(), false);
11146 for (Expression_list::const_iterator pv = this->vals_->begin();
11147 pv != this->vals_->end();
11151 (*pv)->determine_type(&subcontext);
11158 Array_construction_expression::do_check_types(Gogo*)
11160 if (this->vals_ == NULL)
11163 Array_type* at = this->type_->array_type();
11165 Type* element_type = at->element_type();
11166 for (Expression_list::const_iterator pv = this->vals_->begin();
11167 pv != this->vals_->end();
11171 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
11173 error_at((*pv)->location(),
11174 "incompatible type for element %d in composite literal",
11176 this->set_is_error();
11180 Expression* length = at->length();
11181 if (length != NULL)
11186 if (at->length()->integer_constant_value(true, val, &type))
11188 if (this->vals_->size() > mpz_get_ui(val))
11189 this->report_error(_("too many elements in composite literal"));
11195 // Get a constructor tree for the array values.
11198 Array_construction_expression::get_constructor_tree(Translate_context* context,
11201 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11202 (this->vals_ == NULL
11204 : this->vals_->size()));
11205 Type* element_type = this->type_->array_type()->element_type();
11206 bool is_constant = true;
11207 if (this->vals_ != NULL)
11210 for (Expression_list::const_iterator pv = this->vals_->begin();
11211 pv != this->vals_->end();
11214 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
11215 elt->index = size_int(i);
11217 elt->value = element_type->get_init_tree(context->gogo(), false);
11220 tree value_tree = (*pv)->get_tree(context);
11221 elt->value = Expression::convert_for_assignment(context,
11227 if (elt->value == error_mark_node)
11228 return error_mark_node;
11229 if (!TREE_CONSTANT(elt->value))
11230 is_constant = false;
11234 tree ret = build_constructor(type_tree, values);
11236 TREE_CONSTANT(ret) = 1;
11240 // Export an array construction.
11243 Array_construction_expression::do_export(Export* exp) const
11245 exp->write_c_string("convert(");
11246 exp->write_type(this->type_);
11247 if (this->vals_ != NULL)
11249 for (Expression_list::const_iterator pv = this->vals_->begin();
11250 pv != this->vals_->end();
11253 exp->write_c_string(", ");
11255 (*pv)->export_expression(exp);
11258 exp->write_c_string(")");
11261 // Construct a fixed array.
11263 class Fixed_array_construction_expression :
11264 public Array_construction_expression
11267 Fixed_array_construction_expression(Type* type, Expression_list* vals,
11268 source_location location)
11269 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
11270 type, vals, location)
11272 go_assert(type->array_type() != NULL
11273 && type->array_type()->length() != NULL);
11280 return new Fixed_array_construction_expression(this->type(),
11281 (this->vals() == NULL
11283 : this->vals()->copy()),
11288 do_get_tree(Translate_context*);
11291 // Return a tree for constructing a fixed array.
11294 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
11296 Type* type = this->type();
11297 Btype* btype = type->get_backend(context->gogo());
11298 return this->get_constructor_tree(context, type_to_tree(btype));
11301 // Construct an open array.
11303 class Open_array_construction_expression : public Array_construction_expression
11306 Open_array_construction_expression(Type* type, Expression_list* vals,
11307 source_location location)
11308 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
11309 type, vals, location)
11311 go_assert(type->array_type() != NULL
11312 && type->array_type()->length() == NULL);
11316 // Note that taking the address of an open array literal is invalid.
11321 return new Open_array_construction_expression(this->type(),
11322 (this->vals() == NULL
11324 : this->vals()->copy()),
11329 do_get_tree(Translate_context*);
11332 // Return a tree for constructing an open array.
11335 Open_array_construction_expression::do_get_tree(Translate_context* context)
11337 Array_type* array_type = this->type()->array_type();
11338 if (array_type == NULL)
11340 go_assert(this->type()->is_error());
11341 return error_mark_node;
11344 Type* element_type = array_type->element_type();
11345 Btype* belement_type = element_type->get_backend(context->gogo());
11346 tree element_type_tree = type_to_tree(belement_type);
11347 if (element_type_tree == error_mark_node)
11348 return error_mark_node;
11352 if (this->vals() == NULL || this->vals()->empty())
11354 // We need to create a unique value.
11355 tree max = size_int(0);
11356 tree constructor_type = build_array_type(element_type_tree,
11357 build_index_type(max));
11358 if (constructor_type == error_mark_node)
11359 return error_mark_node;
11360 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
11361 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
11362 elt->index = size_int(0);
11363 elt->value = element_type->get_init_tree(context->gogo(), false);
11364 values = build_constructor(constructor_type, vec);
11365 if (TREE_CONSTANT(elt->value))
11366 TREE_CONSTANT(values) = 1;
11367 length_tree = size_int(0);
11371 tree max = size_int(this->vals()->size() - 1);
11372 tree constructor_type = build_array_type(element_type_tree,
11373 build_index_type(max));
11374 if (constructor_type == error_mark_node)
11375 return error_mark_node;
11376 values = this->get_constructor_tree(context, constructor_type);
11377 length_tree = size_int(this->vals()->size());
11380 if (values == error_mark_node)
11381 return error_mark_node;
11383 bool is_constant_initializer = TREE_CONSTANT(values);
11385 // We have to copy the initial values into heap memory if we are in
11386 // a function or if the values are not constants. We also have to
11387 // copy them if they may contain pointers in a non-constant context,
11388 // as otherwise the garbage collector won't see them.
11389 bool copy_to_heap = (context->function() != NULL
11390 || !is_constant_initializer
11391 || (element_type->has_pointer()
11392 && !context->is_const()));
11394 if (is_constant_initializer)
11396 tree tmp = build_decl(this->location(), VAR_DECL,
11397 create_tmp_var_name("C"), TREE_TYPE(values));
11398 DECL_EXTERNAL(tmp) = 0;
11399 TREE_PUBLIC(tmp) = 0;
11400 TREE_STATIC(tmp) = 1;
11401 DECL_ARTIFICIAL(tmp) = 1;
11404 // If we are not copying the value to the heap, we will only
11405 // initialize the value once, so we can use this directly
11406 // rather than copying it. In that case we can't make it
11407 // read-only, because the program is permitted to change it.
11408 TREE_READONLY(tmp) = 1;
11409 TREE_CONSTANT(tmp) = 1;
11411 DECL_INITIAL(tmp) = values;
11412 rest_of_decl_compilation(tmp, 1, 0);
11420 // the initializer will only run once.
11421 space = build_fold_addr_expr(values);
11426 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
11427 space = context->gogo()->allocate_memory(element_type, memsize,
11429 space = save_expr(space);
11431 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
11432 tree ref = build_fold_indirect_ref_loc(this->location(), s);
11433 TREE_THIS_NOTRAP(ref) = 1;
11434 set = build2(MODIFY_EXPR, void_type_node, ref, values);
11437 // Build a constructor for the open array.
11439 tree type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
11440 if (type_tree == error_mark_node)
11441 return error_mark_node;
11442 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11444 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
11446 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
11447 tree field = TYPE_FIELDS(type_tree);
11448 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
11449 elt->index = field;
11450 elt->value = fold_convert(TREE_TYPE(field), space);
11452 elt = VEC_quick_push(constructor_elt, init, NULL);
11453 field = DECL_CHAIN(field);
11454 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
11455 elt->index = field;
11456 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11458 elt = VEC_quick_push(constructor_elt, init, NULL);
11459 field = DECL_CHAIN(field);
11460 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
11461 elt->index = field;
11462 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11464 tree constructor = build_constructor(type_tree, init);
11465 if (constructor == error_mark_node)
11466 return error_mark_node;
11468 TREE_CONSTANT(constructor) = 1;
11470 if (set == NULL_TREE)
11471 return constructor;
11473 return build2(COMPOUND_EXPR, type_tree, set, constructor);
11476 // Make a slice composite literal. This is used by the type
11477 // descriptor code.
11480 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
11481 source_location location)
11483 go_assert(type->is_open_array_type());
11484 return new Open_array_construction_expression(type, vals, location);
11487 // Construct a map.
11489 class Map_construction_expression : public Expression
11492 Map_construction_expression(Type* type, Expression_list* vals,
11493 source_location location)
11494 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
11495 type_(type), vals_(vals)
11496 { go_assert(vals == NULL || vals->size() % 2 == 0); }
11500 do_traverse(Traverse* traverse);
11504 { return this->type_; }
11507 do_determine_type(const Type_context*);
11510 do_check_types(Gogo*);
11515 return new Map_construction_expression(this->type_, this->vals_->copy(),
11520 do_get_tree(Translate_context*);
11523 do_export(Export*) const;
11526 // The type of the map to construct.
11528 // The list of values.
11529 Expression_list* vals_;
11535 Map_construction_expression::do_traverse(Traverse* traverse)
11537 if (this->vals_ != NULL
11538 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11539 return TRAVERSE_EXIT;
11540 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11541 return TRAVERSE_EXIT;
11542 return TRAVERSE_CONTINUE;
11545 // Final type determination.
11548 Map_construction_expression::do_determine_type(const Type_context*)
11550 if (this->vals_ == NULL)
11553 Map_type* mt = this->type_->map_type();
11554 Type_context key_context(mt->key_type(), false);
11555 Type_context val_context(mt->val_type(), false);
11556 for (Expression_list::const_iterator pv = this->vals_->begin();
11557 pv != this->vals_->end();
11560 (*pv)->determine_type(&key_context);
11562 (*pv)->determine_type(&val_context);
11569 Map_construction_expression::do_check_types(Gogo*)
11571 if (this->vals_ == NULL)
11574 Map_type* mt = this->type_->map_type();
11576 Type* key_type = mt->key_type();
11577 Type* val_type = mt->val_type();
11578 for (Expression_list::const_iterator pv = this->vals_->begin();
11579 pv != this->vals_->end();
11582 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
11584 error_at((*pv)->location(),
11585 "incompatible type for element %d key in map construction",
11587 this->set_is_error();
11590 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
11592 error_at((*pv)->location(),
11593 ("incompatible type for element %d value "
11594 "in map construction"),
11596 this->set_is_error();
11601 // Return a tree for constructing a map.
11604 Map_construction_expression::do_get_tree(Translate_context* context)
11606 Gogo* gogo = context->gogo();
11607 source_location loc = this->location();
11609 Map_type* mt = this->type_->map_type();
11611 // Build a struct to hold the key and value.
11612 tree struct_type = make_node(RECORD_TYPE);
11614 Type* key_type = mt->key_type();
11615 tree id = get_identifier("__key");
11616 tree key_type_tree = type_to_tree(key_type->get_backend(gogo));
11617 if (key_type_tree == error_mark_node)
11618 return error_mark_node;
11619 tree key_field = build_decl(loc, FIELD_DECL, id, key_type_tree);
11620 DECL_CONTEXT(key_field) = struct_type;
11621 TYPE_FIELDS(struct_type) = key_field;
11623 Type* val_type = mt->val_type();
11624 id = get_identifier("__val");
11625 tree val_type_tree = type_to_tree(val_type->get_backend(gogo));
11626 if (val_type_tree == error_mark_node)
11627 return error_mark_node;
11628 tree val_field = build_decl(loc, FIELD_DECL, id, val_type_tree);
11629 DECL_CONTEXT(val_field) = struct_type;
11630 DECL_CHAIN(key_field) = val_field;
11632 layout_type(struct_type);
11634 bool is_constant = true;
11639 if (this->vals_ == NULL || this->vals_->empty())
11641 valaddr = null_pointer_node;
11642 make_tmp = NULL_TREE;
11646 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11647 this->vals_->size() / 2);
11649 for (Expression_list::const_iterator pv = this->vals_->begin();
11650 pv != this->vals_->end();
11653 bool one_is_constant = true;
11655 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
11657 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
11658 elt->index = key_field;
11659 tree val_tree = (*pv)->get_tree(context);
11660 elt->value = Expression::convert_for_assignment(context, key_type,
11663 if (elt->value == error_mark_node)
11664 return error_mark_node;
11665 if (!TREE_CONSTANT(elt->value))
11666 one_is_constant = false;
11670 elt = VEC_quick_push(constructor_elt, one, NULL);
11671 elt->index = val_field;
11672 val_tree = (*pv)->get_tree(context);
11673 elt->value = Expression::convert_for_assignment(context, val_type,
11676 if (elt->value == error_mark_node)
11677 return error_mark_node;
11678 if (!TREE_CONSTANT(elt->value))
11679 one_is_constant = false;
11681 elt = VEC_quick_push(constructor_elt, values, NULL);
11682 elt->index = size_int(i);
11683 elt->value = build_constructor(struct_type, one);
11684 if (one_is_constant)
11685 TREE_CONSTANT(elt->value) = 1;
11687 is_constant = false;
11690 tree index_type = build_index_type(size_int(i - 1));
11691 tree array_type = build_array_type(struct_type, index_type);
11692 tree init = build_constructor(array_type, values);
11694 TREE_CONSTANT(init) = 1;
11696 if (current_function_decl != NULL)
11698 tmp = create_tmp_var(array_type, get_name(array_type));
11699 DECL_INITIAL(tmp) = init;
11700 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
11701 TREE_ADDRESSABLE(tmp) = 1;
11705 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
11706 DECL_EXTERNAL(tmp) = 0;
11707 TREE_PUBLIC(tmp) = 0;
11708 TREE_STATIC(tmp) = 1;
11709 DECL_ARTIFICIAL(tmp) = 1;
11710 if (!TREE_CONSTANT(init))
11711 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
11715 TREE_READONLY(tmp) = 1;
11716 TREE_CONSTANT(tmp) = 1;
11717 DECL_INITIAL(tmp) = init;
11718 make_tmp = NULL_TREE;
11720 rest_of_decl_compilation(tmp, 1, 0);
11723 valaddr = build_fold_addr_expr(tmp);
11726 tree descriptor = gogo->map_descriptor(mt);
11728 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
11729 if (type_tree == error_mark_node)
11730 return error_mark_node;
11732 static tree construct_map_fndecl;
11733 tree call = Gogo::call_builtin(&construct_map_fndecl,
11735 "__go_construct_map",
11738 TREE_TYPE(descriptor),
11743 TYPE_SIZE_UNIT(struct_type),
11745 byte_position(val_field),
11747 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
11748 const_ptr_type_node,
11749 fold_convert(const_ptr_type_node, valaddr));
11750 if (call == error_mark_node)
11751 return error_mark_node;
11754 if (make_tmp == NULL)
11757 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
11761 // Export an array construction.
11764 Map_construction_expression::do_export(Export* exp) const
11766 exp->write_c_string("convert(");
11767 exp->write_type(this->type_);
11768 for (Expression_list::const_iterator pv = this->vals_->begin();
11769 pv != this->vals_->end();
11772 exp->write_c_string(", ");
11773 (*pv)->export_expression(exp);
11775 exp->write_c_string(")");
11778 // A general composite literal. This is lowered to a type specific
11781 class Composite_literal_expression : public Parser_expression
11784 Composite_literal_expression(Type* type, int depth, bool has_keys,
11785 Expression_list* vals, source_location location)
11786 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
11787 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
11792 do_traverse(Traverse* traverse);
11795 do_lower(Gogo*, Named_object*, int);
11800 return new Composite_literal_expression(this->type_, this->depth_,
11802 (this->vals_ == NULL
11804 : this->vals_->copy()),
11810 lower_struct(Gogo*, Type*);
11813 lower_array(Type*);
11816 make_array(Type*, Expression_list*);
11819 lower_map(Gogo*, Named_object*, Type*);
11821 // The type of the composite literal.
11823 // The depth within a list of composite literals within a composite
11824 // literal, when the type is omitted.
11826 // The values to put in the composite literal.
11827 Expression_list* vals_;
11828 // If this is true, then VALS_ is a list of pairs: a key and a
11829 // value. In an array initializer, a missing key will be NULL.
11836 Composite_literal_expression::do_traverse(Traverse* traverse)
11838 if (this->vals_ != NULL
11839 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11840 return TRAVERSE_EXIT;
11841 return Type::traverse(this->type_, traverse);
11844 // Lower a generic composite literal into a specific version based on
11848 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int)
11850 Type* type = this->type_;
11852 for (int depth = this->depth_; depth > 0; --depth)
11854 if (type->array_type() != NULL)
11855 type = type->array_type()->element_type();
11856 else if (type->map_type() != NULL)
11857 type = type->map_type()->val_type();
11860 if (!type->is_error())
11861 error_at(this->location(),
11862 ("may only omit types within composite literals "
11863 "of slice, array, or map type"));
11864 return Expression::make_error(this->location());
11868 if (type->is_error())
11869 return Expression::make_error(this->location());
11870 else if (type->struct_type() != NULL)
11871 return this->lower_struct(gogo, type);
11872 else if (type->array_type() != NULL)
11873 return this->lower_array(type);
11874 else if (type->map_type() != NULL)
11875 return this->lower_map(gogo, function, type);
11878 error_at(this->location(),
11879 ("expected struct, slice, array, or map type "
11880 "for composite literal"));
11881 return Expression::make_error(this->location());
11885 // Lower a struct composite literal.
11888 Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
11890 source_location location = this->location();
11891 Struct_type* st = type->struct_type();
11892 if (this->vals_ == NULL || !this->has_keys_)
11893 return new Struct_construction_expression(type, this->vals_, location);
11895 size_t field_count = st->field_count();
11896 std::vector<Expression*> vals(field_count);
11897 Expression_list::const_iterator p = this->vals_->begin();
11898 while (p != this->vals_->end())
11900 Expression* name_expr = *p;
11903 go_assert(p != this->vals_->end());
11904 Expression* val = *p;
11908 if (name_expr == NULL)
11910 error_at(val->location(), "mixture of field and value initializers");
11911 return Expression::make_error(location);
11914 bool bad_key = false;
11916 const Named_object* no = NULL;
11917 switch (name_expr->classification())
11919 case EXPRESSION_UNKNOWN_REFERENCE:
11920 name = name_expr->unknown_expression()->name();
11923 case EXPRESSION_CONST_REFERENCE:
11924 no = static_cast<Const_expression*>(name_expr)->named_object();
11927 case EXPRESSION_TYPE:
11929 Type* t = name_expr->type();
11930 Named_type* nt = t->named_type();
11934 no = nt->named_object();
11938 case EXPRESSION_VAR_REFERENCE:
11939 no = name_expr->var_expression()->named_object();
11942 case EXPRESSION_FUNC_REFERENCE:
11943 no = name_expr->func_expression()->named_object();
11946 case EXPRESSION_UNARY:
11947 // If there is a local variable around with the same name as
11948 // the field, and this occurs in the closure, then the
11949 // parser may turn the field reference into an indirection
11950 // through the closure. FIXME: This is a mess.
11953 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
11954 if (ue->op() == OPERATOR_MULT)
11956 Field_reference_expression* fre =
11957 ue->operand()->field_reference_expression();
11961 fre->expr()->type()->deref()->struct_type();
11964 const Struct_field* sf = st->field(fre->field_index());
11965 name = sf->field_name();
11967 snprintf(buf, sizeof buf, "%u", fre->field_index());
11968 size_t buflen = strlen(buf);
11969 if (name.compare(name.length() - buflen, buflen, buf)
11972 name = name.substr(0, name.length() - buflen);
11987 error_at(name_expr->location(), "expected struct field name");
11988 return Expression::make_error(location);
11995 // A predefined name won't be packed. If it starts with a
11996 // lower case letter we need to check for that case, because
11997 // the field name will be packed.
11998 if (!Gogo::is_hidden_name(name)
12002 Named_object* gno = gogo->lookup_global(name.c_str());
12004 name = gogo->pack_hidden_name(name, false);
12008 unsigned int index;
12009 const Struct_field* sf = st->find_local_field(name, &index);
12012 error_at(name_expr->location(), "unknown field %qs in %qs",
12013 Gogo::message_name(name).c_str(),
12014 (type->named_type() != NULL
12015 ? type->named_type()->message_name().c_str()
12016 : "unnamed struct"));
12017 return Expression::make_error(location);
12019 if (vals[index] != NULL)
12021 error_at(name_expr->location(),
12022 "duplicate value for field %qs in %qs",
12023 Gogo::message_name(name).c_str(),
12024 (type->named_type() != NULL
12025 ? type->named_type()->message_name().c_str()
12026 : "unnamed struct"));
12027 return Expression::make_error(location);
12033 Expression_list* list = new Expression_list;
12034 list->reserve(field_count);
12035 for (size_t i = 0; i < field_count; ++i)
12036 list->push_back(vals[i]);
12038 return new Struct_construction_expression(type, list, location);
12041 // Lower an array composite literal.
12044 Composite_literal_expression::lower_array(Type* type)
12046 source_location location = this->location();
12047 if (this->vals_ == NULL || !this->has_keys_)
12048 return this->make_array(type, this->vals_);
12050 std::vector<Expression*> vals;
12051 vals.reserve(this->vals_->size());
12052 unsigned long index = 0;
12053 Expression_list::const_iterator p = this->vals_->begin();
12054 while (p != this->vals_->end())
12056 Expression* index_expr = *p;
12059 go_assert(p != this->vals_->end());
12060 Expression* val = *p;
12064 if (index_expr != NULL)
12070 if (!index_expr->integer_constant_value(true, ival, &dummy))
12073 error_at(index_expr->location(),
12074 "index expression is not integer constant");
12075 return Expression::make_error(location);
12078 if (mpz_sgn(ival) < 0)
12081 error_at(index_expr->location(), "index expression is negative");
12082 return Expression::make_error(location);
12085 index = mpz_get_ui(ival);
12086 if (mpz_cmp_ui(ival, index) != 0)
12089 error_at(index_expr->location(), "index value overflow");
12090 return Expression::make_error(location);
12093 Named_type* ntype = Type::lookup_integer_type("int");
12094 Integer_type* inttype = ntype->integer_type();
12096 mpz_init_set_ui(max, 1);
12097 mpz_mul_2exp(max, max, inttype->bits() - 1);
12098 bool ok = mpz_cmp(ival, max) < 0;
12103 error_at(index_expr->location(), "index value overflow");
12104 return Expression::make_error(location);
12109 // FIXME: Our representation isn't very good; this avoids
12111 if (index > 0x1000000)
12113 error_at(index_expr->location(), "index too large for compiler");
12114 return Expression::make_error(location);
12118 if (index == vals.size())
12119 vals.push_back(val);
12122 if (index > vals.size())
12124 vals.reserve(index + 32);
12125 vals.resize(index + 1, static_cast<Expression*>(NULL));
12127 if (vals[index] != NULL)
12129 error_at((index_expr != NULL
12130 ? index_expr->location()
12131 : val->location()),
12132 "duplicate value for index %lu",
12134 return Expression::make_error(location);
12142 size_t size = vals.size();
12143 Expression_list* list = new Expression_list;
12144 list->reserve(size);
12145 for (size_t i = 0; i < size; ++i)
12146 list->push_back(vals[i]);
12148 return this->make_array(type, list);
12151 // Actually build the array composite literal. This handles
12155 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
12157 source_location location = this->location();
12158 Array_type* at = type->array_type();
12159 if (at->length() != NULL && at->length()->is_nil_expression())
12161 size_t size = vals == NULL ? 0 : vals->size();
12163 mpz_init_set_ui(vlen, size);
12164 Expression* elen = Expression::make_integer(&vlen, NULL, location);
12166 at = Type::make_array_type(at->element_type(), elen);
12169 if (at->length() != NULL)
12170 return new Fixed_array_construction_expression(type, vals, location);
12172 return new Open_array_construction_expression(type, vals, location);
12175 // Lower a map composite literal.
12178 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
12181 source_location location = this->location();
12182 if (this->vals_ != NULL)
12184 if (!this->has_keys_)
12186 error_at(location, "map composite literal must have keys");
12187 return Expression::make_error(location);
12190 for (Expression_list::iterator p = this->vals_->begin();
12191 p != this->vals_->end();
12197 error_at((*p)->location(),
12198 "map composite literal must have keys for every value");
12199 return Expression::make_error(location);
12201 // Make sure we have lowered the key; it may not have been
12202 // lowered in order to handle keys for struct composite
12203 // literals. Lower it now to get the right error message.
12204 if ((*p)->unknown_expression() != NULL)
12206 (*p)->unknown_expression()->clear_is_composite_literal_key();
12207 gogo->lower_expression(function, &*p);
12208 go_assert((*p)->is_error_expression());
12209 return Expression::make_error(location);
12214 return new Map_construction_expression(type, this->vals_, location);
12217 // Make a composite literal expression.
12220 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
12221 Expression_list* vals,
12222 source_location location)
12224 return new Composite_literal_expression(type, depth, has_keys, vals,
12228 // Return whether this expression is a composite literal.
12231 Expression::is_composite_literal() const
12233 switch (this->classification_)
12235 case EXPRESSION_COMPOSITE_LITERAL:
12236 case EXPRESSION_STRUCT_CONSTRUCTION:
12237 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12238 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12239 case EXPRESSION_MAP_CONSTRUCTION:
12246 // Return whether this expression is a composite literal which is not
12250 Expression::is_nonconstant_composite_literal() const
12252 switch (this->classification_)
12254 case EXPRESSION_STRUCT_CONSTRUCTION:
12256 const Struct_construction_expression *psce =
12257 static_cast<const Struct_construction_expression*>(this);
12258 return !psce->is_constant_struct();
12260 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12262 const Fixed_array_construction_expression *pace =
12263 static_cast<const Fixed_array_construction_expression*>(this);
12264 return !pace->is_constant_array();
12266 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12268 const Open_array_construction_expression *pace =
12269 static_cast<const Open_array_construction_expression*>(this);
12270 return !pace->is_constant_array();
12272 case EXPRESSION_MAP_CONSTRUCTION:
12279 // Return true if this is a reference to a local variable.
12282 Expression::is_local_variable() const
12284 const Var_expression* ve = this->var_expression();
12287 const Named_object* no = ve->named_object();
12288 return (no->is_result_variable()
12289 || (no->is_variable() && !no->var_value()->is_global()));
12292 // Class Type_guard_expression.
12297 Type_guard_expression::do_traverse(Traverse* traverse)
12299 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
12300 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12301 return TRAVERSE_EXIT;
12302 return TRAVERSE_CONTINUE;
12305 // Check types of a type guard expression. The expression must have
12306 // an interface type, but the actual type conversion is checked at run
12310 Type_guard_expression::do_check_types(Gogo*)
12312 // 6g permits using a type guard with unsafe.pointer; we are
12314 Type* expr_type = this->expr_->type();
12315 if (expr_type->is_unsafe_pointer_type())
12317 if (this->type_->points_to() == NULL
12318 && (this->type_->integer_type() == NULL
12319 || (this->type_->forwarded()
12320 != Type::lookup_integer_type("uintptr"))))
12321 this->report_error(_("invalid unsafe.Pointer conversion"));
12323 else if (this->type_->is_unsafe_pointer_type())
12325 if (expr_type->points_to() == NULL
12326 && (expr_type->integer_type() == NULL
12327 || (expr_type->forwarded()
12328 != Type::lookup_integer_type("uintptr"))))
12329 this->report_error(_("invalid unsafe.Pointer conversion"));
12331 else if (expr_type->interface_type() == NULL)
12333 if (!expr_type->is_error() && !this->type_->is_error())
12334 this->report_error(_("type assertion only valid for interface types"));
12335 this->set_is_error();
12337 else if (this->type_->interface_type() == NULL)
12339 std::string reason;
12340 if (!expr_type->interface_type()->implements_interface(this->type_,
12343 if (!this->type_->is_error())
12345 if (reason.empty())
12346 this->report_error(_("impossible type assertion: "
12347 "type does not implement interface"));
12349 error_at(this->location(),
12350 ("impossible type assertion: "
12351 "type does not implement interface (%s)"),
12354 this->set_is_error();
12359 // Return a tree for a type guard expression.
12362 Type_guard_expression::do_get_tree(Translate_context* context)
12364 Gogo* gogo = context->gogo();
12365 tree expr_tree = this->expr_->get_tree(context);
12366 if (expr_tree == error_mark_node)
12367 return error_mark_node;
12368 Type* expr_type = this->expr_->type();
12369 if ((this->type_->is_unsafe_pointer_type()
12370 && (expr_type->points_to() != NULL
12371 || expr_type->integer_type() != NULL))
12372 || (expr_type->is_unsafe_pointer_type()
12373 && this->type_->points_to() != NULL))
12374 return convert_to_pointer(type_to_tree(this->type_->get_backend(gogo)),
12376 else if (expr_type->is_unsafe_pointer_type()
12377 && this->type_->integer_type() != NULL)
12378 return convert_to_integer(type_to_tree(this->type_->get_backend(gogo)),
12380 else if (this->type_->interface_type() != NULL)
12381 return Expression::convert_interface_to_interface(context, this->type_,
12382 this->expr_->type(),
12386 return Expression::convert_for_assignment(context, this->type_,
12387 this->expr_->type(), expr_tree,
12391 // Make a type guard expression.
12394 Expression::make_type_guard(Expression* expr, Type* type,
12395 source_location location)
12397 return new Type_guard_expression(expr, type, location);
12400 // Class Heap_composite_expression.
12402 // When you take the address of a composite literal, it is allocated
12403 // on the heap. This class implements that.
12405 class Heap_composite_expression : public Expression
12408 Heap_composite_expression(Expression* expr, source_location location)
12409 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
12415 do_traverse(Traverse* traverse)
12416 { return Expression::traverse(&this->expr_, traverse); }
12420 { return Type::make_pointer_type(this->expr_->type()); }
12423 do_determine_type(const Type_context*)
12424 { this->expr_->determine_type_no_context(); }
12429 return Expression::make_heap_composite(this->expr_->copy(),
12434 do_get_tree(Translate_context*);
12436 // We only export global objects, and the parser does not generate
12437 // this in global scope.
12439 do_export(Export*) const
12440 { go_unreachable(); }
12443 // The composite literal which is being put on the heap.
12447 // Return a tree which allocates a composite literal on the heap.
12450 Heap_composite_expression::do_get_tree(Translate_context* context)
12452 tree expr_tree = this->expr_->get_tree(context);
12453 if (expr_tree == error_mark_node)
12454 return error_mark_node;
12455 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
12456 go_assert(TREE_CODE(expr_size) == INTEGER_CST);
12457 tree space = context->gogo()->allocate_memory(this->expr_->type(),
12458 expr_size, this->location());
12459 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
12460 space = save_expr(space);
12461 tree ref = build_fold_indirect_ref_loc(this->location(), space);
12462 TREE_THIS_NOTRAP(ref) = 1;
12463 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
12464 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
12466 SET_EXPR_LOCATION(ret, this->location());
12470 // Allocate a composite literal on the heap.
12473 Expression::make_heap_composite(Expression* expr, source_location location)
12475 return new Heap_composite_expression(expr, location);
12478 // Class Receive_expression.
12480 // Return the type of a receive expression.
12483 Receive_expression::do_type()
12485 Channel_type* channel_type = this->channel_->type()->channel_type();
12486 if (channel_type == NULL)
12487 return Type::make_error_type();
12488 return channel_type->element_type();
12491 // Check types for a receive expression.
12494 Receive_expression::do_check_types(Gogo*)
12496 Type* type = this->channel_->type();
12497 if (type->is_error())
12499 this->set_is_error();
12502 if (type->channel_type() == NULL)
12504 this->report_error(_("expected channel"));
12507 if (!type->channel_type()->may_receive())
12509 this->report_error(_("invalid receive on send-only channel"));
12514 // Get a tree for a receive expression.
12517 Receive_expression::do_get_tree(Translate_context* context)
12519 Channel_type* channel_type = this->channel_->type()->channel_type();
12520 if (channel_type == NULL)
12522 go_assert(this->channel_->type()->is_error());
12523 return error_mark_node;
12525 Type* element_type = channel_type->element_type();
12526 Btype* element_type_btype = element_type->get_backend(context->gogo());
12527 tree element_type_tree = type_to_tree(element_type_btype);
12529 tree channel = this->channel_->get_tree(context);
12530 if (element_type_tree == error_mark_node || channel == error_mark_node)
12531 return error_mark_node;
12533 return Gogo::receive_from_channel(element_type_tree, channel,
12534 this->for_select_, this->location());
12537 // Make a receive expression.
12539 Receive_expression*
12540 Expression::make_receive(Expression* channel, source_location location)
12542 return new Receive_expression(channel, location);
12545 // An expression which evaluates to a pointer to the type descriptor
12548 class Type_descriptor_expression : public Expression
12551 Type_descriptor_expression(Type* type, source_location location)
12552 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
12559 { return Type::make_type_descriptor_ptr_type(); }
12562 do_determine_type(const Type_context*)
12570 do_get_tree(Translate_context* context)
12571 { return this->type_->type_descriptor_pointer(context->gogo()); }
12574 // The type for which this is the descriptor.
12578 // Make a type descriptor expression.
12581 Expression::make_type_descriptor(Type* type, source_location location)
12583 return new Type_descriptor_expression(type, location);
12586 // An expression which evaluates to some characteristic of a type.
12587 // This is only used to initialize fields of a type descriptor. Using
12588 // a new expression class is slightly inefficient but gives us a good
12589 // separation between the frontend and the middle-end with regard to
12590 // how types are laid out.
12592 class Type_info_expression : public Expression
12595 Type_info_expression(Type* type, Type_info type_info)
12596 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
12597 type_(type), type_info_(type_info)
12605 do_determine_type(const Type_context*)
12613 do_get_tree(Translate_context* context);
12616 // The type for which we are getting information.
12618 // What information we want.
12619 Type_info type_info_;
12622 // The type is chosen to match what the type descriptor struct
12626 Type_info_expression::do_type()
12628 switch (this->type_info_)
12630 case TYPE_INFO_SIZE:
12631 return Type::lookup_integer_type("uintptr");
12632 case TYPE_INFO_ALIGNMENT:
12633 case TYPE_INFO_FIELD_ALIGNMENT:
12634 return Type::lookup_integer_type("uint8");
12640 // Return type information in GENERIC.
12643 Type_info_expression::do_get_tree(Translate_context* context)
12645 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
12646 if (type_tree == error_mark_node)
12647 return error_mark_node;
12649 tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
12650 go_assert(val_type_tree != error_mark_node);
12652 if (this->type_info_ == TYPE_INFO_SIZE)
12653 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12654 TYPE_SIZE_UNIT(type_tree));
12658 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
12659 val = go_type_alignment(type_tree);
12661 val = go_field_alignment(type_tree);
12662 return build_int_cstu(val_type_tree, val);
12666 // Make a type info expression.
12669 Expression::make_type_info(Type* type, Type_info type_info)
12671 return new Type_info_expression(type, type_info);
12674 // An expression which evaluates to the offset of a field within a
12675 // struct. This, like Type_info_expression, q.v., is only used to
12676 // initialize fields of a type descriptor.
12678 class Struct_field_offset_expression : public Expression
12681 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
12682 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
12683 type_(type), field_(field)
12689 { return Type::lookup_integer_type("uintptr"); }
12692 do_determine_type(const Type_context*)
12700 do_get_tree(Translate_context* context);
12703 // The type of the struct.
12704 Struct_type* type_;
12706 const Struct_field* field_;
12709 // Return a struct field offset in GENERIC.
12712 Struct_field_offset_expression::do_get_tree(Translate_context* context)
12714 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
12715 if (type_tree == error_mark_node)
12716 return error_mark_node;
12718 tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
12719 go_assert(val_type_tree != error_mark_node);
12721 const Struct_field_list* fields = this->type_->fields();
12722 tree struct_field_tree = TYPE_FIELDS(type_tree);
12723 Struct_field_list::const_iterator p;
12724 for (p = fields->begin();
12725 p != fields->end();
12726 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
12728 go_assert(struct_field_tree != NULL_TREE);
12729 if (&*p == this->field_)
12732 go_assert(&*p == this->field_);
12734 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12735 byte_position(struct_field_tree));
12738 // Make an expression for a struct field offset.
12741 Expression::make_struct_field_offset(Struct_type* type,
12742 const Struct_field* field)
12744 return new Struct_field_offset_expression(type, field);
12747 // An expression which evaluates to the address of an unnamed label.
12749 class Label_addr_expression : public Expression
12752 Label_addr_expression(Label* label, source_location location)
12753 : Expression(EXPRESSION_LABEL_ADDR, location),
12760 { return Type::make_pointer_type(Type::make_void_type()); }
12763 do_determine_type(const Type_context*)
12768 { return new Label_addr_expression(this->label_, this->location()); }
12771 do_get_tree(Translate_context* context)
12773 return expr_to_tree(this->label_->get_addr(context, this->location()));
12777 // The label whose address we are taking.
12781 // Make an expression for the address of an unnamed label.
12784 Expression::make_label_addr(Label* label, source_location location)
12786 return new Label_addr_expression(label, location);
12789 // Import an expression. This comes at the end in order to see the
12790 // various class definitions.
12793 Expression::import_expression(Import* imp)
12795 int c = imp->peek_char();
12796 if (imp->match_c_string("- ")
12797 || imp->match_c_string("! ")
12798 || imp->match_c_string("^ "))
12799 return Unary_expression::do_import(imp);
12801 return Binary_expression::do_import(imp);
12802 else if (imp->match_c_string("true")
12803 || imp->match_c_string("false"))
12804 return Boolean_expression::do_import(imp);
12806 return String_expression::do_import(imp);
12807 else if (c == '-' || (c >= '0' && c <= '9'))
12809 // This handles integers, floats and complex constants.
12810 return Integer_expression::do_import(imp);
12812 else if (imp->match_c_string("nil"))
12813 return Nil_expression::do_import(imp);
12814 else if (imp->match_c_string("convert"))
12815 return Type_conversion_expression::do_import(imp);
12818 error_at(imp->location(), "import error: expected expression");
12819 return Expression::make_error(imp->location());
12823 // Class Expression_list.
12825 // Traverse the list.
12828 Expression_list::traverse(Traverse* traverse)
12830 for (Expression_list::iterator p = this->begin();
12836 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12837 return TRAVERSE_EXIT;
12840 return TRAVERSE_CONTINUE;
12846 Expression_list::copy()
12848 Expression_list* ret = new Expression_list();
12849 for (Expression_list::iterator p = this->begin();
12854 ret->push_back(NULL);
12856 ret->push_back((*p)->copy());
12861 // Return whether an expression list has an error expression.
12864 Expression_list::contains_error() const
12866 for (Expression_list::const_iterator p = this->begin();
12869 if (*p != NULL && (*p)->is_error_expression())