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 // Class Set_and_use_temporary_expression.
1143 Set_and_use_temporary_expression::do_type()
1145 return this->statement_->type();
1148 // Take the address.
1151 Set_and_use_temporary_expression::do_address_taken(bool)
1153 this->statement_->set_is_address_taken();
1156 // Return the backend representation.
1159 Set_and_use_temporary_expression::do_get_tree(Translate_context* context)
1161 Bvariable* bvar = this->statement_->get_backend_variable(context);
1162 tree var_tree = var_to_tree(bvar);
1163 tree expr_tree = this->expr_->get_tree(context);
1164 if (var_tree == error_mark_node || expr_tree == error_mark_node)
1165 return error_mark_node;
1166 Location loc = this->location();
1167 return build2_loc(loc.gcc_location(), COMPOUND_EXPR, TREE_TYPE(var_tree),
1168 build2_loc(loc.gcc_location(), MODIFY_EXPR, void_type_node,
1169 var_tree, expr_tree),
1176 Set_and_use_temporary_expression::do_dump_expression(
1177 Ast_dump_context* ast_dump_context) const
1179 ast_dump_context->ostream() << '(';
1180 ast_dump_context->dump_temp_variable_name(this->statement_);
1181 ast_dump_context->ostream() << " = ";
1182 this->expr_->dump_expression(ast_dump_context);
1183 ast_dump_context->ostream() << ')';
1186 // Make a set-and-use temporary.
1188 Set_and_use_temporary_expression*
1189 Expression::make_set_and_use_temporary(Temporary_statement* statement,
1190 Expression* expr, Location location)
1192 return new Set_and_use_temporary_expression(statement, expr, location);
1195 // A sink expression--a use of the blank identifier _.
1197 class Sink_expression : public Expression
1200 Sink_expression(Location location)
1201 : Expression(EXPRESSION_SINK, location),
1202 type_(NULL), var_(NULL_TREE)
1207 do_discarding_value()
1214 do_determine_type(const Type_context*);
1218 { return new Sink_expression(this->location()); }
1221 do_get_tree(Translate_context*);
1224 do_dump_expression(Ast_dump_context*) const;
1227 // The type of this sink variable.
1229 // The temporary variable we generate.
1233 // Return the type of a sink expression.
1236 Sink_expression::do_type()
1238 if (this->type_ == NULL)
1239 return Type::make_sink_type();
1243 // Determine the type of a sink expression.
1246 Sink_expression::do_determine_type(const Type_context* context)
1248 if (context->type != NULL)
1249 this->type_ = context->type;
1252 // Return a temporary variable for a sink expression. This will
1253 // presumably be a write-only variable which the middle-end will drop.
1256 Sink_expression::do_get_tree(Translate_context* context)
1258 if (this->var_ == NULL_TREE)
1260 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
1261 Btype* bt = this->type_->get_backend(context->gogo());
1262 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
1267 // Ast dump for sink expression.
1270 Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1272 ast_dump_context->ostream() << "_" ;
1275 // Make a sink expression.
1278 Expression::make_sink(Location location)
1280 return new Sink_expression(location);
1283 // Class Func_expression.
1285 // FIXME: Can a function expression appear in a constant expression?
1286 // The value is unchanging. Initializing a constant to the address of
1287 // a function seems like it could work, though there might be little
1293 Func_expression::do_traverse(Traverse* traverse)
1295 return (this->closure_ == NULL
1297 : Expression::traverse(&this->closure_, traverse));
1300 // Return the type of a function expression.
1303 Func_expression::do_type()
1305 if (this->function_->is_function())
1306 return this->function_->func_value()->type();
1307 else if (this->function_->is_function_declaration())
1308 return this->function_->func_declaration_value()->type();
1313 // Get the tree for a function expression without evaluating the
1317 Func_expression::get_tree_without_closure(Gogo* gogo)
1319 Function_type* fntype;
1320 if (this->function_->is_function())
1321 fntype = this->function_->func_value()->type();
1322 else if (this->function_->is_function_declaration())
1323 fntype = this->function_->func_declaration_value()->type();
1327 // Builtin functions are handled specially by Call_expression. We
1328 // can't take their address.
1329 if (fntype->is_builtin())
1331 error_at(this->location(), "invalid use of special builtin function %qs",
1332 this->function_->name().c_str());
1333 return error_mark_node;
1336 Named_object* no = this->function_;
1338 tree id = no->get_id(gogo);
1339 if (id == error_mark_node)
1340 return error_mark_node;
1343 if (no->is_function())
1344 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1345 else if (no->is_function_declaration())
1346 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1350 if (fndecl == error_mark_node)
1351 return error_mark_node;
1353 return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl);
1356 // Get the tree for a function expression. This is used when we take
1357 // the address of a function rather than simply calling it. If the
1358 // function has a closure, we must use a trampoline.
1361 Func_expression::do_get_tree(Translate_context* context)
1363 Gogo* gogo = context->gogo();
1365 tree fnaddr = this->get_tree_without_closure(gogo);
1366 if (fnaddr == error_mark_node)
1367 return error_mark_node;
1369 go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1370 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1371 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1373 // For a normal non-nested function call, that is all we have to do.
1374 if (!this->function_->is_function()
1375 || this->function_->func_value()->enclosing() == NULL)
1377 go_assert(this->closure_ == NULL);
1381 // For a nested function call, we have to always allocate a
1382 // trampoline. If we don't always allocate, then closures will not
1383 // be reliably distinct.
1384 Expression* closure = this->closure_;
1386 if (closure == NULL)
1387 closure_tree = null_pointer_node;
1390 // Get the value of the closure. This will be a pointer to
1391 // space allocated on the heap.
1392 closure_tree = closure->get_tree(context);
1393 if (closure_tree == error_mark_node)
1394 return error_mark_node;
1395 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1398 // Now we need to build some code on the heap. This code will load
1399 // the static chain pointer with the closure and then jump to the
1400 // body of the function. The normal gcc approach is to build the
1401 // code on the stack. Unfortunately we can not do that, as Go
1402 // permits us to return the function pointer.
1404 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1407 // Ast dump for function.
1410 Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1412 ast_dump_context->ostream() << this->function_->name();
1413 if (this->closure_ != NULL)
1415 ast_dump_context->ostream() << " {closure = ";
1416 this->closure_->dump_expression(ast_dump_context);
1417 ast_dump_context->ostream() << "}";
1421 // Make a reference to a function in an expression.
1424 Expression::make_func_reference(Named_object* function, Expression* closure,
1427 return new Func_expression(function, closure, location);
1430 // Class Unknown_expression.
1432 // Return the name of an unknown expression.
1435 Unknown_expression::name() const
1437 return this->named_object_->name();
1440 // Lower a reference to an unknown name.
1443 Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1445 Location location = this->location();
1446 Named_object* no = this->named_object_;
1448 if (!no->is_unknown())
1452 real = no->unknown_value()->real_named_object();
1455 if (this->is_composite_literal_key_)
1457 error_at(location, "reference to undefined name %qs",
1458 this->named_object_->message_name().c_str());
1459 return Expression::make_error(location);
1462 switch (real->classification())
1464 case Named_object::NAMED_OBJECT_CONST:
1465 return Expression::make_const_reference(real, location);
1466 case Named_object::NAMED_OBJECT_TYPE:
1467 return Expression::make_type(real->type_value(), location);
1468 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1469 if (this->is_composite_literal_key_)
1471 error_at(location, "reference to undefined type %qs",
1472 real->message_name().c_str());
1473 return Expression::make_error(location);
1474 case Named_object::NAMED_OBJECT_VAR:
1475 return Expression::make_var_reference(real, location);
1476 case Named_object::NAMED_OBJECT_FUNC:
1477 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1478 return Expression::make_func_reference(real, NULL, location);
1479 case Named_object::NAMED_OBJECT_PACKAGE:
1480 if (this->is_composite_literal_key_)
1482 error_at(location, "unexpected reference to package");
1483 return Expression::make_error(location);
1489 // Dump the ast representation for an unknown expression to a dump context.
1492 Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1494 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1498 // Make a reference to an unknown name.
1501 Expression::make_unknown_reference(Named_object* no, Location location)
1503 return new Unknown_expression(no, location);
1506 // A boolean expression.
1508 class Boolean_expression : public Expression
1511 Boolean_expression(bool val, Location location)
1512 : Expression(EXPRESSION_BOOLEAN, location),
1513 val_(val), type_(NULL)
1521 do_is_constant() const
1528 do_determine_type(const Type_context*);
1535 do_get_tree(Translate_context*)
1536 { return this->val_ ? boolean_true_node : boolean_false_node; }
1539 do_export(Export* exp) const
1540 { exp->write_c_string(this->val_ ? "true" : "false"); }
1543 do_dump_expression(Ast_dump_context* ast_dump_context) const
1544 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1549 // The type as determined by context.
1556 Boolean_expression::do_type()
1558 if (this->type_ == NULL)
1559 this->type_ = Type::make_boolean_type();
1563 // Set the type from the context.
1566 Boolean_expression::do_determine_type(const Type_context* context)
1568 if (this->type_ != NULL && !this->type_->is_abstract())
1570 else if (context->type != NULL && context->type->is_boolean_type())
1571 this->type_ = context->type;
1572 else if (!context->may_be_abstract)
1573 this->type_ = Type::lookup_bool_type();
1576 // Import a boolean constant.
1579 Boolean_expression::do_import(Import* imp)
1581 if (imp->peek_char() == 't')
1583 imp->require_c_string("true");
1584 return Expression::make_boolean(true, imp->location());
1588 imp->require_c_string("false");
1589 return Expression::make_boolean(false, imp->location());
1593 // Make a boolean expression.
1596 Expression::make_boolean(bool val, Location location)
1598 return new Boolean_expression(val, location);
1601 // Class String_expression.
1606 String_expression::do_type()
1608 if (this->type_ == NULL)
1609 this->type_ = Type::make_string_type();
1613 // Set the type from the context.
1616 String_expression::do_determine_type(const Type_context* context)
1618 if (this->type_ != NULL && !this->type_->is_abstract())
1620 else if (context->type != NULL && context->type->is_string_type())
1621 this->type_ = context->type;
1622 else if (!context->may_be_abstract)
1623 this->type_ = Type::lookup_string_type();
1626 // Build a string constant.
1629 String_expression::do_get_tree(Translate_context* context)
1631 return context->gogo()->go_string_constant_tree(this->val_);
1634 // Write string literal to string dump.
1637 String_expression::export_string(String_dump* exp,
1638 const String_expression* str)
1641 s.reserve(str->val_.length() * 4 + 2);
1643 for (std::string::const_iterator p = str->val_.begin();
1644 p != str->val_.end();
1647 if (*p == '\\' || *p == '"')
1652 else if (*p >= 0x20 && *p < 0x7f)
1654 else if (*p == '\n')
1656 else if (*p == '\t')
1661 unsigned char c = *p;
1662 unsigned int dig = c >> 4;
1663 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1665 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1669 exp->write_string(s);
1672 // Export a string expression.
1675 String_expression::do_export(Export* exp) const
1677 String_expression::export_string(exp, this);
1680 // Import a string expression.
1683 String_expression::do_import(Import* imp)
1685 imp->require_c_string("\"");
1689 int c = imp->get_char();
1690 if (c == '"' || c == -1)
1693 val += static_cast<char>(c);
1696 c = imp->get_char();
1697 if (c == '\\' || c == '"')
1698 val += static_cast<char>(c);
1705 c = imp->get_char();
1706 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1707 c = imp->get_char();
1708 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1709 char v = (vh << 4) | vl;
1714 error_at(imp->location(), "bad string constant");
1715 return Expression::make_error(imp->location());
1719 return Expression::make_string(val, imp->location());
1722 // Ast dump for string expression.
1725 String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1727 String_expression::export_string(ast_dump_context, this);
1730 // Make a string expression.
1733 Expression::make_string(const std::string& val, Location location)
1735 return new String_expression(val, location);
1738 // Make an integer expression.
1740 class Integer_expression : public Expression
1743 Integer_expression(const mpz_t* val, Type* type, Location location)
1744 : Expression(EXPRESSION_INTEGER, location),
1746 { mpz_init_set(this->val_, *val); }
1751 // Return whether VAL fits in the type.
1753 check_constant(mpz_t val, Type*, Location);
1755 // Write VAL to string dump.
1757 export_integer(String_dump* exp, const mpz_t val);
1759 // Write VAL to dump context.
1761 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1765 do_is_constant() const
1769 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1775 do_determine_type(const Type_context* context);
1778 do_check_types(Gogo*);
1781 do_get_tree(Translate_context*);
1785 { return Expression::make_integer(&this->val_, this->type_,
1786 this->location()); }
1789 do_export(Export*) const;
1792 do_dump_expression(Ast_dump_context*) const;
1795 // The integer value.
1801 // Return an integer constant value.
1804 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1807 if (this->type_ != NULL)
1808 *ptype = this->type_;
1809 mpz_set(val, this->val_);
1813 // Return the current type. If we haven't set the type yet, we return
1814 // an abstract integer type.
1817 Integer_expression::do_type()
1819 if (this->type_ == NULL)
1820 this->type_ = Type::make_abstract_integer_type();
1824 // Set the type of the integer value. Here we may switch from an
1825 // abstract type to a real type.
1828 Integer_expression::do_determine_type(const Type_context* context)
1830 if (this->type_ != NULL && !this->type_->is_abstract())
1832 else if (context->type != NULL
1833 && (context->type->integer_type() != NULL
1834 || context->type->float_type() != NULL
1835 || context->type->complex_type() != NULL))
1836 this->type_ = context->type;
1837 else if (!context->may_be_abstract)
1838 this->type_ = Type::lookup_integer_type("int");
1841 // Return true if the integer VAL fits in the range of the type TYPE.
1842 // Otherwise give an error and return false. TYPE may be NULL.
1845 Integer_expression::check_constant(mpz_t val, Type* type,
1850 Integer_type* itype = type->integer_type();
1851 if (itype == NULL || itype->is_abstract())
1854 int bits = mpz_sizeinbase(val, 2);
1856 if (itype->is_unsigned())
1858 // For an unsigned type we can only accept a nonnegative number,
1859 // and we must be able to represent at least BITS.
1860 if (mpz_sgn(val) >= 0
1861 && bits <= itype->bits())
1866 // For a signed type we need an extra bit to indicate the sign.
1867 // We have to handle the most negative integer specially.
1868 if (bits + 1 <= itype->bits()
1869 || (bits <= itype->bits()
1871 && (mpz_scan1(val, 0)
1872 == static_cast<unsigned long>(itype->bits() - 1))
1873 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1877 error_at(location, "integer constant overflow");
1881 // Check the type of an integer constant.
1884 Integer_expression::do_check_types(Gogo*)
1886 if (this->type_ == NULL)
1888 if (!Integer_expression::check_constant(this->val_, this->type_,
1890 this->set_is_error();
1893 // Get a tree for an integer constant.
1896 Integer_expression::do_get_tree(Translate_context* context)
1898 Gogo* gogo = context->gogo();
1900 if (this->type_ != NULL && !this->type_->is_abstract())
1901 type = type_to_tree(this->type_->get_backend(gogo));
1902 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1904 // We are converting to an abstract floating point type.
1905 Type* ftype = Type::lookup_float_type("float64");
1906 type = type_to_tree(ftype->get_backend(gogo));
1908 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1910 // We are converting to an abstract complex type.
1911 Type* ctype = Type::lookup_complex_type("complex128");
1912 type = type_to_tree(ctype->get_backend(gogo));
1916 // If we still have an abstract type here, then this is being
1917 // used in a constant expression which didn't get reduced for
1918 // some reason. Use a type which will fit the value. We use <,
1919 // not <=, because we need an extra bit for the sign bit.
1920 int bits = mpz_sizeinbase(this->val_, 2);
1921 if (bits < INT_TYPE_SIZE)
1923 Type* t = Type::lookup_integer_type("int");
1924 type = type_to_tree(t->get_backend(gogo));
1928 Type* t = Type::lookup_integer_type("int64");
1929 type = type_to_tree(t->get_backend(gogo));
1932 type = long_long_integer_type_node;
1934 return Expression::integer_constant_tree(this->val_, type);
1937 // Write VAL to export data.
1940 Integer_expression::export_integer(String_dump* exp, const mpz_t val)
1942 char* s = mpz_get_str(NULL, 10, val);
1943 exp->write_c_string(s);
1947 // Export an integer in a constant expression.
1950 Integer_expression::do_export(Export* exp) const
1952 Integer_expression::export_integer(exp, this->val_);
1953 // A trailing space lets us reliably identify the end of the number.
1954 exp->write_c_string(" ");
1957 // Import an integer, floating point, or complex value. This handles
1958 // all these types because they all start with digits.
1961 Integer_expression::do_import(Import* imp)
1963 std::string num = imp->read_identifier();
1964 imp->require_c_string(" ");
1965 if (!num.empty() && num[num.length() - 1] == 'i')
1968 size_t plus_pos = num.find('+', 1);
1969 size_t minus_pos = num.find('-', 1);
1971 if (plus_pos == std::string::npos)
1973 else if (minus_pos == std::string::npos)
1977 error_at(imp->location(), "bad number in import data: %qs",
1979 return Expression::make_error(imp->location());
1981 if (pos == std::string::npos)
1982 mpfr_set_ui(real, 0, GMP_RNDN);
1985 std::string real_str = num.substr(0, pos);
1986 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1988 error_at(imp->location(), "bad number in import data: %qs",
1990 return Expression::make_error(imp->location());
1994 std::string imag_str;
1995 if (pos == std::string::npos)
1998 imag_str = num.substr(pos);
1999 imag_str = imag_str.substr(0, imag_str.size() - 1);
2001 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
2003 error_at(imp->location(), "bad number in import data: %qs",
2005 return Expression::make_error(imp->location());
2007 Expression* ret = Expression::make_complex(&real, &imag, NULL,
2013 else if (num.find('.') == std::string::npos
2014 && num.find('E') == std::string::npos)
2017 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
2019 error_at(imp->location(), "bad number in import data: %qs",
2021 return Expression::make_error(imp->location());
2023 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
2030 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
2032 error_at(imp->location(), "bad number in import data: %qs",
2034 return Expression::make_error(imp->location());
2036 Expression* ret = Expression::make_float(&val, NULL, imp->location());
2041 // Ast dump for integer expression.
2044 Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2046 Integer_expression::export_integer(ast_dump_context, this->val_);
2049 // Build a new integer value.
2052 Expression::make_integer(const mpz_t* val, Type* type,
2055 return new Integer_expression(val, type, location);
2060 class Float_expression : public Expression
2063 Float_expression(const mpfr_t* val, Type* type, Location location)
2064 : Expression(EXPRESSION_FLOAT, location),
2067 mpfr_init_set(this->val_, *val, GMP_RNDN);
2070 // Constrain VAL to fit into TYPE.
2072 constrain_float(mpfr_t val, Type* type);
2074 // Return whether VAL fits in the type.
2076 check_constant(mpfr_t val, Type*, Location);
2078 // Write VAL to export data.
2080 export_float(String_dump* exp, const mpfr_t val);
2082 // Write VAL to dump file.
2084 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
2088 do_is_constant() const
2092 do_float_constant_value(mpfr_t val, Type**) const;
2098 do_determine_type(const Type_context*);
2101 do_check_types(Gogo*);
2105 { return Expression::make_float(&this->val_, this->type_,
2106 this->location()); }
2109 do_get_tree(Translate_context*);
2112 do_export(Export*) const;
2115 do_dump_expression(Ast_dump_context*) const;
2118 // The floating point value.
2124 // Constrain VAL to fit into TYPE.
2127 Float_expression::constrain_float(mpfr_t val, Type* type)
2129 Float_type* ftype = type->float_type();
2130 if (ftype != NULL && !ftype->is_abstract())
2131 mpfr_prec_round(val, ftype->bits(), GMP_RNDN);
2134 // Return a floating point constant value.
2137 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2139 if (this->type_ != NULL)
2140 *ptype = this->type_;
2141 mpfr_set(val, this->val_, GMP_RNDN);
2145 // Return the current type. If we haven't set the type yet, we return
2146 // an abstract float type.
2149 Float_expression::do_type()
2151 if (this->type_ == NULL)
2152 this->type_ = Type::make_abstract_float_type();
2156 // Set the type of the float value. Here we may switch from an
2157 // abstract type to a real type.
2160 Float_expression::do_determine_type(const Type_context* context)
2162 if (this->type_ != NULL && !this->type_->is_abstract())
2164 else if (context->type != NULL
2165 && (context->type->integer_type() != NULL
2166 || context->type->float_type() != NULL
2167 || context->type->complex_type() != NULL))
2168 this->type_ = context->type;
2169 else if (!context->may_be_abstract)
2170 this->type_ = Type::lookup_float_type("float64");
2173 // Return true if the floating point value VAL fits in the range of
2174 // the type TYPE. Otherwise give an error and return false. TYPE may
2178 Float_expression::check_constant(mpfr_t val, Type* type,
2183 Float_type* ftype = type->float_type();
2184 if (ftype == NULL || ftype->is_abstract())
2187 // A NaN or Infinity always fits in the range of the type.
2188 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
2191 mp_exp_t exp = mpfr_get_exp(val);
2193 switch (ftype->bits())
2206 error_at(location, "floating point constant overflow");
2212 // Check the type of a float value.
2215 Float_expression::do_check_types(Gogo*)
2217 if (this->type_ == NULL)
2220 if (!Float_expression::check_constant(this->val_, this->type_,
2222 this->set_is_error();
2224 Integer_type* integer_type = this->type_->integer_type();
2225 if (integer_type != NULL)
2227 if (!mpfr_integer_p(this->val_))
2228 this->report_error(_("floating point constant truncated to integer"));
2231 go_assert(!integer_type->is_abstract());
2234 mpfr_get_z(ival, this->val_, GMP_RNDN);
2235 Integer_expression::check_constant(ival, integer_type,
2242 // Get a tree for a float constant.
2245 Float_expression::do_get_tree(Translate_context* context)
2247 Gogo* gogo = context->gogo();
2249 if (this->type_ != NULL && !this->type_->is_abstract())
2250 type = type_to_tree(this->type_->get_backend(gogo));
2251 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2253 // We have an abstract integer type. We just hope for the best.
2254 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
2258 // If we still have an abstract type here, then this is being
2259 // used in a constant expression which didn't get reduced. We
2260 // just use float64 and hope for the best.
2261 Type* ft = Type::lookup_float_type("float64");
2262 type = type_to_tree(ft->get_backend(gogo));
2264 return Expression::float_constant_tree(this->val_, type);
2267 // Write a floating point number to a string dump.
2270 Float_expression::export_float(String_dump *exp, const mpfr_t val)
2273 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2275 exp->write_c_string("-");
2276 exp->write_c_string("0.");
2277 exp->write_c_string(*s == '-' ? s + 1 : s);
2280 snprintf(buf, sizeof buf, "E%ld", exponent);
2281 exp->write_c_string(buf);
2284 // Export a floating point number in a constant expression.
2287 Float_expression::do_export(Export* exp) const
2289 Float_expression::export_float(exp, this->val_);
2290 // A trailing space lets us reliably identify the end of the number.
2291 exp->write_c_string(" ");
2294 // Dump a floating point number to the dump file.
2297 Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2299 Float_expression::export_float(ast_dump_context, this->val_);
2302 // Make a float expression.
2305 Expression::make_float(const mpfr_t* val, Type* type, Location location)
2307 return new Float_expression(val, type, location);
2312 class Complex_expression : public Expression
2315 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2317 : Expression(EXPRESSION_COMPLEX, location),
2320 mpfr_init_set(this->real_, *real, GMP_RNDN);
2321 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2324 // Constrain REAL/IMAG to fit into TYPE.
2326 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2328 // Return whether REAL/IMAG fits in the type.
2330 check_constant(mpfr_t real, mpfr_t imag, Type*, Location);
2332 // Write REAL/IMAG to string dump.
2334 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
2336 // Write REAL/IMAG to dump context.
2338 dump_complex(Ast_dump_context* ast_dump_context,
2339 const mpfr_t real, const mpfr_t val);
2343 do_is_constant() const
2347 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2353 do_determine_type(const Type_context*);
2356 do_check_types(Gogo*);
2361 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2366 do_get_tree(Translate_context*);
2369 do_export(Export*) const;
2372 do_dump_expression(Ast_dump_context*) const;
2377 // The imaginary part;
2379 // The type if known.
2383 // Constrain REAL/IMAG to fit into TYPE.
2386 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2388 Complex_type* ctype = type->complex_type();
2389 if (ctype != NULL && !ctype->is_abstract())
2391 mpfr_prec_round(real, ctype->bits() / 2, GMP_RNDN);
2392 mpfr_prec_round(imag, ctype->bits() / 2, GMP_RNDN);
2396 // Return a complex constant value.
2399 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2402 if (this->type_ != NULL)
2403 *ptype = this->type_;
2404 mpfr_set(real, this->real_, GMP_RNDN);
2405 mpfr_set(imag, this->imag_, GMP_RNDN);
2409 // Return the current type. If we haven't set the type yet, we return
2410 // an abstract complex type.
2413 Complex_expression::do_type()
2415 if (this->type_ == NULL)
2416 this->type_ = Type::make_abstract_complex_type();
2420 // Set the type of the complex value. Here we may switch from an
2421 // abstract type to a real type.
2424 Complex_expression::do_determine_type(const Type_context* context)
2426 if (this->type_ != NULL && !this->type_->is_abstract())
2428 else if (context->type != NULL
2429 && context->type->complex_type() != NULL)
2430 this->type_ = context->type;
2431 else if (!context->may_be_abstract)
2432 this->type_ = Type::lookup_complex_type("complex128");
2435 // Return true if the complex value REAL/IMAG fits in the range of the
2436 // type TYPE. Otherwise give an error and return false. TYPE may be
2440 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2445 Complex_type* ctype = type->complex_type();
2446 if (ctype == NULL || ctype->is_abstract())
2450 switch (ctype->bits())
2462 // A NaN or Infinity always fits in the range of the type.
2463 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2465 if (mpfr_get_exp(real) > max_exp)
2467 error_at(location, "complex real part constant overflow");
2472 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2474 if (mpfr_get_exp(imag) > max_exp)
2476 error_at(location, "complex imaginary part constant overflow");
2484 // Check the type of a complex value.
2487 Complex_expression::do_check_types(Gogo*)
2489 if (this->type_ == NULL)
2492 if (!Complex_expression::check_constant(this->real_, this->imag_,
2493 this->type_, this->location()))
2494 this->set_is_error();
2497 // Get a tree for a complex constant.
2500 Complex_expression::do_get_tree(Translate_context* context)
2502 Gogo* gogo = context->gogo();
2504 if (this->type_ != NULL && !this->type_->is_abstract())
2505 type = type_to_tree(this->type_->get_backend(gogo));
2508 // If we still have an abstract type here, this this is being
2509 // used in a constant expression which didn't get reduced. We
2510 // just use complex128 and hope for the best.
2511 Type* ct = Type::lookup_complex_type("complex128");
2512 type = type_to_tree(ct->get_backend(gogo));
2514 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2517 // Write REAL/IMAG to export data.
2520 Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
2523 if (!mpfr_zero_p(real))
2525 Float_expression::export_float(exp, real);
2526 if (mpfr_sgn(imag) > 0)
2527 exp->write_c_string("+");
2529 Float_expression::export_float(exp, imag);
2530 exp->write_c_string("i");
2533 // Export a complex number in a constant expression.
2536 Complex_expression::do_export(Export* exp) const
2538 Complex_expression::export_complex(exp, this->real_, this->imag_);
2539 // A trailing space lets us reliably identify the end of the number.
2540 exp->write_c_string(" ");
2543 // Dump a complex expression to the dump file.
2546 Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2548 Complex_expression::export_complex(ast_dump_context,
2553 // Make a complex expression.
2556 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2559 return new Complex_expression(real, imag, type, location);
2562 // Find a named object in an expression.
2564 class Find_named_object : public Traverse
2567 Find_named_object(Named_object* no)
2568 : Traverse(traverse_expressions),
2569 no_(no), found_(false)
2572 // Whether we found the object.
2575 { return this->found_; }
2579 expression(Expression**);
2582 // The object we are looking for.
2584 // Whether we found it.
2588 // A reference to a const in an expression.
2590 class Const_expression : public Expression
2593 Const_expression(Named_object* constant, Location location)
2594 : Expression(EXPRESSION_CONST_REFERENCE, location),
2595 constant_(constant), type_(NULL), seen_(false)
2600 { return this->constant_; }
2602 // Check that the initializer does not refer to the constant itself.
2604 check_for_init_loop();
2608 do_traverse(Traverse*);
2611 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2614 do_is_constant() const
2618 do_integer_constant_value(bool, mpz_t val, Type**) const;
2621 do_float_constant_value(mpfr_t val, Type**) const;
2624 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2627 do_string_constant_value(std::string* val) const
2628 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2633 // The type of a const is set by the declaration, not the use.
2635 do_determine_type(const Type_context*);
2638 do_check_types(Gogo*);
2645 do_get_tree(Translate_context* context);
2647 // When exporting a reference to a const as part of a const
2648 // expression, we export the value. We ignore the fact that it has
2651 do_export(Export* exp) const
2652 { this->constant_->const_value()->expr()->export_expression(exp); }
2655 do_dump_expression(Ast_dump_context*) const;
2659 Named_object* constant_;
2660 // The type of this reference. This is used if the constant has an
2663 // Used to prevent infinite recursion when a constant incorrectly
2664 // refers to itself.
2671 Const_expression::do_traverse(Traverse* traverse)
2673 if (this->type_ != NULL)
2674 return Type::traverse(this->type_, traverse);
2675 return TRAVERSE_CONTINUE;
2678 // Lower a constant expression. This is where we convert the
2679 // predeclared constant iota into an integer value.
2682 Const_expression::do_lower(Gogo* gogo, Named_object*,
2683 Statement_inserter*, int iota_value)
2685 if (this->constant_->const_value()->expr()->classification()
2688 if (iota_value == -1)
2690 error_at(this->location(),
2691 "iota is only defined in const declarations");
2695 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2696 Expression* ret = Expression::make_integer(&val, NULL,
2702 // Make sure that the constant itself has been lowered.
2703 gogo->lower_constant(this->constant_);
2708 // Return an integer constant value.
2711 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2718 if (this->type_ != NULL)
2719 ctype = this->type_;
2721 ctype = this->constant_->const_value()->type();
2722 if (ctype != NULL && ctype->integer_type() == NULL)
2725 Expression* e = this->constant_->const_value()->expr();
2730 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2732 this->seen_ = false;
2736 && !Integer_expression::check_constant(val, ctype, this->location()))
2739 *ptype = ctype != NULL ? ctype : t;
2743 // Return a floating point constant value.
2746 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2752 if (this->type_ != NULL)
2753 ctype = this->type_;
2755 ctype = this->constant_->const_value()->type();
2756 if (ctype != NULL && ctype->float_type() == NULL)
2762 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2765 this->seen_ = false;
2767 if (r && ctype != NULL)
2769 if (!Float_expression::check_constant(val, ctype, this->location()))
2771 Float_expression::constrain_float(val, ctype);
2773 *ptype = ctype != NULL ? ctype : t;
2777 // Return a complex constant value.
2780 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2787 if (this->type_ != NULL)
2788 ctype = this->type_;
2790 ctype = this->constant_->const_value()->type();
2791 if (ctype != NULL && ctype->complex_type() == NULL)
2797 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2801 this->seen_ = false;
2803 if (r && ctype != NULL)
2805 if (!Complex_expression::check_constant(real, imag, ctype,
2808 Complex_expression::constrain_complex(real, imag, ctype);
2810 *ptype = ctype != NULL ? ctype : t;
2814 // Return the type of the const reference.
2817 Const_expression::do_type()
2819 if (this->type_ != NULL)
2822 Named_constant* nc = this->constant_->const_value();
2824 if (this->seen_ || nc->lowering())
2826 this->report_error(_("constant refers to itself"));
2827 this->type_ = Type::make_error_type();
2833 Type* ret = nc->type();
2837 this->seen_ = false;
2841 // During parsing, a named constant may have a NULL type, but we
2842 // must not return a NULL type here.
2843 ret = nc->expr()->type();
2845 this->seen_ = false;
2850 // Set the type of the const reference.
2853 Const_expression::do_determine_type(const Type_context* context)
2855 Type* ctype = this->constant_->const_value()->type();
2856 Type* cetype = (ctype != NULL
2858 : this->constant_->const_value()->expr()->type());
2859 if (ctype != NULL && !ctype->is_abstract())
2861 else if (context->type != NULL
2862 && (context->type->integer_type() != NULL
2863 || context->type->float_type() != NULL
2864 || context->type->complex_type() != NULL)
2865 && (cetype->integer_type() != NULL
2866 || cetype->float_type() != NULL
2867 || cetype->complex_type() != NULL))
2868 this->type_ = context->type;
2869 else if (context->type != NULL
2870 && context->type->is_string_type()
2871 && cetype->is_string_type())
2872 this->type_ = context->type;
2873 else if (context->type != NULL
2874 && context->type->is_boolean_type()
2875 && cetype->is_boolean_type())
2876 this->type_ = context->type;
2877 else if (!context->may_be_abstract)
2879 if (cetype->is_abstract())
2880 cetype = cetype->make_non_abstract_type();
2881 this->type_ = cetype;
2885 // Check for a loop in which the initializer of a constant refers to
2886 // the constant itself.
2889 Const_expression::check_for_init_loop()
2891 if (this->type_ != NULL && this->type_->is_error())
2896 this->report_error(_("constant refers to itself"));
2897 this->type_ = Type::make_error_type();
2901 Expression* init = this->constant_->const_value()->expr();
2902 Find_named_object find_named_object(this->constant_);
2905 Expression::traverse(&init, &find_named_object);
2906 this->seen_ = false;
2908 if (find_named_object.found())
2910 if (this->type_ == NULL || !this->type_->is_error())
2912 this->report_error(_("constant refers to itself"));
2913 this->type_ = Type::make_error_type();
2919 // Check types of a const reference.
2922 Const_expression::do_check_types(Gogo*)
2924 if (this->type_ != NULL && this->type_->is_error())
2927 this->check_for_init_loop();
2929 if (this->type_ == NULL || this->type_->is_abstract())
2932 // Check for integer overflow.
2933 if (this->type_->integer_type() != NULL)
2938 if (!this->integer_constant_value(true, ival, &dummy))
2942 Expression* cexpr = this->constant_->const_value()->expr();
2943 if (cexpr->float_constant_value(fval, &dummy))
2945 if (!mpfr_integer_p(fval))
2946 this->report_error(_("floating point constant "
2947 "truncated to integer"));
2950 mpfr_get_z(ival, fval, GMP_RNDN);
2951 Integer_expression::check_constant(ival, this->type_,
2961 // Return a tree for the const reference.
2964 Const_expression::do_get_tree(Translate_context* context)
2966 Gogo* gogo = context->gogo();
2968 if (this->type_ == NULL)
2969 type_tree = NULL_TREE;
2972 type_tree = type_to_tree(this->type_->get_backend(gogo));
2973 if (type_tree == error_mark_node)
2974 return error_mark_node;
2977 // If the type has been set for this expression, but the underlying
2978 // object is an abstract int or float, we try to get the abstract
2979 // value. Otherwise we may lose something in the conversion.
2980 if (this->type_ != NULL
2981 && (this->constant_->const_value()->type() == NULL
2982 || this->constant_->const_value()->type()->is_abstract()))
2984 Expression* expr = this->constant_->const_value()->expr();
2988 if (expr->integer_constant_value(true, ival, &t))
2990 tree ret = Expression::integer_constant_tree(ival, type_tree);
2998 if (expr->float_constant_value(fval, &t))
3000 tree ret = Expression::float_constant_tree(fval, type_tree);
3007 if (expr->complex_constant_value(fval, imag, &t))
3009 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
3018 tree const_tree = this->constant_->get_tree(gogo, context->function());
3019 if (this->type_ == NULL
3020 || const_tree == error_mark_node
3021 || TREE_TYPE(const_tree) == error_mark_node)
3025 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
3026 ret = fold_convert(type_tree, const_tree);
3027 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
3028 ret = fold(convert_to_integer(type_tree, const_tree));
3029 else if (TREE_CODE(type_tree) == REAL_TYPE)
3030 ret = fold(convert_to_real(type_tree, const_tree));
3031 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
3032 ret = fold(convert_to_complex(type_tree, const_tree));
3038 // Dump ast representation for constant expression.
3041 Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
3043 ast_dump_context->ostream() << this->constant_->name();
3046 // Make a reference to a constant in an expression.
3049 Expression::make_const_reference(Named_object* constant,
3052 return new Const_expression(constant, location);
3055 // Find a named object in an expression.
3058 Find_named_object::expression(Expression** pexpr)
3060 switch ((*pexpr)->classification())
3062 case Expression::EXPRESSION_CONST_REFERENCE:
3064 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
3065 if (ce->named_object() == this->no_)
3068 // We need to check a constant initializer explicitly, as
3069 // loops here will not be caught by the loop checking for
3070 // variable initializers.
3071 ce->check_for_init_loop();
3073 return TRAVERSE_CONTINUE;
3076 case Expression::EXPRESSION_VAR_REFERENCE:
3077 if ((*pexpr)->var_expression()->named_object() == this->no_)
3079 return TRAVERSE_CONTINUE;
3080 case Expression::EXPRESSION_FUNC_REFERENCE:
3081 if ((*pexpr)->func_expression()->named_object() == this->no_)
3083 return TRAVERSE_CONTINUE;
3085 return TRAVERSE_CONTINUE;
3087 this->found_ = true;
3088 return TRAVERSE_EXIT;
3093 class Nil_expression : public Expression
3096 Nil_expression(Location location)
3097 : Expression(EXPRESSION_NIL, location)
3105 do_is_constant() const
3110 { return Type::make_nil_type(); }
3113 do_determine_type(const Type_context*)
3121 do_get_tree(Translate_context*)
3122 { return null_pointer_node; }
3125 do_export(Export* exp) const
3126 { exp->write_c_string("nil"); }
3129 do_dump_expression(Ast_dump_context* ast_dump_context) const
3130 { ast_dump_context->ostream() << "nil"; }
3133 // Import a nil expression.
3136 Nil_expression::do_import(Import* imp)
3138 imp->require_c_string("nil");
3139 return Expression::make_nil(imp->location());
3142 // Make a nil expression.
3145 Expression::make_nil(Location location)
3147 return new Nil_expression(location);
3150 // The value of the predeclared constant iota. This is little more
3151 // than a marker. This will be lowered to an integer in
3152 // Const_expression::do_lower, which is where we know the value that
3155 class Iota_expression : public Parser_expression
3158 Iota_expression(Location location)
3159 : Parser_expression(EXPRESSION_IOTA, location)
3164 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
3165 { go_unreachable(); }
3167 // There should only ever be one of these.
3170 { go_unreachable(); }
3173 do_dump_expression(Ast_dump_context* ast_dump_context) const
3174 { ast_dump_context->ostream() << "iota"; }
3177 // Make an iota expression. This is only called for one case: the
3178 // value of the predeclared constant iota.
3181 Expression::make_iota()
3183 static Iota_expression iota_expression(Linemap::unknown_location());
3184 return &iota_expression;
3187 // A type conversion expression.
3189 class Type_conversion_expression : public Expression
3192 Type_conversion_expression(Type* type, Expression* expr,
3194 : Expression(EXPRESSION_CONVERSION, location),
3195 type_(type), expr_(expr), may_convert_function_types_(false)
3198 // Return the type to which we are converting.
3201 { return this->type_; }
3203 // Return the expression which we are converting.
3206 { return this->expr_; }
3208 // Permit converting from one function type to another. This is
3209 // used internally for method expressions.
3211 set_may_convert_function_types()
3213 this->may_convert_function_types_ = true;
3216 // Import a type conversion expression.
3222 do_traverse(Traverse* traverse);
3225 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3228 do_is_constant() const
3229 { return this->expr_->is_constant(); }
3232 do_integer_constant_value(bool, mpz_t, Type**) const;
3235 do_float_constant_value(mpfr_t, Type**) const;
3238 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3241 do_string_constant_value(std::string*) const;
3245 { return this->type_; }
3248 do_determine_type(const Type_context*)
3250 Type_context subcontext(this->type_, false);
3251 this->expr_->determine_type(&subcontext);
3255 do_check_types(Gogo*);
3260 return new Type_conversion_expression(this->type_, this->expr_->copy(),
3265 do_get_tree(Translate_context* context);
3268 do_export(Export*) const;
3271 do_dump_expression(Ast_dump_context*) const;
3274 // The type to convert to.
3276 // The expression to convert.
3278 // True if this is permitted to convert function types. This is
3279 // used internally for method expressions.
3280 bool may_convert_function_types_;
3286 Type_conversion_expression::do_traverse(Traverse* traverse)
3288 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3289 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3290 return TRAVERSE_EXIT;
3291 return TRAVERSE_CONTINUE;
3294 // Convert to a constant at lowering time.
3297 Type_conversion_expression::do_lower(Gogo*, Named_object*,
3298 Statement_inserter*, int)
3300 Type* type = this->type_;
3301 Expression* val = this->expr_;
3302 Location location = this->location();
3304 if (type->integer_type() != NULL)
3309 if (val->integer_constant_value(false, ival, &dummy))
3311 if (!Integer_expression::check_constant(ival, type, location))
3312 mpz_set_ui(ival, 0);
3313 Expression* ret = Expression::make_integer(&ival, type, location);
3320 if (val->float_constant_value(fval, &dummy))
3322 if (!mpfr_integer_p(fval))
3325 "floating point constant truncated to integer");
3326 return Expression::make_error(location);
3328 mpfr_get_z(ival, fval, GMP_RNDN);
3329 if (!Integer_expression::check_constant(ival, type, location))
3330 mpz_set_ui(ival, 0);
3331 Expression* ret = Expression::make_integer(&ival, type, location);
3340 if (type->float_type() != NULL)
3345 if (val->float_constant_value(fval, &dummy))
3347 if (!Float_expression::check_constant(fval, type, location))
3348 mpfr_set_ui(fval, 0, GMP_RNDN);
3349 Float_expression::constrain_float(fval, type);
3350 Expression *ret = Expression::make_float(&fval, type, location);
3357 if (type->complex_type() != NULL)
3364 if (val->complex_constant_value(real, imag, &dummy))
3366 if (!Complex_expression::check_constant(real, imag, type, location))
3368 mpfr_set_ui(real, 0, GMP_RNDN);
3369 mpfr_set_ui(imag, 0, GMP_RNDN);
3371 Complex_expression::constrain_complex(real, imag, type);
3372 Expression* ret = Expression::make_complex(&real, &imag, type,
3382 if (type->is_slice_type())
3384 Type* element_type = type->array_type()->element_type()->forwarded();
3385 bool is_byte = (element_type->integer_type() != NULL
3386 && element_type->integer_type()->is_byte());
3387 bool is_rune = (element_type->integer_type() != NULL
3388 && element_type->integer_type()->is_rune());
3389 if (is_byte || is_rune)
3392 if (val->string_constant_value(&s))
3394 Expression_list* vals = new Expression_list();
3397 for (std::string::const_iterator p = s.begin();
3402 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3403 Expression* v = Expression::make_integer(&val,
3412 const char *p = s.data();
3413 const char *pend = s.data() + s.length();
3417 int adv = Lex::fetch_char(p, &c);
3420 warning_at(this->location(), 0,
3421 "invalid UTF-8 encoding");
3426 mpz_init_set_ui(val, c);
3427 Expression* v = Expression::make_integer(&val,
3435 return Expression::make_slice_composite_literal(type, vals,
3444 // Return the constant integer value if there is one.
3447 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3451 if (this->type_->integer_type() == NULL)
3457 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3459 if (!Integer_expression::check_constant(ival, this->type_,
3467 *ptype = this->type_;
3474 if (this->expr_->float_constant_value(fval, &dummy))
3476 mpfr_get_z(val, fval, GMP_RNDN);
3478 if (!Integer_expression::check_constant(val, this->type_,
3481 *ptype = this->type_;
3489 // Return the constant floating point value if there is one.
3492 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3495 if (this->type_->float_type() == NULL)
3501 if (this->expr_->float_constant_value(fval, &dummy))
3503 if (!Float_expression::check_constant(fval, this->type_,
3509 mpfr_set(val, fval, GMP_RNDN);
3511 Float_expression::constrain_float(val, this->type_);
3512 *ptype = this->type_;
3520 // Return the constant complex value if there is one.
3523 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3527 if (this->type_->complex_type() == NULL)
3535 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3537 if (!Complex_expression::check_constant(rval, ival, this->type_,
3544 mpfr_set(real, rval, GMP_RNDN);
3545 mpfr_set(imag, ival, GMP_RNDN);
3548 Complex_expression::constrain_complex(real, imag, this->type_);
3549 *ptype = this->type_;
3558 // Return the constant string value if there is one.
3561 Type_conversion_expression::do_string_constant_value(std::string* val) const
3563 if (this->type_->is_string_type()
3564 && this->expr_->type()->integer_type() != NULL)
3569 if (this->expr_->integer_constant_value(false, ival, &dummy))
3571 unsigned long ulval = mpz_get_ui(ival);
3572 if (mpz_cmp_ui(ival, ulval) == 0)
3574 Lex::append_char(ulval, true, val, this->location());
3582 // FIXME: Could handle conversion from const []int here.
3587 // Check that types are convertible.
3590 Type_conversion_expression::do_check_types(Gogo*)
3592 Type* type = this->type_;
3593 Type* expr_type = this->expr_->type();
3596 if (type->is_error() || expr_type->is_error())
3598 this->set_is_error();
3602 if (this->may_convert_function_types_
3603 && type->function_type() != NULL
3604 && expr_type->function_type() != NULL)
3607 if (Type::are_convertible(type, expr_type, &reason))
3610 error_at(this->location(), "%s", reason.c_str());
3611 this->set_is_error();
3614 // Get a tree for a type conversion.
3617 Type_conversion_expression::do_get_tree(Translate_context* context)
3619 Gogo* gogo = context->gogo();
3620 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
3621 tree expr_tree = this->expr_->get_tree(context);
3623 if (type_tree == error_mark_node
3624 || expr_tree == error_mark_node
3625 || TREE_TYPE(expr_tree) == error_mark_node)
3626 return error_mark_node;
3628 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3629 return fold_convert(type_tree, expr_tree);
3631 Type* type = this->type_;
3632 Type* expr_type = this->expr_->type();
3634 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3635 ret = Expression::convert_for_assignment(context, type, expr_type,
3636 expr_tree, this->location());
3637 else if (type->integer_type() != NULL)
3639 if (expr_type->integer_type() != NULL
3640 || expr_type->float_type() != NULL
3641 || expr_type->is_unsafe_pointer_type())
3642 ret = fold(convert_to_integer(type_tree, expr_tree));
3646 else if (type->float_type() != NULL)
3648 if (expr_type->integer_type() != NULL
3649 || expr_type->float_type() != NULL)
3650 ret = fold(convert_to_real(type_tree, expr_tree));
3654 else if (type->complex_type() != NULL)
3656 if (expr_type->complex_type() != NULL)
3657 ret = fold(convert_to_complex(type_tree, expr_tree));
3661 else if (type->is_string_type()
3662 && expr_type->integer_type() != NULL)
3664 expr_tree = fold_convert(integer_type_node, expr_tree);
3665 if (host_integerp(expr_tree, 0))
3667 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3669 Lex::append_char(intval, true, &s, this->location());
3670 Expression* se = Expression::make_string(s, this->location());
3671 return se->get_tree(context);
3674 static tree int_to_string_fndecl;
3675 ret = Gogo::call_builtin(&int_to_string_fndecl,
3677 "__go_int_to_string",
3681 fold_convert(integer_type_node, expr_tree));
3683 else if (type->is_string_type() && expr_type->is_slice_type())
3685 if (!DECL_P(expr_tree))
3686 expr_tree = save_expr(expr_tree);
3687 Array_type* a = expr_type->array_type();
3688 Type* e = a->element_type()->forwarded();
3689 go_assert(e->integer_type() != NULL);
3690 tree valptr = fold_convert(const_ptr_type_node,
3691 a->value_pointer_tree(gogo, expr_tree));
3692 tree len = a->length_tree(gogo, expr_tree);
3693 len = fold_convert_loc(this->location().gcc_location(), integer_type_node,
3695 if (e->integer_type()->is_byte())
3697 static tree byte_array_to_string_fndecl;
3698 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3700 "__go_byte_array_to_string",
3703 const_ptr_type_node,
3710 go_assert(e->integer_type()->is_rune());
3711 static tree int_array_to_string_fndecl;
3712 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3714 "__go_int_array_to_string",
3717 const_ptr_type_node,
3723 else if (type->is_slice_type() && expr_type->is_string_type())
3725 Type* e = type->array_type()->element_type()->forwarded();
3726 go_assert(e->integer_type() != NULL);
3727 if (e->integer_type()->is_byte())
3729 tree string_to_byte_array_fndecl = NULL_TREE;
3730 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3732 "__go_string_to_byte_array",
3735 TREE_TYPE(expr_tree),
3740 go_assert(e->integer_type()->is_rune());
3741 tree string_to_int_array_fndecl = NULL_TREE;
3742 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3744 "__go_string_to_int_array",
3747 TREE_TYPE(expr_tree),
3751 else if ((type->is_unsafe_pointer_type()
3752 && expr_type->points_to() != NULL)
3753 || (expr_type->is_unsafe_pointer_type()
3754 && type->points_to() != NULL))
3755 ret = fold_convert(type_tree, expr_tree);
3756 else if (type->is_unsafe_pointer_type()
3757 && expr_type->integer_type() != NULL)
3758 ret = convert_to_pointer(type_tree, expr_tree);
3759 else if (this->may_convert_function_types_
3760 && type->function_type() != NULL
3761 && expr_type->function_type() != NULL)
3762 ret = fold_convert_loc(this->location().gcc_location(), type_tree,
3765 ret = Expression::convert_for_assignment(context, type, expr_type,
3766 expr_tree, this->location());
3771 // Output a type conversion in a constant expression.
3774 Type_conversion_expression::do_export(Export* exp) const
3776 exp->write_c_string("convert(");
3777 exp->write_type(this->type_);
3778 exp->write_c_string(", ");
3779 this->expr_->export_expression(exp);
3780 exp->write_c_string(")");
3783 // Import a type conversion or a struct construction.
3786 Type_conversion_expression::do_import(Import* imp)
3788 imp->require_c_string("convert(");
3789 Type* type = imp->read_type();
3790 imp->require_c_string(", ");
3791 Expression* val = Expression::import_expression(imp);
3792 imp->require_c_string(")");
3793 return Expression::make_cast(type, val, imp->location());
3796 // Dump ast representation for a type conversion expression.
3799 Type_conversion_expression::do_dump_expression(
3800 Ast_dump_context* ast_dump_context) const
3802 ast_dump_context->dump_type(this->type_);
3803 ast_dump_context->ostream() << "(";
3804 ast_dump_context->dump_expression(this->expr_);
3805 ast_dump_context->ostream() << ") ";
3808 // Make a type cast expression.
3811 Expression::make_cast(Type* type, Expression* val, Location location)
3813 if (type->is_error_type() || val->is_error_expression())
3814 return Expression::make_error(location);
3815 return new Type_conversion_expression(type, val, location);
3818 // An unsafe type conversion, used to pass values to builtin functions.
3820 class Unsafe_type_conversion_expression : public Expression
3823 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3825 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3826 type_(type), expr_(expr)
3831 do_traverse(Traverse* traverse);
3835 { return this->type_; }
3838 do_determine_type(const Type_context*)
3839 { this->expr_->determine_type_no_context(); }
3844 return new Unsafe_type_conversion_expression(this->type_,
3845 this->expr_->copy(),
3850 do_get_tree(Translate_context*);
3853 do_dump_expression(Ast_dump_context*) const;
3856 // The type to convert to.
3858 // The expression to convert.
3865 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3867 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3868 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3869 return TRAVERSE_EXIT;
3870 return TRAVERSE_CONTINUE;
3873 // Convert to backend representation.
3876 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3878 // We are only called for a limited number of cases.
3880 Type* t = this->type_;
3881 Type* et = this->expr_->type();
3883 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
3884 tree expr_tree = this->expr_->get_tree(context);
3885 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3886 return error_mark_node;
3888 Location loc = this->location();
3890 bool use_view_convert = false;
3891 if (t->is_slice_type())
3893 go_assert(et->is_slice_type());
3894 use_view_convert = true;
3896 else if (t->map_type() != NULL)
3897 go_assert(et->map_type() != NULL);
3898 else if (t->channel_type() != NULL)
3899 go_assert(et->channel_type() != NULL);
3900 else if (t->points_to() != NULL && t->points_to()->channel_type() != NULL)
3901 go_assert((et->points_to() != NULL
3902 && et->points_to()->channel_type() != NULL)
3903 || et->is_nil_type());
3904 else if (t->points_to() != NULL)
3905 go_assert(et->points_to() != NULL || et->is_nil_type());
3906 else if (et->is_unsafe_pointer_type())
3907 go_assert(t->points_to() != NULL);
3908 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3910 go_assert(et->interface_type() != NULL
3911 && !et->interface_type()->is_empty());
3912 use_view_convert = true;
3914 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3916 go_assert(et->interface_type() != NULL
3917 && et->interface_type()->is_empty());
3918 use_view_convert = true;
3920 else if (t->integer_type() != NULL)
3922 go_assert(et->is_boolean_type()
3923 || et->integer_type() != NULL
3924 || et->function_type() != NULL
3925 || et->points_to() != NULL
3926 || et->map_type() != NULL
3927 || et->channel_type() != NULL);
3928 return convert_to_integer(type_tree, expr_tree);
3933 if (use_view_convert)
3934 return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree,
3937 return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree);
3940 // Dump ast representation for an unsafe type conversion expression.
3943 Unsafe_type_conversion_expression::do_dump_expression(
3944 Ast_dump_context* ast_dump_context) const
3946 ast_dump_context->dump_type(this->type_);
3947 ast_dump_context->ostream() << "(";
3948 ast_dump_context->dump_expression(this->expr_);
3949 ast_dump_context->ostream() << ") ";
3952 // Make an unsafe type conversion expression.
3955 Expression::make_unsafe_cast(Type* type, Expression* expr,
3958 return new Unsafe_type_conversion_expression(type, expr, location);
3961 // Unary expressions.
3963 class Unary_expression : public Expression
3966 Unary_expression(Operator op, Expression* expr, Location location)
3967 : Expression(EXPRESSION_UNARY, location),
3968 op_(op), escapes_(true), create_temp_(false), expr_(expr)
3971 // Return the operator.
3974 { return this->op_; }
3976 // Return the operand.
3979 { return this->expr_; }
3981 // Record that an address expression does not escape.
3983 set_does_not_escape()
3985 go_assert(this->op_ == OPERATOR_AND);
3986 this->escapes_ = false;
3989 // Record that this is an address expression which should create a
3990 // temporary variable if necessary. This is used for method calls.
3994 go_assert(this->op_ == OPERATOR_AND);
3995 this->create_temp_ = true;
3998 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3999 // could be done, false if not.
4001 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
4004 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
4005 // could be done, false if not.
4007 eval_float(Operator op, mpfr_t uval, mpfr_t val);
4009 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
4010 // true if this could be done, false if not.
4012 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
4020 do_traverse(Traverse* traverse)
4021 { return Expression::traverse(&this->expr_, traverse); }
4024 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
4027 do_is_constant() const;
4030 do_integer_constant_value(bool, mpz_t, Type**) const;
4033 do_float_constant_value(mpfr_t, Type**) const;
4036 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
4042 do_determine_type(const Type_context*);
4045 do_check_types(Gogo*);
4050 return Expression::make_unary(this->op_, this->expr_->copy(),
4055 do_must_eval_subexpressions_in_order(int*) const
4056 { return this->op_ == OPERATOR_MULT; }
4059 do_is_addressable() const
4060 { return this->op_ == OPERATOR_MULT; }
4063 do_get_tree(Translate_context*);
4066 do_export(Export*) const;
4069 do_dump_expression(Ast_dump_context*) const;
4072 // The unary operator to apply.
4074 // Normally true. False if this is an address expression which does
4075 // not escape the current function.
4077 // True if this is an address expression which should create a
4078 // temporary variable if necessary.
4084 // If we are taking the address of a composite literal, and the
4085 // contents are not constant, then we want to make a heap composite
4089 Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
4091 Location loc = this->location();
4092 Operator op = this->op_;
4093 Expression* expr = this->expr_;
4095 if (op == OPERATOR_MULT && expr->is_type_expression())
4096 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
4098 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
4099 // moving x to the heap. FIXME: Is it worth doing a real escape
4100 // analysis here? This case is found in math/unsafe.go and is
4101 // therefore worth special casing.
4102 if (op == OPERATOR_MULT)
4104 Expression* e = expr;
4105 while (e->classification() == EXPRESSION_CONVERSION)
4107 Type_conversion_expression* te
4108 = static_cast<Type_conversion_expression*>(e);
4112 if (e->classification() == EXPRESSION_UNARY)
4114 Unary_expression* ue = static_cast<Unary_expression*>(e);
4115 if (ue->op_ == OPERATOR_AND)
4122 ue->set_does_not_escape();
4127 // Catching an invalid indirection of unsafe.Pointer here avoid
4128 // having to deal with TYPE_VOID in other places.
4129 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
4131 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
4132 return Expression::make_error(this->location());
4135 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
4136 || op == OPERATOR_NOT || op == OPERATOR_XOR)
4138 Expression* ret = NULL;
4143 if (expr->integer_constant_value(false, eval, &etype))
4147 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
4148 ret = Expression::make_integer(&val, etype, loc);
4155 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
4160 if (expr->float_constant_value(fval, &ftype))
4164 if (Unary_expression::eval_float(op, fval, val))
4165 ret = Expression::make_float(&val, ftype, loc);
4176 if (expr->complex_constant_value(fval, ival, &ftype))
4182 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
4183 ret = Expression::make_complex(&real, &imag, ftype, loc);
4197 // Return whether a unary expression is a constant.
4200 Unary_expression::do_is_constant() const
4202 if (this->op_ == OPERATOR_MULT)
4204 // Indirecting through a pointer is only constant if the object
4205 // to which the expression points is constant, but we currently
4206 // have no way to determine that.
4209 else if (this->op_ == OPERATOR_AND)
4211 // Taking the address of a variable is constant if it is a
4212 // global variable, not constant otherwise. In other cases
4213 // taking the address is probably not a constant.
4214 Var_expression* ve = this->expr_->var_expression();
4217 Named_object* no = ve->named_object();
4218 return no->is_variable() && no->var_value()->is_global();
4223 return this->expr_->is_constant();
4226 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
4227 // UVAL, if known; it may be NULL. Return true if this could be done,
4231 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
4239 case OPERATOR_MINUS:
4241 return Integer_expression::check_constant(val, utype, location);
4243 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
4247 || utype->integer_type() == NULL
4248 || utype->integer_type()->is_abstract())
4252 // The number of HOST_WIDE_INTs that it takes to represent
4254 size_t count = ((mpz_sizeinbase(uval, 2)
4255 + HOST_BITS_PER_WIDE_INT
4257 / HOST_BITS_PER_WIDE_INT);
4259 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
4260 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
4263 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
4264 go_assert(ecount <= count);
4266 // Trim down to the number of words required by the type.
4267 size_t obits = utype->integer_type()->bits();
4268 if (!utype->integer_type()->is_unsigned())
4270 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
4271 / HOST_BITS_PER_WIDE_INT);
4272 go_assert(ocount <= count);
4274 for (size_t i = 0; i < ocount; ++i)
4277 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
4279 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
4282 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
4286 return Integer_expression::check_constant(val, utype, location);
4295 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
4296 // could be done, false if not.
4299 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
4304 mpfr_set(val, uval, GMP_RNDN);
4306 case OPERATOR_MINUS:
4307 mpfr_neg(val, uval, GMP_RNDN);
4319 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
4320 // if this could be done, false if not.
4323 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
4324 mpfr_t real, mpfr_t imag)
4329 mpfr_set(real, rval, GMP_RNDN);
4330 mpfr_set(imag, ival, GMP_RNDN);
4332 case OPERATOR_MINUS:
4333 mpfr_neg(real, rval, GMP_RNDN);
4334 mpfr_neg(imag, ival, GMP_RNDN);
4346 // Return the integral constant value of a unary expression, if it has one.
4349 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
4355 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
4358 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
4364 // Return the floating point constant value of a unary expression, if
4368 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
4373 if (!this->expr_->float_constant_value(uval, ptype))
4376 ret = Unary_expression::eval_float(this->op_, uval, val);
4381 // Return the complex constant value of a unary expression, if it has
4385 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
4393 if (!this->expr_->complex_constant_value(rval, ival, ptype))
4396 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
4402 // Return the type of a unary expression.
4405 Unary_expression::do_type()
4410 case OPERATOR_MINUS:
4413 return this->expr_->type();
4416 return Type::make_pointer_type(this->expr_->type());
4420 Type* subtype = this->expr_->type();
4421 Type* points_to = subtype->points_to();
4422 if (points_to == NULL)
4423 return Type::make_error_type();
4432 // Determine abstract types for a unary expression.
4435 Unary_expression::do_determine_type(const Type_context* context)
4440 case OPERATOR_MINUS:
4443 this->expr_->determine_type(context);
4447 // Taking the address of something.
4449 Type* subtype = (context->type == NULL
4451 : context->type->points_to());
4452 Type_context subcontext(subtype, false);
4453 this->expr_->determine_type(&subcontext);
4458 // Indirecting through a pointer.
4460 Type* subtype = (context->type == NULL
4462 : Type::make_pointer_type(context->type));
4463 Type_context subcontext(subtype, false);
4464 this->expr_->determine_type(&subcontext);
4473 // Check types for a unary expression.
4476 Unary_expression::do_check_types(Gogo*)
4478 Type* type = this->expr_->type();
4479 if (type->is_error())
4481 this->set_is_error();
4488 case OPERATOR_MINUS:
4489 if (type->integer_type() == NULL
4490 && type->float_type() == NULL
4491 && type->complex_type() == NULL)
4492 this->report_error(_("expected numeric type"));
4497 if (type->integer_type() == NULL
4498 && !type->is_boolean_type())
4499 this->report_error(_("expected integer or boolean type"));
4503 if (!this->expr_->is_addressable())
4505 if (!this->create_temp_)
4506 this->report_error(_("invalid operand for unary %<&%>"));
4509 this->expr_->address_taken(this->escapes_);
4513 // Indirecting through a pointer.
4514 if (type->points_to() == NULL)
4515 this->report_error(_("expected pointer"));
4523 // Get a tree for a unary expression.
4526 Unary_expression::do_get_tree(Translate_context* context)
4528 Location loc = this->location();
4530 // Taking the address of a set-and-use-temporary expression requires
4531 // setting the temporary and then taking the address.
4532 if (this->op_ == OPERATOR_AND)
4534 Set_and_use_temporary_expression* sut =
4535 this->expr_->set_and_use_temporary_expression();
4538 Temporary_statement* temp = sut->temporary();
4539 Bvariable* bvar = temp->get_backend_variable(context);
4540 tree var_tree = var_to_tree(bvar);
4541 Expression* val = sut->expression();
4542 tree val_tree = val->get_tree(context);
4543 if (var_tree == error_mark_node || val_tree == error_mark_node)
4544 return error_mark_node;
4545 tree addr_tree = build_fold_addr_expr_loc(loc.gcc_location(),
4547 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4548 TREE_TYPE(addr_tree),
4549 build2_loc(sut->location().gcc_location(),
4550 MODIFY_EXPR, void_type_node,
4551 var_tree, val_tree),
4556 tree expr = this->expr_->get_tree(context);
4557 if (expr == error_mark_node)
4558 return error_mark_node;
4565 case OPERATOR_MINUS:
4567 tree type = TREE_TYPE(expr);
4568 tree compute_type = excess_precision_type(type);
4569 if (compute_type != NULL_TREE)
4570 expr = ::convert(compute_type, expr);
4571 tree ret = fold_build1_loc(loc.gcc_location(), NEGATE_EXPR,
4572 (compute_type != NULL_TREE
4576 if (compute_type != NULL_TREE)
4577 ret = ::convert(type, ret);
4582 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4583 return fold_build1_loc(loc.gcc_location(), TRUTH_NOT_EXPR,
4584 TREE_TYPE(expr), expr);
4586 return fold_build2_loc(loc.gcc_location(), NE_EXPR, boolean_type_node,
4587 expr, build_int_cst(TREE_TYPE(expr), 0));
4590 return fold_build1_loc(loc.gcc_location(), BIT_NOT_EXPR, TREE_TYPE(expr),
4594 if (!this->create_temp_)
4596 // We should not see a non-constant constructor here; cases
4597 // where we would see one should have been moved onto the
4598 // heap at parse time. Taking the address of a nonconstant
4599 // constructor will not do what the programmer expects.
4600 go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4601 go_assert(TREE_CODE(expr) != ADDR_EXPR);
4604 // Build a decl for a constant constructor.
4605 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4607 tree decl = build_decl(this->location().gcc_location(), VAR_DECL,
4608 create_tmp_var_name("C"), TREE_TYPE(expr));
4609 DECL_EXTERNAL(decl) = 0;
4610 TREE_PUBLIC(decl) = 0;
4611 TREE_READONLY(decl) = 1;
4612 TREE_CONSTANT(decl) = 1;
4613 TREE_STATIC(decl) = 1;
4614 TREE_ADDRESSABLE(decl) = 1;
4615 DECL_ARTIFICIAL(decl) = 1;
4616 DECL_INITIAL(decl) = expr;
4617 rest_of_decl_compilation(decl, 1, 0);
4621 if (this->create_temp_
4622 && !TREE_ADDRESSABLE(TREE_TYPE(expr))
4624 && TREE_CODE(expr) != INDIRECT_REF
4625 && TREE_CODE(expr) != COMPONENT_REF)
4627 tree tmp = create_tmp_var(TREE_TYPE(expr), get_name(expr));
4628 DECL_IGNORED_P(tmp) = 1;
4629 DECL_INITIAL(tmp) = expr;
4630 TREE_ADDRESSABLE(tmp) = 1;
4631 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4632 build_pointer_type(TREE_TYPE(expr)),
4633 build1_loc(loc.gcc_location(), DECL_EXPR,
4634 void_type_node, tmp),
4635 build_fold_addr_expr_loc(loc.gcc_location(), tmp));
4638 return build_fold_addr_expr_loc(loc.gcc_location(), expr);
4642 go_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4644 // If we are dereferencing the pointer to a large struct, we
4645 // need to check for nil. We don't bother to check for small
4646 // structs because we expect the system to crash on a nil
4647 // pointer dereference.
4648 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4649 if (s == -1 || s >= 4096)
4652 expr = save_expr(expr);
4653 tree compare = fold_build2_loc(loc.gcc_location(), EQ_EXPR,
4656 fold_convert(TREE_TYPE(expr),
4657 null_pointer_node));
4658 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4660 expr = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4661 TREE_TYPE(expr), build3(COND_EXPR,
4668 // If the type of EXPR is a recursive pointer type, then we
4669 // need to insert a cast before indirecting.
4670 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4672 Type* pt = this->expr_->type()->points_to();
4673 tree ind = type_to_tree(pt->get_backend(context->gogo()));
4674 expr = fold_convert_loc(loc.gcc_location(),
4675 build_pointer_type(ind), expr);
4678 return build_fold_indirect_ref_loc(loc.gcc_location(), expr);
4686 // Export a unary expression.
4689 Unary_expression::do_export(Export* exp) const
4694 exp->write_c_string("+ ");
4696 case OPERATOR_MINUS:
4697 exp->write_c_string("- ");
4700 exp->write_c_string("! ");
4703 exp->write_c_string("^ ");
4710 this->expr_->export_expression(exp);
4713 // Import a unary expression.
4716 Unary_expression::do_import(Import* imp)
4719 switch (imp->get_char())
4725 op = OPERATOR_MINUS;
4736 imp->require_c_string(" ");
4737 Expression* expr = Expression::import_expression(imp);
4738 return Expression::make_unary(op, expr, imp->location());
4741 // Dump ast representation of an unary expression.
4744 Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
4746 ast_dump_context->dump_operator(this->op_);
4747 ast_dump_context->ostream() << "(";
4748 ast_dump_context->dump_expression(this->expr_);
4749 ast_dump_context->ostream() << ") ";
4752 // Make a unary expression.
4755 Expression::make_unary(Operator op, Expression* expr, Location location)
4757 return new Unary_expression(op, expr, location);
4760 // If this is an indirection through a pointer, return the expression
4761 // being pointed through. Otherwise return this.
4766 if (this->classification_ == EXPRESSION_UNARY)
4768 Unary_expression* ue = static_cast<Unary_expression*>(this);
4769 if (ue->op() == OPERATOR_MULT)
4770 return ue->operand();
4775 // Class Binary_expression.
4780 Binary_expression::do_traverse(Traverse* traverse)
4782 int t = Expression::traverse(&this->left_, traverse);
4783 if (t == TRAVERSE_EXIT)
4784 return TRAVERSE_EXIT;
4785 return Expression::traverse(&this->right_, traverse);
4788 // Compare integer constants according to OP.
4791 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4794 int i = mpz_cmp(left_val, right_val);
4799 case OPERATOR_NOTEQ:
4814 // Compare floating point constants according to OP.
4817 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4822 i = mpfr_cmp(left_val, right_val);
4826 mpfr_init_set(lv, left_val, GMP_RNDN);
4828 mpfr_init_set(rv, right_val, GMP_RNDN);
4829 Float_expression::constrain_float(lv, type);
4830 Float_expression::constrain_float(rv, type);
4831 i = mpfr_cmp(lv, rv);
4839 case OPERATOR_NOTEQ:
4854 // Compare complex constants according to OP. Complex numbers may
4855 // only be compared for equality.
4858 Binary_expression::compare_complex(Operator op, Type* type,
4859 mpfr_t left_real, mpfr_t left_imag,
4860 mpfr_t right_real, mpfr_t right_imag)
4864 is_equal = (mpfr_cmp(left_real, right_real) == 0
4865 && mpfr_cmp(left_imag, right_imag) == 0);
4870 mpfr_init_set(lr, left_real, GMP_RNDN);
4871 mpfr_init_set(li, left_imag, GMP_RNDN);
4874 mpfr_init_set(rr, right_real, GMP_RNDN);
4875 mpfr_init_set(ri, right_imag, GMP_RNDN);
4876 Complex_expression::constrain_complex(lr, li, type);
4877 Complex_expression::constrain_complex(rr, ri, type);
4878 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4888 case OPERATOR_NOTEQ:
4895 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4896 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4897 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4898 // this could be done, false if not.
4901 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4902 Type* right_type, mpz_t right_val,
4903 Location location, mpz_t val)
4905 bool is_shift_op = false;
4909 case OPERATOR_ANDAND:
4911 case OPERATOR_NOTEQ:
4916 // These return boolean values. We should probably handle them
4917 // anyhow in case a type conversion is used on the result.
4920 mpz_add(val, left_val, right_val);
4922 case OPERATOR_MINUS:
4923 mpz_sub(val, left_val, right_val);
4926 mpz_ior(val, left_val, right_val);
4929 mpz_xor(val, left_val, right_val);
4932 mpz_mul(val, left_val, right_val);
4935 if (mpz_sgn(right_val) != 0)
4936 mpz_tdiv_q(val, left_val, right_val);
4939 error_at(location, "division by zero");
4945 if (mpz_sgn(right_val) != 0)
4946 mpz_tdiv_r(val, left_val, right_val);
4949 error_at(location, "division by zero");
4954 case OPERATOR_LSHIFT:
4956 unsigned long shift = mpz_get_ui(right_val);
4957 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
4959 error_at(location, "shift count overflow");
4963 mpz_mul_2exp(val, left_val, shift);
4968 case OPERATOR_RSHIFT:
4970 unsigned long shift = mpz_get_ui(right_val);
4971 if (mpz_cmp_ui(right_val, shift) != 0)
4973 error_at(location, "shift count overflow");
4977 if (mpz_cmp_ui(left_val, 0) >= 0)
4978 mpz_tdiv_q_2exp(val, left_val, shift);
4980 mpz_fdiv_q_2exp(val, left_val, shift);
4986 mpz_and(val, left_val, right_val);
4988 case OPERATOR_BITCLEAR:
4992 mpz_com(tval, right_val);
4993 mpz_and(val, left_val, tval);
5001 Type* type = left_type;
5006 else if (type != right_type && right_type != NULL)
5008 if (type->is_abstract())
5010 else if (!right_type->is_abstract())
5012 // This look like a type error which should be diagnosed
5013 // elsewhere. Don't do anything here, to avoid an
5014 // unhelpful chain of error messages.
5020 if (type != NULL && !type->is_abstract())
5022 // We have to check the operands too, as we have implicitly
5023 // coerced them to TYPE.
5024 if ((type != left_type
5025 && !Integer_expression::check_constant(left_val, type, location))
5027 && type != right_type
5028 && !Integer_expression::check_constant(right_val, type,
5030 || !Integer_expression::check_constant(val, type, location))
5037 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
5038 // Return true if this could be done, false if not.
5041 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
5042 Type* right_type, mpfr_t right_val,
5043 mpfr_t val, Location location)
5048 case OPERATOR_ANDAND:
5050 case OPERATOR_NOTEQ:
5055 // These return boolean values. We should probably handle them
5056 // anyhow in case a type conversion is used on the result.
5059 mpfr_add(val, left_val, right_val, GMP_RNDN);
5061 case OPERATOR_MINUS:
5062 mpfr_sub(val, left_val, right_val, GMP_RNDN);
5067 case OPERATOR_BITCLEAR:
5070 mpfr_mul(val, left_val, right_val, GMP_RNDN);
5073 if (mpfr_zero_p(right_val))
5074 error_at(location, "division by zero");
5075 mpfr_div(val, left_val, right_val, GMP_RNDN);
5079 case OPERATOR_LSHIFT:
5080 case OPERATOR_RSHIFT:
5086 Type* type = left_type;
5089 else if (type != right_type && right_type != NULL)
5091 if (type->is_abstract())
5093 else if (!right_type->is_abstract())
5095 // This looks like a type error which should be diagnosed
5096 // elsewhere. Don't do anything here, to avoid an unhelpful
5097 // chain of error messages.
5102 if (type != NULL && !type->is_abstract())
5104 if ((type != left_type
5105 && !Float_expression::check_constant(left_val, type, location))
5106 || (type != right_type
5107 && !Float_expression::check_constant(right_val, type,
5109 || !Float_expression::check_constant(val, type, location))
5110 mpfr_set_ui(val, 0, GMP_RNDN);
5116 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
5117 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
5118 // could be done, false if not.
5121 Binary_expression::eval_complex(Operator op, Type* left_type,
5122 mpfr_t left_real, mpfr_t left_imag,
5124 mpfr_t right_real, mpfr_t right_imag,
5125 mpfr_t real, mpfr_t imag,
5131 case OPERATOR_ANDAND:
5133 case OPERATOR_NOTEQ:
5138 // These return boolean values and must be handled differently.
5141 mpfr_add(real, left_real, right_real, GMP_RNDN);
5142 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
5144 case OPERATOR_MINUS:
5145 mpfr_sub(real, left_real, right_real, GMP_RNDN);
5146 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
5151 case OPERATOR_BITCLEAR:
5155 // You might think that multiplying two complex numbers would
5156 // be simple, and you would be right, until you start to think
5157 // about getting the right answer for infinity. If one
5158 // operand here is infinity and the other is anything other
5159 // than zero or NaN, then we are going to wind up subtracting
5160 // two infinity values. That will give us a NaN, but the
5161 // correct answer is infinity.
5165 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
5169 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
5173 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
5177 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
5179 mpfr_sub(real, lrrr, liri, GMP_RNDN);
5180 mpfr_add(imag, lrri, lirr, GMP_RNDN);
5182 // If we get NaN on both sides, check whether it should really
5183 // be infinity. The rule is that if either side of the
5184 // complex number is infinity, then the whole value is
5185 // infinity, even if the other side is NaN. So the only case
5186 // we have to fix is the one in which both sides are NaN.
5187 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
5188 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
5189 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
5191 bool is_infinity = false;
5195 mpfr_init_set(lr, left_real, GMP_RNDN);
5196 mpfr_init_set(li, left_imag, GMP_RNDN);
5200 mpfr_init_set(rr, right_real, GMP_RNDN);
5201 mpfr_init_set(ri, right_imag, GMP_RNDN);
5203 // If the left side is infinity, then the result is
5205 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
5207 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
5208 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5209 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
5210 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5213 mpfr_set_ui(rr, 0, GMP_RNDN);
5214 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5218 mpfr_set_ui(ri, 0, GMP_RNDN);
5219 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5224 // If the right side is infinity, then the result is
5226 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
5228 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5229 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5230 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5231 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5234 mpfr_set_ui(lr, 0, GMP_RNDN);
5235 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5239 mpfr_set_ui(li, 0, GMP_RNDN);
5240 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5245 // If we got an overflow in the intermediate computations,
5246 // then the result is infinity.
5248 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
5249 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
5253 mpfr_set_ui(lr, 0, GMP_RNDN);
5254 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5258 mpfr_set_ui(li, 0, GMP_RNDN);
5259 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5263 mpfr_set_ui(rr, 0, GMP_RNDN);
5264 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5268 mpfr_set_ui(ri, 0, GMP_RNDN);
5269 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5276 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
5277 mpfr_mul(lrri, lr, ri, GMP_RNDN);
5278 mpfr_mul(lirr, li, rr, GMP_RNDN);
5279 mpfr_mul(liri, li, ri, GMP_RNDN);
5280 mpfr_sub(real, lrrr, liri, GMP_RNDN);
5281 mpfr_add(imag, lrri, lirr, GMP_RNDN);
5282 mpfr_set_inf(real, mpfr_sgn(real));
5283 mpfr_set_inf(imag, mpfr_sgn(imag));
5300 // For complex division we want to avoid having an
5301 // intermediate overflow turn the whole result in a NaN. We
5302 // scale the values to try to avoid this.
5304 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
5305 error_at(location, "division by zero");
5311 mpfr_abs(rra, right_real, GMP_RNDN);
5312 mpfr_abs(ria, right_imag, GMP_RNDN);
5315 mpfr_max(t, rra, ria, GMP_RNDN);
5319 mpfr_init_set(rr, right_real, GMP_RNDN);
5320 mpfr_init_set(ri, right_imag, GMP_RNDN);
5322 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
5324 ilogbw = mpfr_get_exp(t);
5325 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
5326 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
5331 mpfr_mul(denom, rr, rr, GMP_RNDN);
5332 mpfr_mul(t, ri, ri, GMP_RNDN);
5333 mpfr_add(denom, denom, t, GMP_RNDN);
5335 mpfr_mul(real, left_real, rr, GMP_RNDN);
5336 mpfr_mul(t, left_imag, ri, GMP_RNDN);
5337 mpfr_add(real, real, t, GMP_RNDN);
5338 mpfr_div(real, real, denom, GMP_RNDN);
5339 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
5341 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
5342 mpfr_mul(t, left_real, ri, GMP_RNDN);
5343 mpfr_sub(imag, imag, t, GMP_RNDN);
5344 mpfr_div(imag, imag, denom, GMP_RNDN);
5345 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
5347 // If we wind up with NaN on both sides, check whether we
5348 // should really have infinity. The rule is that if either
5349 // side of the complex number is infinity, then the whole
5350 // value is infinity, even if the other side is NaN. So the
5351 // only case we have to fix is the one in which both sides are
5353 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
5354 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
5355 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
5357 if (mpfr_zero_p(denom))
5359 mpfr_set_inf(real, mpfr_sgn(rr));
5360 mpfr_mul(real, real, left_real, GMP_RNDN);
5361 mpfr_set_inf(imag, mpfr_sgn(rr));
5362 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
5364 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
5365 && mpfr_number_p(rr) && mpfr_number_p(ri))
5367 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
5368 mpfr_copysign(t, t, left_real, GMP_RNDN);
5371 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
5372 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
5376 mpfr_mul(t3, t, rr, GMP_RNDN);
5380 mpfr_mul(t4, t2, ri, GMP_RNDN);
5382 mpfr_add(t3, t3, t4, GMP_RNDN);
5383 mpfr_set_inf(real, mpfr_sgn(t3));
5385 mpfr_mul(t3, t2, rr, GMP_RNDN);
5386 mpfr_mul(t4, t, ri, GMP_RNDN);
5387 mpfr_sub(t3, t3, t4, GMP_RNDN);
5388 mpfr_set_inf(imag, mpfr_sgn(t3));
5394 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
5395 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
5397 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5398 mpfr_copysign(t, t, rr, GMP_RNDN);
5401 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5402 mpfr_copysign(t2, t2, ri, GMP_RNDN);
5406 mpfr_mul(t3, left_real, t, GMP_RNDN);
5410 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
5412 mpfr_add(t3, t3, t4, GMP_RNDN);
5413 mpfr_set_ui(real, 0, GMP_RNDN);
5414 mpfr_mul(real, real, t3, GMP_RNDN);
5416 mpfr_mul(t3, left_imag, t, GMP_RNDN);
5417 mpfr_mul(t4, left_real, t2, GMP_RNDN);
5418 mpfr_sub(t3, t3, t4, GMP_RNDN);
5419 mpfr_set_ui(imag, 0, GMP_RNDN);
5420 mpfr_mul(imag, imag, t3, GMP_RNDN);
5438 case OPERATOR_LSHIFT:
5439 case OPERATOR_RSHIFT:
5445 Type* type = left_type;
5448 else if (type != right_type && right_type != NULL)
5450 if (type->is_abstract())
5452 else if (!right_type->is_abstract())
5454 // This looks like a type error which should be diagnosed
5455 // elsewhere. Don't do anything here, to avoid an unhelpful
5456 // chain of error messages.
5461 if (type != NULL && !type->is_abstract())
5463 if ((type != left_type
5464 && !Complex_expression::check_constant(left_real, left_imag,
5466 || (type != right_type
5467 && !Complex_expression::check_constant(right_real, right_imag,
5469 || !Complex_expression::check_constant(real, imag, type,
5472 mpfr_set_ui(real, 0, GMP_RNDN);
5473 mpfr_set_ui(imag, 0, GMP_RNDN);
5480 // Lower a binary expression. We have to evaluate constant
5481 // expressions now, in order to implement Go's unlimited precision
5485 Binary_expression::do_lower(Gogo* gogo, Named_object*,
5486 Statement_inserter* inserter, int)
5488 Location location = this->location();
5489 Operator op = this->op_;
5490 Expression* left = this->left_;
5491 Expression* right = this->right_;
5493 const bool is_comparison = (op == OPERATOR_EQEQ
5494 || op == OPERATOR_NOTEQ
5495 || op == OPERATOR_LT
5496 || op == OPERATOR_LE
5497 || op == OPERATOR_GT
5498 || op == OPERATOR_GE);
5500 // Integer constant expressions.
5506 mpz_init(right_val);
5508 if (left->integer_constant_value(false, left_val, &left_type)
5509 && right->integer_constant_value(false, right_val, &right_type))
5511 Expression* ret = NULL;
5512 if (left_type != right_type
5513 && left_type != NULL
5514 && right_type != NULL
5515 && left_type->base() != right_type->base()
5516 && op != OPERATOR_LSHIFT
5517 && op != OPERATOR_RSHIFT)
5519 // May be a type error--let it be diagnosed later.
5521 else if (is_comparison)
5523 bool b = Binary_expression::compare_integer(op, left_val,
5525 ret = Expression::make_cast(Type::lookup_bool_type(),
5526 Expression::make_boolean(b, location),
5534 if (Binary_expression::eval_integer(op, left_type, left_val,
5535 right_type, right_val,
5538 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
5540 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5542 else if (left_type == NULL)
5544 else if (right_type == NULL)
5546 else if (!left_type->is_abstract()
5547 && left_type->named_type() != NULL)
5549 else if (!right_type->is_abstract()
5550 && right_type->named_type() != NULL)
5552 else if (!left_type->is_abstract())
5554 else if (!right_type->is_abstract())
5556 else if (left_type->float_type() != NULL)
5558 else if (right_type->float_type() != NULL)
5560 else if (left_type->complex_type() != NULL)
5562 else if (right_type->complex_type() != NULL)
5566 ret = Expression::make_integer(&val, type, location);
5574 mpz_clear(right_val);
5575 mpz_clear(left_val);
5579 mpz_clear(right_val);
5580 mpz_clear(left_val);
5583 // Floating point constant expressions.
5586 mpfr_init(left_val);
5589 mpfr_init(right_val);
5591 if (left->float_constant_value(left_val, &left_type)
5592 && right->float_constant_value(right_val, &right_type))
5594 Expression* ret = NULL;
5595 if (left_type != right_type
5596 && left_type != NULL
5597 && right_type != NULL
5598 && left_type->base() != right_type->base()
5599 && op != OPERATOR_LSHIFT
5600 && op != OPERATOR_RSHIFT)
5602 // May be a type error--let it be diagnosed later.
5604 else if (is_comparison)
5606 bool b = Binary_expression::compare_float(op,
5610 left_val, right_val);
5611 ret = Expression::make_boolean(b, location);
5618 if (Binary_expression::eval_float(op, left_type, left_val,
5619 right_type, right_val, val,
5622 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5623 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5625 if (left_type == NULL)
5627 else if (right_type == NULL)
5629 else if (!left_type->is_abstract()
5630 && left_type->named_type() != NULL)
5632 else if (!right_type->is_abstract()
5633 && right_type->named_type() != NULL)
5635 else if (!left_type->is_abstract())
5637 else if (!right_type->is_abstract())
5639 else if (left_type->float_type() != NULL)
5641 else if (right_type->float_type() != NULL)
5645 ret = Expression::make_float(&val, type, location);
5653 mpfr_clear(right_val);
5654 mpfr_clear(left_val);
5658 mpfr_clear(right_val);
5659 mpfr_clear(left_val);
5662 // Complex constant expressions.
5666 mpfr_init(left_real);
5667 mpfr_init(left_imag);
5672 mpfr_init(right_real);
5673 mpfr_init(right_imag);
5676 if (left->complex_constant_value(left_real, left_imag, &left_type)
5677 && right->complex_constant_value(right_real, right_imag, &right_type))
5679 Expression* ret = NULL;
5680 if (left_type != right_type
5681 && left_type != NULL
5682 && right_type != NULL
5683 && left_type->base() != right_type->base())
5685 // May be a type error--let it be diagnosed later.
5687 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5689 bool b = Binary_expression::compare_complex(op,
5697 ret = Expression::make_boolean(b, location);
5706 if (Binary_expression::eval_complex(op, left_type,
5707 left_real, left_imag,
5709 right_real, right_imag,
5713 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5714 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5716 if (left_type == NULL)
5718 else if (right_type == NULL)
5720 else if (!left_type->is_abstract()
5721 && left_type->named_type() != NULL)
5723 else if (!right_type->is_abstract()
5724 && right_type->named_type() != NULL)
5726 else if (!left_type->is_abstract())
5728 else if (!right_type->is_abstract())
5730 else if (left_type->complex_type() != NULL)
5732 else if (right_type->complex_type() != NULL)
5736 ret = Expression::make_complex(&real, &imag, type,
5745 mpfr_clear(left_real);
5746 mpfr_clear(left_imag);
5747 mpfr_clear(right_real);
5748 mpfr_clear(right_imag);
5753 mpfr_clear(left_real);
5754 mpfr_clear(left_imag);
5755 mpfr_clear(right_real);
5756 mpfr_clear(right_imag);
5759 // String constant expressions.
5760 if (op == OPERATOR_PLUS
5761 && left->type()->is_string_type()
5762 && right->type()->is_string_type())
5764 std::string left_string;
5765 std::string right_string;
5766 if (left->string_constant_value(&left_string)
5767 && right->string_constant_value(&right_string))
5768 return Expression::make_string(left_string + right_string, location);
5771 // Special case for shift of a floating point constant.
5772 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5775 mpfr_init(left_val);
5778 mpz_init(right_val);
5780 if (left->float_constant_value(left_val, &left_type)
5781 && right->integer_constant_value(false, right_val, &right_type)
5782 && mpfr_integer_p(left_val)
5783 && (left_type == NULL
5784 || left_type->is_abstract()
5785 || left_type->integer_type() != NULL))
5789 mpfr_get_z(left_int, left_val, GMP_RNDN);
5794 Expression* ret = NULL;
5795 if (Binary_expression::eval_integer(op, left_type, left_int,
5796 right_type, right_val,
5798 ret = Expression::make_integer(&val, left_type, location);
5800 mpz_clear(left_int);
5805 mpfr_clear(left_val);
5806 mpz_clear(right_val);
5811 mpfr_clear(left_val);
5812 mpz_clear(right_val);
5815 // Lower struct and array comparisons.
5816 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5818 if (left->type()->struct_type() != NULL)
5819 return this->lower_struct_comparison(gogo, inserter);
5820 else if (left->type()->array_type() != NULL
5821 && !left->type()->is_slice_type())
5822 return this->lower_array_comparison(gogo, inserter);
5828 // Lower a struct comparison.
5831 Binary_expression::lower_struct_comparison(Gogo* gogo,
5832 Statement_inserter* inserter)
5834 Struct_type* st = this->left_->type()->struct_type();
5835 Struct_type* st2 = this->right_->type()->struct_type();
5838 if (st != st2 && !Type::are_identical(st, st2, false, NULL))
5840 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5841 this->right_->type(), NULL))
5844 // See if we can compare using memcmp. As a heuristic, we use
5845 // memcmp rather than field references and comparisons if there are
5846 // more than two fields.
5847 if (st->compare_is_identity(gogo) && st->total_field_count() > 2)
5848 return this->lower_compare_to_memcmp(gogo, inserter);
5850 Location loc = this->location();
5852 Expression* left = this->left_;
5853 Temporary_statement* left_temp = NULL;
5854 if (left->var_expression() == NULL
5855 && left->temporary_reference_expression() == NULL)
5857 left_temp = Statement::make_temporary(left->type(), NULL, loc);
5858 inserter->insert(left_temp);
5859 left = Expression::make_set_and_use_temporary(left_temp, left, loc);
5862 Expression* right = this->right_;
5863 Temporary_statement* right_temp = NULL;
5864 if (right->var_expression() == NULL
5865 && right->temporary_reference_expression() == NULL)
5867 right_temp = Statement::make_temporary(right->type(), NULL, loc);
5868 inserter->insert(right_temp);
5869 right = Expression::make_set_and_use_temporary(right_temp, right, loc);
5872 Expression* ret = Expression::make_boolean(true, loc);
5873 const Struct_field_list* fields = st->fields();
5874 unsigned int field_index = 0;
5875 for (Struct_field_list::const_iterator pf = fields->begin();
5876 pf != fields->end();
5877 ++pf, ++field_index)
5879 if (field_index > 0)
5881 if (left_temp == NULL)
5882 left = left->copy();
5884 left = Expression::make_temporary_reference(left_temp, loc);
5885 if (right_temp == NULL)
5886 right = right->copy();
5888 right = Expression::make_temporary_reference(right_temp, loc);
5890 Expression* f1 = Expression::make_field_reference(left, field_index,
5892 Expression* f2 = Expression::make_field_reference(right, field_index,
5894 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc);
5895 ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc);
5898 if (this->op_ == OPERATOR_NOTEQ)
5899 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5904 // Lower an array comparison.
5907 Binary_expression::lower_array_comparison(Gogo* gogo,
5908 Statement_inserter* inserter)
5910 Array_type* at = this->left_->type()->array_type();
5911 Array_type* at2 = this->right_->type()->array_type();
5914 if (at != at2 && !Type::are_identical(at, at2, false, NULL))
5916 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5917 this->right_->type(), NULL))
5920 // Call memcmp directly if possible. This may let the middle-end
5921 // optimize the call.
5922 if (at->compare_is_identity(gogo))
5923 return this->lower_compare_to_memcmp(gogo, inserter);
5925 // Call the array comparison function.
5926 Named_object* hash_fn;
5927 Named_object* equal_fn;
5928 at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL,
5929 &hash_fn, &equal_fn);
5931 Location loc = this->location();
5933 Expression* func = Expression::make_func_reference(equal_fn, NULL, loc);
5935 Expression_list* args = new Expression_list();
5936 args->push_back(this->operand_address(inserter, this->left_));
5937 args->push_back(this->operand_address(inserter, this->right_));
5938 args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE));
5940 Expression* ret = Expression::make_call(func, args, false, loc);
5942 if (this->op_ == OPERATOR_NOTEQ)
5943 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5948 // Lower a struct or array comparison to a call to memcmp.
5951 Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter)
5953 Location loc = this->location();
5955 Expression* a1 = this->operand_address(inserter, this->left_);
5956 Expression* a2 = this->operand_address(inserter, this->right_);
5957 Expression* len = Expression::make_type_info(this->left_->type(),
5960 Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len);
5963 mpz_init_set_ui(zval, 0);
5964 Expression* zero = Expression::make_integer(&zval, NULL, loc);
5967 return Expression::make_binary(this->op_, call, zero, loc);
5970 // Return the address of EXPR, cast to unsafe.Pointer.
5973 Binary_expression::operand_address(Statement_inserter* inserter,
5976 Location loc = this->location();
5978 if (!expr->is_addressable())
5980 Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL,
5982 inserter->insert(temp);
5983 expr = Expression::make_set_and_use_temporary(temp, expr, loc);
5985 expr = Expression::make_unary(OPERATOR_AND, expr, loc);
5986 static_cast<Unary_expression*>(expr)->set_does_not_escape();
5987 Type* void_type = Type::make_void_type();
5988 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
5989 return Expression::make_cast(unsafe_pointer_type, expr, loc);
5992 // Return the integer constant value, if it has one.
5995 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
6001 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
6004 mpz_clear(left_val);
6009 mpz_init(right_val);
6011 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
6014 mpz_clear(right_val);
6015 mpz_clear(left_val);
6020 if (left_type != right_type
6021 && left_type != NULL
6022 && right_type != NULL
6023 && left_type->base() != right_type->base()
6024 && this->op_ != OPERATOR_RSHIFT
6025 && this->op_ != OPERATOR_LSHIFT)
6028 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
6029 right_type, right_val,
6030 this->location(), val);
6032 mpz_clear(right_val);
6033 mpz_clear(left_val);
6041 // Return the floating point constant value, if it has one.
6044 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
6047 mpfr_init(left_val);
6049 if (!this->left_->float_constant_value(left_val, &left_type))
6051 mpfr_clear(left_val);
6056 mpfr_init(right_val);
6058 if (!this->right_->float_constant_value(right_val, &right_type))
6060 mpfr_clear(right_val);
6061 mpfr_clear(left_val);
6066 if (left_type != right_type
6067 && left_type != NULL
6068 && right_type != NULL
6069 && left_type->base() != right_type->base())
6072 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
6073 right_type, right_val,
6074 val, this->location());
6076 mpfr_clear(left_val);
6077 mpfr_clear(right_val);
6085 // Return the complex constant value, if it has one.
6088 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
6093 mpfr_init(left_real);
6094 mpfr_init(left_imag);
6096 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
6098 mpfr_clear(left_real);
6099 mpfr_clear(left_imag);
6105 mpfr_init(right_real);
6106 mpfr_init(right_imag);
6108 if (!this->right_->complex_constant_value(right_real, right_imag,
6111 mpfr_clear(left_real);
6112 mpfr_clear(left_imag);
6113 mpfr_clear(right_real);
6114 mpfr_clear(right_imag);
6119 if (left_type != right_type
6120 && left_type != NULL
6121 && right_type != NULL
6122 && left_type->base() != right_type->base())
6125 ret = Binary_expression::eval_complex(this->op_, left_type,
6126 left_real, left_imag,
6128 right_real, right_imag,
6131 mpfr_clear(left_real);
6132 mpfr_clear(left_imag);
6133 mpfr_clear(right_real);
6134 mpfr_clear(right_imag);
6142 // Note that the value is being discarded.
6145 Binary_expression::do_discarding_value()
6147 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
6148 this->right_->discarding_value();
6150 this->unused_value_error();
6156 Binary_expression::do_type()
6158 if (this->classification() == EXPRESSION_ERROR)
6159 return Type::make_error_type();
6164 case OPERATOR_ANDAND:
6166 case OPERATOR_NOTEQ:
6171 return Type::lookup_bool_type();
6174 case OPERATOR_MINUS:
6181 case OPERATOR_BITCLEAR:
6183 Type* left_type = this->left_->type();
6184 Type* right_type = this->right_->type();
6185 if (left_type->is_error())
6187 else if (right_type->is_error())
6189 else if (!Type::are_compatible_for_binop(left_type, right_type))
6191 this->report_error(_("incompatible types in binary expression"));
6192 return Type::make_error_type();
6194 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
6196 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
6198 else if (!left_type->is_abstract())
6200 else if (!right_type->is_abstract())
6202 else if (left_type->complex_type() != NULL)
6204 else if (right_type->complex_type() != NULL)
6206 else if (left_type->float_type() != NULL)
6208 else if (right_type->float_type() != NULL)
6214 case OPERATOR_LSHIFT:
6215 case OPERATOR_RSHIFT:
6216 return this->left_->type();
6223 // Set type for a binary expression.
6226 Binary_expression::do_determine_type(const Type_context* context)
6228 Type* tleft = this->left_->type();
6229 Type* tright = this->right_->type();
6231 // Both sides should have the same type, except for the shift
6232 // operations. For a comparison, we should ignore the incoming
6235 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
6236 || this->op_ == OPERATOR_RSHIFT);
6238 bool is_comparison = (this->op_ == OPERATOR_EQEQ
6239 || this->op_ == OPERATOR_NOTEQ
6240 || this->op_ == OPERATOR_LT
6241 || this->op_ == OPERATOR_LE
6242 || this->op_ == OPERATOR_GT
6243 || this->op_ == OPERATOR_GE);
6245 Type_context subcontext(*context);
6249 // In a comparison, the context does not determine the types of
6251 subcontext.type = NULL;
6254 // Set the context for the left hand operand.
6257 // The right hand operand of a shift plays no role in
6258 // determining the type of the left hand operand.
6260 else if (!tleft->is_abstract())
6261 subcontext.type = tleft;
6262 else if (!tright->is_abstract())
6263 subcontext.type = tright;
6264 else if (subcontext.type == NULL)
6266 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
6267 || (tleft->float_type() != NULL && tright->float_type() != NULL)
6268 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
6270 // Both sides have an abstract integer, abstract float, or
6271 // abstract complex type. Just let CONTEXT determine
6272 // whether they may remain abstract or not.
6274 else if (tleft->complex_type() != NULL)
6275 subcontext.type = tleft;
6276 else if (tright->complex_type() != NULL)
6277 subcontext.type = tright;
6278 else if (tleft->float_type() != NULL)
6279 subcontext.type = tleft;
6280 else if (tright->float_type() != NULL)
6281 subcontext.type = tright;
6283 subcontext.type = tleft;
6285 if (subcontext.type != NULL && !context->may_be_abstract)
6286 subcontext.type = subcontext.type->make_non_abstract_type();
6289 this->left_->determine_type(&subcontext);
6293 // We may have inherited an unusable type for the shift operand.
6294 // Give a useful error if that happened.
6295 if (tleft->is_abstract()
6296 && subcontext.type != NULL
6297 && (this->left_->type()->integer_type() == NULL
6298 || (subcontext.type->integer_type() == NULL
6299 && subcontext.type->float_type() == NULL
6300 && subcontext.type->complex_type() == NULL)))
6301 this->report_error(("invalid context-determined non-integer type "
6302 "for shift operand"));
6304 // The context for the right hand operand is the same as for the
6305 // left hand operand, except for a shift operator.
6306 subcontext.type = Type::lookup_integer_type("uint");
6307 subcontext.may_be_abstract = false;
6310 this->right_->determine_type(&subcontext);
6313 // Report an error if the binary operator OP does not support TYPE.
6314 // OTYPE is the type of the other operand. Return whether the
6315 // operation is OK. This should not be used for shift.
6318 Binary_expression::check_operator_type(Operator op, Type* type, Type* otype,
6324 case OPERATOR_ANDAND:
6325 if (!type->is_boolean_type())
6327 error_at(location, "expected boolean type");
6333 case OPERATOR_NOTEQ:
6336 if (!Type::are_compatible_for_comparison(true, type, otype, &reason))
6338 error_at(location, "%s", reason.c_str());
6350 if (!Type::are_compatible_for_comparison(false, type, otype, &reason))
6352 error_at(location, "%s", reason.c_str());
6359 case OPERATOR_PLUSEQ:
6360 if (type->integer_type() == NULL
6361 && type->float_type() == NULL
6362 && type->complex_type() == NULL
6363 && !type->is_string_type())
6366 "expected integer, floating, complex, or string type");
6371 case OPERATOR_MINUS:
6372 case OPERATOR_MINUSEQ:
6374 case OPERATOR_MULTEQ:
6376 case OPERATOR_DIVEQ:
6377 if (type->integer_type() == NULL
6378 && type->float_type() == NULL
6379 && type->complex_type() == NULL)
6381 error_at(location, "expected integer, floating, or complex type");
6387 case OPERATOR_MODEQ:
6391 case OPERATOR_ANDEQ:
6393 case OPERATOR_XOREQ:
6394 case OPERATOR_BITCLEAR:
6395 case OPERATOR_BITCLEAREQ:
6396 if (type->integer_type() == NULL)
6398 error_at(location, "expected integer type");
6413 Binary_expression::do_check_types(Gogo*)
6415 if (this->classification() == EXPRESSION_ERROR)
6418 Type* left_type = this->left_->type();
6419 Type* right_type = this->right_->type();
6420 if (left_type->is_error() || right_type->is_error())
6422 this->set_is_error();
6426 if (this->op_ == OPERATOR_EQEQ
6427 || this->op_ == OPERATOR_NOTEQ
6428 || this->op_ == OPERATOR_LT
6429 || this->op_ == OPERATOR_LE
6430 || this->op_ == OPERATOR_GT
6431 || this->op_ == OPERATOR_GE)
6433 if (!Type::are_assignable(left_type, right_type, NULL)
6434 && !Type::are_assignable(right_type, left_type, NULL))
6436 this->report_error(_("incompatible types in binary expression"));
6439 if (!Binary_expression::check_operator_type(this->op_, left_type,
6442 || !Binary_expression::check_operator_type(this->op_, right_type,
6446 this->set_is_error();
6450 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
6452 if (!Type::are_compatible_for_binop(left_type, right_type))
6454 this->report_error(_("incompatible types in binary expression"));
6457 if (!Binary_expression::check_operator_type(this->op_, left_type,
6461 this->set_is_error();
6467 if (left_type->integer_type() == NULL)
6468 this->report_error(_("shift of non-integer operand"));
6470 if (!right_type->is_abstract()
6471 && (right_type->integer_type() == NULL
6472 || !right_type->integer_type()->is_unsigned()))
6473 this->report_error(_("shift count not unsigned integer"));
6479 if (this->right_->integer_constant_value(true, val, &type))
6481 if (mpz_sgn(val) < 0)
6483 this->report_error(_("negative shift count"));
6485 Location rloc = this->right_->location();
6486 this->right_ = Expression::make_integer(&val, right_type,
6495 // Get a tree for a binary expression.
6498 Binary_expression::do_get_tree(Translate_context* context)
6500 tree left = this->left_->get_tree(context);
6501 tree right = this->right_->get_tree(context);
6503 if (left == error_mark_node || right == error_mark_node)
6504 return error_mark_node;
6506 enum tree_code code;
6507 bool use_left_type = true;
6508 bool is_shift_op = false;
6512 case OPERATOR_NOTEQ:
6517 return Expression::comparison_tree(context, this->op_,
6518 this->left_->type(), left,
6519 this->right_->type(), right,
6523 code = TRUTH_ORIF_EXPR;
6524 use_left_type = false;
6526 case OPERATOR_ANDAND:
6527 code = TRUTH_ANDIF_EXPR;
6528 use_left_type = false;
6533 case OPERATOR_MINUS:
6537 code = BIT_IOR_EXPR;
6540 code = BIT_XOR_EXPR;
6547 Type *t = this->left_->type();
6548 if (t->float_type() != NULL || t->complex_type() != NULL)
6551 code = TRUNC_DIV_EXPR;
6555 code = TRUNC_MOD_EXPR;
6557 case OPERATOR_LSHIFT:
6561 case OPERATOR_RSHIFT:
6566 code = BIT_AND_EXPR;
6568 case OPERATOR_BITCLEAR:
6569 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
6570 code = BIT_AND_EXPR;
6576 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
6578 if (this->left_->type()->is_string_type())
6580 go_assert(this->op_ == OPERATOR_PLUS);
6581 Type* st = Type::make_string_type();
6582 tree string_type = type_to_tree(st->get_backend(context->gogo()));
6583 static tree string_plus_decl;
6584 return Gogo::call_builtin(&string_plus_decl,
6595 tree compute_type = excess_precision_type(type);
6596 if (compute_type != NULL_TREE)
6598 left = ::convert(compute_type, left);
6599 right = ::convert(compute_type, right);
6602 tree eval_saved = NULL_TREE;
6605 // Make sure the values are evaluated.
6606 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
6608 left = save_expr(left);
6611 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
6613 right = save_expr(right);
6614 if (eval_saved == NULL_TREE)
6617 eval_saved = fold_build2_loc(this->location().gcc_location(),
6619 void_type_node, eval_saved, right);
6623 tree ret = fold_build2_loc(this->location().gcc_location(),
6625 compute_type != NULL_TREE ? compute_type : type,
6628 if (compute_type != NULL_TREE)
6629 ret = ::convert(type, ret);
6631 // In Go, a shift larger than the size of the type is well-defined.
6632 // This is not true in GENERIC, so we need to insert a conditional.
6635 go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
6636 go_assert(this->left_->type()->integer_type() != NULL);
6637 int bits = TYPE_PRECISION(TREE_TYPE(left));
6639 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
6640 build_int_cst_type(TREE_TYPE(right), bits));
6642 tree overflow_result = fold_convert_loc(this->location().gcc_location(),
6645 if (this->op_ == OPERATOR_RSHIFT
6646 && !this->left_->type()->integer_type()->is_unsigned())
6649 fold_build2_loc(this->location().gcc_location(), LT_EXPR,
6650 boolean_type_node, left,
6651 fold_convert_loc(this->location().gcc_location(),
6653 integer_zero_node));
6655 fold_build2_loc(this->location().gcc_location(),
6656 MINUS_EXPR, TREE_TYPE(left),
6657 fold_convert_loc(this->location().gcc_location(),
6660 fold_convert_loc(this->location().gcc_location(),
6664 fold_build3_loc(this->location().gcc_location(), COND_EXPR,
6665 TREE_TYPE(left), neg, neg_one,
6669 ret = fold_build3_loc(this->location().gcc_location(), COND_EXPR,
6670 TREE_TYPE(left), compare, ret, overflow_result);
6672 if (eval_saved != NULL_TREE)
6673 ret = fold_build2_loc(this->location().gcc_location(), COMPOUND_EXPR,
6674 TREE_TYPE(ret), eval_saved, ret);
6680 // Export a binary expression.
6683 Binary_expression::do_export(Export* exp) const
6685 exp->write_c_string("(");
6686 this->left_->export_expression(exp);
6690 exp->write_c_string(" || ");
6692 case OPERATOR_ANDAND:
6693 exp->write_c_string(" && ");
6696 exp->write_c_string(" == ");
6698 case OPERATOR_NOTEQ:
6699 exp->write_c_string(" != ");
6702 exp->write_c_string(" < ");
6705 exp->write_c_string(" <= ");
6708 exp->write_c_string(" > ");
6711 exp->write_c_string(" >= ");
6714 exp->write_c_string(" + ");
6716 case OPERATOR_MINUS:
6717 exp->write_c_string(" - ");
6720 exp->write_c_string(" | ");
6723 exp->write_c_string(" ^ ");
6726 exp->write_c_string(" * ");
6729 exp->write_c_string(" / ");
6732 exp->write_c_string(" % ");
6734 case OPERATOR_LSHIFT:
6735 exp->write_c_string(" << ");
6737 case OPERATOR_RSHIFT:
6738 exp->write_c_string(" >> ");
6741 exp->write_c_string(" & ");
6743 case OPERATOR_BITCLEAR:
6744 exp->write_c_string(" &^ ");
6749 this->right_->export_expression(exp);
6750 exp->write_c_string(")");
6753 // Import a binary expression.
6756 Binary_expression::do_import(Import* imp)
6758 imp->require_c_string("(");
6760 Expression* left = Expression::import_expression(imp);
6763 if (imp->match_c_string(" || "))
6768 else if (imp->match_c_string(" && "))
6770 op = OPERATOR_ANDAND;
6773 else if (imp->match_c_string(" == "))
6778 else if (imp->match_c_string(" != "))
6780 op = OPERATOR_NOTEQ;
6783 else if (imp->match_c_string(" < "))
6788 else if (imp->match_c_string(" <= "))
6793 else if (imp->match_c_string(" > "))
6798 else if (imp->match_c_string(" >= "))
6803 else if (imp->match_c_string(" + "))
6808 else if (imp->match_c_string(" - "))
6810 op = OPERATOR_MINUS;
6813 else if (imp->match_c_string(" | "))
6818 else if (imp->match_c_string(" ^ "))
6823 else if (imp->match_c_string(" * "))
6828 else if (imp->match_c_string(" / "))
6833 else if (imp->match_c_string(" % "))
6838 else if (imp->match_c_string(" << "))
6840 op = OPERATOR_LSHIFT;
6843 else if (imp->match_c_string(" >> "))
6845 op = OPERATOR_RSHIFT;
6848 else if (imp->match_c_string(" & "))
6853 else if (imp->match_c_string(" &^ "))
6855 op = OPERATOR_BITCLEAR;
6860 error_at(imp->location(), "unrecognized binary operator");
6861 return Expression::make_error(imp->location());
6864 Expression* right = Expression::import_expression(imp);
6866 imp->require_c_string(")");
6868 return Expression::make_binary(op, left, right, imp->location());
6871 // Dump ast representation of a binary expression.
6874 Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
6876 ast_dump_context->ostream() << "(";
6877 ast_dump_context->dump_expression(this->left_);
6878 ast_dump_context->ostream() << " ";
6879 ast_dump_context->dump_operator(this->op_);
6880 ast_dump_context->ostream() << " ";
6881 ast_dump_context->dump_expression(this->right_);
6882 ast_dump_context->ostream() << ") ";
6885 // Make a binary expression.
6888 Expression::make_binary(Operator op, Expression* left, Expression* right,
6891 return new Binary_expression(op, left, right, location);
6894 // Implement a comparison.
6897 Expression::comparison_tree(Translate_context* context, Operator op,
6898 Type* left_type, tree left_tree,
6899 Type* right_type, tree right_tree,
6902 enum tree_code code;
6908 case OPERATOR_NOTEQ:
6927 if (left_type->is_string_type() && right_type->is_string_type())
6929 Type* st = Type::make_string_type();
6930 tree string_type = type_to_tree(st->get_backend(context->gogo()));
6931 static tree string_compare_decl;
6932 left_tree = Gogo::call_builtin(&string_compare_decl,
6941 right_tree = build_int_cst_type(integer_type_node, 0);
6943 else if ((left_type->interface_type() != NULL
6944 && right_type->interface_type() == NULL
6945 && !right_type->is_nil_type())
6946 || (left_type->interface_type() == NULL
6947 && !left_type->is_nil_type()
6948 && right_type->interface_type() != NULL))
6950 // Comparing an interface value to a non-interface value.
6951 if (left_type->interface_type() == NULL)
6953 std::swap(left_type, right_type);
6954 std::swap(left_tree, right_tree);
6957 // The right operand is not an interface. We need to take its
6958 // address if it is not a pointer.
6961 if (right_type->points_to() != NULL)
6963 make_tmp = NULL_TREE;
6966 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
6968 make_tmp = NULL_TREE;
6969 arg = build_fold_addr_expr_loc(location.gcc_location(), right_tree);
6970 if (DECL_P(right_tree))
6971 TREE_ADDRESSABLE(right_tree) = 1;
6975 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6976 get_name(right_tree));
6977 DECL_IGNORED_P(tmp) = 0;
6978 DECL_INITIAL(tmp) = right_tree;
6979 TREE_ADDRESSABLE(tmp) = 1;
6980 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6981 SET_EXPR_LOCATION(make_tmp, location.gcc_location());
6982 arg = build_fold_addr_expr_loc(location.gcc_location(), tmp);
6984 arg = fold_convert_loc(location.gcc_location(), ptr_type_node, arg);
6986 tree descriptor = right_type->type_descriptor_pointer(context->gogo(),
6989 if (left_type->interface_type()->is_empty())
6991 static tree empty_interface_value_compare_decl;
6992 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6994 "__go_empty_interface_value_compare",
6997 TREE_TYPE(left_tree),
6999 TREE_TYPE(descriptor),
7003 if (left_tree == error_mark_node)
7004 return error_mark_node;
7005 // This can panic if the type is not comparable.
7006 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
7010 static tree interface_value_compare_decl;
7011 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
7013 "__go_interface_value_compare",
7016 TREE_TYPE(left_tree),
7018 TREE_TYPE(descriptor),
7022 if (left_tree == error_mark_node)
7023 return error_mark_node;
7024 // This can panic if the type is not comparable.
7025 TREE_NOTHROW(interface_value_compare_decl) = 0;
7027 right_tree = build_int_cst_type(integer_type_node, 0);
7029 if (make_tmp != NULL_TREE)
7030 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
7033 else if (left_type->interface_type() != NULL
7034 && right_type->interface_type() != NULL)
7036 if (left_type->interface_type()->is_empty()
7037 && right_type->interface_type()->is_empty())
7039 static tree empty_interface_compare_decl;
7040 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
7042 "__go_empty_interface_compare",
7045 TREE_TYPE(left_tree),
7047 TREE_TYPE(right_tree),
7049 if (left_tree == error_mark_node)
7050 return error_mark_node;
7051 // This can panic if the type is uncomparable.
7052 TREE_NOTHROW(empty_interface_compare_decl) = 0;
7054 else if (!left_type->interface_type()->is_empty()
7055 && !right_type->interface_type()->is_empty())
7057 static tree interface_compare_decl;
7058 left_tree = Gogo::call_builtin(&interface_compare_decl,
7060 "__go_interface_compare",
7063 TREE_TYPE(left_tree),
7065 TREE_TYPE(right_tree),
7067 if (left_tree == error_mark_node)
7068 return error_mark_node;
7069 // This can panic if the type is uncomparable.
7070 TREE_NOTHROW(interface_compare_decl) = 0;
7074 if (left_type->interface_type()->is_empty())
7076 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
7077 std::swap(left_type, right_type);
7078 std::swap(left_tree, right_tree);
7080 go_assert(!left_type->interface_type()->is_empty());
7081 go_assert(right_type->interface_type()->is_empty());
7082 static tree interface_empty_compare_decl;
7083 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
7085 "__go_interface_empty_compare",
7088 TREE_TYPE(left_tree),
7090 TREE_TYPE(right_tree),
7092 if (left_tree == error_mark_node)
7093 return error_mark_node;
7094 // This can panic if the type is uncomparable.
7095 TREE_NOTHROW(interface_empty_compare_decl) = 0;
7098 right_tree = build_int_cst_type(integer_type_node, 0);
7101 if (left_type->is_nil_type()
7102 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
7104 std::swap(left_type, right_type);
7105 std::swap(left_tree, right_tree);
7108 if (right_type->is_nil_type())
7110 if (left_type->array_type() != NULL
7111 && left_type->array_type()->length() == NULL)
7113 Array_type* at = left_type->array_type();
7114 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
7115 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
7117 else if (left_type->interface_type() != NULL)
7119 // An interface is nil if the first field is nil.
7120 tree left_type_tree = TREE_TYPE(left_tree);
7121 go_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
7122 tree field = TYPE_FIELDS(left_type_tree);
7123 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
7125 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
7129 go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
7130 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
7134 if (left_tree == error_mark_node || right_tree == error_mark_node)
7135 return error_mark_node;
7137 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
7138 if (CAN_HAVE_LOCATION_P(ret))
7139 SET_EXPR_LOCATION(ret, location.gcc_location());
7143 // Class Bound_method_expression.
7148 Bound_method_expression::do_traverse(Traverse* traverse)
7150 return Expression::traverse(&this->expr_, traverse);
7153 // Return the type of a bound method expression. The type of this
7154 // object is really the type of the method with no receiver. We
7155 // should be able to get away with just returning the type of the
7159 Bound_method_expression::do_type()
7161 if (this->method_->is_function())
7162 return this->method_->func_value()->type();
7163 else if (this->method_->is_function_declaration())
7164 return this->method_->func_declaration_value()->type();
7166 return Type::make_error_type();
7169 // Determine the types of a method expression.
7172 Bound_method_expression::do_determine_type(const Type_context*)
7174 Function_type* fntype = this->type()->function_type();
7175 if (fntype == NULL || !fntype->is_method())
7176 this->expr_->determine_type_no_context();
7179 Type_context subcontext(fntype->receiver()->type(), false);
7180 this->expr_->determine_type(&subcontext);
7184 // Check the types of a method expression.
7187 Bound_method_expression::do_check_types(Gogo*)
7189 if (!this->method_->is_function()
7190 && !this->method_->is_function_declaration())
7191 this->report_error(_("object is not a method"));
7194 Type* rtype = this->type()->function_type()->receiver()->type()->deref();
7195 Type* etype = (this->expr_type_ != NULL
7197 : this->expr_->type());
7198 etype = etype->deref();
7199 if (!Type::are_identical(rtype, etype, true, NULL))
7200 this->report_error(_("method type does not match object type"));
7204 // Get the tree for a method expression. There is no standard tree
7205 // representation for this. The only places it may currently be used
7206 // are in a Call_expression or a Go_statement, which will take it
7207 // apart directly. So this has nothing to do at present.
7210 Bound_method_expression::do_get_tree(Translate_context*)
7212 error_at(this->location(), "reference to method other than calling it");
7213 return error_mark_node;
7216 // Dump ast representation of a bound method expression.
7219 Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
7222 if (this->expr_type_ != NULL)
7223 ast_dump_context->ostream() << "(";
7224 ast_dump_context->dump_expression(this->expr_);
7225 if (this->expr_type_ != NULL)
7227 ast_dump_context->ostream() << ":";
7228 ast_dump_context->dump_type(this->expr_type_);
7229 ast_dump_context->ostream() << ")";
7232 ast_dump_context->ostream() << "." << this->method_->name();
7235 // Make a method expression.
7237 Bound_method_expression*
7238 Expression::make_bound_method(Expression* expr, Named_object* method,
7241 return new Bound_method_expression(expr, method, location);
7244 // Class Builtin_call_expression. This is used for a call to a
7245 // builtin function.
7247 class Builtin_call_expression : public Call_expression
7250 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
7251 bool is_varargs, Location location);
7254 // This overrides Call_expression::do_lower.
7256 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
7259 do_is_constant() const;
7262 do_integer_constant_value(bool, mpz_t, Type**) const;
7265 do_float_constant_value(mpfr_t, Type**) const;
7268 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
7271 do_discarding_value();
7277 do_determine_type(const Type_context*);
7280 do_check_types(Gogo*);
7285 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
7286 this->args()->copy(),
7292 do_get_tree(Translate_context*);
7295 do_export(Export*) const;
7298 do_is_recover_call() const;
7301 do_set_recover_arg(Expression*);
7304 // The builtin functions.
7305 enum Builtin_function_code
7309 // Predeclared builtin functions.
7326 // Builtin functions from the unsafe package.
7339 real_imag_type(Type*);
7342 complex_type(Type*);
7348 check_int_value(Expression*);
7350 // A pointer back to the general IR structure. This avoids a global
7351 // variable, or passing it around everywhere.
7353 // The builtin function being called.
7354 Builtin_function_code code_;
7355 // Used to stop endless loops when the length of an array uses len
7356 // or cap of the array itself.
7360 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
7362 Expression_list* args,
7365 : Call_expression(fn, args, is_varargs, location),
7366 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
7368 Func_expression* fnexp = this->fn()->func_expression();
7369 go_assert(fnexp != NULL);
7370 const std::string& name(fnexp->named_object()->name());
7371 if (name == "append")
7372 this->code_ = BUILTIN_APPEND;
7373 else if (name == "cap")
7374 this->code_ = BUILTIN_CAP;
7375 else if (name == "close")
7376 this->code_ = BUILTIN_CLOSE;
7377 else if (name == "complex")
7378 this->code_ = BUILTIN_COMPLEX;
7379 else if (name == "copy")
7380 this->code_ = BUILTIN_COPY;
7381 else if (name == "delete")
7382 this->code_ = BUILTIN_DELETE;
7383 else if (name == "imag")
7384 this->code_ = BUILTIN_IMAG;
7385 else if (name == "len")
7386 this->code_ = BUILTIN_LEN;
7387 else if (name == "make")
7388 this->code_ = BUILTIN_MAKE;
7389 else if (name == "new")
7390 this->code_ = BUILTIN_NEW;
7391 else if (name == "panic")
7392 this->code_ = BUILTIN_PANIC;
7393 else if (name == "print")
7394 this->code_ = BUILTIN_PRINT;
7395 else if (name == "println")
7396 this->code_ = BUILTIN_PRINTLN;
7397 else if (name == "real")
7398 this->code_ = BUILTIN_REAL;
7399 else if (name == "recover")
7400 this->code_ = BUILTIN_RECOVER;
7401 else if (name == "Alignof")
7402 this->code_ = BUILTIN_ALIGNOF;
7403 else if (name == "Offsetof")
7404 this->code_ = BUILTIN_OFFSETOF;
7405 else if (name == "Sizeof")
7406 this->code_ = BUILTIN_SIZEOF;
7411 // Return whether this is a call to recover. This is a virtual
7412 // function called from the parent class.
7415 Builtin_call_expression::do_is_recover_call() const
7417 if (this->classification() == EXPRESSION_ERROR)
7419 return this->code_ == BUILTIN_RECOVER;
7422 // Set the argument for a call to recover.
7425 Builtin_call_expression::do_set_recover_arg(Expression* arg)
7427 const Expression_list* args = this->args();
7428 go_assert(args == NULL || args->empty());
7429 Expression_list* new_args = new Expression_list();
7430 new_args->push_back(arg);
7431 this->set_args(new_args);
7434 // A traversal class which looks for a call expression.
7436 class Find_call_expression : public Traverse
7439 Find_call_expression()
7440 : Traverse(traverse_expressions),
7445 expression(Expression**);
7449 { return this->found_; }
7456 Find_call_expression::expression(Expression** pexpr)
7458 if ((*pexpr)->call_expression() != NULL)
7460 this->found_ = true;
7461 return TRAVERSE_EXIT;
7463 return TRAVERSE_CONTINUE;
7466 // Lower a builtin call expression. This turns new and make into
7467 // specific expressions. We also convert to a constant if we can.
7470 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function,
7471 Statement_inserter* inserter, int)
7473 if (this->classification() == EXPRESSION_ERROR)
7476 Location loc = this->location();
7478 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
7480 this->report_error(_("invalid use of %<...%> with builtin function"));
7481 return Expression::make_error(loc);
7484 if (this->is_constant())
7486 // We can only lower len and cap if there are no function calls
7487 // in the arguments. Otherwise we have to make the call.
7488 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
7490 Expression* arg = this->one_arg();
7491 if (!arg->is_constant())
7493 Find_call_expression find_call;
7494 Expression::traverse(&arg, &find_call);
7495 if (find_call.found())
7503 if (this->integer_constant_value(true, ival, &type))
7505 Expression* ret = Expression::make_integer(&ival, type, loc);
7513 if (this->float_constant_value(rval, &type))
7515 Expression* ret = Expression::make_float(&rval, type, loc);
7522 if (this->complex_constant_value(rval, imag, &type))
7524 Expression* ret = Expression::make_complex(&rval, &imag, type, loc);
7533 switch (this->code_)
7540 const Expression_list* args = this->args();
7541 if (args == NULL || args->size() < 1)
7542 this->report_error(_("not enough arguments"));
7543 else if (args->size() > 1)
7544 this->report_error(_("too many arguments"));
7547 Expression* arg = args->front();
7548 if (!arg->is_type_expression())
7550 error_at(arg->location(), "expected type");
7551 this->set_is_error();
7554 return Expression::make_allocation(arg->type(), loc);
7560 return this->lower_make();
7562 case BUILTIN_RECOVER:
7563 if (function != NULL)
7564 function->func_value()->set_calls_recover();
7567 // Calling recover outside of a function always returns the
7568 // nil empty interface.
7569 Type* eface = Type::make_interface_type(NULL, loc);
7570 return Expression::make_cast(eface, Expression::make_nil(loc), loc);
7574 case BUILTIN_APPEND:
7576 // Lower the varargs.
7577 const Expression_list* args = this->args();
7578 if (args == NULL || args->empty())
7580 Type* slice_type = args->front()->type();
7581 if (!slice_type->is_slice_type())
7583 error_at(args->front()->location(), "argument 1 must be a slice");
7584 this->set_is_error();
7587 this->lower_varargs(gogo, function, inserter, slice_type, 2);
7591 case BUILTIN_DELETE:
7593 // Lower to a runtime function call.
7594 const Expression_list* args = this->args();
7595 if (args == NULL || args->size() < 2)
7596 this->report_error(_("not enough arguments"));
7597 else if (args->size() > 2)
7598 this->report_error(_("too many arguments"));
7599 else if (args->front()->type()->map_type() == NULL)
7600 this->report_error(_("argument 1 must be a map"));
7603 // Since this function returns no value it must appear in
7604 // a statement by itself, so we don't have to worry about
7605 // order of evaluation of values around it. Evaluate the
7606 // map first to get order of evaluation right.
7607 Map_type* mt = args->front()->type()->map_type();
7608 Temporary_statement* map_temp =
7609 Statement::make_temporary(mt, args->front(), loc);
7610 inserter->insert(map_temp);
7612 Temporary_statement* key_temp =
7613 Statement::make_temporary(mt->key_type(), args->back(), loc);
7614 inserter->insert(key_temp);
7616 Expression* e1 = Expression::make_temporary_reference(map_temp,
7618 Expression* e2 = Expression::make_temporary_reference(key_temp,
7620 e2 = Expression::make_unary(OPERATOR_AND, e2, loc);
7621 return Runtime::make_call(Runtime::MAPDELETE, this->location(),
7631 // Lower a make expression.
7634 Builtin_call_expression::lower_make()
7636 Location loc = this->location();
7638 const Expression_list* args = this->args();
7639 if (args == NULL || args->size() < 1)
7641 this->report_error(_("not enough arguments"));
7642 return Expression::make_error(this->location());
7645 Expression_list::const_iterator parg = args->begin();
7647 Expression* first_arg = *parg;
7648 if (!first_arg->is_type_expression())
7650 error_at(first_arg->location(), "expected type");
7651 this->set_is_error();
7652 return Expression::make_error(this->location());
7654 Type* type = first_arg->type();
7656 bool is_slice = false;
7657 bool is_map = false;
7658 bool is_chan = false;
7659 if (type->is_slice_type())
7661 else if (type->map_type() != NULL)
7663 else if (type->channel_type() != NULL)
7667 this->report_error(_("invalid type for make function"));
7668 return Expression::make_error(this->location());
7672 Expression* len_arg;
7673 if (parg == args->end())
7677 this->report_error(_("length required when allocating a slice"));
7678 return Expression::make_error(this->location());
7682 mpz_init_set_ui(zval, 0);
7683 len_arg = Expression::make_integer(&zval, NULL, loc);
7689 if (!this->check_int_value(len_arg))
7691 this->report_error(_("bad size for make"));
7692 return Expression::make_error(this->location());
7697 Expression* cap_arg = NULL;
7698 if (is_slice && parg != args->end())
7701 if (!this->check_int_value(cap_arg))
7703 this->report_error(_("bad capacity when making slice"));
7704 return Expression::make_error(this->location());
7709 if (parg != args->end())
7711 this->report_error(_("too many arguments to make"));
7712 return Expression::make_error(this->location());
7715 Location type_loc = first_arg->location();
7716 Expression* type_arg;
7717 if (is_slice || is_chan)
7718 type_arg = Expression::make_type_descriptor(type, type_loc);
7720 type_arg = Expression::make_map_descriptor(type->map_type(), type_loc);
7727 if (cap_arg == NULL)
7728 call = Runtime::make_call(Runtime::MAKESLICE1, loc, 2, type_arg,
7731 call = Runtime::make_call(Runtime::MAKESLICE2, loc, 3, type_arg,
7735 call = Runtime::make_call(Runtime::MAKEMAP, loc, 2, type_arg, len_arg);
7737 call = Runtime::make_call(Runtime::MAKECHAN, loc, 2, type_arg, len_arg);
7741 return Expression::make_unsafe_cast(type, call, loc);
7744 // Return whether an expression has an integer value. Report an error
7745 // if not. This is used when handling calls to the predeclared make
7749 Builtin_call_expression::check_int_value(Expression* e)
7751 if (e->type()->integer_type() != NULL)
7754 // Check for a floating point constant with integer value.
7759 if (e->float_constant_value(fval, &dummy) && mpfr_integer_p(fval))
7766 mpfr_clear_overflow();
7767 mpfr_clear_erangeflag();
7768 mpfr_get_z(ival, fval, GMP_RNDN);
7769 if (!mpfr_overflow_p()
7770 && !mpfr_erangeflag_p()
7771 && mpz_sgn(ival) >= 0)
7773 Named_type* ntype = Type::lookup_integer_type("int");
7774 Integer_type* inttype = ntype->integer_type();
7776 mpz_init_set_ui(max, 1);
7777 mpz_mul_2exp(max, max, inttype->bits() - 1);
7778 ok = mpz_cmp(ival, max) < 0;
7795 // Return the type of the real or imag functions, given the type of
7796 // the argument. We need to map complex to float, complex64 to
7797 // float32, and complex128 to float64, so it has to be done by name.
7798 // This returns NULL if it can't figure out the type.
7801 Builtin_call_expression::real_imag_type(Type* arg_type)
7803 if (arg_type == NULL || arg_type->is_abstract())
7805 Named_type* nt = arg_type->named_type();
7808 while (nt->real_type()->named_type() != NULL)
7809 nt = nt->real_type()->named_type();
7810 if (nt->name() == "complex64")
7811 return Type::lookup_float_type("float32");
7812 else if (nt->name() == "complex128")
7813 return Type::lookup_float_type("float64");
7818 // Return the type of the complex function, given the type of one of the
7819 // argments. Like real_imag_type, we have to map by name.
7822 Builtin_call_expression::complex_type(Type* arg_type)
7824 if (arg_type == NULL || arg_type->is_abstract())
7826 Named_type* nt = arg_type->named_type();
7829 while (nt->real_type()->named_type() != NULL)
7830 nt = nt->real_type()->named_type();
7831 if (nt->name() == "float32")
7832 return Type::lookup_complex_type("complex64");
7833 else if (nt->name() == "float64")
7834 return Type::lookup_complex_type("complex128");
7839 // Return a single argument, or NULL if there isn't one.
7842 Builtin_call_expression::one_arg() const
7844 const Expression_list* args = this->args();
7845 if (args->size() != 1)
7847 return args->front();
7850 // Return whether this is constant: len of a string, or len or cap of
7851 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
7854 Builtin_call_expression::do_is_constant() const
7856 switch (this->code_)
7864 Expression* arg = this->one_arg();
7867 Type* arg_type = arg->type();
7869 if (arg_type->points_to() != NULL
7870 && arg_type->points_to()->array_type() != NULL
7871 && !arg_type->points_to()->is_slice_type())
7872 arg_type = arg_type->points_to();
7874 if (arg_type->array_type() != NULL
7875 && arg_type->array_type()->length() != NULL)
7878 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7881 bool ret = arg->is_constant();
7882 this->seen_ = false;
7888 case BUILTIN_SIZEOF:
7889 case BUILTIN_ALIGNOF:
7890 return this->one_arg() != NULL;
7892 case BUILTIN_OFFSETOF:
7894 Expression* arg = this->one_arg();
7897 return arg->field_reference_expression() != NULL;
7900 case BUILTIN_COMPLEX:
7902 const Expression_list* args = this->args();
7903 if (args != NULL && args->size() == 2)
7904 return args->front()->is_constant() && args->back()->is_constant();
7911 Expression* arg = this->one_arg();
7912 return arg != NULL && arg->is_constant();
7922 // Return an integer constant value if possible.
7925 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
7929 if (this->code_ == BUILTIN_LEN
7930 || this->code_ == BUILTIN_CAP)
7932 Expression* arg = this->one_arg();
7935 Type* arg_type = arg->type();
7937 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7940 if (arg->string_constant_value(&sval))
7942 mpz_set_ui(val, sval.length());
7943 *ptype = Type::lookup_integer_type("int");
7948 if (arg_type->points_to() != NULL
7949 && arg_type->points_to()->array_type() != NULL
7950 && !arg_type->points_to()->is_slice_type())
7951 arg_type = arg_type->points_to();
7953 if (arg_type->array_type() != NULL
7954 && arg_type->array_type()->length() != NULL)
7958 Expression* e = arg_type->array_type()->length();
7960 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
7961 this->seen_ = false;
7964 *ptype = Type::lookup_integer_type("int");
7969 else if (this->code_ == BUILTIN_SIZEOF
7970 || this->code_ == BUILTIN_ALIGNOF)
7972 Expression* arg = this->one_arg();
7975 Type* arg_type = arg->type();
7976 if (arg_type->is_error())
7978 if (arg_type->is_abstract())
7980 if (arg_type->named_type() != NULL)
7981 arg_type->named_type()->convert(this->gogo_);
7984 if (this->code_ == BUILTIN_SIZEOF)
7986 if (!arg_type->backend_type_size(this->gogo_, &ret))
7989 else if (this->code_ == BUILTIN_ALIGNOF)
7991 if (arg->field_reference_expression() == NULL)
7993 if (!arg_type->backend_type_align(this->gogo_, &ret))
7998 // Calling unsafe.Alignof(s.f) returns the alignment of
7999 // the type of f when it is used as a field in a struct.
8000 if (!arg_type->backend_type_field_align(this->gogo_, &ret))
8007 mpz_set_ui(val, ret);
8011 else if (this->code_ == BUILTIN_OFFSETOF)
8013 Expression* arg = this->one_arg();
8016 Field_reference_expression* farg = arg->field_reference_expression();
8019 Expression* struct_expr = farg->expr();
8020 Type* st = struct_expr->type();
8021 if (st->struct_type() == NULL)
8023 if (st->named_type() != NULL)
8024 st->named_type()->convert(this->gogo_);
8025 unsigned int offset;
8026 if (!st->struct_type()->backend_field_offset(this->gogo_,
8027 farg->field_index(),
8030 mpz_set_ui(val, offset);
8036 // Return a floating point constant value if possible.
8039 Builtin_call_expression::do_float_constant_value(mpfr_t val,
8042 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
8044 Expression* arg = this->one_arg();
8055 if (arg->complex_constant_value(real, imag, &type))
8057 if (this->code_ == BUILTIN_REAL)
8058 mpfr_set(val, real, GMP_RNDN);
8060 mpfr_set(val, imag, GMP_RNDN);
8061 *ptype = Builtin_call_expression::real_imag_type(type);
8073 // Return a complex constant value if possible.
8076 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
8079 if (this->code_ == BUILTIN_COMPLEX)
8081 const Expression_list* args = this->args();
8082 if (args == NULL || args->size() != 2)
8088 if (!args->front()->float_constant_value(r, &rtype))
8099 if (args->back()->float_constant_value(i, &itype)
8100 && Type::are_identical(rtype, itype, false, NULL))
8102 mpfr_set(real, r, GMP_RNDN);
8103 mpfr_set(imag, i, GMP_RNDN);
8104 *ptype = Builtin_call_expression::complex_type(rtype);
8117 // Give an error if we are discarding the value of an expression which
8118 // should not normally be discarded. We don't give an error for
8119 // discarding the value of an ordinary function call, but we do for
8120 // builtin functions, purely for consistency with the gc compiler.
8123 Builtin_call_expression::do_discarding_value()
8125 switch (this->code_)
8127 case BUILTIN_INVALID:
8131 case BUILTIN_APPEND:
8133 case BUILTIN_COMPLEX:
8139 case BUILTIN_ALIGNOF:
8140 case BUILTIN_OFFSETOF:
8141 case BUILTIN_SIZEOF:
8142 this->unused_value_error();
8147 case BUILTIN_DELETE:
8150 case BUILTIN_PRINTLN:
8151 case BUILTIN_RECOVER:
8159 Builtin_call_expression::do_type()
8161 switch (this->code_)
8163 case BUILTIN_INVALID:
8170 const Expression_list* args = this->args();
8171 if (args == NULL || args->empty())
8172 return Type::make_error_type();
8173 return Type::make_pointer_type(args->front()->type());
8179 case BUILTIN_ALIGNOF:
8180 case BUILTIN_OFFSETOF:
8181 case BUILTIN_SIZEOF:
8182 return Type::lookup_integer_type("int");
8185 case BUILTIN_DELETE:
8188 case BUILTIN_PRINTLN:
8189 return Type::make_void_type();
8191 case BUILTIN_RECOVER:
8192 return Type::make_interface_type(NULL, Linemap::predeclared_location());
8194 case BUILTIN_APPEND:
8196 const Expression_list* args = this->args();
8197 if (args == NULL || args->empty())
8198 return Type::make_error_type();
8199 return args->front()->type();
8205 Expression* arg = this->one_arg();
8207 return Type::make_error_type();
8208 Type* t = arg->type();
8209 if (t->is_abstract())
8210 t = t->make_non_abstract_type();
8211 t = Builtin_call_expression::real_imag_type(t);
8213 t = Type::make_error_type();
8217 case BUILTIN_COMPLEX:
8219 const Expression_list* args = this->args();
8220 if (args == NULL || args->size() != 2)
8221 return Type::make_error_type();
8222 Type* t = args->front()->type();
8223 if (t->is_abstract())
8225 t = args->back()->type();
8226 if (t->is_abstract())
8227 t = t->make_non_abstract_type();
8229 t = Builtin_call_expression::complex_type(t);
8231 t = Type::make_error_type();
8237 // Determine the type.
8240 Builtin_call_expression::do_determine_type(const Type_context* context)
8242 if (!this->determining_types())
8245 this->fn()->determine_type_no_context();
8247 const Expression_list* args = this->args();
8250 Type* arg_type = NULL;
8251 switch (this->code_)
8254 case BUILTIN_PRINTLN:
8255 // Do not force a large integer constant to "int".
8261 arg_type = Builtin_call_expression::complex_type(context->type);
8265 case BUILTIN_COMPLEX:
8267 // For the complex function the type of one operand can
8268 // determine the type of the other, as in a binary expression.
8269 arg_type = Builtin_call_expression::real_imag_type(context->type);
8270 if (args != NULL && args->size() == 2)
8272 Type* t1 = args->front()->type();
8273 Type* t2 = args->front()->type();
8274 if (!t1->is_abstract())
8276 else if (!t2->is_abstract())
8290 for (Expression_list::const_iterator pa = args->begin();
8294 Type_context subcontext;
8295 subcontext.type = arg_type;
8299 // We want to print large constants, we so can't just
8300 // use the appropriate nonabstract type. Use uint64 for
8301 // an integer if we know it is nonnegative, otherwise
8302 // use int64 for a integer, otherwise use float64 for a
8303 // float or complex128 for a complex.
8304 Type* want_type = NULL;
8305 Type* atype = (*pa)->type();
8306 if (atype->is_abstract())
8308 if (atype->integer_type() != NULL)
8313 if (this->integer_constant_value(true, val, &dummy)
8314 && mpz_sgn(val) >= 0)
8315 want_type = Type::lookup_integer_type("uint64");
8317 want_type = Type::lookup_integer_type("int64");
8320 else if (atype->float_type() != NULL)
8321 want_type = Type::lookup_float_type("float64");
8322 else if (atype->complex_type() != NULL)
8323 want_type = Type::lookup_complex_type("complex128");
8324 else if (atype->is_abstract_string_type())
8325 want_type = Type::lookup_string_type();
8326 else if (atype->is_abstract_boolean_type())
8327 want_type = Type::lookup_bool_type();
8330 subcontext.type = want_type;
8334 (*pa)->determine_type(&subcontext);
8339 // If there is exactly one argument, return true. Otherwise give an
8340 // error message and return false.
8343 Builtin_call_expression::check_one_arg()
8345 const Expression_list* args = this->args();
8346 if (args == NULL || args->size() < 1)
8348 this->report_error(_("not enough arguments"));
8351 else if (args->size() > 1)
8353 this->report_error(_("too many arguments"));
8356 if (args->front()->is_error_expression()
8357 || args->front()->type()->is_error())
8359 this->set_is_error();
8365 // Check argument types for a builtin function.
8368 Builtin_call_expression::do_check_types(Gogo*)
8370 switch (this->code_)
8372 case BUILTIN_INVALID:
8380 // The single argument may be either a string or an array or a
8381 // map or a channel, or a pointer to a closed array.
8382 if (this->check_one_arg())
8384 Type* arg_type = this->one_arg()->type();
8385 if (arg_type->points_to() != NULL
8386 && arg_type->points_to()->array_type() != NULL
8387 && !arg_type->points_to()->is_slice_type())
8388 arg_type = arg_type->points_to();
8389 if (this->code_ == BUILTIN_CAP)
8391 if (!arg_type->is_error()
8392 && arg_type->array_type() == NULL
8393 && arg_type->channel_type() == NULL)
8394 this->report_error(_("argument must be array or slice "
8399 if (!arg_type->is_error()
8400 && !arg_type->is_string_type()
8401 && arg_type->array_type() == NULL
8402 && arg_type->map_type() == NULL
8403 && arg_type->channel_type() == NULL)
8404 this->report_error(_("argument must be string or "
8405 "array or slice or map or channel"));
8412 case BUILTIN_PRINTLN:
8414 const Expression_list* args = this->args();
8417 if (this->code_ == BUILTIN_PRINT)
8418 warning_at(this->location(), 0,
8419 "no arguments for builtin function %<%s%>",
8420 (this->code_ == BUILTIN_PRINT
8426 for (Expression_list::const_iterator p = args->begin();
8430 Type* type = (*p)->type();
8431 if (type->is_error()
8432 || type->is_string_type()
8433 || type->integer_type() != NULL
8434 || type->float_type() != NULL
8435 || type->complex_type() != NULL
8436 || type->is_boolean_type()
8437 || type->points_to() != NULL
8438 || type->interface_type() != NULL
8439 || type->channel_type() != NULL
8440 || type->map_type() != NULL
8441 || type->function_type() != NULL
8442 || type->is_slice_type())
8445 this->report_error(_("unsupported argument type to "
8446 "builtin function"));
8453 if (this->check_one_arg())
8455 if (this->one_arg()->type()->channel_type() == NULL)
8456 this->report_error(_("argument must be channel"));
8457 else if (!this->one_arg()->type()->channel_type()->may_send())
8458 this->report_error(_("cannot close receive-only channel"));
8463 case BUILTIN_SIZEOF:
8464 case BUILTIN_ALIGNOF:
8465 this->check_one_arg();
8468 case BUILTIN_RECOVER:
8469 if (this->args() != NULL && !this->args()->empty())
8470 this->report_error(_("too many arguments"));
8473 case BUILTIN_OFFSETOF:
8474 if (this->check_one_arg())
8476 Expression* arg = this->one_arg();
8477 if (arg->field_reference_expression() == NULL)
8478 this->report_error(_("argument must be a field reference"));
8484 const Expression_list* args = this->args();
8485 if (args == NULL || args->size() < 2)
8487 this->report_error(_("not enough arguments"));
8490 else if (args->size() > 2)
8492 this->report_error(_("too many arguments"));
8495 Type* arg1_type = args->front()->type();
8496 Type* arg2_type = args->back()->type();
8497 if (arg1_type->is_error() || arg2_type->is_error())
8501 if (arg1_type->is_slice_type())
8502 e1 = arg1_type->array_type()->element_type();
8505 this->report_error(_("left argument must be a slice"));
8509 if (arg2_type->is_slice_type())
8511 Type* e2 = arg2_type->array_type()->element_type();
8512 if (!Type::are_identical(e1, e2, true, NULL))
8513 this->report_error(_("element types must be the same"));
8515 else if (arg2_type->is_string_type())
8517 if (e1->integer_type() == NULL || !e1->integer_type()->is_byte())
8518 this->report_error(_("first argument must be []byte"));
8521 this->report_error(_("second argument must be slice or string"));
8525 case BUILTIN_APPEND:
8527 const Expression_list* args = this->args();
8528 if (args == NULL || args->size() < 2)
8530 this->report_error(_("not enough arguments"));
8533 if (args->size() > 2)
8535 this->report_error(_("too many arguments"));
8539 // The language permits appending a string to a []byte, as a
8541 if (args->back()->type()->is_string_type())
8543 const Array_type* at = args->front()->type()->array_type();
8544 const Type* e = at->element_type()->forwarded();
8545 if (e->integer_type() != NULL && e->integer_type()->is_byte())
8550 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
8554 this->report_error(_("arguments 1 and 2 have different types"));
8557 error_at(this->location(),
8558 "arguments 1 and 2 have different types (%s)",
8560 this->set_is_error();
8568 if (this->check_one_arg())
8570 if (this->one_arg()->type()->complex_type() == NULL)
8571 this->report_error(_("argument must have complex type"));
8575 case BUILTIN_COMPLEX:
8577 const Expression_list* args = this->args();
8578 if (args == NULL || args->size() < 2)
8579 this->report_error(_("not enough arguments"));
8580 else if (args->size() > 2)
8581 this->report_error(_("too many arguments"));
8582 else if (args->front()->is_error_expression()
8583 || args->front()->type()->is_error()
8584 || args->back()->is_error_expression()
8585 || args->back()->type()->is_error())
8586 this->set_is_error();
8587 else if (!Type::are_identical(args->front()->type(),
8588 args->back()->type(), true, NULL))
8589 this->report_error(_("complex arguments must have identical types"));
8590 else if (args->front()->type()->float_type() == NULL)
8591 this->report_error(_("complex arguments must have "
8592 "floating-point type"));
8601 // Return the tree for a builtin function.
8604 Builtin_call_expression::do_get_tree(Translate_context* context)
8606 Gogo* gogo = context->gogo();
8607 Location location = this->location();
8608 switch (this->code_)
8610 case BUILTIN_INVALID:
8618 const Expression_list* args = this->args();
8619 go_assert(args != NULL && args->size() == 1);
8620 Expression* arg = *args->begin();
8621 Type* arg_type = arg->type();
8625 go_assert(saw_errors());
8626 return error_mark_node;
8630 tree arg_tree = arg->get_tree(context);
8632 this->seen_ = false;
8634 if (arg_tree == error_mark_node)
8635 return error_mark_node;
8637 if (arg_type->points_to() != NULL)
8639 arg_type = arg_type->points_to();
8640 go_assert(arg_type->array_type() != NULL
8641 && !arg_type->is_slice_type());
8642 go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
8643 arg_tree = build_fold_indirect_ref(arg_tree);
8647 if (this->code_ == BUILTIN_LEN)
8649 if (arg_type->is_string_type())
8650 val_tree = String_type::length_tree(gogo, arg_tree);
8651 else if (arg_type->array_type() != NULL)
8655 go_assert(saw_errors());
8656 return error_mark_node;
8659 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
8660 this->seen_ = false;
8662 else if (arg_type->map_type() != NULL)
8664 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
8665 static tree map_len_fndecl;
8666 val_tree = Gogo::call_builtin(&map_len_fndecl,
8674 else if (arg_type->channel_type() != NULL)
8676 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
8677 static tree chan_len_fndecl;
8678 val_tree = Gogo::call_builtin(&chan_len_fndecl,
8691 if (arg_type->array_type() != NULL)
8695 go_assert(saw_errors());
8696 return error_mark_node;
8699 val_tree = arg_type->array_type()->capacity_tree(gogo,
8701 this->seen_ = false;
8703 else if (arg_type->channel_type() != NULL)
8705 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
8706 static tree chan_cap_fndecl;
8707 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
8719 if (val_tree == error_mark_node)
8720 return error_mark_node;
8722 Type* int_type = Type::lookup_integer_type("int");
8723 tree type_tree = type_to_tree(int_type->get_backend(gogo));
8724 if (type_tree == TREE_TYPE(val_tree))
8727 return fold(convert_to_integer(type_tree, val_tree));
8731 case BUILTIN_PRINTLN:
8733 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
8734 tree stmt_list = NULL_TREE;
8736 const Expression_list* call_args = this->args();
8737 if (call_args != NULL)
8739 for (Expression_list::const_iterator p = call_args->begin();
8740 p != call_args->end();
8743 if (is_ln && p != call_args->begin())
8745 static tree print_space_fndecl;
8746 tree call = Gogo::call_builtin(&print_space_fndecl,
8751 if (call == error_mark_node)
8752 return error_mark_node;
8753 append_to_statement_list(call, &stmt_list);
8756 Type* type = (*p)->type();
8758 tree arg = (*p)->get_tree(context);
8759 if (arg == error_mark_node)
8760 return error_mark_node;
8764 if (type->is_string_type())
8766 static tree print_string_fndecl;
8767 pfndecl = &print_string_fndecl;
8768 fnname = "__go_print_string";
8770 else if (type->integer_type() != NULL
8771 && type->integer_type()->is_unsigned())
8773 static tree print_uint64_fndecl;
8774 pfndecl = &print_uint64_fndecl;
8775 fnname = "__go_print_uint64";
8776 Type* itype = Type::lookup_integer_type("uint64");
8777 Btype* bitype = itype->get_backend(gogo);
8778 arg = fold_convert_loc(location.gcc_location(),
8779 type_to_tree(bitype), arg);
8781 else if (type->integer_type() != NULL)
8783 static tree print_int64_fndecl;
8784 pfndecl = &print_int64_fndecl;
8785 fnname = "__go_print_int64";
8786 Type* itype = Type::lookup_integer_type("int64");
8787 Btype* bitype = itype->get_backend(gogo);
8788 arg = fold_convert_loc(location.gcc_location(),
8789 type_to_tree(bitype), arg);
8791 else if (type->float_type() != NULL)
8793 static tree print_double_fndecl;
8794 pfndecl = &print_double_fndecl;
8795 fnname = "__go_print_double";
8796 arg = fold_convert_loc(location.gcc_location(),
8797 double_type_node, arg);
8799 else if (type->complex_type() != NULL)
8801 static tree print_complex_fndecl;
8802 pfndecl = &print_complex_fndecl;
8803 fnname = "__go_print_complex";
8804 arg = fold_convert_loc(location.gcc_location(),
8805 complex_double_type_node, arg);
8807 else if (type->is_boolean_type())
8809 static tree print_bool_fndecl;
8810 pfndecl = &print_bool_fndecl;
8811 fnname = "__go_print_bool";
8813 else if (type->points_to() != NULL
8814 || type->channel_type() != NULL
8815 || type->map_type() != NULL
8816 || type->function_type() != NULL)
8818 static tree print_pointer_fndecl;
8819 pfndecl = &print_pointer_fndecl;
8820 fnname = "__go_print_pointer";
8821 arg = fold_convert_loc(location.gcc_location(),
8822 ptr_type_node, arg);
8824 else if (type->interface_type() != NULL)
8826 if (type->interface_type()->is_empty())
8828 static tree print_empty_interface_fndecl;
8829 pfndecl = &print_empty_interface_fndecl;
8830 fnname = "__go_print_empty_interface";
8834 static tree print_interface_fndecl;
8835 pfndecl = &print_interface_fndecl;
8836 fnname = "__go_print_interface";
8839 else if (type->is_slice_type())
8841 static tree print_slice_fndecl;
8842 pfndecl = &print_slice_fndecl;
8843 fnname = "__go_print_slice";
8848 tree call = Gogo::call_builtin(pfndecl,
8855 if (call == error_mark_node)
8856 return error_mark_node;
8857 append_to_statement_list(call, &stmt_list);
8863 static tree print_nl_fndecl;
8864 tree call = Gogo::call_builtin(&print_nl_fndecl,
8869 if (call == error_mark_node)
8870 return error_mark_node;
8871 append_to_statement_list(call, &stmt_list);
8879 const Expression_list* args = this->args();
8880 go_assert(args != NULL && args->size() == 1);
8881 Expression* arg = args->front();
8882 tree arg_tree = arg->get_tree(context);
8883 if (arg_tree == error_mark_node)
8884 return error_mark_node;
8886 Type::make_interface_type(NULL, Linemap::predeclared_location());
8887 arg_tree = Expression::convert_for_assignment(context, empty,
8889 arg_tree, location);
8890 static tree panic_fndecl;
8891 tree call = Gogo::call_builtin(&panic_fndecl,
8896 TREE_TYPE(arg_tree),
8898 if (call == error_mark_node)
8899 return error_mark_node;
8900 // This function will throw an exception.
8901 TREE_NOTHROW(panic_fndecl) = 0;
8902 // This function will not return.
8903 TREE_THIS_VOLATILE(panic_fndecl) = 1;
8907 case BUILTIN_RECOVER:
8909 // The argument is set when building recover thunks. It's a
8910 // boolean value which is true if we can recover a value now.
8911 const Expression_list* args = this->args();
8912 go_assert(args != NULL && args->size() == 1);
8913 Expression* arg = args->front();
8914 tree arg_tree = arg->get_tree(context);
8915 if (arg_tree == error_mark_node)
8916 return error_mark_node;
8919 Type::make_interface_type(NULL, Linemap::predeclared_location());
8920 tree empty_tree = type_to_tree(empty->get_backend(context->gogo()));
8922 Type* nil_type = Type::make_nil_type();
8923 Expression* nil = Expression::make_nil(location);
8924 tree nil_tree = nil->get_tree(context);
8925 tree empty_nil_tree = Expression::convert_for_assignment(context,
8931 // We need to handle a deferred call to recover specially,
8932 // because it changes whether it can recover a panic or not.
8933 // See test7 in test/recover1.go.
8935 if (this->is_deferred())
8937 static tree deferred_recover_fndecl;
8938 call = Gogo::call_builtin(&deferred_recover_fndecl,
8940 "__go_deferred_recover",
8946 static tree recover_fndecl;
8947 call = Gogo::call_builtin(&recover_fndecl,
8953 if (call == error_mark_node)
8954 return error_mark_node;
8955 return fold_build3_loc(location.gcc_location(), COND_EXPR, empty_tree,
8956 arg_tree, call, empty_nil_tree);
8961 const Expression_list* args = this->args();
8962 go_assert(args != NULL && args->size() == 1);
8963 Expression* arg = args->front();
8964 tree arg_tree = arg->get_tree(context);
8965 if (arg_tree == error_mark_node)
8966 return error_mark_node;
8967 static tree close_fndecl;
8968 return Gogo::call_builtin(&close_fndecl,
8970 "__go_builtin_close",
8973 TREE_TYPE(arg_tree),
8977 case BUILTIN_SIZEOF:
8978 case BUILTIN_OFFSETOF:
8979 case BUILTIN_ALIGNOF:
8984 bool b = this->integer_constant_value(true, val, &dummy);
8987 go_assert(saw_errors());
8988 return error_mark_node;
8990 Type* int_type = Type::lookup_integer_type("int");
8991 tree type = type_to_tree(int_type->get_backend(gogo));
8992 tree ret = Expression::integer_constant_tree(val, type);
8999 const Expression_list* args = this->args();
9000 go_assert(args != NULL && args->size() == 2);
9001 Expression* arg1 = args->front();
9002 Expression* arg2 = args->back();
9004 tree arg1_tree = arg1->get_tree(context);
9005 tree arg2_tree = arg2->get_tree(context);
9006 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
9007 return error_mark_node;
9009 Type* arg1_type = arg1->type();
9010 Array_type* at = arg1_type->array_type();
9011 arg1_tree = save_expr(arg1_tree);
9012 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
9013 tree arg1_len = at->length_tree(gogo, arg1_tree);
9014 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
9015 return error_mark_node;
9017 Type* arg2_type = arg2->type();
9020 if (arg2_type->is_slice_type())
9022 at = arg2_type->array_type();
9023 arg2_tree = save_expr(arg2_tree);
9024 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
9025 arg2_len = at->length_tree(gogo, arg2_tree);
9029 arg2_tree = save_expr(arg2_tree);
9030 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
9031 arg2_len = String_type::length_tree(gogo, arg2_tree);
9033 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
9034 return error_mark_node;
9036 arg1_len = save_expr(arg1_len);
9037 arg2_len = save_expr(arg2_len);
9038 tree len = fold_build3_loc(location.gcc_location(), COND_EXPR,
9039 TREE_TYPE(arg1_len),
9040 fold_build2_loc(location.gcc_location(),
9041 LT_EXPR, boolean_type_node,
9042 arg1_len, arg2_len),
9043 arg1_len, arg2_len);
9044 len = save_expr(len);
9046 Type* element_type = at->element_type();
9047 Btype* element_btype = element_type->get_backend(gogo);
9048 tree element_type_tree = type_to_tree(element_btype);
9049 if (element_type_tree == error_mark_node)
9050 return error_mark_node;
9051 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9052 tree bytecount = fold_convert_loc(location.gcc_location(),
9053 TREE_TYPE(element_size), len);
9054 bytecount = fold_build2_loc(location.gcc_location(), MULT_EXPR,
9055 TREE_TYPE(element_size),
9056 bytecount, element_size);
9057 bytecount = fold_convert_loc(location.gcc_location(), size_type_node,
9060 arg1_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
9062 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
9065 static tree copy_fndecl;
9066 tree call = Gogo::call_builtin(©_fndecl,
9077 if (call == error_mark_node)
9078 return error_mark_node;
9080 return fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
9081 TREE_TYPE(len), call, len);
9084 case BUILTIN_APPEND:
9086 const Expression_list* args = this->args();
9087 go_assert(args != NULL && args->size() == 2);
9088 Expression* arg1 = args->front();
9089 Expression* arg2 = args->back();
9091 tree arg1_tree = arg1->get_tree(context);
9092 tree arg2_tree = arg2->get_tree(context);
9093 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
9094 return error_mark_node;
9096 Array_type* at = arg1->type()->array_type();
9097 Type* element_type = at->element_type()->forwarded();
9102 if (arg2->type()->is_string_type()
9103 && element_type->integer_type() != NULL
9104 && element_type->integer_type()->is_byte())
9106 arg2_tree = save_expr(arg2_tree);
9107 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
9108 arg2_len = String_type::length_tree(gogo, arg2_tree);
9109 element_size = size_int(1);
9113 arg2_tree = Expression::convert_for_assignment(context, at,
9117 if (arg2_tree == error_mark_node)
9118 return error_mark_node;
9120 arg2_tree = save_expr(arg2_tree);
9122 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
9123 arg2_len = at->length_tree(gogo, arg2_tree);
9125 Btype* element_btype = element_type->get_backend(gogo);
9126 tree element_type_tree = type_to_tree(element_btype);
9127 if (element_type_tree == error_mark_node)
9128 return error_mark_node;
9129 element_size = TYPE_SIZE_UNIT(element_type_tree);
9132 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
9134 arg2_len = fold_convert_loc(location.gcc_location(), size_type_node,
9136 element_size = fold_convert_loc(location.gcc_location(), size_type_node,
9139 if (arg2_val == error_mark_node
9140 || arg2_len == error_mark_node
9141 || element_size == error_mark_node)
9142 return error_mark_node;
9144 // We rebuild the decl each time since the slice types may
9146 tree append_fndecl = NULL_TREE;
9147 return Gogo::call_builtin(&append_fndecl,
9151 TREE_TYPE(arg1_tree),
9152 TREE_TYPE(arg1_tree),
9165 const Expression_list* args = this->args();
9166 go_assert(args != NULL && args->size() == 1);
9167 Expression* arg = args->front();
9168 tree arg_tree = arg->get_tree(context);
9169 if (arg_tree == error_mark_node)
9170 return error_mark_node;
9171 go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
9172 if (this->code_ == BUILTIN_REAL)
9173 return fold_build1_loc(location.gcc_location(), REALPART_EXPR,
9174 TREE_TYPE(TREE_TYPE(arg_tree)),
9177 return fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
9178 TREE_TYPE(TREE_TYPE(arg_tree)),
9182 case BUILTIN_COMPLEX:
9184 const Expression_list* args = this->args();
9185 go_assert(args != NULL && args->size() == 2);
9186 tree r = args->front()->get_tree(context);
9187 tree i = args->back()->get_tree(context);
9188 if (r == error_mark_node || i == error_mark_node)
9189 return error_mark_node;
9190 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
9191 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
9192 go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
9193 return fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
9194 build_complex_type(TREE_TYPE(r)),
9203 // We have to support exporting a builtin call expression, because
9204 // code can set a constant to the result of a builtin expression.
9207 Builtin_call_expression::do_export(Export* exp) const
9214 if (this->integer_constant_value(true, val, &dummy))
9216 Integer_expression::export_integer(exp, val);
9225 if (this->float_constant_value(fval, &dummy))
9227 Float_expression::export_float(exp, fval);
9239 if (this->complex_constant_value(real, imag, &dummy))
9241 Complex_expression::export_complex(exp, real, imag);
9250 error_at(this->location(), "value is not constant");
9254 // A trailing space lets us reliably identify the end of the number.
9255 exp->write_c_string(" ");
9258 // Class Call_expression.
9263 Call_expression::do_traverse(Traverse* traverse)
9265 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
9266 return TRAVERSE_EXIT;
9267 if (this->args_ != NULL)
9269 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
9270 return TRAVERSE_EXIT;
9272 return TRAVERSE_CONTINUE;
9275 // Lower a call statement.
9278 Call_expression::do_lower(Gogo* gogo, Named_object* function,
9279 Statement_inserter* inserter, int)
9281 Location loc = this->location();
9283 // A type cast can look like a function call.
9284 if (this->fn_->is_type_expression()
9285 && this->args_ != NULL
9286 && this->args_->size() == 1)
9287 return Expression::make_cast(this->fn_->type(), this->args_->front(),
9290 // Recognize a call to a builtin function.
9291 Func_expression* fne = this->fn_->func_expression();
9293 && fne->named_object()->is_function_declaration()
9294 && fne->named_object()->func_declaration_value()->type()->is_builtin())
9295 return new Builtin_call_expression(gogo, this->fn_, this->args_,
9296 this->is_varargs_, loc);
9298 // Handle an argument which is a call to a function which returns
9299 // multiple results.
9300 if (this->args_ != NULL
9301 && this->args_->size() == 1
9302 && this->args_->front()->call_expression() != NULL
9303 && this->fn_->type()->function_type() != NULL)
9305 Function_type* fntype = this->fn_->type()->function_type();
9306 size_t rc = this->args_->front()->call_expression()->result_count();
9308 && fntype->parameters() != NULL
9309 && (fntype->parameters()->size() == rc
9310 || (fntype->is_varargs()
9311 && fntype->parameters()->size() - 1 <= rc)))
9313 Call_expression* call = this->args_->front()->call_expression();
9314 Expression_list* args = new Expression_list;
9315 for (size_t i = 0; i < rc; ++i)
9316 args->push_back(Expression::make_call_result(call, i));
9317 // We can't return a new call expression here, because this
9318 // one may be referenced by Call_result expressions. We
9319 // also can't delete the old arguments, because we may still
9320 // traverse them somewhere up the call stack. FIXME.
9325 // If this call returns multiple results, create a temporary
9326 // variable for each result.
9327 size_t rc = this->result_count();
9328 if (rc > 1 && this->results_ == NULL)
9330 std::vector<Temporary_statement*>* temps =
9331 new std::vector<Temporary_statement*>;
9333 const Typed_identifier_list* results =
9334 this->fn_->type()->function_type()->results();
9335 for (Typed_identifier_list::const_iterator p = results->begin();
9336 p != results->end();
9339 Temporary_statement* temp = Statement::make_temporary(p->type(),
9341 inserter->insert(temp);
9342 temps->push_back(temp);
9344 this->results_ = temps;
9347 // Handle a call to a varargs function by packaging up the extra
9349 if (this->fn_->type()->function_type() != NULL
9350 && this->fn_->type()->function_type()->is_varargs())
9352 Function_type* fntype = this->fn_->type()->function_type();
9353 const Typed_identifier_list* parameters = fntype->parameters();
9354 go_assert(parameters != NULL && !parameters->empty());
9355 Type* varargs_type = parameters->back().type();
9356 this->lower_varargs(gogo, function, inserter, varargs_type,
9357 parameters->size());
9360 // If this is call to a method, call the method directly passing the
9361 // object as the first parameter.
9362 Bound_method_expression* bme = this->fn_->bound_method_expression();
9365 Named_object* method = bme->method();
9366 Expression* first_arg = bme->first_argument();
9368 // We always pass a pointer when calling a method.
9369 if (first_arg->type()->points_to() == NULL
9370 && !first_arg->type()->is_error())
9372 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc);
9373 // We may need to create a temporary variable so that we can
9374 // take the address. We can't do that here because it will
9375 // mess up the order of evaluation.
9376 Unary_expression* ue = static_cast<Unary_expression*>(first_arg);
9377 ue->set_create_temp();
9380 // If we are calling a method which was inherited from an
9381 // embedded struct, and the method did not get a stub, then the
9382 // first type may be wrong.
9383 Type* fatype = bme->first_argument_type();
9386 if (fatype->points_to() == NULL)
9387 fatype = Type::make_pointer_type(fatype);
9388 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc);
9391 Expression_list* new_args = new Expression_list();
9392 new_args->push_back(first_arg);
9393 if (this->args_ != NULL)
9395 for (Expression_list::const_iterator p = this->args_->begin();
9396 p != this->args_->end();
9398 new_args->push_back(*p);
9401 // We have to change in place because this structure may be
9402 // referenced by Call_result_expressions. We can't delete the
9403 // old arguments, because we may be traversing them up in some
9405 this->args_ = new_args;
9406 this->fn_ = Expression::make_func_reference(method, NULL,
9413 // Lower a call to a varargs function. FUNCTION is the function in
9414 // which the call occurs--it's not the function we are calling.
9415 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
9416 // PARAM_COUNT is the number of parameters of the function we are
9417 // calling; the last of these parameters will be the varargs
9421 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
9422 Statement_inserter* inserter,
9423 Type* varargs_type, size_t param_count)
9425 if (this->varargs_are_lowered_)
9428 Location loc = this->location();
9430 go_assert(param_count > 0);
9431 go_assert(varargs_type->is_slice_type());
9433 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
9434 if (arg_count < param_count - 1)
9436 // Not enough arguments; will be caught in check_types.
9440 Expression_list* old_args = this->args_;
9441 Expression_list* new_args = new Expression_list();
9442 bool push_empty_arg = false;
9443 if (old_args == NULL || old_args->empty())
9445 go_assert(param_count == 1);
9446 push_empty_arg = true;
9450 Expression_list::const_iterator pa;
9452 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
9454 if (static_cast<size_t>(i) == param_count)
9456 new_args->push_back(*pa);
9459 // We have reached the varargs parameter.
9461 bool issued_error = false;
9462 if (pa == old_args->end())
9463 push_empty_arg = true;
9464 else if (pa + 1 == old_args->end() && this->is_varargs_)
9465 new_args->push_back(*pa);
9466 else if (this->is_varargs_)
9468 this->report_error(_("too many arguments"));
9473 Type* element_type = varargs_type->array_type()->element_type();
9474 Expression_list* vals = new Expression_list;
9475 for (; pa != old_args->end(); ++pa, ++i)
9477 // Check types here so that we get a better message.
9478 Type* patype = (*pa)->type();
9479 Location paloc = (*pa)->location();
9480 if (!this->check_argument_type(i, element_type, patype,
9481 paloc, issued_error))
9483 vals->push_back(*pa);
9486 Expression::make_slice_composite_literal(varargs_type, vals, loc);
9487 gogo->lower_expression(function, inserter, &val);
9488 new_args->push_back(val);
9493 new_args->push_back(Expression::make_nil(loc));
9495 // We can't return a new call expression here, because this one may
9496 // be referenced by Call_result expressions. FIXME. We can't
9497 // delete OLD_ARGS because we may have both a Call_expression and a
9498 // Builtin_call_expression which refer to them. FIXME.
9499 this->args_ = new_args;
9500 this->varargs_are_lowered_ = true;
9503 // Get the function type. This can return NULL in error cases.
9506 Call_expression::get_function_type() const
9508 return this->fn_->type()->function_type();
9511 // Return the number of values which this call will return.
9514 Call_expression::result_count() const
9516 const Function_type* fntype = this->get_function_type();
9519 if (fntype->results() == NULL)
9521 return fntype->results()->size();
9524 // Return the temporary which holds a result.
9526 Temporary_statement*
9527 Call_expression::result(size_t i) const
9529 go_assert(this->results_ != NULL
9530 && this->results_->size() > i);
9531 return (*this->results_)[i];
9534 // Return whether this is a call to the predeclared function recover.
9537 Call_expression::is_recover_call() const
9539 return this->do_is_recover_call();
9542 // Set the argument to the recover function.
9545 Call_expression::set_recover_arg(Expression* arg)
9547 this->do_set_recover_arg(arg);
9550 // Virtual functions also implemented by Builtin_call_expression.
9553 Call_expression::do_is_recover_call() const
9559 Call_expression::do_set_recover_arg(Expression*)
9564 // We have found an error with this call expression; return true if
9565 // we should report it.
9568 Call_expression::issue_error()
9570 if (this->issued_error_)
9574 this->issued_error_ = true;
9582 Call_expression::do_type()
9584 if (this->type_ != NULL)
9588 Function_type* fntype = this->get_function_type();
9590 return Type::make_error_type();
9592 const Typed_identifier_list* results = fntype->results();
9593 if (results == NULL)
9594 ret = Type::make_void_type();
9595 else if (results->size() == 1)
9596 ret = results->begin()->type();
9598 ret = Type::make_call_multiple_result_type(this);
9605 // Determine types for a call expression. We can use the function
9606 // parameter types to set the types of the arguments.
9609 Call_expression::do_determine_type(const Type_context*)
9611 if (!this->determining_types())
9614 this->fn_->determine_type_no_context();
9615 Function_type* fntype = this->get_function_type();
9616 const Typed_identifier_list* parameters = NULL;
9618 parameters = fntype->parameters();
9619 if (this->args_ != NULL)
9621 Typed_identifier_list::const_iterator pt;
9622 if (parameters != NULL)
9623 pt = parameters->begin();
9625 for (Expression_list::const_iterator pa = this->args_->begin();
9626 pa != this->args_->end();
9632 // If this is a method, the first argument is the
9634 if (fntype != NULL && fntype->is_method())
9636 Type* rtype = fntype->receiver()->type();
9637 // The receiver is always passed as a pointer.
9638 if (rtype->points_to() == NULL)
9639 rtype = Type::make_pointer_type(rtype);
9640 Type_context subcontext(rtype, false);
9641 (*pa)->determine_type(&subcontext);
9646 if (parameters != NULL && pt != parameters->end())
9648 Type_context subcontext(pt->type(), false);
9649 (*pa)->determine_type(&subcontext);
9653 (*pa)->determine_type_no_context();
9658 // Called when determining types for a Call_expression. Return true
9659 // if we should go ahead, false if they have already been determined.
9662 Call_expression::determining_types()
9664 if (this->types_are_determined_)
9668 this->types_are_determined_ = true;
9673 // Check types for parameter I.
9676 Call_expression::check_argument_type(int i, const Type* parameter_type,
9677 const Type* argument_type,
9678 Location argument_location,
9683 if (this->are_hidden_fields_ok_)
9684 ok = Type::are_assignable_hidden_ok(parameter_type, argument_type,
9687 ok = Type::are_assignable(parameter_type, argument_type, &reason);
9693 error_at(argument_location, "argument %d has incompatible type", i);
9695 error_at(argument_location,
9696 "argument %d has incompatible type (%s)",
9699 this->set_is_error();
9708 Call_expression::do_check_types(Gogo*)
9710 Function_type* fntype = this->get_function_type();
9713 if (!this->fn_->type()->is_error())
9714 this->report_error(_("expected function"));
9718 bool is_method = fntype->is_method();
9721 go_assert(this->args_ != NULL && !this->args_->empty());
9722 Type* rtype = fntype->receiver()->type();
9723 Expression* first_arg = this->args_->front();
9724 // The language permits copying hidden fields for a method
9725 // receiver. We dereference the values since receivers are
9726 // always passed as pointers.
9728 if (!Type::are_assignable_hidden_ok(rtype->deref(),
9729 first_arg->type()->deref(),
9733 this->report_error(_("incompatible type for receiver"));
9736 error_at(this->location(),
9737 "incompatible type for receiver (%s)",
9739 this->set_is_error();
9744 // Note that varargs was handled by the lower_varargs() method, so
9745 // we don't have to worry about it here.
9747 const Typed_identifier_list* parameters = fntype->parameters();
9748 if (this->args_ == NULL)
9750 if (parameters != NULL && !parameters->empty())
9751 this->report_error(_("not enough arguments"));
9753 else if (parameters == NULL)
9755 if (!is_method || this->args_->size() > 1)
9756 this->report_error(_("too many arguments"));
9761 Expression_list::const_iterator pa = this->args_->begin();
9764 for (Typed_identifier_list::const_iterator pt = parameters->begin();
9765 pt != parameters->end();
9768 if (pa == this->args_->end())
9770 this->report_error(_("not enough arguments"));
9773 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
9774 (*pa)->location(), false);
9776 if (pa != this->args_->end())
9777 this->report_error(_("too many arguments"));
9781 // Return whether we have to use a temporary variable to ensure that
9782 // we evaluate this call expression in order. If the call returns no
9783 // results then it will inevitably be executed last.
9786 Call_expression::do_must_eval_in_order() const
9788 return this->result_count() > 0;
9791 // Get the function and the first argument to use when calling an
9792 // interface method.
9795 Call_expression::interface_method_function(
9796 Translate_context* context,
9797 Interface_field_reference_expression* interface_method,
9798 tree* first_arg_ptr)
9800 tree expr = interface_method->expr()->get_tree(context);
9801 if (expr == error_mark_node)
9802 return error_mark_node;
9803 expr = save_expr(expr);
9804 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
9805 if (first_arg == error_mark_node)
9806 return error_mark_node;
9807 *first_arg_ptr = first_arg;
9808 return interface_method->get_function_tree(context, expr);
9811 // Build the call expression.
9814 Call_expression::do_get_tree(Translate_context* context)
9816 if (this->tree_ != NULL_TREE)
9819 Function_type* fntype = this->get_function_type();
9821 return error_mark_node;
9823 if (this->fn_->is_error_expression())
9824 return error_mark_node;
9826 Gogo* gogo = context->gogo();
9827 Location location = this->location();
9829 Func_expression* func = this->fn_->func_expression();
9830 Interface_field_reference_expression* interface_method =
9831 this->fn_->interface_field_reference_expression();
9832 const bool has_closure = func != NULL && func->closure() != NULL;
9833 const bool is_interface_method = interface_method != NULL;
9837 if (this->args_ == NULL || this->args_->empty())
9839 nargs = is_interface_method ? 1 : 0;
9840 args = nargs == 0 ? NULL : new tree[nargs];
9842 else if (fntype->parameters() == NULL || fntype->parameters()->empty())
9844 // Passing a receiver parameter.
9845 go_assert(!is_interface_method
9846 && fntype->is_method()
9847 && this->args_->size() == 1);
9849 args = new tree[nargs];
9850 args[0] = this->args_->front()->get_tree(context);
9854 const Typed_identifier_list* params = fntype->parameters();
9856 nargs = this->args_->size();
9857 int i = is_interface_method ? 1 : 0;
9859 args = new tree[nargs];
9861 Typed_identifier_list::const_iterator pp = params->begin();
9862 Expression_list::const_iterator pe = this->args_->begin();
9863 if (!is_interface_method && fntype->is_method())
9865 args[i] = (*pe)->get_tree(context);
9869 for (; pe != this->args_->end(); ++pe, ++pp, ++i)
9871 go_assert(pp != params->end());
9872 tree arg_val = (*pe)->get_tree(context);
9873 args[i] = Expression::convert_for_assignment(context,
9878 if (args[i] == error_mark_node)
9881 return error_mark_node;
9884 go_assert(pp == params->end());
9885 go_assert(i == nargs);
9888 tree rettype = TREE_TYPE(TREE_TYPE(type_to_tree(fntype->get_backend(gogo))));
9889 if (rettype == error_mark_node)
9892 return error_mark_node;
9897 fn = func->get_tree_without_closure(gogo);
9898 else if (!is_interface_method)
9899 fn = this->fn_->get_tree(context);
9901 fn = this->interface_method_function(context, interface_method, &args[0]);
9903 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
9906 return error_mark_node;
9910 if (TREE_CODE(fndecl) == ADDR_EXPR)
9911 fndecl = TREE_OPERAND(fndecl, 0);
9913 // Add a type cast in case the type of the function is a recursive
9914 // type which refers to itself.
9915 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
9917 tree fnt = type_to_tree(fntype->get_backend(gogo));
9918 if (fnt == error_mark_node)
9919 return error_mark_node;
9920 fn = fold_convert_loc(location.gcc_location(), fnt, fn);
9923 // This is to support builtin math functions when using 80387 math.
9924 tree excess_type = NULL_TREE;
9925 if (TREE_CODE(fndecl) == FUNCTION_DECL
9926 && DECL_IS_BUILTIN(fndecl)
9927 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
9929 && ((SCALAR_FLOAT_TYPE_P(rettype)
9930 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
9931 || (COMPLEX_FLOAT_TYPE_P(rettype)
9932 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
9934 excess_type = excess_precision_type(TREE_TYPE(args[0]));
9935 if (excess_type != NULL_TREE)
9937 tree excess_fndecl = mathfn_built_in(excess_type,
9938 DECL_FUNCTION_CODE(fndecl));
9939 if (excess_fndecl == NULL_TREE)
9940 excess_type = NULL_TREE;
9943 fn = build_fold_addr_expr_loc(location.gcc_location(),
9945 for (int i = 0; i < nargs; ++i)
9946 args[i] = ::convert(excess_type, args[i]);
9951 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
9955 SET_EXPR_LOCATION(ret, location.gcc_location());
9959 tree closure_tree = func->closure()->get_tree(context);
9960 if (closure_tree != error_mark_node)
9961 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
9964 // If this is a recursive function type which returns itself, as in
9966 // we have used ptr_type_node for the return type. Add a cast here
9967 // to the correct type.
9968 if (TREE_TYPE(ret) == ptr_type_node)
9970 tree t = type_to_tree(this->type()->base()->get_backend(gogo));
9971 ret = fold_convert_loc(location.gcc_location(), t, ret);
9974 if (excess_type != NULL_TREE)
9976 // Calling convert here can undo our excess precision change.
9977 // That may or may not be a bug in convert_to_real.
9978 ret = build1(NOP_EXPR, rettype, ret);
9981 if (this->results_ != NULL)
9982 ret = this->set_results(context, ret);
9989 // Set the result variables if this call returns multiple results.
9992 Call_expression::set_results(Translate_context* context, tree call_tree)
9994 tree stmt_list = NULL_TREE;
9996 call_tree = save_expr(call_tree);
9998 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
10000 go_assert(saw_errors());
10004 Location loc = this->location();
10005 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
10006 size_t rc = this->result_count();
10007 for (size_t i = 0; i < rc; ++i, field = DECL_CHAIN(field))
10009 go_assert(field != NULL_TREE);
10011 Temporary_statement* temp = this->result(i);
10012 Temporary_reference_expression* ref =
10013 Expression::make_temporary_reference(temp, loc);
10014 ref->set_is_lvalue();
10015 tree temp_tree = ref->get_tree(context);
10016 if (temp_tree == error_mark_node)
10019 tree val_tree = build3_loc(loc.gcc_location(), COMPONENT_REF,
10020 TREE_TYPE(field), call_tree, field, NULL_TREE);
10021 tree set_tree = build2_loc(loc.gcc_location(), MODIFY_EXPR,
10022 void_type_node, temp_tree, val_tree);
10024 append_to_statement_list(set_tree, &stmt_list);
10026 go_assert(field == NULL_TREE);
10028 return save_expr(stmt_list);
10031 // Dump ast representation for a call expressin.
10034 Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
10036 this->fn_->dump_expression(ast_dump_context);
10037 ast_dump_context->ostream() << "(";
10039 ast_dump_context->dump_expression_list(this->args_);
10041 ast_dump_context->ostream() << ") ";
10044 // Make a call expression.
10047 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
10050 return new Call_expression(fn, args, is_varargs, location);
10053 // A single result from a call which returns multiple results.
10055 class Call_result_expression : public Expression
10058 Call_result_expression(Call_expression* call, unsigned int index)
10059 : Expression(EXPRESSION_CALL_RESULT, call->location()),
10060 call_(call), index_(index)
10065 do_traverse(Traverse*);
10071 do_determine_type(const Type_context*);
10074 do_check_types(Gogo*);
10079 return new Call_result_expression(this->call_->call_expression(),
10084 do_must_eval_in_order() const
10088 do_get_tree(Translate_context*);
10091 do_dump_expression(Ast_dump_context*) const;
10094 // The underlying call expression.
10096 // Which result we want.
10097 unsigned int index_;
10100 // Traverse a call result.
10103 Call_result_expression::do_traverse(Traverse* traverse)
10105 if (traverse->remember_expression(this->call_))
10107 // We have already traversed the call expression.
10108 return TRAVERSE_CONTINUE;
10110 return Expression::traverse(&this->call_, traverse);
10116 Call_result_expression::do_type()
10118 if (this->classification() == EXPRESSION_ERROR)
10119 return Type::make_error_type();
10121 // THIS->CALL_ can be replaced with a temporary reference due to
10122 // Call_expression::do_must_eval_in_order when there is an error.
10123 Call_expression* ce = this->call_->call_expression();
10126 this->set_is_error();
10127 return Type::make_error_type();
10129 Function_type* fntype = ce->get_function_type();
10130 if (fntype == NULL)
10132 if (ce->issue_error())
10134 if (!ce->fn()->type()->is_error())
10135 this->report_error(_("expected function"));
10137 this->set_is_error();
10138 return Type::make_error_type();
10140 const Typed_identifier_list* results = fntype->results();
10141 if (results == NULL || results->size() < 2)
10143 if (ce->issue_error())
10144 this->report_error(_("number of results does not match "
10145 "number of values"));
10146 return Type::make_error_type();
10148 Typed_identifier_list::const_iterator pr = results->begin();
10149 for (unsigned int i = 0; i < this->index_; ++i)
10151 if (pr == results->end())
10155 if (pr == results->end())
10157 if (ce->issue_error())
10158 this->report_error(_("number of results does not match "
10159 "number of values"));
10160 return Type::make_error_type();
10165 // Check the type. Just make sure that we trigger the warning in
10169 Call_result_expression::do_check_types(Gogo*)
10174 // Determine the type. We have nothing to do here, but the 0 result
10175 // needs to pass down to the caller.
10178 Call_result_expression::do_determine_type(const Type_context*)
10180 this->call_->determine_type_no_context();
10183 // Return the tree. We just refer to the temporary set by the call
10184 // expression. We don't do this at lowering time because it makes it
10185 // hard to evaluate the call at the right time.
10188 Call_result_expression::do_get_tree(Translate_context* context)
10190 Call_expression* ce = this->call_->call_expression();
10191 go_assert(ce != NULL);
10192 Temporary_statement* ts = ce->result(this->index_);
10193 Expression* ref = Expression::make_temporary_reference(ts, this->location());
10194 return ref->get_tree(context);
10197 // Dump ast representation for a call result expression.
10200 Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10203 // FIXME: Wouldn't it be better if the call is assigned to a temporary
10204 // (struct) and the fields are referenced instead.
10205 ast_dump_context->ostream() << this->index_ << "@(";
10206 ast_dump_context->dump_expression(this->call_);
10207 ast_dump_context->ostream() << ")";
10210 // Make a reference to a single result of a call which returns
10211 // multiple results.
10214 Expression::make_call_result(Call_expression* call, unsigned int index)
10216 return new Call_result_expression(call, index);
10219 // Class Index_expression.
10224 Index_expression::do_traverse(Traverse* traverse)
10226 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
10227 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
10228 || (this->end_ != NULL
10229 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
10230 return TRAVERSE_EXIT;
10231 return TRAVERSE_CONTINUE;
10234 // Lower an index expression. This converts the generic index
10235 // expression into an array index, a string index, or a map index.
10238 Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
10240 Location location = this->location();
10241 Expression* left = this->left_;
10242 Expression* start = this->start_;
10243 Expression* end = this->end_;
10245 Type* type = left->type();
10246 if (type->is_error())
10247 return Expression::make_error(location);
10248 else if (left->is_type_expression())
10250 error_at(location, "attempt to index type expression");
10251 return Expression::make_error(location);
10253 else if (type->array_type() != NULL)
10254 return Expression::make_array_index(left, start, end, location);
10255 else if (type->points_to() != NULL
10256 && type->points_to()->array_type() != NULL
10257 && !type->points_to()->is_slice_type())
10259 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
10261 return Expression::make_array_index(deref, start, end, location);
10263 else if (type->is_string_type())
10264 return Expression::make_string_index(left, start, end, location);
10265 else if (type->map_type() != NULL)
10269 error_at(location, "invalid slice of map");
10270 return Expression::make_error(location);
10272 Map_index_expression* ret = Expression::make_map_index(left, start,
10274 if (this->is_lvalue_)
10275 ret->set_is_lvalue();
10281 "attempt to index object which is not array, string, or map");
10282 return Expression::make_error(location);
10286 // Write an indexed expression (expr[expr:expr] or expr[expr]) to a
10290 Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context,
10291 const Expression* expr,
10292 const Expression* start,
10293 const Expression* end)
10295 expr->dump_expression(ast_dump_context);
10296 ast_dump_context->ostream() << "[";
10297 start->dump_expression(ast_dump_context);
10300 ast_dump_context->ostream() << ":";
10301 end->dump_expression(ast_dump_context);
10303 ast_dump_context->ostream() << "]";
10306 // Dump ast representation for an index expression.
10309 Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10312 Index_expression::dump_index_expression(ast_dump_context, this->left_,
10313 this->start_, this->end_);
10316 // Make an index expression.
10319 Expression::make_index(Expression* left, Expression* start, Expression* end,
10322 return new Index_expression(left, start, end, location);
10325 // An array index. This is used for both indexing and slicing.
10327 class Array_index_expression : public Expression
10330 Array_index_expression(Expression* array, Expression* start,
10331 Expression* end, Location location)
10332 : Expression(EXPRESSION_ARRAY_INDEX, location),
10333 array_(array), start_(start), end_(end), type_(NULL)
10338 do_traverse(Traverse*);
10344 do_determine_type(const Type_context*);
10347 do_check_types(Gogo*);
10352 return Expression::make_array_index(this->array_->copy(),
10353 this->start_->copy(),
10354 (this->end_ == NULL
10356 : this->end_->copy()),
10361 do_must_eval_subexpressions_in_order(int* skip) const
10368 do_is_addressable() const;
10371 do_address_taken(bool escapes)
10372 { this->array_->address_taken(escapes); }
10375 do_get_tree(Translate_context*);
10378 do_dump_expression(Ast_dump_context*) const;
10381 // The array we are getting a value from.
10382 Expression* array_;
10383 // The start or only index.
10384 Expression* start_;
10385 // The end index of a slice. This may be NULL for a simple array
10386 // index, or it may be a nil expression for the length of the array.
10388 // The type of the expression.
10392 // Array index traversal.
10395 Array_index_expression::do_traverse(Traverse* traverse)
10397 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
10398 return TRAVERSE_EXIT;
10399 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10400 return TRAVERSE_EXIT;
10401 if (this->end_ != NULL)
10403 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10404 return TRAVERSE_EXIT;
10406 return TRAVERSE_CONTINUE;
10409 // Return the type of an array index.
10412 Array_index_expression::do_type()
10414 if (this->type_ == NULL)
10416 Array_type* type = this->array_->type()->array_type();
10418 this->type_ = Type::make_error_type();
10419 else if (this->end_ == NULL)
10420 this->type_ = type->element_type();
10421 else if (type->is_slice_type())
10423 // A slice of a slice has the same type as the original
10425 this->type_ = this->array_->type()->deref();
10429 // A slice of an array is a slice.
10430 this->type_ = Type::make_array_type(type->element_type(), NULL);
10433 return this->type_;
10436 // Set the type of an array index.
10439 Array_index_expression::do_determine_type(const Type_context*)
10441 this->array_->determine_type_no_context();
10442 this->start_->determine_type_no_context();
10443 if (this->end_ != NULL)
10444 this->end_->determine_type_no_context();
10447 // Check types of an array index.
10450 Array_index_expression::do_check_types(Gogo*)
10452 if (this->start_->type()->integer_type() == NULL)
10453 this->report_error(_("index must be integer"));
10454 if (this->end_ != NULL
10455 && this->end_->type()->integer_type() == NULL
10456 && !this->end_->type()->is_error()
10457 && !this->end_->is_nil_expression()
10458 && !this->end_->is_error_expression())
10459 this->report_error(_("slice end must be integer"));
10461 Array_type* array_type = this->array_->type()->array_type();
10462 if (array_type == NULL)
10464 go_assert(this->array_->type()->is_error());
10468 unsigned int int_bits =
10469 Type::lookup_integer_type("int")->integer_type()->bits();
10474 bool lval_valid = (array_type->length() != NULL
10475 && array_type->length()->integer_constant_value(true,
10480 if (this->start_->integer_constant_value(true, ival, &dummy))
10482 if (mpz_sgn(ival) < 0
10483 || mpz_sizeinbase(ival, 2) >= int_bits
10485 && (this->end_ == NULL
10486 ? mpz_cmp(ival, lval) >= 0
10487 : mpz_cmp(ival, lval) > 0)))
10489 error_at(this->start_->location(), "array index out of bounds");
10490 this->set_is_error();
10493 if (this->end_ != NULL && !this->end_->is_nil_expression())
10495 if (this->end_->integer_constant_value(true, ival, &dummy))
10497 if (mpz_sgn(ival) < 0
10498 || mpz_sizeinbase(ival, 2) >= int_bits
10499 || (lval_valid && mpz_cmp(ival, lval) > 0))
10501 error_at(this->end_->location(), "array index out of bounds");
10502 this->set_is_error();
10509 // A slice of an array requires an addressable array. A slice of a
10510 // slice is always possible.
10511 if (this->end_ != NULL && !array_type->is_slice_type())
10513 if (!this->array_->is_addressable())
10514 this->report_error(_("array is not addressable"));
10516 this->array_->address_taken(true);
10520 // Return whether this expression is addressable.
10523 Array_index_expression::do_is_addressable() const
10525 // A slice expression is not addressable.
10526 if (this->end_ != NULL)
10529 // An index into a slice is addressable.
10530 if (this->array_->type()->is_slice_type())
10533 // An index into an array is addressable if the array is
10535 return this->array_->is_addressable();
10538 // Get a tree for an array index.
10541 Array_index_expression::do_get_tree(Translate_context* context)
10543 Gogo* gogo = context->gogo();
10544 Location loc = this->location();
10546 Array_type* array_type = this->array_->type()->array_type();
10547 if (array_type == NULL)
10549 go_assert(this->array_->type()->is_error());
10550 return error_mark_node;
10553 tree type_tree = type_to_tree(array_type->get_backend(gogo));
10554 if (type_tree == error_mark_node)
10555 return error_mark_node;
10557 tree array_tree = this->array_->get_tree(context);
10558 if (array_tree == error_mark_node)
10559 return error_mark_node;
10561 if (array_type->length() == NULL && !DECL_P(array_tree))
10562 array_tree = save_expr(array_tree);
10563 tree length_tree = array_type->length_tree(gogo, array_tree);
10564 if (length_tree == error_mark_node)
10565 return error_mark_node;
10566 length_tree = save_expr(length_tree);
10567 tree length_type = TREE_TYPE(length_tree);
10569 tree bad_index = boolean_false_node;
10571 tree start_tree = this->start_->get_tree(context);
10572 if (start_tree == error_mark_node)
10573 return error_mark_node;
10574 if (!DECL_P(start_tree))
10575 start_tree = save_expr(start_tree);
10576 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
10577 start_tree = convert_to_integer(length_type, start_tree);
10579 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
10582 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
10583 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10584 boolean_type_node, bad_index,
10585 fold_build2_loc(loc.gcc_location(),
10586 (this->end_ == NULL
10589 boolean_type_node, start_tree,
10592 int code = (array_type->length() != NULL
10593 ? (this->end_ == NULL
10594 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
10595 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
10596 : (this->end_ == NULL
10597 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
10598 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
10599 tree crash = Gogo::runtime_error(code, loc);
10601 if (this->end_ == NULL)
10603 // Simple array indexing. This has to return an l-value, so
10604 // wrap the index check into START_TREE.
10605 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
10606 build3(COND_EXPR, void_type_node,
10607 bad_index, crash, NULL_TREE),
10609 start_tree = fold_convert_loc(loc.gcc_location(), sizetype, start_tree);
10611 if (array_type->length() != NULL)
10614 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
10615 start_tree, NULL_TREE, NULL_TREE);
10620 tree values = array_type->value_pointer_tree(gogo, array_tree);
10621 Type* element_type = array_type->element_type();
10622 Btype* belement_type = element_type->get_backend(gogo);
10623 tree element_type_tree = type_to_tree(belement_type);
10624 if (element_type_tree == error_mark_node)
10625 return error_mark_node;
10626 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
10627 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
10628 start_tree, element_size);
10629 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
10630 TREE_TYPE(values), values, offset);
10631 return build_fold_indirect_ref(ptr);
10637 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
10638 if (capacity_tree == error_mark_node)
10639 return error_mark_node;
10640 capacity_tree = fold_convert_loc(loc.gcc_location(), length_type,
10644 if (this->end_->is_nil_expression())
10645 end_tree = length_tree;
10648 end_tree = this->end_->get_tree(context);
10649 if (end_tree == error_mark_node)
10650 return error_mark_node;
10651 if (!DECL_P(end_tree))
10652 end_tree = save_expr(end_tree);
10653 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
10654 end_tree = convert_to_integer(length_type, end_tree);
10656 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
10659 end_tree = fold_convert_loc(loc.gcc_location(), length_type, end_tree);
10661 capacity_tree = save_expr(capacity_tree);
10662 tree bad_end = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10664 fold_build2_loc(loc.gcc_location(),
10665 LT_EXPR, boolean_type_node,
10666 end_tree, start_tree),
10667 fold_build2_loc(loc.gcc_location(),
10668 GT_EXPR, boolean_type_node,
10669 end_tree, capacity_tree));
10670 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10671 boolean_type_node, bad_index, bad_end);
10674 Type* element_type = array_type->element_type();
10675 tree element_type_tree = type_to_tree(element_type->get_backend(gogo));
10676 if (element_type_tree == error_mark_node)
10677 return error_mark_node;
10678 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
10680 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
10681 fold_convert_loc(loc.gcc_location(), sizetype,
10685 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
10686 if (value_pointer == error_mark_node)
10687 return error_mark_node;
10689 value_pointer = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
10690 TREE_TYPE(value_pointer),
10691 value_pointer, offset);
10693 tree result_length_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
10694 length_type, end_tree, start_tree);
10696 tree result_capacity_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
10697 length_type, capacity_tree,
10700 tree struct_tree = type_to_tree(this->type()->get_backend(gogo));
10701 go_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
10703 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
10705 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
10706 tree field = TYPE_FIELDS(struct_tree);
10707 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
10708 elt->index = field;
10709 elt->value = value_pointer;
10711 elt = VEC_quick_push(constructor_elt, init, NULL);
10712 field = DECL_CHAIN(field);
10713 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
10714 elt->index = field;
10715 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
10716 result_length_tree);
10718 elt = VEC_quick_push(constructor_elt, init, NULL);
10719 field = DECL_CHAIN(field);
10720 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
10721 elt->index = field;
10722 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
10723 result_capacity_tree);
10725 tree constructor = build_constructor(struct_tree, init);
10727 if (TREE_CONSTANT(value_pointer)
10728 && TREE_CONSTANT(result_length_tree)
10729 && TREE_CONSTANT(result_capacity_tree))
10730 TREE_CONSTANT(constructor) = 1;
10732 return fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
10733 TREE_TYPE(constructor),
10734 build3(COND_EXPR, void_type_node,
10735 bad_index, crash, NULL_TREE),
10739 // Dump ast representation for an array index expression.
10742 Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10745 Index_expression::dump_index_expression(ast_dump_context, this->array_,
10746 this->start_, this->end_);
10749 // Make an array index expression. END may be NULL.
10752 Expression::make_array_index(Expression* array, Expression* start,
10753 Expression* end, Location location)
10755 // Taking a slice of a composite literal requires moving the literal
10757 if (end != NULL && array->is_composite_literal())
10759 array = Expression::make_heap_composite(array, location);
10760 array = Expression::make_unary(OPERATOR_MULT, array, location);
10762 return new Array_index_expression(array, start, end, location);
10765 // A string index. This is used for both indexing and slicing.
10767 class String_index_expression : public Expression
10770 String_index_expression(Expression* string, Expression* start,
10771 Expression* end, Location location)
10772 : Expression(EXPRESSION_STRING_INDEX, location),
10773 string_(string), start_(start), end_(end)
10778 do_traverse(Traverse*);
10784 do_determine_type(const Type_context*);
10787 do_check_types(Gogo*);
10792 return Expression::make_string_index(this->string_->copy(),
10793 this->start_->copy(),
10794 (this->end_ == NULL
10796 : this->end_->copy()),
10801 do_must_eval_subexpressions_in_order(int* skip) const
10808 do_get_tree(Translate_context*);
10811 do_dump_expression(Ast_dump_context*) const;
10814 // The string we are getting a value from.
10815 Expression* string_;
10816 // The start or only index.
10817 Expression* start_;
10818 // The end index of a slice. This may be NULL for a single index,
10819 // or it may be a nil expression for the length of the string.
10823 // String index traversal.
10826 String_index_expression::do_traverse(Traverse* traverse)
10828 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
10829 return TRAVERSE_EXIT;
10830 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10831 return TRAVERSE_EXIT;
10832 if (this->end_ != NULL)
10834 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10835 return TRAVERSE_EXIT;
10837 return TRAVERSE_CONTINUE;
10840 // Return the type of a string index.
10843 String_index_expression::do_type()
10845 if (this->end_ == NULL)
10846 return Type::lookup_integer_type("uint8");
10848 return this->string_->type();
10851 // Determine the type of a string index.
10854 String_index_expression::do_determine_type(const Type_context*)
10856 this->string_->determine_type_no_context();
10857 this->start_->determine_type_no_context();
10858 if (this->end_ != NULL)
10859 this->end_->determine_type_no_context();
10862 // Check types of a string index.
10865 String_index_expression::do_check_types(Gogo*)
10867 if (this->start_->type()->integer_type() == NULL)
10868 this->report_error(_("index must be integer"));
10869 if (this->end_ != NULL
10870 && this->end_->type()->integer_type() == NULL
10871 && !this->end_->is_nil_expression())
10872 this->report_error(_("slice end must be integer"));
10875 bool sval_valid = this->string_->string_constant_value(&sval);
10880 if (this->start_->integer_constant_value(true, ival, &dummy))
10882 if (mpz_sgn(ival) < 0
10883 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
10885 error_at(this->start_->location(), "string index out of bounds");
10886 this->set_is_error();
10889 if (this->end_ != NULL && !this->end_->is_nil_expression())
10891 if (this->end_->integer_constant_value(true, ival, &dummy))
10893 if (mpz_sgn(ival) < 0
10894 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
10896 error_at(this->end_->location(), "string index out of bounds");
10897 this->set_is_error();
10904 // Get a tree for a string index.
10907 String_index_expression::do_get_tree(Translate_context* context)
10909 Location loc = this->location();
10911 tree string_tree = this->string_->get_tree(context);
10912 if (string_tree == error_mark_node)
10913 return error_mark_node;
10915 if (this->string_->type()->points_to() != NULL)
10916 string_tree = build_fold_indirect_ref(string_tree);
10917 if (!DECL_P(string_tree))
10918 string_tree = save_expr(string_tree);
10919 tree string_type = TREE_TYPE(string_tree);
10921 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
10922 length_tree = save_expr(length_tree);
10923 tree length_type = TREE_TYPE(length_tree);
10925 tree bad_index = boolean_false_node;
10927 tree start_tree = this->start_->get_tree(context);
10928 if (start_tree == error_mark_node)
10929 return error_mark_node;
10930 if (!DECL_P(start_tree))
10931 start_tree = save_expr(start_tree);
10932 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
10933 start_tree = convert_to_integer(length_type, start_tree);
10935 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
10938 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
10940 int code = (this->end_ == NULL
10941 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
10942 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
10943 tree crash = Gogo::runtime_error(code, loc);
10945 if (this->end_ == NULL)
10947 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10948 boolean_type_node, bad_index,
10949 fold_build2_loc(loc.gcc_location(), GE_EXPR,
10951 start_tree, length_tree));
10953 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
10954 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
10955 TREE_TYPE(bytes_tree),
10957 fold_convert_loc(loc.gcc_location(), sizetype,
10959 tree index = build_fold_indirect_ref_loc(loc.gcc_location(), ptr);
10961 return build2(COMPOUND_EXPR, TREE_TYPE(index),
10962 build3(COND_EXPR, void_type_node,
10963 bad_index, crash, NULL_TREE),
10969 if (this->end_->is_nil_expression())
10970 end_tree = build_int_cst(length_type, -1);
10973 end_tree = this->end_->get_tree(context);
10974 if (end_tree == error_mark_node)
10975 return error_mark_node;
10976 if (!DECL_P(end_tree))
10977 end_tree = save_expr(end_tree);
10978 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
10979 end_tree = convert_to_integer(length_type, end_tree);
10981 bad_index = Expression::check_bounds(end_tree, length_type,
10984 end_tree = fold_convert_loc(loc.gcc_location(), length_type,
10988 static tree strslice_fndecl;
10989 tree ret = Gogo::call_builtin(&strslice_fndecl,
10991 "__go_string_slice",
11000 if (ret == error_mark_node)
11001 return error_mark_node;
11002 // This will panic if the bounds are out of range for the
11004 TREE_NOTHROW(strslice_fndecl) = 0;
11006 if (bad_index == boolean_false_node)
11009 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
11010 build3(COND_EXPR, void_type_node,
11011 bad_index, crash, NULL_TREE),
11016 // Dump ast representation for a string index expression.
11019 String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11022 Index_expression::dump_index_expression(ast_dump_context, this->string_,
11023 this->start_, this->end_);
11026 // Make a string index expression. END may be NULL.
11029 Expression::make_string_index(Expression* string, Expression* start,
11030 Expression* end, Location location)
11032 return new String_index_expression(string, start, end, location);
11035 // Class Map_index.
11037 // Get the type of the map.
11040 Map_index_expression::get_map_type() const
11042 Map_type* mt = this->map_->type()->deref()->map_type();
11044 go_assert(saw_errors());
11048 // Map index traversal.
11051 Map_index_expression::do_traverse(Traverse* traverse)
11053 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
11054 return TRAVERSE_EXIT;
11055 return Expression::traverse(&this->index_, traverse);
11058 // Return the type of a map index.
11061 Map_index_expression::do_type()
11063 Map_type* mt = this->get_map_type();
11065 return Type::make_error_type();
11066 Type* type = mt->val_type();
11067 // If this map index is in a tuple assignment, we actually return a
11068 // pointer to the value type. Tuple_map_assignment_statement is
11069 // responsible for handling this correctly. We need to get the type
11070 // right in case this gets assigned to a temporary variable.
11071 if (this->is_in_tuple_assignment_)
11072 type = Type::make_pointer_type(type);
11076 // Fix the type of a map index.
11079 Map_index_expression::do_determine_type(const Type_context*)
11081 this->map_->determine_type_no_context();
11082 Map_type* mt = this->get_map_type();
11083 Type* key_type = mt == NULL ? NULL : mt->key_type();
11084 Type_context subcontext(key_type, false);
11085 this->index_->determine_type(&subcontext);
11088 // Check types of a map index.
11091 Map_index_expression::do_check_types(Gogo*)
11093 std::string reason;
11094 Map_type* mt = this->get_map_type();
11097 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
11099 if (reason.empty())
11100 this->report_error(_("incompatible type for map index"));
11103 error_at(this->location(), "incompatible type for map index (%s)",
11105 this->set_is_error();
11110 // Get a tree for a map index.
11113 Map_index_expression::do_get_tree(Translate_context* context)
11115 Map_type* type = this->get_map_type();
11117 return error_mark_node;
11119 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
11120 if (valptr == error_mark_node)
11121 return error_mark_node;
11122 valptr = save_expr(valptr);
11124 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
11126 if (this->is_lvalue_)
11127 return build_fold_indirect_ref(valptr);
11128 else if (this->is_in_tuple_assignment_)
11130 // Tuple_map_assignment_statement is responsible for using this
11136 Gogo* gogo = context->gogo();
11137 Btype* val_btype = type->val_type()->get_backend(gogo);
11138 Bexpression* val_zero = gogo->backend()->zero_expression(val_btype);
11139 return fold_build3(COND_EXPR, val_type_tree,
11140 fold_build2(EQ_EXPR, boolean_type_node, valptr,
11141 fold_convert(TREE_TYPE(valptr),
11142 null_pointer_node)),
11143 expr_to_tree(val_zero),
11144 build_fold_indirect_ref(valptr));
11148 // Get a tree for the map index. This returns a tree which evaluates
11149 // to a pointer to a value. The pointer will be NULL if the key is
11153 Map_index_expression::get_value_pointer(Translate_context* context,
11156 Map_type* type = this->get_map_type();
11158 return error_mark_node;
11160 tree map_tree = this->map_->get_tree(context);
11161 tree index_tree = this->index_->get_tree(context);
11162 index_tree = Expression::convert_for_assignment(context, type->key_type(),
11163 this->index_->type(),
11166 if (map_tree == error_mark_node || index_tree == error_mark_node)
11167 return error_mark_node;
11169 if (this->map_->type()->points_to() != NULL)
11170 map_tree = build_fold_indirect_ref(map_tree);
11172 // We need to pass in a pointer to the key, so stuff it into a
11176 if (current_function_decl != NULL)
11178 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
11179 DECL_IGNORED_P(tmp) = 0;
11180 DECL_INITIAL(tmp) = index_tree;
11181 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
11182 TREE_ADDRESSABLE(tmp) = 1;
11186 tmp = build_decl(this->location().gcc_location(), VAR_DECL,
11187 create_tmp_var_name("M"),
11188 TREE_TYPE(index_tree));
11189 DECL_EXTERNAL(tmp) = 0;
11190 TREE_PUBLIC(tmp) = 0;
11191 TREE_STATIC(tmp) = 1;
11192 DECL_ARTIFICIAL(tmp) = 1;
11193 if (!TREE_CONSTANT(index_tree))
11194 make_tmp = fold_build2_loc(this->location().gcc_location(),
11195 INIT_EXPR, void_type_node,
11199 TREE_READONLY(tmp) = 1;
11200 TREE_CONSTANT(tmp) = 1;
11201 DECL_INITIAL(tmp) = index_tree;
11202 make_tmp = NULL_TREE;
11204 rest_of_decl_compilation(tmp, 1, 0);
11207 fold_convert_loc(this->location().gcc_location(), const_ptr_type_node,
11208 build_fold_addr_expr_loc(this->location().gcc_location(),
11211 static tree map_index_fndecl;
11212 tree call = Gogo::call_builtin(&map_index_fndecl,
11216 const_ptr_type_node,
11217 TREE_TYPE(map_tree),
11219 const_ptr_type_node,
11223 ? boolean_true_node
11224 : boolean_false_node));
11225 if (call == error_mark_node)
11226 return error_mark_node;
11227 // This can panic on a map of interface type if the interface holds
11228 // an uncomparable or unhashable type.
11229 TREE_NOTHROW(map_index_fndecl) = 0;
11231 Type* val_type = type->val_type();
11232 tree val_type_tree = type_to_tree(val_type->get_backend(context->gogo()));
11233 if (val_type_tree == error_mark_node)
11234 return error_mark_node;
11235 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
11237 tree ret = fold_convert_loc(this->location().gcc_location(),
11238 ptr_val_type_tree, call);
11239 if (make_tmp != NULL_TREE)
11240 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
11244 // Dump ast representation for a map index expression
11247 Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11250 Index_expression::dump_index_expression(ast_dump_context,
11251 this->map_, this->index_, NULL);
11254 // Make a map index expression.
11256 Map_index_expression*
11257 Expression::make_map_index(Expression* map, Expression* index,
11260 return new Map_index_expression(map, index, location);
11263 // Class Field_reference_expression.
11265 // Return the type of a field reference.
11268 Field_reference_expression::do_type()
11270 Type* type = this->expr_->type();
11271 if (type->is_error())
11273 Struct_type* struct_type = type->struct_type();
11274 go_assert(struct_type != NULL);
11275 return struct_type->field(this->field_index_)->type();
11278 // Check the types for a field reference.
11281 Field_reference_expression::do_check_types(Gogo*)
11283 Type* type = this->expr_->type();
11284 if (type->is_error())
11286 Struct_type* struct_type = type->struct_type();
11287 go_assert(struct_type != NULL);
11288 go_assert(struct_type->field(this->field_index_) != NULL);
11291 // Get a tree for a field reference.
11294 Field_reference_expression::do_get_tree(Translate_context* context)
11296 tree struct_tree = this->expr_->get_tree(context);
11297 if (struct_tree == error_mark_node
11298 || TREE_TYPE(struct_tree) == error_mark_node)
11299 return error_mark_node;
11300 go_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
11301 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
11302 if (field == NULL_TREE)
11304 // This can happen for a type which refers to itself indirectly
11305 // and then turns out to be erroneous.
11306 go_assert(saw_errors());
11307 return error_mark_node;
11309 for (unsigned int i = this->field_index_; i > 0; --i)
11311 field = DECL_CHAIN(field);
11312 go_assert(field != NULL_TREE);
11314 if (TREE_TYPE(field) == error_mark_node)
11315 return error_mark_node;
11316 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
11320 // Dump ast representation for a field reference expression.
11323 Field_reference_expression::do_dump_expression(
11324 Ast_dump_context* ast_dump_context) const
11326 this->expr_->dump_expression(ast_dump_context);
11327 ast_dump_context->ostream() << "." << this->field_index_;
11330 // Make a reference to a qualified identifier in an expression.
11332 Field_reference_expression*
11333 Expression::make_field_reference(Expression* expr, unsigned int field_index,
11336 return new Field_reference_expression(expr, field_index, location);
11339 // Class Interface_field_reference_expression.
11341 // Return a tree for the pointer to the function to call.
11344 Interface_field_reference_expression::get_function_tree(Translate_context*,
11347 if (this->expr_->type()->points_to() != NULL)
11348 expr = build_fold_indirect_ref(expr);
11350 tree expr_type = TREE_TYPE(expr);
11351 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
11353 tree field = TYPE_FIELDS(expr_type);
11354 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
11356 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
11357 go_assert(POINTER_TYPE_P(TREE_TYPE(table)));
11359 table = build_fold_indirect_ref(table);
11360 go_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
11362 std::string name = Gogo::unpack_hidden_name(this->name_);
11363 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
11364 field != NULL_TREE;
11365 field = DECL_CHAIN(field))
11367 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
11370 go_assert(field != NULL_TREE);
11372 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
11375 // Return a tree for the first argument to pass to the interface
11379 Interface_field_reference_expression::get_underlying_object_tree(
11380 Translate_context*,
11383 if (this->expr_->type()->points_to() != NULL)
11384 expr = build_fold_indirect_ref(expr);
11386 tree expr_type = TREE_TYPE(expr);
11387 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
11389 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
11390 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
11392 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
11398 Interface_field_reference_expression::do_traverse(Traverse* traverse)
11400 return Expression::traverse(&this->expr_, traverse);
11403 // Return the type of an interface field reference.
11406 Interface_field_reference_expression::do_type()
11408 Type* expr_type = this->expr_->type();
11410 Type* points_to = expr_type->points_to();
11411 if (points_to != NULL)
11412 expr_type = points_to;
11414 Interface_type* interface_type = expr_type->interface_type();
11415 if (interface_type == NULL)
11416 return Type::make_error_type();
11418 const Typed_identifier* method = interface_type->find_method(this->name_);
11419 if (method == NULL)
11420 return Type::make_error_type();
11422 return method->type();
11425 // Determine types.
11428 Interface_field_reference_expression::do_determine_type(const Type_context*)
11430 this->expr_->determine_type_no_context();
11433 // Check the types for an interface field reference.
11436 Interface_field_reference_expression::do_check_types(Gogo*)
11438 Type* type = this->expr_->type();
11440 Type* points_to = type->points_to();
11441 if (points_to != NULL)
11444 Interface_type* interface_type = type->interface_type();
11445 if (interface_type == NULL)
11447 if (!type->is_error_type())
11448 this->report_error(_("expected interface or pointer to interface"));
11452 const Typed_identifier* method =
11453 interface_type->find_method(this->name_);
11454 if (method == NULL)
11456 error_at(this->location(), "method %qs not in interface",
11457 Gogo::message_name(this->name_).c_str());
11458 this->set_is_error();
11463 // Get a tree for a reference to a field in an interface. There is no
11464 // standard tree type representation for this: it's a function
11465 // attached to its first argument, like a Bound_method_expression.
11466 // The only places it may currently be used are in a Call_expression
11467 // or a Go_statement, which will take it apart directly. So this has
11468 // nothing to do at present.
11471 Interface_field_reference_expression::do_get_tree(Translate_context*)
11476 // Dump ast representation for an interface field reference.
11479 Interface_field_reference_expression::do_dump_expression(
11480 Ast_dump_context* ast_dump_context) const
11482 this->expr_->dump_expression(ast_dump_context);
11483 ast_dump_context->ostream() << "." << this->name_;
11486 // Make a reference to a field in an interface.
11489 Expression::make_interface_field_reference(Expression* expr,
11490 const std::string& field,
11493 return new Interface_field_reference_expression(expr, field, location);
11496 // A general selector. This is a Parser_expression for LEFT.NAME. It
11497 // is lowered after we know the type of the left hand side.
11499 class Selector_expression : public Parser_expression
11502 Selector_expression(Expression* left, const std::string& name,
11504 : Parser_expression(EXPRESSION_SELECTOR, location),
11505 left_(left), name_(name)
11510 do_traverse(Traverse* traverse)
11511 { return Expression::traverse(&this->left_, traverse); }
11514 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
11519 return new Selector_expression(this->left_->copy(), this->name_,
11524 do_dump_expression(Ast_dump_context* ast_dump_context) const;
11528 lower_method_expression(Gogo*);
11530 // The expression on the left hand side.
11532 // The name on the right hand side.
11536 // Lower a selector expression once we know the real type of the left
11540 Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*,
11543 Expression* left = this->left_;
11544 if (left->is_type_expression())
11545 return this->lower_method_expression(gogo);
11546 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
11550 // Lower a method expression T.M or (*T).M. We turn this into a
11551 // function literal.
11554 Selector_expression::lower_method_expression(Gogo* gogo)
11556 Location location = this->location();
11557 Type* type = this->left_->type();
11558 const std::string& name(this->name_);
11561 if (type->points_to() == NULL)
11562 is_pointer = false;
11566 type = type->points_to();
11568 Named_type* nt = type->named_type();
11572 ("method expression requires named type or "
11573 "pointer to named type"));
11574 return Expression::make_error(location);
11578 Method* method = nt->method_function(name, &is_ambiguous);
11579 const Typed_identifier* imethod = NULL;
11580 if (method == NULL && !is_pointer)
11582 Interface_type* it = nt->interface_type();
11584 imethod = it->find_method(name);
11587 if (method == NULL && imethod == NULL)
11590 error_at(location, "type %<%s%s%> has no method %<%s%>",
11591 is_pointer ? "*" : "",
11592 nt->message_name().c_str(),
11593 Gogo::message_name(name).c_str());
11595 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
11596 Gogo::message_name(name).c_str(),
11597 is_pointer ? "*" : "",
11598 nt->message_name().c_str());
11599 return Expression::make_error(location);
11602 if (method != NULL && !is_pointer && !method->is_value_method())
11604 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
11605 nt->message_name().c_str(),
11606 Gogo::message_name(name).c_str());
11607 return Expression::make_error(location);
11610 // Build a new function type in which the receiver becomes the first
11612 Function_type* method_type;
11613 if (method != NULL)
11615 method_type = method->type();
11616 go_assert(method_type->is_method());
11620 method_type = imethod->type()->function_type();
11621 go_assert(method_type != NULL && !method_type->is_method());
11624 const char* const receiver_name = "$this";
11625 Typed_identifier_list* parameters = new Typed_identifier_list();
11626 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
11629 const Typed_identifier_list* method_parameters = method_type->parameters();
11630 if (method_parameters != NULL)
11632 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
11633 p != method_parameters->end();
11635 parameters->push_back(*p);
11638 const Typed_identifier_list* method_results = method_type->results();
11639 Typed_identifier_list* results;
11640 if (method_results == NULL)
11644 results = new Typed_identifier_list();
11645 for (Typed_identifier_list::const_iterator p = method_results->begin();
11646 p != method_results->end();
11648 results->push_back(*p);
11651 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
11653 if (method_type->is_varargs())
11654 fntype->set_is_varargs();
11656 // We generate methods which always takes a pointer to the receiver
11657 // as their first argument. If this is for a pointer type, we can
11658 // simply reuse the existing function. We use an internal hack to
11659 // get the right type.
11661 if (method != NULL && is_pointer)
11663 Named_object* mno = (method->needs_stub_method()
11664 ? method->stub_object()
11665 : method->named_object());
11666 Expression* f = Expression::make_func_reference(mno, NULL, location);
11667 f = Expression::make_cast(fntype, f, location);
11668 Type_conversion_expression* tce =
11669 static_cast<Type_conversion_expression*>(f);
11670 tce->set_may_convert_function_types();
11674 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
11677 Named_object* vno = gogo->lookup(receiver_name, NULL);
11678 go_assert(vno != NULL);
11679 Expression* ve = Expression::make_var_reference(vno, location);
11681 if (method != NULL)
11682 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
11684 bm = Expression::make_interface_field_reference(ve, name, location);
11686 // Even though we found the method above, if it has an error type we
11687 // may see an error here.
11688 if (bm->is_error_expression())
11690 gogo->finish_function(location);
11694 Expression_list* args;
11695 if (method_parameters == NULL)
11699 args = new Expression_list();
11700 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
11701 p != method_parameters->end();
11704 vno = gogo->lookup(p->name(), NULL);
11705 go_assert(vno != NULL);
11706 args->push_back(Expression::make_var_reference(vno, location));
11710 gogo->start_block(location);
11712 Call_expression* call = Expression::make_call(bm, args,
11713 method_type->is_varargs(),
11716 size_t count = call->result_count();
11719 s = Statement::make_statement(call, true);
11722 Expression_list* retvals = new Expression_list();
11724 retvals->push_back(call);
11727 for (size_t i = 0; i < count; ++i)
11728 retvals->push_back(Expression::make_call_result(call, i));
11730 s = Statement::make_return_statement(retvals, location);
11732 gogo->add_statement(s);
11734 Block* b = gogo->finish_block(location);
11736 gogo->add_block(b, location);
11738 // Lower the call in case there are multiple results.
11739 gogo->lower_block(no, b);
11741 gogo->finish_function(location);
11743 return Expression::make_func_reference(no, NULL, location);
11746 // Dump the ast for a selector expression.
11749 Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11752 ast_dump_context->dump_expression(this->left_);
11753 ast_dump_context->ostream() << ".";
11754 ast_dump_context->ostream() << this->name_;
11757 // Make a selector expression.
11760 Expression::make_selector(Expression* left, const std::string& name,
11763 return new Selector_expression(left, name, location);
11766 // Implement the builtin function new.
11768 class Allocation_expression : public Expression
11771 Allocation_expression(Type* type, Location location)
11772 : Expression(EXPRESSION_ALLOCATION, location),
11778 do_traverse(Traverse* traverse)
11779 { return Type::traverse(this->type_, traverse); }
11783 { return Type::make_pointer_type(this->type_); }
11786 do_determine_type(const Type_context*)
11791 { return new Allocation_expression(this->type_, this->location()); }
11794 do_get_tree(Translate_context*);
11797 do_dump_expression(Ast_dump_context*) const;
11800 // The type we are allocating.
11804 // Return a tree for an allocation expression.
11807 Allocation_expression::do_get_tree(Translate_context* context)
11809 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
11810 if (type_tree == error_mark_node)
11811 return error_mark_node;
11812 tree size_tree = TYPE_SIZE_UNIT(type_tree);
11813 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
11815 if (space == error_mark_node)
11816 return error_mark_node;
11817 return fold_convert(build_pointer_type(type_tree), space);
11820 // Dump ast representation for an allocation expression.
11823 Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11826 ast_dump_context->ostream() << "new(";
11827 ast_dump_context->dump_type(this->type_);
11828 ast_dump_context->ostream() << ")";
11831 // Make an allocation expression.
11834 Expression::make_allocation(Type* type, Location location)
11836 return new Allocation_expression(type, location);
11839 // Construct a struct.
11841 class Struct_construction_expression : public Expression
11844 Struct_construction_expression(Type* type, Expression_list* vals,
11846 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
11847 type_(type), vals_(vals)
11850 // Return whether this is a constant initializer.
11852 is_constant_struct() const;
11856 do_traverse(Traverse* traverse);
11860 { return this->type_; }
11863 do_determine_type(const Type_context*);
11866 do_check_types(Gogo*);
11871 return new Struct_construction_expression(this->type_, this->vals_->copy(),
11876 do_is_addressable() const
11880 do_get_tree(Translate_context*);
11883 do_export(Export*) const;
11886 do_dump_expression(Ast_dump_context*) const;
11889 // The type of the struct to construct.
11891 // The list of values, in order of the fields in the struct. A NULL
11892 // entry means that the field should be zero-initialized.
11893 Expression_list* vals_;
11899 Struct_construction_expression::do_traverse(Traverse* traverse)
11901 if (this->vals_ != NULL
11902 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11903 return TRAVERSE_EXIT;
11904 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11905 return TRAVERSE_EXIT;
11906 return TRAVERSE_CONTINUE;
11909 // Return whether this is a constant initializer.
11912 Struct_construction_expression::is_constant_struct() const
11914 if (this->vals_ == NULL)
11916 for (Expression_list::const_iterator pv = this->vals_->begin();
11917 pv != this->vals_->end();
11921 && !(*pv)->is_constant()
11922 && (!(*pv)->is_composite_literal()
11923 || (*pv)->is_nonconstant_composite_literal()))
11927 const Struct_field_list* fields = this->type_->struct_type()->fields();
11928 for (Struct_field_list::const_iterator pf = fields->begin();
11929 pf != fields->end();
11932 // There are no constant constructors for interfaces.
11933 if (pf->type()->interface_type() != NULL)
11940 // Final type determination.
11943 Struct_construction_expression::do_determine_type(const Type_context*)
11945 if (this->vals_ == NULL)
11947 const Struct_field_list* fields = this->type_->struct_type()->fields();
11948 Expression_list::const_iterator pv = this->vals_->begin();
11949 for (Struct_field_list::const_iterator pf = fields->begin();
11950 pf != fields->end();
11953 if (pv == this->vals_->end())
11957 Type_context subcontext(pf->type(), false);
11958 (*pv)->determine_type(&subcontext);
11961 // Extra values are an error we will report elsewhere; we still want
11962 // to determine the type to avoid knockon errors.
11963 for (; pv != this->vals_->end(); ++pv)
11964 (*pv)->determine_type_no_context();
11970 Struct_construction_expression::do_check_types(Gogo*)
11972 if (this->vals_ == NULL)
11975 Struct_type* st = this->type_->struct_type();
11976 if (this->vals_->size() > st->field_count())
11978 this->report_error(_("too many expressions for struct"));
11982 const Struct_field_list* fields = st->fields();
11983 Expression_list::const_iterator pv = this->vals_->begin();
11985 for (Struct_field_list::const_iterator pf = fields->begin();
11986 pf != fields->end();
11989 if (pv == this->vals_->end())
11991 this->report_error(_("too few expressions for struct"));
11998 std::string reason;
11999 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
12001 if (reason.empty())
12002 error_at((*pv)->location(),
12003 "incompatible type for field %d in struct construction",
12006 error_at((*pv)->location(),
12007 ("incompatible type for field %d in "
12008 "struct construction (%s)"),
12009 i + 1, reason.c_str());
12010 this->set_is_error();
12013 go_assert(pv == this->vals_->end());
12016 // Return a tree for constructing a struct.
12019 Struct_construction_expression::do_get_tree(Translate_context* context)
12021 Gogo* gogo = context->gogo();
12023 if (this->vals_ == NULL)
12025 Btype* btype = this->type_->get_backend(gogo);
12026 return expr_to_tree(gogo->backend()->zero_expression(btype));
12029 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
12030 if (type_tree == error_mark_node)
12031 return error_mark_node;
12032 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
12034 bool is_constant = true;
12035 const Struct_field_list* fields = this->type_->struct_type()->fields();
12036 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
12038 Struct_field_list::const_iterator pf = fields->begin();
12039 Expression_list::const_iterator pv = this->vals_->begin();
12040 for (tree field = TYPE_FIELDS(type_tree);
12041 field != NULL_TREE;
12042 field = DECL_CHAIN(field), ++pf)
12044 go_assert(pf != fields->end());
12046 Btype* fbtype = pf->type()->get_backend(gogo);
12049 if (pv == this->vals_->end())
12050 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
12051 else if (*pv == NULL)
12053 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
12058 val = Expression::convert_for_assignment(context, pf->type(),
12060 (*pv)->get_tree(context),
12065 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
12066 return error_mark_node;
12068 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
12069 elt->index = field;
12071 if (!TREE_CONSTANT(val))
12072 is_constant = false;
12074 go_assert(pf == fields->end());
12076 tree ret = build_constructor(type_tree, elts);
12078 TREE_CONSTANT(ret) = 1;
12082 // Export a struct construction.
12085 Struct_construction_expression::do_export(Export* exp) const
12087 exp->write_c_string("convert(");
12088 exp->write_type(this->type_);
12089 for (Expression_list::const_iterator pv = this->vals_->begin();
12090 pv != this->vals_->end();
12093 exp->write_c_string(", ");
12095 (*pv)->export_expression(exp);
12097 exp->write_c_string(")");
12100 // Dump ast representation of a struct construction expression.
12103 Struct_construction_expression::do_dump_expression(
12104 Ast_dump_context* ast_dump_context) const
12106 ast_dump_context->dump_type(this->type_);
12107 ast_dump_context->ostream() << "{";
12108 ast_dump_context->dump_expression_list(this->vals_);
12109 ast_dump_context->ostream() << "}";
12112 // Make a struct composite literal. This used by the thunk code.
12115 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
12118 go_assert(type->struct_type() != NULL);
12119 return new Struct_construction_expression(type, vals, location);
12122 // Construct an array. This class is not used directly; instead we
12123 // use the child classes, Fixed_array_construction_expression and
12124 // Open_array_construction_expression.
12126 class Array_construction_expression : public Expression
12129 Array_construction_expression(Expression_classification classification,
12130 Type* type, Expression_list* vals,
12132 : Expression(classification, location),
12133 type_(type), vals_(vals)
12137 // Return whether this is a constant initializer.
12139 is_constant_array() const;
12141 // Return the number of elements.
12143 element_count() const
12144 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
12148 do_traverse(Traverse* traverse);
12152 { return this->type_; }
12155 do_determine_type(const Type_context*);
12158 do_check_types(Gogo*);
12161 do_is_addressable() const
12165 do_export(Export*) const;
12167 // The list of values.
12170 { return this->vals_; }
12172 // Get a constructor tree for the array values.
12174 get_constructor_tree(Translate_context* context, tree type_tree);
12177 do_dump_expression(Ast_dump_context*) const;
12180 // The type of the array to construct.
12182 // The list of values.
12183 Expression_list* vals_;
12189 Array_construction_expression::do_traverse(Traverse* traverse)
12191 if (this->vals_ != NULL
12192 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12193 return TRAVERSE_EXIT;
12194 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12195 return TRAVERSE_EXIT;
12196 return TRAVERSE_CONTINUE;
12199 // Return whether this is a constant initializer.
12202 Array_construction_expression::is_constant_array() const
12204 if (this->vals_ == NULL)
12207 // There are no constant constructors for interfaces.
12208 if (this->type_->array_type()->element_type()->interface_type() != NULL)
12211 for (Expression_list::const_iterator pv = this->vals_->begin();
12212 pv != this->vals_->end();
12216 && !(*pv)->is_constant()
12217 && (!(*pv)->is_composite_literal()
12218 || (*pv)->is_nonconstant_composite_literal()))
12224 // Final type determination.
12227 Array_construction_expression::do_determine_type(const Type_context*)
12229 if (this->vals_ == NULL)
12231 Type_context subcontext(this->type_->array_type()->element_type(), false);
12232 for (Expression_list::const_iterator pv = this->vals_->begin();
12233 pv != this->vals_->end();
12237 (*pv)->determine_type(&subcontext);
12244 Array_construction_expression::do_check_types(Gogo*)
12246 if (this->vals_ == NULL)
12249 Array_type* at = this->type_->array_type();
12251 Type* element_type = at->element_type();
12252 for (Expression_list::const_iterator pv = this->vals_->begin();
12253 pv != this->vals_->end();
12257 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
12259 error_at((*pv)->location(),
12260 "incompatible type for element %d in composite literal",
12262 this->set_is_error();
12266 Expression* length = at->length();
12267 if (length != NULL && !length->is_error_expression())
12272 if (at->length()->integer_constant_value(true, val, &type))
12274 if (this->vals_->size() > mpz_get_ui(val))
12275 this->report_error(_("too many elements in composite literal"));
12281 // Get a constructor tree for the array values.
12284 Array_construction_expression::get_constructor_tree(Translate_context* context,
12287 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
12288 (this->vals_ == NULL
12290 : this->vals_->size()));
12291 Type* element_type = this->type_->array_type()->element_type();
12292 bool is_constant = true;
12293 if (this->vals_ != NULL)
12296 for (Expression_list::const_iterator pv = this->vals_->begin();
12297 pv != this->vals_->end();
12300 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
12301 elt->index = size_int(i);
12304 Gogo* gogo = context->gogo();
12305 Btype* ebtype = element_type->get_backend(gogo);
12306 Bexpression *zv = gogo->backend()->zero_expression(ebtype);
12307 elt->value = expr_to_tree(zv);
12311 tree value_tree = (*pv)->get_tree(context);
12312 elt->value = Expression::convert_for_assignment(context,
12318 if (elt->value == error_mark_node)
12319 return error_mark_node;
12320 if (!TREE_CONSTANT(elt->value))
12321 is_constant = false;
12325 tree ret = build_constructor(type_tree, values);
12327 TREE_CONSTANT(ret) = 1;
12331 // Export an array construction.
12334 Array_construction_expression::do_export(Export* exp) const
12336 exp->write_c_string("convert(");
12337 exp->write_type(this->type_);
12338 if (this->vals_ != NULL)
12340 for (Expression_list::const_iterator pv = this->vals_->begin();
12341 pv != this->vals_->end();
12344 exp->write_c_string(", ");
12346 (*pv)->export_expression(exp);
12349 exp->write_c_string(")");
12352 // Dump ast representation of an array construction expressin.
12355 Array_construction_expression::do_dump_expression(
12356 Ast_dump_context* ast_dump_context) const
12358 Expression* length = this->type_->array_type() != NULL ?
12359 this->type_->array_type()->length() : NULL;
12361 ast_dump_context->ostream() << "[" ;
12362 if (length != NULL)
12364 ast_dump_context->dump_expression(length);
12366 ast_dump_context->ostream() << "]" ;
12367 ast_dump_context->dump_type(this->type_);
12368 ast_dump_context->ostream() << "{" ;
12369 ast_dump_context->dump_expression_list(this->vals_);
12370 ast_dump_context->ostream() << "}" ;
12374 // Construct a fixed array.
12376 class Fixed_array_construction_expression :
12377 public Array_construction_expression
12380 Fixed_array_construction_expression(Type* type, Expression_list* vals,
12382 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
12383 type, vals, location)
12385 go_assert(type->array_type() != NULL
12386 && type->array_type()->length() != NULL);
12393 return new Fixed_array_construction_expression(this->type(),
12394 (this->vals() == NULL
12396 : this->vals()->copy()),
12401 do_get_tree(Translate_context*);
12404 do_dump_expression(Ast_dump_context*);
12407 // Return a tree for constructing a fixed array.
12410 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
12412 Type* type = this->type();
12413 Btype* btype = type->get_backend(context->gogo());
12414 return this->get_constructor_tree(context, type_to_tree(btype));
12417 // Dump ast representation of an array construction expressin.
12420 Fixed_array_construction_expression::do_dump_expression(
12421 Ast_dump_context* ast_dump_context)
12424 ast_dump_context->ostream() << "[";
12425 ast_dump_context->dump_expression (this->type()->array_type()->length());
12426 ast_dump_context->ostream() << "]";
12427 ast_dump_context->dump_type(this->type());
12428 ast_dump_context->ostream() << "{";
12429 ast_dump_context->dump_expression_list(this->vals());
12430 ast_dump_context->ostream() << "}";
12433 // Construct an open array.
12435 class Open_array_construction_expression : public Array_construction_expression
12438 Open_array_construction_expression(Type* type, Expression_list* vals,
12440 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
12441 type, vals, location)
12443 go_assert(type->array_type() != NULL
12444 && type->array_type()->length() == NULL);
12448 // Note that taking the address of an open array literal is invalid.
12453 return new Open_array_construction_expression(this->type(),
12454 (this->vals() == NULL
12456 : this->vals()->copy()),
12461 do_get_tree(Translate_context*);
12464 // Return a tree for constructing an open array.
12467 Open_array_construction_expression::do_get_tree(Translate_context* context)
12469 Array_type* array_type = this->type()->array_type();
12470 if (array_type == NULL)
12472 go_assert(this->type()->is_error());
12473 return error_mark_node;
12476 Type* element_type = array_type->element_type();
12477 Btype* belement_type = element_type->get_backend(context->gogo());
12478 tree element_type_tree = type_to_tree(belement_type);
12479 if (element_type_tree == error_mark_node)
12480 return error_mark_node;
12484 if (this->vals() == NULL || this->vals()->empty())
12486 // We need to create a unique value.
12487 tree max = size_int(0);
12488 tree constructor_type = build_array_type(element_type_tree,
12489 build_index_type(max));
12490 if (constructor_type == error_mark_node)
12491 return error_mark_node;
12492 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
12493 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
12494 elt->index = size_int(0);
12495 Gogo* gogo = context->gogo();
12496 Btype* btype = element_type->get_backend(gogo);
12497 elt->value = expr_to_tree(gogo->backend()->zero_expression(btype));
12498 values = build_constructor(constructor_type, vec);
12499 if (TREE_CONSTANT(elt->value))
12500 TREE_CONSTANT(values) = 1;
12501 length_tree = size_int(0);
12505 tree max = size_int(this->vals()->size() - 1);
12506 tree constructor_type = build_array_type(element_type_tree,
12507 build_index_type(max));
12508 if (constructor_type == error_mark_node)
12509 return error_mark_node;
12510 values = this->get_constructor_tree(context, constructor_type);
12511 length_tree = size_int(this->vals()->size());
12514 if (values == error_mark_node)
12515 return error_mark_node;
12517 bool is_constant_initializer = TREE_CONSTANT(values);
12519 // We have to copy the initial values into heap memory if we are in
12520 // a function or if the values are not constants. We also have to
12521 // copy them if they may contain pointers in a non-constant context,
12522 // as otherwise the garbage collector won't see them.
12523 bool copy_to_heap = (context->function() != NULL
12524 || !is_constant_initializer
12525 || (element_type->has_pointer()
12526 && !context->is_const()));
12528 if (is_constant_initializer)
12530 tree tmp = build_decl(this->location().gcc_location(), VAR_DECL,
12531 create_tmp_var_name("C"), TREE_TYPE(values));
12532 DECL_EXTERNAL(tmp) = 0;
12533 TREE_PUBLIC(tmp) = 0;
12534 TREE_STATIC(tmp) = 1;
12535 DECL_ARTIFICIAL(tmp) = 1;
12538 // If we are not copying the value to the heap, we will only
12539 // initialize the value once, so we can use this directly
12540 // rather than copying it. In that case we can't make it
12541 // read-only, because the program is permitted to change it.
12542 TREE_READONLY(tmp) = 1;
12543 TREE_CONSTANT(tmp) = 1;
12545 DECL_INITIAL(tmp) = values;
12546 rest_of_decl_compilation(tmp, 1, 0);
12554 // the initializer will only run once.
12555 space = build_fold_addr_expr(values);
12560 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
12561 space = context->gogo()->allocate_memory(element_type, memsize,
12563 space = save_expr(space);
12565 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
12566 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
12568 TREE_THIS_NOTRAP(ref) = 1;
12569 set = build2(MODIFY_EXPR, void_type_node, ref, values);
12572 // Build a constructor for the open array.
12574 tree type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
12575 if (type_tree == error_mark_node)
12576 return error_mark_node;
12577 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
12579 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
12581 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
12582 tree field = TYPE_FIELDS(type_tree);
12583 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
12584 elt->index = field;
12585 elt->value = fold_convert(TREE_TYPE(field), space);
12587 elt = VEC_quick_push(constructor_elt, init, NULL);
12588 field = DECL_CHAIN(field);
12589 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
12590 elt->index = field;
12591 elt->value = fold_convert(TREE_TYPE(field), length_tree);
12593 elt = VEC_quick_push(constructor_elt, init, NULL);
12594 field = DECL_CHAIN(field);
12595 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
12596 elt->index = field;
12597 elt->value = fold_convert(TREE_TYPE(field), length_tree);
12599 tree constructor = build_constructor(type_tree, init);
12600 if (constructor == error_mark_node)
12601 return error_mark_node;
12603 TREE_CONSTANT(constructor) = 1;
12605 if (set == NULL_TREE)
12606 return constructor;
12608 return build2(COMPOUND_EXPR, type_tree, set, constructor);
12611 // Make a slice composite literal. This is used by the type
12612 // descriptor code.
12615 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
12618 go_assert(type->is_slice_type());
12619 return new Open_array_construction_expression(type, vals, location);
12622 // Construct a map.
12624 class Map_construction_expression : public Expression
12627 Map_construction_expression(Type* type, Expression_list* vals,
12629 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
12630 type_(type), vals_(vals)
12631 { go_assert(vals == NULL || vals->size() % 2 == 0); }
12635 do_traverse(Traverse* traverse);
12639 { return this->type_; }
12642 do_determine_type(const Type_context*);
12645 do_check_types(Gogo*);
12650 return new Map_construction_expression(this->type_, this->vals_->copy(),
12655 do_get_tree(Translate_context*);
12658 do_export(Export*) const;
12661 do_dump_expression(Ast_dump_context*) const;
12664 // The type of the map to construct.
12666 // The list of values.
12667 Expression_list* vals_;
12673 Map_construction_expression::do_traverse(Traverse* traverse)
12675 if (this->vals_ != NULL
12676 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12677 return TRAVERSE_EXIT;
12678 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12679 return TRAVERSE_EXIT;
12680 return TRAVERSE_CONTINUE;
12683 // Final type determination.
12686 Map_construction_expression::do_determine_type(const Type_context*)
12688 if (this->vals_ == NULL)
12691 Map_type* mt = this->type_->map_type();
12692 Type_context key_context(mt->key_type(), false);
12693 Type_context val_context(mt->val_type(), false);
12694 for (Expression_list::const_iterator pv = this->vals_->begin();
12695 pv != this->vals_->end();
12698 (*pv)->determine_type(&key_context);
12700 (*pv)->determine_type(&val_context);
12707 Map_construction_expression::do_check_types(Gogo*)
12709 if (this->vals_ == NULL)
12712 Map_type* mt = this->type_->map_type();
12714 Type* key_type = mt->key_type();
12715 Type* val_type = mt->val_type();
12716 for (Expression_list::const_iterator pv = this->vals_->begin();
12717 pv != this->vals_->end();
12720 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
12722 error_at((*pv)->location(),
12723 "incompatible type for element %d key in map construction",
12725 this->set_is_error();
12728 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
12730 error_at((*pv)->location(),
12731 ("incompatible type for element %d value "
12732 "in map construction"),
12734 this->set_is_error();
12739 // Return a tree for constructing a map.
12742 Map_construction_expression::do_get_tree(Translate_context* context)
12744 Gogo* gogo = context->gogo();
12745 Location loc = this->location();
12747 Map_type* mt = this->type_->map_type();
12749 // Build a struct to hold the key and value.
12750 tree struct_type = make_node(RECORD_TYPE);
12752 Type* key_type = mt->key_type();
12753 tree id = get_identifier("__key");
12754 tree key_type_tree = type_to_tree(key_type->get_backend(gogo));
12755 if (key_type_tree == error_mark_node)
12756 return error_mark_node;
12757 tree key_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
12759 DECL_CONTEXT(key_field) = struct_type;
12760 TYPE_FIELDS(struct_type) = key_field;
12762 Type* val_type = mt->val_type();
12763 id = get_identifier("__val");
12764 tree val_type_tree = type_to_tree(val_type->get_backend(gogo));
12765 if (val_type_tree == error_mark_node)
12766 return error_mark_node;
12767 tree val_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
12769 DECL_CONTEXT(val_field) = struct_type;
12770 DECL_CHAIN(key_field) = val_field;
12772 layout_type(struct_type);
12774 bool is_constant = true;
12779 if (this->vals_ == NULL || this->vals_->empty())
12781 valaddr = null_pointer_node;
12782 make_tmp = NULL_TREE;
12786 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
12787 this->vals_->size() / 2);
12789 for (Expression_list::const_iterator pv = this->vals_->begin();
12790 pv != this->vals_->end();
12793 bool one_is_constant = true;
12795 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
12797 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
12798 elt->index = key_field;
12799 tree val_tree = (*pv)->get_tree(context);
12800 elt->value = Expression::convert_for_assignment(context, key_type,
12803 if (elt->value == error_mark_node)
12804 return error_mark_node;
12805 if (!TREE_CONSTANT(elt->value))
12806 one_is_constant = false;
12810 elt = VEC_quick_push(constructor_elt, one, NULL);
12811 elt->index = val_field;
12812 val_tree = (*pv)->get_tree(context);
12813 elt->value = Expression::convert_for_assignment(context, val_type,
12816 if (elt->value == error_mark_node)
12817 return error_mark_node;
12818 if (!TREE_CONSTANT(elt->value))
12819 one_is_constant = false;
12821 elt = VEC_quick_push(constructor_elt, values, NULL);
12822 elt->index = size_int(i);
12823 elt->value = build_constructor(struct_type, one);
12824 if (one_is_constant)
12825 TREE_CONSTANT(elt->value) = 1;
12827 is_constant = false;
12830 tree index_type = build_index_type(size_int(i - 1));
12831 tree array_type = build_array_type(struct_type, index_type);
12832 tree init = build_constructor(array_type, values);
12834 TREE_CONSTANT(init) = 1;
12836 if (current_function_decl != NULL)
12838 tmp = create_tmp_var(array_type, get_name(array_type));
12839 DECL_INITIAL(tmp) = init;
12840 make_tmp = fold_build1_loc(loc.gcc_location(), DECL_EXPR,
12841 void_type_node, tmp);
12842 TREE_ADDRESSABLE(tmp) = 1;
12846 tmp = build_decl(loc.gcc_location(), VAR_DECL,
12847 create_tmp_var_name("M"), array_type);
12848 DECL_EXTERNAL(tmp) = 0;
12849 TREE_PUBLIC(tmp) = 0;
12850 TREE_STATIC(tmp) = 1;
12851 DECL_ARTIFICIAL(tmp) = 1;
12852 if (!TREE_CONSTANT(init))
12853 make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR,
12854 void_type_node, tmp, init);
12857 TREE_READONLY(tmp) = 1;
12858 TREE_CONSTANT(tmp) = 1;
12859 DECL_INITIAL(tmp) = init;
12860 make_tmp = NULL_TREE;
12862 rest_of_decl_compilation(tmp, 1, 0);
12865 valaddr = build_fold_addr_expr(tmp);
12868 tree descriptor = mt->map_descriptor_pointer(gogo, loc);
12870 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
12871 if (type_tree == error_mark_node)
12872 return error_mark_node;
12874 static tree construct_map_fndecl;
12875 tree call = Gogo::call_builtin(&construct_map_fndecl,
12877 "__go_construct_map",
12880 TREE_TYPE(descriptor),
12885 TYPE_SIZE_UNIT(struct_type),
12887 byte_position(val_field),
12889 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
12890 const_ptr_type_node,
12891 fold_convert(const_ptr_type_node, valaddr));
12892 if (call == error_mark_node)
12893 return error_mark_node;
12896 if (make_tmp == NULL)
12899 ret = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, type_tree,
12904 // Export an array construction.
12907 Map_construction_expression::do_export(Export* exp) const
12909 exp->write_c_string("convert(");
12910 exp->write_type(this->type_);
12911 for (Expression_list::const_iterator pv = this->vals_->begin();
12912 pv != this->vals_->end();
12915 exp->write_c_string(", ");
12916 (*pv)->export_expression(exp);
12918 exp->write_c_string(")");
12921 // Dump ast representation for a map construction expression.
12924 Map_construction_expression::do_dump_expression(
12925 Ast_dump_context* ast_dump_context) const
12927 ast_dump_context->ostream() << "{" ;
12928 ast_dump_context->dump_expression_list(this->vals_, true);
12929 ast_dump_context->ostream() << "}";
12932 // A general composite literal. This is lowered to a type specific
12935 class Composite_literal_expression : public Parser_expression
12938 Composite_literal_expression(Type* type, int depth, bool has_keys,
12939 Expression_list* vals, Location location)
12940 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
12941 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
12946 do_traverse(Traverse* traverse);
12949 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
12954 return new Composite_literal_expression(this->type_, this->depth_,
12956 (this->vals_ == NULL
12958 : this->vals_->copy()),
12963 do_dump_expression(Ast_dump_context*) const;
12967 lower_struct(Gogo*, Type*);
12970 lower_array(Type*);
12973 make_array(Type*, Expression_list*);
12976 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
12978 // The type of the composite literal.
12980 // The depth within a list of composite literals within a composite
12981 // literal, when the type is omitted.
12983 // The values to put in the composite literal.
12984 Expression_list* vals_;
12985 // If this is true, then VALS_ is a list of pairs: a key and a
12986 // value. In an array initializer, a missing key will be NULL.
12993 Composite_literal_expression::do_traverse(Traverse* traverse)
12995 if (this->vals_ != NULL
12996 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12997 return TRAVERSE_EXIT;
12998 return Type::traverse(this->type_, traverse);
13001 // Lower a generic composite literal into a specific version based on
13005 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
13006 Statement_inserter* inserter, int)
13008 Type* type = this->type_;
13010 for (int depth = this->depth_; depth > 0; --depth)
13012 if (type->array_type() != NULL)
13013 type = type->array_type()->element_type();
13014 else if (type->map_type() != NULL)
13015 type = type->map_type()->val_type();
13018 if (!type->is_error())
13019 error_at(this->location(),
13020 ("may only omit types within composite literals "
13021 "of slice, array, or map type"));
13022 return Expression::make_error(this->location());
13026 Type *pt = type->points_to();
13027 bool is_pointer = false;
13035 if (type->is_error())
13036 return Expression::make_error(this->location());
13037 else if (type->struct_type() != NULL)
13038 ret = this->lower_struct(gogo, type);
13039 else if (type->array_type() != NULL)
13040 ret = this->lower_array(type);
13041 else if (type->map_type() != NULL)
13042 ret = this->lower_map(gogo, function, inserter, type);
13045 error_at(this->location(),
13046 ("expected struct, slice, array, or map type "
13047 "for composite literal"));
13048 return Expression::make_error(this->location());
13052 ret = Expression::make_heap_composite(ret, this->location());
13057 // Lower a struct composite literal.
13060 Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
13062 Location location = this->location();
13063 Struct_type* st = type->struct_type();
13064 if (this->vals_ == NULL || !this->has_keys_)
13066 if (this->vals_ != NULL
13067 && !this->vals_->empty()
13068 && type->named_type() != NULL
13069 && type->named_type()->named_object()->package() != NULL)
13071 for (Struct_field_list::const_iterator pf = st->fields()->begin();
13072 pf != st->fields()->end();
13075 if (Gogo::is_hidden_name(pf->field_name()))
13076 error_at(this->location(),
13077 "assignment of unexported field %qs in %qs literal",
13078 Gogo::message_name(pf->field_name()).c_str(),
13079 type->named_type()->message_name().c_str());
13083 return new Struct_construction_expression(type, this->vals_, location);
13086 size_t field_count = st->field_count();
13087 std::vector<Expression*> vals(field_count);
13088 Expression_list::const_iterator p = this->vals_->begin();
13089 while (p != this->vals_->end())
13091 Expression* name_expr = *p;
13094 go_assert(p != this->vals_->end());
13095 Expression* val = *p;
13099 if (name_expr == NULL)
13101 error_at(val->location(), "mixture of field and value initializers");
13102 return Expression::make_error(location);
13105 bool bad_key = false;
13107 const Named_object* no = NULL;
13108 switch (name_expr->classification())
13110 case EXPRESSION_UNKNOWN_REFERENCE:
13111 name = name_expr->unknown_expression()->name();
13114 case EXPRESSION_CONST_REFERENCE:
13115 no = static_cast<Const_expression*>(name_expr)->named_object();
13118 case EXPRESSION_TYPE:
13120 Type* t = name_expr->type();
13121 Named_type* nt = t->named_type();
13125 no = nt->named_object();
13129 case EXPRESSION_VAR_REFERENCE:
13130 no = name_expr->var_expression()->named_object();
13133 case EXPRESSION_FUNC_REFERENCE:
13134 no = name_expr->func_expression()->named_object();
13137 case EXPRESSION_UNARY:
13138 // If there is a local variable around with the same name as
13139 // the field, and this occurs in the closure, then the
13140 // parser may turn the field reference into an indirection
13141 // through the closure. FIXME: This is a mess.
13144 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
13145 if (ue->op() == OPERATOR_MULT)
13147 Field_reference_expression* fre =
13148 ue->operand()->field_reference_expression();
13152 fre->expr()->type()->deref()->struct_type();
13155 const Struct_field* sf = st->field(fre->field_index());
13156 name = sf->field_name();
13158 // See below. FIXME.
13159 if (!Gogo::is_hidden_name(name)
13163 if (gogo->lookup_global(name.c_str()) != NULL)
13164 name = gogo->pack_hidden_name(name, false);
13168 snprintf(buf, sizeof buf, "%u", fre->field_index());
13169 size_t buflen = strlen(buf);
13170 if (name.compare(name.length() - buflen, buflen, buf)
13173 name = name.substr(0, name.length() - buflen);
13188 error_at(name_expr->location(), "expected struct field name");
13189 return Expression::make_error(location);
13196 // A predefined name won't be packed. If it starts with a
13197 // lower case letter we need to check for that case, because
13198 // the field name will be packed. FIXME.
13199 if (!Gogo::is_hidden_name(name)
13203 Named_object* gno = gogo->lookup_global(name.c_str());
13205 name = gogo->pack_hidden_name(name, false);
13209 unsigned int index;
13210 const Struct_field* sf = st->find_local_field(name, &index);
13213 error_at(name_expr->location(), "unknown field %qs in %qs",
13214 Gogo::message_name(name).c_str(),
13215 (type->named_type() != NULL
13216 ? type->named_type()->message_name().c_str()
13217 : "unnamed struct"));
13218 return Expression::make_error(location);
13220 if (vals[index] != NULL)
13222 error_at(name_expr->location(),
13223 "duplicate value for field %qs in %qs",
13224 Gogo::message_name(name).c_str(),
13225 (type->named_type() != NULL
13226 ? type->named_type()->message_name().c_str()
13227 : "unnamed struct"));
13228 return Expression::make_error(location);
13231 if (type->named_type() != NULL
13232 && type->named_type()->named_object()->package() != NULL
13233 && Gogo::is_hidden_name(sf->field_name()))
13234 error_at(name_expr->location(),
13235 "assignment of unexported field %qs in %qs literal",
13236 Gogo::message_name(sf->field_name()).c_str(),
13237 type->named_type()->message_name().c_str());
13242 Expression_list* list = new Expression_list;
13243 list->reserve(field_count);
13244 for (size_t i = 0; i < field_count; ++i)
13245 list->push_back(vals[i]);
13247 return new Struct_construction_expression(type, list, location);
13250 // Lower an array composite literal.
13253 Composite_literal_expression::lower_array(Type* type)
13255 Location location = this->location();
13256 if (this->vals_ == NULL || !this->has_keys_)
13257 return this->make_array(type, this->vals_);
13259 std::vector<Expression*> vals;
13260 vals.reserve(this->vals_->size());
13261 unsigned long index = 0;
13262 Expression_list::const_iterator p = this->vals_->begin();
13263 while (p != this->vals_->end())
13265 Expression* index_expr = *p;
13268 go_assert(p != this->vals_->end());
13269 Expression* val = *p;
13273 if (index_expr != NULL)
13279 if (!index_expr->integer_constant_value(true, ival, &dummy))
13282 error_at(index_expr->location(),
13283 "index expression is not integer constant");
13284 return Expression::make_error(location);
13287 if (mpz_sgn(ival) < 0)
13290 error_at(index_expr->location(), "index expression is negative");
13291 return Expression::make_error(location);
13294 index = mpz_get_ui(ival);
13295 if (mpz_cmp_ui(ival, index) != 0)
13298 error_at(index_expr->location(), "index value overflow");
13299 return Expression::make_error(location);
13302 Named_type* ntype = Type::lookup_integer_type("int");
13303 Integer_type* inttype = ntype->integer_type();
13305 mpz_init_set_ui(max, 1);
13306 mpz_mul_2exp(max, max, inttype->bits() - 1);
13307 bool ok = mpz_cmp(ival, max) < 0;
13312 error_at(index_expr->location(), "index value overflow");
13313 return Expression::make_error(location);
13318 // FIXME: Our representation isn't very good; this avoids
13320 if (index > 0x1000000)
13322 error_at(index_expr->location(), "index too large for compiler");
13323 return Expression::make_error(location);
13327 if (index == vals.size())
13328 vals.push_back(val);
13331 if (index > vals.size())
13333 vals.reserve(index + 32);
13334 vals.resize(index + 1, static_cast<Expression*>(NULL));
13336 if (vals[index] != NULL)
13338 error_at((index_expr != NULL
13339 ? index_expr->location()
13340 : val->location()),
13341 "duplicate value for index %lu",
13343 return Expression::make_error(location);
13351 size_t size = vals.size();
13352 Expression_list* list = new Expression_list;
13353 list->reserve(size);
13354 for (size_t i = 0; i < size; ++i)
13355 list->push_back(vals[i]);
13357 return this->make_array(type, list);
13360 // Actually build the array composite literal. This handles
13364 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
13366 Location location = this->location();
13367 Array_type* at = type->array_type();
13368 if (at->length() != NULL && at->length()->is_nil_expression())
13370 size_t size = vals == NULL ? 0 : vals->size();
13372 mpz_init_set_ui(vlen, size);
13373 Expression* elen = Expression::make_integer(&vlen, NULL, location);
13375 at = Type::make_array_type(at->element_type(), elen);
13378 if (at->length() != NULL)
13379 return new Fixed_array_construction_expression(type, vals, location);
13381 return new Open_array_construction_expression(type, vals, location);
13384 // Lower a map composite literal.
13387 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
13388 Statement_inserter* inserter,
13391 Location location = this->location();
13392 if (this->vals_ != NULL)
13394 if (!this->has_keys_)
13396 error_at(location, "map composite literal must have keys");
13397 return Expression::make_error(location);
13400 for (Expression_list::iterator p = this->vals_->begin();
13401 p != this->vals_->end();
13407 error_at((*p)->location(),
13408 "map composite literal must have keys for every value");
13409 return Expression::make_error(location);
13411 // Make sure we have lowered the key; it may not have been
13412 // lowered in order to handle keys for struct composite
13413 // literals. Lower it now to get the right error message.
13414 if ((*p)->unknown_expression() != NULL)
13416 (*p)->unknown_expression()->clear_is_composite_literal_key();
13417 gogo->lower_expression(function, inserter, &*p);
13418 go_assert((*p)->is_error_expression());
13419 return Expression::make_error(location);
13424 return new Map_construction_expression(type, this->vals_, location);
13427 // Dump ast representation for a composite literal expression.
13430 Composite_literal_expression::do_dump_expression(
13431 Ast_dump_context* ast_dump_context) const
13433 ast_dump_context->ostream() << "composite(";
13434 ast_dump_context->dump_type(this->type_);
13435 ast_dump_context->ostream() << ", {";
13436 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_);
13437 ast_dump_context->ostream() << "})";
13440 // Make a composite literal expression.
13443 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
13444 Expression_list* vals,
13447 return new Composite_literal_expression(type, depth, has_keys, vals,
13451 // Return whether this expression is a composite literal.
13454 Expression::is_composite_literal() const
13456 switch (this->classification_)
13458 case EXPRESSION_COMPOSITE_LITERAL:
13459 case EXPRESSION_STRUCT_CONSTRUCTION:
13460 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
13461 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
13462 case EXPRESSION_MAP_CONSTRUCTION:
13469 // Return whether this expression is a composite literal which is not
13473 Expression::is_nonconstant_composite_literal() const
13475 switch (this->classification_)
13477 case EXPRESSION_STRUCT_CONSTRUCTION:
13479 const Struct_construction_expression *psce =
13480 static_cast<const Struct_construction_expression*>(this);
13481 return !psce->is_constant_struct();
13483 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
13485 const Fixed_array_construction_expression *pace =
13486 static_cast<const Fixed_array_construction_expression*>(this);
13487 return !pace->is_constant_array();
13489 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
13491 const Open_array_construction_expression *pace =
13492 static_cast<const Open_array_construction_expression*>(this);
13493 return !pace->is_constant_array();
13495 case EXPRESSION_MAP_CONSTRUCTION:
13502 // Return true if this is a reference to a local variable.
13505 Expression::is_local_variable() const
13507 const Var_expression* ve = this->var_expression();
13510 const Named_object* no = ve->named_object();
13511 return (no->is_result_variable()
13512 || (no->is_variable() && !no->var_value()->is_global()));
13515 // Class Type_guard_expression.
13520 Type_guard_expression::do_traverse(Traverse* traverse)
13522 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
13523 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
13524 return TRAVERSE_EXIT;
13525 return TRAVERSE_CONTINUE;
13528 // Check types of a type guard expression. The expression must have
13529 // an interface type, but the actual type conversion is checked at run
13533 Type_guard_expression::do_check_types(Gogo*)
13535 // 6g permits using a type guard with unsafe.pointer; we are
13537 Type* expr_type = this->expr_->type();
13538 if (expr_type->is_unsafe_pointer_type())
13540 if (this->type_->points_to() == NULL
13541 && (this->type_->integer_type() == NULL
13542 || (this->type_->forwarded()
13543 != Type::lookup_integer_type("uintptr"))))
13544 this->report_error(_("invalid unsafe.Pointer conversion"));
13546 else if (this->type_->is_unsafe_pointer_type())
13548 if (expr_type->points_to() == NULL
13549 && (expr_type->integer_type() == NULL
13550 || (expr_type->forwarded()
13551 != Type::lookup_integer_type("uintptr"))))
13552 this->report_error(_("invalid unsafe.Pointer conversion"));
13554 else if (expr_type->interface_type() == NULL)
13556 if (!expr_type->is_error() && !this->type_->is_error())
13557 this->report_error(_("type assertion only valid for interface types"));
13558 this->set_is_error();
13560 else if (this->type_->interface_type() == NULL)
13562 std::string reason;
13563 if (!expr_type->interface_type()->implements_interface(this->type_,
13566 if (!this->type_->is_error())
13568 if (reason.empty())
13569 this->report_error(_("impossible type assertion: "
13570 "type does not implement interface"));
13572 error_at(this->location(),
13573 ("impossible type assertion: "
13574 "type does not implement interface (%s)"),
13577 this->set_is_error();
13582 // Return a tree for a type guard expression.
13585 Type_guard_expression::do_get_tree(Translate_context* context)
13587 Gogo* gogo = context->gogo();
13588 tree expr_tree = this->expr_->get_tree(context);
13589 if (expr_tree == error_mark_node)
13590 return error_mark_node;
13591 Type* expr_type = this->expr_->type();
13592 if ((this->type_->is_unsafe_pointer_type()
13593 && (expr_type->points_to() != NULL
13594 || expr_type->integer_type() != NULL))
13595 || (expr_type->is_unsafe_pointer_type()
13596 && this->type_->points_to() != NULL))
13597 return convert_to_pointer(type_to_tree(this->type_->get_backend(gogo)),
13599 else if (expr_type->is_unsafe_pointer_type()
13600 && this->type_->integer_type() != NULL)
13601 return convert_to_integer(type_to_tree(this->type_->get_backend(gogo)),
13603 else if (this->type_->interface_type() != NULL)
13604 return Expression::convert_interface_to_interface(context, this->type_,
13605 this->expr_->type(),
13609 return Expression::convert_for_assignment(context, this->type_,
13610 this->expr_->type(), expr_tree,
13614 // Dump ast representation for a type guard expression.
13617 Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
13620 this->expr_->dump_expression(ast_dump_context);
13621 ast_dump_context->ostream() << ".";
13622 ast_dump_context->dump_type(this->type_);
13625 // Make a type guard expression.
13628 Expression::make_type_guard(Expression* expr, Type* type,
13631 return new Type_guard_expression(expr, type, location);
13634 // Class Heap_composite_expression.
13636 // When you take the address of a composite literal, it is allocated
13637 // on the heap. This class implements that.
13639 class Heap_composite_expression : public Expression
13642 Heap_composite_expression(Expression* expr, Location location)
13643 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
13649 do_traverse(Traverse* traverse)
13650 { return Expression::traverse(&this->expr_, traverse); }
13654 { return Type::make_pointer_type(this->expr_->type()); }
13657 do_determine_type(const Type_context*)
13658 { this->expr_->determine_type_no_context(); }
13663 return Expression::make_heap_composite(this->expr_->copy(),
13668 do_get_tree(Translate_context*);
13670 // We only export global objects, and the parser does not generate
13671 // this in global scope.
13673 do_export(Export*) const
13674 { go_unreachable(); }
13677 do_dump_expression(Ast_dump_context*) const;
13680 // The composite literal which is being put on the heap.
13684 // Return a tree which allocates a composite literal on the heap.
13687 Heap_composite_expression::do_get_tree(Translate_context* context)
13689 tree expr_tree = this->expr_->get_tree(context);
13690 if (expr_tree == error_mark_node)
13691 return error_mark_node;
13692 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
13693 go_assert(TREE_CODE(expr_size) == INTEGER_CST);
13694 tree space = context->gogo()->allocate_memory(this->expr_->type(),
13695 expr_size, this->location());
13696 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
13697 space = save_expr(space);
13698 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
13700 TREE_THIS_NOTRAP(ref) = 1;
13701 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
13702 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
13704 SET_EXPR_LOCATION(ret, this->location().gcc_location());
13708 // Dump ast representation for a heap composite expression.
13711 Heap_composite_expression::do_dump_expression(
13712 Ast_dump_context* ast_dump_context) const
13714 ast_dump_context->ostream() << "&(";
13715 ast_dump_context->dump_expression(this->expr_);
13716 ast_dump_context->ostream() << ")";
13719 // Allocate a composite literal on the heap.
13722 Expression::make_heap_composite(Expression* expr, Location location)
13724 return new Heap_composite_expression(expr, location);
13727 // Class Receive_expression.
13729 // Return the type of a receive expression.
13732 Receive_expression::do_type()
13734 Channel_type* channel_type = this->channel_->type()->channel_type();
13735 if (channel_type == NULL)
13736 return Type::make_error_type();
13737 return channel_type->element_type();
13740 // Check types for a receive expression.
13743 Receive_expression::do_check_types(Gogo*)
13745 Type* type = this->channel_->type();
13746 if (type->is_error())
13748 this->set_is_error();
13751 if (type->channel_type() == NULL)
13753 this->report_error(_("expected channel"));
13756 if (!type->channel_type()->may_receive())
13758 this->report_error(_("invalid receive on send-only channel"));
13763 // Get a tree for a receive expression.
13766 Receive_expression::do_get_tree(Translate_context* context)
13768 Location loc = this->location();
13770 Channel_type* channel_type = this->channel_->type()->channel_type();
13771 if (channel_type == NULL)
13773 go_assert(this->channel_->type()->is_error());
13774 return error_mark_node;
13777 Expression* td = Expression::make_type_descriptor(channel_type, loc);
13778 tree td_tree = td->get_tree(context);
13780 Type* element_type = channel_type->element_type();
13781 Btype* element_type_btype = element_type->get_backend(context->gogo());
13782 tree element_type_tree = type_to_tree(element_type_btype);
13784 tree channel = this->channel_->get_tree(context);
13785 if (element_type_tree == error_mark_node || channel == error_mark_node)
13786 return error_mark_node;
13788 return Gogo::receive_from_channel(element_type_tree, td_tree, channel, loc);
13791 // Dump ast representation for a receive expression.
13794 Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
13796 ast_dump_context->ostream() << " <- " ;
13797 ast_dump_context->dump_expression(channel_);
13800 // Make a receive expression.
13802 Receive_expression*
13803 Expression::make_receive(Expression* channel, Location location)
13805 return new Receive_expression(channel, location);
13808 // An expression which evaluates to a pointer to the type descriptor
13811 class Type_descriptor_expression : public Expression
13814 Type_descriptor_expression(Type* type, Location location)
13815 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
13822 { return Type::make_type_descriptor_ptr_type(); }
13825 do_determine_type(const Type_context*)
13833 do_get_tree(Translate_context* context)
13835 return this->type_->type_descriptor_pointer(context->gogo(),
13840 do_dump_expression(Ast_dump_context*) const;
13843 // The type for which this is the descriptor.
13847 // Dump ast representation for a type descriptor expression.
13850 Type_descriptor_expression::do_dump_expression(
13851 Ast_dump_context* ast_dump_context) const
13853 ast_dump_context->dump_type(this->type_);
13856 // Make a type descriptor expression.
13859 Expression::make_type_descriptor(Type* type, Location location)
13861 return new Type_descriptor_expression(type, location);
13864 // An expression which evaluates to some characteristic of a type.
13865 // This is only used to initialize fields of a type descriptor. Using
13866 // a new expression class is slightly inefficient but gives us a good
13867 // separation between the frontend and the middle-end with regard to
13868 // how types are laid out.
13870 class Type_info_expression : public Expression
13873 Type_info_expression(Type* type, Type_info type_info)
13874 : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()),
13875 type_(type), type_info_(type_info)
13883 do_determine_type(const Type_context*)
13891 do_get_tree(Translate_context* context);
13894 do_dump_expression(Ast_dump_context*) const;
13897 // The type for which we are getting information.
13899 // What information we want.
13900 Type_info type_info_;
13903 // The type is chosen to match what the type descriptor struct
13907 Type_info_expression::do_type()
13909 switch (this->type_info_)
13911 case TYPE_INFO_SIZE:
13912 return Type::lookup_integer_type("uintptr");
13913 case TYPE_INFO_ALIGNMENT:
13914 case TYPE_INFO_FIELD_ALIGNMENT:
13915 return Type::lookup_integer_type("uint8");
13921 // Return type information in GENERIC.
13924 Type_info_expression::do_get_tree(Translate_context* context)
13926 Btype* btype = this->type_->get_backend(context->gogo());
13927 Gogo* gogo = context->gogo();
13929 switch (this->type_info_)
13931 case TYPE_INFO_SIZE:
13932 val = gogo->backend()->type_size(btype);
13934 case TYPE_INFO_ALIGNMENT:
13935 val = gogo->backend()->type_alignment(btype);
13937 case TYPE_INFO_FIELD_ALIGNMENT:
13938 val = gogo->backend()->type_field_alignment(btype);
13943 tree val_type_tree = type_to_tree(this->type()->get_backend(gogo));
13944 go_assert(val_type_tree != error_mark_node);
13945 return build_int_cstu(val_type_tree, val);
13948 // Dump ast representation for a type info expression.
13951 Type_info_expression::do_dump_expression(
13952 Ast_dump_context* ast_dump_context) const
13954 ast_dump_context->ostream() << "typeinfo(";
13955 ast_dump_context->dump_type(this->type_);
13956 ast_dump_context->ostream() << ",";
13957 ast_dump_context->ostream() <<
13958 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment"
13959 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment"
13960 : this->type_info_ == TYPE_INFO_SIZE ? "size "
13962 ast_dump_context->ostream() << ")";
13965 // Make a type info expression.
13968 Expression::make_type_info(Type* type, Type_info type_info)
13970 return new Type_info_expression(type, type_info);
13973 // An expression which evaluates to the offset of a field within a
13974 // struct. This, like Type_info_expression, q.v., is only used to
13975 // initialize fields of a type descriptor.
13977 class Struct_field_offset_expression : public Expression
13980 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
13981 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET,
13982 Linemap::predeclared_location()),
13983 type_(type), field_(field)
13989 { return Type::lookup_integer_type("uintptr"); }
13992 do_determine_type(const Type_context*)
14000 do_get_tree(Translate_context* context);
14003 do_dump_expression(Ast_dump_context*) const;
14006 // The type of the struct.
14007 Struct_type* type_;
14009 const Struct_field* field_;
14012 // Return a struct field offset in GENERIC.
14015 Struct_field_offset_expression::do_get_tree(Translate_context* context)
14017 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
14018 if (type_tree == error_mark_node)
14019 return error_mark_node;
14021 tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
14022 go_assert(val_type_tree != error_mark_node);
14024 const Struct_field_list* fields = this->type_->fields();
14025 tree struct_field_tree = TYPE_FIELDS(type_tree);
14026 Struct_field_list::const_iterator p;
14027 for (p = fields->begin();
14028 p != fields->end();
14029 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
14031 go_assert(struct_field_tree != NULL_TREE);
14032 if (&*p == this->field_)
14035 go_assert(&*p == this->field_);
14037 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
14038 byte_position(struct_field_tree));
14041 // Dump ast representation for a struct field offset expression.
14044 Struct_field_offset_expression::do_dump_expression(
14045 Ast_dump_context* ast_dump_context) const
14047 ast_dump_context->ostream() << "unsafe.Offsetof(";
14048 ast_dump_context->dump_type(this->type_);
14049 ast_dump_context->ostream() << '.';
14050 ast_dump_context->ostream() <<
14051 Gogo::message_name(this->field_->field_name());
14052 ast_dump_context->ostream() << ")";
14055 // Make an expression for a struct field offset.
14058 Expression::make_struct_field_offset(Struct_type* type,
14059 const Struct_field* field)
14061 return new Struct_field_offset_expression(type, field);
14064 // An expression which evaluates to a pointer to the map descriptor of
14067 class Map_descriptor_expression : public Expression
14070 Map_descriptor_expression(Map_type* type, Location location)
14071 : Expression(EXPRESSION_MAP_DESCRIPTOR, location),
14078 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
14081 do_determine_type(const Type_context*)
14089 do_get_tree(Translate_context* context)
14091 return this->type_->map_descriptor_pointer(context->gogo(),
14096 do_dump_expression(Ast_dump_context*) const;
14099 // The type for which this is the descriptor.
14103 // Dump ast representation for a map descriptor expression.
14106 Map_descriptor_expression::do_dump_expression(
14107 Ast_dump_context* ast_dump_context) const
14109 ast_dump_context->ostream() << "map_descriptor(";
14110 ast_dump_context->dump_type(this->type_);
14111 ast_dump_context->ostream() << ")";
14114 // Make a map descriptor expression.
14117 Expression::make_map_descriptor(Map_type* type, Location location)
14119 return new Map_descriptor_expression(type, location);
14122 // An expression which evaluates to the address of an unnamed label.
14124 class Label_addr_expression : public Expression
14127 Label_addr_expression(Label* label, Location location)
14128 : Expression(EXPRESSION_LABEL_ADDR, location),
14135 { return Type::make_pointer_type(Type::make_void_type()); }
14138 do_determine_type(const Type_context*)
14143 { return new Label_addr_expression(this->label_, this->location()); }
14146 do_get_tree(Translate_context* context)
14148 return expr_to_tree(this->label_->get_addr(context, this->location()));
14152 do_dump_expression(Ast_dump_context* ast_dump_context) const
14153 { ast_dump_context->ostream() << this->label_->name(); }
14156 // The label whose address we are taking.
14160 // Make an expression for the address of an unnamed label.
14163 Expression::make_label_addr(Label* label, Location location)
14165 return new Label_addr_expression(label, location);
14168 // Import an expression. This comes at the end in order to see the
14169 // various class definitions.
14172 Expression::import_expression(Import* imp)
14174 int c = imp->peek_char();
14175 if (imp->match_c_string("- ")
14176 || imp->match_c_string("! ")
14177 || imp->match_c_string("^ "))
14178 return Unary_expression::do_import(imp);
14180 return Binary_expression::do_import(imp);
14181 else if (imp->match_c_string("true")
14182 || imp->match_c_string("false"))
14183 return Boolean_expression::do_import(imp);
14185 return String_expression::do_import(imp);
14186 else if (c == '-' || (c >= '0' && c <= '9'))
14188 // This handles integers, floats and complex constants.
14189 return Integer_expression::do_import(imp);
14191 else if (imp->match_c_string("nil"))
14192 return Nil_expression::do_import(imp);
14193 else if (imp->match_c_string("convert"))
14194 return Type_conversion_expression::do_import(imp);
14197 error_at(imp->location(), "import error: expected expression");
14198 return Expression::make_error(imp->location());
14202 // Class Expression_list.
14204 // Traverse the list.
14207 Expression_list::traverse(Traverse* traverse)
14209 for (Expression_list::iterator p = this->begin();
14215 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
14216 return TRAVERSE_EXIT;
14219 return TRAVERSE_CONTINUE;
14225 Expression_list::copy()
14227 Expression_list* ret = new Expression_list();
14228 for (Expression_list::iterator p = this->begin();
14233 ret->push_back(NULL);
14235 ret->push_back((*p)->copy());
14240 // Return whether an expression list has an error expression.
14243 Expression_list::contains_error() const
14245 for (Expression_list::const_iterator p = this->begin();
14248 if (*p != NULL && (*p)->is_error_expression())