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"
38 #include "expressions.h"
43 Expression::Expression(Expression_classification classification,
45 : classification_(classification), location_(location)
49 Expression::~Expression()
53 // If this expression has a constant integer value, return it.
56 Expression::integer_constant_value(bool iota_is_constant, mpz_t val,
60 return this->do_integer_constant_value(iota_is_constant, val, ptype);
63 // If this expression has a constant floating point value, return it.
66 Expression::float_constant_value(mpfr_t val, Type** ptype) const
69 if (this->do_float_constant_value(val, ptype))
75 if (!this->do_integer_constant_value(false, ival, &t))
79 mpfr_set_z(val, ival, GMP_RNDN);
86 // If this expression has a constant complex value, return it.
89 Expression::complex_constant_value(mpfr_t real, mpfr_t imag,
93 if (this->do_complex_constant_value(real, imag, ptype))
96 if (this->float_constant_value(real, &t))
98 mpfr_set_ui(imag, 0, GMP_RNDN);
104 // Traverse the expressions.
107 Expression::traverse(Expression** pexpr, Traverse* traverse)
109 Expression* expr = *pexpr;
110 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
112 int t = traverse->expression(pexpr);
113 if (t == TRAVERSE_EXIT)
114 return TRAVERSE_EXIT;
115 else if (t == TRAVERSE_SKIP_COMPONENTS)
116 return TRAVERSE_CONTINUE;
118 return expr->do_traverse(traverse);
121 // Traverse subexpressions of this expression.
124 Expression::traverse_subexpressions(Traverse* traverse)
126 return this->do_traverse(traverse);
129 // Default implementation for do_traverse for child classes.
132 Expression::do_traverse(Traverse*)
134 return TRAVERSE_CONTINUE;
137 // This virtual function is called by the parser if the value of this
138 // expression is being discarded. By default, we give an error.
139 // Expressions with side effects override.
142 Expression::do_discarding_value()
144 this->unused_value_error();
147 // This virtual function is called to export expressions. This will
148 // only be used by expressions which may be constant.
151 Expression::do_export(Export*) const
156 // Give an error saying that the value of the expression is not used.
159 Expression::unused_value_error()
161 error_at(this->location(), "value computed is not used");
164 // Note that this expression is an error. This is called by children
165 // when they discover an error.
168 Expression::set_is_error()
170 this->classification_ = EXPRESSION_ERROR;
173 // For children to call to report an error conveniently.
176 Expression::report_error(const char* msg)
178 error_at(this->location_, "%s", msg);
179 this->set_is_error();
182 // Set types of variables and constants. This is implemented by the
186 Expression::determine_type(const Type_context* context)
188 this->do_determine_type(context);
191 // Set types when there is no context.
194 Expression::determine_type_no_context()
196 Type_context context;
197 this->do_determine_type(&context);
200 // Return a tree handling any conversions which must be done during
204 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
205 Type* rhs_type, tree rhs_tree,
208 if (lhs_type == rhs_type)
211 if (lhs_type->is_error() || rhs_type->is_error())
212 return error_mark_node;
214 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
215 return error_mark_node;
217 Gogo* gogo = context->gogo();
219 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
220 if (lhs_type_tree == error_mark_node)
221 return error_mark_node;
223 if (lhs_type->interface_type() != NULL)
225 if (rhs_type->interface_type() == NULL)
226 return Expression::convert_type_to_interface(context, lhs_type,
230 return Expression::convert_interface_to_interface(context, lhs_type,
234 else if (rhs_type->interface_type() != NULL)
235 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
237 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
239 // Assigning nil to an open array.
240 go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
242 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
244 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
245 tree field = TYPE_FIELDS(lhs_type_tree);
246 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
249 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
251 elt = VEC_quick_push(constructor_elt, init, NULL);
252 field = DECL_CHAIN(field);
253 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
256 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
258 elt = VEC_quick_push(constructor_elt, init, NULL);
259 field = DECL_CHAIN(field);
260 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
263 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
265 tree val = build_constructor(lhs_type_tree, init);
266 TREE_CONSTANT(val) = 1;
270 else if (rhs_type->is_nil_type())
272 // The left hand side should be a pointer type at the tree
274 go_assert(POINTER_TYPE_P(lhs_type_tree));
275 return fold_convert(lhs_type_tree, null_pointer_node);
277 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
279 // No conversion is needed.
282 else if (POINTER_TYPE_P(lhs_type_tree)
283 || INTEGRAL_TYPE_P(lhs_type_tree)
284 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
285 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
286 return fold_convert_loc(location.gcc_location(), lhs_type_tree, rhs_tree);
287 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
288 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
290 // This conversion must be permitted by Go, or we wouldn't have
292 go_assert(int_size_in_bytes(lhs_type_tree)
293 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
294 return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
295 lhs_type_tree, rhs_tree);
299 go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
304 // Return a tree for a conversion from a non-interface type to an
308 Expression::convert_type_to_interface(Translate_context* context,
309 Type* lhs_type, Type* rhs_type,
310 tree rhs_tree, Location location)
312 Gogo* gogo = context->gogo();
313 Interface_type* lhs_interface_type = lhs_type->interface_type();
314 bool lhs_is_empty = lhs_interface_type->is_empty();
316 // Since RHS_TYPE is a static type, we can create the interface
317 // method table at compile time.
319 // When setting an interface to nil, we just set both fields to
321 if (rhs_type->is_nil_type())
323 Btype* lhs_btype = lhs_type->get_backend(gogo);
324 return expr_to_tree(gogo->backend()->zero_expression(lhs_btype));
327 // This should have been checked already.
328 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
330 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
331 if (lhs_type_tree == error_mark_node)
332 return error_mark_node;
334 // An interface is a tuple. If LHS_TYPE is an empty interface type,
335 // then the first field is the type descriptor for RHS_TYPE.
336 // Otherwise it is the interface method table for RHS_TYPE.
337 tree first_field_value;
339 first_field_value = rhs_type->type_descriptor_pointer(gogo, location);
342 // Build the interface method table for this interface and this
343 // object type: a list of function pointers for each interface
345 Named_type* rhs_named_type = rhs_type->named_type();
346 bool is_pointer = false;
347 if (rhs_named_type == NULL)
349 rhs_named_type = rhs_type->deref()->named_type();
353 if (rhs_named_type == NULL)
354 method_table = null_pointer_node;
357 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
359 first_field_value = fold_convert_loc(location.gcc_location(),
360 const_ptr_type_node, method_table);
362 if (first_field_value == error_mark_node)
363 return error_mark_node;
365 // Start building a constructor for the value we will return.
367 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
369 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
370 tree field = TYPE_FIELDS(lhs_type_tree);
371 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
372 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
374 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
377 elt = VEC_quick_push(constructor_elt, init, NULL);
378 field = DECL_CHAIN(field);
379 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
382 if (rhs_type->points_to() != NULL)
384 // We are assigning a pointer to the interface; the interface
385 // holds the pointer itself.
386 elt->value = rhs_tree;
387 return build_constructor(lhs_type_tree, init);
390 // We are assigning a non-pointer value to the interface; the
391 // interface gets a copy of the value in the heap.
393 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
395 tree space = gogo->allocate_memory(rhs_type, object_size, location);
396 space = fold_convert_loc(location.gcc_location(),
397 build_pointer_type(TREE_TYPE(rhs_tree)), space);
398 space = save_expr(space);
400 tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space);
401 TREE_THIS_NOTRAP(ref) = 1;
402 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
403 void_type_node, ref, rhs_tree);
405 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
408 return build2(COMPOUND_EXPR, lhs_type_tree, set,
409 build_constructor(lhs_type_tree, init));
412 // Return a tree for the type descriptor of RHS_TREE, which has
413 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
417 Expression::get_interface_type_descriptor(Translate_context*,
418 Type* rhs_type, tree rhs_tree,
421 tree rhs_type_tree = TREE_TYPE(rhs_tree);
422 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
423 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
424 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
426 if (rhs_type->interface_type()->is_empty())
428 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
429 "__type_descriptor") == 0);
433 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
435 go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
437 tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v);
438 go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
439 tree f = TYPE_FIELDS(TREE_TYPE(v1));
440 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
442 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
444 tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node,
445 v, fold_convert_loc(location.gcc_location(),
448 tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1),
450 return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1),
454 // Return a tree for the conversion of an interface type to an
458 Expression::convert_interface_to_interface(Translate_context* context,
459 Type *lhs_type, Type *rhs_type,
460 tree rhs_tree, bool for_type_guard,
463 Gogo* gogo = context->gogo();
464 Interface_type* lhs_interface_type = lhs_type->interface_type();
465 bool lhs_is_empty = lhs_interface_type->is_empty();
467 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
468 if (lhs_type_tree == error_mark_node)
469 return error_mark_node;
471 // In the general case this requires runtime examination of the type
472 // method table to match it up with the interface methods.
474 // FIXME: If all of the methods in the right hand side interface
475 // also appear in the left hand side interface, then we don't need
476 // to do a runtime check, although we still need to build a new
479 // Get the type descriptor for the right hand side. This will be
480 // NULL for a nil interface.
482 if (!DECL_P(rhs_tree))
483 rhs_tree = save_expr(rhs_tree);
485 tree rhs_type_descriptor =
486 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
489 // The result is going to be a two element constructor.
491 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
493 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
494 tree field = TYPE_FIELDS(lhs_type_tree);
499 // A type assertion fails when converting a nil interface.
500 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
502 static tree assert_interface_decl;
503 tree call = Gogo::call_builtin(&assert_interface_decl,
505 "__go_assert_interface",
508 TREE_TYPE(lhs_type_descriptor),
510 TREE_TYPE(rhs_type_descriptor),
511 rhs_type_descriptor);
512 if (call == error_mark_node)
513 return error_mark_node;
514 // This will panic if the interface conversion fails.
515 TREE_NOTHROW(assert_interface_decl) = 0;
516 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
519 else if (lhs_is_empty)
521 // A convertion to an empty interface always succeeds, and the
522 // first field is just the type descriptor of the object.
523 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
524 "__type_descriptor") == 0);
525 go_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
526 elt->value = rhs_type_descriptor;
530 // A conversion to a non-empty interface may fail, but unlike a
531 // type assertion converting nil will always succeed.
532 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
534 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
536 static tree convert_interface_decl;
537 tree call = Gogo::call_builtin(&convert_interface_decl,
539 "__go_convert_interface",
542 TREE_TYPE(lhs_type_descriptor),
544 TREE_TYPE(rhs_type_descriptor),
545 rhs_type_descriptor);
546 if (call == error_mark_node)
547 return error_mark_node;
548 // This will panic if the interface conversion fails.
549 TREE_NOTHROW(convert_interface_decl) = 0;
550 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
554 // The second field is simply the object pointer.
556 elt = VEC_quick_push(constructor_elt, init, NULL);
557 field = DECL_CHAIN(field);
558 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
561 tree rhs_type_tree = TREE_TYPE(rhs_tree);
562 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
563 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
564 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
565 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
568 return build_constructor(lhs_type_tree, init);
571 // Return a tree for the conversion of an interface type to a
572 // non-interface type.
575 Expression::convert_interface_to_type(Translate_context* context,
576 Type *lhs_type, Type* rhs_type,
577 tree rhs_tree, Location location)
579 Gogo* gogo = context->gogo();
580 tree rhs_type_tree = TREE_TYPE(rhs_tree);
582 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
583 if (lhs_type_tree == error_mark_node)
584 return error_mark_node;
586 // Call a function to check that the type is valid. The function
587 // will panic with an appropriate runtime type error if the type is
590 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location);
592 if (!DECL_P(rhs_tree))
593 rhs_tree = save_expr(rhs_tree);
595 tree rhs_type_descriptor =
596 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
599 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo,
602 static tree check_interface_type_decl;
603 tree call = Gogo::call_builtin(&check_interface_type_decl,
605 "__go_check_interface_type",
608 TREE_TYPE(lhs_type_descriptor),
610 TREE_TYPE(rhs_type_descriptor),
612 TREE_TYPE(rhs_inter_descriptor),
613 rhs_inter_descriptor);
614 if (call == error_mark_node)
615 return error_mark_node;
616 // This call will panic if the conversion is invalid.
617 TREE_NOTHROW(check_interface_type_decl) = 0;
619 // If the call succeeds, pull out the value.
620 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
621 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
622 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
623 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
626 // If the value is a pointer, then it is the value we want.
627 // Otherwise it points to the value.
628 if (lhs_type->points_to() == NULL)
630 val = fold_convert_loc(location.gcc_location(),
631 build_pointer_type(lhs_type_tree), val);
632 val = build_fold_indirect_ref_loc(location.gcc_location(), val);
635 return build2(COMPOUND_EXPR, lhs_type_tree, call,
636 fold_convert_loc(location.gcc_location(), lhs_type_tree, val));
639 // Convert an expression to a tree. This is implemented by the child
640 // class. Not that it is not in general safe to call this multiple
641 // times for a single expression, but that we don't catch such errors.
644 Expression::get_tree(Translate_context* context)
646 // The child may have marked this expression as having an error.
647 if (this->classification_ == EXPRESSION_ERROR)
648 return error_mark_node;
650 return this->do_get_tree(context);
653 // Return a tree for VAL in TYPE.
656 Expression::integer_constant_tree(mpz_t val, tree type)
658 if (type == error_mark_node)
659 return error_mark_node;
660 else if (TREE_CODE(type) == INTEGER_TYPE)
661 return double_int_to_tree(type,
662 mpz_get_double_int(type, val, true));
663 else if (TREE_CODE(type) == REAL_TYPE)
666 mpfr_init_set_z(fval, val, GMP_RNDN);
667 tree ret = Expression::float_constant_tree(fval, type);
671 else if (TREE_CODE(type) == COMPLEX_TYPE)
674 mpfr_init_set_z(fval, val, GMP_RNDN);
675 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
677 tree imag = build_real_from_int_cst(TREE_TYPE(type),
679 return build_complex(type, real, imag);
685 // Return a tree for VAL in TYPE.
688 Expression::float_constant_tree(mpfr_t val, tree type)
690 if (type == error_mark_node)
691 return error_mark_node;
692 else if (TREE_CODE(type) == INTEGER_TYPE)
696 mpfr_get_z(ival, val, GMP_RNDN);
697 tree ret = Expression::integer_constant_tree(ival, type);
701 else if (TREE_CODE(type) == REAL_TYPE)
704 real_from_mpfr(&r1, val, type, GMP_RNDN);
706 real_convert(&r2, TYPE_MODE(type), &r1);
707 return build_real(type, r2);
709 else if (TREE_CODE(type) == COMPLEX_TYPE)
712 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
714 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
715 tree imag = build_real_from_int_cst(TREE_TYPE(type),
717 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
723 // Return a tree for REAL/IMAG in TYPE.
726 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
728 if (type == error_mark_node)
729 return error_mark_node;
730 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
731 return Expression::float_constant_tree(real, type);
732 else if (TREE_CODE(type) == COMPLEX_TYPE)
735 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
737 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
740 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
742 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
744 return build_complex(type, build_real(TREE_TYPE(type), r2),
745 build_real(TREE_TYPE(type), r4));
751 // Return a tree which evaluates to true if VAL, of arbitrary integer
752 // type, is negative or is more than the maximum value of BOUND_TYPE.
753 // If SOFAR is not NULL, it is or'red into the result. The return
754 // value may be NULL if SOFAR is NULL.
757 Expression::check_bounds(tree val, tree bound_type, tree sofar,
760 tree val_type = TREE_TYPE(val);
761 tree ret = NULL_TREE;
763 if (!TYPE_UNSIGNED(val_type))
765 ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val,
766 build_int_cst(val_type, 0));
767 if (ret == boolean_false_node)
771 HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type);
772 HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type);
773 go_assert(val_type_size != -1 && bound_type_size != -1);
774 if (val_type_size > bound_type_size
775 || (val_type_size == bound_type_size
776 && TYPE_UNSIGNED(val_type)
777 && !TYPE_UNSIGNED(bound_type)))
779 tree max = TYPE_MAX_VALUE(bound_type);
780 tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node,
781 val, fold_convert_loc(loc.gcc_location(),
783 if (big == boolean_false_node)
785 else if (ret == NULL_TREE)
788 ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
789 boolean_type_node, ret, big);
792 if (ret == NULL_TREE)
794 else if (sofar == NULL_TREE)
797 return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node,
802 Expression::dump_expression(Ast_dump_context* ast_dump_context) const
804 this->do_dump_expression(ast_dump_context);
807 // Error expressions. This are used to avoid cascading errors.
809 class Error_expression : public Expression
812 Error_expression(Location location)
813 : Expression(EXPRESSION_ERROR, location)
818 do_is_constant() const
822 do_integer_constant_value(bool, mpz_t val, Type**) const
829 do_float_constant_value(mpfr_t val, Type**) const
831 mpfr_set_ui(val, 0, GMP_RNDN);
836 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
838 mpfr_set_ui(real, 0, GMP_RNDN);
839 mpfr_set_ui(imag, 0, GMP_RNDN);
844 do_discarding_value()
849 { return Type::make_error_type(); }
852 do_determine_type(const Type_context*)
860 do_is_addressable() const
864 do_get_tree(Translate_context*)
865 { return error_mark_node; }
868 do_dump_expression(Ast_dump_context*) const;
871 // Dump the ast representation for an error expression to a dump context.
874 Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
876 ast_dump_context->ostream() << "_Error_" ;
880 Expression::make_error(Location location)
882 return new Error_expression(location);
885 // An expression which is really a type. This is used during parsing.
886 // It is an error if these survive after lowering.
889 Type_expression : public Expression
892 Type_expression(Type* type, Location location)
893 : Expression(EXPRESSION_TYPE, location),
899 do_traverse(Traverse* traverse)
900 { return Type::traverse(this->type_, traverse); }
904 { return this->type_; }
907 do_determine_type(const Type_context*)
911 do_check_types(Gogo*)
912 { this->report_error(_("invalid use of type")); }
919 do_get_tree(Translate_context*)
920 { go_unreachable(); }
922 void do_dump_expression(Ast_dump_context*) const;
925 // The type which we are representing as an expression.
930 Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
932 ast_dump_context->dump_type(this->type_);
936 Expression::make_type(Type* type, Location location)
938 return new Type_expression(type, location);
941 // Class Parser_expression.
944 Parser_expression::do_type()
946 // We should never really ask for the type of a Parser_expression.
947 // However, it can happen, at least when we have an invalid const
948 // whose initializer refers to the const itself. In that case we
949 // may ask for the type when lowering the const itself.
950 go_assert(saw_errors());
951 return Type::make_error_type();
954 // Class Var_expression.
956 // Lower a variable expression. Here we just make sure that the
957 // initialization expression of the variable has been lowered. This
958 // ensures that we will be able to determine the type of the variable
962 Var_expression::do_lower(Gogo* gogo, Named_object* function,
963 Statement_inserter* inserter, int)
965 if (this->variable_->is_variable())
967 Variable* var = this->variable_->var_value();
968 // This is either a local variable or a global variable. A
969 // reference to a variable which is local to an enclosing
970 // function will be a reference to a field in a closure.
971 if (var->is_global())
976 var->lower_init_expression(gogo, function, inserter);
981 // Return the type of a reference to a variable.
984 Var_expression::do_type()
986 if (this->variable_->is_variable())
987 return this->variable_->var_value()->type();
988 else if (this->variable_->is_result_variable())
989 return this->variable_->result_var_value()->type();
994 // Determine the type of a reference to a variable.
997 Var_expression::do_determine_type(const Type_context*)
999 if (this->variable_->is_variable())
1000 this->variable_->var_value()->determine_type();
1003 // Something takes the address of this variable. This means that we
1004 // may want to move the variable onto the heap.
1007 Var_expression::do_address_taken(bool escapes)
1011 if (this->variable_->is_variable())
1012 this->variable_->var_value()->set_non_escaping_address_taken();
1013 else if (this->variable_->is_result_variable())
1014 this->variable_->result_var_value()->set_non_escaping_address_taken();
1020 if (this->variable_->is_variable())
1021 this->variable_->var_value()->set_address_taken();
1022 else if (this->variable_->is_result_variable())
1023 this->variable_->result_var_value()->set_address_taken();
1029 // Get the tree for a reference to a variable.
1032 Var_expression::do_get_tree(Translate_context* context)
1034 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
1035 context->function());
1036 tree ret = var_to_tree(bvar);
1037 if (ret == error_mark_node)
1038 return error_mark_node;
1040 if (this->variable_->is_variable())
1041 is_in_heap = this->variable_->var_value()->is_in_heap();
1042 else if (this->variable_->is_result_variable())
1043 is_in_heap = this->variable_->result_var_value()->is_in_heap();
1048 ret = build_fold_indirect_ref_loc(this->location().gcc_location(), ret);
1049 TREE_THIS_NOTRAP(ret) = 1;
1054 // Ast dump for variable expression.
1057 Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1059 ast_dump_context->ostream() << this->variable_->name() ;
1062 // Make a reference to a variable in an expression.
1065 Expression::make_var_reference(Named_object* var, Location location)
1068 return Expression::make_sink(location);
1070 // FIXME: Creating a new object for each reference to a variable is
1072 return new Var_expression(var, location);
1075 // Class Temporary_reference_expression.
1080 Temporary_reference_expression::do_type()
1082 return this->statement_->type();
1085 // Called if something takes the address of this temporary variable.
1086 // We never have to move temporary variables to the heap, but we do
1087 // need to know that they must live in the stack rather than in a
1091 Temporary_reference_expression::do_address_taken(bool)
1093 this->statement_->set_is_address_taken();
1096 // Get a tree referring to the variable.
1099 Temporary_reference_expression::do_get_tree(Translate_context* context)
1101 Bvariable* bvar = this->statement_->get_backend_variable(context);
1103 // The gcc backend can't represent the same set of recursive types
1104 // that the Go frontend can. In some cases this means that a
1105 // temporary variable won't have the right backend type. Correct
1106 // that here by adding a type cast. We need to use base() to push
1107 // the circularity down one level.
1108 tree ret = var_to_tree(bvar);
1109 if (!this->is_lvalue_
1110 && POINTER_TYPE_P(TREE_TYPE(ret))
1111 && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
1113 Btype* type_btype = this->type()->base()->get_backend(context->gogo());
1114 tree type_tree = type_to_tree(type_btype);
1115 ret = fold_convert_loc(this->location().gcc_location(), type_tree, ret);
1120 // Ast dump for temporary reference.
1123 Temporary_reference_expression::do_dump_expression(
1124 Ast_dump_context* ast_dump_context) const
1126 ast_dump_context->dump_temp_variable_name(this->statement_);
1129 // Make a reference to a temporary variable.
1131 Temporary_reference_expression*
1132 Expression::make_temporary_reference(Temporary_statement* statement,
1135 return new Temporary_reference_expression(statement, location);
1138 // A sink expression--a use of the blank identifier _.
1140 class Sink_expression : public Expression
1143 Sink_expression(Location location)
1144 : Expression(EXPRESSION_SINK, location),
1145 type_(NULL), var_(NULL_TREE)
1150 do_discarding_value()
1157 do_determine_type(const Type_context*);
1161 { return new Sink_expression(this->location()); }
1164 do_get_tree(Translate_context*);
1167 do_dump_expression(Ast_dump_context*) const;
1170 // The type of this sink variable.
1172 // The temporary variable we generate.
1176 // Return the type of a sink expression.
1179 Sink_expression::do_type()
1181 if (this->type_ == NULL)
1182 return Type::make_sink_type();
1186 // Determine the type of a sink expression.
1189 Sink_expression::do_determine_type(const Type_context* context)
1191 if (context->type != NULL)
1192 this->type_ = context->type;
1195 // Return a temporary variable for a sink expression. This will
1196 // presumably be a write-only variable which the middle-end will drop.
1199 Sink_expression::do_get_tree(Translate_context* context)
1201 if (this->var_ == NULL_TREE)
1203 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
1204 Btype* bt = this->type_->get_backend(context->gogo());
1205 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
1210 // Ast dump for sink expression.
1213 Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1215 ast_dump_context->ostream() << "_" ;
1218 // Make a sink expression.
1221 Expression::make_sink(Location location)
1223 return new Sink_expression(location);
1226 // Class Func_expression.
1228 // FIXME: Can a function expression appear in a constant expression?
1229 // The value is unchanging. Initializing a constant to the address of
1230 // a function seems like it could work, though there might be little
1236 Func_expression::do_traverse(Traverse* traverse)
1238 return (this->closure_ == NULL
1240 : Expression::traverse(&this->closure_, traverse));
1243 // Return the type of a function expression.
1246 Func_expression::do_type()
1248 if (this->function_->is_function())
1249 return this->function_->func_value()->type();
1250 else if (this->function_->is_function_declaration())
1251 return this->function_->func_declaration_value()->type();
1256 // Get the tree for a function expression without evaluating the
1260 Func_expression::get_tree_without_closure(Gogo* gogo)
1262 Function_type* fntype;
1263 if (this->function_->is_function())
1264 fntype = this->function_->func_value()->type();
1265 else if (this->function_->is_function_declaration())
1266 fntype = this->function_->func_declaration_value()->type();
1270 // Builtin functions are handled specially by Call_expression. We
1271 // can't take their address.
1272 if (fntype->is_builtin())
1274 error_at(this->location(), "invalid use of special builtin function %qs",
1275 this->function_->name().c_str());
1276 return error_mark_node;
1279 Named_object* no = this->function_;
1281 tree id = no->get_id(gogo);
1282 if (id == error_mark_node)
1283 return error_mark_node;
1286 if (no->is_function())
1287 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1288 else if (no->is_function_declaration())
1289 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1293 if (fndecl == error_mark_node)
1294 return error_mark_node;
1296 return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl);
1299 // Get the tree for a function expression. This is used when we take
1300 // the address of a function rather than simply calling it. If the
1301 // function has a closure, we must use a trampoline.
1304 Func_expression::do_get_tree(Translate_context* context)
1306 Gogo* gogo = context->gogo();
1308 tree fnaddr = this->get_tree_without_closure(gogo);
1309 if (fnaddr == error_mark_node)
1310 return error_mark_node;
1312 go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1313 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1314 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1316 // For a normal non-nested function call, that is all we have to do.
1317 if (!this->function_->is_function()
1318 || this->function_->func_value()->enclosing() == NULL)
1320 go_assert(this->closure_ == NULL);
1324 // For a nested function call, we have to always allocate a
1325 // trampoline. If we don't always allocate, then closures will not
1326 // be reliably distinct.
1327 Expression* closure = this->closure_;
1329 if (closure == NULL)
1330 closure_tree = null_pointer_node;
1333 // Get the value of the closure. This will be a pointer to
1334 // space allocated on the heap.
1335 closure_tree = closure->get_tree(context);
1336 if (closure_tree == error_mark_node)
1337 return error_mark_node;
1338 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1341 // Now we need to build some code on the heap. This code will load
1342 // the static chain pointer with the closure and then jump to the
1343 // body of the function. The normal gcc approach is to build the
1344 // code on the stack. Unfortunately we can not do that, as Go
1345 // permits us to return the function pointer.
1347 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1350 // Ast dump for function.
1353 Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1355 ast_dump_context->ostream() << this->function_->name();
1356 if (this->closure_ != NULL)
1358 ast_dump_context->ostream() << " {closure = ";
1359 this->closure_->dump_expression(ast_dump_context);
1360 ast_dump_context->ostream() << "}";
1364 // Make a reference to a function in an expression.
1367 Expression::make_func_reference(Named_object* function, Expression* closure,
1370 return new Func_expression(function, closure, location);
1373 // Class Unknown_expression.
1375 // Return the name of an unknown expression.
1378 Unknown_expression::name() const
1380 return this->named_object_->name();
1383 // Lower a reference to an unknown name.
1386 Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1388 Location location = this->location();
1389 Named_object* no = this->named_object_;
1391 if (!no->is_unknown())
1395 real = no->unknown_value()->real_named_object();
1398 if (this->is_composite_literal_key_)
1400 error_at(location, "reference to undefined name %qs",
1401 this->named_object_->message_name().c_str());
1402 return Expression::make_error(location);
1405 switch (real->classification())
1407 case Named_object::NAMED_OBJECT_CONST:
1408 return Expression::make_const_reference(real, location);
1409 case Named_object::NAMED_OBJECT_TYPE:
1410 return Expression::make_type(real->type_value(), location);
1411 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1412 if (this->is_composite_literal_key_)
1414 error_at(location, "reference to undefined type %qs",
1415 real->message_name().c_str());
1416 return Expression::make_error(location);
1417 case Named_object::NAMED_OBJECT_VAR:
1418 return Expression::make_var_reference(real, location);
1419 case Named_object::NAMED_OBJECT_FUNC:
1420 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1421 return Expression::make_func_reference(real, NULL, location);
1422 case Named_object::NAMED_OBJECT_PACKAGE:
1423 if (this->is_composite_literal_key_)
1425 error_at(location, "unexpected reference to package");
1426 return Expression::make_error(location);
1432 // Dump the ast representation for an unknown expression to a dump context.
1435 Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1437 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1441 // Make a reference to an unknown name.
1444 Expression::make_unknown_reference(Named_object* no, Location location)
1446 return new Unknown_expression(no, location);
1449 // A boolean expression.
1451 class Boolean_expression : public Expression
1454 Boolean_expression(bool val, Location location)
1455 : Expression(EXPRESSION_BOOLEAN, location),
1456 val_(val), type_(NULL)
1464 do_is_constant() const
1471 do_determine_type(const Type_context*);
1478 do_get_tree(Translate_context*)
1479 { return this->val_ ? boolean_true_node : boolean_false_node; }
1482 do_export(Export* exp) const
1483 { exp->write_c_string(this->val_ ? "true" : "false"); }
1486 do_dump_expression(Ast_dump_context* ast_dump_context) const
1487 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1492 // The type as determined by context.
1499 Boolean_expression::do_type()
1501 if (this->type_ == NULL)
1502 this->type_ = Type::make_boolean_type();
1506 // Set the type from the context.
1509 Boolean_expression::do_determine_type(const Type_context* context)
1511 if (this->type_ != NULL && !this->type_->is_abstract())
1513 else if (context->type != NULL && context->type->is_boolean_type())
1514 this->type_ = context->type;
1515 else if (!context->may_be_abstract)
1516 this->type_ = Type::lookup_bool_type();
1519 // Import a boolean constant.
1522 Boolean_expression::do_import(Import* imp)
1524 if (imp->peek_char() == 't')
1526 imp->require_c_string("true");
1527 return Expression::make_boolean(true, imp->location());
1531 imp->require_c_string("false");
1532 return Expression::make_boolean(false, imp->location());
1536 // Make a boolean expression.
1539 Expression::make_boolean(bool val, Location location)
1541 return new Boolean_expression(val, location);
1544 // Class String_expression.
1549 String_expression::do_type()
1551 if (this->type_ == NULL)
1552 this->type_ = Type::make_string_type();
1556 // Set the type from the context.
1559 String_expression::do_determine_type(const Type_context* context)
1561 if (this->type_ != NULL && !this->type_->is_abstract())
1563 else if (context->type != NULL && context->type->is_string_type())
1564 this->type_ = context->type;
1565 else if (!context->may_be_abstract)
1566 this->type_ = Type::lookup_string_type();
1569 // Build a string constant.
1572 String_expression::do_get_tree(Translate_context* context)
1574 return context->gogo()->go_string_constant_tree(this->val_);
1577 // Write string literal to string dump.
1580 String_expression::export_string(String_dump* exp,
1581 const String_expression* str)
1584 s.reserve(str->val_.length() * 4 + 2);
1586 for (std::string::const_iterator p = str->val_.begin();
1587 p != str->val_.end();
1590 if (*p == '\\' || *p == '"')
1595 else if (*p >= 0x20 && *p < 0x7f)
1597 else if (*p == '\n')
1599 else if (*p == '\t')
1604 unsigned char c = *p;
1605 unsigned int dig = c >> 4;
1606 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1608 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1612 exp->write_string(s);
1615 // Export a string expression.
1618 String_expression::do_export(Export* exp) const
1620 String_expression::export_string(exp, this);
1623 // Import a string expression.
1626 String_expression::do_import(Import* imp)
1628 imp->require_c_string("\"");
1632 int c = imp->get_char();
1633 if (c == '"' || c == -1)
1636 val += static_cast<char>(c);
1639 c = imp->get_char();
1640 if (c == '\\' || c == '"')
1641 val += static_cast<char>(c);
1648 c = imp->get_char();
1649 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1650 c = imp->get_char();
1651 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1652 char v = (vh << 4) | vl;
1657 error_at(imp->location(), "bad string constant");
1658 return Expression::make_error(imp->location());
1662 return Expression::make_string(val, imp->location());
1665 // Ast dump for string expression.
1668 String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1670 String_expression::export_string(ast_dump_context, this);
1673 // Make a string expression.
1676 Expression::make_string(const std::string& val, Location location)
1678 return new String_expression(val, location);
1681 // Make an integer expression.
1683 class Integer_expression : public Expression
1686 Integer_expression(const mpz_t* val, Type* type, Location location)
1687 : Expression(EXPRESSION_INTEGER, location),
1689 { mpz_init_set(this->val_, *val); }
1694 // Return whether VAL fits in the type.
1696 check_constant(mpz_t val, Type*, Location);
1698 // Write VAL to string dump.
1700 export_integer(String_dump* exp, const mpz_t val);
1702 // Write VAL to dump context.
1704 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1708 do_is_constant() const
1712 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1718 do_determine_type(const Type_context* context);
1721 do_check_types(Gogo*);
1724 do_get_tree(Translate_context*);
1728 { return Expression::make_integer(&this->val_, this->type_,
1729 this->location()); }
1732 do_export(Export*) const;
1735 do_dump_expression(Ast_dump_context*) const;
1738 // The integer value.
1744 // Return an integer constant value.
1747 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1750 if (this->type_ != NULL)
1751 *ptype = this->type_;
1752 mpz_set(val, this->val_);
1756 // Return the current type. If we haven't set the type yet, we return
1757 // an abstract integer type.
1760 Integer_expression::do_type()
1762 if (this->type_ == NULL)
1763 this->type_ = Type::make_abstract_integer_type();
1767 // Set the type of the integer value. Here we may switch from an
1768 // abstract type to a real type.
1771 Integer_expression::do_determine_type(const Type_context* context)
1773 if (this->type_ != NULL && !this->type_->is_abstract())
1775 else if (context->type != NULL
1776 && (context->type->integer_type() != NULL
1777 || context->type->float_type() != NULL
1778 || context->type->complex_type() != NULL))
1779 this->type_ = context->type;
1780 else if (!context->may_be_abstract)
1781 this->type_ = Type::lookup_integer_type("int");
1784 // Return true if the integer VAL fits in the range of the type TYPE.
1785 // Otherwise give an error and return false. TYPE may be NULL.
1788 Integer_expression::check_constant(mpz_t val, Type* type,
1793 Integer_type* itype = type->integer_type();
1794 if (itype == NULL || itype->is_abstract())
1797 int bits = mpz_sizeinbase(val, 2);
1799 if (itype->is_unsigned())
1801 // For an unsigned type we can only accept a nonnegative number,
1802 // and we must be able to represent at least BITS.
1803 if (mpz_sgn(val) >= 0
1804 && bits <= itype->bits())
1809 // For a signed type we need an extra bit to indicate the sign.
1810 // We have to handle the most negative integer specially.
1811 if (bits + 1 <= itype->bits()
1812 || (bits <= itype->bits()
1814 && (mpz_scan1(val, 0)
1815 == static_cast<unsigned long>(itype->bits() - 1))
1816 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1820 error_at(location, "integer constant overflow");
1824 // Check the type of an integer constant.
1827 Integer_expression::do_check_types(Gogo*)
1829 if (this->type_ == NULL)
1831 if (!Integer_expression::check_constant(this->val_, this->type_,
1833 this->set_is_error();
1836 // Get a tree for an integer constant.
1839 Integer_expression::do_get_tree(Translate_context* context)
1841 Gogo* gogo = context->gogo();
1843 if (this->type_ != NULL && !this->type_->is_abstract())
1844 type = type_to_tree(this->type_->get_backend(gogo));
1845 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1847 // We are converting to an abstract floating point type.
1848 Type* ftype = Type::lookup_float_type("float64");
1849 type = type_to_tree(ftype->get_backend(gogo));
1851 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1853 // We are converting to an abstract complex type.
1854 Type* ctype = Type::lookup_complex_type("complex128");
1855 type = type_to_tree(ctype->get_backend(gogo));
1859 // If we still have an abstract type here, then this is being
1860 // used in a constant expression which didn't get reduced for
1861 // some reason. Use a type which will fit the value. We use <,
1862 // not <=, because we need an extra bit for the sign bit.
1863 int bits = mpz_sizeinbase(this->val_, 2);
1864 if (bits < INT_TYPE_SIZE)
1866 Type* t = Type::lookup_integer_type("int");
1867 type = type_to_tree(t->get_backend(gogo));
1871 Type* t = Type::lookup_integer_type("int64");
1872 type = type_to_tree(t->get_backend(gogo));
1875 type = long_long_integer_type_node;
1877 return Expression::integer_constant_tree(this->val_, type);
1880 // Write VAL to export data.
1883 Integer_expression::export_integer(String_dump* exp, const mpz_t val)
1885 char* s = mpz_get_str(NULL, 10, val);
1886 exp->write_c_string(s);
1890 // Export an integer in a constant expression.
1893 Integer_expression::do_export(Export* exp) const
1895 Integer_expression::export_integer(exp, this->val_);
1896 // A trailing space lets us reliably identify the end of the number.
1897 exp->write_c_string(" ");
1900 // Import an integer, floating point, or complex value. This handles
1901 // all these types because they all start with digits.
1904 Integer_expression::do_import(Import* imp)
1906 std::string num = imp->read_identifier();
1907 imp->require_c_string(" ");
1908 if (!num.empty() && num[num.length() - 1] == 'i')
1911 size_t plus_pos = num.find('+', 1);
1912 size_t minus_pos = num.find('-', 1);
1914 if (plus_pos == std::string::npos)
1916 else if (minus_pos == std::string::npos)
1920 error_at(imp->location(), "bad number in import data: %qs",
1922 return Expression::make_error(imp->location());
1924 if (pos == std::string::npos)
1925 mpfr_set_ui(real, 0, GMP_RNDN);
1928 std::string real_str = num.substr(0, pos);
1929 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1931 error_at(imp->location(), "bad number in import data: %qs",
1933 return Expression::make_error(imp->location());
1937 std::string imag_str;
1938 if (pos == std::string::npos)
1941 imag_str = num.substr(pos);
1942 imag_str = imag_str.substr(0, imag_str.size() - 1);
1944 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1946 error_at(imp->location(), "bad number in import data: %qs",
1948 return Expression::make_error(imp->location());
1950 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1956 else if (num.find('.') == std::string::npos
1957 && num.find('E') == std::string::npos)
1960 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1962 error_at(imp->location(), "bad number in import data: %qs",
1964 return Expression::make_error(imp->location());
1966 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1973 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1975 error_at(imp->location(), "bad number in import data: %qs",
1977 return Expression::make_error(imp->location());
1979 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1984 // Ast dump for integer expression.
1987 Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1989 Integer_expression::export_integer(ast_dump_context, this->val_);
1992 // Build a new integer value.
1995 Expression::make_integer(const mpz_t* val, Type* type,
1998 return new Integer_expression(val, type, location);
2003 class Float_expression : public Expression
2006 Float_expression(const mpfr_t* val, Type* type, Location location)
2007 : Expression(EXPRESSION_FLOAT, location),
2010 mpfr_init_set(this->val_, *val, GMP_RNDN);
2013 // Constrain VAL to fit into TYPE.
2015 constrain_float(mpfr_t val, Type* type);
2017 // Return whether VAL fits in the type.
2019 check_constant(mpfr_t val, Type*, Location);
2021 // Write VAL to export data.
2023 export_float(String_dump* exp, const mpfr_t val);
2025 // Write VAL to dump file.
2027 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
2031 do_is_constant() const
2035 do_float_constant_value(mpfr_t val, Type**) const;
2041 do_determine_type(const Type_context*);
2044 do_check_types(Gogo*);
2048 { return Expression::make_float(&this->val_, this->type_,
2049 this->location()); }
2052 do_get_tree(Translate_context*);
2055 do_export(Export*) const;
2058 do_dump_expression(Ast_dump_context*) const;
2061 // The floating point value.
2067 // Constrain VAL to fit into TYPE.
2070 Float_expression::constrain_float(mpfr_t val, Type* type)
2072 Float_type* ftype = type->float_type();
2073 if (ftype != NULL && !ftype->is_abstract())
2074 mpfr_prec_round(val, ftype->bits(), GMP_RNDN);
2077 // Return a floating point constant value.
2080 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2082 if (this->type_ != NULL)
2083 *ptype = this->type_;
2084 mpfr_set(val, this->val_, GMP_RNDN);
2088 // Return the current type. If we haven't set the type yet, we return
2089 // an abstract float type.
2092 Float_expression::do_type()
2094 if (this->type_ == NULL)
2095 this->type_ = Type::make_abstract_float_type();
2099 // Set the type of the float value. Here we may switch from an
2100 // abstract type to a real type.
2103 Float_expression::do_determine_type(const Type_context* context)
2105 if (this->type_ != NULL && !this->type_->is_abstract())
2107 else if (context->type != NULL
2108 && (context->type->integer_type() != NULL
2109 || context->type->float_type() != NULL
2110 || context->type->complex_type() != NULL))
2111 this->type_ = context->type;
2112 else if (!context->may_be_abstract)
2113 this->type_ = Type::lookup_float_type("float64");
2116 // Return true if the floating point value VAL fits in the range of
2117 // the type TYPE. Otherwise give an error and return false. TYPE may
2121 Float_expression::check_constant(mpfr_t val, Type* type,
2126 Float_type* ftype = type->float_type();
2127 if (ftype == NULL || ftype->is_abstract())
2130 // A NaN or Infinity always fits in the range of the type.
2131 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
2134 mp_exp_t exp = mpfr_get_exp(val);
2136 switch (ftype->bits())
2149 error_at(location, "floating point constant overflow");
2155 // Check the type of a float value.
2158 Float_expression::do_check_types(Gogo*)
2160 if (this->type_ == NULL)
2163 if (!Float_expression::check_constant(this->val_, this->type_,
2165 this->set_is_error();
2167 Integer_type* integer_type = this->type_->integer_type();
2168 if (integer_type != NULL)
2170 if (!mpfr_integer_p(this->val_))
2171 this->report_error(_("floating point constant truncated to integer"));
2174 go_assert(!integer_type->is_abstract());
2177 mpfr_get_z(ival, this->val_, GMP_RNDN);
2178 Integer_expression::check_constant(ival, integer_type,
2185 // Get a tree for a float constant.
2188 Float_expression::do_get_tree(Translate_context* context)
2190 Gogo* gogo = context->gogo();
2192 if (this->type_ != NULL && !this->type_->is_abstract())
2193 type = type_to_tree(this->type_->get_backend(gogo));
2194 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2196 // We have an abstract integer type. We just hope for the best.
2197 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
2201 // If we still have an abstract type here, then this is being
2202 // used in a constant expression which didn't get reduced. We
2203 // just use float64 and hope for the best.
2204 Type* ft = Type::lookup_float_type("float64");
2205 type = type_to_tree(ft->get_backend(gogo));
2207 return Expression::float_constant_tree(this->val_, type);
2210 // Write a floating point number to a string dump.
2213 Float_expression::export_float(String_dump *exp, const mpfr_t val)
2216 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2218 exp->write_c_string("-");
2219 exp->write_c_string("0.");
2220 exp->write_c_string(*s == '-' ? s + 1 : s);
2223 snprintf(buf, sizeof buf, "E%ld", exponent);
2224 exp->write_c_string(buf);
2227 // Export a floating point number in a constant expression.
2230 Float_expression::do_export(Export* exp) const
2232 Float_expression::export_float(exp, this->val_);
2233 // A trailing space lets us reliably identify the end of the number.
2234 exp->write_c_string(" ");
2237 // Dump a floating point number to the dump file.
2240 Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2242 Float_expression::export_float(ast_dump_context, this->val_);
2245 // Make a float expression.
2248 Expression::make_float(const mpfr_t* val, Type* type, Location location)
2250 return new Float_expression(val, type, location);
2255 class Complex_expression : public Expression
2258 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2260 : Expression(EXPRESSION_COMPLEX, location),
2263 mpfr_init_set(this->real_, *real, GMP_RNDN);
2264 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2267 // Constrain REAL/IMAG to fit into TYPE.
2269 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2271 // Return whether REAL/IMAG fits in the type.
2273 check_constant(mpfr_t real, mpfr_t imag, Type*, Location);
2275 // Write REAL/IMAG to string dump.
2277 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
2279 // Write REAL/IMAG to dump context.
2281 dump_complex(Ast_dump_context* ast_dump_context,
2282 const mpfr_t real, const mpfr_t val);
2286 do_is_constant() const
2290 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2296 do_determine_type(const Type_context*);
2299 do_check_types(Gogo*);
2304 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2309 do_get_tree(Translate_context*);
2312 do_export(Export*) const;
2315 do_dump_expression(Ast_dump_context*) const;
2320 // The imaginary part;
2322 // The type if known.
2326 // Constrain REAL/IMAG to fit into TYPE.
2329 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2331 Complex_type* ctype = type->complex_type();
2332 if (ctype != NULL && !ctype->is_abstract())
2334 mpfr_prec_round(real, ctype->bits() / 2, GMP_RNDN);
2335 mpfr_prec_round(imag, ctype->bits() / 2, GMP_RNDN);
2339 // Return a complex constant value.
2342 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2345 if (this->type_ != NULL)
2346 *ptype = this->type_;
2347 mpfr_set(real, this->real_, GMP_RNDN);
2348 mpfr_set(imag, this->imag_, GMP_RNDN);
2352 // Return the current type. If we haven't set the type yet, we return
2353 // an abstract complex type.
2356 Complex_expression::do_type()
2358 if (this->type_ == NULL)
2359 this->type_ = Type::make_abstract_complex_type();
2363 // Set the type of the complex value. Here we may switch from an
2364 // abstract type to a real type.
2367 Complex_expression::do_determine_type(const Type_context* context)
2369 if (this->type_ != NULL && !this->type_->is_abstract())
2371 else if (context->type != NULL
2372 && context->type->complex_type() != NULL)
2373 this->type_ = context->type;
2374 else if (!context->may_be_abstract)
2375 this->type_ = Type::lookup_complex_type("complex128");
2378 // Return true if the complex value REAL/IMAG fits in the range of the
2379 // type TYPE. Otherwise give an error and return false. TYPE may be
2383 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2388 Complex_type* ctype = type->complex_type();
2389 if (ctype == NULL || ctype->is_abstract())
2393 switch (ctype->bits())
2405 // A NaN or Infinity always fits in the range of the type.
2406 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2408 if (mpfr_get_exp(real) > max_exp)
2410 error_at(location, "complex real part constant overflow");
2415 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2417 if (mpfr_get_exp(imag) > max_exp)
2419 error_at(location, "complex imaginary part constant overflow");
2427 // Check the type of a complex value.
2430 Complex_expression::do_check_types(Gogo*)
2432 if (this->type_ == NULL)
2435 if (!Complex_expression::check_constant(this->real_, this->imag_,
2436 this->type_, this->location()))
2437 this->set_is_error();
2440 // Get a tree for a complex constant.
2443 Complex_expression::do_get_tree(Translate_context* context)
2445 Gogo* gogo = context->gogo();
2447 if (this->type_ != NULL && !this->type_->is_abstract())
2448 type = type_to_tree(this->type_->get_backend(gogo));
2451 // If we still have an abstract type here, this this is being
2452 // used in a constant expression which didn't get reduced. We
2453 // just use complex128 and hope for the best.
2454 Type* ct = Type::lookup_complex_type("complex128");
2455 type = type_to_tree(ct->get_backend(gogo));
2457 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2460 // Write REAL/IMAG to export data.
2463 Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
2466 if (!mpfr_zero_p(real))
2468 Float_expression::export_float(exp, real);
2469 if (mpfr_sgn(imag) > 0)
2470 exp->write_c_string("+");
2472 Float_expression::export_float(exp, imag);
2473 exp->write_c_string("i");
2476 // Export a complex number in a constant expression.
2479 Complex_expression::do_export(Export* exp) const
2481 Complex_expression::export_complex(exp, this->real_, this->imag_);
2482 // A trailing space lets us reliably identify the end of the number.
2483 exp->write_c_string(" ");
2486 // Dump a complex expression to the dump file.
2489 Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2491 Complex_expression::export_complex(ast_dump_context,
2496 // Make a complex expression.
2499 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2502 return new Complex_expression(real, imag, type, location);
2505 // Find a named object in an expression.
2507 class Find_named_object : public Traverse
2510 Find_named_object(Named_object* no)
2511 : Traverse(traverse_expressions),
2512 no_(no), found_(false)
2515 // Whether we found the object.
2518 { return this->found_; }
2522 expression(Expression**);
2525 // The object we are looking for.
2527 // Whether we found it.
2531 // A reference to a const in an expression.
2533 class Const_expression : public Expression
2536 Const_expression(Named_object* constant, Location location)
2537 : Expression(EXPRESSION_CONST_REFERENCE, location),
2538 constant_(constant), type_(NULL), seen_(false)
2543 { return this->constant_; }
2545 // Check that the initializer does not refer to the constant itself.
2547 check_for_init_loop();
2551 do_traverse(Traverse*);
2554 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2557 do_is_constant() const
2561 do_integer_constant_value(bool, mpz_t val, Type**) const;
2564 do_float_constant_value(mpfr_t val, Type**) const;
2567 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2570 do_string_constant_value(std::string* val) const
2571 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2576 // The type of a const is set by the declaration, not the use.
2578 do_determine_type(const Type_context*);
2581 do_check_types(Gogo*);
2588 do_get_tree(Translate_context* context);
2590 // When exporting a reference to a const as part of a const
2591 // expression, we export the value. We ignore the fact that it has
2594 do_export(Export* exp) const
2595 { this->constant_->const_value()->expr()->export_expression(exp); }
2598 do_dump_expression(Ast_dump_context*) const;
2602 Named_object* constant_;
2603 // The type of this reference. This is used if the constant has an
2606 // Used to prevent infinite recursion when a constant incorrectly
2607 // refers to itself.
2614 Const_expression::do_traverse(Traverse* traverse)
2616 if (this->type_ != NULL)
2617 return Type::traverse(this->type_, traverse);
2618 return TRAVERSE_CONTINUE;
2621 // Lower a constant expression. This is where we convert the
2622 // predeclared constant iota into an integer value.
2625 Const_expression::do_lower(Gogo* gogo, Named_object*,
2626 Statement_inserter*, int iota_value)
2628 if (this->constant_->const_value()->expr()->classification()
2631 if (iota_value == -1)
2633 error_at(this->location(),
2634 "iota is only defined in const declarations");
2638 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2639 Expression* ret = Expression::make_integer(&val, NULL,
2645 // Make sure that the constant itself has been lowered.
2646 gogo->lower_constant(this->constant_);
2651 // Return an integer constant value.
2654 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2661 if (this->type_ != NULL)
2662 ctype = this->type_;
2664 ctype = this->constant_->const_value()->type();
2665 if (ctype != NULL && ctype->integer_type() == NULL)
2668 Expression* e = this->constant_->const_value()->expr();
2673 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2675 this->seen_ = false;
2679 && !Integer_expression::check_constant(val, ctype, this->location()))
2682 *ptype = ctype != NULL ? ctype : t;
2686 // Return a floating point constant value.
2689 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2695 if (this->type_ != NULL)
2696 ctype = this->type_;
2698 ctype = this->constant_->const_value()->type();
2699 if (ctype != NULL && ctype->float_type() == NULL)
2705 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2708 this->seen_ = false;
2710 if (r && ctype != NULL)
2712 if (!Float_expression::check_constant(val, ctype, this->location()))
2714 Float_expression::constrain_float(val, ctype);
2716 *ptype = ctype != NULL ? ctype : t;
2720 // Return a complex constant value.
2723 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2730 if (this->type_ != NULL)
2731 ctype = this->type_;
2733 ctype = this->constant_->const_value()->type();
2734 if (ctype != NULL && ctype->complex_type() == NULL)
2740 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2744 this->seen_ = false;
2746 if (r && ctype != NULL)
2748 if (!Complex_expression::check_constant(real, imag, ctype,
2751 Complex_expression::constrain_complex(real, imag, ctype);
2753 *ptype = ctype != NULL ? ctype : t;
2757 // Return the type of the const reference.
2760 Const_expression::do_type()
2762 if (this->type_ != NULL)
2765 Named_constant* nc = this->constant_->const_value();
2767 if (this->seen_ || nc->lowering())
2769 this->report_error(_("constant refers to itself"));
2770 this->type_ = Type::make_error_type();
2776 Type* ret = nc->type();
2780 this->seen_ = false;
2784 // During parsing, a named constant may have a NULL type, but we
2785 // must not return a NULL type here.
2786 ret = nc->expr()->type();
2788 this->seen_ = false;
2793 // Set the type of the const reference.
2796 Const_expression::do_determine_type(const Type_context* context)
2798 Type* ctype = this->constant_->const_value()->type();
2799 Type* cetype = (ctype != NULL
2801 : this->constant_->const_value()->expr()->type());
2802 if (ctype != NULL && !ctype->is_abstract())
2804 else if (context->type != NULL
2805 && (context->type->integer_type() != NULL
2806 || context->type->float_type() != NULL
2807 || context->type->complex_type() != NULL)
2808 && (cetype->integer_type() != NULL
2809 || cetype->float_type() != NULL
2810 || cetype->complex_type() != NULL))
2811 this->type_ = context->type;
2812 else if (context->type != NULL
2813 && context->type->is_string_type()
2814 && cetype->is_string_type())
2815 this->type_ = context->type;
2816 else if (context->type != NULL
2817 && context->type->is_boolean_type()
2818 && cetype->is_boolean_type())
2819 this->type_ = context->type;
2820 else if (!context->may_be_abstract)
2822 if (cetype->is_abstract())
2823 cetype = cetype->make_non_abstract_type();
2824 this->type_ = cetype;
2828 // Check for a loop in which the initializer of a constant refers to
2829 // the constant itself.
2832 Const_expression::check_for_init_loop()
2834 if (this->type_ != NULL && this->type_->is_error())
2839 this->report_error(_("constant refers to itself"));
2840 this->type_ = Type::make_error_type();
2844 Expression* init = this->constant_->const_value()->expr();
2845 Find_named_object find_named_object(this->constant_);
2848 Expression::traverse(&init, &find_named_object);
2849 this->seen_ = false;
2851 if (find_named_object.found())
2853 if (this->type_ == NULL || !this->type_->is_error())
2855 this->report_error(_("constant refers to itself"));
2856 this->type_ = Type::make_error_type();
2862 // Check types of a const reference.
2865 Const_expression::do_check_types(Gogo*)
2867 if (this->type_ != NULL && this->type_->is_error())
2870 this->check_for_init_loop();
2872 if (this->type_ == NULL || this->type_->is_abstract())
2875 // Check for integer overflow.
2876 if (this->type_->integer_type() != NULL)
2881 if (!this->integer_constant_value(true, ival, &dummy))
2885 Expression* cexpr = this->constant_->const_value()->expr();
2886 if (cexpr->float_constant_value(fval, &dummy))
2888 if (!mpfr_integer_p(fval))
2889 this->report_error(_("floating point constant "
2890 "truncated to integer"));
2893 mpfr_get_z(ival, fval, GMP_RNDN);
2894 Integer_expression::check_constant(ival, this->type_,
2904 // Return a tree for the const reference.
2907 Const_expression::do_get_tree(Translate_context* context)
2909 Gogo* gogo = context->gogo();
2911 if (this->type_ == NULL)
2912 type_tree = NULL_TREE;
2915 type_tree = type_to_tree(this->type_->get_backend(gogo));
2916 if (type_tree == error_mark_node)
2917 return error_mark_node;
2920 // If the type has been set for this expression, but the underlying
2921 // object is an abstract int or float, we try to get the abstract
2922 // value. Otherwise we may lose something in the conversion.
2923 if (this->type_ != NULL
2924 && (this->constant_->const_value()->type() == NULL
2925 || this->constant_->const_value()->type()->is_abstract()))
2927 Expression* expr = this->constant_->const_value()->expr();
2931 if (expr->integer_constant_value(true, ival, &t))
2933 tree ret = Expression::integer_constant_tree(ival, type_tree);
2941 if (expr->float_constant_value(fval, &t))
2943 tree ret = Expression::float_constant_tree(fval, type_tree);
2950 if (expr->complex_constant_value(fval, imag, &t))
2952 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2961 tree const_tree = this->constant_->get_tree(gogo, context->function());
2962 if (this->type_ == NULL
2963 || const_tree == error_mark_node
2964 || TREE_TYPE(const_tree) == error_mark_node)
2968 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2969 ret = fold_convert(type_tree, const_tree);
2970 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2971 ret = fold(convert_to_integer(type_tree, const_tree));
2972 else if (TREE_CODE(type_tree) == REAL_TYPE)
2973 ret = fold(convert_to_real(type_tree, const_tree));
2974 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2975 ret = fold(convert_to_complex(type_tree, const_tree));
2981 // Dump ast representation for constant expression.
2984 Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2986 ast_dump_context->ostream() << this->constant_->name();
2989 // Make a reference to a constant in an expression.
2992 Expression::make_const_reference(Named_object* constant,
2995 return new Const_expression(constant, location);
2998 // Find a named object in an expression.
3001 Find_named_object::expression(Expression** pexpr)
3003 switch ((*pexpr)->classification())
3005 case Expression::EXPRESSION_CONST_REFERENCE:
3007 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
3008 if (ce->named_object() == this->no_)
3011 // We need to check a constant initializer explicitly, as
3012 // loops here will not be caught by the loop checking for
3013 // variable initializers.
3014 ce->check_for_init_loop();
3016 return TRAVERSE_CONTINUE;
3019 case Expression::EXPRESSION_VAR_REFERENCE:
3020 if ((*pexpr)->var_expression()->named_object() == this->no_)
3022 return TRAVERSE_CONTINUE;
3023 case Expression::EXPRESSION_FUNC_REFERENCE:
3024 if ((*pexpr)->func_expression()->named_object() == this->no_)
3026 return TRAVERSE_CONTINUE;
3028 return TRAVERSE_CONTINUE;
3030 this->found_ = true;
3031 return TRAVERSE_EXIT;
3036 class Nil_expression : public Expression
3039 Nil_expression(Location location)
3040 : Expression(EXPRESSION_NIL, location)
3048 do_is_constant() const
3053 { return Type::make_nil_type(); }
3056 do_determine_type(const Type_context*)
3064 do_get_tree(Translate_context*)
3065 { return null_pointer_node; }
3068 do_export(Export* exp) const
3069 { exp->write_c_string("nil"); }
3072 do_dump_expression(Ast_dump_context* ast_dump_context) const
3073 { ast_dump_context->ostream() << "nil"; }
3076 // Import a nil expression.
3079 Nil_expression::do_import(Import* imp)
3081 imp->require_c_string("nil");
3082 return Expression::make_nil(imp->location());
3085 // Make a nil expression.
3088 Expression::make_nil(Location location)
3090 return new Nil_expression(location);
3093 // The value of the predeclared constant iota. This is little more
3094 // than a marker. This will be lowered to an integer in
3095 // Const_expression::do_lower, which is where we know the value that
3098 class Iota_expression : public Parser_expression
3101 Iota_expression(Location location)
3102 : Parser_expression(EXPRESSION_IOTA, location)
3107 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
3108 { go_unreachable(); }
3110 // There should only ever be one of these.
3113 { go_unreachable(); }
3116 do_dump_expression(Ast_dump_context* ast_dump_context) const
3117 { ast_dump_context->ostream() << "iota"; }
3120 // Make an iota expression. This is only called for one case: the
3121 // value of the predeclared constant iota.
3124 Expression::make_iota()
3126 static Iota_expression iota_expression(Linemap::unknown_location());
3127 return &iota_expression;
3130 // A type conversion expression.
3132 class Type_conversion_expression : public Expression
3135 Type_conversion_expression(Type* type, Expression* expr,
3137 : Expression(EXPRESSION_CONVERSION, location),
3138 type_(type), expr_(expr), may_convert_function_types_(false)
3141 // Return the type to which we are converting.
3144 { return this->type_; }
3146 // Return the expression which we are converting.
3149 { return this->expr_; }
3151 // Permit converting from one function type to another. This is
3152 // used internally for method expressions.
3154 set_may_convert_function_types()
3156 this->may_convert_function_types_ = true;
3159 // Import a type conversion expression.
3165 do_traverse(Traverse* traverse);
3168 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3171 do_is_constant() const
3172 { return this->expr_->is_constant(); }
3175 do_integer_constant_value(bool, mpz_t, Type**) const;
3178 do_float_constant_value(mpfr_t, Type**) const;
3181 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3184 do_string_constant_value(std::string*) const;
3188 { return this->type_; }
3191 do_determine_type(const Type_context*)
3193 Type_context subcontext(this->type_, false);
3194 this->expr_->determine_type(&subcontext);
3198 do_check_types(Gogo*);
3203 return new Type_conversion_expression(this->type_, this->expr_->copy(),
3208 do_get_tree(Translate_context* context);
3211 do_export(Export*) const;
3214 do_dump_expression(Ast_dump_context*) const;
3217 // The type to convert to.
3219 // The expression to convert.
3221 // True if this is permitted to convert function types. This is
3222 // used internally for method expressions.
3223 bool may_convert_function_types_;
3229 Type_conversion_expression::do_traverse(Traverse* traverse)
3231 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3232 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3233 return TRAVERSE_EXIT;
3234 return TRAVERSE_CONTINUE;
3237 // Convert to a constant at lowering time.
3240 Type_conversion_expression::do_lower(Gogo*, Named_object*,
3241 Statement_inserter*, int)
3243 Type* type = this->type_;
3244 Expression* val = this->expr_;
3245 Location location = this->location();
3247 if (type->integer_type() != NULL)
3252 if (val->integer_constant_value(false, ival, &dummy))
3254 if (!Integer_expression::check_constant(ival, type, location))
3255 mpz_set_ui(ival, 0);
3256 Expression* ret = Expression::make_integer(&ival, type, location);
3263 if (val->float_constant_value(fval, &dummy))
3265 if (!mpfr_integer_p(fval))
3268 "floating point constant truncated to integer");
3269 return Expression::make_error(location);
3271 mpfr_get_z(ival, fval, GMP_RNDN);
3272 if (!Integer_expression::check_constant(ival, type, location))
3273 mpz_set_ui(ival, 0);
3274 Expression* ret = Expression::make_integer(&ival, type, location);
3283 if (type->float_type() != NULL)
3288 if (val->float_constant_value(fval, &dummy))
3290 if (!Float_expression::check_constant(fval, type, location))
3291 mpfr_set_ui(fval, 0, GMP_RNDN);
3292 Float_expression::constrain_float(fval, type);
3293 Expression *ret = Expression::make_float(&fval, type, location);
3300 if (type->complex_type() != NULL)
3307 if (val->complex_constant_value(real, imag, &dummy))
3309 if (!Complex_expression::check_constant(real, imag, type, location))
3311 mpfr_set_ui(real, 0, GMP_RNDN);
3312 mpfr_set_ui(imag, 0, GMP_RNDN);
3314 Complex_expression::constrain_complex(real, imag, type);
3315 Expression* ret = Expression::make_complex(&real, &imag, type,
3325 if (type->is_slice_type() && type->named_type() == NULL)
3327 Type* element_type = type->array_type()->element_type()->forwarded();
3328 bool is_byte = element_type == Type::lookup_integer_type("uint8");
3329 bool is_int = element_type == Type::lookup_integer_type("int");
3330 if (is_byte || is_int)
3333 if (val->string_constant_value(&s))
3335 Expression_list* vals = new Expression_list();
3338 for (std::string::const_iterator p = s.begin();
3343 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3344 Expression* v = Expression::make_integer(&val,
3353 const char *p = s.data();
3354 const char *pend = s.data() + s.length();
3358 int adv = Lex::fetch_char(p, &c);
3361 warning_at(this->location(), 0,
3362 "invalid UTF-8 encoding");
3367 mpz_init_set_ui(val, c);
3368 Expression* v = Expression::make_integer(&val,
3376 return Expression::make_slice_composite_literal(type, vals,
3385 // Return the constant integer value if there is one.
3388 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3392 if (this->type_->integer_type() == NULL)
3398 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3400 if (!Integer_expression::check_constant(ival, this->type_,
3408 *ptype = this->type_;
3415 if (this->expr_->float_constant_value(fval, &dummy))
3417 mpfr_get_z(val, fval, GMP_RNDN);
3419 if (!Integer_expression::check_constant(val, this->type_,
3422 *ptype = this->type_;
3430 // Return the constant floating point value if there is one.
3433 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3436 if (this->type_->float_type() == NULL)
3442 if (this->expr_->float_constant_value(fval, &dummy))
3444 if (!Float_expression::check_constant(fval, this->type_,
3450 mpfr_set(val, fval, GMP_RNDN);
3452 Float_expression::constrain_float(val, this->type_);
3453 *ptype = this->type_;
3461 // Return the constant complex value if there is one.
3464 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3468 if (this->type_->complex_type() == NULL)
3476 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3478 if (!Complex_expression::check_constant(rval, ival, this->type_,
3485 mpfr_set(real, rval, GMP_RNDN);
3486 mpfr_set(imag, ival, GMP_RNDN);
3489 Complex_expression::constrain_complex(real, imag, this->type_);
3490 *ptype = this->type_;
3499 // Return the constant string value if there is one.
3502 Type_conversion_expression::do_string_constant_value(std::string* val) const
3504 if (this->type_->is_string_type()
3505 && this->expr_->type()->integer_type() != NULL)
3510 if (this->expr_->integer_constant_value(false, ival, &dummy))
3512 unsigned long ulval = mpz_get_ui(ival);
3513 if (mpz_cmp_ui(ival, ulval) == 0)
3515 Lex::append_char(ulval, true, val, this->location());
3523 // FIXME: Could handle conversion from const []int here.
3528 // Check that types are convertible.
3531 Type_conversion_expression::do_check_types(Gogo*)
3533 Type* type = this->type_;
3534 Type* expr_type = this->expr_->type();
3537 if (type->is_error() || expr_type->is_error())
3539 this->set_is_error();
3543 if (this->may_convert_function_types_
3544 && type->function_type() != NULL
3545 && expr_type->function_type() != NULL)
3548 if (Type::are_convertible(type, expr_type, &reason))
3551 error_at(this->location(), "%s", reason.c_str());
3552 this->set_is_error();
3555 // Get a tree for a type conversion.
3558 Type_conversion_expression::do_get_tree(Translate_context* context)
3560 Gogo* gogo = context->gogo();
3561 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
3562 tree expr_tree = this->expr_->get_tree(context);
3564 if (type_tree == error_mark_node
3565 || expr_tree == error_mark_node
3566 || TREE_TYPE(expr_tree) == error_mark_node)
3567 return error_mark_node;
3569 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3570 return fold_convert(type_tree, expr_tree);
3572 Type* type = this->type_;
3573 Type* expr_type = this->expr_->type();
3575 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3576 ret = Expression::convert_for_assignment(context, type, expr_type,
3577 expr_tree, this->location());
3578 else if (type->integer_type() != NULL)
3580 if (expr_type->integer_type() != NULL
3581 || expr_type->float_type() != NULL
3582 || expr_type->is_unsafe_pointer_type())
3583 ret = fold(convert_to_integer(type_tree, expr_tree));
3587 else if (type->float_type() != NULL)
3589 if (expr_type->integer_type() != NULL
3590 || expr_type->float_type() != NULL)
3591 ret = fold(convert_to_real(type_tree, expr_tree));
3595 else if (type->complex_type() != NULL)
3597 if (expr_type->complex_type() != NULL)
3598 ret = fold(convert_to_complex(type_tree, expr_tree));
3602 else if (type->is_string_type()
3603 && expr_type->integer_type() != NULL)
3605 expr_tree = fold_convert(integer_type_node, expr_tree);
3606 if (host_integerp(expr_tree, 0))
3608 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3610 Lex::append_char(intval, true, &s, this->location());
3611 Expression* se = Expression::make_string(s, this->location());
3612 return se->get_tree(context);
3615 static tree int_to_string_fndecl;
3616 ret = Gogo::call_builtin(&int_to_string_fndecl,
3618 "__go_int_to_string",
3622 fold_convert(integer_type_node, expr_tree));
3624 else if (type->is_string_type()
3625 && (expr_type->array_type() != NULL
3626 || (expr_type->points_to() != NULL
3627 && expr_type->points_to()->array_type() != NULL)))
3629 Type* t = expr_type;
3630 if (t->points_to() != NULL)
3633 expr_tree = build_fold_indirect_ref(expr_tree);
3635 if (!DECL_P(expr_tree))
3636 expr_tree = save_expr(expr_tree);
3637 Array_type* a = t->array_type();
3638 Type* e = a->element_type()->forwarded();
3639 go_assert(e->integer_type() != NULL);
3640 tree valptr = fold_convert(const_ptr_type_node,
3641 a->value_pointer_tree(gogo, expr_tree));
3642 tree len = a->length_tree(gogo, expr_tree);
3643 len = fold_convert_loc(this->location().gcc_location(), integer_type_node,
3645 if (e->integer_type()->is_unsigned()
3646 && e->integer_type()->bits() == 8)
3648 static tree byte_array_to_string_fndecl;
3649 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3651 "__go_byte_array_to_string",
3654 const_ptr_type_node,
3661 go_assert(e == Type::lookup_integer_type("int"));
3662 static tree int_array_to_string_fndecl;
3663 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3665 "__go_int_array_to_string",
3668 const_ptr_type_node,
3674 else if (type->is_slice_type() && expr_type->is_string_type())
3676 Type* e = type->array_type()->element_type()->forwarded();
3677 go_assert(e->integer_type() != NULL);
3678 if (e->integer_type()->is_unsigned()
3679 && e->integer_type()->bits() == 8)
3681 static tree string_to_byte_array_fndecl;
3682 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3684 "__go_string_to_byte_array",
3687 TREE_TYPE(expr_tree),
3692 go_assert(e == Type::lookup_integer_type("int"));
3693 static tree string_to_int_array_fndecl;
3694 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3696 "__go_string_to_int_array",
3699 TREE_TYPE(expr_tree),
3703 else if ((type->is_unsafe_pointer_type()
3704 && expr_type->points_to() != NULL)
3705 || (expr_type->is_unsafe_pointer_type()
3706 && type->points_to() != NULL))
3707 ret = fold_convert(type_tree, expr_tree);
3708 else if (type->is_unsafe_pointer_type()
3709 && expr_type->integer_type() != NULL)
3710 ret = convert_to_pointer(type_tree, expr_tree);
3711 else if (this->may_convert_function_types_
3712 && type->function_type() != NULL
3713 && expr_type->function_type() != NULL)
3714 ret = fold_convert_loc(this->location().gcc_location(), type_tree,
3717 ret = Expression::convert_for_assignment(context, type, expr_type,
3718 expr_tree, this->location());
3723 // Output a type conversion in a constant expression.
3726 Type_conversion_expression::do_export(Export* exp) const
3728 exp->write_c_string("convert(");
3729 exp->write_type(this->type_);
3730 exp->write_c_string(", ");
3731 this->expr_->export_expression(exp);
3732 exp->write_c_string(")");
3735 // Import a type conversion or a struct construction.
3738 Type_conversion_expression::do_import(Import* imp)
3740 imp->require_c_string("convert(");
3741 Type* type = imp->read_type();
3742 imp->require_c_string(", ");
3743 Expression* val = Expression::import_expression(imp);
3744 imp->require_c_string(")");
3745 return Expression::make_cast(type, val, imp->location());
3748 // Dump ast representation for a type conversion expression.
3751 Type_conversion_expression::do_dump_expression(
3752 Ast_dump_context* ast_dump_context) const
3754 ast_dump_context->dump_type(this->type_);
3755 ast_dump_context->ostream() << "(";
3756 ast_dump_context->dump_expression(this->expr_);
3757 ast_dump_context->ostream() << ") ";
3760 // Make a type cast expression.
3763 Expression::make_cast(Type* type, Expression* val, Location location)
3765 if (type->is_error_type() || val->is_error_expression())
3766 return Expression::make_error(location);
3767 return new Type_conversion_expression(type, val, location);
3770 // An unsafe type conversion, used to pass values to builtin functions.
3772 class Unsafe_type_conversion_expression : public Expression
3775 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3777 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3778 type_(type), expr_(expr)
3783 do_traverse(Traverse* traverse);
3787 { return this->type_; }
3790 do_determine_type(const Type_context*)
3791 { this->expr_->determine_type_no_context(); }
3796 return new Unsafe_type_conversion_expression(this->type_,
3797 this->expr_->copy(),
3802 do_get_tree(Translate_context*);
3805 do_dump_expression(Ast_dump_context*) const;
3808 // The type to convert to.
3810 // The expression to convert.
3817 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3819 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3820 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3821 return TRAVERSE_EXIT;
3822 return TRAVERSE_CONTINUE;
3825 // Convert to backend representation.
3828 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3830 // We are only called for a limited number of cases.
3832 Type* t = this->type_;
3833 Type* et = this->expr_->type();
3835 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
3836 tree expr_tree = this->expr_->get_tree(context);
3837 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3838 return error_mark_node;
3840 Location loc = this->location();
3842 bool use_view_convert = false;
3843 if (t->is_slice_type())
3845 go_assert(et->is_slice_type());
3846 use_view_convert = true;
3848 else if (t->map_type() != NULL)
3849 go_assert(et->map_type() != NULL);
3850 else if (t->channel_type() != NULL)
3851 go_assert(et->channel_type() != NULL);
3852 else if (t->points_to() != NULL && t->points_to()->channel_type() != NULL)
3853 go_assert((et->points_to() != NULL
3854 && et->points_to()->channel_type() != NULL)
3855 || et->is_nil_type());
3856 else if (t->points_to() != NULL)
3857 go_assert(et->points_to() != NULL || et->is_nil_type());
3858 else if (et->is_unsafe_pointer_type())
3859 go_assert(t->points_to() != NULL);
3860 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3862 go_assert(et->interface_type() != NULL
3863 && !et->interface_type()->is_empty());
3864 use_view_convert = true;
3866 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3868 go_assert(et->interface_type() != NULL
3869 && et->interface_type()->is_empty());
3870 use_view_convert = true;
3872 else if (t->integer_type() != NULL)
3874 go_assert(et->is_boolean_type()
3875 || et->integer_type() != NULL
3876 || et->function_type() != NULL
3877 || et->points_to() != NULL
3878 || et->map_type() != NULL
3879 || et->channel_type() != NULL);
3880 return convert_to_integer(type_tree, expr_tree);
3885 if (use_view_convert)
3886 return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree,
3889 return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree);
3892 // Dump ast representation for an unsafe type conversion expression.
3895 Unsafe_type_conversion_expression::do_dump_expression(
3896 Ast_dump_context* ast_dump_context) const
3898 ast_dump_context->dump_type(this->type_);
3899 ast_dump_context->ostream() << "(";
3900 ast_dump_context->dump_expression(this->expr_);
3901 ast_dump_context->ostream() << ") ";
3904 // Make an unsafe type conversion expression.
3907 Expression::make_unsafe_cast(Type* type, Expression* expr,
3910 return new Unsafe_type_conversion_expression(type, expr, location);
3913 // Unary expressions.
3915 class Unary_expression : public Expression
3918 Unary_expression(Operator op, Expression* expr, Location location)
3919 : Expression(EXPRESSION_UNARY, location),
3920 op_(op), escapes_(true), create_temp_(false), expr_(expr)
3923 // Return the operator.
3926 { return this->op_; }
3928 // Return the operand.
3931 { return this->expr_; }
3933 // Record that an address expression does not escape.
3935 set_does_not_escape()
3937 go_assert(this->op_ == OPERATOR_AND);
3938 this->escapes_ = false;
3941 // Record that this is an address expression which should create a
3942 // temporary variable if necessary. This is used for method calls.
3946 go_assert(this->op_ == OPERATOR_AND);
3947 this->create_temp_ = true;
3950 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3951 // could be done, false if not.
3953 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3956 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3957 // could be done, false if not.
3959 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3961 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3962 // true if this could be done, false if not.
3964 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3972 do_traverse(Traverse* traverse)
3973 { return Expression::traverse(&this->expr_, traverse); }
3976 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3979 do_is_constant() const;
3982 do_integer_constant_value(bool, mpz_t, Type**) const;
3985 do_float_constant_value(mpfr_t, Type**) const;
3988 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3994 do_determine_type(const Type_context*);
3997 do_check_types(Gogo*);
4002 return Expression::make_unary(this->op_, this->expr_->copy(),
4007 do_must_eval_subexpressions_in_order(int*) const
4008 { return this->op_ == OPERATOR_MULT; }
4011 do_is_addressable() const
4012 { return this->op_ == OPERATOR_MULT; }
4015 do_get_tree(Translate_context*);
4018 do_export(Export*) const;
4021 do_dump_expression(Ast_dump_context*) const;
4024 // The unary operator to apply.
4026 // Normally true. False if this is an address expression which does
4027 // not escape the current function.
4029 // True if this is an address expression which should create a
4030 // temporary variable if necessary.
4036 // If we are taking the address of a composite literal, and the
4037 // contents are not constant, then we want to make a heap composite
4041 Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
4043 Location loc = this->location();
4044 Operator op = this->op_;
4045 Expression* expr = this->expr_;
4047 if (op == OPERATOR_MULT && expr->is_type_expression())
4048 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
4050 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
4051 // moving x to the heap. FIXME: Is it worth doing a real escape
4052 // analysis here? This case is found in math/unsafe.go and is
4053 // therefore worth special casing.
4054 if (op == OPERATOR_MULT)
4056 Expression* e = expr;
4057 while (e->classification() == EXPRESSION_CONVERSION)
4059 Type_conversion_expression* te
4060 = static_cast<Type_conversion_expression*>(e);
4064 if (e->classification() == EXPRESSION_UNARY)
4066 Unary_expression* ue = static_cast<Unary_expression*>(e);
4067 if (ue->op_ == OPERATOR_AND)
4074 ue->set_does_not_escape();
4079 // Catching an invalid indirection of unsafe.Pointer here avoid
4080 // having to deal with TYPE_VOID in other places.
4081 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
4083 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
4084 return Expression::make_error(this->location());
4087 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
4088 || op == OPERATOR_NOT || op == OPERATOR_XOR)
4090 Expression* ret = NULL;
4095 if (expr->integer_constant_value(false, eval, &etype))
4099 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
4100 ret = Expression::make_integer(&val, etype, loc);
4107 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
4112 if (expr->float_constant_value(fval, &ftype))
4116 if (Unary_expression::eval_float(op, fval, val))
4117 ret = Expression::make_float(&val, ftype, loc);
4128 if (expr->complex_constant_value(fval, ival, &ftype))
4134 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
4135 ret = Expression::make_complex(&real, &imag, ftype, loc);
4149 // Return whether a unary expression is a constant.
4152 Unary_expression::do_is_constant() const
4154 if (this->op_ == OPERATOR_MULT)
4156 // Indirecting through a pointer is only constant if the object
4157 // to which the expression points is constant, but we currently
4158 // have no way to determine that.
4161 else if (this->op_ == OPERATOR_AND)
4163 // Taking the address of a variable is constant if it is a
4164 // global variable, not constant otherwise. In other cases
4165 // taking the address is probably not a constant.
4166 Var_expression* ve = this->expr_->var_expression();
4169 Named_object* no = ve->named_object();
4170 return no->is_variable() && no->var_value()->is_global();
4175 return this->expr_->is_constant();
4178 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
4179 // UVAL, if known; it may be NULL. Return true if this could be done,
4183 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
4191 case OPERATOR_MINUS:
4193 return Integer_expression::check_constant(val, utype, location);
4195 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
4199 || utype->integer_type() == NULL
4200 || utype->integer_type()->is_abstract())
4204 // The number of HOST_WIDE_INTs that it takes to represent
4206 size_t count = ((mpz_sizeinbase(uval, 2)
4207 + HOST_BITS_PER_WIDE_INT
4209 / HOST_BITS_PER_WIDE_INT);
4211 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
4212 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
4215 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
4216 go_assert(ecount <= count);
4218 // Trim down to the number of words required by the type.
4219 size_t obits = utype->integer_type()->bits();
4220 if (!utype->integer_type()->is_unsigned())
4222 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
4223 / HOST_BITS_PER_WIDE_INT);
4224 go_assert(ocount <= count);
4226 for (size_t i = 0; i < ocount; ++i)
4229 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
4231 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
4234 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
4238 return Integer_expression::check_constant(val, utype, location);
4247 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
4248 // could be done, false if not.
4251 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
4256 mpfr_set(val, uval, GMP_RNDN);
4258 case OPERATOR_MINUS:
4259 mpfr_neg(val, uval, GMP_RNDN);
4271 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
4272 // if this could be done, false if not.
4275 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
4276 mpfr_t real, mpfr_t imag)
4281 mpfr_set(real, rval, GMP_RNDN);
4282 mpfr_set(imag, ival, GMP_RNDN);
4284 case OPERATOR_MINUS:
4285 mpfr_neg(real, rval, GMP_RNDN);
4286 mpfr_neg(imag, ival, GMP_RNDN);
4298 // Return the integral constant value of a unary expression, if it has one.
4301 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
4307 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
4310 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
4316 // Return the floating point constant value of a unary expression, if
4320 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
4325 if (!this->expr_->float_constant_value(uval, ptype))
4328 ret = Unary_expression::eval_float(this->op_, uval, val);
4333 // Return the complex constant value of a unary expression, if it has
4337 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
4345 if (!this->expr_->complex_constant_value(rval, ival, ptype))
4348 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
4354 // Return the type of a unary expression.
4357 Unary_expression::do_type()
4362 case OPERATOR_MINUS:
4365 return this->expr_->type();
4368 return Type::make_pointer_type(this->expr_->type());
4372 Type* subtype = this->expr_->type();
4373 Type* points_to = subtype->points_to();
4374 if (points_to == NULL)
4375 return Type::make_error_type();
4384 // Determine abstract types for a unary expression.
4387 Unary_expression::do_determine_type(const Type_context* context)
4392 case OPERATOR_MINUS:
4395 this->expr_->determine_type(context);
4399 // Taking the address of something.
4401 Type* subtype = (context->type == NULL
4403 : context->type->points_to());
4404 Type_context subcontext(subtype, false);
4405 this->expr_->determine_type(&subcontext);
4410 // Indirecting through a pointer.
4412 Type* subtype = (context->type == NULL
4414 : Type::make_pointer_type(context->type));
4415 Type_context subcontext(subtype, false);
4416 this->expr_->determine_type(&subcontext);
4425 // Check types for a unary expression.
4428 Unary_expression::do_check_types(Gogo*)
4430 Type* type = this->expr_->type();
4431 if (type->is_error())
4433 this->set_is_error();
4440 case OPERATOR_MINUS:
4441 if (type->integer_type() == NULL
4442 && type->float_type() == NULL
4443 && type->complex_type() == NULL)
4444 this->report_error(_("expected numeric type"));
4449 if (type->integer_type() == NULL
4450 && !type->is_boolean_type())
4451 this->report_error(_("expected integer or boolean type"));
4455 if (!this->expr_->is_addressable())
4457 if (!this->create_temp_)
4458 this->report_error(_("invalid operand for unary %<&%>"));
4461 this->expr_->address_taken(this->escapes_);
4465 // Indirecting through a pointer.
4466 if (type->points_to() == NULL)
4467 this->report_error(_("expected pointer"));
4475 // Get a tree for a unary expression.
4478 Unary_expression::do_get_tree(Translate_context* context)
4480 tree expr = this->expr_->get_tree(context);
4481 if (expr == error_mark_node)
4482 return error_mark_node;
4484 Location loc = this->location();
4490 case OPERATOR_MINUS:
4492 tree type = TREE_TYPE(expr);
4493 tree compute_type = excess_precision_type(type);
4494 if (compute_type != NULL_TREE)
4495 expr = ::convert(compute_type, expr);
4496 tree ret = fold_build1_loc(loc.gcc_location(), NEGATE_EXPR,
4497 (compute_type != NULL_TREE
4501 if (compute_type != NULL_TREE)
4502 ret = ::convert(type, ret);
4507 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4508 return fold_build1_loc(loc.gcc_location(), TRUTH_NOT_EXPR,
4509 TREE_TYPE(expr), expr);
4511 return fold_build2_loc(loc.gcc_location(), NE_EXPR, boolean_type_node,
4512 expr, build_int_cst(TREE_TYPE(expr), 0));
4515 return fold_build1_loc(loc.gcc_location(), BIT_NOT_EXPR, TREE_TYPE(expr),
4519 if (!this->create_temp_)
4521 // We should not see a non-constant constructor here; cases
4522 // where we would see one should have been moved onto the
4523 // heap at parse time. Taking the address of a nonconstant
4524 // constructor will not do what the programmer expects.
4525 go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4526 go_assert(TREE_CODE(expr) != ADDR_EXPR);
4529 // Build a decl for a constant constructor.
4530 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4532 tree decl = build_decl(this->location().gcc_location(), VAR_DECL,
4533 create_tmp_var_name("C"), TREE_TYPE(expr));
4534 DECL_EXTERNAL(decl) = 0;
4535 TREE_PUBLIC(decl) = 0;
4536 TREE_READONLY(decl) = 1;
4537 TREE_CONSTANT(decl) = 1;
4538 TREE_STATIC(decl) = 1;
4539 TREE_ADDRESSABLE(decl) = 1;
4540 DECL_ARTIFICIAL(decl) = 1;
4541 DECL_INITIAL(decl) = expr;
4542 rest_of_decl_compilation(decl, 1, 0);
4546 if (this->create_temp_
4547 && !TREE_ADDRESSABLE(TREE_TYPE(expr))
4549 && TREE_CODE(expr) != INDIRECT_REF
4550 && TREE_CODE(expr) != COMPONENT_REF)
4552 tree tmp = create_tmp_var(TREE_TYPE(expr), get_name(expr));
4553 DECL_IGNORED_P(tmp) = 1;
4554 DECL_INITIAL(tmp) = expr;
4555 TREE_ADDRESSABLE(tmp) = 1;
4556 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4557 build_pointer_type(TREE_TYPE(expr)),
4558 build1_loc(loc.gcc_location(), DECL_EXPR,
4559 void_type_node, tmp),
4560 build_fold_addr_expr_loc(loc.gcc_location(), tmp));
4563 return build_fold_addr_expr_loc(loc.gcc_location(), expr);
4567 go_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4569 // If we are dereferencing the pointer to a large struct, we
4570 // need to check for nil. We don't bother to check for small
4571 // structs because we expect the system to crash on a nil
4572 // pointer dereference.
4573 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4574 if (s == -1 || s >= 4096)
4577 expr = save_expr(expr);
4578 tree compare = fold_build2_loc(loc.gcc_location(), EQ_EXPR,
4581 fold_convert(TREE_TYPE(expr),
4582 null_pointer_node));
4583 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4585 expr = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4586 TREE_TYPE(expr), build3(COND_EXPR,
4593 // If the type of EXPR is a recursive pointer type, then we
4594 // need to insert a cast before indirecting.
4595 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4597 Type* pt = this->expr_->type()->points_to();
4598 tree ind = type_to_tree(pt->get_backend(context->gogo()));
4599 expr = fold_convert_loc(loc.gcc_location(),
4600 build_pointer_type(ind), expr);
4603 return build_fold_indirect_ref_loc(loc.gcc_location(), expr);
4611 // Export a unary expression.
4614 Unary_expression::do_export(Export* exp) const
4619 exp->write_c_string("+ ");
4621 case OPERATOR_MINUS:
4622 exp->write_c_string("- ");
4625 exp->write_c_string("! ");
4628 exp->write_c_string("^ ");
4635 this->expr_->export_expression(exp);
4638 // Import a unary expression.
4641 Unary_expression::do_import(Import* imp)
4644 switch (imp->get_char())
4650 op = OPERATOR_MINUS;
4661 imp->require_c_string(" ");
4662 Expression* expr = Expression::import_expression(imp);
4663 return Expression::make_unary(op, expr, imp->location());
4666 // Dump ast representation of an unary expression.
4669 Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
4671 ast_dump_context->dump_operator(this->op_);
4672 ast_dump_context->ostream() << "(";
4673 ast_dump_context->dump_expression(this->expr_);
4674 ast_dump_context->ostream() << ") ";
4677 // Make a unary expression.
4680 Expression::make_unary(Operator op, Expression* expr, Location location)
4682 return new Unary_expression(op, expr, location);
4685 // If this is an indirection through a pointer, return the expression
4686 // being pointed through. Otherwise return this.
4691 if (this->classification_ == EXPRESSION_UNARY)
4693 Unary_expression* ue = static_cast<Unary_expression*>(this);
4694 if (ue->op() == OPERATOR_MULT)
4695 return ue->operand();
4700 // Class Binary_expression.
4705 Binary_expression::do_traverse(Traverse* traverse)
4707 int t = Expression::traverse(&this->left_, traverse);
4708 if (t == TRAVERSE_EXIT)
4709 return TRAVERSE_EXIT;
4710 return Expression::traverse(&this->right_, traverse);
4713 // Compare integer constants according to OP.
4716 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4719 int i = mpz_cmp(left_val, right_val);
4724 case OPERATOR_NOTEQ:
4739 // Compare floating point constants according to OP.
4742 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4747 i = mpfr_cmp(left_val, right_val);
4751 mpfr_init_set(lv, left_val, GMP_RNDN);
4753 mpfr_init_set(rv, right_val, GMP_RNDN);
4754 Float_expression::constrain_float(lv, type);
4755 Float_expression::constrain_float(rv, type);
4756 i = mpfr_cmp(lv, rv);
4764 case OPERATOR_NOTEQ:
4779 // Compare complex constants according to OP. Complex numbers may
4780 // only be compared for equality.
4783 Binary_expression::compare_complex(Operator op, Type* type,
4784 mpfr_t left_real, mpfr_t left_imag,
4785 mpfr_t right_real, mpfr_t right_imag)
4789 is_equal = (mpfr_cmp(left_real, right_real) == 0
4790 && mpfr_cmp(left_imag, right_imag) == 0);
4795 mpfr_init_set(lr, left_real, GMP_RNDN);
4796 mpfr_init_set(li, left_imag, GMP_RNDN);
4799 mpfr_init_set(rr, right_real, GMP_RNDN);
4800 mpfr_init_set(ri, right_imag, GMP_RNDN);
4801 Complex_expression::constrain_complex(lr, li, type);
4802 Complex_expression::constrain_complex(rr, ri, type);
4803 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4813 case OPERATOR_NOTEQ:
4820 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4821 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4822 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4823 // this could be done, false if not.
4826 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4827 Type* right_type, mpz_t right_val,
4828 Location location, mpz_t val)
4830 bool is_shift_op = false;
4834 case OPERATOR_ANDAND:
4836 case OPERATOR_NOTEQ:
4841 // These return boolean values. We should probably handle them
4842 // anyhow in case a type conversion is used on the result.
4845 mpz_add(val, left_val, right_val);
4847 case OPERATOR_MINUS:
4848 mpz_sub(val, left_val, right_val);
4851 mpz_ior(val, left_val, right_val);
4854 mpz_xor(val, left_val, right_val);
4857 mpz_mul(val, left_val, right_val);
4860 if (mpz_sgn(right_val) != 0)
4861 mpz_tdiv_q(val, left_val, right_val);
4864 error_at(location, "division by zero");
4870 if (mpz_sgn(right_val) != 0)
4871 mpz_tdiv_r(val, left_val, right_val);
4874 error_at(location, "division by zero");
4879 case OPERATOR_LSHIFT:
4881 unsigned long shift = mpz_get_ui(right_val);
4882 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
4884 error_at(location, "shift count overflow");
4888 mpz_mul_2exp(val, left_val, shift);
4893 case OPERATOR_RSHIFT:
4895 unsigned long shift = mpz_get_ui(right_val);
4896 if (mpz_cmp_ui(right_val, shift) != 0)
4898 error_at(location, "shift count overflow");
4902 if (mpz_cmp_ui(left_val, 0) >= 0)
4903 mpz_tdiv_q_2exp(val, left_val, shift);
4905 mpz_fdiv_q_2exp(val, left_val, shift);
4911 mpz_and(val, left_val, right_val);
4913 case OPERATOR_BITCLEAR:
4917 mpz_com(tval, right_val);
4918 mpz_and(val, left_val, tval);
4926 Type* type = left_type;
4931 else if (type != right_type && right_type != NULL)
4933 if (type->is_abstract())
4935 else if (!right_type->is_abstract())
4937 // This look like a type error which should be diagnosed
4938 // elsewhere. Don't do anything here, to avoid an
4939 // unhelpful chain of error messages.
4945 if (type != NULL && !type->is_abstract())
4947 // We have to check the operands too, as we have implicitly
4948 // coerced them to TYPE.
4949 if ((type != left_type
4950 && !Integer_expression::check_constant(left_val, type, location))
4952 && type != right_type
4953 && !Integer_expression::check_constant(right_val, type,
4955 || !Integer_expression::check_constant(val, type, location))
4962 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4963 // Return true if this could be done, false if not.
4966 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4967 Type* right_type, mpfr_t right_val,
4968 mpfr_t val, Location location)
4973 case OPERATOR_ANDAND:
4975 case OPERATOR_NOTEQ:
4980 // These return boolean values. We should probably handle them
4981 // anyhow in case a type conversion is used on the result.
4984 mpfr_add(val, left_val, right_val, GMP_RNDN);
4986 case OPERATOR_MINUS:
4987 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4992 case OPERATOR_BITCLEAR:
4995 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4998 if (mpfr_zero_p(right_val))
4999 error_at(location, "division by zero");
5000 mpfr_div(val, left_val, right_val, GMP_RNDN);
5004 case OPERATOR_LSHIFT:
5005 case OPERATOR_RSHIFT:
5011 Type* type = left_type;
5014 else if (type != right_type && right_type != NULL)
5016 if (type->is_abstract())
5018 else if (!right_type->is_abstract())
5020 // This looks like a type error which should be diagnosed
5021 // elsewhere. Don't do anything here, to avoid an unhelpful
5022 // chain of error messages.
5027 if (type != NULL && !type->is_abstract())
5029 if ((type != left_type
5030 && !Float_expression::check_constant(left_val, type, location))
5031 || (type != right_type
5032 && !Float_expression::check_constant(right_val, type,
5034 || !Float_expression::check_constant(val, type, location))
5035 mpfr_set_ui(val, 0, GMP_RNDN);
5041 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
5042 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
5043 // could be done, false if not.
5046 Binary_expression::eval_complex(Operator op, Type* left_type,
5047 mpfr_t left_real, mpfr_t left_imag,
5049 mpfr_t right_real, mpfr_t right_imag,
5050 mpfr_t real, mpfr_t imag,
5056 case OPERATOR_ANDAND:
5058 case OPERATOR_NOTEQ:
5063 // These return boolean values and must be handled differently.
5066 mpfr_add(real, left_real, right_real, GMP_RNDN);
5067 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
5069 case OPERATOR_MINUS:
5070 mpfr_sub(real, left_real, right_real, GMP_RNDN);
5071 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
5076 case OPERATOR_BITCLEAR:
5080 // You might think that multiplying two complex numbers would
5081 // be simple, and you would be right, until you start to think
5082 // about getting the right answer for infinity. If one
5083 // operand here is infinity and the other is anything other
5084 // than zero or NaN, then we are going to wind up subtracting
5085 // two infinity values. That will give us a NaN, but the
5086 // correct answer is infinity.
5090 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
5094 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
5098 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
5102 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
5104 mpfr_sub(real, lrrr, liri, GMP_RNDN);
5105 mpfr_add(imag, lrri, lirr, GMP_RNDN);
5107 // If we get NaN on both sides, check whether it should really
5108 // be infinity. The rule is that if either side of the
5109 // complex number is infinity, then the whole value is
5110 // infinity, even if the other side is NaN. So the only case
5111 // we have to fix is the one in which both sides are NaN.
5112 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
5113 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
5114 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
5116 bool is_infinity = false;
5120 mpfr_init_set(lr, left_real, GMP_RNDN);
5121 mpfr_init_set(li, left_imag, GMP_RNDN);
5125 mpfr_init_set(rr, right_real, GMP_RNDN);
5126 mpfr_init_set(ri, right_imag, GMP_RNDN);
5128 // If the left side is infinity, then the result is
5130 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
5132 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
5133 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5134 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
5135 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5138 mpfr_set_ui(rr, 0, GMP_RNDN);
5139 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5143 mpfr_set_ui(ri, 0, GMP_RNDN);
5144 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5149 // If the right side is infinity, then the result is
5151 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
5153 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5154 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5155 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5156 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5159 mpfr_set_ui(lr, 0, GMP_RNDN);
5160 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5164 mpfr_set_ui(li, 0, GMP_RNDN);
5165 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5170 // If we got an overflow in the intermediate computations,
5171 // then the result is infinity.
5173 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
5174 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
5178 mpfr_set_ui(lr, 0, GMP_RNDN);
5179 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5183 mpfr_set_ui(li, 0, GMP_RNDN);
5184 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5188 mpfr_set_ui(rr, 0, GMP_RNDN);
5189 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5193 mpfr_set_ui(ri, 0, GMP_RNDN);
5194 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5201 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
5202 mpfr_mul(lrri, lr, ri, GMP_RNDN);
5203 mpfr_mul(lirr, li, rr, GMP_RNDN);
5204 mpfr_mul(liri, li, ri, GMP_RNDN);
5205 mpfr_sub(real, lrrr, liri, GMP_RNDN);
5206 mpfr_add(imag, lrri, lirr, GMP_RNDN);
5207 mpfr_set_inf(real, mpfr_sgn(real));
5208 mpfr_set_inf(imag, mpfr_sgn(imag));
5225 // For complex division we want to avoid having an
5226 // intermediate overflow turn the whole result in a NaN. We
5227 // scale the values to try to avoid this.
5229 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
5230 error_at(location, "division by zero");
5236 mpfr_abs(rra, right_real, GMP_RNDN);
5237 mpfr_abs(ria, right_imag, GMP_RNDN);
5240 mpfr_max(t, rra, ria, GMP_RNDN);
5244 mpfr_init_set(rr, right_real, GMP_RNDN);
5245 mpfr_init_set(ri, right_imag, GMP_RNDN);
5247 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
5249 ilogbw = mpfr_get_exp(t);
5250 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
5251 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
5256 mpfr_mul(denom, rr, rr, GMP_RNDN);
5257 mpfr_mul(t, ri, ri, GMP_RNDN);
5258 mpfr_add(denom, denom, t, GMP_RNDN);
5260 mpfr_mul(real, left_real, rr, GMP_RNDN);
5261 mpfr_mul(t, left_imag, ri, GMP_RNDN);
5262 mpfr_add(real, real, t, GMP_RNDN);
5263 mpfr_div(real, real, denom, GMP_RNDN);
5264 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
5266 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
5267 mpfr_mul(t, left_real, ri, GMP_RNDN);
5268 mpfr_sub(imag, imag, t, GMP_RNDN);
5269 mpfr_div(imag, imag, denom, GMP_RNDN);
5270 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
5272 // If we wind up with NaN on both sides, check whether we
5273 // should really have infinity. The rule is that if either
5274 // side of the complex number is infinity, then the whole
5275 // value is infinity, even if the other side is NaN. So the
5276 // only case we have to fix is the one in which both sides are
5278 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
5279 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
5280 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
5282 if (mpfr_zero_p(denom))
5284 mpfr_set_inf(real, mpfr_sgn(rr));
5285 mpfr_mul(real, real, left_real, GMP_RNDN);
5286 mpfr_set_inf(imag, mpfr_sgn(rr));
5287 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
5289 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
5290 && mpfr_number_p(rr) && mpfr_number_p(ri))
5292 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
5293 mpfr_copysign(t, t, left_real, GMP_RNDN);
5296 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
5297 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
5301 mpfr_mul(t3, t, rr, GMP_RNDN);
5305 mpfr_mul(t4, t2, ri, GMP_RNDN);
5307 mpfr_add(t3, t3, t4, GMP_RNDN);
5308 mpfr_set_inf(real, mpfr_sgn(t3));
5310 mpfr_mul(t3, t2, rr, GMP_RNDN);
5311 mpfr_mul(t4, t, ri, GMP_RNDN);
5312 mpfr_sub(t3, t3, t4, GMP_RNDN);
5313 mpfr_set_inf(imag, mpfr_sgn(t3));
5319 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
5320 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
5322 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5323 mpfr_copysign(t, t, rr, GMP_RNDN);
5326 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5327 mpfr_copysign(t2, t2, ri, GMP_RNDN);
5331 mpfr_mul(t3, left_real, t, GMP_RNDN);
5335 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
5337 mpfr_add(t3, t3, t4, GMP_RNDN);
5338 mpfr_set_ui(real, 0, GMP_RNDN);
5339 mpfr_mul(real, real, t3, GMP_RNDN);
5341 mpfr_mul(t3, left_imag, t, GMP_RNDN);
5342 mpfr_mul(t4, left_real, t2, GMP_RNDN);
5343 mpfr_sub(t3, t3, t4, GMP_RNDN);
5344 mpfr_set_ui(imag, 0, GMP_RNDN);
5345 mpfr_mul(imag, imag, t3, GMP_RNDN);
5363 case OPERATOR_LSHIFT:
5364 case OPERATOR_RSHIFT:
5370 Type* type = left_type;
5373 else if (type != right_type && right_type != NULL)
5375 if (type->is_abstract())
5377 else if (!right_type->is_abstract())
5379 // This looks like a type error which should be diagnosed
5380 // elsewhere. Don't do anything here, to avoid an unhelpful
5381 // chain of error messages.
5386 if (type != NULL && !type->is_abstract())
5388 if ((type != left_type
5389 && !Complex_expression::check_constant(left_real, left_imag,
5391 || (type != right_type
5392 && !Complex_expression::check_constant(right_real, right_imag,
5394 || !Complex_expression::check_constant(real, imag, type,
5397 mpfr_set_ui(real, 0, GMP_RNDN);
5398 mpfr_set_ui(imag, 0, GMP_RNDN);
5405 // Lower a binary expression. We have to evaluate constant
5406 // expressions now, in order to implement Go's unlimited precision
5410 Binary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
5412 Location location = this->location();
5413 Operator op = this->op_;
5414 Expression* left = this->left_;
5415 Expression* right = this->right_;
5417 const bool is_comparison = (op == OPERATOR_EQEQ
5418 || op == OPERATOR_NOTEQ
5419 || op == OPERATOR_LT
5420 || op == OPERATOR_LE
5421 || op == OPERATOR_GT
5422 || op == OPERATOR_GE);
5424 // Integer constant expressions.
5430 mpz_init(right_val);
5432 if (left->integer_constant_value(false, left_val, &left_type)
5433 && right->integer_constant_value(false, right_val, &right_type))
5435 Expression* ret = NULL;
5436 if (left_type != right_type
5437 && left_type != NULL
5438 && right_type != NULL
5439 && left_type->base() != right_type->base()
5440 && op != OPERATOR_LSHIFT
5441 && op != OPERATOR_RSHIFT)
5443 // May be a type error--let it be diagnosed later.
5445 else if (is_comparison)
5447 bool b = Binary_expression::compare_integer(op, left_val,
5449 ret = Expression::make_cast(Type::lookup_bool_type(),
5450 Expression::make_boolean(b, location),
5458 if (Binary_expression::eval_integer(op, left_type, left_val,
5459 right_type, right_val,
5462 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
5464 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5466 else if (left_type == NULL)
5468 else if (right_type == NULL)
5470 else if (!left_type->is_abstract()
5471 && left_type->named_type() != NULL)
5473 else if (!right_type->is_abstract()
5474 && right_type->named_type() != NULL)
5476 else if (!left_type->is_abstract())
5478 else if (!right_type->is_abstract())
5480 else if (left_type->float_type() != NULL)
5482 else if (right_type->float_type() != NULL)
5484 else if (left_type->complex_type() != NULL)
5486 else if (right_type->complex_type() != NULL)
5490 ret = Expression::make_integer(&val, type, location);
5498 mpz_clear(right_val);
5499 mpz_clear(left_val);
5503 mpz_clear(right_val);
5504 mpz_clear(left_val);
5507 // Floating point constant expressions.
5510 mpfr_init(left_val);
5513 mpfr_init(right_val);
5515 if (left->float_constant_value(left_val, &left_type)
5516 && right->float_constant_value(right_val, &right_type))
5518 Expression* ret = NULL;
5519 if (left_type != right_type
5520 && left_type != NULL
5521 && right_type != NULL
5522 && left_type->base() != right_type->base()
5523 && op != OPERATOR_LSHIFT
5524 && op != OPERATOR_RSHIFT)
5526 // May be a type error--let it be diagnosed later.
5528 else if (is_comparison)
5530 bool b = Binary_expression::compare_float(op,
5534 left_val, right_val);
5535 ret = Expression::make_boolean(b, location);
5542 if (Binary_expression::eval_float(op, left_type, left_val,
5543 right_type, right_val, val,
5546 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5547 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5549 if (left_type == NULL)
5551 else if (right_type == NULL)
5553 else if (!left_type->is_abstract()
5554 && left_type->named_type() != NULL)
5556 else if (!right_type->is_abstract()
5557 && right_type->named_type() != NULL)
5559 else if (!left_type->is_abstract())
5561 else if (!right_type->is_abstract())
5563 else if (left_type->float_type() != NULL)
5565 else if (right_type->float_type() != NULL)
5569 ret = Expression::make_float(&val, type, location);
5577 mpfr_clear(right_val);
5578 mpfr_clear(left_val);
5582 mpfr_clear(right_val);
5583 mpfr_clear(left_val);
5586 // Complex constant expressions.
5590 mpfr_init(left_real);
5591 mpfr_init(left_imag);
5596 mpfr_init(right_real);
5597 mpfr_init(right_imag);
5600 if (left->complex_constant_value(left_real, left_imag, &left_type)
5601 && right->complex_constant_value(right_real, right_imag, &right_type))
5603 Expression* ret = NULL;
5604 if (left_type != right_type
5605 && left_type != NULL
5606 && right_type != NULL
5607 && left_type->base() != right_type->base())
5609 // May be a type error--let it be diagnosed later.
5611 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5613 bool b = Binary_expression::compare_complex(op,
5621 ret = Expression::make_boolean(b, location);
5630 if (Binary_expression::eval_complex(op, left_type,
5631 left_real, left_imag,
5633 right_real, right_imag,
5637 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5638 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5640 if (left_type == NULL)
5642 else if (right_type == NULL)
5644 else if (!left_type->is_abstract()
5645 && left_type->named_type() != NULL)
5647 else if (!right_type->is_abstract()
5648 && right_type->named_type() != NULL)
5650 else if (!left_type->is_abstract())
5652 else if (!right_type->is_abstract())
5654 else if (left_type->complex_type() != NULL)
5656 else if (right_type->complex_type() != NULL)
5660 ret = Expression::make_complex(&real, &imag, type,
5669 mpfr_clear(left_real);
5670 mpfr_clear(left_imag);
5671 mpfr_clear(right_real);
5672 mpfr_clear(right_imag);
5677 mpfr_clear(left_real);
5678 mpfr_clear(left_imag);
5679 mpfr_clear(right_real);
5680 mpfr_clear(right_imag);
5683 // String constant expressions.
5684 if (op == OPERATOR_PLUS
5685 && left->type()->is_string_type()
5686 && right->type()->is_string_type())
5688 std::string left_string;
5689 std::string right_string;
5690 if (left->string_constant_value(&left_string)
5691 && right->string_constant_value(&right_string))
5692 return Expression::make_string(left_string + right_string, location);
5695 // Special case for shift of a floating point constant.
5696 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5699 mpfr_init(left_val);
5702 mpz_init(right_val);
5704 if (left->float_constant_value(left_val, &left_type)
5705 && right->integer_constant_value(false, right_val, &right_type)
5706 && mpfr_integer_p(left_val)
5707 && (left_type == NULL
5708 || left_type->is_abstract()
5709 || left_type->integer_type() != NULL))
5713 mpfr_get_z(left_int, left_val, GMP_RNDN);
5718 Expression* ret = NULL;
5719 if (Binary_expression::eval_integer(op, left_type, left_int,
5720 right_type, right_val,
5722 ret = Expression::make_integer(&val, left_type, location);
5724 mpz_clear(left_int);
5729 mpfr_clear(left_val);
5730 mpz_clear(right_val);
5735 mpfr_clear(left_val);
5736 mpz_clear(right_val);
5742 // Return the integer constant value, if it has one.
5745 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5751 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5754 mpz_clear(left_val);
5759 mpz_init(right_val);
5761 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5764 mpz_clear(right_val);
5765 mpz_clear(left_val);
5770 if (left_type != right_type
5771 && left_type != NULL
5772 && right_type != NULL
5773 && left_type->base() != right_type->base()
5774 && this->op_ != OPERATOR_RSHIFT
5775 && this->op_ != OPERATOR_LSHIFT)
5778 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5779 right_type, right_val,
5780 this->location(), val);
5782 mpz_clear(right_val);
5783 mpz_clear(left_val);
5791 // Return the floating point constant value, if it has one.
5794 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5797 mpfr_init(left_val);
5799 if (!this->left_->float_constant_value(left_val, &left_type))
5801 mpfr_clear(left_val);
5806 mpfr_init(right_val);
5808 if (!this->right_->float_constant_value(right_val, &right_type))
5810 mpfr_clear(right_val);
5811 mpfr_clear(left_val);
5816 if (left_type != right_type
5817 && left_type != NULL
5818 && right_type != NULL
5819 && left_type->base() != right_type->base())
5822 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5823 right_type, right_val,
5824 val, this->location());
5826 mpfr_clear(left_val);
5827 mpfr_clear(right_val);
5835 // Return the complex constant value, if it has one.
5838 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5843 mpfr_init(left_real);
5844 mpfr_init(left_imag);
5846 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5848 mpfr_clear(left_real);
5849 mpfr_clear(left_imag);
5855 mpfr_init(right_real);
5856 mpfr_init(right_imag);
5858 if (!this->right_->complex_constant_value(right_real, right_imag,
5861 mpfr_clear(left_real);
5862 mpfr_clear(left_imag);
5863 mpfr_clear(right_real);
5864 mpfr_clear(right_imag);
5869 if (left_type != right_type
5870 && left_type != NULL
5871 && right_type != NULL
5872 && left_type->base() != right_type->base())
5875 ret = Binary_expression::eval_complex(this->op_, left_type,
5876 left_real, left_imag,
5878 right_real, right_imag,
5881 mpfr_clear(left_real);
5882 mpfr_clear(left_imag);
5883 mpfr_clear(right_real);
5884 mpfr_clear(right_imag);
5892 // Note that the value is being discarded.
5895 Binary_expression::do_discarding_value()
5897 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5898 this->right_->discarding_value();
5900 this->unused_value_error();
5906 Binary_expression::do_type()
5908 if (this->classification() == EXPRESSION_ERROR)
5909 return Type::make_error_type();
5914 case OPERATOR_ANDAND:
5916 case OPERATOR_NOTEQ:
5921 return Type::lookup_bool_type();
5924 case OPERATOR_MINUS:
5931 case OPERATOR_BITCLEAR:
5933 Type* left_type = this->left_->type();
5934 Type* right_type = this->right_->type();
5935 if (left_type->is_error())
5937 else if (right_type->is_error())
5939 else if (!Type::are_compatible_for_binop(left_type, right_type))
5941 this->report_error(_("incompatible types in binary expression"));
5942 return Type::make_error_type();
5944 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
5946 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5948 else if (!left_type->is_abstract())
5950 else if (!right_type->is_abstract())
5952 else if (left_type->complex_type() != NULL)
5954 else if (right_type->complex_type() != NULL)
5956 else if (left_type->float_type() != NULL)
5958 else if (right_type->float_type() != NULL)
5964 case OPERATOR_LSHIFT:
5965 case OPERATOR_RSHIFT:
5966 return this->left_->type();
5973 // Set type for a binary expression.
5976 Binary_expression::do_determine_type(const Type_context* context)
5978 Type* tleft = this->left_->type();
5979 Type* tright = this->right_->type();
5981 // Both sides should have the same type, except for the shift
5982 // operations. For a comparison, we should ignore the incoming
5985 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5986 || this->op_ == OPERATOR_RSHIFT);
5988 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5989 || this->op_ == OPERATOR_NOTEQ
5990 || this->op_ == OPERATOR_LT
5991 || this->op_ == OPERATOR_LE
5992 || this->op_ == OPERATOR_GT
5993 || this->op_ == OPERATOR_GE);
5995 Type_context subcontext(*context);
5999 // In a comparison, the context does not determine the types of
6001 subcontext.type = NULL;
6004 // Set the context for the left hand operand.
6007 // The right hand operand of a shift plays no role in
6008 // determining the type of the left hand operand.
6010 else if (!tleft->is_abstract())
6011 subcontext.type = tleft;
6012 else if (!tright->is_abstract())
6013 subcontext.type = tright;
6014 else if (subcontext.type == NULL)
6016 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
6017 || (tleft->float_type() != NULL && tright->float_type() != NULL)
6018 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
6020 // Both sides have an abstract integer, abstract float, or
6021 // abstract complex type. Just let CONTEXT determine
6022 // whether they may remain abstract or not.
6024 else if (tleft->complex_type() != NULL)
6025 subcontext.type = tleft;
6026 else if (tright->complex_type() != NULL)
6027 subcontext.type = tright;
6028 else if (tleft->float_type() != NULL)
6029 subcontext.type = tleft;
6030 else if (tright->float_type() != NULL)
6031 subcontext.type = tright;
6033 subcontext.type = tleft;
6035 if (subcontext.type != NULL && !context->may_be_abstract)
6036 subcontext.type = subcontext.type->make_non_abstract_type();
6039 this->left_->determine_type(&subcontext);
6043 // We may have inherited an unusable type for the shift operand.
6044 // Give a useful error if that happened.
6045 if (tleft->is_abstract()
6046 && subcontext.type != NULL
6047 && (this->left_->type()->integer_type() == NULL
6048 || (subcontext.type->integer_type() == NULL
6049 && subcontext.type->float_type() == NULL
6050 && subcontext.type->complex_type() == NULL)))
6051 this->report_error(("invalid context-determined non-integer type "
6052 "for shift operand"));
6054 // The context for the right hand operand is the same as for the
6055 // left hand operand, except for a shift operator.
6056 subcontext.type = Type::lookup_integer_type("uint");
6057 subcontext.may_be_abstract = false;
6060 this->right_->determine_type(&subcontext);
6063 // Report an error if the binary operator OP does not support TYPE.
6064 // Return whether the operation is OK. This should not be used for
6068 Binary_expression::check_operator_type(Operator op, Type* type,
6074 case OPERATOR_ANDAND:
6075 if (!type->is_boolean_type())
6077 error_at(location, "expected boolean type");
6083 case OPERATOR_NOTEQ:
6084 if (type->integer_type() == NULL
6085 && type->float_type() == NULL
6086 && type->complex_type() == NULL
6087 && !type->is_string_type()
6088 && type->points_to() == NULL
6089 && !type->is_nil_type()
6090 && !type->is_boolean_type()
6091 && type->interface_type() == NULL
6092 && (type->array_type() == NULL
6093 || type->array_type()->length() != NULL)
6094 && type->map_type() == NULL
6095 && type->channel_type() == NULL
6096 && type->function_type() == NULL)
6099 ("expected integer, floating, complex, string, pointer, "
6100 "boolean, interface, slice, map, channel, "
6101 "or function type"));
6110 if (type->integer_type() == NULL
6111 && type->float_type() == NULL
6112 && !type->is_string_type())
6114 error_at(location, "expected integer, floating, or string type");
6120 case OPERATOR_PLUSEQ:
6121 if (type->integer_type() == NULL
6122 && type->float_type() == NULL
6123 && type->complex_type() == NULL
6124 && !type->is_string_type())
6127 "expected integer, floating, complex, or string type");
6132 case OPERATOR_MINUS:
6133 case OPERATOR_MINUSEQ:
6135 case OPERATOR_MULTEQ:
6137 case OPERATOR_DIVEQ:
6138 if (type->integer_type() == NULL
6139 && type->float_type() == NULL
6140 && type->complex_type() == NULL)
6142 error_at(location, "expected integer, floating, or complex type");
6148 case OPERATOR_MODEQ:
6152 case OPERATOR_ANDEQ:
6154 case OPERATOR_XOREQ:
6155 case OPERATOR_BITCLEAR:
6156 case OPERATOR_BITCLEAREQ:
6157 if (type->integer_type() == NULL)
6159 error_at(location, "expected integer type");
6174 Binary_expression::do_check_types(Gogo*)
6176 if (this->classification() == EXPRESSION_ERROR)
6179 Type* left_type = this->left_->type();
6180 Type* right_type = this->right_->type();
6181 if (left_type->is_error() || right_type->is_error())
6183 this->set_is_error();
6187 if (this->op_ == OPERATOR_EQEQ
6188 || this->op_ == OPERATOR_NOTEQ
6189 || this->op_ == OPERATOR_LT
6190 || this->op_ == OPERATOR_LE
6191 || this->op_ == OPERATOR_GT
6192 || this->op_ == OPERATOR_GE)
6194 if (!Type::are_assignable(left_type, right_type, NULL)
6195 && !Type::are_assignable(right_type, left_type, NULL))
6197 this->report_error(_("incompatible types in binary expression"));
6200 if (!Binary_expression::check_operator_type(this->op_, left_type,
6202 || !Binary_expression::check_operator_type(this->op_, right_type,
6205 this->set_is_error();
6209 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
6211 if (!Type::are_compatible_for_binop(left_type, right_type))
6213 this->report_error(_("incompatible types in binary expression"));
6216 if (!Binary_expression::check_operator_type(this->op_, left_type,
6219 this->set_is_error();
6225 if (left_type->integer_type() == NULL)
6226 this->report_error(_("shift of non-integer operand"));
6228 if (!right_type->is_abstract()
6229 && (right_type->integer_type() == NULL
6230 || !right_type->integer_type()->is_unsigned()))
6231 this->report_error(_("shift count not unsigned integer"));
6237 if (this->right_->integer_constant_value(true, val, &type))
6239 if (mpz_sgn(val) < 0)
6241 this->report_error(_("negative shift count"));
6243 Location rloc = this->right_->location();
6244 this->right_ = Expression::make_integer(&val, right_type,
6253 // Get a tree for a binary expression.
6256 Binary_expression::do_get_tree(Translate_context* context)
6258 tree left = this->left_->get_tree(context);
6259 tree right = this->right_->get_tree(context);
6261 if (left == error_mark_node || right == error_mark_node)
6262 return error_mark_node;
6264 enum tree_code code;
6265 bool use_left_type = true;
6266 bool is_shift_op = false;
6270 case OPERATOR_NOTEQ:
6275 return Expression::comparison_tree(context, this->op_,
6276 this->left_->type(), left,
6277 this->right_->type(), right,
6281 code = TRUTH_ORIF_EXPR;
6282 use_left_type = false;
6284 case OPERATOR_ANDAND:
6285 code = TRUTH_ANDIF_EXPR;
6286 use_left_type = false;
6291 case OPERATOR_MINUS:
6295 code = BIT_IOR_EXPR;
6298 code = BIT_XOR_EXPR;
6305 Type *t = this->left_->type();
6306 if (t->float_type() != NULL || t->complex_type() != NULL)
6309 code = TRUNC_DIV_EXPR;
6313 code = TRUNC_MOD_EXPR;
6315 case OPERATOR_LSHIFT:
6319 case OPERATOR_RSHIFT:
6324 code = BIT_AND_EXPR;
6326 case OPERATOR_BITCLEAR:
6327 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
6328 code = BIT_AND_EXPR;
6334 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
6336 if (this->left_->type()->is_string_type())
6338 go_assert(this->op_ == OPERATOR_PLUS);
6339 Type* st = Type::make_string_type();
6340 tree string_type = type_to_tree(st->get_backend(context->gogo()));
6341 static tree string_plus_decl;
6342 return Gogo::call_builtin(&string_plus_decl,
6353 tree compute_type = excess_precision_type(type);
6354 if (compute_type != NULL_TREE)
6356 left = ::convert(compute_type, left);
6357 right = ::convert(compute_type, right);
6360 tree eval_saved = NULL_TREE;
6363 // Make sure the values are evaluated.
6364 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
6366 left = save_expr(left);
6369 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
6371 right = save_expr(right);
6372 if (eval_saved == NULL_TREE)
6375 eval_saved = fold_build2_loc(this->location().gcc_location(),
6377 void_type_node, eval_saved, right);
6381 tree ret = fold_build2_loc(this->location().gcc_location(),
6383 compute_type != NULL_TREE ? compute_type : type,
6386 if (compute_type != NULL_TREE)
6387 ret = ::convert(type, ret);
6389 // In Go, a shift larger than the size of the type is well-defined.
6390 // This is not true in GENERIC, so we need to insert a conditional.
6393 go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
6394 go_assert(this->left_->type()->integer_type() != NULL);
6395 int bits = TYPE_PRECISION(TREE_TYPE(left));
6397 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
6398 build_int_cst_type(TREE_TYPE(right), bits));
6400 tree overflow_result = fold_convert_loc(this->location().gcc_location(),
6403 if (this->op_ == OPERATOR_RSHIFT
6404 && !this->left_->type()->integer_type()->is_unsigned())
6407 fold_build2_loc(this->location().gcc_location(), LT_EXPR,
6408 boolean_type_node, left,
6409 fold_convert_loc(this->location().gcc_location(),
6411 integer_zero_node));
6413 fold_build2_loc(this->location().gcc_location(),
6414 MINUS_EXPR, TREE_TYPE(left),
6415 fold_convert_loc(this->location().gcc_location(),
6418 fold_convert_loc(this->location().gcc_location(),
6422 fold_build3_loc(this->location().gcc_location(), COND_EXPR,
6423 TREE_TYPE(left), neg, neg_one,
6427 ret = fold_build3_loc(this->location().gcc_location(), COND_EXPR,
6428 TREE_TYPE(left), compare, ret, overflow_result);
6430 if (eval_saved != NULL_TREE)
6431 ret = fold_build2_loc(this->location().gcc_location(), COMPOUND_EXPR,
6432 TREE_TYPE(ret), eval_saved, ret);
6438 // Export a binary expression.
6441 Binary_expression::do_export(Export* exp) const
6443 exp->write_c_string("(");
6444 this->left_->export_expression(exp);
6448 exp->write_c_string(" || ");
6450 case OPERATOR_ANDAND:
6451 exp->write_c_string(" && ");
6454 exp->write_c_string(" == ");
6456 case OPERATOR_NOTEQ:
6457 exp->write_c_string(" != ");
6460 exp->write_c_string(" < ");
6463 exp->write_c_string(" <= ");
6466 exp->write_c_string(" > ");
6469 exp->write_c_string(" >= ");
6472 exp->write_c_string(" + ");
6474 case OPERATOR_MINUS:
6475 exp->write_c_string(" - ");
6478 exp->write_c_string(" | ");
6481 exp->write_c_string(" ^ ");
6484 exp->write_c_string(" * ");
6487 exp->write_c_string(" / ");
6490 exp->write_c_string(" % ");
6492 case OPERATOR_LSHIFT:
6493 exp->write_c_string(" << ");
6495 case OPERATOR_RSHIFT:
6496 exp->write_c_string(" >> ");
6499 exp->write_c_string(" & ");
6501 case OPERATOR_BITCLEAR:
6502 exp->write_c_string(" &^ ");
6507 this->right_->export_expression(exp);
6508 exp->write_c_string(")");
6511 // Import a binary expression.
6514 Binary_expression::do_import(Import* imp)
6516 imp->require_c_string("(");
6518 Expression* left = Expression::import_expression(imp);
6521 if (imp->match_c_string(" || "))
6526 else if (imp->match_c_string(" && "))
6528 op = OPERATOR_ANDAND;
6531 else if (imp->match_c_string(" == "))
6536 else if (imp->match_c_string(" != "))
6538 op = OPERATOR_NOTEQ;
6541 else if (imp->match_c_string(" < "))
6546 else if (imp->match_c_string(" <= "))
6551 else if (imp->match_c_string(" > "))
6556 else if (imp->match_c_string(" >= "))
6561 else if (imp->match_c_string(" + "))
6566 else if (imp->match_c_string(" - "))
6568 op = OPERATOR_MINUS;
6571 else if (imp->match_c_string(" | "))
6576 else if (imp->match_c_string(" ^ "))
6581 else if (imp->match_c_string(" * "))
6586 else if (imp->match_c_string(" / "))
6591 else if (imp->match_c_string(" % "))
6596 else if (imp->match_c_string(" << "))
6598 op = OPERATOR_LSHIFT;
6601 else if (imp->match_c_string(" >> "))
6603 op = OPERATOR_RSHIFT;
6606 else if (imp->match_c_string(" & "))
6611 else if (imp->match_c_string(" &^ "))
6613 op = OPERATOR_BITCLEAR;
6618 error_at(imp->location(), "unrecognized binary operator");
6619 return Expression::make_error(imp->location());
6622 Expression* right = Expression::import_expression(imp);
6624 imp->require_c_string(")");
6626 return Expression::make_binary(op, left, right, imp->location());
6629 // Dump ast representation of a binary expression.
6632 Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
6634 ast_dump_context->ostream() << "(";
6635 ast_dump_context->dump_expression(this->left_);
6636 ast_dump_context->ostream() << " ";
6637 ast_dump_context->dump_operator(this->op_);
6638 ast_dump_context->ostream() << " ";
6639 ast_dump_context->dump_expression(this->right_);
6640 ast_dump_context->ostream() << ") ";
6643 // Make a binary expression.
6646 Expression::make_binary(Operator op, Expression* left, Expression* right,
6649 return new Binary_expression(op, left, right, location);
6652 // Implement a comparison.
6655 Expression::comparison_tree(Translate_context* context, Operator op,
6656 Type* left_type, tree left_tree,
6657 Type* right_type, tree right_tree,
6660 enum tree_code code;
6666 case OPERATOR_NOTEQ:
6685 if (left_type->is_string_type() && right_type->is_string_type())
6687 Type* st = Type::make_string_type();
6688 tree string_type = type_to_tree(st->get_backend(context->gogo()));
6689 static tree string_compare_decl;
6690 left_tree = Gogo::call_builtin(&string_compare_decl,
6699 right_tree = build_int_cst_type(integer_type_node, 0);
6701 else if ((left_type->interface_type() != NULL
6702 && right_type->interface_type() == NULL
6703 && !right_type->is_nil_type())
6704 || (left_type->interface_type() == NULL
6705 && !left_type->is_nil_type()
6706 && right_type->interface_type() != NULL))
6708 // Comparing an interface value to a non-interface value.
6709 if (left_type->interface_type() == NULL)
6711 std::swap(left_type, right_type);
6712 std::swap(left_tree, right_tree);
6715 // The right operand is not an interface. We need to take its
6716 // address if it is not a pointer.
6719 if (right_type->points_to() != NULL)
6721 make_tmp = NULL_TREE;
6724 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
6726 make_tmp = NULL_TREE;
6727 arg = build_fold_addr_expr_loc(location.gcc_location(), right_tree);
6728 if (DECL_P(right_tree))
6729 TREE_ADDRESSABLE(right_tree) = 1;
6733 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6734 get_name(right_tree));
6735 DECL_IGNORED_P(tmp) = 0;
6736 DECL_INITIAL(tmp) = right_tree;
6737 TREE_ADDRESSABLE(tmp) = 1;
6738 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6739 SET_EXPR_LOCATION(make_tmp, location.gcc_location());
6740 arg = build_fold_addr_expr_loc(location.gcc_location(), tmp);
6742 arg = fold_convert_loc(location.gcc_location(), ptr_type_node, arg);
6744 tree descriptor = right_type->type_descriptor_pointer(context->gogo(),
6747 if (left_type->interface_type()->is_empty())
6749 static tree empty_interface_value_compare_decl;
6750 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6752 "__go_empty_interface_value_compare",
6755 TREE_TYPE(left_tree),
6757 TREE_TYPE(descriptor),
6761 if (left_tree == error_mark_node)
6762 return error_mark_node;
6763 // This can panic if the type is not comparable.
6764 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6768 static tree interface_value_compare_decl;
6769 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6771 "__go_interface_value_compare",
6774 TREE_TYPE(left_tree),
6776 TREE_TYPE(descriptor),
6780 if (left_tree == error_mark_node)
6781 return error_mark_node;
6782 // This can panic if the type is not comparable.
6783 TREE_NOTHROW(interface_value_compare_decl) = 0;
6785 right_tree = build_int_cst_type(integer_type_node, 0);
6787 if (make_tmp != NULL_TREE)
6788 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6791 else if (left_type->interface_type() != NULL
6792 && right_type->interface_type() != NULL)
6794 if (left_type->interface_type()->is_empty()
6795 && right_type->interface_type()->is_empty())
6797 static tree empty_interface_compare_decl;
6798 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6800 "__go_empty_interface_compare",
6803 TREE_TYPE(left_tree),
6805 TREE_TYPE(right_tree),
6807 if (left_tree == error_mark_node)
6808 return error_mark_node;
6809 // This can panic if the type is uncomparable.
6810 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6812 else if (!left_type->interface_type()->is_empty()
6813 && !right_type->interface_type()->is_empty())
6815 static tree interface_compare_decl;
6816 left_tree = Gogo::call_builtin(&interface_compare_decl,
6818 "__go_interface_compare",
6821 TREE_TYPE(left_tree),
6823 TREE_TYPE(right_tree),
6825 if (left_tree == error_mark_node)
6826 return error_mark_node;
6827 // This can panic if the type is uncomparable.
6828 TREE_NOTHROW(interface_compare_decl) = 0;
6832 if (left_type->interface_type()->is_empty())
6834 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6835 std::swap(left_type, right_type);
6836 std::swap(left_tree, right_tree);
6838 go_assert(!left_type->interface_type()->is_empty());
6839 go_assert(right_type->interface_type()->is_empty());
6840 static tree interface_empty_compare_decl;
6841 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
6843 "__go_interface_empty_compare",
6846 TREE_TYPE(left_tree),
6848 TREE_TYPE(right_tree),
6850 if (left_tree == error_mark_node)
6851 return error_mark_node;
6852 // This can panic if the type is uncomparable.
6853 TREE_NOTHROW(interface_empty_compare_decl) = 0;
6856 right_tree = build_int_cst_type(integer_type_node, 0);
6859 if (left_type->is_nil_type()
6860 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6862 std::swap(left_type, right_type);
6863 std::swap(left_tree, right_tree);
6866 if (right_type->is_nil_type())
6868 if (left_type->array_type() != NULL
6869 && left_type->array_type()->length() == NULL)
6871 Array_type* at = left_type->array_type();
6872 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6873 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6875 else if (left_type->interface_type() != NULL)
6877 // An interface is nil if the first field is nil.
6878 tree left_type_tree = TREE_TYPE(left_tree);
6879 go_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6880 tree field = TYPE_FIELDS(left_type_tree);
6881 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6883 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6887 go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6888 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6892 if (left_tree == error_mark_node || right_tree == error_mark_node)
6893 return error_mark_node;
6895 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6896 if (CAN_HAVE_LOCATION_P(ret))
6897 SET_EXPR_LOCATION(ret, location.gcc_location());
6901 // Class Bound_method_expression.
6906 Bound_method_expression::do_traverse(Traverse* traverse)
6908 return Expression::traverse(&this->expr_, traverse);
6911 // Return the type of a bound method expression. The type of this
6912 // object is really the type of the method with no receiver. We
6913 // should be able to get away with just returning the type of the
6917 Bound_method_expression::do_type()
6919 if (this->method_->is_function())
6920 return this->method_->func_value()->type();
6921 else if (this->method_->is_function_declaration())
6922 return this->method_->func_declaration_value()->type();
6924 return Type::make_error_type();
6927 // Determine the types of a method expression.
6930 Bound_method_expression::do_determine_type(const Type_context*)
6932 Function_type* fntype = this->type()->function_type();
6933 if (fntype == NULL || !fntype->is_method())
6934 this->expr_->determine_type_no_context();
6937 Type_context subcontext(fntype->receiver()->type(), false);
6938 this->expr_->determine_type(&subcontext);
6942 // Check the types of a method expression.
6945 Bound_method_expression::do_check_types(Gogo*)
6947 if (!this->method_->is_function()
6948 && !this->method_->is_function_declaration())
6949 this->report_error(_("object is not a method"));
6952 Type* rtype = this->type()->function_type()->receiver()->type()->deref();
6953 Type* etype = (this->expr_type_ != NULL
6955 : this->expr_->type());
6956 etype = etype->deref();
6957 if (!Type::are_identical(rtype, etype, true, NULL))
6958 this->report_error(_("method type does not match object type"));
6962 // Get the tree for a method expression. There is no standard tree
6963 // representation for this. The only places it may currently be used
6964 // are in a Call_expression or a Go_statement, which will take it
6965 // apart directly. So this has nothing to do at present.
6968 Bound_method_expression::do_get_tree(Translate_context*)
6970 error_at(this->location(), "reference to method other than calling it");
6971 return error_mark_node;
6974 // Dump ast representation of a bound method expression.
6977 Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
6980 if (this->expr_type_ != NULL)
6981 ast_dump_context->ostream() << "(";
6982 ast_dump_context->dump_expression(this->expr_);
6983 if (this->expr_type_ != NULL)
6985 ast_dump_context->ostream() << ":";
6986 ast_dump_context->dump_type(this->expr_type_);
6987 ast_dump_context->ostream() << ")";
6990 ast_dump_context->ostream() << "." << this->method_->name();
6993 // Make a method expression.
6995 Bound_method_expression*
6996 Expression::make_bound_method(Expression* expr, Named_object* method,
6999 return new Bound_method_expression(expr, method, location);
7002 // Class Builtin_call_expression. This is used for a call to a
7003 // builtin function.
7005 class Builtin_call_expression : public Call_expression
7008 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
7009 bool is_varargs, Location location);
7012 // This overrides Call_expression::do_lower.
7014 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
7017 do_is_constant() const;
7020 do_integer_constant_value(bool, mpz_t, Type**) const;
7023 do_float_constant_value(mpfr_t, Type**) const;
7026 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
7029 do_discarding_value();
7035 do_determine_type(const Type_context*);
7038 do_check_types(Gogo*);
7043 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
7044 this->args()->copy(),
7050 do_get_tree(Translate_context*);
7053 do_export(Export*) const;
7056 do_is_recover_call() const;
7059 do_set_recover_arg(Expression*);
7062 // The builtin functions.
7063 enum Builtin_function_code
7067 // Predeclared builtin functions.
7084 // Builtin functions from the unsafe package.
7097 real_imag_type(Type*);
7100 complex_type(Type*);
7106 check_int_value(Expression*);
7108 // A pointer back to the general IR structure. This avoids a global
7109 // variable, or passing it around everywhere.
7111 // The builtin function being called.
7112 Builtin_function_code code_;
7113 // Used to stop endless loops when the length of an array uses len
7114 // or cap of the array itself.
7118 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
7120 Expression_list* args,
7123 : Call_expression(fn, args, is_varargs, location),
7124 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
7126 Func_expression* fnexp = this->fn()->func_expression();
7127 go_assert(fnexp != NULL);
7128 const std::string& name(fnexp->named_object()->name());
7129 if (name == "append")
7130 this->code_ = BUILTIN_APPEND;
7131 else if (name == "cap")
7132 this->code_ = BUILTIN_CAP;
7133 else if (name == "close")
7134 this->code_ = BUILTIN_CLOSE;
7135 else if (name == "complex")
7136 this->code_ = BUILTIN_COMPLEX;
7137 else if (name == "copy")
7138 this->code_ = BUILTIN_COPY;
7139 else if (name == "delete")
7140 this->code_ = BUILTIN_DELETE;
7141 else if (name == "imag")
7142 this->code_ = BUILTIN_IMAG;
7143 else if (name == "len")
7144 this->code_ = BUILTIN_LEN;
7145 else if (name == "make")
7146 this->code_ = BUILTIN_MAKE;
7147 else if (name == "new")
7148 this->code_ = BUILTIN_NEW;
7149 else if (name == "panic")
7150 this->code_ = BUILTIN_PANIC;
7151 else if (name == "print")
7152 this->code_ = BUILTIN_PRINT;
7153 else if (name == "println")
7154 this->code_ = BUILTIN_PRINTLN;
7155 else if (name == "real")
7156 this->code_ = BUILTIN_REAL;
7157 else if (name == "recover")
7158 this->code_ = BUILTIN_RECOVER;
7159 else if (name == "Alignof")
7160 this->code_ = BUILTIN_ALIGNOF;
7161 else if (name == "Offsetof")
7162 this->code_ = BUILTIN_OFFSETOF;
7163 else if (name == "Sizeof")
7164 this->code_ = BUILTIN_SIZEOF;
7169 // Return whether this is a call to recover. This is a virtual
7170 // function called from the parent class.
7173 Builtin_call_expression::do_is_recover_call() const
7175 if (this->classification() == EXPRESSION_ERROR)
7177 return this->code_ == BUILTIN_RECOVER;
7180 // Set the argument for a call to recover.
7183 Builtin_call_expression::do_set_recover_arg(Expression* arg)
7185 const Expression_list* args = this->args();
7186 go_assert(args == NULL || args->empty());
7187 Expression_list* new_args = new Expression_list();
7188 new_args->push_back(arg);
7189 this->set_args(new_args);
7192 // A traversal class which looks for a call expression.
7194 class Find_call_expression : public Traverse
7197 Find_call_expression()
7198 : Traverse(traverse_expressions),
7203 expression(Expression**);
7207 { return this->found_; }
7214 Find_call_expression::expression(Expression** pexpr)
7216 if ((*pexpr)->call_expression() != NULL)
7218 this->found_ = true;
7219 return TRAVERSE_EXIT;
7221 return TRAVERSE_CONTINUE;
7224 // Lower a builtin call expression. This turns new and make into
7225 // specific expressions. We also convert to a constant if we can.
7228 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function,
7229 Statement_inserter* inserter, int)
7231 if (this->classification() == EXPRESSION_ERROR)
7234 Location loc = this->location();
7236 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
7238 this->report_error(_("invalid use of %<...%> with builtin function"));
7239 return Expression::make_error(loc);
7242 if (this->is_constant())
7244 // We can only lower len and cap if there are no function calls
7245 // in the arguments. Otherwise we have to make the call.
7246 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
7248 Expression* arg = this->one_arg();
7249 if (!arg->is_constant())
7251 Find_call_expression find_call;
7252 Expression::traverse(&arg, &find_call);
7253 if (find_call.found())
7261 if (this->integer_constant_value(true, ival, &type))
7263 Expression* ret = Expression::make_integer(&ival, type, loc);
7271 if (this->float_constant_value(rval, &type))
7273 Expression* ret = Expression::make_float(&rval, type, loc);
7280 if (this->complex_constant_value(rval, imag, &type))
7282 Expression* ret = Expression::make_complex(&rval, &imag, type, loc);
7291 switch (this->code_)
7298 const Expression_list* args = this->args();
7299 if (args == NULL || args->size() < 1)
7300 this->report_error(_("not enough arguments"));
7301 else if (args->size() > 1)
7302 this->report_error(_("too many arguments"));
7305 Expression* arg = args->front();
7306 if (!arg->is_type_expression())
7308 error_at(arg->location(), "expected type");
7309 this->set_is_error();
7312 return Expression::make_allocation(arg->type(), loc);
7318 return this->lower_make();
7320 case BUILTIN_RECOVER:
7321 if (function != NULL)
7322 function->func_value()->set_calls_recover();
7325 // Calling recover outside of a function always returns the
7326 // nil empty interface.
7327 Type* eface = Type::make_interface_type(NULL, loc);
7328 return Expression::make_cast(eface, Expression::make_nil(loc), loc);
7332 case BUILTIN_APPEND:
7334 // Lower the varargs.
7335 const Expression_list* args = this->args();
7336 if (args == NULL || args->empty())
7338 Type* slice_type = args->front()->type();
7339 if (!slice_type->is_slice_type())
7341 error_at(args->front()->location(), "argument 1 must be a slice");
7342 this->set_is_error();
7345 this->lower_varargs(gogo, function, inserter, slice_type, 2);
7349 case BUILTIN_DELETE:
7351 // Lower to a runtime function call.
7352 const Expression_list* args = this->args();
7353 if (args == NULL || args->size() < 2)
7354 this->report_error(_("not enough arguments"));
7355 else if (args->size() > 2)
7356 this->report_error(_("too many arguments"));
7357 else if (args->front()->type()->map_type() == NULL)
7358 this->report_error(_("argument 1 must be a map"));
7361 // Since this function returns no value it must appear in
7362 // a statement by itself, so we don't have to worry about
7363 // order of evaluation of values around it. Evaluate the
7364 // map first to get order of evaluation right.
7365 Map_type* mt = args->front()->type()->map_type();
7366 Temporary_statement* map_temp =
7367 Statement::make_temporary(mt, args->front(), loc);
7368 inserter->insert(map_temp);
7370 Temporary_statement* key_temp =
7371 Statement::make_temporary(mt->key_type(), args->back(), loc);
7372 inserter->insert(key_temp);
7374 Expression* e1 = Expression::make_temporary_reference(map_temp,
7376 Expression* e2 = Expression::make_temporary_reference(key_temp,
7378 e2 = Expression::make_unary(OPERATOR_AND, e2, loc);
7379 return Runtime::make_call(Runtime::MAPDELETE, this->location(),
7389 // Lower a make expression.
7392 Builtin_call_expression::lower_make()
7394 Location loc = this->location();
7396 const Expression_list* args = this->args();
7397 if (args == NULL || args->size() < 1)
7399 this->report_error(_("not enough arguments"));
7400 return Expression::make_error(this->location());
7403 Expression_list::const_iterator parg = args->begin();
7405 Expression* first_arg = *parg;
7406 if (!first_arg->is_type_expression())
7408 error_at(first_arg->location(), "expected type");
7409 this->set_is_error();
7410 return Expression::make_error(this->location());
7412 Type* type = first_arg->type();
7414 bool is_slice = false;
7415 bool is_map = false;
7416 bool is_chan = false;
7417 if (type->is_slice_type())
7419 else if (type->map_type() != NULL)
7421 else if (type->channel_type() != NULL)
7425 this->report_error(_("invalid type for make function"));
7426 return Expression::make_error(this->location());
7430 Expression* len_arg;
7431 if (parg == args->end())
7435 this->report_error(_("length required when allocating a slice"));
7436 return Expression::make_error(this->location());
7440 mpz_init_set_ui(zval, 0);
7441 len_arg = Expression::make_integer(&zval, NULL, loc);
7447 if (!this->check_int_value(len_arg))
7449 this->report_error(_("bad size for make"));
7450 return Expression::make_error(this->location());
7455 Expression* cap_arg = NULL;
7456 if (is_slice && parg != args->end())
7459 if (!this->check_int_value(cap_arg))
7461 this->report_error(_("bad capacity when making slice"));
7462 return Expression::make_error(this->location());
7467 if (parg != args->end())
7469 this->report_error(_("too many arguments to make"));
7470 return Expression::make_error(this->location());
7473 Location type_loc = first_arg->location();
7474 Expression* type_arg;
7475 if (is_slice || is_chan)
7476 type_arg = Expression::make_type_descriptor(type, type_loc);
7478 type_arg = Expression::make_map_descriptor(type->map_type(), type_loc);
7485 if (cap_arg == NULL)
7486 call = Runtime::make_call(Runtime::MAKESLICE1, loc, 2, type_arg,
7489 call = Runtime::make_call(Runtime::MAKESLICE2, loc, 3, type_arg,
7493 call = Runtime::make_call(Runtime::MAKEMAP, loc, 2, type_arg, len_arg);
7495 call = Runtime::make_call(Runtime::MAKECHAN, loc, 2, type_arg, len_arg);
7499 return Expression::make_unsafe_cast(type, call, loc);
7502 // Return whether an expression has an integer value. Report an error
7503 // if not. This is used when handling calls to the predeclared make
7507 Builtin_call_expression::check_int_value(Expression* e)
7509 if (e->type()->integer_type() != NULL)
7512 // Check for a floating point constant with integer value.
7517 if (e->float_constant_value(fval, &dummy) && mpfr_integer_p(fval))
7524 mpfr_clear_overflow();
7525 mpfr_clear_erangeflag();
7526 mpfr_get_z(ival, fval, GMP_RNDN);
7527 if (!mpfr_overflow_p()
7528 && !mpfr_erangeflag_p()
7529 && mpz_sgn(ival) >= 0)
7531 Named_type* ntype = Type::lookup_integer_type("int");
7532 Integer_type* inttype = ntype->integer_type();
7534 mpz_init_set_ui(max, 1);
7535 mpz_mul_2exp(max, max, inttype->bits() - 1);
7536 ok = mpz_cmp(ival, max) < 0;
7553 // Return the type of the real or imag functions, given the type of
7554 // the argument. We need to map complex to float, complex64 to
7555 // float32, and complex128 to float64, so it has to be done by name.
7556 // This returns NULL if it can't figure out the type.
7559 Builtin_call_expression::real_imag_type(Type* arg_type)
7561 if (arg_type == NULL || arg_type->is_abstract())
7563 Named_type* nt = arg_type->named_type();
7566 while (nt->real_type()->named_type() != NULL)
7567 nt = nt->real_type()->named_type();
7568 if (nt->name() == "complex64")
7569 return Type::lookup_float_type("float32");
7570 else if (nt->name() == "complex128")
7571 return Type::lookup_float_type("float64");
7576 // Return the type of the complex function, given the type of one of the
7577 // argments. Like real_imag_type, we have to map by name.
7580 Builtin_call_expression::complex_type(Type* arg_type)
7582 if (arg_type == NULL || arg_type->is_abstract())
7584 Named_type* nt = arg_type->named_type();
7587 while (nt->real_type()->named_type() != NULL)
7588 nt = nt->real_type()->named_type();
7589 if (nt->name() == "float32")
7590 return Type::lookup_complex_type("complex64");
7591 else if (nt->name() == "float64")
7592 return Type::lookup_complex_type("complex128");
7597 // Return a single argument, or NULL if there isn't one.
7600 Builtin_call_expression::one_arg() const
7602 const Expression_list* args = this->args();
7603 if (args->size() != 1)
7605 return args->front();
7608 // Return whether this is constant: len of a string, or len or cap of
7609 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
7612 Builtin_call_expression::do_is_constant() const
7614 switch (this->code_)
7622 Expression* arg = this->one_arg();
7625 Type* arg_type = arg->type();
7627 if (arg_type->points_to() != NULL
7628 && arg_type->points_to()->array_type() != NULL
7629 && !arg_type->points_to()->is_slice_type())
7630 arg_type = arg_type->points_to();
7632 if (arg_type->array_type() != NULL
7633 && arg_type->array_type()->length() != NULL)
7636 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7639 bool ret = arg->is_constant();
7640 this->seen_ = false;
7646 case BUILTIN_SIZEOF:
7647 case BUILTIN_ALIGNOF:
7648 return this->one_arg() != NULL;
7650 case BUILTIN_OFFSETOF:
7652 Expression* arg = this->one_arg();
7655 return arg->field_reference_expression() != NULL;
7658 case BUILTIN_COMPLEX:
7660 const Expression_list* args = this->args();
7661 if (args != NULL && args->size() == 2)
7662 return args->front()->is_constant() && args->back()->is_constant();
7669 Expression* arg = this->one_arg();
7670 return arg != NULL && arg->is_constant();
7680 // Return an integer constant value if possible.
7683 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
7687 if (this->code_ == BUILTIN_LEN
7688 || this->code_ == BUILTIN_CAP)
7690 Expression* arg = this->one_arg();
7693 Type* arg_type = arg->type();
7695 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7698 if (arg->string_constant_value(&sval))
7700 mpz_set_ui(val, sval.length());
7701 *ptype = Type::lookup_integer_type("int");
7706 if (arg_type->points_to() != NULL
7707 && arg_type->points_to()->array_type() != NULL
7708 && !arg_type->points_to()->is_slice_type())
7709 arg_type = arg_type->points_to();
7711 if (arg_type->array_type() != NULL
7712 && arg_type->array_type()->length() != NULL)
7716 Expression* e = arg_type->array_type()->length();
7718 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
7719 this->seen_ = false;
7722 *ptype = Type::lookup_integer_type("int");
7727 else if (this->code_ == BUILTIN_SIZEOF
7728 || this->code_ == BUILTIN_ALIGNOF)
7730 Expression* arg = this->one_arg();
7733 Type* arg_type = arg->type();
7734 if (arg_type->is_error())
7736 if (arg_type->is_abstract())
7738 if (arg_type->named_type() != NULL)
7739 arg_type->named_type()->convert(this->gogo_);
7740 tree arg_type_tree = type_to_tree(arg_type->get_backend(this->gogo_));
7741 if (arg_type_tree == error_mark_node)
7743 unsigned long val_long;
7744 if (this->code_ == BUILTIN_SIZEOF)
7746 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
7747 go_assert(TREE_CODE(type_size) == INTEGER_CST);
7748 if (TREE_INT_CST_HIGH(type_size) != 0)
7750 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
7751 val_long = static_cast<unsigned long>(val_wide);
7752 if (val_long != val_wide)
7755 else if (this->code_ == BUILTIN_ALIGNOF)
7757 if (arg->field_reference_expression() == NULL)
7758 val_long = go_type_alignment(arg_type_tree);
7761 // Calling unsafe.Alignof(s.f) returns the alignment of
7762 // the type of f when it is used as a field in a struct.
7763 val_long = go_field_alignment(arg_type_tree);
7768 mpz_set_ui(val, val_long);
7772 else if (this->code_ == BUILTIN_OFFSETOF)
7774 Expression* arg = this->one_arg();
7777 Field_reference_expression* farg = arg->field_reference_expression();
7780 Expression* struct_expr = farg->expr();
7781 Type* st = struct_expr->type();
7782 if (st->struct_type() == NULL)
7784 if (st->named_type() != NULL)
7785 st->named_type()->convert(this->gogo_);
7786 tree struct_tree = type_to_tree(st->get_backend(this->gogo_));
7787 go_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
7788 tree field = TYPE_FIELDS(struct_tree);
7789 for (unsigned int index = farg->field_index(); index > 0; --index)
7791 field = DECL_CHAIN(field);
7792 go_assert(field != NULL_TREE);
7794 HOST_WIDE_INT offset_wide = int_byte_position (field);
7795 if (offset_wide < 0)
7797 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
7798 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
7800 mpz_set_ui(val, offset_long);
7806 // Return a floating point constant value if possible.
7809 Builtin_call_expression::do_float_constant_value(mpfr_t val,
7812 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
7814 Expression* arg = this->one_arg();
7825 if (arg->complex_constant_value(real, imag, &type))
7827 if (this->code_ == BUILTIN_REAL)
7828 mpfr_set(val, real, GMP_RNDN);
7830 mpfr_set(val, imag, GMP_RNDN);
7831 *ptype = Builtin_call_expression::real_imag_type(type);
7843 // Return a complex constant value if possible.
7846 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
7849 if (this->code_ == BUILTIN_COMPLEX)
7851 const Expression_list* args = this->args();
7852 if (args == NULL || args->size() != 2)
7858 if (!args->front()->float_constant_value(r, &rtype))
7869 if (args->back()->float_constant_value(i, &itype)
7870 && Type::are_identical(rtype, itype, false, NULL))
7872 mpfr_set(real, r, GMP_RNDN);
7873 mpfr_set(imag, i, GMP_RNDN);
7874 *ptype = Builtin_call_expression::complex_type(rtype);
7887 // Give an error if we are discarding the value of an expression which
7888 // should not normally be discarded. We don't give an error for
7889 // discarding the value of an ordinary function call, but we do for
7890 // builtin functions, purely for consistency with the gc compiler.
7893 Builtin_call_expression::do_discarding_value()
7895 switch (this->code_)
7897 case BUILTIN_INVALID:
7901 case BUILTIN_APPEND:
7903 case BUILTIN_COMPLEX:
7909 case BUILTIN_ALIGNOF:
7910 case BUILTIN_OFFSETOF:
7911 case BUILTIN_SIZEOF:
7912 this->unused_value_error();
7917 case BUILTIN_DELETE:
7920 case BUILTIN_PRINTLN:
7921 case BUILTIN_RECOVER:
7929 Builtin_call_expression::do_type()
7931 switch (this->code_)
7933 case BUILTIN_INVALID:
7940 const Expression_list* args = this->args();
7941 if (args == NULL || args->empty())
7942 return Type::make_error_type();
7943 return Type::make_pointer_type(args->front()->type());
7949 case BUILTIN_ALIGNOF:
7950 case BUILTIN_OFFSETOF:
7951 case BUILTIN_SIZEOF:
7952 return Type::lookup_integer_type("int");
7955 case BUILTIN_DELETE:
7958 case BUILTIN_PRINTLN:
7959 return Type::make_void_type();
7961 case BUILTIN_RECOVER:
7962 return Type::make_interface_type(NULL, Linemap::predeclared_location());
7964 case BUILTIN_APPEND:
7966 const Expression_list* args = this->args();
7967 if (args == NULL || args->empty())
7968 return Type::make_error_type();
7969 return args->front()->type();
7975 Expression* arg = this->one_arg();
7977 return Type::make_error_type();
7978 Type* t = arg->type();
7979 if (t->is_abstract())
7980 t = t->make_non_abstract_type();
7981 t = Builtin_call_expression::real_imag_type(t);
7983 t = Type::make_error_type();
7987 case BUILTIN_COMPLEX:
7989 const Expression_list* args = this->args();
7990 if (args == NULL || args->size() != 2)
7991 return Type::make_error_type();
7992 Type* t = args->front()->type();
7993 if (t->is_abstract())
7995 t = args->back()->type();
7996 if (t->is_abstract())
7997 t = t->make_non_abstract_type();
7999 t = Builtin_call_expression::complex_type(t);
8001 t = Type::make_error_type();
8007 // Determine the type.
8010 Builtin_call_expression::do_determine_type(const Type_context* context)
8012 if (!this->determining_types())
8015 this->fn()->determine_type_no_context();
8017 const Expression_list* args = this->args();
8020 Type* arg_type = NULL;
8021 switch (this->code_)
8024 case BUILTIN_PRINTLN:
8025 // Do not force a large integer constant to "int".
8031 arg_type = Builtin_call_expression::complex_type(context->type);
8035 case BUILTIN_COMPLEX:
8037 // For the complex function the type of one operand can
8038 // determine the type of the other, as in a binary expression.
8039 arg_type = Builtin_call_expression::real_imag_type(context->type);
8040 if (args != NULL && args->size() == 2)
8042 Type* t1 = args->front()->type();
8043 Type* t2 = args->front()->type();
8044 if (!t1->is_abstract())
8046 else if (!t2->is_abstract())
8060 for (Expression_list::const_iterator pa = args->begin();
8064 Type_context subcontext;
8065 subcontext.type = arg_type;
8069 // We want to print large constants, we so can't just
8070 // use the appropriate nonabstract type. Use uint64 for
8071 // an integer if we know it is nonnegative, otherwise
8072 // use int64 for a integer, otherwise use float64 for a
8073 // float or complex128 for a complex.
8074 Type* want_type = NULL;
8075 Type* atype = (*pa)->type();
8076 if (atype->is_abstract())
8078 if (atype->integer_type() != NULL)
8083 if (this->integer_constant_value(true, val, &dummy)
8084 && mpz_sgn(val) >= 0)
8085 want_type = Type::lookup_integer_type("uint64");
8087 want_type = Type::lookup_integer_type("int64");
8090 else if (atype->float_type() != NULL)
8091 want_type = Type::lookup_float_type("float64");
8092 else if (atype->complex_type() != NULL)
8093 want_type = Type::lookup_complex_type("complex128");
8094 else if (atype->is_abstract_string_type())
8095 want_type = Type::lookup_string_type();
8096 else if (atype->is_abstract_boolean_type())
8097 want_type = Type::lookup_bool_type();
8100 subcontext.type = want_type;
8104 (*pa)->determine_type(&subcontext);
8109 // If there is exactly one argument, return true. Otherwise give an
8110 // error message and return false.
8113 Builtin_call_expression::check_one_arg()
8115 const Expression_list* args = this->args();
8116 if (args == NULL || args->size() < 1)
8118 this->report_error(_("not enough arguments"));
8121 else if (args->size() > 1)
8123 this->report_error(_("too many arguments"));
8126 if (args->front()->is_error_expression()
8127 || args->front()->type()->is_error())
8129 this->set_is_error();
8135 // Check argument types for a builtin function.
8138 Builtin_call_expression::do_check_types(Gogo*)
8140 switch (this->code_)
8142 case BUILTIN_INVALID:
8150 // The single argument may be either a string or an array or a
8151 // map or a channel, or a pointer to a closed array.
8152 if (this->check_one_arg())
8154 Type* arg_type = this->one_arg()->type();
8155 if (arg_type->points_to() != NULL
8156 && arg_type->points_to()->array_type() != NULL
8157 && !arg_type->points_to()->is_slice_type())
8158 arg_type = arg_type->points_to();
8159 if (this->code_ == BUILTIN_CAP)
8161 if (!arg_type->is_error()
8162 && arg_type->array_type() == NULL
8163 && arg_type->channel_type() == NULL)
8164 this->report_error(_("argument must be array or slice "
8169 if (!arg_type->is_error()
8170 && !arg_type->is_string_type()
8171 && arg_type->array_type() == NULL
8172 && arg_type->map_type() == NULL
8173 && arg_type->channel_type() == NULL)
8174 this->report_error(_("argument must be string or "
8175 "array or slice or map or channel"));
8182 case BUILTIN_PRINTLN:
8184 const Expression_list* args = this->args();
8187 if (this->code_ == BUILTIN_PRINT)
8188 warning_at(this->location(), 0,
8189 "no arguments for builtin function %<%s%>",
8190 (this->code_ == BUILTIN_PRINT
8196 for (Expression_list::const_iterator p = args->begin();
8200 Type* type = (*p)->type();
8201 if (type->is_error()
8202 || type->is_string_type()
8203 || type->integer_type() != NULL
8204 || type->float_type() != NULL
8205 || type->complex_type() != NULL
8206 || type->is_boolean_type()
8207 || type->points_to() != NULL
8208 || type->interface_type() != NULL
8209 || type->channel_type() != NULL
8210 || type->map_type() != NULL
8211 || type->function_type() != NULL
8212 || type->is_slice_type())
8215 this->report_error(_("unsupported argument type to "
8216 "builtin function"));
8223 if (this->check_one_arg())
8225 if (this->one_arg()->type()->channel_type() == NULL)
8226 this->report_error(_("argument must be channel"));
8227 else if (!this->one_arg()->type()->channel_type()->may_send())
8228 this->report_error(_("cannot close receive-only channel"));
8233 case BUILTIN_SIZEOF:
8234 case BUILTIN_ALIGNOF:
8235 this->check_one_arg();
8238 case BUILTIN_RECOVER:
8239 if (this->args() != NULL && !this->args()->empty())
8240 this->report_error(_("too many arguments"));
8243 case BUILTIN_OFFSETOF:
8244 if (this->check_one_arg())
8246 Expression* arg = this->one_arg();
8247 if (arg->field_reference_expression() == NULL)
8248 this->report_error(_("argument must be a field reference"));
8254 const Expression_list* args = this->args();
8255 if (args == NULL || args->size() < 2)
8257 this->report_error(_("not enough arguments"));
8260 else if (args->size() > 2)
8262 this->report_error(_("too many arguments"));
8265 Type* arg1_type = args->front()->type();
8266 Type* arg2_type = args->back()->type();
8267 if (arg1_type->is_error() || arg2_type->is_error())
8271 if (arg1_type->is_slice_type())
8272 e1 = arg1_type->array_type()->element_type();
8275 this->report_error(_("left argument must be a slice"));
8280 if (arg2_type->is_slice_type())
8281 e2 = arg2_type->array_type()->element_type();
8282 else if (arg2_type->is_string_type())
8283 e2 = Type::lookup_integer_type("uint8");
8286 this->report_error(_("right argument must be a slice or a string"));
8290 if (!Type::are_identical(e1, e2, true, NULL))
8291 this->report_error(_("element types must be the same"));
8295 case BUILTIN_APPEND:
8297 const Expression_list* args = this->args();
8298 if (args == NULL || args->size() < 2)
8300 this->report_error(_("not enough arguments"));
8303 if (args->size() > 2)
8305 this->report_error(_("too many arguments"));
8309 // The language permits appending a string to a []byte, as a
8311 if (args->back()->type()->is_string_type())
8313 const Array_type* at = args->front()->type()->array_type();
8314 const Type* e = at->element_type()->forwarded();
8315 if (e == Type::lookup_integer_type("uint8"))
8320 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
8324 this->report_error(_("arguments 1 and 2 have different types"));
8327 error_at(this->location(),
8328 "arguments 1 and 2 have different types (%s)",
8330 this->set_is_error();
8338 if (this->check_one_arg())
8340 if (this->one_arg()->type()->complex_type() == NULL)
8341 this->report_error(_("argument must have complex type"));
8345 case BUILTIN_COMPLEX:
8347 const Expression_list* args = this->args();
8348 if (args == NULL || args->size() < 2)
8349 this->report_error(_("not enough arguments"));
8350 else if (args->size() > 2)
8351 this->report_error(_("too many arguments"));
8352 else if (args->front()->is_error_expression()
8353 || args->front()->type()->is_error()
8354 || args->back()->is_error_expression()
8355 || args->back()->type()->is_error())
8356 this->set_is_error();
8357 else if (!Type::are_identical(args->front()->type(),
8358 args->back()->type(), true, NULL))
8359 this->report_error(_("complex arguments must have identical types"));
8360 else if (args->front()->type()->float_type() == NULL)
8361 this->report_error(_("complex arguments must have "
8362 "floating-point type"));
8371 // Return the tree for a builtin function.
8374 Builtin_call_expression::do_get_tree(Translate_context* context)
8376 Gogo* gogo = context->gogo();
8377 Location location = this->location();
8378 switch (this->code_)
8380 case BUILTIN_INVALID:
8388 const Expression_list* args = this->args();
8389 go_assert(args != NULL && args->size() == 1);
8390 Expression* arg = *args->begin();
8391 Type* arg_type = arg->type();
8395 go_assert(saw_errors());
8396 return error_mark_node;
8400 tree arg_tree = arg->get_tree(context);
8402 this->seen_ = false;
8404 if (arg_tree == error_mark_node)
8405 return error_mark_node;
8407 if (arg_type->points_to() != NULL)
8409 arg_type = arg_type->points_to();
8410 go_assert(arg_type->array_type() != NULL
8411 && !arg_type->is_slice_type());
8412 go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
8413 arg_tree = build_fold_indirect_ref(arg_tree);
8417 if (this->code_ == BUILTIN_LEN)
8419 if (arg_type->is_string_type())
8420 val_tree = String_type::length_tree(gogo, arg_tree);
8421 else if (arg_type->array_type() != NULL)
8425 go_assert(saw_errors());
8426 return error_mark_node;
8429 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
8430 this->seen_ = false;
8432 else if (arg_type->map_type() != NULL)
8434 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
8435 static tree map_len_fndecl;
8436 val_tree = Gogo::call_builtin(&map_len_fndecl,
8444 else if (arg_type->channel_type() != NULL)
8446 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
8447 static tree chan_len_fndecl;
8448 val_tree = Gogo::call_builtin(&chan_len_fndecl,
8461 if (arg_type->array_type() != NULL)
8465 go_assert(saw_errors());
8466 return error_mark_node;
8469 val_tree = arg_type->array_type()->capacity_tree(gogo,
8471 this->seen_ = false;
8473 else if (arg_type->channel_type() != NULL)
8475 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
8476 static tree chan_cap_fndecl;
8477 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
8489 if (val_tree == error_mark_node)
8490 return error_mark_node;
8492 Type* int_type = Type::lookup_integer_type("int");
8493 tree type_tree = type_to_tree(int_type->get_backend(gogo));
8494 if (type_tree == TREE_TYPE(val_tree))
8497 return fold(convert_to_integer(type_tree, val_tree));
8501 case BUILTIN_PRINTLN:
8503 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
8504 tree stmt_list = NULL_TREE;
8506 const Expression_list* call_args = this->args();
8507 if (call_args != NULL)
8509 for (Expression_list::const_iterator p = call_args->begin();
8510 p != call_args->end();
8513 if (is_ln && p != call_args->begin())
8515 static tree print_space_fndecl;
8516 tree call = Gogo::call_builtin(&print_space_fndecl,
8521 if (call == error_mark_node)
8522 return error_mark_node;
8523 append_to_statement_list(call, &stmt_list);
8526 Type* type = (*p)->type();
8528 tree arg = (*p)->get_tree(context);
8529 if (arg == error_mark_node)
8530 return error_mark_node;
8534 if (type->is_string_type())
8536 static tree print_string_fndecl;
8537 pfndecl = &print_string_fndecl;
8538 fnname = "__go_print_string";
8540 else if (type->integer_type() != NULL
8541 && type->integer_type()->is_unsigned())
8543 static tree print_uint64_fndecl;
8544 pfndecl = &print_uint64_fndecl;
8545 fnname = "__go_print_uint64";
8546 Type* itype = Type::lookup_integer_type("uint64");
8547 Btype* bitype = itype->get_backend(gogo);
8548 arg = fold_convert_loc(location.gcc_location(),
8549 type_to_tree(bitype), arg);
8551 else if (type->integer_type() != NULL)
8553 static tree print_int64_fndecl;
8554 pfndecl = &print_int64_fndecl;
8555 fnname = "__go_print_int64";
8556 Type* itype = Type::lookup_integer_type("int64");
8557 Btype* bitype = itype->get_backend(gogo);
8558 arg = fold_convert_loc(location.gcc_location(),
8559 type_to_tree(bitype), arg);
8561 else if (type->float_type() != NULL)
8563 static tree print_double_fndecl;
8564 pfndecl = &print_double_fndecl;
8565 fnname = "__go_print_double";
8566 arg = fold_convert_loc(location.gcc_location(),
8567 double_type_node, arg);
8569 else if (type->complex_type() != NULL)
8571 static tree print_complex_fndecl;
8572 pfndecl = &print_complex_fndecl;
8573 fnname = "__go_print_complex";
8574 arg = fold_convert_loc(location.gcc_location(),
8575 complex_double_type_node, arg);
8577 else if (type->is_boolean_type())
8579 static tree print_bool_fndecl;
8580 pfndecl = &print_bool_fndecl;
8581 fnname = "__go_print_bool";
8583 else if (type->points_to() != NULL
8584 || type->channel_type() != NULL
8585 || type->map_type() != NULL
8586 || type->function_type() != NULL)
8588 static tree print_pointer_fndecl;
8589 pfndecl = &print_pointer_fndecl;
8590 fnname = "__go_print_pointer";
8591 arg = fold_convert_loc(location.gcc_location(),
8592 ptr_type_node, arg);
8594 else if (type->interface_type() != NULL)
8596 if (type->interface_type()->is_empty())
8598 static tree print_empty_interface_fndecl;
8599 pfndecl = &print_empty_interface_fndecl;
8600 fnname = "__go_print_empty_interface";
8604 static tree print_interface_fndecl;
8605 pfndecl = &print_interface_fndecl;
8606 fnname = "__go_print_interface";
8609 else if (type->is_slice_type())
8611 static tree print_slice_fndecl;
8612 pfndecl = &print_slice_fndecl;
8613 fnname = "__go_print_slice";
8618 tree call = Gogo::call_builtin(pfndecl,
8625 if (call == error_mark_node)
8626 return error_mark_node;
8627 append_to_statement_list(call, &stmt_list);
8633 static tree print_nl_fndecl;
8634 tree call = Gogo::call_builtin(&print_nl_fndecl,
8639 if (call == error_mark_node)
8640 return error_mark_node;
8641 append_to_statement_list(call, &stmt_list);
8649 const Expression_list* args = this->args();
8650 go_assert(args != NULL && args->size() == 1);
8651 Expression* arg = args->front();
8652 tree arg_tree = arg->get_tree(context);
8653 if (arg_tree == error_mark_node)
8654 return error_mark_node;
8656 Type::make_interface_type(NULL, Linemap::predeclared_location());
8657 arg_tree = Expression::convert_for_assignment(context, empty,
8659 arg_tree, location);
8660 static tree panic_fndecl;
8661 tree call = Gogo::call_builtin(&panic_fndecl,
8666 TREE_TYPE(arg_tree),
8668 if (call == error_mark_node)
8669 return error_mark_node;
8670 // This function will throw an exception.
8671 TREE_NOTHROW(panic_fndecl) = 0;
8672 // This function will not return.
8673 TREE_THIS_VOLATILE(panic_fndecl) = 1;
8677 case BUILTIN_RECOVER:
8679 // The argument is set when building recover thunks. It's a
8680 // boolean value which is true if we can recover a value now.
8681 const Expression_list* args = this->args();
8682 go_assert(args != NULL && args->size() == 1);
8683 Expression* arg = args->front();
8684 tree arg_tree = arg->get_tree(context);
8685 if (arg_tree == error_mark_node)
8686 return error_mark_node;
8689 Type::make_interface_type(NULL, Linemap::predeclared_location());
8690 tree empty_tree = type_to_tree(empty->get_backend(context->gogo()));
8692 Type* nil_type = Type::make_nil_type();
8693 Expression* nil = Expression::make_nil(location);
8694 tree nil_tree = nil->get_tree(context);
8695 tree empty_nil_tree = Expression::convert_for_assignment(context,
8701 // We need to handle a deferred call to recover specially,
8702 // because it changes whether it can recover a panic or not.
8703 // See test7 in test/recover1.go.
8705 if (this->is_deferred())
8707 static tree deferred_recover_fndecl;
8708 call = Gogo::call_builtin(&deferred_recover_fndecl,
8710 "__go_deferred_recover",
8716 static tree recover_fndecl;
8717 call = Gogo::call_builtin(&recover_fndecl,
8723 if (call == error_mark_node)
8724 return error_mark_node;
8725 return fold_build3_loc(location.gcc_location(), COND_EXPR, empty_tree,
8726 arg_tree, call, empty_nil_tree);
8731 const Expression_list* args = this->args();
8732 go_assert(args != NULL && args->size() == 1);
8733 Expression* arg = args->front();
8734 tree arg_tree = arg->get_tree(context);
8735 if (arg_tree == error_mark_node)
8736 return error_mark_node;
8737 static tree close_fndecl;
8738 return Gogo::call_builtin(&close_fndecl,
8740 "__go_builtin_close",
8743 TREE_TYPE(arg_tree),
8747 case BUILTIN_SIZEOF:
8748 case BUILTIN_OFFSETOF:
8749 case BUILTIN_ALIGNOF:
8754 bool b = this->integer_constant_value(true, val, &dummy);
8757 go_assert(saw_errors());
8758 return error_mark_node;
8760 Type* int_type = Type::lookup_integer_type("int");
8761 tree type = type_to_tree(int_type->get_backend(gogo));
8762 tree ret = Expression::integer_constant_tree(val, type);
8769 const Expression_list* args = this->args();
8770 go_assert(args != NULL && args->size() == 2);
8771 Expression* arg1 = args->front();
8772 Expression* arg2 = args->back();
8774 tree arg1_tree = arg1->get_tree(context);
8775 tree arg2_tree = arg2->get_tree(context);
8776 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8777 return error_mark_node;
8779 Type* arg1_type = arg1->type();
8780 Array_type* at = arg1_type->array_type();
8781 arg1_tree = save_expr(arg1_tree);
8782 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
8783 tree arg1_len = at->length_tree(gogo, arg1_tree);
8784 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
8785 return error_mark_node;
8787 Type* arg2_type = arg2->type();
8790 if (arg2_type->is_slice_type())
8792 at = arg2_type->array_type();
8793 arg2_tree = save_expr(arg2_tree);
8794 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8795 arg2_len = at->length_tree(gogo, arg2_tree);
8799 arg2_tree = save_expr(arg2_tree);
8800 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8801 arg2_len = String_type::length_tree(gogo, arg2_tree);
8803 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8804 return error_mark_node;
8806 arg1_len = save_expr(arg1_len);
8807 arg2_len = save_expr(arg2_len);
8808 tree len = fold_build3_loc(location.gcc_location(), COND_EXPR,
8809 TREE_TYPE(arg1_len),
8810 fold_build2_loc(location.gcc_location(),
8811 LT_EXPR, boolean_type_node,
8812 arg1_len, arg2_len),
8813 arg1_len, arg2_len);
8814 len = save_expr(len);
8816 Type* element_type = at->element_type();
8817 Btype* element_btype = element_type->get_backend(gogo);
8818 tree element_type_tree = type_to_tree(element_btype);
8819 if (element_type_tree == error_mark_node)
8820 return error_mark_node;
8821 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8822 tree bytecount = fold_convert_loc(location.gcc_location(),
8823 TREE_TYPE(element_size), len);
8824 bytecount = fold_build2_loc(location.gcc_location(), MULT_EXPR,
8825 TREE_TYPE(element_size),
8826 bytecount, element_size);
8827 bytecount = fold_convert_loc(location.gcc_location(), size_type_node,
8830 arg1_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
8832 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
8835 static tree copy_fndecl;
8836 tree call = Gogo::call_builtin(©_fndecl,
8847 if (call == error_mark_node)
8848 return error_mark_node;
8850 return fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
8851 TREE_TYPE(len), call, len);
8854 case BUILTIN_APPEND:
8856 const Expression_list* args = this->args();
8857 go_assert(args != NULL && args->size() == 2);
8858 Expression* arg1 = args->front();
8859 Expression* arg2 = args->back();
8861 tree arg1_tree = arg1->get_tree(context);
8862 tree arg2_tree = arg2->get_tree(context);
8863 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8864 return error_mark_node;
8866 Array_type* at = arg1->type()->array_type();
8867 Type* element_type = at->element_type()->forwarded();
8872 if (arg2->type()->is_string_type()
8873 && element_type == Type::lookup_integer_type("uint8"))
8875 arg2_tree = save_expr(arg2_tree);
8876 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8877 arg2_len = String_type::length_tree(gogo, arg2_tree);
8878 element_size = size_int(1);
8882 arg2_tree = Expression::convert_for_assignment(context, at,
8886 if (arg2_tree == error_mark_node)
8887 return error_mark_node;
8889 arg2_tree = save_expr(arg2_tree);
8891 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8892 arg2_len = at->length_tree(gogo, arg2_tree);
8894 Btype* element_btype = element_type->get_backend(gogo);
8895 tree element_type_tree = type_to_tree(element_btype);
8896 if (element_type_tree == error_mark_node)
8897 return error_mark_node;
8898 element_size = TYPE_SIZE_UNIT(element_type_tree);
8901 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
8903 arg2_len = fold_convert_loc(location.gcc_location(), size_type_node,
8905 element_size = fold_convert_loc(location.gcc_location(), size_type_node,
8908 if (arg2_val == error_mark_node
8909 || arg2_len == error_mark_node
8910 || element_size == error_mark_node)
8911 return error_mark_node;
8913 // We rebuild the decl each time since the slice types may
8915 tree append_fndecl = NULL_TREE;
8916 return Gogo::call_builtin(&append_fndecl,
8920 TREE_TYPE(arg1_tree),
8921 TREE_TYPE(arg1_tree),
8934 const Expression_list* args = this->args();
8935 go_assert(args != NULL && args->size() == 1);
8936 Expression* arg = args->front();
8937 tree arg_tree = arg->get_tree(context);
8938 if (arg_tree == error_mark_node)
8939 return error_mark_node;
8940 go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
8941 if (this->code_ == BUILTIN_REAL)
8942 return fold_build1_loc(location.gcc_location(), REALPART_EXPR,
8943 TREE_TYPE(TREE_TYPE(arg_tree)),
8946 return fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
8947 TREE_TYPE(TREE_TYPE(arg_tree)),
8951 case BUILTIN_COMPLEX:
8953 const Expression_list* args = this->args();
8954 go_assert(args != NULL && args->size() == 2);
8955 tree r = args->front()->get_tree(context);
8956 tree i = args->back()->get_tree(context);
8957 if (r == error_mark_node || i == error_mark_node)
8958 return error_mark_node;
8959 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
8960 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
8961 go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8962 return fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
8963 build_complex_type(TREE_TYPE(r)),
8972 // We have to support exporting a builtin call expression, because
8973 // code can set a constant to the result of a builtin expression.
8976 Builtin_call_expression::do_export(Export* exp) const
8983 if (this->integer_constant_value(true, val, &dummy))
8985 Integer_expression::export_integer(exp, val);
8994 if (this->float_constant_value(fval, &dummy))
8996 Float_expression::export_float(exp, fval);
9008 if (this->complex_constant_value(real, imag, &dummy))
9010 Complex_expression::export_complex(exp, real, imag);
9019 error_at(this->location(), "value is not constant");
9023 // A trailing space lets us reliably identify the end of the number.
9024 exp->write_c_string(" ");
9027 // Class Call_expression.
9032 Call_expression::do_traverse(Traverse* traverse)
9034 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
9035 return TRAVERSE_EXIT;
9036 if (this->args_ != NULL)
9038 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
9039 return TRAVERSE_EXIT;
9041 return TRAVERSE_CONTINUE;
9044 // Lower a call statement.
9047 Call_expression::do_lower(Gogo* gogo, Named_object* function,
9048 Statement_inserter* inserter, int)
9050 Location loc = this->location();
9052 // A type cast can look like a function call.
9053 if (this->fn_->is_type_expression()
9054 && this->args_ != NULL
9055 && this->args_->size() == 1)
9056 return Expression::make_cast(this->fn_->type(), this->args_->front(),
9059 // Recognize a call to a builtin function.
9060 Func_expression* fne = this->fn_->func_expression();
9062 && fne->named_object()->is_function_declaration()
9063 && fne->named_object()->func_declaration_value()->type()->is_builtin())
9064 return new Builtin_call_expression(gogo, this->fn_, this->args_,
9065 this->is_varargs_, loc);
9067 // Handle an argument which is a call to a function which returns
9068 // multiple results.
9069 if (this->args_ != NULL
9070 && this->args_->size() == 1
9071 && this->args_->front()->call_expression() != NULL
9072 && this->fn_->type()->function_type() != NULL)
9074 Function_type* fntype = this->fn_->type()->function_type();
9075 size_t rc = this->args_->front()->call_expression()->result_count();
9077 && fntype->parameters() != NULL
9078 && (fntype->parameters()->size() == rc
9079 || (fntype->is_varargs()
9080 && fntype->parameters()->size() - 1 <= rc)))
9082 Call_expression* call = this->args_->front()->call_expression();
9083 Expression_list* args = new Expression_list;
9084 for (size_t i = 0; i < rc; ++i)
9085 args->push_back(Expression::make_call_result(call, i));
9086 // We can't return a new call expression here, because this
9087 // one may be referenced by Call_result expressions. We
9088 // also can't delete the old arguments, because we may still
9089 // traverse them somewhere up the call stack. FIXME.
9094 // If this call returns multiple results, create a temporary
9095 // variable for each result.
9096 size_t rc = this->result_count();
9097 if (rc > 1 && this->results_ == NULL)
9099 std::vector<Temporary_statement*>* temps =
9100 new std::vector<Temporary_statement*>;
9102 const Typed_identifier_list* results =
9103 this->fn_->type()->function_type()->results();
9104 for (Typed_identifier_list::const_iterator p = results->begin();
9105 p != results->end();
9108 Temporary_statement* temp = Statement::make_temporary(p->type(),
9110 inserter->insert(temp);
9111 temps->push_back(temp);
9113 this->results_ = temps;
9116 // Handle a call to a varargs function by packaging up the extra
9118 if (this->fn_->type()->function_type() != NULL
9119 && this->fn_->type()->function_type()->is_varargs())
9121 Function_type* fntype = this->fn_->type()->function_type();
9122 const Typed_identifier_list* parameters = fntype->parameters();
9123 go_assert(parameters != NULL && !parameters->empty());
9124 Type* varargs_type = parameters->back().type();
9125 this->lower_varargs(gogo, function, inserter, varargs_type,
9126 parameters->size());
9129 // If this is call to a method, call the method directly passing the
9130 // object as the first parameter.
9131 Bound_method_expression* bme = this->fn_->bound_method_expression();
9134 Named_object* method = bme->method();
9135 Expression* first_arg = bme->first_argument();
9137 // We always pass a pointer when calling a method.
9138 if (first_arg->type()->points_to() == NULL
9139 && !first_arg->type()->is_error())
9141 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc);
9142 // We may need to create a temporary variable so that we can
9143 // take the address. We can't do that here because it will
9144 // mess up the order of evaluation.
9145 Unary_expression* ue = static_cast<Unary_expression*>(first_arg);
9146 ue->set_create_temp();
9149 // If we are calling a method which was inherited from an
9150 // embedded struct, and the method did not get a stub, then the
9151 // first type may be wrong.
9152 Type* fatype = bme->first_argument_type();
9155 if (fatype->points_to() == NULL)
9156 fatype = Type::make_pointer_type(fatype);
9157 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc);
9160 Expression_list* new_args = new Expression_list();
9161 new_args->push_back(first_arg);
9162 if (this->args_ != NULL)
9164 for (Expression_list::const_iterator p = this->args_->begin();
9165 p != this->args_->end();
9167 new_args->push_back(*p);
9170 // We have to change in place because this structure may be
9171 // referenced by Call_result_expressions. We can't delete the
9172 // old arguments, because we may be traversing them up in some
9174 this->args_ = new_args;
9175 this->fn_ = Expression::make_func_reference(method, NULL,
9182 // Lower a call to a varargs function. FUNCTION is the function in
9183 // which the call occurs--it's not the function we are calling.
9184 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
9185 // PARAM_COUNT is the number of parameters of the function we are
9186 // calling; the last of these parameters will be the varargs
9190 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
9191 Statement_inserter* inserter,
9192 Type* varargs_type, size_t param_count)
9194 if (this->varargs_are_lowered_)
9197 Location loc = this->location();
9199 go_assert(param_count > 0);
9200 go_assert(varargs_type->is_slice_type());
9202 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
9203 if (arg_count < param_count - 1)
9205 // Not enough arguments; will be caught in check_types.
9209 Expression_list* old_args = this->args_;
9210 Expression_list* new_args = new Expression_list();
9211 bool push_empty_arg = false;
9212 if (old_args == NULL || old_args->empty())
9214 go_assert(param_count == 1);
9215 push_empty_arg = true;
9219 Expression_list::const_iterator pa;
9221 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
9223 if (static_cast<size_t>(i) == param_count)
9225 new_args->push_back(*pa);
9228 // We have reached the varargs parameter.
9230 bool issued_error = false;
9231 if (pa == old_args->end())
9232 push_empty_arg = true;
9233 else if (pa + 1 == old_args->end() && this->is_varargs_)
9234 new_args->push_back(*pa);
9235 else if (this->is_varargs_)
9237 this->report_error(_("too many arguments"));
9242 Type* element_type = varargs_type->array_type()->element_type();
9243 Expression_list* vals = new Expression_list;
9244 for (; pa != old_args->end(); ++pa, ++i)
9246 // Check types here so that we get a better message.
9247 Type* patype = (*pa)->type();
9248 Location paloc = (*pa)->location();
9249 if (!this->check_argument_type(i, element_type, patype,
9250 paloc, issued_error))
9252 vals->push_back(*pa);
9255 Expression::make_slice_composite_literal(varargs_type, vals, loc);
9256 gogo->lower_expression(function, inserter, &val);
9257 new_args->push_back(val);
9262 new_args->push_back(Expression::make_nil(loc));
9264 // We can't return a new call expression here, because this one may
9265 // be referenced by Call_result expressions. FIXME. We can't
9266 // delete OLD_ARGS because we may have both a Call_expression and a
9267 // Builtin_call_expression which refer to them. FIXME.
9268 this->args_ = new_args;
9269 this->varargs_are_lowered_ = true;
9272 // Get the function type. This can return NULL in error cases.
9275 Call_expression::get_function_type() const
9277 return this->fn_->type()->function_type();
9280 // Return the number of values which this call will return.
9283 Call_expression::result_count() const
9285 const Function_type* fntype = this->get_function_type();
9288 if (fntype->results() == NULL)
9290 return fntype->results()->size();
9293 // Return the temporary which holds a result.
9295 Temporary_statement*
9296 Call_expression::result(size_t i) const
9298 go_assert(this->results_ != NULL
9299 && this->results_->size() > i);
9300 return (*this->results_)[i];
9303 // Return whether this is a call to the predeclared function recover.
9306 Call_expression::is_recover_call() const
9308 return this->do_is_recover_call();
9311 // Set the argument to the recover function.
9314 Call_expression::set_recover_arg(Expression* arg)
9316 this->do_set_recover_arg(arg);
9319 // Virtual functions also implemented by Builtin_call_expression.
9322 Call_expression::do_is_recover_call() const
9328 Call_expression::do_set_recover_arg(Expression*)
9333 // We have found an error with this call expression; return true if
9334 // we should report it.
9337 Call_expression::issue_error()
9339 if (this->issued_error_)
9343 this->issued_error_ = true;
9351 Call_expression::do_type()
9353 if (this->type_ != NULL)
9357 Function_type* fntype = this->get_function_type();
9359 return Type::make_error_type();
9361 const Typed_identifier_list* results = fntype->results();
9362 if (results == NULL)
9363 ret = Type::make_void_type();
9364 else if (results->size() == 1)
9365 ret = results->begin()->type();
9367 ret = Type::make_call_multiple_result_type(this);
9374 // Determine types for a call expression. We can use the function
9375 // parameter types to set the types of the arguments.
9378 Call_expression::do_determine_type(const Type_context*)
9380 if (!this->determining_types())
9383 this->fn_->determine_type_no_context();
9384 Function_type* fntype = this->get_function_type();
9385 const Typed_identifier_list* parameters = NULL;
9387 parameters = fntype->parameters();
9388 if (this->args_ != NULL)
9390 Typed_identifier_list::const_iterator pt;
9391 if (parameters != NULL)
9392 pt = parameters->begin();
9394 for (Expression_list::const_iterator pa = this->args_->begin();
9395 pa != this->args_->end();
9401 // If this is a method, the first argument is the
9403 if (fntype != NULL && fntype->is_method())
9405 Type* rtype = fntype->receiver()->type();
9406 // The receiver is always passed as a pointer.
9407 if (rtype->points_to() == NULL)
9408 rtype = Type::make_pointer_type(rtype);
9409 Type_context subcontext(rtype, false);
9410 (*pa)->determine_type(&subcontext);
9415 if (parameters != NULL && pt != parameters->end())
9417 Type_context subcontext(pt->type(), false);
9418 (*pa)->determine_type(&subcontext);
9422 (*pa)->determine_type_no_context();
9427 // Called when determining types for a Call_expression. Return true
9428 // if we should go ahead, false if they have already been determined.
9431 Call_expression::determining_types()
9433 if (this->types_are_determined_)
9437 this->types_are_determined_ = true;
9442 // Check types for parameter I.
9445 Call_expression::check_argument_type(int i, const Type* parameter_type,
9446 const Type* argument_type,
9447 Location argument_location,
9452 if (this->are_hidden_fields_ok_)
9453 ok = Type::are_assignable_hidden_ok(parameter_type, argument_type,
9456 ok = Type::are_assignable(parameter_type, argument_type, &reason);
9462 error_at(argument_location, "argument %d has incompatible type", i);
9464 error_at(argument_location,
9465 "argument %d has incompatible type (%s)",
9468 this->set_is_error();
9477 Call_expression::do_check_types(Gogo*)
9479 Function_type* fntype = this->get_function_type();
9482 if (!this->fn_->type()->is_error())
9483 this->report_error(_("expected function"));
9487 bool is_method = fntype->is_method();
9490 go_assert(this->args_ != NULL && !this->args_->empty());
9491 Type* rtype = fntype->receiver()->type();
9492 Expression* first_arg = this->args_->front();
9493 // The language permits copying hidden fields for a method
9494 // receiver. We dereference the values since receivers are
9495 // always passed as pointers.
9497 if (!Type::are_assignable_hidden_ok(rtype->deref(),
9498 first_arg->type()->deref(),
9502 this->report_error(_("incompatible type for receiver"));
9505 error_at(this->location(),
9506 "incompatible type for receiver (%s)",
9508 this->set_is_error();
9513 // Note that varargs was handled by the lower_varargs() method, so
9514 // we don't have to worry about it here.
9516 const Typed_identifier_list* parameters = fntype->parameters();
9517 if (this->args_ == NULL)
9519 if (parameters != NULL && !parameters->empty())
9520 this->report_error(_("not enough arguments"));
9522 else if (parameters == NULL)
9524 if (!is_method || this->args_->size() > 1)
9525 this->report_error(_("too many arguments"));
9530 Expression_list::const_iterator pa = this->args_->begin();
9533 for (Typed_identifier_list::const_iterator pt = parameters->begin();
9534 pt != parameters->end();
9537 if (pa == this->args_->end())
9539 this->report_error(_("not enough arguments"));
9542 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
9543 (*pa)->location(), false);
9545 if (pa != this->args_->end())
9546 this->report_error(_("too many arguments"));
9550 // Return whether we have to use a temporary variable to ensure that
9551 // we evaluate this call expression in order. If the call returns no
9552 // results then it will inevitably be executed last.
9555 Call_expression::do_must_eval_in_order() const
9557 return this->result_count() > 0;
9560 // Get the function and the first argument to use when calling an
9561 // interface method.
9564 Call_expression::interface_method_function(
9565 Translate_context* context,
9566 Interface_field_reference_expression* interface_method,
9567 tree* first_arg_ptr)
9569 tree expr = interface_method->expr()->get_tree(context);
9570 if (expr == error_mark_node)
9571 return error_mark_node;
9572 expr = save_expr(expr);
9573 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
9574 if (first_arg == error_mark_node)
9575 return error_mark_node;
9576 *first_arg_ptr = first_arg;
9577 return interface_method->get_function_tree(context, expr);
9580 // Build the call expression.
9583 Call_expression::do_get_tree(Translate_context* context)
9585 if (this->tree_ != NULL_TREE)
9588 Function_type* fntype = this->get_function_type();
9590 return error_mark_node;
9592 if (this->fn_->is_error_expression())
9593 return error_mark_node;
9595 Gogo* gogo = context->gogo();
9596 Location location = this->location();
9598 Func_expression* func = this->fn_->func_expression();
9599 Interface_field_reference_expression* interface_method =
9600 this->fn_->interface_field_reference_expression();
9601 const bool has_closure = func != NULL && func->closure() != NULL;
9602 const bool is_interface_method = interface_method != NULL;
9606 if (this->args_ == NULL || this->args_->empty())
9608 nargs = is_interface_method ? 1 : 0;
9609 args = nargs == 0 ? NULL : new tree[nargs];
9611 else if (fntype->parameters() == NULL || fntype->parameters()->empty())
9613 // Passing a receiver parameter.
9614 go_assert(!is_interface_method
9615 && fntype->is_method()
9616 && this->args_->size() == 1);
9618 args = new tree[nargs];
9619 args[0] = this->args_->front()->get_tree(context);
9623 const Typed_identifier_list* params = fntype->parameters();
9625 nargs = this->args_->size();
9626 int i = is_interface_method ? 1 : 0;
9628 args = new tree[nargs];
9630 Typed_identifier_list::const_iterator pp = params->begin();
9631 Expression_list::const_iterator pe = this->args_->begin();
9632 if (!is_interface_method && fntype->is_method())
9634 args[i] = (*pe)->get_tree(context);
9638 for (; pe != this->args_->end(); ++pe, ++pp, ++i)
9640 go_assert(pp != params->end());
9641 tree arg_val = (*pe)->get_tree(context);
9642 args[i] = Expression::convert_for_assignment(context,
9647 if (args[i] == error_mark_node)
9650 return error_mark_node;
9653 go_assert(pp == params->end());
9654 go_assert(i == nargs);
9657 tree rettype = TREE_TYPE(TREE_TYPE(type_to_tree(fntype->get_backend(gogo))));
9658 if (rettype == error_mark_node)
9661 return error_mark_node;
9666 fn = func->get_tree_without_closure(gogo);
9667 else if (!is_interface_method)
9668 fn = this->fn_->get_tree(context);
9670 fn = this->interface_method_function(context, interface_method, &args[0]);
9672 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
9675 return error_mark_node;
9679 if (TREE_CODE(fndecl) == ADDR_EXPR)
9680 fndecl = TREE_OPERAND(fndecl, 0);
9682 // Add a type cast in case the type of the function is a recursive
9683 // type which refers to itself.
9684 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
9686 tree fnt = type_to_tree(fntype->get_backend(gogo));
9687 if (fnt == error_mark_node)
9688 return error_mark_node;
9689 fn = fold_convert_loc(location.gcc_location(), fnt, fn);
9692 // This is to support builtin math functions when using 80387 math.
9693 tree excess_type = NULL_TREE;
9694 if (TREE_CODE(fndecl) == FUNCTION_DECL
9695 && DECL_IS_BUILTIN(fndecl)
9696 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
9698 && ((SCALAR_FLOAT_TYPE_P(rettype)
9699 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
9700 || (COMPLEX_FLOAT_TYPE_P(rettype)
9701 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
9703 excess_type = excess_precision_type(TREE_TYPE(args[0]));
9704 if (excess_type != NULL_TREE)
9706 tree excess_fndecl = mathfn_built_in(excess_type,
9707 DECL_FUNCTION_CODE(fndecl));
9708 if (excess_fndecl == NULL_TREE)
9709 excess_type = NULL_TREE;
9712 fn = build_fold_addr_expr_loc(location.gcc_location(),
9714 for (int i = 0; i < nargs; ++i)
9715 args[i] = ::convert(excess_type, args[i]);
9720 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
9724 SET_EXPR_LOCATION(ret, location.gcc_location());
9728 tree closure_tree = func->closure()->get_tree(context);
9729 if (closure_tree != error_mark_node)
9730 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
9733 // If this is a recursive function type which returns itself, as in
9735 // we have used ptr_type_node for the return type. Add a cast here
9736 // to the correct type.
9737 if (TREE_TYPE(ret) == ptr_type_node)
9739 tree t = type_to_tree(this->type()->base()->get_backend(gogo));
9740 ret = fold_convert_loc(location.gcc_location(), t, ret);
9743 if (excess_type != NULL_TREE)
9745 // Calling convert here can undo our excess precision change.
9746 // That may or may not be a bug in convert_to_real.
9747 ret = build1(NOP_EXPR, rettype, ret);
9750 if (this->results_ != NULL)
9751 ret = this->set_results(context, ret);
9758 // Set the result variables if this call returns multiple results.
9761 Call_expression::set_results(Translate_context* context, tree call_tree)
9763 tree stmt_list = NULL_TREE;
9765 call_tree = save_expr(call_tree);
9767 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9769 go_assert(saw_errors());
9773 Location loc = this->location();
9774 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9775 size_t rc = this->result_count();
9776 for (size_t i = 0; i < rc; ++i, field = DECL_CHAIN(field))
9778 go_assert(field != NULL_TREE);
9780 Temporary_statement* temp = this->result(i);
9781 Temporary_reference_expression* ref =
9782 Expression::make_temporary_reference(temp, loc);
9783 ref->set_is_lvalue();
9784 tree temp_tree = ref->get_tree(context);
9785 if (temp_tree == error_mark_node)
9788 tree val_tree = build3_loc(loc.gcc_location(), COMPONENT_REF,
9789 TREE_TYPE(field), call_tree, field, NULL_TREE);
9790 tree set_tree = build2_loc(loc.gcc_location(), MODIFY_EXPR,
9791 void_type_node, temp_tree, val_tree);
9793 append_to_statement_list(set_tree, &stmt_list);
9795 go_assert(field == NULL_TREE);
9797 return save_expr(stmt_list);
9800 // Dump ast representation for a call expressin.
9803 Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
9805 this->fn_->dump_expression(ast_dump_context);
9806 ast_dump_context->ostream() << "(";
9808 ast_dump_context->dump_expression_list(this->args_);
9810 ast_dump_context->ostream() << ") ";
9813 // Make a call expression.
9816 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
9819 return new Call_expression(fn, args, is_varargs, location);
9822 // A single result from a call which returns multiple results.
9824 class Call_result_expression : public Expression
9827 Call_result_expression(Call_expression* call, unsigned int index)
9828 : Expression(EXPRESSION_CALL_RESULT, call->location()),
9829 call_(call), index_(index)
9834 do_traverse(Traverse*);
9840 do_determine_type(const Type_context*);
9843 do_check_types(Gogo*);
9848 return new Call_result_expression(this->call_->call_expression(),
9853 do_must_eval_in_order() const
9857 do_get_tree(Translate_context*);
9860 do_dump_expression(Ast_dump_context*) const;
9863 // The underlying call expression.
9865 // Which result we want.
9866 unsigned int index_;
9869 // Traverse a call result.
9872 Call_result_expression::do_traverse(Traverse* traverse)
9874 if (traverse->remember_expression(this->call_))
9876 // We have already traversed the call expression.
9877 return TRAVERSE_CONTINUE;
9879 return Expression::traverse(&this->call_, traverse);
9885 Call_result_expression::do_type()
9887 if (this->classification() == EXPRESSION_ERROR)
9888 return Type::make_error_type();
9890 // THIS->CALL_ can be replaced with a temporary reference due to
9891 // Call_expression::do_must_eval_in_order when there is an error.
9892 Call_expression* ce = this->call_->call_expression();
9895 this->set_is_error();
9896 return Type::make_error_type();
9898 Function_type* fntype = ce->get_function_type();
9901 if (ce->issue_error())
9903 if (!ce->fn()->type()->is_error())
9904 this->report_error(_("expected function"));
9906 this->set_is_error();
9907 return Type::make_error_type();
9909 const Typed_identifier_list* results = fntype->results();
9910 if (results == NULL || results->size() < 2)
9912 if (ce->issue_error())
9913 this->report_error(_("number of results does not match "
9914 "number of values"));
9915 return Type::make_error_type();
9917 Typed_identifier_list::const_iterator pr = results->begin();
9918 for (unsigned int i = 0; i < this->index_; ++i)
9920 if (pr == results->end())
9924 if (pr == results->end())
9926 if (ce->issue_error())
9927 this->report_error(_("number of results does not match "
9928 "number of values"));
9929 return Type::make_error_type();
9934 // Check the type. Just make sure that we trigger the warning in
9938 Call_result_expression::do_check_types(Gogo*)
9943 // Determine the type. We have nothing to do here, but the 0 result
9944 // needs to pass down to the caller.
9947 Call_result_expression::do_determine_type(const Type_context*)
9949 this->call_->determine_type_no_context();
9952 // Return the tree. We just refer to the temporary set by the call
9953 // expression. We don't do this at lowering time because it makes it
9954 // hard to evaluate the call at the right time.
9957 Call_result_expression::do_get_tree(Translate_context* context)
9959 Call_expression* ce = this->call_->call_expression();
9960 go_assert(ce != NULL);
9961 Temporary_statement* ts = ce->result(this->index_);
9962 Expression* ref = Expression::make_temporary_reference(ts, this->location());
9963 return ref->get_tree(context);
9966 // Dump ast representation for a call result expression.
9969 Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
9972 // FIXME: Wouldn't it be better if the call is assigned to a temporary
9973 // (struct) and the fields are referenced instead.
9974 ast_dump_context->ostream() << this->index_ << "@(";
9975 ast_dump_context->dump_expression(this->call_);
9976 ast_dump_context->ostream() << ")";
9979 // Make a reference to a single result of a call which returns
9980 // multiple results.
9983 Expression::make_call_result(Call_expression* call, unsigned int index)
9985 return new Call_result_expression(call, index);
9988 // Class Index_expression.
9993 Index_expression::do_traverse(Traverse* traverse)
9995 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9996 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9997 || (this->end_ != NULL
9998 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9999 return TRAVERSE_EXIT;
10000 return TRAVERSE_CONTINUE;
10003 // Lower an index expression. This converts the generic index
10004 // expression into an array index, a string index, or a map index.
10007 Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
10009 Location location = this->location();
10010 Expression* left = this->left_;
10011 Expression* start = this->start_;
10012 Expression* end = this->end_;
10014 Type* type = left->type();
10015 if (type->is_error())
10016 return Expression::make_error(location);
10017 else if (left->is_type_expression())
10019 error_at(location, "attempt to index type expression");
10020 return Expression::make_error(location);
10022 else if (type->array_type() != NULL)
10023 return Expression::make_array_index(left, start, end, location);
10024 else if (type->points_to() != NULL
10025 && type->points_to()->array_type() != NULL
10026 && !type->points_to()->is_slice_type())
10028 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
10030 return Expression::make_array_index(deref, start, end, location);
10032 else if (type->is_string_type())
10033 return Expression::make_string_index(left, start, end, location);
10034 else if (type->map_type() != NULL)
10038 error_at(location, "invalid slice of map");
10039 return Expression::make_error(location);
10041 Map_index_expression* ret = Expression::make_map_index(left, start,
10043 if (this->is_lvalue_)
10044 ret->set_is_lvalue();
10050 "attempt to index object which is not array, string, or map");
10051 return Expression::make_error(location);
10055 // Write an indexed expression (expr[expr:expr] or expr[expr]) to a
10059 Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context,
10060 const Expression* expr,
10061 const Expression* start,
10062 const Expression* end)
10064 expr->dump_expression(ast_dump_context);
10065 ast_dump_context->ostream() << "[";
10066 start->dump_expression(ast_dump_context);
10069 ast_dump_context->ostream() << ":";
10070 end->dump_expression(ast_dump_context);
10072 ast_dump_context->ostream() << "]";
10075 // Dump ast representation for an index expression.
10078 Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10081 Index_expression::dump_index_expression(ast_dump_context, this->left_,
10082 this->start_, this->end_);
10085 // Make an index expression.
10088 Expression::make_index(Expression* left, Expression* start, Expression* end,
10091 return new Index_expression(left, start, end, location);
10094 // An array index. This is used for both indexing and slicing.
10096 class Array_index_expression : public Expression
10099 Array_index_expression(Expression* array, Expression* start,
10100 Expression* end, Location location)
10101 : Expression(EXPRESSION_ARRAY_INDEX, location),
10102 array_(array), start_(start), end_(end), type_(NULL)
10107 do_traverse(Traverse*);
10113 do_determine_type(const Type_context*);
10116 do_check_types(Gogo*);
10121 return Expression::make_array_index(this->array_->copy(),
10122 this->start_->copy(),
10123 (this->end_ == NULL
10125 : this->end_->copy()),
10130 do_must_eval_subexpressions_in_order(int* skip) const
10137 do_is_addressable() const;
10140 do_address_taken(bool escapes)
10141 { this->array_->address_taken(escapes); }
10144 do_get_tree(Translate_context*);
10147 do_dump_expression(Ast_dump_context*) const;
10150 // The array we are getting a value from.
10151 Expression* array_;
10152 // The start or only index.
10153 Expression* start_;
10154 // The end index of a slice. This may be NULL for a simple array
10155 // index, or it may be a nil expression for the length of the array.
10157 // The type of the expression.
10161 // Array index traversal.
10164 Array_index_expression::do_traverse(Traverse* traverse)
10166 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
10167 return TRAVERSE_EXIT;
10168 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10169 return TRAVERSE_EXIT;
10170 if (this->end_ != NULL)
10172 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10173 return TRAVERSE_EXIT;
10175 return TRAVERSE_CONTINUE;
10178 // Return the type of an array index.
10181 Array_index_expression::do_type()
10183 if (this->type_ == NULL)
10185 Array_type* type = this->array_->type()->array_type();
10187 this->type_ = Type::make_error_type();
10188 else if (this->end_ == NULL)
10189 this->type_ = type->element_type();
10190 else if (type->is_slice_type())
10192 // A slice of a slice has the same type as the original
10194 this->type_ = this->array_->type()->deref();
10198 // A slice of an array is a slice.
10199 this->type_ = Type::make_array_type(type->element_type(), NULL);
10202 return this->type_;
10205 // Set the type of an array index.
10208 Array_index_expression::do_determine_type(const Type_context*)
10210 this->array_->determine_type_no_context();
10211 this->start_->determine_type_no_context();
10212 if (this->end_ != NULL)
10213 this->end_->determine_type_no_context();
10216 // Check types of an array index.
10219 Array_index_expression::do_check_types(Gogo*)
10221 if (this->start_->type()->integer_type() == NULL)
10222 this->report_error(_("index must be integer"));
10223 if (this->end_ != NULL
10224 && this->end_->type()->integer_type() == NULL
10225 && !this->end_->type()->is_error()
10226 && !this->end_->is_nil_expression()
10227 && !this->end_->is_error_expression())
10228 this->report_error(_("slice end must be integer"));
10230 Array_type* array_type = this->array_->type()->array_type();
10231 if (array_type == NULL)
10233 go_assert(this->array_->type()->is_error());
10237 unsigned int int_bits =
10238 Type::lookup_integer_type("int")->integer_type()->bits();
10243 bool lval_valid = (array_type->length() != NULL
10244 && array_type->length()->integer_constant_value(true,
10249 if (this->start_->integer_constant_value(true, ival, &dummy))
10251 if (mpz_sgn(ival) < 0
10252 || mpz_sizeinbase(ival, 2) >= int_bits
10254 && (this->end_ == NULL
10255 ? mpz_cmp(ival, lval) >= 0
10256 : mpz_cmp(ival, lval) > 0)))
10258 error_at(this->start_->location(), "array index out of bounds");
10259 this->set_is_error();
10262 if (this->end_ != NULL && !this->end_->is_nil_expression())
10264 if (this->end_->integer_constant_value(true, ival, &dummy))
10266 if (mpz_sgn(ival) < 0
10267 || mpz_sizeinbase(ival, 2) >= int_bits
10268 || (lval_valid && mpz_cmp(ival, lval) > 0))
10270 error_at(this->end_->location(), "array index out of bounds");
10271 this->set_is_error();
10278 // A slice of an array requires an addressable array. A slice of a
10279 // slice is always possible.
10280 if (this->end_ != NULL && !array_type->is_slice_type())
10282 if (!this->array_->is_addressable())
10283 this->report_error(_("array is not addressable"));
10285 this->array_->address_taken(true);
10289 // Return whether this expression is addressable.
10292 Array_index_expression::do_is_addressable() const
10294 // A slice expression is not addressable.
10295 if (this->end_ != NULL)
10298 // An index into a slice is addressable.
10299 if (this->array_->type()->is_slice_type())
10302 // An index into an array is addressable if the array is
10304 return this->array_->is_addressable();
10307 // Get a tree for an array index.
10310 Array_index_expression::do_get_tree(Translate_context* context)
10312 Gogo* gogo = context->gogo();
10313 Location loc = this->location();
10315 Array_type* array_type = this->array_->type()->array_type();
10316 if (array_type == NULL)
10318 go_assert(this->array_->type()->is_error());
10319 return error_mark_node;
10322 tree type_tree = type_to_tree(array_type->get_backend(gogo));
10323 if (type_tree == error_mark_node)
10324 return error_mark_node;
10326 tree array_tree = this->array_->get_tree(context);
10327 if (array_tree == error_mark_node)
10328 return error_mark_node;
10330 if (array_type->length() == NULL && !DECL_P(array_tree))
10331 array_tree = save_expr(array_tree);
10332 tree length_tree = array_type->length_tree(gogo, array_tree);
10333 if (length_tree == error_mark_node)
10334 return error_mark_node;
10335 length_tree = save_expr(length_tree);
10336 tree length_type = TREE_TYPE(length_tree);
10338 tree bad_index = boolean_false_node;
10340 tree start_tree = this->start_->get_tree(context);
10341 if (start_tree == error_mark_node)
10342 return error_mark_node;
10343 if (!DECL_P(start_tree))
10344 start_tree = save_expr(start_tree);
10345 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
10346 start_tree = convert_to_integer(length_type, start_tree);
10348 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
10351 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
10352 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10353 boolean_type_node, bad_index,
10354 fold_build2_loc(loc.gcc_location(),
10355 (this->end_ == NULL
10358 boolean_type_node, start_tree,
10361 int code = (array_type->length() != NULL
10362 ? (this->end_ == NULL
10363 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
10364 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
10365 : (this->end_ == NULL
10366 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
10367 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
10368 tree crash = Gogo::runtime_error(code, loc);
10370 if (this->end_ == NULL)
10372 // Simple array indexing. This has to return an l-value, so
10373 // wrap the index check into START_TREE.
10374 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
10375 build3(COND_EXPR, void_type_node,
10376 bad_index, crash, NULL_TREE),
10378 start_tree = fold_convert_loc(loc.gcc_location(), sizetype, start_tree);
10380 if (array_type->length() != NULL)
10383 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
10384 start_tree, NULL_TREE, NULL_TREE);
10389 tree values = array_type->value_pointer_tree(gogo, array_tree);
10390 Type* element_type = array_type->element_type();
10391 Btype* belement_type = element_type->get_backend(gogo);
10392 tree element_type_tree = type_to_tree(belement_type);
10393 if (element_type_tree == error_mark_node)
10394 return error_mark_node;
10395 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
10396 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
10397 start_tree, element_size);
10398 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
10399 TREE_TYPE(values), values, offset);
10400 return build_fold_indirect_ref(ptr);
10406 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
10407 if (capacity_tree == error_mark_node)
10408 return error_mark_node;
10409 capacity_tree = fold_convert_loc(loc.gcc_location(), length_type,
10413 if (this->end_->is_nil_expression())
10414 end_tree = length_tree;
10417 end_tree = this->end_->get_tree(context);
10418 if (end_tree == error_mark_node)
10419 return error_mark_node;
10420 if (!DECL_P(end_tree))
10421 end_tree = save_expr(end_tree);
10422 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
10423 end_tree = convert_to_integer(length_type, end_tree);
10425 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
10428 end_tree = fold_convert_loc(loc.gcc_location(), length_type, end_tree);
10430 capacity_tree = save_expr(capacity_tree);
10431 tree bad_end = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10433 fold_build2_loc(loc.gcc_location(),
10434 LT_EXPR, boolean_type_node,
10435 end_tree, start_tree),
10436 fold_build2_loc(loc.gcc_location(),
10437 GT_EXPR, boolean_type_node,
10438 end_tree, capacity_tree));
10439 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10440 boolean_type_node, bad_index, bad_end);
10443 Type* element_type = array_type->element_type();
10444 tree element_type_tree = type_to_tree(element_type->get_backend(gogo));
10445 if (element_type_tree == error_mark_node)
10446 return error_mark_node;
10447 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
10449 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
10450 fold_convert_loc(loc.gcc_location(), sizetype,
10454 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
10455 if (value_pointer == error_mark_node)
10456 return error_mark_node;
10458 value_pointer = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
10459 TREE_TYPE(value_pointer),
10460 value_pointer, offset);
10462 tree result_length_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
10463 length_type, end_tree, start_tree);
10465 tree result_capacity_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
10466 length_type, capacity_tree,
10469 tree struct_tree = type_to_tree(this->type()->get_backend(gogo));
10470 go_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
10472 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
10474 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
10475 tree field = TYPE_FIELDS(struct_tree);
10476 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
10477 elt->index = field;
10478 elt->value = value_pointer;
10480 elt = VEC_quick_push(constructor_elt, init, NULL);
10481 field = DECL_CHAIN(field);
10482 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
10483 elt->index = field;
10484 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
10485 result_length_tree);
10487 elt = VEC_quick_push(constructor_elt, init, NULL);
10488 field = DECL_CHAIN(field);
10489 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
10490 elt->index = field;
10491 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
10492 result_capacity_tree);
10494 tree constructor = build_constructor(struct_tree, init);
10496 if (TREE_CONSTANT(value_pointer)
10497 && TREE_CONSTANT(result_length_tree)
10498 && TREE_CONSTANT(result_capacity_tree))
10499 TREE_CONSTANT(constructor) = 1;
10501 return fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
10502 TREE_TYPE(constructor),
10503 build3(COND_EXPR, void_type_node,
10504 bad_index, crash, NULL_TREE),
10508 // Dump ast representation for an array index expression.
10511 Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10514 Index_expression::dump_index_expression(ast_dump_context, this->array_,
10515 this->start_, this->end_);
10518 // Make an array index expression. END may be NULL.
10521 Expression::make_array_index(Expression* array, Expression* start,
10522 Expression* end, Location location)
10524 // Taking a slice of a composite literal requires moving the literal
10526 if (end != NULL && array->is_composite_literal())
10528 array = Expression::make_heap_composite(array, location);
10529 array = Expression::make_unary(OPERATOR_MULT, array, location);
10531 return new Array_index_expression(array, start, end, location);
10534 // A string index. This is used for both indexing and slicing.
10536 class String_index_expression : public Expression
10539 String_index_expression(Expression* string, Expression* start,
10540 Expression* end, Location location)
10541 : Expression(EXPRESSION_STRING_INDEX, location),
10542 string_(string), start_(start), end_(end)
10547 do_traverse(Traverse*);
10553 do_determine_type(const Type_context*);
10556 do_check_types(Gogo*);
10561 return Expression::make_string_index(this->string_->copy(),
10562 this->start_->copy(),
10563 (this->end_ == NULL
10565 : this->end_->copy()),
10570 do_must_eval_subexpressions_in_order(int* skip) const
10577 do_get_tree(Translate_context*);
10580 do_dump_expression(Ast_dump_context*) const;
10583 // The string we are getting a value from.
10584 Expression* string_;
10585 // The start or only index.
10586 Expression* start_;
10587 // The end index of a slice. This may be NULL for a single index,
10588 // or it may be a nil expression for the length of the string.
10592 // String index traversal.
10595 String_index_expression::do_traverse(Traverse* traverse)
10597 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
10598 return TRAVERSE_EXIT;
10599 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10600 return TRAVERSE_EXIT;
10601 if (this->end_ != NULL)
10603 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10604 return TRAVERSE_EXIT;
10606 return TRAVERSE_CONTINUE;
10609 // Return the type of a string index.
10612 String_index_expression::do_type()
10614 if (this->end_ == NULL)
10615 return Type::lookup_integer_type("uint8");
10617 return this->string_->type();
10620 // Determine the type of a string index.
10623 String_index_expression::do_determine_type(const Type_context*)
10625 this->string_->determine_type_no_context();
10626 this->start_->determine_type_no_context();
10627 if (this->end_ != NULL)
10628 this->end_->determine_type_no_context();
10631 // Check types of a string index.
10634 String_index_expression::do_check_types(Gogo*)
10636 if (this->start_->type()->integer_type() == NULL)
10637 this->report_error(_("index must be integer"));
10638 if (this->end_ != NULL
10639 && this->end_->type()->integer_type() == NULL
10640 && !this->end_->is_nil_expression())
10641 this->report_error(_("slice end must be integer"));
10644 bool sval_valid = this->string_->string_constant_value(&sval);
10649 if (this->start_->integer_constant_value(true, ival, &dummy))
10651 if (mpz_sgn(ival) < 0
10652 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
10654 error_at(this->start_->location(), "string index out of bounds");
10655 this->set_is_error();
10658 if (this->end_ != NULL && !this->end_->is_nil_expression())
10660 if (this->end_->integer_constant_value(true, ival, &dummy))
10662 if (mpz_sgn(ival) < 0
10663 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
10665 error_at(this->end_->location(), "string index out of bounds");
10666 this->set_is_error();
10673 // Get a tree for a string index.
10676 String_index_expression::do_get_tree(Translate_context* context)
10678 Location loc = this->location();
10680 tree string_tree = this->string_->get_tree(context);
10681 if (string_tree == error_mark_node)
10682 return error_mark_node;
10684 if (this->string_->type()->points_to() != NULL)
10685 string_tree = build_fold_indirect_ref(string_tree);
10686 if (!DECL_P(string_tree))
10687 string_tree = save_expr(string_tree);
10688 tree string_type = TREE_TYPE(string_tree);
10690 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
10691 length_tree = save_expr(length_tree);
10692 tree length_type = TREE_TYPE(length_tree);
10694 tree bad_index = boolean_false_node;
10696 tree start_tree = this->start_->get_tree(context);
10697 if (start_tree == error_mark_node)
10698 return error_mark_node;
10699 if (!DECL_P(start_tree))
10700 start_tree = save_expr(start_tree);
10701 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
10702 start_tree = convert_to_integer(length_type, start_tree);
10704 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
10707 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
10709 int code = (this->end_ == NULL
10710 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
10711 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
10712 tree crash = Gogo::runtime_error(code, loc);
10714 if (this->end_ == NULL)
10716 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10717 boolean_type_node, bad_index,
10718 fold_build2_loc(loc.gcc_location(), GE_EXPR,
10720 start_tree, length_tree));
10722 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
10723 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
10724 TREE_TYPE(bytes_tree),
10726 fold_convert_loc(loc.gcc_location(), sizetype,
10728 tree index = build_fold_indirect_ref_loc(loc.gcc_location(), ptr);
10730 return build2(COMPOUND_EXPR, TREE_TYPE(index),
10731 build3(COND_EXPR, void_type_node,
10732 bad_index, crash, NULL_TREE),
10738 if (this->end_->is_nil_expression())
10739 end_tree = build_int_cst(length_type, -1);
10742 end_tree = this->end_->get_tree(context);
10743 if (end_tree == error_mark_node)
10744 return error_mark_node;
10745 if (!DECL_P(end_tree))
10746 end_tree = save_expr(end_tree);
10747 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
10748 end_tree = convert_to_integer(length_type, end_tree);
10750 bad_index = Expression::check_bounds(end_tree, length_type,
10753 end_tree = fold_convert_loc(loc.gcc_location(), length_type,
10757 static tree strslice_fndecl;
10758 tree ret = Gogo::call_builtin(&strslice_fndecl,
10760 "__go_string_slice",
10769 if (ret == error_mark_node)
10770 return error_mark_node;
10771 // This will panic if the bounds are out of range for the
10773 TREE_NOTHROW(strslice_fndecl) = 0;
10775 if (bad_index == boolean_false_node)
10778 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
10779 build3(COND_EXPR, void_type_node,
10780 bad_index, crash, NULL_TREE),
10785 // Dump ast representation for a string index expression.
10788 String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10791 Index_expression::dump_index_expression(ast_dump_context, this->string_,
10792 this->start_, this->end_);
10795 // Make a string index expression. END may be NULL.
10798 Expression::make_string_index(Expression* string, Expression* start,
10799 Expression* end, Location location)
10801 return new String_index_expression(string, start, end, location);
10804 // Class Map_index.
10806 // Get the type of the map.
10809 Map_index_expression::get_map_type() const
10811 Map_type* mt = this->map_->type()->deref()->map_type();
10813 go_assert(saw_errors());
10817 // Map index traversal.
10820 Map_index_expression::do_traverse(Traverse* traverse)
10822 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
10823 return TRAVERSE_EXIT;
10824 return Expression::traverse(&this->index_, traverse);
10827 // Return the type of a map index.
10830 Map_index_expression::do_type()
10832 Map_type* mt = this->get_map_type();
10834 return Type::make_error_type();
10835 Type* type = mt->val_type();
10836 // If this map index is in a tuple assignment, we actually return a
10837 // pointer to the value type. Tuple_map_assignment_statement is
10838 // responsible for handling this correctly. We need to get the type
10839 // right in case this gets assigned to a temporary variable.
10840 if (this->is_in_tuple_assignment_)
10841 type = Type::make_pointer_type(type);
10845 // Fix the type of a map index.
10848 Map_index_expression::do_determine_type(const Type_context*)
10850 this->map_->determine_type_no_context();
10851 Map_type* mt = this->get_map_type();
10852 Type* key_type = mt == NULL ? NULL : mt->key_type();
10853 Type_context subcontext(key_type, false);
10854 this->index_->determine_type(&subcontext);
10857 // Check types of a map index.
10860 Map_index_expression::do_check_types(Gogo*)
10862 std::string reason;
10863 Map_type* mt = this->get_map_type();
10866 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
10868 if (reason.empty())
10869 this->report_error(_("incompatible type for map index"));
10872 error_at(this->location(), "incompatible type for map index (%s)",
10874 this->set_is_error();
10879 // Get a tree for a map index.
10882 Map_index_expression::do_get_tree(Translate_context* context)
10884 Map_type* type = this->get_map_type();
10886 return error_mark_node;
10888 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
10889 if (valptr == error_mark_node)
10890 return error_mark_node;
10891 valptr = save_expr(valptr);
10893 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
10895 if (this->is_lvalue_)
10896 return build_fold_indirect_ref(valptr);
10897 else if (this->is_in_tuple_assignment_)
10899 // Tuple_map_assignment_statement is responsible for using this
10905 Gogo* gogo = context->gogo();
10906 Btype* val_btype = type->val_type()->get_backend(gogo);
10907 Bexpression* val_zero = gogo->backend()->zero_expression(val_btype);
10908 return fold_build3(COND_EXPR, val_type_tree,
10909 fold_build2(EQ_EXPR, boolean_type_node, valptr,
10910 fold_convert(TREE_TYPE(valptr),
10911 null_pointer_node)),
10912 expr_to_tree(val_zero),
10913 build_fold_indirect_ref(valptr));
10917 // Get a tree for the map index. This returns a tree which evaluates
10918 // to a pointer to a value. The pointer will be NULL if the key is
10922 Map_index_expression::get_value_pointer(Translate_context* context,
10925 Map_type* type = this->get_map_type();
10927 return error_mark_node;
10929 tree map_tree = this->map_->get_tree(context);
10930 tree index_tree = this->index_->get_tree(context);
10931 index_tree = Expression::convert_for_assignment(context, type->key_type(),
10932 this->index_->type(),
10935 if (map_tree == error_mark_node || index_tree == error_mark_node)
10936 return error_mark_node;
10938 if (this->map_->type()->points_to() != NULL)
10939 map_tree = build_fold_indirect_ref(map_tree);
10941 // We need to pass in a pointer to the key, so stuff it into a
10945 if (current_function_decl != NULL)
10947 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
10948 DECL_IGNORED_P(tmp) = 0;
10949 DECL_INITIAL(tmp) = index_tree;
10950 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
10951 TREE_ADDRESSABLE(tmp) = 1;
10955 tmp = build_decl(this->location().gcc_location(), VAR_DECL,
10956 create_tmp_var_name("M"),
10957 TREE_TYPE(index_tree));
10958 DECL_EXTERNAL(tmp) = 0;
10959 TREE_PUBLIC(tmp) = 0;
10960 TREE_STATIC(tmp) = 1;
10961 DECL_ARTIFICIAL(tmp) = 1;
10962 if (!TREE_CONSTANT(index_tree))
10963 make_tmp = fold_build2_loc(this->location().gcc_location(),
10964 INIT_EXPR, void_type_node,
10968 TREE_READONLY(tmp) = 1;
10969 TREE_CONSTANT(tmp) = 1;
10970 DECL_INITIAL(tmp) = index_tree;
10971 make_tmp = NULL_TREE;
10973 rest_of_decl_compilation(tmp, 1, 0);
10976 fold_convert_loc(this->location().gcc_location(), const_ptr_type_node,
10977 build_fold_addr_expr_loc(this->location().gcc_location(),
10980 static tree map_index_fndecl;
10981 tree call = Gogo::call_builtin(&map_index_fndecl,
10985 const_ptr_type_node,
10986 TREE_TYPE(map_tree),
10988 const_ptr_type_node,
10992 ? boolean_true_node
10993 : boolean_false_node));
10994 if (call == error_mark_node)
10995 return error_mark_node;
10996 // This can panic on a map of interface type if the interface holds
10997 // an uncomparable or unhashable type.
10998 TREE_NOTHROW(map_index_fndecl) = 0;
11000 Type* val_type = type->val_type();
11001 tree val_type_tree = type_to_tree(val_type->get_backend(context->gogo()));
11002 if (val_type_tree == error_mark_node)
11003 return error_mark_node;
11004 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
11006 tree ret = fold_convert_loc(this->location().gcc_location(),
11007 ptr_val_type_tree, call);
11008 if (make_tmp != NULL_TREE)
11009 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
11013 // Dump ast representation for a map index expression
11016 Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11019 Index_expression::dump_index_expression(ast_dump_context,
11020 this->map_, this->index_, NULL);
11023 // Make a map index expression.
11025 Map_index_expression*
11026 Expression::make_map_index(Expression* map, Expression* index,
11029 return new Map_index_expression(map, index, location);
11032 // Class Field_reference_expression.
11034 // Return the type of a field reference.
11037 Field_reference_expression::do_type()
11039 Type* type = this->expr_->type();
11040 if (type->is_error())
11042 Struct_type* struct_type = type->struct_type();
11043 go_assert(struct_type != NULL);
11044 return struct_type->field(this->field_index_)->type();
11047 // Check the types for a field reference.
11050 Field_reference_expression::do_check_types(Gogo*)
11052 Type* type = this->expr_->type();
11053 if (type->is_error())
11055 Struct_type* struct_type = type->struct_type();
11056 go_assert(struct_type != NULL);
11057 go_assert(struct_type->field(this->field_index_) != NULL);
11060 // Get a tree for a field reference.
11063 Field_reference_expression::do_get_tree(Translate_context* context)
11065 tree struct_tree = this->expr_->get_tree(context);
11066 if (struct_tree == error_mark_node
11067 || TREE_TYPE(struct_tree) == error_mark_node)
11068 return error_mark_node;
11069 go_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
11070 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
11071 if (field == NULL_TREE)
11073 // This can happen for a type which refers to itself indirectly
11074 // and then turns out to be erroneous.
11075 go_assert(saw_errors());
11076 return error_mark_node;
11078 for (unsigned int i = this->field_index_; i > 0; --i)
11080 field = DECL_CHAIN(field);
11081 go_assert(field != NULL_TREE);
11083 if (TREE_TYPE(field) == error_mark_node)
11084 return error_mark_node;
11085 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
11089 // Dump ast representation for a field reference expression.
11092 Field_reference_expression::do_dump_expression(
11093 Ast_dump_context* ast_dump_context) const
11095 this->expr_->dump_expression(ast_dump_context);
11096 ast_dump_context->ostream() << "." << this->field_index_;
11099 // Make a reference to a qualified identifier in an expression.
11101 Field_reference_expression*
11102 Expression::make_field_reference(Expression* expr, unsigned int field_index,
11105 return new Field_reference_expression(expr, field_index, location);
11108 // Class Interface_field_reference_expression.
11110 // Return a tree for the pointer to the function to call.
11113 Interface_field_reference_expression::get_function_tree(Translate_context*,
11116 if (this->expr_->type()->points_to() != NULL)
11117 expr = build_fold_indirect_ref(expr);
11119 tree expr_type = TREE_TYPE(expr);
11120 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
11122 tree field = TYPE_FIELDS(expr_type);
11123 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
11125 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
11126 go_assert(POINTER_TYPE_P(TREE_TYPE(table)));
11128 table = build_fold_indirect_ref(table);
11129 go_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
11131 std::string name = Gogo::unpack_hidden_name(this->name_);
11132 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
11133 field != NULL_TREE;
11134 field = DECL_CHAIN(field))
11136 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
11139 go_assert(field != NULL_TREE);
11141 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
11144 // Return a tree for the first argument to pass to the interface
11148 Interface_field_reference_expression::get_underlying_object_tree(
11149 Translate_context*,
11152 if (this->expr_->type()->points_to() != NULL)
11153 expr = build_fold_indirect_ref(expr);
11155 tree expr_type = TREE_TYPE(expr);
11156 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
11158 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
11159 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
11161 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
11167 Interface_field_reference_expression::do_traverse(Traverse* traverse)
11169 return Expression::traverse(&this->expr_, traverse);
11172 // Return the type of an interface field reference.
11175 Interface_field_reference_expression::do_type()
11177 Type* expr_type = this->expr_->type();
11179 Type* points_to = expr_type->points_to();
11180 if (points_to != NULL)
11181 expr_type = points_to;
11183 Interface_type* interface_type = expr_type->interface_type();
11184 if (interface_type == NULL)
11185 return Type::make_error_type();
11187 const Typed_identifier* method = interface_type->find_method(this->name_);
11188 if (method == NULL)
11189 return Type::make_error_type();
11191 return method->type();
11194 // Determine types.
11197 Interface_field_reference_expression::do_determine_type(const Type_context*)
11199 this->expr_->determine_type_no_context();
11202 // Check the types for an interface field reference.
11205 Interface_field_reference_expression::do_check_types(Gogo*)
11207 Type* type = this->expr_->type();
11209 Type* points_to = type->points_to();
11210 if (points_to != NULL)
11213 Interface_type* interface_type = type->interface_type();
11214 if (interface_type == NULL)
11216 if (!type->is_error_type())
11217 this->report_error(_("expected interface or pointer to interface"));
11221 const Typed_identifier* method =
11222 interface_type->find_method(this->name_);
11223 if (method == NULL)
11225 error_at(this->location(), "method %qs not in interface",
11226 Gogo::message_name(this->name_).c_str());
11227 this->set_is_error();
11232 // Get a tree for a reference to a field in an interface. There is no
11233 // standard tree type representation for this: it's a function
11234 // attached to its first argument, like a Bound_method_expression.
11235 // The only places it may currently be used are in a Call_expression
11236 // or a Go_statement, which will take it apart directly. So this has
11237 // nothing to do at present.
11240 Interface_field_reference_expression::do_get_tree(Translate_context*)
11245 // Dump ast representation for an interface field reference.
11248 Interface_field_reference_expression::do_dump_expression(
11249 Ast_dump_context* ast_dump_context) const
11251 this->expr_->dump_expression(ast_dump_context);
11252 ast_dump_context->ostream() << "." << this->name_;
11255 // Make a reference to a field in an interface.
11258 Expression::make_interface_field_reference(Expression* expr,
11259 const std::string& field,
11262 return new Interface_field_reference_expression(expr, field, location);
11265 // A general selector. This is a Parser_expression for LEFT.NAME. It
11266 // is lowered after we know the type of the left hand side.
11268 class Selector_expression : public Parser_expression
11271 Selector_expression(Expression* left, const std::string& name,
11273 : Parser_expression(EXPRESSION_SELECTOR, location),
11274 left_(left), name_(name)
11279 do_traverse(Traverse* traverse)
11280 { return Expression::traverse(&this->left_, traverse); }
11283 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
11288 return new Selector_expression(this->left_->copy(), this->name_,
11293 do_dump_expression(Ast_dump_context* ast_dump_context) const;
11297 lower_method_expression(Gogo*);
11299 // The expression on the left hand side.
11301 // The name on the right hand side.
11305 // Lower a selector expression once we know the real type of the left
11309 Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*,
11312 Expression* left = this->left_;
11313 if (left->is_type_expression())
11314 return this->lower_method_expression(gogo);
11315 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
11319 // Lower a method expression T.M or (*T).M. We turn this into a
11320 // function literal.
11323 Selector_expression::lower_method_expression(Gogo* gogo)
11325 Location location = this->location();
11326 Type* type = this->left_->type();
11327 const std::string& name(this->name_);
11330 if (type->points_to() == NULL)
11331 is_pointer = false;
11335 type = type->points_to();
11337 Named_type* nt = type->named_type();
11341 ("method expression requires named type or "
11342 "pointer to named type"));
11343 return Expression::make_error(location);
11347 Method* method = nt->method_function(name, &is_ambiguous);
11348 const Typed_identifier* imethod = NULL;
11349 if (method == NULL && !is_pointer)
11351 Interface_type* it = nt->interface_type();
11353 imethod = it->find_method(name);
11356 if (method == NULL && imethod == NULL)
11359 error_at(location, "type %<%s%s%> has no method %<%s%>",
11360 is_pointer ? "*" : "",
11361 nt->message_name().c_str(),
11362 Gogo::message_name(name).c_str());
11364 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
11365 Gogo::message_name(name).c_str(),
11366 is_pointer ? "*" : "",
11367 nt->message_name().c_str());
11368 return Expression::make_error(location);
11371 if (method != NULL && !is_pointer && !method->is_value_method())
11373 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
11374 nt->message_name().c_str(),
11375 Gogo::message_name(name).c_str());
11376 return Expression::make_error(location);
11379 // Build a new function type in which the receiver becomes the first
11381 Function_type* method_type;
11382 if (method != NULL)
11384 method_type = method->type();
11385 go_assert(method_type->is_method());
11389 method_type = imethod->type()->function_type();
11390 go_assert(method_type != NULL && !method_type->is_method());
11393 const char* const receiver_name = "$this";
11394 Typed_identifier_list* parameters = new Typed_identifier_list();
11395 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
11398 const Typed_identifier_list* method_parameters = method_type->parameters();
11399 if (method_parameters != NULL)
11401 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
11402 p != method_parameters->end();
11404 parameters->push_back(*p);
11407 const Typed_identifier_list* method_results = method_type->results();
11408 Typed_identifier_list* results;
11409 if (method_results == NULL)
11413 results = new Typed_identifier_list();
11414 for (Typed_identifier_list::const_iterator p = method_results->begin();
11415 p != method_results->end();
11417 results->push_back(*p);
11420 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
11422 if (method_type->is_varargs())
11423 fntype->set_is_varargs();
11425 // We generate methods which always takes a pointer to the receiver
11426 // as their first argument. If this is for a pointer type, we can
11427 // simply reuse the existing function. We use an internal hack to
11428 // get the right type.
11430 if (method != NULL && is_pointer)
11432 Named_object* mno = (method->needs_stub_method()
11433 ? method->stub_object()
11434 : method->named_object());
11435 Expression* f = Expression::make_func_reference(mno, NULL, location);
11436 f = Expression::make_cast(fntype, f, location);
11437 Type_conversion_expression* tce =
11438 static_cast<Type_conversion_expression*>(f);
11439 tce->set_may_convert_function_types();
11443 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
11446 Named_object* vno = gogo->lookup(receiver_name, NULL);
11447 go_assert(vno != NULL);
11448 Expression* ve = Expression::make_var_reference(vno, location);
11450 if (method != NULL)
11451 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
11453 bm = Expression::make_interface_field_reference(ve, name, location);
11455 // Even though we found the method above, if it has an error type we
11456 // may see an error here.
11457 if (bm->is_error_expression())
11459 gogo->finish_function(location);
11463 Expression_list* args;
11464 if (method_parameters == NULL)
11468 args = new Expression_list();
11469 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
11470 p != method_parameters->end();
11473 vno = gogo->lookup(p->name(), NULL);
11474 go_assert(vno != NULL);
11475 args->push_back(Expression::make_var_reference(vno, location));
11479 gogo->start_block(location);
11481 Call_expression* call = Expression::make_call(bm, args,
11482 method_type->is_varargs(),
11485 size_t count = call->result_count();
11488 s = Statement::make_statement(call, true);
11491 Expression_list* retvals = new Expression_list();
11493 retvals->push_back(call);
11496 for (size_t i = 0; i < count; ++i)
11497 retvals->push_back(Expression::make_call_result(call, i));
11499 s = Statement::make_return_statement(retvals, location);
11501 gogo->add_statement(s);
11503 Block* b = gogo->finish_block(location);
11505 gogo->add_block(b, location);
11507 // Lower the call in case there are multiple results.
11508 gogo->lower_block(no, b);
11510 gogo->finish_function(location);
11512 return Expression::make_func_reference(no, NULL, location);
11515 // Dump the ast for a selector expression.
11518 Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11521 ast_dump_context->dump_expression(this->left_);
11522 ast_dump_context->ostream() << ".";
11523 ast_dump_context->ostream() << this->name_;
11526 // Make a selector expression.
11529 Expression::make_selector(Expression* left, const std::string& name,
11532 return new Selector_expression(left, name, location);
11535 // Implement the builtin function new.
11537 class Allocation_expression : public Expression
11540 Allocation_expression(Type* type, Location location)
11541 : Expression(EXPRESSION_ALLOCATION, location),
11547 do_traverse(Traverse* traverse)
11548 { return Type::traverse(this->type_, traverse); }
11552 { return Type::make_pointer_type(this->type_); }
11555 do_determine_type(const Type_context*)
11560 { return new Allocation_expression(this->type_, this->location()); }
11563 do_get_tree(Translate_context*);
11566 do_dump_expression(Ast_dump_context*) const;
11569 // The type we are allocating.
11573 // Return a tree for an allocation expression.
11576 Allocation_expression::do_get_tree(Translate_context* context)
11578 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
11579 if (type_tree == error_mark_node)
11580 return error_mark_node;
11581 tree size_tree = TYPE_SIZE_UNIT(type_tree);
11582 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
11584 if (space == error_mark_node)
11585 return error_mark_node;
11586 return fold_convert(build_pointer_type(type_tree), space);
11589 // Dump ast representation for an allocation expression.
11592 Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11595 ast_dump_context->ostream() << "new(";
11596 ast_dump_context->dump_type(this->type_);
11597 ast_dump_context->ostream() << ")";
11600 // Make an allocation expression.
11603 Expression::make_allocation(Type* type, Location location)
11605 return new Allocation_expression(type, location);
11608 // Construct a struct.
11610 class Struct_construction_expression : public Expression
11613 Struct_construction_expression(Type* type, Expression_list* vals,
11615 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
11616 type_(type), vals_(vals)
11619 // Return whether this is a constant initializer.
11621 is_constant_struct() const;
11625 do_traverse(Traverse* traverse);
11629 { return this->type_; }
11632 do_determine_type(const Type_context*);
11635 do_check_types(Gogo*);
11640 return new Struct_construction_expression(this->type_, this->vals_->copy(),
11645 do_is_addressable() const
11649 do_get_tree(Translate_context*);
11652 do_export(Export*) const;
11655 do_dump_expression(Ast_dump_context*) const;
11658 // The type of the struct to construct.
11660 // The list of values, in order of the fields in the struct. A NULL
11661 // entry means that the field should be zero-initialized.
11662 Expression_list* vals_;
11668 Struct_construction_expression::do_traverse(Traverse* traverse)
11670 if (this->vals_ != NULL
11671 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11672 return TRAVERSE_EXIT;
11673 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11674 return TRAVERSE_EXIT;
11675 return TRAVERSE_CONTINUE;
11678 // Return whether this is a constant initializer.
11681 Struct_construction_expression::is_constant_struct() const
11683 if (this->vals_ == NULL)
11685 for (Expression_list::const_iterator pv = this->vals_->begin();
11686 pv != this->vals_->end();
11690 && !(*pv)->is_constant()
11691 && (!(*pv)->is_composite_literal()
11692 || (*pv)->is_nonconstant_composite_literal()))
11696 const Struct_field_list* fields = this->type_->struct_type()->fields();
11697 for (Struct_field_list::const_iterator pf = fields->begin();
11698 pf != fields->end();
11701 // There are no constant constructors for interfaces.
11702 if (pf->type()->interface_type() != NULL)
11709 // Final type determination.
11712 Struct_construction_expression::do_determine_type(const Type_context*)
11714 if (this->vals_ == NULL)
11716 const Struct_field_list* fields = this->type_->struct_type()->fields();
11717 Expression_list::const_iterator pv = this->vals_->begin();
11718 for (Struct_field_list::const_iterator pf = fields->begin();
11719 pf != fields->end();
11722 if (pv == this->vals_->end())
11726 Type_context subcontext(pf->type(), false);
11727 (*pv)->determine_type(&subcontext);
11730 // Extra values are an error we will report elsewhere; we still want
11731 // to determine the type to avoid knockon errors.
11732 for (; pv != this->vals_->end(); ++pv)
11733 (*pv)->determine_type_no_context();
11739 Struct_construction_expression::do_check_types(Gogo*)
11741 if (this->vals_ == NULL)
11744 Struct_type* st = this->type_->struct_type();
11745 if (this->vals_->size() > st->field_count())
11747 this->report_error(_("too many expressions for struct"));
11751 const Struct_field_list* fields = st->fields();
11752 Expression_list::const_iterator pv = this->vals_->begin();
11754 for (Struct_field_list::const_iterator pf = fields->begin();
11755 pf != fields->end();
11758 if (pv == this->vals_->end())
11760 this->report_error(_("too few expressions for struct"));
11767 std::string reason;
11768 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
11770 if (reason.empty())
11771 error_at((*pv)->location(),
11772 "incompatible type for field %d in struct construction",
11775 error_at((*pv)->location(),
11776 ("incompatible type for field %d in "
11777 "struct construction (%s)"),
11778 i + 1, reason.c_str());
11779 this->set_is_error();
11782 go_assert(pv == this->vals_->end());
11785 // Return a tree for constructing a struct.
11788 Struct_construction_expression::do_get_tree(Translate_context* context)
11790 Gogo* gogo = context->gogo();
11792 if (this->vals_ == NULL)
11794 Btype* btype = this->type_->get_backend(gogo);
11795 return expr_to_tree(gogo->backend()->zero_expression(btype));
11798 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
11799 if (type_tree == error_mark_node)
11800 return error_mark_node;
11801 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11803 bool is_constant = true;
11804 const Struct_field_list* fields = this->type_->struct_type()->fields();
11805 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
11807 Struct_field_list::const_iterator pf = fields->begin();
11808 Expression_list::const_iterator pv = this->vals_->begin();
11809 for (tree field = TYPE_FIELDS(type_tree);
11810 field != NULL_TREE;
11811 field = DECL_CHAIN(field), ++pf)
11813 go_assert(pf != fields->end());
11815 Btype* fbtype = pf->type()->get_backend(gogo);
11818 if (pv == this->vals_->end())
11819 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
11820 else if (*pv == NULL)
11822 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
11827 val = Expression::convert_for_assignment(context, pf->type(),
11829 (*pv)->get_tree(context),
11834 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
11835 return error_mark_node;
11837 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
11838 elt->index = field;
11840 if (!TREE_CONSTANT(val))
11841 is_constant = false;
11843 go_assert(pf == fields->end());
11845 tree ret = build_constructor(type_tree, elts);
11847 TREE_CONSTANT(ret) = 1;
11851 // Export a struct construction.
11854 Struct_construction_expression::do_export(Export* exp) const
11856 exp->write_c_string("convert(");
11857 exp->write_type(this->type_);
11858 for (Expression_list::const_iterator pv = this->vals_->begin();
11859 pv != this->vals_->end();
11862 exp->write_c_string(", ");
11864 (*pv)->export_expression(exp);
11866 exp->write_c_string(")");
11869 // Dump ast representation of a struct construction expression.
11872 Struct_construction_expression::do_dump_expression(
11873 Ast_dump_context* ast_dump_context) const
11875 ast_dump_context->dump_type(this->type_);
11876 ast_dump_context->ostream() << "{";
11877 ast_dump_context->dump_expression_list(this->vals_);
11878 ast_dump_context->ostream() << "}";
11881 // Make a struct composite literal. This used by the thunk code.
11884 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
11887 go_assert(type->struct_type() != NULL);
11888 return new Struct_construction_expression(type, vals, location);
11891 // Construct an array. This class is not used directly; instead we
11892 // use the child classes, Fixed_array_construction_expression and
11893 // Open_array_construction_expression.
11895 class Array_construction_expression : public Expression
11898 Array_construction_expression(Expression_classification classification,
11899 Type* type, Expression_list* vals,
11901 : Expression(classification, location),
11902 type_(type), vals_(vals)
11906 // Return whether this is a constant initializer.
11908 is_constant_array() const;
11910 // Return the number of elements.
11912 element_count() const
11913 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
11917 do_traverse(Traverse* traverse);
11921 { return this->type_; }
11924 do_determine_type(const Type_context*);
11927 do_check_types(Gogo*);
11930 do_is_addressable() const
11934 do_export(Export*) const;
11936 // The list of values.
11939 { return this->vals_; }
11941 // Get a constructor tree for the array values.
11943 get_constructor_tree(Translate_context* context, tree type_tree);
11946 do_dump_expression(Ast_dump_context*) const;
11949 // The type of the array to construct.
11951 // The list of values.
11952 Expression_list* vals_;
11958 Array_construction_expression::do_traverse(Traverse* traverse)
11960 if (this->vals_ != NULL
11961 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11962 return TRAVERSE_EXIT;
11963 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11964 return TRAVERSE_EXIT;
11965 return TRAVERSE_CONTINUE;
11968 // Return whether this is a constant initializer.
11971 Array_construction_expression::is_constant_array() const
11973 if (this->vals_ == NULL)
11976 // There are no constant constructors for interfaces.
11977 if (this->type_->array_type()->element_type()->interface_type() != NULL)
11980 for (Expression_list::const_iterator pv = this->vals_->begin();
11981 pv != this->vals_->end();
11985 && !(*pv)->is_constant()
11986 && (!(*pv)->is_composite_literal()
11987 || (*pv)->is_nonconstant_composite_literal()))
11993 // Final type determination.
11996 Array_construction_expression::do_determine_type(const Type_context*)
11998 if (this->vals_ == NULL)
12000 Type_context subcontext(this->type_->array_type()->element_type(), false);
12001 for (Expression_list::const_iterator pv = this->vals_->begin();
12002 pv != this->vals_->end();
12006 (*pv)->determine_type(&subcontext);
12013 Array_construction_expression::do_check_types(Gogo*)
12015 if (this->vals_ == NULL)
12018 Array_type* at = this->type_->array_type();
12020 Type* element_type = at->element_type();
12021 for (Expression_list::const_iterator pv = this->vals_->begin();
12022 pv != this->vals_->end();
12026 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
12028 error_at((*pv)->location(),
12029 "incompatible type for element %d in composite literal",
12031 this->set_is_error();
12035 Expression* length = at->length();
12036 if (length != NULL && !length->is_error_expression())
12041 if (at->length()->integer_constant_value(true, val, &type))
12043 if (this->vals_->size() > mpz_get_ui(val))
12044 this->report_error(_("too many elements in composite literal"));
12050 // Get a constructor tree for the array values.
12053 Array_construction_expression::get_constructor_tree(Translate_context* context,
12056 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
12057 (this->vals_ == NULL
12059 : this->vals_->size()));
12060 Type* element_type = this->type_->array_type()->element_type();
12061 bool is_constant = true;
12062 if (this->vals_ != NULL)
12065 for (Expression_list::const_iterator pv = this->vals_->begin();
12066 pv != this->vals_->end();
12069 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
12070 elt->index = size_int(i);
12073 Gogo* gogo = context->gogo();
12074 Btype* ebtype = element_type->get_backend(gogo);
12075 Bexpression *zv = gogo->backend()->zero_expression(ebtype);
12076 elt->value = expr_to_tree(zv);
12080 tree value_tree = (*pv)->get_tree(context);
12081 elt->value = Expression::convert_for_assignment(context,
12087 if (elt->value == error_mark_node)
12088 return error_mark_node;
12089 if (!TREE_CONSTANT(elt->value))
12090 is_constant = false;
12094 tree ret = build_constructor(type_tree, values);
12096 TREE_CONSTANT(ret) = 1;
12100 // Export an array construction.
12103 Array_construction_expression::do_export(Export* exp) const
12105 exp->write_c_string("convert(");
12106 exp->write_type(this->type_);
12107 if (this->vals_ != NULL)
12109 for (Expression_list::const_iterator pv = this->vals_->begin();
12110 pv != this->vals_->end();
12113 exp->write_c_string(", ");
12115 (*pv)->export_expression(exp);
12118 exp->write_c_string(")");
12121 // Dump ast representation of an array construction expressin.
12124 Array_construction_expression::do_dump_expression(
12125 Ast_dump_context* ast_dump_context) const
12127 Expression* length = this->type_->array_type() != NULL ?
12128 this->type_->array_type()->length() : NULL;
12130 ast_dump_context->ostream() << "[" ;
12131 if (length != NULL)
12133 ast_dump_context->dump_expression(length);
12135 ast_dump_context->ostream() << "]" ;
12136 ast_dump_context->dump_type(this->type_);
12137 ast_dump_context->ostream() << "{" ;
12138 ast_dump_context->dump_expression_list(this->vals_);
12139 ast_dump_context->ostream() << "}" ;
12143 // Construct a fixed array.
12145 class Fixed_array_construction_expression :
12146 public Array_construction_expression
12149 Fixed_array_construction_expression(Type* type, Expression_list* vals,
12151 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
12152 type, vals, location)
12154 go_assert(type->array_type() != NULL
12155 && type->array_type()->length() != NULL);
12162 return new Fixed_array_construction_expression(this->type(),
12163 (this->vals() == NULL
12165 : this->vals()->copy()),
12170 do_get_tree(Translate_context*);
12173 do_dump_expression(Ast_dump_context*);
12176 // Return a tree for constructing a fixed array.
12179 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
12181 Type* type = this->type();
12182 Btype* btype = type->get_backend(context->gogo());
12183 return this->get_constructor_tree(context, type_to_tree(btype));
12186 // Dump ast representation of an array construction expressin.
12189 Fixed_array_construction_expression::do_dump_expression(
12190 Ast_dump_context* ast_dump_context)
12193 ast_dump_context->ostream() << "[";
12194 ast_dump_context->dump_expression (this->type()->array_type()->length());
12195 ast_dump_context->ostream() << "]";
12196 ast_dump_context->dump_type(this->type());
12197 ast_dump_context->ostream() << "{";
12198 ast_dump_context->dump_expression_list(this->vals());
12199 ast_dump_context->ostream() << "}";
12202 // Construct an open array.
12204 class Open_array_construction_expression : public Array_construction_expression
12207 Open_array_construction_expression(Type* type, Expression_list* vals,
12209 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
12210 type, vals, location)
12212 go_assert(type->array_type() != NULL
12213 && type->array_type()->length() == NULL);
12217 // Note that taking the address of an open array literal is invalid.
12222 return new Open_array_construction_expression(this->type(),
12223 (this->vals() == NULL
12225 : this->vals()->copy()),
12230 do_get_tree(Translate_context*);
12233 // Return a tree for constructing an open array.
12236 Open_array_construction_expression::do_get_tree(Translate_context* context)
12238 Array_type* array_type = this->type()->array_type();
12239 if (array_type == NULL)
12241 go_assert(this->type()->is_error());
12242 return error_mark_node;
12245 Type* element_type = array_type->element_type();
12246 Btype* belement_type = element_type->get_backend(context->gogo());
12247 tree element_type_tree = type_to_tree(belement_type);
12248 if (element_type_tree == error_mark_node)
12249 return error_mark_node;
12253 if (this->vals() == NULL || this->vals()->empty())
12255 // We need to create a unique value.
12256 tree max = size_int(0);
12257 tree constructor_type = build_array_type(element_type_tree,
12258 build_index_type(max));
12259 if (constructor_type == error_mark_node)
12260 return error_mark_node;
12261 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
12262 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
12263 elt->index = size_int(0);
12264 Gogo* gogo = context->gogo();
12265 Btype* btype = element_type->get_backend(gogo);
12266 elt->value = expr_to_tree(gogo->backend()->zero_expression(btype));
12267 values = build_constructor(constructor_type, vec);
12268 if (TREE_CONSTANT(elt->value))
12269 TREE_CONSTANT(values) = 1;
12270 length_tree = size_int(0);
12274 tree max = size_int(this->vals()->size() - 1);
12275 tree constructor_type = build_array_type(element_type_tree,
12276 build_index_type(max));
12277 if (constructor_type == error_mark_node)
12278 return error_mark_node;
12279 values = this->get_constructor_tree(context, constructor_type);
12280 length_tree = size_int(this->vals()->size());
12283 if (values == error_mark_node)
12284 return error_mark_node;
12286 bool is_constant_initializer = TREE_CONSTANT(values);
12288 // We have to copy the initial values into heap memory if we are in
12289 // a function or if the values are not constants. We also have to
12290 // copy them if they may contain pointers in a non-constant context,
12291 // as otherwise the garbage collector won't see them.
12292 bool copy_to_heap = (context->function() != NULL
12293 || !is_constant_initializer
12294 || (element_type->has_pointer()
12295 && !context->is_const()));
12297 if (is_constant_initializer)
12299 tree tmp = build_decl(this->location().gcc_location(), VAR_DECL,
12300 create_tmp_var_name("C"), TREE_TYPE(values));
12301 DECL_EXTERNAL(tmp) = 0;
12302 TREE_PUBLIC(tmp) = 0;
12303 TREE_STATIC(tmp) = 1;
12304 DECL_ARTIFICIAL(tmp) = 1;
12307 // If we are not copying the value to the heap, we will only
12308 // initialize the value once, so we can use this directly
12309 // rather than copying it. In that case we can't make it
12310 // read-only, because the program is permitted to change it.
12311 TREE_READONLY(tmp) = 1;
12312 TREE_CONSTANT(tmp) = 1;
12314 DECL_INITIAL(tmp) = values;
12315 rest_of_decl_compilation(tmp, 1, 0);
12323 // the initializer will only run once.
12324 space = build_fold_addr_expr(values);
12329 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
12330 space = context->gogo()->allocate_memory(element_type, memsize,
12332 space = save_expr(space);
12334 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
12335 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
12337 TREE_THIS_NOTRAP(ref) = 1;
12338 set = build2(MODIFY_EXPR, void_type_node, ref, values);
12341 // Build a constructor for the open array.
12343 tree type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
12344 if (type_tree == error_mark_node)
12345 return error_mark_node;
12346 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
12348 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
12350 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
12351 tree field = TYPE_FIELDS(type_tree);
12352 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
12353 elt->index = field;
12354 elt->value = fold_convert(TREE_TYPE(field), space);
12356 elt = VEC_quick_push(constructor_elt, init, NULL);
12357 field = DECL_CHAIN(field);
12358 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
12359 elt->index = field;
12360 elt->value = fold_convert(TREE_TYPE(field), length_tree);
12362 elt = VEC_quick_push(constructor_elt, init, NULL);
12363 field = DECL_CHAIN(field);
12364 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
12365 elt->index = field;
12366 elt->value = fold_convert(TREE_TYPE(field), length_tree);
12368 tree constructor = build_constructor(type_tree, init);
12369 if (constructor == error_mark_node)
12370 return error_mark_node;
12372 TREE_CONSTANT(constructor) = 1;
12374 if (set == NULL_TREE)
12375 return constructor;
12377 return build2(COMPOUND_EXPR, type_tree, set, constructor);
12380 // Make a slice composite literal. This is used by the type
12381 // descriptor code.
12384 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
12387 go_assert(type->is_slice_type());
12388 return new Open_array_construction_expression(type, vals, location);
12391 // Construct a map.
12393 class Map_construction_expression : public Expression
12396 Map_construction_expression(Type* type, Expression_list* vals,
12398 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
12399 type_(type), vals_(vals)
12400 { go_assert(vals == NULL || vals->size() % 2 == 0); }
12404 do_traverse(Traverse* traverse);
12408 { return this->type_; }
12411 do_determine_type(const Type_context*);
12414 do_check_types(Gogo*);
12419 return new Map_construction_expression(this->type_, this->vals_->copy(),
12424 do_get_tree(Translate_context*);
12427 do_export(Export*) const;
12430 do_dump_expression(Ast_dump_context*) const;
12433 // The type of the map to construct.
12435 // The list of values.
12436 Expression_list* vals_;
12442 Map_construction_expression::do_traverse(Traverse* traverse)
12444 if (this->vals_ != NULL
12445 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12446 return TRAVERSE_EXIT;
12447 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12448 return TRAVERSE_EXIT;
12449 return TRAVERSE_CONTINUE;
12452 // Final type determination.
12455 Map_construction_expression::do_determine_type(const Type_context*)
12457 if (this->vals_ == NULL)
12460 Map_type* mt = this->type_->map_type();
12461 Type_context key_context(mt->key_type(), false);
12462 Type_context val_context(mt->val_type(), false);
12463 for (Expression_list::const_iterator pv = this->vals_->begin();
12464 pv != this->vals_->end();
12467 (*pv)->determine_type(&key_context);
12469 (*pv)->determine_type(&val_context);
12476 Map_construction_expression::do_check_types(Gogo*)
12478 if (this->vals_ == NULL)
12481 Map_type* mt = this->type_->map_type();
12483 Type* key_type = mt->key_type();
12484 Type* val_type = mt->val_type();
12485 for (Expression_list::const_iterator pv = this->vals_->begin();
12486 pv != this->vals_->end();
12489 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
12491 error_at((*pv)->location(),
12492 "incompatible type for element %d key in map construction",
12494 this->set_is_error();
12497 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
12499 error_at((*pv)->location(),
12500 ("incompatible type for element %d value "
12501 "in map construction"),
12503 this->set_is_error();
12508 // Return a tree for constructing a map.
12511 Map_construction_expression::do_get_tree(Translate_context* context)
12513 Gogo* gogo = context->gogo();
12514 Location loc = this->location();
12516 Map_type* mt = this->type_->map_type();
12518 // Build a struct to hold the key and value.
12519 tree struct_type = make_node(RECORD_TYPE);
12521 Type* key_type = mt->key_type();
12522 tree id = get_identifier("__key");
12523 tree key_type_tree = type_to_tree(key_type->get_backend(gogo));
12524 if (key_type_tree == error_mark_node)
12525 return error_mark_node;
12526 tree key_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
12528 DECL_CONTEXT(key_field) = struct_type;
12529 TYPE_FIELDS(struct_type) = key_field;
12531 Type* val_type = mt->val_type();
12532 id = get_identifier("__val");
12533 tree val_type_tree = type_to_tree(val_type->get_backend(gogo));
12534 if (val_type_tree == error_mark_node)
12535 return error_mark_node;
12536 tree val_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
12538 DECL_CONTEXT(val_field) = struct_type;
12539 DECL_CHAIN(key_field) = val_field;
12541 layout_type(struct_type);
12543 bool is_constant = true;
12548 if (this->vals_ == NULL || this->vals_->empty())
12550 valaddr = null_pointer_node;
12551 make_tmp = NULL_TREE;
12555 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
12556 this->vals_->size() / 2);
12558 for (Expression_list::const_iterator pv = this->vals_->begin();
12559 pv != this->vals_->end();
12562 bool one_is_constant = true;
12564 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
12566 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
12567 elt->index = key_field;
12568 tree val_tree = (*pv)->get_tree(context);
12569 elt->value = Expression::convert_for_assignment(context, key_type,
12572 if (elt->value == error_mark_node)
12573 return error_mark_node;
12574 if (!TREE_CONSTANT(elt->value))
12575 one_is_constant = false;
12579 elt = VEC_quick_push(constructor_elt, one, NULL);
12580 elt->index = val_field;
12581 val_tree = (*pv)->get_tree(context);
12582 elt->value = Expression::convert_for_assignment(context, val_type,
12585 if (elt->value == error_mark_node)
12586 return error_mark_node;
12587 if (!TREE_CONSTANT(elt->value))
12588 one_is_constant = false;
12590 elt = VEC_quick_push(constructor_elt, values, NULL);
12591 elt->index = size_int(i);
12592 elt->value = build_constructor(struct_type, one);
12593 if (one_is_constant)
12594 TREE_CONSTANT(elt->value) = 1;
12596 is_constant = false;
12599 tree index_type = build_index_type(size_int(i - 1));
12600 tree array_type = build_array_type(struct_type, index_type);
12601 tree init = build_constructor(array_type, values);
12603 TREE_CONSTANT(init) = 1;
12605 if (current_function_decl != NULL)
12607 tmp = create_tmp_var(array_type, get_name(array_type));
12608 DECL_INITIAL(tmp) = init;
12609 make_tmp = fold_build1_loc(loc.gcc_location(), DECL_EXPR,
12610 void_type_node, tmp);
12611 TREE_ADDRESSABLE(tmp) = 1;
12615 tmp = build_decl(loc.gcc_location(), VAR_DECL,
12616 create_tmp_var_name("M"), array_type);
12617 DECL_EXTERNAL(tmp) = 0;
12618 TREE_PUBLIC(tmp) = 0;
12619 TREE_STATIC(tmp) = 1;
12620 DECL_ARTIFICIAL(tmp) = 1;
12621 if (!TREE_CONSTANT(init))
12622 make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR,
12623 void_type_node, tmp, init);
12626 TREE_READONLY(tmp) = 1;
12627 TREE_CONSTANT(tmp) = 1;
12628 DECL_INITIAL(tmp) = init;
12629 make_tmp = NULL_TREE;
12631 rest_of_decl_compilation(tmp, 1, 0);
12634 valaddr = build_fold_addr_expr(tmp);
12637 tree descriptor = mt->map_descriptor_pointer(gogo, loc);
12639 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
12640 if (type_tree == error_mark_node)
12641 return error_mark_node;
12643 static tree construct_map_fndecl;
12644 tree call = Gogo::call_builtin(&construct_map_fndecl,
12646 "__go_construct_map",
12649 TREE_TYPE(descriptor),
12654 TYPE_SIZE_UNIT(struct_type),
12656 byte_position(val_field),
12658 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
12659 const_ptr_type_node,
12660 fold_convert(const_ptr_type_node, valaddr));
12661 if (call == error_mark_node)
12662 return error_mark_node;
12665 if (make_tmp == NULL)
12668 ret = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, type_tree,
12673 // Export an array construction.
12676 Map_construction_expression::do_export(Export* exp) const
12678 exp->write_c_string("convert(");
12679 exp->write_type(this->type_);
12680 for (Expression_list::const_iterator pv = this->vals_->begin();
12681 pv != this->vals_->end();
12684 exp->write_c_string(", ");
12685 (*pv)->export_expression(exp);
12687 exp->write_c_string(")");
12690 // Dump ast representation for a map construction expression.
12693 Map_construction_expression::do_dump_expression(
12694 Ast_dump_context* ast_dump_context) const
12696 ast_dump_context->ostream() << "{" ;
12697 ast_dump_context->dump_expression_list(this->vals_, true);
12698 ast_dump_context->ostream() << "}";
12701 // A general composite literal. This is lowered to a type specific
12704 class Composite_literal_expression : public Parser_expression
12707 Composite_literal_expression(Type* type, int depth, bool has_keys,
12708 Expression_list* vals, Location location)
12709 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
12710 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
12715 do_traverse(Traverse* traverse);
12718 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
12723 return new Composite_literal_expression(this->type_, this->depth_,
12725 (this->vals_ == NULL
12727 : this->vals_->copy()),
12732 do_dump_expression(Ast_dump_context*) const;
12736 lower_struct(Gogo*, Type*);
12739 lower_array(Type*);
12742 make_array(Type*, Expression_list*);
12745 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
12747 // The type of the composite literal.
12749 // The depth within a list of composite literals within a composite
12750 // literal, when the type is omitted.
12752 // The values to put in the composite literal.
12753 Expression_list* vals_;
12754 // If this is true, then VALS_ is a list of pairs: a key and a
12755 // value. In an array initializer, a missing key will be NULL.
12762 Composite_literal_expression::do_traverse(Traverse* traverse)
12764 if (this->vals_ != NULL
12765 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12766 return TRAVERSE_EXIT;
12767 return Type::traverse(this->type_, traverse);
12770 // Lower a generic composite literal into a specific version based on
12774 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
12775 Statement_inserter* inserter, int)
12777 Type* type = this->type_;
12779 for (int depth = this->depth_; depth > 0; --depth)
12781 if (type->array_type() != NULL)
12782 type = type->array_type()->element_type();
12783 else if (type->map_type() != NULL)
12784 type = type->map_type()->val_type();
12787 if (!type->is_error())
12788 error_at(this->location(),
12789 ("may only omit types within composite literals "
12790 "of slice, array, or map type"));
12791 return Expression::make_error(this->location());
12795 if (type->is_error())
12796 return Expression::make_error(this->location());
12797 else if (type->struct_type() != NULL)
12798 return this->lower_struct(gogo, type);
12799 else if (type->array_type() != NULL)
12800 return this->lower_array(type);
12801 else if (type->map_type() != NULL)
12802 return this->lower_map(gogo, function, inserter, type);
12805 error_at(this->location(),
12806 ("expected struct, slice, array, or map type "
12807 "for composite literal"));
12808 return Expression::make_error(this->location());
12812 // Lower a struct composite literal.
12815 Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
12817 Location location = this->location();
12818 Struct_type* st = type->struct_type();
12819 if (this->vals_ == NULL || !this->has_keys_)
12821 if (this->vals_ != NULL && !this->vals_->empty())
12823 std::string reason;
12824 if (type->has_hidden_fields(NULL, &reason))
12826 if (reason.empty())
12827 error_at(this->location(),
12828 "implicit assignment of hidden field");
12830 error_at(this->location(), "%s", reason.c_str());
12834 return new Struct_construction_expression(type, this->vals_, location);
12837 size_t field_count = st->field_count();
12838 std::vector<Expression*> vals(field_count);
12839 Expression_list::const_iterator p = this->vals_->begin();
12840 while (p != this->vals_->end())
12842 Expression* name_expr = *p;
12845 go_assert(p != this->vals_->end());
12846 Expression* val = *p;
12850 if (name_expr == NULL)
12852 error_at(val->location(), "mixture of field and value initializers");
12853 return Expression::make_error(location);
12856 bool bad_key = false;
12858 const Named_object* no = NULL;
12859 switch (name_expr->classification())
12861 case EXPRESSION_UNKNOWN_REFERENCE:
12862 name = name_expr->unknown_expression()->name();
12865 case EXPRESSION_CONST_REFERENCE:
12866 no = static_cast<Const_expression*>(name_expr)->named_object();
12869 case EXPRESSION_TYPE:
12871 Type* t = name_expr->type();
12872 Named_type* nt = t->named_type();
12876 no = nt->named_object();
12880 case EXPRESSION_VAR_REFERENCE:
12881 no = name_expr->var_expression()->named_object();
12884 case EXPRESSION_FUNC_REFERENCE:
12885 no = name_expr->func_expression()->named_object();
12888 case EXPRESSION_UNARY:
12889 // If there is a local variable around with the same name as
12890 // the field, and this occurs in the closure, then the
12891 // parser may turn the field reference into an indirection
12892 // through the closure. FIXME: This is a mess.
12895 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
12896 if (ue->op() == OPERATOR_MULT)
12898 Field_reference_expression* fre =
12899 ue->operand()->field_reference_expression();
12903 fre->expr()->type()->deref()->struct_type();
12906 const Struct_field* sf = st->field(fre->field_index());
12907 name = sf->field_name();
12909 // See below. FIXME.
12910 if (!Gogo::is_hidden_name(name)
12914 if (gogo->lookup_global(name.c_str()) != NULL)
12915 name = gogo->pack_hidden_name(name, false);
12919 snprintf(buf, sizeof buf, "%u", fre->field_index());
12920 size_t buflen = strlen(buf);
12921 if (name.compare(name.length() - buflen, buflen, buf)
12924 name = name.substr(0, name.length() - buflen);
12939 error_at(name_expr->location(), "expected struct field name");
12940 return Expression::make_error(location);
12947 // A predefined name won't be packed. If it starts with a
12948 // lower case letter we need to check for that case, because
12949 // the field name will be packed. FIXME.
12950 if (!Gogo::is_hidden_name(name)
12954 Named_object* gno = gogo->lookup_global(name.c_str());
12956 name = gogo->pack_hidden_name(name, false);
12960 unsigned int index;
12961 const Struct_field* sf = st->find_local_field(name, &index);
12964 error_at(name_expr->location(), "unknown field %qs in %qs",
12965 Gogo::message_name(name).c_str(),
12966 (type->named_type() != NULL
12967 ? type->named_type()->message_name().c_str()
12968 : "unnamed struct"));
12969 return Expression::make_error(location);
12971 if (vals[index] != NULL)
12973 error_at(name_expr->location(),
12974 "duplicate value for field %qs in %qs",
12975 Gogo::message_name(name).c_str(),
12976 (type->named_type() != NULL
12977 ? type->named_type()->message_name().c_str()
12978 : "unnamed struct"));
12979 return Expression::make_error(location);
12982 if (type->named_type() != NULL
12983 && type->named_type()->named_object()->package() != NULL
12984 && Gogo::is_hidden_name(sf->field_name()))
12985 error_at(name_expr->location(),
12986 "assignment of unexported field %qs in %qs literal",
12987 Gogo::message_name(sf->field_name()).c_str(),
12988 type->named_type()->message_name().c_str());
12993 Expression_list* list = new Expression_list;
12994 list->reserve(field_count);
12995 for (size_t i = 0; i < field_count; ++i)
12996 list->push_back(vals[i]);
12998 return new Struct_construction_expression(type, list, location);
13001 // Lower an array composite literal.
13004 Composite_literal_expression::lower_array(Type* type)
13006 Location location = this->location();
13007 if (this->vals_ == NULL || !this->has_keys_)
13008 return this->make_array(type, this->vals_);
13010 std::vector<Expression*> vals;
13011 vals.reserve(this->vals_->size());
13012 unsigned long index = 0;
13013 Expression_list::const_iterator p = this->vals_->begin();
13014 while (p != this->vals_->end())
13016 Expression* index_expr = *p;
13019 go_assert(p != this->vals_->end());
13020 Expression* val = *p;
13024 if (index_expr != NULL)
13030 if (!index_expr->integer_constant_value(true, ival, &dummy))
13033 error_at(index_expr->location(),
13034 "index expression is not integer constant");
13035 return Expression::make_error(location);
13038 if (mpz_sgn(ival) < 0)
13041 error_at(index_expr->location(), "index expression is negative");
13042 return Expression::make_error(location);
13045 index = mpz_get_ui(ival);
13046 if (mpz_cmp_ui(ival, index) != 0)
13049 error_at(index_expr->location(), "index value overflow");
13050 return Expression::make_error(location);
13053 Named_type* ntype = Type::lookup_integer_type("int");
13054 Integer_type* inttype = ntype->integer_type();
13056 mpz_init_set_ui(max, 1);
13057 mpz_mul_2exp(max, max, inttype->bits() - 1);
13058 bool ok = mpz_cmp(ival, max) < 0;
13063 error_at(index_expr->location(), "index value overflow");
13064 return Expression::make_error(location);
13069 // FIXME: Our representation isn't very good; this avoids
13071 if (index > 0x1000000)
13073 error_at(index_expr->location(), "index too large for compiler");
13074 return Expression::make_error(location);
13078 if (index == vals.size())
13079 vals.push_back(val);
13082 if (index > vals.size())
13084 vals.reserve(index + 32);
13085 vals.resize(index + 1, static_cast<Expression*>(NULL));
13087 if (vals[index] != NULL)
13089 error_at((index_expr != NULL
13090 ? index_expr->location()
13091 : val->location()),
13092 "duplicate value for index %lu",
13094 return Expression::make_error(location);
13102 size_t size = vals.size();
13103 Expression_list* list = new Expression_list;
13104 list->reserve(size);
13105 for (size_t i = 0; i < size; ++i)
13106 list->push_back(vals[i]);
13108 return this->make_array(type, list);
13111 // Actually build the array composite literal. This handles
13115 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
13117 Location location = this->location();
13118 Array_type* at = type->array_type();
13119 if (at->length() != NULL && at->length()->is_nil_expression())
13121 size_t size = vals == NULL ? 0 : vals->size();
13123 mpz_init_set_ui(vlen, size);
13124 Expression* elen = Expression::make_integer(&vlen, NULL, location);
13126 at = Type::make_array_type(at->element_type(), elen);
13129 if (at->length() != NULL)
13130 return new Fixed_array_construction_expression(type, vals, location);
13132 return new Open_array_construction_expression(type, vals, location);
13135 // Lower a map composite literal.
13138 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
13139 Statement_inserter* inserter,
13142 Location location = this->location();
13143 if (this->vals_ != NULL)
13145 if (!this->has_keys_)
13147 error_at(location, "map composite literal must have keys");
13148 return Expression::make_error(location);
13151 for (Expression_list::iterator p = this->vals_->begin();
13152 p != this->vals_->end();
13158 error_at((*p)->location(),
13159 "map composite literal must have keys for every value");
13160 return Expression::make_error(location);
13162 // Make sure we have lowered the key; it may not have been
13163 // lowered in order to handle keys for struct composite
13164 // literals. Lower it now to get the right error message.
13165 if ((*p)->unknown_expression() != NULL)
13167 (*p)->unknown_expression()->clear_is_composite_literal_key();
13168 gogo->lower_expression(function, inserter, &*p);
13169 go_assert((*p)->is_error_expression());
13170 return Expression::make_error(location);
13175 return new Map_construction_expression(type, this->vals_, location);
13178 // Dump ast representation for a composite literal expression.
13181 Composite_literal_expression::do_dump_expression(
13182 Ast_dump_context* ast_dump_context) const
13184 ast_dump_context->ostream() << "composite(";
13185 ast_dump_context->dump_type(this->type_);
13186 ast_dump_context->ostream() << ", {";
13187 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_);
13188 ast_dump_context->ostream() << "})";
13191 // Make a composite literal expression.
13194 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
13195 Expression_list* vals,
13198 return new Composite_literal_expression(type, depth, has_keys, vals,
13202 // Return whether this expression is a composite literal.
13205 Expression::is_composite_literal() const
13207 switch (this->classification_)
13209 case EXPRESSION_COMPOSITE_LITERAL:
13210 case EXPRESSION_STRUCT_CONSTRUCTION:
13211 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
13212 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
13213 case EXPRESSION_MAP_CONSTRUCTION:
13220 // Return whether this expression is a composite literal which is not
13224 Expression::is_nonconstant_composite_literal() const
13226 switch (this->classification_)
13228 case EXPRESSION_STRUCT_CONSTRUCTION:
13230 const Struct_construction_expression *psce =
13231 static_cast<const Struct_construction_expression*>(this);
13232 return !psce->is_constant_struct();
13234 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
13236 const Fixed_array_construction_expression *pace =
13237 static_cast<const Fixed_array_construction_expression*>(this);
13238 return !pace->is_constant_array();
13240 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
13242 const Open_array_construction_expression *pace =
13243 static_cast<const Open_array_construction_expression*>(this);
13244 return !pace->is_constant_array();
13246 case EXPRESSION_MAP_CONSTRUCTION:
13253 // Return true if this is a reference to a local variable.
13256 Expression::is_local_variable() const
13258 const Var_expression* ve = this->var_expression();
13261 const Named_object* no = ve->named_object();
13262 return (no->is_result_variable()
13263 || (no->is_variable() && !no->var_value()->is_global()));
13266 // Class Type_guard_expression.
13271 Type_guard_expression::do_traverse(Traverse* traverse)
13273 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
13274 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
13275 return TRAVERSE_EXIT;
13276 return TRAVERSE_CONTINUE;
13279 // Check types of a type guard expression. The expression must have
13280 // an interface type, but the actual type conversion is checked at run
13284 Type_guard_expression::do_check_types(Gogo*)
13286 // 6g permits using a type guard with unsafe.pointer; we are
13288 Type* expr_type = this->expr_->type();
13289 if (expr_type->is_unsafe_pointer_type())
13291 if (this->type_->points_to() == NULL
13292 && (this->type_->integer_type() == NULL
13293 || (this->type_->forwarded()
13294 != Type::lookup_integer_type("uintptr"))))
13295 this->report_error(_("invalid unsafe.Pointer conversion"));
13297 else if (this->type_->is_unsafe_pointer_type())
13299 if (expr_type->points_to() == NULL
13300 && (expr_type->integer_type() == NULL
13301 || (expr_type->forwarded()
13302 != Type::lookup_integer_type("uintptr"))))
13303 this->report_error(_("invalid unsafe.Pointer conversion"));
13305 else if (expr_type->interface_type() == NULL)
13307 if (!expr_type->is_error() && !this->type_->is_error())
13308 this->report_error(_("type assertion only valid for interface types"));
13309 this->set_is_error();
13311 else if (this->type_->interface_type() == NULL)
13313 std::string reason;
13314 if (!expr_type->interface_type()->implements_interface(this->type_,
13317 if (!this->type_->is_error())
13319 if (reason.empty())
13320 this->report_error(_("impossible type assertion: "
13321 "type does not implement interface"));
13323 error_at(this->location(),
13324 ("impossible type assertion: "
13325 "type does not implement interface (%s)"),
13328 this->set_is_error();
13333 // Return a tree for a type guard expression.
13336 Type_guard_expression::do_get_tree(Translate_context* context)
13338 Gogo* gogo = context->gogo();
13339 tree expr_tree = this->expr_->get_tree(context);
13340 if (expr_tree == error_mark_node)
13341 return error_mark_node;
13342 Type* expr_type = this->expr_->type();
13343 if ((this->type_->is_unsafe_pointer_type()
13344 && (expr_type->points_to() != NULL
13345 || expr_type->integer_type() != NULL))
13346 || (expr_type->is_unsafe_pointer_type()
13347 && this->type_->points_to() != NULL))
13348 return convert_to_pointer(type_to_tree(this->type_->get_backend(gogo)),
13350 else if (expr_type->is_unsafe_pointer_type()
13351 && this->type_->integer_type() != NULL)
13352 return convert_to_integer(type_to_tree(this->type_->get_backend(gogo)),
13354 else if (this->type_->interface_type() != NULL)
13355 return Expression::convert_interface_to_interface(context, this->type_,
13356 this->expr_->type(),
13360 return Expression::convert_for_assignment(context, this->type_,
13361 this->expr_->type(), expr_tree,
13365 // Dump ast representation for a type guard expression.
13368 Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
13371 this->expr_->dump_expression(ast_dump_context);
13372 ast_dump_context->ostream() << ".";
13373 ast_dump_context->dump_type(this->type_);
13376 // Make a type guard expression.
13379 Expression::make_type_guard(Expression* expr, Type* type,
13382 return new Type_guard_expression(expr, type, location);
13385 // Class Heap_composite_expression.
13387 // When you take the address of a composite literal, it is allocated
13388 // on the heap. This class implements that.
13390 class Heap_composite_expression : public Expression
13393 Heap_composite_expression(Expression* expr, Location location)
13394 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
13400 do_traverse(Traverse* traverse)
13401 { return Expression::traverse(&this->expr_, traverse); }
13405 { return Type::make_pointer_type(this->expr_->type()); }
13408 do_determine_type(const Type_context*)
13409 { this->expr_->determine_type_no_context(); }
13414 return Expression::make_heap_composite(this->expr_->copy(),
13419 do_get_tree(Translate_context*);
13421 // We only export global objects, and the parser does not generate
13422 // this in global scope.
13424 do_export(Export*) const
13425 { go_unreachable(); }
13428 do_dump_expression(Ast_dump_context*) const;
13431 // The composite literal which is being put on the heap.
13435 // Return a tree which allocates a composite literal on the heap.
13438 Heap_composite_expression::do_get_tree(Translate_context* context)
13440 tree expr_tree = this->expr_->get_tree(context);
13441 if (expr_tree == error_mark_node)
13442 return error_mark_node;
13443 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
13444 go_assert(TREE_CODE(expr_size) == INTEGER_CST);
13445 tree space = context->gogo()->allocate_memory(this->expr_->type(),
13446 expr_size, this->location());
13447 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
13448 space = save_expr(space);
13449 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
13451 TREE_THIS_NOTRAP(ref) = 1;
13452 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
13453 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
13455 SET_EXPR_LOCATION(ret, this->location().gcc_location());
13459 // Dump ast representation for a heap composite expression.
13462 Heap_composite_expression::do_dump_expression(
13463 Ast_dump_context* ast_dump_context) const
13465 ast_dump_context->ostream() << "&(";
13466 ast_dump_context->dump_expression(this->expr_);
13467 ast_dump_context->ostream() << ")";
13470 // Allocate a composite literal on the heap.
13473 Expression::make_heap_composite(Expression* expr, Location location)
13475 return new Heap_composite_expression(expr, location);
13478 // Class Receive_expression.
13480 // Return the type of a receive expression.
13483 Receive_expression::do_type()
13485 Channel_type* channel_type = this->channel_->type()->channel_type();
13486 if (channel_type == NULL)
13487 return Type::make_error_type();
13488 return channel_type->element_type();
13491 // Check types for a receive expression.
13494 Receive_expression::do_check_types(Gogo*)
13496 Type* type = this->channel_->type();
13497 if (type->is_error())
13499 this->set_is_error();
13502 if (type->channel_type() == NULL)
13504 this->report_error(_("expected channel"));
13507 if (!type->channel_type()->may_receive())
13509 this->report_error(_("invalid receive on send-only channel"));
13514 // Get a tree for a receive expression.
13517 Receive_expression::do_get_tree(Translate_context* context)
13519 Location loc = this->location();
13521 Channel_type* channel_type = this->channel_->type()->channel_type();
13522 if (channel_type == NULL)
13524 go_assert(this->channel_->type()->is_error());
13525 return error_mark_node;
13528 Expression* td = Expression::make_type_descriptor(channel_type, loc);
13529 tree td_tree = td->get_tree(context);
13531 Type* element_type = channel_type->element_type();
13532 Btype* element_type_btype = element_type->get_backend(context->gogo());
13533 tree element_type_tree = type_to_tree(element_type_btype);
13535 tree channel = this->channel_->get_tree(context);
13536 if (element_type_tree == error_mark_node || channel == error_mark_node)
13537 return error_mark_node;
13539 return Gogo::receive_from_channel(element_type_tree, td_tree, channel, loc);
13542 // Dump ast representation for a receive expression.
13545 Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
13547 ast_dump_context->ostream() << " <- " ;
13548 ast_dump_context->dump_expression(channel_);
13551 // Make a receive expression.
13553 Receive_expression*
13554 Expression::make_receive(Expression* channel, Location location)
13556 return new Receive_expression(channel, location);
13559 // An expression which evaluates to a pointer to the type descriptor
13562 class Type_descriptor_expression : public Expression
13565 Type_descriptor_expression(Type* type, Location location)
13566 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
13573 { return Type::make_type_descriptor_ptr_type(); }
13576 do_determine_type(const Type_context*)
13584 do_get_tree(Translate_context* context)
13586 return this->type_->type_descriptor_pointer(context->gogo(),
13591 do_dump_expression(Ast_dump_context*) const;
13594 // The type for which this is the descriptor.
13598 // Dump ast representation for a type descriptor expression.
13601 Type_descriptor_expression::do_dump_expression(
13602 Ast_dump_context* ast_dump_context) const
13604 ast_dump_context->dump_type(this->type_);
13607 // Make a type descriptor expression.
13610 Expression::make_type_descriptor(Type* type, Location location)
13612 return new Type_descriptor_expression(type, location);
13615 // An expression which evaluates to some characteristic of a type.
13616 // This is only used to initialize fields of a type descriptor. Using
13617 // a new expression class is slightly inefficient but gives us a good
13618 // separation between the frontend and the middle-end with regard to
13619 // how types are laid out.
13621 class Type_info_expression : public Expression
13624 Type_info_expression(Type* type, Type_info type_info)
13625 : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()),
13626 type_(type), type_info_(type_info)
13634 do_determine_type(const Type_context*)
13642 do_get_tree(Translate_context* context);
13645 do_dump_expression(Ast_dump_context*) const;
13648 // The type for which we are getting information.
13650 // What information we want.
13651 Type_info type_info_;
13654 // The type is chosen to match what the type descriptor struct
13658 Type_info_expression::do_type()
13660 switch (this->type_info_)
13662 case TYPE_INFO_SIZE:
13663 return Type::lookup_integer_type("uintptr");
13664 case TYPE_INFO_ALIGNMENT:
13665 case TYPE_INFO_FIELD_ALIGNMENT:
13666 return Type::lookup_integer_type("uint8");
13672 // Return type information in GENERIC.
13675 Type_info_expression::do_get_tree(Translate_context* context)
13677 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
13678 if (type_tree == error_mark_node)
13679 return error_mark_node;
13681 tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
13682 go_assert(val_type_tree != error_mark_node);
13684 if (this->type_info_ == TYPE_INFO_SIZE)
13685 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
13686 TYPE_SIZE_UNIT(type_tree));
13690 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
13691 val = go_type_alignment(type_tree);
13693 val = go_field_alignment(type_tree);
13694 return build_int_cstu(val_type_tree, val);
13698 // Dump ast representation for a type info expression.
13701 Type_info_expression::do_dump_expression(
13702 Ast_dump_context* ast_dump_context) const
13704 ast_dump_context->ostream() << "typeinfo(";
13705 ast_dump_context->dump_type(this->type_);
13706 ast_dump_context->ostream() << ",";
13707 ast_dump_context->ostream() <<
13708 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment"
13709 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment"
13710 : this->type_info_ == TYPE_INFO_SIZE ? "size "
13712 ast_dump_context->ostream() << ")";
13715 // Make a type info expression.
13718 Expression::make_type_info(Type* type, Type_info type_info)
13720 return new Type_info_expression(type, type_info);
13723 // An expression which evaluates to the offset of a field within a
13724 // struct. This, like Type_info_expression, q.v., is only used to
13725 // initialize fields of a type descriptor.
13727 class Struct_field_offset_expression : public Expression
13730 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
13731 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET,
13732 Linemap::predeclared_location()),
13733 type_(type), field_(field)
13739 { return Type::lookup_integer_type("uintptr"); }
13742 do_determine_type(const Type_context*)
13750 do_get_tree(Translate_context* context);
13753 do_dump_expression(Ast_dump_context*) const;
13756 // The type of the struct.
13757 Struct_type* type_;
13759 const Struct_field* field_;
13762 // Return a struct field offset in GENERIC.
13765 Struct_field_offset_expression::do_get_tree(Translate_context* context)
13767 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
13768 if (type_tree == error_mark_node)
13769 return error_mark_node;
13771 tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
13772 go_assert(val_type_tree != error_mark_node);
13774 const Struct_field_list* fields = this->type_->fields();
13775 tree struct_field_tree = TYPE_FIELDS(type_tree);
13776 Struct_field_list::const_iterator p;
13777 for (p = fields->begin();
13778 p != fields->end();
13779 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
13781 go_assert(struct_field_tree != NULL_TREE);
13782 if (&*p == this->field_)
13785 go_assert(&*p == this->field_);
13787 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
13788 byte_position(struct_field_tree));
13791 // Dump ast representation for a struct field offset expression.
13794 Struct_field_offset_expression::do_dump_expression(
13795 Ast_dump_context* ast_dump_context) const
13797 ast_dump_context->ostream() << "unsafe.Offsetof(";
13798 ast_dump_context->dump_type(this->type_);
13799 ast_dump_context->ostream() << '.';
13800 ast_dump_context->ostream() <<
13801 Gogo::message_name(this->field_->field_name());
13802 ast_dump_context->ostream() << ")";
13805 // Make an expression for a struct field offset.
13808 Expression::make_struct_field_offset(Struct_type* type,
13809 const Struct_field* field)
13811 return new Struct_field_offset_expression(type, field);
13814 // An expression which evaluates to a pointer to the map descriptor of
13817 class Map_descriptor_expression : public Expression
13820 Map_descriptor_expression(Map_type* type, Location location)
13821 : Expression(EXPRESSION_MAP_DESCRIPTOR, location),
13828 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
13831 do_determine_type(const Type_context*)
13839 do_get_tree(Translate_context* context)
13841 return this->type_->map_descriptor_pointer(context->gogo(),
13846 do_dump_expression(Ast_dump_context*) const;
13849 // The type for which this is the descriptor.
13853 // Dump ast representation for a map descriptor expression.
13856 Map_descriptor_expression::do_dump_expression(
13857 Ast_dump_context* ast_dump_context) const
13859 ast_dump_context->ostream() << "map_descriptor(";
13860 ast_dump_context->dump_type(this->type_);
13861 ast_dump_context->ostream() << ")";
13864 // Make a map descriptor expression.
13867 Expression::make_map_descriptor(Map_type* type, Location location)
13869 return new Map_descriptor_expression(type, location);
13872 // An expression which evaluates to the address of an unnamed label.
13874 class Label_addr_expression : public Expression
13877 Label_addr_expression(Label* label, Location location)
13878 : Expression(EXPRESSION_LABEL_ADDR, location),
13885 { return Type::make_pointer_type(Type::make_void_type()); }
13888 do_determine_type(const Type_context*)
13893 { return new Label_addr_expression(this->label_, this->location()); }
13896 do_get_tree(Translate_context* context)
13898 return expr_to_tree(this->label_->get_addr(context, this->location()));
13902 do_dump_expression(Ast_dump_context* ast_dump_context) const
13903 { ast_dump_context->ostream() << this->label_->name(); }
13906 // The label whose address we are taking.
13910 // Make an expression for the address of an unnamed label.
13913 Expression::make_label_addr(Label* label, Location location)
13915 return new Label_addr_expression(label, location);
13918 // Import an expression. This comes at the end in order to see the
13919 // various class definitions.
13922 Expression::import_expression(Import* imp)
13924 int c = imp->peek_char();
13925 if (imp->match_c_string("- ")
13926 || imp->match_c_string("! ")
13927 || imp->match_c_string("^ "))
13928 return Unary_expression::do_import(imp);
13930 return Binary_expression::do_import(imp);
13931 else if (imp->match_c_string("true")
13932 || imp->match_c_string("false"))
13933 return Boolean_expression::do_import(imp);
13935 return String_expression::do_import(imp);
13936 else if (c == '-' || (c >= '0' && c <= '9'))
13938 // This handles integers, floats and complex constants.
13939 return Integer_expression::do_import(imp);
13941 else if (imp->match_c_string("nil"))
13942 return Nil_expression::do_import(imp);
13943 else if (imp->match_c_string("convert"))
13944 return Type_conversion_expression::do_import(imp);
13947 error_at(imp->location(), "import error: expected expression");
13948 return Expression::make_error(imp->location());
13952 // Class Expression_list.
13954 // Traverse the list.
13957 Expression_list::traverse(Traverse* traverse)
13959 for (Expression_list::iterator p = this->begin();
13965 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
13966 return TRAVERSE_EXIT;
13969 return TRAVERSE_CONTINUE;
13975 Expression_list::copy()
13977 Expression_list* ret = new Expression_list();
13978 for (Expression_list::iterator p = this->begin();
13983 ret->push_back(NULL);
13985 ret->push_back((*p)->copy());
13990 // Return whether an expression list has an error expression.
13993 Expression_list::contains_error() const
13995 for (Expression_list::const_iterator p = this->begin();
13998 if (*p != NULL && (*p)->is_error_expression())