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"
36 #include "expressions.h"
40 Expression::Expression(Expression_classification classification
,
41 source_location location
)
42 : classification_(classification
), location_(location
)
46 Expression::~Expression()
50 // If this expression has a constant integer value, return it.
53 Expression::integer_constant_value(bool iota_is_constant
, mpz_t val
,
57 return this->do_integer_constant_value(iota_is_constant
, val
, ptype
);
60 // If this expression has a constant floating point value, return it.
63 Expression::float_constant_value(mpfr_t val
, Type
** ptype
) const
66 if (this->do_float_constant_value(val
, ptype
))
72 if (!this->do_integer_constant_value(false, ival
, &t
))
76 mpfr_set_z(val
, ival
, GMP_RNDN
);
83 // If this expression has a constant complex value, return it.
86 Expression::complex_constant_value(mpfr_t real
, mpfr_t imag
,
90 if (this->do_complex_constant_value(real
, imag
, ptype
))
93 if (this->float_constant_value(real
, &t
))
95 mpfr_set_ui(imag
, 0, GMP_RNDN
);
101 // Traverse the expressions.
104 Expression::traverse(Expression
** pexpr
, Traverse
* traverse
)
106 Expression
* expr
= *pexpr
;
107 if ((traverse
->traverse_mask() & Traverse::traverse_expressions
) != 0)
109 int t
= traverse
->expression(pexpr
);
110 if (t
== TRAVERSE_EXIT
)
111 return TRAVERSE_EXIT
;
112 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
113 return TRAVERSE_CONTINUE
;
115 return expr
->do_traverse(traverse
);
118 // Traverse subexpressions of this expression.
121 Expression::traverse_subexpressions(Traverse
* traverse
)
123 return this->do_traverse(traverse
);
126 // Default implementation for do_traverse for child classes.
129 Expression::do_traverse(Traverse
*)
131 return TRAVERSE_CONTINUE
;
134 // This virtual function is called by the parser if the value of this
135 // expression is being discarded. By default, we warn. Expressions
136 // with side effects override.
139 Expression::do_discarding_value()
141 this->warn_about_unused_value();
144 // This virtual function is called to export expressions. This will
145 // only be used by expressions which may be constant.
148 Expression::do_export(Export
*) const
153 // Warn that the value of the expression is not used.
156 Expression::warn_about_unused_value()
158 warning_at(this->location(), OPT_Wunused_value
, "value computed is not used");
161 // Note that this expression is an error. This is called by children
162 // when they discover an error.
165 Expression::set_is_error()
167 this->classification_
= EXPRESSION_ERROR
;
170 // For children to call to report an error conveniently.
173 Expression::report_error(const char* msg
)
175 error_at(this->location_
, "%s", msg
);
176 this->set_is_error();
179 // Set types of variables and constants. This is implemented by the
183 Expression::determine_type(const Type_context
* context
)
185 this->do_determine_type(context
);
188 // Set types when there is no context.
191 Expression::determine_type_no_context()
193 Type_context context
;
194 this->do_determine_type(&context
);
197 // Return a tree handling any conversions which must be done during
201 Expression::convert_for_assignment(Translate_context
* context
, Type
* lhs_type
,
202 Type
* rhs_type
, tree rhs_tree
,
203 source_location location
)
205 if (lhs_type
== rhs_type
)
208 if (lhs_type
->is_error_type() || rhs_type
->is_error_type())
209 return error_mark_node
;
211 if (lhs_type
->is_undefined() || rhs_type
->is_undefined())
213 // Make sure we report the error.
216 return error_mark_node
;
219 if (rhs_tree
== error_mark_node
|| TREE_TYPE(rhs_tree
) == error_mark_node
)
220 return error_mark_node
;
222 Gogo
* gogo
= context
->gogo();
224 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
225 if (lhs_type_tree
== error_mark_node
)
226 return error_mark_node
;
228 if (lhs_type
->interface_type() != NULL
)
230 if (rhs_type
->interface_type() == NULL
)
231 return Expression::convert_type_to_interface(context
, lhs_type
,
235 return Expression::convert_interface_to_interface(context
, lhs_type
,
239 else if (rhs_type
->interface_type() != NULL
)
240 return Expression::convert_interface_to_type(context
, lhs_type
, rhs_type
,
242 else if (lhs_type
->is_open_array_type()
243 && rhs_type
->is_nil_type())
245 // Assigning nil to an open array.
246 gcc_assert(TREE_CODE(lhs_type_tree
) == RECORD_TYPE
);
248 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
250 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
251 tree field
= TYPE_FIELDS(lhs_type_tree
);
252 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
255 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
257 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
258 field
= DECL_CHAIN(field
);
259 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
262 elt
->value
= fold_convert(TREE_TYPE(field
), integer_zero_node
);
264 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
265 field
= DECL_CHAIN(field
);
266 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
269 elt
->value
= fold_convert(TREE_TYPE(field
), integer_zero_node
);
271 tree val
= build_constructor(lhs_type_tree
, init
);
272 TREE_CONSTANT(val
) = 1;
276 else if (rhs_type
->is_nil_type())
278 // The left hand side should be a pointer type at the tree
280 gcc_assert(POINTER_TYPE_P(lhs_type_tree
));
281 return fold_convert(lhs_type_tree
, null_pointer_node
);
283 else if (lhs_type_tree
== TREE_TYPE(rhs_tree
))
285 // No conversion is needed.
288 else if (POINTER_TYPE_P(lhs_type_tree
)
289 || INTEGRAL_TYPE_P(lhs_type_tree
)
290 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
291 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
292 return fold_convert_loc(location
, lhs_type_tree
, rhs_tree
);
293 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
294 && TREE_CODE(TREE_TYPE(rhs_tree
)) == RECORD_TYPE
)
296 // This conversion must be permitted by Go, or we wouldn't have
298 gcc_assert(int_size_in_bytes(lhs_type_tree
)
299 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
300 return fold_build1_loc(location
, VIEW_CONVERT_EXPR
, lhs_type_tree
,
305 gcc_assert(useless_type_conversion_p(lhs_type_tree
, TREE_TYPE(rhs_tree
)));
310 // Return a tree for a conversion from a non-interface type to an
314 Expression::convert_type_to_interface(Translate_context
* context
,
315 Type
* lhs_type
, Type
* rhs_type
,
316 tree rhs_tree
, source_location location
)
318 Gogo
* gogo
= context
->gogo();
319 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
320 bool lhs_is_empty
= lhs_interface_type
->is_empty();
322 // Since RHS_TYPE is a static type, we can create the interface
323 // method table at compile time.
325 // When setting an interface to nil, we just set both fields to
327 if (rhs_type
->is_nil_type())
328 return lhs_type
->get_init_tree(gogo
, false);
330 // This should have been checked already.
331 gcc_assert(lhs_interface_type
->implements_interface(rhs_type
, NULL
));
333 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
334 if (lhs_type_tree
== error_mark_node
)
335 return error_mark_node
;
337 // An interface is a tuple. If LHS_TYPE is an empty interface type,
338 // then the first field is the type descriptor for RHS_TYPE.
339 // Otherwise it is the interface method table for RHS_TYPE.
340 tree first_field_value
;
342 first_field_value
= rhs_type
->type_descriptor_pointer(gogo
);
345 // Build the interface method table for this interface and this
346 // object type: a list of function pointers for each interface
348 Named_type
* rhs_named_type
= rhs_type
->named_type();
349 bool is_pointer
= false;
350 if (rhs_named_type
== NULL
)
352 rhs_named_type
= rhs_type
->deref()->named_type();
356 if (rhs_named_type
== NULL
)
357 method_table
= null_pointer_node
;
360 rhs_named_type
->interface_method_table(gogo
, lhs_interface_type
,
362 first_field_value
= fold_convert_loc(location
, const_ptr_type_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 gcc_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 gcc_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 gcc_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 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)),
428 "__type_descriptor") == 0);
432 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__methods")
434 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v
)));
436 tree v1
= build_fold_indirect_ref_loc(location
, v
);
437 gcc_assert(TREE_CODE(TREE_TYPE(v1
)) == RECORD_TYPE
);
438 tree f
= TYPE_FIELDS(TREE_TYPE(v1
));
439 gcc_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
= lhs_type
->get_tree(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
);
498 static tree assert_interface_decl
;
499 tree call
= Gogo::call_builtin(&assert_interface_decl
,
501 "__go_assert_interface",
504 TREE_TYPE(lhs_type_descriptor
),
506 TREE_TYPE(rhs_type_descriptor
),
507 rhs_type_descriptor
);
508 if (call
== error_mark_node
)
509 return error_mark_node
;
510 // This will panic if the interface conversion fails.
511 TREE_NOTHROW(assert_interface_decl
) = 0;
512 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
514 else if (lhs_is_empty
)
516 // A convertion to an empty interface always succeeds, and the
517 // first field is just the type descriptor of the object.
518 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
519 "__type_descriptor") == 0);
520 gcc_assert(TREE_TYPE(field
) == TREE_TYPE(rhs_type_descriptor
));
521 elt
->value
= rhs_type_descriptor
;
525 // A conversion to a non-empty interface may fail, but unlike a
526 // type assertion converting nil will always succeed.
527 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods")
529 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
530 static tree convert_interface_decl
;
531 tree call
= Gogo::call_builtin(&convert_interface_decl
,
533 "__go_convert_interface",
536 TREE_TYPE(lhs_type_descriptor
),
538 TREE_TYPE(rhs_type_descriptor
),
539 rhs_type_descriptor
);
540 if (call
== error_mark_node
)
541 return error_mark_node
;
542 // This will panic if the interface conversion fails.
543 TREE_NOTHROW(convert_interface_decl
) = 0;
544 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
547 // The second field is simply the object pointer.
549 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
550 field
= DECL_CHAIN(field
);
551 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
554 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
555 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
556 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
557 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
558 elt
->value
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
561 return build_constructor(lhs_type_tree
, init
);
564 // Return a tree for the conversion of an interface type to a
565 // non-interface type.
568 Expression::convert_interface_to_type(Translate_context
* context
,
569 Type
*lhs_type
, Type
* rhs_type
,
570 tree rhs_tree
, source_location location
)
572 Gogo
* gogo
= context
->gogo();
573 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
575 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
576 if (lhs_type_tree
== error_mark_node
)
577 return error_mark_node
;
579 // Call a function to check that the type is valid. The function
580 // will panic with an appropriate runtime type error if the type is
583 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
585 if (!DECL_P(rhs_tree
))
586 rhs_tree
= save_expr(rhs_tree
);
588 tree rhs_type_descriptor
=
589 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
592 tree rhs_inter_descriptor
= rhs_type
->type_descriptor_pointer(gogo
);
594 static tree check_interface_type_decl
;
595 tree call
= Gogo::call_builtin(&check_interface_type_decl
,
597 "__go_check_interface_type",
600 TREE_TYPE(lhs_type_descriptor
),
602 TREE_TYPE(rhs_type_descriptor
),
604 TREE_TYPE(rhs_inter_descriptor
),
605 rhs_inter_descriptor
);
606 if (call
== error_mark_node
)
607 return error_mark_node
;
608 // This call will panic if the conversion is invalid.
609 TREE_NOTHROW(check_interface_type_decl
) = 0;
611 // If the call succeeds, pull out the value.
612 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
613 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
614 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
615 tree val
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
618 // If the value is a pointer, then it is the value we want.
619 // Otherwise it points to the value.
620 if (lhs_type
->points_to() == NULL
)
622 val
= fold_convert_loc(location
, build_pointer_type(lhs_type_tree
), val
);
623 val
= build_fold_indirect_ref_loc(location
, val
);
626 return build2(COMPOUND_EXPR
, lhs_type_tree
, call
,
627 fold_convert_loc(location
, lhs_type_tree
, val
));
630 // Convert an expression to a tree. This is implemented by the child
631 // class. Not that it is not in general safe to call this multiple
632 // times for a single expression, but that we don't catch such errors.
635 Expression::get_tree(Translate_context
* context
)
637 // The child may have marked this expression as having an error.
638 if (this->classification_
== EXPRESSION_ERROR
)
639 return error_mark_node
;
641 return this->do_get_tree(context
);
644 // Return a tree for VAL in TYPE.
647 Expression::integer_constant_tree(mpz_t val
, tree type
)
649 if (type
== error_mark_node
)
650 return error_mark_node
;
651 else if (TREE_CODE(type
) == INTEGER_TYPE
)
652 return double_int_to_tree(type
,
653 mpz_get_double_int(type
, val
, true));
654 else if (TREE_CODE(type
) == REAL_TYPE
)
657 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
658 tree ret
= Expression::float_constant_tree(fval
, type
);
662 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
665 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
666 tree real
= Expression::float_constant_tree(fval
, TREE_TYPE(type
));
668 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
670 return build_complex(type
, real
, imag
);
676 // Return a tree for VAL in TYPE.
679 Expression::float_constant_tree(mpfr_t val
, tree type
)
681 if (type
== error_mark_node
)
682 return error_mark_node
;
683 else if (TREE_CODE(type
) == INTEGER_TYPE
)
687 mpfr_get_z(ival
, val
, GMP_RNDN
);
688 tree ret
= Expression::integer_constant_tree(ival
, type
);
692 else if (TREE_CODE(type
) == REAL_TYPE
)
695 real_from_mpfr(&r1
, val
, type
, GMP_RNDN
);
697 real_convert(&r2
, TYPE_MODE(type
), &r1
);
698 return build_real(type
, r2
);
700 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
703 real_from_mpfr(&r1
, val
, TREE_TYPE(type
), GMP_RNDN
);
705 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
706 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
708 return build_complex(type
, build_real(TREE_TYPE(type
), r2
), imag
);
714 // Return a tree for REAL/IMAG in TYPE.
717 Expression::complex_constant_tree(mpfr_t real
, mpfr_t imag
, tree type
)
719 if (TREE_CODE(type
) == COMPLEX_TYPE
)
722 real_from_mpfr(&r1
, real
, TREE_TYPE(type
), GMP_RNDN
);
724 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
727 real_from_mpfr(&r3
, imag
, TREE_TYPE(type
), GMP_RNDN
);
729 real_convert(&r4
, TYPE_MODE(TREE_TYPE(type
)), &r3
);
731 return build_complex(type
, build_real(TREE_TYPE(type
), r2
),
732 build_real(TREE_TYPE(type
), r4
));
738 // Return a tree which evaluates to true if VAL, of arbitrary integer
739 // type, is negative or is more than the maximum value of BOUND_TYPE.
740 // If SOFAR is not NULL, it is or'red into the result. The return
741 // value may be NULL if SOFAR is NULL.
744 Expression::check_bounds(tree val
, tree bound_type
, tree sofar
,
747 tree val_type
= TREE_TYPE(val
);
748 tree ret
= NULL_TREE
;
750 if (!TYPE_UNSIGNED(val_type
))
752 ret
= fold_build2_loc(loc
, LT_EXPR
, boolean_type_node
, val
,
753 build_int_cst(val_type
, 0));
754 if (ret
== boolean_false_node
)
758 if ((TYPE_UNSIGNED(val_type
) && !TYPE_UNSIGNED(bound_type
))
759 || TYPE_SIZE(val_type
) > TYPE_SIZE(bound_type
))
761 tree max
= TYPE_MAX_VALUE(bound_type
);
762 tree big
= fold_build2_loc(loc
, GT_EXPR
, boolean_type_node
, val
,
763 fold_convert_loc(loc
, val_type
, max
));
764 if (big
== boolean_false_node
)
766 else if (ret
== NULL_TREE
)
769 ret
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
773 if (ret
== NULL_TREE
)
775 else if (sofar
== NULL_TREE
)
778 return fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
782 // Error expressions. This are used to avoid cascading errors.
784 class Error_expression
: public Expression
787 Error_expression(source_location location
)
788 : Expression(EXPRESSION_ERROR
, location
)
793 do_is_constant() const
797 do_integer_constant_value(bool, mpz_t val
, Type
**) const
804 do_float_constant_value(mpfr_t val
, Type
**) const
806 mpfr_set_ui(val
, 0, GMP_RNDN
);
811 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const
813 mpfr_set_ui(real
, 0, GMP_RNDN
);
814 mpfr_set_ui(imag
, 0, GMP_RNDN
);
819 do_discarding_value()
824 { return Type::make_error_type(); }
827 do_determine_type(const Type_context
*)
835 do_is_addressable() const
839 do_get_tree(Translate_context
*)
840 { return error_mark_node
; }
844 Expression::make_error(source_location location
)
846 return new Error_expression(location
);
849 // An expression which is really a type. This is used during parsing.
850 // It is an error if these survive after lowering.
853 Type_expression
: public Expression
856 Type_expression(Type
* type
, source_location location
)
857 : Expression(EXPRESSION_TYPE
, location
),
863 do_traverse(Traverse
* traverse
)
864 { return Type::traverse(this->type_
, traverse
); }
868 { return this->type_
; }
871 do_determine_type(const Type_context
*)
875 do_check_types(Gogo
*)
876 { this->report_error(_("invalid use of type")); }
883 do_get_tree(Translate_context
*)
884 { gcc_unreachable(); }
887 // The type which we are representing as an expression.
892 Expression::make_type(Type
* type
, source_location location
)
894 return new Type_expression(type
, location
);
897 // Class Var_expression.
899 // Lower a variable expression. Here we just make sure that the
900 // initialization expression of the variable has been lowered. This
901 // ensures that we will be able to determine the type of the variable
905 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
907 if (this->variable_
->is_variable())
909 Variable
* var
= this->variable_
->var_value();
910 // This is either a local variable or a global variable. A
911 // reference to a variable which is local to an enclosing
912 // function will be a reference to a field in a closure.
913 if (var
->is_global())
915 var
->lower_init_expression(gogo
, function
);
920 // Return the name of the variable.
923 Var_expression::name() const
925 return this->variable_
->name();
928 // Return the type of a reference to a variable.
931 Var_expression::do_type()
933 if (this->variable_
->is_variable())
934 return this->variable_
->var_value()->type();
935 else if (this->variable_
->is_result_variable())
936 return this->variable_
->result_var_value()->type();
941 // Something takes the address of this variable. This means that we
942 // may want to move the variable onto the heap.
945 Var_expression::do_address_taken(bool escapes
)
949 else if (this->variable_
->is_variable())
950 this->variable_
->var_value()->set_address_taken();
951 else if (this->variable_
->is_result_variable())
952 this->variable_
->result_var_value()->set_address_taken();
957 // Get the tree for a reference to a variable.
960 Var_expression::do_get_tree(Translate_context
* context
)
962 return this->variable_
->get_tree(context
->gogo(), context
->function());
965 // Make a reference to a variable in an expression.
968 Expression::make_var_reference(Named_object
* var
, source_location location
)
971 return Expression::make_sink(location
);
973 // FIXME: Creating a new object for each reference to a variable is
975 return new Var_expression(var
, location
);
978 // Class Temporary_reference_expression.
983 Temporary_reference_expression::do_type()
985 return this->statement_
->type();
988 // Called if something takes the address of this temporary variable.
989 // We never have to move temporary variables to the heap, but we do
990 // need to know that they must live in the stack rather than in a
994 Temporary_reference_expression::do_address_taken(bool)
996 this->statement_
->set_is_address_taken();
999 // Get a tree referring to the variable.
1002 Temporary_reference_expression::do_get_tree(Translate_context
*)
1004 return this->statement_
->get_decl();
1007 // Make a reference to a temporary variable.
1010 Expression::make_temporary_reference(Temporary_statement
* statement
,
1011 source_location location
)
1013 return new Temporary_reference_expression(statement
, location
);
1016 // A sink expression--a use of the blank identifier _.
1018 class Sink_expression
: public Expression
1021 Sink_expression(source_location location
)
1022 : Expression(EXPRESSION_SINK
, location
),
1023 type_(NULL
), var_(NULL_TREE
)
1028 do_discarding_value()
1035 do_determine_type(const Type_context
*);
1039 { return new Sink_expression(this->location()); }
1042 do_get_tree(Translate_context
*);
1045 // The type of this sink variable.
1047 // The temporary variable we generate.
1051 // Return the type of a sink expression.
1054 Sink_expression::do_type()
1056 if (this->type_
== NULL
)
1057 return Type::make_sink_type();
1061 // Determine the type of a sink expression.
1064 Sink_expression::do_determine_type(const Type_context
* context
)
1066 if (context
->type
!= NULL
)
1067 this->type_
= context
->type
;
1070 // Return a temporary variable for a sink expression. This will
1071 // presumably be a write-only variable which the middle-end will drop.
1074 Sink_expression::do_get_tree(Translate_context
* context
)
1076 if (this->var_
== NULL_TREE
)
1078 gcc_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
1079 this->var_
= create_tmp_var(this->type_
->get_tree(context
->gogo()),
1085 // Make a sink expression.
1088 Expression::make_sink(source_location location
)
1090 return new Sink_expression(location
);
1093 // Class Func_expression.
1095 // FIXME: Can a function expression appear in a constant expression?
1096 // The value is unchanging. Initializing a constant to the address of
1097 // a function seems like it could work, though there might be little
1100 // Return the name of the function.
1103 Func_expression::name() const
1105 return this->function_
->name();
1111 Func_expression::do_traverse(Traverse
* traverse
)
1113 return (this->closure_
== NULL
1115 : Expression::traverse(&this->closure_
, traverse
));
1118 // Return the type of a function expression.
1121 Func_expression::do_type()
1123 if (this->function_
->is_function())
1124 return this->function_
->func_value()->type();
1125 else if (this->function_
->is_function_declaration())
1126 return this->function_
->func_declaration_value()->type();
1131 // Get the tree for a function expression without evaluating the
1135 Func_expression::get_tree_without_closure(Gogo
* gogo
)
1137 Function_type
* fntype
;
1138 if (this->function_
->is_function())
1139 fntype
= this->function_
->func_value()->type();
1140 else if (this->function_
->is_function_declaration())
1141 fntype
= this->function_
->func_declaration_value()->type();
1145 // Builtin functions are handled specially by Call_expression. We
1146 // can't take their address.
1147 if (fntype
->is_builtin())
1149 error_at(this->location(), "invalid use of special builtin function %qs",
1150 this->function_
->name().c_str());
1151 return error_mark_node
;
1154 Named_object
* no
= this->function_
;
1156 tree id
= no
->get_id(gogo
);
1157 if (id
== error_mark_node
)
1158 return error_mark_node
;
1161 if (no
->is_function())
1162 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
, id
);
1163 else if (no
->is_function_declaration())
1164 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
, id
);
1168 if (fndecl
== error_mark_node
)
1169 return error_mark_node
;
1171 return build_fold_addr_expr_loc(this->location(), fndecl
);
1174 // Get the tree for a function expression. This is used when we take
1175 // the address of a function rather than simply calling it. If the
1176 // function has a closure, we must use a trampoline.
1179 Func_expression::do_get_tree(Translate_context
* context
)
1181 Gogo
* gogo
= context
->gogo();
1183 tree fnaddr
= this->get_tree_without_closure(gogo
);
1184 if (fnaddr
== error_mark_node
)
1185 return error_mark_node
;
1187 gcc_assert(TREE_CODE(fnaddr
) == ADDR_EXPR
1188 && TREE_CODE(TREE_OPERAND(fnaddr
, 0)) == FUNCTION_DECL
);
1189 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr
, 0)) = 1;
1191 // For a normal non-nested function call, that is all we have to do.
1192 if (!this->function_
->is_function()
1193 || this->function_
->func_value()->enclosing() == NULL
)
1195 gcc_assert(this->closure_
== NULL
);
1199 // For a nested function call, we have to always allocate a
1200 // trampoline. If we don't always allocate, then closures will not
1201 // be reliably distinct.
1202 Expression
* closure
= this->closure_
;
1204 if (closure
== NULL
)
1205 closure_tree
= null_pointer_node
;
1208 // Get the value of the closure. This will be a pointer to
1209 // space allocated on the heap.
1210 closure_tree
= closure
->get_tree(context
);
1211 if (closure_tree
== error_mark_node
)
1212 return error_mark_node
;
1213 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree
)));
1216 // Now we need to build some code on the heap. This code will load
1217 // the static chain pointer with the closure and then jump to the
1218 // body of the function. The normal gcc approach is to build the
1219 // code on the stack. Unfortunately we can not do that, as Go
1220 // permits us to return the function pointer.
1222 return gogo
->make_trampoline(fnaddr
, closure_tree
, this->location());
1225 // Make a reference to a function in an expression.
1228 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1229 source_location location
)
1231 return new Func_expression(function
, closure
, location
);
1234 // Class Unknown_expression.
1236 // Return the name of an unknown expression.
1239 Unknown_expression::name() const
1241 return this->named_object_
->name();
1244 // Lower a reference to an unknown name.
1247 Unknown_expression::do_lower(Gogo
*, Named_object
*, int)
1249 source_location location
= this->location();
1250 Named_object
* no
= this->named_object_
;
1251 Named_object
* real
= no
->unknown_value()->real_named_object();
1254 if (this->is_composite_literal_key_
)
1256 error_at(location
, "reference to undefined name %qs",
1257 this->named_object_
->message_name().c_str());
1258 return Expression::make_error(location
);
1260 switch (real
->classification())
1262 case Named_object::NAMED_OBJECT_CONST
:
1263 return Expression::make_const_reference(real
, location
);
1264 case Named_object::NAMED_OBJECT_TYPE
:
1265 return Expression::make_type(real
->type_value(), location
);
1266 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1267 if (this->is_composite_literal_key_
)
1269 error_at(location
, "reference to undefined type %qs",
1270 real
->message_name().c_str());
1271 return Expression::make_error(location
);
1272 case Named_object::NAMED_OBJECT_VAR
:
1273 return Expression::make_var_reference(real
, location
);
1274 case Named_object::NAMED_OBJECT_FUNC
:
1275 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1276 return Expression::make_func_reference(real
, NULL
, location
);
1277 case Named_object::NAMED_OBJECT_PACKAGE
:
1278 if (this->is_composite_literal_key_
)
1280 error_at(location
, "unexpected reference to package");
1281 return Expression::make_error(location
);
1287 // Make a reference to an unknown name.
1290 Expression::make_unknown_reference(Named_object
* no
, source_location location
)
1292 gcc_assert(no
->resolve()->is_unknown());
1293 return new Unknown_expression(no
, location
);
1296 // A boolean expression.
1298 class Boolean_expression
: public Expression
1301 Boolean_expression(bool val
, source_location location
)
1302 : Expression(EXPRESSION_BOOLEAN
, location
),
1303 val_(val
), type_(NULL
)
1311 do_is_constant() const
1318 do_determine_type(const Type_context
*);
1325 do_get_tree(Translate_context
*)
1326 { return this->val_
? boolean_true_node
: boolean_false_node
; }
1329 do_export(Export
* exp
) const
1330 { exp
->write_c_string(this->val_
? "true" : "false"); }
1335 // The type as determined by context.
1342 Boolean_expression::do_type()
1344 if (this->type_
== NULL
)
1345 this->type_
= Type::make_boolean_type();
1349 // Set the type from the context.
1352 Boolean_expression::do_determine_type(const Type_context
* context
)
1354 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1356 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1357 this->type_
= context
->type
;
1358 else if (!context
->may_be_abstract
)
1359 this->type_
= Type::lookup_bool_type();
1362 // Import a boolean constant.
1365 Boolean_expression::do_import(Import
* imp
)
1367 if (imp
->peek_char() == 't')
1369 imp
->require_c_string("true");
1370 return Expression::make_boolean(true, imp
->location());
1374 imp
->require_c_string("false");
1375 return Expression::make_boolean(false, imp
->location());
1379 // Make a boolean expression.
1382 Expression::make_boolean(bool val
, source_location location
)
1384 return new Boolean_expression(val
, location
);
1387 // Class String_expression.
1392 String_expression::do_type()
1394 if (this->type_
== NULL
)
1395 this->type_
= Type::make_string_type();
1399 // Set the type from the context.
1402 String_expression::do_determine_type(const Type_context
* context
)
1404 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1406 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1407 this->type_
= context
->type
;
1408 else if (!context
->may_be_abstract
)
1409 this->type_
= Type::lookup_string_type();
1412 // Build a string constant.
1415 String_expression::do_get_tree(Translate_context
* context
)
1417 return context
->gogo()->go_string_constant_tree(this->val_
);
1420 // Export a string expression.
1423 String_expression::do_export(Export
* exp
) const
1426 s
.reserve(this->val_
.length() * 4 + 2);
1428 for (std::string::const_iterator p
= this->val_
.begin();
1429 p
!= this->val_
.end();
1432 if (*p
== '\\' || *p
== '"')
1437 else if (*p
>= 0x20 && *p
< 0x7f)
1439 else if (*p
== '\n')
1441 else if (*p
== '\t')
1446 unsigned char c
= *p
;
1447 unsigned int dig
= c
>> 4;
1448 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1450 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1454 exp
->write_string(s
);
1457 // Import a string expression.
1460 String_expression::do_import(Import
* imp
)
1462 imp
->require_c_string("\"");
1466 int c
= imp
->get_char();
1467 if (c
== '"' || c
== -1)
1470 val
+= static_cast<char>(c
);
1473 c
= imp
->get_char();
1474 if (c
== '\\' || c
== '"')
1475 val
+= static_cast<char>(c
);
1482 c
= imp
->get_char();
1483 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1484 c
= imp
->get_char();
1485 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1486 char v
= (vh
<< 4) | vl
;
1491 error_at(imp
->location(), "bad string constant");
1492 return Expression::make_error(imp
->location());
1496 return Expression::make_string(val
, imp
->location());
1499 // Make a string expression.
1502 Expression::make_string(const std::string
& val
, source_location location
)
1504 return new String_expression(val
, location
);
1507 // Make an integer expression.
1509 class Integer_expression
: public Expression
1512 Integer_expression(const mpz_t
* val
, Type
* type
, source_location location
)
1513 : Expression(EXPRESSION_INTEGER
, location
),
1515 { mpz_init_set(this->val_
, *val
); }
1520 // Return whether VAL fits in the type.
1522 check_constant(mpz_t val
, Type
*, source_location
);
1524 // Write VAL to export data.
1526 export_integer(Export
* exp
, const mpz_t val
);
1530 do_is_constant() const
1534 do_integer_constant_value(bool, mpz_t val
, Type
** ptype
) const;
1540 do_determine_type(const Type_context
* context
);
1543 do_check_types(Gogo
*);
1546 do_get_tree(Translate_context
*);
1550 { return Expression::make_integer(&this->val_
, this->type_
,
1551 this->location()); }
1554 do_export(Export
*) const;
1557 // The integer value.
1563 // Return an integer constant value.
1566 Integer_expression::do_integer_constant_value(bool, mpz_t val
,
1569 if (this->type_
!= NULL
)
1570 *ptype
= this->type_
;
1571 mpz_set(val
, this->val_
);
1575 // Return the current type. If we haven't set the type yet, we return
1576 // an abstract integer type.
1579 Integer_expression::do_type()
1581 if (this->type_
== NULL
)
1582 this->type_
= Type::make_abstract_integer_type();
1586 // Set the type of the integer value. Here we may switch from an
1587 // abstract type to a real type.
1590 Integer_expression::do_determine_type(const Type_context
* context
)
1592 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1594 else if (context
->type
!= NULL
1595 && (context
->type
->integer_type() != NULL
1596 || context
->type
->float_type() != NULL
1597 || context
->type
->complex_type() != NULL
))
1598 this->type_
= context
->type
;
1599 else if (!context
->may_be_abstract
)
1600 this->type_
= Type::lookup_integer_type("int");
1603 // Return true if the integer VAL fits in the range of the type TYPE.
1604 // Otherwise give an error and return false. TYPE may be NULL.
1607 Integer_expression::check_constant(mpz_t val
, Type
* type
,
1608 source_location location
)
1612 Integer_type
* itype
= type
->integer_type();
1613 if (itype
== NULL
|| itype
->is_abstract())
1616 int bits
= mpz_sizeinbase(val
, 2);
1618 if (itype
->is_unsigned())
1620 // For an unsigned type we can only accept a nonnegative number,
1621 // and we must be able to represent at least BITS.
1622 if (mpz_sgn(val
) >= 0
1623 && bits
<= itype
->bits())
1628 // For a signed type we need an extra bit to indicate the sign.
1629 // We have to handle the most negative integer specially.
1630 if (bits
+ 1 <= itype
->bits()
1631 || (bits
<= itype
->bits()
1633 && (mpz_scan1(val
, 0)
1634 == static_cast<unsigned long>(itype
->bits() - 1))
1635 && mpz_scan0(val
, itype
->bits()) == ULONG_MAX
))
1639 error_at(location
, "integer constant overflow");
1643 // Check the type of an integer constant.
1646 Integer_expression::do_check_types(Gogo
*)
1648 if (this->type_
== NULL
)
1650 if (!Integer_expression::check_constant(this->val_
, this->type_
,
1652 this->set_is_error();
1655 // Get a tree for an integer constant.
1658 Integer_expression::do_get_tree(Translate_context
* context
)
1660 Gogo
* gogo
= context
->gogo();
1662 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1663 type
= this->type_
->get_tree(gogo
);
1664 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
1666 // We are converting to an abstract floating point type.
1667 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
1669 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
1671 // We are converting to an abstract complex type.
1672 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
1676 // If we still have an abstract type here, then this is being
1677 // used in a constant expression which didn't get reduced for
1678 // some reason. Use a type which will fit the value. We use <,
1679 // not <=, because we need an extra bit for the sign bit.
1680 int bits
= mpz_sizeinbase(this->val_
, 2);
1681 if (bits
< INT_TYPE_SIZE
)
1682 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
1684 type
= Type::lookup_integer_type("int64")->get_tree(gogo
);
1686 type
= long_long_integer_type_node
;
1688 return Expression::integer_constant_tree(this->val_
, type
);
1691 // Write VAL to export data.
1694 Integer_expression::export_integer(Export
* exp
, const mpz_t val
)
1696 char* s
= mpz_get_str(NULL
, 10, val
);
1697 exp
->write_c_string(s
);
1701 // Export an integer in a constant expression.
1704 Integer_expression::do_export(Export
* exp
) const
1706 Integer_expression::export_integer(exp
, this->val_
);
1707 // A trailing space lets us reliably identify the end of the number.
1708 exp
->write_c_string(" ");
1711 // Import an integer, floating point, or complex value. This handles
1712 // all these types because they all start with digits.
1715 Integer_expression::do_import(Import
* imp
)
1717 std::string num
= imp
->read_identifier();
1718 imp
->require_c_string(" ");
1719 if (!num
.empty() && num
[num
.length() - 1] == 'i')
1722 size_t plus_pos
= num
.find('+', 1);
1723 size_t minus_pos
= num
.find('-', 1);
1725 if (plus_pos
== std::string::npos
)
1727 else if (minus_pos
== std::string::npos
)
1731 error_at(imp
->location(), "bad number in import data: %qs",
1733 return Expression::make_error(imp
->location());
1735 if (pos
== std::string::npos
)
1736 mpfr_set_ui(real
, 0, GMP_RNDN
);
1739 std::string real_str
= num
.substr(0, pos
);
1740 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
1742 error_at(imp
->location(), "bad number in import data: %qs",
1744 return Expression::make_error(imp
->location());
1748 std::string imag_str
;
1749 if (pos
== std::string::npos
)
1752 imag_str
= num
.substr(pos
);
1753 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
1755 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
1757 error_at(imp
->location(), "bad number in import data: %qs",
1759 return Expression::make_error(imp
->location());
1761 Expression
* ret
= Expression::make_complex(&real
, &imag
, NULL
,
1767 else if (num
.find('.') == std::string::npos
1768 && num
.find('E') == std::string::npos
)
1771 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
1773 error_at(imp
->location(), "bad number in import data: %qs",
1775 return Expression::make_error(imp
->location());
1777 Expression
* ret
= Expression::make_integer(&val
, NULL
, imp
->location());
1784 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
1786 error_at(imp
->location(), "bad number in import data: %qs",
1788 return Expression::make_error(imp
->location());
1790 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
1796 // Build a new integer value.
1799 Expression::make_integer(const mpz_t
* val
, Type
* type
,
1800 source_location location
)
1802 return new Integer_expression(val
, type
, location
);
1807 class Float_expression
: public Expression
1810 Float_expression(const mpfr_t
* val
, Type
* type
, source_location location
)
1811 : Expression(EXPRESSION_FLOAT
, location
),
1814 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
1817 // Constrain VAL to fit into TYPE.
1819 constrain_float(mpfr_t val
, Type
* type
);
1821 // Return whether VAL fits in the type.
1823 check_constant(mpfr_t val
, Type
*, source_location
);
1825 // Write VAL to export data.
1827 export_float(Export
* exp
, const mpfr_t val
);
1831 do_is_constant() const
1835 do_float_constant_value(mpfr_t val
, Type
**) const;
1841 do_determine_type(const Type_context
*);
1844 do_check_types(Gogo
*);
1848 { return Expression::make_float(&this->val_
, this->type_
,
1849 this->location()); }
1852 do_get_tree(Translate_context
*);
1855 do_export(Export
*) const;
1858 // The floating point value.
1864 // Constrain VAL to fit into TYPE.
1867 Float_expression::constrain_float(mpfr_t val
, Type
* type
)
1869 Float_type
* ftype
= type
->float_type();
1870 if (ftype
!= NULL
&& !ftype
->is_abstract())
1872 tree type_tree
= ftype
->type_tree();
1873 REAL_VALUE_TYPE rvt
;
1874 real_from_mpfr(&rvt
, val
, type_tree
, GMP_RNDN
);
1875 real_convert(&rvt
, TYPE_MODE(type_tree
), &rvt
);
1876 mpfr_from_real(val
, &rvt
, GMP_RNDN
);
1880 // Return a floating point constant value.
1883 Float_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
1885 if (this->type_
!= NULL
)
1886 *ptype
= this->type_
;
1887 mpfr_set(val
, this->val_
, GMP_RNDN
);
1891 // Return the current type. If we haven't set the type yet, we return
1892 // an abstract float type.
1895 Float_expression::do_type()
1897 if (this->type_
== NULL
)
1898 this->type_
= Type::make_abstract_float_type();
1902 // Set the type of the float value. Here we may switch from an
1903 // abstract type to a real type.
1906 Float_expression::do_determine_type(const Type_context
* context
)
1908 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1910 else if (context
->type
!= NULL
1911 && (context
->type
->integer_type() != NULL
1912 || context
->type
->float_type() != NULL
1913 || context
->type
->complex_type() != NULL
))
1914 this->type_
= context
->type
;
1915 else if (!context
->may_be_abstract
)
1916 this->type_
= Type::lookup_float_type("float");
1919 // Return true if the floating point value VAL fits in the range of
1920 // the type TYPE. Otherwise give an error and return false. TYPE may
1924 Float_expression::check_constant(mpfr_t val
, Type
* type
,
1925 source_location location
)
1929 Float_type
* ftype
= type
->float_type();
1930 if (ftype
== NULL
|| ftype
->is_abstract())
1933 // A NaN or Infinity always fits in the range of the type.
1934 if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
1937 mp_exp_t exp
= mpfr_get_exp(val
);
1939 switch (ftype
->bits())
1952 error_at(location
, "floating point constant overflow");
1958 // Check the type of a float value.
1961 Float_expression::do_check_types(Gogo
*)
1963 if (this->type_
== NULL
)
1966 if (!Float_expression::check_constant(this->val_
, this->type_
,
1968 this->set_is_error();
1970 Integer_type
* integer_type
= this->type_
->integer_type();
1971 if (integer_type
!= NULL
)
1973 if (!mpfr_integer_p(this->val_
))
1974 this->report_error(_("floating point constant truncated to integer"));
1977 gcc_assert(!integer_type
->is_abstract());
1980 mpfr_get_z(ival
, this->val_
, GMP_RNDN
);
1981 Integer_expression::check_constant(ival
, integer_type
,
1988 // Get a tree for a float constant.
1991 Float_expression::do_get_tree(Translate_context
* context
)
1993 Gogo
* gogo
= context
->gogo();
1995 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1996 type
= this->type_
->get_tree(gogo
);
1997 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
1999 // We have an abstract integer type. We just hope for the best.
2000 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
2004 // If we still have an abstract type here, then this is being
2005 // used in a constant expression which didn't get reduced. We
2006 // just use float64 and hope for the best.
2007 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
2009 return Expression::float_constant_tree(this->val_
, type
);
2012 // Write a floating point number to export data.
2015 Float_expression::export_float(Export
*exp
, const mpfr_t val
)
2018 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2020 exp
->write_c_string("-");
2021 exp
->write_c_string("0.");
2022 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2025 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2026 exp
->write_c_string(buf
);
2029 // Export a floating point number in a constant expression.
2032 Float_expression::do_export(Export
* exp
) const
2034 Float_expression::export_float(exp
, this->val_
);
2035 // A trailing space lets us reliably identify the end of the number.
2036 exp
->write_c_string(" ");
2039 // Make a float expression.
2042 Expression::make_float(const mpfr_t
* val
, Type
* type
, source_location location
)
2044 return new Float_expression(val
, type
, location
);
2049 class Complex_expression
: public Expression
2052 Complex_expression(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2053 source_location location
)
2054 : Expression(EXPRESSION_COMPLEX
, location
),
2057 mpfr_init_set(this->real_
, *real
, GMP_RNDN
);
2058 mpfr_init_set(this->imag_
, *imag
, GMP_RNDN
);
2061 // Constrain REAL/IMAG to fit into TYPE.
2063 constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
);
2065 // Return whether REAL/IMAG fits in the type.
2067 check_constant(mpfr_t real
, mpfr_t imag
, Type
*, source_location
);
2069 // Write REAL/IMAG to export data.
2071 export_complex(Export
* exp
, const mpfr_t real
, const mpfr_t val
);
2075 do_is_constant() const
2079 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2085 do_determine_type(const Type_context
*);
2088 do_check_types(Gogo
*);
2093 return Expression::make_complex(&this->real_
, &this->imag_
, this->type_
,
2098 do_get_tree(Translate_context
*);
2101 do_export(Export
*) const;
2106 // The imaginary part;
2108 // The type if known.
2112 // Constrain REAL/IMAG to fit into TYPE.
2115 Complex_expression::constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
)
2117 Complex_type
* ctype
= type
->complex_type();
2118 if (ctype
!= NULL
&& !ctype
->is_abstract())
2120 tree type_tree
= ctype
->type_tree();
2122 REAL_VALUE_TYPE rvt
;
2123 real_from_mpfr(&rvt
, real
, TREE_TYPE(type_tree
), GMP_RNDN
);
2124 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2125 mpfr_from_real(real
, &rvt
, GMP_RNDN
);
2127 real_from_mpfr(&rvt
, imag
, TREE_TYPE(type_tree
), GMP_RNDN
);
2128 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2129 mpfr_from_real(imag
, &rvt
, GMP_RNDN
);
2133 // Return a complex constant value.
2136 Complex_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2139 if (this->type_
!= NULL
)
2140 *ptype
= this->type_
;
2141 mpfr_set(real
, this->real_
, GMP_RNDN
);
2142 mpfr_set(imag
, this->imag_
, GMP_RNDN
);
2146 // Return the current type. If we haven't set the type yet, we return
2147 // an abstract complex type.
2150 Complex_expression::do_type()
2152 if (this->type_
== NULL
)
2153 this->type_
= Type::make_abstract_complex_type();
2157 // Set the type of the complex value. Here we may switch from an
2158 // abstract type to a real type.
2161 Complex_expression::do_determine_type(const Type_context
* context
)
2163 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2165 else if (context
->type
!= NULL
2166 && context
->type
->complex_type() != NULL
)
2167 this->type_
= context
->type
;
2168 else if (!context
->may_be_abstract
)
2169 this->type_
= Type::lookup_complex_type("complex");
2172 // Return true if the complex value REAL/IMAG fits in the range of the
2173 // type TYPE. Otherwise give an error and return false. TYPE may be
2177 Complex_expression::check_constant(mpfr_t real
, mpfr_t imag
, Type
* type
,
2178 source_location location
)
2182 Complex_type
* ctype
= type
->complex_type();
2183 if (ctype
== NULL
|| ctype
->is_abstract())
2187 switch (ctype
->bits())
2199 // A NaN or Infinity always fits in the range of the type.
2200 if (!mpfr_nan_p(real
) && !mpfr_inf_p(real
) && !mpfr_zero_p(real
))
2202 if (mpfr_get_exp(real
) > max_exp
)
2204 error_at(location
, "complex real part constant overflow");
2209 if (!mpfr_nan_p(imag
) && !mpfr_inf_p(imag
) && !mpfr_zero_p(imag
))
2211 if (mpfr_get_exp(imag
) > max_exp
)
2213 error_at(location
, "complex imaginary part constant overflow");
2221 // Check the type of a complex value.
2224 Complex_expression::do_check_types(Gogo
*)
2226 if (this->type_
== NULL
)
2229 if (!Complex_expression::check_constant(this->real_
, this->imag_
,
2230 this->type_
, this->location()))
2231 this->set_is_error();
2234 // Get a tree for a complex constant.
2237 Complex_expression::do_get_tree(Translate_context
* context
)
2239 Gogo
* gogo
= context
->gogo();
2241 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2242 type
= this->type_
->get_tree(gogo
);
2245 // If we still have an abstract type here, this this is being
2246 // used in a constant expression which didn't get reduced. We
2247 // just use complex128 and hope for the best.
2248 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
2250 return Expression::complex_constant_tree(this->real_
, this->imag_
, type
);
2253 // Write REAL/IMAG to export data.
2256 Complex_expression::export_complex(Export
* exp
, const mpfr_t real
,
2259 if (!mpfr_zero_p(real
))
2261 Float_expression::export_float(exp
, real
);
2262 if (mpfr_sgn(imag
) > 0)
2263 exp
->write_c_string("+");
2265 Float_expression::export_float(exp
, imag
);
2266 exp
->write_c_string("i");
2269 // Export a complex number in a constant expression.
2272 Complex_expression::do_export(Export
* exp
) const
2274 Complex_expression::export_complex(exp
, this->real_
, this->imag_
);
2275 // A trailing space lets us reliably identify the end of the number.
2276 exp
->write_c_string(" ");
2279 // Make a complex expression.
2282 Expression::make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2283 source_location location
)
2285 return new Complex_expression(real
, imag
, type
, location
);
2288 // A reference to a const in an expression.
2290 class Const_expression
: public Expression
2293 Const_expression(Named_object
* constant
, source_location location
)
2294 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2295 constant_(constant
), type_(NULL
), seen_(false)
2300 { return this->constant_
->name(); }
2304 do_lower(Gogo
*, Named_object
*, int);
2307 do_is_constant() const
2311 do_integer_constant_value(bool, mpz_t val
, Type
**) const;
2314 do_float_constant_value(mpfr_t val
, Type
**) const;
2317 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2320 do_string_constant_value(std::string
* val
) const
2321 { return this->constant_
->const_value()->expr()->string_constant_value(val
); }
2326 // The type of a const is set by the declaration, not the use.
2328 do_determine_type(const Type_context
*);
2331 do_check_types(Gogo
*);
2338 do_get_tree(Translate_context
* context
);
2340 // When exporting a reference to a const as part of a const
2341 // expression, we export the value. We ignore the fact that it has
2344 do_export(Export
* exp
) const
2345 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2349 Named_object
* constant_
;
2350 // The type of this reference. This is used if the constant has an
2353 // Used to prevent infinite recursion when a constant incorrectly
2354 // refers to itself.
2358 // Lower a constant expression. This is where we convert the
2359 // predeclared constant iota into an integer value.
2362 Const_expression::do_lower(Gogo
* gogo
, Named_object
*, int iota_value
)
2364 if (this->constant_
->const_value()->expr()->classification()
2367 if (iota_value
== -1)
2369 error_at(this->location(),
2370 "iota is only defined in const declarations");
2374 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2375 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2381 // Make sure that the constant itself has been lowered.
2382 gogo
->lower_constant(this->constant_
);
2387 // Return an integer constant value.
2390 Const_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
2397 if (this->type_
!= NULL
)
2398 ctype
= this->type_
;
2400 ctype
= this->constant_
->const_value()->type();
2401 if (ctype
!= NULL
&& ctype
->integer_type() == NULL
)
2404 Expression
* e
= this->constant_
->const_value()->expr();
2409 bool r
= e
->integer_constant_value(iota_is_constant
, val
, &t
);
2411 this->seen_
= false;
2415 && !Integer_expression::check_constant(val
, ctype
, this->location()))
2418 *ptype
= ctype
!= NULL
? ctype
: t
;
2422 // Return a floating point constant value.
2425 Const_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
2431 if (this->type_
!= NULL
)
2432 ctype
= this->type_
;
2434 ctype
= this->constant_
->const_value()->type();
2435 if (ctype
!= NULL
&& ctype
->float_type() == NULL
)
2441 bool r
= this->constant_
->const_value()->expr()->float_constant_value(val
,
2444 this->seen_
= false;
2446 if (r
&& ctype
!= NULL
)
2448 if (!Float_expression::check_constant(val
, ctype
, this->location()))
2450 Float_expression::constrain_float(val
, ctype
);
2452 *ptype
= ctype
!= NULL
? ctype
: t
;
2456 // Return a complex constant value.
2459 Const_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2466 if (this->type_
!= NULL
)
2467 ctype
= this->type_
;
2469 ctype
= this->constant_
->const_value()->type();
2470 if (ctype
!= NULL
&& ctype
->complex_type() == NULL
)
2476 bool r
= this->constant_
->const_value()->expr()->complex_constant_value(real
,
2480 this->seen_
= false;
2482 if (r
&& ctype
!= NULL
)
2484 if (!Complex_expression::check_constant(real
, imag
, ctype
,
2487 Complex_expression::constrain_complex(real
, imag
, ctype
);
2489 *ptype
= ctype
!= NULL
? ctype
: t
;
2493 // Return the type of the const reference.
2496 Const_expression::do_type()
2498 if (this->type_
!= NULL
)
2503 this->report_error(_("constant refers to itself"));
2504 this->type_
= Type::make_error_type();
2510 Named_constant
* nc
= this->constant_
->const_value();
2511 Type
* ret
= nc
->type();
2515 this->seen_
= false;
2519 // During parsing, a named constant may have a NULL type, but we
2520 // must not return a NULL type here.
2521 ret
= nc
->expr()->type();
2523 this->seen_
= false;
2528 // Set the type of the const reference.
2531 Const_expression::do_determine_type(const Type_context
* context
)
2533 Type
* ctype
= this->constant_
->const_value()->type();
2534 Type
* cetype
= (ctype
!= NULL
2536 : this->constant_
->const_value()->expr()->type());
2537 if (ctype
!= NULL
&& !ctype
->is_abstract())
2539 else if (context
->type
!= NULL
2540 && (context
->type
->integer_type() != NULL
2541 || context
->type
->float_type() != NULL
2542 || context
->type
->complex_type() != NULL
)
2543 && (cetype
->integer_type() != NULL
2544 || cetype
->float_type() != NULL
2545 || cetype
->complex_type() != NULL
))
2546 this->type_
= context
->type
;
2547 else if (context
->type
!= NULL
2548 && context
->type
->is_string_type()
2549 && cetype
->is_string_type())
2550 this->type_
= context
->type
;
2551 else if (context
->type
!= NULL
2552 && context
->type
->is_boolean_type()
2553 && cetype
->is_boolean_type())
2554 this->type_
= context
->type
;
2555 else if (!context
->may_be_abstract
)
2557 if (cetype
->is_abstract())
2558 cetype
= cetype
->make_non_abstract_type();
2559 this->type_
= cetype
;
2563 // Check types of a const reference.
2566 Const_expression::do_check_types(Gogo
*)
2568 if (this->type_
== NULL
|| this->type_
->is_abstract())
2571 // Check for integer overflow.
2572 if (this->type_
->integer_type() != NULL
)
2577 if (!this->integer_constant_value(true, ival
, &dummy
))
2581 Expression
* cexpr
= this->constant_
->const_value()->expr();
2582 if (cexpr
->float_constant_value(fval
, &dummy
))
2584 if (!mpfr_integer_p(fval
))
2585 this->report_error(_("floating point constant "
2586 "truncated to integer"));
2589 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2590 Integer_expression::check_constant(ival
, this->type_
,
2600 // Return a tree for the const reference.
2603 Const_expression::do_get_tree(Translate_context
* context
)
2605 Gogo
* gogo
= context
->gogo();
2607 if (this->type_
== NULL
)
2608 type_tree
= NULL_TREE
;
2611 type_tree
= this->type_
->get_tree(gogo
);
2612 if (type_tree
== error_mark_node
)
2613 return error_mark_node
;
2616 // If the type has been set for this expression, but the underlying
2617 // object is an abstract int or float, we try to get the abstract
2618 // value. Otherwise we may lose something in the conversion.
2619 if (this->type_
!= NULL
2620 && this->constant_
->const_value()->type()->is_abstract())
2622 Expression
* expr
= this->constant_
->const_value()->expr();
2626 if (expr
->integer_constant_value(true, ival
, &t
))
2628 tree ret
= Expression::integer_constant_tree(ival
, type_tree
);
2636 if (expr
->float_constant_value(fval
, &t
))
2638 tree ret
= Expression::float_constant_tree(fval
, type_tree
);
2645 if (expr
->complex_constant_value(fval
, imag
, &t
))
2647 tree ret
= Expression::complex_constant_tree(fval
, imag
, type_tree
);
2656 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2657 if (this->type_
== NULL
2658 || const_tree
== error_mark_node
2659 || TREE_TYPE(const_tree
) == error_mark_node
)
2663 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2664 ret
= fold_convert(type_tree
, const_tree
);
2665 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2666 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2667 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2668 ret
= fold(convert_to_real(type_tree
, const_tree
));
2669 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2670 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2676 // Make a reference to a constant in an expression.
2679 Expression::make_const_reference(Named_object
* constant
,
2680 source_location location
)
2682 return new Const_expression(constant
, location
);
2687 class Nil_expression
: public Expression
2690 Nil_expression(source_location location
)
2691 : Expression(EXPRESSION_NIL
, location
)
2699 do_is_constant() const
2704 { return Type::make_nil_type(); }
2707 do_determine_type(const Type_context
*)
2715 do_get_tree(Translate_context
*)
2716 { return null_pointer_node
; }
2719 do_export(Export
* exp
) const
2720 { exp
->write_c_string("nil"); }
2723 // Import a nil expression.
2726 Nil_expression::do_import(Import
* imp
)
2728 imp
->require_c_string("nil");
2729 return Expression::make_nil(imp
->location());
2732 // Make a nil expression.
2735 Expression::make_nil(source_location location
)
2737 return new Nil_expression(location
);
2740 // The value of the predeclared constant iota. This is little more
2741 // than a marker. This will be lowered to an integer in
2742 // Const_expression::do_lower, which is where we know the value that
2745 class Iota_expression
: public Parser_expression
2748 Iota_expression(source_location location
)
2749 : Parser_expression(EXPRESSION_IOTA
, location
)
2754 do_lower(Gogo
*, Named_object
*, int)
2755 { gcc_unreachable(); }
2757 // There should only ever be one of these.
2760 { gcc_unreachable(); }
2763 // Make an iota expression. This is only called for one case: the
2764 // value of the predeclared constant iota.
2767 Expression::make_iota()
2769 static Iota_expression
iota_expression(UNKNOWN_LOCATION
);
2770 return &iota_expression
;
2773 // A type conversion expression.
2775 class Type_conversion_expression
: public Expression
2778 Type_conversion_expression(Type
* type
, Expression
* expr
,
2779 source_location location
)
2780 : Expression(EXPRESSION_CONVERSION
, location
),
2781 type_(type
), expr_(expr
), may_convert_function_types_(false)
2784 // Return the type to which we are converting.
2787 { return this->type_
; }
2789 // Return the expression which we are converting.
2792 { return this->expr_
; }
2794 // Permit converting from one function type to another. This is
2795 // used internally for method expressions.
2797 set_may_convert_function_types()
2799 this->may_convert_function_types_
= true;
2802 // Import a type conversion expression.
2808 do_traverse(Traverse
* traverse
);
2811 do_lower(Gogo
*, Named_object
*, int);
2814 do_is_constant() const
2815 { return this->expr_
->is_constant(); }
2818 do_integer_constant_value(bool, mpz_t
, Type
**) const;
2821 do_float_constant_value(mpfr_t
, Type
**) const;
2824 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
2827 do_string_constant_value(std::string
*) const;
2831 { return this->type_
; }
2834 do_determine_type(const Type_context
*)
2836 Type_context
subcontext(this->type_
, false);
2837 this->expr_
->determine_type(&subcontext
);
2841 do_check_types(Gogo
*);
2846 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
2851 do_get_tree(Translate_context
* context
);
2854 do_export(Export
*) const;
2857 // The type to convert to.
2859 // The expression to convert.
2861 // True if this is permitted to convert function types. This is
2862 // used internally for method expressions.
2863 bool may_convert_function_types_
;
2869 Type_conversion_expression::do_traverse(Traverse
* traverse
)
2871 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
2872 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
2873 return TRAVERSE_EXIT
;
2874 return TRAVERSE_CONTINUE
;
2877 // Convert to a constant at lowering time.
2880 Type_conversion_expression::do_lower(Gogo
*, Named_object
*, int)
2882 Type
* type
= this->type_
;
2883 Expression
* val
= this->expr_
;
2884 source_location location
= this->location();
2886 if (type
->integer_type() != NULL
)
2891 if (val
->integer_constant_value(false, ival
, &dummy
))
2893 if (!Integer_expression::check_constant(ival
, type
, location
))
2894 mpz_set_ui(ival
, 0);
2895 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2902 if (val
->float_constant_value(fval
, &dummy
))
2904 if (!mpfr_integer_p(fval
))
2907 "floating point constant truncated to integer");
2908 return Expression::make_error(location
);
2910 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2911 if (!Integer_expression::check_constant(ival
, type
, location
))
2912 mpz_set_ui(ival
, 0);
2913 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2922 if (type
->float_type() != NULL
)
2927 if (val
->float_constant_value(fval
, &dummy
))
2929 if (!Float_expression::check_constant(fval
, type
, location
))
2930 mpfr_set_ui(fval
, 0, GMP_RNDN
);
2931 Float_expression::constrain_float(fval
, type
);
2932 Expression
*ret
= Expression::make_float(&fval
, type
, location
);
2939 if (type
->complex_type() != NULL
)
2946 if (val
->complex_constant_value(real
, imag
, &dummy
))
2948 if (!Complex_expression::check_constant(real
, imag
, type
, location
))
2950 mpfr_set_ui(real
, 0, GMP_RNDN
);
2951 mpfr_set_ui(imag
, 0, GMP_RNDN
);
2953 Complex_expression::constrain_complex(real
, imag
, type
);
2954 Expression
* ret
= Expression::make_complex(&real
, &imag
, type
,
2964 if (type
->is_open_array_type() && type
->named_type() == NULL
)
2966 Type
* element_type
= type
->array_type()->element_type()->forwarded();
2967 bool is_byte
= element_type
== Type::lookup_integer_type("uint8");
2968 bool is_int
= element_type
== Type::lookup_integer_type("int");
2969 if (is_byte
|| is_int
)
2972 if (val
->string_constant_value(&s
))
2974 Expression_list
* vals
= new Expression_list();
2977 for (std::string::const_iterator p
= s
.begin();
2982 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
2983 Expression
* v
= Expression::make_integer(&val
,
2992 const char *p
= s
.data();
2993 const char *pend
= s
.data() + s
.length();
2997 int adv
= Lex::fetch_char(p
, &c
);
3000 warning_at(this->location(), 0,
3001 "invalid UTF-8 encoding");
3006 mpz_init_set_ui(val
, c
);
3007 Expression
* v
= Expression::make_integer(&val
,
3015 return Expression::make_slice_composite_literal(type
, vals
,
3024 // Return the constant integer value if there is one.
3027 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant
,
3031 if (this->type_
->integer_type() == NULL
)
3037 if (this->expr_
->integer_constant_value(iota_is_constant
, ival
, &dummy
))
3039 if (!Integer_expression::check_constant(ival
, this->type_
,
3047 *ptype
= this->type_
;
3054 if (this->expr_
->float_constant_value(fval
, &dummy
))
3056 mpfr_get_z(val
, fval
, GMP_RNDN
);
3058 if (!Integer_expression::check_constant(val
, this->type_
,
3061 *ptype
= this->type_
;
3069 // Return the constant floating point value if there is one.
3072 Type_conversion_expression::do_float_constant_value(mpfr_t val
,
3075 if (this->type_
->float_type() == NULL
)
3081 if (this->expr_
->float_constant_value(fval
, &dummy
))
3083 if (!Float_expression::check_constant(fval
, this->type_
,
3089 mpfr_set(val
, fval
, GMP_RNDN
);
3091 Float_expression::constrain_float(val
, this->type_
);
3092 *ptype
= this->type_
;
3100 // Return the constant complex value if there is one.
3103 Type_conversion_expression::do_complex_constant_value(mpfr_t real
,
3107 if (this->type_
->complex_type() == NULL
)
3115 if (this->expr_
->complex_constant_value(rval
, ival
, &dummy
))
3117 if (!Complex_expression::check_constant(rval
, ival
, this->type_
,
3124 mpfr_set(real
, rval
, GMP_RNDN
);
3125 mpfr_set(imag
, ival
, GMP_RNDN
);
3128 Complex_expression::constrain_complex(real
, imag
, this->type_
);
3129 *ptype
= this->type_
;
3138 // Return the constant string value if there is one.
3141 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3143 if (this->type_
->is_string_type()
3144 && this->expr_
->type()->integer_type() != NULL
)
3149 if (this->expr_
->integer_constant_value(false, ival
, &dummy
))
3151 unsigned long ulval
= mpz_get_ui(ival
);
3152 if (mpz_cmp_ui(ival
, ulval
) == 0)
3154 Lex::append_char(ulval
, true, val
, this->location());
3162 // FIXME: Could handle conversion from const []int here.
3167 // Check that types are convertible.
3170 Type_conversion_expression::do_check_types(Gogo
*)
3172 Type
* type
= this->type_
;
3173 Type
* expr_type
= this->expr_
->type();
3176 if (this->may_convert_function_types_
3177 && type
->function_type() != NULL
3178 && expr_type
->function_type() != NULL
)
3181 if (Type::are_convertible(type
, expr_type
, &reason
))
3184 error_at(this->location(), "%s", reason
.c_str());
3185 this->set_is_error();
3188 // Get a tree for a type conversion.
3191 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3193 Gogo
* gogo
= context
->gogo();
3194 tree type_tree
= this->type_
->get_tree(gogo
);
3195 tree expr_tree
= this->expr_
->get_tree(context
);
3197 if (type_tree
== error_mark_node
3198 || expr_tree
== error_mark_node
3199 || TREE_TYPE(expr_tree
) == error_mark_node
)
3200 return error_mark_node
;
3202 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3203 return fold_convert(type_tree
, expr_tree
);
3205 Type
* type
= this->type_
;
3206 Type
* expr_type
= this->expr_
->type();
3208 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3209 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3210 expr_tree
, this->location());
3211 else if (type
->integer_type() != NULL
)
3213 if (expr_type
->integer_type() != NULL
3214 || expr_type
->float_type() != NULL
3215 || expr_type
->is_unsafe_pointer_type())
3216 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3220 else if (type
->float_type() != NULL
)
3222 if (expr_type
->integer_type() != NULL
3223 || expr_type
->float_type() != NULL
)
3224 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3228 else if (type
->complex_type() != NULL
)
3230 if (expr_type
->complex_type() != NULL
)
3231 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3235 else if (type
->is_string_type()
3236 && expr_type
->integer_type() != NULL
)
3238 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3239 if (host_integerp(expr_tree
, 0))
3241 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3243 Lex::append_char(intval
, true, &s
, this->location());
3244 Expression
* se
= Expression::make_string(s
, this->location());
3245 return se
->get_tree(context
);
3248 static tree int_to_string_fndecl
;
3249 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3251 "__go_int_to_string",
3255 fold_convert(integer_type_node
, expr_tree
));
3257 else if (type
->is_string_type()
3258 && (expr_type
->array_type() != NULL
3259 || (expr_type
->points_to() != NULL
3260 && expr_type
->points_to()->array_type() != NULL
)))
3262 Type
* t
= expr_type
;
3263 if (t
->points_to() != NULL
)
3266 expr_tree
= build_fold_indirect_ref(expr_tree
);
3268 if (!DECL_P(expr_tree
))
3269 expr_tree
= save_expr(expr_tree
);
3270 Array_type
* a
= t
->array_type();
3271 Type
* e
= a
->element_type()->forwarded();
3272 gcc_assert(e
->integer_type() != NULL
);
3273 tree valptr
= fold_convert(const_ptr_type_node
,
3274 a
->value_pointer_tree(gogo
, expr_tree
));
3275 tree len
= a
->length_tree(gogo
, expr_tree
);
3276 len
= fold_convert_loc(this->location(), size_type_node
, len
);
3277 if (e
->integer_type()->is_unsigned()
3278 && e
->integer_type()->bits() == 8)
3280 static tree byte_array_to_string_fndecl
;
3281 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3283 "__go_byte_array_to_string",
3286 const_ptr_type_node
,
3293 gcc_assert(e
== Type::lookup_integer_type("int"));
3294 static tree int_array_to_string_fndecl
;
3295 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3297 "__go_int_array_to_string",
3300 const_ptr_type_node
,
3306 else if (type
->is_open_array_type() && expr_type
->is_string_type())
3308 Type
* e
= type
->array_type()->element_type()->forwarded();
3309 gcc_assert(e
->integer_type() != NULL
);
3310 if (e
->integer_type()->is_unsigned()
3311 && e
->integer_type()->bits() == 8)
3313 static tree string_to_byte_array_fndecl
;
3314 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3316 "__go_string_to_byte_array",
3319 TREE_TYPE(expr_tree
),
3324 gcc_assert(e
== Type::lookup_integer_type("int"));
3325 static tree string_to_int_array_fndecl
;
3326 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3328 "__go_string_to_int_array",
3331 TREE_TYPE(expr_tree
),
3335 else if ((type
->is_unsafe_pointer_type()
3336 && expr_type
->points_to() != NULL
)
3337 || (expr_type
->is_unsafe_pointer_type()
3338 && type
->points_to() != NULL
))
3339 ret
= fold_convert(type_tree
, expr_tree
);
3340 else if (type
->is_unsafe_pointer_type()
3341 && expr_type
->integer_type() != NULL
)
3342 ret
= convert_to_pointer(type_tree
, expr_tree
);
3343 else if (this->may_convert_function_types_
3344 && type
->function_type() != NULL
3345 && expr_type
->function_type() != NULL
)
3346 ret
= fold_convert_loc(this->location(), type_tree
, expr_tree
);
3348 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3349 expr_tree
, this->location());
3354 // Output a type conversion in a constant expression.
3357 Type_conversion_expression::do_export(Export
* exp
) const
3359 exp
->write_c_string("convert(");
3360 exp
->write_type(this->type_
);
3361 exp
->write_c_string(", ");
3362 this->expr_
->export_expression(exp
);
3363 exp
->write_c_string(")");
3366 // Import a type conversion or a struct construction.
3369 Type_conversion_expression::do_import(Import
* imp
)
3371 imp
->require_c_string("convert(");
3372 Type
* type
= imp
->read_type();
3373 imp
->require_c_string(", ");
3374 Expression
* val
= Expression::import_expression(imp
);
3375 imp
->require_c_string(")");
3376 return Expression::make_cast(type
, val
, imp
->location());
3379 // Make a type cast expression.
3382 Expression::make_cast(Type
* type
, Expression
* val
, source_location location
)
3384 if (type
->is_error_type() || val
->is_error_expression())
3385 return Expression::make_error(location
);
3386 return new Type_conversion_expression(type
, val
, location
);
3389 // Unary expressions.
3391 class Unary_expression
: public Expression
3394 Unary_expression(Operator op
, Expression
* expr
, source_location location
)
3395 : Expression(EXPRESSION_UNARY
, location
),
3396 op_(op
), escapes_(true), expr_(expr
)
3399 // Return the operator.
3402 { return this->op_
; }
3404 // Return the operand.
3407 { return this->expr_
; }
3409 // Record that an address expression does not escape.
3411 set_does_not_escape()
3413 gcc_assert(this->op_
== OPERATOR_AND
);
3414 this->escapes_
= false;
3417 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3418 // could be done, false if not.
3420 eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3423 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3424 // could be done, false if not.
3426 eval_float(Operator op
, mpfr_t uval
, mpfr_t val
);
3428 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3429 // true if this could be done, false if not.
3431 eval_complex(Operator op
, mpfr_t ureal
, mpfr_t uimag
, mpfr_t real
,
3439 do_traverse(Traverse
* traverse
)
3440 { return Expression::traverse(&this->expr_
, traverse
); }
3443 do_lower(Gogo
*, Named_object
*, int);
3446 do_is_constant() const;
3449 do_integer_constant_value(bool, mpz_t
, Type
**) const;
3452 do_float_constant_value(mpfr_t
, Type
**) const;
3455 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
3461 do_determine_type(const Type_context
*);
3464 do_check_types(Gogo
*);
3469 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3474 do_is_addressable() const
3475 { return this->op_
== OPERATOR_MULT
; }
3478 do_get_tree(Translate_context
*);
3481 do_export(Export
*) const;
3484 // The unary operator to apply.
3486 // Normally true. False if this is an address expression which does
3487 // not escape the current function.
3493 // If we are taking the address of a composite literal, and the
3494 // contents are not constant, then we want to make a heap composite
3498 Unary_expression::do_lower(Gogo
*, Named_object
*, int)
3500 source_location loc
= this->location();
3501 Operator op
= this->op_
;
3502 Expression
* expr
= this->expr_
;
3504 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3505 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3507 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3508 // moving x to the heap. FIXME: Is it worth doing a real escape
3509 // analysis here? This case is found in math/unsafe.go and is
3510 // therefore worth special casing.
3511 if (op
== OPERATOR_MULT
)
3513 Expression
* e
= expr
;
3514 while (e
->classification() == EXPRESSION_CONVERSION
)
3516 Type_conversion_expression
* te
3517 = static_cast<Type_conversion_expression
*>(e
);
3521 if (e
->classification() == EXPRESSION_UNARY
)
3523 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3524 if (ue
->op_
== OPERATOR_AND
)
3531 ue
->set_does_not_escape();
3536 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
3537 || op
== OPERATOR_NOT
|| op
== OPERATOR_XOR
)
3539 Expression
* ret
= NULL
;
3544 if (expr
->integer_constant_value(false, eval
, &etype
))
3548 if (Unary_expression::eval_integer(op
, etype
, eval
, val
, loc
))
3549 ret
= Expression::make_integer(&val
, etype
, loc
);
3556 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
)
3561 if (expr
->float_constant_value(fval
, &ftype
))
3565 if (Unary_expression::eval_float(op
, fval
, val
))
3566 ret
= Expression::make_float(&val
, ftype
, loc
);
3577 if (expr
->complex_constant_value(fval
, ival
, &ftype
))
3583 if (Unary_expression::eval_complex(op
, fval
, ival
, real
, imag
))
3584 ret
= Expression::make_complex(&real
, &imag
, ftype
, loc
);
3598 // Return whether a unary expression is a constant.
3601 Unary_expression::do_is_constant() const
3603 if (this->op_
== OPERATOR_MULT
)
3605 // Indirecting through a pointer is only constant if the object
3606 // to which the expression points is constant, but we currently
3607 // have no way to determine that.
3610 else if (this->op_
== OPERATOR_AND
)
3612 // Taking the address of a variable is constant if it is a
3613 // global variable, not constant otherwise. In other cases
3614 // taking the address is probably not a constant.
3615 Var_expression
* ve
= this->expr_
->var_expression();
3618 Named_object
* no
= ve
->named_object();
3619 return no
->is_variable() && no
->var_value()->is_global();
3624 return this->expr_
->is_constant();
3627 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3628 // UVAL, if known; it may be NULL. Return true if this could be done,
3632 Unary_expression::eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3633 source_location location
)
3640 case OPERATOR_MINUS
:
3642 return Integer_expression::check_constant(val
, utype
, location
);
3644 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3648 || utype
->integer_type() == NULL
3649 || utype
->integer_type()->is_abstract())
3653 // The number of HOST_WIDE_INTs that it takes to represent
3655 size_t count
= ((mpz_sizeinbase(uval
, 2)
3656 + HOST_BITS_PER_WIDE_INT
3658 / HOST_BITS_PER_WIDE_INT
);
3660 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3661 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3664 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3665 gcc_assert(ecount
<= count
);
3667 // Trim down to the number of words required by the type.
3668 size_t obits
= utype
->integer_type()->bits();
3669 if (!utype
->integer_type()->is_unsigned())
3671 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3672 / HOST_BITS_PER_WIDE_INT
);
3673 gcc_assert(ocount
<= ocount
);
3675 for (size_t i
= 0; i
< ocount
; ++i
)
3678 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3680 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3683 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3687 return Integer_expression::check_constant(val
, utype
, location
);
3696 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3697 // could be done, false if not.
3700 Unary_expression::eval_float(Operator op
, mpfr_t uval
, mpfr_t val
)
3705 mpfr_set(val
, uval
, GMP_RNDN
);
3707 case OPERATOR_MINUS
:
3708 mpfr_neg(val
, uval
, GMP_RNDN
);
3720 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3721 // if this could be done, false if not.
3724 Unary_expression::eval_complex(Operator op
, mpfr_t rval
, mpfr_t ival
,
3725 mpfr_t real
, mpfr_t imag
)
3730 mpfr_set(real
, rval
, GMP_RNDN
);
3731 mpfr_set(imag
, ival
, GMP_RNDN
);
3733 case OPERATOR_MINUS
:
3734 mpfr_neg(real
, rval
, GMP_RNDN
);
3735 mpfr_neg(imag
, ival
, GMP_RNDN
);
3747 // Return the integral constant value of a unary expression, if it has one.
3750 Unary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
3756 if (!this->expr_
->integer_constant_value(iota_is_constant
, uval
, ptype
))
3759 ret
= Unary_expression::eval_integer(this->op_
, *ptype
, uval
, val
,
3765 // Return the floating point constant value of a unary expression, if
3769 Unary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
3774 if (!this->expr_
->float_constant_value(uval
, ptype
))
3777 ret
= Unary_expression::eval_float(this->op_
, uval
, val
);
3782 // Return the complex constant value of a unary expression, if it has
3786 Unary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
3794 if (!this->expr_
->complex_constant_value(rval
, ival
, ptype
))
3797 ret
= Unary_expression::eval_complex(this->op_
, rval
, ival
, real
, imag
);
3803 // Return the type of a unary expression.
3806 Unary_expression::do_type()
3811 case OPERATOR_MINUS
:
3814 return this->expr_
->type();
3817 return Type::make_pointer_type(this->expr_
->type());
3821 Type
* subtype
= this->expr_
->type();
3822 Type
* points_to
= subtype
->points_to();
3823 if (points_to
== NULL
)
3824 return Type::make_error_type();
3833 // Determine abstract types for a unary expression.
3836 Unary_expression::do_determine_type(const Type_context
* context
)
3841 case OPERATOR_MINUS
:
3844 this->expr_
->determine_type(context
);
3848 // Taking the address of something.
3850 Type
* subtype
= (context
->type
== NULL
3852 : context
->type
->points_to());
3853 Type_context
subcontext(subtype
, false);
3854 this->expr_
->determine_type(&subcontext
);
3859 // Indirecting through a pointer.
3861 Type
* subtype
= (context
->type
== NULL
3863 : Type::make_pointer_type(context
->type
));
3864 Type_context
subcontext(subtype
, false);
3865 this->expr_
->determine_type(&subcontext
);
3874 // Check types for a unary expression.
3877 Unary_expression::do_check_types(Gogo
*)
3879 Type
* type
= this->expr_
->type();
3880 if (type
->is_error_type())
3882 this->set_is_error();
3889 case OPERATOR_MINUS
:
3890 if (type
->integer_type() == NULL
3891 && type
->float_type() == NULL
3892 && type
->complex_type() == NULL
)
3893 this->report_error(_("expected numeric type"));
3898 if (type
->integer_type() == NULL
3899 && !type
->is_boolean_type())
3900 this->report_error(_("expected integer or boolean type"));
3904 if (!this->expr_
->is_addressable())
3905 this->report_error(_("invalid operand for unary %<&%>"));
3907 this->expr_
->address_taken(this->escapes_
);
3911 // Indirecting through a pointer.
3912 if (type
->points_to() == NULL
)
3913 this->report_error(_("expected pointer"));
3921 // Get a tree for a unary expression.
3924 Unary_expression::do_get_tree(Translate_context
* context
)
3926 tree expr
= this->expr_
->get_tree(context
);
3927 if (expr
== error_mark_node
)
3928 return error_mark_node
;
3930 source_location loc
= this->location();
3936 case OPERATOR_MINUS
:
3938 tree type
= TREE_TYPE(expr
);
3939 tree compute_type
= excess_precision_type(type
);
3940 if (compute_type
!= NULL_TREE
)
3941 expr
= ::convert(compute_type
, expr
);
3942 tree ret
= fold_build1_loc(loc
, NEGATE_EXPR
,
3943 (compute_type
!= NULL_TREE
3947 if (compute_type
!= NULL_TREE
)
3948 ret
= ::convert(type
, ret
);
3953 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
3954 return fold_build1_loc(loc
, TRUTH_NOT_EXPR
, TREE_TYPE(expr
), expr
);
3956 return fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, expr
,
3957 build_int_cst(TREE_TYPE(expr
), 0));
3960 return fold_build1_loc(loc
, BIT_NOT_EXPR
, TREE_TYPE(expr
), expr
);
3963 // We should not see a non-constant constructor here; cases
3964 // where we would see one should have been moved onto the heap
3965 // at parse time. Taking the address of a nonconstant
3966 // constructor will not do what the programmer expects.
3967 gcc_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
3968 gcc_assert(TREE_CODE(expr
) != ADDR_EXPR
);
3970 // Build a decl for a constant constructor.
3971 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
3973 tree decl
= build_decl(this->location(), VAR_DECL
,
3974 create_tmp_var_name("C"), TREE_TYPE(expr
));
3975 DECL_EXTERNAL(decl
) = 0;
3976 TREE_PUBLIC(decl
) = 0;
3977 TREE_READONLY(decl
) = 1;
3978 TREE_CONSTANT(decl
) = 1;
3979 TREE_STATIC(decl
) = 1;
3980 TREE_ADDRESSABLE(decl
) = 1;
3981 DECL_ARTIFICIAL(decl
) = 1;
3982 DECL_INITIAL(decl
) = expr
;
3983 rest_of_decl_compilation(decl
, 1, 0);
3987 return build_fold_addr_expr_loc(loc
, expr
);
3991 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
3993 // If we are dereferencing the pointer to a large struct, we
3994 // need to check for nil. We don't bother to check for small
3995 // structs because we expect the system to crash on a nil
3996 // pointer dereference.
3997 HOST_WIDE_INT s
= int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr
)));
3998 if (s
== -1 || s
>= 4096)
4001 expr
= save_expr(expr
);
4002 tree compare
= fold_build2_loc(loc
, EQ_EXPR
, boolean_type_node
,
4004 fold_convert(TREE_TYPE(expr
),
4005 null_pointer_node
));
4006 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4008 expr
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(expr
),
4009 build3(COND_EXPR
, void_type_node
,
4010 compare
, crash
, NULL_TREE
),
4014 // If the type of EXPR is a recursive pointer type, then we
4015 // need to insert a cast before indirecting.
4016 if (TREE_TYPE(TREE_TYPE(expr
)) == ptr_type_node
)
4018 Type
* pt
= this->expr_
->type()->points_to();
4019 tree ind
= pt
->get_tree(context
->gogo());
4020 expr
= fold_convert_loc(loc
, build_pointer_type(ind
), expr
);
4023 return build_fold_indirect_ref_loc(loc
, expr
);
4031 // Export a unary expression.
4034 Unary_expression::do_export(Export
* exp
) const
4039 exp
->write_c_string("+ ");
4041 case OPERATOR_MINUS
:
4042 exp
->write_c_string("- ");
4045 exp
->write_c_string("! ");
4048 exp
->write_c_string("^ ");
4055 this->expr_
->export_expression(exp
);
4058 // Import a unary expression.
4061 Unary_expression::do_import(Import
* imp
)
4064 switch (imp
->get_char())
4070 op
= OPERATOR_MINUS
;
4081 imp
->require_c_string(" ");
4082 Expression
* expr
= Expression::import_expression(imp
);
4083 return Expression::make_unary(op
, expr
, imp
->location());
4086 // Make a unary expression.
4089 Expression::make_unary(Operator op
, Expression
* expr
, source_location location
)
4091 return new Unary_expression(op
, expr
, location
);
4094 // If this is an indirection through a pointer, return the expression
4095 // being pointed through. Otherwise return this.
4100 if (this->classification_
== EXPRESSION_UNARY
)
4102 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4103 if (ue
->op() == OPERATOR_MULT
)
4104 return ue
->operand();
4109 // Class Binary_expression.
4114 Binary_expression::do_traverse(Traverse
* traverse
)
4116 int t
= Expression::traverse(&this->left_
, traverse
);
4117 if (t
== TRAVERSE_EXIT
)
4118 return TRAVERSE_EXIT
;
4119 return Expression::traverse(&this->right_
, traverse
);
4122 // Compare integer constants according to OP.
4125 Binary_expression::compare_integer(Operator op
, mpz_t left_val
,
4128 int i
= mpz_cmp(left_val
, right_val
);
4133 case OPERATOR_NOTEQ
:
4148 // Compare floating point constants according to OP.
4151 Binary_expression::compare_float(Operator op
, Type
* type
, mpfr_t left_val
,
4156 i
= mpfr_cmp(left_val
, right_val
);
4160 mpfr_init_set(lv
, left_val
, GMP_RNDN
);
4162 mpfr_init_set(rv
, right_val
, GMP_RNDN
);
4163 Float_expression::constrain_float(lv
, type
);
4164 Float_expression::constrain_float(rv
, type
);
4165 i
= mpfr_cmp(lv
, rv
);
4173 case OPERATOR_NOTEQ
:
4188 // Compare complex constants according to OP. Complex numbers may
4189 // only be compared for equality.
4192 Binary_expression::compare_complex(Operator op
, Type
* type
,
4193 mpfr_t left_real
, mpfr_t left_imag
,
4194 mpfr_t right_real
, mpfr_t right_imag
)
4198 is_equal
= (mpfr_cmp(left_real
, right_real
) == 0
4199 && mpfr_cmp(left_imag
, right_imag
) == 0);
4204 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4205 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4208 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4209 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4210 Complex_expression::constrain_complex(lr
, li
, type
);
4211 Complex_expression::constrain_complex(rr
, ri
, type
);
4212 is_equal
= mpfr_cmp(lr
, rr
) == 0 && mpfr_cmp(li
, ri
) == 0;
4222 case OPERATOR_NOTEQ
:
4229 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4230 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4231 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4232 // this could be done, false if not.
4235 Binary_expression::eval_integer(Operator op
, Type
* left_type
, mpz_t left_val
,
4236 Type
* right_type
, mpz_t right_val
,
4237 source_location location
, mpz_t val
)
4239 bool is_shift_op
= false;
4243 case OPERATOR_ANDAND
:
4245 case OPERATOR_NOTEQ
:
4250 // These return boolean values. We should probably handle them
4251 // anyhow in case a type conversion is used on the result.
4254 mpz_add(val
, left_val
, right_val
);
4256 case OPERATOR_MINUS
:
4257 mpz_sub(val
, left_val
, right_val
);
4260 mpz_ior(val
, left_val
, right_val
);
4263 mpz_xor(val
, left_val
, right_val
);
4266 mpz_mul(val
, left_val
, right_val
);
4269 if (mpz_sgn(right_val
) != 0)
4270 mpz_tdiv_q(val
, left_val
, right_val
);
4273 error_at(location
, "division by zero");
4279 if (mpz_sgn(right_val
) != 0)
4280 mpz_tdiv_r(val
, left_val
, right_val
);
4283 error_at(location
, "division by zero");
4288 case OPERATOR_LSHIFT
:
4290 unsigned long shift
= mpz_get_ui(right_val
);
4291 if (mpz_cmp_ui(right_val
, shift
) != 0)
4293 error_at(location
, "shift count overflow");
4297 mpz_mul_2exp(val
, left_val
, shift
);
4302 case OPERATOR_RSHIFT
:
4304 unsigned long shift
= mpz_get_ui(right_val
);
4305 if (mpz_cmp_ui(right_val
, shift
) != 0)
4307 error_at(location
, "shift count overflow");
4311 if (mpz_cmp_ui(left_val
, 0) >= 0)
4312 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4314 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4320 mpz_and(val
, left_val
, right_val
);
4322 case OPERATOR_BITCLEAR
:
4326 mpz_com(tval
, right_val
);
4327 mpz_and(val
, left_val
, tval
);
4335 Type
* type
= left_type
;
4340 else if (type
!= right_type
&& right_type
!= NULL
)
4342 if (type
->is_abstract())
4344 else if (!right_type
->is_abstract())
4346 // This look like a type error which should be diagnosed
4347 // elsewhere. Don't do anything here, to avoid an
4348 // unhelpful chain of error messages.
4354 if (type
!= NULL
&& !type
->is_abstract())
4356 // We have to check the operands too, as we have implicitly
4357 // coerced them to TYPE.
4358 if ((type
!= left_type
4359 && !Integer_expression::check_constant(left_val
, type
, location
))
4361 && type
!= right_type
4362 && !Integer_expression::check_constant(right_val
, type
,
4364 || !Integer_expression::check_constant(val
, type
, location
))
4371 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4372 // Return true if this could be done, false if not.
4375 Binary_expression::eval_float(Operator op
, Type
* left_type
, mpfr_t left_val
,
4376 Type
* right_type
, mpfr_t right_val
,
4377 mpfr_t val
, source_location location
)
4382 case OPERATOR_ANDAND
:
4384 case OPERATOR_NOTEQ
:
4389 // These return boolean values. We should probably handle them
4390 // anyhow in case a type conversion is used on the result.
4393 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4395 case OPERATOR_MINUS
:
4396 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4401 case OPERATOR_BITCLEAR
:
4404 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4407 if (mpfr_zero_p(right_val
))
4408 error_at(location
, "division by zero");
4409 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4413 case OPERATOR_LSHIFT
:
4414 case OPERATOR_RSHIFT
:
4420 Type
* type
= left_type
;
4423 else if (type
!= right_type
&& right_type
!= NULL
)
4425 if (type
->is_abstract())
4427 else if (!right_type
->is_abstract())
4429 // This looks like a type error which should be diagnosed
4430 // elsewhere. Don't do anything here, to avoid an unhelpful
4431 // chain of error messages.
4436 if (type
!= NULL
&& !type
->is_abstract())
4438 if ((type
!= left_type
4439 && !Float_expression::check_constant(left_val
, type
, location
))
4440 || (type
!= right_type
4441 && !Float_expression::check_constant(right_val
, type
,
4443 || !Float_expression::check_constant(val
, type
, location
))
4444 mpfr_set_ui(val
, 0, GMP_RNDN
);
4450 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4451 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4452 // could be done, false if not.
4455 Binary_expression::eval_complex(Operator op
, Type
* left_type
,
4456 mpfr_t left_real
, mpfr_t left_imag
,
4458 mpfr_t right_real
, mpfr_t right_imag
,
4459 mpfr_t real
, mpfr_t imag
,
4460 source_location location
)
4465 case OPERATOR_ANDAND
:
4467 case OPERATOR_NOTEQ
:
4472 // These return boolean values and must be handled differently.
4475 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4476 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4478 case OPERATOR_MINUS
:
4479 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4480 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4485 case OPERATOR_BITCLEAR
:
4489 // You might think that multiplying two complex numbers would
4490 // be simple, and you would be right, until you start to think
4491 // about getting the right answer for infinity. If one
4492 // operand here is infinity and the other is anything other
4493 // than zero or NaN, then we are going to wind up subtracting
4494 // two infinity values. That will give us a NaN, but the
4495 // correct answer is infinity.
4499 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4503 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4507 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4511 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4513 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4514 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4516 // If we get NaN on both sides, check whether it should really
4517 // be infinity. The rule is that if either side of the
4518 // complex number is infinity, then the whole value is
4519 // infinity, even if the other side is NaN. So the only case
4520 // we have to fix is the one in which both sides are NaN.
4521 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4522 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4523 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4525 bool is_infinity
= false;
4529 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4530 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4534 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4535 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4537 // If the left side is infinity, then the result is
4539 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4541 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4542 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4543 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4544 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4547 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4548 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4552 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4553 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4558 // If the right side is infinity, then the result is
4560 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4562 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4563 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4564 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4565 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4568 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4569 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4573 mpfr_set_ui(li
, 0, GMP_RNDN
);
4574 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4579 // If we got an overflow in the intermediate computations,
4580 // then the result is infinity.
4582 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4583 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4587 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4588 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4592 mpfr_set_ui(li
, 0, GMP_RNDN
);
4593 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4597 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4598 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4602 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4603 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4610 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4611 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4612 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4613 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4614 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4615 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4616 mpfr_set_inf(real
, mpfr_sgn(real
));
4617 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4634 // For complex division we want to avoid having an
4635 // intermediate overflow turn the whole result in a NaN. We
4636 // scale the values to try to avoid this.
4638 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4639 error_at(location
, "division by zero");
4645 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4646 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4649 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4653 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4654 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4656 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4658 ilogbw
= mpfr_get_exp(t
);
4659 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4660 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4665 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4666 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4667 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4669 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4670 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4671 mpfr_add(real
, real
, t
, GMP_RNDN
);
4672 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4673 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4675 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4676 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4677 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4678 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4679 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4681 // If we wind up with NaN on both sides, check whether we
4682 // should really have infinity. The rule is that if either
4683 // side of the complex number is infinity, then the whole
4684 // value is infinity, even if the other side is NaN. So the
4685 // only case we have to fix is the one in which both sides are
4687 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4688 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4689 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4691 if (mpfr_zero_p(denom
))
4693 mpfr_set_inf(real
, mpfr_sgn(rr
));
4694 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4695 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4696 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4698 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4699 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4701 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4702 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4705 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4706 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4710 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
4714 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
4716 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4717 mpfr_set_inf(real
, mpfr_sgn(t3
));
4719 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
4720 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
4721 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4722 mpfr_set_inf(imag
, mpfr_sgn(t3
));
4728 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
4729 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
4731 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4732 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
4735 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4736 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
4740 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
4744 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
4746 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4747 mpfr_set_ui(real
, 0, GMP_RNDN
);
4748 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
4750 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
4751 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
4752 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4753 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4754 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
4772 case OPERATOR_LSHIFT
:
4773 case OPERATOR_RSHIFT
:
4779 Type
* type
= left_type
;
4782 else if (type
!= right_type
&& right_type
!= NULL
)
4784 if (type
->is_abstract())
4786 else if (!right_type
->is_abstract())
4788 // This looks like a type error which should be diagnosed
4789 // elsewhere. Don't do anything here, to avoid an unhelpful
4790 // chain of error messages.
4795 if (type
!= NULL
&& !type
->is_abstract())
4797 if ((type
!= left_type
4798 && !Complex_expression::check_constant(left_real
, left_imag
,
4800 || (type
!= right_type
4801 && !Complex_expression::check_constant(right_real
, right_imag
,
4803 || !Complex_expression::check_constant(real
, imag
, type
,
4806 mpfr_set_ui(real
, 0, GMP_RNDN
);
4807 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4814 // Lower a binary expression. We have to evaluate constant
4815 // expressions now, in order to implement Go's unlimited precision
4819 Binary_expression::do_lower(Gogo
*, Named_object
*, int)
4821 source_location location
= this->location();
4822 Operator op
= this->op_
;
4823 Expression
* left
= this->left_
;
4824 Expression
* right
= this->right_
;
4826 const bool is_comparison
= (op
== OPERATOR_EQEQ
4827 || op
== OPERATOR_NOTEQ
4828 || op
== OPERATOR_LT
4829 || op
== OPERATOR_LE
4830 || op
== OPERATOR_GT
4831 || op
== OPERATOR_GE
);
4833 // Integer constant expressions.
4839 mpz_init(right_val
);
4841 if (left
->integer_constant_value(false, left_val
, &left_type
)
4842 && right
->integer_constant_value(false, right_val
, &right_type
))
4844 Expression
* ret
= NULL
;
4845 if (left_type
!= right_type
4846 && left_type
!= NULL
4847 && right_type
!= NULL
4848 && left_type
->base() != right_type
->base()
4849 && op
!= OPERATOR_LSHIFT
4850 && op
!= OPERATOR_RSHIFT
)
4852 // May be a type error--let it be diagnosed later.
4854 else if (is_comparison
)
4856 bool b
= Binary_expression::compare_integer(op
, left_val
,
4858 ret
= Expression::make_cast(Type::lookup_bool_type(),
4859 Expression::make_boolean(b
, location
),
4867 if (Binary_expression::eval_integer(op
, left_type
, left_val
,
4868 right_type
, right_val
,
4871 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
);
4873 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
4875 else if (left_type
== NULL
)
4877 else if (right_type
== NULL
)
4879 else if (!left_type
->is_abstract()
4880 && left_type
->named_type() != NULL
)
4882 else if (!right_type
->is_abstract()
4883 && right_type
->named_type() != NULL
)
4885 else if (!left_type
->is_abstract())
4887 else if (!right_type
->is_abstract())
4889 else if (left_type
->float_type() != NULL
)
4891 else if (right_type
->float_type() != NULL
)
4893 else if (left_type
->complex_type() != NULL
)
4895 else if (right_type
->complex_type() != NULL
)
4899 ret
= Expression::make_integer(&val
, type
, location
);
4907 mpz_clear(right_val
);
4908 mpz_clear(left_val
);
4912 mpz_clear(right_val
);
4913 mpz_clear(left_val
);
4916 // Floating point constant expressions.
4919 mpfr_init(left_val
);
4922 mpfr_init(right_val
);
4924 if (left
->float_constant_value(left_val
, &left_type
)
4925 && right
->float_constant_value(right_val
, &right_type
))
4927 Expression
* ret
= NULL
;
4928 if (left_type
!= right_type
4929 && left_type
!= NULL
4930 && right_type
!= NULL
4931 && left_type
->base() != right_type
->base()
4932 && op
!= OPERATOR_LSHIFT
4933 && op
!= OPERATOR_RSHIFT
)
4935 // May be a type error--let it be diagnosed later.
4937 else if (is_comparison
)
4939 bool b
= Binary_expression::compare_float(op
,
4943 left_val
, right_val
);
4944 ret
= Expression::make_boolean(b
, location
);
4951 if (Binary_expression::eval_float(op
, left_type
, left_val
,
4952 right_type
, right_val
, val
,
4955 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
4956 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
4958 if (left_type
== NULL
)
4960 else if (right_type
== NULL
)
4962 else if (!left_type
->is_abstract()
4963 && left_type
->named_type() != NULL
)
4965 else if (!right_type
->is_abstract()
4966 && right_type
->named_type() != NULL
)
4968 else if (!left_type
->is_abstract())
4970 else if (!right_type
->is_abstract())
4972 else if (left_type
->float_type() != NULL
)
4974 else if (right_type
->float_type() != NULL
)
4978 ret
= Expression::make_float(&val
, type
, location
);
4986 mpfr_clear(right_val
);
4987 mpfr_clear(left_val
);
4991 mpfr_clear(right_val
);
4992 mpfr_clear(left_val
);
4995 // Complex constant expressions.
4999 mpfr_init(left_real
);
5000 mpfr_init(left_imag
);
5005 mpfr_init(right_real
);
5006 mpfr_init(right_imag
);
5009 if (left
->complex_constant_value(left_real
, left_imag
, &left_type
)
5010 && right
->complex_constant_value(right_real
, right_imag
, &right_type
))
5012 Expression
* ret
= NULL
;
5013 if (left_type
!= right_type
5014 && left_type
!= NULL
5015 && right_type
!= NULL
5016 && left_type
->base() != right_type
->base())
5018 // May be a type error--let it be diagnosed later.
5020 else if (is_comparison
)
5022 bool b
= Binary_expression::compare_complex(op
,
5030 ret
= Expression::make_boolean(b
, location
);
5039 if (Binary_expression::eval_complex(op
, left_type
,
5040 left_real
, left_imag
,
5042 right_real
, right_imag
,
5046 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5047 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5049 if (left_type
== NULL
)
5051 else if (right_type
== NULL
)
5053 else if (!left_type
->is_abstract()
5054 && left_type
->named_type() != NULL
)
5056 else if (!right_type
->is_abstract()
5057 && right_type
->named_type() != NULL
)
5059 else if (!left_type
->is_abstract())
5061 else if (!right_type
->is_abstract())
5063 else if (left_type
->complex_type() != NULL
)
5065 else if (right_type
->complex_type() != NULL
)
5069 ret
= Expression::make_complex(&real
, &imag
, type
,
5078 mpfr_clear(left_real
);
5079 mpfr_clear(left_imag
);
5080 mpfr_clear(right_real
);
5081 mpfr_clear(right_imag
);
5086 mpfr_clear(left_real
);
5087 mpfr_clear(left_imag
);
5088 mpfr_clear(right_real
);
5089 mpfr_clear(right_imag
);
5092 // String constant expressions.
5093 if (op
== OPERATOR_PLUS
5094 && left
->type()->is_string_type()
5095 && right
->type()->is_string_type())
5097 std::string left_string
;
5098 std::string right_string
;
5099 if (left
->string_constant_value(&left_string
)
5100 && right
->string_constant_value(&right_string
))
5101 return Expression::make_string(left_string
+ right_string
, location
);
5107 // Return the integer constant value, if it has one.
5110 Binary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
5116 if (!this->left_
->integer_constant_value(iota_is_constant
, left_val
,
5119 mpz_clear(left_val
);
5124 mpz_init(right_val
);
5126 if (!this->right_
->integer_constant_value(iota_is_constant
, right_val
,
5129 mpz_clear(right_val
);
5130 mpz_clear(left_val
);
5135 if (left_type
!= right_type
5136 && left_type
!= NULL
5137 && right_type
!= NULL
5138 && left_type
->base() != right_type
->base()
5139 && this->op_
!= OPERATOR_RSHIFT
5140 && this->op_
!= OPERATOR_LSHIFT
)
5143 ret
= Binary_expression::eval_integer(this->op_
, left_type
, left_val
,
5144 right_type
, right_val
,
5145 this->location(), val
);
5147 mpz_clear(right_val
);
5148 mpz_clear(left_val
);
5156 // Return the floating point constant value, if it has one.
5159 Binary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
5162 mpfr_init(left_val
);
5164 if (!this->left_
->float_constant_value(left_val
, &left_type
))
5166 mpfr_clear(left_val
);
5171 mpfr_init(right_val
);
5173 if (!this->right_
->float_constant_value(right_val
, &right_type
))
5175 mpfr_clear(right_val
);
5176 mpfr_clear(left_val
);
5181 if (left_type
!= right_type
5182 && left_type
!= NULL
5183 && right_type
!= NULL
5184 && left_type
->base() != right_type
->base())
5187 ret
= Binary_expression::eval_float(this->op_
, left_type
, left_val
,
5188 right_type
, right_val
,
5189 val
, this->location());
5191 mpfr_clear(left_val
);
5192 mpfr_clear(right_val
);
5200 // Return the complex constant value, if it has one.
5203 Binary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
5208 mpfr_init(left_real
);
5209 mpfr_init(left_imag
);
5211 if (!this->left_
->complex_constant_value(left_real
, left_imag
, &left_type
))
5213 mpfr_clear(left_real
);
5214 mpfr_clear(left_imag
);
5220 mpfr_init(right_real
);
5221 mpfr_init(right_imag
);
5223 if (!this->right_
->complex_constant_value(right_real
, right_imag
,
5226 mpfr_clear(left_real
);
5227 mpfr_clear(left_imag
);
5228 mpfr_clear(right_real
);
5229 mpfr_clear(right_imag
);
5234 if (left_type
!= right_type
5235 && left_type
!= NULL
5236 && right_type
!= NULL
5237 && left_type
->base() != right_type
->base())
5240 ret
= Binary_expression::eval_complex(this->op_
, left_type
,
5241 left_real
, left_imag
,
5243 right_real
, right_imag
,
5246 mpfr_clear(left_real
);
5247 mpfr_clear(left_imag
);
5248 mpfr_clear(right_real
);
5249 mpfr_clear(right_imag
);
5257 // Note that the value is being discarded.
5260 Binary_expression::do_discarding_value()
5262 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5263 this->right_
->discarding_value();
5265 this->warn_about_unused_value();
5271 Binary_expression::do_type()
5276 case OPERATOR_ANDAND
:
5278 case OPERATOR_NOTEQ
:
5283 return Type::lookup_bool_type();
5286 case OPERATOR_MINUS
:
5293 case OPERATOR_BITCLEAR
:
5295 Type
* left_type
= this->left_
->type();
5296 Type
* right_type
= this->right_
->type();
5297 if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5299 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5301 else if (!left_type
->is_abstract())
5303 else if (!right_type
->is_abstract())
5305 else if (left_type
->complex_type() != NULL
)
5307 else if (right_type
->complex_type() != NULL
)
5309 else if (left_type
->float_type() != NULL
)
5311 else if (right_type
->float_type() != NULL
)
5317 case OPERATOR_LSHIFT
:
5318 case OPERATOR_RSHIFT
:
5319 return this->left_
->type();
5326 // Set type for a binary expression.
5329 Binary_expression::do_determine_type(const Type_context
* context
)
5331 Type
* tleft
= this->left_
->type();
5332 Type
* tright
= this->right_
->type();
5334 // Both sides should have the same type, except for the shift
5335 // operations. For a comparison, we should ignore the incoming
5338 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5339 || this->op_
== OPERATOR_RSHIFT
);
5341 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5342 || this->op_
== OPERATOR_NOTEQ
5343 || this->op_
== OPERATOR_LT
5344 || this->op_
== OPERATOR_LE
5345 || this->op_
== OPERATOR_GT
5346 || this->op_
== OPERATOR_GE
);
5348 Type_context
subcontext(*context
);
5352 // In a comparison, the context does not determine the types of
5354 subcontext
.type
= NULL
;
5357 // Set the context for the left hand operand.
5360 // The right hand operand plays no role in determining the type
5361 // of the left hand operand. A shift of an abstract integer in
5362 // a string context gets special treatment, which may be a
5364 if (subcontext
.type
!= NULL
5365 && subcontext
.type
->is_string_type()
5366 && tleft
->is_abstract())
5367 error_at(this->location(), "shift of non-integer operand");
5369 else if (!tleft
->is_abstract())
5370 subcontext
.type
= tleft
;
5371 else if (!tright
->is_abstract())
5372 subcontext
.type
= tright
;
5373 else if (subcontext
.type
== NULL
)
5375 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5376 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5377 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5379 // Both sides have an abstract integer, abstract float, or
5380 // abstract complex type. Just let CONTEXT determine
5381 // whether they may remain abstract or not.
5383 else if (tleft
->complex_type() != NULL
)
5384 subcontext
.type
= tleft
;
5385 else if (tright
->complex_type() != NULL
)
5386 subcontext
.type
= tright
;
5387 else if (tleft
->float_type() != NULL
)
5388 subcontext
.type
= tleft
;
5389 else if (tright
->float_type() != NULL
)
5390 subcontext
.type
= tright
;
5392 subcontext
.type
= tleft
;
5395 this->left_
->determine_type(&subcontext
);
5397 // The context for the right hand operand is the same as for the
5398 // left hand operand, except for a shift operator.
5401 subcontext
.type
= Type::lookup_integer_type("uint");
5402 subcontext
.may_be_abstract
= false;
5405 this->right_
->determine_type(&subcontext
);
5408 // Report an error if the binary operator OP does not support TYPE.
5409 // Return whether the operation is OK. This should not be used for
5413 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5414 source_location location
)
5419 case OPERATOR_ANDAND
:
5420 if (!type
->is_boolean_type())
5422 error_at(location
, "expected boolean type");
5428 case OPERATOR_NOTEQ
:
5429 if (type
->integer_type() == NULL
5430 && type
->float_type() == NULL
5431 && type
->complex_type() == NULL
5432 && !type
->is_string_type()
5433 && type
->points_to() == NULL
5434 && !type
->is_nil_type()
5435 && !type
->is_boolean_type()
5436 && type
->interface_type() == NULL
5437 && (type
->array_type() == NULL
5438 || type
->array_type()->length() != NULL
)
5439 && type
->map_type() == NULL
5440 && type
->channel_type() == NULL
5441 && type
->function_type() == NULL
)
5444 ("expected integer, floating, complex, string, pointer, "
5445 "boolean, interface, slice, map, channel, "
5446 "or function type"));
5455 if (type
->integer_type() == NULL
5456 && type
->float_type() == NULL
5457 && !type
->is_string_type())
5459 error_at(location
, "expected integer, floating, or string type");
5465 case OPERATOR_PLUSEQ
:
5466 if (type
->integer_type() == NULL
5467 && type
->float_type() == NULL
5468 && type
->complex_type() == NULL
5469 && !type
->is_string_type())
5472 "expected integer, floating, complex, or string type");
5477 case OPERATOR_MINUS
:
5478 case OPERATOR_MINUSEQ
:
5480 case OPERATOR_MULTEQ
:
5482 case OPERATOR_DIVEQ
:
5483 if (type
->integer_type() == NULL
5484 && type
->float_type() == NULL
5485 && type
->complex_type() == NULL
)
5487 error_at(location
, "expected integer, floating, or complex type");
5493 case OPERATOR_MODEQ
:
5497 case OPERATOR_ANDEQ
:
5499 case OPERATOR_XOREQ
:
5500 case OPERATOR_BITCLEAR
:
5501 case OPERATOR_BITCLEAREQ
:
5502 if (type
->integer_type() == NULL
)
5504 error_at(location
, "expected integer type");
5519 Binary_expression::do_check_types(Gogo
*)
5521 Type
* left_type
= this->left_
->type();
5522 Type
* right_type
= this->right_
->type();
5523 if (left_type
->is_error_type() || right_type
->is_error_type())
5525 this->set_is_error();
5529 if (this->op_
== OPERATOR_EQEQ
5530 || this->op_
== OPERATOR_NOTEQ
5531 || this->op_
== OPERATOR_LT
5532 || this->op_
== OPERATOR_LE
5533 || this->op_
== OPERATOR_GT
5534 || this->op_
== OPERATOR_GE
)
5536 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5537 && !Type::are_assignable(right_type
, left_type
, NULL
))
5539 this->report_error(_("incompatible types in binary expression"));
5542 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5544 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5547 this->set_is_error();
5551 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5553 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5555 this->report_error(_("incompatible types in binary expression"));
5558 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5561 this->set_is_error();
5567 if (left_type
->integer_type() == NULL
)
5568 this->report_error(_("shift of non-integer operand"));
5570 if (!right_type
->is_abstract()
5571 && (right_type
->integer_type() == NULL
5572 || !right_type
->integer_type()->is_unsigned()))
5573 this->report_error(_("shift count not unsigned integer"));
5579 if (this->right_
->integer_constant_value(true, val
, &type
))
5581 if (mpz_sgn(val
) < 0)
5582 this->report_error(_("negative shift count"));
5589 // Get a tree for a binary expression.
5592 Binary_expression::do_get_tree(Translate_context
* context
)
5594 tree left
= this->left_
->get_tree(context
);
5595 tree right
= this->right_
->get_tree(context
);
5597 if (left
== error_mark_node
|| right
== error_mark_node
)
5598 return error_mark_node
;
5600 enum tree_code code
;
5601 bool use_left_type
= true;
5602 bool is_shift_op
= false;
5606 case OPERATOR_NOTEQ
:
5611 return Expression::comparison_tree(context
, this->op_
,
5612 this->left_
->type(), left
,
5613 this->right_
->type(), right
,
5617 code
= TRUTH_ORIF_EXPR
;
5618 use_left_type
= false;
5620 case OPERATOR_ANDAND
:
5621 code
= TRUTH_ANDIF_EXPR
;
5622 use_left_type
= false;
5627 case OPERATOR_MINUS
:
5631 code
= BIT_IOR_EXPR
;
5634 code
= BIT_XOR_EXPR
;
5641 Type
*t
= this->left_
->type();
5642 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5645 code
= TRUNC_DIV_EXPR
;
5649 code
= TRUNC_MOD_EXPR
;
5651 case OPERATOR_LSHIFT
:
5655 case OPERATOR_RSHIFT
:
5660 code
= BIT_AND_EXPR
;
5662 case OPERATOR_BITCLEAR
:
5663 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5664 code
= BIT_AND_EXPR
;
5670 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5672 if (this->left_
->type()->is_string_type())
5674 gcc_assert(this->op_
== OPERATOR_PLUS
);
5675 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5676 static tree string_plus_decl
;
5677 return Gogo::call_builtin(&string_plus_decl
,
5688 tree compute_type
= excess_precision_type(type
);
5689 if (compute_type
!= NULL_TREE
)
5691 left
= ::convert(compute_type
, left
);
5692 right
= ::convert(compute_type
, right
);
5695 tree eval_saved
= NULL_TREE
;
5699 left
= save_expr(left
);
5701 right
= save_expr(right
);
5702 // Make sure the values are evaluated.
5703 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5704 void_type_node
, left
, right
);
5707 tree ret
= fold_build2_loc(this->location(),
5709 compute_type
!= NULL_TREE
? compute_type
: type
,
5712 if (compute_type
!= NULL_TREE
)
5713 ret
= ::convert(type
, ret
);
5715 // In Go, a shift larger than the size of the type is well-defined.
5716 // This is not true in GENERIC, so we need to insert a conditional.
5719 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5720 gcc_assert(this->left_
->type()->integer_type() != NULL
);
5721 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5723 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5724 build_int_cst_type(TREE_TYPE(right
), bits
));
5726 tree overflow_result
= fold_convert_loc(this->location(),
5729 if (this->op_
== OPERATOR_RSHIFT
5730 && !this->left_
->type()->integer_type()->is_unsigned())
5732 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
5733 boolean_type_node
, left
,
5734 fold_convert_loc(this->location(),
5736 integer_zero_node
));
5737 tree neg_one
= fold_build2_loc(this->location(),
5738 MINUS_EXPR
, TREE_TYPE(left
),
5739 fold_convert_loc(this->location(),
5742 fold_convert_loc(this->location(),
5745 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
5746 TREE_TYPE(left
), neg
, neg_one
,
5750 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
5751 compare
, ret
, overflow_result
);
5753 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5754 TREE_TYPE(ret
), eval_saved
, ret
);
5760 // Export a binary expression.
5763 Binary_expression::do_export(Export
* exp
) const
5765 exp
->write_c_string("(");
5766 this->left_
->export_expression(exp
);
5770 exp
->write_c_string(" || ");
5772 case OPERATOR_ANDAND
:
5773 exp
->write_c_string(" && ");
5776 exp
->write_c_string(" == ");
5778 case OPERATOR_NOTEQ
:
5779 exp
->write_c_string(" != ");
5782 exp
->write_c_string(" < ");
5785 exp
->write_c_string(" <= ");
5788 exp
->write_c_string(" > ");
5791 exp
->write_c_string(" >= ");
5794 exp
->write_c_string(" + ");
5796 case OPERATOR_MINUS
:
5797 exp
->write_c_string(" - ");
5800 exp
->write_c_string(" | ");
5803 exp
->write_c_string(" ^ ");
5806 exp
->write_c_string(" * ");
5809 exp
->write_c_string(" / ");
5812 exp
->write_c_string(" % ");
5814 case OPERATOR_LSHIFT
:
5815 exp
->write_c_string(" << ");
5817 case OPERATOR_RSHIFT
:
5818 exp
->write_c_string(" >> ");
5821 exp
->write_c_string(" & ");
5823 case OPERATOR_BITCLEAR
:
5824 exp
->write_c_string(" &^ ");
5829 this->right_
->export_expression(exp
);
5830 exp
->write_c_string(")");
5833 // Import a binary expression.
5836 Binary_expression::do_import(Import
* imp
)
5838 imp
->require_c_string("(");
5840 Expression
* left
= Expression::import_expression(imp
);
5843 if (imp
->match_c_string(" || "))
5848 else if (imp
->match_c_string(" && "))
5850 op
= OPERATOR_ANDAND
;
5853 else if (imp
->match_c_string(" == "))
5858 else if (imp
->match_c_string(" != "))
5860 op
= OPERATOR_NOTEQ
;
5863 else if (imp
->match_c_string(" < "))
5868 else if (imp
->match_c_string(" <= "))
5873 else if (imp
->match_c_string(" > "))
5878 else if (imp
->match_c_string(" >= "))
5883 else if (imp
->match_c_string(" + "))
5888 else if (imp
->match_c_string(" - "))
5890 op
= OPERATOR_MINUS
;
5893 else if (imp
->match_c_string(" | "))
5898 else if (imp
->match_c_string(" ^ "))
5903 else if (imp
->match_c_string(" * "))
5908 else if (imp
->match_c_string(" / "))
5913 else if (imp
->match_c_string(" % "))
5918 else if (imp
->match_c_string(" << "))
5920 op
= OPERATOR_LSHIFT
;
5923 else if (imp
->match_c_string(" >> "))
5925 op
= OPERATOR_RSHIFT
;
5928 else if (imp
->match_c_string(" & "))
5933 else if (imp
->match_c_string(" &^ "))
5935 op
= OPERATOR_BITCLEAR
;
5940 error_at(imp
->location(), "unrecognized binary operator");
5941 return Expression::make_error(imp
->location());
5944 Expression
* right
= Expression::import_expression(imp
);
5946 imp
->require_c_string(")");
5948 return Expression::make_binary(op
, left
, right
, imp
->location());
5951 // Make a binary expression.
5954 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
5955 source_location location
)
5957 return new Binary_expression(op
, left
, right
, location
);
5960 // Implement a comparison.
5963 Expression::comparison_tree(Translate_context
* context
, Operator op
,
5964 Type
* left_type
, tree left_tree
,
5965 Type
* right_type
, tree right_tree
,
5966 source_location location
)
5968 enum tree_code code
;
5974 case OPERATOR_NOTEQ
:
5993 if (left_type
->is_string_type() && right_type
->is_string_type())
5995 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5996 static tree string_compare_decl
;
5997 left_tree
= Gogo::call_builtin(&string_compare_decl
,
6006 right_tree
= build_int_cst_type(integer_type_node
, 0);
6008 else if ((left_type
->interface_type() != NULL
6009 && right_type
->interface_type() == NULL
6010 && !right_type
->is_nil_type())
6011 || (left_type
->interface_type() == NULL
6012 && !left_type
->is_nil_type()
6013 && right_type
->interface_type() != NULL
))
6015 // Comparing an interface value to a non-interface value.
6016 if (left_type
->interface_type() == NULL
)
6018 std::swap(left_type
, right_type
);
6019 std::swap(left_tree
, right_tree
);
6022 // The right operand is not an interface. We need to take its
6023 // address if it is not a pointer.
6026 if (right_type
->points_to() != NULL
)
6028 make_tmp
= NULL_TREE
;
6031 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
6033 make_tmp
= NULL_TREE
;
6034 arg
= build_fold_addr_expr_loc(location
, right_tree
);
6035 if (DECL_P(right_tree
))
6036 TREE_ADDRESSABLE(right_tree
) = 1;
6040 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
6041 get_name(right_tree
));
6042 DECL_IGNORED_P(tmp
) = 0;
6043 DECL_INITIAL(tmp
) = right_tree
;
6044 TREE_ADDRESSABLE(tmp
) = 1;
6045 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6046 SET_EXPR_LOCATION(make_tmp
, location
);
6047 arg
= build_fold_addr_expr_loc(location
, tmp
);
6049 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
6051 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
6053 if (left_type
->interface_type()->is_empty())
6055 static tree empty_interface_value_compare_decl
;
6056 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6058 "__go_empty_interface_value_compare",
6061 TREE_TYPE(left_tree
),
6063 TREE_TYPE(descriptor
),
6067 if (left_tree
== error_mark_node
)
6068 return error_mark_node
;
6069 // This can panic if the type is not comparable.
6070 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6074 static tree interface_value_compare_decl
;
6075 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6077 "__go_interface_value_compare",
6080 TREE_TYPE(left_tree
),
6082 TREE_TYPE(descriptor
),
6086 if (left_tree
== error_mark_node
)
6087 return error_mark_node
;
6088 // This can panic if the type is not comparable.
6089 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6091 right_tree
= build_int_cst_type(integer_type_node
, 0);
6093 if (make_tmp
!= NULL_TREE
)
6094 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6097 else if (left_type
->interface_type() != NULL
6098 && right_type
->interface_type() != NULL
)
6100 if (left_type
->interface_type()->is_empty())
6102 gcc_assert(right_type
->interface_type()->is_empty());
6103 static tree empty_interface_compare_decl
;
6104 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6106 "__go_empty_interface_compare",
6109 TREE_TYPE(left_tree
),
6111 TREE_TYPE(right_tree
),
6113 if (left_tree
== error_mark_node
)
6114 return error_mark_node
;
6115 // This can panic if the type is uncomparable.
6116 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6120 gcc_assert(!right_type
->interface_type()->is_empty());
6121 static tree interface_compare_decl
;
6122 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6124 "__go_interface_compare",
6127 TREE_TYPE(left_tree
),
6129 TREE_TYPE(right_tree
),
6131 if (left_tree
== error_mark_node
)
6132 return error_mark_node
;
6133 // This can panic if the type is uncomparable.
6134 TREE_NOTHROW(interface_compare_decl
) = 0;
6136 right_tree
= build_int_cst_type(integer_type_node
, 0);
6139 if (left_type
->is_nil_type()
6140 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6142 std::swap(left_type
, right_type
);
6143 std::swap(left_tree
, right_tree
);
6146 if (right_type
->is_nil_type())
6148 if (left_type
->array_type() != NULL
6149 && left_type
->array_type()->length() == NULL
)
6151 Array_type
* at
= left_type
->array_type();
6152 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6153 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6155 else if (left_type
->interface_type() != NULL
)
6157 // An interface is nil if the first field is nil.
6158 tree left_type_tree
= TREE_TYPE(left_tree
);
6159 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6160 tree field
= TYPE_FIELDS(left_type_tree
);
6161 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6163 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6167 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6168 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6172 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6173 return error_mark_node
;
6175 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6176 if (CAN_HAVE_LOCATION_P(ret
))
6177 SET_EXPR_LOCATION(ret
, location
);
6181 // Class Bound_method_expression.
6186 Bound_method_expression::do_traverse(Traverse
* traverse
)
6188 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6189 return TRAVERSE_EXIT
;
6190 return Expression::traverse(&this->method_
, traverse
);
6193 // Return the type of a bound method expression. The type of this
6194 // object is really the type of the method with no receiver. We
6195 // should be able to get away with just returning the type of the
6199 Bound_method_expression::do_type()
6201 return this->method_
->type();
6204 // Determine the types of a method expression.
6207 Bound_method_expression::do_determine_type(const Type_context
*)
6209 this->method_
->determine_type_no_context();
6210 Type
* mtype
= this->method_
->type();
6211 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6212 if (fntype
== NULL
|| !fntype
->is_method())
6213 this->expr_
->determine_type_no_context();
6216 Type_context
subcontext(fntype
->receiver()->type(), false);
6217 this->expr_
->determine_type(&subcontext
);
6221 // Check the types of a method expression.
6224 Bound_method_expression::do_check_types(Gogo
*)
6226 Type
* type
= this->method_
->type()->deref();
6228 || type
->function_type() == NULL
6229 || !type
->function_type()->is_method())
6230 this->report_error(_("object is not a method"));
6233 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6234 Type
* etype
= (this->expr_type_
!= NULL
6236 : this->expr_
->type());
6237 etype
= etype
->deref();
6238 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6239 this->report_error(_("method type does not match object type"));
6243 // Get the tree for a method expression. There is no standard tree
6244 // representation for this. The only places it may currently be used
6245 // are in a Call_expression or a Go_statement, which will take it
6246 // apart directly. So this has nothing to do at present.
6249 Bound_method_expression::do_get_tree(Translate_context
*)
6254 // Make a method expression.
6256 Bound_method_expression
*
6257 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6258 source_location location
)
6260 return new Bound_method_expression(expr
, method
, location
);
6263 // Class Builtin_call_expression. This is used for a call to a
6264 // builtin function.
6266 class Builtin_call_expression
: public Call_expression
6269 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6270 bool is_varargs
, source_location location
);
6273 // This overrides Call_expression::do_lower.
6275 do_lower(Gogo
*, Named_object
*, int);
6278 do_is_constant() const;
6281 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6284 do_float_constant_value(mpfr_t
, Type
**) const;
6287 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6293 do_determine_type(const Type_context
*);
6296 do_check_types(Gogo
*);
6301 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6302 this->args()->copy(),
6308 do_get_tree(Translate_context
*);
6311 do_export(Export
*) const;
6314 do_is_recover_call() const;
6317 do_set_recover_arg(Expression
*);
6320 // The builtin functions.
6321 enum Builtin_function_code
6325 // Predeclared builtin functions.
6342 // Builtin functions from the unsafe package.
6355 real_imag_type(Type
*);
6360 // A pointer back to the general IR structure. This avoids a global
6361 // variable, or passing it around everywhere.
6363 // The builtin function being called.
6364 Builtin_function_code code_
;
6367 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6369 Expression_list
* args
,
6371 source_location location
)
6372 : Call_expression(fn
, args
, is_varargs
, location
),
6373 gogo_(gogo
), code_(BUILTIN_INVALID
)
6375 Func_expression
* fnexp
= this->fn()->func_expression();
6376 gcc_assert(fnexp
!= NULL
);
6377 const std::string
& name(fnexp
->named_object()->name());
6378 if (name
== "append")
6379 this->code_
= BUILTIN_APPEND
;
6380 else if (name
== "cap")
6381 this->code_
= BUILTIN_CAP
;
6382 else if (name
== "close")
6383 this->code_
= BUILTIN_CLOSE
;
6384 else if (name
== "closed")
6385 this->code_
= BUILTIN_CLOSED
;
6386 else if (name
== "cmplx")
6387 this->code_
= BUILTIN_CMPLX
;
6388 else if (name
== "copy")
6389 this->code_
= BUILTIN_COPY
;
6390 else if (name
== "imag")
6391 this->code_
= BUILTIN_IMAG
;
6392 else if (name
== "len")
6393 this->code_
= BUILTIN_LEN
;
6394 else if (name
== "make")
6395 this->code_
= BUILTIN_MAKE
;
6396 else if (name
== "new")
6397 this->code_
= BUILTIN_NEW
;
6398 else if (name
== "panic")
6399 this->code_
= BUILTIN_PANIC
;
6400 else if (name
== "print")
6401 this->code_
= BUILTIN_PRINT
;
6402 else if (name
== "println")
6403 this->code_
= BUILTIN_PRINTLN
;
6404 else if (name
== "real")
6405 this->code_
= BUILTIN_REAL
;
6406 else if (name
== "recover")
6407 this->code_
= BUILTIN_RECOVER
;
6408 else if (name
== "Alignof")
6409 this->code_
= BUILTIN_ALIGNOF
;
6410 else if (name
== "Offsetof")
6411 this->code_
= BUILTIN_OFFSETOF
;
6412 else if (name
== "Sizeof")
6413 this->code_
= BUILTIN_SIZEOF
;
6418 // Return whether this is a call to recover. This is a virtual
6419 // function called from the parent class.
6422 Builtin_call_expression::do_is_recover_call() const
6424 if (this->classification() == EXPRESSION_ERROR
)
6426 return this->code_
== BUILTIN_RECOVER
;
6429 // Set the argument for a call to recover.
6432 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6434 const Expression_list
* args
= this->args();
6435 gcc_assert(args
== NULL
|| args
->empty());
6436 Expression_list
* new_args
= new Expression_list();
6437 new_args
->push_back(arg
);
6438 this->set_args(new_args
);
6441 // A traversal class which looks for a call expression.
6443 class Find_call_expression
: public Traverse
6446 Find_call_expression()
6447 : Traverse(traverse_expressions
),
6452 expression(Expression
**);
6456 { return this->found_
; }
6463 Find_call_expression::expression(Expression
** pexpr
)
6465 if ((*pexpr
)->call_expression() != NULL
)
6467 this->found_
= true;
6468 return TRAVERSE_EXIT
;
6470 return TRAVERSE_CONTINUE
;
6473 // Lower a builtin call expression. This turns new and make into
6474 // specific expressions. We also convert to a constant if we can.
6477 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6479 if (this->code_
== BUILTIN_NEW
)
6481 const Expression_list
* args
= this->args();
6482 if (args
== NULL
|| args
->size() < 1)
6483 this->report_error(_("not enough arguments"));
6484 else if (args
->size() > 1)
6485 this->report_error(_("too many arguments"));
6488 Expression
* arg
= args
->front();
6489 if (!arg
->is_type_expression())
6491 error_at(arg
->location(), "expected type");
6492 this->set_is_error();
6495 return Expression::make_allocation(arg
->type(), this->location());
6498 else if (this->code_
== BUILTIN_MAKE
)
6500 const Expression_list
* args
= this->args();
6501 if (args
== NULL
|| args
->size() < 1)
6502 this->report_error(_("not enough arguments"));
6505 Expression
* arg
= args
->front();
6506 if (!arg
->is_type_expression())
6508 error_at(arg
->location(), "expected type");
6509 this->set_is_error();
6513 Expression_list
* newargs
;
6514 if (args
->size() == 1)
6518 newargs
= new Expression_list();
6519 Expression_list::const_iterator p
= args
->begin();
6521 for (; p
!= args
->end(); ++p
)
6522 newargs
->push_back(*p
);
6524 return Expression::make_make(arg
->type(), newargs
,
6529 else if (this->is_constant())
6531 // We can only lower len and cap if there are no function calls
6532 // in the arguments. Otherwise we have to make the call.
6533 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6535 Expression
* arg
= this->one_arg();
6536 if (!arg
->is_constant())
6538 Find_call_expression find_call
;
6539 Expression::traverse(&arg
, &find_call
);
6540 if (find_call
.found())
6548 if (this->integer_constant_value(true, ival
, &type
))
6550 Expression
* ret
= Expression::make_integer(&ival
, type
,
6559 if (this->float_constant_value(rval
, &type
))
6561 Expression
* ret
= Expression::make_float(&rval
, type
,
6569 if (this->complex_constant_value(rval
, imag
, &type
))
6571 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6580 else if (this->code_
== BUILTIN_RECOVER
)
6582 if (function
!= NULL
)
6583 function
->func_value()->set_calls_recover();
6586 // Calling recover outside of a function always returns the
6587 // nil empty interface.
6588 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6589 return Expression::make_cast(eface
,
6590 Expression::make_nil(this->location()),
6594 else if (this->code_
== BUILTIN_APPEND
)
6596 // Lower the varargs.
6597 const Expression_list
* args
= this->args();
6598 if (args
== NULL
|| args
->empty())
6600 Type
* slice_type
= args
->front()->type();
6601 if (!slice_type
->is_open_array_type())
6603 error_at(args
->front()->location(), "argument 1 must be a slice");
6604 this->set_is_error();
6607 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6613 // Return the type of the real or imag functions, given the type of
6614 // the argument. We need to map complex to float, complex64 to
6615 // float32, and complex128 to float64, so it has to be done by name.
6616 // This returns NULL if it can't figure out the type.
6619 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6621 if (arg_type
== NULL
|| arg_type
->is_abstract())
6623 Named_type
* nt
= arg_type
->named_type();
6626 while (nt
->real_type()->named_type() != NULL
)
6627 nt
= nt
->real_type()->named_type();
6628 if (nt
->name() == "complex")
6629 return Type::lookup_float_type("float");
6630 else if (nt
->name() == "complex64")
6631 return Type::lookup_float_type("float32");
6632 else if (nt
->name() == "complex128")
6633 return Type::lookup_float_type("float64");
6638 // Return the type of the cmplx function, given the type of one of the
6639 // argments. Like real_imag_type, we have to map by name.
6642 Builtin_call_expression::cmplx_type(Type
* arg_type
)
6644 if (arg_type
== NULL
|| arg_type
->is_abstract())
6646 Named_type
* nt
= arg_type
->named_type();
6649 while (nt
->real_type()->named_type() != NULL
)
6650 nt
= nt
->real_type()->named_type();
6651 if (nt
->name() == "float")
6652 return Type::lookup_complex_type("complex");
6653 else if (nt
->name() == "float32")
6654 return Type::lookup_complex_type("complex64");
6655 else if (nt
->name() == "float64")
6656 return Type::lookup_complex_type("complex128");
6661 // Return a single argument, or NULL if there isn't one.
6664 Builtin_call_expression::one_arg() const
6666 const Expression_list
* args
= this->args();
6667 if (args
->size() != 1)
6669 return args
->front();
6672 // Return whether this is constant: len of a string, or len or cap of
6673 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6676 Builtin_call_expression::do_is_constant() const
6678 switch (this->code_
)
6683 Expression
* arg
= this->one_arg();
6686 Type
* arg_type
= arg
->type();
6688 if (arg_type
->points_to() != NULL
6689 && arg_type
->points_to()->array_type() != NULL
6690 && !arg_type
->points_to()->is_open_array_type())
6691 arg_type
= arg_type
->points_to();
6693 if (arg_type
->array_type() != NULL
6694 && arg_type
->array_type()->length() != NULL
)
6695 return arg_type
->array_type()->length()->is_constant();
6697 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6698 return arg
->is_constant();
6702 case BUILTIN_SIZEOF
:
6703 case BUILTIN_ALIGNOF
:
6704 return this->one_arg() != NULL
;
6706 case BUILTIN_OFFSETOF
:
6708 Expression
* arg
= this->one_arg();
6711 return arg
->field_reference_expression() != NULL
;
6716 const Expression_list
* args
= this->args();
6717 if (args
!= NULL
&& args
->size() == 2)
6718 return args
->front()->is_constant() && args
->back()->is_constant();
6725 Expression
* arg
= this->one_arg();
6726 return arg
!= NULL
&& arg
->is_constant();
6736 // Return an integer constant value if possible.
6739 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
6743 if (this->code_
== BUILTIN_LEN
6744 || this->code_
== BUILTIN_CAP
)
6746 Expression
* arg
= this->one_arg();
6749 Type
* arg_type
= arg
->type();
6751 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6754 if (arg
->string_constant_value(&sval
))
6756 mpz_set_ui(val
, sval
.length());
6757 *ptype
= Type::lookup_integer_type("int");
6762 if (arg_type
->points_to() != NULL
6763 && arg_type
->points_to()->array_type() != NULL
6764 && !arg_type
->points_to()->is_open_array_type())
6765 arg_type
= arg_type
->points_to();
6767 if (arg_type
->array_type() != NULL
6768 && arg_type
->array_type()->length() != NULL
)
6770 Expression
* e
= arg_type
->array_type()->length();
6771 if (e
->integer_constant_value(iota_is_constant
, val
, ptype
))
6773 *ptype
= Type::lookup_integer_type("int");
6778 else if (this->code_
== BUILTIN_SIZEOF
6779 || this->code_
== BUILTIN_ALIGNOF
)
6781 Expression
* arg
= this->one_arg();
6784 Type
* arg_type
= arg
->type();
6785 if (arg_type
->is_error_type() || arg_type
->is_undefined())
6787 if (arg_type
->is_abstract())
6789 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
6790 unsigned long val_long
;
6791 if (this->code_
== BUILTIN_SIZEOF
)
6793 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
6794 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
6795 if (TREE_INT_CST_HIGH(type_size
) != 0)
6797 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
6798 val_long
= static_cast<unsigned long>(val_wide
);
6799 if (val_long
!= val_wide
)
6802 else if (this->code_
== BUILTIN_ALIGNOF
)
6804 if (arg
->field_reference_expression() == NULL
)
6805 val_long
= go_type_alignment(arg_type_tree
);
6808 // Calling unsafe.Alignof(s.f) returns the alignment of
6809 // the type of f when it is used as a field in a struct.
6810 val_long
= go_field_alignment(arg_type_tree
);
6815 mpz_set_ui(val
, val_long
);
6819 else if (this->code_
== BUILTIN_OFFSETOF
)
6821 Expression
* arg
= this->one_arg();
6824 Field_reference_expression
* farg
= arg
->field_reference_expression();
6827 Expression
* struct_expr
= farg
->expr();
6828 Type
* st
= struct_expr
->type();
6829 if (st
->struct_type() == NULL
)
6831 tree struct_tree
= st
->get_tree(this->gogo_
);
6832 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
6833 tree field
= TYPE_FIELDS(struct_tree
);
6834 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
6836 field
= DECL_CHAIN(field
);
6837 gcc_assert(field
!= NULL_TREE
);
6839 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
6840 if (offset_wide
< 0)
6842 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
6843 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
6845 mpz_set_ui(val
, offset_long
);
6851 // Return a floating point constant value if possible.
6854 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
6857 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
6859 Expression
* arg
= this->one_arg();
6870 if (arg
->complex_constant_value(real
, imag
, &type
))
6872 if (this->code_
== BUILTIN_REAL
)
6873 mpfr_set(val
, real
, GMP_RNDN
);
6875 mpfr_set(val
, imag
, GMP_RNDN
);
6876 *ptype
= Builtin_call_expression::real_imag_type(type
);
6888 // Return a complex constant value if possible.
6891 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
6894 if (this->code_
== BUILTIN_CMPLX
)
6896 const Expression_list
* args
= this->args();
6897 if (args
== NULL
|| args
->size() != 2)
6903 if (!args
->front()->float_constant_value(r
, &rtype
))
6914 if (args
->back()->float_constant_value(i
, &itype
)
6915 && Type::are_identical(rtype
, itype
, false, NULL
))
6917 mpfr_set(real
, r
, GMP_RNDN
);
6918 mpfr_set(imag
, i
, GMP_RNDN
);
6919 *ptype
= Builtin_call_expression::cmplx_type(rtype
);
6935 Builtin_call_expression::do_type()
6937 switch (this->code_
)
6939 case BUILTIN_INVALID
:
6946 const Expression_list
* args
= this->args();
6947 if (args
== NULL
|| args
->empty())
6948 return Type::make_error_type();
6949 return Type::make_pointer_type(args
->front()->type());
6955 case BUILTIN_ALIGNOF
:
6956 case BUILTIN_OFFSETOF
:
6957 case BUILTIN_SIZEOF
:
6958 return Type::lookup_integer_type("int");
6963 case BUILTIN_PRINTLN
:
6964 return Type::make_void_type();
6966 case BUILTIN_CLOSED
:
6967 return Type::lookup_bool_type();
6969 case BUILTIN_RECOVER
:
6970 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
6972 case BUILTIN_APPEND
:
6974 const Expression_list
* args
= this->args();
6975 if (args
== NULL
|| args
->empty())
6976 return Type::make_error_type();
6977 return args
->front()->type();
6983 Expression
* arg
= this->one_arg();
6985 return Type::make_error_type();
6986 Type
* t
= arg
->type();
6987 if (t
->is_abstract())
6988 t
= t
->make_non_abstract_type();
6989 t
= Builtin_call_expression::real_imag_type(t
);
6991 t
= Type::make_error_type();
6997 const Expression_list
* args
= this->args();
6998 if (args
== NULL
|| args
->size() != 2)
6999 return Type::make_error_type();
7000 Type
* t
= args
->front()->type();
7001 if (t
->is_abstract())
7003 t
= args
->back()->type();
7004 if (t
->is_abstract())
7005 t
= t
->make_non_abstract_type();
7007 t
= Builtin_call_expression::cmplx_type(t
);
7009 t
= Type::make_error_type();
7015 // Determine the type.
7018 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7020 this->fn()->determine_type_no_context();
7022 const Expression_list
* args
= this->args();
7025 Type
* arg_type
= NULL
;
7026 switch (this->code_
)
7029 case BUILTIN_PRINTLN
:
7030 // Do not force a large integer constant to "int".
7036 arg_type
= Builtin_call_expression::cmplx_type(context
->type
);
7042 // For the cmplx function the type of one operand can
7043 // determine the type of the other, as in a binary expression.
7044 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7045 if (args
!= NULL
&& args
->size() == 2)
7047 Type
* t1
= args
->front()->type();
7048 Type
* t2
= args
->front()->type();
7049 if (!t1
->is_abstract())
7051 else if (!t2
->is_abstract())
7065 for (Expression_list::const_iterator pa
= args
->begin();
7069 Type_context subcontext
;
7070 subcontext
.type
= arg_type
;
7074 // We want to print large constants, we so can't just
7075 // use the appropriate nonabstract type. Use uint64 for
7076 // an integer if we know it is nonnegative, otherwise
7077 // use int64 for a integer, otherwise use float64 for a
7078 // float or complex128 for a complex.
7079 Type
* want_type
= NULL
;
7080 Type
* atype
= (*pa
)->type();
7081 if (atype
->is_abstract())
7083 if (atype
->integer_type() != NULL
)
7088 if (this->integer_constant_value(true, val
, &dummy
)
7089 && mpz_sgn(val
) >= 0)
7090 want_type
= Type::lookup_integer_type("uint64");
7092 want_type
= Type::lookup_integer_type("int64");
7095 else if (atype
->float_type() != NULL
)
7096 want_type
= Type::lookup_float_type("float64");
7097 else if (atype
->complex_type() != NULL
)
7098 want_type
= Type::lookup_complex_type("complex128");
7099 else if (atype
->is_abstract_string_type())
7100 want_type
= Type::lookup_string_type();
7101 else if (atype
->is_abstract_boolean_type())
7102 want_type
= Type::lookup_bool_type();
7105 subcontext
.type
= want_type
;
7109 (*pa
)->determine_type(&subcontext
);
7114 // If there is exactly one argument, return true. Otherwise give an
7115 // error message and return false.
7118 Builtin_call_expression::check_one_arg()
7120 const Expression_list
* args
= this->args();
7121 if (args
== NULL
|| args
->size() < 1)
7123 this->report_error(_("not enough arguments"));
7126 else if (args
->size() > 1)
7128 this->report_error(_("too many arguments"));
7131 if (args
->front()->is_error_expression()
7132 || args
->front()->type()->is_error_type()
7133 || args
->front()->type()->is_undefined())
7135 this->set_is_error();
7141 // Check argument types for a builtin function.
7144 Builtin_call_expression::do_check_types(Gogo
*)
7146 switch (this->code_
)
7148 case BUILTIN_INVALID
:
7156 // The single argument may be either a string or an array or a
7157 // map or a channel, or a pointer to a closed array.
7158 if (this->check_one_arg())
7160 Type
* arg_type
= this->one_arg()->type();
7161 if (arg_type
->points_to() != NULL
7162 && arg_type
->points_to()->array_type() != NULL
7163 && !arg_type
->points_to()->is_open_array_type())
7164 arg_type
= arg_type
->points_to();
7165 if (this->code_
== BUILTIN_CAP
)
7167 if (!arg_type
->is_error_type()
7168 && arg_type
->array_type() == NULL
7169 && arg_type
->channel_type() == NULL
)
7170 this->report_error(_("argument must be array or slice "
7175 if (!arg_type
->is_error_type()
7176 && !arg_type
->is_string_type()
7177 && arg_type
->array_type() == NULL
7178 && arg_type
->map_type() == NULL
7179 && arg_type
->channel_type() == NULL
)
7180 this->report_error(_("argument must be string or "
7181 "array or slice or map or channel"));
7188 case BUILTIN_PRINTLN
:
7190 const Expression_list
* args
= this->args();
7193 if (this->code_
== BUILTIN_PRINT
)
7194 warning_at(this->location(), 0,
7195 "no arguments for builtin function %<%s%>",
7196 (this->code_
== BUILTIN_PRINT
7202 for (Expression_list::const_iterator p
= args
->begin();
7206 Type
* type
= (*p
)->type();
7207 if (type
->is_error_type()
7208 || type
->is_string_type()
7209 || type
->integer_type() != NULL
7210 || type
->float_type() != NULL
7211 || type
->complex_type() != NULL
7212 || type
->is_boolean_type()
7213 || type
->points_to() != NULL
7214 || type
->interface_type() != NULL
7215 || type
->channel_type() != NULL
7216 || type
->map_type() != NULL
7217 || type
->function_type() != NULL
7218 || type
->is_open_array_type())
7221 this->report_error(_("unsupported argument type to "
7222 "builtin function"));
7229 case BUILTIN_CLOSED
:
7230 if (this->check_one_arg())
7232 if (this->one_arg()->type()->channel_type() == NULL
)
7233 this->report_error(_("argument must be channel"));
7238 case BUILTIN_SIZEOF
:
7239 case BUILTIN_ALIGNOF
:
7240 this->check_one_arg();
7243 case BUILTIN_RECOVER
:
7244 if (this->args() != NULL
&& !this->args()->empty())
7245 this->report_error(_("too many arguments"));
7248 case BUILTIN_OFFSETOF
:
7249 if (this->check_one_arg())
7251 Expression
* arg
= this->one_arg();
7252 if (arg
->field_reference_expression() == NULL
)
7253 this->report_error(_("argument must be a field reference"));
7259 const Expression_list
* args
= this->args();
7260 if (args
== NULL
|| args
->size() < 2)
7262 this->report_error(_("not enough arguments"));
7265 else if (args
->size() > 2)
7267 this->report_error(_("too many arguments"));
7270 Type
* arg1_type
= args
->front()->type();
7271 Type
* arg2_type
= args
->back()->type();
7272 if (arg1_type
->is_error_type() || arg2_type
->is_error_type())
7276 if (arg1_type
->is_open_array_type())
7277 e1
= arg1_type
->array_type()->element_type();
7280 this->report_error(_("left argument must be a slice"));
7285 if (arg2_type
->is_open_array_type())
7286 e2
= arg2_type
->array_type()->element_type();
7287 else if (arg2_type
->is_string_type())
7288 e2
= Type::lookup_integer_type("uint8");
7291 this->report_error(_("right argument must be a slice or a string"));
7295 if (!Type::are_identical(e1
, e2
, true, NULL
))
7296 this->report_error(_("element types must be the same"));
7300 case BUILTIN_APPEND
:
7302 const Expression_list
* args
= this->args();
7303 if (args
== NULL
|| args
->empty())
7305 this->report_error(_("not enough arguments"));
7308 /* Lowering varargs should have left us with 2 arguments. */
7309 gcc_assert(args
->size() == 2);
7311 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7315 this->report_error(_("arguments 1 and 2 have different types"));
7318 error_at(this->location(),
7319 "arguments 1 and 2 have different types (%s)",
7321 this->set_is_error();
7329 if (this->check_one_arg())
7331 if (this->one_arg()->type()->complex_type() == NULL
)
7332 this->report_error(_("argument must have complex type"));
7338 const Expression_list
* args
= this->args();
7339 if (args
== NULL
|| args
->size() < 2)
7340 this->report_error(_("not enough arguments"));
7341 else if (args
->size() > 2)
7342 this->report_error(_("too many arguments"));
7343 else if (args
->front()->is_error_expression()
7344 || args
->front()->type()->is_error_type()
7345 || args
->back()->is_error_expression()
7346 || args
->back()->type()->is_error_type())
7347 this->set_is_error();
7348 else if (!Type::are_identical(args
->front()->type(),
7349 args
->back()->type(), true, NULL
))
7350 this->report_error(_("cmplx arguments must have identical types"));
7351 else if (args
->front()->type()->float_type() == NULL
)
7352 this->report_error(_("cmplx arguments must have "
7353 "floating-point type"));
7362 // Return the tree for a builtin function.
7365 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7367 Gogo
* gogo
= context
->gogo();
7368 source_location location
= this->location();
7369 switch (this->code_
)
7371 case BUILTIN_INVALID
:
7379 const Expression_list
* args
= this->args();
7380 gcc_assert(args
!= NULL
&& args
->size() == 1);
7381 Expression
* arg
= *args
->begin();
7382 Type
* arg_type
= arg
->type();
7383 tree arg_tree
= arg
->get_tree(context
);
7384 if (arg_tree
== error_mark_node
)
7385 return error_mark_node
;
7387 if (arg_type
->points_to() != NULL
)
7389 arg_type
= arg_type
->points_to();
7390 gcc_assert(arg_type
->array_type() != NULL
7391 && !arg_type
->is_open_array_type());
7392 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7393 arg_tree
= build_fold_indirect_ref(arg_tree
);
7397 if (this->code_
== BUILTIN_LEN
)
7399 if (arg_type
->is_string_type())
7400 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7401 else if (arg_type
->array_type() != NULL
)
7402 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7403 else if (arg_type
->map_type() != NULL
)
7405 static tree map_len_fndecl
;
7406 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7411 arg_type
->get_tree(gogo
),
7414 else if (arg_type
->channel_type() != NULL
)
7416 static tree chan_len_fndecl
;
7417 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7422 arg_type
->get_tree(gogo
),
7430 if (arg_type
->array_type() != NULL
)
7431 val_tree
= arg_type
->array_type()->capacity_tree(gogo
, arg_tree
);
7432 else if (arg_type
->channel_type() != NULL
)
7434 static tree chan_cap_fndecl
;
7435 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7440 arg_type
->get_tree(gogo
),
7447 if (val_tree
== error_mark_node
)
7448 return error_mark_node
;
7450 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7451 if (type_tree
== TREE_TYPE(val_tree
))
7454 return fold(convert_to_integer(type_tree
, val_tree
));
7458 case BUILTIN_PRINTLN
:
7460 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7461 tree stmt_list
= NULL_TREE
;
7463 const Expression_list
* call_args
= this->args();
7464 if (call_args
!= NULL
)
7466 for (Expression_list::const_iterator p
= call_args
->begin();
7467 p
!= call_args
->end();
7470 if (is_ln
&& p
!= call_args
->begin())
7472 static tree print_space_fndecl
;
7473 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7478 if (call
== error_mark_node
)
7479 return error_mark_node
;
7480 append_to_statement_list(call
, &stmt_list
);
7483 Type
* type
= (*p
)->type();
7485 tree arg
= (*p
)->get_tree(context
);
7486 if (arg
== error_mark_node
)
7487 return error_mark_node
;
7491 if (type
->is_string_type())
7493 static tree print_string_fndecl
;
7494 pfndecl
= &print_string_fndecl
;
7495 fnname
= "__go_print_string";
7497 else if (type
->integer_type() != NULL
7498 && type
->integer_type()->is_unsigned())
7500 static tree print_uint64_fndecl
;
7501 pfndecl
= &print_uint64_fndecl
;
7502 fnname
= "__go_print_uint64";
7503 Type
* itype
= Type::lookup_integer_type("uint64");
7504 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7507 else if (type
->integer_type() != NULL
)
7509 static tree print_int64_fndecl
;
7510 pfndecl
= &print_int64_fndecl
;
7511 fnname
= "__go_print_int64";
7512 Type
* itype
= Type::lookup_integer_type("int64");
7513 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7516 else if (type
->float_type() != NULL
)
7518 static tree print_double_fndecl
;
7519 pfndecl
= &print_double_fndecl
;
7520 fnname
= "__go_print_double";
7521 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7523 else if (type
->complex_type() != NULL
)
7525 static tree print_complex_fndecl
;
7526 pfndecl
= &print_complex_fndecl
;
7527 fnname
= "__go_print_complex";
7528 arg
= fold_convert_loc(location
, complex_double_type_node
,
7531 else if (type
->is_boolean_type())
7533 static tree print_bool_fndecl
;
7534 pfndecl
= &print_bool_fndecl
;
7535 fnname
= "__go_print_bool";
7537 else if (type
->points_to() != NULL
7538 || type
->channel_type() != NULL
7539 || type
->map_type() != NULL
7540 || type
->function_type() != NULL
)
7542 static tree print_pointer_fndecl
;
7543 pfndecl
= &print_pointer_fndecl
;
7544 fnname
= "__go_print_pointer";
7545 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7547 else if (type
->interface_type() != NULL
)
7549 if (type
->interface_type()->is_empty())
7551 static tree print_empty_interface_fndecl
;
7552 pfndecl
= &print_empty_interface_fndecl
;
7553 fnname
= "__go_print_empty_interface";
7557 static tree print_interface_fndecl
;
7558 pfndecl
= &print_interface_fndecl
;
7559 fnname
= "__go_print_interface";
7562 else if (type
->is_open_array_type())
7564 static tree print_slice_fndecl
;
7565 pfndecl
= &print_slice_fndecl
;
7566 fnname
= "__go_print_slice";
7571 tree call
= Gogo::call_builtin(pfndecl
,
7578 if (call
== error_mark_node
)
7579 return error_mark_node
;
7580 append_to_statement_list(call
, &stmt_list
);
7586 static tree print_nl_fndecl
;
7587 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7592 if (call
== error_mark_node
)
7593 return error_mark_node
;
7594 append_to_statement_list(call
, &stmt_list
);
7602 const Expression_list
* args
= this->args();
7603 gcc_assert(args
!= NULL
&& args
->size() == 1);
7604 Expression
* arg
= args
->front();
7605 tree arg_tree
= arg
->get_tree(context
);
7606 if (arg_tree
== error_mark_node
)
7607 return error_mark_node
;
7608 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7609 arg_tree
= Expression::convert_for_assignment(context
, empty
,
7611 arg_tree
, location
);
7612 static tree panic_fndecl
;
7613 tree call
= Gogo::call_builtin(&panic_fndecl
,
7618 TREE_TYPE(arg_tree
),
7620 if (call
== error_mark_node
)
7621 return error_mark_node
;
7622 // This function will throw an exception.
7623 TREE_NOTHROW(panic_fndecl
) = 0;
7624 // This function will not return.
7625 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
7629 case BUILTIN_RECOVER
:
7631 // The argument is set when building recover thunks. It's a
7632 // boolean value which is true if we can recover a value now.
7633 const Expression_list
* args
= this->args();
7634 gcc_assert(args
!= NULL
&& args
->size() == 1);
7635 Expression
* arg
= args
->front();
7636 tree arg_tree
= arg
->get_tree(context
);
7637 if (arg_tree
== error_mark_node
)
7638 return error_mark_node
;
7640 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7641 tree empty_tree
= empty
->get_tree(context
->gogo());
7643 Type
* nil_type
= Type::make_nil_type();
7644 Expression
* nil
= Expression::make_nil(location
);
7645 tree nil_tree
= nil
->get_tree(context
);
7646 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
7652 // We need to handle a deferred call to recover specially,
7653 // because it changes whether it can recover a panic or not.
7654 // See test7 in test/recover1.go.
7656 if (this->is_deferred())
7658 static tree deferred_recover_fndecl
;
7659 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
7661 "__go_deferred_recover",
7667 static tree recover_fndecl
;
7668 call
= Gogo::call_builtin(&recover_fndecl
,
7674 if (call
== error_mark_node
)
7675 return error_mark_node
;
7676 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
7677 call
, empty_nil_tree
);
7681 case BUILTIN_CLOSED
:
7683 const Expression_list
* args
= this->args();
7684 gcc_assert(args
!= NULL
&& args
->size() == 1);
7685 Expression
* arg
= args
->front();
7686 tree arg_tree
= arg
->get_tree(context
);
7687 if (arg_tree
== error_mark_node
)
7688 return error_mark_node
;
7689 if (this->code_
== BUILTIN_CLOSE
)
7691 static tree close_fndecl
;
7692 return Gogo::call_builtin(&close_fndecl
,
7694 "__go_builtin_close",
7697 TREE_TYPE(arg_tree
),
7702 static tree closed_fndecl
;
7703 return Gogo::call_builtin(&closed_fndecl
,
7705 "__go_builtin_closed",
7708 TREE_TYPE(arg_tree
),
7713 case BUILTIN_SIZEOF
:
7714 case BUILTIN_OFFSETOF
:
7715 case BUILTIN_ALIGNOF
:
7720 bool b
= this->integer_constant_value(true, val
, &dummy
);
7722 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
7723 tree ret
= Expression::integer_constant_tree(val
, type
);
7730 const Expression_list
* args
= this->args();
7731 gcc_assert(args
!= NULL
&& args
->size() == 2);
7732 Expression
* arg1
= args
->front();
7733 Expression
* arg2
= args
->back();
7735 tree arg1_tree
= arg1
->get_tree(context
);
7736 tree arg2_tree
= arg2
->get_tree(context
);
7737 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7738 return error_mark_node
;
7740 Type
* arg1_type
= arg1
->type();
7741 Array_type
* at
= arg1_type
->array_type();
7742 arg1_tree
= save_expr(arg1_tree
);
7743 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
7744 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
7745 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
7746 return error_mark_node
;
7748 Type
* arg2_type
= arg2
->type();
7751 if (arg2_type
->is_open_array_type())
7753 at
= arg2_type
->array_type();
7754 arg2_tree
= save_expr(arg2_tree
);
7755 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7756 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7760 arg2_tree
= save_expr(arg2_tree
);
7761 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
7762 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
7764 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7765 return error_mark_node
;
7767 arg1_len
= save_expr(arg1_len
);
7768 arg2_len
= save_expr(arg2_len
);
7769 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
7770 fold_build2_loc(location
, LT_EXPR
,
7772 arg1_len
, arg2_len
),
7773 arg1_len
, arg2_len
);
7774 len
= save_expr(len
);
7776 Type
* element_type
= at
->element_type();
7777 tree element_type_tree
= element_type
->get_tree(gogo
);
7778 if (element_type_tree
== error_mark_node
)
7779 return error_mark_node
;
7780 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7781 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
7783 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
7784 TREE_TYPE(element_size
),
7785 bytecount
, element_size
);
7786 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
7788 tree call
= build_call_expr_loc(location
,
7789 built_in_decls
[BUILT_IN_MEMMOVE
],
7790 3, arg1_val
, arg2_val
, bytecount
);
7792 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
7796 case BUILTIN_APPEND
:
7798 const Expression_list
* args
= this->args();
7799 gcc_assert(args
!= NULL
&& args
->size() == 2);
7800 Expression
* arg1
= args
->front();
7801 Expression
* arg2
= args
->back();
7803 tree arg1_tree
= arg1
->get_tree(context
);
7804 tree arg2_tree
= arg2
->get_tree(context
);
7805 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7806 return error_mark_node
;
7808 tree descriptor_tree
= arg1
->type()->type_descriptor_pointer(gogo
);
7810 // We rebuild the decl each time since the slice types may
7812 tree append_fndecl
= NULL_TREE
;
7813 return Gogo::call_builtin(&append_fndecl
,
7817 TREE_TYPE(arg1_tree
),
7818 TREE_TYPE(descriptor_tree
),
7820 TREE_TYPE(arg1_tree
),
7822 TREE_TYPE(arg2_tree
),
7829 const Expression_list
* args
= this->args();
7830 gcc_assert(args
!= NULL
&& args
->size() == 1);
7831 Expression
* arg
= args
->front();
7832 tree arg_tree
= arg
->get_tree(context
);
7833 if (arg_tree
== error_mark_node
)
7834 return error_mark_node
;
7835 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
7836 if (this->code_
== BUILTIN_REAL
)
7837 return fold_build1_loc(location
, REALPART_EXPR
,
7838 TREE_TYPE(TREE_TYPE(arg_tree
)),
7841 return fold_build1_loc(location
, IMAGPART_EXPR
,
7842 TREE_TYPE(TREE_TYPE(arg_tree
)),
7848 const Expression_list
* args
= this->args();
7849 gcc_assert(args
!= NULL
&& args
->size() == 2);
7850 tree r
= args
->front()->get_tree(context
);
7851 tree i
= args
->back()->get_tree(context
);
7852 if (r
== error_mark_node
|| i
== error_mark_node
)
7853 return error_mark_node
;
7854 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
7855 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
7856 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
7857 return fold_build2_loc(location
, COMPLEX_EXPR
,
7858 build_complex_type(TREE_TYPE(r
)),
7867 // We have to support exporting a builtin call expression, because
7868 // code can set a constant to the result of a builtin expression.
7871 Builtin_call_expression::do_export(Export
* exp
) const
7878 if (this->integer_constant_value(true, val
, &dummy
))
7880 Integer_expression::export_integer(exp
, val
);
7889 if (this->float_constant_value(fval
, &dummy
))
7891 Float_expression::export_float(exp
, fval
);
7903 if (this->complex_constant_value(real
, imag
, &dummy
))
7905 Complex_expression::export_complex(exp
, real
, imag
);
7914 error_at(this->location(), "value is not constant");
7918 // A trailing space lets us reliably identify the end of the number.
7919 exp
->write_c_string(" ");
7922 // Class Call_expression.
7927 Call_expression::do_traverse(Traverse
* traverse
)
7929 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
7930 return TRAVERSE_EXIT
;
7931 if (this->args_
!= NULL
)
7933 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
7934 return TRAVERSE_EXIT
;
7936 return TRAVERSE_CONTINUE
;
7939 // Lower a call statement.
7942 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
7944 // A type case can look like a function call.
7945 if (this->fn_
->is_type_expression()
7946 && this->args_
!= NULL
7947 && this->args_
->size() == 1)
7948 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
7951 // Recognize a call to a builtin function.
7952 Func_expression
* fne
= this->fn_
->func_expression();
7954 && fne
->named_object()->is_function_declaration()
7955 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
7956 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
7957 this->is_varargs_
, this->location());
7959 // Handle an argument which is a call to a function which returns
7960 // multiple results.
7961 if (this->args_
!= NULL
7962 && this->args_
->size() == 1
7963 && this->args_
->front()->call_expression() != NULL
7964 && this->fn_
->type()->function_type() != NULL
)
7966 Function_type
* fntype
= this->fn_
->type()->function_type();
7967 size_t rc
= this->args_
->front()->call_expression()->result_count();
7969 && fntype
->parameters() != NULL
7970 && (fntype
->parameters()->size() == rc
7971 || (fntype
->is_varargs()
7972 && fntype
->parameters()->size() - 1 <= rc
)))
7974 Call_expression
* call
= this->args_
->front()->call_expression();
7975 Expression_list
* args
= new Expression_list
;
7976 for (size_t i
= 0; i
< rc
; ++i
)
7977 args
->push_back(Expression::make_call_result(call
, i
));
7978 // We can't return a new call expression here, because this
7979 // one may be referenced by Call_result expressions. FIXME.
7985 // Handle a call to a varargs function by packaging up the extra
7987 if (this->fn_
->type()->function_type() != NULL
7988 && this->fn_
->type()->function_type()->is_varargs())
7990 Function_type
* fntype
= this->fn_
->type()->function_type();
7991 const Typed_identifier_list
* parameters
= fntype
->parameters();
7992 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
7993 Type
* varargs_type
= parameters
->back().type();
7994 return this->lower_varargs(gogo
, function
, varargs_type
,
7995 parameters
->size());
8001 // Lower a call to a varargs function. FUNCTION is the function in
8002 // which the call occurs--it's not the function we are calling.
8003 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8004 // PARAM_COUNT is the number of parameters of the function we are
8005 // calling; the last of these parameters will be the varargs
8009 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8010 Type
* varargs_type
, size_t param_count
)
8012 if (this->varargs_are_lowered_
)
8015 source_location loc
= this->location();
8017 gcc_assert(param_count
> 0);
8018 gcc_assert(varargs_type
->is_open_array_type());
8020 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8021 if (arg_count
< param_count
- 1)
8023 // Not enough arguments; will be caught in check_types.
8027 Expression_list
* old_args
= this->args_
;
8028 Expression_list
* new_args
= new Expression_list();
8029 bool push_empty_arg
= false;
8030 if (old_args
== NULL
|| old_args
->empty())
8032 gcc_assert(param_count
== 1);
8033 push_empty_arg
= true;
8037 Expression_list::const_iterator pa
;
8039 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8041 if (static_cast<size_t>(i
) == param_count
)
8043 new_args
->push_back(*pa
);
8046 // We have reached the varargs parameter.
8048 bool issued_error
= false;
8049 if (pa
== old_args
->end())
8050 push_empty_arg
= true;
8051 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8052 new_args
->push_back(*pa
);
8053 else if (this->is_varargs_
)
8055 this->report_error(_("too many arguments"));
8058 else if (pa
+ 1 == old_args
->end()
8059 && this->is_compatible_varargs_argument(function
, *pa
,
8062 new_args
->push_back(*pa
);
8065 Type
* element_type
= varargs_type
->array_type()->element_type();
8066 Expression_list
* vals
= new Expression_list
;
8067 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8069 // Check types here so that we get a better message.
8070 Type
* patype
= (*pa
)->type();
8071 source_location paloc
= (*pa
)->location();
8072 if (!this->check_argument_type(i
, element_type
, patype
,
8073 paloc
, issued_error
))
8075 vals
->push_back(*pa
);
8078 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8079 new_args
->push_back(val
);
8084 new_args
->push_back(Expression::make_nil(loc
));
8086 // We can't return a new call expression here, because this one may
8087 // be referenced by Call_result expressions. FIXME.
8088 if (old_args
!= NULL
)
8090 this->args_
= new_args
;
8091 this->varargs_are_lowered_
= true;
8093 // Lower all the new subexpressions.
8094 Expression
* ret
= this;
8095 gogo
->lower_expression(function
, &ret
);
8096 gcc_assert(ret
== this);
8100 // Return true if ARG is a varargs argment which should be passed to
8101 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8102 // will be the last argument passed in the call, and PARAM_TYPE will
8103 // be the type of the last parameter of the varargs function being
8107 Call_expression::is_compatible_varargs_argument(Named_object
* function
,
8112 *issued_error
= false;
8114 Type
* var_type
= NULL
;
8116 // The simple case is passing the varargs parameter of the caller.
8117 Var_expression
* ve
= arg
->var_expression();
8118 if (ve
!= NULL
&& ve
->named_object()->is_variable())
8120 Variable
* var
= ve
->named_object()->var_value();
8121 if (var
->is_varargs_parameter())
8122 var_type
= var
->type();
8125 // The complex case is passing the varargs parameter of some
8126 // enclosing function. This will look like passing down *c.f where
8127 // c is the closure variable and f is a field in the closure.
8128 if (function
!= NULL
8129 && function
->func_value()->needs_closure()
8130 && arg
->classification() == EXPRESSION_UNARY
)
8132 Unary_expression
* ue
= static_cast<Unary_expression
*>(arg
);
8133 if (ue
->op() == OPERATOR_MULT
)
8135 Field_reference_expression
* fre
=
8136 ue
->operand()->deref()->field_reference_expression();
8139 Var_expression
* ve
= fre
->expr()->deref()->var_expression();
8142 Named_object
* no
= ve
->named_object();
8143 Function
* f
= function
->func_value();
8144 if (no
== f
->closure_var())
8146 // At this point we know that this indeed a
8147 // reference to some enclosing variable. Now we
8148 // need to figure out whether that variable is a
8149 // varargs parameter.
8150 Named_object
* enclosing
=
8151 f
->enclosing_var(fre
->field_index());
8152 Variable
* var
= enclosing
->var_value();
8153 if (var
->is_varargs_parameter())
8154 var_type
= var
->type();
8161 if (var_type
== NULL
)
8164 // We only match if the parameter is the same, with an identical
8166 Array_type
* var_at
= var_type
->array_type();
8167 gcc_assert(var_at
!= NULL
);
8168 Array_type
* param_at
= param_type
->array_type();
8169 if (param_at
!= NULL
8170 && Type::are_identical(var_at
->element_type(),
8171 param_at
->element_type(), true, NULL
))
8173 error_at(arg
->location(), "... mismatch: passing ...T as ...");
8174 *issued_error
= true;
8178 // Get the function type. Returns NULL if we don't know the type. If
8179 // this returns NULL, and if_ERROR is true, issues an error.
8182 Call_expression::get_function_type() const
8184 return this->fn_
->type()->function_type();
8187 // Return the number of values which this call will return.
8190 Call_expression::result_count() const
8192 const Function_type
* fntype
= this->get_function_type();
8195 if (fntype
->results() == NULL
)
8197 return fntype
->results()->size();
8200 // Return whether this is a call to the predeclared function recover.
8203 Call_expression::is_recover_call() const
8205 return this->do_is_recover_call();
8208 // Set the argument to the recover function.
8211 Call_expression::set_recover_arg(Expression
* arg
)
8213 this->do_set_recover_arg(arg
);
8216 // Virtual functions also implemented by Builtin_call_expression.
8219 Call_expression::do_is_recover_call() const
8225 Call_expression::do_set_recover_arg(Expression
*)
8233 Call_expression::do_type()
8235 if (this->type_
!= NULL
)
8239 Function_type
* fntype
= this->get_function_type();
8241 return Type::make_error_type();
8243 const Typed_identifier_list
* results
= fntype
->results();
8244 if (results
== NULL
)
8245 ret
= Type::make_void_type();
8246 else if (results
->size() == 1)
8247 ret
= results
->begin()->type();
8249 ret
= Type::make_call_multiple_result_type(this);
8256 // Determine types for a call expression. We can use the function
8257 // parameter types to set the types of the arguments.
8260 Call_expression::do_determine_type(const Type_context
*)
8262 this->fn_
->determine_type_no_context();
8263 Function_type
* fntype
= this->get_function_type();
8264 const Typed_identifier_list
* parameters
= NULL
;
8266 parameters
= fntype
->parameters();
8267 if (this->args_
!= NULL
)
8269 Typed_identifier_list::const_iterator pt
;
8270 if (parameters
!= NULL
)
8271 pt
= parameters
->begin();
8272 for (Expression_list::const_iterator pa
= this->args_
->begin();
8273 pa
!= this->args_
->end();
8276 if (parameters
!= NULL
&& pt
!= parameters
->end())
8278 Type_context
subcontext(pt
->type(), false);
8279 (*pa
)->determine_type(&subcontext
);
8283 (*pa
)->determine_type_no_context();
8288 // Check types for parameter I.
8291 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8292 const Type
* argument_type
,
8293 source_location argument_location
,
8297 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8302 error_at(argument_location
, "argument %d has incompatible type", i
);
8304 error_at(argument_location
,
8305 "argument %d has incompatible type (%s)",
8308 this->set_is_error();
8317 Call_expression::do_check_types(Gogo
*)
8319 Function_type
* fntype
= this->get_function_type();
8322 if (!this->fn_
->type()->is_error_type())
8323 this->report_error(_("expected function"));
8327 if (fntype
->is_method())
8329 // We don't support pointers to methods, so the function has to
8330 // be a bound method expression.
8331 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8334 this->report_error(_("method call without object"));
8337 Type
* first_arg_type
= bme
->first_argument()->type();
8338 if (first_arg_type
->points_to() == NULL
)
8340 // When passing a value, we need to check that we are
8341 // permitted to copy it.
8343 if (!Type::are_assignable(fntype
->receiver()->type(),
8344 first_arg_type
, &reason
))
8347 this->report_error(_("incompatible type for receiver"));
8350 error_at(this->location(),
8351 "incompatible type for receiver (%s)",
8353 this->set_is_error();
8359 // Note that varargs was handled by the lower_varargs() method, so
8360 // we don't have to worry about it here.
8362 const Typed_identifier_list
* parameters
= fntype
->parameters();
8363 if (this->args_
== NULL
)
8365 if (parameters
!= NULL
&& !parameters
->empty())
8366 this->report_error(_("not enough arguments"));
8368 else if (parameters
== NULL
)
8369 this->report_error(_("too many arguments"));
8373 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8374 for (Expression_list::const_iterator pa
= this->args_
->begin();
8375 pa
!= this->args_
->end();
8378 if (pt
== parameters
->end())
8380 this->report_error(_("too many arguments"));
8383 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8384 (*pa
)->location(), false);
8386 if (pt
!= parameters
->end())
8387 this->report_error(_("not enough arguments"));
8391 // Return whether we have to use a temporary variable to ensure that
8392 // we evaluate this call expression in order. If the call returns no
8393 // results then it will inevitably be executed last. If the call
8394 // returns more than one result then it will be used with Call_result
8395 // expressions. So we only have to use a temporary variable if the
8396 // call returns exactly one result.
8399 Call_expression::do_must_eval_in_order() const
8401 return this->result_count() == 1;
8404 // Get the function and the first argument to use when calling a bound
8408 Call_expression::bound_method_function(Translate_context
* context
,
8409 Bound_method_expression
* bound_method
,
8410 tree
* first_arg_ptr
)
8412 Expression
* first_argument
= bound_method
->first_argument();
8413 tree first_arg
= first_argument
->get_tree(context
);
8414 if (first_arg
== error_mark_node
)
8415 return error_mark_node
;
8417 // We always pass a pointer to the first argument when calling a
8419 if (first_argument
->type()->points_to() == NULL
)
8421 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8422 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8423 || DECL_P(first_arg
)
8424 || TREE_CODE(first_arg
) == INDIRECT_REF
8425 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8427 first_arg
= build_fold_addr_expr(first_arg
);
8428 if (DECL_P(first_arg
))
8429 TREE_ADDRESSABLE(first_arg
) = 1;
8433 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8434 get_name(first_arg
));
8435 DECL_IGNORED_P(tmp
) = 0;
8436 DECL_INITIAL(tmp
) = first_arg
;
8437 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8438 build1(DECL_EXPR
, void_type_node
, tmp
),
8439 build_fold_addr_expr(tmp
));
8440 TREE_ADDRESSABLE(tmp
) = 1;
8442 if (first_arg
== error_mark_node
)
8443 return error_mark_node
;
8446 Type
* fatype
= bound_method
->first_argument_type();
8449 if (fatype
->points_to() == NULL
)
8450 fatype
= Type::make_pointer_type(fatype
);
8451 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8452 if (first_arg
== error_mark_node
8453 || TREE_TYPE(first_arg
) == error_mark_node
)
8454 return error_mark_node
;
8457 *first_arg_ptr
= first_arg
;
8459 return bound_method
->method()->get_tree(context
);
8462 // Get the function and the first argument to use when calling an
8463 // interface method.
8466 Call_expression::interface_method_function(
8467 Translate_context
* context
,
8468 Interface_field_reference_expression
* interface_method
,
8469 tree
* first_arg_ptr
)
8471 tree expr
= interface_method
->expr()->get_tree(context
);
8472 if (expr
== error_mark_node
)
8473 return error_mark_node
;
8474 expr
= save_expr(expr
);
8475 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8476 if (first_arg
== error_mark_node
)
8477 return error_mark_node
;
8478 *first_arg_ptr
= first_arg
;
8479 return interface_method
->get_function_tree(context
, expr
);
8482 // Build the call expression.
8485 Call_expression::do_get_tree(Translate_context
* context
)
8487 if (this->tree_
!= NULL_TREE
)
8490 Function_type
* fntype
= this->get_function_type();
8492 return error_mark_node
;
8494 if (this->fn_
->is_error_expression())
8495 return error_mark_node
;
8497 Gogo
* gogo
= context
->gogo();
8498 source_location location
= this->location();
8500 Func_expression
* func
= this->fn_
->func_expression();
8501 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8502 Interface_field_reference_expression
* interface_method
=
8503 this->fn_
->interface_field_reference_expression();
8504 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8505 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8506 gcc_assert(!fntype
->is_method() || is_method
);
8510 if (this->args_
== NULL
|| this->args_
->empty())
8512 nargs
= is_method
? 1 : 0;
8513 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8517 const Typed_identifier_list
* params
= fntype
->parameters();
8518 gcc_assert(params
!= NULL
);
8520 nargs
= this->args_
->size();
8521 int i
= is_method
? 1 : 0;
8523 args
= new tree
[nargs
];
8525 Typed_identifier_list::const_iterator pp
= params
->begin();
8526 Expression_list::const_iterator pe
;
8527 for (pe
= this->args_
->begin();
8528 pe
!= this->args_
->end();
8531 tree arg_val
= (*pe
)->get_tree(context
);
8532 args
[i
] = Expression::convert_for_assignment(context
,
8537 if (args
[i
] == error_mark_node
)
8538 return error_mark_node
;
8540 gcc_assert(pp
== params
->end());
8541 gcc_assert(i
== nargs
);
8544 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8545 if (rettype
== error_mark_node
)
8546 return error_mark_node
;
8550 fn
= func
->get_tree_without_closure(gogo
);
8551 else if (!is_method
)
8552 fn
= this->fn_
->get_tree(context
);
8553 else if (bound_method
!= NULL
)
8554 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8555 else if (interface_method
!= NULL
)
8556 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8560 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8561 return error_mark_node
;
8563 // This is to support builtin math functions when using 80387 math.
8565 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8566 fndecl
= TREE_OPERAND(fndecl
, 0);
8567 tree excess_type
= NULL_TREE
;
8569 && DECL_IS_BUILTIN(fndecl
)
8570 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8572 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8573 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8574 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8575 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8577 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8578 if (excess_type
!= NULL_TREE
)
8580 tree excess_fndecl
= mathfn_built_in(excess_type
,
8581 DECL_FUNCTION_CODE(fndecl
));
8582 if (excess_fndecl
== NULL_TREE
)
8583 excess_type
= NULL_TREE
;
8586 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8587 for (int i
= 0; i
< nargs
; ++i
)
8588 args
[i
] = ::convert(excess_type
, args
[i
]);
8593 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8597 SET_EXPR_LOCATION(ret
, location
);
8601 tree closure_tree
= func
->closure()->get_tree(context
);
8602 if (closure_tree
!= error_mark_node
)
8603 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8606 // If this is a recursive function type which returns itself, as in
8608 // we have used ptr_type_node for the return type. Add a cast here
8609 // to the correct type.
8610 if (TREE_TYPE(ret
) == ptr_type_node
)
8612 tree t
= this->type()->get_tree(gogo
);
8613 ret
= fold_convert_loc(location
, t
, ret
);
8616 if (excess_type
!= NULL_TREE
)
8618 // Calling convert here can undo our excess precision change.
8619 // That may or may not be a bug in convert_to_real.
8620 ret
= build1(NOP_EXPR
, rettype
, ret
);
8623 // If there is more than one result, we will refer to the call
8625 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
8626 ret
= save_expr(ret
);
8633 // Make a call expression.
8636 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
8637 source_location location
)
8639 return new Call_expression(fn
, args
, is_varargs
, location
);
8642 // A single result from a call which returns multiple results.
8644 class Call_result_expression
: public Expression
8647 Call_result_expression(Call_expression
* call
, unsigned int index
)
8648 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
8649 call_(call
), index_(index
)
8654 do_traverse(Traverse
*);
8660 do_determine_type(const Type_context
*);
8663 do_check_types(Gogo
*);
8668 return new Call_result_expression(this->call_
->call_expression(),
8673 do_must_eval_in_order() const
8677 do_get_tree(Translate_context
*);
8680 // The underlying call expression.
8682 // Which result we want.
8683 unsigned int index_
;
8686 // Traverse a call result.
8689 Call_result_expression::do_traverse(Traverse
* traverse
)
8691 if (traverse
->remember_expression(this->call_
))
8693 // We have already traversed the call expression.
8694 return TRAVERSE_CONTINUE
;
8696 return Expression::traverse(&this->call_
, traverse
);
8702 Call_result_expression::do_type()
8704 if (this->classification() == EXPRESSION_ERROR
)
8705 return Type::make_error_type();
8707 // THIS->CALL_ can be replaced with a temporary reference due to
8708 // Call_expression::do_must_eval_in_order when there is an error.
8709 Call_expression
* ce
= this->call_
->call_expression();
8711 return Type::make_error_type();
8712 Function_type
* fntype
= ce
->get_function_type();
8714 return Type::make_error_type();
8715 const Typed_identifier_list
* results
= fntype
->results();
8716 Typed_identifier_list::const_iterator pr
= results
->begin();
8717 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8719 if (pr
== results
->end())
8723 if (pr
== results
->end())
8725 this->report_error(_("number of results does not match "
8726 "number of values"));
8727 return Type::make_error_type();
8732 // Check the type. Just make sure that we trigger the warning in
8736 Call_result_expression::do_check_types(Gogo
*)
8741 // Determine the type. We have nothing to do here, but the 0 result
8742 // needs to pass down to the caller.
8745 Call_result_expression::do_determine_type(const Type_context
*)
8747 if (this->index_
== 0)
8748 this->call_
->determine_type_no_context();
8754 Call_result_expression::do_get_tree(Translate_context
* context
)
8756 tree call_tree
= this->call_
->get_tree(context
);
8757 if (call_tree
== error_mark_node
)
8758 return error_mark_node
;
8759 gcc_assert(TREE_CODE(TREE_TYPE(call_tree
)) == RECORD_TYPE
);
8760 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
8761 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8763 gcc_assert(field
!= NULL_TREE
);
8764 field
= DECL_CHAIN(field
);
8766 gcc_assert(field
!= NULL_TREE
);
8767 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
8770 // Make a reference to a single result of a call which returns
8771 // multiple results.
8774 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
8776 return new Call_result_expression(call
, index
);
8779 // Class Index_expression.
8784 Index_expression::do_traverse(Traverse
* traverse
)
8786 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
8787 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
8788 || (this->end_
!= NULL
8789 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
8790 return TRAVERSE_EXIT
;
8791 return TRAVERSE_CONTINUE
;
8794 // Lower an index expression. This converts the generic index
8795 // expression into an array index, a string index, or a map index.
8798 Index_expression::do_lower(Gogo
*, Named_object
*, int)
8800 source_location location
= this->location();
8801 Expression
* left
= this->left_
;
8802 Expression
* start
= this->start_
;
8803 Expression
* end
= this->end_
;
8805 Type
* type
= left
->type();
8806 if (type
->is_error_type())
8807 return Expression::make_error(location
);
8808 else if (type
->array_type() != NULL
)
8809 return Expression::make_array_index(left
, start
, end
, location
);
8810 else if (type
->points_to() != NULL
8811 && type
->points_to()->array_type() != NULL
8812 && !type
->points_to()->is_open_array_type())
8814 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
8816 return Expression::make_array_index(deref
, start
, end
, location
);
8818 else if (type
->is_string_type())
8819 return Expression::make_string_index(left
, start
, end
, location
);
8820 else if (type
->map_type() != NULL
)
8824 error_at(location
, "invalid slice of map");
8825 return Expression::make_error(location
);
8827 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
8829 if (this->is_lvalue_
)
8830 ret
->set_is_lvalue();
8836 "attempt to index object which is not array, string, or map");
8837 return Expression::make_error(location
);
8841 // Make an index expression.
8844 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
8845 source_location location
)
8847 return new Index_expression(left
, start
, end
, location
);
8850 // An array index. This is used for both indexing and slicing.
8852 class Array_index_expression
: public Expression
8855 Array_index_expression(Expression
* array
, Expression
* start
,
8856 Expression
* end
, source_location location
)
8857 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
8858 array_(array
), start_(start
), end_(end
), type_(NULL
)
8863 do_traverse(Traverse
*);
8869 do_determine_type(const Type_context
*);
8872 do_check_types(Gogo
*);
8877 return Expression::make_array_index(this->array_
->copy(),
8878 this->start_
->copy(),
8881 : this->end_
->copy()),
8886 do_is_addressable() const;
8889 do_address_taken(bool escapes
)
8890 { this->array_
->address_taken(escapes
); }
8893 do_get_tree(Translate_context
*);
8896 // The array we are getting a value from.
8898 // The start or only index.
8900 // The end index of a slice. This may be NULL for a simple array
8901 // index, or it may be a nil expression for the length of the array.
8903 // The type of the expression.
8907 // Array index traversal.
8910 Array_index_expression::do_traverse(Traverse
* traverse
)
8912 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
8913 return TRAVERSE_EXIT
;
8914 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
8915 return TRAVERSE_EXIT
;
8916 if (this->end_
!= NULL
)
8918 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
8919 return TRAVERSE_EXIT
;
8921 return TRAVERSE_CONTINUE
;
8924 // Return the type of an array index.
8927 Array_index_expression::do_type()
8929 if (this->type_
== NULL
)
8931 Array_type
* type
= this->array_
->type()->array_type();
8933 this->type_
= Type::make_error_type();
8934 else if (this->end_
== NULL
)
8935 this->type_
= type
->element_type();
8936 else if (type
->is_open_array_type())
8938 // A slice of a slice has the same type as the original
8940 this->type_
= this->array_
->type()->deref();
8944 // A slice of an array is a slice.
8945 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
8951 // Set the type of an array index.
8954 Array_index_expression::do_determine_type(const Type_context
*)
8956 this->array_
->determine_type_no_context();
8957 Type_context
subcontext(NULL
, true);
8958 this->start_
->determine_type(&subcontext
);
8959 if (this->end_
!= NULL
)
8960 this->end_
->determine_type(&subcontext
);
8963 // Check types of an array index.
8966 Array_index_expression::do_check_types(Gogo
*)
8968 if (this->start_
->type()->integer_type() == NULL
)
8969 this->report_error(_("index must be integer"));
8970 if (this->end_
!= NULL
8971 && this->end_
->type()->integer_type() == NULL
8972 && !this->end_
->is_nil_expression())
8973 this->report_error(_("slice end must be integer"));
8975 Array_type
* array_type
= this->array_
->type()->array_type();
8976 gcc_assert(array_type
!= NULL
);
8978 unsigned int int_bits
=
8979 Type::lookup_integer_type("int")->integer_type()->bits();
8984 bool lval_valid
= (array_type
->length() != NULL
8985 && array_type
->length()->integer_constant_value(true,
8990 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
8992 if (mpz_sgn(ival
) < 0
8993 || mpz_sizeinbase(ival
, 2) >= int_bits
8995 && (this->end_
== NULL
8996 ? mpz_cmp(ival
, lval
) >= 0
8997 : mpz_cmp(ival
, lval
) > 0)))
8999 error_at(this->start_
->location(), "array index out of bounds");
9000 this->set_is_error();
9003 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9005 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9007 if (mpz_sgn(ival
) < 0
9008 || mpz_sizeinbase(ival
, 2) >= int_bits
9009 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
9011 error_at(this->end_
->location(), "array index out of bounds");
9012 this->set_is_error();
9019 // A slice of an array requires an addressable array. A slice of a
9020 // slice is always possible.
9021 if (this->end_
!= NULL
9022 && !array_type
->is_open_array_type()
9023 && !this->array_
->is_addressable())
9024 this->report_error(_("array is not addressable"));
9027 // Return whether this expression is addressable.
9030 Array_index_expression::do_is_addressable() const
9032 // A slice expression is not addressable.
9033 if (this->end_
!= NULL
)
9036 // An index into a slice is addressable.
9037 if (this->array_
->type()->is_open_array_type())
9040 // An index into an array is addressable if the array is
9042 return this->array_
->is_addressable();
9045 // Get a tree for an array index.
9048 Array_index_expression::do_get_tree(Translate_context
* context
)
9050 Gogo
* gogo
= context
->gogo();
9051 source_location loc
= this->location();
9053 Array_type
* array_type
= this->array_
->type()->array_type();
9054 gcc_assert(array_type
!= NULL
);
9056 tree type_tree
= array_type
->get_tree(gogo
);
9057 if (type_tree
== error_mark_node
)
9058 return error_mark_node
;
9060 tree array_tree
= this->array_
->get_tree(context
);
9061 if (array_tree
== error_mark_node
)
9062 return error_mark_node
;
9064 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9065 array_tree
= save_expr(array_tree
);
9066 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
9067 if (length_tree
== error_mark_node
)
9068 return error_mark_node
;
9069 length_tree
= save_expr(length_tree
);
9070 tree length_type
= TREE_TYPE(length_tree
);
9072 tree bad_index
= boolean_false_node
;
9074 tree start_tree
= this->start_
->get_tree(context
);
9075 if (start_tree
== error_mark_node
)
9076 return error_mark_node
;
9077 if (!DECL_P(start_tree
))
9078 start_tree
= save_expr(start_tree
);
9079 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9080 start_tree
= convert_to_integer(length_type
, start_tree
);
9082 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9085 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9086 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9087 fold_build2_loc(loc
,
9091 boolean_type_node
, start_tree
,
9094 int code
= (array_type
->length() != NULL
9095 ? (this->end_
== NULL
9096 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9097 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9098 : (this->end_
== NULL
9099 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9100 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9101 tree crash
= Gogo::runtime_error(code
, loc
);
9103 if (this->end_
== NULL
)
9105 // Simple array indexing. This has to return an l-value, so
9106 // wrap the index check into START_TREE.
9107 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9108 build3(COND_EXPR
, void_type_node
,
9109 bad_index
, crash
, NULL_TREE
),
9111 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9113 if (array_type
->length() != NULL
)
9116 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9117 start_tree
, NULL_TREE
, NULL_TREE
);
9122 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9123 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9124 if (element_type_tree
== error_mark_node
)
9125 return error_mark_node
;
9126 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9127 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9128 start_tree
, element_size
);
9129 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9130 TREE_TYPE(values
), values
, offset
);
9131 return build_fold_indirect_ref(ptr
);
9137 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9138 if (capacity_tree
== error_mark_node
)
9139 return error_mark_node
;
9140 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9143 if (this->end_
->is_nil_expression())
9144 end_tree
= length_tree
;
9147 end_tree
= this->end_
->get_tree(context
);
9148 if (end_tree
== error_mark_node
)
9149 return error_mark_node
;
9150 if (!DECL_P(end_tree
))
9151 end_tree
= save_expr(end_tree
);
9152 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9153 end_tree
= convert_to_integer(length_type
, end_tree
);
9155 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9158 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9160 capacity_tree
= save_expr(capacity_tree
);
9161 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9162 fold_build2_loc(loc
, LT_EXPR
,
9164 end_tree
, start_tree
),
9165 fold_build2_loc(loc
, GT_EXPR
,
9167 end_tree
, capacity_tree
));
9168 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9169 bad_index
, bad_end
);
9172 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9173 if (element_type_tree
== error_mark_node
)
9174 return error_mark_node
;
9175 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9177 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9178 fold_convert_loc(loc
, sizetype
, start_tree
),
9181 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9182 if (value_pointer
== error_mark_node
)
9183 return error_mark_node
;
9185 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9186 TREE_TYPE(value_pointer
),
9187 value_pointer
, offset
);
9189 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9190 end_tree
, start_tree
);
9192 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9193 capacity_tree
, start_tree
);
9195 tree struct_tree
= this->type()->get_tree(gogo
);
9196 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9198 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9200 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9201 tree field
= TYPE_FIELDS(struct_tree
);
9202 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9204 elt
->value
= value_pointer
;
9206 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9207 field
= DECL_CHAIN(field
);
9208 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9210 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9212 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9213 field
= DECL_CHAIN(field
);
9214 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9216 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9218 tree constructor
= build_constructor(struct_tree
, init
);
9220 if (TREE_CONSTANT(value_pointer
)
9221 && TREE_CONSTANT(result_length_tree
)
9222 && TREE_CONSTANT(result_capacity_tree
))
9223 TREE_CONSTANT(constructor
) = 1;
9225 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9226 build3(COND_EXPR
, void_type_node
,
9227 bad_index
, crash
, NULL_TREE
),
9231 // Make an array index expression. END may be NULL.
9234 Expression::make_array_index(Expression
* array
, Expression
* start
,
9235 Expression
* end
, source_location location
)
9237 // Taking a slice of a composite literal requires moving the literal
9239 if (end
!= NULL
&& array
->is_composite_literal())
9241 array
= Expression::make_heap_composite(array
, location
);
9242 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9244 return new Array_index_expression(array
, start
, end
, location
);
9247 // A string index. This is used for both indexing and slicing.
9249 class String_index_expression
: public Expression
9252 String_index_expression(Expression
* string
, Expression
* start
,
9253 Expression
* end
, source_location location
)
9254 : Expression(EXPRESSION_STRING_INDEX
, location
),
9255 string_(string
), start_(start
), end_(end
)
9260 do_traverse(Traverse
*);
9266 do_determine_type(const Type_context
*);
9269 do_check_types(Gogo
*);
9274 return Expression::make_string_index(this->string_
->copy(),
9275 this->start_
->copy(),
9278 : this->end_
->copy()),
9283 do_get_tree(Translate_context
*);
9286 // The string we are getting a value from.
9287 Expression
* string_
;
9288 // The start or only index.
9290 // The end index of a slice. This may be NULL for a single index,
9291 // or it may be a nil expression for the length of the string.
9295 // String index traversal.
9298 String_index_expression::do_traverse(Traverse
* traverse
)
9300 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9301 return TRAVERSE_EXIT
;
9302 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9303 return TRAVERSE_EXIT
;
9304 if (this->end_
!= NULL
)
9306 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9307 return TRAVERSE_EXIT
;
9309 return TRAVERSE_CONTINUE
;
9312 // Return the type of a string index.
9315 String_index_expression::do_type()
9317 if (this->end_
== NULL
)
9318 return Type::lookup_integer_type("uint8");
9320 return Type::make_string_type();
9323 // Determine the type of a string index.
9326 String_index_expression::do_determine_type(const Type_context
*)
9328 this->string_
->determine_type_no_context();
9329 Type_context
subcontext(NULL
, true);
9330 this->start_
->determine_type(&subcontext
);
9331 if (this->end_
!= NULL
)
9332 this->end_
->determine_type(&subcontext
);
9335 // Check types of a string index.
9338 String_index_expression::do_check_types(Gogo
*)
9340 if (this->start_
->type()->integer_type() == NULL
)
9341 this->report_error(_("index must be integer"));
9342 if (this->end_
!= NULL
9343 && this->end_
->type()->integer_type() == NULL
9344 && !this->end_
->is_nil_expression())
9345 this->report_error(_("slice end must be integer"));
9348 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9353 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9355 if (mpz_sgn(ival
) < 0
9356 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9358 error_at(this->start_
->location(), "string index out of bounds");
9359 this->set_is_error();
9362 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9364 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9366 if (mpz_sgn(ival
) < 0
9367 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9369 error_at(this->end_
->location(), "string index out of bounds");
9370 this->set_is_error();
9377 // Get a tree for a string index.
9380 String_index_expression::do_get_tree(Translate_context
* context
)
9382 source_location loc
= this->location();
9384 tree string_tree
= this->string_
->get_tree(context
);
9385 if (string_tree
== error_mark_node
)
9386 return error_mark_node
;
9388 if (this->string_
->type()->points_to() != NULL
)
9389 string_tree
= build_fold_indirect_ref(string_tree
);
9390 if (!DECL_P(string_tree
))
9391 string_tree
= save_expr(string_tree
);
9392 tree string_type
= TREE_TYPE(string_tree
);
9394 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9395 length_tree
= save_expr(length_tree
);
9396 tree length_type
= TREE_TYPE(length_tree
);
9398 tree bad_index
= boolean_false_node
;
9400 tree start_tree
= this->start_
->get_tree(context
);
9401 if (start_tree
== error_mark_node
)
9402 return error_mark_node
;
9403 if (!DECL_P(start_tree
))
9404 start_tree
= save_expr(start_tree
);
9405 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9406 start_tree
= convert_to_integer(length_type
, start_tree
);
9408 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9411 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9413 int code
= (this->end_
== NULL
9414 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9415 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9416 tree crash
= Gogo::runtime_error(code
, loc
);
9418 if (this->end_
== NULL
)
9420 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9422 fold_build2_loc(loc
, GE_EXPR
,
9424 start_tree
, length_tree
));
9426 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9427 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9429 fold_convert_loc(loc
, sizetype
, start_tree
));
9430 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9432 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9433 build3(COND_EXPR
, void_type_node
,
9434 bad_index
, crash
, NULL_TREE
),
9440 if (this->end_
->is_nil_expression())
9441 end_tree
= build_int_cst(length_type
, -1);
9444 end_tree
= this->end_
->get_tree(context
);
9445 if (end_tree
== error_mark_node
)
9446 return error_mark_node
;
9447 if (!DECL_P(end_tree
))
9448 end_tree
= save_expr(end_tree
);
9449 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9450 end_tree
= convert_to_integer(length_type
, end_tree
);
9452 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9455 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9458 static tree strslice_fndecl
;
9459 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9461 "__go_string_slice",
9470 if (ret
== error_mark_node
)
9471 return error_mark_node
;
9472 // This will panic if the bounds are out of range for the
9474 TREE_NOTHROW(strslice_fndecl
) = 0;
9476 if (bad_index
== boolean_false_node
)
9479 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9480 build3(COND_EXPR
, void_type_node
,
9481 bad_index
, crash
, NULL_TREE
),
9486 // Make a string index expression. END may be NULL.
9489 Expression::make_string_index(Expression
* string
, Expression
* start
,
9490 Expression
* end
, source_location location
)
9492 return new String_index_expression(string
, start
, end
, location
);
9497 // Get the type of the map.
9500 Map_index_expression::get_map_type() const
9502 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9503 gcc_assert(mt
!= NULL
);
9507 // Map index traversal.
9510 Map_index_expression::do_traverse(Traverse
* traverse
)
9512 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9513 return TRAVERSE_EXIT
;
9514 return Expression::traverse(&this->index_
, traverse
);
9517 // Return the type of a map index.
9520 Map_index_expression::do_type()
9522 Type
* type
= this->get_map_type()->val_type();
9523 // If this map index is in a tuple assignment, we actually return a
9524 // pointer to the value type. Tuple_map_assignment_statement is
9525 // responsible for handling this correctly. We need to get the type
9526 // right in case this gets assigned to a temporary variable.
9527 if (this->is_in_tuple_assignment_
)
9528 type
= Type::make_pointer_type(type
);
9532 // Fix the type of a map index.
9535 Map_index_expression::do_determine_type(const Type_context
*)
9537 this->map_
->determine_type_no_context();
9538 Type_context
subcontext(this->get_map_type()->key_type(), false);
9539 this->index_
->determine_type(&subcontext
);
9542 // Check types of a map index.
9545 Map_index_expression::do_check_types(Gogo
*)
9548 if (!Type::are_assignable(this->get_map_type()->key_type(),
9549 this->index_
->type(), &reason
))
9552 this->report_error(_("incompatible type for map index"));
9555 error_at(this->location(), "incompatible type for map index (%s)",
9557 this->set_is_error();
9562 // Get a tree for a map index.
9565 Map_index_expression::do_get_tree(Translate_context
* context
)
9567 Map_type
* type
= this->get_map_type();
9569 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9570 if (valptr
== error_mark_node
)
9571 return error_mark_node
;
9572 valptr
= save_expr(valptr
);
9574 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
9576 if (this->is_lvalue_
)
9577 return build_fold_indirect_ref(valptr
);
9578 else if (this->is_in_tuple_assignment_
)
9580 // Tuple_map_assignment_statement is responsible for using this
9586 return fold_build3(COND_EXPR
, val_type_tree
,
9587 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
9588 fold_convert(TREE_TYPE(valptr
),
9589 null_pointer_node
)),
9590 type
->val_type()->get_init_tree(context
->gogo(),
9592 build_fold_indirect_ref(valptr
));
9596 // Get a tree for the map index. This returns a tree which evaluates
9597 // to a pointer to a value. The pointer will be NULL if the key is
9601 Map_index_expression::get_value_pointer(Translate_context
* context
,
9604 Map_type
* type
= this->get_map_type();
9606 tree map_tree
= this->map_
->get_tree(context
);
9607 tree index_tree
= this->index_
->get_tree(context
);
9608 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
9609 this->index_
->type(),
9612 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
9613 return error_mark_node
;
9615 if (this->map_
->type()->points_to() != NULL
)
9616 map_tree
= build_fold_indirect_ref(map_tree
);
9618 // We need to pass in a pointer to the key, so stuff it into a
9620 tree tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
9621 DECL_IGNORED_P(tmp
) = 0;
9622 DECL_INITIAL(tmp
) = index_tree
;
9623 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
9624 tree tmpref
= fold_convert(const_ptr_type_node
, build_fold_addr_expr(tmp
));
9625 TREE_ADDRESSABLE(tmp
) = 1;
9627 static tree map_index_fndecl
;
9628 tree call
= Gogo::call_builtin(&map_index_fndecl
,
9632 const_ptr_type_node
,
9633 TREE_TYPE(map_tree
),
9635 const_ptr_type_node
,
9640 : boolean_false_node
));
9641 if (call
== error_mark_node
)
9642 return error_mark_node
;
9643 // This can panic on a map of interface type if the interface holds
9644 // an uncomparable or unhashable type.
9645 TREE_NOTHROW(map_index_fndecl
) = 0;
9647 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
9648 if (val_type_tree
== error_mark_node
)
9649 return error_mark_node
;
9650 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
9652 return build2(COMPOUND_EXPR
, ptr_val_type_tree
,
9654 fold_convert(ptr_val_type_tree
, call
));
9657 // Make a map index expression.
9659 Map_index_expression
*
9660 Expression::make_map_index(Expression
* map
, Expression
* index
,
9661 source_location location
)
9663 return new Map_index_expression(map
, index
, location
);
9666 // Class Field_reference_expression.
9668 // Return the type of a field reference.
9671 Field_reference_expression::do_type()
9673 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9674 gcc_assert(struct_type
!= NULL
);
9675 return struct_type
->field(this->field_index_
)->type();
9678 // Check the types for a field reference.
9681 Field_reference_expression::do_check_types(Gogo
*)
9683 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9684 gcc_assert(struct_type
!= NULL
);
9685 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
9688 // Get a tree for a field reference.
9691 Field_reference_expression::do_get_tree(Translate_context
* context
)
9693 tree struct_tree
= this->expr_
->get_tree(context
);
9694 if (struct_tree
== error_mark_node
9695 || TREE_TYPE(struct_tree
) == error_mark_node
)
9696 return error_mark_node
;
9697 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
9698 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
9699 if (field
== NULL_TREE
)
9701 // This can happen for a type which refers to itself indirectly
9702 // and then turns out to be erroneous.
9703 gcc_assert(saw_errors());
9704 return error_mark_node
;
9706 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
9708 field
= DECL_CHAIN(field
);
9709 gcc_assert(field
!= NULL_TREE
);
9711 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
9715 // Make a reference to a qualified identifier in an expression.
9717 Field_reference_expression
*
9718 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
9719 source_location location
)
9721 return new Field_reference_expression(expr
, field_index
, location
);
9724 // Class Interface_field_reference_expression.
9726 // Return a tree for the pointer to the function to call.
9729 Interface_field_reference_expression::get_function_tree(Translate_context
*,
9732 if (this->expr_
->type()->points_to() != NULL
)
9733 expr
= build_fold_indirect_ref(expr
);
9735 tree expr_type
= TREE_TYPE(expr
);
9736 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9738 tree field
= TYPE_FIELDS(expr_type
);
9739 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
9741 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9742 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
9744 table
= build_fold_indirect_ref(table
);
9745 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
9747 std::string name
= Gogo::unpack_hidden_name(this->name_
);
9748 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
9750 field
= DECL_CHAIN(field
))
9752 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
9755 gcc_assert(field
!= NULL_TREE
);
9757 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
9760 // Return a tree for the first argument to pass to the interface
9764 Interface_field_reference_expression::get_underlying_object_tree(
9768 if (this->expr_
->type()->points_to() != NULL
)
9769 expr
= build_fold_indirect_ref(expr
);
9771 tree expr_type
= TREE_TYPE(expr
);
9772 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9774 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
9775 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
9777 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9783 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
9785 return Expression::traverse(&this->expr_
, traverse
);
9788 // Return the type of an interface field reference.
9791 Interface_field_reference_expression::do_type()
9793 Type
* expr_type
= this->expr_
->type();
9795 Type
* points_to
= expr_type
->points_to();
9796 if (points_to
!= NULL
)
9797 expr_type
= points_to
;
9799 Interface_type
* interface_type
= expr_type
->interface_type();
9800 if (interface_type
== NULL
)
9801 return Type::make_error_type();
9803 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
9805 return Type::make_error_type();
9807 return method
->type();
9813 Interface_field_reference_expression::do_determine_type(const Type_context
*)
9815 this->expr_
->determine_type_no_context();
9818 // Check the types for an interface field reference.
9821 Interface_field_reference_expression::do_check_types(Gogo
*)
9823 Type
* type
= this->expr_
->type();
9825 Type
* points_to
= type
->points_to();
9826 if (points_to
!= NULL
)
9829 Interface_type
* interface_type
= type
->interface_type();
9830 if (interface_type
== NULL
)
9831 this->report_error(_("expected interface or pointer to interface"));
9834 const Typed_identifier
* method
=
9835 interface_type
->find_method(this->name_
);
9838 error_at(this->location(), "method %qs not in interface",
9839 Gogo::message_name(this->name_
).c_str());
9840 this->set_is_error();
9845 // Get a tree for a reference to a field in an interface. There is no
9846 // standard tree type representation for this: it's a function
9847 // attached to its first argument, like a Bound_method_expression.
9848 // The only places it may currently be used are in a Call_expression
9849 // or a Go_statement, which will take it apart directly. So this has
9850 // nothing to do at present.
9853 Interface_field_reference_expression::do_get_tree(Translate_context
*)
9858 // Make a reference to a field in an interface.
9861 Expression::make_interface_field_reference(Expression
* expr
,
9862 const std::string
& field
,
9863 source_location location
)
9865 return new Interface_field_reference_expression(expr
, field
, location
);
9868 // A general selector. This is a Parser_expression for LEFT.NAME. It
9869 // is lowered after we know the type of the left hand side.
9871 class Selector_expression
: public Parser_expression
9874 Selector_expression(Expression
* left
, const std::string
& name
,
9875 source_location location
)
9876 : Parser_expression(EXPRESSION_SELECTOR
, location
),
9877 left_(left
), name_(name
)
9882 do_traverse(Traverse
* traverse
)
9883 { return Expression::traverse(&this->left_
, traverse
); }
9886 do_lower(Gogo
*, Named_object
*, int);
9891 return new Selector_expression(this->left_
->copy(), this->name_
,
9897 lower_method_expression(Gogo
*);
9899 // The expression on the left hand side.
9901 // The name on the right hand side.
9905 // Lower a selector expression once we know the real type of the left
9909 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
9911 Expression
* left
= this->left_
;
9912 if (left
->is_type_expression())
9913 return this->lower_method_expression(gogo
);
9914 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
9918 // Lower a method expression T.M or (*T).M. We turn this into a
9919 // function literal.
9922 Selector_expression::lower_method_expression(Gogo
* gogo
)
9924 source_location location
= this->location();
9925 Type
* type
= this->left_
->type();
9926 const std::string
& name(this->name_
);
9929 if (type
->points_to() == NULL
)
9934 type
= type
->points_to();
9936 Named_type
* nt
= type
->named_type();
9940 ("method expression requires named type or "
9941 "pointer to named type"));
9942 return Expression::make_error(location
);
9946 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
9950 error_at(location
, "type %<%s%> has no method %<%s%>",
9951 nt
->message_name().c_str(),
9952 Gogo::message_name(name
).c_str());
9954 error_at(location
, "method %<%s%> is ambiguous in type %<%s%>",
9955 Gogo::message_name(name
).c_str(),
9956 nt
->message_name().c_str());
9957 return Expression::make_error(location
);
9960 if (!is_pointer
&& !method
->is_value_method())
9962 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
9963 nt
->message_name().c_str(),
9964 Gogo::message_name(name
).c_str());
9965 return Expression::make_error(location
);
9968 // Build a new function type in which the receiver becomes the first
9970 Function_type
* method_type
= method
->type();
9971 gcc_assert(method_type
->is_method());
9973 const char* const receiver_name
= "$this";
9974 Typed_identifier_list
* parameters
= new Typed_identifier_list();
9975 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
9978 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
9979 if (method_parameters
!= NULL
)
9981 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
9982 p
!= method_parameters
->end();
9984 parameters
->push_back(*p
);
9987 const Typed_identifier_list
* method_results
= method_type
->results();
9988 Typed_identifier_list
* results
;
9989 if (method_results
== NULL
)
9993 results
= new Typed_identifier_list();
9994 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
9995 p
!= method_results
->end();
9997 results
->push_back(*p
);
10000 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
10002 if (method_type
->is_varargs())
10003 fntype
->set_is_varargs();
10005 // We generate methods which always takes a pointer to the receiver
10006 // as their first argument. If this is for a pointer type, we can
10007 // simply reuse the existing function. We use an internal hack to
10008 // get the right type.
10012 Named_object
* mno
= (method
->needs_stub_method()
10013 ? method
->stub_object()
10014 : method
->named_object());
10015 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
10016 f
= Expression::make_cast(fntype
, f
, location
);
10017 Type_conversion_expression
* tce
=
10018 static_cast<Type_conversion_expression
*>(f
);
10019 tce
->set_may_convert_function_types();
10023 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
10026 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
10027 gcc_assert(vno
!= NULL
);
10028 Expression
* ve
= Expression::make_var_reference(vno
, location
);
10029 Expression
* bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
10030 gcc_assert(bm
!= NULL
&& !bm
->is_error_expression());
10032 Expression_list
* args
;
10033 if (method_parameters
== NULL
)
10037 args
= new Expression_list();
10038 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10039 p
!= method_parameters
->end();
10042 vno
= gogo
->lookup(p
->name(), NULL
);
10043 gcc_assert(vno
!= NULL
);
10044 args
->push_back(Expression::make_var_reference(vno
, location
));
10048 Call_expression
* call
= Expression::make_call(bm
, args
,
10049 method_type
->is_varargs(),
10052 size_t count
= call
->result_count();
10055 s
= Statement::make_statement(call
);
10058 Expression_list
* retvals
= new Expression_list();
10060 retvals
->push_back(call
);
10063 for (size_t i
= 0; i
< count
; ++i
)
10064 retvals
->push_back(Expression::make_call_result(call
, i
));
10066 s
= Statement::make_return_statement(no
->func_value()->type()->results(),
10067 retvals
, location
);
10069 gogo
->add_statement(s
);
10071 gogo
->finish_function(location
);
10073 return Expression::make_func_reference(no
, NULL
, location
);
10076 // Make a selector expression.
10079 Expression::make_selector(Expression
* left
, const std::string
& name
,
10080 source_location location
)
10082 return new Selector_expression(left
, name
, location
);
10085 // Implement the builtin function new.
10087 class Allocation_expression
: public Expression
10090 Allocation_expression(Type
* type
, source_location location
)
10091 : Expression(EXPRESSION_ALLOCATION
, location
),
10097 do_traverse(Traverse
* traverse
)
10098 { return Type::traverse(this->type_
, traverse
); }
10102 { return Type::make_pointer_type(this->type_
); }
10105 do_determine_type(const Type_context
*)
10109 do_check_types(Gogo
*);
10113 { return new Allocation_expression(this->type_
, this->location()); }
10116 do_get_tree(Translate_context
*);
10119 // The type we are allocating.
10123 // Check the type of an allocation expression.
10126 Allocation_expression::do_check_types(Gogo
*)
10128 if (this->type_
->function_type() != NULL
)
10129 this->report_error(_("invalid new of function type"));
10132 // Return a tree for an allocation expression.
10135 Allocation_expression::do_get_tree(Translate_context
* context
)
10137 tree type_tree
= this->type_
->get_tree(context
->gogo());
10138 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10139 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10141 return fold_convert(build_pointer_type(type_tree
), space
);
10144 // Make an allocation expression.
10147 Expression::make_allocation(Type
* type
, source_location location
)
10149 return new Allocation_expression(type
, location
);
10152 // Implement the builtin function make.
10154 class Make_expression
: public Expression
10157 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10158 : Expression(EXPRESSION_MAKE
, location
),
10159 type_(type
), args_(args
)
10164 do_traverse(Traverse
* traverse
);
10168 { return this->type_
; }
10171 do_determine_type(const Type_context
*);
10174 do_check_types(Gogo
*);
10179 return new Make_expression(this->type_
, this->args_
->copy(),
10184 do_get_tree(Translate_context
*);
10187 // The type we are making.
10189 // The arguments to pass to the make routine.
10190 Expression_list
* args_
;
10196 Make_expression::do_traverse(Traverse
* traverse
)
10198 if (this->args_
!= NULL
10199 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10200 return TRAVERSE_EXIT
;
10201 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10202 return TRAVERSE_EXIT
;
10203 return TRAVERSE_CONTINUE
;
10206 // Set types of arguments.
10209 Make_expression::do_determine_type(const Type_context
*)
10211 if (this->args_
!= NULL
)
10213 Type_context
context(Type::lookup_integer_type("int"), false);
10214 for (Expression_list::const_iterator pe
= this->args_
->begin();
10215 pe
!= this->args_
->end();
10217 (*pe
)->determine_type(&context
);
10221 // Check types for a make expression.
10224 Make_expression::do_check_types(Gogo
*)
10226 if (this->type_
->channel_type() == NULL
10227 && this->type_
->map_type() == NULL
10228 && (this->type_
->array_type() == NULL
10229 || this->type_
->array_type()->length() != NULL
))
10230 this->report_error(_("invalid type for make function"));
10231 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10232 this->set_is_error();
10235 // Return a tree for a make expression.
10238 Make_expression::do_get_tree(Translate_context
* context
)
10240 return this->type_
->make_expression_tree(context
, this->args_
,
10244 // Make a make expression.
10247 Expression::make_make(Type
* type
, Expression_list
* args
,
10248 source_location location
)
10250 return new Make_expression(type
, args
, location
);
10253 // Construct a struct.
10255 class Struct_construction_expression
: public Expression
10258 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10259 source_location location
)
10260 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10261 type_(type
), vals_(vals
)
10264 // Return whether this is a constant initializer.
10266 is_constant_struct() const;
10270 do_traverse(Traverse
* traverse
);
10274 { return this->type_
; }
10277 do_determine_type(const Type_context
*);
10280 do_check_types(Gogo
*);
10285 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10290 do_is_addressable() const
10294 do_get_tree(Translate_context
*);
10297 do_export(Export
*) const;
10300 // The type of the struct to construct.
10302 // The list of values, in order of the fields in the struct. A NULL
10303 // entry means that the field should be zero-initialized.
10304 Expression_list
* vals_
;
10310 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10312 if (this->vals_
!= NULL
10313 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10314 return TRAVERSE_EXIT
;
10315 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10316 return TRAVERSE_EXIT
;
10317 return TRAVERSE_CONTINUE
;
10320 // Return whether this is a constant initializer.
10323 Struct_construction_expression::is_constant_struct() const
10325 if (this->vals_
== NULL
)
10327 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10328 pv
!= this->vals_
->end();
10332 && !(*pv
)->is_constant()
10333 && (!(*pv
)->is_composite_literal()
10334 || (*pv
)->is_nonconstant_composite_literal()))
10338 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10339 for (Struct_field_list::const_iterator pf
= fields
->begin();
10340 pf
!= fields
->end();
10343 // There are no constant constructors for interfaces.
10344 if (pf
->type()->interface_type() != NULL
)
10351 // Final type determination.
10354 Struct_construction_expression::do_determine_type(const Type_context
*)
10356 if (this->vals_
== NULL
)
10358 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10359 Expression_list::const_iterator pv
= this->vals_
->begin();
10360 for (Struct_field_list::const_iterator pf
= fields
->begin();
10361 pf
!= fields
->end();
10364 if (pv
== this->vals_
->end())
10368 Type_context
subcontext(pf
->type(), false);
10369 (*pv
)->determine_type(&subcontext
);
10377 Struct_construction_expression::do_check_types(Gogo
*)
10379 if (this->vals_
== NULL
)
10382 Struct_type
* st
= this->type_
->struct_type();
10383 if (this->vals_
->size() > st
->field_count())
10385 this->report_error(_("too many expressions for struct"));
10389 const Struct_field_list
* fields
= st
->fields();
10390 Expression_list::const_iterator pv
= this->vals_
->begin();
10392 for (Struct_field_list::const_iterator pf
= fields
->begin();
10393 pf
!= fields
->end();
10396 if (pv
== this->vals_
->end())
10398 this->report_error(_("too few expressions for struct"));
10405 std::string reason
;
10406 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10408 if (reason
.empty())
10409 error_at((*pv
)->location(),
10410 "incompatible type for field %d in struct construction",
10413 error_at((*pv
)->location(),
10414 ("incompatible type for field %d in "
10415 "struct construction (%s)"),
10416 i
+ 1, reason
.c_str());
10417 this->set_is_error();
10420 gcc_assert(pv
== this->vals_
->end());
10423 // Return a tree for constructing a struct.
10426 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10428 Gogo
* gogo
= context
->gogo();
10430 if (this->vals_
== NULL
)
10431 return this->type_
->get_init_tree(gogo
, false);
10433 tree type_tree
= this->type_
->get_tree(gogo
);
10434 if (type_tree
== error_mark_node
)
10435 return error_mark_node
;
10436 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10438 bool is_constant
= true;
10439 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10440 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10442 Struct_field_list::const_iterator pf
= fields
->begin();
10443 Expression_list::const_iterator pv
= this->vals_
->begin();
10444 for (tree field
= TYPE_FIELDS(type_tree
);
10445 field
!= NULL_TREE
;
10446 field
= DECL_CHAIN(field
), ++pf
)
10448 gcc_assert(pf
!= fields
->end());
10451 if (pv
== this->vals_
->end())
10452 val
= pf
->type()->get_init_tree(gogo
, false);
10453 else if (*pv
== NULL
)
10455 val
= pf
->type()->get_init_tree(gogo
, false);
10460 val
= Expression::convert_for_assignment(context
, pf
->type(),
10462 (*pv
)->get_tree(context
),
10467 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10468 return error_mark_node
;
10470 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10471 elt
->index
= field
;
10473 if (!TREE_CONSTANT(val
))
10474 is_constant
= false;
10476 gcc_assert(pf
== fields
->end());
10478 tree ret
= build_constructor(type_tree
, elts
);
10480 TREE_CONSTANT(ret
) = 1;
10484 // Export a struct construction.
10487 Struct_construction_expression::do_export(Export
* exp
) const
10489 exp
->write_c_string("convert(");
10490 exp
->write_type(this->type_
);
10491 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10492 pv
!= this->vals_
->end();
10495 exp
->write_c_string(", ");
10497 (*pv
)->export_expression(exp
);
10499 exp
->write_c_string(")");
10502 // Make a struct composite literal. This used by the thunk code.
10505 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
10506 source_location location
)
10508 gcc_assert(type
->struct_type() != NULL
);
10509 return new Struct_construction_expression(type
, vals
, location
);
10512 // Construct an array. This class is not used directly; instead we
10513 // use the child classes, Fixed_array_construction_expression and
10514 // Open_array_construction_expression.
10516 class Array_construction_expression
: public Expression
10519 Array_construction_expression(Expression_classification classification
,
10520 Type
* type
, Expression_list
* vals
,
10521 source_location location
)
10522 : Expression(classification
, location
),
10523 type_(type
), vals_(vals
)
10527 // Return whether this is a constant initializer.
10529 is_constant_array() const;
10531 // Return the number of elements.
10533 element_count() const
10534 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
10538 do_traverse(Traverse
* traverse
);
10542 { return this->type_
; }
10545 do_determine_type(const Type_context
*);
10548 do_check_types(Gogo
*);
10551 do_is_addressable() const
10555 do_export(Export
*) const;
10557 // The list of values.
10560 { return this->vals_
; }
10562 // Get a constructor tree for the array values.
10564 get_constructor_tree(Translate_context
* context
, tree type_tree
);
10567 // The type of the array to construct.
10569 // The list of values.
10570 Expression_list
* vals_
;
10576 Array_construction_expression::do_traverse(Traverse
* traverse
)
10578 if (this->vals_
!= NULL
10579 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10580 return TRAVERSE_EXIT
;
10581 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10582 return TRAVERSE_EXIT
;
10583 return TRAVERSE_CONTINUE
;
10586 // Return whether this is a constant initializer.
10589 Array_construction_expression::is_constant_array() const
10591 if (this->vals_
== NULL
)
10594 // There are no constant constructors for interfaces.
10595 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
10598 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10599 pv
!= this->vals_
->end();
10603 && !(*pv
)->is_constant()
10604 && (!(*pv
)->is_composite_literal()
10605 || (*pv
)->is_nonconstant_composite_literal()))
10611 // Final type determination.
10614 Array_construction_expression::do_determine_type(const Type_context
*)
10616 if (this->vals_
== NULL
)
10618 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
10619 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10620 pv
!= this->vals_
->end();
10624 (*pv
)->determine_type(&subcontext
);
10631 Array_construction_expression::do_check_types(Gogo
*)
10633 if (this->vals_
== NULL
)
10636 Array_type
* at
= this->type_
->array_type();
10638 Type
* element_type
= at
->element_type();
10639 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10640 pv
!= this->vals_
->end();
10644 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
10646 error_at((*pv
)->location(),
10647 "incompatible type for element %d in composite literal",
10649 this->set_is_error();
10653 Expression
* length
= at
->length();
10654 if (length
!= NULL
)
10659 if (at
->length()->integer_constant_value(true, val
, &type
))
10661 if (this->vals_
->size() > mpz_get_ui(val
))
10662 this->report_error(_("too many elements in composite literal"));
10668 // Get a constructor tree for the array values.
10671 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
10674 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10675 (this->vals_
== NULL
10677 : this->vals_
->size()));
10678 Type
* element_type
= this->type_
->array_type()->element_type();
10679 bool is_constant
= true;
10680 if (this->vals_
!= NULL
)
10683 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10684 pv
!= this->vals_
->end();
10687 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
10688 elt
->index
= size_int(i
);
10690 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10693 tree value_tree
= (*pv
)->get_tree(context
);
10694 elt
->value
= Expression::convert_for_assignment(context
,
10700 if (elt
->value
== error_mark_node
)
10701 return error_mark_node
;
10702 if (!TREE_CONSTANT(elt
->value
))
10703 is_constant
= false;
10707 tree ret
= build_constructor(type_tree
, values
);
10709 TREE_CONSTANT(ret
) = 1;
10713 // Export an array construction.
10716 Array_construction_expression::do_export(Export
* exp
) const
10718 exp
->write_c_string("convert(");
10719 exp
->write_type(this->type_
);
10720 if (this->vals_
!= NULL
)
10722 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10723 pv
!= this->vals_
->end();
10726 exp
->write_c_string(", ");
10728 (*pv
)->export_expression(exp
);
10731 exp
->write_c_string(")");
10734 // Construct a fixed array.
10736 class Fixed_array_construction_expression
:
10737 public Array_construction_expression
10740 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
10741 source_location location
)
10742 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
10743 type
, vals
, location
)
10745 gcc_assert(type
->array_type() != NULL
10746 && type
->array_type()->length() != NULL
);
10753 return new Fixed_array_construction_expression(this->type(),
10754 (this->vals() == NULL
10756 : this->vals()->copy()),
10761 do_get_tree(Translate_context
*);
10764 // Return a tree for constructing a fixed array.
10767 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
10769 return this->get_constructor_tree(context
,
10770 this->type()->get_tree(context
->gogo()));
10773 // Construct an open array.
10775 class Open_array_construction_expression
: public Array_construction_expression
10778 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
10779 source_location location
)
10780 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
10781 type
, vals
, location
)
10783 gcc_assert(type
->array_type() != NULL
10784 && type
->array_type()->length() == NULL
);
10788 // Note that taking the address of an open array literal is invalid.
10793 return new Open_array_construction_expression(this->type(),
10794 (this->vals() == NULL
10796 : this->vals()->copy()),
10801 do_get_tree(Translate_context
*);
10804 // Return a tree for constructing an open array.
10807 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
10809 Type
* element_type
= this->type()->array_type()->element_type();
10810 tree element_type_tree
= element_type
->get_tree(context
->gogo());
10811 if (element_type_tree
== error_mark_node
)
10812 return error_mark_node
;
10816 if (this->vals() == NULL
|| this->vals()->empty())
10818 // We need to create a unique value.
10819 tree max
= size_int(0);
10820 tree constructor_type
= build_array_type(element_type_tree
,
10821 build_index_type(max
));
10822 if (constructor_type
== error_mark_node
)
10823 return error_mark_node
;
10824 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
10825 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
10826 elt
->index
= size_int(0);
10827 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10828 values
= build_constructor(constructor_type
, vec
);
10829 if (TREE_CONSTANT(elt
->value
))
10830 TREE_CONSTANT(values
) = 1;
10831 length_tree
= size_int(0);
10835 tree max
= size_int(this->vals()->size() - 1);
10836 tree constructor_type
= build_array_type(element_type_tree
,
10837 build_index_type(max
));
10838 if (constructor_type
== error_mark_node
)
10839 return error_mark_node
;
10840 values
= this->get_constructor_tree(context
, constructor_type
);
10841 length_tree
= size_int(this->vals()->size());
10844 if (values
== error_mark_node
)
10845 return error_mark_node
;
10847 bool is_constant_initializer
= TREE_CONSTANT(values
);
10848 bool is_in_function
= context
->function() != NULL
;
10850 if (is_constant_initializer
)
10852 tree tmp
= build_decl(this->location(), VAR_DECL
,
10853 create_tmp_var_name("C"), TREE_TYPE(values
));
10854 DECL_EXTERNAL(tmp
) = 0;
10855 TREE_PUBLIC(tmp
) = 0;
10856 TREE_STATIC(tmp
) = 1;
10857 DECL_ARTIFICIAL(tmp
) = 1;
10858 if (is_in_function
)
10860 // If this is not a function, we will only initialize the
10861 // value once, so we can use this directly rather than
10862 // copying it. In that case we can't make it read-only,
10863 // because the program is permitted to change it.
10864 TREE_READONLY(tmp
) = 1;
10865 TREE_CONSTANT(tmp
) = 1;
10867 DECL_INITIAL(tmp
) = values
;
10868 rest_of_decl_compilation(tmp
, 1, 0);
10874 if (!is_in_function
&& is_constant_initializer
)
10876 // Outside of a function, we know the initializer will only run
10878 space
= build_fold_addr_expr(values
);
10883 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
10884 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
10886 space
= save_expr(space
);
10888 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
10889 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
10890 TREE_THIS_NOTRAP(ref
) = 1;
10891 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
10894 // Build a constructor for the open array.
10896 tree type_tree
= this->type()->get_tree(context
->gogo());
10897 if (type_tree
== error_mark_node
)
10898 return error_mark_node
;
10899 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10901 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
10903 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
10904 tree field
= TYPE_FIELDS(type_tree
);
10905 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
10906 elt
->index
= field
;
10907 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
10909 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
10910 field
= DECL_CHAIN(field
);
10911 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
10912 elt
->index
= field
;
10913 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
10915 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
10916 field
= DECL_CHAIN(field
);
10917 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
10918 elt
->index
= field
;
10919 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
10921 tree constructor
= build_constructor(type_tree
, init
);
10922 if (constructor
== error_mark_node
)
10923 return error_mark_node
;
10924 if (!is_in_function
&& is_constant_initializer
)
10925 TREE_CONSTANT(constructor
) = 1;
10927 if (set
== NULL_TREE
)
10928 return constructor
;
10930 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
10933 // Make a slice composite literal. This is used by the type
10934 // descriptor code.
10937 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
10938 source_location location
)
10940 gcc_assert(type
->is_open_array_type());
10941 return new Open_array_construction_expression(type
, vals
, location
);
10944 // Construct a map.
10946 class Map_construction_expression
: public Expression
10949 Map_construction_expression(Type
* type
, Expression_list
* vals
,
10950 source_location location
)
10951 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
10952 type_(type
), vals_(vals
)
10953 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
10957 do_traverse(Traverse
* traverse
);
10961 { return this->type_
; }
10964 do_determine_type(const Type_context
*);
10967 do_check_types(Gogo
*);
10972 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
10977 do_get_tree(Translate_context
*);
10980 do_export(Export
*) const;
10983 // The type of the map to construct.
10985 // The list of values.
10986 Expression_list
* vals_
;
10992 Map_construction_expression::do_traverse(Traverse
* traverse
)
10994 if (this->vals_
!= NULL
10995 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10996 return TRAVERSE_EXIT
;
10997 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10998 return TRAVERSE_EXIT
;
10999 return TRAVERSE_CONTINUE
;
11002 // Final type determination.
11005 Map_construction_expression::do_determine_type(const Type_context
*)
11007 if (this->vals_
== NULL
)
11010 Map_type
* mt
= this->type_
->map_type();
11011 Type_context
key_context(mt
->key_type(), false);
11012 Type_context
val_context(mt
->val_type(), false);
11013 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11014 pv
!= this->vals_
->end();
11017 (*pv
)->determine_type(&key_context
);
11019 (*pv
)->determine_type(&val_context
);
11026 Map_construction_expression::do_check_types(Gogo
*)
11028 if (this->vals_
== NULL
)
11031 Map_type
* mt
= this->type_
->map_type();
11033 Type
* key_type
= mt
->key_type();
11034 Type
* val_type
= mt
->val_type();
11035 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11036 pv
!= this->vals_
->end();
11039 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
11041 error_at((*pv
)->location(),
11042 "incompatible type for element %d key in map construction",
11044 this->set_is_error();
11047 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
11049 error_at((*pv
)->location(),
11050 ("incompatible type for element %d value "
11051 "in map construction"),
11053 this->set_is_error();
11058 // Return a tree for constructing a map.
11061 Map_construction_expression::do_get_tree(Translate_context
* context
)
11063 Gogo
* gogo
= context
->gogo();
11064 source_location loc
= this->location();
11066 Map_type
* mt
= this->type_
->map_type();
11068 // Build a struct to hold the key and value.
11069 tree struct_type
= make_node(RECORD_TYPE
);
11071 Type
* key_type
= mt
->key_type();
11072 tree id
= get_identifier("__key");
11073 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type
->get_tree(gogo
));
11074 DECL_CONTEXT(key_field
) = struct_type
;
11075 TYPE_FIELDS(struct_type
) = key_field
;
11077 Type
* val_type
= mt
->val_type();
11078 id
= get_identifier("__val");
11079 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type
->get_tree(gogo
));
11080 DECL_CONTEXT(val_field
) = struct_type
;
11081 DECL_CHAIN(key_field
) = val_field
;
11083 layout_type(struct_type
);
11085 bool is_constant
= true;
11090 if (this->vals_
== NULL
|| this->vals_
->empty())
11092 valaddr
= null_pointer_node
;
11093 make_tmp
= NULL_TREE
;
11097 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11098 this->vals_
->size() / 2);
11100 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11101 pv
!= this->vals_
->end();
11104 bool one_is_constant
= true;
11106 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11108 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11109 elt
->index
= key_field
;
11110 tree val_tree
= (*pv
)->get_tree(context
);
11111 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11114 if (elt
->value
== error_mark_node
)
11115 return error_mark_node
;
11116 if (!TREE_CONSTANT(elt
->value
))
11117 one_is_constant
= false;
11121 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11122 elt
->index
= val_field
;
11123 val_tree
= (*pv
)->get_tree(context
);
11124 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11127 if (elt
->value
== error_mark_node
)
11128 return error_mark_node
;
11129 if (!TREE_CONSTANT(elt
->value
))
11130 one_is_constant
= false;
11132 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11133 elt
->index
= size_int(i
);
11134 elt
->value
= build_constructor(struct_type
, one
);
11135 if (one_is_constant
)
11136 TREE_CONSTANT(elt
->value
) = 1;
11138 is_constant
= false;
11141 tree index_type
= build_index_type(size_int(i
- 1));
11142 tree array_type
= build_array_type(struct_type
, index_type
);
11143 tree init
= build_constructor(array_type
, values
);
11145 TREE_CONSTANT(init
) = 1;
11147 if (current_function_decl
!= NULL
)
11149 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11150 DECL_INITIAL(tmp
) = init
;
11151 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11152 TREE_ADDRESSABLE(tmp
) = 1;
11156 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11157 DECL_EXTERNAL(tmp
) = 0;
11158 TREE_PUBLIC(tmp
) = 0;
11159 TREE_STATIC(tmp
) = 1;
11160 DECL_ARTIFICIAL(tmp
) = 1;
11161 if (!TREE_CONSTANT(init
))
11162 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11166 TREE_READONLY(tmp
) = 1;
11167 TREE_CONSTANT(tmp
) = 1;
11168 DECL_INITIAL(tmp
) = init
;
11169 make_tmp
= NULL_TREE
;
11171 rest_of_decl_compilation(tmp
, 1, 0);
11174 valaddr
= build_fold_addr_expr(tmp
);
11177 tree descriptor
= gogo
->map_descriptor(mt
);
11179 tree type_tree
= this->type_
->get_tree(gogo
);
11181 static tree construct_map_fndecl
;
11182 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11184 "__go_construct_map",
11187 TREE_TYPE(descriptor
),
11192 TYPE_SIZE_UNIT(struct_type
),
11194 byte_position(val_field
),
11196 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11197 const_ptr_type_node
,
11198 fold_convert(const_ptr_type_node
, valaddr
));
11199 if (call
== error_mark_node
)
11200 return error_mark_node
;
11203 if (make_tmp
== NULL
)
11206 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11210 // Export an array construction.
11213 Map_construction_expression::do_export(Export
* exp
) const
11215 exp
->write_c_string("convert(");
11216 exp
->write_type(this->type_
);
11217 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11218 pv
!= this->vals_
->end();
11221 exp
->write_c_string(", ");
11222 (*pv
)->export_expression(exp
);
11224 exp
->write_c_string(")");
11227 // A general composite literal. This is lowered to a type specific
11230 class Composite_literal_expression
: public Parser_expression
11233 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11234 Expression_list
* vals
, source_location location
)
11235 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11236 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11241 do_traverse(Traverse
* traverse
);
11244 do_lower(Gogo
*, Named_object
*, int);
11249 return new Composite_literal_expression(this->type_
, this->depth_
,
11251 (this->vals_
== NULL
11253 : this->vals_
->copy()),
11259 lower_struct(Type
*);
11262 lower_array(Type
*);
11265 make_array(Type
*, Expression_list
*);
11268 lower_map(Gogo
*, Named_object
*, Type
*);
11270 // The type of the composite literal.
11272 // The depth within a list of composite literals within a composite
11273 // literal, when the type is omitted.
11275 // The values to put in the composite literal.
11276 Expression_list
* vals_
;
11277 // If this is true, then VALS_ is a list of pairs: a key and a
11278 // value. In an array initializer, a missing key will be NULL.
11285 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11287 if (this->vals_
!= NULL
11288 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11289 return TRAVERSE_EXIT
;
11290 return Type::traverse(this->type_
, traverse
);
11293 // Lower a generic composite literal into a specific version based on
11297 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
11299 Type
* type
= this->type_
;
11301 for (int depth
= this->depth_
; depth
> 0; --depth
)
11303 if (type
->array_type() != NULL
)
11304 type
= type
->array_type()->element_type();
11305 else if (type
->map_type() != NULL
)
11306 type
= type
->map_type()->val_type();
11309 if (!type
->is_error_type())
11310 error_at(this->location(),
11311 ("may only omit types within composite literals "
11312 "of slice, array, or map type"));
11313 return Expression::make_error(this->location());
11317 if (type
->is_error_type())
11318 return Expression::make_error(this->location());
11319 else if (type
->struct_type() != NULL
)
11320 return this->lower_struct(type
);
11321 else if (type
->array_type() != NULL
)
11322 return this->lower_array(type
);
11323 else if (type
->map_type() != NULL
)
11324 return this->lower_map(gogo
, function
, type
);
11327 error_at(this->location(),
11328 ("expected struct, slice, array, or map type "
11329 "for composite literal"));
11330 return Expression::make_error(this->location());
11334 // Lower a struct composite literal.
11337 Composite_literal_expression::lower_struct(Type
* type
)
11339 source_location location
= this->location();
11340 Struct_type
* st
= type
->struct_type();
11341 if (this->vals_
== NULL
|| !this->has_keys_
)
11342 return new Struct_construction_expression(type
, this->vals_
, location
);
11344 size_t field_count
= st
->field_count();
11345 std::vector
<Expression
*> vals(field_count
);
11346 Expression_list::const_iterator p
= this->vals_
->begin();
11347 while (p
!= this->vals_
->end())
11349 Expression
* name_expr
= *p
;
11352 gcc_assert(p
!= this->vals_
->end());
11353 Expression
* val
= *p
;
11357 if (name_expr
== NULL
)
11359 error_at(val
->location(), "mixture of field and value initializers");
11360 return Expression::make_error(location
);
11363 bool bad_key
= false;
11365 switch (name_expr
->classification())
11367 case EXPRESSION_UNKNOWN_REFERENCE
:
11368 name
= name_expr
->unknown_expression()->name();
11371 case EXPRESSION_CONST_REFERENCE
:
11372 name
= static_cast<Const_expression
*>(name_expr
)->name();
11375 case EXPRESSION_TYPE
:
11377 Type
* t
= name_expr
->type();
11378 Named_type
* nt
= t
->named_type();
11386 case EXPRESSION_VAR_REFERENCE
:
11387 name
= name_expr
->var_expression()->name();
11390 case EXPRESSION_FUNC_REFERENCE
:
11391 name
= name_expr
->func_expression()->name();
11394 case EXPRESSION_UNARY
:
11395 // If there is a local variable around with the same name as
11396 // the field, and this occurs in the closure, then the
11397 // parser may turn the field reference into an indirection
11398 // through the closure. FIXME: This is a mess.
11401 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11402 if (ue
->op() == OPERATOR_MULT
)
11404 Field_reference_expression
* fre
=
11405 ue
->operand()->field_reference_expression();
11409 fre
->expr()->type()->deref()->struct_type();
11412 const Struct_field
* sf
= st
->field(fre
->field_index());
11413 name
= sf
->field_name();
11415 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11416 size_t buflen
= strlen(buf
);
11417 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11420 name
= name
.substr(0, name
.length() - buflen
);
11435 error_at(name_expr
->location(), "expected struct field name");
11436 return Expression::make_error(location
);
11439 unsigned int index
;
11440 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11443 error_at(name_expr
->location(), "unknown field %qs in %qs",
11444 Gogo::message_name(name
).c_str(),
11445 (type
->named_type() != NULL
11446 ? type
->named_type()->message_name().c_str()
11447 : "unnamed struct"));
11448 return Expression::make_error(location
);
11450 if (vals
[index
] != NULL
)
11452 error_at(name_expr
->location(),
11453 "duplicate value for field %qs in %qs",
11454 Gogo::message_name(name
).c_str(),
11455 (type
->named_type() != NULL
11456 ? type
->named_type()->message_name().c_str()
11457 : "unnamed struct"));
11458 return Expression::make_error(location
);
11464 Expression_list
* list
= new Expression_list
;
11465 list
->reserve(field_count
);
11466 for (size_t i
= 0; i
< field_count
; ++i
)
11467 list
->push_back(vals
[i
]);
11469 return new Struct_construction_expression(type
, list
, location
);
11472 // Lower an array composite literal.
11475 Composite_literal_expression::lower_array(Type
* type
)
11477 source_location location
= this->location();
11478 if (this->vals_
== NULL
|| !this->has_keys_
)
11479 return this->make_array(type
, this->vals_
);
11481 std::vector
<Expression
*> vals
;
11482 vals
.reserve(this->vals_
->size());
11483 unsigned long index
= 0;
11484 Expression_list::const_iterator p
= this->vals_
->begin();
11485 while (p
!= this->vals_
->end())
11487 Expression
* index_expr
= *p
;
11490 gcc_assert(p
!= this->vals_
->end());
11491 Expression
* val
= *p
;
11495 if (index_expr
!= NULL
)
11500 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
11503 error_at(index_expr
->location(),
11504 "index expression is not integer constant");
11505 return Expression::make_error(location
);
11507 if (mpz_sgn(ival
) < 0)
11510 error_at(index_expr
->location(), "index expression is negative");
11511 return Expression::make_error(location
);
11513 index
= mpz_get_ui(ival
);
11514 if (mpz_cmp_ui(ival
, index
) != 0)
11517 error_at(index_expr
->location(), "index value overflow");
11518 return Expression::make_error(location
);
11523 if (index
== vals
.size())
11524 vals
.push_back(val
);
11527 if (index
> vals
.size())
11529 vals
.reserve(index
+ 32);
11530 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
11532 if (vals
[index
] != NULL
)
11534 error_at((index_expr
!= NULL
11535 ? index_expr
->location()
11536 : val
->location()),
11537 "duplicate value for index %lu",
11539 return Expression::make_error(location
);
11547 size_t size
= vals
.size();
11548 Expression_list
* list
= new Expression_list
;
11549 list
->reserve(size
);
11550 for (size_t i
= 0; i
< size
; ++i
)
11551 list
->push_back(vals
[i
]);
11553 return this->make_array(type
, list
);
11556 // Actually build the array composite literal. This handles
11560 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
11562 source_location location
= this->location();
11563 Array_type
* at
= type
->array_type();
11564 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
11566 size_t size
= vals
== NULL
? 0 : vals
->size();
11568 mpz_init_set_ui(vlen
, size
);
11569 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
11571 at
= Type::make_array_type(at
->element_type(), elen
);
11574 if (at
->length() != NULL
)
11575 return new Fixed_array_construction_expression(type
, vals
, location
);
11577 return new Open_array_construction_expression(type
, vals
, location
);
11580 // Lower a map composite literal.
11583 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
11586 source_location location
= this->location();
11587 if (this->vals_
!= NULL
)
11589 if (!this->has_keys_
)
11591 error_at(location
, "map composite literal must have keys");
11592 return Expression::make_error(location
);
11595 for (Expression_list::iterator p
= this->vals_
->begin();
11596 p
!= this->vals_
->end();
11602 error_at((*p
)->location(),
11603 "map composite literal must have keys for every value");
11604 return Expression::make_error(location
);
11606 // Make sure we have lowered the key; it may not have been
11607 // lowered in order to handle keys for struct composite
11608 // literals. Lower it now to get the right error message.
11609 if ((*p
)->unknown_expression() != NULL
)
11611 (*p
)->unknown_expression()->clear_is_composite_literal_key();
11612 gogo
->lower_expression(function
, &*p
);
11613 gcc_assert((*p
)->is_error_expression());
11614 return Expression::make_error(location
);
11619 return new Map_construction_expression(type
, this->vals_
, location
);
11622 // Make a composite literal expression.
11625 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
11626 Expression_list
* vals
,
11627 source_location location
)
11629 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
11633 // Return whether this expression is a composite literal.
11636 Expression::is_composite_literal() const
11638 switch (this->classification_
)
11640 case EXPRESSION_COMPOSITE_LITERAL
:
11641 case EXPRESSION_STRUCT_CONSTRUCTION
:
11642 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11643 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11644 case EXPRESSION_MAP_CONSTRUCTION
:
11651 // Return whether this expression is a composite literal which is not
11655 Expression::is_nonconstant_composite_literal() const
11657 switch (this->classification_
)
11659 case EXPRESSION_STRUCT_CONSTRUCTION
:
11661 const Struct_construction_expression
*psce
=
11662 static_cast<const Struct_construction_expression
*>(this);
11663 return !psce
->is_constant_struct();
11665 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11667 const Fixed_array_construction_expression
*pace
=
11668 static_cast<const Fixed_array_construction_expression
*>(this);
11669 return !pace
->is_constant_array();
11671 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11673 const Open_array_construction_expression
*pace
=
11674 static_cast<const Open_array_construction_expression
*>(this);
11675 return !pace
->is_constant_array();
11677 case EXPRESSION_MAP_CONSTRUCTION
:
11684 // Return true if this is a reference to a local variable.
11687 Expression::is_local_variable() const
11689 const Var_expression
* ve
= this->var_expression();
11692 const Named_object
* no
= ve
->named_object();
11693 return (no
->is_result_variable()
11694 || (no
->is_variable() && !no
->var_value()->is_global()));
11697 // Class Type_guard_expression.
11702 Type_guard_expression::do_traverse(Traverse
* traverse
)
11704 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
11705 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11706 return TRAVERSE_EXIT
;
11707 return TRAVERSE_CONTINUE
;
11710 // Check types of a type guard expression. The expression must have
11711 // an interface type, but the actual type conversion is checked at run
11715 Type_guard_expression::do_check_types(Gogo
*)
11717 // 6g permits using a type guard with unsafe.pointer; we are
11719 Type
* expr_type
= this->expr_
->type();
11720 if (expr_type
->is_unsafe_pointer_type())
11722 if (this->type_
->points_to() == NULL
11723 && (this->type_
->integer_type() == NULL
11724 || (this->type_
->forwarded()
11725 != Type::lookup_integer_type("uintptr"))))
11726 this->report_error(_("invalid unsafe.Pointer conversion"));
11728 else if (this->type_
->is_unsafe_pointer_type())
11730 if (expr_type
->points_to() == NULL
11731 && (expr_type
->integer_type() == NULL
11732 || (expr_type
->forwarded()
11733 != Type::lookup_integer_type("uintptr"))))
11734 this->report_error(_("invalid unsafe.Pointer conversion"));
11736 else if (expr_type
->interface_type() == NULL
)
11738 if (!expr_type
->is_error_type() && !this->type_
->is_error_type())
11739 this->report_error(_("type assertion only valid for interface types"));
11740 this->set_is_error();
11742 else if (this->type_
->interface_type() == NULL
)
11744 std::string reason
;
11745 if (!expr_type
->interface_type()->implements_interface(this->type_
,
11748 if (!this->type_
->is_error_type())
11750 if (reason
.empty())
11751 this->report_error(_("impossible type assertion: "
11752 "type does not implement interface"));
11754 error_at(this->location(),
11755 ("impossible type assertion: "
11756 "type does not implement interface (%s)"),
11759 this->set_is_error();
11764 // Return a tree for a type guard expression.
11767 Type_guard_expression::do_get_tree(Translate_context
* context
)
11769 Gogo
* gogo
= context
->gogo();
11770 tree expr_tree
= this->expr_
->get_tree(context
);
11771 if (expr_tree
== error_mark_node
)
11772 return error_mark_node
;
11773 Type
* expr_type
= this->expr_
->type();
11774 if ((this->type_
->is_unsafe_pointer_type()
11775 && (expr_type
->points_to() != NULL
11776 || expr_type
->integer_type() != NULL
))
11777 || (expr_type
->is_unsafe_pointer_type()
11778 && this->type_
->points_to() != NULL
))
11779 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
11780 else if (expr_type
->is_unsafe_pointer_type()
11781 && this->type_
->integer_type() != NULL
)
11782 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
11783 else if (this->type_
->interface_type() != NULL
)
11784 return Expression::convert_interface_to_interface(context
, this->type_
,
11785 this->expr_
->type(),
11789 return Expression::convert_for_assignment(context
, this->type_
,
11790 this->expr_
->type(), expr_tree
,
11794 // Make a type guard expression.
11797 Expression::make_type_guard(Expression
* expr
, Type
* type
,
11798 source_location location
)
11800 return new Type_guard_expression(expr
, type
, location
);
11803 // Class Heap_composite_expression.
11805 // When you take the address of a composite literal, it is allocated
11806 // on the heap. This class implements that.
11808 class Heap_composite_expression
: public Expression
11811 Heap_composite_expression(Expression
* expr
, source_location location
)
11812 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
11818 do_traverse(Traverse
* traverse
)
11819 { return Expression::traverse(&this->expr_
, traverse
); }
11823 { return Type::make_pointer_type(this->expr_
->type()); }
11826 do_determine_type(const Type_context
*)
11827 { this->expr_
->determine_type_no_context(); }
11832 return Expression::make_heap_composite(this->expr_
->copy(),
11837 do_get_tree(Translate_context
*);
11839 // We only export global objects, and the parser does not generate
11840 // this in global scope.
11842 do_export(Export
*) const
11843 { gcc_unreachable(); }
11846 // The composite literal which is being put on the heap.
11850 // Return a tree which allocates a composite literal on the heap.
11853 Heap_composite_expression::do_get_tree(Translate_context
* context
)
11855 tree expr_tree
= this->expr_
->get_tree(context
);
11856 if (expr_tree
== error_mark_node
)
11857 return error_mark_node
;
11858 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
11859 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
11860 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
11861 expr_size
, this->location());
11862 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
11863 space
= save_expr(space
);
11864 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
11865 TREE_THIS_NOTRAP(ref
) = 1;
11866 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
11867 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
11869 SET_EXPR_LOCATION(ret
, this->location());
11873 // Allocate a composite literal on the heap.
11876 Expression::make_heap_composite(Expression
* expr
, source_location location
)
11878 return new Heap_composite_expression(expr
, location
);
11881 // Class Receive_expression.
11883 // Return the type of a receive expression.
11886 Receive_expression::do_type()
11888 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
11889 if (channel_type
== NULL
)
11890 return Type::make_error_type();
11891 return channel_type
->element_type();
11894 // Check types for a receive expression.
11897 Receive_expression::do_check_types(Gogo
*)
11899 Type
* type
= this->channel_
->type();
11900 if (type
->is_error_type())
11902 this->set_is_error();
11905 if (type
->channel_type() == NULL
)
11907 this->report_error(_("expected channel"));
11910 if (!type
->channel_type()->may_receive())
11912 this->report_error(_("invalid receive on send-only channel"));
11917 // Get a tree for a receive expression.
11920 Receive_expression::do_get_tree(Translate_context
* context
)
11922 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
11923 gcc_assert(channel_type
!= NULL
);
11924 Type
* element_type
= channel_type
->element_type();
11925 tree element_type_tree
= element_type
->get_tree(context
->gogo());
11927 tree channel
= this->channel_
->get_tree(context
);
11928 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
11929 return error_mark_node
;
11931 return Gogo::receive_from_channel(element_type_tree
, channel
,
11932 this->for_select_
, this->location());
11935 // Make a receive expression.
11937 Receive_expression
*
11938 Expression::make_receive(Expression
* channel
, source_location location
)
11940 return new Receive_expression(channel
, location
);
11943 // Class Send_expression.
11948 Send_expression::do_traverse(Traverse
* traverse
)
11950 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
11951 return TRAVERSE_EXIT
;
11952 return Expression::traverse(&this->val_
, traverse
);
11958 Send_expression::do_type()
11960 return Type::lookup_bool_type();
11966 Send_expression::do_determine_type(const Type_context
*)
11968 this->channel_
->determine_type_no_context();
11970 Type
* type
= this->channel_
->type();
11971 Type_context subcontext
;
11972 if (type
->channel_type() != NULL
)
11973 subcontext
.type
= type
->channel_type()->element_type();
11974 this->val_
->determine_type(&subcontext
);
11980 Send_expression::do_check_types(Gogo
*)
11982 Type
* type
= this->channel_
->type();
11983 if (type
->is_error_type())
11985 this->set_is_error();
11988 Channel_type
* channel_type
= type
->channel_type();
11989 if (channel_type
== NULL
)
11991 error_at(this->location(), "left operand of %<<-%> must be channel");
11992 this->set_is_error();
11995 Type
* element_type
= channel_type
->element_type();
11996 if (element_type
!= NULL
11997 && !Type::are_assignable(element_type
, this->val_
->type(), NULL
))
11999 this->report_error(_("incompatible types in send"));
12002 if (!channel_type
->may_send())
12004 this->report_error(_("invalid send on receive-only channel"));
12009 // Get a tree for a send expression.
12012 Send_expression::do_get_tree(Translate_context
* context
)
12014 tree channel
= this->channel_
->get_tree(context
);
12015 tree val
= this->val_
->get_tree(context
);
12016 if (channel
== error_mark_node
|| val
== error_mark_node
)
12017 return error_mark_node
;
12018 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12019 val
= Expression::convert_for_assignment(context
,
12020 channel_type
->element_type(),
12021 this->val_
->type(),
12024 return Gogo::send_on_channel(channel
, val
, this->is_value_discarded_
,
12025 this->for_select_
, this->location());
12028 // Make a send expression
12031 Expression::make_send(Expression
* channel
, Expression
* val
,
12032 source_location location
)
12034 return new Send_expression(channel
, val
, location
);
12037 // An expression which evaluates to a pointer to the type descriptor
12040 class Type_descriptor_expression
: public Expression
12043 Type_descriptor_expression(Type
* type
, source_location location
)
12044 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
12051 { return Type::make_type_descriptor_ptr_type(); }
12054 do_determine_type(const Type_context
*)
12062 do_get_tree(Translate_context
* context
)
12063 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
12066 // The type for which this is the descriptor.
12070 // Make a type descriptor expression.
12073 Expression::make_type_descriptor(Type
* type
, source_location location
)
12075 return new Type_descriptor_expression(type
, location
);
12078 // An expression which evaluates to some characteristic of a type.
12079 // This is only used to initialize fields of a type descriptor. Using
12080 // a new expression class is slightly inefficient but gives us a good
12081 // separation between the frontend and the middle-end with regard to
12082 // how types are laid out.
12084 class Type_info_expression
: public Expression
12087 Type_info_expression(Type
* type
, Type_info type_info
)
12088 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
12089 type_(type
), type_info_(type_info
)
12097 do_determine_type(const Type_context
*)
12105 do_get_tree(Translate_context
* context
);
12108 // The type for which we are getting information.
12110 // What information we want.
12111 Type_info type_info_
;
12114 // The type is chosen to match what the type descriptor struct
12118 Type_info_expression::do_type()
12120 switch (this->type_info_
)
12122 case TYPE_INFO_SIZE
:
12123 return Type::lookup_integer_type("uintptr");
12124 case TYPE_INFO_ALIGNMENT
:
12125 case TYPE_INFO_FIELD_ALIGNMENT
:
12126 return Type::lookup_integer_type("uint8");
12132 // Return type information in GENERIC.
12135 Type_info_expression::do_get_tree(Translate_context
* context
)
12137 tree type_tree
= this->type_
->get_tree(context
->gogo());
12138 if (type_tree
== error_mark_node
)
12139 return error_mark_node
;
12141 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12142 gcc_assert(val_type_tree
!= error_mark_node
);
12144 if (this->type_info_
== TYPE_INFO_SIZE
)
12145 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12146 TYPE_SIZE_UNIT(type_tree
));
12150 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12151 val
= go_type_alignment(type_tree
);
12153 val
= go_field_alignment(type_tree
);
12154 return build_int_cstu(val_type_tree
, val
);
12158 // Make a type info expression.
12161 Expression::make_type_info(Type
* type
, Type_info type_info
)
12163 return new Type_info_expression(type
, type_info
);
12166 // An expression which evaluates to the offset of a field within a
12167 // struct. This, like Type_info_expression, q.v., is only used to
12168 // initialize fields of a type descriptor.
12170 class Struct_field_offset_expression
: public Expression
12173 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12174 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12175 type_(type
), field_(field
)
12181 { return Type::lookup_integer_type("uintptr"); }
12184 do_determine_type(const Type_context
*)
12192 do_get_tree(Translate_context
* context
);
12195 // The type of the struct.
12196 Struct_type
* type_
;
12198 const Struct_field
* field_
;
12201 // Return a struct field offset in GENERIC.
12204 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12206 tree type_tree
= this->type_
->get_tree(context
->gogo());
12207 if (type_tree
== error_mark_node
)
12208 return error_mark_node
;
12210 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12211 gcc_assert(val_type_tree
!= error_mark_node
);
12213 const Struct_field_list
* fields
= this->type_
->fields();
12214 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12215 Struct_field_list::const_iterator p
;
12216 for (p
= fields
->begin();
12217 p
!= fields
->end();
12218 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12220 gcc_assert(struct_field_tree
!= NULL_TREE
);
12221 if (&*p
== this->field_
)
12224 gcc_assert(&*p
== this->field_
);
12226 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12227 byte_position(struct_field_tree
));
12230 // Make an expression for a struct field offset.
12233 Expression::make_struct_field_offset(Struct_type
* type
,
12234 const Struct_field
* field
)
12236 return new Struct_field_offset_expression(type
, field
);
12239 // An expression which evaluates to the address of an unnamed label.
12241 class Label_addr_expression
: public Expression
12244 Label_addr_expression(Label
* label
, source_location location
)
12245 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12252 { return Type::make_pointer_type(Type::make_void_type()); }
12255 do_determine_type(const Type_context
*)
12260 { return new Label_addr_expression(this->label_
, this->location()); }
12263 do_get_tree(Translate_context
*)
12264 { return this->label_
->get_addr(this->location()); }
12267 // The label whose address we are taking.
12271 // Make an expression for the address of an unnamed label.
12274 Expression::make_label_addr(Label
* label
, source_location location
)
12276 return new Label_addr_expression(label
, location
);
12279 // Import an expression. This comes at the end in order to see the
12280 // various class definitions.
12283 Expression::import_expression(Import
* imp
)
12285 int c
= imp
->peek_char();
12286 if (imp
->match_c_string("- ")
12287 || imp
->match_c_string("! ")
12288 || imp
->match_c_string("^ "))
12289 return Unary_expression::do_import(imp
);
12291 return Binary_expression::do_import(imp
);
12292 else if (imp
->match_c_string("true")
12293 || imp
->match_c_string("false"))
12294 return Boolean_expression::do_import(imp
);
12296 return String_expression::do_import(imp
);
12297 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12299 // This handles integers, floats and complex constants.
12300 return Integer_expression::do_import(imp
);
12302 else if (imp
->match_c_string("nil"))
12303 return Nil_expression::do_import(imp
);
12304 else if (imp
->match_c_string("convert"))
12305 return Type_conversion_expression::do_import(imp
);
12308 error_at(imp
->location(), "import error: expected expression");
12309 return Expression::make_error(imp
->location());
12313 // Class Expression_list.
12315 // Traverse the list.
12318 Expression_list::traverse(Traverse
* traverse
)
12320 for (Expression_list::iterator p
= this->begin();
12326 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12327 return TRAVERSE_EXIT
;
12330 return TRAVERSE_CONTINUE
;
12336 Expression_list::copy()
12338 Expression_list
* ret
= new Expression_list();
12339 for (Expression_list::iterator p
= this->begin();
12344 ret
->push_back(NULL
);
12346 ret
->push_back((*p
)->copy());
12351 // Return whether an expression list has an error expression.
12354 Expression_list::contains_error() const
12356 for (Expression_list::const_iterator p
= this->begin();
12359 if (*p
!= NULL
&& (*p
)->is_error_expression())