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
)
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
2355 // Lower a constant expression. This is where we convert the
2356 // predeclared constant iota into an integer value.
2359 Const_expression::do_lower(Gogo
* gogo
, Named_object
*, int iota_value
)
2361 if (this->constant_
->const_value()->expr()->classification()
2364 if (iota_value
== -1)
2366 error_at(this->location(),
2367 "iota is only defined in const declarations");
2371 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2372 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2378 // Make sure that the constant itself has been lowered.
2379 gogo
->lower_constant(this->constant_
);
2384 // Return an integer constant value.
2387 Const_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
2391 if (this->type_
!= NULL
)
2392 ctype
= this->type_
;
2394 ctype
= this->constant_
->const_value()->type();
2395 if (ctype
!= NULL
&& ctype
->integer_type() == NULL
)
2398 Expression
* e
= this->constant_
->const_value()->expr();
2400 bool r
= e
->integer_constant_value(iota_is_constant
, val
, &t
);
2404 && !Integer_expression::check_constant(val
, ctype
, this->location()))
2407 *ptype
= ctype
!= NULL
? ctype
: t
;
2411 // Return a floating point constant value.
2414 Const_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
2417 if (this->type_
!= NULL
)
2418 ctype
= this->type_
;
2420 ctype
= this->constant_
->const_value()->type();
2421 if (ctype
!= NULL
&& ctype
->float_type() == NULL
)
2425 bool r
= this->constant_
->const_value()->expr()->float_constant_value(val
,
2427 if (r
&& ctype
!= NULL
)
2429 if (!Float_expression::check_constant(val
, ctype
, this->location()))
2431 Float_expression::constrain_float(val
, ctype
);
2433 *ptype
= ctype
!= NULL
? ctype
: t
;
2437 // Return a complex constant value.
2440 Const_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2444 if (this->type_
!= NULL
)
2445 ctype
= this->type_
;
2447 ctype
= this->constant_
->const_value()->type();
2448 if (ctype
!= NULL
&& ctype
->complex_type() == NULL
)
2452 bool r
= this->constant_
->const_value()->expr()->complex_constant_value(real
,
2455 if (r
&& ctype
!= NULL
)
2457 if (!Complex_expression::check_constant(real
, imag
, ctype
,
2460 Complex_expression::constrain_complex(real
, imag
, ctype
);
2462 *ptype
= ctype
!= NULL
? ctype
: t
;
2466 // Return the type of the const reference.
2469 Const_expression::do_type()
2471 if (this->type_
!= NULL
)
2473 Named_constant
* nc
= this->constant_
->const_value();
2474 Type
* ret
= nc
->type();
2477 // During parsing, a named constant may have a NULL type, but we
2478 // must not return a NULL type here.
2479 return nc
->expr()->type();
2482 // Set the type of the const reference.
2485 Const_expression::do_determine_type(const Type_context
* context
)
2487 Type
* ctype
= this->constant_
->const_value()->type();
2488 Type
* cetype
= (ctype
!= NULL
2490 : this->constant_
->const_value()->expr()->type());
2491 if (ctype
!= NULL
&& !ctype
->is_abstract())
2493 else if (context
->type
!= NULL
2494 && (context
->type
->integer_type() != NULL
2495 || context
->type
->float_type() != NULL
2496 || context
->type
->complex_type() != NULL
)
2497 && (cetype
->integer_type() != NULL
2498 || cetype
->float_type() != NULL
2499 || cetype
->complex_type() != NULL
))
2500 this->type_
= context
->type
;
2501 else if (context
->type
!= NULL
2502 && context
->type
->is_string_type()
2503 && cetype
->is_string_type())
2504 this->type_
= context
->type
;
2505 else if (context
->type
!= NULL
2506 && context
->type
->is_boolean_type()
2507 && cetype
->is_boolean_type())
2508 this->type_
= context
->type
;
2509 else if (!context
->may_be_abstract
)
2511 if (cetype
->is_abstract())
2512 cetype
= cetype
->make_non_abstract_type();
2513 this->type_
= cetype
;
2517 // Check types of a const reference.
2520 Const_expression::do_check_types(Gogo
*)
2522 if (this->type_
== NULL
|| this->type_
->is_abstract())
2525 // Check for integer overflow.
2526 if (this->type_
->integer_type() != NULL
)
2531 if (!this->integer_constant_value(true, ival
, &dummy
))
2535 Expression
* cexpr
= this->constant_
->const_value()->expr();
2536 if (cexpr
->float_constant_value(fval
, &dummy
))
2538 if (!mpfr_integer_p(fval
))
2539 this->report_error(_("floating point constant "
2540 "truncated to integer"));
2543 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2544 Integer_expression::check_constant(ival
, this->type_
,
2554 // Return a tree for the const reference.
2557 Const_expression::do_get_tree(Translate_context
* context
)
2559 Gogo
* gogo
= context
->gogo();
2561 if (this->type_
== NULL
)
2562 type_tree
= NULL_TREE
;
2565 type_tree
= this->type_
->get_tree(gogo
);
2566 if (type_tree
== error_mark_node
)
2567 return error_mark_node
;
2570 // If the type has been set for this expression, but the underlying
2571 // object is an abstract int or float, we try to get the abstract
2572 // value. Otherwise we may lose something in the conversion.
2573 if (this->type_
!= NULL
2574 && this->constant_
->const_value()->type()->is_abstract())
2576 Expression
* expr
= this->constant_
->const_value()->expr();
2580 if (expr
->integer_constant_value(true, ival
, &t
))
2582 tree ret
= Expression::integer_constant_tree(ival
, type_tree
);
2590 if (expr
->float_constant_value(fval
, &t
))
2592 tree ret
= Expression::float_constant_tree(fval
, type_tree
);
2599 if (expr
->complex_constant_value(fval
, imag
, &t
))
2601 tree ret
= Expression::complex_constant_tree(fval
, imag
, type_tree
);
2610 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2611 if (this->type_
== NULL
2612 || const_tree
== error_mark_node
2613 || TREE_TYPE(const_tree
) == error_mark_node
)
2617 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2618 ret
= fold_convert(type_tree
, const_tree
);
2619 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2620 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2621 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2622 ret
= fold(convert_to_real(type_tree
, const_tree
));
2623 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2624 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2630 // Make a reference to a constant in an expression.
2633 Expression::make_const_reference(Named_object
* constant
,
2634 source_location location
)
2636 return new Const_expression(constant
, location
);
2641 class Nil_expression
: public Expression
2644 Nil_expression(source_location location
)
2645 : Expression(EXPRESSION_NIL
, location
)
2653 do_is_constant() const
2658 { return Type::make_nil_type(); }
2661 do_determine_type(const Type_context
*)
2669 do_get_tree(Translate_context
*)
2670 { return null_pointer_node
; }
2673 do_export(Export
* exp
) const
2674 { exp
->write_c_string("nil"); }
2677 // Import a nil expression.
2680 Nil_expression::do_import(Import
* imp
)
2682 imp
->require_c_string("nil");
2683 return Expression::make_nil(imp
->location());
2686 // Make a nil expression.
2689 Expression::make_nil(source_location location
)
2691 return new Nil_expression(location
);
2694 // The value of the predeclared constant iota. This is little more
2695 // than a marker. This will be lowered to an integer in
2696 // Const_expression::do_lower, which is where we know the value that
2699 class Iota_expression
: public Parser_expression
2702 Iota_expression(source_location location
)
2703 : Parser_expression(EXPRESSION_IOTA
, location
)
2708 do_lower(Gogo
*, Named_object
*, int)
2709 { gcc_unreachable(); }
2711 // There should only ever be one of these.
2714 { gcc_unreachable(); }
2717 // Make an iota expression. This is only called for one case: the
2718 // value of the predeclared constant iota.
2721 Expression::make_iota()
2723 static Iota_expression
iota_expression(UNKNOWN_LOCATION
);
2724 return &iota_expression
;
2727 // A type conversion expression.
2729 class Type_conversion_expression
: public Expression
2732 Type_conversion_expression(Type
* type
, Expression
* expr
,
2733 source_location location
)
2734 : Expression(EXPRESSION_CONVERSION
, location
),
2735 type_(type
), expr_(expr
), may_convert_function_types_(false)
2738 // Return the type to which we are converting.
2741 { return this->type_
; }
2743 // Return the expression which we are converting.
2746 { return this->expr_
; }
2748 // Permit converting from one function type to another. This is
2749 // used internally for method expressions.
2751 set_may_convert_function_types()
2753 this->may_convert_function_types_
= true;
2756 // Import a type conversion expression.
2762 do_traverse(Traverse
* traverse
);
2765 do_lower(Gogo
*, Named_object
*, int);
2768 do_is_constant() const
2769 { return this->expr_
->is_constant(); }
2772 do_integer_constant_value(bool, mpz_t
, Type
**) const;
2775 do_float_constant_value(mpfr_t
, Type
**) const;
2778 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
2781 do_string_constant_value(std::string
*) const;
2785 { return this->type_
; }
2788 do_determine_type(const Type_context
*)
2790 Type_context
subcontext(this->type_
, false);
2791 this->expr_
->determine_type(&subcontext
);
2795 do_check_types(Gogo
*);
2800 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
2805 do_get_tree(Translate_context
* context
);
2808 do_export(Export
*) const;
2811 // The type to convert to.
2813 // The expression to convert.
2815 // True if this is permitted to convert function types. This is
2816 // used internally for method expressions.
2817 bool may_convert_function_types_
;
2823 Type_conversion_expression::do_traverse(Traverse
* traverse
)
2825 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
2826 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
2827 return TRAVERSE_EXIT
;
2828 return TRAVERSE_CONTINUE
;
2831 // Convert to a constant at lowering time.
2834 Type_conversion_expression::do_lower(Gogo
*, Named_object
*, int)
2836 Type
* type
= this->type_
;
2837 Expression
* val
= this->expr_
;
2838 source_location location
= this->location();
2840 if (type
->integer_type() != NULL
)
2845 if (val
->integer_constant_value(false, ival
, &dummy
))
2847 if (!Integer_expression::check_constant(ival
, type
, location
))
2848 mpz_set_ui(ival
, 0);
2849 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2856 if (val
->float_constant_value(fval
, &dummy
))
2858 if (!mpfr_integer_p(fval
))
2861 "floating point constant truncated to integer");
2862 return Expression::make_error(location
);
2864 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2865 if (!Integer_expression::check_constant(ival
, type
, location
))
2866 mpz_set_ui(ival
, 0);
2867 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2876 if (type
->float_type() != NULL
)
2881 if (val
->float_constant_value(fval
, &dummy
))
2883 if (!Float_expression::check_constant(fval
, type
, location
))
2884 mpfr_set_ui(fval
, 0, GMP_RNDN
);
2885 Float_expression::constrain_float(fval
, type
);
2886 Expression
*ret
= Expression::make_float(&fval
, type
, location
);
2893 if (type
->complex_type() != NULL
)
2900 if (val
->complex_constant_value(real
, imag
, &dummy
))
2902 if (!Complex_expression::check_constant(real
, imag
, type
, location
))
2904 mpfr_set_ui(real
, 0, GMP_RNDN
);
2905 mpfr_set_ui(imag
, 0, GMP_RNDN
);
2907 Complex_expression::constrain_complex(real
, imag
, type
);
2908 Expression
* ret
= Expression::make_complex(&real
, &imag
, type
,
2918 if (type
->is_open_array_type() && type
->named_type() == NULL
)
2920 Type
* element_type
= type
->array_type()->element_type()->forwarded();
2921 bool is_byte
= element_type
== Type::lookup_integer_type("uint8");
2922 bool is_int
= element_type
== Type::lookup_integer_type("int");
2923 if (is_byte
|| is_int
)
2926 if (val
->string_constant_value(&s
))
2928 Expression_list
* vals
= new Expression_list();
2931 for (std::string::const_iterator p
= s
.begin();
2936 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
2937 Expression
* v
= Expression::make_integer(&val
,
2946 const char *p
= s
.data();
2947 const char *pend
= s
.data() + s
.length();
2951 int adv
= Lex::fetch_char(p
, &c
);
2954 warning_at(this->location(), 0,
2955 "invalid UTF-8 encoding");
2960 mpz_init_set_ui(val
, c
);
2961 Expression
* v
= Expression::make_integer(&val
,
2969 return Expression::make_slice_composite_literal(type
, vals
,
2978 // Return the constant integer value if there is one.
2981 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant
,
2985 if (this->type_
->integer_type() == NULL
)
2991 if (this->expr_
->integer_constant_value(iota_is_constant
, ival
, &dummy
))
2993 if (!Integer_expression::check_constant(ival
, this->type_
,
3001 *ptype
= this->type_
;
3008 if (this->expr_
->float_constant_value(fval
, &dummy
))
3010 mpfr_get_z(val
, fval
, GMP_RNDN
);
3012 if (!Integer_expression::check_constant(val
, this->type_
,
3015 *ptype
= this->type_
;
3023 // Return the constant floating point value if there is one.
3026 Type_conversion_expression::do_float_constant_value(mpfr_t val
,
3029 if (this->type_
->float_type() == NULL
)
3035 if (this->expr_
->float_constant_value(fval
, &dummy
))
3037 if (!Float_expression::check_constant(fval
, this->type_
,
3043 mpfr_set(val
, fval
, GMP_RNDN
);
3045 Float_expression::constrain_float(val
, this->type_
);
3046 *ptype
= this->type_
;
3054 // Return the constant complex value if there is one.
3057 Type_conversion_expression::do_complex_constant_value(mpfr_t real
,
3061 if (this->type_
->complex_type() == NULL
)
3069 if (this->expr_
->complex_constant_value(rval
, ival
, &dummy
))
3071 if (!Complex_expression::check_constant(rval
, ival
, this->type_
,
3078 mpfr_set(real
, rval
, GMP_RNDN
);
3079 mpfr_set(imag
, ival
, GMP_RNDN
);
3082 Complex_expression::constrain_complex(real
, imag
, this->type_
);
3083 *ptype
= this->type_
;
3092 // Return the constant string value if there is one.
3095 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3097 if (this->type_
->is_string_type()
3098 && this->expr_
->type()->integer_type() != NULL
)
3103 if (this->expr_
->integer_constant_value(false, ival
, &dummy
))
3105 unsigned long ulval
= mpz_get_ui(ival
);
3106 if (mpz_cmp_ui(ival
, ulval
) == 0)
3108 Lex::append_char(ulval
, true, val
, this->location());
3116 // FIXME: Could handle conversion from const []int here.
3121 // Check that types are convertible.
3124 Type_conversion_expression::do_check_types(Gogo
*)
3126 Type
* type
= this->type_
;
3127 Type
* expr_type
= this->expr_
->type();
3130 if (this->may_convert_function_types_
3131 && type
->function_type() != NULL
3132 && expr_type
->function_type() != NULL
)
3135 if (Type::are_convertible(type
, expr_type
, &reason
))
3138 error_at(this->location(), "%s", reason
.c_str());
3139 this->set_is_error();
3142 // Get a tree for a type conversion.
3145 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3147 Gogo
* gogo
= context
->gogo();
3148 tree type_tree
= this->type_
->get_tree(gogo
);
3149 tree expr_tree
= this->expr_
->get_tree(context
);
3151 if (type_tree
== error_mark_node
3152 || expr_tree
== error_mark_node
3153 || TREE_TYPE(expr_tree
) == error_mark_node
)
3154 return error_mark_node
;
3156 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3157 return fold_convert(type_tree
, expr_tree
);
3159 Type
* type
= this->type_
;
3160 Type
* expr_type
= this->expr_
->type();
3162 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3163 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3164 expr_tree
, this->location());
3165 else if (type
->integer_type() != NULL
)
3167 if (expr_type
->integer_type() != NULL
3168 || expr_type
->float_type() != NULL
3169 || expr_type
->is_unsafe_pointer_type())
3170 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3174 else if (type
->float_type() != NULL
)
3176 if (expr_type
->integer_type() != NULL
3177 || expr_type
->float_type() != NULL
)
3178 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3182 else if (type
->complex_type() != NULL
)
3184 if (expr_type
->complex_type() != NULL
)
3185 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3189 else if (type
->is_string_type()
3190 && expr_type
->integer_type() != NULL
)
3192 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3193 if (host_integerp(expr_tree
, 0))
3195 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3197 Lex::append_char(intval
, true, &s
, this->location());
3198 Expression
* se
= Expression::make_string(s
, this->location());
3199 return se
->get_tree(context
);
3202 static tree int_to_string_fndecl
;
3203 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3205 "__go_int_to_string",
3209 fold_convert(integer_type_node
, expr_tree
));
3211 else if (type
->is_string_type()
3212 && (expr_type
->array_type() != NULL
3213 || (expr_type
->points_to() != NULL
3214 && expr_type
->points_to()->array_type() != NULL
)))
3216 Type
* t
= expr_type
;
3217 if (t
->points_to() != NULL
)
3220 expr_tree
= build_fold_indirect_ref(expr_tree
);
3222 if (!DECL_P(expr_tree
))
3223 expr_tree
= save_expr(expr_tree
);
3224 Array_type
* a
= t
->array_type();
3225 Type
* e
= a
->element_type()->forwarded();
3226 gcc_assert(e
->integer_type() != NULL
);
3227 tree valptr
= fold_convert(const_ptr_type_node
,
3228 a
->value_pointer_tree(gogo
, expr_tree
));
3229 tree len
= a
->length_tree(gogo
, expr_tree
);
3230 len
= fold_convert_loc(this->location(), size_type_node
, len
);
3231 if (e
->integer_type()->is_unsigned()
3232 && e
->integer_type()->bits() == 8)
3234 static tree byte_array_to_string_fndecl
;
3235 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3237 "__go_byte_array_to_string",
3240 const_ptr_type_node
,
3247 gcc_assert(e
== Type::lookup_integer_type("int"));
3248 static tree int_array_to_string_fndecl
;
3249 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3251 "__go_int_array_to_string",
3254 const_ptr_type_node
,
3260 else if (type
->is_open_array_type() && expr_type
->is_string_type())
3262 Type
* e
= type
->array_type()->element_type()->forwarded();
3263 gcc_assert(e
->integer_type() != NULL
);
3264 if (e
->integer_type()->is_unsigned()
3265 && e
->integer_type()->bits() == 8)
3267 static tree string_to_byte_array_fndecl
;
3268 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3270 "__go_string_to_byte_array",
3273 TREE_TYPE(expr_tree
),
3278 gcc_assert(e
== Type::lookup_integer_type("int"));
3279 static tree string_to_int_array_fndecl
;
3280 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3282 "__go_string_to_int_array",
3285 TREE_TYPE(expr_tree
),
3289 else if ((type
->is_unsafe_pointer_type()
3290 && expr_type
->points_to() != NULL
)
3291 || (expr_type
->is_unsafe_pointer_type()
3292 && type
->points_to() != NULL
))
3293 ret
= fold_convert(type_tree
, expr_tree
);
3294 else if (type
->is_unsafe_pointer_type()
3295 && expr_type
->integer_type() != NULL
)
3296 ret
= convert_to_pointer(type_tree
, expr_tree
);
3297 else if (this->may_convert_function_types_
3298 && type
->function_type() != NULL
3299 && expr_type
->function_type() != NULL
)
3300 ret
= fold_convert_loc(this->location(), type_tree
, expr_tree
);
3302 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3303 expr_tree
, this->location());
3308 // Output a type conversion in a constant expression.
3311 Type_conversion_expression::do_export(Export
* exp
) const
3313 exp
->write_c_string("convert(");
3314 exp
->write_type(this->type_
);
3315 exp
->write_c_string(", ");
3316 this->expr_
->export_expression(exp
);
3317 exp
->write_c_string(")");
3320 // Import a type conversion or a struct construction.
3323 Type_conversion_expression::do_import(Import
* imp
)
3325 imp
->require_c_string("convert(");
3326 Type
* type
= imp
->read_type();
3327 imp
->require_c_string(", ");
3328 Expression
* val
= Expression::import_expression(imp
);
3329 imp
->require_c_string(")");
3330 return Expression::make_cast(type
, val
, imp
->location());
3333 // Make a type cast expression.
3336 Expression::make_cast(Type
* type
, Expression
* val
, source_location location
)
3338 if (type
->is_error_type() || val
->is_error_expression())
3339 return Expression::make_error(location
);
3340 return new Type_conversion_expression(type
, val
, location
);
3343 // Unary expressions.
3345 class Unary_expression
: public Expression
3348 Unary_expression(Operator op
, Expression
* expr
, source_location location
)
3349 : Expression(EXPRESSION_UNARY
, location
),
3350 op_(op
), escapes_(true), expr_(expr
)
3353 // Return the operator.
3356 { return this->op_
; }
3358 // Return the operand.
3361 { return this->expr_
; }
3363 // Record that an address expression does not escape.
3365 set_does_not_escape()
3367 gcc_assert(this->op_
== OPERATOR_AND
);
3368 this->escapes_
= false;
3371 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3372 // could be done, false if not.
3374 eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3377 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3378 // could be done, false if not.
3380 eval_float(Operator op
, mpfr_t uval
, mpfr_t val
);
3382 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3383 // true if this could be done, false if not.
3385 eval_complex(Operator op
, mpfr_t ureal
, mpfr_t uimag
, mpfr_t real
,
3393 do_traverse(Traverse
* traverse
)
3394 { return Expression::traverse(&this->expr_
, traverse
); }
3397 do_lower(Gogo
*, Named_object
*, int);
3400 do_is_constant() const;
3403 do_integer_constant_value(bool, mpz_t
, Type
**) const;
3406 do_float_constant_value(mpfr_t
, Type
**) const;
3409 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
3415 do_determine_type(const Type_context
*);
3418 do_check_types(Gogo
*);
3423 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3428 do_is_addressable() const
3429 { return this->op_
== OPERATOR_MULT
; }
3432 do_get_tree(Translate_context
*);
3435 do_export(Export
*) const;
3438 // The unary operator to apply.
3440 // Normally true. False if this is an address expression which does
3441 // not escape the current function.
3447 // If we are taking the address of a composite literal, and the
3448 // contents are not constant, then we want to make a heap composite
3452 Unary_expression::do_lower(Gogo
*, Named_object
*, int)
3454 source_location loc
= this->location();
3455 Operator op
= this->op_
;
3456 Expression
* expr
= this->expr_
;
3458 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3459 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3461 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3462 // moving x to the heap. FIXME: Is it worth doing a real escape
3463 // analysis here? This case is found in math/unsafe.go and is
3464 // therefore worth special casing.
3465 if (op
== OPERATOR_MULT
)
3467 Expression
* e
= expr
;
3468 while (e
->classification() == EXPRESSION_CONVERSION
)
3470 Type_conversion_expression
* te
3471 = static_cast<Type_conversion_expression
*>(e
);
3475 if (e
->classification() == EXPRESSION_UNARY
)
3477 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3478 if (ue
->op_
== OPERATOR_AND
)
3485 ue
->set_does_not_escape();
3490 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
3491 || op
== OPERATOR_NOT
|| op
== OPERATOR_XOR
)
3493 Expression
* ret
= NULL
;
3498 if (expr
->integer_constant_value(false, eval
, &etype
))
3502 if (Unary_expression::eval_integer(op
, etype
, eval
, val
, loc
))
3503 ret
= Expression::make_integer(&val
, etype
, loc
);
3510 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
)
3515 if (expr
->float_constant_value(fval
, &ftype
))
3519 if (Unary_expression::eval_float(op
, fval
, val
))
3520 ret
= Expression::make_float(&val
, ftype
, loc
);
3531 if (expr
->complex_constant_value(fval
, ival
, &ftype
))
3537 if (Unary_expression::eval_complex(op
, fval
, ival
, real
, imag
))
3538 ret
= Expression::make_complex(&real
, &imag
, ftype
, loc
);
3552 // Return whether a unary expression is a constant.
3555 Unary_expression::do_is_constant() const
3557 if (this->op_
== OPERATOR_MULT
)
3559 // Indirecting through a pointer is only constant if the object
3560 // to which the expression points is constant, but we currently
3561 // have no way to determine that.
3564 else if (this->op_
== OPERATOR_AND
)
3566 // Taking the address of a variable is constant if it is a
3567 // global variable, not constant otherwise. In other cases
3568 // taking the address is probably not a constant.
3569 Var_expression
* ve
= this->expr_
->var_expression();
3572 Named_object
* no
= ve
->named_object();
3573 return no
->is_variable() && no
->var_value()->is_global();
3578 return this->expr_
->is_constant();
3581 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3582 // UVAL, if known; it may be NULL. Return true if this could be done,
3586 Unary_expression::eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3587 source_location location
)
3594 case OPERATOR_MINUS
:
3596 return Integer_expression::check_constant(val
, utype
, location
);
3598 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3602 || utype
->integer_type() == NULL
3603 || utype
->integer_type()->is_abstract())
3607 // The number of HOST_WIDE_INTs that it takes to represent
3609 size_t count
= ((mpz_sizeinbase(uval
, 2)
3610 + HOST_BITS_PER_WIDE_INT
3612 / HOST_BITS_PER_WIDE_INT
);
3614 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3615 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3618 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3619 gcc_assert(ecount
<= count
);
3621 // Trim down to the number of words required by the type.
3622 size_t obits
= utype
->integer_type()->bits();
3623 if (!utype
->integer_type()->is_unsigned())
3625 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3626 / HOST_BITS_PER_WIDE_INT
);
3627 gcc_assert(ocount
<= ocount
);
3629 for (size_t i
= 0; i
< ocount
; ++i
)
3632 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3634 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3637 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3641 return Integer_expression::check_constant(val
, utype
, location
);
3650 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3651 // could be done, false if not.
3654 Unary_expression::eval_float(Operator op
, mpfr_t uval
, mpfr_t val
)
3659 mpfr_set(val
, uval
, GMP_RNDN
);
3661 case OPERATOR_MINUS
:
3662 mpfr_neg(val
, uval
, GMP_RNDN
);
3674 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3675 // if this could be done, false if not.
3678 Unary_expression::eval_complex(Operator op
, mpfr_t rval
, mpfr_t ival
,
3679 mpfr_t real
, mpfr_t imag
)
3684 mpfr_set(real
, rval
, GMP_RNDN
);
3685 mpfr_set(imag
, ival
, GMP_RNDN
);
3687 case OPERATOR_MINUS
:
3688 mpfr_neg(real
, rval
, GMP_RNDN
);
3689 mpfr_neg(imag
, ival
, GMP_RNDN
);
3701 // Return the integral constant value of a unary expression, if it has one.
3704 Unary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
3710 if (!this->expr_
->integer_constant_value(iota_is_constant
, uval
, ptype
))
3713 ret
= Unary_expression::eval_integer(this->op_
, *ptype
, uval
, val
,
3719 // Return the floating point constant value of a unary expression, if
3723 Unary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
3728 if (!this->expr_
->float_constant_value(uval
, ptype
))
3731 ret
= Unary_expression::eval_float(this->op_
, uval
, val
);
3736 // Return the complex constant value of a unary expression, if it has
3740 Unary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
3748 if (!this->expr_
->complex_constant_value(rval
, ival
, ptype
))
3751 ret
= Unary_expression::eval_complex(this->op_
, rval
, ival
, real
, imag
);
3757 // Return the type of a unary expression.
3760 Unary_expression::do_type()
3765 case OPERATOR_MINUS
:
3768 return this->expr_
->type();
3771 return Type::make_pointer_type(this->expr_
->type());
3775 Type
* subtype
= this->expr_
->type();
3776 Type
* points_to
= subtype
->points_to();
3777 if (points_to
== NULL
)
3778 return Type::make_error_type();
3787 // Determine abstract types for a unary expression.
3790 Unary_expression::do_determine_type(const Type_context
* context
)
3795 case OPERATOR_MINUS
:
3798 this->expr_
->determine_type(context
);
3802 // Taking the address of something.
3804 Type
* subtype
= (context
->type
== NULL
3806 : context
->type
->points_to());
3807 Type_context
subcontext(subtype
, false);
3808 this->expr_
->determine_type(&subcontext
);
3813 // Indirecting through a pointer.
3815 Type
* subtype
= (context
->type
== NULL
3817 : Type::make_pointer_type(context
->type
));
3818 Type_context
subcontext(subtype
, false);
3819 this->expr_
->determine_type(&subcontext
);
3828 // Check types for a unary expression.
3831 Unary_expression::do_check_types(Gogo
*)
3833 Type
* type
= this->expr_
->type();
3834 if (type
->is_error_type())
3836 this->set_is_error();
3843 case OPERATOR_MINUS
:
3844 if (type
->integer_type() == NULL
3845 && type
->float_type() == NULL
3846 && type
->complex_type() == NULL
)
3847 this->report_error(_("expected numeric type"));
3852 if (type
->integer_type() == NULL
3853 && !type
->is_boolean_type())
3854 this->report_error(_("expected integer or boolean type"));
3858 if (!this->expr_
->is_addressable())
3859 this->report_error(_("invalid operand for unary %<&%>"));
3861 this->expr_
->address_taken(this->escapes_
);
3865 // Indirecting through a pointer.
3866 if (type
->points_to() == NULL
)
3867 this->report_error(_("expected pointer"));
3875 // Get a tree for a unary expression.
3878 Unary_expression::do_get_tree(Translate_context
* context
)
3880 tree expr
= this->expr_
->get_tree(context
);
3881 if (expr
== error_mark_node
)
3882 return error_mark_node
;
3884 source_location loc
= this->location();
3890 case OPERATOR_MINUS
:
3892 tree type
= TREE_TYPE(expr
);
3893 tree compute_type
= excess_precision_type(type
);
3894 if (compute_type
!= NULL_TREE
)
3895 expr
= ::convert(compute_type
, expr
);
3896 tree ret
= fold_build1_loc(loc
, NEGATE_EXPR
,
3897 (compute_type
!= NULL_TREE
3901 if (compute_type
!= NULL_TREE
)
3902 ret
= ::convert(type
, ret
);
3907 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
3908 return fold_build1_loc(loc
, TRUTH_NOT_EXPR
, TREE_TYPE(expr
), expr
);
3910 return fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, expr
,
3911 build_int_cst(TREE_TYPE(expr
), 0));
3914 return fold_build1_loc(loc
, BIT_NOT_EXPR
, TREE_TYPE(expr
), expr
);
3917 // We should not see a non-constant constructor here; cases
3918 // where we would see one should have been moved onto the heap
3919 // at parse time. Taking the address of a nonconstant
3920 // constructor will not do what the programmer expects.
3921 gcc_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
3922 gcc_assert(TREE_CODE(expr
) != ADDR_EXPR
);
3924 // Build a decl for a constant constructor.
3925 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
3927 tree decl
= build_decl(this->location(), VAR_DECL
,
3928 create_tmp_var_name("C"), TREE_TYPE(expr
));
3929 DECL_EXTERNAL(decl
) = 0;
3930 TREE_PUBLIC(decl
) = 0;
3931 TREE_READONLY(decl
) = 1;
3932 TREE_CONSTANT(decl
) = 1;
3933 TREE_STATIC(decl
) = 1;
3934 TREE_ADDRESSABLE(decl
) = 1;
3935 DECL_ARTIFICIAL(decl
) = 1;
3936 DECL_INITIAL(decl
) = expr
;
3937 rest_of_decl_compilation(decl
, 1, 0);
3941 return build_fold_addr_expr_loc(loc
, expr
);
3945 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
3947 // If we are dereferencing the pointer to a large struct, we
3948 // need to check for nil. We don't bother to check for small
3949 // structs because we expect the system to crash on a nil
3950 // pointer dereference.
3951 HOST_WIDE_INT s
= int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr
)));
3952 if (s
== -1 || s
>= 4096)
3955 expr
= save_expr(expr
);
3956 tree compare
= fold_build2_loc(loc
, EQ_EXPR
, boolean_type_node
,
3958 fold_convert(TREE_TYPE(expr
),
3959 null_pointer_node
));
3960 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
3962 expr
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(expr
),
3963 build3(COND_EXPR
, void_type_node
,
3964 compare
, crash
, NULL_TREE
),
3968 // If the type of EXPR is a recursive pointer type, then we
3969 // need to insert a cast before indirecting.
3970 if (TREE_TYPE(TREE_TYPE(expr
)) == ptr_type_node
)
3972 Type
* pt
= this->expr_
->type()->points_to();
3973 tree ind
= pt
->get_tree(context
->gogo());
3974 expr
= fold_convert_loc(loc
, build_pointer_type(ind
), expr
);
3977 return build_fold_indirect_ref_loc(loc
, expr
);
3985 // Export a unary expression.
3988 Unary_expression::do_export(Export
* exp
) const
3993 exp
->write_c_string("+ ");
3995 case OPERATOR_MINUS
:
3996 exp
->write_c_string("- ");
3999 exp
->write_c_string("! ");
4002 exp
->write_c_string("^ ");
4009 this->expr_
->export_expression(exp
);
4012 // Import a unary expression.
4015 Unary_expression::do_import(Import
* imp
)
4018 switch (imp
->get_char())
4024 op
= OPERATOR_MINUS
;
4035 imp
->require_c_string(" ");
4036 Expression
* expr
= Expression::import_expression(imp
);
4037 return Expression::make_unary(op
, expr
, imp
->location());
4040 // Make a unary expression.
4043 Expression::make_unary(Operator op
, Expression
* expr
, source_location location
)
4045 return new Unary_expression(op
, expr
, location
);
4048 // If this is an indirection through a pointer, return the expression
4049 // being pointed through. Otherwise return this.
4054 if (this->classification_
== EXPRESSION_UNARY
)
4056 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4057 if (ue
->op() == OPERATOR_MULT
)
4058 return ue
->operand();
4063 // Class Binary_expression.
4068 Binary_expression::do_traverse(Traverse
* traverse
)
4070 int t
= Expression::traverse(&this->left_
, traverse
);
4071 if (t
== TRAVERSE_EXIT
)
4072 return TRAVERSE_EXIT
;
4073 return Expression::traverse(&this->right_
, traverse
);
4076 // Compare integer constants according to OP.
4079 Binary_expression::compare_integer(Operator op
, mpz_t left_val
,
4082 int i
= mpz_cmp(left_val
, right_val
);
4087 case OPERATOR_NOTEQ
:
4102 // Compare floating point constants according to OP.
4105 Binary_expression::compare_float(Operator op
, Type
* type
, mpfr_t left_val
,
4110 i
= mpfr_cmp(left_val
, right_val
);
4114 mpfr_init_set(lv
, left_val
, GMP_RNDN
);
4116 mpfr_init_set(rv
, right_val
, GMP_RNDN
);
4117 Float_expression::constrain_float(lv
, type
);
4118 Float_expression::constrain_float(rv
, type
);
4119 i
= mpfr_cmp(lv
, rv
);
4127 case OPERATOR_NOTEQ
:
4142 // Compare complex constants according to OP. Complex numbers may
4143 // only be compared for equality.
4146 Binary_expression::compare_complex(Operator op
, Type
* type
,
4147 mpfr_t left_real
, mpfr_t left_imag
,
4148 mpfr_t right_real
, mpfr_t right_imag
)
4152 is_equal
= (mpfr_cmp(left_real
, right_real
) == 0
4153 && mpfr_cmp(left_imag
, right_imag
) == 0);
4158 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4159 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4162 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4163 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4164 Complex_expression::constrain_complex(lr
, li
, type
);
4165 Complex_expression::constrain_complex(rr
, ri
, type
);
4166 is_equal
= mpfr_cmp(lr
, rr
) == 0 && mpfr_cmp(li
, ri
) == 0;
4176 case OPERATOR_NOTEQ
:
4183 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4184 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4185 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4186 // this could be done, false if not.
4189 Binary_expression::eval_integer(Operator op
, Type
* left_type
, mpz_t left_val
,
4190 Type
* right_type
, mpz_t right_val
,
4191 source_location location
, mpz_t val
)
4193 bool is_shift_op
= false;
4197 case OPERATOR_ANDAND
:
4199 case OPERATOR_NOTEQ
:
4204 // These return boolean values. We should probably handle them
4205 // anyhow in case a type conversion is used on the result.
4208 mpz_add(val
, left_val
, right_val
);
4210 case OPERATOR_MINUS
:
4211 mpz_sub(val
, left_val
, right_val
);
4214 mpz_ior(val
, left_val
, right_val
);
4217 mpz_xor(val
, left_val
, right_val
);
4220 mpz_mul(val
, left_val
, right_val
);
4223 if (mpz_sgn(right_val
) != 0)
4224 mpz_tdiv_q(val
, left_val
, right_val
);
4227 error_at(location
, "division by zero");
4233 if (mpz_sgn(right_val
) != 0)
4234 mpz_tdiv_r(val
, left_val
, right_val
);
4237 error_at(location
, "division by zero");
4242 case OPERATOR_LSHIFT
:
4244 unsigned long shift
= mpz_get_ui(right_val
);
4245 if (mpz_cmp_ui(right_val
, shift
) != 0)
4247 error_at(location
, "shift count overflow");
4251 mpz_mul_2exp(val
, left_val
, shift
);
4256 case OPERATOR_RSHIFT
:
4258 unsigned long shift
= mpz_get_ui(right_val
);
4259 if (mpz_cmp_ui(right_val
, shift
) != 0)
4261 error_at(location
, "shift count overflow");
4265 if (mpz_cmp_ui(left_val
, 0) >= 0)
4266 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4268 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4274 mpz_and(val
, left_val
, right_val
);
4276 case OPERATOR_BITCLEAR
:
4280 mpz_com(tval
, right_val
);
4281 mpz_and(val
, left_val
, tval
);
4289 Type
* type
= left_type
;
4294 else if (type
!= right_type
&& right_type
!= NULL
)
4296 if (type
->is_abstract())
4298 else if (!right_type
->is_abstract())
4300 // This look like a type error which should be diagnosed
4301 // elsewhere. Don't do anything here, to avoid an
4302 // unhelpful chain of error messages.
4308 if (type
!= NULL
&& !type
->is_abstract())
4310 // We have to check the operands too, as we have implicitly
4311 // coerced them to TYPE.
4312 if ((type
!= left_type
4313 && !Integer_expression::check_constant(left_val
, type
, location
))
4315 && type
!= right_type
4316 && !Integer_expression::check_constant(right_val
, type
,
4318 || !Integer_expression::check_constant(val
, type
, location
))
4325 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4326 // Return true if this could be done, false if not.
4329 Binary_expression::eval_float(Operator op
, Type
* left_type
, mpfr_t left_val
,
4330 Type
* right_type
, mpfr_t right_val
,
4331 mpfr_t val
, source_location location
)
4336 case OPERATOR_ANDAND
:
4338 case OPERATOR_NOTEQ
:
4343 // These return boolean values. We should probably handle them
4344 // anyhow in case a type conversion is used on the result.
4347 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4349 case OPERATOR_MINUS
:
4350 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4355 case OPERATOR_BITCLEAR
:
4358 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4361 if (mpfr_zero_p(right_val
))
4362 error_at(location
, "division by zero");
4363 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4367 case OPERATOR_LSHIFT
:
4368 case OPERATOR_RSHIFT
:
4374 Type
* type
= left_type
;
4377 else if (type
!= right_type
&& right_type
!= NULL
)
4379 if (type
->is_abstract())
4381 else if (!right_type
->is_abstract())
4383 // This looks like a type error which should be diagnosed
4384 // elsewhere. Don't do anything here, to avoid an unhelpful
4385 // chain of error messages.
4390 if (type
!= NULL
&& !type
->is_abstract())
4392 if ((type
!= left_type
4393 && !Float_expression::check_constant(left_val
, type
, location
))
4394 || (type
!= right_type
4395 && !Float_expression::check_constant(right_val
, type
,
4397 || !Float_expression::check_constant(val
, type
, location
))
4398 mpfr_set_ui(val
, 0, GMP_RNDN
);
4404 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4405 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4406 // could be done, false if not.
4409 Binary_expression::eval_complex(Operator op
, Type
* left_type
,
4410 mpfr_t left_real
, mpfr_t left_imag
,
4412 mpfr_t right_real
, mpfr_t right_imag
,
4413 mpfr_t real
, mpfr_t imag
,
4414 source_location location
)
4419 case OPERATOR_ANDAND
:
4421 case OPERATOR_NOTEQ
:
4426 // These return boolean values and must be handled differently.
4429 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4430 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4432 case OPERATOR_MINUS
:
4433 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4434 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4439 case OPERATOR_BITCLEAR
:
4443 // You might think that multiplying two complex numbers would
4444 // be simple, and you would be right, until you start to think
4445 // about getting the right answer for infinity. If one
4446 // operand here is infinity and the other is anything other
4447 // than zero or NaN, then we are going to wind up subtracting
4448 // two infinity values. That will give us a NaN, but the
4449 // correct answer is infinity.
4453 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4457 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4461 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4465 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4467 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4468 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4470 // If we get NaN on both sides, check whether it should really
4471 // be infinity. The rule is that if either side of the
4472 // complex number is infinity, then the whole value is
4473 // infinity, even if the other side is NaN. So the only case
4474 // we have to fix is the one in which both sides are NaN.
4475 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4476 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4477 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4479 bool is_infinity
= false;
4483 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4484 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4488 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4489 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4491 // If the left side is infinity, then the result is
4493 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4495 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4496 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4497 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4498 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4501 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4502 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4506 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4507 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4512 // If the right side is infinity, then the result is
4514 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4516 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4517 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4518 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4519 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4522 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4523 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4527 mpfr_set_ui(li
, 0, GMP_RNDN
);
4528 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4533 // If we got an overflow in the intermediate computations,
4534 // then the result is infinity.
4536 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4537 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4541 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4542 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4546 mpfr_set_ui(li
, 0, GMP_RNDN
);
4547 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4551 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4552 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4556 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4557 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4564 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4565 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4566 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4567 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4568 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4569 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4570 mpfr_set_inf(real
, mpfr_sgn(real
));
4571 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4588 // For complex division we want to avoid having an
4589 // intermediate overflow turn the whole result in a NaN. We
4590 // scale the values to try to avoid this.
4592 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4593 error_at(location
, "division by zero");
4599 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4600 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4603 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4607 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4608 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4610 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4612 ilogbw
= mpfr_get_exp(t
);
4613 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4614 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4619 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4620 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4621 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4623 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4624 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4625 mpfr_add(real
, real
, t
, GMP_RNDN
);
4626 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4627 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4629 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4630 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4631 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4632 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4633 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4635 // If we wind up with NaN on both sides, check whether we
4636 // should really have infinity. The rule is that if either
4637 // side of the complex number is infinity, then the whole
4638 // value is infinity, even if the other side is NaN. So the
4639 // only case we have to fix is the one in which both sides are
4641 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4642 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4643 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4645 if (mpfr_zero_p(denom
))
4647 mpfr_set_inf(real
, mpfr_sgn(rr
));
4648 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4649 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4650 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4652 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4653 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4655 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4656 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4659 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4660 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4664 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
4668 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
4670 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4671 mpfr_set_inf(real
, mpfr_sgn(t3
));
4673 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
4674 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
4675 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4676 mpfr_set_inf(imag
, mpfr_sgn(t3
));
4682 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
4683 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
4685 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4686 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
4689 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4690 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
4694 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
4698 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
4700 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4701 mpfr_set_ui(real
, 0, GMP_RNDN
);
4702 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
4704 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
4705 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
4706 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4707 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4708 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
4726 case OPERATOR_LSHIFT
:
4727 case OPERATOR_RSHIFT
:
4733 Type
* type
= left_type
;
4736 else if (type
!= right_type
&& right_type
!= NULL
)
4738 if (type
->is_abstract())
4740 else if (!right_type
->is_abstract())
4742 // This looks like a type error which should be diagnosed
4743 // elsewhere. Don't do anything here, to avoid an unhelpful
4744 // chain of error messages.
4749 if (type
!= NULL
&& !type
->is_abstract())
4751 if ((type
!= left_type
4752 && !Complex_expression::check_constant(left_real
, left_imag
,
4754 || (type
!= right_type
4755 && !Complex_expression::check_constant(right_real
, right_imag
,
4757 || !Complex_expression::check_constant(real
, imag
, type
,
4760 mpfr_set_ui(real
, 0, GMP_RNDN
);
4761 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4768 // Lower a binary expression. We have to evaluate constant
4769 // expressions now, in order to implement Go's unlimited precision
4773 Binary_expression::do_lower(Gogo
*, Named_object
*, int)
4775 source_location location
= this->location();
4776 Operator op
= this->op_
;
4777 Expression
* left
= this->left_
;
4778 Expression
* right
= this->right_
;
4780 const bool is_comparison
= (op
== OPERATOR_EQEQ
4781 || op
== OPERATOR_NOTEQ
4782 || op
== OPERATOR_LT
4783 || op
== OPERATOR_LE
4784 || op
== OPERATOR_GT
4785 || op
== OPERATOR_GE
);
4787 // Integer constant expressions.
4793 mpz_init(right_val
);
4795 if (left
->integer_constant_value(false, left_val
, &left_type
)
4796 && right
->integer_constant_value(false, right_val
, &right_type
))
4798 Expression
* ret
= NULL
;
4799 if (left_type
!= right_type
4800 && left_type
!= NULL
4801 && right_type
!= NULL
4802 && left_type
->base() != right_type
->base()
4803 && op
!= OPERATOR_LSHIFT
4804 && op
!= OPERATOR_RSHIFT
)
4806 // May be a type error--let it be diagnosed later.
4808 else if (is_comparison
)
4810 bool b
= Binary_expression::compare_integer(op
, left_val
,
4812 ret
= Expression::make_cast(Type::lookup_bool_type(),
4813 Expression::make_boolean(b
, location
),
4821 if (Binary_expression::eval_integer(op
, left_type
, left_val
,
4822 right_type
, right_val
,
4825 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
);
4827 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
4829 else if (left_type
== NULL
)
4831 else if (right_type
== NULL
)
4833 else if (!left_type
->is_abstract()
4834 && left_type
->named_type() != NULL
)
4836 else if (!right_type
->is_abstract()
4837 && right_type
->named_type() != NULL
)
4839 else if (!left_type
->is_abstract())
4841 else if (!right_type
->is_abstract())
4843 else if (left_type
->float_type() != NULL
)
4845 else if (right_type
->float_type() != NULL
)
4847 else if (left_type
->complex_type() != NULL
)
4849 else if (right_type
->complex_type() != NULL
)
4853 ret
= Expression::make_integer(&val
, type
, location
);
4861 mpz_clear(right_val
);
4862 mpz_clear(left_val
);
4866 mpz_clear(right_val
);
4867 mpz_clear(left_val
);
4870 // Floating point constant expressions.
4873 mpfr_init(left_val
);
4876 mpfr_init(right_val
);
4878 if (left
->float_constant_value(left_val
, &left_type
)
4879 && right
->float_constant_value(right_val
, &right_type
))
4881 Expression
* ret
= NULL
;
4882 if (left_type
!= right_type
4883 && left_type
!= NULL
4884 && right_type
!= NULL
4885 && left_type
->base() != right_type
->base()
4886 && op
!= OPERATOR_LSHIFT
4887 && op
!= OPERATOR_RSHIFT
)
4889 // May be a type error--let it be diagnosed later.
4891 else if (is_comparison
)
4893 bool b
= Binary_expression::compare_float(op
,
4897 left_val
, right_val
);
4898 ret
= Expression::make_boolean(b
, location
);
4905 if (Binary_expression::eval_float(op
, left_type
, left_val
,
4906 right_type
, right_val
, val
,
4909 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
4910 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
4912 if (left_type
== NULL
)
4914 else if (right_type
== NULL
)
4916 else if (!left_type
->is_abstract()
4917 && left_type
->named_type() != NULL
)
4919 else if (!right_type
->is_abstract()
4920 && right_type
->named_type() != NULL
)
4922 else if (!left_type
->is_abstract())
4924 else if (!right_type
->is_abstract())
4926 else if (left_type
->float_type() != NULL
)
4928 else if (right_type
->float_type() != NULL
)
4932 ret
= Expression::make_float(&val
, type
, location
);
4940 mpfr_clear(right_val
);
4941 mpfr_clear(left_val
);
4945 mpfr_clear(right_val
);
4946 mpfr_clear(left_val
);
4949 // Complex constant expressions.
4953 mpfr_init(left_real
);
4954 mpfr_init(left_imag
);
4959 mpfr_init(right_real
);
4960 mpfr_init(right_imag
);
4963 if (left
->complex_constant_value(left_real
, left_imag
, &left_type
)
4964 && right
->complex_constant_value(right_real
, right_imag
, &right_type
))
4966 Expression
* ret
= NULL
;
4967 if (left_type
!= right_type
4968 && left_type
!= NULL
4969 && right_type
!= NULL
4970 && left_type
->base() != right_type
->base())
4972 // May be a type error--let it be diagnosed later.
4974 else if (is_comparison
)
4976 bool b
= Binary_expression::compare_complex(op
,
4984 ret
= Expression::make_boolean(b
, location
);
4993 if (Binary_expression::eval_complex(op
, left_type
,
4994 left_real
, left_imag
,
4996 right_real
, right_imag
,
5000 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5001 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5003 if (left_type
== NULL
)
5005 else if (right_type
== NULL
)
5007 else if (!left_type
->is_abstract()
5008 && left_type
->named_type() != NULL
)
5010 else if (!right_type
->is_abstract()
5011 && right_type
->named_type() != NULL
)
5013 else if (!left_type
->is_abstract())
5015 else if (!right_type
->is_abstract())
5017 else if (left_type
->complex_type() != NULL
)
5019 else if (right_type
->complex_type() != NULL
)
5023 ret
= Expression::make_complex(&real
, &imag
, type
,
5032 mpfr_clear(left_real
);
5033 mpfr_clear(left_imag
);
5034 mpfr_clear(right_real
);
5035 mpfr_clear(right_imag
);
5040 mpfr_clear(left_real
);
5041 mpfr_clear(left_imag
);
5042 mpfr_clear(right_real
);
5043 mpfr_clear(right_imag
);
5046 // String constant expressions.
5047 if (op
== OPERATOR_PLUS
5048 && left
->type()->is_string_type()
5049 && right
->type()->is_string_type())
5051 std::string left_string
;
5052 std::string right_string
;
5053 if (left
->string_constant_value(&left_string
)
5054 && right
->string_constant_value(&right_string
))
5055 return Expression::make_string(left_string
+ right_string
, location
);
5061 // Return the integer constant value, if it has one.
5064 Binary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
5070 if (!this->left_
->integer_constant_value(iota_is_constant
, left_val
,
5073 mpz_clear(left_val
);
5078 mpz_init(right_val
);
5080 if (!this->right_
->integer_constant_value(iota_is_constant
, right_val
,
5083 mpz_clear(right_val
);
5084 mpz_clear(left_val
);
5089 if (left_type
!= right_type
5090 && left_type
!= NULL
5091 && right_type
!= NULL
5092 && left_type
->base() != right_type
->base()
5093 && this->op_
!= OPERATOR_RSHIFT
5094 && this->op_
!= OPERATOR_LSHIFT
)
5097 ret
= Binary_expression::eval_integer(this->op_
, left_type
, left_val
,
5098 right_type
, right_val
,
5099 this->location(), val
);
5101 mpz_clear(right_val
);
5102 mpz_clear(left_val
);
5110 // Return the floating point constant value, if it has one.
5113 Binary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
5116 mpfr_init(left_val
);
5118 if (!this->left_
->float_constant_value(left_val
, &left_type
))
5120 mpfr_clear(left_val
);
5125 mpfr_init(right_val
);
5127 if (!this->right_
->float_constant_value(right_val
, &right_type
))
5129 mpfr_clear(right_val
);
5130 mpfr_clear(left_val
);
5135 if (left_type
!= right_type
5136 && left_type
!= NULL
5137 && right_type
!= NULL
5138 && left_type
->base() != right_type
->base())
5141 ret
= Binary_expression::eval_float(this->op_
, left_type
, left_val
,
5142 right_type
, right_val
,
5143 val
, this->location());
5145 mpfr_clear(left_val
);
5146 mpfr_clear(right_val
);
5154 // Return the complex constant value, if it has one.
5157 Binary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
5162 mpfr_init(left_real
);
5163 mpfr_init(left_imag
);
5165 if (!this->left_
->complex_constant_value(left_real
, left_imag
, &left_type
))
5167 mpfr_clear(left_real
);
5168 mpfr_clear(left_imag
);
5174 mpfr_init(right_real
);
5175 mpfr_init(right_imag
);
5177 if (!this->right_
->complex_constant_value(right_real
, right_imag
,
5180 mpfr_clear(left_real
);
5181 mpfr_clear(left_imag
);
5182 mpfr_clear(right_real
);
5183 mpfr_clear(right_imag
);
5188 if (left_type
!= right_type
5189 && left_type
!= NULL
5190 && right_type
!= NULL
5191 && left_type
->base() != right_type
->base())
5194 ret
= Binary_expression::eval_complex(this->op_
, left_type
,
5195 left_real
, left_imag
,
5197 right_real
, right_imag
,
5200 mpfr_clear(left_real
);
5201 mpfr_clear(left_imag
);
5202 mpfr_clear(right_real
);
5203 mpfr_clear(right_imag
);
5211 // Note that the value is being discarded.
5214 Binary_expression::do_discarding_value()
5216 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5217 this->right_
->discarding_value();
5219 this->warn_about_unused_value();
5225 Binary_expression::do_type()
5230 case OPERATOR_ANDAND
:
5232 case OPERATOR_NOTEQ
:
5237 return Type::lookup_bool_type();
5240 case OPERATOR_MINUS
:
5247 case OPERATOR_BITCLEAR
:
5249 Type
* left_type
= this->left_
->type();
5250 Type
* right_type
= this->right_
->type();
5251 if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5253 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5255 else if (!left_type
->is_abstract())
5257 else if (!right_type
->is_abstract())
5259 else if (left_type
->complex_type() != NULL
)
5261 else if (right_type
->complex_type() != NULL
)
5263 else if (left_type
->float_type() != NULL
)
5265 else if (right_type
->float_type() != NULL
)
5271 case OPERATOR_LSHIFT
:
5272 case OPERATOR_RSHIFT
:
5273 return this->left_
->type();
5280 // Set type for a binary expression.
5283 Binary_expression::do_determine_type(const Type_context
* context
)
5285 Type
* tleft
= this->left_
->type();
5286 Type
* tright
= this->right_
->type();
5288 // Both sides should have the same type, except for the shift
5289 // operations. For a comparison, we should ignore the incoming
5292 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5293 || this->op_
== OPERATOR_RSHIFT
);
5295 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5296 || this->op_
== OPERATOR_NOTEQ
5297 || this->op_
== OPERATOR_LT
5298 || this->op_
== OPERATOR_LE
5299 || this->op_
== OPERATOR_GT
5300 || this->op_
== OPERATOR_GE
);
5302 Type_context
subcontext(*context
);
5306 // In a comparison, the context does not determine the types of
5308 subcontext
.type
= NULL
;
5311 // Set the context for the left hand operand.
5314 // The right hand operand plays no role in determining the type
5315 // of the left hand operand. A shift of an abstract integer in
5316 // a string context gets special treatment, which may be a
5318 if (subcontext
.type
!= NULL
5319 && subcontext
.type
->is_string_type()
5320 && tleft
->is_abstract())
5321 error_at(this->location(), "shift of non-integer operand");
5323 else if (!tleft
->is_abstract())
5324 subcontext
.type
= tleft
;
5325 else if (!tright
->is_abstract())
5326 subcontext
.type
= tright
;
5327 else if (subcontext
.type
== NULL
)
5329 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5330 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5331 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5333 // Both sides have an abstract integer, abstract float, or
5334 // abstract complex type. Just let CONTEXT determine
5335 // whether they may remain abstract or not.
5337 else if (tleft
->complex_type() != NULL
)
5338 subcontext
.type
= tleft
;
5339 else if (tright
->complex_type() != NULL
)
5340 subcontext
.type
= tright
;
5341 else if (tleft
->float_type() != NULL
)
5342 subcontext
.type
= tleft
;
5343 else if (tright
->float_type() != NULL
)
5344 subcontext
.type
= tright
;
5346 subcontext
.type
= tleft
;
5349 this->left_
->determine_type(&subcontext
);
5351 // The context for the right hand operand is the same as for the
5352 // left hand operand, except for a shift operator.
5355 subcontext
.type
= Type::lookup_integer_type("uint");
5356 subcontext
.may_be_abstract
= false;
5359 this->right_
->determine_type(&subcontext
);
5362 // Report an error if the binary operator OP does not support TYPE.
5363 // Return whether the operation is OK. This should not be used for
5367 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5368 source_location location
)
5373 case OPERATOR_ANDAND
:
5374 if (!type
->is_boolean_type())
5376 error_at(location
, "expected boolean type");
5382 case OPERATOR_NOTEQ
:
5383 if (type
->integer_type() == NULL
5384 && type
->float_type() == NULL
5385 && type
->complex_type() == NULL
5386 && !type
->is_string_type()
5387 && type
->points_to() == NULL
5388 && !type
->is_nil_type()
5389 && !type
->is_boolean_type()
5390 && type
->interface_type() == NULL
5391 && (type
->array_type() == NULL
5392 || type
->array_type()->length() != NULL
)
5393 && type
->map_type() == NULL
5394 && type
->channel_type() == NULL
5395 && type
->function_type() == NULL
)
5398 ("expected integer, floating, complex, string, pointer, "
5399 "boolean, interface, slice, map, channel, "
5400 "or function type"));
5409 if (type
->integer_type() == NULL
5410 && type
->float_type() == NULL
5411 && !type
->is_string_type())
5413 error_at(location
, "expected integer, floating, or string type");
5419 case OPERATOR_PLUSEQ
:
5420 if (type
->integer_type() == NULL
5421 && type
->float_type() == NULL
5422 && type
->complex_type() == NULL
5423 && !type
->is_string_type())
5426 "expected integer, floating, complex, or string type");
5431 case OPERATOR_MINUS
:
5432 case OPERATOR_MINUSEQ
:
5434 case OPERATOR_MULTEQ
:
5436 case OPERATOR_DIVEQ
:
5437 if (type
->integer_type() == NULL
5438 && type
->float_type() == NULL
5439 && type
->complex_type() == NULL
)
5441 error_at(location
, "expected integer, floating, or complex type");
5447 case OPERATOR_MODEQ
:
5451 case OPERATOR_ANDEQ
:
5453 case OPERATOR_XOREQ
:
5454 case OPERATOR_BITCLEAR
:
5455 case OPERATOR_BITCLEAREQ
:
5456 if (type
->integer_type() == NULL
)
5458 error_at(location
, "expected integer type");
5473 Binary_expression::do_check_types(Gogo
*)
5475 Type
* left_type
= this->left_
->type();
5476 Type
* right_type
= this->right_
->type();
5477 if (left_type
->is_error_type() || right_type
->is_error_type())
5479 this->set_is_error();
5483 if (this->op_
== OPERATOR_EQEQ
5484 || this->op_
== OPERATOR_NOTEQ
5485 || this->op_
== OPERATOR_LT
5486 || this->op_
== OPERATOR_LE
5487 || this->op_
== OPERATOR_GT
5488 || this->op_
== OPERATOR_GE
)
5490 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5491 && !Type::are_assignable(right_type
, left_type
, NULL
))
5493 this->report_error(_("incompatible types in binary expression"));
5496 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5498 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5501 this->set_is_error();
5505 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5507 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5509 this->report_error(_("incompatible types in binary expression"));
5512 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5515 this->set_is_error();
5521 if (left_type
->integer_type() == NULL
)
5522 this->report_error(_("shift of non-integer operand"));
5524 if (!right_type
->is_abstract()
5525 && (right_type
->integer_type() == NULL
5526 || !right_type
->integer_type()->is_unsigned()))
5527 this->report_error(_("shift count not unsigned integer"));
5533 if (this->right_
->integer_constant_value(true, val
, &type
))
5535 if (mpz_sgn(val
) < 0)
5536 this->report_error(_("negative shift count"));
5543 // Get a tree for a binary expression.
5546 Binary_expression::do_get_tree(Translate_context
* context
)
5548 tree left
= this->left_
->get_tree(context
);
5549 tree right
= this->right_
->get_tree(context
);
5551 if (left
== error_mark_node
|| right
== error_mark_node
)
5552 return error_mark_node
;
5554 enum tree_code code
;
5555 bool use_left_type
= true;
5556 bool is_shift_op
= false;
5560 case OPERATOR_NOTEQ
:
5565 return Expression::comparison_tree(context
, this->op_
,
5566 this->left_
->type(), left
,
5567 this->right_
->type(), right
,
5571 code
= TRUTH_ORIF_EXPR
;
5572 use_left_type
= false;
5574 case OPERATOR_ANDAND
:
5575 code
= TRUTH_ANDIF_EXPR
;
5576 use_left_type
= false;
5581 case OPERATOR_MINUS
:
5585 code
= BIT_IOR_EXPR
;
5588 code
= BIT_XOR_EXPR
;
5595 Type
*t
= this->left_
->type();
5596 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5599 code
= TRUNC_DIV_EXPR
;
5603 code
= TRUNC_MOD_EXPR
;
5605 case OPERATOR_LSHIFT
:
5609 case OPERATOR_RSHIFT
:
5614 code
= BIT_AND_EXPR
;
5616 case OPERATOR_BITCLEAR
:
5617 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5618 code
= BIT_AND_EXPR
;
5624 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5626 if (this->left_
->type()->is_string_type())
5628 gcc_assert(this->op_
== OPERATOR_PLUS
);
5629 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5630 static tree string_plus_decl
;
5631 return Gogo::call_builtin(&string_plus_decl
,
5642 tree compute_type
= excess_precision_type(type
);
5643 if (compute_type
!= NULL_TREE
)
5645 left
= ::convert(compute_type
, left
);
5646 right
= ::convert(compute_type
, right
);
5649 tree eval_saved
= NULL_TREE
;
5653 left
= save_expr(left
);
5655 right
= save_expr(right
);
5656 // Make sure the values are evaluated.
5657 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5658 void_type_node
, left
, right
);
5661 tree ret
= fold_build2_loc(this->location(),
5663 compute_type
!= NULL_TREE
? compute_type
: type
,
5666 if (compute_type
!= NULL_TREE
)
5667 ret
= ::convert(type
, ret
);
5669 // In Go, a shift larger than the size of the type is well-defined.
5670 // This is not true in GENERIC, so we need to insert a conditional.
5673 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5674 gcc_assert(this->left_
->type()->integer_type() != NULL
);
5675 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5677 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5678 build_int_cst_type(TREE_TYPE(right
), bits
));
5680 tree overflow_result
= fold_convert_loc(this->location(),
5683 if (this->op_
== OPERATOR_RSHIFT
5684 && !this->left_
->type()->integer_type()->is_unsigned())
5686 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
5687 boolean_type_node
, left
,
5688 fold_convert_loc(this->location(),
5690 integer_zero_node
));
5691 tree neg_one
= fold_build2_loc(this->location(),
5692 MINUS_EXPR
, TREE_TYPE(left
),
5693 fold_convert_loc(this->location(),
5696 fold_convert_loc(this->location(),
5699 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
5700 TREE_TYPE(left
), neg
, neg_one
,
5704 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
5705 compare
, ret
, overflow_result
);
5707 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5708 TREE_TYPE(ret
), eval_saved
, ret
);
5714 // Export a binary expression.
5717 Binary_expression::do_export(Export
* exp
) const
5719 exp
->write_c_string("(");
5720 this->left_
->export_expression(exp
);
5724 exp
->write_c_string(" || ");
5726 case OPERATOR_ANDAND
:
5727 exp
->write_c_string(" && ");
5730 exp
->write_c_string(" == ");
5732 case OPERATOR_NOTEQ
:
5733 exp
->write_c_string(" != ");
5736 exp
->write_c_string(" < ");
5739 exp
->write_c_string(" <= ");
5742 exp
->write_c_string(" > ");
5745 exp
->write_c_string(" >= ");
5748 exp
->write_c_string(" + ");
5750 case OPERATOR_MINUS
:
5751 exp
->write_c_string(" - ");
5754 exp
->write_c_string(" | ");
5757 exp
->write_c_string(" ^ ");
5760 exp
->write_c_string(" * ");
5763 exp
->write_c_string(" / ");
5766 exp
->write_c_string(" % ");
5768 case OPERATOR_LSHIFT
:
5769 exp
->write_c_string(" << ");
5771 case OPERATOR_RSHIFT
:
5772 exp
->write_c_string(" >> ");
5775 exp
->write_c_string(" & ");
5777 case OPERATOR_BITCLEAR
:
5778 exp
->write_c_string(" &^ ");
5783 this->right_
->export_expression(exp
);
5784 exp
->write_c_string(")");
5787 // Import a binary expression.
5790 Binary_expression::do_import(Import
* imp
)
5792 imp
->require_c_string("(");
5794 Expression
* left
= Expression::import_expression(imp
);
5797 if (imp
->match_c_string(" || "))
5802 else if (imp
->match_c_string(" && "))
5804 op
= OPERATOR_ANDAND
;
5807 else if (imp
->match_c_string(" == "))
5812 else if (imp
->match_c_string(" != "))
5814 op
= OPERATOR_NOTEQ
;
5817 else if (imp
->match_c_string(" < "))
5822 else if (imp
->match_c_string(" <= "))
5827 else if (imp
->match_c_string(" > "))
5832 else if (imp
->match_c_string(" >= "))
5837 else if (imp
->match_c_string(" + "))
5842 else if (imp
->match_c_string(" - "))
5844 op
= OPERATOR_MINUS
;
5847 else if (imp
->match_c_string(" | "))
5852 else if (imp
->match_c_string(" ^ "))
5857 else if (imp
->match_c_string(" * "))
5862 else if (imp
->match_c_string(" / "))
5867 else if (imp
->match_c_string(" % "))
5872 else if (imp
->match_c_string(" << "))
5874 op
= OPERATOR_LSHIFT
;
5877 else if (imp
->match_c_string(" >> "))
5879 op
= OPERATOR_RSHIFT
;
5882 else if (imp
->match_c_string(" & "))
5887 else if (imp
->match_c_string(" &^ "))
5889 op
= OPERATOR_BITCLEAR
;
5894 error_at(imp
->location(), "unrecognized binary operator");
5895 return Expression::make_error(imp
->location());
5898 Expression
* right
= Expression::import_expression(imp
);
5900 imp
->require_c_string(")");
5902 return Expression::make_binary(op
, left
, right
, imp
->location());
5905 // Make a binary expression.
5908 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
5909 source_location location
)
5911 return new Binary_expression(op
, left
, right
, location
);
5914 // Implement a comparison.
5917 Expression::comparison_tree(Translate_context
* context
, Operator op
,
5918 Type
* left_type
, tree left_tree
,
5919 Type
* right_type
, tree right_tree
,
5920 source_location location
)
5922 enum tree_code code
;
5928 case OPERATOR_NOTEQ
:
5947 if (left_type
->is_string_type() && right_type
->is_string_type())
5949 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5950 static tree string_compare_decl
;
5951 left_tree
= Gogo::call_builtin(&string_compare_decl
,
5960 right_tree
= build_int_cst_type(integer_type_node
, 0);
5962 else if ((left_type
->interface_type() != NULL
5963 && right_type
->interface_type() == NULL
5964 && !right_type
->is_nil_type())
5965 || (left_type
->interface_type() == NULL
5966 && !left_type
->is_nil_type()
5967 && right_type
->interface_type() != NULL
))
5969 // Comparing an interface value to a non-interface value.
5970 if (left_type
->interface_type() == NULL
)
5972 std::swap(left_type
, right_type
);
5973 std::swap(left_tree
, right_tree
);
5976 // The right operand is not an interface. We need to take its
5977 // address if it is not a pointer.
5980 if (right_type
->points_to() != NULL
)
5982 make_tmp
= NULL_TREE
;
5985 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
5987 make_tmp
= NULL_TREE
;
5988 arg
= build_fold_addr_expr_loc(location
, right_tree
);
5989 if (DECL_P(right_tree
))
5990 TREE_ADDRESSABLE(right_tree
) = 1;
5994 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
5995 get_name(right_tree
));
5996 DECL_IGNORED_P(tmp
) = 0;
5997 DECL_INITIAL(tmp
) = right_tree
;
5998 TREE_ADDRESSABLE(tmp
) = 1;
5999 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6000 SET_EXPR_LOCATION(make_tmp
, location
);
6001 arg
= build_fold_addr_expr_loc(location
, tmp
);
6003 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
6005 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
6007 if (left_type
->interface_type()->is_empty())
6009 static tree empty_interface_value_compare_decl
;
6010 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6012 "__go_empty_interface_value_compare",
6015 TREE_TYPE(left_tree
),
6017 TREE_TYPE(descriptor
),
6021 if (left_tree
== error_mark_node
)
6022 return error_mark_node
;
6023 // This can panic if the type is not comparable.
6024 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6028 static tree interface_value_compare_decl
;
6029 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6031 "__go_interface_value_compare",
6034 TREE_TYPE(left_tree
),
6036 TREE_TYPE(descriptor
),
6040 if (left_tree
== error_mark_node
)
6041 return error_mark_node
;
6042 // This can panic if the type is not comparable.
6043 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6045 right_tree
= build_int_cst_type(integer_type_node
, 0);
6047 if (make_tmp
!= NULL_TREE
)
6048 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6051 else if (left_type
->interface_type() != NULL
6052 && right_type
->interface_type() != NULL
)
6054 if (left_type
->interface_type()->is_empty())
6056 gcc_assert(right_type
->interface_type()->is_empty());
6057 static tree empty_interface_compare_decl
;
6058 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6060 "__go_empty_interface_compare",
6063 TREE_TYPE(left_tree
),
6065 TREE_TYPE(right_tree
),
6067 if (left_tree
== error_mark_node
)
6068 return error_mark_node
;
6069 // This can panic if the type is uncomparable.
6070 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6074 gcc_assert(!right_type
->interface_type()->is_empty());
6075 static tree interface_compare_decl
;
6076 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6078 "__go_interface_compare",
6081 TREE_TYPE(left_tree
),
6083 TREE_TYPE(right_tree
),
6085 if (left_tree
== error_mark_node
)
6086 return error_mark_node
;
6087 // This can panic if the type is uncomparable.
6088 TREE_NOTHROW(interface_compare_decl
) = 0;
6090 right_tree
= build_int_cst_type(integer_type_node
, 0);
6093 if (left_type
->is_nil_type()
6094 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6096 std::swap(left_type
, right_type
);
6097 std::swap(left_tree
, right_tree
);
6100 if (right_type
->is_nil_type())
6102 if (left_type
->array_type() != NULL
6103 && left_type
->array_type()->length() == NULL
)
6105 Array_type
* at
= left_type
->array_type();
6106 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6107 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6109 else if (left_type
->interface_type() != NULL
)
6111 // An interface is nil if the first field is nil.
6112 tree left_type_tree
= TREE_TYPE(left_tree
);
6113 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6114 tree field
= TYPE_FIELDS(left_type_tree
);
6115 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6117 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6121 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6122 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6126 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6127 return error_mark_node
;
6129 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6130 if (CAN_HAVE_LOCATION_P(ret
))
6131 SET_EXPR_LOCATION(ret
, location
);
6135 // Class Bound_method_expression.
6140 Bound_method_expression::do_traverse(Traverse
* traverse
)
6142 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6143 return TRAVERSE_EXIT
;
6144 return Expression::traverse(&this->method_
, traverse
);
6147 // Return the type of a bound method expression. The type of this
6148 // object is really the type of the method with no receiver. We
6149 // should be able to get away with just returning the type of the
6153 Bound_method_expression::do_type()
6155 return this->method_
->type();
6158 // Determine the types of a method expression.
6161 Bound_method_expression::do_determine_type(const Type_context
*)
6163 this->method_
->determine_type_no_context();
6164 Type
* mtype
= this->method_
->type();
6165 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6166 if (fntype
== NULL
|| !fntype
->is_method())
6167 this->expr_
->determine_type_no_context();
6170 Type_context
subcontext(fntype
->receiver()->type(), false);
6171 this->expr_
->determine_type(&subcontext
);
6175 // Check the types of a method expression.
6178 Bound_method_expression::do_check_types(Gogo
*)
6180 Type
* type
= this->method_
->type()->deref();
6182 || type
->function_type() == NULL
6183 || !type
->function_type()->is_method())
6184 this->report_error(_("object is not a method"));
6187 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6188 Type
* etype
= (this->expr_type_
!= NULL
6190 : this->expr_
->type());
6191 etype
= etype
->deref();
6192 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6193 this->report_error(_("method type does not match object type"));
6197 // Get the tree for a method expression. There is no standard tree
6198 // representation for this. The only places it may currently be used
6199 // are in a Call_expression or a Go_statement, which will take it
6200 // apart directly. So this has nothing to do at present.
6203 Bound_method_expression::do_get_tree(Translate_context
*)
6208 // Make a method expression.
6210 Bound_method_expression
*
6211 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6212 source_location location
)
6214 return new Bound_method_expression(expr
, method
, location
);
6217 // Class Builtin_call_expression. This is used for a call to a
6218 // builtin function.
6220 class Builtin_call_expression
: public Call_expression
6223 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6224 bool is_varargs
, source_location location
);
6227 // This overrides Call_expression::do_lower.
6229 do_lower(Gogo
*, Named_object
*, int);
6232 do_is_constant() const;
6235 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6238 do_float_constant_value(mpfr_t
, Type
**) const;
6241 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6247 do_determine_type(const Type_context
*);
6250 do_check_types(Gogo
*);
6255 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6256 this->args()->copy(),
6262 do_get_tree(Translate_context
*);
6265 do_export(Export
*) const;
6268 do_is_recover_call() const;
6271 do_set_recover_arg(Expression
*);
6274 // The builtin functions.
6275 enum Builtin_function_code
6279 // Predeclared builtin functions.
6296 // Builtin functions from the unsafe package.
6309 real_imag_type(Type
*);
6314 // A pointer back to the general IR structure. This avoids a global
6315 // variable, or passing it around everywhere.
6317 // The builtin function being called.
6318 Builtin_function_code code_
;
6321 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6323 Expression_list
* args
,
6325 source_location location
)
6326 : Call_expression(fn
, args
, is_varargs
, location
),
6327 gogo_(gogo
), code_(BUILTIN_INVALID
)
6329 Func_expression
* fnexp
= this->fn()->func_expression();
6330 gcc_assert(fnexp
!= NULL
);
6331 const std::string
& name(fnexp
->named_object()->name());
6332 if (name
== "append")
6333 this->code_
= BUILTIN_APPEND
;
6334 else if (name
== "cap")
6335 this->code_
= BUILTIN_CAP
;
6336 else if (name
== "close")
6337 this->code_
= BUILTIN_CLOSE
;
6338 else if (name
== "closed")
6339 this->code_
= BUILTIN_CLOSED
;
6340 else if (name
== "cmplx")
6341 this->code_
= BUILTIN_CMPLX
;
6342 else if (name
== "copy")
6343 this->code_
= BUILTIN_COPY
;
6344 else if (name
== "imag")
6345 this->code_
= BUILTIN_IMAG
;
6346 else if (name
== "len")
6347 this->code_
= BUILTIN_LEN
;
6348 else if (name
== "make")
6349 this->code_
= BUILTIN_MAKE
;
6350 else if (name
== "new")
6351 this->code_
= BUILTIN_NEW
;
6352 else if (name
== "panic")
6353 this->code_
= BUILTIN_PANIC
;
6354 else if (name
== "print")
6355 this->code_
= BUILTIN_PRINT
;
6356 else if (name
== "println")
6357 this->code_
= BUILTIN_PRINTLN
;
6358 else if (name
== "real")
6359 this->code_
= BUILTIN_REAL
;
6360 else if (name
== "recover")
6361 this->code_
= BUILTIN_RECOVER
;
6362 else if (name
== "Alignof")
6363 this->code_
= BUILTIN_ALIGNOF
;
6364 else if (name
== "Offsetof")
6365 this->code_
= BUILTIN_OFFSETOF
;
6366 else if (name
== "Sizeof")
6367 this->code_
= BUILTIN_SIZEOF
;
6372 // Return whether this is a call to recover. This is a virtual
6373 // function called from the parent class.
6376 Builtin_call_expression::do_is_recover_call() const
6378 if (this->classification() == EXPRESSION_ERROR
)
6380 return this->code_
== BUILTIN_RECOVER
;
6383 // Set the argument for a call to recover.
6386 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6388 const Expression_list
* args
= this->args();
6389 gcc_assert(args
== NULL
|| args
->empty());
6390 Expression_list
* new_args
= new Expression_list();
6391 new_args
->push_back(arg
);
6392 this->set_args(new_args
);
6395 // A traversal class which looks for a call expression.
6397 class Find_call_expression
: public Traverse
6400 Find_call_expression()
6401 : Traverse(traverse_expressions
),
6406 expression(Expression
**);
6410 { return this->found_
; }
6417 Find_call_expression::expression(Expression
** pexpr
)
6419 if ((*pexpr
)->call_expression() != NULL
)
6421 this->found_
= true;
6422 return TRAVERSE_EXIT
;
6424 return TRAVERSE_CONTINUE
;
6427 // Lower a builtin call expression. This turns new and make into
6428 // specific expressions. We also convert to a constant if we can.
6431 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6433 if (this->code_
== BUILTIN_NEW
)
6435 const Expression_list
* args
= this->args();
6436 if (args
== NULL
|| args
->size() < 1)
6437 this->report_error(_("not enough arguments"));
6438 else if (args
->size() > 1)
6439 this->report_error(_("too many arguments"));
6442 Expression
* arg
= args
->front();
6443 if (!arg
->is_type_expression())
6445 error_at(arg
->location(), "expected type");
6446 this->set_is_error();
6449 return Expression::make_allocation(arg
->type(), this->location());
6452 else if (this->code_
== BUILTIN_MAKE
)
6454 const Expression_list
* args
= this->args();
6455 if (args
== NULL
|| args
->size() < 1)
6456 this->report_error(_("not enough arguments"));
6459 Expression
* arg
= args
->front();
6460 if (!arg
->is_type_expression())
6462 error_at(arg
->location(), "expected type");
6463 this->set_is_error();
6467 Expression_list
* newargs
;
6468 if (args
->size() == 1)
6472 newargs
= new Expression_list();
6473 Expression_list::const_iterator p
= args
->begin();
6475 for (; p
!= args
->end(); ++p
)
6476 newargs
->push_back(*p
);
6478 return Expression::make_make(arg
->type(), newargs
,
6483 else if (this->is_constant())
6485 // We can only lower len and cap if there are no function calls
6486 // in the arguments. Otherwise we have to make the call.
6487 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6489 Expression
* arg
= this->one_arg();
6490 if (!arg
->is_constant())
6492 Find_call_expression find_call
;
6493 Expression::traverse(&arg
, &find_call
);
6494 if (find_call
.found())
6502 if (this->integer_constant_value(true, ival
, &type
))
6504 Expression
* ret
= Expression::make_integer(&ival
, type
,
6513 if (this->float_constant_value(rval
, &type
))
6515 Expression
* ret
= Expression::make_float(&rval
, type
,
6523 if (this->complex_constant_value(rval
, imag
, &type
))
6525 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6534 else if (this->code_
== BUILTIN_RECOVER
)
6536 if (function
!= NULL
)
6537 function
->func_value()->set_calls_recover();
6540 // Calling recover outside of a function always returns the
6541 // nil empty interface.
6542 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6543 return Expression::make_cast(eface
,
6544 Expression::make_nil(this->location()),
6548 else if (this->code_
== BUILTIN_APPEND
)
6550 // Lower the varargs.
6551 const Expression_list
* args
= this->args();
6552 if (args
== NULL
|| args
->empty())
6554 Type
* slice_type
= args
->front()->type();
6555 if (!slice_type
->is_open_array_type())
6557 error_at(args
->front()->location(), "argument 1 must be a slice");
6558 this->set_is_error();
6561 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6567 // Return the type of the real or imag functions, given the type of
6568 // the argument. We need to map complex to float, complex64 to
6569 // float32, and complex128 to float64, so it has to be done by name.
6570 // This returns NULL if it can't figure out the type.
6573 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6575 if (arg_type
== NULL
|| arg_type
->is_abstract())
6577 Named_type
* nt
= arg_type
->named_type();
6580 while (nt
->real_type()->named_type() != NULL
)
6581 nt
= nt
->real_type()->named_type();
6582 if (nt
->name() == "complex")
6583 return Type::lookup_float_type("float");
6584 else if (nt
->name() == "complex64")
6585 return Type::lookup_float_type("float32");
6586 else if (nt
->name() == "complex128")
6587 return Type::lookup_float_type("float64");
6592 // Return the type of the cmplx function, given the type of one of the
6593 // argments. Like real_imag_type, we have to map by name.
6596 Builtin_call_expression::cmplx_type(Type
* arg_type
)
6598 if (arg_type
== NULL
|| arg_type
->is_abstract())
6600 Named_type
* nt
= arg_type
->named_type();
6603 while (nt
->real_type()->named_type() != NULL
)
6604 nt
= nt
->real_type()->named_type();
6605 if (nt
->name() == "float")
6606 return Type::lookup_complex_type("complex");
6607 else if (nt
->name() == "float32")
6608 return Type::lookup_complex_type("complex64");
6609 else if (nt
->name() == "float64")
6610 return Type::lookup_complex_type("complex128");
6615 // Return a single argument, or NULL if there isn't one.
6618 Builtin_call_expression::one_arg() const
6620 const Expression_list
* args
= this->args();
6621 if (args
->size() != 1)
6623 return args
->front();
6626 // Return whether this is constant: len of a string, or len or cap of
6627 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6630 Builtin_call_expression::do_is_constant() const
6632 switch (this->code_
)
6637 Expression
* arg
= this->one_arg();
6640 Type
* arg_type
= arg
->type();
6642 if (arg_type
->points_to() != NULL
6643 && arg_type
->points_to()->array_type() != NULL
6644 && !arg_type
->points_to()->is_open_array_type())
6645 arg_type
= arg_type
->points_to();
6647 if (arg_type
->array_type() != NULL
6648 && arg_type
->array_type()->length() != NULL
)
6649 return arg_type
->array_type()->length()->is_constant();
6651 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6652 return arg
->is_constant();
6656 case BUILTIN_SIZEOF
:
6657 case BUILTIN_ALIGNOF
:
6658 return this->one_arg() != NULL
;
6660 case BUILTIN_OFFSETOF
:
6662 Expression
* arg
= this->one_arg();
6665 return arg
->field_reference_expression() != NULL
;
6670 const Expression_list
* args
= this->args();
6671 if (args
!= NULL
&& args
->size() == 2)
6672 return args
->front()->is_constant() && args
->back()->is_constant();
6679 Expression
* arg
= this->one_arg();
6680 return arg
!= NULL
&& arg
->is_constant();
6690 // Return an integer constant value if possible.
6693 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
6697 if (this->code_
== BUILTIN_LEN
6698 || this->code_
== BUILTIN_CAP
)
6700 Expression
* arg
= this->one_arg();
6703 Type
* arg_type
= arg
->type();
6705 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6708 if (arg
->string_constant_value(&sval
))
6710 mpz_set_ui(val
, sval
.length());
6711 *ptype
= Type::lookup_integer_type("int");
6716 if (arg_type
->points_to() != NULL
6717 && arg_type
->points_to()->array_type() != NULL
6718 && !arg_type
->points_to()->is_open_array_type())
6719 arg_type
= arg_type
->points_to();
6721 if (arg_type
->array_type() != NULL
6722 && arg_type
->array_type()->length() != NULL
)
6724 Expression
* e
= arg_type
->array_type()->length();
6725 if (e
->integer_constant_value(iota_is_constant
, val
, ptype
))
6727 *ptype
= Type::lookup_integer_type("int");
6732 else if (this->code_
== BUILTIN_SIZEOF
6733 || this->code_
== BUILTIN_ALIGNOF
)
6735 Expression
* arg
= this->one_arg();
6738 Type
* arg_type
= arg
->type();
6739 if (arg_type
->is_error_type() || arg_type
->is_undefined())
6741 if (arg_type
->is_abstract())
6743 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
6744 unsigned long val_long
;
6745 if (this->code_
== BUILTIN_SIZEOF
)
6747 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
6748 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
6749 if (TREE_INT_CST_HIGH(type_size
) != 0)
6751 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
6752 val_long
= static_cast<unsigned long>(val_wide
);
6753 if (val_long
!= val_wide
)
6756 else if (this->code_
== BUILTIN_ALIGNOF
)
6758 if (arg
->field_reference_expression() == NULL
)
6759 val_long
= go_type_alignment(arg_type_tree
);
6762 // Calling unsafe.Alignof(s.f) returns the alignment of
6763 // the type of f when it is used as a field in a struct.
6764 val_long
= go_field_alignment(arg_type_tree
);
6769 mpz_set_ui(val
, val_long
);
6773 else if (this->code_
== BUILTIN_OFFSETOF
)
6775 Expression
* arg
= this->one_arg();
6778 Field_reference_expression
* farg
= arg
->field_reference_expression();
6781 Expression
* struct_expr
= farg
->expr();
6782 Type
* st
= struct_expr
->type();
6783 if (st
->struct_type() == NULL
)
6785 tree struct_tree
= st
->get_tree(this->gogo_
);
6786 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
6787 tree field
= TYPE_FIELDS(struct_tree
);
6788 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
6790 field
= DECL_CHAIN(field
);
6791 gcc_assert(field
!= NULL_TREE
);
6793 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
6794 if (offset_wide
< 0)
6796 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
6797 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
6799 mpz_set_ui(val
, offset_long
);
6805 // Return a floating point constant value if possible.
6808 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
6811 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
6813 Expression
* arg
= this->one_arg();
6824 if (arg
->complex_constant_value(real
, imag
, &type
))
6826 if (this->code_
== BUILTIN_REAL
)
6827 mpfr_set(val
, real
, GMP_RNDN
);
6829 mpfr_set(val
, imag
, GMP_RNDN
);
6830 *ptype
= Builtin_call_expression::real_imag_type(type
);
6842 // Return a complex constant value if possible.
6845 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
6848 if (this->code_
== BUILTIN_CMPLX
)
6850 const Expression_list
* args
= this->args();
6851 if (args
== NULL
|| args
->size() != 2)
6857 if (!args
->front()->float_constant_value(r
, &rtype
))
6868 if (args
->back()->float_constant_value(i
, &itype
)
6869 && Type::are_identical(rtype
, itype
, false, NULL
))
6871 mpfr_set(real
, r
, GMP_RNDN
);
6872 mpfr_set(imag
, i
, GMP_RNDN
);
6873 *ptype
= Builtin_call_expression::cmplx_type(rtype
);
6889 Builtin_call_expression::do_type()
6891 switch (this->code_
)
6893 case BUILTIN_INVALID
:
6900 const Expression_list
* args
= this->args();
6901 if (args
== NULL
|| args
->empty())
6902 return Type::make_error_type();
6903 return Type::make_pointer_type(args
->front()->type());
6909 case BUILTIN_ALIGNOF
:
6910 case BUILTIN_OFFSETOF
:
6911 case BUILTIN_SIZEOF
:
6912 return Type::lookup_integer_type("int");
6917 case BUILTIN_PRINTLN
:
6918 return Type::make_void_type();
6920 case BUILTIN_CLOSED
:
6921 return Type::lookup_bool_type();
6923 case BUILTIN_RECOVER
:
6924 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
6926 case BUILTIN_APPEND
:
6928 const Expression_list
* args
= this->args();
6929 if (args
== NULL
|| args
->empty())
6930 return Type::make_error_type();
6931 return args
->front()->type();
6937 Expression
* arg
= this->one_arg();
6939 return Type::make_error_type();
6940 Type
* t
= arg
->type();
6941 if (t
->is_abstract())
6942 t
= t
->make_non_abstract_type();
6943 t
= Builtin_call_expression::real_imag_type(t
);
6945 t
= Type::make_error_type();
6951 const Expression_list
* args
= this->args();
6952 if (args
== NULL
|| args
->size() != 2)
6953 return Type::make_error_type();
6954 Type
* t
= args
->front()->type();
6955 if (t
->is_abstract())
6957 t
= args
->back()->type();
6958 if (t
->is_abstract())
6959 t
= t
->make_non_abstract_type();
6961 t
= Builtin_call_expression::cmplx_type(t
);
6963 t
= Type::make_error_type();
6969 // Determine the type.
6972 Builtin_call_expression::do_determine_type(const Type_context
* context
)
6974 this->fn()->determine_type_no_context();
6976 const Expression_list
* args
= this->args();
6979 Type
* arg_type
= NULL
;
6980 switch (this->code_
)
6983 case BUILTIN_PRINTLN
:
6984 // Do not force a large integer constant to "int".
6990 arg_type
= Builtin_call_expression::cmplx_type(context
->type
);
6996 // For the cmplx function the type of one operand can
6997 // determine the type of the other, as in a binary expression.
6998 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
6999 if (args
!= NULL
&& args
->size() == 2)
7001 Type
* t1
= args
->front()->type();
7002 Type
* t2
= args
->front()->type();
7003 if (!t1
->is_abstract())
7005 else if (!t2
->is_abstract())
7019 for (Expression_list::const_iterator pa
= args
->begin();
7023 Type_context subcontext
;
7024 subcontext
.type
= arg_type
;
7028 // We want to print large constants, we so can't just
7029 // use the appropriate nonabstract type. Use uint64 for
7030 // an integer if we know it is nonnegative, otherwise
7031 // use int64 for a integer, otherwise use float64 for a
7032 // float or complex128 for a complex.
7033 Type
* want_type
= NULL
;
7034 Type
* atype
= (*pa
)->type();
7035 if (atype
->is_abstract())
7037 if (atype
->integer_type() != NULL
)
7042 if (this->integer_constant_value(true, val
, &dummy
)
7043 && mpz_sgn(val
) >= 0)
7044 want_type
= Type::lookup_integer_type("uint64");
7046 want_type
= Type::lookup_integer_type("int64");
7049 else if (atype
->float_type() != NULL
)
7050 want_type
= Type::lookup_float_type("float64");
7051 else if (atype
->complex_type() != NULL
)
7052 want_type
= Type::lookup_complex_type("complex128");
7053 else if (atype
->is_abstract_string_type())
7054 want_type
= Type::lookup_string_type();
7055 else if (atype
->is_abstract_boolean_type())
7056 want_type
= Type::lookup_bool_type();
7059 subcontext
.type
= want_type
;
7063 (*pa
)->determine_type(&subcontext
);
7068 // If there is exactly one argument, return true. Otherwise give an
7069 // error message and return false.
7072 Builtin_call_expression::check_one_arg()
7074 const Expression_list
* args
= this->args();
7075 if (args
== NULL
|| args
->size() < 1)
7077 this->report_error(_("not enough arguments"));
7080 else if (args
->size() > 1)
7082 this->report_error(_("too many arguments"));
7085 if (args
->front()->is_error_expression()
7086 || args
->front()->type()->is_error_type()
7087 || args
->front()->type()->is_undefined())
7089 this->set_is_error();
7095 // Check argument types for a builtin function.
7098 Builtin_call_expression::do_check_types(Gogo
*)
7100 switch (this->code_
)
7102 case BUILTIN_INVALID
:
7110 // The single argument may be either a string or an array or a
7111 // map or a channel, or a pointer to a closed array.
7112 if (this->check_one_arg())
7114 Type
* arg_type
= this->one_arg()->type();
7115 if (arg_type
->points_to() != NULL
7116 && arg_type
->points_to()->array_type() != NULL
7117 && !arg_type
->points_to()->is_open_array_type())
7118 arg_type
= arg_type
->points_to();
7119 if (this->code_
== BUILTIN_CAP
)
7121 if (!arg_type
->is_error_type()
7122 && arg_type
->array_type() == NULL
7123 && arg_type
->channel_type() == NULL
)
7124 this->report_error(_("argument must be array or slice "
7129 if (!arg_type
->is_error_type()
7130 && !arg_type
->is_string_type()
7131 && arg_type
->array_type() == NULL
7132 && arg_type
->map_type() == NULL
7133 && arg_type
->channel_type() == NULL
)
7134 this->report_error(_("argument must be string or "
7135 "array or slice or map or channel"));
7142 case BUILTIN_PRINTLN
:
7144 const Expression_list
* args
= this->args();
7147 if (this->code_
== BUILTIN_PRINT
)
7148 warning_at(this->location(), 0,
7149 "no arguments for builtin function %<%s%>",
7150 (this->code_
== BUILTIN_PRINT
7156 for (Expression_list::const_iterator p
= args
->begin();
7160 Type
* type
= (*p
)->type();
7161 if (type
->is_error_type()
7162 || type
->is_string_type()
7163 || type
->integer_type() != NULL
7164 || type
->float_type() != NULL
7165 || type
->complex_type() != NULL
7166 || type
->is_boolean_type()
7167 || type
->points_to() != NULL
7168 || type
->interface_type() != NULL
7169 || type
->channel_type() != NULL
7170 || type
->map_type() != NULL
7171 || type
->function_type() != NULL
7172 || type
->is_open_array_type())
7175 this->report_error(_("unsupported argument type to "
7176 "builtin function"));
7183 case BUILTIN_CLOSED
:
7184 if (this->check_one_arg())
7186 if (this->one_arg()->type()->channel_type() == NULL
)
7187 this->report_error(_("argument must be channel"));
7192 case BUILTIN_SIZEOF
:
7193 case BUILTIN_ALIGNOF
:
7194 this->check_one_arg();
7197 case BUILTIN_RECOVER
:
7198 if (this->args() != NULL
&& !this->args()->empty())
7199 this->report_error(_("too many arguments"));
7202 case BUILTIN_OFFSETOF
:
7203 if (this->check_one_arg())
7205 Expression
* arg
= this->one_arg();
7206 if (arg
->field_reference_expression() == NULL
)
7207 this->report_error(_("argument must be a field reference"));
7213 const Expression_list
* args
= this->args();
7214 if (args
== NULL
|| args
->size() < 2)
7216 this->report_error(_("not enough arguments"));
7219 else if (args
->size() > 2)
7221 this->report_error(_("too many arguments"));
7224 Type
* arg1_type
= args
->front()->type();
7225 Type
* arg2_type
= args
->back()->type();
7226 if (arg1_type
->is_error_type() || arg2_type
->is_error_type())
7230 if (arg1_type
->is_open_array_type())
7231 e1
= arg1_type
->array_type()->element_type();
7234 this->report_error(_("left argument must be a slice"));
7239 if (arg2_type
->is_open_array_type())
7240 e2
= arg2_type
->array_type()->element_type();
7241 else if (arg2_type
->is_string_type())
7242 e2
= Type::lookup_integer_type("uint8");
7245 this->report_error(_("right argument must be a slice or a string"));
7249 if (!Type::are_identical(e1
, e2
, true, NULL
))
7250 this->report_error(_("element types must be the same"));
7254 case BUILTIN_APPEND
:
7256 const Expression_list
* args
= this->args();
7257 if (args
== NULL
|| args
->empty())
7259 this->report_error(_("not enough arguments"));
7262 /* Lowering varargs should have left us with 2 arguments. */
7263 gcc_assert(args
->size() == 2);
7265 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7269 this->report_error(_("arguments 1 and 2 have different types"));
7272 error_at(this->location(),
7273 "arguments 1 and 2 have different types (%s)",
7275 this->set_is_error();
7283 if (this->check_one_arg())
7285 if (this->one_arg()->type()->complex_type() == NULL
)
7286 this->report_error(_("argument must have complex type"));
7292 const Expression_list
* args
= this->args();
7293 if (args
== NULL
|| args
->size() < 2)
7294 this->report_error(_("not enough arguments"));
7295 else if (args
->size() > 2)
7296 this->report_error(_("too many arguments"));
7297 else if (args
->front()->is_error_expression()
7298 || args
->front()->type()->is_error_type()
7299 || args
->back()->is_error_expression()
7300 || args
->back()->type()->is_error_type())
7301 this->set_is_error();
7302 else if (!Type::are_identical(args
->front()->type(),
7303 args
->back()->type(), true, NULL
))
7304 this->report_error(_("cmplx arguments must have identical types"));
7305 else if (args
->front()->type()->float_type() == NULL
)
7306 this->report_error(_("cmplx arguments must have "
7307 "floating-point type"));
7316 // Return the tree for a builtin function.
7319 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7321 Gogo
* gogo
= context
->gogo();
7322 source_location location
= this->location();
7323 switch (this->code_
)
7325 case BUILTIN_INVALID
:
7333 const Expression_list
* args
= this->args();
7334 gcc_assert(args
!= NULL
&& args
->size() == 1);
7335 Expression
* arg
= *args
->begin();
7336 Type
* arg_type
= arg
->type();
7337 tree arg_tree
= arg
->get_tree(context
);
7338 if (arg_tree
== error_mark_node
)
7339 return error_mark_node
;
7341 if (arg_type
->points_to() != NULL
)
7343 arg_type
= arg_type
->points_to();
7344 gcc_assert(arg_type
->array_type() != NULL
7345 && !arg_type
->is_open_array_type());
7346 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7347 arg_tree
= build_fold_indirect_ref(arg_tree
);
7351 if (this->code_
== BUILTIN_LEN
)
7353 if (arg_type
->is_string_type())
7354 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7355 else if (arg_type
->array_type() != NULL
)
7356 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7357 else if (arg_type
->map_type() != NULL
)
7359 static tree map_len_fndecl
;
7360 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7365 arg_type
->get_tree(gogo
),
7368 else if (arg_type
->channel_type() != NULL
)
7370 static tree chan_len_fndecl
;
7371 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7376 arg_type
->get_tree(gogo
),
7384 if (arg_type
->array_type() != NULL
)
7385 val_tree
= arg_type
->array_type()->capacity_tree(gogo
, arg_tree
);
7386 else if (arg_type
->channel_type() != NULL
)
7388 static tree chan_cap_fndecl
;
7389 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7394 arg_type
->get_tree(gogo
),
7401 if (val_tree
== error_mark_node
)
7402 return error_mark_node
;
7404 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7405 if (type_tree
== TREE_TYPE(val_tree
))
7408 return fold(convert_to_integer(type_tree
, val_tree
));
7412 case BUILTIN_PRINTLN
:
7414 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7415 tree stmt_list
= NULL_TREE
;
7417 const Expression_list
* call_args
= this->args();
7418 if (call_args
!= NULL
)
7420 for (Expression_list::const_iterator p
= call_args
->begin();
7421 p
!= call_args
->end();
7424 if (is_ln
&& p
!= call_args
->begin())
7426 static tree print_space_fndecl
;
7427 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7432 if (call
== error_mark_node
)
7433 return error_mark_node
;
7434 append_to_statement_list(call
, &stmt_list
);
7437 Type
* type
= (*p
)->type();
7439 tree arg
= (*p
)->get_tree(context
);
7440 if (arg
== error_mark_node
)
7441 return error_mark_node
;
7445 if (type
->is_string_type())
7447 static tree print_string_fndecl
;
7448 pfndecl
= &print_string_fndecl
;
7449 fnname
= "__go_print_string";
7451 else if (type
->integer_type() != NULL
7452 && type
->integer_type()->is_unsigned())
7454 static tree print_uint64_fndecl
;
7455 pfndecl
= &print_uint64_fndecl
;
7456 fnname
= "__go_print_uint64";
7457 Type
* itype
= Type::lookup_integer_type("uint64");
7458 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7461 else if (type
->integer_type() != NULL
)
7463 static tree print_int64_fndecl
;
7464 pfndecl
= &print_int64_fndecl
;
7465 fnname
= "__go_print_int64";
7466 Type
* itype
= Type::lookup_integer_type("int64");
7467 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7470 else if (type
->float_type() != NULL
)
7472 static tree print_double_fndecl
;
7473 pfndecl
= &print_double_fndecl
;
7474 fnname
= "__go_print_double";
7475 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7477 else if (type
->complex_type() != NULL
)
7479 static tree print_complex_fndecl
;
7480 pfndecl
= &print_complex_fndecl
;
7481 fnname
= "__go_print_complex";
7482 arg
= fold_convert_loc(location
, complex_double_type_node
,
7485 else if (type
->is_boolean_type())
7487 static tree print_bool_fndecl
;
7488 pfndecl
= &print_bool_fndecl
;
7489 fnname
= "__go_print_bool";
7491 else if (type
->points_to() != NULL
7492 || type
->channel_type() != NULL
7493 || type
->map_type() != NULL
7494 || type
->function_type() != NULL
)
7496 static tree print_pointer_fndecl
;
7497 pfndecl
= &print_pointer_fndecl
;
7498 fnname
= "__go_print_pointer";
7499 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7501 else if (type
->interface_type() != NULL
)
7503 if (type
->interface_type()->is_empty())
7505 static tree print_empty_interface_fndecl
;
7506 pfndecl
= &print_empty_interface_fndecl
;
7507 fnname
= "__go_print_empty_interface";
7511 static tree print_interface_fndecl
;
7512 pfndecl
= &print_interface_fndecl
;
7513 fnname
= "__go_print_interface";
7516 else if (type
->is_open_array_type())
7518 static tree print_slice_fndecl
;
7519 pfndecl
= &print_slice_fndecl
;
7520 fnname
= "__go_print_slice";
7525 tree call
= Gogo::call_builtin(pfndecl
,
7532 if (call
== error_mark_node
)
7533 return error_mark_node
;
7534 append_to_statement_list(call
, &stmt_list
);
7540 static tree print_nl_fndecl
;
7541 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7546 if (call
== error_mark_node
)
7547 return error_mark_node
;
7548 append_to_statement_list(call
, &stmt_list
);
7556 const Expression_list
* args
= this->args();
7557 gcc_assert(args
!= NULL
&& args
->size() == 1);
7558 Expression
* arg
= args
->front();
7559 tree arg_tree
= arg
->get_tree(context
);
7560 if (arg_tree
== error_mark_node
)
7561 return error_mark_node
;
7562 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7563 arg_tree
= Expression::convert_for_assignment(context
, empty
,
7565 arg_tree
, location
);
7566 static tree panic_fndecl
;
7567 tree call
= Gogo::call_builtin(&panic_fndecl
,
7572 TREE_TYPE(arg_tree
),
7574 if (call
== error_mark_node
)
7575 return error_mark_node
;
7576 // This function will throw an exception.
7577 TREE_NOTHROW(panic_fndecl
) = 0;
7578 // This function will not return.
7579 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
7583 case BUILTIN_RECOVER
:
7585 // The argument is set when building recover thunks. It's a
7586 // boolean value which is true if we can recover a value now.
7587 const Expression_list
* args
= this->args();
7588 gcc_assert(args
!= NULL
&& args
->size() == 1);
7589 Expression
* arg
= args
->front();
7590 tree arg_tree
= arg
->get_tree(context
);
7591 if (arg_tree
== error_mark_node
)
7592 return error_mark_node
;
7594 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7595 tree empty_tree
= empty
->get_tree(context
->gogo());
7597 Type
* nil_type
= Type::make_nil_type();
7598 Expression
* nil
= Expression::make_nil(location
);
7599 tree nil_tree
= nil
->get_tree(context
);
7600 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
7606 // We need to handle a deferred call to recover specially,
7607 // because it changes whether it can recover a panic or not.
7608 // See test7 in test/recover1.go.
7610 if (this->is_deferred())
7612 static tree deferred_recover_fndecl
;
7613 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
7615 "__go_deferred_recover",
7621 static tree recover_fndecl
;
7622 call
= Gogo::call_builtin(&recover_fndecl
,
7628 if (call
== error_mark_node
)
7629 return error_mark_node
;
7630 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
7631 call
, empty_nil_tree
);
7635 case BUILTIN_CLOSED
:
7637 const Expression_list
* args
= this->args();
7638 gcc_assert(args
!= NULL
&& args
->size() == 1);
7639 Expression
* arg
= args
->front();
7640 tree arg_tree
= arg
->get_tree(context
);
7641 if (arg_tree
== error_mark_node
)
7642 return error_mark_node
;
7643 if (this->code_
== BUILTIN_CLOSE
)
7645 static tree close_fndecl
;
7646 return Gogo::call_builtin(&close_fndecl
,
7648 "__go_builtin_close",
7651 TREE_TYPE(arg_tree
),
7656 static tree closed_fndecl
;
7657 return Gogo::call_builtin(&closed_fndecl
,
7659 "__go_builtin_closed",
7662 TREE_TYPE(arg_tree
),
7667 case BUILTIN_SIZEOF
:
7668 case BUILTIN_OFFSETOF
:
7669 case BUILTIN_ALIGNOF
:
7674 bool b
= this->integer_constant_value(true, val
, &dummy
);
7676 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
7677 tree ret
= Expression::integer_constant_tree(val
, type
);
7684 const Expression_list
* args
= this->args();
7685 gcc_assert(args
!= NULL
&& args
->size() == 2);
7686 Expression
* arg1
= args
->front();
7687 Expression
* arg2
= args
->back();
7689 tree arg1_tree
= arg1
->get_tree(context
);
7690 tree arg2_tree
= arg2
->get_tree(context
);
7691 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7692 return error_mark_node
;
7694 Type
* arg1_type
= arg1
->type();
7695 Array_type
* at
= arg1_type
->array_type();
7696 arg1_tree
= save_expr(arg1_tree
);
7697 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
7698 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
7699 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
7700 return error_mark_node
;
7702 Type
* arg2_type
= arg2
->type();
7705 if (arg2_type
->is_open_array_type())
7707 at
= arg2_type
->array_type();
7708 arg2_tree
= save_expr(arg2_tree
);
7709 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7710 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7714 arg2_tree
= save_expr(arg2_tree
);
7715 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
7716 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
7718 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7719 return error_mark_node
;
7721 arg1_len
= save_expr(arg1_len
);
7722 arg2_len
= save_expr(arg2_len
);
7723 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
7724 fold_build2_loc(location
, LT_EXPR
,
7726 arg1_len
, arg2_len
),
7727 arg1_len
, arg2_len
);
7728 len
= save_expr(len
);
7730 Type
* element_type
= at
->element_type();
7731 tree element_type_tree
= element_type
->get_tree(gogo
);
7732 if (element_type_tree
== error_mark_node
)
7733 return error_mark_node
;
7734 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7735 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
7737 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
7738 TREE_TYPE(element_size
),
7739 bytecount
, element_size
);
7740 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
7742 tree call
= build_call_expr_loc(location
,
7743 built_in_decls
[BUILT_IN_MEMMOVE
],
7744 3, arg1_val
, arg2_val
, bytecount
);
7746 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
7750 case BUILTIN_APPEND
:
7752 const Expression_list
* args
= this->args();
7753 gcc_assert(args
!= NULL
&& args
->size() == 2);
7754 Expression
* arg1
= args
->front();
7755 Expression
* arg2
= args
->back();
7757 tree arg1_tree
= arg1
->get_tree(context
);
7758 tree arg2_tree
= arg2
->get_tree(context
);
7759 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7760 return error_mark_node
;
7762 tree descriptor_tree
= arg1
->type()->type_descriptor_pointer(gogo
);
7764 // We rebuild the decl each time since the slice types may
7766 tree append_fndecl
= NULL_TREE
;
7767 return Gogo::call_builtin(&append_fndecl
,
7771 TREE_TYPE(arg1_tree
),
7772 TREE_TYPE(descriptor_tree
),
7774 TREE_TYPE(arg1_tree
),
7776 TREE_TYPE(arg2_tree
),
7783 const Expression_list
* args
= this->args();
7784 gcc_assert(args
!= NULL
&& args
->size() == 1);
7785 Expression
* arg
= args
->front();
7786 tree arg_tree
= arg
->get_tree(context
);
7787 if (arg_tree
== error_mark_node
)
7788 return error_mark_node
;
7789 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
7790 if (this->code_
== BUILTIN_REAL
)
7791 return fold_build1_loc(location
, REALPART_EXPR
,
7792 TREE_TYPE(TREE_TYPE(arg_tree
)),
7795 return fold_build1_loc(location
, IMAGPART_EXPR
,
7796 TREE_TYPE(TREE_TYPE(arg_tree
)),
7802 const Expression_list
* args
= this->args();
7803 gcc_assert(args
!= NULL
&& args
->size() == 2);
7804 tree r
= args
->front()->get_tree(context
);
7805 tree i
= args
->back()->get_tree(context
);
7806 if (r
== error_mark_node
|| i
== error_mark_node
)
7807 return error_mark_node
;
7808 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
7809 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
7810 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
7811 return fold_build2_loc(location
, COMPLEX_EXPR
,
7812 build_complex_type(TREE_TYPE(r
)),
7821 // We have to support exporting a builtin call expression, because
7822 // code can set a constant to the result of a builtin expression.
7825 Builtin_call_expression::do_export(Export
* exp
) const
7832 if (this->integer_constant_value(true, val
, &dummy
))
7834 Integer_expression::export_integer(exp
, val
);
7843 if (this->float_constant_value(fval
, &dummy
))
7845 Float_expression::export_float(exp
, fval
);
7857 if (this->complex_constant_value(real
, imag
, &dummy
))
7859 Complex_expression::export_complex(exp
, real
, imag
);
7868 error_at(this->location(), "value is not constant");
7872 // A trailing space lets us reliably identify the end of the number.
7873 exp
->write_c_string(" ");
7876 // Class Call_expression.
7881 Call_expression::do_traverse(Traverse
* traverse
)
7883 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
7884 return TRAVERSE_EXIT
;
7885 if (this->args_
!= NULL
)
7887 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
7888 return TRAVERSE_EXIT
;
7890 return TRAVERSE_CONTINUE
;
7893 // Lower a call statement.
7896 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
7898 // A type case can look like a function call.
7899 if (this->fn_
->is_type_expression()
7900 && this->args_
!= NULL
7901 && this->args_
->size() == 1)
7902 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
7905 // Recognize a call to a builtin function.
7906 Func_expression
* fne
= this->fn_
->func_expression();
7908 && fne
->named_object()->is_function_declaration()
7909 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
7910 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
7911 this->is_varargs_
, this->location());
7913 // Handle an argument which is a call to a function which returns
7914 // multiple results.
7915 if (this->args_
!= NULL
7916 && this->args_
->size() == 1
7917 && this->args_
->front()->call_expression() != NULL
7918 && this->fn_
->type()->function_type() != NULL
)
7920 Function_type
* fntype
= this->fn_
->type()->function_type();
7921 size_t rc
= this->args_
->front()->call_expression()->result_count();
7923 && fntype
->parameters() != NULL
7924 && (fntype
->parameters()->size() == rc
7925 || (fntype
->is_varargs()
7926 && fntype
->parameters()->size() - 1 <= rc
)))
7928 Call_expression
* call
= this->args_
->front()->call_expression();
7929 Expression_list
* args
= new Expression_list
;
7930 for (size_t i
= 0; i
< rc
; ++i
)
7931 args
->push_back(Expression::make_call_result(call
, i
));
7932 // We can't return a new call expression here, because this
7933 // one may be referenced by Call_result expressions. FIXME.
7939 // Handle a call to a varargs function by packaging up the extra
7941 if (this->fn_
->type()->function_type() != NULL
7942 && this->fn_
->type()->function_type()->is_varargs())
7944 Function_type
* fntype
= this->fn_
->type()->function_type();
7945 const Typed_identifier_list
* parameters
= fntype
->parameters();
7946 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
7947 Type
* varargs_type
= parameters
->back().type();
7948 return this->lower_varargs(gogo
, function
, varargs_type
,
7949 parameters
->size());
7955 // Lower a call to a varargs function. FUNCTION is the function in
7956 // which the call occurs--it's not the function we are calling.
7957 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
7958 // PARAM_COUNT is the number of parameters of the function we are
7959 // calling; the last of these parameters will be the varargs
7963 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
7964 Type
* varargs_type
, size_t param_count
)
7966 if (this->varargs_are_lowered_
)
7969 source_location loc
= this->location();
7971 gcc_assert(param_count
> 0);
7972 gcc_assert(varargs_type
->is_open_array_type());
7974 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
7975 if (arg_count
< param_count
- 1)
7977 // Not enough arguments; will be caught in check_types.
7981 Expression_list
* old_args
= this->args_
;
7982 Expression_list
* new_args
= new Expression_list();
7983 bool push_empty_arg
= false;
7984 if (old_args
== NULL
|| old_args
->empty())
7986 gcc_assert(param_count
== 1);
7987 push_empty_arg
= true;
7991 Expression_list::const_iterator pa
;
7993 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
7995 if (static_cast<size_t>(i
) == param_count
)
7997 new_args
->push_back(*pa
);
8000 // We have reached the varargs parameter.
8002 bool issued_error
= false;
8003 if (pa
== old_args
->end())
8004 push_empty_arg
= true;
8005 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8006 new_args
->push_back(*pa
);
8007 else if (this->is_varargs_
)
8009 this->report_error(_("too many arguments"));
8012 else if (pa
+ 1 == old_args
->end()
8013 && this->is_compatible_varargs_argument(function
, *pa
,
8016 new_args
->push_back(*pa
);
8019 Type
* element_type
= varargs_type
->array_type()->element_type();
8020 Expression_list
* vals
= new Expression_list
;
8021 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8023 // Check types here so that we get a better message.
8024 Type
* patype
= (*pa
)->type();
8025 source_location paloc
= (*pa
)->location();
8026 if (!this->check_argument_type(i
, element_type
, patype
,
8027 paloc
, issued_error
))
8029 vals
->push_back(*pa
);
8032 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8033 new_args
->push_back(val
);
8038 new_args
->push_back(Expression::make_nil(loc
));
8040 // We can't return a new call expression here, because this one may
8041 // be referenced by Call_result expressions. FIXME.
8042 if (old_args
!= NULL
)
8044 this->args_
= new_args
;
8045 this->varargs_are_lowered_
= true;
8047 // Lower all the new subexpressions.
8048 Expression
* ret
= this;
8049 gogo
->lower_expression(function
, &ret
);
8050 gcc_assert(ret
== this);
8054 // Return true if ARG is a varargs argment which should be passed to
8055 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8056 // will be the last argument passed in the call, and PARAM_TYPE will
8057 // be the type of the last parameter of the varargs function being
8061 Call_expression::is_compatible_varargs_argument(Named_object
* function
,
8066 *issued_error
= false;
8068 Type
* var_type
= NULL
;
8070 // The simple case is passing the varargs parameter of the caller.
8071 Var_expression
* ve
= arg
->var_expression();
8072 if (ve
!= NULL
&& ve
->named_object()->is_variable())
8074 Variable
* var
= ve
->named_object()->var_value();
8075 if (var
->is_varargs_parameter())
8076 var_type
= var
->type();
8079 // The complex case is passing the varargs parameter of some
8080 // enclosing function. This will look like passing down *c.f where
8081 // c is the closure variable and f is a field in the closure.
8082 if (function
!= NULL
8083 && function
->func_value()->needs_closure()
8084 && arg
->classification() == EXPRESSION_UNARY
)
8086 Unary_expression
* ue
= static_cast<Unary_expression
*>(arg
);
8087 if (ue
->op() == OPERATOR_MULT
)
8089 Field_reference_expression
* fre
=
8090 ue
->operand()->deref()->field_reference_expression();
8093 Var_expression
* ve
= fre
->expr()->deref()->var_expression();
8096 Named_object
* no
= ve
->named_object();
8097 Function
* f
= function
->func_value();
8098 if (no
== f
->closure_var())
8100 // At this point we know that this indeed a
8101 // reference to some enclosing variable. Now we
8102 // need to figure out whether that variable is a
8103 // varargs parameter.
8104 Named_object
* enclosing
=
8105 f
->enclosing_var(fre
->field_index());
8106 Variable
* var
= enclosing
->var_value();
8107 if (var
->is_varargs_parameter())
8108 var_type
= var
->type();
8115 if (var_type
== NULL
)
8118 // We only match if the parameter is the same, with an identical
8120 Array_type
* var_at
= var_type
->array_type();
8121 gcc_assert(var_at
!= NULL
);
8122 Array_type
* param_at
= param_type
->array_type();
8123 if (param_at
!= NULL
8124 && Type::are_identical(var_at
->element_type(),
8125 param_at
->element_type(), true, NULL
))
8127 error_at(arg
->location(), "... mismatch: passing ...T as ...");
8128 *issued_error
= true;
8132 // Get the function type. Returns NULL if we don't know the type. If
8133 // this returns NULL, and if_ERROR is true, issues an error.
8136 Call_expression::get_function_type() const
8138 return this->fn_
->type()->function_type();
8141 // Return the number of values which this call will return.
8144 Call_expression::result_count() const
8146 const Function_type
* fntype
= this->get_function_type();
8149 if (fntype
->results() == NULL
)
8151 return fntype
->results()->size();
8154 // Return whether this is a call to the predeclared function recover.
8157 Call_expression::is_recover_call() const
8159 return this->do_is_recover_call();
8162 // Set the argument to the recover function.
8165 Call_expression::set_recover_arg(Expression
* arg
)
8167 this->do_set_recover_arg(arg
);
8170 // Virtual functions also implemented by Builtin_call_expression.
8173 Call_expression::do_is_recover_call() const
8179 Call_expression::do_set_recover_arg(Expression
*)
8187 Call_expression::do_type()
8189 if (this->type_
!= NULL
)
8193 Function_type
* fntype
= this->get_function_type();
8195 return Type::make_error_type();
8197 const Typed_identifier_list
* results
= fntype
->results();
8198 if (results
== NULL
)
8199 ret
= Type::make_void_type();
8200 else if (results
->size() == 1)
8201 ret
= results
->begin()->type();
8203 ret
= Type::make_call_multiple_result_type(this);
8210 // Determine types for a call expression. We can use the function
8211 // parameter types to set the types of the arguments.
8214 Call_expression::do_determine_type(const Type_context
*)
8216 this->fn_
->determine_type_no_context();
8217 Function_type
* fntype
= this->get_function_type();
8218 const Typed_identifier_list
* parameters
= NULL
;
8220 parameters
= fntype
->parameters();
8221 if (this->args_
!= NULL
)
8223 Typed_identifier_list::const_iterator pt
;
8224 if (parameters
!= NULL
)
8225 pt
= parameters
->begin();
8226 for (Expression_list::const_iterator pa
= this->args_
->begin();
8227 pa
!= this->args_
->end();
8230 if (parameters
!= NULL
&& pt
!= parameters
->end())
8232 Type_context
subcontext(pt
->type(), false);
8233 (*pa
)->determine_type(&subcontext
);
8237 (*pa
)->determine_type_no_context();
8242 // Check types for parameter I.
8245 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8246 const Type
* argument_type
,
8247 source_location argument_location
,
8251 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8256 error_at(argument_location
, "argument %d has incompatible type", i
);
8258 error_at(argument_location
,
8259 "argument %d has incompatible type (%s)",
8262 this->set_is_error();
8271 Call_expression::do_check_types(Gogo
*)
8273 Function_type
* fntype
= this->get_function_type();
8276 if (!this->fn_
->type()->is_error_type())
8277 this->report_error(_("expected function"));
8281 if (fntype
->is_method())
8283 // We don't support pointers to methods, so the function has to
8284 // be a bound method expression.
8285 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8288 this->report_error(_("method call without object"));
8291 Type
* first_arg_type
= bme
->first_argument()->type();
8292 if (first_arg_type
->points_to() == NULL
)
8294 // When passing a value, we need to check that we are
8295 // permitted to copy it.
8297 if (!Type::are_assignable(fntype
->receiver()->type(),
8298 first_arg_type
, &reason
))
8301 this->report_error(_("incompatible type for receiver"));
8304 error_at(this->location(),
8305 "incompatible type for receiver (%s)",
8307 this->set_is_error();
8313 // Note that varargs was handled by the lower_varargs() method, so
8314 // we don't have to worry about it here.
8316 const Typed_identifier_list
* parameters
= fntype
->parameters();
8317 if (this->args_
== NULL
)
8319 if (parameters
!= NULL
&& !parameters
->empty())
8320 this->report_error(_("not enough arguments"));
8322 else if (parameters
== NULL
)
8323 this->report_error(_("too many arguments"));
8327 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8328 for (Expression_list::const_iterator pa
= this->args_
->begin();
8329 pa
!= this->args_
->end();
8332 if (pt
== parameters
->end())
8334 this->report_error(_("too many arguments"));
8337 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8338 (*pa
)->location(), false);
8340 if (pt
!= parameters
->end())
8341 this->report_error(_("not enough arguments"));
8345 // Return whether we have to use a temporary variable to ensure that
8346 // we evaluate this call expression in order. If the call returns no
8347 // results then it will inevitably be executed last. If the call
8348 // returns more than one result then it will be used with Call_result
8349 // expressions. So we only have to use a temporary variable if the
8350 // call returns exactly one result.
8353 Call_expression::do_must_eval_in_order() const
8355 return this->result_count() == 1;
8358 // Get the function and the first argument to use when calling a bound
8362 Call_expression::bound_method_function(Translate_context
* context
,
8363 Bound_method_expression
* bound_method
,
8364 tree
* first_arg_ptr
)
8366 Expression
* first_argument
= bound_method
->first_argument();
8367 tree first_arg
= first_argument
->get_tree(context
);
8368 if (first_arg
== error_mark_node
)
8369 return error_mark_node
;
8371 // We always pass a pointer to the first argument when calling a
8373 if (first_argument
->type()->points_to() == NULL
)
8375 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8376 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8377 || DECL_P(first_arg
)
8378 || TREE_CODE(first_arg
) == INDIRECT_REF
8379 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8381 first_arg
= build_fold_addr_expr(first_arg
);
8382 if (DECL_P(first_arg
))
8383 TREE_ADDRESSABLE(first_arg
) = 1;
8387 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8388 get_name(first_arg
));
8389 DECL_IGNORED_P(tmp
) = 0;
8390 DECL_INITIAL(tmp
) = first_arg
;
8391 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8392 build1(DECL_EXPR
, void_type_node
, tmp
),
8393 build_fold_addr_expr(tmp
));
8394 TREE_ADDRESSABLE(tmp
) = 1;
8396 if (first_arg
== error_mark_node
)
8397 return error_mark_node
;
8400 Type
* fatype
= bound_method
->first_argument_type();
8403 if (fatype
->points_to() == NULL
)
8404 fatype
= Type::make_pointer_type(fatype
);
8405 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8406 if (first_arg
== error_mark_node
8407 || TREE_TYPE(first_arg
) == error_mark_node
)
8408 return error_mark_node
;
8411 *first_arg_ptr
= first_arg
;
8413 return bound_method
->method()->get_tree(context
);
8416 // Get the function and the first argument to use when calling an
8417 // interface method.
8420 Call_expression::interface_method_function(
8421 Translate_context
* context
,
8422 Interface_field_reference_expression
* interface_method
,
8423 tree
* first_arg_ptr
)
8425 tree expr
= interface_method
->expr()->get_tree(context
);
8426 if (expr
== error_mark_node
)
8427 return error_mark_node
;
8428 expr
= save_expr(expr
);
8429 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8430 if (first_arg
== error_mark_node
)
8431 return error_mark_node
;
8432 *first_arg_ptr
= first_arg
;
8433 return interface_method
->get_function_tree(context
, expr
);
8436 // Build the call expression.
8439 Call_expression::do_get_tree(Translate_context
* context
)
8441 if (this->tree_
!= NULL_TREE
)
8444 Function_type
* fntype
= this->get_function_type();
8446 return error_mark_node
;
8448 if (this->fn_
->is_error_expression())
8449 return error_mark_node
;
8451 Gogo
* gogo
= context
->gogo();
8452 source_location location
= this->location();
8454 Func_expression
* func
= this->fn_
->func_expression();
8455 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8456 Interface_field_reference_expression
* interface_method
=
8457 this->fn_
->interface_field_reference_expression();
8458 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8459 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8460 gcc_assert(!fntype
->is_method() || is_method
);
8464 if (this->args_
== NULL
|| this->args_
->empty())
8466 nargs
= is_method
? 1 : 0;
8467 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8471 const Typed_identifier_list
* params
= fntype
->parameters();
8472 gcc_assert(params
!= NULL
);
8474 nargs
= this->args_
->size();
8475 int i
= is_method
? 1 : 0;
8477 args
= new tree
[nargs
];
8479 Typed_identifier_list::const_iterator pp
= params
->begin();
8480 Expression_list::const_iterator pe
;
8481 for (pe
= this->args_
->begin();
8482 pe
!= this->args_
->end();
8485 tree arg_val
= (*pe
)->get_tree(context
);
8486 args
[i
] = Expression::convert_for_assignment(context
,
8491 if (args
[i
] == error_mark_node
)
8492 return error_mark_node
;
8494 gcc_assert(pp
== params
->end());
8495 gcc_assert(i
== nargs
);
8498 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8499 if (rettype
== error_mark_node
)
8500 return error_mark_node
;
8504 fn
= func
->get_tree_without_closure(gogo
);
8505 else if (!is_method
)
8506 fn
= this->fn_
->get_tree(context
);
8507 else if (bound_method
!= NULL
)
8508 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8509 else if (interface_method
!= NULL
)
8510 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8514 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8515 return error_mark_node
;
8517 // This is to support builtin math functions when using 80387 math.
8519 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8520 fndecl
= TREE_OPERAND(fndecl
, 0);
8521 tree excess_type
= NULL_TREE
;
8523 && DECL_IS_BUILTIN(fndecl
)
8524 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8526 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8527 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8528 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8529 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8531 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8532 if (excess_type
!= NULL_TREE
)
8534 tree excess_fndecl
= mathfn_built_in(excess_type
,
8535 DECL_FUNCTION_CODE(fndecl
));
8536 if (excess_fndecl
== NULL_TREE
)
8537 excess_type
= NULL_TREE
;
8540 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8541 for (int i
= 0; i
< nargs
; ++i
)
8542 args
[i
] = ::convert(excess_type
, args
[i
]);
8547 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8551 SET_EXPR_LOCATION(ret
, location
);
8555 tree closure_tree
= func
->closure()->get_tree(context
);
8556 if (closure_tree
!= error_mark_node
)
8557 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8560 // If this is a recursive function type which returns itself, as in
8562 // we have used ptr_type_node for the return type. Add a cast here
8563 // to the correct type.
8564 if (TREE_TYPE(ret
) == ptr_type_node
)
8566 tree t
= this->type()->get_tree(gogo
);
8567 ret
= fold_convert_loc(location
, t
, ret
);
8570 if (excess_type
!= NULL_TREE
)
8572 // Calling convert here can undo our excess precision change.
8573 // That may or may not be a bug in convert_to_real.
8574 ret
= build1(NOP_EXPR
, rettype
, ret
);
8577 // If there is more than one result, we will refer to the call
8579 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
8580 ret
= save_expr(ret
);
8587 // Make a call expression.
8590 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
8591 source_location location
)
8593 return new Call_expression(fn
, args
, is_varargs
, location
);
8596 // A single result from a call which returns multiple results.
8598 class Call_result_expression
: public Expression
8601 Call_result_expression(Call_expression
* call
, unsigned int index
)
8602 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
8603 call_(call
), index_(index
)
8608 do_traverse(Traverse
*);
8614 do_determine_type(const Type_context
*);
8617 do_check_types(Gogo
*);
8622 return new Call_result_expression(this->call_
->call_expression(),
8627 do_must_eval_in_order() const
8631 do_get_tree(Translate_context
*);
8634 // The underlying call expression.
8636 // Which result we want.
8637 unsigned int index_
;
8640 // Traverse a call result.
8643 Call_result_expression::do_traverse(Traverse
* traverse
)
8645 if (traverse
->remember_expression(this->call_
))
8647 // We have already traversed the call expression.
8648 return TRAVERSE_CONTINUE
;
8650 return Expression::traverse(&this->call_
, traverse
);
8656 Call_result_expression::do_type()
8658 // THIS->CALL_ can be replaced with a temporary reference due to
8659 // Call_expression::do_must_eval_in_order when there is an error.
8660 Call_expression
* ce
= this->call_
->call_expression();
8662 return Type::make_error_type();
8663 Function_type
* fntype
= ce
->get_function_type();
8665 return Type::make_error_type();
8666 const Typed_identifier_list
* results
= fntype
->results();
8667 Typed_identifier_list::const_iterator pr
= results
->begin();
8668 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8670 if (pr
== results
->end())
8671 return Type::make_error_type();
8674 if (pr
== results
->end())
8675 return Type::make_error_type();
8679 // Check the type. This is where we give an error if we're trying to
8680 // extract too many values from a call.
8683 Call_result_expression::do_check_types(Gogo
*)
8686 Call_expression
* ce
= this->call_
->call_expression();
8688 ok
= this->index_
< ce
->result_count();
8691 // This can happen when the call returns a single value but we
8692 // are asking for the second result.
8693 if (this->call_
->is_error_expression())
8698 this->report_error(_("number of results does not match number of values"));
8701 // Determine the type. We have nothing to do here, but the 0 result
8702 // needs to pass down to the caller.
8705 Call_result_expression::do_determine_type(const Type_context
*)
8707 if (this->index_
== 0)
8708 this->call_
->determine_type_no_context();
8714 Call_result_expression::do_get_tree(Translate_context
* context
)
8716 tree call_tree
= this->call_
->get_tree(context
);
8717 if (call_tree
== error_mark_node
)
8718 return error_mark_node
;
8719 gcc_assert(TREE_CODE(TREE_TYPE(call_tree
)) == RECORD_TYPE
);
8720 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
8721 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8723 gcc_assert(field
!= NULL_TREE
);
8724 field
= DECL_CHAIN(field
);
8726 gcc_assert(field
!= NULL_TREE
);
8727 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
8730 // Make a reference to a single result of a call which returns
8731 // multiple results.
8734 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
8736 return new Call_result_expression(call
, index
);
8739 // Class Index_expression.
8744 Index_expression::do_traverse(Traverse
* traverse
)
8746 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
8747 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
8748 || (this->end_
!= NULL
8749 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
8750 return TRAVERSE_EXIT
;
8751 return TRAVERSE_CONTINUE
;
8754 // Lower an index expression. This converts the generic index
8755 // expression into an array index, a string index, or a map index.
8758 Index_expression::do_lower(Gogo
*, Named_object
*, int)
8760 source_location location
= this->location();
8761 Expression
* left
= this->left_
;
8762 Expression
* start
= this->start_
;
8763 Expression
* end
= this->end_
;
8765 Type
* type
= left
->type();
8766 if (type
->is_error_type())
8767 return Expression::make_error(location
);
8768 else if (type
->array_type() != NULL
)
8769 return Expression::make_array_index(left
, start
, end
, location
);
8770 else if (type
->points_to() != NULL
8771 && type
->points_to()->array_type() != NULL
8772 && !type
->points_to()->is_open_array_type())
8774 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
8776 return Expression::make_array_index(deref
, start
, end
, location
);
8778 else if (type
->is_string_type())
8779 return Expression::make_string_index(left
, start
, end
, location
);
8780 else if (type
->map_type() != NULL
)
8784 error_at(location
, "invalid slice of map");
8785 return Expression::make_error(location
);
8787 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
8789 if (this->is_lvalue_
)
8790 ret
->set_is_lvalue();
8796 "attempt to index object which is not array, string, or map");
8797 return Expression::make_error(location
);
8801 // Make an index expression.
8804 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
8805 source_location location
)
8807 return new Index_expression(left
, start
, end
, location
);
8810 // An array index. This is used for both indexing and slicing.
8812 class Array_index_expression
: public Expression
8815 Array_index_expression(Expression
* array
, Expression
* start
,
8816 Expression
* end
, source_location location
)
8817 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
8818 array_(array
), start_(start
), end_(end
), type_(NULL
)
8823 do_traverse(Traverse
*);
8829 do_determine_type(const Type_context
*);
8832 do_check_types(Gogo
*);
8837 return Expression::make_array_index(this->array_
->copy(),
8838 this->start_
->copy(),
8841 : this->end_
->copy()),
8846 do_is_addressable() const;
8849 do_address_taken(bool escapes
)
8850 { this->array_
->address_taken(escapes
); }
8853 do_get_tree(Translate_context
*);
8856 // The array we are getting a value from.
8858 // The start or only index.
8860 // The end index of a slice. This may be NULL for a simple array
8861 // index, or it may be a nil expression for the length of the array.
8863 // The type of the expression.
8867 // Array index traversal.
8870 Array_index_expression::do_traverse(Traverse
* traverse
)
8872 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
8873 return TRAVERSE_EXIT
;
8874 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
8875 return TRAVERSE_EXIT
;
8876 if (this->end_
!= NULL
)
8878 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
8879 return TRAVERSE_EXIT
;
8881 return TRAVERSE_CONTINUE
;
8884 // Return the type of an array index.
8887 Array_index_expression::do_type()
8889 if (this->type_
== NULL
)
8891 Array_type
* type
= this->array_
->type()->array_type();
8893 this->type_
= Type::make_error_type();
8894 else if (this->end_
== NULL
)
8895 this->type_
= type
->element_type();
8896 else if (type
->is_open_array_type())
8898 // A slice of a slice has the same type as the original
8900 this->type_
= this->array_
->type()->deref();
8904 // A slice of an array is a slice.
8905 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
8911 // Set the type of an array index.
8914 Array_index_expression::do_determine_type(const Type_context
*)
8916 this->array_
->determine_type_no_context();
8917 Type_context
subcontext(NULL
, true);
8918 this->start_
->determine_type(&subcontext
);
8919 if (this->end_
!= NULL
)
8920 this->end_
->determine_type(&subcontext
);
8923 // Check types of an array index.
8926 Array_index_expression::do_check_types(Gogo
*)
8928 if (this->start_
->type()->integer_type() == NULL
)
8929 this->report_error(_("index must be integer"));
8930 if (this->end_
!= NULL
8931 && this->end_
->type()->integer_type() == NULL
8932 && !this->end_
->is_nil_expression())
8933 this->report_error(_("slice end must be integer"));
8935 Array_type
* array_type
= this->array_
->type()->array_type();
8936 gcc_assert(array_type
!= NULL
);
8938 unsigned int int_bits
=
8939 Type::lookup_integer_type("int")->integer_type()->bits();
8944 bool lval_valid
= (array_type
->length() != NULL
8945 && array_type
->length()->integer_constant_value(true,
8950 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
8952 if (mpz_sgn(ival
) < 0
8953 || mpz_sizeinbase(ival
, 2) >= int_bits
8955 && (this->end_
== NULL
8956 ? mpz_cmp(ival
, lval
) >= 0
8957 : mpz_cmp(ival
, lval
) > 0)))
8959 error_at(this->start_
->location(), "array index out of bounds");
8960 this->set_is_error();
8963 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
8965 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
8967 if (mpz_sgn(ival
) < 0
8968 || mpz_sizeinbase(ival
, 2) >= int_bits
8969 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
8971 error_at(this->end_
->location(), "array index out of bounds");
8972 this->set_is_error();
8979 // A slice of an array requires an addressable array. A slice of a
8980 // slice is always possible.
8981 if (this->end_
!= NULL
8982 && !array_type
->is_open_array_type()
8983 && !this->array_
->is_addressable())
8984 this->report_error(_("array is not addressable"));
8987 // Return whether this expression is addressable.
8990 Array_index_expression::do_is_addressable() const
8992 // A slice expression is not addressable.
8993 if (this->end_
!= NULL
)
8996 // An index into a slice is addressable.
8997 if (this->array_
->type()->is_open_array_type())
9000 // An index into an array is addressable if the array is
9002 return this->array_
->is_addressable();
9005 // Get a tree for an array index.
9008 Array_index_expression::do_get_tree(Translate_context
* context
)
9010 Gogo
* gogo
= context
->gogo();
9011 source_location loc
= this->location();
9013 Array_type
* array_type
= this->array_
->type()->array_type();
9014 gcc_assert(array_type
!= NULL
);
9016 tree type_tree
= array_type
->get_tree(gogo
);
9017 if (type_tree
== error_mark_node
)
9018 return error_mark_node
;
9020 tree array_tree
= this->array_
->get_tree(context
);
9021 if (array_tree
== error_mark_node
)
9022 return error_mark_node
;
9024 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9025 array_tree
= save_expr(array_tree
);
9026 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
9027 if (length_tree
== error_mark_node
)
9028 return error_mark_node
;
9029 length_tree
= save_expr(length_tree
);
9030 tree length_type
= TREE_TYPE(length_tree
);
9032 tree bad_index
= boolean_false_node
;
9034 tree start_tree
= this->start_
->get_tree(context
);
9035 if (start_tree
== error_mark_node
)
9036 return error_mark_node
;
9037 if (!DECL_P(start_tree
))
9038 start_tree
= save_expr(start_tree
);
9039 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9040 start_tree
= convert_to_integer(length_type
, start_tree
);
9042 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9045 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9046 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9047 fold_build2_loc(loc
,
9051 boolean_type_node
, start_tree
,
9054 int code
= (array_type
->length() != NULL
9055 ? (this->end_
== NULL
9056 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9057 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9058 : (this->end_
== NULL
9059 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9060 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9061 tree crash
= Gogo::runtime_error(code
, loc
);
9063 if (this->end_
== NULL
)
9065 // Simple array indexing. This has to return an l-value, so
9066 // wrap the index check into START_TREE.
9067 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9068 build3(COND_EXPR
, void_type_node
,
9069 bad_index
, crash
, NULL_TREE
),
9071 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9073 if (array_type
->length() != NULL
)
9076 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9077 start_tree
, NULL_TREE
, NULL_TREE
);
9082 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9083 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9084 if (element_type_tree
== error_mark_node
)
9085 return error_mark_node
;
9086 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9087 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9088 start_tree
, element_size
);
9089 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9090 TREE_TYPE(values
), values
, offset
);
9091 return build_fold_indirect_ref(ptr
);
9097 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9098 if (capacity_tree
== error_mark_node
)
9099 return error_mark_node
;
9100 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9103 if (this->end_
->is_nil_expression())
9104 end_tree
= length_tree
;
9107 end_tree
= this->end_
->get_tree(context
);
9108 if (end_tree
== error_mark_node
)
9109 return error_mark_node
;
9110 if (!DECL_P(end_tree
))
9111 end_tree
= save_expr(end_tree
);
9112 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9113 end_tree
= convert_to_integer(length_type
, end_tree
);
9115 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9118 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9120 capacity_tree
= save_expr(capacity_tree
);
9121 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9122 fold_build2_loc(loc
, LT_EXPR
,
9124 end_tree
, start_tree
),
9125 fold_build2_loc(loc
, GT_EXPR
,
9127 end_tree
, capacity_tree
));
9128 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9129 bad_index
, bad_end
);
9132 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9133 if (element_type_tree
== error_mark_node
)
9134 return error_mark_node
;
9135 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9137 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9138 fold_convert_loc(loc
, sizetype
, start_tree
),
9141 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9142 if (value_pointer
== error_mark_node
)
9143 return error_mark_node
;
9145 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9146 TREE_TYPE(value_pointer
),
9147 value_pointer
, offset
);
9149 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9150 end_tree
, start_tree
);
9152 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9153 capacity_tree
, start_tree
);
9155 tree struct_tree
= this->type()->get_tree(gogo
);
9156 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9158 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9160 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9161 tree field
= TYPE_FIELDS(struct_tree
);
9162 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9164 elt
->value
= value_pointer
;
9166 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9167 field
= DECL_CHAIN(field
);
9168 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9170 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9172 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9173 field
= DECL_CHAIN(field
);
9174 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9176 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9178 tree constructor
= build_constructor(struct_tree
, init
);
9180 if (TREE_CONSTANT(value_pointer
)
9181 && TREE_CONSTANT(result_length_tree
)
9182 && TREE_CONSTANT(result_capacity_tree
))
9183 TREE_CONSTANT(constructor
) = 1;
9185 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9186 build3(COND_EXPR
, void_type_node
,
9187 bad_index
, crash
, NULL_TREE
),
9191 // Make an array index expression. END may be NULL.
9194 Expression::make_array_index(Expression
* array
, Expression
* start
,
9195 Expression
* end
, source_location location
)
9197 // Taking a slice of a composite literal requires moving the literal
9199 if (end
!= NULL
&& array
->is_composite_literal())
9201 array
= Expression::make_heap_composite(array
, location
);
9202 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9204 return new Array_index_expression(array
, start
, end
, location
);
9207 // A string index. This is used for both indexing and slicing.
9209 class String_index_expression
: public Expression
9212 String_index_expression(Expression
* string
, Expression
* start
,
9213 Expression
* end
, source_location location
)
9214 : Expression(EXPRESSION_STRING_INDEX
, location
),
9215 string_(string
), start_(start
), end_(end
)
9220 do_traverse(Traverse
*);
9226 do_determine_type(const Type_context
*);
9229 do_check_types(Gogo
*);
9234 return Expression::make_string_index(this->string_
->copy(),
9235 this->start_
->copy(),
9238 : this->end_
->copy()),
9243 do_get_tree(Translate_context
*);
9246 // The string we are getting a value from.
9247 Expression
* string_
;
9248 // The start or only index.
9250 // The end index of a slice. This may be NULL for a single index,
9251 // or it may be a nil expression for the length of the string.
9255 // String index traversal.
9258 String_index_expression::do_traverse(Traverse
* traverse
)
9260 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9261 return TRAVERSE_EXIT
;
9262 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9263 return TRAVERSE_EXIT
;
9264 if (this->end_
!= NULL
)
9266 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9267 return TRAVERSE_EXIT
;
9269 return TRAVERSE_CONTINUE
;
9272 // Return the type of a string index.
9275 String_index_expression::do_type()
9277 if (this->end_
== NULL
)
9278 return Type::lookup_integer_type("uint8");
9280 return Type::make_string_type();
9283 // Determine the type of a string index.
9286 String_index_expression::do_determine_type(const Type_context
*)
9288 this->string_
->determine_type_no_context();
9289 Type_context
subcontext(NULL
, true);
9290 this->start_
->determine_type(&subcontext
);
9291 if (this->end_
!= NULL
)
9292 this->end_
->determine_type(&subcontext
);
9295 // Check types of a string index.
9298 String_index_expression::do_check_types(Gogo
*)
9300 if (this->start_
->type()->integer_type() == NULL
)
9301 this->report_error(_("index must be integer"));
9302 if (this->end_
!= NULL
9303 && this->end_
->type()->integer_type() == NULL
9304 && !this->end_
->is_nil_expression())
9305 this->report_error(_("slice end must be integer"));
9308 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9313 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9315 if (mpz_sgn(ival
) < 0
9316 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9318 error_at(this->start_
->location(), "string index out of bounds");
9319 this->set_is_error();
9322 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9324 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9326 if (mpz_sgn(ival
) < 0
9327 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9329 error_at(this->end_
->location(), "string index out of bounds");
9330 this->set_is_error();
9337 // Get a tree for a string index.
9340 String_index_expression::do_get_tree(Translate_context
* context
)
9342 source_location loc
= this->location();
9344 tree string_tree
= this->string_
->get_tree(context
);
9345 if (string_tree
== error_mark_node
)
9346 return error_mark_node
;
9348 if (this->string_
->type()->points_to() != NULL
)
9349 string_tree
= build_fold_indirect_ref(string_tree
);
9350 if (!DECL_P(string_tree
))
9351 string_tree
= save_expr(string_tree
);
9352 tree string_type
= TREE_TYPE(string_tree
);
9354 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9355 length_tree
= save_expr(length_tree
);
9356 tree length_type
= TREE_TYPE(length_tree
);
9358 tree bad_index
= boolean_false_node
;
9360 tree start_tree
= this->start_
->get_tree(context
);
9361 if (start_tree
== error_mark_node
)
9362 return error_mark_node
;
9363 if (!DECL_P(start_tree
))
9364 start_tree
= save_expr(start_tree
);
9365 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9366 start_tree
= convert_to_integer(length_type
, start_tree
);
9368 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9371 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9373 int code
= (this->end_
== NULL
9374 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9375 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9376 tree crash
= Gogo::runtime_error(code
, loc
);
9378 if (this->end_
== NULL
)
9380 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9382 fold_build2_loc(loc
, GE_EXPR
,
9384 start_tree
, length_tree
));
9386 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9387 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9389 fold_convert_loc(loc
, sizetype
, start_tree
));
9390 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9392 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9393 build3(COND_EXPR
, void_type_node
,
9394 bad_index
, crash
, NULL_TREE
),
9400 if (this->end_
->is_nil_expression())
9401 end_tree
= build_int_cst(length_type
, -1);
9404 end_tree
= this->end_
->get_tree(context
);
9405 if (end_tree
== error_mark_node
)
9406 return error_mark_node
;
9407 if (!DECL_P(end_tree
))
9408 end_tree
= save_expr(end_tree
);
9409 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9410 end_tree
= convert_to_integer(length_type
, end_tree
);
9412 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9415 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9418 static tree strslice_fndecl
;
9419 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9421 "__go_string_slice",
9430 if (ret
== error_mark_node
)
9431 return error_mark_node
;
9432 // This will panic if the bounds are out of range for the
9434 TREE_NOTHROW(strslice_fndecl
) = 0;
9436 if (bad_index
== boolean_false_node
)
9439 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9440 build3(COND_EXPR
, void_type_node
,
9441 bad_index
, crash
, NULL_TREE
),
9446 // Make a string index expression. END may be NULL.
9449 Expression::make_string_index(Expression
* string
, Expression
* start
,
9450 Expression
* end
, source_location location
)
9452 return new String_index_expression(string
, start
, end
, location
);
9457 // Get the type of the map.
9460 Map_index_expression::get_map_type() const
9462 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9463 gcc_assert(mt
!= NULL
);
9467 // Map index traversal.
9470 Map_index_expression::do_traverse(Traverse
* traverse
)
9472 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9473 return TRAVERSE_EXIT
;
9474 return Expression::traverse(&this->index_
, traverse
);
9477 // Return the type of a map index.
9480 Map_index_expression::do_type()
9482 Type
* type
= this->get_map_type()->val_type();
9483 // If this map index is in a tuple assignment, we actually return a
9484 // pointer to the value type. Tuple_map_assignment_statement is
9485 // responsible for handling this correctly. We need to get the type
9486 // right in case this gets assigned to a temporary variable.
9487 if (this->is_in_tuple_assignment_
)
9488 type
= Type::make_pointer_type(type
);
9492 // Fix the type of a map index.
9495 Map_index_expression::do_determine_type(const Type_context
*)
9497 this->map_
->determine_type_no_context();
9498 Type_context
subcontext(this->get_map_type()->key_type(), false);
9499 this->index_
->determine_type(&subcontext
);
9502 // Check types of a map index.
9505 Map_index_expression::do_check_types(Gogo
*)
9508 if (!Type::are_assignable(this->get_map_type()->key_type(),
9509 this->index_
->type(), &reason
))
9512 this->report_error(_("incompatible type for map index"));
9515 error_at(this->location(), "incompatible type for map index (%s)",
9517 this->set_is_error();
9522 // Get a tree for a map index.
9525 Map_index_expression::do_get_tree(Translate_context
* context
)
9527 Map_type
* type
= this->get_map_type();
9529 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9530 if (valptr
== error_mark_node
)
9531 return error_mark_node
;
9532 valptr
= save_expr(valptr
);
9534 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
9536 if (this->is_lvalue_
)
9537 return build_fold_indirect_ref(valptr
);
9538 else if (this->is_in_tuple_assignment_
)
9540 // Tuple_map_assignment_statement is responsible for using this
9546 return fold_build3(COND_EXPR
, val_type_tree
,
9547 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
9548 fold_convert(TREE_TYPE(valptr
),
9549 null_pointer_node
)),
9550 type
->val_type()->get_init_tree(context
->gogo(),
9552 build_fold_indirect_ref(valptr
));
9556 // Get a tree for the map index. This returns a tree which evaluates
9557 // to a pointer to a value. The pointer will be NULL if the key is
9561 Map_index_expression::get_value_pointer(Translate_context
* context
,
9564 Map_type
* type
= this->get_map_type();
9566 tree map_tree
= this->map_
->get_tree(context
);
9567 tree index_tree
= this->index_
->get_tree(context
);
9568 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
9569 this->index_
->type(),
9572 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
9573 return error_mark_node
;
9575 if (this->map_
->type()->points_to() != NULL
)
9576 map_tree
= build_fold_indirect_ref(map_tree
);
9578 // We need to pass in a pointer to the key, so stuff it into a
9580 tree tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
9581 DECL_IGNORED_P(tmp
) = 0;
9582 DECL_INITIAL(tmp
) = index_tree
;
9583 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
9584 tree tmpref
= fold_convert(const_ptr_type_node
, build_fold_addr_expr(tmp
));
9585 TREE_ADDRESSABLE(tmp
) = 1;
9587 static tree map_index_fndecl
;
9588 tree call
= Gogo::call_builtin(&map_index_fndecl
,
9592 const_ptr_type_node
,
9593 TREE_TYPE(map_tree
),
9595 const_ptr_type_node
,
9600 : boolean_false_node
));
9601 if (call
== error_mark_node
)
9602 return error_mark_node
;
9603 // This can panic on a map of interface type if the interface holds
9604 // an uncomparable or unhashable type.
9605 TREE_NOTHROW(map_index_fndecl
) = 0;
9607 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
9608 if (val_type_tree
== error_mark_node
)
9609 return error_mark_node
;
9610 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
9612 return build2(COMPOUND_EXPR
, ptr_val_type_tree
,
9614 fold_convert(ptr_val_type_tree
, call
));
9617 // Make a map index expression.
9619 Map_index_expression
*
9620 Expression::make_map_index(Expression
* map
, Expression
* index
,
9621 source_location location
)
9623 return new Map_index_expression(map
, index
, location
);
9626 // Class Field_reference_expression.
9628 // Return the type of a field reference.
9631 Field_reference_expression::do_type()
9633 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9634 gcc_assert(struct_type
!= NULL
);
9635 return struct_type
->field(this->field_index_
)->type();
9638 // Check the types for a field reference.
9641 Field_reference_expression::do_check_types(Gogo
*)
9643 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9644 gcc_assert(struct_type
!= NULL
);
9645 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
9648 // Get a tree for a field reference.
9651 Field_reference_expression::do_get_tree(Translate_context
* context
)
9653 tree struct_tree
= this->expr_
->get_tree(context
);
9654 if (struct_tree
== error_mark_node
9655 || TREE_TYPE(struct_tree
) == error_mark_node
)
9656 return error_mark_node
;
9657 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
9658 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
9659 if (field
== NULL_TREE
)
9661 // This can happen for a type which refers to itself indirectly
9662 // and then turns out to be erroneous.
9663 gcc_assert(saw_errors());
9664 return error_mark_node
;
9666 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
9668 field
= DECL_CHAIN(field
);
9669 gcc_assert(field
!= NULL_TREE
);
9671 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
9675 // Make a reference to a qualified identifier in an expression.
9677 Field_reference_expression
*
9678 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
9679 source_location location
)
9681 return new Field_reference_expression(expr
, field_index
, location
);
9684 // Class Interface_field_reference_expression.
9686 // Return a tree for the pointer to the function to call.
9689 Interface_field_reference_expression::get_function_tree(Translate_context
*,
9692 if (this->expr_
->type()->points_to() != NULL
)
9693 expr
= build_fold_indirect_ref(expr
);
9695 tree expr_type
= TREE_TYPE(expr
);
9696 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9698 tree field
= TYPE_FIELDS(expr_type
);
9699 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
9701 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9702 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
9704 table
= build_fold_indirect_ref(table
);
9705 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
9707 std::string name
= Gogo::unpack_hidden_name(this->name_
);
9708 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
9710 field
= DECL_CHAIN(field
))
9712 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
9715 gcc_assert(field
!= NULL_TREE
);
9717 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
9720 // Return a tree for the first argument to pass to the interface
9724 Interface_field_reference_expression::get_underlying_object_tree(
9728 if (this->expr_
->type()->points_to() != NULL
)
9729 expr
= build_fold_indirect_ref(expr
);
9731 tree expr_type
= TREE_TYPE(expr
);
9732 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9734 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
9735 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
9737 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9743 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
9745 return Expression::traverse(&this->expr_
, traverse
);
9748 // Return the type of an interface field reference.
9751 Interface_field_reference_expression::do_type()
9753 Type
* expr_type
= this->expr_
->type();
9755 Type
* points_to
= expr_type
->points_to();
9756 if (points_to
!= NULL
)
9757 expr_type
= points_to
;
9759 Interface_type
* interface_type
= expr_type
->interface_type();
9760 if (interface_type
== NULL
)
9761 return Type::make_error_type();
9763 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
9765 return Type::make_error_type();
9767 return method
->type();
9773 Interface_field_reference_expression::do_determine_type(const Type_context
*)
9775 this->expr_
->determine_type_no_context();
9778 // Check the types for an interface field reference.
9781 Interface_field_reference_expression::do_check_types(Gogo
*)
9783 Type
* type
= this->expr_
->type();
9785 Type
* points_to
= type
->points_to();
9786 if (points_to
!= NULL
)
9789 Interface_type
* interface_type
= type
->interface_type();
9790 if (interface_type
== NULL
)
9791 this->report_error(_("expected interface or pointer to interface"));
9794 const Typed_identifier
* method
=
9795 interface_type
->find_method(this->name_
);
9798 error_at(this->location(), "method %qs not in interface",
9799 Gogo::message_name(this->name_
).c_str());
9800 this->set_is_error();
9805 // Get a tree for a reference to a field in an interface. There is no
9806 // standard tree type representation for this: it's a function
9807 // attached to its first argument, like a Bound_method_expression.
9808 // The only places it may currently be used are in a Call_expression
9809 // or a Go_statement, which will take it apart directly. So this has
9810 // nothing to do at present.
9813 Interface_field_reference_expression::do_get_tree(Translate_context
*)
9818 // Make a reference to a field in an interface.
9821 Expression::make_interface_field_reference(Expression
* expr
,
9822 const std::string
& field
,
9823 source_location location
)
9825 return new Interface_field_reference_expression(expr
, field
, location
);
9828 // A general selector. This is a Parser_expression for LEFT.NAME. It
9829 // is lowered after we know the type of the left hand side.
9831 class Selector_expression
: public Parser_expression
9834 Selector_expression(Expression
* left
, const std::string
& name
,
9835 source_location location
)
9836 : Parser_expression(EXPRESSION_SELECTOR
, location
),
9837 left_(left
), name_(name
)
9842 do_traverse(Traverse
* traverse
)
9843 { return Expression::traverse(&this->left_
, traverse
); }
9846 do_lower(Gogo
*, Named_object
*, int);
9851 return new Selector_expression(this->left_
->copy(), this->name_
,
9857 lower_method_expression(Gogo
*);
9859 // The expression on the left hand side.
9861 // The name on the right hand side.
9865 // Lower a selector expression once we know the real type of the left
9869 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
9871 Expression
* left
= this->left_
;
9872 if (left
->is_type_expression())
9873 return this->lower_method_expression(gogo
);
9874 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
9878 // Lower a method expression T.M or (*T).M. We turn this into a
9879 // function literal.
9882 Selector_expression::lower_method_expression(Gogo
* gogo
)
9884 source_location location
= this->location();
9885 Type
* type
= this->left_
->type();
9886 const std::string
& name(this->name_
);
9889 if (type
->points_to() == NULL
)
9894 type
= type
->points_to();
9896 Named_type
* nt
= type
->named_type();
9900 ("method expression requires named type or "
9901 "pointer to named type"));
9902 return Expression::make_error(location
);
9906 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
9910 error_at(location
, "type %<%s%> has no method %<%s%>",
9911 nt
->message_name().c_str(),
9912 Gogo::message_name(name
).c_str());
9914 error_at(location
, "method %<%s%> is ambiguous in type %<%s%>",
9915 Gogo::message_name(name
).c_str(),
9916 nt
->message_name().c_str());
9917 return Expression::make_error(location
);
9920 if (!is_pointer
&& !method
->is_value_method())
9922 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
9923 nt
->message_name().c_str(),
9924 Gogo::message_name(name
).c_str());
9925 return Expression::make_error(location
);
9928 // Build a new function type in which the receiver becomes the first
9930 Function_type
* method_type
= method
->type();
9931 gcc_assert(method_type
->is_method());
9933 const char* const receiver_name
= "$this";
9934 Typed_identifier_list
* parameters
= new Typed_identifier_list();
9935 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
9938 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
9939 if (method_parameters
!= NULL
)
9941 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
9942 p
!= method_parameters
->end();
9944 parameters
->push_back(*p
);
9947 const Typed_identifier_list
* method_results
= method_type
->results();
9948 Typed_identifier_list
* results
;
9949 if (method_results
== NULL
)
9953 results
= new Typed_identifier_list();
9954 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
9955 p
!= method_results
->end();
9957 results
->push_back(*p
);
9960 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
9962 if (method_type
->is_varargs())
9963 fntype
->set_is_varargs();
9965 // We generate methods which always takes a pointer to the receiver
9966 // as their first argument. If this is for a pointer type, we can
9967 // simply reuse the existing function. We use an internal hack to
9968 // get the right type.
9972 Named_object
* mno
= (method
->needs_stub_method()
9973 ? method
->stub_object()
9974 : method
->named_object());
9975 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
9976 f
= Expression::make_cast(fntype
, f
, location
);
9977 Type_conversion_expression
* tce
=
9978 static_cast<Type_conversion_expression
*>(f
);
9979 tce
->set_may_convert_function_types();
9983 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
9986 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
9987 gcc_assert(vno
!= NULL
);
9988 Expression
* ve
= Expression::make_var_reference(vno
, location
);
9989 Expression
* bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
9990 gcc_assert(bm
!= NULL
&& !bm
->is_error_expression());
9992 Expression_list
* args
;
9993 if (method_parameters
== NULL
)
9997 args
= new Expression_list();
9998 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
9999 p
!= method_parameters
->end();
10002 vno
= gogo
->lookup(p
->name(), NULL
);
10003 gcc_assert(vno
!= NULL
);
10004 args
->push_back(Expression::make_var_reference(vno
, location
));
10008 Call_expression
* call
= Expression::make_call(bm
, args
,
10009 method_type
->is_varargs(),
10012 size_t count
= call
->result_count();
10015 s
= Statement::make_statement(call
);
10018 Expression_list
* retvals
= new Expression_list();
10020 retvals
->push_back(call
);
10023 for (size_t i
= 0; i
< count
; ++i
)
10024 retvals
->push_back(Expression::make_call_result(call
, i
));
10026 s
= Statement::make_return_statement(no
->func_value()->type()->results(),
10027 retvals
, location
);
10029 gogo
->add_statement(s
);
10031 gogo
->finish_function(location
);
10033 return Expression::make_func_reference(no
, NULL
, location
);
10036 // Make a selector expression.
10039 Expression::make_selector(Expression
* left
, const std::string
& name
,
10040 source_location location
)
10042 return new Selector_expression(left
, name
, location
);
10045 // Implement the builtin function new.
10047 class Allocation_expression
: public Expression
10050 Allocation_expression(Type
* type
, source_location location
)
10051 : Expression(EXPRESSION_ALLOCATION
, location
),
10057 do_traverse(Traverse
* traverse
)
10058 { return Type::traverse(this->type_
, traverse
); }
10062 { return Type::make_pointer_type(this->type_
); }
10065 do_determine_type(const Type_context
*)
10069 do_check_types(Gogo
*);
10073 { return new Allocation_expression(this->type_
, this->location()); }
10076 do_get_tree(Translate_context
*);
10079 // The type we are allocating.
10083 // Check the type of an allocation expression.
10086 Allocation_expression::do_check_types(Gogo
*)
10088 if (this->type_
->function_type() != NULL
)
10089 this->report_error(_("invalid new of function type"));
10092 // Return a tree for an allocation expression.
10095 Allocation_expression::do_get_tree(Translate_context
* context
)
10097 tree type_tree
= this->type_
->get_tree(context
->gogo());
10098 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10099 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10101 return fold_convert(build_pointer_type(type_tree
), space
);
10104 // Make an allocation expression.
10107 Expression::make_allocation(Type
* type
, source_location location
)
10109 return new Allocation_expression(type
, location
);
10112 // Implement the builtin function make.
10114 class Make_expression
: public Expression
10117 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10118 : Expression(EXPRESSION_MAKE
, location
),
10119 type_(type
), args_(args
)
10124 do_traverse(Traverse
* traverse
);
10128 { return this->type_
; }
10131 do_determine_type(const Type_context
*);
10134 do_check_types(Gogo
*);
10139 return new Make_expression(this->type_
, this->args_
->copy(),
10144 do_get_tree(Translate_context
*);
10147 // The type we are making.
10149 // The arguments to pass to the make routine.
10150 Expression_list
* args_
;
10156 Make_expression::do_traverse(Traverse
* traverse
)
10158 if (this->args_
!= NULL
10159 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10160 return TRAVERSE_EXIT
;
10161 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10162 return TRAVERSE_EXIT
;
10163 return TRAVERSE_CONTINUE
;
10166 // Set types of arguments.
10169 Make_expression::do_determine_type(const Type_context
*)
10171 if (this->args_
!= NULL
)
10173 Type_context
context(Type::lookup_integer_type("int"), false);
10174 for (Expression_list::const_iterator pe
= this->args_
->begin();
10175 pe
!= this->args_
->end();
10177 (*pe
)->determine_type(&context
);
10181 // Check types for a make expression.
10184 Make_expression::do_check_types(Gogo
*)
10186 if (this->type_
->channel_type() == NULL
10187 && this->type_
->map_type() == NULL
10188 && (this->type_
->array_type() == NULL
10189 || this->type_
->array_type()->length() != NULL
))
10190 this->report_error(_("invalid type for make function"));
10191 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10192 this->set_is_error();
10195 // Return a tree for a make expression.
10198 Make_expression::do_get_tree(Translate_context
* context
)
10200 return this->type_
->make_expression_tree(context
, this->args_
,
10204 // Make a make expression.
10207 Expression::make_make(Type
* type
, Expression_list
* args
,
10208 source_location location
)
10210 return new Make_expression(type
, args
, location
);
10213 // Construct a struct.
10215 class Struct_construction_expression
: public Expression
10218 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10219 source_location location
)
10220 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10221 type_(type
), vals_(vals
)
10224 // Return whether this is a constant initializer.
10226 is_constant_struct() const;
10230 do_traverse(Traverse
* traverse
);
10234 { return this->type_
; }
10237 do_determine_type(const Type_context
*);
10240 do_check_types(Gogo
*);
10245 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10250 do_is_addressable() const
10254 do_get_tree(Translate_context
*);
10257 do_export(Export
*) const;
10260 // The type of the struct to construct.
10262 // The list of values, in order of the fields in the struct. A NULL
10263 // entry means that the field should be zero-initialized.
10264 Expression_list
* vals_
;
10270 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10272 if (this->vals_
!= NULL
10273 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10274 return TRAVERSE_EXIT
;
10275 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10276 return TRAVERSE_EXIT
;
10277 return TRAVERSE_CONTINUE
;
10280 // Return whether this is a constant initializer.
10283 Struct_construction_expression::is_constant_struct() const
10285 if (this->vals_
== NULL
)
10287 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10288 pv
!= this->vals_
->end();
10292 && !(*pv
)->is_constant()
10293 && (!(*pv
)->is_composite_literal()
10294 || (*pv
)->is_nonconstant_composite_literal()))
10298 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10299 for (Struct_field_list::const_iterator pf
= fields
->begin();
10300 pf
!= fields
->end();
10303 // There are no constant constructors for interfaces.
10304 if (pf
->type()->interface_type() != NULL
)
10311 // Final type determination.
10314 Struct_construction_expression::do_determine_type(const Type_context
*)
10316 if (this->vals_
== NULL
)
10318 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10319 Expression_list::const_iterator pv
= this->vals_
->begin();
10320 for (Struct_field_list::const_iterator pf
= fields
->begin();
10321 pf
!= fields
->end();
10324 if (pv
== this->vals_
->end())
10328 Type_context
subcontext(pf
->type(), false);
10329 (*pv
)->determine_type(&subcontext
);
10337 Struct_construction_expression::do_check_types(Gogo
*)
10339 if (this->vals_
== NULL
)
10342 Struct_type
* st
= this->type_
->struct_type();
10343 if (this->vals_
->size() > st
->field_count())
10345 this->report_error(_("too many expressions for struct"));
10349 const Struct_field_list
* fields
= st
->fields();
10350 Expression_list::const_iterator pv
= this->vals_
->begin();
10352 for (Struct_field_list::const_iterator pf
= fields
->begin();
10353 pf
!= fields
->end();
10356 if (pv
== this->vals_
->end())
10358 this->report_error(_("too few expressions for struct"));
10365 std::string reason
;
10366 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10368 if (reason
.empty())
10369 error_at((*pv
)->location(),
10370 "incompatible type for field %d in struct construction",
10373 error_at((*pv
)->location(),
10374 ("incompatible type for field %d in "
10375 "struct construction (%s)"),
10376 i
+ 1, reason
.c_str());
10377 this->set_is_error();
10380 gcc_assert(pv
== this->vals_
->end());
10383 // Return a tree for constructing a struct.
10386 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10388 Gogo
* gogo
= context
->gogo();
10390 if (this->vals_
== NULL
)
10391 return this->type_
->get_init_tree(gogo
, false);
10393 tree type_tree
= this->type_
->get_tree(gogo
);
10394 if (type_tree
== error_mark_node
)
10395 return error_mark_node
;
10396 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10398 bool is_constant
= true;
10399 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10400 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10402 Struct_field_list::const_iterator pf
= fields
->begin();
10403 Expression_list::const_iterator pv
= this->vals_
->begin();
10404 for (tree field
= TYPE_FIELDS(type_tree
);
10405 field
!= NULL_TREE
;
10406 field
= DECL_CHAIN(field
), ++pf
)
10408 gcc_assert(pf
!= fields
->end());
10411 if (pv
== this->vals_
->end())
10412 val
= pf
->type()->get_init_tree(gogo
, false);
10413 else if (*pv
== NULL
)
10415 val
= pf
->type()->get_init_tree(gogo
, false);
10420 val
= Expression::convert_for_assignment(context
, pf
->type(),
10422 (*pv
)->get_tree(context
),
10427 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10428 return error_mark_node
;
10430 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10431 elt
->index
= field
;
10433 if (!TREE_CONSTANT(val
))
10434 is_constant
= false;
10436 gcc_assert(pf
== fields
->end());
10438 tree ret
= build_constructor(type_tree
, elts
);
10440 TREE_CONSTANT(ret
) = 1;
10444 // Export a struct construction.
10447 Struct_construction_expression::do_export(Export
* exp
) const
10449 exp
->write_c_string("convert(");
10450 exp
->write_type(this->type_
);
10451 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10452 pv
!= this->vals_
->end();
10455 exp
->write_c_string(", ");
10457 (*pv
)->export_expression(exp
);
10459 exp
->write_c_string(")");
10462 // Make a struct composite literal. This used by the thunk code.
10465 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
10466 source_location location
)
10468 gcc_assert(type
->struct_type() != NULL
);
10469 return new Struct_construction_expression(type
, vals
, location
);
10472 // Construct an array. This class is not used directly; instead we
10473 // use the child classes, Fixed_array_construction_expression and
10474 // Open_array_construction_expression.
10476 class Array_construction_expression
: public Expression
10479 Array_construction_expression(Expression_classification classification
,
10480 Type
* type
, Expression_list
* vals
,
10481 source_location location
)
10482 : Expression(classification
, location
),
10483 type_(type
), vals_(vals
)
10487 // Return whether this is a constant initializer.
10489 is_constant_array() const;
10491 // Return the number of elements.
10493 element_count() const
10494 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
10498 do_traverse(Traverse
* traverse
);
10502 { return this->type_
; }
10505 do_determine_type(const Type_context
*);
10508 do_check_types(Gogo
*);
10511 do_is_addressable() const
10515 do_export(Export
*) const;
10517 // The list of values.
10520 { return this->vals_
; }
10522 // Get a constructor tree for the array values.
10524 get_constructor_tree(Translate_context
* context
, tree type_tree
);
10527 // The type of the array to construct.
10529 // The list of values.
10530 Expression_list
* vals_
;
10536 Array_construction_expression::do_traverse(Traverse
* traverse
)
10538 if (this->vals_
!= NULL
10539 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10540 return TRAVERSE_EXIT
;
10541 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10542 return TRAVERSE_EXIT
;
10543 return TRAVERSE_CONTINUE
;
10546 // Return whether this is a constant initializer.
10549 Array_construction_expression::is_constant_array() const
10551 if (this->vals_
== NULL
)
10554 // There are no constant constructors for interfaces.
10555 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
10558 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10559 pv
!= this->vals_
->end();
10563 && !(*pv
)->is_constant()
10564 && (!(*pv
)->is_composite_literal()
10565 || (*pv
)->is_nonconstant_composite_literal()))
10571 // Final type determination.
10574 Array_construction_expression::do_determine_type(const Type_context
*)
10576 if (this->vals_
== NULL
)
10578 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
10579 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10580 pv
!= this->vals_
->end();
10584 (*pv
)->determine_type(&subcontext
);
10591 Array_construction_expression::do_check_types(Gogo
*)
10593 if (this->vals_
== NULL
)
10596 Array_type
* at
= this->type_
->array_type();
10598 Type
* element_type
= at
->element_type();
10599 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10600 pv
!= this->vals_
->end();
10604 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
10606 error_at((*pv
)->location(),
10607 "incompatible type for element %d in composite literal",
10609 this->set_is_error();
10613 Expression
* length
= at
->length();
10614 if (length
!= NULL
)
10619 if (at
->length()->integer_constant_value(true, val
, &type
))
10621 if (this->vals_
->size() > mpz_get_ui(val
))
10622 this->report_error(_("too many elements in composite literal"));
10628 // Get a constructor tree for the array values.
10631 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
10634 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10635 (this->vals_
== NULL
10637 : this->vals_
->size()));
10638 Type
* element_type
= this->type_
->array_type()->element_type();
10639 bool is_constant
= true;
10640 if (this->vals_
!= NULL
)
10643 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10644 pv
!= this->vals_
->end();
10647 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
10648 elt
->index
= size_int(i
);
10650 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10653 tree value_tree
= (*pv
)->get_tree(context
);
10654 elt
->value
= Expression::convert_for_assignment(context
,
10660 if (elt
->value
== error_mark_node
)
10661 return error_mark_node
;
10662 if (!TREE_CONSTANT(elt
->value
))
10663 is_constant
= false;
10667 tree ret
= build_constructor(type_tree
, values
);
10669 TREE_CONSTANT(ret
) = 1;
10673 // Export an array construction.
10676 Array_construction_expression::do_export(Export
* exp
) const
10678 exp
->write_c_string("convert(");
10679 exp
->write_type(this->type_
);
10680 if (this->vals_
!= NULL
)
10682 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10683 pv
!= this->vals_
->end();
10686 exp
->write_c_string(", ");
10688 (*pv
)->export_expression(exp
);
10691 exp
->write_c_string(")");
10694 // Construct a fixed array.
10696 class Fixed_array_construction_expression
:
10697 public Array_construction_expression
10700 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
10701 source_location location
)
10702 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
10703 type
, vals
, location
)
10705 gcc_assert(type
->array_type() != NULL
10706 && type
->array_type()->length() != NULL
);
10713 return new Fixed_array_construction_expression(this->type(),
10714 (this->vals() == NULL
10716 : this->vals()->copy()),
10721 do_get_tree(Translate_context
*);
10724 // Return a tree for constructing a fixed array.
10727 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
10729 return this->get_constructor_tree(context
,
10730 this->type()->get_tree(context
->gogo()));
10733 // Construct an open array.
10735 class Open_array_construction_expression
: public Array_construction_expression
10738 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
10739 source_location location
)
10740 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
10741 type
, vals
, location
)
10743 gcc_assert(type
->array_type() != NULL
10744 && type
->array_type()->length() == NULL
);
10748 // Note that taking the address of an open array literal is invalid.
10753 return new Open_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 an open array.
10767 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
10769 Type
* element_type
= this->type()->array_type()->element_type();
10770 tree element_type_tree
= element_type
->get_tree(context
->gogo());
10771 if (element_type_tree
== error_mark_node
)
10772 return error_mark_node
;
10776 if (this->vals() == NULL
|| this->vals()->empty())
10778 // We need to create a unique value.
10779 tree max
= size_int(0);
10780 tree constructor_type
= build_array_type(element_type_tree
,
10781 build_index_type(max
));
10782 if (constructor_type
== error_mark_node
)
10783 return error_mark_node
;
10784 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
10785 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
10786 elt
->index
= size_int(0);
10787 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10788 values
= build_constructor(constructor_type
, vec
);
10789 if (TREE_CONSTANT(elt
->value
))
10790 TREE_CONSTANT(values
) = 1;
10791 length_tree
= size_int(0);
10795 tree max
= size_int(this->vals()->size() - 1);
10796 tree constructor_type
= build_array_type(element_type_tree
,
10797 build_index_type(max
));
10798 if (constructor_type
== error_mark_node
)
10799 return error_mark_node
;
10800 values
= this->get_constructor_tree(context
, constructor_type
);
10801 length_tree
= size_int(this->vals()->size());
10804 if (values
== error_mark_node
)
10805 return error_mark_node
;
10807 bool is_constant_initializer
= TREE_CONSTANT(values
);
10808 bool is_in_function
= context
->function() != NULL
;
10810 if (is_constant_initializer
)
10812 tree tmp
= build_decl(this->location(), VAR_DECL
,
10813 create_tmp_var_name("C"), TREE_TYPE(values
));
10814 DECL_EXTERNAL(tmp
) = 0;
10815 TREE_PUBLIC(tmp
) = 0;
10816 TREE_STATIC(tmp
) = 1;
10817 DECL_ARTIFICIAL(tmp
) = 1;
10818 if (is_in_function
)
10820 // If this is not a function, we will only initialize the
10821 // value once, so we can use this directly rather than
10822 // copying it. In that case we can't make it read-only,
10823 // because the program is permitted to change it.
10824 TREE_READONLY(tmp
) = 1;
10825 TREE_CONSTANT(tmp
) = 1;
10827 DECL_INITIAL(tmp
) = values
;
10828 rest_of_decl_compilation(tmp
, 1, 0);
10834 if (!is_in_function
&& is_constant_initializer
)
10836 // Outside of a function, we know the initializer will only run
10838 space
= build_fold_addr_expr(values
);
10843 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
10844 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
10846 space
= save_expr(space
);
10848 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
10849 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
10850 TREE_THIS_NOTRAP(ref
) = 1;
10851 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
10854 // Build a constructor for the open array.
10856 tree type_tree
= this->type()->get_tree(context
->gogo());
10857 if (type_tree
== error_mark_node
)
10858 return error_mark_node
;
10859 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10861 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
10863 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
10864 tree field
= TYPE_FIELDS(type_tree
);
10865 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
10866 elt
->index
= field
;
10867 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
10869 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
10870 field
= DECL_CHAIN(field
);
10871 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
10872 elt
->index
= field
;
10873 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
10875 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
10876 field
= DECL_CHAIN(field
);
10877 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
10878 elt
->index
= field
;
10879 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
10881 tree constructor
= build_constructor(type_tree
, init
);
10882 if (constructor
== error_mark_node
)
10883 return error_mark_node
;
10884 if (!is_in_function
&& is_constant_initializer
)
10885 TREE_CONSTANT(constructor
) = 1;
10887 if (set
== NULL_TREE
)
10888 return constructor
;
10890 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
10893 // Make a slice composite literal. This is used by the type
10894 // descriptor code.
10897 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
10898 source_location location
)
10900 gcc_assert(type
->is_open_array_type());
10901 return new Open_array_construction_expression(type
, vals
, location
);
10904 // Construct a map.
10906 class Map_construction_expression
: public Expression
10909 Map_construction_expression(Type
* type
, Expression_list
* vals
,
10910 source_location location
)
10911 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
10912 type_(type
), vals_(vals
)
10913 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
10917 do_traverse(Traverse
* traverse
);
10921 { return this->type_
; }
10924 do_determine_type(const Type_context
*);
10927 do_check_types(Gogo
*);
10932 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
10937 do_get_tree(Translate_context
*);
10940 do_export(Export
*) const;
10943 // The type of the map to construct.
10945 // The list of values.
10946 Expression_list
* vals_
;
10952 Map_construction_expression::do_traverse(Traverse
* traverse
)
10954 if (this->vals_
!= NULL
10955 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10956 return TRAVERSE_EXIT
;
10957 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10958 return TRAVERSE_EXIT
;
10959 return TRAVERSE_CONTINUE
;
10962 // Final type determination.
10965 Map_construction_expression::do_determine_type(const Type_context
*)
10967 if (this->vals_
== NULL
)
10970 Map_type
* mt
= this->type_
->map_type();
10971 Type_context
key_context(mt
->key_type(), false);
10972 Type_context
val_context(mt
->val_type(), false);
10973 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10974 pv
!= this->vals_
->end();
10977 (*pv
)->determine_type(&key_context
);
10979 (*pv
)->determine_type(&val_context
);
10986 Map_construction_expression::do_check_types(Gogo
*)
10988 if (this->vals_
== NULL
)
10991 Map_type
* mt
= this->type_
->map_type();
10993 Type
* key_type
= mt
->key_type();
10994 Type
* val_type
= mt
->val_type();
10995 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10996 pv
!= this->vals_
->end();
10999 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
11001 error_at((*pv
)->location(),
11002 "incompatible type for element %d key in map construction",
11004 this->set_is_error();
11007 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
11009 error_at((*pv
)->location(),
11010 ("incompatible type for element %d value "
11011 "in map construction"),
11013 this->set_is_error();
11018 // Return a tree for constructing a map.
11021 Map_construction_expression::do_get_tree(Translate_context
* context
)
11023 Gogo
* gogo
= context
->gogo();
11024 source_location loc
= this->location();
11026 Map_type
* mt
= this->type_
->map_type();
11028 // Build a struct to hold the key and value.
11029 tree struct_type
= make_node(RECORD_TYPE
);
11031 Type
* key_type
= mt
->key_type();
11032 tree id
= get_identifier("__key");
11033 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type
->get_tree(gogo
));
11034 DECL_CONTEXT(key_field
) = struct_type
;
11035 TYPE_FIELDS(struct_type
) = key_field
;
11037 Type
* val_type
= mt
->val_type();
11038 id
= get_identifier("__val");
11039 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type
->get_tree(gogo
));
11040 DECL_CONTEXT(val_field
) = struct_type
;
11041 DECL_CHAIN(key_field
) = val_field
;
11043 layout_type(struct_type
);
11045 bool is_constant
= true;
11050 if (this->vals_
== NULL
|| this->vals_
->empty())
11052 valaddr
= null_pointer_node
;
11053 make_tmp
= NULL_TREE
;
11057 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11058 this->vals_
->size() / 2);
11060 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11061 pv
!= this->vals_
->end();
11064 bool one_is_constant
= true;
11066 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11068 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11069 elt
->index
= key_field
;
11070 tree val_tree
= (*pv
)->get_tree(context
);
11071 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11074 if (elt
->value
== error_mark_node
)
11075 return error_mark_node
;
11076 if (!TREE_CONSTANT(elt
->value
))
11077 one_is_constant
= false;
11081 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11082 elt
->index
= val_field
;
11083 val_tree
= (*pv
)->get_tree(context
);
11084 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11087 if (elt
->value
== error_mark_node
)
11088 return error_mark_node
;
11089 if (!TREE_CONSTANT(elt
->value
))
11090 one_is_constant
= false;
11092 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11093 elt
->index
= size_int(i
);
11094 elt
->value
= build_constructor(struct_type
, one
);
11095 if (one_is_constant
)
11096 TREE_CONSTANT(elt
->value
) = 1;
11098 is_constant
= false;
11101 tree index_type
= build_index_type(size_int(i
- 1));
11102 tree array_type
= build_array_type(struct_type
, index_type
);
11103 tree init
= build_constructor(array_type
, values
);
11105 TREE_CONSTANT(init
) = 1;
11107 if (current_function_decl
!= NULL
)
11109 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11110 DECL_INITIAL(tmp
) = init
;
11111 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11112 TREE_ADDRESSABLE(tmp
) = 1;
11116 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11117 DECL_EXTERNAL(tmp
) = 0;
11118 TREE_PUBLIC(tmp
) = 0;
11119 TREE_STATIC(tmp
) = 1;
11120 DECL_ARTIFICIAL(tmp
) = 1;
11121 if (!TREE_CONSTANT(init
))
11122 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11126 TREE_READONLY(tmp
) = 1;
11127 TREE_CONSTANT(tmp
) = 1;
11128 DECL_INITIAL(tmp
) = init
;
11129 make_tmp
= NULL_TREE
;
11131 rest_of_decl_compilation(tmp
, 1, 0);
11134 valaddr
= build_fold_addr_expr(tmp
);
11137 tree descriptor
= gogo
->map_descriptor(mt
);
11139 tree type_tree
= this->type_
->get_tree(gogo
);
11141 static tree construct_map_fndecl
;
11142 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11144 "__go_construct_map",
11147 TREE_TYPE(descriptor
),
11152 TYPE_SIZE_UNIT(struct_type
),
11154 byte_position(val_field
),
11156 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11157 const_ptr_type_node
,
11158 fold_convert(const_ptr_type_node
, valaddr
));
11159 if (call
== error_mark_node
)
11160 return error_mark_node
;
11163 if (make_tmp
== NULL
)
11166 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11170 // Export an array construction.
11173 Map_construction_expression::do_export(Export
* exp
) const
11175 exp
->write_c_string("convert(");
11176 exp
->write_type(this->type_
);
11177 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11178 pv
!= this->vals_
->end();
11181 exp
->write_c_string(", ");
11182 (*pv
)->export_expression(exp
);
11184 exp
->write_c_string(")");
11187 // A general composite literal. This is lowered to a type specific
11190 class Composite_literal_expression
: public Parser_expression
11193 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11194 Expression_list
* vals
, source_location location
)
11195 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11196 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11201 do_traverse(Traverse
* traverse
);
11204 do_lower(Gogo
*, Named_object
*, int);
11209 return new Composite_literal_expression(this->type_
, this->depth_
,
11211 (this->vals_
== NULL
11213 : this->vals_
->copy()),
11219 lower_struct(Type
*);
11222 lower_array(Type
*);
11225 make_array(Type
*, Expression_list
*);
11228 lower_map(Gogo
*, Named_object
*, Type
*);
11230 // The type of the composite literal.
11232 // The depth within a list of composite literals within a composite
11233 // literal, when the type is omitted.
11235 // The values to put in the composite literal.
11236 Expression_list
* vals_
;
11237 // If this is true, then VALS_ is a list of pairs: a key and a
11238 // value. In an array initializer, a missing key will be NULL.
11245 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11247 if (this->vals_
!= NULL
11248 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11249 return TRAVERSE_EXIT
;
11250 return Type::traverse(this->type_
, traverse
);
11253 // Lower a generic composite literal into a specific version based on
11257 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
11259 Type
* type
= this->type_
;
11261 for (int depth
= this->depth_
; depth
> 0; --depth
)
11263 if (type
->array_type() != NULL
)
11264 type
= type
->array_type()->element_type();
11265 else if (type
->map_type() != NULL
)
11266 type
= type
->map_type()->val_type();
11269 if (!type
->is_error_type())
11270 error_at(this->location(),
11271 ("may only omit types within composite literals "
11272 "of slice, array, or map type"));
11273 return Expression::make_error(this->location());
11277 if (type
->is_error_type())
11278 return Expression::make_error(this->location());
11279 else if (type
->struct_type() != NULL
)
11280 return this->lower_struct(type
);
11281 else if (type
->array_type() != NULL
)
11282 return this->lower_array(type
);
11283 else if (type
->map_type() != NULL
)
11284 return this->lower_map(gogo
, function
, type
);
11287 error_at(this->location(),
11288 ("expected struct, slice, array, or map type "
11289 "for composite literal"));
11290 return Expression::make_error(this->location());
11294 // Lower a struct composite literal.
11297 Composite_literal_expression::lower_struct(Type
* type
)
11299 source_location location
= this->location();
11300 Struct_type
* st
= type
->struct_type();
11301 if (this->vals_
== NULL
|| !this->has_keys_
)
11302 return new Struct_construction_expression(type
, this->vals_
, location
);
11304 size_t field_count
= st
->field_count();
11305 std::vector
<Expression
*> vals(field_count
);
11306 Expression_list::const_iterator p
= this->vals_
->begin();
11307 while (p
!= this->vals_
->end())
11309 Expression
* name_expr
= *p
;
11312 gcc_assert(p
!= this->vals_
->end());
11313 Expression
* val
= *p
;
11317 if (name_expr
== NULL
)
11319 error_at(val
->location(), "mixture of field and value initializers");
11320 return Expression::make_error(location
);
11323 bool bad_key
= false;
11325 switch (name_expr
->classification())
11327 case EXPRESSION_UNKNOWN_REFERENCE
:
11328 name
= name_expr
->unknown_expression()->name();
11331 case EXPRESSION_CONST_REFERENCE
:
11332 name
= static_cast<Const_expression
*>(name_expr
)->name();
11335 case EXPRESSION_TYPE
:
11337 Type
* t
= name_expr
->type();
11338 Named_type
* nt
= t
->named_type();
11346 case EXPRESSION_VAR_REFERENCE
:
11347 name
= name_expr
->var_expression()->name();
11350 case EXPRESSION_FUNC_REFERENCE
:
11351 name
= name_expr
->func_expression()->name();
11354 case EXPRESSION_UNARY
:
11355 // If there is a local variable around with the same name as
11356 // the field, and this occurs in the closure, then the
11357 // parser may turn the field reference into an indirection
11358 // through the closure. FIXME: This is a mess.
11361 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11362 if (ue
->op() == OPERATOR_MULT
)
11364 Field_reference_expression
* fre
=
11365 ue
->operand()->field_reference_expression();
11369 fre
->expr()->type()->deref()->struct_type();
11372 const Struct_field
* sf
= st
->field(fre
->field_index());
11373 name
= sf
->field_name();
11375 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11376 size_t buflen
= strlen(buf
);
11377 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11380 name
= name
.substr(0, name
.length() - buflen
);
11395 error_at(name_expr
->location(), "expected struct field name");
11396 return Expression::make_error(location
);
11399 unsigned int index
;
11400 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11403 error_at(name_expr
->location(), "unknown field %qs in %qs",
11404 Gogo::message_name(name
).c_str(),
11405 (type
->named_type() != NULL
11406 ? type
->named_type()->message_name().c_str()
11407 : "unnamed struct"));
11408 return Expression::make_error(location
);
11410 if (vals
[index
] != NULL
)
11412 error_at(name_expr
->location(),
11413 "duplicate value for field %qs in %qs",
11414 Gogo::message_name(name
).c_str(),
11415 (type
->named_type() != NULL
11416 ? type
->named_type()->message_name().c_str()
11417 : "unnamed struct"));
11418 return Expression::make_error(location
);
11424 Expression_list
* list
= new Expression_list
;
11425 list
->reserve(field_count
);
11426 for (size_t i
= 0; i
< field_count
; ++i
)
11427 list
->push_back(vals
[i
]);
11429 return new Struct_construction_expression(type
, list
, location
);
11432 // Lower an array composite literal.
11435 Composite_literal_expression::lower_array(Type
* type
)
11437 source_location location
= this->location();
11438 if (this->vals_
== NULL
|| !this->has_keys_
)
11439 return this->make_array(type
, this->vals_
);
11441 std::vector
<Expression
*> vals
;
11442 vals
.reserve(this->vals_
->size());
11443 unsigned long index
= 0;
11444 Expression_list::const_iterator p
= this->vals_
->begin();
11445 while (p
!= this->vals_
->end())
11447 Expression
* index_expr
= *p
;
11450 gcc_assert(p
!= this->vals_
->end());
11451 Expression
* val
= *p
;
11455 if (index_expr
!= NULL
)
11460 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
11463 error_at(index_expr
->location(),
11464 "index expression is not integer constant");
11465 return Expression::make_error(location
);
11467 if (mpz_sgn(ival
) < 0)
11470 error_at(index_expr
->location(), "index expression is negative");
11471 return Expression::make_error(location
);
11473 index
= mpz_get_ui(ival
);
11474 if (mpz_cmp_ui(ival
, index
) != 0)
11477 error_at(index_expr
->location(), "index value overflow");
11478 return Expression::make_error(location
);
11483 if (index
== vals
.size())
11484 vals
.push_back(val
);
11487 if (index
> vals
.size())
11489 vals
.reserve(index
+ 32);
11490 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
11492 if (vals
[index
] != NULL
)
11494 error_at((index_expr
!= NULL
11495 ? index_expr
->location()
11496 : val
->location()),
11497 "duplicate value for index %lu",
11499 return Expression::make_error(location
);
11507 size_t size
= vals
.size();
11508 Expression_list
* list
= new Expression_list
;
11509 list
->reserve(size
);
11510 for (size_t i
= 0; i
< size
; ++i
)
11511 list
->push_back(vals
[i
]);
11513 return this->make_array(type
, list
);
11516 // Actually build the array composite literal. This handles
11520 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
11522 source_location location
= this->location();
11523 Array_type
* at
= type
->array_type();
11524 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
11526 size_t size
= vals
== NULL
? 0 : vals
->size();
11528 mpz_init_set_ui(vlen
, size
);
11529 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
11531 at
= Type::make_array_type(at
->element_type(), elen
);
11534 if (at
->length() != NULL
)
11535 return new Fixed_array_construction_expression(type
, vals
, location
);
11537 return new Open_array_construction_expression(type
, vals
, location
);
11540 // Lower a map composite literal.
11543 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
11546 source_location location
= this->location();
11547 if (this->vals_
!= NULL
)
11549 if (!this->has_keys_
)
11551 error_at(location
, "map composite literal must have keys");
11552 return Expression::make_error(location
);
11555 for (Expression_list::iterator p
= this->vals_
->begin();
11556 p
!= this->vals_
->end();
11562 error_at((*p
)->location(),
11563 "map composite literal must have keys for every value");
11564 return Expression::make_error(location
);
11566 // Make sure we have lowered the key; it may not have been
11567 // lowered in order to handle keys for struct composite
11568 // literals. Lower it now to get the right error message.
11569 if ((*p
)->unknown_expression() != NULL
)
11571 (*p
)->unknown_expression()->clear_is_composite_literal_key();
11572 gogo
->lower_expression(function
, &*p
);
11573 gcc_assert((*p
)->is_error_expression());
11574 return Expression::make_error(location
);
11579 return new Map_construction_expression(type
, this->vals_
, location
);
11582 // Make a composite literal expression.
11585 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
11586 Expression_list
* vals
,
11587 source_location location
)
11589 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
11593 // Return whether this expression is a composite literal.
11596 Expression::is_composite_literal() const
11598 switch (this->classification_
)
11600 case EXPRESSION_COMPOSITE_LITERAL
:
11601 case EXPRESSION_STRUCT_CONSTRUCTION
:
11602 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11603 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11604 case EXPRESSION_MAP_CONSTRUCTION
:
11611 // Return whether this expression is a composite literal which is not
11615 Expression::is_nonconstant_composite_literal() const
11617 switch (this->classification_
)
11619 case EXPRESSION_STRUCT_CONSTRUCTION
:
11621 const Struct_construction_expression
*psce
=
11622 static_cast<const Struct_construction_expression
*>(this);
11623 return !psce
->is_constant_struct();
11625 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11627 const Fixed_array_construction_expression
*pace
=
11628 static_cast<const Fixed_array_construction_expression
*>(this);
11629 return !pace
->is_constant_array();
11631 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11633 const Open_array_construction_expression
*pace
=
11634 static_cast<const Open_array_construction_expression
*>(this);
11635 return !pace
->is_constant_array();
11637 case EXPRESSION_MAP_CONSTRUCTION
:
11644 // Return true if this is a reference to a local variable.
11647 Expression::is_local_variable() const
11649 const Var_expression
* ve
= this->var_expression();
11652 const Named_object
* no
= ve
->named_object();
11653 return (no
->is_result_variable()
11654 || (no
->is_variable() && !no
->var_value()->is_global()));
11657 // Class Type_guard_expression.
11662 Type_guard_expression::do_traverse(Traverse
* traverse
)
11664 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
11665 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11666 return TRAVERSE_EXIT
;
11667 return TRAVERSE_CONTINUE
;
11670 // Check types of a type guard expression. The expression must have
11671 // an interface type, but the actual type conversion is checked at run
11675 Type_guard_expression::do_check_types(Gogo
*)
11677 // 6g permits using a type guard with unsafe.pointer; we are
11679 Type
* expr_type
= this->expr_
->type();
11680 if (expr_type
->is_unsafe_pointer_type())
11682 if (this->type_
->points_to() == NULL
11683 && (this->type_
->integer_type() == NULL
11684 || (this->type_
->forwarded()
11685 != Type::lookup_integer_type("uintptr"))))
11686 this->report_error(_("invalid unsafe.Pointer conversion"));
11688 else if (this->type_
->is_unsafe_pointer_type())
11690 if (expr_type
->points_to() == NULL
11691 && (expr_type
->integer_type() == NULL
11692 || (expr_type
->forwarded()
11693 != Type::lookup_integer_type("uintptr"))))
11694 this->report_error(_("invalid unsafe.Pointer conversion"));
11696 else if (expr_type
->interface_type() == NULL
)
11698 if (!expr_type
->is_error_type() && !this->type_
->is_error_type())
11699 this->report_error(_("type assertion only valid for interface types"));
11700 this->set_is_error();
11702 else if (this->type_
->interface_type() == NULL
)
11704 std::string reason
;
11705 if (!expr_type
->interface_type()->implements_interface(this->type_
,
11708 if (!this->type_
->is_error_type())
11710 if (reason
.empty())
11711 this->report_error(_("impossible type assertion: "
11712 "type does not implement interface"));
11714 error_at(this->location(),
11715 ("impossible type assertion: "
11716 "type does not implement interface (%s)"),
11719 this->set_is_error();
11724 // Return a tree for a type guard expression.
11727 Type_guard_expression::do_get_tree(Translate_context
* context
)
11729 Gogo
* gogo
= context
->gogo();
11730 tree expr_tree
= this->expr_
->get_tree(context
);
11731 if (expr_tree
== error_mark_node
)
11732 return error_mark_node
;
11733 Type
* expr_type
= this->expr_
->type();
11734 if ((this->type_
->is_unsafe_pointer_type()
11735 && (expr_type
->points_to() != NULL
11736 || expr_type
->integer_type() != NULL
))
11737 || (expr_type
->is_unsafe_pointer_type()
11738 && this->type_
->points_to() != NULL
))
11739 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
11740 else if (expr_type
->is_unsafe_pointer_type()
11741 && this->type_
->integer_type() != NULL
)
11742 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
11743 else if (this->type_
->interface_type() != NULL
)
11744 return Expression::convert_interface_to_interface(context
, this->type_
,
11745 this->expr_
->type(),
11749 return Expression::convert_for_assignment(context
, this->type_
,
11750 this->expr_
->type(), expr_tree
,
11754 // Make a type guard expression.
11757 Expression::make_type_guard(Expression
* expr
, Type
* type
,
11758 source_location location
)
11760 return new Type_guard_expression(expr
, type
, location
);
11763 // Class Heap_composite_expression.
11765 // When you take the address of a composite literal, it is allocated
11766 // on the heap. This class implements that.
11768 class Heap_composite_expression
: public Expression
11771 Heap_composite_expression(Expression
* expr
, source_location location
)
11772 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
11778 do_traverse(Traverse
* traverse
)
11779 { return Expression::traverse(&this->expr_
, traverse
); }
11783 { return Type::make_pointer_type(this->expr_
->type()); }
11786 do_determine_type(const Type_context
*)
11787 { this->expr_
->determine_type_no_context(); }
11792 return Expression::make_heap_composite(this->expr_
->copy(),
11797 do_get_tree(Translate_context
*);
11799 // We only export global objects, and the parser does not generate
11800 // this in global scope.
11802 do_export(Export
*) const
11803 { gcc_unreachable(); }
11806 // The composite literal which is being put on the heap.
11810 // Return a tree which allocates a composite literal on the heap.
11813 Heap_composite_expression::do_get_tree(Translate_context
* context
)
11815 tree expr_tree
= this->expr_
->get_tree(context
);
11816 if (expr_tree
== error_mark_node
)
11817 return error_mark_node
;
11818 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
11819 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
11820 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
11821 expr_size
, this->location());
11822 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
11823 space
= save_expr(space
);
11824 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
11825 TREE_THIS_NOTRAP(ref
) = 1;
11826 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
11827 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
11829 SET_EXPR_LOCATION(ret
, this->location());
11833 // Allocate a composite literal on the heap.
11836 Expression::make_heap_composite(Expression
* expr
, source_location location
)
11838 return new Heap_composite_expression(expr
, location
);
11841 // Class Receive_expression.
11843 // Return the type of a receive expression.
11846 Receive_expression::do_type()
11848 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
11849 if (channel_type
== NULL
)
11850 return Type::make_error_type();
11851 return channel_type
->element_type();
11854 // Check types for a receive expression.
11857 Receive_expression::do_check_types(Gogo
*)
11859 Type
* type
= this->channel_
->type();
11860 if (type
->is_error_type())
11862 this->set_is_error();
11865 if (type
->channel_type() == NULL
)
11867 this->report_error(_("expected channel"));
11870 if (!type
->channel_type()->may_receive())
11872 this->report_error(_("invalid receive on send-only channel"));
11877 // Get a tree for a receive expression.
11880 Receive_expression::do_get_tree(Translate_context
* context
)
11882 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
11883 gcc_assert(channel_type
!= NULL
);
11884 Type
* element_type
= channel_type
->element_type();
11885 tree element_type_tree
= element_type
->get_tree(context
->gogo());
11887 tree channel
= this->channel_
->get_tree(context
);
11888 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
11889 return error_mark_node
;
11891 return Gogo::receive_from_channel(element_type_tree
, channel
,
11892 this->for_select_
, this->location());
11895 // Make a receive expression.
11897 Receive_expression
*
11898 Expression::make_receive(Expression
* channel
, source_location location
)
11900 return new Receive_expression(channel
, location
);
11903 // Class Send_expression.
11908 Send_expression::do_traverse(Traverse
* traverse
)
11910 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
11911 return TRAVERSE_EXIT
;
11912 return Expression::traverse(&this->val_
, traverse
);
11918 Send_expression::do_type()
11920 return Type::lookup_bool_type();
11926 Send_expression::do_determine_type(const Type_context
*)
11928 this->channel_
->determine_type_no_context();
11930 Type
* type
= this->channel_
->type();
11931 Type_context subcontext
;
11932 if (type
->channel_type() != NULL
)
11933 subcontext
.type
= type
->channel_type()->element_type();
11934 this->val_
->determine_type(&subcontext
);
11940 Send_expression::do_check_types(Gogo
*)
11942 Type
* type
= this->channel_
->type();
11943 if (type
->is_error_type())
11945 this->set_is_error();
11948 Channel_type
* channel_type
= type
->channel_type();
11949 if (channel_type
== NULL
)
11951 error_at(this->location(), "left operand of %<<-%> must be channel");
11952 this->set_is_error();
11955 Type
* element_type
= channel_type
->element_type();
11956 if (element_type
!= NULL
11957 && !Type::are_assignable(element_type
, this->val_
->type(), NULL
))
11959 this->report_error(_("incompatible types in send"));
11962 if (!channel_type
->may_send())
11964 this->report_error(_("invalid send on receive-only channel"));
11969 // Get a tree for a send expression.
11972 Send_expression::do_get_tree(Translate_context
* context
)
11974 tree channel
= this->channel_
->get_tree(context
);
11975 tree val
= this->val_
->get_tree(context
);
11976 if (channel
== error_mark_node
|| val
== error_mark_node
)
11977 return error_mark_node
;
11978 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
11979 val
= Expression::convert_for_assignment(context
,
11980 channel_type
->element_type(),
11981 this->val_
->type(),
11984 return Gogo::send_on_channel(channel
, val
, this->is_value_discarded_
,
11985 this->for_select_
, this->location());
11988 // Make a send expression
11991 Expression::make_send(Expression
* channel
, Expression
* val
,
11992 source_location location
)
11994 return new Send_expression(channel
, val
, location
);
11997 // An expression which evaluates to a pointer to the type descriptor
12000 class Type_descriptor_expression
: public Expression
12003 Type_descriptor_expression(Type
* type
, source_location location
)
12004 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
12011 { return Type::make_type_descriptor_ptr_type(); }
12014 do_determine_type(const Type_context
*)
12022 do_get_tree(Translate_context
* context
)
12023 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
12026 // The type for which this is the descriptor.
12030 // Make a type descriptor expression.
12033 Expression::make_type_descriptor(Type
* type
, source_location location
)
12035 return new Type_descriptor_expression(type
, location
);
12038 // An expression which evaluates to some characteristic of a type.
12039 // This is only used to initialize fields of a type descriptor. Using
12040 // a new expression class is slightly inefficient but gives us a good
12041 // separation between the frontend and the middle-end with regard to
12042 // how types are laid out.
12044 class Type_info_expression
: public Expression
12047 Type_info_expression(Type
* type
, Type_info type_info
)
12048 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
12049 type_(type
), type_info_(type_info
)
12057 do_determine_type(const Type_context
*)
12065 do_get_tree(Translate_context
* context
);
12068 // The type for which we are getting information.
12070 // What information we want.
12071 Type_info type_info_
;
12074 // The type is chosen to match what the type descriptor struct
12078 Type_info_expression::do_type()
12080 switch (this->type_info_
)
12082 case TYPE_INFO_SIZE
:
12083 return Type::lookup_integer_type("uintptr");
12084 case TYPE_INFO_ALIGNMENT
:
12085 case TYPE_INFO_FIELD_ALIGNMENT
:
12086 return Type::lookup_integer_type("uint8");
12092 // Return type information in GENERIC.
12095 Type_info_expression::do_get_tree(Translate_context
* context
)
12097 tree type_tree
= this->type_
->get_tree(context
->gogo());
12098 if (type_tree
== error_mark_node
)
12099 return error_mark_node
;
12101 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12102 gcc_assert(val_type_tree
!= error_mark_node
);
12104 if (this->type_info_
== TYPE_INFO_SIZE
)
12105 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12106 TYPE_SIZE_UNIT(type_tree
));
12110 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12111 val
= go_type_alignment(type_tree
);
12113 val
= go_field_alignment(type_tree
);
12114 return build_int_cstu(val_type_tree
, val
);
12118 // Make a type info expression.
12121 Expression::make_type_info(Type
* type
, Type_info type_info
)
12123 return new Type_info_expression(type
, type_info
);
12126 // An expression which evaluates to the offset of a field within a
12127 // struct. This, like Type_info_expression, q.v., is only used to
12128 // initialize fields of a type descriptor.
12130 class Struct_field_offset_expression
: public Expression
12133 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12134 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12135 type_(type
), field_(field
)
12141 { return Type::lookup_integer_type("uintptr"); }
12144 do_determine_type(const Type_context
*)
12152 do_get_tree(Translate_context
* context
);
12155 // The type of the struct.
12156 Struct_type
* type_
;
12158 const Struct_field
* field_
;
12161 // Return a struct field offset in GENERIC.
12164 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12166 tree type_tree
= this->type_
->get_tree(context
->gogo());
12167 if (type_tree
== error_mark_node
)
12168 return error_mark_node
;
12170 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12171 gcc_assert(val_type_tree
!= error_mark_node
);
12173 const Struct_field_list
* fields
= this->type_
->fields();
12174 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12175 Struct_field_list::const_iterator p
;
12176 for (p
= fields
->begin();
12177 p
!= fields
->end();
12178 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12180 gcc_assert(struct_field_tree
!= NULL_TREE
);
12181 if (&*p
== this->field_
)
12184 gcc_assert(&*p
== this->field_
);
12186 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12187 byte_position(struct_field_tree
));
12190 // Make an expression for a struct field offset.
12193 Expression::make_struct_field_offset(Struct_type
* type
,
12194 const Struct_field
* field
)
12196 return new Struct_field_offset_expression(type
, field
);
12199 // An expression which evaluates to the address of an unnamed label.
12201 class Label_addr_expression
: public Expression
12204 Label_addr_expression(Label
* label
, source_location location
)
12205 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12212 { return Type::make_pointer_type(Type::make_void_type()); }
12215 do_determine_type(const Type_context
*)
12220 { return new Label_addr_expression(this->label_
, this->location()); }
12223 do_get_tree(Translate_context
*)
12224 { return this->label_
->get_addr(this->location()); }
12227 // The label whose address we are taking.
12231 // Make an expression for the address of an unnamed label.
12234 Expression::make_label_addr(Label
* label
, source_location location
)
12236 return new Label_addr_expression(label
, location
);
12239 // Import an expression. This comes at the end in order to see the
12240 // various class definitions.
12243 Expression::import_expression(Import
* imp
)
12245 int c
= imp
->peek_char();
12246 if (imp
->match_c_string("- ")
12247 || imp
->match_c_string("! ")
12248 || imp
->match_c_string("^ "))
12249 return Unary_expression::do_import(imp
);
12251 return Binary_expression::do_import(imp
);
12252 else if (imp
->match_c_string("true")
12253 || imp
->match_c_string("false"))
12254 return Boolean_expression::do_import(imp
);
12256 return String_expression::do_import(imp
);
12257 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12259 // This handles integers, floats and complex constants.
12260 return Integer_expression::do_import(imp
);
12262 else if (imp
->match_c_string("nil"))
12263 return Nil_expression::do_import(imp
);
12264 else if (imp
->match_c_string("convert"))
12265 return Type_conversion_expression::do_import(imp
);
12268 error_at(imp
->location(), "import error: expected expression");
12269 return Expression::make_error(imp
->location());
12273 // Class Expression_list.
12275 // Traverse the list.
12278 Expression_list::traverse(Traverse
* traverse
)
12280 for (Expression_list::iterator p
= this->begin();
12286 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12287 return TRAVERSE_EXIT
;
12290 return TRAVERSE_CONTINUE
;
12296 Expression_list::copy()
12298 Expression_list
* ret
= new Expression_list();
12299 for (Expression_list::iterator p
= this->begin();
12304 ret
->push_back(NULL
);
12306 ret
->push_back((*p
)->copy());
12311 // Return whether an expression list has an error expression.
12314 Expression_list::contains_error() const
12316 for (Expression_list::const_iterator p
= this->begin();
12319 if (*p
!= NULL
&& (*p
)->is_error_expression())