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,
44 source_location location)
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 warn. Expressions
139 // with side effects override.
142 Expression::do_discarding_value()
144 this->warn_about_unused_value();
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 // Warn that the value of the expression is not used.
159 Expression::warn_about_unused_value()
161 warning_at(this->location(), OPT_Wunused_value, "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,
206 source_location location)
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_open_array_type()
238 && rhs_type->is_nil_type())
240 // Assigning nil to an open array.
241 go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
243 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
245 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
246 tree field = TYPE_FIELDS(lhs_type_tree);
247 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
250 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
252 elt = VEC_quick_push(constructor_elt, init, NULL);
253 field = DECL_CHAIN(field);
254 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
257 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
259 elt = VEC_quick_push(constructor_elt, init, NULL);
260 field = DECL_CHAIN(field);
261 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
264 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
266 tree val = build_constructor(lhs_type_tree, init);
267 TREE_CONSTANT(val) = 1;
271 else if (rhs_type->is_nil_type())
273 // The left hand side should be a pointer type at the tree
275 go_assert(POINTER_TYPE_P(lhs_type_tree));
276 return fold_convert(lhs_type_tree, null_pointer_node);
278 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
280 // No conversion is needed.
283 else if (POINTER_TYPE_P(lhs_type_tree)
284 || INTEGRAL_TYPE_P(lhs_type_tree)
285 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
286 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
287 return fold_convert_loc(location, lhs_type_tree, rhs_tree);
288 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
289 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
291 // This conversion must be permitted by Go, or we wouldn't have
293 go_assert(int_size_in_bytes(lhs_type_tree)
294 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
295 return fold_build1_loc(location, VIEW_CONVERT_EXPR, lhs_type_tree,
300 go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
305 // Return a tree for a conversion from a non-interface type to an
309 Expression::convert_type_to_interface(Translate_context* context,
310 Type* lhs_type, Type* rhs_type,
311 tree rhs_tree, source_location location)
313 Gogo* gogo = context->gogo();
314 Interface_type* lhs_interface_type = lhs_type->interface_type();
315 bool lhs_is_empty = lhs_interface_type->is_empty();
317 // Since RHS_TYPE is a static type, we can create the interface
318 // method table at compile time.
320 // When setting an interface to nil, we just set both fields to
322 if (rhs_type->is_nil_type())
324 Btype* lhs_btype = lhs_type->get_backend(gogo);
325 return expr_to_tree(gogo->backend()->zero_expression(lhs_btype));
328 // This should have been checked already.
329 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
331 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
332 if (lhs_type_tree == error_mark_node)
333 return error_mark_node;
335 // An interface is a tuple. If LHS_TYPE is an empty interface type,
336 // then the first field is the type descriptor for RHS_TYPE.
337 // Otherwise it is the interface method table for RHS_TYPE.
338 tree first_field_value;
340 first_field_value = rhs_type->type_descriptor_pointer(gogo, location);
343 // Build the interface method table for this interface and this
344 // object type: a list of function pointers for each interface
346 Named_type* rhs_named_type = rhs_type->named_type();
347 bool is_pointer = false;
348 if (rhs_named_type == NULL)
350 rhs_named_type = rhs_type->deref()->named_type();
354 if (rhs_named_type == NULL)
355 method_table = null_pointer_node;
358 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
360 first_field_value = fold_convert_loc(location, const_ptr_type_node,
363 if (first_field_value == error_mark_node)
364 return error_mark_node;
366 // Start building a constructor for the value we will return.
368 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
370 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
371 tree field = TYPE_FIELDS(lhs_type_tree);
372 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
373 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
375 elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value);
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, build_pointer_type(TREE_TYPE(rhs_tree)),
398 space = save_expr(space);
400 tree ref = build_fold_indirect_ref_loc(location, space);
401 TREE_THIS_NOTRAP(ref) = 1;
402 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
405 elt->value = fold_convert_loc(location, TREE_TYPE(field), space);
407 return build2(COMPOUND_EXPR, lhs_type_tree, set,
408 build_constructor(lhs_type_tree, init));
411 // Return a tree for the type descriptor of RHS_TREE, which has
412 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
416 Expression::get_interface_type_descriptor(Translate_context*,
417 Type* rhs_type, tree rhs_tree,
418 source_location location)
420 tree rhs_type_tree = TREE_TYPE(rhs_tree);
421 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
422 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
423 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
425 if (rhs_type->interface_type()->is_empty())
427 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
428 "__type_descriptor") == 0);
432 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
434 go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
436 tree v1 = build_fold_indirect_ref_loc(location, v);
437 go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
438 tree f = TYPE_FIELDS(TREE_TYPE(v1));
439 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
441 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
443 tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v,
444 fold_convert_loc(location, TREE_TYPE(v),
446 tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node);
447 return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1),
451 // Return a tree for the conversion of an interface type to an
455 Expression::convert_interface_to_interface(Translate_context* context,
456 Type *lhs_type, Type *rhs_type,
457 tree rhs_tree, bool for_type_guard,
458 source_location location)
460 Gogo* gogo = context->gogo();
461 Interface_type* lhs_interface_type = lhs_type->interface_type();
462 bool lhs_is_empty = lhs_interface_type->is_empty();
464 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
465 if (lhs_type_tree == error_mark_node)
466 return error_mark_node;
468 // In the general case this requires runtime examination of the type
469 // method table to match it up with the interface methods.
471 // FIXME: If all of the methods in the right hand side interface
472 // also appear in the left hand side interface, then we don't need
473 // to do a runtime check, although we still need to build a new
476 // Get the type descriptor for the right hand side. This will be
477 // NULL for a nil interface.
479 if (!DECL_P(rhs_tree))
480 rhs_tree = save_expr(rhs_tree);
482 tree rhs_type_descriptor =
483 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
486 // The result is going to be a two element constructor.
488 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
490 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
491 tree field = TYPE_FIELDS(lhs_type_tree);
496 // A type assertion fails when converting a nil interface.
497 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
499 static tree assert_interface_decl;
500 tree call = Gogo::call_builtin(&assert_interface_decl,
502 "__go_assert_interface",
505 TREE_TYPE(lhs_type_descriptor),
507 TREE_TYPE(rhs_type_descriptor),
508 rhs_type_descriptor);
509 if (call == error_mark_node)
510 return error_mark_node;
511 // This will panic if the interface conversion fails.
512 TREE_NOTHROW(assert_interface_decl) = 0;
513 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
515 else if (lhs_is_empty)
517 // A convertion to an empty interface always succeeds, and the
518 // first field is just the type descriptor of the object.
519 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
520 "__type_descriptor") == 0);
521 go_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
522 elt->value = rhs_type_descriptor;
526 // A conversion to a non-empty interface may fail, but unlike a
527 // type assertion converting nil will always succeed.
528 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
530 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
532 static tree convert_interface_decl;
533 tree call = Gogo::call_builtin(&convert_interface_decl,
535 "__go_convert_interface",
538 TREE_TYPE(lhs_type_descriptor),
540 TREE_TYPE(rhs_type_descriptor),
541 rhs_type_descriptor);
542 if (call == error_mark_node)
543 return error_mark_node;
544 // This will panic if the interface conversion fails.
545 TREE_NOTHROW(convert_interface_decl) = 0;
546 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
549 // The second field is simply the object pointer.
551 elt = VEC_quick_push(constructor_elt, init, NULL);
552 field = DECL_CHAIN(field);
553 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
556 tree rhs_type_tree = TREE_TYPE(rhs_tree);
557 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
558 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
559 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
560 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
563 return build_constructor(lhs_type_tree, init);
566 // Return a tree for the conversion of an interface type to a
567 // non-interface type.
570 Expression::convert_interface_to_type(Translate_context* context,
571 Type *lhs_type, Type* rhs_type,
572 tree rhs_tree, source_location location)
574 Gogo* gogo = context->gogo();
575 tree rhs_type_tree = TREE_TYPE(rhs_tree);
577 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
578 if (lhs_type_tree == error_mark_node)
579 return error_mark_node;
581 // Call a function to check that the type is valid. The function
582 // will panic with an appropriate runtime type error if the type is
585 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location);
587 if (!DECL_P(rhs_tree))
588 rhs_tree = save_expr(rhs_tree);
590 tree rhs_type_descriptor =
591 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
594 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo,
597 static tree check_interface_type_decl;
598 tree call = Gogo::call_builtin(&check_interface_type_decl,
600 "__go_check_interface_type",
603 TREE_TYPE(lhs_type_descriptor),
605 TREE_TYPE(rhs_type_descriptor),
607 TREE_TYPE(rhs_inter_descriptor),
608 rhs_inter_descriptor);
609 if (call == error_mark_node)
610 return error_mark_node;
611 // This call will panic if the conversion is invalid.
612 TREE_NOTHROW(check_interface_type_decl) = 0;
614 // If the call succeeds, pull out the value.
615 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
616 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
617 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
618 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
621 // If the value is a pointer, then it is the value we want.
622 // Otherwise it points to the value.
623 if (lhs_type->points_to() == NULL)
625 val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val);
626 val = build_fold_indirect_ref_loc(location, val);
629 return build2(COMPOUND_EXPR, lhs_type_tree, call,
630 fold_convert_loc(location, lhs_type_tree, val));
633 // Convert an expression to a tree. This is implemented by the child
634 // class. Not that it is not in general safe to call this multiple
635 // times for a single expression, but that we don't catch such errors.
638 Expression::get_tree(Translate_context* context)
640 // The child may have marked this expression as having an error.
641 if (this->classification_ == EXPRESSION_ERROR)
642 return error_mark_node;
644 return this->do_get_tree(context);
647 // Return a tree for VAL in TYPE.
650 Expression::integer_constant_tree(mpz_t val, tree type)
652 if (type == error_mark_node)
653 return error_mark_node;
654 else if (TREE_CODE(type) == INTEGER_TYPE)
655 return double_int_to_tree(type,
656 mpz_get_double_int(type, val, true));
657 else if (TREE_CODE(type) == REAL_TYPE)
660 mpfr_init_set_z(fval, val, GMP_RNDN);
661 tree ret = Expression::float_constant_tree(fval, type);
665 else if (TREE_CODE(type) == COMPLEX_TYPE)
668 mpfr_init_set_z(fval, val, GMP_RNDN);
669 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
671 tree imag = build_real_from_int_cst(TREE_TYPE(type),
673 return build_complex(type, real, imag);
679 // Return a tree for VAL in TYPE.
682 Expression::float_constant_tree(mpfr_t val, tree type)
684 if (type == error_mark_node)
685 return error_mark_node;
686 else if (TREE_CODE(type) == INTEGER_TYPE)
690 mpfr_get_z(ival, val, GMP_RNDN);
691 tree ret = Expression::integer_constant_tree(ival, type);
695 else if (TREE_CODE(type) == REAL_TYPE)
698 real_from_mpfr(&r1, val, type, GMP_RNDN);
700 real_convert(&r2, TYPE_MODE(type), &r1);
701 return build_real(type, r2);
703 else if (TREE_CODE(type) == COMPLEX_TYPE)
706 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
708 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
709 tree imag = build_real_from_int_cst(TREE_TYPE(type),
711 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
717 // Return a tree for REAL/IMAG in TYPE.
720 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
722 if (type == error_mark_node)
723 return error_mark_node;
724 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
725 return Expression::float_constant_tree(real, type);
726 else if (TREE_CODE(type) == COMPLEX_TYPE)
729 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
731 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
734 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
736 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
738 return build_complex(type, build_real(TREE_TYPE(type), r2),
739 build_real(TREE_TYPE(type), r4));
745 // Return a tree which evaluates to true if VAL, of arbitrary integer
746 // type, is negative or is more than the maximum value of BOUND_TYPE.
747 // If SOFAR is not NULL, it is or'red into the result. The return
748 // value may be NULL if SOFAR is NULL.
751 Expression::check_bounds(tree val, tree bound_type, tree sofar,
754 tree val_type = TREE_TYPE(val);
755 tree ret = NULL_TREE;
757 if (!TYPE_UNSIGNED(val_type))
759 ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val,
760 build_int_cst(val_type, 0));
761 if (ret == boolean_false_node)
765 HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type);
766 HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type);
767 go_assert(val_type_size != -1 && bound_type_size != -1);
768 if (val_type_size > bound_type_size
769 || (val_type_size == bound_type_size
770 && TYPE_UNSIGNED(val_type)
771 && !TYPE_UNSIGNED(bound_type)))
773 tree max = TYPE_MAX_VALUE(bound_type);
774 tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val,
775 fold_convert_loc(loc, val_type, max));
776 if (big == boolean_false_node)
778 else if (ret == NULL_TREE)
781 ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
785 if (ret == NULL_TREE)
787 else if (sofar == NULL_TREE)
790 return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
795 Expression::dump_expression(Ast_dump_context* ast_dump_context) const
797 this->do_dump_expression(ast_dump_context);
800 // Error expressions. This are used to avoid cascading errors.
802 class Error_expression : public Expression
805 Error_expression(source_location location)
806 : Expression(EXPRESSION_ERROR, location)
811 do_is_constant() const
815 do_integer_constant_value(bool, mpz_t val, Type**) const
822 do_float_constant_value(mpfr_t val, Type**) const
824 mpfr_set_ui(val, 0, GMP_RNDN);
829 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
831 mpfr_set_ui(real, 0, GMP_RNDN);
832 mpfr_set_ui(imag, 0, GMP_RNDN);
837 do_discarding_value()
842 { return Type::make_error_type(); }
845 do_determine_type(const Type_context*)
853 do_is_addressable() const
857 do_get_tree(Translate_context*)
858 { return error_mark_node; }
861 do_dump_expression(Ast_dump_context*) const;
864 // Dump the ast representation for an error expression to a dump context.
867 Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
869 ast_dump_context->ostream() << "_Error_" ;
873 Expression::make_error(source_location location)
875 return new Error_expression(location);
878 // An expression which is really a type. This is used during parsing.
879 // It is an error if these survive after lowering.
882 Type_expression : public Expression
885 Type_expression(Type* type, source_location location)
886 : Expression(EXPRESSION_TYPE, location),
892 do_traverse(Traverse* traverse)
893 { return Type::traverse(this->type_, traverse); }
897 { return this->type_; }
900 do_determine_type(const Type_context*)
904 do_check_types(Gogo*)
905 { this->report_error(_("invalid use of type")); }
912 do_get_tree(Translate_context*)
913 { go_unreachable(); }
915 void do_dump_expression(Ast_dump_context*) const;
918 // The type which we are representing as an expression.
923 Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
925 ast_dump_context->dump_type(this->type_);
929 Expression::make_type(Type* type, source_location location)
931 return new Type_expression(type, location);
934 // Class Parser_expression.
937 Parser_expression::do_type()
939 // We should never really ask for the type of a Parser_expression.
940 // However, it can happen, at least when we have an invalid const
941 // whose initializer refers to the const itself. In that case we
942 // may ask for the type when lowering the const itself.
943 go_assert(saw_errors());
944 return Type::make_error_type();
947 // Class Var_expression.
949 // Lower a variable expression. Here we just make sure that the
950 // initialization expression of the variable has been lowered. This
951 // ensures that we will be able to determine the type of the variable
955 Var_expression::do_lower(Gogo* gogo, Named_object* function,
956 Statement_inserter* inserter, int)
958 if (this->variable_->is_variable())
960 Variable* var = this->variable_->var_value();
961 // This is either a local variable or a global variable. A
962 // reference to a variable which is local to an enclosing
963 // function will be a reference to a field in a closure.
964 if (var->is_global())
969 var->lower_init_expression(gogo, function, inserter);
974 // Return the type of a reference to a variable.
977 Var_expression::do_type()
979 if (this->variable_->is_variable())
980 return this->variable_->var_value()->type();
981 else if (this->variable_->is_result_variable())
982 return this->variable_->result_var_value()->type();
987 // Determine the type of a reference to a variable.
990 Var_expression::do_determine_type(const Type_context*)
992 if (this->variable_->is_variable())
993 this->variable_->var_value()->determine_type();
996 // Something takes the address of this variable. This means that we
997 // may want to move the variable onto the heap.
1000 Var_expression::do_address_taken(bool escapes)
1004 if (this->variable_->is_variable())
1005 this->variable_->var_value()->set_non_escaping_address_taken();
1006 else if (this->variable_->is_result_variable())
1007 this->variable_->result_var_value()->set_non_escaping_address_taken();
1013 if (this->variable_->is_variable())
1014 this->variable_->var_value()->set_address_taken();
1015 else if (this->variable_->is_result_variable())
1016 this->variable_->result_var_value()->set_address_taken();
1022 // Get the tree for a reference to a variable.
1025 Var_expression::do_get_tree(Translate_context* context)
1027 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
1028 context->function());
1029 tree ret = var_to_tree(bvar);
1030 if (ret == error_mark_node)
1031 return error_mark_node;
1033 if (this->variable_->is_variable())
1034 is_in_heap = this->variable_->var_value()->is_in_heap();
1035 else if (this->variable_->is_result_variable())
1036 is_in_heap = this->variable_->result_var_value()->is_in_heap();
1041 ret = build_fold_indirect_ref_loc(this->location(), ret);
1042 TREE_THIS_NOTRAP(ret) = 1;
1047 // Ast dump for variable expression.
1050 Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1052 ast_dump_context->ostream() << this->variable_->name() ;
1055 // Make a reference to a variable in an expression.
1058 Expression::make_var_reference(Named_object* var, source_location location)
1061 return Expression::make_sink(location);
1063 // FIXME: Creating a new object for each reference to a variable is
1065 return new Var_expression(var, location);
1068 // Class Temporary_reference_expression.
1073 Temporary_reference_expression::do_type()
1075 return this->statement_->type();
1078 // Called if something takes the address of this temporary variable.
1079 // We never have to move temporary variables to the heap, but we do
1080 // need to know that they must live in the stack rather than in a
1084 Temporary_reference_expression::do_address_taken(bool)
1086 this->statement_->set_is_address_taken();
1089 // Get a tree referring to the variable.
1092 Temporary_reference_expression::do_get_tree(Translate_context* context)
1094 Bvariable* bvar = this->statement_->get_backend_variable(context);
1096 // The gcc backend can't represent the same set of recursive types
1097 // that the Go frontend can. In some cases this means that a
1098 // temporary variable won't have the right backend type. Correct
1099 // that here by adding a type cast. We need to use base() to push
1100 // the circularity down one level.
1101 tree ret = var_to_tree(bvar);
1102 if (!this->is_lvalue_
1103 && POINTER_TYPE_P(TREE_TYPE(ret))
1104 && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
1106 Btype* type_btype = this->type()->base()->get_backend(context->gogo());
1107 tree type_tree = type_to_tree(type_btype);
1108 ret = fold_convert_loc(this->location(), type_tree, ret);
1113 // Ast dump for temporary reference.
1116 Temporary_reference_expression::do_dump_expression(
1117 Ast_dump_context* ast_dump_context) const
1119 ast_dump_context->dump_temp_variable_name(this->statement_);
1122 // Make a reference to a temporary variable.
1124 Temporary_reference_expression*
1125 Expression::make_temporary_reference(Temporary_statement* statement,
1126 source_location location)
1128 return new Temporary_reference_expression(statement, location);
1131 // A sink expression--a use of the blank identifier _.
1133 class Sink_expression : public Expression
1136 Sink_expression(source_location location)
1137 : Expression(EXPRESSION_SINK, location),
1138 type_(NULL), var_(NULL_TREE)
1143 do_discarding_value()
1150 do_determine_type(const Type_context*);
1154 { return new Sink_expression(this->location()); }
1157 do_get_tree(Translate_context*);
1160 do_dump_expression(Ast_dump_context*) const;
1163 // The type of this sink variable.
1165 // The temporary variable we generate.
1169 // Return the type of a sink expression.
1172 Sink_expression::do_type()
1174 if (this->type_ == NULL)
1175 return Type::make_sink_type();
1179 // Determine the type of a sink expression.
1182 Sink_expression::do_determine_type(const Type_context* context)
1184 if (context->type != NULL)
1185 this->type_ = context->type;
1188 // Return a temporary variable for a sink expression. This will
1189 // presumably be a write-only variable which the middle-end will drop.
1192 Sink_expression::do_get_tree(Translate_context* context)
1194 if (this->var_ == NULL_TREE)
1196 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
1197 Btype* bt = this->type_->get_backend(context->gogo());
1198 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
1203 // Ast dump for sink expression.
1206 Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1208 ast_dump_context->ostream() << "_" ;
1211 // Make a sink expression.
1214 Expression::make_sink(source_location location)
1216 return new Sink_expression(location);
1219 // Class Func_expression.
1221 // FIXME: Can a function expression appear in a constant expression?
1222 // The value is unchanging. Initializing a constant to the address of
1223 // a function seems like it could work, though there might be little
1229 Func_expression::do_traverse(Traverse* traverse)
1231 return (this->closure_ == NULL
1233 : Expression::traverse(&this->closure_, traverse));
1236 // Return the type of a function expression.
1239 Func_expression::do_type()
1241 if (this->function_->is_function())
1242 return this->function_->func_value()->type();
1243 else if (this->function_->is_function_declaration())
1244 return this->function_->func_declaration_value()->type();
1249 // Get the tree for a function expression without evaluating the
1253 Func_expression::get_tree_without_closure(Gogo* gogo)
1255 Function_type* fntype;
1256 if (this->function_->is_function())
1257 fntype = this->function_->func_value()->type();
1258 else if (this->function_->is_function_declaration())
1259 fntype = this->function_->func_declaration_value()->type();
1263 // Builtin functions are handled specially by Call_expression. We
1264 // can't take their address.
1265 if (fntype->is_builtin())
1267 error_at(this->location(), "invalid use of special builtin function %qs",
1268 this->function_->name().c_str());
1269 return error_mark_node;
1272 Named_object* no = this->function_;
1274 tree id = no->get_id(gogo);
1275 if (id == error_mark_node)
1276 return error_mark_node;
1279 if (no->is_function())
1280 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1281 else if (no->is_function_declaration())
1282 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1286 if (fndecl == error_mark_node)
1287 return error_mark_node;
1289 return build_fold_addr_expr_loc(this->location(), fndecl);
1292 // Get the tree for a function expression. This is used when we take
1293 // the address of a function rather than simply calling it. If the
1294 // function has a closure, we must use a trampoline.
1297 Func_expression::do_get_tree(Translate_context* context)
1299 Gogo* gogo = context->gogo();
1301 tree fnaddr = this->get_tree_without_closure(gogo);
1302 if (fnaddr == error_mark_node)
1303 return error_mark_node;
1305 go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1306 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1307 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1309 // For a normal non-nested function call, that is all we have to do.
1310 if (!this->function_->is_function()
1311 || this->function_->func_value()->enclosing() == NULL)
1313 go_assert(this->closure_ == NULL);
1317 // For a nested function call, we have to always allocate a
1318 // trampoline. If we don't always allocate, then closures will not
1319 // be reliably distinct.
1320 Expression* closure = this->closure_;
1322 if (closure == NULL)
1323 closure_tree = null_pointer_node;
1326 // Get the value of the closure. This will be a pointer to
1327 // space allocated on the heap.
1328 closure_tree = closure->get_tree(context);
1329 if (closure_tree == error_mark_node)
1330 return error_mark_node;
1331 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1334 // Now we need to build some code on the heap. This code will load
1335 // the static chain pointer with the closure and then jump to the
1336 // body of the function. The normal gcc approach is to build the
1337 // code on the stack. Unfortunately we can not do that, as Go
1338 // permits us to return the function pointer.
1340 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1343 // Ast dump for function.
1346 Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1348 ast_dump_context->ostream() << this->function_->name();
1349 if (this->closure_ != NULL)
1351 ast_dump_context->ostream() << " {closure = ";
1352 this->closure_->dump_expression(ast_dump_context);
1353 ast_dump_context->ostream() << "}";
1357 // Make a reference to a function in an expression.
1360 Expression::make_func_reference(Named_object* function, Expression* closure,
1361 source_location location)
1363 return new Func_expression(function, closure, location);
1366 // Class Unknown_expression.
1368 // Return the name of an unknown expression.
1371 Unknown_expression::name() const
1373 return this->named_object_->name();
1376 // Lower a reference to an unknown name.
1379 Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1381 source_location location = this->location();
1382 Named_object* no = this->named_object_;
1384 if (!no->is_unknown())
1388 real = no->unknown_value()->real_named_object();
1391 if (this->is_composite_literal_key_)
1393 error_at(location, "reference to undefined name %qs",
1394 this->named_object_->message_name().c_str());
1395 return Expression::make_error(location);
1398 switch (real->classification())
1400 case Named_object::NAMED_OBJECT_CONST:
1401 return Expression::make_const_reference(real, location);
1402 case Named_object::NAMED_OBJECT_TYPE:
1403 return Expression::make_type(real->type_value(), location);
1404 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1405 if (this->is_composite_literal_key_)
1407 error_at(location, "reference to undefined type %qs",
1408 real->message_name().c_str());
1409 return Expression::make_error(location);
1410 case Named_object::NAMED_OBJECT_VAR:
1411 return Expression::make_var_reference(real, location);
1412 case Named_object::NAMED_OBJECT_FUNC:
1413 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1414 return Expression::make_func_reference(real, NULL, location);
1415 case Named_object::NAMED_OBJECT_PACKAGE:
1416 if (this->is_composite_literal_key_)
1418 error_at(location, "unexpected reference to package");
1419 return Expression::make_error(location);
1425 // Dump the ast representation for an unknown expression to a dump context.
1428 Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1430 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1434 // Make a reference to an unknown name.
1437 Expression::make_unknown_reference(Named_object* no, source_location location)
1439 return new Unknown_expression(no, location);
1442 // A boolean expression.
1444 class Boolean_expression : public Expression
1447 Boolean_expression(bool val, source_location location)
1448 : Expression(EXPRESSION_BOOLEAN, location),
1449 val_(val), type_(NULL)
1457 do_is_constant() const
1464 do_determine_type(const Type_context*);
1471 do_get_tree(Translate_context*)
1472 { return this->val_ ? boolean_true_node : boolean_false_node; }
1475 do_export(Export* exp) const
1476 { exp->write_c_string(this->val_ ? "true" : "false"); }
1479 do_dump_expression(Ast_dump_context* ast_dump_context) const
1480 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1485 // The type as determined by context.
1492 Boolean_expression::do_type()
1494 if (this->type_ == NULL)
1495 this->type_ = Type::make_boolean_type();
1499 // Set the type from the context.
1502 Boolean_expression::do_determine_type(const Type_context* context)
1504 if (this->type_ != NULL && !this->type_->is_abstract())
1506 else if (context->type != NULL && context->type->is_boolean_type())
1507 this->type_ = context->type;
1508 else if (!context->may_be_abstract)
1509 this->type_ = Type::lookup_bool_type();
1512 // Import a boolean constant.
1515 Boolean_expression::do_import(Import* imp)
1517 if (imp->peek_char() == 't')
1519 imp->require_c_string("true");
1520 return Expression::make_boolean(true, imp->location());
1524 imp->require_c_string("false");
1525 return Expression::make_boolean(false, imp->location());
1529 // Make a boolean expression.
1532 Expression::make_boolean(bool val, source_location location)
1534 return new Boolean_expression(val, location);
1537 // Class String_expression.
1542 String_expression::do_type()
1544 if (this->type_ == NULL)
1545 this->type_ = Type::make_string_type();
1549 // Set the type from the context.
1552 String_expression::do_determine_type(const Type_context* context)
1554 if (this->type_ != NULL && !this->type_->is_abstract())
1556 else if (context->type != NULL && context->type->is_string_type())
1557 this->type_ = context->type;
1558 else if (!context->may_be_abstract)
1559 this->type_ = Type::lookup_string_type();
1562 // Build a string constant.
1565 String_expression::do_get_tree(Translate_context* context)
1567 return context->gogo()->go_string_constant_tree(this->val_);
1570 // Write string literal to string dump.
1573 String_expression::export_string(String_dump* exp,
1574 const String_expression* str)
1577 s.reserve(str->val_.length() * 4 + 2);
1579 for (std::string::const_iterator p = str->val_.begin();
1580 p != str->val_.end();
1583 if (*p == '\\' || *p == '"')
1588 else if (*p >= 0x20 && *p < 0x7f)
1590 else if (*p == '\n')
1592 else if (*p == '\t')
1597 unsigned char c = *p;
1598 unsigned int dig = c >> 4;
1599 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1601 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1605 exp->write_string(s);
1608 // Export a string expression.
1611 String_expression::do_export(Export* exp) const
1613 String_expression::export_string(exp, this);
1616 // Import a string expression.
1619 String_expression::do_import(Import* imp)
1621 imp->require_c_string("\"");
1625 int c = imp->get_char();
1626 if (c == '"' || c == -1)
1629 val += static_cast<char>(c);
1632 c = imp->get_char();
1633 if (c == '\\' || c == '"')
1634 val += static_cast<char>(c);
1641 c = imp->get_char();
1642 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1643 c = imp->get_char();
1644 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1645 char v = (vh << 4) | vl;
1650 error_at(imp->location(), "bad string constant");
1651 return Expression::make_error(imp->location());
1655 return Expression::make_string(val, imp->location());
1658 // Ast dump for string expression.
1661 String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1663 String_expression::export_string(ast_dump_context, this);
1666 // Make a string expression.
1669 Expression::make_string(const std::string& val, source_location location)
1671 return new String_expression(val, location);
1674 // Make an integer expression.
1676 class Integer_expression : public Expression
1679 Integer_expression(const mpz_t* val, Type* type, source_location location)
1680 : Expression(EXPRESSION_INTEGER, location),
1682 { mpz_init_set(this->val_, *val); }
1687 // Return whether VAL fits in the type.
1689 check_constant(mpz_t val, Type*, source_location);
1691 // Write VAL to string dump.
1693 export_integer(String_dump* exp, const mpz_t val);
1695 // Write VAL to dump context.
1697 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1701 do_is_constant() const
1705 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1711 do_determine_type(const Type_context* context);
1714 do_check_types(Gogo*);
1717 do_get_tree(Translate_context*);
1721 { return Expression::make_integer(&this->val_, this->type_,
1722 this->location()); }
1725 do_export(Export*) const;
1728 do_dump_expression(Ast_dump_context*) const;
1731 // The integer value.
1737 // Return an integer constant value.
1740 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1743 if (this->type_ != NULL)
1744 *ptype = this->type_;
1745 mpz_set(val, this->val_);
1749 // Return the current type. If we haven't set the type yet, we return
1750 // an abstract integer type.
1753 Integer_expression::do_type()
1755 if (this->type_ == NULL)
1756 this->type_ = Type::make_abstract_integer_type();
1760 // Set the type of the integer value. Here we may switch from an
1761 // abstract type to a real type.
1764 Integer_expression::do_determine_type(const Type_context* context)
1766 if (this->type_ != NULL && !this->type_->is_abstract())
1768 else if (context->type != NULL
1769 && (context->type->integer_type() != NULL
1770 || context->type->float_type() != NULL
1771 || context->type->complex_type() != NULL))
1772 this->type_ = context->type;
1773 else if (!context->may_be_abstract)
1774 this->type_ = Type::lookup_integer_type("int");
1777 // Return true if the integer VAL fits in the range of the type TYPE.
1778 // Otherwise give an error and return false. TYPE may be NULL.
1781 Integer_expression::check_constant(mpz_t val, Type* type,
1782 source_location location)
1786 Integer_type* itype = type->integer_type();
1787 if (itype == NULL || itype->is_abstract())
1790 int bits = mpz_sizeinbase(val, 2);
1792 if (itype->is_unsigned())
1794 // For an unsigned type we can only accept a nonnegative number,
1795 // and we must be able to represent at least BITS.
1796 if (mpz_sgn(val) >= 0
1797 && bits <= itype->bits())
1802 // For a signed type we need an extra bit to indicate the sign.
1803 // We have to handle the most negative integer specially.
1804 if (bits + 1 <= itype->bits()
1805 || (bits <= itype->bits()
1807 && (mpz_scan1(val, 0)
1808 == static_cast<unsigned long>(itype->bits() - 1))
1809 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1813 error_at(location, "integer constant overflow");
1817 // Check the type of an integer constant.
1820 Integer_expression::do_check_types(Gogo*)
1822 if (this->type_ == NULL)
1824 if (!Integer_expression::check_constant(this->val_, this->type_,
1826 this->set_is_error();
1829 // Get a tree for an integer constant.
1832 Integer_expression::do_get_tree(Translate_context* context)
1834 Gogo* gogo = context->gogo();
1836 if (this->type_ != NULL && !this->type_->is_abstract())
1837 type = type_to_tree(this->type_->get_backend(gogo));
1838 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1840 // We are converting to an abstract floating point type.
1841 Type* ftype = Type::lookup_float_type("float64");
1842 type = type_to_tree(ftype->get_backend(gogo));
1844 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1846 // We are converting to an abstract complex type.
1847 Type* ctype = Type::lookup_complex_type("complex128");
1848 type = type_to_tree(ctype->get_backend(gogo));
1852 // If we still have an abstract type here, then this is being
1853 // used in a constant expression which didn't get reduced for
1854 // some reason. Use a type which will fit the value. We use <,
1855 // not <=, because we need an extra bit for the sign bit.
1856 int bits = mpz_sizeinbase(this->val_, 2);
1857 if (bits < INT_TYPE_SIZE)
1859 Type* t = Type::lookup_integer_type("int");
1860 type = type_to_tree(t->get_backend(gogo));
1864 Type* t = Type::lookup_integer_type("int64");
1865 type = type_to_tree(t->get_backend(gogo));
1868 type = long_long_integer_type_node;
1870 return Expression::integer_constant_tree(this->val_, type);
1873 // Write VAL to export data.
1876 Integer_expression::export_integer(String_dump* exp, const mpz_t val)
1878 char* s = mpz_get_str(NULL, 10, val);
1879 exp->write_c_string(s);
1883 // Export an integer in a constant expression.
1886 Integer_expression::do_export(Export* exp) const
1888 Integer_expression::export_integer(exp, this->val_);
1889 // A trailing space lets us reliably identify the end of the number.
1890 exp->write_c_string(" ");
1893 // Import an integer, floating point, or complex value. This handles
1894 // all these types because they all start with digits.
1897 Integer_expression::do_import(Import* imp)
1899 std::string num = imp->read_identifier();
1900 imp->require_c_string(" ");
1901 if (!num.empty() && num[num.length() - 1] == 'i')
1904 size_t plus_pos = num.find('+', 1);
1905 size_t minus_pos = num.find('-', 1);
1907 if (plus_pos == std::string::npos)
1909 else if (minus_pos == std::string::npos)
1913 error_at(imp->location(), "bad number in import data: %qs",
1915 return Expression::make_error(imp->location());
1917 if (pos == std::string::npos)
1918 mpfr_set_ui(real, 0, GMP_RNDN);
1921 std::string real_str = num.substr(0, pos);
1922 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1924 error_at(imp->location(), "bad number in import data: %qs",
1926 return Expression::make_error(imp->location());
1930 std::string imag_str;
1931 if (pos == std::string::npos)
1934 imag_str = num.substr(pos);
1935 imag_str = imag_str.substr(0, imag_str.size() - 1);
1937 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1939 error_at(imp->location(), "bad number in import data: %qs",
1941 return Expression::make_error(imp->location());
1943 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1949 else if (num.find('.') == std::string::npos
1950 && num.find('E') == std::string::npos)
1953 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1955 error_at(imp->location(), "bad number in import data: %qs",
1957 return Expression::make_error(imp->location());
1959 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1966 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1968 error_at(imp->location(), "bad number in import data: %qs",
1970 return Expression::make_error(imp->location());
1972 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1977 // Ast dump for integer expression.
1980 Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1982 Integer_expression::export_integer(ast_dump_context, this->val_);
1985 // Build a new integer value.
1988 Expression::make_integer(const mpz_t* val, Type* type,
1989 source_location location)
1991 return new Integer_expression(val, type, location);
1996 class Float_expression : public Expression
1999 Float_expression(const mpfr_t* val, Type* type, source_location location)
2000 : Expression(EXPRESSION_FLOAT, location),
2003 mpfr_init_set(this->val_, *val, GMP_RNDN);
2006 // Constrain VAL to fit into TYPE.
2008 constrain_float(mpfr_t val, Type* type);
2010 // Return whether VAL fits in the type.
2012 check_constant(mpfr_t val, Type*, source_location);
2014 // Write VAL to export data.
2016 export_float(String_dump* exp, const mpfr_t val);
2018 // Write VAL to dump file.
2020 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
2024 do_is_constant() const
2028 do_float_constant_value(mpfr_t val, Type**) const;
2034 do_determine_type(const Type_context*);
2037 do_check_types(Gogo*);
2041 { return Expression::make_float(&this->val_, this->type_,
2042 this->location()); }
2045 do_get_tree(Translate_context*);
2048 do_export(Export*) const;
2051 do_dump_expression(Ast_dump_context*) const;
2054 // The floating point value.
2060 // Constrain VAL to fit into TYPE.
2063 Float_expression::constrain_float(mpfr_t val, Type* type)
2065 Float_type* ftype = type->float_type();
2066 if (ftype != NULL && !ftype->is_abstract())
2067 mpfr_prec_round(val, ftype->bits(), GMP_RNDN);
2070 // Return a floating point constant value.
2073 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2075 if (this->type_ != NULL)
2076 *ptype = this->type_;
2077 mpfr_set(val, this->val_, GMP_RNDN);
2081 // Return the current type. If we haven't set the type yet, we return
2082 // an abstract float type.
2085 Float_expression::do_type()
2087 if (this->type_ == NULL)
2088 this->type_ = Type::make_abstract_float_type();
2092 // Set the type of the float value. Here we may switch from an
2093 // abstract type to a real type.
2096 Float_expression::do_determine_type(const Type_context* context)
2098 if (this->type_ != NULL && !this->type_->is_abstract())
2100 else if (context->type != NULL
2101 && (context->type->integer_type() != NULL
2102 || context->type->float_type() != NULL
2103 || context->type->complex_type() != NULL))
2104 this->type_ = context->type;
2105 else if (!context->may_be_abstract)
2106 this->type_ = Type::lookup_float_type("float64");
2109 // Return true if the floating point value VAL fits in the range of
2110 // the type TYPE. Otherwise give an error and return false. TYPE may
2114 Float_expression::check_constant(mpfr_t val, Type* type,
2115 source_location location)
2119 Float_type* ftype = type->float_type();
2120 if (ftype == NULL || ftype->is_abstract())
2123 // A NaN or Infinity always fits in the range of the type.
2124 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
2127 mp_exp_t exp = mpfr_get_exp(val);
2129 switch (ftype->bits())
2142 error_at(location, "floating point constant overflow");
2148 // Check the type of a float value.
2151 Float_expression::do_check_types(Gogo*)
2153 if (this->type_ == NULL)
2156 if (!Float_expression::check_constant(this->val_, this->type_,
2158 this->set_is_error();
2160 Integer_type* integer_type = this->type_->integer_type();
2161 if (integer_type != NULL)
2163 if (!mpfr_integer_p(this->val_))
2164 this->report_error(_("floating point constant truncated to integer"));
2167 go_assert(!integer_type->is_abstract());
2170 mpfr_get_z(ival, this->val_, GMP_RNDN);
2171 Integer_expression::check_constant(ival, integer_type,
2178 // Get a tree for a float constant.
2181 Float_expression::do_get_tree(Translate_context* context)
2183 Gogo* gogo = context->gogo();
2185 if (this->type_ != NULL && !this->type_->is_abstract())
2186 type = type_to_tree(this->type_->get_backend(gogo));
2187 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2189 // We have an abstract integer type. We just hope for the best.
2190 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
2194 // If we still have an abstract type here, then this is being
2195 // used in a constant expression which didn't get reduced. We
2196 // just use float64 and hope for the best.
2197 Type* ft = Type::lookup_float_type("float64");
2198 type = type_to_tree(ft->get_backend(gogo));
2200 return Expression::float_constant_tree(this->val_, type);
2203 // Write a floating point number to a string dump.
2206 Float_expression::export_float(String_dump *exp, const mpfr_t val)
2209 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2211 exp->write_c_string("-");
2212 exp->write_c_string("0.");
2213 exp->write_c_string(*s == '-' ? s + 1 : s);
2216 snprintf(buf, sizeof buf, "E%ld", exponent);
2217 exp->write_c_string(buf);
2220 // Export a floating point number in a constant expression.
2223 Float_expression::do_export(Export* exp) const
2225 Float_expression::export_float(exp, this->val_);
2226 // A trailing space lets us reliably identify the end of the number.
2227 exp->write_c_string(" ");
2230 // Dump a floating point number to the dump file.
2233 Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2235 Float_expression::export_float(ast_dump_context, this->val_);
2238 // Make a float expression.
2241 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2243 return new Float_expression(val, type, location);
2248 class Complex_expression : public Expression
2251 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2252 source_location location)
2253 : Expression(EXPRESSION_COMPLEX, location),
2256 mpfr_init_set(this->real_, *real, GMP_RNDN);
2257 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2260 // Constrain REAL/IMAG to fit into TYPE.
2262 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2264 // Return whether REAL/IMAG fits in the type.
2266 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2268 // Write REAL/IMAG to string dump.
2270 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
2272 // Write REAL/IMAG to dump context.
2274 dump_complex(Ast_dump_context* ast_dump_context,
2275 const mpfr_t real, const mpfr_t val);
2279 do_is_constant() const
2283 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2289 do_determine_type(const Type_context*);
2292 do_check_types(Gogo*);
2297 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2302 do_get_tree(Translate_context*);
2305 do_export(Export*) const;
2308 do_dump_expression(Ast_dump_context*) const;
2313 // The imaginary part;
2315 // The type if known.
2319 // Constrain REAL/IMAG to fit into TYPE.
2322 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2324 Complex_type* ctype = type->complex_type();
2325 if (ctype != NULL && !ctype->is_abstract())
2327 mpfr_prec_round(real, ctype->bits() / 2, GMP_RNDN);
2328 mpfr_prec_round(imag, ctype->bits() / 2, GMP_RNDN);
2332 // Return a complex constant value.
2335 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2338 if (this->type_ != NULL)
2339 *ptype = this->type_;
2340 mpfr_set(real, this->real_, GMP_RNDN);
2341 mpfr_set(imag, this->imag_, GMP_RNDN);
2345 // Return the current type. If we haven't set the type yet, we return
2346 // an abstract complex type.
2349 Complex_expression::do_type()
2351 if (this->type_ == NULL)
2352 this->type_ = Type::make_abstract_complex_type();
2356 // Set the type of the complex value. Here we may switch from an
2357 // abstract type to a real type.
2360 Complex_expression::do_determine_type(const Type_context* context)
2362 if (this->type_ != NULL && !this->type_->is_abstract())
2364 else if (context->type != NULL
2365 && context->type->complex_type() != NULL)
2366 this->type_ = context->type;
2367 else if (!context->may_be_abstract)
2368 this->type_ = Type::lookup_complex_type("complex128");
2371 // Return true if the complex value REAL/IMAG fits in the range of the
2372 // type TYPE. Otherwise give an error and return false. TYPE may be
2376 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2377 source_location location)
2381 Complex_type* ctype = type->complex_type();
2382 if (ctype == NULL || ctype->is_abstract())
2386 switch (ctype->bits())
2398 // A NaN or Infinity always fits in the range of the type.
2399 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2401 if (mpfr_get_exp(real) > max_exp)
2403 error_at(location, "complex real part constant overflow");
2408 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2410 if (mpfr_get_exp(imag) > max_exp)
2412 error_at(location, "complex imaginary part constant overflow");
2420 // Check the type of a complex value.
2423 Complex_expression::do_check_types(Gogo*)
2425 if (this->type_ == NULL)
2428 if (!Complex_expression::check_constant(this->real_, this->imag_,
2429 this->type_, this->location()))
2430 this->set_is_error();
2433 // Get a tree for a complex constant.
2436 Complex_expression::do_get_tree(Translate_context* context)
2438 Gogo* gogo = context->gogo();
2440 if (this->type_ != NULL && !this->type_->is_abstract())
2441 type = type_to_tree(this->type_->get_backend(gogo));
2444 // If we still have an abstract type here, this this is being
2445 // used in a constant expression which didn't get reduced. We
2446 // just use complex128 and hope for the best.
2447 Type* ct = Type::lookup_complex_type("complex128");
2448 type = type_to_tree(ct->get_backend(gogo));
2450 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2453 // Write REAL/IMAG to export data.
2456 Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
2459 if (!mpfr_zero_p(real))
2461 Float_expression::export_float(exp, real);
2462 if (mpfr_sgn(imag) > 0)
2463 exp->write_c_string("+");
2465 Float_expression::export_float(exp, imag);
2466 exp->write_c_string("i");
2469 // Export a complex number in a constant expression.
2472 Complex_expression::do_export(Export* exp) const
2474 Complex_expression::export_complex(exp, this->real_, this->imag_);
2475 // A trailing space lets us reliably identify the end of the number.
2476 exp->write_c_string(" ");
2479 // Dump a complex expression to the dump file.
2482 Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2484 Complex_expression::export_complex(ast_dump_context,
2489 // Make a complex expression.
2492 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2493 source_location location)
2495 return new Complex_expression(real, imag, type, location);
2498 // Find a named object in an expression.
2500 class Find_named_object : public Traverse
2503 Find_named_object(Named_object* no)
2504 : Traverse(traverse_expressions),
2505 no_(no), found_(false)
2508 // Whether we found the object.
2511 { return this->found_; }
2515 expression(Expression**);
2518 // The object we are looking for.
2520 // Whether we found it.
2524 // A reference to a const in an expression.
2526 class Const_expression : public Expression
2529 Const_expression(Named_object* constant, source_location location)
2530 : Expression(EXPRESSION_CONST_REFERENCE, location),
2531 constant_(constant), type_(NULL), seen_(false)
2536 { return this->constant_; }
2538 // Check that the initializer does not refer to the constant itself.
2540 check_for_init_loop();
2544 do_traverse(Traverse*);
2547 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2550 do_is_constant() const
2554 do_integer_constant_value(bool, mpz_t val, Type**) const;
2557 do_float_constant_value(mpfr_t val, Type**) const;
2560 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2563 do_string_constant_value(std::string* val) const
2564 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2569 // The type of a const is set by the declaration, not the use.
2571 do_determine_type(const Type_context*);
2574 do_check_types(Gogo*);
2581 do_get_tree(Translate_context* context);
2583 // When exporting a reference to a const as part of a const
2584 // expression, we export the value. We ignore the fact that it has
2587 do_export(Export* exp) const
2588 { this->constant_->const_value()->expr()->export_expression(exp); }
2591 do_dump_expression(Ast_dump_context*) const;
2595 Named_object* constant_;
2596 // The type of this reference. This is used if the constant has an
2599 // Used to prevent infinite recursion when a constant incorrectly
2600 // refers to itself.
2607 Const_expression::do_traverse(Traverse* traverse)
2609 if (this->type_ != NULL)
2610 return Type::traverse(this->type_, traverse);
2611 return TRAVERSE_CONTINUE;
2614 // Lower a constant expression. This is where we convert the
2615 // predeclared constant iota into an integer value.
2618 Const_expression::do_lower(Gogo* gogo, Named_object*,
2619 Statement_inserter*, int iota_value)
2621 if (this->constant_->const_value()->expr()->classification()
2624 if (iota_value == -1)
2626 error_at(this->location(),
2627 "iota is only defined in const declarations");
2631 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2632 Expression* ret = Expression::make_integer(&val, NULL,
2638 // Make sure that the constant itself has been lowered.
2639 gogo->lower_constant(this->constant_);
2644 // Return an integer constant value.
2647 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2654 if (this->type_ != NULL)
2655 ctype = this->type_;
2657 ctype = this->constant_->const_value()->type();
2658 if (ctype != NULL && ctype->integer_type() == NULL)
2661 Expression* e = this->constant_->const_value()->expr();
2666 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2668 this->seen_ = false;
2672 && !Integer_expression::check_constant(val, ctype, this->location()))
2675 *ptype = ctype != NULL ? ctype : t;
2679 // Return a floating point constant value.
2682 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2688 if (this->type_ != NULL)
2689 ctype = this->type_;
2691 ctype = this->constant_->const_value()->type();
2692 if (ctype != NULL && ctype->float_type() == NULL)
2698 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2701 this->seen_ = false;
2703 if (r && ctype != NULL)
2705 if (!Float_expression::check_constant(val, ctype, this->location()))
2707 Float_expression::constrain_float(val, ctype);
2709 *ptype = ctype != NULL ? ctype : t;
2713 // Return a complex constant value.
2716 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2723 if (this->type_ != NULL)
2724 ctype = this->type_;
2726 ctype = this->constant_->const_value()->type();
2727 if (ctype != NULL && ctype->complex_type() == NULL)
2733 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2737 this->seen_ = false;
2739 if (r && ctype != NULL)
2741 if (!Complex_expression::check_constant(real, imag, ctype,
2744 Complex_expression::constrain_complex(real, imag, ctype);
2746 *ptype = ctype != NULL ? ctype : t;
2750 // Return the type of the const reference.
2753 Const_expression::do_type()
2755 if (this->type_ != NULL)
2758 Named_constant* nc = this->constant_->const_value();
2760 if (this->seen_ || nc->lowering())
2762 this->report_error(_("constant refers to itself"));
2763 this->type_ = Type::make_error_type();
2769 Type* ret = nc->type();
2773 this->seen_ = false;
2777 // During parsing, a named constant may have a NULL type, but we
2778 // must not return a NULL type here.
2779 ret = nc->expr()->type();
2781 this->seen_ = false;
2786 // Set the type of the const reference.
2789 Const_expression::do_determine_type(const Type_context* context)
2791 Type* ctype = this->constant_->const_value()->type();
2792 Type* cetype = (ctype != NULL
2794 : this->constant_->const_value()->expr()->type());
2795 if (ctype != NULL && !ctype->is_abstract())
2797 else if (context->type != NULL
2798 && (context->type->integer_type() != NULL
2799 || context->type->float_type() != NULL
2800 || context->type->complex_type() != NULL)
2801 && (cetype->integer_type() != NULL
2802 || cetype->float_type() != NULL
2803 || cetype->complex_type() != NULL))
2804 this->type_ = context->type;
2805 else if (context->type != NULL
2806 && context->type->is_string_type()
2807 && cetype->is_string_type())
2808 this->type_ = context->type;
2809 else if (context->type != NULL
2810 && context->type->is_boolean_type()
2811 && cetype->is_boolean_type())
2812 this->type_ = context->type;
2813 else if (!context->may_be_abstract)
2815 if (cetype->is_abstract())
2816 cetype = cetype->make_non_abstract_type();
2817 this->type_ = cetype;
2821 // Check for a loop in which the initializer of a constant refers to
2822 // the constant itself.
2825 Const_expression::check_for_init_loop()
2827 if (this->type_ != NULL && this->type_->is_error())
2832 this->report_error(_("constant refers to itself"));
2833 this->type_ = Type::make_error_type();
2837 Expression* init = this->constant_->const_value()->expr();
2838 Find_named_object find_named_object(this->constant_);
2841 Expression::traverse(&init, &find_named_object);
2842 this->seen_ = false;
2844 if (find_named_object.found())
2846 if (this->type_ == NULL || !this->type_->is_error())
2848 this->report_error(_("constant refers to itself"));
2849 this->type_ = Type::make_error_type();
2855 // Check types of a const reference.
2858 Const_expression::do_check_types(Gogo*)
2860 if (this->type_ != NULL && this->type_->is_error())
2863 this->check_for_init_loop();
2865 if (this->type_ == NULL || this->type_->is_abstract())
2868 // Check for integer overflow.
2869 if (this->type_->integer_type() != NULL)
2874 if (!this->integer_constant_value(true, ival, &dummy))
2878 Expression* cexpr = this->constant_->const_value()->expr();
2879 if (cexpr->float_constant_value(fval, &dummy))
2881 if (!mpfr_integer_p(fval))
2882 this->report_error(_("floating point constant "
2883 "truncated to integer"));
2886 mpfr_get_z(ival, fval, GMP_RNDN);
2887 Integer_expression::check_constant(ival, this->type_,
2897 // Return a tree for the const reference.
2900 Const_expression::do_get_tree(Translate_context* context)
2902 Gogo* gogo = context->gogo();
2904 if (this->type_ == NULL)
2905 type_tree = NULL_TREE;
2908 type_tree = type_to_tree(this->type_->get_backend(gogo));
2909 if (type_tree == error_mark_node)
2910 return error_mark_node;
2913 // If the type has been set for this expression, but the underlying
2914 // object is an abstract int or float, we try to get the abstract
2915 // value. Otherwise we may lose something in the conversion.
2916 if (this->type_ != NULL
2917 && (this->constant_->const_value()->type() == NULL
2918 || this->constant_->const_value()->type()->is_abstract()))
2920 Expression* expr = this->constant_->const_value()->expr();
2924 if (expr->integer_constant_value(true, ival, &t))
2926 tree ret = Expression::integer_constant_tree(ival, type_tree);
2934 if (expr->float_constant_value(fval, &t))
2936 tree ret = Expression::float_constant_tree(fval, type_tree);
2943 if (expr->complex_constant_value(fval, imag, &t))
2945 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2954 tree const_tree = this->constant_->get_tree(gogo, context->function());
2955 if (this->type_ == NULL
2956 || const_tree == error_mark_node
2957 || TREE_TYPE(const_tree) == error_mark_node)
2961 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2962 ret = fold_convert(type_tree, const_tree);
2963 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2964 ret = fold(convert_to_integer(type_tree, const_tree));
2965 else if (TREE_CODE(type_tree) == REAL_TYPE)
2966 ret = fold(convert_to_real(type_tree, const_tree));
2967 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2968 ret = fold(convert_to_complex(type_tree, const_tree));
2974 // Dump ast representation for constant expression.
2977 Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2979 ast_dump_context->ostream() << this->constant_->name();
2982 // Make a reference to a constant in an expression.
2985 Expression::make_const_reference(Named_object* constant,
2986 source_location location)
2988 return new Const_expression(constant, location);
2991 // Find a named object in an expression.
2994 Find_named_object::expression(Expression** pexpr)
2996 switch ((*pexpr)->classification())
2998 case Expression::EXPRESSION_CONST_REFERENCE:
3000 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
3001 if (ce->named_object() == this->no_)
3004 // We need to check a constant initializer explicitly, as
3005 // loops here will not be caught by the loop checking for
3006 // variable initializers.
3007 ce->check_for_init_loop();
3009 return TRAVERSE_CONTINUE;
3012 case Expression::EXPRESSION_VAR_REFERENCE:
3013 if ((*pexpr)->var_expression()->named_object() == this->no_)
3015 return TRAVERSE_CONTINUE;
3016 case Expression::EXPRESSION_FUNC_REFERENCE:
3017 if ((*pexpr)->func_expression()->named_object() == this->no_)
3019 return TRAVERSE_CONTINUE;
3021 return TRAVERSE_CONTINUE;
3023 this->found_ = true;
3024 return TRAVERSE_EXIT;
3029 class Nil_expression : public Expression
3032 Nil_expression(source_location location)
3033 : Expression(EXPRESSION_NIL, location)
3041 do_is_constant() const
3046 { return Type::make_nil_type(); }
3049 do_determine_type(const Type_context*)
3057 do_get_tree(Translate_context*)
3058 { return null_pointer_node; }
3061 do_export(Export* exp) const
3062 { exp->write_c_string("nil"); }
3065 do_dump_expression(Ast_dump_context* ast_dump_context) const
3066 { ast_dump_context->ostream() << "nil"; }
3069 // Import a nil expression.
3072 Nil_expression::do_import(Import* imp)
3074 imp->require_c_string("nil");
3075 return Expression::make_nil(imp->location());
3078 // Make a nil expression.
3081 Expression::make_nil(source_location location)
3083 return new Nil_expression(location);
3086 // The value of the predeclared constant iota. This is little more
3087 // than a marker. This will be lowered to an integer in
3088 // Const_expression::do_lower, which is where we know the value that
3091 class Iota_expression : public Parser_expression
3094 Iota_expression(source_location location)
3095 : Parser_expression(EXPRESSION_IOTA, location)
3100 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
3101 { go_unreachable(); }
3103 // There should only ever be one of these.
3106 { go_unreachable(); }
3109 do_dump_expression(Ast_dump_context* ast_dump_context) const
3110 { ast_dump_context->ostream() << "iota"; }
3113 // Make an iota expression. This is only called for one case: the
3114 // value of the predeclared constant iota.
3117 Expression::make_iota()
3119 static Iota_expression iota_expression(UNKNOWN_LOCATION);
3120 return &iota_expression;
3123 // A type conversion expression.
3125 class Type_conversion_expression : public Expression
3128 Type_conversion_expression(Type* type, Expression* expr,
3129 source_location location)
3130 : Expression(EXPRESSION_CONVERSION, location),
3131 type_(type), expr_(expr), may_convert_function_types_(false)
3134 // Return the type to which we are converting.
3137 { return this->type_; }
3139 // Return the expression which we are converting.
3142 { return this->expr_; }
3144 // Permit converting from one function type to another. This is
3145 // used internally for method expressions.
3147 set_may_convert_function_types()
3149 this->may_convert_function_types_ = true;
3152 // Import a type conversion expression.
3158 do_traverse(Traverse* traverse);
3161 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3164 do_is_constant() const
3165 { return this->expr_->is_constant(); }
3168 do_integer_constant_value(bool, mpz_t, Type**) const;
3171 do_float_constant_value(mpfr_t, Type**) const;
3174 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3177 do_string_constant_value(std::string*) const;
3181 { return this->type_; }
3184 do_determine_type(const Type_context*)
3186 Type_context subcontext(this->type_, false);
3187 this->expr_->determine_type(&subcontext);
3191 do_check_types(Gogo*);
3196 return new Type_conversion_expression(this->type_, this->expr_->copy(),
3201 do_get_tree(Translate_context* context);
3204 do_export(Export*) const;
3207 do_dump_expression(Ast_dump_context*) const;
3210 // The type to convert to.
3212 // The expression to convert.
3214 // True if this is permitted to convert function types. This is
3215 // used internally for method expressions.
3216 bool may_convert_function_types_;
3222 Type_conversion_expression::do_traverse(Traverse* traverse)
3224 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3225 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3226 return TRAVERSE_EXIT;
3227 return TRAVERSE_CONTINUE;
3230 // Convert to a constant at lowering time.
3233 Type_conversion_expression::do_lower(Gogo*, Named_object*,
3234 Statement_inserter*, int)
3236 Type* type = this->type_;
3237 Expression* val = this->expr_;
3238 source_location location = this->location();
3240 if (type->integer_type() != NULL)
3245 if (val->integer_constant_value(false, ival, &dummy))
3247 if (!Integer_expression::check_constant(ival, type, location))
3248 mpz_set_ui(ival, 0);
3249 Expression* ret = Expression::make_integer(&ival, type, location);
3256 if (val->float_constant_value(fval, &dummy))
3258 if (!mpfr_integer_p(fval))
3261 "floating point constant truncated to integer");
3262 return Expression::make_error(location);
3264 mpfr_get_z(ival, fval, GMP_RNDN);
3265 if (!Integer_expression::check_constant(ival, type, location))
3266 mpz_set_ui(ival, 0);
3267 Expression* ret = Expression::make_integer(&ival, type, location);
3276 if (type->float_type() != NULL)
3281 if (val->float_constant_value(fval, &dummy))
3283 if (!Float_expression::check_constant(fval, type, location))
3284 mpfr_set_ui(fval, 0, GMP_RNDN);
3285 Float_expression::constrain_float(fval, type);
3286 Expression *ret = Expression::make_float(&fval, type, location);
3293 if (type->complex_type() != NULL)
3300 if (val->complex_constant_value(real, imag, &dummy))
3302 if (!Complex_expression::check_constant(real, imag, type, location))
3304 mpfr_set_ui(real, 0, GMP_RNDN);
3305 mpfr_set_ui(imag, 0, GMP_RNDN);
3307 Complex_expression::constrain_complex(real, imag, type);
3308 Expression* ret = Expression::make_complex(&real, &imag, type,
3318 if (type->is_open_array_type() && type->named_type() == NULL)
3320 Type* element_type = type->array_type()->element_type()->forwarded();
3321 bool is_byte = element_type == Type::lookup_integer_type("uint8");
3322 bool is_int = element_type == Type::lookup_integer_type("int");
3323 if (is_byte || is_int)
3326 if (val->string_constant_value(&s))
3328 Expression_list* vals = new Expression_list();
3331 for (std::string::const_iterator p = s.begin();
3336 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3337 Expression* v = Expression::make_integer(&val,
3346 const char *p = s.data();
3347 const char *pend = s.data() + s.length();
3351 int adv = Lex::fetch_char(p, &c);
3354 warning_at(this->location(), 0,
3355 "invalid UTF-8 encoding");
3360 mpz_init_set_ui(val, c);
3361 Expression* v = Expression::make_integer(&val,
3369 return Expression::make_slice_composite_literal(type, vals,
3378 // Return the constant integer value if there is one.
3381 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3385 if (this->type_->integer_type() == NULL)
3391 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3393 if (!Integer_expression::check_constant(ival, this->type_,
3401 *ptype = this->type_;
3408 if (this->expr_->float_constant_value(fval, &dummy))
3410 mpfr_get_z(val, fval, GMP_RNDN);
3412 if (!Integer_expression::check_constant(val, this->type_,
3415 *ptype = this->type_;
3423 // Return the constant floating point value if there is one.
3426 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3429 if (this->type_->float_type() == NULL)
3435 if (this->expr_->float_constant_value(fval, &dummy))
3437 if (!Float_expression::check_constant(fval, this->type_,
3443 mpfr_set(val, fval, GMP_RNDN);
3445 Float_expression::constrain_float(val, this->type_);
3446 *ptype = this->type_;
3454 // Return the constant complex value if there is one.
3457 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3461 if (this->type_->complex_type() == NULL)
3469 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3471 if (!Complex_expression::check_constant(rval, ival, this->type_,
3478 mpfr_set(real, rval, GMP_RNDN);
3479 mpfr_set(imag, ival, GMP_RNDN);
3482 Complex_expression::constrain_complex(real, imag, this->type_);
3483 *ptype = this->type_;
3492 // Return the constant string value if there is one.
3495 Type_conversion_expression::do_string_constant_value(std::string* val) const
3497 if (this->type_->is_string_type()
3498 && this->expr_->type()->integer_type() != NULL)
3503 if (this->expr_->integer_constant_value(false, ival, &dummy))
3505 unsigned long ulval = mpz_get_ui(ival);
3506 if (mpz_cmp_ui(ival, ulval) == 0)
3508 Lex::append_char(ulval, true, val, this->location());
3516 // FIXME: Could handle conversion from const []int here.
3521 // Check that types are convertible.
3524 Type_conversion_expression::do_check_types(Gogo*)
3526 Type* type = this->type_;
3527 Type* expr_type = this->expr_->type();
3530 if (type->is_error() || expr_type->is_error())
3532 this->set_is_error();
3536 if (this->may_convert_function_types_
3537 && type->function_type() != NULL
3538 && expr_type->function_type() != NULL)
3541 if (Type::are_convertible(type, expr_type, &reason))
3544 error_at(this->location(), "%s", reason.c_str());
3545 this->set_is_error();
3548 // Get a tree for a type conversion.
3551 Type_conversion_expression::do_get_tree(Translate_context* context)
3553 Gogo* gogo = context->gogo();
3554 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
3555 tree expr_tree = this->expr_->get_tree(context);
3557 if (type_tree == error_mark_node
3558 || expr_tree == error_mark_node
3559 || TREE_TYPE(expr_tree) == error_mark_node)
3560 return error_mark_node;
3562 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3563 return fold_convert(type_tree, expr_tree);
3565 Type* type = this->type_;
3566 Type* expr_type = this->expr_->type();
3568 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3569 ret = Expression::convert_for_assignment(context, type, expr_type,
3570 expr_tree, this->location());
3571 else if (type->integer_type() != NULL)
3573 if (expr_type->integer_type() != NULL
3574 || expr_type->float_type() != NULL
3575 || expr_type->is_unsafe_pointer_type())
3576 ret = fold(convert_to_integer(type_tree, expr_tree));
3580 else if (type->float_type() != NULL)
3582 if (expr_type->integer_type() != NULL
3583 || expr_type->float_type() != NULL)
3584 ret = fold(convert_to_real(type_tree, expr_tree));
3588 else if (type->complex_type() != NULL)
3590 if (expr_type->complex_type() != NULL)
3591 ret = fold(convert_to_complex(type_tree, expr_tree));
3595 else if (type->is_string_type()
3596 && expr_type->integer_type() != NULL)
3598 expr_tree = fold_convert(integer_type_node, expr_tree);
3599 if (host_integerp(expr_tree, 0))
3601 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3603 Lex::append_char(intval, true, &s, this->location());
3604 Expression* se = Expression::make_string(s, this->location());
3605 return se->get_tree(context);
3608 static tree int_to_string_fndecl;
3609 ret = Gogo::call_builtin(&int_to_string_fndecl,
3611 "__go_int_to_string",
3615 fold_convert(integer_type_node, expr_tree));
3617 else if (type->is_string_type()
3618 && (expr_type->array_type() != NULL
3619 || (expr_type->points_to() != NULL
3620 && expr_type->points_to()->array_type() != NULL)))
3622 Type* t = expr_type;
3623 if (t->points_to() != NULL)
3626 expr_tree = build_fold_indirect_ref(expr_tree);
3628 if (!DECL_P(expr_tree))
3629 expr_tree = save_expr(expr_tree);
3630 Array_type* a = t->array_type();
3631 Type* e = a->element_type()->forwarded();
3632 go_assert(e->integer_type() != NULL);
3633 tree valptr = fold_convert(const_ptr_type_node,
3634 a->value_pointer_tree(gogo, expr_tree));
3635 tree len = a->length_tree(gogo, expr_tree);
3636 len = fold_convert_loc(this->location(), integer_type_node, len);
3637 if (e->integer_type()->is_unsigned()
3638 && e->integer_type()->bits() == 8)
3640 static tree byte_array_to_string_fndecl;
3641 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3643 "__go_byte_array_to_string",
3646 const_ptr_type_node,
3653 go_assert(e == Type::lookup_integer_type("int"));
3654 static tree int_array_to_string_fndecl;
3655 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3657 "__go_int_array_to_string",
3660 const_ptr_type_node,
3666 else if (type->is_open_array_type() && expr_type->is_string_type())
3668 Type* e = type->array_type()->element_type()->forwarded();
3669 go_assert(e->integer_type() != NULL);
3670 if (e->integer_type()->is_unsigned()
3671 && e->integer_type()->bits() == 8)
3673 static tree string_to_byte_array_fndecl;
3674 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3676 "__go_string_to_byte_array",
3679 TREE_TYPE(expr_tree),
3684 go_assert(e == Type::lookup_integer_type("int"));
3685 static tree string_to_int_array_fndecl;
3686 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3688 "__go_string_to_int_array",
3691 TREE_TYPE(expr_tree),
3695 else if ((type->is_unsafe_pointer_type()
3696 && expr_type->points_to() != NULL)
3697 || (expr_type->is_unsafe_pointer_type()
3698 && type->points_to() != NULL))
3699 ret = fold_convert(type_tree, expr_tree);
3700 else if (type->is_unsafe_pointer_type()
3701 && expr_type->integer_type() != NULL)
3702 ret = convert_to_pointer(type_tree, expr_tree);
3703 else if (this->may_convert_function_types_
3704 && type->function_type() != NULL
3705 && expr_type->function_type() != NULL)
3706 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3708 ret = Expression::convert_for_assignment(context, type, expr_type,
3709 expr_tree, this->location());
3714 // Output a type conversion in a constant expression.
3717 Type_conversion_expression::do_export(Export* exp) const
3719 exp->write_c_string("convert(");
3720 exp->write_type(this->type_);
3721 exp->write_c_string(", ");
3722 this->expr_->export_expression(exp);
3723 exp->write_c_string(")");
3726 // Import a type conversion or a struct construction.
3729 Type_conversion_expression::do_import(Import* imp)
3731 imp->require_c_string("convert(");
3732 Type* type = imp->read_type();
3733 imp->require_c_string(", ");
3734 Expression* val = Expression::import_expression(imp);
3735 imp->require_c_string(")");
3736 return Expression::make_cast(type, val, imp->location());
3739 // Dump ast representation for a type conversion expression.
3742 Type_conversion_expression::do_dump_expression(
3743 Ast_dump_context* ast_dump_context) const
3745 ast_dump_context->dump_type(this->type_);
3746 ast_dump_context->ostream() << "(";
3747 ast_dump_context->dump_expression(this->expr_);
3748 ast_dump_context->ostream() << ") ";
3751 // Make a type cast expression.
3754 Expression::make_cast(Type* type, Expression* val, source_location location)
3756 if (type->is_error_type() || val->is_error_expression())
3757 return Expression::make_error(location);
3758 return new Type_conversion_expression(type, val, location);
3761 // An unsafe type conversion, used to pass values to builtin functions.
3763 class Unsafe_type_conversion_expression : public Expression
3766 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3767 source_location location)
3768 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3769 type_(type), expr_(expr)
3774 do_traverse(Traverse* traverse);
3778 { return this->type_; }
3781 do_determine_type(const Type_context*)
3782 { this->expr_->determine_type_no_context(); }
3787 return new Unsafe_type_conversion_expression(this->type_,
3788 this->expr_->copy(),
3793 do_get_tree(Translate_context*);
3796 do_dump_expression(Ast_dump_context*) const;
3799 // The type to convert to.
3801 // The expression to convert.
3808 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3810 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3811 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3812 return TRAVERSE_EXIT;
3813 return TRAVERSE_CONTINUE;
3816 // Convert to backend representation.
3819 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3821 // We are only called for a limited number of cases.
3823 Type* t = this->type_;
3824 Type* et = this->expr_->type();
3826 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
3827 tree expr_tree = this->expr_->get_tree(context);
3828 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3829 return error_mark_node;
3831 source_location loc = this->location();
3833 bool use_view_convert = false;
3834 if (t->is_open_array_type())
3836 go_assert(et->is_open_array_type());
3837 use_view_convert = true;
3839 else if (t->map_type() != NULL)
3840 go_assert(et->map_type() != NULL);
3841 else if (t->channel_type() != NULL)
3842 go_assert(et->channel_type() != NULL);
3843 else if (t->points_to() != NULL && t->points_to()->channel_type() != NULL)
3844 go_assert((et->points_to() != NULL
3845 && et->points_to()->channel_type() != NULL)
3846 || et->is_nil_type());
3847 else if (t->points_to() != NULL)
3848 go_assert(et->points_to() != NULL || et->is_nil_type());
3849 else if (et->is_unsafe_pointer_type())
3850 go_assert(t->points_to() != NULL);
3851 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3853 go_assert(et->interface_type() != NULL
3854 && !et->interface_type()->is_empty());
3855 use_view_convert = true;
3857 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3859 go_assert(et->interface_type() != NULL
3860 && et->interface_type()->is_empty());
3861 use_view_convert = true;
3863 else if (t->integer_type() != NULL)
3865 go_assert(et->is_boolean_type()
3866 || et->integer_type() != NULL
3867 || et->function_type() != NULL
3868 || et->points_to() != NULL
3869 || et->map_type() != NULL
3870 || et->channel_type() != NULL);
3871 return convert_to_integer(type_tree, expr_tree);
3876 if (use_view_convert)
3877 return fold_build1_loc(loc, VIEW_CONVERT_EXPR, type_tree, expr_tree);
3879 return fold_convert_loc(loc, type_tree, expr_tree);
3882 // Dump ast representation for an unsafe type conversion expression.
3885 Unsafe_type_conversion_expression::do_dump_expression(
3886 Ast_dump_context* ast_dump_context) const
3888 ast_dump_context->dump_type(this->type_);
3889 ast_dump_context->ostream() << "(";
3890 ast_dump_context->dump_expression(this->expr_);
3891 ast_dump_context->ostream() << ") ";
3894 // Make an unsafe type conversion expression.
3897 Expression::make_unsafe_cast(Type* type, Expression* expr,
3898 source_location location)
3900 return new Unsafe_type_conversion_expression(type, expr, location);
3903 // Unary expressions.
3905 class Unary_expression : public Expression
3908 Unary_expression(Operator op, Expression* expr, source_location location)
3909 : Expression(EXPRESSION_UNARY, location),
3910 op_(op), escapes_(true), create_temp_(false), expr_(expr)
3913 // Return the operator.
3916 { return this->op_; }
3918 // Return the operand.
3921 { return this->expr_; }
3923 // Record that an address expression does not escape.
3925 set_does_not_escape()
3927 go_assert(this->op_ == OPERATOR_AND);
3928 this->escapes_ = false;
3931 // Record that this is an address expression which should create a
3932 // temporary variable if necessary. This is used for method calls.
3936 go_assert(this->op_ == OPERATOR_AND);
3937 this->create_temp_ = true;
3940 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3941 // could be done, false if not.
3943 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3946 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3947 // could be done, false if not.
3949 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3951 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3952 // true if this could be done, false if not.
3954 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3962 do_traverse(Traverse* traverse)
3963 { return Expression::traverse(&this->expr_, traverse); }
3966 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3969 do_is_constant() const;
3972 do_integer_constant_value(bool, mpz_t, Type**) const;
3975 do_float_constant_value(mpfr_t, Type**) const;
3978 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3984 do_determine_type(const Type_context*);
3987 do_check_types(Gogo*);
3992 return Expression::make_unary(this->op_, this->expr_->copy(),
3997 do_is_addressable() const
3998 { return this->op_ == OPERATOR_MULT; }
4001 do_get_tree(Translate_context*);
4004 do_export(Export*) const;
4007 do_dump_expression(Ast_dump_context*) const;
4010 // The unary operator to apply.
4012 // Normally true. False if this is an address expression which does
4013 // not escape the current function.
4015 // True if this is an address expression which should create a
4016 // temporary variable if necessary.
4022 // If we are taking the address of a composite literal, and the
4023 // contents are not constant, then we want to make a heap composite
4027 Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
4029 source_location loc = this->location();
4030 Operator op = this->op_;
4031 Expression* expr = this->expr_;
4033 if (op == OPERATOR_MULT && expr->is_type_expression())
4034 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
4036 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
4037 // moving x to the heap. FIXME: Is it worth doing a real escape
4038 // analysis here? This case is found in math/unsafe.go and is
4039 // therefore worth special casing.
4040 if (op == OPERATOR_MULT)
4042 Expression* e = expr;
4043 while (e->classification() == EXPRESSION_CONVERSION)
4045 Type_conversion_expression* te
4046 = static_cast<Type_conversion_expression*>(e);
4050 if (e->classification() == EXPRESSION_UNARY)
4052 Unary_expression* ue = static_cast<Unary_expression*>(e);
4053 if (ue->op_ == OPERATOR_AND)
4060 ue->set_does_not_escape();
4065 // Catching an invalid indirection of unsafe.Pointer here avoid
4066 // having to deal with TYPE_VOID in other places.
4067 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
4069 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
4070 return Expression::make_error(this->location());
4073 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
4074 || op == OPERATOR_NOT || op == OPERATOR_XOR)
4076 Expression* ret = NULL;
4081 if (expr->integer_constant_value(false, eval, &etype))
4085 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
4086 ret = Expression::make_integer(&val, etype, loc);
4093 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
4098 if (expr->float_constant_value(fval, &ftype))
4102 if (Unary_expression::eval_float(op, fval, val))
4103 ret = Expression::make_float(&val, ftype, loc);
4114 if (expr->complex_constant_value(fval, ival, &ftype))
4120 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
4121 ret = Expression::make_complex(&real, &imag, ftype, loc);
4135 // Return whether a unary expression is a constant.
4138 Unary_expression::do_is_constant() const
4140 if (this->op_ == OPERATOR_MULT)
4142 // Indirecting through a pointer is only constant if the object
4143 // to which the expression points is constant, but we currently
4144 // have no way to determine that.
4147 else if (this->op_ == OPERATOR_AND)
4149 // Taking the address of a variable is constant if it is a
4150 // global variable, not constant otherwise. In other cases
4151 // taking the address is probably not a constant.
4152 Var_expression* ve = this->expr_->var_expression();
4155 Named_object* no = ve->named_object();
4156 return no->is_variable() && no->var_value()->is_global();
4161 return this->expr_->is_constant();
4164 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
4165 // UVAL, if known; it may be NULL. Return true if this could be done,
4169 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
4170 source_location location)
4177 case OPERATOR_MINUS:
4179 return Integer_expression::check_constant(val, utype, location);
4181 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
4185 || utype->integer_type() == NULL
4186 || utype->integer_type()->is_abstract())
4190 // The number of HOST_WIDE_INTs that it takes to represent
4192 size_t count = ((mpz_sizeinbase(uval, 2)
4193 + HOST_BITS_PER_WIDE_INT
4195 / HOST_BITS_PER_WIDE_INT);
4197 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
4198 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
4201 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
4202 go_assert(ecount <= count);
4204 // Trim down to the number of words required by the type.
4205 size_t obits = utype->integer_type()->bits();
4206 if (!utype->integer_type()->is_unsigned())
4208 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
4209 / HOST_BITS_PER_WIDE_INT);
4210 go_assert(ocount <= count);
4212 for (size_t i = 0; i < ocount; ++i)
4215 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
4217 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
4220 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
4224 return Integer_expression::check_constant(val, utype, location);
4233 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
4234 // could be done, false if not.
4237 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
4242 mpfr_set(val, uval, GMP_RNDN);
4244 case OPERATOR_MINUS:
4245 mpfr_neg(val, uval, GMP_RNDN);
4257 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
4258 // if this could be done, false if not.
4261 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
4262 mpfr_t real, mpfr_t imag)
4267 mpfr_set(real, rval, GMP_RNDN);
4268 mpfr_set(imag, ival, GMP_RNDN);
4270 case OPERATOR_MINUS:
4271 mpfr_neg(real, rval, GMP_RNDN);
4272 mpfr_neg(imag, ival, GMP_RNDN);
4284 // Return the integral constant value of a unary expression, if it has one.
4287 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
4293 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
4296 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
4302 // Return the floating point constant value of a unary expression, if
4306 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
4311 if (!this->expr_->float_constant_value(uval, ptype))
4314 ret = Unary_expression::eval_float(this->op_, uval, val);
4319 // Return the complex constant value of a unary expression, if it has
4323 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
4331 if (!this->expr_->complex_constant_value(rval, ival, ptype))
4334 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
4340 // Return the type of a unary expression.
4343 Unary_expression::do_type()
4348 case OPERATOR_MINUS:
4351 return this->expr_->type();
4354 return Type::make_pointer_type(this->expr_->type());
4358 Type* subtype = this->expr_->type();
4359 Type* points_to = subtype->points_to();
4360 if (points_to == NULL)
4361 return Type::make_error_type();
4370 // Determine abstract types for a unary expression.
4373 Unary_expression::do_determine_type(const Type_context* context)
4378 case OPERATOR_MINUS:
4381 this->expr_->determine_type(context);
4385 // Taking the address of something.
4387 Type* subtype = (context->type == NULL
4389 : context->type->points_to());
4390 Type_context subcontext(subtype, false);
4391 this->expr_->determine_type(&subcontext);
4396 // Indirecting through a pointer.
4398 Type* subtype = (context->type == NULL
4400 : Type::make_pointer_type(context->type));
4401 Type_context subcontext(subtype, false);
4402 this->expr_->determine_type(&subcontext);
4411 // Check types for a unary expression.
4414 Unary_expression::do_check_types(Gogo*)
4416 Type* type = this->expr_->type();
4417 if (type->is_error())
4419 this->set_is_error();
4426 case OPERATOR_MINUS:
4427 if (type->integer_type() == NULL
4428 && type->float_type() == NULL
4429 && type->complex_type() == NULL)
4430 this->report_error(_("expected numeric type"));
4435 if (type->integer_type() == NULL
4436 && !type->is_boolean_type())
4437 this->report_error(_("expected integer or boolean type"));
4441 if (!this->expr_->is_addressable())
4443 if (!this->create_temp_)
4444 this->report_error(_("invalid operand for unary %<&%>"));
4447 this->expr_->address_taken(this->escapes_);
4451 // Indirecting through a pointer.
4452 if (type->points_to() == NULL)
4453 this->report_error(_("expected pointer"));
4461 // Get a tree for a unary expression.
4464 Unary_expression::do_get_tree(Translate_context* context)
4466 tree expr = this->expr_->get_tree(context);
4467 if (expr == error_mark_node)
4468 return error_mark_node;
4470 source_location loc = this->location();
4476 case OPERATOR_MINUS:
4478 tree type = TREE_TYPE(expr);
4479 tree compute_type = excess_precision_type(type);
4480 if (compute_type != NULL_TREE)
4481 expr = ::convert(compute_type, expr);
4482 tree ret = fold_build1_loc(loc, NEGATE_EXPR,
4483 (compute_type != NULL_TREE
4487 if (compute_type != NULL_TREE)
4488 ret = ::convert(type, ret);
4493 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4494 return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr);
4496 return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr,
4497 build_int_cst(TREE_TYPE(expr), 0));
4500 return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr);
4503 if (!this->create_temp_)
4505 // We should not see a non-constant constructor here; cases
4506 // where we would see one should have been moved onto the
4507 // heap at parse time. Taking the address of a nonconstant
4508 // constructor will not do what the programmer expects.
4509 go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4510 go_assert(TREE_CODE(expr) != ADDR_EXPR);
4513 // Build a decl for a constant constructor.
4514 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4516 tree decl = build_decl(this->location(), VAR_DECL,
4517 create_tmp_var_name("C"), TREE_TYPE(expr));
4518 DECL_EXTERNAL(decl) = 0;
4519 TREE_PUBLIC(decl) = 0;
4520 TREE_READONLY(decl) = 1;
4521 TREE_CONSTANT(decl) = 1;
4522 TREE_STATIC(decl) = 1;
4523 TREE_ADDRESSABLE(decl) = 1;
4524 DECL_ARTIFICIAL(decl) = 1;
4525 DECL_INITIAL(decl) = expr;
4526 rest_of_decl_compilation(decl, 1, 0);
4530 if (this->create_temp_
4531 && !TREE_ADDRESSABLE(TREE_TYPE(expr))
4533 && TREE_CODE(expr) != INDIRECT_REF
4534 && TREE_CODE(expr) != COMPONENT_REF)
4536 tree tmp = create_tmp_var(TREE_TYPE(expr), get_name(expr));
4537 DECL_IGNORED_P(tmp) = 1;
4538 DECL_INITIAL(tmp) = expr;
4539 TREE_ADDRESSABLE(tmp) = 1;
4540 return build2_loc(loc, COMPOUND_EXPR,
4541 build_pointer_type(TREE_TYPE(expr)),
4542 build1_loc(loc, DECL_EXPR, void_type_node, tmp),
4543 build_fold_addr_expr_loc(loc, tmp));
4546 return build_fold_addr_expr_loc(loc, expr);
4550 go_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4552 // If we are dereferencing the pointer to a large struct, we
4553 // need to check for nil. We don't bother to check for small
4554 // structs because we expect the system to crash on a nil
4555 // pointer dereference.
4556 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4557 if (s == -1 || s >= 4096)
4560 expr = save_expr(expr);
4561 tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node,
4563 fold_convert(TREE_TYPE(expr),
4564 null_pointer_node));
4565 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4567 expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr),
4568 build3(COND_EXPR, void_type_node,
4569 compare, crash, NULL_TREE),
4573 // If the type of EXPR is a recursive pointer type, then we
4574 // need to insert a cast before indirecting.
4575 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4577 Type* pt = this->expr_->type()->points_to();
4578 tree ind = type_to_tree(pt->get_backend(context->gogo()));
4579 expr = fold_convert_loc(loc, build_pointer_type(ind), expr);
4582 return build_fold_indirect_ref_loc(loc, expr);
4590 // Export a unary expression.
4593 Unary_expression::do_export(Export* exp) const
4598 exp->write_c_string("+ ");
4600 case OPERATOR_MINUS:
4601 exp->write_c_string("- ");
4604 exp->write_c_string("! ");
4607 exp->write_c_string("^ ");
4614 this->expr_->export_expression(exp);
4617 // Import a unary expression.
4620 Unary_expression::do_import(Import* imp)
4623 switch (imp->get_char())
4629 op = OPERATOR_MINUS;
4640 imp->require_c_string(" ");
4641 Expression* expr = Expression::import_expression(imp);
4642 return Expression::make_unary(op, expr, imp->location());
4645 // Dump ast representation of an unary expression.
4648 Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
4650 ast_dump_context->dump_operator(this->op_);
4651 ast_dump_context->ostream() << "(";
4652 ast_dump_context->dump_expression(this->expr_);
4653 ast_dump_context->ostream() << ") ";
4656 // Make a unary expression.
4659 Expression::make_unary(Operator op, Expression* expr, source_location location)
4661 return new Unary_expression(op, expr, location);
4664 // If this is an indirection through a pointer, return the expression
4665 // being pointed through. Otherwise return this.
4670 if (this->classification_ == EXPRESSION_UNARY)
4672 Unary_expression* ue = static_cast<Unary_expression*>(this);
4673 if (ue->op() == OPERATOR_MULT)
4674 return ue->operand();
4679 // Class Binary_expression.
4684 Binary_expression::do_traverse(Traverse* traverse)
4686 int t = Expression::traverse(&this->left_, traverse);
4687 if (t == TRAVERSE_EXIT)
4688 return TRAVERSE_EXIT;
4689 return Expression::traverse(&this->right_, traverse);
4692 // Compare integer constants according to OP.
4695 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4698 int i = mpz_cmp(left_val, right_val);
4703 case OPERATOR_NOTEQ:
4718 // Compare floating point constants according to OP.
4721 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4726 i = mpfr_cmp(left_val, right_val);
4730 mpfr_init_set(lv, left_val, GMP_RNDN);
4732 mpfr_init_set(rv, right_val, GMP_RNDN);
4733 Float_expression::constrain_float(lv, type);
4734 Float_expression::constrain_float(rv, type);
4735 i = mpfr_cmp(lv, rv);
4743 case OPERATOR_NOTEQ:
4758 // Compare complex constants according to OP. Complex numbers may
4759 // only be compared for equality.
4762 Binary_expression::compare_complex(Operator op, Type* type,
4763 mpfr_t left_real, mpfr_t left_imag,
4764 mpfr_t right_real, mpfr_t right_imag)
4768 is_equal = (mpfr_cmp(left_real, right_real) == 0
4769 && mpfr_cmp(left_imag, right_imag) == 0);
4774 mpfr_init_set(lr, left_real, GMP_RNDN);
4775 mpfr_init_set(li, left_imag, GMP_RNDN);
4778 mpfr_init_set(rr, right_real, GMP_RNDN);
4779 mpfr_init_set(ri, right_imag, GMP_RNDN);
4780 Complex_expression::constrain_complex(lr, li, type);
4781 Complex_expression::constrain_complex(rr, ri, type);
4782 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4792 case OPERATOR_NOTEQ:
4799 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4800 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4801 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4802 // this could be done, false if not.
4805 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4806 Type* right_type, mpz_t right_val,
4807 source_location location, mpz_t val)
4809 bool is_shift_op = false;
4813 case OPERATOR_ANDAND:
4815 case OPERATOR_NOTEQ:
4820 // These return boolean values. We should probably handle them
4821 // anyhow in case a type conversion is used on the result.
4824 mpz_add(val, left_val, right_val);
4826 case OPERATOR_MINUS:
4827 mpz_sub(val, left_val, right_val);
4830 mpz_ior(val, left_val, right_val);
4833 mpz_xor(val, left_val, right_val);
4836 mpz_mul(val, left_val, right_val);
4839 if (mpz_sgn(right_val) != 0)
4840 mpz_tdiv_q(val, left_val, right_val);
4843 error_at(location, "division by zero");
4849 if (mpz_sgn(right_val) != 0)
4850 mpz_tdiv_r(val, left_val, right_val);
4853 error_at(location, "division by zero");
4858 case OPERATOR_LSHIFT:
4860 unsigned long shift = mpz_get_ui(right_val);
4861 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
4863 error_at(location, "shift count overflow");
4867 mpz_mul_2exp(val, left_val, shift);
4872 case OPERATOR_RSHIFT:
4874 unsigned long shift = mpz_get_ui(right_val);
4875 if (mpz_cmp_ui(right_val, shift) != 0)
4877 error_at(location, "shift count overflow");
4881 if (mpz_cmp_ui(left_val, 0) >= 0)
4882 mpz_tdiv_q_2exp(val, left_val, shift);
4884 mpz_fdiv_q_2exp(val, left_val, shift);
4890 mpz_and(val, left_val, right_val);
4892 case OPERATOR_BITCLEAR:
4896 mpz_com(tval, right_val);
4897 mpz_and(val, left_val, tval);
4905 Type* type = left_type;
4910 else if (type != right_type && right_type != NULL)
4912 if (type->is_abstract())
4914 else if (!right_type->is_abstract())
4916 // This look like a type error which should be diagnosed
4917 // elsewhere. Don't do anything here, to avoid an
4918 // unhelpful chain of error messages.
4924 if (type != NULL && !type->is_abstract())
4926 // We have to check the operands too, as we have implicitly
4927 // coerced them to TYPE.
4928 if ((type != left_type
4929 && !Integer_expression::check_constant(left_val, type, location))
4931 && type != right_type
4932 && !Integer_expression::check_constant(right_val, type,
4934 || !Integer_expression::check_constant(val, type, location))
4941 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4942 // Return true if this could be done, false if not.
4945 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4946 Type* right_type, mpfr_t right_val,
4947 mpfr_t val, source_location location)
4952 case OPERATOR_ANDAND:
4954 case OPERATOR_NOTEQ:
4959 // These return boolean values. We should probably handle them
4960 // anyhow in case a type conversion is used on the result.
4963 mpfr_add(val, left_val, right_val, GMP_RNDN);
4965 case OPERATOR_MINUS:
4966 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4971 case OPERATOR_BITCLEAR:
4974 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4977 if (mpfr_zero_p(right_val))
4978 error_at(location, "division by zero");
4979 mpfr_div(val, left_val, right_val, GMP_RNDN);
4983 case OPERATOR_LSHIFT:
4984 case OPERATOR_RSHIFT:
4990 Type* type = left_type;
4993 else if (type != right_type && right_type != NULL)
4995 if (type->is_abstract())
4997 else if (!right_type->is_abstract())
4999 // This looks like a type error which should be diagnosed
5000 // elsewhere. Don't do anything here, to avoid an unhelpful
5001 // chain of error messages.
5006 if (type != NULL && !type->is_abstract())
5008 if ((type != left_type
5009 && !Float_expression::check_constant(left_val, type, location))
5010 || (type != right_type
5011 && !Float_expression::check_constant(right_val, type,
5013 || !Float_expression::check_constant(val, type, location))
5014 mpfr_set_ui(val, 0, GMP_RNDN);
5020 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
5021 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
5022 // could be done, false if not.
5025 Binary_expression::eval_complex(Operator op, Type* left_type,
5026 mpfr_t left_real, mpfr_t left_imag,
5028 mpfr_t right_real, mpfr_t right_imag,
5029 mpfr_t real, mpfr_t imag,
5030 source_location location)
5035 case OPERATOR_ANDAND:
5037 case OPERATOR_NOTEQ:
5042 // These return boolean values and must be handled differently.
5045 mpfr_add(real, left_real, right_real, GMP_RNDN);
5046 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
5048 case OPERATOR_MINUS:
5049 mpfr_sub(real, left_real, right_real, GMP_RNDN);
5050 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
5055 case OPERATOR_BITCLEAR:
5059 // You might think that multiplying two complex numbers would
5060 // be simple, and you would be right, until you start to think
5061 // about getting the right answer for infinity. If one
5062 // operand here is infinity and the other is anything other
5063 // than zero or NaN, then we are going to wind up subtracting
5064 // two infinity values. That will give us a NaN, but the
5065 // correct answer is infinity.
5069 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
5073 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
5077 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
5081 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
5083 mpfr_sub(real, lrrr, liri, GMP_RNDN);
5084 mpfr_add(imag, lrri, lirr, GMP_RNDN);
5086 // If we get NaN on both sides, check whether it should really
5087 // be infinity. The rule is that if either side of the
5088 // complex number is infinity, then the whole value is
5089 // infinity, even if the other side is NaN. So the only case
5090 // we have to fix is the one in which both sides are NaN.
5091 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
5092 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
5093 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
5095 bool is_infinity = false;
5099 mpfr_init_set(lr, left_real, GMP_RNDN);
5100 mpfr_init_set(li, left_imag, GMP_RNDN);
5104 mpfr_init_set(rr, right_real, GMP_RNDN);
5105 mpfr_init_set(ri, right_imag, GMP_RNDN);
5107 // If the left side is infinity, then the result is
5109 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
5111 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
5112 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5113 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
5114 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5117 mpfr_set_ui(rr, 0, GMP_RNDN);
5118 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5122 mpfr_set_ui(ri, 0, GMP_RNDN);
5123 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5128 // If the right side is infinity, then the result is
5130 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
5132 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5133 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5134 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5135 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5138 mpfr_set_ui(lr, 0, GMP_RNDN);
5139 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5143 mpfr_set_ui(li, 0, GMP_RNDN);
5144 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5149 // If we got an overflow in the intermediate computations,
5150 // then the result is infinity.
5152 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
5153 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
5157 mpfr_set_ui(lr, 0, GMP_RNDN);
5158 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5162 mpfr_set_ui(li, 0, GMP_RNDN);
5163 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5167 mpfr_set_ui(rr, 0, GMP_RNDN);
5168 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5172 mpfr_set_ui(ri, 0, GMP_RNDN);
5173 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5180 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
5181 mpfr_mul(lrri, lr, ri, GMP_RNDN);
5182 mpfr_mul(lirr, li, rr, GMP_RNDN);
5183 mpfr_mul(liri, li, ri, GMP_RNDN);
5184 mpfr_sub(real, lrrr, liri, GMP_RNDN);
5185 mpfr_add(imag, lrri, lirr, GMP_RNDN);
5186 mpfr_set_inf(real, mpfr_sgn(real));
5187 mpfr_set_inf(imag, mpfr_sgn(imag));
5204 // For complex division we want to avoid having an
5205 // intermediate overflow turn the whole result in a NaN. We
5206 // scale the values to try to avoid this.
5208 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
5209 error_at(location, "division by zero");
5215 mpfr_abs(rra, right_real, GMP_RNDN);
5216 mpfr_abs(ria, right_imag, GMP_RNDN);
5219 mpfr_max(t, rra, ria, GMP_RNDN);
5223 mpfr_init_set(rr, right_real, GMP_RNDN);
5224 mpfr_init_set(ri, right_imag, GMP_RNDN);
5226 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
5228 ilogbw = mpfr_get_exp(t);
5229 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
5230 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
5235 mpfr_mul(denom, rr, rr, GMP_RNDN);
5236 mpfr_mul(t, ri, ri, GMP_RNDN);
5237 mpfr_add(denom, denom, t, GMP_RNDN);
5239 mpfr_mul(real, left_real, rr, GMP_RNDN);
5240 mpfr_mul(t, left_imag, ri, GMP_RNDN);
5241 mpfr_add(real, real, t, GMP_RNDN);
5242 mpfr_div(real, real, denom, GMP_RNDN);
5243 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
5245 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
5246 mpfr_mul(t, left_real, ri, GMP_RNDN);
5247 mpfr_sub(imag, imag, t, GMP_RNDN);
5248 mpfr_div(imag, imag, denom, GMP_RNDN);
5249 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
5251 // If we wind up with NaN on both sides, check whether we
5252 // should really have infinity. The rule is that if either
5253 // side of the complex number is infinity, then the whole
5254 // value is infinity, even if the other side is NaN. So the
5255 // only case we have to fix is the one in which both sides are
5257 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
5258 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
5259 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
5261 if (mpfr_zero_p(denom))
5263 mpfr_set_inf(real, mpfr_sgn(rr));
5264 mpfr_mul(real, real, left_real, GMP_RNDN);
5265 mpfr_set_inf(imag, mpfr_sgn(rr));
5266 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
5268 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
5269 && mpfr_number_p(rr) && mpfr_number_p(ri))
5271 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
5272 mpfr_copysign(t, t, left_real, GMP_RNDN);
5275 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
5276 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
5280 mpfr_mul(t3, t, rr, GMP_RNDN);
5284 mpfr_mul(t4, t2, ri, GMP_RNDN);
5286 mpfr_add(t3, t3, t4, GMP_RNDN);
5287 mpfr_set_inf(real, mpfr_sgn(t3));
5289 mpfr_mul(t3, t2, rr, GMP_RNDN);
5290 mpfr_mul(t4, t, ri, GMP_RNDN);
5291 mpfr_sub(t3, t3, t4, GMP_RNDN);
5292 mpfr_set_inf(imag, mpfr_sgn(t3));
5298 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
5299 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
5301 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5302 mpfr_copysign(t, t, rr, GMP_RNDN);
5305 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5306 mpfr_copysign(t2, t2, ri, GMP_RNDN);
5310 mpfr_mul(t3, left_real, t, GMP_RNDN);
5314 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
5316 mpfr_add(t3, t3, t4, GMP_RNDN);
5317 mpfr_set_ui(real, 0, GMP_RNDN);
5318 mpfr_mul(real, real, t3, GMP_RNDN);
5320 mpfr_mul(t3, left_imag, t, GMP_RNDN);
5321 mpfr_mul(t4, left_real, t2, GMP_RNDN);
5322 mpfr_sub(t3, t3, t4, GMP_RNDN);
5323 mpfr_set_ui(imag, 0, GMP_RNDN);
5324 mpfr_mul(imag, imag, t3, GMP_RNDN);
5342 case OPERATOR_LSHIFT:
5343 case OPERATOR_RSHIFT:
5349 Type* type = left_type;
5352 else if (type != right_type && right_type != NULL)
5354 if (type->is_abstract())
5356 else if (!right_type->is_abstract())
5358 // This looks like a type error which should be diagnosed
5359 // elsewhere. Don't do anything here, to avoid an unhelpful
5360 // chain of error messages.
5365 if (type != NULL && !type->is_abstract())
5367 if ((type != left_type
5368 && !Complex_expression::check_constant(left_real, left_imag,
5370 || (type != right_type
5371 && !Complex_expression::check_constant(right_real, right_imag,
5373 || !Complex_expression::check_constant(real, imag, type,
5376 mpfr_set_ui(real, 0, GMP_RNDN);
5377 mpfr_set_ui(imag, 0, GMP_RNDN);
5384 // Lower a binary expression. We have to evaluate constant
5385 // expressions now, in order to implement Go's unlimited precision
5389 Binary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
5391 source_location location = this->location();
5392 Operator op = this->op_;
5393 Expression* left = this->left_;
5394 Expression* right = this->right_;
5396 const bool is_comparison = (op == OPERATOR_EQEQ
5397 || op == OPERATOR_NOTEQ
5398 || op == OPERATOR_LT
5399 || op == OPERATOR_LE
5400 || op == OPERATOR_GT
5401 || op == OPERATOR_GE);
5403 // Integer constant expressions.
5409 mpz_init(right_val);
5411 if (left->integer_constant_value(false, left_val, &left_type)
5412 && right->integer_constant_value(false, right_val, &right_type))
5414 Expression* ret = NULL;
5415 if (left_type != right_type
5416 && left_type != NULL
5417 && right_type != NULL
5418 && left_type->base() != right_type->base()
5419 && op != OPERATOR_LSHIFT
5420 && op != OPERATOR_RSHIFT)
5422 // May be a type error--let it be diagnosed later.
5424 else if (is_comparison)
5426 bool b = Binary_expression::compare_integer(op, left_val,
5428 ret = Expression::make_cast(Type::lookup_bool_type(),
5429 Expression::make_boolean(b, location),
5437 if (Binary_expression::eval_integer(op, left_type, left_val,
5438 right_type, right_val,
5441 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
5443 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5445 else if (left_type == NULL)
5447 else if (right_type == NULL)
5449 else if (!left_type->is_abstract()
5450 && left_type->named_type() != NULL)
5452 else if (!right_type->is_abstract()
5453 && right_type->named_type() != NULL)
5455 else if (!left_type->is_abstract())
5457 else if (!right_type->is_abstract())
5459 else if (left_type->float_type() != NULL)
5461 else if (right_type->float_type() != NULL)
5463 else if (left_type->complex_type() != NULL)
5465 else if (right_type->complex_type() != NULL)
5469 ret = Expression::make_integer(&val, type, location);
5477 mpz_clear(right_val);
5478 mpz_clear(left_val);
5482 mpz_clear(right_val);
5483 mpz_clear(left_val);
5486 // Floating point constant expressions.
5489 mpfr_init(left_val);
5492 mpfr_init(right_val);
5494 if (left->float_constant_value(left_val, &left_type)
5495 && right->float_constant_value(right_val, &right_type))
5497 Expression* ret = NULL;
5498 if (left_type != right_type
5499 && left_type != NULL
5500 && right_type != NULL
5501 && left_type->base() != right_type->base()
5502 && op != OPERATOR_LSHIFT
5503 && op != OPERATOR_RSHIFT)
5505 // May be a type error--let it be diagnosed later.
5507 else if (is_comparison)
5509 bool b = Binary_expression::compare_float(op,
5513 left_val, right_val);
5514 ret = Expression::make_boolean(b, location);
5521 if (Binary_expression::eval_float(op, left_type, left_val,
5522 right_type, right_val, val,
5525 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5526 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5528 if (left_type == NULL)
5530 else if (right_type == NULL)
5532 else if (!left_type->is_abstract()
5533 && left_type->named_type() != NULL)
5535 else if (!right_type->is_abstract()
5536 && right_type->named_type() != NULL)
5538 else if (!left_type->is_abstract())
5540 else if (!right_type->is_abstract())
5542 else if (left_type->float_type() != NULL)
5544 else if (right_type->float_type() != NULL)
5548 ret = Expression::make_float(&val, type, location);
5556 mpfr_clear(right_val);
5557 mpfr_clear(left_val);
5561 mpfr_clear(right_val);
5562 mpfr_clear(left_val);
5565 // Complex constant expressions.
5569 mpfr_init(left_real);
5570 mpfr_init(left_imag);
5575 mpfr_init(right_real);
5576 mpfr_init(right_imag);
5579 if (left->complex_constant_value(left_real, left_imag, &left_type)
5580 && right->complex_constant_value(right_real, right_imag, &right_type))
5582 Expression* ret = NULL;
5583 if (left_type != right_type
5584 && left_type != NULL
5585 && right_type != NULL
5586 && left_type->base() != right_type->base())
5588 // May be a type error--let it be diagnosed later.
5590 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5592 bool b = Binary_expression::compare_complex(op,
5600 ret = Expression::make_boolean(b, location);
5609 if (Binary_expression::eval_complex(op, left_type,
5610 left_real, left_imag,
5612 right_real, right_imag,
5616 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5617 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5619 if (left_type == NULL)
5621 else if (right_type == NULL)
5623 else if (!left_type->is_abstract()
5624 && left_type->named_type() != NULL)
5626 else if (!right_type->is_abstract()
5627 && right_type->named_type() != NULL)
5629 else if (!left_type->is_abstract())
5631 else if (!right_type->is_abstract())
5633 else if (left_type->complex_type() != NULL)
5635 else if (right_type->complex_type() != NULL)
5639 ret = Expression::make_complex(&real, &imag, type,
5648 mpfr_clear(left_real);
5649 mpfr_clear(left_imag);
5650 mpfr_clear(right_real);
5651 mpfr_clear(right_imag);
5656 mpfr_clear(left_real);
5657 mpfr_clear(left_imag);
5658 mpfr_clear(right_real);
5659 mpfr_clear(right_imag);
5662 // String constant expressions.
5663 if (op == OPERATOR_PLUS
5664 && left->type()->is_string_type()
5665 && right->type()->is_string_type())
5667 std::string left_string;
5668 std::string right_string;
5669 if (left->string_constant_value(&left_string)
5670 && right->string_constant_value(&right_string))
5671 return Expression::make_string(left_string + right_string, location);
5677 // Return the integer constant value, if it has one.
5680 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5686 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5689 mpz_clear(left_val);
5694 mpz_init(right_val);
5696 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5699 mpz_clear(right_val);
5700 mpz_clear(left_val);
5705 if (left_type != right_type
5706 && left_type != NULL
5707 && right_type != NULL
5708 && left_type->base() != right_type->base()
5709 && this->op_ != OPERATOR_RSHIFT
5710 && this->op_ != OPERATOR_LSHIFT)
5713 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5714 right_type, right_val,
5715 this->location(), val);
5717 mpz_clear(right_val);
5718 mpz_clear(left_val);
5726 // Return the floating point constant value, if it has one.
5729 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5732 mpfr_init(left_val);
5734 if (!this->left_->float_constant_value(left_val, &left_type))
5736 mpfr_clear(left_val);
5741 mpfr_init(right_val);
5743 if (!this->right_->float_constant_value(right_val, &right_type))
5745 mpfr_clear(right_val);
5746 mpfr_clear(left_val);
5751 if (left_type != right_type
5752 && left_type != NULL
5753 && right_type != NULL
5754 && left_type->base() != right_type->base())
5757 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5758 right_type, right_val,
5759 val, this->location());
5761 mpfr_clear(left_val);
5762 mpfr_clear(right_val);
5770 // Return the complex constant value, if it has one.
5773 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5778 mpfr_init(left_real);
5779 mpfr_init(left_imag);
5781 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5783 mpfr_clear(left_real);
5784 mpfr_clear(left_imag);
5790 mpfr_init(right_real);
5791 mpfr_init(right_imag);
5793 if (!this->right_->complex_constant_value(right_real, right_imag,
5796 mpfr_clear(left_real);
5797 mpfr_clear(left_imag);
5798 mpfr_clear(right_real);
5799 mpfr_clear(right_imag);
5804 if (left_type != right_type
5805 && left_type != NULL
5806 && right_type != NULL
5807 && left_type->base() != right_type->base())
5810 ret = Binary_expression::eval_complex(this->op_, left_type,
5811 left_real, left_imag,
5813 right_real, right_imag,
5816 mpfr_clear(left_real);
5817 mpfr_clear(left_imag);
5818 mpfr_clear(right_real);
5819 mpfr_clear(right_imag);
5827 // Note that the value is being discarded.
5830 Binary_expression::do_discarding_value()
5832 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5833 this->right_->discarding_value();
5835 this->warn_about_unused_value();
5841 Binary_expression::do_type()
5843 if (this->classification() == EXPRESSION_ERROR)
5844 return Type::make_error_type();
5849 case OPERATOR_ANDAND:
5851 case OPERATOR_NOTEQ:
5856 return Type::lookup_bool_type();
5859 case OPERATOR_MINUS:
5866 case OPERATOR_BITCLEAR:
5868 Type* left_type = this->left_->type();
5869 Type* right_type = this->right_->type();
5870 if (left_type->is_error())
5872 else if (right_type->is_error())
5874 else if (!Type::are_compatible_for_binop(left_type, right_type))
5876 this->report_error(_("incompatible types in binary expression"));
5877 return Type::make_error_type();
5879 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
5881 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5883 else if (!left_type->is_abstract())
5885 else if (!right_type->is_abstract())
5887 else if (left_type->complex_type() != NULL)
5889 else if (right_type->complex_type() != NULL)
5891 else if (left_type->float_type() != NULL)
5893 else if (right_type->float_type() != NULL)
5899 case OPERATOR_LSHIFT:
5900 case OPERATOR_RSHIFT:
5901 return this->left_->type();
5908 // Set type for a binary expression.
5911 Binary_expression::do_determine_type(const Type_context* context)
5913 Type* tleft = this->left_->type();
5914 Type* tright = this->right_->type();
5916 // Both sides should have the same type, except for the shift
5917 // operations. For a comparison, we should ignore the incoming
5920 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5921 || this->op_ == OPERATOR_RSHIFT);
5923 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5924 || this->op_ == OPERATOR_NOTEQ
5925 || this->op_ == OPERATOR_LT
5926 || this->op_ == OPERATOR_LE
5927 || this->op_ == OPERATOR_GT
5928 || this->op_ == OPERATOR_GE);
5930 Type_context subcontext(*context);
5934 // In a comparison, the context does not determine the types of
5936 subcontext.type = NULL;
5939 // Set the context for the left hand operand.
5942 // The right hand operand plays no role in determining the type
5943 // of the left hand operand. A shift of an abstract integer in
5944 // a string context gets special treatment, which may be a
5946 if (subcontext.type != NULL
5947 && subcontext.type->is_string_type()
5948 && tleft->is_abstract())
5949 error_at(this->location(), "shift of non-integer operand");
5951 else if (!tleft->is_abstract())
5952 subcontext.type = tleft;
5953 else if (!tright->is_abstract())
5954 subcontext.type = tright;
5955 else if (subcontext.type == NULL)
5957 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5958 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5959 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5961 // Both sides have an abstract integer, abstract float, or
5962 // abstract complex type. Just let CONTEXT determine
5963 // whether they may remain abstract or not.
5965 else if (tleft->complex_type() != NULL)
5966 subcontext.type = tleft;
5967 else if (tright->complex_type() != NULL)
5968 subcontext.type = tright;
5969 else if (tleft->float_type() != NULL)
5970 subcontext.type = tleft;
5971 else if (tright->float_type() != NULL)
5972 subcontext.type = tright;
5974 subcontext.type = tleft;
5976 if (subcontext.type != NULL && !context->may_be_abstract)
5977 subcontext.type = subcontext.type->make_non_abstract_type();
5980 this->left_->determine_type(&subcontext);
5982 // The context for the right hand operand is the same as for the
5983 // left hand operand, except for a shift operator.
5986 subcontext.type = Type::lookup_integer_type("uint");
5987 subcontext.may_be_abstract = false;
5990 this->right_->determine_type(&subcontext);
5993 // Report an error if the binary operator OP does not support TYPE.
5994 // Return whether the operation is OK. This should not be used for
5998 Binary_expression::check_operator_type(Operator op, Type* type,
5999 source_location location)
6004 case OPERATOR_ANDAND:
6005 if (!type->is_boolean_type())
6007 error_at(location, "expected boolean type");
6013 case OPERATOR_NOTEQ:
6014 if (type->integer_type() == NULL
6015 && type->float_type() == NULL
6016 && type->complex_type() == NULL
6017 && !type->is_string_type()
6018 && type->points_to() == NULL
6019 && !type->is_nil_type()
6020 && !type->is_boolean_type()
6021 && type->interface_type() == NULL
6022 && (type->array_type() == NULL
6023 || type->array_type()->length() != NULL)
6024 && type->map_type() == NULL
6025 && type->channel_type() == NULL
6026 && type->function_type() == NULL)
6029 ("expected integer, floating, complex, string, pointer, "
6030 "boolean, interface, slice, map, channel, "
6031 "or function type"));
6040 if (type->integer_type() == NULL
6041 && type->float_type() == NULL
6042 && !type->is_string_type())
6044 error_at(location, "expected integer, floating, or string type");
6050 case OPERATOR_PLUSEQ:
6051 if (type->integer_type() == NULL
6052 && type->float_type() == NULL
6053 && type->complex_type() == NULL
6054 && !type->is_string_type())
6057 "expected integer, floating, complex, or string type");
6062 case OPERATOR_MINUS:
6063 case OPERATOR_MINUSEQ:
6065 case OPERATOR_MULTEQ:
6067 case OPERATOR_DIVEQ:
6068 if (type->integer_type() == NULL
6069 && type->float_type() == NULL
6070 && type->complex_type() == NULL)
6072 error_at(location, "expected integer, floating, or complex type");
6078 case OPERATOR_MODEQ:
6082 case OPERATOR_ANDEQ:
6084 case OPERATOR_XOREQ:
6085 case OPERATOR_BITCLEAR:
6086 case OPERATOR_BITCLEAREQ:
6087 if (type->integer_type() == NULL)
6089 error_at(location, "expected integer type");
6104 Binary_expression::do_check_types(Gogo*)
6106 if (this->classification() == EXPRESSION_ERROR)
6109 Type* left_type = this->left_->type();
6110 Type* right_type = this->right_->type();
6111 if (left_type->is_error() || right_type->is_error())
6113 this->set_is_error();
6117 if (this->op_ == OPERATOR_EQEQ
6118 || this->op_ == OPERATOR_NOTEQ
6119 || this->op_ == OPERATOR_LT
6120 || this->op_ == OPERATOR_LE
6121 || this->op_ == OPERATOR_GT
6122 || this->op_ == OPERATOR_GE)
6124 if (!Type::are_assignable(left_type, right_type, NULL)
6125 && !Type::are_assignable(right_type, left_type, NULL))
6127 this->report_error(_("incompatible types in binary expression"));
6130 if (!Binary_expression::check_operator_type(this->op_, left_type,
6132 || !Binary_expression::check_operator_type(this->op_, right_type,
6135 this->set_is_error();
6139 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
6141 if (!Type::are_compatible_for_binop(left_type, right_type))
6143 this->report_error(_("incompatible types in binary expression"));
6146 if (!Binary_expression::check_operator_type(this->op_, left_type,
6149 this->set_is_error();
6155 if (left_type->integer_type() == NULL)
6156 this->report_error(_("shift of non-integer operand"));
6158 if (!right_type->is_abstract()
6159 && (right_type->integer_type() == NULL
6160 || !right_type->integer_type()->is_unsigned()))
6161 this->report_error(_("shift count not unsigned integer"));
6167 if (this->right_->integer_constant_value(true, val, &type))
6169 if (mpz_sgn(val) < 0)
6171 this->report_error(_("negative shift count"));
6173 source_location rloc = this->right_->location();
6174 this->right_ = Expression::make_integer(&val, right_type,
6183 // Get a tree for a binary expression.
6186 Binary_expression::do_get_tree(Translate_context* context)
6188 tree left = this->left_->get_tree(context);
6189 tree right = this->right_->get_tree(context);
6191 if (left == error_mark_node || right == error_mark_node)
6192 return error_mark_node;
6194 enum tree_code code;
6195 bool use_left_type = true;
6196 bool is_shift_op = false;
6200 case OPERATOR_NOTEQ:
6205 return Expression::comparison_tree(context, this->op_,
6206 this->left_->type(), left,
6207 this->right_->type(), right,
6211 code = TRUTH_ORIF_EXPR;
6212 use_left_type = false;
6214 case OPERATOR_ANDAND:
6215 code = TRUTH_ANDIF_EXPR;
6216 use_left_type = false;
6221 case OPERATOR_MINUS:
6225 code = BIT_IOR_EXPR;
6228 code = BIT_XOR_EXPR;
6235 Type *t = this->left_->type();
6236 if (t->float_type() != NULL || t->complex_type() != NULL)
6239 code = TRUNC_DIV_EXPR;
6243 code = TRUNC_MOD_EXPR;
6245 case OPERATOR_LSHIFT:
6249 case OPERATOR_RSHIFT:
6254 code = BIT_AND_EXPR;
6256 case OPERATOR_BITCLEAR:
6257 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
6258 code = BIT_AND_EXPR;
6264 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
6266 if (this->left_->type()->is_string_type())
6268 go_assert(this->op_ == OPERATOR_PLUS);
6269 Type* st = Type::make_string_type();
6270 tree string_type = type_to_tree(st->get_backend(context->gogo()));
6271 static tree string_plus_decl;
6272 return Gogo::call_builtin(&string_plus_decl,
6283 tree compute_type = excess_precision_type(type);
6284 if (compute_type != NULL_TREE)
6286 left = ::convert(compute_type, left);
6287 right = ::convert(compute_type, right);
6290 tree eval_saved = NULL_TREE;
6293 // Make sure the values are evaluated.
6294 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
6296 left = save_expr(left);
6299 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
6301 right = save_expr(right);
6302 if (eval_saved == NULL_TREE)
6305 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
6306 void_type_node, eval_saved, right);
6310 tree ret = fold_build2_loc(this->location(),
6312 compute_type != NULL_TREE ? compute_type : type,
6315 if (compute_type != NULL_TREE)
6316 ret = ::convert(type, ret);
6318 // In Go, a shift larger than the size of the type is well-defined.
6319 // This is not true in GENERIC, so we need to insert a conditional.
6322 go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
6323 go_assert(this->left_->type()->integer_type() != NULL);
6324 int bits = TYPE_PRECISION(TREE_TYPE(left));
6326 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
6327 build_int_cst_type(TREE_TYPE(right), bits));
6329 tree overflow_result = fold_convert_loc(this->location(),
6332 if (this->op_ == OPERATOR_RSHIFT
6333 && !this->left_->type()->integer_type()->is_unsigned())
6335 tree neg = fold_build2_loc(this->location(), LT_EXPR,
6336 boolean_type_node, left,
6337 fold_convert_loc(this->location(),
6339 integer_zero_node));
6340 tree neg_one = fold_build2_loc(this->location(),
6341 MINUS_EXPR, TREE_TYPE(left),
6342 fold_convert_loc(this->location(),
6345 fold_convert_loc(this->location(),
6348 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
6349 TREE_TYPE(left), neg, neg_one,
6353 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
6354 compare, ret, overflow_result);
6356 if (eval_saved != NULL_TREE)
6357 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
6358 TREE_TYPE(ret), eval_saved, ret);
6364 // Export a binary expression.
6367 Binary_expression::do_export(Export* exp) const
6369 exp->write_c_string("(");
6370 this->left_->export_expression(exp);
6374 exp->write_c_string(" || ");
6376 case OPERATOR_ANDAND:
6377 exp->write_c_string(" && ");
6380 exp->write_c_string(" == ");
6382 case OPERATOR_NOTEQ:
6383 exp->write_c_string(" != ");
6386 exp->write_c_string(" < ");
6389 exp->write_c_string(" <= ");
6392 exp->write_c_string(" > ");
6395 exp->write_c_string(" >= ");
6398 exp->write_c_string(" + ");
6400 case OPERATOR_MINUS:
6401 exp->write_c_string(" - ");
6404 exp->write_c_string(" | ");
6407 exp->write_c_string(" ^ ");
6410 exp->write_c_string(" * ");
6413 exp->write_c_string(" / ");
6416 exp->write_c_string(" % ");
6418 case OPERATOR_LSHIFT:
6419 exp->write_c_string(" << ");
6421 case OPERATOR_RSHIFT:
6422 exp->write_c_string(" >> ");
6425 exp->write_c_string(" & ");
6427 case OPERATOR_BITCLEAR:
6428 exp->write_c_string(" &^ ");
6433 this->right_->export_expression(exp);
6434 exp->write_c_string(")");
6437 // Import a binary expression.
6440 Binary_expression::do_import(Import* imp)
6442 imp->require_c_string("(");
6444 Expression* left = Expression::import_expression(imp);
6447 if (imp->match_c_string(" || "))
6452 else if (imp->match_c_string(" && "))
6454 op = OPERATOR_ANDAND;
6457 else if (imp->match_c_string(" == "))
6462 else if (imp->match_c_string(" != "))
6464 op = OPERATOR_NOTEQ;
6467 else if (imp->match_c_string(" < "))
6472 else if (imp->match_c_string(" <= "))
6477 else if (imp->match_c_string(" > "))
6482 else if (imp->match_c_string(" >= "))
6487 else if (imp->match_c_string(" + "))
6492 else if (imp->match_c_string(" - "))
6494 op = OPERATOR_MINUS;
6497 else if (imp->match_c_string(" | "))
6502 else if (imp->match_c_string(" ^ "))
6507 else if (imp->match_c_string(" * "))
6512 else if (imp->match_c_string(" / "))
6517 else if (imp->match_c_string(" % "))
6522 else if (imp->match_c_string(" << "))
6524 op = OPERATOR_LSHIFT;
6527 else if (imp->match_c_string(" >> "))
6529 op = OPERATOR_RSHIFT;
6532 else if (imp->match_c_string(" & "))
6537 else if (imp->match_c_string(" &^ "))
6539 op = OPERATOR_BITCLEAR;
6544 error_at(imp->location(), "unrecognized binary operator");
6545 return Expression::make_error(imp->location());
6548 Expression* right = Expression::import_expression(imp);
6550 imp->require_c_string(")");
6552 return Expression::make_binary(op, left, right, imp->location());
6555 // Dump ast representation of a binary expression.
6558 Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
6560 ast_dump_context->ostream() << "(";
6561 ast_dump_context->dump_expression(this->left_);
6562 ast_dump_context->ostream() << " ";
6563 ast_dump_context->dump_operator(this->op_);
6564 ast_dump_context->ostream() << " ";
6565 ast_dump_context->dump_expression(this->right_);
6566 ast_dump_context->ostream() << ") ";
6569 // Make a binary expression.
6572 Expression::make_binary(Operator op, Expression* left, Expression* right,
6573 source_location location)
6575 return new Binary_expression(op, left, right, location);
6578 // Implement a comparison.
6581 Expression::comparison_tree(Translate_context* context, Operator op,
6582 Type* left_type, tree left_tree,
6583 Type* right_type, tree right_tree,
6584 source_location location)
6586 enum tree_code code;
6592 case OPERATOR_NOTEQ:
6611 if (left_type->is_string_type() && right_type->is_string_type())
6613 Type* st = Type::make_string_type();
6614 tree string_type = type_to_tree(st->get_backend(context->gogo()));
6615 static tree string_compare_decl;
6616 left_tree = Gogo::call_builtin(&string_compare_decl,
6625 right_tree = build_int_cst_type(integer_type_node, 0);
6627 else if ((left_type->interface_type() != NULL
6628 && right_type->interface_type() == NULL
6629 && !right_type->is_nil_type())
6630 || (left_type->interface_type() == NULL
6631 && !left_type->is_nil_type()
6632 && right_type->interface_type() != NULL))
6634 // Comparing an interface value to a non-interface value.
6635 if (left_type->interface_type() == NULL)
6637 std::swap(left_type, right_type);
6638 std::swap(left_tree, right_tree);
6641 // The right operand is not an interface. We need to take its
6642 // address if it is not a pointer.
6645 if (right_type->points_to() != NULL)
6647 make_tmp = NULL_TREE;
6650 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
6652 make_tmp = NULL_TREE;
6653 arg = build_fold_addr_expr_loc(location, right_tree);
6654 if (DECL_P(right_tree))
6655 TREE_ADDRESSABLE(right_tree) = 1;
6659 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6660 get_name(right_tree));
6661 DECL_IGNORED_P(tmp) = 0;
6662 DECL_INITIAL(tmp) = right_tree;
6663 TREE_ADDRESSABLE(tmp) = 1;
6664 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6665 SET_EXPR_LOCATION(make_tmp, location);
6666 arg = build_fold_addr_expr_loc(location, tmp);
6668 arg = fold_convert_loc(location, ptr_type_node, arg);
6670 tree descriptor = right_type->type_descriptor_pointer(context->gogo(),
6673 if (left_type->interface_type()->is_empty())
6675 static tree empty_interface_value_compare_decl;
6676 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6678 "__go_empty_interface_value_compare",
6681 TREE_TYPE(left_tree),
6683 TREE_TYPE(descriptor),
6687 if (left_tree == error_mark_node)
6688 return error_mark_node;
6689 // This can panic if the type is not comparable.
6690 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6694 static tree interface_value_compare_decl;
6695 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6697 "__go_interface_value_compare",
6700 TREE_TYPE(left_tree),
6702 TREE_TYPE(descriptor),
6706 if (left_tree == error_mark_node)
6707 return error_mark_node;
6708 // This can panic if the type is not comparable.
6709 TREE_NOTHROW(interface_value_compare_decl) = 0;
6711 right_tree = build_int_cst_type(integer_type_node, 0);
6713 if (make_tmp != NULL_TREE)
6714 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6717 else if (left_type->interface_type() != NULL
6718 && right_type->interface_type() != NULL)
6720 if (left_type->interface_type()->is_empty()
6721 && right_type->interface_type()->is_empty())
6723 static tree empty_interface_compare_decl;
6724 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6726 "__go_empty_interface_compare",
6729 TREE_TYPE(left_tree),
6731 TREE_TYPE(right_tree),
6733 if (left_tree == error_mark_node)
6734 return error_mark_node;
6735 // This can panic if the type is uncomparable.
6736 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6738 else if (!left_type->interface_type()->is_empty()
6739 && !right_type->interface_type()->is_empty())
6741 static tree interface_compare_decl;
6742 left_tree = Gogo::call_builtin(&interface_compare_decl,
6744 "__go_interface_compare",
6747 TREE_TYPE(left_tree),
6749 TREE_TYPE(right_tree),
6751 if (left_tree == error_mark_node)
6752 return error_mark_node;
6753 // This can panic if the type is uncomparable.
6754 TREE_NOTHROW(interface_compare_decl) = 0;
6758 if (left_type->interface_type()->is_empty())
6760 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6761 std::swap(left_type, right_type);
6762 std::swap(left_tree, right_tree);
6764 go_assert(!left_type->interface_type()->is_empty());
6765 go_assert(right_type->interface_type()->is_empty());
6766 static tree interface_empty_compare_decl;
6767 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
6769 "__go_interface_empty_compare",
6772 TREE_TYPE(left_tree),
6774 TREE_TYPE(right_tree),
6776 if (left_tree == error_mark_node)
6777 return error_mark_node;
6778 // This can panic if the type is uncomparable.
6779 TREE_NOTHROW(interface_empty_compare_decl) = 0;
6782 right_tree = build_int_cst_type(integer_type_node, 0);
6785 if (left_type->is_nil_type()
6786 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6788 std::swap(left_type, right_type);
6789 std::swap(left_tree, right_tree);
6792 if (right_type->is_nil_type())
6794 if (left_type->array_type() != NULL
6795 && left_type->array_type()->length() == NULL)
6797 Array_type* at = left_type->array_type();
6798 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6799 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6801 else if (left_type->interface_type() != NULL)
6803 // An interface is nil if the first field is nil.
6804 tree left_type_tree = TREE_TYPE(left_tree);
6805 go_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6806 tree field = TYPE_FIELDS(left_type_tree);
6807 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6809 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6813 go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6814 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6818 if (left_tree == error_mark_node || right_tree == error_mark_node)
6819 return error_mark_node;
6821 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6822 if (CAN_HAVE_LOCATION_P(ret))
6823 SET_EXPR_LOCATION(ret, location);
6827 // Class Bound_method_expression.
6832 Bound_method_expression::do_traverse(Traverse* traverse)
6834 return Expression::traverse(&this->expr_, traverse);
6837 // Return the type of a bound method expression. The type of this
6838 // object is really the type of the method with no receiver. We
6839 // should be able to get away with just returning the type of the
6843 Bound_method_expression::do_type()
6845 if (this->method_->is_function())
6846 return this->method_->func_value()->type();
6847 else if (this->method_->is_function_declaration())
6848 return this->method_->func_declaration_value()->type();
6850 return Type::make_error_type();
6853 // Determine the types of a method expression.
6856 Bound_method_expression::do_determine_type(const Type_context*)
6858 Function_type* fntype = this->type()->function_type();
6859 if (fntype == NULL || !fntype->is_method())
6860 this->expr_->determine_type_no_context();
6863 Type_context subcontext(fntype->receiver()->type(), false);
6864 this->expr_->determine_type(&subcontext);
6868 // Check the types of a method expression.
6871 Bound_method_expression::do_check_types(Gogo*)
6873 if (!this->method_->is_function()
6874 && !this->method_->is_function_declaration())
6875 this->report_error(_("object is not a method"));
6878 Type* rtype = this->type()->function_type()->receiver()->type()->deref();
6879 Type* etype = (this->expr_type_ != NULL
6881 : this->expr_->type());
6882 etype = etype->deref();
6883 if (!Type::are_identical(rtype, etype, true, NULL))
6884 this->report_error(_("method type does not match object type"));
6888 // Get the tree for a method expression. There is no standard tree
6889 // representation for this. The only places it may currently be used
6890 // are in a Call_expression or a Go_statement, which will take it
6891 // apart directly. So this has nothing to do at present.
6894 Bound_method_expression::do_get_tree(Translate_context*)
6896 error_at(this->location(), "reference to method other than calling it");
6897 return error_mark_node;
6900 // Dump ast representation of a bound method expression.
6903 Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
6906 if (this->expr_type_ != NULL)
6907 ast_dump_context->ostream() << "(";
6908 ast_dump_context->dump_expression(this->expr_);
6909 if (this->expr_type_ != NULL)
6911 ast_dump_context->ostream() << ":";
6912 ast_dump_context->dump_type(this->expr_type_);
6913 ast_dump_context->ostream() << ")";
6916 ast_dump_context->ostream() << "." << this->method_->name();
6919 // Make a method expression.
6921 Bound_method_expression*
6922 Expression::make_bound_method(Expression* expr, Named_object* method,
6923 source_location location)
6925 return new Bound_method_expression(expr, method, location);
6928 // Class Builtin_call_expression. This is used for a call to a
6929 // builtin function.
6931 class Builtin_call_expression : public Call_expression
6934 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6935 bool is_varargs, source_location location);
6938 // This overrides Call_expression::do_lower.
6940 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
6943 do_is_constant() const;
6946 do_integer_constant_value(bool, mpz_t, Type**) const;
6949 do_float_constant_value(mpfr_t, Type**) const;
6952 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6958 do_determine_type(const Type_context*);
6961 do_check_types(Gogo*);
6966 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6967 this->args()->copy(),
6973 do_get_tree(Translate_context*);
6976 do_export(Export*) const;
6979 do_is_recover_call() const;
6982 do_set_recover_arg(Expression*);
6985 // The builtin functions.
6986 enum Builtin_function_code
6990 // Predeclared builtin functions.
7006 // Builtin functions from the unsafe package.
7019 real_imag_type(Type*);
7022 complex_type(Type*);
7028 check_int_value(Expression*);
7030 // A pointer back to the general IR structure. This avoids a global
7031 // variable, or passing it around everywhere.
7033 // The builtin function being called.
7034 Builtin_function_code code_;
7035 // Used to stop endless loops when the length of an array uses len
7036 // or cap of the array itself.
7040 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
7042 Expression_list* args,
7044 source_location location)
7045 : Call_expression(fn, args, is_varargs, location),
7046 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
7048 Func_expression* fnexp = this->fn()->func_expression();
7049 go_assert(fnexp != NULL);
7050 const std::string& name(fnexp->named_object()->name());
7051 if (name == "append")
7052 this->code_ = BUILTIN_APPEND;
7053 else if (name == "cap")
7054 this->code_ = BUILTIN_CAP;
7055 else if (name == "close")
7056 this->code_ = BUILTIN_CLOSE;
7057 else if (name == "complex")
7058 this->code_ = BUILTIN_COMPLEX;
7059 else if (name == "copy")
7060 this->code_ = BUILTIN_COPY;
7061 else if (name == "imag")
7062 this->code_ = BUILTIN_IMAG;
7063 else if (name == "len")
7064 this->code_ = BUILTIN_LEN;
7065 else if (name == "make")
7066 this->code_ = BUILTIN_MAKE;
7067 else if (name == "new")
7068 this->code_ = BUILTIN_NEW;
7069 else if (name == "panic")
7070 this->code_ = BUILTIN_PANIC;
7071 else if (name == "print")
7072 this->code_ = BUILTIN_PRINT;
7073 else if (name == "println")
7074 this->code_ = BUILTIN_PRINTLN;
7075 else if (name == "real")
7076 this->code_ = BUILTIN_REAL;
7077 else if (name == "recover")
7078 this->code_ = BUILTIN_RECOVER;
7079 else if (name == "Alignof")
7080 this->code_ = BUILTIN_ALIGNOF;
7081 else if (name == "Offsetof")
7082 this->code_ = BUILTIN_OFFSETOF;
7083 else if (name == "Sizeof")
7084 this->code_ = BUILTIN_SIZEOF;
7089 // Return whether this is a call to recover. This is a virtual
7090 // function called from the parent class.
7093 Builtin_call_expression::do_is_recover_call() const
7095 if (this->classification() == EXPRESSION_ERROR)
7097 return this->code_ == BUILTIN_RECOVER;
7100 // Set the argument for a call to recover.
7103 Builtin_call_expression::do_set_recover_arg(Expression* arg)
7105 const Expression_list* args = this->args();
7106 go_assert(args == NULL || args->empty());
7107 Expression_list* new_args = new Expression_list();
7108 new_args->push_back(arg);
7109 this->set_args(new_args);
7112 // A traversal class which looks for a call expression.
7114 class Find_call_expression : public Traverse
7117 Find_call_expression()
7118 : Traverse(traverse_expressions),
7123 expression(Expression**);
7127 { return this->found_; }
7134 Find_call_expression::expression(Expression** pexpr)
7136 if ((*pexpr)->call_expression() != NULL)
7138 this->found_ = true;
7139 return TRAVERSE_EXIT;
7141 return TRAVERSE_CONTINUE;
7144 // Lower a builtin call expression. This turns new and make into
7145 // specific expressions. We also convert to a constant if we can.
7148 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function,
7149 Statement_inserter* inserter, int)
7151 if (this->classification() == EXPRESSION_ERROR)
7154 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
7156 this->report_error(_("invalid use of %<...%> with builtin function"));
7157 return Expression::make_error(this->location());
7160 if (this->code_ == BUILTIN_NEW)
7162 const Expression_list* args = this->args();
7163 if (args == NULL || args->size() < 1)
7164 this->report_error(_("not enough arguments"));
7165 else if (args->size() > 1)
7166 this->report_error(_("too many arguments"));
7169 Expression* arg = args->front();
7170 if (!arg->is_type_expression())
7172 error_at(arg->location(), "expected type");
7173 this->set_is_error();
7176 return Expression::make_allocation(arg->type(), this->location());
7179 else if (this->code_ == BUILTIN_MAKE)
7180 return this->lower_make();
7181 else if (this->is_constant())
7183 // We can only lower len and cap if there are no function calls
7184 // in the arguments. Otherwise we have to make the call.
7185 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
7187 Expression* arg = this->one_arg();
7188 if (!arg->is_constant())
7190 Find_call_expression find_call;
7191 Expression::traverse(&arg, &find_call);
7192 if (find_call.found())
7200 if (this->integer_constant_value(true, ival, &type))
7202 Expression* ret = Expression::make_integer(&ival, type,
7211 if (this->float_constant_value(rval, &type))
7213 Expression* ret = Expression::make_float(&rval, type,
7221 if (this->complex_constant_value(rval, imag, &type))
7223 Expression* ret = Expression::make_complex(&rval, &imag, type,
7232 else if (this->code_ == BUILTIN_RECOVER)
7234 if (function != NULL)
7235 function->func_value()->set_calls_recover();
7238 // Calling recover outside of a function always returns the
7239 // nil empty interface.
7240 Type* eface = Type::make_interface_type(NULL, this->location());
7241 return Expression::make_cast(eface,
7242 Expression::make_nil(this->location()),
7246 else if (this->code_ == BUILTIN_APPEND)
7248 // Lower the varargs.
7249 const Expression_list* args = this->args();
7250 if (args == NULL || args->empty())
7252 Type* slice_type = args->front()->type();
7253 if (!slice_type->is_open_array_type())
7255 error_at(args->front()->location(), "argument 1 must be a slice");
7256 this->set_is_error();
7259 this->lower_varargs(gogo, function, inserter, slice_type, 2);
7265 // Lower a make expression.
7268 Builtin_call_expression::lower_make()
7270 source_location loc = this->location();
7272 const Expression_list* args = this->args();
7273 if (args == NULL || args->size() < 1)
7275 this->report_error(_("not enough arguments"));
7276 return Expression::make_error(this->location());
7279 Expression_list::const_iterator parg = args->begin();
7281 Expression* first_arg = *parg;
7282 if (!first_arg->is_type_expression())
7284 error_at(first_arg->location(), "expected type");
7285 this->set_is_error();
7286 return Expression::make_error(this->location());
7288 Type* type = first_arg->type();
7290 bool is_slice = false;
7291 bool is_map = false;
7292 bool is_chan = false;
7293 if (type->is_open_array_type())
7295 else if (type->map_type() != NULL)
7297 else if (type->channel_type() != NULL)
7301 this->report_error(_("invalid type for make function"));
7302 return Expression::make_error(this->location());
7306 Expression* len_arg;
7307 if (parg == args->end())
7311 this->report_error(_("length required when allocating a slice"));
7312 return Expression::make_error(this->location());
7316 mpz_init_set_ui(zval, 0);
7317 len_arg = Expression::make_integer(&zval, NULL, loc);
7323 if (!this->check_int_value(len_arg))
7325 this->report_error(_("bad size for make"));
7326 return Expression::make_error(this->location());
7331 Expression* cap_arg = NULL;
7332 if (is_slice && parg != args->end())
7335 if (!this->check_int_value(cap_arg))
7337 this->report_error(_("bad capacity when making slice"));
7338 return Expression::make_error(this->location());
7343 if (parg != args->end())
7345 this->report_error(_("too many arguments to make"));
7346 return Expression::make_error(this->location());
7349 source_location type_loc = first_arg->location();
7350 Expression* type_arg;
7351 if (is_slice || is_chan)
7352 type_arg = Expression::make_type_descriptor(type, type_loc);
7354 type_arg = Expression::make_map_descriptor(type->map_type(), type_loc);
7361 if (cap_arg == NULL)
7362 call = Runtime::make_call(Runtime::MAKESLICE1, loc, 2, type_arg,
7365 call = Runtime::make_call(Runtime::MAKESLICE2, loc, 3, type_arg,
7369 call = Runtime::make_call(Runtime::MAKEMAP, loc, 2, type_arg, len_arg);
7371 call = Runtime::make_call(Runtime::MAKECHAN, loc, 2, type_arg, len_arg);
7375 return Expression::make_unsafe_cast(type, call, loc);
7378 // Return whether an expression has an integer value. Report an error
7379 // if not. This is used when handling calls to the predeclared make
7383 Builtin_call_expression::check_int_value(Expression* e)
7385 if (e->type()->integer_type() != NULL)
7388 // Check for a floating point constant with integer value.
7393 if (e->float_constant_value(fval, &dummy) && mpfr_integer_p(fval))
7400 mpfr_clear_overflow();
7401 mpfr_clear_erangeflag();
7402 mpfr_get_z(ival, fval, GMP_RNDN);
7403 if (!mpfr_overflow_p()
7404 && !mpfr_erangeflag_p()
7405 && mpz_sgn(ival) >= 0)
7407 Named_type* ntype = Type::lookup_integer_type("int");
7408 Integer_type* inttype = ntype->integer_type();
7410 mpz_init_set_ui(max, 1);
7411 mpz_mul_2exp(max, max, inttype->bits() - 1);
7412 ok = mpz_cmp(ival, max) < 0;
7429 // Return the type of the real or imag functions, given the type of
7430 // the argument. We need to map complex to float, complex64 to
7431 // float32, and complex128 to float64, so it has to be done by name.
7432 // This returns NULL if it can't figure out the type.
7435 Builtin_call_expression::real_imag_type(Type* arg_type)
7437 if (arg_type == NULL || arg_type->is_abstract())
7439 Named_type* nt = arg_type->named_type();
7442 while (nt->real_type()->named_type() != NULL)
7443 nt = nt->real_type()->named_type();
7444 if (nt->name() == "complex64")
7445 return Type::lookup_float_type("float32");
7446 else if (nt->name() == "complex128")
7447 return Type::lookup_float_type("float64");
7452 // Return the type of the complex function, given the type of one of the
7453 // argments. Like real_imag_type, we have to map by name.
7456 Builtin_call_expression::complex_type(Type* arg_type)
7458 if (arg_type == NULL || arg_type->is_abstract())
7460 Named_type* nt = arg_type->named_type();
7463 while (nt->real_type()->named_type() != NULL)
7464 nt = nt->real_type()->named_type();
7465 if (nt->name() == "float32")
7466 return Type::lookup_complex_type("complex64");
7467 else if (nt->name() == "float64")
7468 return Type::lookup_complex_type("complex128");
7473 // Return a single argument, or NULL if there isn't one.
7476 Builtin_call_expression::one_arg() const
7478 const Expression_list* args = this->args();
7479 if (args->size() != 1)
7481 return args->front();
7484 // Return whether this is constant: len of a string, or len or cap of
7485 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
7488 Builtin_call_expression::do_is_constant() const
7490 switch (this->code_)
7498 Expression* arg = this->one_arg();
7501 Type* arg_type = arg->type();
7503 if (arg_type->points_to() != NULL
7504 && arg_type->points_to()->array_type() != NULL
7505 && !arg_type->points_to()->is_open_array_type())
7506 arg_type = arg_type->points_to();
7508 if (arg_type->array_type() != NULL
7509 && arg_type->array_type()->length() != NULL)
7512 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7515 bool ret = arg->is_constant();
7516 this->seen_ = false;
7522 case BUILTIN_SIZEOF:
7523 case BUILTIN_ALIGNOF:
7524 return this->one_arg() != NULL;
7526 case BUILTIN_OFFSETOF:
7528 Expression* arg = this->one_arg();
7531 return arg->field_reference_expression() != NULL;
7534 case BUILTIN_COMPLEX:
7536 const Expression_list* args = this->args();
7537 if (args != NULL && args->size() == 2)
7538 return args->front()->is_constant() && args->back()->is_constant();
7545 Expression* arg = this->one_arg();
7546 return arg != NULL && arg->is_constant();
7556 // Return an integer constant value if possible.
7559 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
7563 if (this->code_ == BUILTIN_LEN
7564 || this->code_ == BUILTIN_CAP)
7566 Expression* arg = this->one_arg();
7569 Type* arg_type = arg->type();
7571 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7574 if (arg->string_constant_value(&sval))
7576 mpz_set_ui(val, sval.length());
7577 *ptype = Type::lookup_integer_type("int");
7582 if (arg_type->points_to() != NULL
7583 && arg_type->points_to()->array_type() != NULL
7584 && !arg_type->points_to()->is_open_array_type())
7585 arg_type = arg_type->points_to();
7587 if (arg_type->array_type() != NULL
7588 && arg_type->array_type()->length() != NULL)
7592 Expression* e = arg_type->array_type()->length();
7594 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
7595 this->seen_ = false;
7598 *ptype = Type::lookup_integer_type("int");
7603 else if (this->code_ == BUILTIN_SIZEOF
7604 || this->code_ == BUILTIN_ALIGNOF)
7606 Expression* arg = this->one_arg();
7609 Type* arg_type = arg->type();
7610 if (arg_type->is_error())
7612 if (arg_type->is_abstract())
7614 if (arg_type->named_type() != NULL)
7615 arg_type->named_type()->convert(this->gogo_);
7616 tree arg_type_tree = type_to_tree(arg_type->get_backend(this->gogo_));
7617 if (arg_type_tree == error_mark_node)
7619 unsigned long val_long;
7620 if (this->code_ == BUILTIN_SIZEOF)
7622 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
7623 go_assert(TREE_CODE(type_size) == INTEGER_CST);
7624 if (TREE_INT_CST_HIGH(type_size) != 0)
7626 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
7627 val_long = static_cast<unsigned long>(val_wide);
7628 if (val_long != val_wide)
7631 else if (this->code_ == BUILTIN_ALIGNOF)
7633 if (arg->field_reference_expression() == NULL)
7634 val_long = go_type_alignment(arg_type_tree);
7637 // Calling unsafe.Alignof(s.f) returns the alignment of
7638 // the type of f when it is used as a field in a struct.
7639 val_long = go_field_alignment(arg_type_tree);
7644 mpz_set_ui(val, val_long);
7648 else if (this->code_ == BUILTIN_OFFSETOF)
7650 Expression* arg = this->one_arg();
7653 Field_reference_expression* farg = arg->field_reference_expression();
7656 Expression* struct_expr = farg->expr();
7657 Type* st = struct_expr->type();
7658 if (st->struct_type() == NULL)
7660 if (st->named_type() != NULL)
7661 st->named_type()->convert(this->gogo_);
7662 tree struct_tree = type_to_tree(st->get_backend(this->gogo_));
7663 go_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
7664 tree field = TYPE_FIELDS(struct_tree);
7665 for (unsigned int index = farg->field_index(); index > 0; --index)
7667 field = DECL_CHAIN(field);
7668 go_assert(field != NULL_TREE);
7670 HOST_WIDE_INT offset_wide = int_byte_position (field);
7671 if (offset_wide < 0)
7673 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
7674 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
7676 mpz_set_ui(val, offset_long);
7682 // Return a floating point constant value if possible.
7685 Builtin_call_expression::do_float_constant_value(mpfr_t val,
7688 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
7690 Expression* arg = this->one_arg();
7701 if (arg->complex_constant_value(real, imag, &type))
7703 if (this->code_ == BUILTIN_REAL)
7704 mpfr_set(val, real, GMP_RNDN);
7706 mpfr_set(val, imag, GMP_RNDN);
7707 *ptype = Builtin_call_expression::real_imag_type(type);
7719 // Return a complex constant value if possible.
7722 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
7725 if (this->code_ == BUILTIN_COMPLEX)
7727 const Expression_list* args = this->args();
7728 if (args == NULL || args->size() != 2)
7734 if (!args->front()->float_constant_value(r, &rtype))
7745 if (args->back()->float_constant_value(i, &itype)
7746 && Type::are_identical(rtype, itype, false, NULL))
7748 mpfr_set(real, r, GMP_RNDN);
7749 mpfr_set(imag, i, GMP_RNDN);
7750 *ptype = Builtin_call_expression::complex_type(rtype);
7766 Builtin_call_expression::do_type()
7768 switch (this->code_)
7770 case BUILTIN_INVALID:
7777 const Expression_list* args = this->args();
7778 if (args == NULL || args->empty())
7779 return Type::make_error_type();
7780 return Type::make_pointer_type(args->front()->type());
7786 case BUILTIN_ALIGNOF:
7787 case BUILTIN_OFFSETOF:
7788 case BUILTIN_SIZEOF:
7789 return Type::lookup_integer_type("int");
7794 case BUILTIN_PRINTLN:
7795 return Type::make_void_type();
7797 case BUILTIN_RECOVER:
7798 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
7800 case BUILTIN_APPEND:
7802 const Expression_list* args = this->args();
7803 if (args == NULL || args->empty())
7804 return Type::make_error_type();
7805 return args->front()->type();
7811 Expression* arg = this->one_arg();
7813 return Type::make_error_type();
7814 Type* t = arg->type();
7815 if (t->is_abstract())
7816 t = t->make_non_abstract_type();
7817 t = Builtin_call_expression::real_imag_type(t);
7819 t = Type::make_error_type();
7823 case BUILTIN_COMPLEX:
7825 const Expression_list* args = this->args();
7826 if (args == NULL || args->size() != 2)
7827 return Type::make_error_type();
7828 Type* t = args->front()->type();
7829 if (t->is_abstract())
7831 t = args->back()->type();
7832 if (t->is_abstract())
7833 t = t->make_non_abstract_type();
7835 t = Builtin_call_expression::complex_type(t);
7837 t = Type::make_error_type();
7843 // Determine the type.
7846 Builtin_call_expression::do_determine_type(const Type_context* context)
7848 if (!this->determining_types())
7851 this->fn()->determine_type_no_context();
7853 const Expression_list* args = this->args();
7856 Type* arg_type = NULL;
7857 switch (this->code_)
7860 case BUILTIN_PRINTLN:
7861 // Do not force a large integer constant to "int".
7867 arg_type = Builtin_call_expression::complex_type(context->type);
7871 case BUILTIN_COMPLEX:
7873 // For the complex function the type of one operand can
7874 // determine the type of the other, as in a binary expression.
7875 arg_type = Builtin_call_expression::real_imag_type(context->type);
7876 if (args != NULL && args->size() == 2)
7878 Type* t1 = args->front()->type();
7879 Type* t2 = args->front()->type();
7880 if (!t1->is_abstract())
7882 else if (!t2->is_abstract())
7896 for (Expression_list::const_iterator pa = args->begin();
7900 Type_context subcontext;
7901 subcontext.type = arg_type;
7905 // We want to print large constants, we so can't just
7906 // use the appropriate nonabstract type. Use uint64 for
7907 // an integer if we know it is nonnegative, otherwise
7908 // use int64 for a integer, otherwise use float64 for a
7909 // float or complex128 for a complex.
7910 Type* want_type = NULL;
7911 Type* atype = (*pa)->type();
7912 if (atype->is_abstract())
7914 if (atype->integer_type() != NULL)
7919 if (this->integer_constant_value(true, val, &dummy)
7920 && mpz_sgn(val) >= 0)
7921 want_type = Type::lookup_integer_type("uint64");
7923 want_type = Type::lookup_integer_type("int64");
7926 else if (atype->float_type() != NULL)
7927 want_type = Type::lookup_float_type("float64");
7928 else if (atype->complex_type() != NULL)
7929 want_type = Type::lookup_complex_type("complex128");
7930 else if (atype->is_abstract_string_type())
7931 want_type = Type::lookup_string_type();
7932 else if (atype->is_abstract_boolean_type())
7933 want_type = Type::lookup_bool_type();
7936 subcontext.type = want_type;
7940 (*pa)->determine_type(&subcontext);
7945 // If there is exactly one argument, return true. Otherwise give an
7946 // error message and return false.
7949 Builtin_call_expression::check_one_arg()
7951 const Expression_list* args = this->args();
7952 if (args == NULL || args->size() < 1)
7954 this->report_error(_("not enough arguments"));
7957 else if (args->size() > 1)
7959 this->report_error(_("too many arguments"));
7962 if (args->front()->is_error_expression()
7963 || args->front()->type()->is_error())
7965 this->set_is_error();
7971 // Check argument types for a builtin function.
7974 Builtin_call_expression::do_check_types(Gogo*)
7976 switch (this->code_)
7978 case BUILTIN_INVALID:
7986 // The single argument may be either a string or an array or a
7987 // map or a channel, or a pointer to a closed array.
7988 if (this->check_one_arg())
7990 Type* arg_type = this->one_arg()->type();
7991 if (arg_type->points_to() != NULL
7992 && arg_type->points_to()->array_type() != NULL
7993 && !arg_type->points_to()->is_open_array_type())
7994 arg_type = arg_type->points_to();
7995 if (this->code_ == BUILTIN_CAP)
7997 if (!arg_type->is_error()
7998 && arg_type->array_type() == NULL
7999 && arg_type->channel_type() == NULL)
8000 this->report_error(_("argument must be array or slice "
8005 if (!arg_type->is_error()
8006 && !arg_type->is_string_type()
8007 && arg_type->array_type() == NULL
8008 && arg_type->map_type() == NULL
8009 && arg_type->channel_type() == NULL)
8010 this->report_error(_("argument must be string or "
8011 "array or slice or map or channel"));
8018 case BUILTIN_PRINTLN:
8020 const Expression_list* args = this->args();
8023 if (this->code_ == BUILTIN_PRINT)
8024 warning_at(this->location(), 0,
8025 "no arguments for builtin function %<%s%>",
8026 (this->code_ == BUILTIN_PRINT
8032 for (Expression_list::const_iterator p = args->begin();
8036 Type* type = (*p)->type();
8037 if (type->is_error()
8038 || type->is_string_type()
8039 || type->integer_type() != NULL
8040 || type->float_type() != NULL
8041 || type->complex_type() != NULL
8042 || type->is_boolean_type()
8043 || type->points_to() != NULL
8044 || type->interface_type() != NULL
8045 || type->channel_type() != NULL
8046 || type->map_type() != NULL
8047 || type->function_type() != NULL
8048 || type->is_open_array_type())
8051 this->report_error(_("unsupported argument type to "
8052 "builtin function"));
8059 if (this->check_one_arg())
8061 if (this->one_arg()->type()->channel_type() == NULL)
8062 this->report_error(_("argument must be channel"));
8067 case BUILTIN_SIZEOF:
8068 case BUILTIN_ALIGNOF:
8069 this->check_one_arg();
8072 case BUILTIN_RECOVER:
8073 if (this->args() != NULL && !this->args()->empty())
8074 this->report_error(_("too many arguments"));
8077 case BUILTIN_OFFSETOF:
8078 if (this->check_one_arg())
8080 Expression* arg = this->one_arg();
8081 if (arg->field_reference_expression() == NULL)
8082 this->report_error(_("argument must be a field reference"));
8088 const Expression_list* args = this->args();
8089 if (args == NULL || args->size() < 2)
8091 this->report_error(_("not enough arguments"));
8094 else if (args->size() > 2)
8096 this->report_error(_("too many arguments"));
8099 Type* arg1_type = args->front()->type();
8100 Type* arg2_type = args->back()->type();
8101 if (arg1_type->is_error() || arg2_type->is_error())
8105 if (arg1_type->is_open_array_type())
8106 e1 = arg1_type->array_type()->element_type();
8109 this->report_error(_("left argument must be a slice"));
8114 if (arg2_type->is_open_array_type())
8115 e2 = arg2_type->array_type()->element_type();
8116 else if (arg2_type->is_string_type())
8117 e2 = Type::lookup_integer_type("uint8");
8120 this->report_error(_("right argument must be a slice or a string"));
8124 if (!Type::are_identical(e1, e2, true, NULL))
8125 this->report_error(_("element types must be the same"));
8129 case BUILTIN_APPEND:
8131 const Expression_list* args = this->args();
8132 if (args == NULL || args->size() < 2)
8134 this->report_error(_("not enough arguments"));
8137 if (args->size() > 2)
8139 this->report_error(_("too many arguments"));
8143 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
8147 this->report_error(_("arguments 1 and 2 have different types"));
8150 error_at(this->location(),
8151 "arguments 1 and 2 have different types (%s)",
8153 this->set_is_error();
8161 if (this->check_one_arg())
8163 if (this->one_arg()->type()->complex_type() == NULL)
8164 this->report_error(_("argument must have complex type"));
8168 case BUILTIN_COMPLEX:
8170 const Expression_list* args = this->args();
8171 if (args == NULL || args->size() < 2)
8172 this->report_error(_("not enough arguments"));
8173 else if (args->size() > 2)
8174 this->report_error(_("too many arguments"));
8175 else if (args->front()->is_error_expression()
8176 || args->front()->type()->is_error()
8177 || args->back()->is_error_expression()
8178 || args->back()->type()->is_error())
8179 this->set_is_error();
8180 else if (!Type::are_identical(args->front()->type(),
8181 args->back()->type(), true, NULL))
8182 this->report_error(_("complex arguments must have identical types"));
8183 else if (args->front()->type()->float_type() == NULL)
8184 this->report_error(_("complex arguments must have "
8185 "floating-point type"));
8194 // Return the tree for a builtin function.
8197 Builtin_call_expression::do_get_tree(Translate_context* context)
8199 Gogo* gogo = context->gogo();
8200 source_location location = this->location();
8201 switch (this->code_)
8203 case BUILTIN_INVALID:
8211 const Expression_list* args = this->args();
8212 go_assert(args != NULL && args->size() == 1);
8213 Expression* arg = *args->begin();
8214 Type* arg_type = arg->type();
8218 go_assert(saw_errors());
8219 return error_mark_node;
8223 tree arg_tree = arg->get_tree(context);
8225 this->seen_ = false;
8227 if (arg_tree == error_mark_node)
8228 return error_mark_node;
8230 if (arg_type->points_to() != NULL)
8232 arg_type = arg_type->points_to();
8233 go_assert(arg_type->array_type() != NULL
8234 && !arg_type->is_open_array_type());
8235 go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
8236 arg_tree = build_fold_indirect_ref(arg_tree);
8240 if (this->code_ == BUILTIN_LEN)
8242 if (arg_type->is_string_type())
8243 val_tree = String_type::length_tree(gogo, arg_tree);
8244 else if (arg_type->array_type() != NULL)
8248 go_assert(saw_errors());
8249 return error_mark_node;
8252 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
8253 this->seen_ = false;
8255 else if (arg_type->map_type() != NULL)
8257 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
8258 static tree map_len_fndecl;
8259 val_tree = Gogo::call_builtin(&map_len_fndecl,
8267 else if (arg_type->channel_type() != NULL)
8269 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
8270 static tree chan_len_fndecl;
8271 val_tree = Gogo::call_builtin(&chan_len_fndecl,
8284 if (arg_type->array_type() != NULL)
8288 go_assert(saw_errors());
8289 return error_mark_node;
8292 val_tree = arg_type->array_type()->capacity_tree(gogo,
8294 this->seen_ = false;
8296 else if (arg_type->channel_type() != NULL)
8298 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
8299 static tree chan_cap_fndecl;
8300 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
8312 if (val_tree == error_mark_node)
8313 return error_mark_node;
8315 Type* int_type = Type::lookup_integer_type("int");
8316 tree type_tree = type_to_tree(int_type->get_backend(gogo));
8317 if (type_tree == TREE_TYPE(val_tree))
8320 return fold(convert_to_integer(type_tree, val_tree));
8324 case BUILTIN_PRINTLN:
8326 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
8327 tree stmt_list = NULL_TREE;
8329 const Expression_list* call_args = this->args();
8330 if (call_args != NULL)
8332 for (Expression_list::const_iterator p = call_args->begin();
8333 p != call_args->end();
8336 if (is_ln && p != call_args->begin())
8338 static tree print_space_fndecl;
8339 tree call = Gogo::call_builtin(&print_space_fndecl,
8344 if (call == error_mark_node)
8345 return error_mark_node;
8346 append_to_statement_list(call, &stmt_list);
8349 Type* type = (*p)->type();
8351 tree arg = (*p)->get_tree(context);
8352 if (arg == error_mark_node)
8353 return error_mark_node;
8357 if (type->is_string_type())
8359 static tree print_string_fndecl;
8360 pfndecl = &print_string_fndecl;
8361 fnname = "__go_print_string";
8363 else if (type->integer_type() != NULL
8364 && type->integer_type()->is_unsigned())
8366 static tree print_uint64_fndecl;
8367 pfndecl = &print_uint64_fndecl;
8368 fnname = "__go_print_uint64";
8369 Type* itype = Type::lookup_integer_type("uint64");
8370 Btype* bitype = itype->get_backend(gogo);
8371 arg = fold_convert_loc(location, type_to_tree(bitype), arg);
8373 else if (type->integer_type() != NULL)
8375 static tree print_int64_fndecl;
8376 pfndecl = &print_int64_fndecl;
8377 fnname = "__go_print_int64";
8378 Type* itype = Type::lookup_integer_type("int64");
8379 Btype* bitype = itype->get_backend(gogo);
8380 arg = fold_convert_loc(location, type_to_tree(bitype), arg);
8382 else if (type->float_type() != NULL)
8384 static tree print_double_fndecl;
8385 pfndecl = &print_double_fndecl;
8386 fnname = "__go_print_double";
8387 arg = fold_convert_loc(location, double_type_node, arg);
8389 else if (type->complex_type() != NULL)
8391 static tree print_complex_fndecl;
8392 pfndecl = &print_complex_fndecl;
8393 fnname = "__go_print_complex";
8394 arg = fold_convert_loc(location, complex_double_type_node,
8397 else if (type->is_boolean_type())
8399 static tree print_bool_fndecl;
8400 pfndecl = &print_bool_fndecl;
8401 fnname = "__go_print_bool";
8403 else if (type->points_to() != NULL
8404 || type->channel_type() != NULL
8405 || type->map_type() != NULL
8406 || type->function_type() != NULL)
8408 static tree print_pointer_fndecl;
8409 pfndecl = &print_pointer_fndecl;
8410 fnname = "__go_print_pointer";
8411 arg = fold_convert_loc(location, ptr_type_node, arg);
8413 else if (type->interface_type() != NULL)
8415 if (type->interface_type()->is_empty())
8417 static tree print_empty_interface_fndecl;
8418 pfndecl = &print_empty_interface_fndecl;
8419 fnname = "__go_print_empty_interface";
8423 static tree print_interface_fndecl;
8424 pfndecl = &print_interface_fndecl;
8425 fnname = "__go_print_interface";
8428 else if (type->is_open_array_type())
8430 static tree print_slice_fndecl;
8431 pfndecl = &print_slice_fndecl;
8432 fnname = "__go_print_slice";
8437 tree call = Gogo::call_builtin(pfndecl,
8444 if (call == error_mark_node)
8445 return error_mark_node;
8446 append_to_statement_list(call, &stmt_list);
8452 static tree print_nl_fndecl;
8453 tree call = Gogo::call_builtin(&print_nl_fndecl,
8458 if (call == error_mark_node)
8459 return error_mark_node;
8460 append_to_statement_list(call, &stmt_list);
8468 const Expression_list* args = this->args();
8469 go_assert(args != NULL && args->size() == 1);
8470 Expression* arg = args->front();
8471 tree arg_tree = arg->get_tree(context);
8472 if (arg_tree == error_mark_node)
8473 return error_mark_node;
8474 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
8475 arg_tree = Expression::convert_for_assignment(context, empty,
8477 arg_tree, location);
8478 static tree panic_fndecl;
8479 tree call = Gogo::call_builtin(&panic_fndecl,
8484 TREE_TYPE(arg_tree),
8486 if (call == error_mark_node)
8487 return error_mark_node;
8488 // This function will throw an exception.
8489 TREE_NOTHROW(panic_fndecl) = 0;
8490 // This function will not return.
8491 TREE_THIS_VOLATILE(panic_fndecl) = 1;
8495 case BUILTIN_RECOVER:
8497 // The argument is set when building recover thunks. It's a
8498 // boolean value which is true if we can recover a value now.
8499 const Expression_list* args = this->args();
8500 go_assert(args != NULL && args->size() == 1);
8501 Expression* arg = args->front();
8502 tree arg_tree = arg->get_tree(context);
8503 if (arg_tree == error_mark_node)
8504 return error_mark_node;
8506 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
8507 tree empty_tree = type_to_tree(empty->get_backend(context->gogo()));
8509 Type* nil_type = Type::make_nil_type();
8510 Expression* nil = Expression::make_nil(location);
8511 tree nil_tree = nil->get_tree(context);
8512 tree empty_nil_tree = Expression::convert_for_assignment(context,
8518 // We need to handle a deferred call to recover specially,
8519 // because it changes whether it can recover a panic or not.
8520 // See test7 in test/recover1.go.
8522 if (this->is_deferred())
8524 static tree deferred_recover_fndecl;
8525 call = Gogo::call_builtin(&deferred_recover_fndecl,
8527 "__go_deferred_recover",
8533 static tree recover_fndecl;
8534 call = Gogo::call_builtin(&recover_fndecl,
8540 if (call == error_mark_node)
8541 return error_mark_node;
8542 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
8543 call, empty_nil_tree);
8548 const Expression_list* args = this->args();
8549 go_assert(args != NULL && args->size() == 1);
8550 Expression* arg = args->front();
8551 tree arg_tree = arg->get_tree(context);
8552 if (arg_tree == error_mark_node)
8553 return error_mark_node;
8554 static tree close_fndecl;
8555 return Gogo::call_builtin(&close_fndecl,
8557 "__go_builtin_close",
8560 TREE_TYPE(arg_tree),
8564 case BUILTIN_SIZEOF:
8565 case BUILTIN_OFFSETOF:
8566 case BUILTIN_ALIGNOF:
8571 bool b = this->integer_constant_value(true, val, &dummy);
8574 go_assert(saw_errors());
8575 return error_mark_node;
8577 Type* int_type = Type::lookup_integer_type("int");
8578 tree type = type_to_tree(int_type->get_backend(gogo));
8579 tree ret = Expression::integer_constant_tree(val, type);
8586 const Expression_list* args = this->args();
8587 go_assert(args != NULL && args->size() == 2);
8588 Expression* arg1 = args->front();
8589 Expression* arg2 = args->back();
8591 tree arg1_tree = arg1->get_tree(context);
8592 tree arg2_tree = arg2->get_tree(context);
8593 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8594 return error_mark_node;
8596 Type* arg1_type = arg1->type();
8597 Array_type* at = arg1_type->array_type();
8598 arg1_tree = save_expr(arg1_tree);
8599 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
8600 tree arg1_len = at->length_tree(gogo, arg1_tree);
8601 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
8602 return error_mark_node;
8604 Type* arg2_type = arg2->type();
8607 if (arg2_type->is_open_array_type())
8609 at = arg2_type->array_type();
8610 arg2_tree = save_expr(arg2_tree);
8611 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8612 arg2_len = at->length_tree(gogo, arg2_tree);
8616 arg2_tree = save_expr(arg2_tree);
8617 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8618 arg2_len = String_type::length_tree(gogo, arg2_tree);
8620 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8621 return error_mark_node;
8623 arg1_len = save_expr(arg1_len);
8624 arg2_len = save_expr(arg2_len);
8625 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
8626 fold_build2_loc(location, LT_EXPR,
8628 arg1_len, arg2_len),
8629 arg1_len, arg2_len);
8630 len = save_expr(len);
8632 Type* element_type = at->element_type();
8633 Btype* element_btype = element_type->get_backend(gogo);
8634 tree element_type_tree = type_to_tree(element_btype);
8635 if (element_type_tree == error_mark_node)
8636 return error_mark_node;
8637 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8638 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
8640 bytecount = fold_build2_loc(location, MULT_EXPR,
8641 TREE_TYPE(element_size),
8642 bytecount, element_size);
8643 bytecount = fold_convert_loc(location, size_type_node, bytecount);
8645 arg1_val = fold_convert_loc(location, ptr_type_node, arg1_val);
8646 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8648 static tree copy_fndecl;
8649 tree call = Gogo::call_builtin(©_fndecl,
8660 if (call == error_mark_node)
8661 return error_mark_node;
8663 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
8667 case BUILTIN_APPEND:
8669 const Expression_list* args = this->args();
8670 go_assert(args != NULL && args->size() == 2);
8671 Expression* arg1 = args->front();
8672 Expression* arg2 = args->back();
8674 tree arg1_tree = arg1->get_tree(context);
8675 tree arg2_tree = arg2->get_tree(context);
8676 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8677 return error_mark_node;
8679 Array_type* at = arg1->type()->array_type();
8680 Type* element_type = at->element_type();
8682 arg2_tree = Expression::convert_for_assignment(context, at,
8686 if (arg2_tree == error_mark_node)
8687 return error_mark_node;
8689 arg2_tree = save_expr(arg2_tree);
8690 tree arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8691 tree arg2_len = at->length_tree(gogo, arg2_tree);
8692 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8693 return error_mark_node;
8694 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8695 arg2_len = fold_convert_loc(location, size_type_node, arg2_len);
8697 tree element_type_tree = type_to_tree(element_type->get_backend(gogo));
8698 if (element_type_tree == error_mark_node)
8699 return error_mark_node;
8700 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8701 element_size = fold_convert_loc(location, size_type_node,
8704 // We rebuild the decl each time since the slice types may
8706 tree append_fndecl = NULL_TREE;
8707 return Gogo::call_builtin(&append_fndecl,
8711 TREE_TYPE(arg1_tree),
8712 TREE_TYPE(arg1_tree),
8725 const Expression_list* args = this->args();
8726 go_assert(args != NULL && args->size() == 1);
8727 Expression* arg = args->front();
8728 tree arg_tree = arg->get_tree(context);
8729 if (arg_tree == error_mark_node)
8730 return error_mark_node;
8731 go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
8732 if (this->code_ == BUILTIN_REAL)
8733 return fold_build1_loc(location, REALPART_EXPR,
8734 TREE_TYPE(TREE_TYPE(arg_tree)),
8737 return fold_build1_loc(location, IMAGPART_EXPR,
8738 TREE_TYPE(TREE_TYPE(arg_tree)),
8742 case BUILTIN_COMPLEX:
8744 const Expression_list* args = this->args();
8745 go_assert(args != NULL && args->size() == 2);
8746 tree r = args->front()->get_tree(context);
8747 tree i = args->back()->get_tree(context);
8748 if (r == error_mark_node || i == error_mark_node)
8749 return error_mark_node;
8750 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
8751 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
8752 go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8753 return fold_build2_loc(location, COMPLEX_EXPR,
8754 build_complex_type(TREE_TYPE(r)),
8763 // We have to support exporting a builtin call expression, because
8764 // code can set a constant to the result of a builtin expression.
8767 Builtin_call_expression::do_export(Export* exp) const
8774 if (this->integer_constant_value(true, val, &dummy))
8776 Integer_expression::export_integer(exp, val);
8785 if (this->float_constant_value(fval, &dummy))
8787 Float_expression::export_float(exp, fval);
8799 if (this->complex_constant_value(real, imag, &dummy))
8801 Complex_expression::export_complex(exp, real, imag);
8810 error_at(this->location(), "value is not constant");
8814 // A trailing space lets us reliably identify the end of the number.
8815 exp->write_c_string(" ");
8818 // Class Call_expression.
8823 Call_expression::do_traverse(Traverse* traverse)
8825 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8826 return TRAVERSE_EXIT;
8827 if (this->args_ != NULL)
8829 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8830 return TRAVERSE_EXIT;
8832 return TRAVERSE_CONTINUE;
8835 // Lower a call statement.
8838 Call_expression::do_lower(Gogo* gogo, Named_object* function,
8839 Statement_inserter* inserter, int)
8841 source_location loc = this->location();
8843 // A type cast can look like a function call.
8844 if (this->fn_->is_type_expression()
8845 && this->args_ != NULL
8846 && this->args_->size() == 1)
8847 return Expression::make_cast(this->fn_->type(), this->args_->front(),
8850 // Recognize a call to a builtin function.
8851 Func_expression* fne = this->fn_->func_expression();
8853 && fne->named_object()->is_function_declaration()
8854 && fne->named_object()->func_declaration_value()->type()->is_builtin())
8855 return new Builtin_call_expression(gogo, this->fn_, this->args_,
8856 this->is_varargs_, loc);
8858 // Handle an argument which is a call to a function which returns
8859 // multiple results.
8860 if (this->args_ != NULL
8861 && this->args_->size() == 1
8862 && this->args_->front()->call_expression() != NULL
8863 && this->fn_->type()->function_type() != NULL)
8865 Function_type* fntype = this->fn_->type()->function_type();
8866 size_t rc = this->args_->front()->call_expression()->result_count();
8868 && fntype->parameters() != NULL
8869 && (fntype->parameters()->size() == rc
8870 || (fntype->is_varargs()
8871 && fntype->parameters()->size() - 1 <= rc)))
8873 Call_expression* call = this->args_->front()->call_expression();
8874 Expression_list* args = new Expression_list;
8875 for (size_t i = 0; i < rc; ++i)
8876 args->push_back(Expression::make_call_result(call, i));
8877 // We can't return a new call expression here, because this
8878 // one may be referenced by Call_result expressions. We
8879 // also can't delete the old arguments, because we may still
8880 // traverse them somewhere up the call stack. FIXME.
8885 // If this call returns multiple results, create a temporary
8886 // variable for each result.
8887 size_t rc = this->result_count();
8888 if (rc > 1 && this->results_ == NULL)
8890 std::vector<Temporary_statement*>* temps =
8891 new std::vector<Temporary_statement*>;
8893 const Typed_identifier_list* results =
8894 this->fn_->type()->function_type()->results();
8895 for (Typed_identifier_list::const_iterator p = results->begin();
8896 p != results->end();
8899 Temporary_statement* temp = Statement::make_temporary(p->type(),
8901 inserter->insert(temp);
8902 temps->push_back(temp);
8904 this->results_ = temps;
8907 // Handle a call to a varargs function by packaging up the extra
8909 if (this->fn_->type()->function_type() != NULL
8910 && this->fn_->type()->function_type()->is_varargs())
8912 Function_type* fntype = this->fn_->type()->function_type();
8913 const Typed_identifier_list* parameters = fntype->parameters();
8914 go_assert(parameters != NULL && !parameters->empty());
8915 Type* varargs_type = parameters->back().type();
8916 this->lower_varargs(gogo, function, inserter, varargs_type,
8917 parameters->size());
8920 // If this is call to a method, call the method directly passing the
8921 // object as the first parameter.
8922 Bound_method_expression* bme = this->fn_->bound_method_expression();
8925 Named_object* method = bme->method();
8926 Expression* first_arg = bme->first_argument();
8928 // We always pass a pointer when calling a method.
8929 if (first_arg->type()->points_to() == NULL
8930 && !first_arg->type()->is_error())
8932 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc);
8933 // We may need to create a temporary variable so that we can
8934 // take the address. We can't do that here because it will
8935 // mess up the order of evaluation.
8936 Unary_expression* ue = static_cast<Unary_expression*>(first_arg);
8937 ue->set_create_temp();
8940 // If we are calling a method which was inherited from an
8941 // embedded struct, and the method did not get a stub, then the
8942 // first type may be wrong.
8943 Type* fatype = bme->first_argument_type();
8946 if (fatype->points_to() == NULL)
8947 fatype = Type::make_pointer_type(fatype);
8948 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc);
8951 Expression_list* new_args = new Expression_list();
8952 new_args->push_back(first_arg);
8953 if (this->args_ != NULL)
8955 for (Expression_list::const_iterator p = this->args_->begin();
8956 p != this->args_->end();
8958 new_args->push_back(*p);
8961 // We have to change in place because this structure may be
8962 // referenced by Call_result_expressions. We can't delete the
8963 // old arguments, because we may be traversing them up in some
8965 this->args_ = new_args;
8966 this->fn_ = Expression::make_func_reference(method, NULL,
8973 // Lower a call to a varargs function. FUNCTION is the function in
8974 // which the call occurs--it's not the function we are calling.
8975 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8976 // PARAM_COUNT is the number of parameters of the function we are
8977 // calling; the last of these parameters will be the varargs
8981 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
8982 Statement_inserter* inserter,
8983 Type* varargs_type, size_t param_count)
8985 if (this->varargs_are_lowered_)
8988 source_location loc = this->location();
8990 go_assert(param_count > 0);
8991 go_assert(varargs_type->is_open_array_type());
8993 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8994 if (arg_count < param_count - 1)
8996 // Not enough arguments; will be caught in check_types.
9000 Expression_list* old_args = this->args_;
9001 Expression_list* new_args = new Expression_list();
9002 bool push_empty_arg = false;
9003 if (old_args == NULL || old_args->empty())
9005 go_assert(param_count == 1);
9006 push_empty_arg = true;
9010 Expression_list::const_iterator pa;
9012 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
9014 if (static_cast<size_t>(i) == param_count)
9016 new_args->push_back(*pa);
9019 // We have reached the varargs parameter.
9021 bool issued_error = false;
9022 if (pa == old_args->end())
9023 push_empty_arg = true;
9024 else if (pa + 1 == old_args->end() && this->is_varargs_)
9025 new_args->push_back(*pa);
9026 else if (this->is_varargs_)
9028 this->report_error(_("too many arguments"));
9033 Type* element_type = varargs_type->array_type()->element_type();
9034 Expression_list* vals = new Expression_list;
9035 for (; pa != old_args->end(); ++pa, ++i)
9037 // Check types here so that we get a better message.
9038 Type* patype = (*pa)->type();
9039 source_location paloc = (*pa)->location();
9040 if (!this->check_argument_type(i, element_type, patype,
9041 paloc, issued_error))
9043 vals->push_back(*pa);
9046 Expression::make_slice_composite_literal(varargs_type, vals, loc);
9047 gogo->lower_expression(function, inserter, &val);
9048 new_args->push_back(val);
9053 new_args->push_back(Expression::make_nil(loc));
9055 // We can't return a new call expression here, because this one may
9056 // be referenced by Call_result expressions. FIXME. We can't
9057 // delete OLD_ARGS because we may have both a Call_expression and a
9058 // Builtin_call_expression which refer to them. FIXME.
9059 this->args_ = new_args;
9060 this->varargs_are_lowered_ = true;
9063 // Get the function type. This can return NULL in error cases.
9066 Call_expression::get_function_type() const
9068 return this->fn_->type()->function_type();
9071 // Return the number of values which this call will return.
9074 Call_expression::result_count() const
9076 const Function_type* fntype = this->get_function_type();
9079 if (fntype->results() == NULL)
9081 return fntype->results()->size();
9084 // Return the temporary which holds a result.
9086 Temporary_statement*
9087 Call_expression::result(size_t i) const
9089 go_assert(this->results_ != NULL
9090 && this->results_->size() > i);
9091 return (*this->results_)[i];
9094 // Return whether this is a call to the predeclared function recover.
9097 Call_expression::is_recover_call() const
9099 return this->do_is_recover_call();
9102 // Set the argument to the recover function.
9105 Call_expression::set_recover_arg(Expression* arg)
9107 this->do_set_recover_arg(arg);
9110 // Virtual functions also implemented by Builtin_call_expression.
9113 Call_expression::do_is_recover_call() const
9119 Call_expression::do_set_recover_arg(Expression*)
9124 // We have found an error with this call expression; return true if
9125 // we should report it.
9128 Call_expression::issue_error()
9130 if (this->issued_error_)
9134 this->issued_error_ = true;
9142 Call_expression::do_type()
9144 if (this->type_ != NULL)
9148 Function_type* fntype = this->get_function_type();
9150 return Type::make_error_type();
9152 const Typed_identifier_list* results = fntype->results();
9153 if (results == NULL)
9154 ret = Type::make_void_type();
9155 else if (results->size() == 1)
9156 ret = results->begin()->type();
9158 ret = Type::make_call_multiple_result_type(this);
9165 // Determine types for a call expression. We can use the function
9166 // parameter types to set the types of the arguments.
9169 Call_expression::do_determine_type(const Type_context*)
9171 if (!this->determining_types())
9174 this->fn_->determine_type_no_context();
9175 Function_type* fntype = this->get_function_type();
9176 const Typed_identifier_list* parameters = NULL;
9178 parameters = fntype->parameters();
9179 if (this->args_ != NULL)
9181 Typed_identifier_list::const_iterator pt;
9182 if (parameters != NULL)
9183 pt = parameters->begin();
9185 for (Expression_list::const_iterator pa = this->args_->begin();
9186 pa != this->args_->end();
9192 // If this is a method, the first argument is the
9194 if (fntype != NULL && fntype->is_method())
9196 Type* rtype = fntype->receiver()->type();
9197 // The receiver is always passed as a pointer.
9198 if (rtype->points_to() == NULL)
9199 rtype = Type::make_pointer_type(rtype);
9200 Type_context subcontext(rtype, false);
9201 (*pa)->determine_type(&subcontext);
9206 if (parameters != NULL && pt != parameters->end())
9208 Type_context subcontext(pt->type(), false);
9209 (*pa)->determine_type(&subcontext);
9213 (*pa)->determine_type_no_context();
9218 // Called when determining types for a Call_expression. Return true
9219 // if we should go ahead, false if they have already been determined.
9222 Call_expression::determining_types()
9224 if (this->types_are_determined_)
9228 this->types_are_determined_ = true;
9233 // Check types for parameter I.
9236 Call_expression::check_argument_type(int i, const Type* parameter_type,
9237 const Type* argument_type,
9238 source_location argument_location,
9243 if (this->are_hidden_fields_ok_)
9244 ok = Type::are_assignable_hidden_ok(parameter_type, argument_type,
9247 ok = Type::are_assignable(parameter_type, argument_type, &reason);
9253 error_at(argument_location, "argument %d has incompatible type", i);
9255 error_at(argument_location,
9256 "argument %d has incompatible type (%s)",
9259 this->set_is_error();
9268 Call_expression::do_check_types(Gogo*)
9270 Function_type* fntype = this->get_function_type();
9273 if (!this->fn_->type()->is_error())
9274 this->report_error(_("expected function"));
9278 bool is_method = fntype->is_method();
9281 go_assert(this->args_ != NULL && !this->args_->empty());
9282 Type* rtype = fntype->receiver()->type();
9283 Expression* first_arg = this->args_->front();
9284 // The language permits copying hidden fields for a method
9285 // receiver. We dereference the values since receivers are
9286 // always passed as pointers.
9288 if (!Type::are_assignable_hidden_ok(rtype->deref(),
9289 first_arg->type()->deref(),
9293 this->report_error(_("incompatible type for receiver"));
9296 error_at(this->location(),
9297 "incompatible type for receiver (%s)",
9299 this->set_is_error();
9304 // Note that varargs was handled by the lower_varargs() method, so
9305 // we don't have to worry about it here.
9307 const Typed_identifier_list* parameters = fntype->parameters();
9308 if (this->args_ == NULL)
9310 if (parameters != NULL && !parameters->empty())
9311 this->report_error(_("not enough arguments"));
9313 else if (parameters == NULL)
9315 if (!is_method || this->args_->size() > 1)
9316 this->report_error(_("too many arguments"));
9321 Expression_list::const_iterator pa = this->args_->begin();
9324 for (Typed_identifier_list::const_iterator pt = parameters->begin();
9325 pt != parameters->end();
9328 if (pa == this->args_->end())
9330 this->report_error(_("not enough arguments"));
9333 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
9334 (*pa)->location(), false);
9336 if (pa != this->args_->end())
9337 this->report_error(_("too many arguments"));
9341 // Return whether we have to use a temporary variable to ensure that
9342 // we evaluate this call expression in order. If the call returns no
9343 // results then it will inevitably be executed last.
9346 Call_expression::do_must_eval_in_order() const
9348 return this->result_count() > 0;
9351 // Get the function and the first argument to use when calling an
9352 // interface method.
9355 Call_expression::interface_method_function(
9356 Translate_context* context,
9357 Interface_field_reference_expression* interface_method,
9358 tree* first_arg_ptr)
9360 tree expr = interface_method->expr()->get_tree(context);
9361 if (expr == error_mark_node)
9362 return error_mark_node;
9363 expr = save_expr(expr);
9364 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
9365 if (first_arg == error_mark_node)
9366 return error_mark_node;
9367 *first_arg_ptr = first_arg;
9368 return interface_method->get_function_tree(context, expr);
9371 // Build the call expression.
9374 Call_expression::do_get_tree(Translate_context* context)
9376 if (this->tree_ != NULL_TREE)
9379 Function_type* fntype = this->get_function_type();
9381 return error_mark_node;
9383 if (this->fn_->is_error_expression())
9384 return error_mark_node;
9386 Gogo* gogo = context->gogo();
9387 source_location location = this->location();
9389 Func_expression* func = this->fn_->func_expression();
9390 Interface_field_reference_expression* interface_method =
9391 this->fn_->interface_field_reference_expression();
9392 const bool has_closure = func != NULL && func->closure() != NULL;
9393 const bool is_interface_method = interface_method != NULL;
9397 if (this->args_ == NULL || this->args_->empty())
9399 nargs = is_interface_method ? 1 : 0;
9400 args = nargs == 0 ? NULL : new tree[nargs];
9402 else if (fntype->parameters() == NULL || fntype->parameters()->empty())
9404 // Passing a receiver parameter.
9405 go_assert(!is_interface_method
9406 && fntype->is_method()
9407 && this->args_->size() == 1);
9409 args = new tree[nargs];
9410 args[0] = this->args_->front()->get_tree(context);
9414 const Typed_identifier_list* params = fntype->parameters();
9416 nargs = this->args_->size();
9417 int i = is_interface_method ? 1 : 0;
9419 args = new tree[nargs];
9421 Typed_identifier_list::const_iterator pp = params->begin();
9422 Expression_list::const_iterator pe = this->args_->begin();
9423 if (!is_interface_method && fntype->is_method())
9425 args[i] = (*pe)->get_tree(context);
9429 for (; pe != this->args_->end(); ++pe, ++pp, ++i)
9431 go_assert(pp != params->end());
9432 tree arg_val = (*pe)->get_tree(context);
9433 args[i] = Expression::convert_for_assignment(context,
9438 if (args[i] == error_mark_node)
9441 return error_mark_node;
9444 go_assert(pp == params->end());
9445 go_assert(i == nargs);
9448 tree rettype = TREE_TYPE(TREE_TYPE(type_to_tree(fntype->get_backend(gogo))));
9449 if (rettype == error_mark_node)
9452 return error_mark_node;
9457 fn = func->get_tree_without_closure(gogo);
9458 else if (!is_interface_method)
9459 fn = this->fn_->get_tree(context);
9461 fn = this->interface_method_function(context, interface_method, &args[0]);
9463 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
9466 return error_mark_node;
9470 if (TREE_CODE(fndecl) == ADDR_EXPR)
9471 fndecl = TREE_OPERAND(fndecl, 0);
9473 // Add a type cast in case the type of the function is a recursive
9474 // type which refers to itself.
9475 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
9477 tree fnt = type_to_tree(fntype->get_backend(gogo));
9478 if (fnt == error_mark_node)
9479 return error_mark_node;
9480 fn = fold_convert_loc(location, fnt, fn);
9483 // This is to support builtin math functions when using 80387 math.
9484 tree excess_type = NULL_TREE;
9485 if (TREE_CODE(fndecl) == FUNCTION_DECL
9486 && DECL_IS_BUILTIN(fndecl)
9487 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
9489 && ((SCALAR_FLOAT_TYPE_P(rettype)
9490 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
9491 || (COMPLEX_FLOAT_TYPE_P(rettype)
9492 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
9494 excess_type = excess_precision_type(TREE_TYPE(args[0]));
9495 if (excess_type != NULL_TREE)
9497 tree excess_fndecl = mathfn_built_in(excess_type,
9498 DECL_FUNCTION_CODE(fndecl));
9499 if (excess_fndecl == NULL_TREE)
9500 excess_type = NULL_TREE;
9503 fn = build_fold_addr_expr_loc(location, excess_fndecl);
9504 for (int i = 0; i < nargs; ++i)
9505 args[i] = ::convert(excess_type, args[i]);
9510 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
9514 SET_EXPR_LOCATION(ret, location);
9518 tree closure_tree = func->closure()->get_tree(context);
9519 if (closure_tree != error_mark_node)
9520 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
9523 // If this is a recursive function type which returns itself, as in
9525 // we have used ptr_type_node for the return type. Add a cast here
9526 // to the correct type.
9527 if (TREE_TYPE(ret) == ptr_type_node)
9529 tree t = type_to_tree(this->type()->base()->get_backend(gogo));
9530 ret = fold_convert_loc(location, t, ret);
9533 if (excess_type != NULL_TREE)
9535 // Calling convert here can undo our excess precision change.
9536 // That may or may not be a bug in convert_to_real.
9537 ret = build1(NOP_EXPR, rettype, ret);
9540 if (this->results_ != NULL)
9541 ret = this->set_results(context, ret);
9548 // Set the result variables if this call returns multiple results.
9551 Call_expression::set_results(Translate_context* context, tree call_tree)
9553 tree stmt_list = NULL_TREE;
9555 call_tree = save_expr(call_tree);
9557 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9559 go_assert(saw_errors());
9563 source_location loc = this->location();
9564 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9565 size_t rc = this->result_count();
9566 for (size_t i = 0; i < rc; ++i, field = DECL_CHAIN(field))
9568 go_assert(field != NULL_TREE);
9570 Temporary_statement* temp = this->result(i);
9571 Temporary_reference_expression* ref =
9572 Expression::make_temporary_reference(temp, loc);
9573 ref->set_is_lvalue();
9574 tree temp_tree = ref->get_tree(context);
9575 if (temp_tree == error_mark_node)
9578 tree val_tree = build3_loc(loc, COMPONENT_REF, TREE_TYPE(field),
9579 call_tree, field, NULL_TREE);
9580 tree set_tree = build2_loc(loc, MODIFY_EXPR, void_type_node, temp_tree,
9583 append_to_statement_list(set_tree, &stmt_list);
9585 go_assert(field == NULL_TREE);
9587 return save_expr(stmt_list);
9590 // Dump ast representation for a call expressin.
9593 Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
9595 this->fn_->dump_expression(ast_dump_context);
9596 ast_dump_context->ostream() << "(";
9598 ast_dump_context->dump_expression_list(this->args_);
9600 ast_dump_context->ostream() << ") ";
9603 // Make a call expression.
9606 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
9607 source_location location)
9609 return new Call_expression(fn, args, is_varargs, location);
9612 // A single result from a call which returns multiple results.
9614 class Call_result_expression : public Expression
9617 Call_result_expression(Call_expression* call, unsigned int index)
9618 : Expression(EXPRESSION_CALL_RESULT, call->location()),
9619 call_(call), index_(index)
9624 do_traverse(Traverse*);
9630 do_determine_type(const Type_context*);
9633 do_check_types(Gogo*);
9638 return new Call_result_expression(this->call_->call_expression(),
9643 do_must_eval_in_order() const
9647 do_get_tree(Translate_context*);
9650 do_dump_expression(Ast_dump_context*) const;
9653 // The underlying call expression.
9655 // Which result we want.
9656 unsigned int index_;
9659 // Traverse a call result.
9662 Call_result_expression::do_traverse(Traverse* traverse)
9664 if (traverse->remember_expression(this->call_))
9666 // We have already traversed the call expression.
9667 return TRAVERSE_CONTINUE;
9669 return Expression::traverse(&this->call_, traverse);
9675 Call_result_expression::do_type()
9677 if (this->classification() == EXPRESSION_ERROR)
9678 return Type::make_error_type();
9680 // THIS->CALL_ can be replaced with a temporary reference due to
9681 // Call_expression::do_must_eval_in_order when there is an error.
9682 Call_expression* ce = this->call_->call_expression();
9685 this->set_is_error();
9686 return Type::make_error_type();
9688 Function_type* fntype = ce->get_function_type();
9691 if (ce->issue_error())
9692 this->report_error(_("expected function"));
9693 this->set_is_error();
9694 return Type::make_error_type();
9696 const Typed_identifier_list* results = fntype->results();
9697 if (results == NULL || results->size() < 2)
9699 if (ce->issue_error())
9700 this->report_error(_("number of results does not match "
9701 "number of values"));
9702 return Type::make_error_type();
9704 Typed_identifier_list::const_iterator pr = results->begin();
9705 for (unsigned int i = 0; i < this->index_; ++i)
9707 if (pr == results->end())
9711 if (pr == results->end())
9713 if (ce->issue_error())
9714 this->report_error(_("number of results does not match "
9715 "number of values"));
9716 return Type::make_error_type();
9721 // Check the type. Just make sure that we trigger the warning in
9725 Call_result_expression::do_check_types(Gogo*)
9730 // Determine the type. We have nothing to do here, but the 0 result
9731 // needs to pass down to the caller.
9734 Call_result_expression::do_determine_type(const Type_context*)
9736 this->call_->determine_type_no_context();
9739 // Return the tree. We just refer to the temporary set by the call
9740 // expression. We don't do this at lowering time because it makes it
9741 // hard to evaluate the call at the right time.
9744 Call_result_expression::do_get_tree(Translate_context* context)
9746 Call_expression* ce = this->call_->call_expression();
9747 go_assert(ce != NULL);
9748 Temporary_statement* ts = ce->result(this->index_);
9749 Expression* ref = Expression::make_temporary_reference(ts, this->location());
9750 return ref->get_tree(context);
9753 // Dump ast representation for a call result expression.
9756 Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
9759 // FIXME: Wouldn't it be better if the call is assigned to a temporary
9760 // (struct) and the fields are referenced instead.
9761 ast_dump_context->ostream() << this->index_ << "@(";
9762 ast_dump_context->dump_expression(this->call_);
9763 ast_dump_context->ostream() << ")";
9766 // Make a reference to a single result of a call which returns
9767 // multiple results.
9770 Expression::make_call_result(Call_expression* call, unsigned int index)
9772 return new Call_result_expression(call, index);
9775 // Class Index_expression.
9780 Index_expression::do_traverse(Traverse* traverse)
9782 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9783 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9784 || (this->end_ != NULL
9785 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9786 return TRAVERSE_EXIT;
9787 return TRAVERSE_CONTINUE;
9790 // Lower an index expression. This converts the generic index
9791 // expression into an array index, a string index, or a map index.
9794 Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
9796 source_location location = this->location();
9797 Expression* left = this->left_;
9798 Expression* start = this->start_;
9799 Expression* end = this->end_;
9801 Type* type = left->type();
9802 if (type->is_error())
9803 return Expression::make_error(location);
9804 else if (left->is_type_expression())
9806 error_at(location, "attempt to index type expression");
9807 return Expression::make_error(location);
9809 else if (type->array_type() != NULL)
9810 return Expression::make_array_index(left, start, end, location);
9811 else if (type->points_to() != NULL
9812 && type->points_to()->array_type() != NULL
9813 && !type->points_to()->is_open_array_type())
9815 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9817 return Expression::make_array_index(deref, start, end, location);
9819 else if (type->is_string_type())
9820 return Expression::make_string_index(left, start, end, location);
9821 else if (type->map_type() != NULL)
9825 error_at(location, "invalid slice of map");
9826 return Expression::make_error(location);
9828 Map_index_expression* ret = Expression::make_map_index(left, start,
9830 if (this->is_lvalue_)
9831 ret->set_is_lvalue();
9837 "attempt to index object which is not array, string, or map");
9838 return Expression::make_error(location);
9842 // Write an indexed expression (expr[expr:expr] or expr[expr]) to a
9846 Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context,
9847 const Expression* expr,
9848 const Expression* start,
9849 const Expression* end)
9851 expr->dump_expression(ast_dump_context);
9852 ast_dump_context->ostream() << "[";
9853 start->dump_expression(ast_dump_context);
9856 ast_dump_context->ostream() << ":";
9857 end->dump_expression(ast_dump_context);
9859 ast_dump_context->ostream() << "]";
9862 // Dump ast representation for an index expression.
9865 Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
9868 Index_expression::dump_index_expression(ast_dump_context, this->left_,
9869 this->start_, this->end_);
9872 // Make an index expression.
9875 Expression::make_index(Expression* left, Expression* start, Expression* end,
9876 source_location location)
9878 return new Index_expression(left, start, end, location);
9881 // An array index. This is used for both indexing and slicing.
9883 class Array_index_expression : public Expression
9886 Array_index_expression(Expression* array, Expression* start,
9887 Expression* end, source_location location)
9888 : Expression(EXPRESSION_ARRAY_INDEX, location),
9889 array_(array), start_(start), end_(end), type_(NULL)
9894 do_traverse(Traverse*);
9900 do_determine_type(const Type_context*);
9903 do_check_types(Gogo*);
9908 return Expression::make_array_index(this->array_->copy(),
9909 this->start_->copy(),
9912 : this->end_->copy()),
9917 do_is_addressable() const;
9920 do_address_taken(bool escapes)
9921 { this->array_->address_taken(escapes); }
9924 do_get_tree(Translate_context*);
9927 do_dump_expression(Ast_dump_context*) const;
9930 // The array we are getting a value from.
9932 // The start or only index.
9934 // The end index of a slice. This may be NULL for a simple array
9935 // index, or it may be a nil expression for the length of the array.
9937 // The type of the expression.
9941 // Array index traversal.
9944 Array_index_expression::do_traverse(Traverse* traverse)
9946 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9947 return TRAVERSE_EXIT;
9948 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9949 return TRAVERSE_EXIT;
9950 if (this->end_ != NULL)
9952 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9953 return TRAVERSE_EXIT;
9955 return TRAVERSE_CONTINUE;
9958 // Return the type of an array index.
9961 Array_index_expression::do_type()
9963 if (this->type_ == NULL)
9965 Array_type* type = this->array_->type()->array_type();
9967 this->type_ = Type::make_error_type();
9968 else if (this->end_ == NULL)
9969 this->type_ = type->element_type();
9970 else if (type->is_open_array_type())
9972 // A slice of a slice has the same type as the original
9974 this->type_ = this->array_->type()->deref();
9978 // A slice of an array is a slice.
9979 this->type_ = Type::make_array_type(type->element_type(), NULL);
9985 // Set the type of an array index.
9988 Array_index_expression::do_determine_type(const Type_context*)
9990 this->array_->determine_type_no_context();
9991 this->start_->determine_type_no_context();
9992 if (this->end_ != NULL)
9993 this->end_->determine_type_no_context();
9996 // Check types of an array index.
9999 Array_index_expression::do_check_types(Gogo*)
10001 if (this->start_->type()->integer_type() == NULL)
10002 this->report_error(_("index must be integer"));
10003 if (this->end_ != NULL
10004 && this->end_->type()->integer_type() == NULL
10005 && !this->end_->is_nil_expression())
10006 this->report_error(_("slice end must be integer"));
10008 Array_type* array_type = this->array_->type()->array_type();
10009 if (array_type == NULL)
10011 go_assert(this->array_->type()->is_error());
10015 unsigned int int_bits =
10016 Type::lookup_integer_type("int")->integer_type()->bits();
10021 bool lval_valid = (array_type->length() != NULL
10022 && array_type->length()->integer_constant_value(true,
10027 if (this->start_->integer_constant_value(true, ival, &dummy))
10029 if (mpz_sgn(ival) < 0
10030 || mpz_sizeinbase(ival, 2) >= int_bits
10032 && (this->end_ == NULL
10033 ? mpz_cmp(ival, lval) >= 0
10034 : mpz_cmp(ival, lval) > 0)))
10036 error_at(this->start_->location(), "array index out of bounds");
10037 this->set_is_error();
10040 if (this->end_ != NULL && !this->end_->is_nil_expression())
10042 if (this->end_->integer_constant_value(true, ival, &dummy))
10044 if (mpz_sgn(ival) < 0
10045 || mpz_sizeinbase(ival, 2) >= int_bits
10046 || (lval_valid && mpz_cmp(ival, lval) > 0))
10048 error_at(this->end_->location(), "array index out of bounds");
10049 this->set_is_error();
10056 // A slice of an array requires an addressable array. A slice of a
10057 // slice is always possible.
10058 if (this->end_ != NULL && !array_type->is_open_array_type())
10060 if (!this->array_->is_addressable())
10061 this->report_error(_("array is not addressable"));
10063 this->array_->address_taken(true);
10067 // Return whether this expression is addressable.
10070 Array_index_expression::do_is_addressable() const
10072 // A slice expression is not addressable.
10073 if (this->end_ != NULL)
10076 // An index into a slice is addressable.
10077 if (this->array_->type()->is_open_array_type())
10080 // An index into an array is addressable if the array is
10082 return this->array_->is_addressable();
10085 // Get a tree for an array index.
10088 Array_index_expression::do_get_tree(Translate_context* context)
10090 Gogo* gogo = context->gogo();
10091 source_location loc = this->location();
10093 Array_type* array_type = this->array_->type()->array_type();
10094 if (array_type == NULL)
10096 go_assert(this->array_->type()->is_error());
10097 return error_mark_node;
10100 tree type_tree = type_to_tree(array_type->get_backend(gogo));
10101 if (type_tree == error_mark_node)
10102 return error_mark_node;
10104 tree array_tree = this->array_->get_tree(context);
10105 if (array_tree == error_mark_node)
10106 return error_mark_node;
10108 if (array_type->length() == NULL && !DECL_P(array_tree))
10109 array_tree = save_expr(array_tree);
10110 tree length_tree = array_type->length_tree(gogo, array_tree);
10111 if (length_tree == error_mark_node)
10112 return error_mark_node;
10113 length_tree = save_expr(length_tree);
10114 tree length_type = TREE_TYPE(length_tree);
10116 tree bad_index = boolean_false_node;
10118 tree start_tree = this->start_->get_tree(context);
10119 if (start_tree == error_mark_node)
10120 return error_mark_node;
10121 if (!DECL_P(start_tree))
10122 start_tree = save_expr(start_tree);
10123 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
10124 start_tree = convert_to_integer(length_type, start_tree);
10126 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
10129 start_tree = fold_convert_loc(loc, length_type, start_tree);
10130 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
10131 fold_build2_loc(loc,
10132 (this->end_ == NULL
10135 boolean_type_node, start_tree,
10138 int code = (array_type->length() != NULL
10139 ? (this->end_ == NULL
10140 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
10141 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
10142 : (this->end_ == NULL
10143 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
10144 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
10145 tree crash = Gogo::runtime_error(code, loc);
10147 if (this->end_ == NULL)
10149 // Simple array indexing. This has to return an l-value, so
10150 // wrap the index check into START_TREE.
10151 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
10152 build3(COND_EXPR, void_type_node,
10153 bad_index, crash, NULL_TREE),
10155 start_tree = fold_convert_loc(loc, sizetype, start_tree);
10157 if (array_type->length() != NULL)
10160 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
10161 start_tree, NULL_TREE, NULL_TREE);
10166 tree values = array_type->value_pointer_tree(gogo, array_tree);
10167 Type* element_type = array_type->element_type();
10168 Btype* belement_type = element_type->get_backend(gogo);
10169 tree element_type_tree = type_to_tree(belement_type);
10170 if (element_type_tree == error_mark_node)
10171 return error_mark_node;
10172 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
10173 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
10174 start_tree, element_size);
10175 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
10176 TREE_TYPE(values), values, offset);
10177 return build_fold_indirect_ref(ptr);
10183 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
10184 if (capacity_tree == error_mark_node)
10185 return error_mark_node;
10186 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
10189 if (this->end_->is_nil_expression())
10190 end_tree = length_tree;
10193 end_tree = this->end_->get_tree(context);
10194 if (end_tree == error_mark_node)
10195 return error_mark_node;
10196 if (!DECL_P(end_tree))
10197 end_tree = save_expr(end_tree);
10198 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
10199 end_tree = convert_to_integer(length_type, end_tree);
10201 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
10204 end_tree = fold_convert_loc(loc, length_type, end_tree);
10206 capacity_tree = save_expr(capacity_tree);
10207 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
10208 fold_build2_loc(loc, LT_EXPR,
10210 end_tree, start_tree),
10211 fold_build2_loc(loc, GT_EXPR,
10213 end_tree, capacity_tree));
10214 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
10215 bad_index, bad_end);
10218 Type* element_type = array_type->element_type();
10219 tree element_type_tree = type_to_tree(element_type->get_backend(gogo));
10220 if (element_type_tree == error_mark_node)
10221 return error_mark_node;
10222 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
10224 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
10225 fold_convert_loc(loc, sizetype, start_tree),
10228 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
10229 if (value_pointer == error_mark_node)
10230 return error_mark_node;
10232 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
10233 TREE_TYPE(value_pointer),
10234 value_pointer, offset);
10236 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
10237 end_tree, start_tree);
10239 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
10240 capacity_tree, start_tree);
10242 tree struct_tree = type_to_tree(this->type()->get_backend(gogo));
10243 go_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
10245 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
10247 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
10248 tree field = TYPE_FIELDS(struct_tree);
10249 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
10250 elt->index = field;
10251 elt->value = value_pointer;
10253 elt = VEC_quick_push(constructor_elt, init, NULL);
10254 field = DECL_CHAIN(field);
10255 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
10256 elt->index = field;
10257 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
10259 elt = VEC_quick_push(constructor_elt, init, NULL);
10260 field = DECL_CHAIN(field);
10261 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
10262 elt->index = field;
10263 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
10265 tree constructor = build_constructor(struct_tree, init);
10267 if (TREE_CONSTANT(value_pointer)
10268 && TREE_CONSTANT(result_length_tree)
10269 && TREE_CONSTANT(result_capacity_tree))
10270 TREE_CONSTANT(constructor) = 1;
10272 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
10273 build3(COND_EXPR, void_type_node,
10274 bad_index, crash, NULL_TREE),
10278 // Dump ast representation for an array index expression.
10281 Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10284 Index_expression::dump_index_expression(ast_dump_context, this->array_,
10285 this->start_, this->end_);
10288 // Make an array index expression. END may be NULL.
10291 Expression::make_array_index(Expression* array, Expression* start,
10292 Expression* end, source_location location)
10294 // Taking a slice of a composite literal requires moving the literal
10296 if (end != NULL && array->is_composite_literal())
10298 array = Expression::make_heap_composite(array, location);
10299 array = Expression::make_unary(OPERATOR_MULT, array, location);
10301 return new Array_index_expression(array, start, end, location);
10304 // A string index. This is used for both indexing and slicing.
10306 class String_index_expression : public Expression
10309 String_index_expression(Expression* string, Expression* start,
10310 Expression* end, source_location location)
10311 : Expression(EXPRESSION_STRING_INDEX, location),
10312 string_(string), start_(start), end_(end)
10317 do_traverse(Traverse*);
10323 do_determine_type(const Type_context*);
10326 do_check_types(Gogo*);
10331 return Expression::make_string_index(this->string_->copy(),
10332 this->start_->copy(),
10333 (this->end_ == NULL
10335 : this->end_->copy()),
10340 do_get_tree(Translate_context*);
10343 do_dump_expression(Ast_dump_context*) const;
10346 // The string we are getting a value from.
10347 Expression* string_;
10348 // The start or only index.
10349 Expression* start_;
10350 // The end index of a slice. This may be NULL for a single index,
10351 // or it may be a nil expression for the length of the string.
10355 // String index traversal.
10358 String_index_expression::do_traverse(Traverse* traverse)
10360 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
10361 return TRAVERSE_EXIT;
10362 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10363 return TRAVERSE_EXIT;
10364 if (this->end_ != NULL)
10366 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10367 return TRAVERSE_EXIT;
10369 return TRAVERSE_CONTINUE;
10372 // Return the type of a string index.
10375 String_index_expression::do_type()
10377 if (this->end_ == NULL)
10378 return Type::lookup_integer_type("uint8");
10380 return this->string_->type();
10383 // Determine the type of a string index.
10386 String_index_expression::do_determine_type(const Type_context*)
10388 this->string_->determine_type_no_context();
10389 this->start_->determine_type_no_context();
10390 if (this->end_ != NULL)
10391 this->end_->determine_type_no_context();
10394 // Check types of a string index.
10397 String_index_expression::do_check_types(Gogo*)
10399 if (this->start_->type()->integer_type() == NULL)
10400 this->report_error(_("index must be integer"));
10401 if (this->end_ != NULL
10402 && this->end_->type()->integer_type() == NULL
10403 && !this->end_->is_nil_expression())
10404 this->report_error(_("slice end must be integer"));
10407 bool sval_valid = this->string_->string_constant_value(&sval);
10412 if (this->start_->integer_constant_value(true, ival, &dummy))
10414 if (mpz_sgn(ival) < 0
10415 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
10417 error_at(this->start_->location(), "string index out of bounds");
10418 this->set_is_error();
10421 if (this->end_ != NULL && !this->end_->is_nil_expression())
10423 if (this->end_->integer_constant_value(true, ival, &dummy))
10425 if (mpz_sgn(ival) < 0
10426 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
10428 error_at(this->end_->location(), "string index out of bounds");
10429 this->set_is_error();
10436 // Get a tree for a string index.
10439 String_index_expression::do_get_tree(Translate_context* context)
10441 source_location loc = this->location();
10443 tree string_tree = this->string_->get_tree(context);
10444 if (string_tree == error_mark_node)
10445 return error_mark_node;
10447 if (this->string_->type()->points_to() != NULL)
10448 string_tree = build_fold_indirect_ref(string_tree);
10449 if (!DECL_P(string_tree))
10450 string_tree = save_expr(string_tree);
10451 tree string_type = TREE_TYPE(string_tree);
10453 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
10454 length_tree = save_expr(length_tree);
10455 tree length_type = TREE_TYPE(length_tree);
10457 tree bad_index = boolean_false_node;
10459 tree start_tree = this->start_->get_tree(context);
10460 if (start_tree == error_mark_node)
10461 return error_mark_node;
10462 if (!DECL_P(start_tree))
10463 start_tree = save_expr(start_tree);
10464 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
10465 start_tree = convert_to_integer(length_type, start_tree);
10467 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
10470 start_tree = fold_convert_loc(loc, length_type, start_tree);
10472 int code = (this->end_ == NULL
10473 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
10474 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
10475 tree crash = Gogo::runtime_error(code, loc);
10477 if (this->end_ == NULL)
10479 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
10481 fold_build2_loc(loc, GE_EXPR,
10483 start_tree, length_tree));
10485 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
10486 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
10488 fold_convert_loc(loc, sizetype, start_tree));
10489 tree index = build_fold_indirect_ref_loc(loc, ptr);
10491 return build2(COMPOUND_EXPR, TREE_TYPE(index),
10492 build3(COND_EXPR, void_type_node,
10493 bad_index, crash, NULL_TREE),
10499 if (this->end_->is_nil_expression())
10500 end_tree = build_int_cst(length_type, -1);
10503 end_tree = this->end_->get_tree(context);
10504 if (end_tree == error_mark_node)
10505 return error_mark_node;
10506 if (!DECL_P(end_tree))
10507 end_tree = save_expr(end_tree);
10508 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
10509 end_tree = convert_to_integer(length_type, end_tree);
10511 bad_index = Expression::check_bounds(end_tree, length_type,
10514 end_tree = fold_convert_loc(loc, length_type, end_tree);
10517 static tree strslice_fndecl;
10518 tree ret = Gogo::call_builtin(&strslice_fndecl,
10520 "__go_string_slice",
10529 if (ret == error_mark_node)
10530 return error_mark_node;
10531 // This will panic if the bounds are out of range for the
10533 TREE_NOTHROW(strslice_fndecl) = 0;
10535 if (bad_index == boolean_false_node)
10538 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
10539 build3(COND_EXPR, void_type_node,
10540 bad_index, crash, NULL_TREE),
10545 // Dump ast representation for a string index expression.
10548 String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10551 Index_expression::dump_index_expression(ast_dump_context, this->string_,
10552 this->start_, this->end_);
10555 // Make a string index expression. END may be NULL.
10558 Expression::make_string_index(Expression* string, Expression* start,
10559 Expression* end, source_location location)
10561 return new String_index_expression(string, start, end, location);
10564 // Class Map_index.
10566 // Get the type of the map.
10569 Map_index_expression::get_map_type() const
10571 Map_type* mt = this->map_->type()->deref()->map_type();
10573 go_assert(saw_errors());
10577 // Map index traversal.
10580 Map_index_expression::do_traverse(Traverse* traverse)
10582 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
10583 return TRAVERSE_EXIT;
10584 return Expression::traverse(&this->index_, traverse);
10587 // Return the type of a map index.
10590 Map_index_expression::do_type()
10592 Map_type* mt = this->get_map_type();
10594 return Type::make_error_type();
10595 Type* type = mt->val_type();
10596 // If this map index is in a tuple assignment, we actually return a
10597 // pointer to the value type. Tuple_map_assignment_statement is
10598 // responsible for handling this correctly. We need to get the type
10599 // right in case this gets assigned to a temporary variable.
10600 if (this->is_in_tuple_assignment_)
10601 type = Type::make_pointer_type(type);
10605 // Fix the type of a map index.
10608 Map_index_expression::do_determine_type(const Type_context*)
10610 this->map_->determine_type_no_context();
10611 Map_type* mt = this->get_map_type();
10612 Type* key_type = mt == NULL ? NULL : mt->key_type();
10613 Type_context subcontext(key_type, false);
10614 this->index_->determine_type(&subcontext);
10617 // Check types of a map index.
10620 Map_index_expression::do_check_types(Gogo*)
10622 std::string reason;
10623 Map_type* mt = this->get_map_type();
10626 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
10628 if (reason.empty())
10629 this->report_error(_("incompatible type for map index"));
10632 error_at(this->location(), "incompatible type for map index (%s)",
10634 this->set_is_error();
10639 // Get a tree for a map index.
10642 Map_index_expression::do_get_tree(Translate_context* context)
10644 Map_type* type = this->get_map_type();
10646 return error_mark_node;
10648 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
10649 if (valptr == error_mark_node)
10650 return error_mark_node;
10651 valptr = save_expr(valptr);
10653 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
10655 if (this->is_lvalue_)
10656 return build_fold_indirect_ref(valptr);
10657 else if (this->is_in_tuple_assignment_)
10659 // Tuple_map_assignment_statement is responsible for using this
10665 Gogo* gogo = context->gogo();
10666 Btype* val_btype = type->val_type()->get_backend(gogo);
10667 Bexpression* val_zero = gogo->backend()->zero_expression(val_btype);
10668 return fold_build3(COND_EXPR, val_type_tree,
10669 fold_build2(EQ_EXPR, boolean_type_node, valptr,
10670 fold_convert(TREE_TYPE(valptr),
10671 null_pointer_node)),
10672 expr_to_tree(val_zero),
10673 build_fold_indirect_ref(valptr));
10677 // Get a tree for the map index. This returns a tree which evaluates
10678 // to a pointer to a value. The pointer will be NULL if the key is
10682 Map_index_expression::get_value_pointer(Translate_context* context,
10685 Map_type* type = this->get_map_type();
10687 return error_mark_node;
10689 tree map_tree = this->map_->get_tree(context);
10690 tree index_tree = this->index_->get_tree(context);
10691 index_tree = Expression::convert_for_assignment(context, type->key_type(),
10692 this->index_->type(),
10695 if (map_tree == error_mark_node || index_tree == error_mark_node)
10696 return error_mark_node;
10698 if (this->map_->type()->points_to() != NULL)
10699 map_tree = build_fold_indirect_ref(map_tree);
10701 // We need to pass in a pointer to the key, so stuff it into a
10705 if (current_function_decl != NULL)
10707 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
10708 DECL_IGNORED_P(tmp) = 0;
10709 DECL_INITIAL(tmp) = index_tree;
10710 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
10711 TREE_ADDRESSABLE(tmp) = 1;
10715 tmp = build_decl(this->location(), VAR_DECL, create_tmp_var_name("M"),
10716 TREE_TYPE(index_tree));
10717 DECL_EXTERNAL(tmp) = 0;
10718 TREE_PUBLIC(tmp) = 0;
10719 TREE_STATIC(tmp) = 1;
10720 DECL_ARTIFICIAL(tmp) = 1;
10721 if (!TREE_CONSTANT(index_tree))
10722 make_tmp = fold_build2_loc(this->location(), INIT_EXPR, void_type_node,
10726 TREE_READONLY(tmp) = 1;
10727 TREE_CONSTANT(tmp) = 1;
10728 DECL_INITIAL(tmp) = index_tree;
10729 make_tmp = NULL_TREE;
10731 rest_of_decl_compilation(tmp, 1, 0);
10733 tree tmpref = fold_convert_loc(this->location(), const_ptr_type_node,
10734 build_fold_addr_expr_loc(this->location(),
10737 static tree map_index_fndecl;
10738 tree call = Gogo::call_builtin(&map_index_fndecl,
10742 const_ptr_type_node,
10743 TREE_TYPE(map_tree),
10745 const_ptr_type_node,
10749 ? boolean_true_node
10750 : boolean_false_node));
10751 if (call == error_mark_node)
10752 return error_mark_node;
10753 // This can panic on a map of interface type if the interface holds
10754 // an uncomparable or unhashable type.
10755 TREE_NOTHROW(map_index_fndecl) = 0;
10757 Type* val_type = type->val_type();
10758 tree val_type_tree = type_to_tree(val_type->get_backend(context->gogo()));
10759 if (val_type_tree == error_mark_node)
10760 return error_mark_node;
10761 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
10763 tree ret = fold_convert_loc(this->location(), ptr_val_type_tree, call);
10764 if (make_tmp != NULL_TREE)
10765 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
10769 // Dump ast representation for a map index expression
10772 Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10775 Index_expression::dump_index_expression(ast_dump_context,
10776 this->map_, this->index_, NULL);
10779 // Make a map index expression.
10781 Map_index_expression*
10782 Expression::make_map_index(Expression* map, Expression* index,
10783 source_location location)
10785 return new Map_index_expression(map, index, location);
10788 // Class Field_reference_expression.
10790 // Return the type of a field reference.
10793 Field_reference_expression::do_type()
10795 Type* type = this->expr_->type();
10796 if (type->is_error())
10798 Struct_type* struct_type = type->struct_type();
10799 go_assert(struct_type != NULL);
10800 return struct_type->field(this->field_index_)->type();
10803 // Check the types for a field reference.
10806 Field_reference_expression::do_check_types(Gogo*)
10808 Type* type = this->expr_->type();
10809 if (type->is_error())
10811 Struct_type* struct_type = type->struct_type();
10812 go_assert(struct_type != NULL);
10813 go_assert(struct_type->field(this->field_index_) != NULL);
10816 // Get a tree for a field reference.
10819 Field_reference_expression::do_get_tree(Translate_context* context)
10821 tree struct_tree = this->expr_->get_tree(context);
10822 if (struct_tree == error_mark_node
10823 || TREE_TYPE(struct_tree) == error_mark_node)
10824 return error_mark_node;
10825 go_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
10826 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
10827 if (field == NULL_TREE)
10829 // This can happen for a type which refers to itself indirectly
10830 // and then turns out to be erroneous.
10831 go_assert(saw_errors());
10832 return error_mark_node;
10834 for (unsigned int i = this->field_index_; i > 0; --i)
10836 field = DECL_CHAIN(field);
10837 go_assert(field != NULL_TREE);
10839 if (TREE_TYPE(field) == error_mark_node)
10840 return error_mark_node;
10841 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
10845 // Dump ast representation for a field reference expression.
10848 Field_reference_expression::do_dump_expression(
10849 Ast_dump_context* ast_dump_context) const
10851 this->expr_->dump_expression(ast_dump_context);
10852 ast_dump_context->ostream() << "." << this->field_index_;
10855 // Make a reference to a qualified identifier in an expression.
10857 Field_reference_expression*
10858 Expression::make_field_reference(Expression* expr, unsigned int field_index,
10859 source_location location)
10861 return new Field_reference_expression(expr, field_index, location);
10864 // Class Interface_field_reference_expression.
10866 // Return a tree for the pointer to the function to call.
10869 Interface_field_reference_expression::get_function_tree(Translate_context*,
10872 if (this->expr_->type()->points_to() != NULL)
10873 expr = build_fold_indirect_ref(expr);
10875 tree expr_type = TREE_TYPE(expr);
10876 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10878 tree field = TYPE_FIELDS(expr_type);
10879 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
10881 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10882 go_assert(POINTER_TYPE_P(TREE_TYPE(table)));
10884 table = build_fold_indirect_ref(table);
10885 go_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
10887 std::string name = Gogo::unpack_hidden_name(this->name_);
10888 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
10889 field != NULL_TREE;
10890 field = DECL_CHAIN(field))
10892 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
10895 go_assert(field != NULL_TREE);
10897 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
10900 // Return a tree for the first argument to pass to the interface
10904 Interface_field_reference_expression::get_underlying_object_tree(
10905 Translate_context*,
10908 if (this->expr_->type()->points_to() != NULL)
10909 expr = build_fold_indirect_ref(expr);
10911 tree expr_type = TREE_TYPE(expr);
10912 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10914 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
10915 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
10917 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10923 Interface_field_reference_expression::do_traverse(Traverse* traverse)
10925 return Expression::traverse(&this->expr_, traverse);
10928 // Return the type of an interface field reference.
10931 Interface_field_reference_expression::do_type()
10933 Type* expr_type = this->expr_->type();
10935 Type* points_to = expr_type->points_to();
10936 if (points_to != NULL)
10937 expr_type = points_to;
10939 Interface_type* interface_type = expr_type->interface_type();
10940 if (interface_type == NULL)
10941 return Type::make_error_type();
10943 const Typed_identifier* method = interface_type->find_method(this->name_);
10944 if (method == NULL)
10945 return Type::make_error_type();
10947 return method->type();
10950 // Determine types.
10953 Interface_field_reference_expression::do_determine_type(const Type_context*)
10955 this->expr_->determine_type_no_context();
10958 // Check the types for an interface field reference.
10961 Interface_field_reference_expression::do_check_types(Gogo*)
10963 Type* type = this->expr_->type();
10965 Type* points_to = type->points_to();
10966 if (points_to != NULL)
10969 Interface_type* interface_type = type->interface_type();
10970 if (interface_type == NULL)
10972 if (!type->is_error_type())
10973 this->report_error(_("expected interface or pointer to interface"));
10977 const Typed_identifier* method =
10978 interface_type->find_method(this->name_);
10979 if (method == NULL)
10981 error_at(this->location(), "method %qs not in interface",
10982 Gogo::message_name(this->name_).c_str());
10983 this->set_is_error();
10988 // Get a tree for a reference to a field in an interface. There is no
10989 // standard tree type representation for this: it's a function
10990 // attached to its first argument, like a Bound_method_expression.
10991 // The only places it may currently be used are in a Call_expression
10992 // or a Go_statement, which will take it apart directly. So this has
10993 // nothing to do at present.
10996 Interface_field_reference_expression::do_get_tree(Translate_context*)
11001 // Dump ast representation for an interface field reference.
11004 Interface_field_reference_expression::do_dump_expression(
11005 Ast_dump_context* ast_dump_context) const
11007 this->expr_->dump_expression(ast_dump_context);
11008 ast_dump_context->ostream() << "." << this->name_;
11011 // Make a reference to a field in an interface.
11014 Expression::make_interface_field_reference(Expression* expr,
11015 const std::string& field,
11016 source_location location)
11018 return new Interface_field_reference_expression(expr, field, location);
11021 // A general selector. This is a Parser_expression for LEFT.NAME. It
11022 // is lowered after we know the type of the left hand side.
11024 class Selector_expression : public Parser_expression
11027 Selector_expression(Expression* left, const std::string& name,
11028 source_location location)
11029 : Parser_expression(EXPRESSION_SELECTOR, location),
11030 left_(left), name_(name)
11035 do_traverse(Traverse* traverse)
11036 { return Expression::traverse(&this->left_, traverse); }
11039 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
11044 return new Selector_expression(this->left_->copy(), this->name_,
11049 do_dump_expression(Ast_dump_context* ast_dump_context) const;
11053 lower_method_expression(Gogo*);
11055 // The expression on the left hand side.
11057 // The name on the right hand side.
11061 // Lower a selector expression once we know the real type of the left
11065 Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*,
11068 Expression* left = this->left_;
11069 if (left->is_type_expression())
11070 return this->lower_method_expression(gogo);
11071 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
11075 // Lower a method expression T.M or (*T).M. We turn this into a
11076 // function literal.
11079 Selector_expression::lower_method_expression(Gogo* gogo)
11081 source_location location = this->location();
11082 Type* type = this->left_->type();
11083 const std::string& name(this->name_);
11086 if (type->points_to() == NULL)
11087 is_pointer = false;
11091 type = type->points_to();
11093 Named_type* nt = type->named_type();
11097 ("method expression requires named type or "
11098 "pointer to named type"));
11099 return Expression::make_error(location);
11103 Method* method = nt->method_function(name, &is_ambiguous);
11104 const Typed_identifier* imethod = NULL;
11105 if (method == NULL && !is_pointer)
11107 Interface_type* it = nt->interface_type();
11109 imethod = it->find_method(name);
11112 if (method == NULL && imethod == NULL)
11115 error_at(location, "type %<%s%s%> has no method %<%s%>",
11116 is_pointer ? "*" : "",
11117 nt->message_name().c_str(),
11118 Gogo::message_name(name).c_str());
11120 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
11121 Gogo::message_name(name).c_str(),
11122 is_pointer ? "*" : "",
11123 nt->message_name().c_str());
11124 return Expression::make_error(location);
11127 if (method != NULL && !is_pointer && !method->is_value_method())
11129 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
11130 nt->message_name().c_str(),
11131 Gogo::message_name(name).c_str());
11132 return Expression::make_error(location);
11135 // Build a new function type in which the receiver becomes the first
11137 Function_type* method_type;
11138 if (method != NULL)
11140 method_type = method->type();
11141 go_assert(method_type->is_method());
11145 method_type = imethod->type()->function_type();
11146 go_assert(method_type != NULL && !method_type->is_method());
11149 const char* const receiver_name = "$this";
11150 Typed_identifier_list* parameters = new Typed_identifier_list();
11151 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
11154 const Typed_identifier_list* method_parameters = method_type->parameters();
11155 if (method_parameters != NULL)
11157 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
11158 p != method_parameters->end();
11160 parameters->push_back(*p);
11163 const Typed_identifier_list* method_results = method_type->results();
11164 Typed_identifier_list* results;
11165 if (method_results == NULL)
11169 results = new Typed_identifier_list();
11170 for (Typed_identifier_list::const_iterator p = method_results->begin();
11171 p != method_results->end();
11173 results->push_back(*p);
11176 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
11178 if (method_type->is_varargs())
11179 fntype->set_is_varargs();
11181 // We generate methods which always takes a pointer to the receiver
11182 // as their first argument. If this is for a pointer type, we can
11183 // simply reuse the existing function. We use an internal hack to
11184 // get the right type.
11186 if (method != NULL && is_pointer)
11188 Named_object* mno = (method->needs_stub_method()
11189 ? method->stub_object()
11190 : method->named_object());
11191 Expression* f = Expression::make_func_reference(mno, NULL, location);
11192 f = Expression::make_cast(fntype, f, location);
11193 Type_conversion_expression* tce =
11194 static_cast<Type_conversion_expression*>(f);
11195 tce->set_may_convert_function_types();
11199 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
11202 Named_object* vno = gogo->lookup(receiver_name, NULL);
11203 go_assert(vno != NULL);
11204 Expression* ve = Expression::make_var_reference(vno, location);
11206 if (method != NULL)
11207 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
11209 bm = Expression::make_interface_field_reference(ve, name, location);
11211 // Even though we found the method above, if it has an error type we
11212 // may see an error here.
11213 if (bm->is_error_expression())
11215 gogo->finish_function(location);
11219 Expression_list* args;
11220 if (method_parameters == NULL)
11224 args = new Expression_list();
11225 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
11226 p != method_parameters->end();
11229 vno = gogo->lookup(p->name(), NULL);
11230 go_assert(vno != NULL);
11231 args->push_back(Expression::make_var_reference(vno, location));
11235 gogo->start_block(location);
11237 Call_expression* call = Expression::make_call(bm, args,
11238 method_type->is_varargs(),
11241 size_t count = call->result_count();
11244 s = Statement::make_statement(call);
11247 Expression_list* retvals = new Expression_list();
11249 retvals->push_back(call);
11252 for (size_t i = 0; i < count; ++i)
11253 retvals->push_back(Expression::make_call_result(call, i));
11255 s = Statement::make_return_statement(retvals, location);
11257 gogo->add_statement(s);
11259 Block* b = gogo->finish_block(location);
11261 gogo->add_block(b, location);
11263 // Lower the call in case there are multiple results.
11264 gogo->lower_block(no, b);
11266 gogo->finish_function(location);
11268 return Expression::make_func_reference(no, NULL, location);
11271 // Dump the ast for a selector expression.
11274 Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11277 ast_dump_context->dump_expression(this->left_);
11278 ast_dump_context->ostream() << ".";
11279 ast_dump_context->ostream() << this->name_;
11282 // Make a selector expression.
11285 Expression::make_selector(Expression* left, const std::string& name,
11286 source_location location)
11288 return new Selector_expression(left, name, location);
11291 // Implement the builtin function new.
11293 class Allocation_expression : public Expression
11296 Allocation_expression(Type* type, source_location location)
11297 : Expression(EXPRESSION_ALLOCATION, location),
11303 do_traverse(Traverse* traverse)
11304 { return Type::traverse(this->type_, traverse); }
11308 { return Type::make_pointer_type(this->type_); }
11311 do_determine_type(const Type_context*)
11316 { return new Allocation_expression(this->type_, this->location()); }
11319 do_get_tree(Translate_context*);
11322 do_dump_expression(Ast_dump_context*) const;
11325 // The type we are allocating.
11329 // Return a tree for an allocation expression.
11332 Allocation_expression::do_get_tree(Translate_context* context)
11334 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
11335 if (type_tree == error_mark_node)
11336 return error_mark_node;
11337 tree size_tree = TYPE_SIZE_UNIT(type_tree);
11338 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
11340 if (space == error_mark_node)
11341 return error_mark_node;
11342 return fold_convert(build_pointer_type(type_tree), space);
11345 // Dump ast representation for an allocation expression.
11348 Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11351 ast_dump_context->ostream() << "new(";
11352 ast_dump_context->dump_type(this->type_);
11353 ast_dump_context->ostream() << ")";
11356 // Make an allocation expression.
11359 Expression::make_allocation(Type* type, source_location location)
11361 return new Allocation_expression(type, location);
11364 // Construct a struct.
11366 class Struct_construction_expression : public Expression
11369 Struct_construction_expression(Type* type, Expression_list* vals,
11370 source_location location)
11371 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
11372 type_(type), vals_(vals)
11375 // Return whether this is a constant initializer.
11377 is_constant_struct() const;
11381 do_traverse(Traverse* traverse);
11385 { return this->type_; }
11388 do_determine_type(const Type_context*);
11391 do_check_types(Gogo*);
11396 return new Struct_construction_expression(this->type_, this->vals_->copy(),
11401 do_is_addressable() const
11405 do_get_tree(Translate_context*);
11408 do_export(Export*) const;
11411 do_dump_expression(Ast_dump_context*) const;
11414 // The type of the struct to construct.
11416 // The list of values, in order of the fields in the struct. A NULL
11417 // entry means that the field should be zero-initialized.
11418 Expression_list* vals_;
11424 Struct_construction_expression::do_traverse(Traverse* traverse)
11426 if (this->vals_ != NULL
11427 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11428 return TRAVERSE_EXIT;
11429 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11430 return TRAVERSE_EXIT;
11431 return TRAVERSE_CONTINUE;
11434 // Return whether this is a constant initializer.
11437 Struct_construction_expression::is_constant_struct() const
11439 if (this->vals_ == NULL)
11441 for (Expression_list::const_iterator pv = this->vals_->begin();
11442 pv != this->vals_->end();
11446 && !(*pv)->is_constant()
11447 && (!(*pv)->is_composite_literal()
11448 || (*pv)->is_nonconstant_composite_literal()))
11452 const Struct_field_list* fields = this->type_->struct_type()->fields();
11453 for (Struct_field_list::const_iterator pf = fields->begin();
11454 pf != fields->end();
11457 // There are no constant constructors for interfaces.
11458 if (pf->type()->interface_type() != NULL)
11465 // Final type determination.
11468 Struct_construction_expression::do_determine_type(const Type_context*)
11470 if (this->vals_ == NULL)
11472 const Struct_field_list* fields = this->type_->struct_type()->fields();
11473 Expression_list::const_iterator pv = this->vals_->begin();
11474 for (Struct_field_list::const_iterator pf = fields->begin();
11475 pf != fields->end();
11478 if (pv == this->vals_->end())
11482 Type_context subcontext(pf->type(), false);
11483 (*pv)->determine_type(&subcontext);
11486 // Extra values are an error we will report elsewhere; we still want
11487 // to determine the type to avoid knockon errors.
11488 for (; pv != this->vals_->end(); ++pv)
11489 (*pv)->determine_type_no_context();
11495 Struct_construction_expression::do_check_types(Gogo*)
11497 if (this->vals_ == NULL)
11500 Struct_type* st = this->type_->struct_type();
11501 if (this->vals_->size() > st->field_count())
11503 this->report_error(_("too many expressions for struct"));
11507 const Struct_field_list* fields = st->fields();
11508 Expression_list::const_iterator pv = this->vals_->begin();
11510 for (Struct_field_list::const_iterator pf = fields->begin();
11511 pf != fields->end();
11514 if (pv == this->vals_->end())
11516 this->report_error(_("too few expressions for struct"));
11523 std::string reason;
11524 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
11526 if (reason.empty())
11527 error_at((*pv)->location(),
11528 "incompatible type for field %d in struct construction",
11531 error_at((*pv)->location(),
11532 ("incompatible type for field %d in "
11533 "struct construction (%s)"),
11534 i + 1, reason.c_str());
11535 this->set_is_error();
11538 go_assert(pv == this->vals_->end());
11541 // Return a tree for constructing a struct.
11544 Struct_construction_expression::do_get_tree(Translate_context* context)
11546 Gogo* gogo = context->gogo();
11548 if (this->vals_ == NULL)
11550 Btype* btype = this->type_->get_backend(gogo);
11551 return expr_to_tree(gogo->backend()->zero_expression(btype));
11554 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
11555 if (type_tree == error_mark_node)
11556 return error_mark_node;
11557 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11559 bool is_constant = true;
11560 const Struct_field_list* fields = this->type_->struct_type()->fields();
11561 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
11563 Struct_field_list::const_iterator pf = fields->begin();
11564 Expression_list::const_iterator pv = this->vals_->begin();
11565 for (tree field = TYPE_FIELDS(type_tree);
11566 field != NULL_TREE;
11567 field = DECL_CHAIN(field), ++pf)
11569 go_assert(pf != fields->end());
11571 Btype* fbtype = pf->type()->get_backend(gogo);
11574 if (pv == this->vals_->end())
11575 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
11576 else if (*pv == NULL)
11578 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
11583 val = Expression::convert_for_assignment(context, pf->type(),
11585 (*pv)->get_tree(context),
11590 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
11591 return error_mark_node;
11593 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
11594 elt->index = field;
11596 if (!TREE_CONSTANT(val))
11597 is_constant = false;
11599 go_assert(pf == fields->end());
11601 tree ret = build_constructor(type_tree, elts);
11603 TREE_CONSTANT(ret) = 1;
11607 // Export a struct construction.
11610 Struct_construction_expression::do_export(Export* exp) const
11612 exp->write_c_string("convert(");
11613 exp->write_type(this->type_);
11614 for (Expression_list::const_iterator pv = this->vals_->begin();
11615 pv != this->vals_->end();
11618 exp->write_c_string(", ");
11620 (*pv)->export_expression(exp);
11622 exp->write_c_string(")");
11625 // Dump ast representation of a struct construction expression.
11628 Struct_construction_expression::do_dump_expression(
11629 Ast_dump_context* ast_dump_context) const
11631 ast_dump_context->dump_type(this->type_);
11632 ast_dump_context->ostream() << "{";
11633 ast_dump_context->dump_expression_list(this->vals_);
11634 ast_dump_context->ostream() << "}";
11637 // Make a struct composite literal. This used by the thunk code.
11640 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
11641 source_location location)
11643 go_assert(type->struct_type() != NULL);
11644 return new Struct_construction_expression(type, vals, location);
11647 // Construct an array. This class is not used directly; instead we
11648 // use the child classes, Fixed_array_construction_expression and
11649 // Open_array_construction_expression.
11651 class Array_construction_expression : public Expression
11654 Array_construction_expression(Expression_classification classification,
11655 Type* type, Expression_list* vals,
11656 source_location location)
11657 : Expression(classification, location),
11658 type_(type), vals_(vals)
11662 // Return whether this is a constant initializer.
11664 is_constant_array() const;
11666 // Return the number of elements.
11668 element_count() const
11669 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
11673 do_traverse(Traverse* traverse);
11677 { return this->type_; }
11680 do_determine_type(const Type_context*);
11683 do_check_types(Gogo*);
11686 do_is_addressable() const
11690 do_export(Export*) const;
11692 // The list of values.
11695 { return this->vals_; }
11697 // Get a constructor tree for the array values.
11699 get_constructor_tree(Translate_context* context, tree type_tree);
11702 do_dump_expression(Ast_dump_context*) const;
11705 // The type of the array to construct.
11707 // The list of values.
11708 Expression_list* vals_;
11714 Array_construction_expression::do_traverse(Traverse* traverse)
11716 if (this->vals_ != NULL
11717 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11718 return TRAVERSE_EXIT;
11719 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11720 return TRAVERSE_EXIT;
11721 return TRAVERSE_CONTINUE;
11724 // Return whether this is a constant initializer.
11727 Array_construction_expression::is_constant_array() const
11729 if (this->vals_ == NULL)
11732 // There are no constant constructors for interfaces.
11733 if (this->type_->array_type()->element_type()->interface_type() != NULL)
11736 for (Expression_list::const_iterator pv = this->vals_->begin();
11737 pv != this->vals_->end();
11741 && !(*pv)->is_constant()
11742 && (!(*pv)->is_composite_literal()
11743 || (*pv)->is_nonconstant_composite_literal()))
11749 // Final type determination.
11752 Array_construction_expression::do_determine_type(const Type_context*)
11754 if (this->vals_ == NULL)
11756 Type_context subcontext(this->type_->array_type()->element_type(), false);
11757 for (Expression_list::const_iterator pv = this->vals_->begin();
11758 pv != this->vals_->end();
11762 (*pv)->determine_type(&subcontext);
11769 Array_construction_expression::do_check_types(Gogo*)
11771 if (this->vals_ == NULL)
11774 Array_type* at = this->type_->array_type();
11776 Type* element_type = at->element_type();
11777 for (Expression_list::const_iterator pv = this->vals_->begin();
11778 pv != this->vals_->end();
11782 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
11784 error_at((*pv)->location(),
11785 "incompatible type for element %d in composite literal",
11787 this->set_is_error();
11791 Expression* length = at->length();
11792 if (length != NULL && !length->is_error_expression())
11797 if (at->length()->integer_constant_value(true, val, &type))
11799 if (this->vals_->size() > mpz_get_ui(val))
11800 this->report_error(_("too many elements in composite literal"));
11806 // Get a constructor tree for the array values.
11809 Array_construction_expression::get_constructor_tree(Translate_context* context,
11812 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11813 (this->vals_ == NULL
11815 : this->vals_->size()));
11816 Type* element_type = this->type_->array_type()->element_type();
11817 bool is_constant = true;
11818 if (this->vals_ != NULL)
11821 for (Expression_list::const_iterator pv = this->vals_->begin();
11822 pv != this->vals_->end();
11825 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
11826 elt->index = size_int(i);
11829 Gogo* gogo = context->gogo();
11830 Btype* ebtype = element_type->get_backend(gogo);
11831 Bexpression *zv = gogo->backend()->zero_expression(ebtype);
11832 elt->value = expr_to_tree(zv);
11836 tree value_tree = (*pv)->get_tree(context);
11837 elt->value = Expression::convert_for_assignment(context,
11843 if (elt->value == error_mark_node)
11844 return error_mark_node;
11845 if (!TREE_CONSTANT(elt->value))
11846 is_constant = false;
11850 tree ret = build_constructor(type_tree, values);
11852 TREE_CONSTANT(ret) = 1;
11856 // Export an array construction.
11859 Array_construction_expression::do_export(Export* exp) const
11861 exp->write_c_string("convert(");
11862 exp->write_type(this->type_);
11863 if (this->vals_ != NULL)
11865 for (Expression_list::const_iterator pv = this->vals_->begin();
11866 pv != this->vals_->end();
11869 exp->write_c_string(", ");
11871 (*pv)->export_expression(exp);
11874 exp->write_c_string(")");
11877 // Dump ast representation of an array construction expressin.
11880 Array_construction_expression::do_dump_expression(
11881 Ast_dump_context* ast_dump_context) const
11883 Expression* length = this->type_->array_type() != NULL ?
11884 this->type_->array_type()->length() : NULL;
11886 ast_dump_context->ostream() << "[" ;
11887 if (length != NULL)
11889 ast_dump_context->dump_expression(length);
11891 ast_dump_context->ostream() << "]" ;
11892 ast_dump_context->dump_type(this->type_);
11893 ast_dump_context->ostream() << "{" ;
11894 ast_dump_context->dump_expression_list(this->vals_);
11895 ast_dump_context->ostream() << "}" ;
11899 // Construct a fixed array.
11901 class Fixed_array_construction_expression :
11902 public Array_construction_expression
11905 Fixed_array_construction_expression(Type* type, Expression_list* vals,
11906 source_location location)
11907 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
11908 type, vals, location)
11910 go_assert(type->array_type() != NULL
11911 && type->array_type()->length() != NULL);
11918 return new Fixed_array_construction_expression(this->type(),
11919 (this->vals() == NULL
11921 : this->vals()->copy()),
11926 do_get_tree(Translate_context*);
11929 do_dump_expression(Ast_dump_context*);
11932 // Return a tree for constructing a fixed array.
11935 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
11937 Type* type = this->type();
11938 Btype* btype = type->get_backend(context->gogo());
11939 return this->get_constructor_tree(context, type_to_tree(btype));
11942 // Dump ast representation of an array construction expressin.
11945 Fixed_array_construction_expression::do_dump_expression(
11946 Ast_dump_context* ast_dump_context)
11949 ast_dump_context->ostream() << "[";
11950 ast_dump_context->dump_expression (this->type()->array_type()->length());
11951 ast_dump_context->ostream() << "]";
11952 ast_dump_context->dump_type(this->type());
11953 ast_dump_context->ostream() << "{";
11954 ast_dump_context->dump_expression_list(this->vals());
11955 ast_dump_context->ostream() << "}";
11958 // Construct an open array.
11960 class Open_array_construction_expression : public Array_construction_expression
11963 Open_array_construction_expression(Type* type, Expression_list* vals,
11964 source_location location)
11965 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
11966 type, vals, location)
11968 go_assert(type->array_type() != NULL
11969 && type->array_type()->length() == NULL);
11973 // Note that taking the address of an open array literal is invalid.
11978 return new Open_array_construction_expression(this->type(),
11979 (this->vals() == NULL
11981 : this->vals()->copy()),
11986 do_get_tree(Translate_context*);
11989 // Return a tree for constructing an open array.
11992 Open_array_construction_expression::do_get_tree(Translate_context* context)
11994 Array_type* array_type = this->type()->array_type();
11995 if (array_type == NULL)
11997 go_assert(this->type()->is_error());
11998 return error_mark_node;
12001 Type* element_type = array_type->element_type();
12002 Btype* belement_type = element_type->get_backend(context->gogo());
12003 tree element_type_tree = type_to_tree(belement_type);
12004 if (element_type_tree == error_mark_node)
12005 return error_mark_node;
12009 if (this->vals() == NULL || this->vals()->empty())
12011 // We need to create a unique value.
12012 tree max = size_int(0);
12013 tree constructor_type = build_array_type(element_type_tree,
12014 build_index_type(max));
12015 if (constructor_type == error_mark_node)
12016 return error_mark_node;
12017 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
12018 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
12019 elt->index = size_int(0);
12020 Gogo* gogo = context->gogo();
12021 Btype* btype = element_type->get_backend(gogo);
12022 elt->value = expr_to_tree(gogo->backend()->zero_expression(btype));
12023 values = build_constructor(constructor_type, vec);
12024 if (TREE_CONSTANT(elt->value))
12025 TREE_CONSTANT(values) = 1;
12026 length_tree = size_int(0);
12030 tree max = size_int(this->vals()->size() - 1);
12031 tree constructor_type = build_array_type(element_type_tree,
12032 build_index_type(max));
12033 if (constructor_type == error_mark_node)
12034 return error_mark_node;
12035 values = this->get_constructor_tree(context, constructor_type);
12036 length_tree = size_int(this->vals()->size());
12039 if (values == error_mark_node)
12040 return error_mark_node;
12042 bool is_constant_initializer = TREE_CONSTANT(values);
12044 // We have to copy the initial values into heap memory if we are in
12045 // a function or if the values are not constants. We also have to
12046 // copy them if they may contain pointers in a non-constant context,
12047 // as otherwise the garbage collector won't see them.
12048 bool copy_to_heap = (context->function() != NULL
12049 || !is_constant_initializer
12050 || (element_type->has_pointer()
12051 && !context->is_const()));
12053 if (is_constant_initializer)
12055 tree tmp = build_decl(this->location(), VAR_DECL,
12056 create_tmp_var_name("C"), TREE_TYPE(values));
12057 DECL_EXTERNAL(tmp) = 0;
12058 TREE_PUBLIC(tmp) = 0;
12059 TREE_STATIC(tmp) = 1;
12060 DECL_ARTIFICIAL(tmp) = 1;
12063 // If we are not copying the value to the heap, we will only
12064 // initialize the value once, so we can use this directly
12065 // rather than copying it. In that case we can't make it
12066 // read-only, because the program is permitted to change it.
12067 TREE_READONLY(tmp) = 1;
12068 TREE_CONSTANT(tmp) = 1;
12070 DECL_INITIAL(tmp) = values;
12071 rest_of_decl_compilation(tmp, 1, 0);
12079 // the initializer will only run once.
12080 space = build_fold_addr_expr(values);
12085 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
12086 space = context->gogo()->allocate_memory(element_type, memsize,
12088 space = save_expr(space);
12090 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
12091 tree ref = build_fold_indirect_ref_loc(this->location(), s);
12092 TREE_THIS_NOTRAP(ref) = 1;
12093 set = build2(MODIFY_EXPR, void_type_node, ref, values);
12096 // Build a constructor for the open array.
12098 tree type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
12099 if (type_tree == error_mark_node)
12100 return error_mark_node;
12101 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
12103 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
12105 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
12106 tree field = TYPE_FIELDS(type_tree);
12107 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
12108 elt->index = field;
12109 elt->value = fold_convert(TREE_TYPE(field), space);
12111 elt = VEC_quick_push(constructor_elt, init, NULL);
12112 field = DECL_CHAIN(field);
12113 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
12114 elt->index = field;
12115 elt->value = fold_convert(TREE_TYPE(field), length_tree);
12117 elt = VEC_quick_push(constructor_elt, init, NULL);
12118 field = DECL_CHAIN(field);
12119 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
12120 elt->index = field;
12121 elt->value = fold_convert(TREE_TYPE(field), length_tree);
12123 tree constructor = build_constructor(type_tree, init);
12124 if (constructor == error_mark_node)
12125 return error_mark_node;
12127 TREE_CONSTANT(constructor) = 1;
12129 if (set == NULL_TREE)
12130 return constructor;
12132 return build2(COMPOUND_EXPR, type_tree, set, constructor);
12135 // Make a slice composite literal. This is used by the type
12136 // descriptor code.
12139 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
12140 source_location location)
12142 go_assert(type->is_open_array_type());
12143 return new Open_array_construction_expression(type, vals, location);
12146 // Construct a map.
12148 class Map_construction_expression : public Expression
12151 Map_construction_expression(Type* type, Expression_list* vals,
12152 source_location location)
12153 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
12154 type_(type), vals_(vals)
12155 { go_assert(vals == NULL || vals->size() % 2 == 0); }
12159 do_traverse(Traverse* traverse);
12163 { return this->type_; }
12166 do_determine_type(const Type_context*);
12169 do_check_types(Gogo*);
12174 return new Map_construction_expression(this->type_, this->vals_->copy(),
12179 do_get_tree(Translate_context*);
12182 do_export(Export*) const;
12185 do_dump_expression(Ast_dump_context*) const;
12188 // The type of the map to construct.
12190 // The list of values.
12191 Expression_list* vals_;
12197 Map_construction_expression::do_traverse(Traverse* traverse)
12199 if (this->vals_ != NULL
12200 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12201 return TRAVERSE_EXIT;
12202 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12203 return TRAVERSE_EXIT;
12204 return TRAVERSE_CONTINUE;
12207 // Final type determination.
12210 Map_construction_expression::do_determine_type(const Type_context*)
12212 if (this->vals_ == NULL)
12215 Map_type* mt = this->type_->map_type();
12216 Type_context key_context(mt->key_type(), false);
12217 Type_context val_context(mt->val_type(), false);
12218 for (Expression_list::const_iterator pv = this->vals_->begin();
12219 pv != this->vals_->end();
12222 (*pv)->determine_type(&key_context);
12224 (*pv)->determine_type(&val_context);
12231 Map_construction_expression::do_check_types(Gogo*)
12233 if (this->vals_ == NULL)
12236 Map_type* mt = this->type_->map_type();
12238 Type* key_type = mt->key_type();
12239 Type* val_type = mt->val_type();
12240 for (Expression_list::const_iterator pv = this->vals_->begin();
12241 pv != this->vals_->end();
12244 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
12246 error_at((*pv)->location(),
12247 "incompatible type for element %d key in map construction",
12249 this->set_is_error();
12252 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
12254 error_at((*pv)->location(),
12255 ("incompatible type for element %d value "
12256 "in map construction"),
12258 this->set_is_error();
12263 // Return a tree for constructing a map.
12266 Map_construction_expression::do_get_tree(Translate_context* context)
12268 Gogo* gogo = context->gogo();
12269 source_location loc = this->location();
12271 Map_type* mt = this->type_->map_type();
12273 // Build a struct to hold the key and value.
12274 tree struct_type = make_node(RECORD_TYPE);
12276 Type* key_type = mt->key_type();
12277 tree id = get_identifier("__key");
12278 tree key_type_tree = type_to_tree(key_type->get_backend(gogo));
12279 if (key_type_tree == error_mark_node)
12280 return error_mark_node;
12281 tree key_field = build_decl(loc, FIELD_DECL, id, key_type_tree);
12282 DECL_CONTEXT(key_field) = struct_type;
12283 TYPE_FIELDS(struct_type) = key_field;
12285 Type* val_type = mt->val_type();
12286 id = get_identifier("__val");
12287 tree val_type_tree = type_to_tree(val_type->get_backend(gogo));
12288 if (val_type_tree == error_mark_node)
12289 return error_mark_node;
12290 tree val_field = build_decl(loc, FIELD_DECL, id, val_type_tree);
12291 DECL_CONTEXT(val_field) = struct_type;
12292 DECL_CHAIN(key_field) = val_field;
12294 layout_type(struct_type);
12296 bool is_constant = true;
12301 if (this->vals_ == NULL || this->vals_->empty())
12303 valaddr = null_pointer_node;
12304 make_tmp = NULL_TREE;
12308 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
12309 this->vals_->size() / 2);
12311 for (Expression_list::const_iterator pv = this->vals_->begin();
12312 pv != this->vals_->end();
12315 bool one_is_constant = true;
12317 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
12319 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
12320 elt->index = key_field;
12321 tree val_tree = (*pv)->get_tree(context);
12322 elt->value = Expression::convert_for_assignment(context, key_type,
12325 if (elt->value == error_mark_node)
12326 return error_mark_node;
12327 if (!TREE_CONSTANT(elt->value))
12328 one_is_constant = false;
12332 elt = VEC_quick_push(constructor_elt, one, NULL);
12333 elt->index = val_field;
12334 val_tree = (*pv)->get_tree(context);
12335 elt->value = Expression::convert_for_assignment(context, val_type,
12338 if (elt->value == error_mark_node)
12339 return error_mark_node;
12340 if (!TREE_CONSTANT(elt->value))
12341 one_is_constant = false;
12343 elt = VEC_quick_push(constructor_elt, values, NULL);
12344 elt->index = size_int(i);
12345 elt->value = build_constructor(struct_type, one);
12346 if (one_is_constant)
12347 TREE_CONSTANT(elt->value) = 1;
12349 is_constant = false;
12352 tree index_type = build_index_type(size_int(i - 1));
12353 tree array_type = build_array_type(struct_type, index_type);
12354 tree init = build_constructor(array_type, values);
12356 TREE_CONSTANT(init) = 1;
12358 if (current_function_decl != NULL)
12360 tmp = create_tmp_var(array_type, get_name(array_type));
12361 DECL_INITIAL(tmp) = init;
12362 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
12363 TREE_ADDRESSABLE(tmp) = 1;
12367 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
12368 DECL_EXTERNAL(tmp) = 0;
12369 TREE_PUBLIC(tmp) = 0;
12370 TREE_STATIC(tmp) = 1;
12371 DECL_ARTIFICIAL(tmp) = 1;
12372 if (!TREE_CONSTANT(init))
12373 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
12377 TREE_READONLY(tmp) = 1;
12378 TREE_CONSTANT(tmp) = 1;
12379 DECL_INITIAL(tmp) = init;
12380 make_tmp = NULL_TREE;
12382 rest_of_decl_compilation(tmp, 1, 0);
12385 valaddr = build_fold_addr_expr(tmp);
12388 tree descriptor = mt->map_descriptor_pointer(gogo, loc);
12390 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
12391 if (type_tree == error_mark_node)
12392 return error_mark_node;
12394 static tree construct_map_fndecl;
12395 tree call = Gogo::call_builtin(&construct_map_fndecl,
12397 "__go_construct_map",
12400 TREE_TYPE(descriptor),
12405 TYPE_SIZE_UNIT(struct_type),
12407 byte_position(val_field),
12409 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
12410 const_ptr_type_node,
12411 fold_convert(const_ptr_type_node, valaddr));
12412 if (call == error_mark_node)
12413 return error_mark_node;
12416 if (make_tmp == NULL)
12419 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
12423 // Export an array construction.
12426 Map_construction_expression::do_export(Export* exp) const
12428 exp->write_c_string("convert(");
12429 exp->write_type(this->type_);
12430 for (Expression_list::const_iterator pv = this->vals_->begin();
12431 pv != this->vals_->end();
12434 exp->write_c_string(", ");
12435 (*pv)->export_expression(exp);
12437 exp->write_c_string(")");
12440 // Dump ast representation for a map construction expression.
12443 Map_construction_expression::do_dump_expression(
12444 Ast_dump_context* ast_dump_context) const
12446 ast_dump_context->ostream() << "{" ;
12447 ast_dump_context->dump_expression_list(this->vals_, true);
12448 ast_dump_context->ostream() << "}";
12451 // A general composite literal. This is lowered to a type specific
12454 class Composite_literal_expression : public Parser_expression
12457 Composite_literal_expression(Type* type, int depth, bool has_keys,
12458 Expression_list* vals, source_location location)
12459 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
12460 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
12465 do_traverse(Traverse* traverse);
12468 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
12473 return new Composite_literal_expression(this->type_, this->depth_,
12475 (this->vals_ == NULL
12477 : this->vals_->copy()),
12482 do_dump_expression(Ast_dump_context*) const;
12486 lower_struct(Gogo*, Type*);
12489 lower_array(Type*);
12492 make_array(Type*, Expression_list*);
12495 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
12497 // The type of the composite literal.
12499 // The depth within a list of composite literals within a composite
12500 // literal, when the type is omitted.
12502 // The values to put in the composite literal.
12503 Expression_list* vals_;
12504 // If this is true, then VALS_ is a list of pairs: a key and a
12505 // value. In an array initializer, a missing key will be NULL.
12512 Composite_literal_expression::do_traverse(Traverse* traverse)
12514 if (this->vals_ != NULL
12515 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12516 return TRAVERSE_EXIT;
12517 return Type::traverse(this->type_, traverse);
12520 // Lower a generic composite literal into a specific version based on
12524 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
12525 Statement_inserter* inserter, int)
12527 Type* type = this->type_;
12529 for (int depth = this->depth_; depth > 0; --depth)
12531 if (type->array_type() != NULL)
12532 type = type->array_type()->element_type();
12533 else if (type->map_type() != NULL)
12534 type = type->map_type()->val_type();
12537 if (!type->is_error())
12538 error_at(this->location(),
12539 ("may only omit types within composite literals "
12540 "of slice, array, or map type"));
12541 return Expression::make_error(this->location());
12545 if (type->is_error())
12546 return Expression::make_error(this->location());
12547 else if (type->struct_type() != NULL)
12548 return this->lower_struct(gogo, type);
12549 else if (type->array_type() != NULL)
12550 return this->lower_array(type);
12551 else if (type->map_type() != NULL)
12552 return this->lower_map(gogo, function, inserter, type);
12555 error_at(this->location(),
12556 ("expected struct, slice, array, or map type "
12557 "for composite literal"));
12558 return Expression::make_error(this->location());
12562 // Lower a struct composite literal.
12565 Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
12567 source_location location = this->location();
12568 Struct_type* st = type->struct_type();
12569 if (this->vals_ == NULL || !this->has_keys_)
12570 return new Struct_construction_expression(type, this->vals_, location);
12572 size_t field_count = st->field_count();
12573 std::vector<Expression*> vals(field_count);
12574 Expression_list::const_iterator p = this->vals_->begin();
12575 while (p != this->vals_->end())
12577 Expression* name_expr = *p;
12580 go_assert(p != this->vals_->end());
12581 Expression* val = *p;
12585 if (name_expr == NULL)
12587 error_at(val->location(), "mixture of field and value initializers");
12588 return Expression::make_error(location);
12591 bool bad_key = false;
12593 const Named_object* no = NULL;
12594 switch (name_expr->classification())
12596 case EXPRESSION_UNKNOWN_REFERENCE:
12597 name = name_expr->unknown_expression()->name();
12600 case EXPRESSION_CONST_REFERENCE:
12601 no = static_cast<Const_expression*>(name_expr)->named_object();
12604 case EXPRESSION_TYPE:
12606 Type* t = name_expr->type();
12607 Named_type* nt = t->named_type();
12611 no = nt->named_object();
12615 case EXPRESSION_VAR_REFERENCE:
12616 no = name_expr->var_expression()->named_object();
12619 case EXPRESSION_FUNC_REFERENCE:
12620 no = name_expr->func_expression()->named_object();
12623 case EXPRESSION_UNARY:
12624 // If there is a local variable around with the same name as
12625 // the field, and this occurs in the closure, then the
12626 // parser may turn the field reference into an indirection
12627 // through the closure. FIXME: This is a mess.
12630 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
12631 if (ue->op() == OPERATOR_MULT)
12633 Field_reference_expression* fre =
12634 ue->operand()->field_reference_expression();
12638 fre->expr()->type()->deref()->struct_type();
12641 const Struct_field* sf = st->field(fre->field_index());
12642 name = sf->field_name();
12644 // See below. FIXME.
12645 if (!Gogo::is_hidden_name(name)
12649 if (gogo->lookup_global(name.c_str()) != NULL)
12650 name = gogo->pack_hidden_name(name, false);
12654 snprintf(buf, sizeof buf, "%u", fre->field_index());
12655 size_t buflen = strlen(buf);
12656 if (name.compare(name.length() - buflen, buflen, buf)
12659 name = name.substr(0, name.length() - buflen);
12674 error_at(name_expr->location(), "expected struct field name");
12675 return Expression::make_error(location);
12682 // A predefined name won't be packed. If it starts with a
12683 // lower case letter we need to check for that case, because
12684 // the field name will be packed. FIXME.
12685 if (!Gogo::is_hidden_name(name)
12689 Named_object* gno = gogo->lookup_global(name.c_str());
12691 name = gogo->pack_hidden_name(name, false);
12695 unsigned int index;
12696 const Struct_field* sf = st->find_local_field(name, &index);
12699 error_at(name_expr->location(), "unknown field %qs in %qs",
12700 Gogo::message_name(name).c_str(),
12701 (type->named_type() != NULL
12702 ? type->named_type()->message_name().c_str()
12703 : "unnamed struct"));
12704 return Expression::make_error(location);
12706 if (vals[index] != NULL)
12708 error_at(name_expr->location(),
12709 "duplicate value for field %qs in %qs",
12710 Gogo::message_name(name).c_str(),
12711 (type->named_type() != NULL
12712 ? type->named_type()->message_name().c_str()
12713 : "unnamed struct"));
12714 return Expression::make_error(location);
12720 Expression_list* list = new Expression_list;
12721 list->reserve(field_count);
12722 for (size_t i = 0; i < field_count; ++i)
12723 list->push_back(vals[i]);
12725 return new Struct_construction_expression(type, list, location);
12728 // Lower an array composite literal.
12731 Composite_literal_expression::lower_array(Type* type)
12733 source_location location = this->location();
12734 if (this->vals_ == NULL || !this->has_keys_)
12735 return this->make_array(type, this->vals_);
12737 std::vector<Expression*> vals;
12738 vals.reserve(this->vals_->size());
12739 unsigned long index = 0;
12740 Expression_list::const_iterator p = this->vals_->begin();
12741 while (p != this->vals_->end())
12743 Expression* index_expr = *p;
12746 go_assert(p != this->vals_->end());
12747 Expression* val = *p;
12751 if (index_expr != NULL)
12757 if (!index_expr->integer_constant_value(true, ival, &dummy))
12760 error_at(index_expr->location(),
12761 "index expression is not integer constant");
12762 return Expression::make_error(location);
12765 if (mpz_sgn(ival) < 0)
12768 error_at(index_expr->location(), "index expression is negative");
12769 return Expression::make_error(location);
12772 index = mpz_get_ui(ival);
12773 if (mpz_cmp_ui(ival, index) != 0)
12776 error_at(index_expr->location(), "index value overflow");
12777 return Expression::make_error(location);
12780 Named_type* ntype = Type::lookup_integer_type("int");
12781 Integer_type* inttype = ntype->integer_type();
12783 mpz_init_set_ui(max, 1);
12784 mpz_mul_2exp(max, max, inttype->bits() - 1);
12785 bool ok = mpz_cmp(ival, max) < 0;
12790 error_at(index_expr->location(), "index value overflow");
12791 return Expression::make_error(location);
12796 // FIXME: Our representation isn't very good; this avoids
12798 if (index > 0x1000000)
12800 error_at(index_expr->location(), "index too large for compiler");
12801 return Expression::make_error(location);
12805 if (index == vals.size())
12806 vals.push_back(val);
12809 if (index > vals.size())
12811 vals.reserve(index + 32);
12812 vals.resize(index + 1, static_cast<Expression*>(NULL));
12814 if (vals[index] != NULL)
12816 error_at((index_expr != NULL
12817 ? index_expr->location()
12818 : val->location()),
12819 "duplicate value for index %lu",
12821 return Expression::make_error(location);
12829 size_t size = vals.size();
12830 Expression_list* list = new Expression_list;
12831 list->reserve(size);
12832 for (size_t i = 0; i < size; ++i)
12833 list->push_back(vals[i]);
12835 return this->make_array(type, list);
12838 // Actually build the array composite literal. This handles
12842 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
12844 source_location location = this->location();
12845 Array_type* at = type->array_type();
12846 if (at->length() != NULL && at->length()->is_nil_expression())
12848 size_t size = vals == NULL ? 0 : vals->size();
12850 mpz_init_set_ui(vlen, size);
12851 Expression* elen = Expression::make_integer(&vlen, NULL, location);
12853 at = Type::make_array_type(at->element_type(), elen);
12856 if (at->length() != NULL)
12857 return new Fixed_array_construction_expression(type, vals, location);
12859 return new Open_array_construction_expression(type, vals, location);
12862 // Lower a map composite literal.
12865 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
12866 Statement_inserter* inserter,
12869 source_location location = this->location();
12870 if (this->vals_ != NULL)
12872 if (!this->has_keys_)
12874 error_at(location, "map composite literal must have keys");
12875 return Expression::make_error(location);
12878 for (Expression_list::iterator p = this->vals_->begin();
12879 p != this->vals_->end();
12885 error_at((*p)->location(),
12886 "map composite literal must have keys for every value");
12887 return Expression::make_error(location);
12889 // Make sure we have lowered the key; it may not have been
12890 // lowered in order to handle keys for struct composite
12891 // literals. Lower it now to get the right error message.
12892 if ((*p)->unknown_expression() != NULL)
12894 (*p)->unknown_expression()->clear_is_composite_literal_key();
12895 gogo->lower_expression(function, inserter, &*p);
12896 go_assert((*p)->is_error_expression());
12897 return Expression::make_error(location);
12902 return new Map_construction_expression(type, this->vals_, location);
12905 // Dump ast representation for a composite literal expression.
12908 Composite_literal_expression::do_dump_expression(
12909 Ast_dump_context* ast_dump_context) const
12911 ast_dump_context->ostream() << "composite(";
12912 ast_dump_context->dump_type(this->type_);
12913 ast_dump_context->ostream() << ", {";
12914 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_);
12915 ast_dump_context->ostream() << "})";
12918 // Make a composite literal expression.
12921 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
12922 Expression_list* vals,
12923 source_location location)
12925 return new Composite_literal_expression(type, depth, has_keys, vals,
12929 // Return whether this expression is a composite literal.
12932 Expression::is_composite_literal() const
12934 switch (this->classification_)
12936 case EXPRESSION_COMPOSITE_LITERAL:
12937 case EXPRESSION_STRUCT_CONSTRUCTION:
12938 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12939 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12940 case EXPRESSION_MAP_CONSTRUCTION:
12947 // Return whether this expression is a composite literal which is not
12951 Expression::is_nonconstant_composite_literal() const
12953 switch (this->classification_)
12955 case EXPRESSION_STRUCT_CONSTRUCTION:
12957 const Struct_construction_expression *psce =
12958 static_cast<const Struct_construction_expression*>(this);
12959 return !psce->is_constant_struct();
12961 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12963 const Fixed_array_construction_expression *pace =
12964 static_cast<const Fixed_array_construction_expression*>(this);
12965 return !pace->is_constant_array();
12967 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12969 const Open_array_construction_expression *pace =
12970 static_cast<const Open_array_construction_expression*>(this);
12971 return !pace->is_constant_array();
12973 case EXPRESSION_MAP_CONSTRUCTION:
12980 // Return true if this is a reference to a local variable.
12983 Expression::is_local_variable() const
12985 const Var_expression* ve = this->var_expression();
12988 const Named_object* no = ve->named_object();
12989 return (no->is_result_variable()
12990 || (no->is_variable() && !no->var_value()->is_global()));
12993 // Class Type_guard_expression.
12998 Type_guard_expression::do_traverse(Traverse* traverse)
13000 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
13001 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
13002 return TRAVERSE_EXIT;
13003 return TRAVERSE_CONTINUE;
13006 // Check types of a type guard expression. The expression must have
13007 // an interface type, but the actual type conversion is checked at run
13011 Type_guard_expression::do_check_types(Gogo*)
13013 // 6g permits using a type guard with unsafe.pointer; we are
13015 Type* expr_type = this->expr_->type();
13016 if (expr_type->is_unsafe_pointer_type())
13018 if (this->type_->points_to() == NULL
13019 && (this->type_->integer_type() == NULL
13020 || (this->type_->forwarded()
13021 != Type::lookup_integer_type("uintptr"))))
13022 this->report_error(_("invalid unsafe.Pointer conversion"));
13024 else if (this->type_->is_unsafe_pointer_type())
13026 if (expr_type->points_to() == NULL
13027 && (expr_type->integer_type() == NULL
13028 || (expr_type->forwarded()
13029 != Type::lookup_integer_type("uintptr"))))
13030 this->report_error(_("invalid unsafe.Pointer conversion"));
13032 else if (expr_type->interface_type() == NULL)
13034 if (!expr_type->is_error() && !this->type_->is_error())
13035 this->report_error(_("type assertion only valid for interface types"));
13036 this->set_is_error();
13038 else if (this->type_->interface_type() == NULL)
13040 std::string reason;
13041 if (!expr_type->interface_type()->implements_interface(this->type_,
13044 if (!this->type_->is_error())
13046 if (reason.empty())
13047 this->report_error(_("impossible type assertion: "
13048 "type does not implement interface"));
13050 error_at(this->location(),
13051 ("impossible type assertion: "
13052 "type does not implement interface (%s)"),
13055 this->set_is_error();
13060 // Return a tree for a type guard expression.
13063 Type_guard_expression::do_get_tree(Translate_context* context)
13065 Gogo* gogo = context->gogo();
13066 tree expr_tree = this->expr_->get_tree(context);
13067 if (expr_tree == error_mark_node)
13068 return error_mark_node;
13069 Type* expr_type = this->expr_->type();
13070 if ((this->type_->is_unsafe_pointer_type()
13071 && (expr_type->points_to() != NULL
13072 || expr_type->integer_type() != NULL))
13073 || (expr_type->is_unsafe_pointer_type()
13074 && this->type_->points_to() != NULL))
13075 return convert_to_pointer(type_to_tree(this->type_->get_backend(gogo)),
13077 else if (expr_type->is_unsafe_pointer_type()
13078 && this->type_->integer_type() != NULL)
13079 return convert_to_integer(type_to_tree(this->type_->get_backend(gogo)),
13081 else if (this->type_->interface_type() != NULL)
13082 return Expression::convert_interface_to_interface(context, this->type_,
13083 this->expr_->type(),
13087 return Expression::convert_for_assignment(context, this->type_,
13088 this->expr_->type(), expr_tree,
13092 // Dump ast representation for a type guard expression.
13095 Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
13098 this->expr_->dump_expression(ast_dump_context);
13099 ast_dump_context->ostream() << ".";
13100 ast_dump_context->dump_type(this->type_);
13103 // Make a type guard expression.
13106 Expression::make_type_guard(Expression* expr, Type* type,
13107 source_location location)
13109 return new Type_guard_expression(expr, type, location);
13112 // Class Heap_composite_expression.
13114 // When you take the address of a composite literal, it is allocated
13115 // on the heap. This class implements that.
13117 class Heap_composite_expression : public Expression
13120 Heap_composite_expression(Expression* expr, source_location location)
13121 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
13127 do_traverse(Traverse* traverse)
13128 { return Expression::traverse(&this->expr_, traverse); }
13132 { return Type::make_pointer_type(this->expr_->type()); }
13135 do_determine_type(const Type_context*)
13136 { this->expr_->determine_type_no_context(); }
13141 return Expression::make_heap_composite(this->expr_->copy(),
13146 do_get_tree(Translate_context*);
13148 // We only export global objects, and the parser does not generate
13149 // this in global scope.
13151 do_export(Export*) const
13152 { go_unreachable(); }
13155 do_dump_expression(Ast_dump_context*) const;
13158 // The composite literal which is being put on the heap.
13162 // Return a tree which allocates a composite literal on the heap.
13165 Heap_composite_expression::do_get_tree(Translate_context* context)
13167 tree expr_tree = this->expr_->get_tree(context);
13168 if (expr_tree == error_mark_node)
13169 return error_mark_node;
13170 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
13171 go_assert(TREE_CODE(expr_size) == INTEGER_CST);
13172 tree space = context->gogo()->allocate_memory(this->expr_->type(),
13173 expr_size, this->location());
13174 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
13175 space = save_expr(space);
13176 tree ref = build_fold_indirect_ref_loc(this->location(), space);
13177 TREE_THIS_NOTRAP(ref) = 1;
13178 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
13179 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
13181 SET_EXPR_LOCATION(ret, this->location());
13185 // Dump ast representation for a heap composite expression.
13188 Heap_composite_expression::do_dump_expression(
13189 Ast_dump_context* ast_dump_context) const
13191 ast_dump_context->ostream() << "&(";
13192 ast_dump_context->dump_expression(this->expr_);
13193 ast_dump_context->ostream() << ")";
13196 // Allocate a composite literal on the heap.
13199 Expression::make_heap_composite(Expression* expr, source_location location)
13201 return new Heap_composite_expression(expr, location);
13204 // Class Receive_expression.
13206 // Return the type of a receive expression.
13209 Receive_expression::do_type()
13211 Channel_type* channel_type = this->channel_->type()->channel_type();
13212 if (channel_type == NULL)
13213 return Type::make_error_type();
13214 return channel_type->element_type();
13217 // Check types for a receive expression.
13220 Receive_expression::do_check_types(Gogo*)
13222 Type* type = this->channel_->type();
13223 if (type->is_error())
13225 this->set_is_error();
13228 if (type->channel_type() == NULL)
13230 this->report_error(_("expected channel"));
13233 if (!type->channel_type()->may_receive())
13235 this->report_error(_("invalid receive on send-only channel"));
13240 // Get a tree for a receive expression.
13243 Receive_expression::do_get_tree(Translate_context* context)
13245 Channel_type* channel_type = this->channel_->type()->channel_type();
13246 if (channel_type == NULL)
13248 go_assert(this->channel_->type()->is_error());
13249 return error_mark_node;
13251 Type* element_type = channel_type->element_type();
13252 Btype* element_type_btype = element_type->get_backend(context->gogo());
13253 tree element_type_tree = type_to_tree(element_type_btype);
13255 tree channel = this->channel_->get_tree(context);
13256 if (element_type_tree == error_mark_node || channel == error_mark_node)
13257 return error_mark_node;
13259 return Gogo::receive_from_channel(element_type_tree, channel,
13260 this->for_select_, this->location());
13263 // Dump ast representation for a receive expression.
13266 Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
13268 ast_dump_context->ostream() << " <- " ;
13269 ast_dump_context->dump_expression(channel_);
13272 // Make a receive expression.
13274 Receive_expression*
13275 Expression::make_receive(Expression* channel, source_location location)
13277 return new Receive_expression(channel, location);
13280 // An expression which evaluates to a pointer to the type descriptor
13283 class Type_descriptor_expression : public Expression
13286 Type_descriptor_expression(Type* type, source_location location)
13287 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
13294 { return Type::make_type_descriptor_ptr_type(); }
13297 do_determine_type(const Type_context*)
13305 do_get_tree(Translate_context* context)
13307 return this->type_->type_descriptor_pointer(context->gogo(),
13312 do_dump_expression(Ast_dump_context*) const;
13315 // The type for which this is the descriptor.
13319 // Dump ast representation for a type descriptor expression.
13322 Type_descriptor_expression::do_dump_expression(
13323 Ast_dump_context* ast_dump_context) const
13325 ast_dump_context->dump_type(this->type_);
13328 // Make a type descriptor expression.
13331 Expression::make_type_descriptor(Type* type, source_location location)
13333 return new Type_descriptor_expression(type, location);
13336 // An expression which evaluates to some characteristic of a type.
13337 // This is only used to initialize fields of a type descriptor. Using
13338 // a new expression class is slightly inefficient but gives us a good
13339 // separation between the frontend and the middle-end with regard to
13340 // how types are laid out.
13342 class Type_info_expression : public Expression
13345 Type_info_expression(Type* type, Type_info type_info)
13346 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
13347 type_(type), type_info_(type_info)
13355 do_determine_type(const Type_context*)
13363 do_get_tree(Translate_context* context);
13366 do_dump_expression(Ast_dump_context*) const;
13369 // The type for which we are getting information.
13371 // What information we want.
13372 Type_info type_info_;
13375 // The type is chosen to match what the type descriptor struct
13379 Type_info_expression::do_type()
13381 switch (this->type_info_)
13383 case TYPE_INFO_SIZE:
13384 return Type::lookup_integer_type("uintptr");
13385 case TYPE_INFO_ALIGNMENT:
13386 case TYPE_INFO_FIELD_ALIGNMENT:
13387 return Type::lookup_integer_type("uint8");
13393 // Return type information in GENERIC.
13396 Type_info_expression::do_get_tree(Translate_context* context)
13398 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
13399 if (type_tree == error_mark_node)
13400 return error_mark_node;
13402 tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
13403 go_assert(val_type_tree != error_mark_node);
13405 if (this->type_info_ == TYPE_INFO_SIZE)
13406 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
13407 TYPE_SIZE_UNIT(type_tree));
13411 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
13412 val = go_type_alignment(type_tree);
13414 val = go_field_alignment(type_tree);
13415 return build_int_cstu(val_type_tree, val);
13419 // Dump ast representation for a type info expression.
13422 Type_info_expression::do_dump_expression(
13423 Ast_dump_context* ast_dump_context) const
13425 ast_dump_context->ostream() << "typeinfo(";
13426 ast_dump_context->dump_type(this->type_);
13427 ast_dump_context->ostream() << ",";
13428 ast_dump_context->ostream() <<
13429 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment"
13430 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment"
13431 : this->type_info_ == TYPE_INFO_SIZE ? "size "
13433 ast_dump_context->ostream() << ")";
13436 // Make a type info expression.
13439 Expression::make_type_info(Type* type, Type_info type_info)
13441 return new Type_info_expression(type, type_info);
13444 // An expression which evaluates to the offset of a field within a
13445 // struct. This, like Type_info_expression, q.v., is only used to
13446 // initialize fields of a type descriptor.
13448 class Struct_field_offset_expression : public Expression
13451 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
13452 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
13453 type_(type), field_(field)
13459 { return Type::lookup_integer_type("uintptr"); }
13462 do_determine_type(const Type_context*)
13470 do_get_tree(Translate_context* context);
13473 do_dump_expression(Ast_dump_context*) const;
13476 // The type of the struct.
13477 Struct_type* type_;
13479 const Struct_field* field_;
13482 // Return a struct field offset in GENERIC.
13485 Struct_field_offset_expression::do_get_tree(Translate_context* context)
13487 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
13488 if (type_tree == error_mark_node)
13489 return error_mark_node;
13491 tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
13492 go_assert(val_type_tree != error_mark_node);
13494 const Struct_field_list* fields = this->type_->fields();
13495 tree struct_field_tree = TYPE_FIELDS(type_tree);
13496 Struct_field_list::const_iterator p;
13497 for (p = fields->begin();
13498 p != fields->end();
13499 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
13501 go_assert(struct_field_tree != NULL_TREE);
13502 if (&*p == this->field_)
13505 go_assert(&*p == this->field_);
13507 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
13508 byte_position(struct_field_tree));
13511 // Dump ast representation for a struct field offset expression.
13514 Struct_field_offset_expression::do_dump_expression(
13515 Ast_dump_context* ast_dump_context) const
13517 ast_dump_context->ostream() << "unsafe.Offsetof(";
13518 ast_dump_context->dump_type(this->type_);
13519 ast_dump_context->ostream() << '.';
13520 ast_dump_context->ostream() <<
13521 Gogo::message_name(this->field_->field_name());
13522 ast_dump_context->ostream() << ")";
13525 // Make an expression for a struct field offset.
13528 Expression::make_struct_field_offset(Struct_type* type,
13529 const Struct_field* field)
13531 return new Struct_field_offset_expression(type, field);
13534 // An expression which evaluates to a pointer to the map descriptor of
13537 class Map_descriptor_expression : public Expression
13540 Map_descriptor_expression(Map_type* type, source_location location)
13541 : Expression(EXPRESSION_MAP_DESCRIPTOR, location),
13548 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
13551 do_determine_type(const Type_context*)
13559 do_get_tree(Translate_context* context)
13561 return this->type_->map_descriptor_pointer(context->gogo(),
13566 do_dump_expression(Ast_dump_context*) const;
13569 // The type for which this is the descriptor.
13573 // Dump ast representation for a map descriptor expression.
13576 Map_descriptor_expression::do_dump_expression(
13577 Ast_dump_context* ast_dump_context) const
13579 ast_dump_context->ostream() << "map_descriptor(";
13580 ast_dump_context->dump_type(this->type_);
13581 ast_dump_context->ostream() << ")";
13584 // Make a map descriptor expression.
13587 Expression::make_map_descriptor(Map_type* type, source_location location)
13589 return new Map_descriptor_expression(type, location);
13592 // An expression which evaluates to the address of an unnamed label.
13594 class Label_addr_expression : public Expression
13597 Label_addr_expression(Label* label, source_location location)
13598 : Expression(EXPRESSION_LABEL_ADDR, location),
13605 { return Type::make_pointer_type(Type::make_void_type()); }
13608 do_determine_type(const Type_context*)
13613 { return new Label_addr_expression(this->label_, this->location()); }
13616 do_get_tree(Translate_context* context)
13618 return expr_to_tree(this->label_->get_addr(context, this->location()));
13622 do_dump_expression(Ast_dump_context* ast_dump_context) const
13623 { ast_dump_context->ostream() << this->label_->name(); }
13626 // The label whose address we are taking.
13630 // Make an expression for the address of an unnamed label.
13633 Expression::make_label_addr(Label* label, source_location location)
13635 return new Label_addr_expression(label, location);
13638 // Import an expression. This comes at the end in order to see the
13639 // various class definitions.
13642 Expression::import_expression(Import* imp)
13644 int c = imp->peek_char();
13645 if (imp->match_c_string("- ")
13646 || imp->match_c_string("! ")
13647 || imp->match_c_string("^ "))
13648 return Unary_expression::do_import(imp);
13650 return Binary_expression::do_import(imp);
13651 else if (imp->match_c_string("true")
13652 || imp->match_c_string("false"))
13653 return Boolean_expression::do_import(imp);
13655 return String_expression::do_import(imp);
13656 else if (c == '-' || (c >= '0' && c <= '9'))
13658 // This handles integers, floats and complex constants.
13659 return Integer_expression::do_import(imp);
13661 else if (imp->match_c_string("nil"))
13662 return Nil_expression::do_import(imp);
13663 else if (imp->match_c_string("convert"))
13664 return Type_conversion_expression::do_import(imp);
13667 error_at(imp->location(), "import error: expected expression");
13668 return Expression::make_error(imp->location());
13672 // Class Expression_list.
13674 // Traverse the list.
13677 Expression_list::traverse(Traverse* traverse)
13679 for (Expression_list::iterator p = this->begin();
13685 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
13686 return TRAVERSE_EXIT;
13689 return TRAVERSE_CONTINUE;
13695 Expression_list::copy()
13697 Expression_list* ret = new Expression_list();
13698 for (Expression_list::iterator p = this->begin();
13703 ret->push_back(NULL);
13705 ret->push_back((*p)->copy());
13710 // Return whether an expression list has an error expression.
13713 Expression_list::contains_error() const
13715 for (Expression_list::const_iterator p = this->begin();
13718 if (*p != NULL && (*p)->is_error_expression())