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
,
365 if (first_field_value
== error_mark_node
)
366 return error_mark_node
;
368 // Start building a constructor for the value we will return.
370 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
372 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
373 tree field
= TYPE_FIELDS(lhs_type_tree
);
374 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
375 (lhs_is_empty
? "__type_descriptor" : "__methods")) == 0);
377 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), first_field_value
);
379 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
380 field
= DECL_CHAIN(field
);
381 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
384 if (rhs_type
->points_to() != NULL
)
386 // We are assigning a pointer to the interface; the interface
387 // holds the pointer itself.
388 elt
->value
= rhs_tree
;
389 return build_constructor(lhs_type_tree
, init
);
392 // We are assigning a non-pointer value to the interface; the
393 // interface gets a copy of the value in the heap.
395 tree object_size
= TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree
));
397 tree space
= gogo
->allocate_memory(rhs_type
, object_size
, location
);
398 space
= fold_convert_loc(location
, build_pointer_type(TREE_TYPE(rhs_tree
)),
400 space
= save_expr(space
);
402 tree ref
= build_fold_indirect_ref_loc(location
, space
);
403 TREE_THIS_NOTRAP(ref
) = 1;
404 tree set
= fold_build2_loc(location
, MODIFY_EXPR
, void_type_node
,
407 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), space
);
409 return build2(COMPOUND_EXPR
, lhs_type_tree
, set
,
410 build_constructor(lhs_type_tree
, init
));
413 // Return a tree for the type descriptor of RHS_TREE, which has
414 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
418 Expression::get_interface_type_descriptor(Translate_context
*,
419 Type
* rhs_type
, tree rhs_tree
,
420 source_location location
)
422 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
423 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
424 tree rhs_field
= TYPE_FIELDS(rhs_type_tree
);
425 tree v
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
427 if (rhs_type
->interface_type()->is_empty())
429 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)),
430 "__type_descriptor") == 0);
434 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__methods")
436 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v
)));
438 tree v1
= build_fold_indirect_ref_loc(location
, v
);
439 gcc_assert(TREE_CODE(TREE_TYPE(v1
)) == RECORD_TYPE
);
440 tree f
= TYPE_FIELDS(TREE_TYPE(v1
));
441 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f
)), "__type_descriptor")
443 v1
= build3(COMPONENT_REF
, TREE_TYPE(f
), v1
, f
, NULL_TREE
);
445 tree eq
= fold_build2_loc(location
, EQ_EXPR
, boolean_type_node
, v
,
446 fold_convert_loc(location
, TREE_TYPE(v
),
448 tree n
= fold_convert_loc(location
, TREE_TYPE(v1
), null_pointer_node
);
449 return fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(v1
),
453 // Return a tree for the conversion of an interface type to an
457 Expression::convert_interface_to_interface(Translate_context
* context
,
458 Type
*lhs_type
, Type
*rhs_type
,
459 tree rhs_tree
, bool for_type_guard
,
460 source_location location
)
462 Gogo
* gogo
= context
->gogo();
463 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
464 bool lhs_is_empty
= lhs_interface_type
->is_empty();
466 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
467 if (lhs_type_tree
== error_mark_node
)
468 return error_mark_node
;
470 // In the general case this requires runtime examination of the type
471 // method table to match it up with the interface methods.
473 // FIXME: If all of the methods in the right hand side interface
474 // also appear in the left hand side interface, then we don't need
475 // to do a runtime check, although we still need to build a new
478 // Get the type descriptor for the right hand side. This will be
479 // NULL for a nil interface.
481 if (!DECL_P(rhs_tree
))
482 rhs_tree
= save_expr(rhs_tree
);
484 tree rhs_type_descriptor
=
485 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
488 // The result is going to be a two element constructor.
490 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
492 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
493 tree field
= TYPE_FIELDS(lhs_type_tree
);
498 // A type assertion fails when converting a nil interface.
499 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
500 static tree assert_interface_decl
;
501 tree call
= Gogo::call_builtin(&assert_interface_decl
,
503 "__go_assert_interface",
506 TREE_TYPE(lhs_type_descriptor
),
508 TREE_TYPE(rhs_type_descriptor
),
509 rhs_type_descriptor
);
510 if (call
== error_mark_node
)
511 return error_mark_node
;
512 // This will panic if the interface conversion fails.
513 TREE_NOTHROW(assert_interface_decl
) = 0;
514 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
516 else if (lhs_is_empty
)
518 // A convertion to an empty interface always succeeds, and the
519 // first field is just the type descriptor of the object.
520 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
521 "__type_descriptor") == 0);
522 gcc_assert(TREE_TYPE(field
) == TREE_TYPE(rhs_type_descriptor
));
523 elt
->value
= rhs_type_descriptor
;
527 // A conversion to a non-empty interface may fail, but unlike a
528 // type assertion converting nil will always succeed.
529 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods")
531 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
532 static tree convert_interface_decl
;
533 tree call
= Gogo::call_builtin(&convert_interface_decl
,
535 "__go_convert_interface",
538 TREE_TYPE(lhs_type_descriptor
),
540 TREE_TYPE(rhs_type_descriptor
),
541 rhs_type_descriptor
);
542 if (call
== error_mark_node
)
543 return error_mark_node
;
544 // This will panic if the interface conversion fails.
545 TREE_NOTHROW(convert_interface_decl
) = 0;
546 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
549 // The second field is simply the object pointer.
551 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
552 field
= DECL_CHAIN(field
);
553 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
556 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
557 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
558 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
559 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
560 elt
->value
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
563 return build_constructor(lhs_type_tree
, init
);
566 // Return a tree for the conversion of an interface type to a
567 // non-interface type.
570 Expression::convert_interface_to_type(Translate_context
* context
,
571 Type
*lhs_type
, Type
* rhs_type
,
572 tree rhs_tree
, source_location location
)
574 Gogo
* gogo
= context
->gogo();
575 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
577 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
578 if (lhs_type_tree
== error_mark_node
)
579 return error_mark_node
;
581 // Call a function to check that the type is valid. The function
582 // will panic with an appropriate runtime type error if the type is
585 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
587 if (!DECL_P(rhs_tree
))
588 rhs_tree
= save_expr(rhs_tree
);
590 tree rhs_type_descriptor
=
591 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
594 tree rhs_inter_descriptor
= rhs_type
->type_descriptor_pointer(gogo
);
596 static tree check_interface_type_decl
;
597 tree call
= Gogo::call_builtin(&check_interface_type_decl
,
599 "__go_check_interface_type",
602 TREE_TYPE(lhs_type_descriptor
),
604 TREE_TYPE(rhs_type_descriptor
),
606 TREE_TYPE(rhs_inter_descriptor
),
607 rhs_inter_descriptor
);
608 if (call
== error_mark_node
)
609 return error_mark_node
;
610 // This call will panic if the conversion is invalid.
611 TREE_NOTHROW(check_interface_type_decl
) = 0;
613 // If the call succeeds, pull out the value.
614 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
615 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
616 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
617 tree val
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
620 // If the value is a pointer, then it is the value we want.
621 // Otherwise it points to the value.
622 if (lhs_type
->points_to() == NULL
)
624 val
= fold_convert_loc(location
, build_pointer_type(lhs_type_tree
), val
);
625 val
= build_fold_indirect_ref_loc(location
, val
);
628 return build2(COMPOUND_EXPR
, lhs_type_tree
, call
,
629 fold_convert_loc(location
, lhs_type_tree
, val
));
632 // Convert an expression to a tree. This is implemented by the child
633 // class. Not that it is not in general safe to call this multiple
634 // times for a single expression, but that we don't catch such errors.
637 Expression::get_tree(Translate_context
* context
)
639 // The child may have marked this expression as having an error.
640 if (this->classification_
== EXPRESSION_ERROR
)
641 return error_mark_node
;
643 return this->do_get_tree(context
);
646 // Return a tree for VAL in TYPE.
649 Expression::integer_constant_tree(mpz_t val
, tree type
)
651 if (type
== error_mark_node
)
652 return error_mark_node
;
653 else if (TREE_CODE(type
) == INTEGER_TYPE
)
654 return double_int_to_tree(type
,
655 mpz_get_double_int(type
, val
, true));
656 else if (TREE_CODE(type
) == REAL_TYPE
)
659 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
660 tree ret
= Expression::float_constant_tree(fval
, type
);
664 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
667 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
668 tree real
= Expression::float_constant_tree(fval
, TREE_TYPE(type
));
670 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
672 return build_complex(type
, real
, imag
);
678 // Return a tree for VAL in TYPE.
681 Expression::float_constant_tree(mpfr_t val
, tree type
)
683 if (type
== error_mark_node
)
684 return error_mark_node
;
685 else if (TREE_CODE(type
) == INTEGER_TYPE
)
689 mpfr_get_z(ival
, val
, GMP_RNDN
);
690 tree ret
= Expression::integer_constant_tree(ival
, type
);
694 else if (TREE_CODE(type
) == REAL_TYPE
)
697 real_from_mpfr(&r1
, val
, type
, GMP_RNDN
);
699 real_convert(&r2
, TYPE_MODE(type
), &r1
);
700 return build_real(type
, r2
);
702 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
705 real_from_mpfr(&r1
, val
, TREE_TYPE(type
), GMP_RNDN
);
707 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
708 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
710 return build_complex(type
, build_real(TREE_TYPE(type
), r2
), imag
);
716 // Return a tree for REAL/IMAG in TYPE.
719 Expression::complex_constant_tree(mpfr_t real
, mpfr_t imag
, tree type
)
721 if (type
== error_mark_node
)
722 return error_mark_node
;
723 else if (TREE_CODE(type
) == INTEGER_TYPE
|| TREE_CODE(type
) == REAL_TYPE
)
724 return Expression::float_constant_tree(real
, type
);
725 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
728 real_from_mpfr(&r1
, real
, TREE_TYPE(type
), GMP_RNDN
);
730 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
733 real_from_mpfr(&r3
, imag
, TREE_TYPE(type
), GMP_RNDN
);
735 real_convert(&r4
, TYPE_MODE(TREE_TYPE(type
)), &r3
);
737 return build_complex(type
, build_real(TREE_TYPE(type
), r2
),
738 build_real(TREE_TYPE(type
), r4
));
744 // Return a tree which evaluates to true if VAL, of arbitrary integer
745 // type, is negative or is more than the maximum value of BOUND_TYPE.
746 // If SOFAR is not NULL, it is or'red into the result. The return
747 // value may be NULL if SOFAR is NULL.
750 Expression::check_bounds(tree val
, tree bound_type
, tree sofar
,
753 tree val_type
= TREE_TYPE(val
);
754 tree ret
= NULL_TREE
;
756 if (!TYPE_UNSIGNED(val_type
))
758 ret
= fold_build2_loc(loc
, LT_EXPR
, boolean_type_node
, val
,
759 build_int_cst(val_type
, 0));
760 if (ret
== boolean_false_node
)
764 if ((TYPE_UNSIGNED(val_type
) && !TYPE_UNSIGNED(bound_type
))
765 || TYPE_SIZE(val_type
) > TYPE_SIZE(bound_type
))
767 tree max
= TYPE_MAX_VALUE(bound_type
);
768 tree big
= fold_build2_loc(loc
, GT_EXPR
, boolean_type_node
, val
,
769 fold_convert_loc(loc
, val_type
, max
));
770 if (big
== boolean_false_node
)
772 else if (ret
== NULL_TREE
)
775 ret
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
779 if (ret
== NULL_TREE
)
781 else if (sofar
== NULL_TREE
)
784 return fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
788 // Error expressions. This are used to avoid cascading errors.
790 class Error_expression
: public Expression
793 Error_expression(source_location location
)
794 : Expression(EXPRESSION_ERROR
, location
)
799 do_is_constant() const
803 do_integer_constant_value(bool, mpz_t val
, Type
**) const
810 do_float_constant_value(mpfr_t val
, Type
**) const
812 mpfr_set_ui(val
, 0, GMP_RNDN
);
817 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const
819 mpfr_set_ui(real
, 0, GMP_RNDN
);
820 mpfr_set_ui(imag
, 0, GMP_RNDN
);
825 do_discarding_value()
830 { return Type::make_error_type(); }
833 do_determine_type(const Type_context
*)
841 do_is_addressable() const
845 do_get_tree(Translate_context
*)
846 { return error_mark_node
; }
850 Expression::make_error(source_location location
)
852 return new Error_expression(location
);
855 // An expression which is really a type. This is used during parsing.
856 // It is an error if these survive after lowering.
859 Type_expression
: public Expression
862 Type_expression(Type
* type
, source_location location
)
863 : Expression(EXPRESSION_TYPE
, location
),
869 do_traverse(Traverse
* traverse
)
870 { return Type::traverse(this->type_
, traverse
); }
874 { return this->type_
; }
877 do_determine_type(const Type_context
*)
881 do_check_types(Gogo
*)
882 { this->report_error(_("invalid use of type")); }
889 do_get_tree(Translate_context
*)
890 { gcc_unreachable(); }
893 // The type which we are representing as an expression.
898 Expression::make_type(Type
* type
, source_location location
)
900 return new Type_expression(type
, location
);
903 // Class Parser_expression.
906 Parser_expression::do_type()
908 // We should never really ask for the type of a Parser_expression.
909 // However, it can happen, at least when we have an invalid const
910 // whose initializer refers to the const itself. In that case we
911 // may ask for the type when lowering the const itself.
912 gcc_assert(saw_errors());
913 return Type::make_error_type();
916 // Class Var_expression.
918 // Lower a variable expression. Here we just make sure that the
919 // initialization expression of the variable has been lowered. This
920 // ensures that we will be able to determine the type of the variable
924 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
926 if (this->variable_
->is_variable())
928 Variable
* var
= this->variable_
->var_value();
929 // This is either a local variable or a global variable. A
930 // reference to a variable which is local to an enclosing
931 // function will be a reference to a field in a closure.
932 if (var
->is_global())
934 var
->lower_init_expression(gogo
, function
);
939 // Return the name of the variable.
942 Var_expression::name() const
944 return this->variable_
->name();
947 // Return the type of a reference to a variable.
950 Var_expression::do_type()
952 if (this->variable_
->is_variable())
953 return this->variable_
->var_value()->type();
954 else if (this->variable_
->is_result_variable())
955 return this->variable_
->result_var_value()->type();
960 // Something takes the address of this variable. This means that we
961 // may want to move the variable onto the heap.
964 Var_expression::do_address_taken(bool escapes
)
968 else if (this->variable_
->is_variable())
969 this->variable_
->var_value()->set_address_taken();
970 else if (this->variable_
->is_result_variable())
971 this->variable_
->result_var_value()->set_address_taken();
976 // Get the tree for a reference to a variable.
979 Var_expression::do_get_tree(Translate_context
* context
)
981 return this->variable_
->get_tree(context
->gogo(), context
->function());
984 // Make a reference to a variable in an expression.
987 Expression::make_var_reference(Named_object
* var
, source_location location
)
990 return Expression::make_sink(location
);
992 // FIXME: Creating a new object for each reference to a variable is
994 return new Var_expression(var
, location
);
997 // Class Temporary_reference_expression.
1002 Temporary_reference_expression::do_type()
1004 return this->statement_
->type();
1007 // Called if something takes the address of this temporary variable.
1008 // We never have to move temporary variables to the heap, but we do
1009 // need to know that they must live in the stack rather than in a
1013 Temporary_reference_expression::do_address_taken(bool)
1015 this->statement_
->set_is_address_taken();
1018 // Get a tree referring to the variable.
1021 Temporary_reference_expression::do_get_tree(Translate_context
*)
1023 return this->statement_
->get_decl();
1026 // Make a reference to a temporary variable.
1029 Expression::make_temporary_reference(Temporary_statement
* statement
,
1030 source_location location
)
1032 return new Temporary_reference_expression(statement
, location
);
1035 // A sink expression--a use of the blank identifier _.
1037 class Sink_expression
: public Expression
1040 Sink_expression(source_location location
)
1041 : Expression(EXPRESSION_SINK
, location
),
1042 type_(NULL
), var_(NULL_TREE
)
1047 do_discarding_value()
1054 do_determine_type(const Type_context
*);
1058 { return new Sink_expression(this->location()); }
1061 do_get_tree(Translate_context
*);
1064 // The type of this sink variable.
1066 // The temporary variable we generate.
1070 // Return the type of a sink expression.
1073 Sink_expression::do_type()
1075 if (this->type_
== NULL
)
1076 return Type::make_sink_type();
1080 // Determine the type of a sink expression.
1083 Sink_expression::do_determine_type(const Type_context
* context
)
1085 if (context
->type
!= NULL
)
1086 this->type_
= context
->type
;
1089 // Return a temporary variable for a sink expression. This will
1090 // presumably be a write-only variable which the middle-end will drop.
1093 Sink_expression::do_get_tree(Translate_context
* context
)
1095 if (this->var_
== NULL_TREE
)
1097 gcc_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
1098 this->var_
= create_tmp_var(this->type_
->get_tree(context
->gogo()),
1104 // Make a sink expression.
1107 Expression::make_sink(source_location location
)
1109 return new Sink_expression(location
);
1112 // Class Func_expression.
1114 // FIXME: Can a function expression appear in a constant expression?
1115 // The value is unchanging. Initializing a constant to the address of
1116 // a function seems like it could work, though there might be little
1119 // Return the name of the function.
1122 Func_expression::name() const
1124 return this->function_
->name();
1130 Func_expression::do_traverse(Traverse
* traverse
)
1132 return (this->closure_
== NULL
1134 : Expression::traverse(&this->closure_
, traverse
));
1137 // Return the type of a function expression.
1140 Func_expression::do_type()
1142 if (this->function_
->is_function())
1143 return this->function_
->func_value()->type();
1144 else if (this->function_
->is_function_declaration())
1145 return this->function_
->func_declaration_value()->type();
1150 // Get the tree for a function expression without evaluating the
1154 Func_expression::get_tree_without_closure(Gogo
* gogo
)
1156 Function_type
* fntype
;
1157 if (this->function_
->is_function())
1158 fntype
= this->function_
->func_value()->type();
1159 else if (this->function_
->is_function_declaration())
1160 fntype
= this->function_
->func_declaration_value()->type();
1164 // Builtin functions are handled specially by Call_expression. We
1165 // can't take their address.
1166 if (fntype
->is_builtin())
1168 error_at(this->location(), "invalid use of special builtin function %qs",
1169 this->function_
->name().c_str());
1170 return error_mark_node
;
1173 Named_object
* no
= this->function_
;
1175 tree id
= no
->get_id(gogo
);
1176 if (id
== error_mark_node
)
1177 return error_mark_node
;
1180 if (no
->is_function())
1181 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
, id
);
1182 else if (no
->is_function_declaration())
1183 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
, id
);
1187 if (fndecl
== error_mark_node
)
1188 return error_mark_node
;
1190 return build_fold_addr_expr_loc(this->location(), fndecl
);
1193 // Get the tree for a function expression. This is used when we take
1194 // the address of a function rather than simply calling it. If the
1195 // function has a closure, we must use a trampoline.
1198 Func_expression::do_get_tree(Translate_context
* context
)
1200 Gogo
* gogo
= context
->gogo();
1202 tree fnaddr
= this->get_tree_without_closure(gogo
);
1203 if (fnaddr
== error_mark_node
)
1204 return error_mark_node
;
1206 gcc_assert(TREE_CODE(fnaddr
) == ADDR_EXPR
1207 && TREE_CODE(TREE_OPERAND(fnaddr
, 0)) == FUNCTION_DECL
);
1208 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr
, 0)) = 1;
1210 // For a normal non-nested function call, that is all we have to do.
1211 if (!this->function_
->is_function()
1212 || this->function_
->func_value()->enclosing() == NULL
)
1214 gcc_assert(this->closure_
== NULL
);
1218 // For a nested function call, we have to always allocate a
1219 // trampoline. If we don't always allocate, then closures will not
1220 // be reliably distinct.
1221 Expression
* closure
= this->closure_
;
1223 if (closure
== NULL
)
1224 closure_tree
= null_pointer_node
;
1227 // Get the value of the closure. This will be a pointer to
1228 // space allocated on the heap.
1229 closure_tree
= closure
->get_tree(context
);
1230 if (closure_tree
== error_mark_node
)
1231 return error_mark_node
;
1232 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree
)));
1235 // Now we need to build some code on the heap. This code will load
1236 // the static chain pointer with the closure and then jump to the
1237 // body of the function. The normal gcc approach is to build the
1238 // code on the stack. Unfortunately we can not do that, as Go
1239 // permits us to return the function pointer.
1241 return gogo
->make_trampoline(fnaddr
, closure_tree
, this->location());
1244 // Make a reference to a function in an expression.
1247 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1248 source_location location
)
1250 return new Func_expression(function
, closure
, location
);
1253 // Class Unknown_expression.
1255 // Return the name of an unknown expression.
1258 Unknown_expression::name() const
1260 return this->named_object_
->name();
1263 // Lower a reference to an unknown name.
1266 Unknown_expression::do_lower(Gogo
*, Named_object
*, int)
1268 source_location location
= this->location();
1269 Named_object
* no
= this->named_object_
;
1271 if (!no
->is_unknown())
1275 real
= no
->unknown_value()->real_named_object();
1278 if (this->is_composite_literal_key_
)
1280 error_at(location
, "reference to undefined name %qs",
1281 this->named_object_
->message_name().c_str());
1282 return Expression::make_error(location
);
1285 switch (real
->classification())
1287 case Named_object::NAMED_OBJECT_CONST
:
1288 return Expression::make_const_reference(real
, location
);
1289 case Named_object::NAMED_OBJECT_TYPE
:
1290 return Expression::make_type(real
->type_value(), location
);
1291 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1292 if (this->is_composite_literal_key_
)
1294 error_at(location
, "reference to undefined type %qs",
1295 real
->message_name().c_str());
1296 return Expression::make_error(location
);
1297 case Named_object::NAMED_OBJECT_VAR
:
1298 return Expression::make_var_reference(real
, location
);
1299 case Named_object::NAMED_OBJECT_FUNC
:
1300 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1301 return Expression::make_func_reference(real
, NULL
, location
);
1302 case Named_object::NAMED_OBJECT_PACKAGE
:
1303 if (this->is_composite_literal_key_
)
1305 error_at(location
, "unexpected reference to package");
1306 return Expression::make_error(location
);
1312 // Make a reference to an unknown name.
1315 Expression::make_unknown_reference(Named_object
* no
, source_location location
)
1317 gcc_assert(no
->resolve()->is_unknown());
1318 return new Unknown_expression(no
, location
);
1321 // A boolean expression.
1323 class Boolean_expression
: public Expression
1326 Boolean_expression(bool val
, source_location location
)
1327 : Expression(EXPRESSION_BOOLEAN
, location
),
1328 val_(val
), type_(NULL
)
1336 do_is_constant() const
1343 do_determine_type(const Type_context
*);
1350 do_get_tree(Translate_context
*)
1351 { return this->val_
? boolean_true_node
: boolean_false_node
; }
1354 do_export(Export
* exp
) const
1355 { exp
->write_c_string(this->val_
? "true" : "false"); }
1360 // The type as determined by context.
1367 Boolean_expression::do_type()
1369 if (this->type_
== NULL
)
1370 this->type_
= Type::make_boolean_type();
1374 // Set the type from the context.
1377 Boolean_expression::do_determine_type(const Type_context
* context
)
1379 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1381 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1382 this->type_
= context
->type
;
1383 else if (!context
->may_be_abstract
)
1384 this->type_
= Type::lookup_bool_type();
1387 // Import a boolean constant.
1390 Boolean_expression::do_import(Import
* imp
)
1392 if (imp
->peek_char() == 't')
1394 imp
->require_c_string("true");
1395 return Expression::make_boolean(true, imp
->location());
1399 imp
->require_c_string("false");
1400 return Expression::make_boolean(false, imp
->location());
1404 // Make a boolean expression.
1407 Expression::make_boolean(bool val
, source_location location
)
1409 return new Boolean_expression(val
, location
);
1412 // Class String_expression.
1417 String_expression::do_type()
1419 if (this->type_
== NULL
)
1420 this->type_
= Type::make_string_type();
1424 // Set the type from the context.
1427 String_expression::do_determine_type(const Type_context
* context
)
1429 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1431 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1432 this->type_
= context
->type
;
1433 else if (!context
->may_be_abstract
)
1434 this->type_
= Type::lookup_string_type();
1437 // Build a string constant.
1440 String_expression::do_get_tree(Translate_context
* context
)
1442 return context
->gogo()->go_string_constant_tree(this->val_
);
1445 // Export a string expression.
1448 String_expression::do_export(Export
* exp
) const
1451 s
.reserve(this->val_
.length() * 4 + 2);
1453 for (std::string::const_iterator p
= this->val_
.begin();
1454 p
!= this->val_
.end();
1457 if (*p
== '\\' || *p
== '"')
1462 else if (*p
>= 0x20 && *p
< 0x7f)
1464 else if (*p
== '\n')
1466 else if (*p
== '\t')
1471 unsigned char c
= *p
;
1472 unsigned int dig
= c
>> 4;
1473 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1475 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1479 exp
->write_string(s
);
1482 // Import a string expression.
1485 String_expression::do_import(Import
* imp
)
1487 imp
->require_c_string("\"");
1491 int c
= imp
->get_char();
1492 if (c
== '"' || c
== -1)
1495 val
+= static_cast<char>(c
);
1498 c
= imp
->get_char();
1499 if (c
== '\\' || c
== '"')
1500 val
+= static_cast<char>(c
);
1507 c
= imp
->get_char();
1508 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1509 c
= imp
->get_char();
1510 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1511 char v
= (vh
<< 4) | vl
;
1516 error_at(imp
->location(), "bad string constant");
1517 return Expression::make_error(imp
->location());
1521 return Expression::make_string(val
, imp
->location());
1524 // Make a string expression.
1527 Expression::make_string(const std::string
& val
, source_location location
)
1529 return new String_expression(val
, location
);
1532 // Make an integer expression.
1534 class Integer_expression
: public Expression
1537 Integer_expression(const mpz_t
* val
, Type
* type
, source_location location
)
1538 : Expression(EXPRESSION_INTEGER
, location
),
1540 { mpz_init_set(this->val_
, *val
); }
1545 // Return whether VAL fits in the type.
1547 check_constant(mpz_t val
, Type
*, source_location
);
1549 // Write VAL to export data.
1551 export_integer(Export
* exp
, const mpz_t val
);
1555 do_is_constant() const
1559 do_integer_constant_value(bool, mpz_t val
, Type
** ptype
) const;
1565 do_determine_type(const Type_context
* context
);
1568 do_check_types(Gogo
*);
1571 do_get_tree(Translate_context
*);
1575 { return Expression::make_integer(&this->val_
, this->type_
,
1576 this->location()); }
1579 do_export(Export
*) const;
1582 // The integer value.
1588 // Return an integer constant value.
1591 Integer_expression::do_integer_constant_value(bool, mpz_t val
,
1594 if (this->type_
!= NULL
)
1595 *ptype
= this->type_
;
1596 mpz_set(val
, this->val_
);
1600 // Return the current type. If we haven't set the type yet, we return
1601 // an abstract integer type.
1604 Integer_expression::do_type()
1606 if (this->type_
== NULL
)
1607 this->type_
= Type::make_abstract_integer_type();
1611 // Set the type of the integer value. Here we may switch from an
1612 // abstract type to a real type.
1615 Integer_expression::do_determine_type(const Type_context
* context
)
1617 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1619 else if (context
->type
!= NULL
1620 && (context
->type
->integer_type() != NULL
1621 || context
->type
->float_type() != NULL
1622 || context
->type
->complex_type() != NULL
))
1623 this->type_
= context
->type
;
1624 else if (!context
->may_be_abstract
)
1625 this->type_
= Type::lookup_integer_type("int");
1628 // Return true if the integer VAL fits in the range of the type TYPE.
1629 // Otherwise give an error and return false. TYPE may be NULL.
1632 Integer_expression::check_constant(mpz_t val
, Type
* type
,
1633 source_location location
)
1637 Integer_type
* itype
= type
->integer_type();
1638 if (itype
== NULL
|| itype
->is_abstract())
1641 int bits
= mpz_sizeinbase(val
, 2);
1643 if (itype
->is_unsigned())
1645 // For an unsigned type we can only accept a nonnegative number,
1646 // and we must be able to represent at least BITS.
1647 if (mpz_sgn(val
) >= 0
1648 && bits
<= itype
->bits())
1653 // For a signed type we need an extra bit to indicate the sign.
1654 // We have to handle the most negative integer specially.
1655 if (bits
+ 1 <= itype
->bits()
1656 || (bits
<= itype
->bits()
1658 && (mpz_scan1(val
, 0)
1659 == static_cast<unsigned long>(itype
->bits() - 1))
1660 && mpz_scan0(val
, itype
->bits()) == ULONG_MAX
))
1664 error_at(location
, "integer constant overflow");
1668 // Check the type of an integer constant.
1671 Integer_expression::do_check_types(Gogo
*)
1673 if (this->type_
== NULL
)
1675 if (!Integer_expression::check_constant(this->val_
, this->type_
,
1677 this->set_is_error();
1680 // Get a tree for an integer constant.
1683 Integer_expression::do_get_tree(Translate_context
* context
)
1685 Gogo
* gogo
= context
->gogo();
1687 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1688 type
= this->type_
->get_tree(gogo
);
1689 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
1691 // We are converting to an abstract floating point type.
1692 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
1694 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
1696 // We are converting to an abstract complex type.
1697 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
1701 // If we still have an abstract type here, then this is being
1702 // used in a constant expression which didn't get reduced for
1703 // some reason. Use a type which will fit the value. We use <,
1704 // not <=, because we need an extra bit for the sign bit.
1705 int bits
= mpz_sizeinbase(this->val_
, 2);
1706 if (bits
< INT_TYPE_SIZE
)
1707 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
1709 type
= Type::lookup_integer_type("int64")->get_tree(gogo
);
1711 type
= long_long_integer_type_node
;
1713 return Expression::integer_constant_tree(this->val_
, type
);
1716 // Write VAL to export data.
1719 Integer_expression::export_integer(Export
* exp
, const mpz_t val
)
1721 char* s
= mpz_get_str(NULL
, 10, val
);
1722 exp
->write_c_string(s
);
1726 // Export an integer in a constant expression.
1729 Integer_expression::do_export(Export
* exp
) const
1731 Integer_expression::export_integer(exp
, this->val_
);
1732 // A trailing space lets us reliably identify the end of the number.
1733 exp
->write_c_string(" ");
1736 // Import an integer, floating point, or complex value. This handles
1737 // all these types because they all start with digits.
1740 Integer_expression::do_import(Import
* imp
)
1742 std::string num
= imp
->read_identifier();
1743 imp
->require_c_string(" ");
1744 if (!num
.empty() && num
[num
.length() - 1] == 'i')
1747 size_t plus_pos
= num
.find('+', 1);
1748 size_t minus_pos
= num
.find('-', 1);
1750 if (plus_pos
== std::string::npos
)
1752 else if (minus_pos
== std::string::npos
)
1756 error_at(imp
->location(), "bad number in import data: %qs",
1758 return Expression::make_error(imp
->location());
1760 if (pos
== std::string::npos
)
1761 mpfr_set_ui(real
, 0, GMP_RNDN
);
1764 std::string real_str
= num
.substr(0, pos
);
1765 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
1767 error_at(imp
->location(), "bad number in import data: %qs",
1769 return Expression::make_error(imp
->location());
1773 std::string imag_str
;
1774 if (pos
== std::string::npos
)
1777 imag_str
= num
.substr(pos
);
1778 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
1780 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
1782 error_at(imp
->location(), "bad number in import data: %qs",
1784 return Expression::make_error(imp
->location());
1786 Expression
* ret
= Expression::make_complex(&real
, &imag
, NULL
,
1792 else if (num
.find('.') == std::string::npos
1793 && num
.find('E') == std::string::npos
)
1796 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
1798 error_at(imp
->location(), "bad number in import data: %qs",
1800 return Expression::make_error(imp
->location());
1802 Expression
* ret
= Expression::make_integer(&val
, NULL
, imp
->location());
1809 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
1811 error_at(imp
->location(), "bad number in import data: %qs",
1813 return Expression::make_error(imp
->location());
1815 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
1821 // Build a new integer value.
1824 Expression::make_integer(const mpz_t
* val
, Type
* type
,
1825 source_location location
)
1827 return new Integer_expression(val
, type
, location
);
1832 class Float_expression
: public Expression
1835 Float_expression(const mpfr_t
* val
, Type
* type
, source_location location
)
1836 : Expression(EXPRESSION_FLOAT
, location
),
1839 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
1842 // Constrain VAL to fit into TYPE.
1844 constrain_float(mpfr_t val
, Type
* type
);
1846 // Return whether VAL fits in the type.
1848 check_constant(mpfr_t val
, Type
*, source_location
);
1850 // Write VAL to export data.
1852 export_float(Export
* exp
, const mpfr_t val
);
1856 do_is_constant() const
1860 do_float_constant_value(mpfr_t val
, Type
**) const;
1866 do_determine_type(const Type_context
*);
1869 do_check_types(Gogo
*);
1873 { return Expression::make_float(&this->val_
, this->type_
,
1874 this->location()); }
1877 do_get_tree(Translate_context
*);
1880 do_export(Export
*) const;
1883 // The floating point value.
1889 // Constrain VAL to fit into TYPE.
1892 Float_expression::constrain_float(mpfr_t val
, Type
* type
)
1894 Float_type
* ftype
= type
->float_type();
1895 if (ftype
!= NULL
&& !ftype
->is_abstract())
1897 tree type_tree
= ftype
->type_tree();
1898 REAL_VALUE_TYPE rvt
;
1899 real_from_mpfr(&rvt
, val
, type_tree
, GMP_RNDN
);
1900 real_convert(&rvt
, TYPE_MODE(type_tree
), &rvt
);
1901 mpfr_from_real(val
, &rvt
, GMP_RNDN
);
1905 // Return a floating point constant value.
1908 Float_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
1910 if (this->type_
!= NULL
)
1911 *ptype
= this->type_
;
1912 mpfr_set(val
, this->val_
, GMP_RNDN
);
1916 // Return the current type. If we haven't set the type yet, we return
1917 // an abstract float type.
1920 Float_expression::do_type()
1922 if (this->type_
== NULL
)
1923 this->type_
= Type::make_abstract_float_type();
1927 // Set the type of the float value. Here we may switch from an
1928 // abstract type to a real type.
1931 Float_expression::do_determine_type(const Type_context
* context
)
1933 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1935 else if (context
->type
!= NULL
1936 && (context
->type
->integer_type() != NULL
1937 || context
->type
->float_type() != NULL
1938 || context
->type
->complex_type() != NULL
))
1939 this->type_
= context
->type
;
1940 else if (!context
->may_be_abstract
)
1941 this->type_
= Type::lookup_float_type("float64");
1944 // Return true if the floating point value VAL fits in the range of
1945 // the type TYPE. Otherwise give an error and return false. TYPE may
1949 Float_expression::check_constant(mpfr_t val
, Type
* type
,
1950 source_location location
)
1954 Float_type
* ftype
= type
->float_type();
1955 if (ftype
== NULL
|| ftype
->is_abstract())
1958 // A NaN or Infinity always fits in the range of the type.
1959 if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
1962 mp_exp_t exp
= mpfr_get_exp(val
);
1964 switch (ftype
->bits())
1977 error_at(location
, "floating point constant overflow");
1983 // Check the type of a float value.
1986 Float_expression::do_check_types(Gogo
*)
1988 if (this->type_
== NULL
)
1991 if (!Float_expression::check_constant(this->val_
, this->type_
,
1993 this->set_is_error();
1995 Integer_type
* integer_type
= this->type_
->integer_type();
1996 if (integer_type
!= NULL
)
1998 if (!mpfr_integer_p(this->val_
))
1999 this->report_error(_("floating point constant truncated to integer"));
2002 gcc_assert(!integer_type
->is_abstract());
2005 mpfr_get_z(ival
, this->val_
, GMP_RNDN
);
2006 Integer_expression::check_constant(ival
, integer_type
,
2013 // Get a tree for a float constant.
2016 Float_expression::do_get_tree(Translate_context
* context
)
2018 Gogo
* gogo
= context
->gogo();
2020 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2021 type
= this->type_
->get_tree(gogo
);
2022 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2024 // We have an abstract integer type. We just hope for the best.
2025 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
2029 // If we still have an abstract type here, then this is being
2030 // used in a constant expression which didn't get reduced. We
2031 // just use float64 and hope for the best.
2032 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
2034 return Expression::float_constant_tree(this->val_
, type
);
2037 // Write a floating point number to export data.
2040 Float_expression::export_float(Export
*exp
, const mpfr_t val
)
2043 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2045 exp
->write_c_string("-");
2046 exp
->write_c_string("0.");
2047 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2050 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2051 exp
->write_c_string(buf
);
2054 // Export a floating point number in a constant expression.
2057 Float_expression::do_export(Export
* exp
) const
2059 Float_expression::export_float(exp
, this->val_
);
2060 // A trailing space lets us reliably identify the end of the number.
2061 exp
->write_c_string(" ");
2064 // Make a float expression.
2067 Expression::make_float(const mpfr_t
* val
, Type
* type
, source_location location
)
2069 return new Float_expression(val
, type
, location
);
2074 class Complex_expression
: public Expression
2077 Complex_expression(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2078 source_location location
)
2079 : Expression(EXPRESSION_COMPLEX
, location
),
2082 mpfr_init_set(this->real_
, *real
, GMP_RNDN
);
2083 mpfr_init_set(this->imag_
, *imag
, GMP_RNDN
);
2086 // Constrain REAL/IMAG to fit into TYPE.
2088 constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
);
2090 // Return whether REAL/IMAG fits in the type.
2092 check_constant(mpfr_t real
, mpfr_t imag
, Type
*, source_location
);
2094 // Write REAL/IMAG to export data.
2096 export_complex(Export
* exp
, const mpfr_t real
, const mpfr_t val
);
2100 do_is_constant() const
2104 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2110 do_determine_type(const Type_context
*);
2113 do_check_types(Gogo
*);
2118 return Expression::make_complex(&this->real_
, &this->imag_
, this->type_
,
2123 do_get_tree(Translate_context
*);
2126 do_export(Export
*) const;
2131 // The imaginary part;
2133 // The type if known.
2137 // Constrain REAL/IMAG to fit into TYPE.
2140 Complex_expression::constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
)
2142 Complex_type
* ctype
= type
->complex_type();
2143 if (ctype
!= NULL
&& !ctype
->is_abstract())
2145 tree type_tree
= ctype
->type_tree();
2147 REAL_VALUE_TYPE rvt
;
2148 real_from_mpfr(&rvt
, real
, TREE_TYPE(type_tree
), GMP_RNDN
);
2149 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2150 mpfr_from_real(real
, &rvt
, GMP_RNDN
);
2152 real_from_mpfr(&rvt
, imag
, TREE_TYPE(type_tree
), GMP_RNDN
);
2153 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2154 mpfr_from_real(imag
, &rvt
, GMP_RNDN
);
2158 // Return a complex constant value.
2161 Complex_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2164 if (this->type_
!= NULL
)
2165 *ptype
= this->type_
;
2166 mpfr_set(real
, this->real_
, GMP_RNDN
);
2167 mpfr_set(imag
, this->imag_
, GMP_RNDN
);
2171 // Return the current type. If we haven't set the type yet, we return
2172 // an abstract complex type.
2175 Complex_expression::do_type()
2177 if (this->type_
== NULL
)
2178 this->type_
= Type::make_abstract_complex_type();
2182 // Set the type of the complex value. Here we may switch from an
2183 // abstract type to a real type.
2186 Complex_expression::do_determine_type(const Type_context
* context
)
2188 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2190 else if (context
->type
!= NULL
2191 && context
->type
->complex_type() != NULL
)
2192 this->type_
= context
->type
;
2193 else if (!context
->may_be_abstract
)
2194 this->type_
= Type::lookup_complex_type("complex128");
2197 // Return true if the complex value REAL/IMAG fits in the range of the
2198 // type TYPE. Otherwise give an error and return false. TYPE may be
2202 Complex_expression::check_constant(mpfr_t real
, mpfr_t imag
, Type
* type
,
2203 source_location location
)
2207 Complex_type
* ctype
= type
->complex_type();
2208 if (ctype
== NULL
|| ctype
->is_abstract())
2212 switch (ctype
->bits())
2224 // A NaN or Infinity always fits in the range of the type.
2225 if (!mpfr_nan_p(real
) && !mpfr_inf_p(real
) && !mpfr_zero_p(real
))
2227 if (mpfr_get_exp(real
) > max_exp
)
2229 error_at(location
, "complex real part constant overflow");
2234 if (!mpfr_nan_p(imag
) && !mpfr_inf_p(imag
) && !mpfr_zero_p(imag
))
2236 if (mpfr_get_exp(imag
) > max_exp
)
2238 error_at(location
, "complex imaginary part constant overflow");
2246 // Check the type of a complex value.
2249 Complex_expression::do_check_types(Gogo
*)
2251 if (this->type_
== NULL
)
2254 if (!Complex_expression::check_constant(this->real_
, this->imag_
,
2255 this->type_
, this->location()))
2256 this->set_is_error();
2259 // Get a tree for a complex constant.
2262 Complex_expression::do_get_tree(Translate_context
* context
)
2264 Gogo
* gogo
= context
->gogo();
2266 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2267 type
= this->type_
->get_tree(gogo
);
2270 // If we still have an abstract type here, this this is being
2271 // used in a constant expression which didn't get reduced. We
2272 // just use complex128 and hope for the best.
2273 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
2275 return Expression::complex_constant_tree(this->real_
, this->imag_
, type
);
2278 // Write REAL/IMAG to export data.
2281 Complex_expression::export_complex(Export
* exp
, const mpfr_t real
,
2284 if (!mpfr_zero_p(real
))
2286 Float_expression::export_float(exp
, real
);
2287 if (mpfr_sgn(imag
) > 0)
2288 exp
->write_c_string("+");
2290 Float_expression::export_float(exp
, imag
);
2291 exp
->write_c_string("i");
2294 // Export a complex number in a constant expression.
2297 Complex_expression::do_export(Export
* exp
) const
2299 Complex_expression::export_complex(exp
, this->real_
, this->imag_
);
2300 // A trailing space lets us reliably identify the end of the number.
2301 exp
->write_c_string(" ");
2304 // Make a complex expression.
2307 Expression::make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2308 source_location location
)
2310 return new Complex_expression(real
, imag
, type
, location
);
2313 // Find a named object in an expression.
2315 class Find_named_object
: public Traverse
2318 Find_named_object(Named_object
* no
)
2319 : Traverse(traverse_expressions
),
2320 no_(no
), found_(false)
2323 // Whether we found the object.
2326 { return this->found_
; }
2330 expression(Expression
**);
2333 // The object we are looking for.
2335 // Whether we found it.
2339 // A reference to a const in an expression.
2341 class Const_expression
: public Expression
2344 Const_expression(Named_object
* constant
, source_location location
)
2345 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2346 constant_(constant
), type_(NULL
), seen_(false)
2351 { return this->constant_
; }
2355 { return this->constant_
->name(); }
2357 // Check that the initializer does not refer to the constant itself.
2359 check_for_init_loop();
2363 do_traverse(Traverse
*);
2366 do_lower(Gogo
*, Named_object
*, int);
2369 do_is_constant() const
2373 do_integer_constant_value(bool, mpz_t val
, Type
**) const;
2376 do_float_constant_value(mpfr_t val
, Type
**) const;
2379 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2382 do_string_constant_value(std::string
* val
) const
2383 { return this->constant_
->const_value()->expr()->string_constant_value(val
); }
2388 // The type of a const is set by the declaration, not the use.
2390 do_determine_type(const Type_context
*);
2393 do_check_types(Gogo
*);
2400 do_get_tree(Translate_context
* context
);
2402 // When exporting a reference to a const as part of a const
2403 // expression, we export the value. We ignore the fact that it has
2406 do_export(Export
* exp
) const
2407 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2411 Named_object
* constant_
;
2412 // The type of this reference. This is used if the constant has an
2415 // Used to prevent infinite recursion when a constant incorrectly
2416 // refers to itself.
2423 Const_expression::do_traverse(Traverse
* traverse
)
2425 if (this->type_
!= NULL
)
2426 return Type::traverse(this->type_
, traverse
);
2427 return TRAVERSE_CONTINUE
;
2430 // Lower a constant expression. This is where we convert the
2431 // predeclared constant iota into an integer value.
2434 Const_expression::do_lower(Gogo
* gogo
, Named_object
*, int iota_value
)
2436 if (this->constant_
->const_value()->expr()->classification()
2439 if (iota_value
== -1)
2441 error_at(this->location(),
2442 "iota is only defined in const declarations");
2446 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2447 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2453 // Make sure that the constant itself has been lowered.
2454 gogo
->lower_constant(this->constant_
);
2459 // Return an integer constant value.
2462 Const_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
2469 if (this->type_
!= NULL
)
2470 ctype
= this->type_
;
2472 ctype
= this->constant_
->const_value()->type();
2473 if (ctype
!= NULL
&& ctype
->integer_type() == NULL
)
2476 Expression
* e
= this->constant_
->const_value()->expr();
2481 bool r
= e
->integer_constant_value(iota_is_constant
, val
, &t
);
2483 this->seen_
= false;
2487 && !Integer_expression::check_constant(val
, ctype
, this->location()))
2490 *ptype
= ctype
!= NULL
? ctype
: t
;
2494 // Return a floating point constant value.
2497 Const_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
2503 if (this->type_
!= NULL
)
2504 ctype
= this->type_
;
2506 ctype
= this->constant_
->const_value()->type();
2507 if (ctype
!= NULL
&& ctype
->float_type() == NULL
)
2513 bool r
= this->constant_
->const_value()->expr()->float_constant_value(val
,
2516 this->seen_
= false;
2518 if (r
&& ctype
!= NULL
)
2520 if (!Float_expression::check_constant(val
, ctype
, this->location()))
2522 Float_expression::constrain_float(val
, ctype
);
2524 *ptype
= ctype
!= NULL
? ctype
: t
;
2528 // Return a complex constant value.
2531 Const_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2538 if (this->type_
!= NULL
)
2539 ctype
= this->type_
;
2541 ctype
= this->constant_
->const_value()->type();
2542 if (ctype
!= NULL
&& ctype
->complex_type() == NULL
)
2548 bool r
= this->constant_
->const_value()->expr()->complex_constant_value(real
,
2552 this->seen_
= false;
2554 if (r
&& ctype
!= NULL
)
2556 if (!Complex_expression::check_constant(real
, imag
, ctype
,
2559 Complex_expression::constrain_complex(real
, imag
, ctype
);
2561 *ptype
= ctype
!= NULL
? ctype
: t
;
2565 // Return the type of the const reference.
2568 Const_expression::do_type()
2570 if (this->type_
!= NULL
)
2573 Named_constant
* nc
= this->constant_
->const_value();
2575 if (this->seen_
|| nc
->lowering())
2577 this->report_error(_("constant refers to itself"));
2578 this->type_
= Type::make_error_type();
2584 Type
* ret
= nc
->type();
2588 this->seen_
= false;
2592 // During parsing, a named constant may have a NULL type, but we
2593 // must not return a NULL type here.
2594 ret
= nc
->expr()->type();
2596 this->seen_
= false;
2601 // Set the type of the const reference.
2604 Const_expression::do_determine_type(const Type_context
* context
)
2606 Type
* ctype
= this->constant_
->const_value()->type();
2607 Type
* cetype
= (ctype
!= NULL
2609 : this->constant_
->const_value()->expr()->type());
2610 if (ctype
!= NULL
&& !ctype
->is_abstract())
2612 else if (context
->type
!= NULL
2613 && (context
->type
->integer_type() != NULL
2614 || context
->type
->float_type() != NULL
2615 || context
->type
->complex_type() != NULL
)
2616 && (cetype
->integer_type() != NULL
2617 || cetype
->float_type() != NULL
2618 || cetype
->complex_type() != NULL
))
2619 this->type_
= context
->type
;
2620 else if (context
->type
!= NULL
2621 && context
->type
->is_string_type()
2622 && cetype
->is_string_type())
2623 this->type_
= context
->type
;
2624 else if (context
->type
!= NULL
2625 && context
->type
->is_boolean_type()
2626 && cetype
->is_boolean_type())
2627 this->type_
= context
->type
;
2628 else if (!context
->may_be_abstract
)
2630 if (cetype
->is_abstract())
2631 cetype
= cetype
->make_non_abstract_type();
2632 this->type_
= cetype
;
2636 // Check for a loop in which the initializer of a constant refers to
2637 // the constant itself.
2640 Const_expression::check_for_init_loop()
2642 if (this->type_
!= NULL
&& this->type_
->is_error_type())
2647 this->report_error(_("constant refers to itself"));
2648 this->type_
= Type::make_error_type();
2652 Expression
* init
= this->constant_
->const_value()->expr();
2653 Find_named_object
find_named_object(this->constant_
);
2656 Expression::traverse(&init
, &find_named_object
);
2657 this->seen_
= false;
2659 if (find_named_object
.found())
2661 if (this->type_
== NULL
|| !this->type_
->is_error_type())
2663 this->report_error(_("constant refers to itself"));
2664 this->type_
= Type::make_error_type();
2670 // Check types of a const reference.
2673 Const_expression::do_check_types(Gogo
*)
2675 if (this->type_
!= NULL
&& this->type_
->is_error_type())
2678 this->check_for_init_loop();
2680 if (this->type_
== NULL
|| this->type_
->is_abstract())
2683 // Check for integer overflow.
2684 if (this->type_
->integer_type() != NULL
)
2689 if (!this->integer_constant_value(true, ival
, &dummy
))
2693 Expression
* cexpr
= this->constant_
->const_value()->expr();
2694 if (cexpr
->float_constant_value(fval
, &dummy
))
2696 if (!mpfr_integer_p(fval
))
2697 this->report_error(_("floating point constant "
2698 "truncated to integer"));
2701 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2702 Integer_expression::check_constant(ival
, this->type_
,
2712 // Return a tree for the const reference.
2715 Const_expression::do_get_tree(Translate_context
* context
)
2717 Gogo
* gogo
= context
->gogo();
2719 if (this->type_
== NULL
)
2720 type_tree
= NULL_TREE
;
2723 type_tree
= this->type_
->get_tree(gogo
);
2724 if (type_tree
== error_mark_node
)
2725 return error_mark_node
;
2728 // If the type has been set for this expression, but the underlying
2729 // object is an abstract int or float, we try to get the abstract
2730 // value. Otherwise we may lose something in the conversion.
2731 if (this->type_
!= NULL
2732 && (this->constant_
->const_value()->type() == NULL
2733 || this->constant_
->const_value()->type()->is_abstract()))
2735 Expression
* expr
= this->constant_
->const_value()->expr();
2739 if (expr
->integer_constant_value(true, ival
, &t
))
2741 tree ret
= Expression::integer_constant_tree(ival
, type_tree
);
2749 if (expr
->float_constant_value(fval
, &t
))
2751 tree ret
= Expression::float_constant_tree(fval
, type_tree
);
2758 if (expr
->complex_constant_value(fval
, imag
, &t
))
2760 tree ret
= Expression::complex_constant_tree(fval
, imag
, type_tree
);
2769 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2770 if (this->type_
== NULL
2771 || const_tree
== error_mark_node
2772 || TREE_TYPE(const_tree
) == error_mark_node
)
2776 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2777 ret
= fold_convert(type_tree
, const_tree
);
2778 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2779 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2780 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2781 ret
= fold(convert_to_real(type_tree
, const_tree
));
2782 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2783 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2789 // Make a reference to a constant in an expression.
2792 Expression::make_const_reference(Named_object
* constant
,
2793 source_location location
)
2795 return new Const_expression(constant
, location
);
2798 // Find a named object in an expression.
2801 Find_named_object::expression(Expression
** pexpr
)
2803 switch ((*pexpr
)->classification())
2805 case Expression::EXPRESSION_CONST_REFERENCE
:
2807 Const_expression
* ce
= static_cast<Const_expression
*>(*pexpr
);
2808 if (ce
->named_object() == this->no_
)
2811 // We need to check a constant initializer explicitly, as
2812 // loops here will not be caught by the loop checking for
2813 // variable initializers.
2814 ce
->check_for_init_loop();
2816 return TRAVERSE_CONTINUE
;
2819 case Expression::EXPRESSION_VAR_REFERENCE
:
2820 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
2822 return TRAVERSE_CONTINUE
;
2823 case Expression::EXPRESSION_FUNC_REFERENCE
:
2824 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
2826 return TRAVERSE_CONTINUE
;
2828 return TRAVERSE_CONTINUE
;
2830 this->found_
= true;
2831 return TRAVERSE_EXIT
;
2836 class Nil_expression
: public Expression
2839 Nil_expression(source_location location
)
2840 : Expression(EXPRESSION_NIL
, location
)
2848 do_is_constant() const
2853 { return Type::make_nil_type(); }
2856 do_determine_type(const Type_context
*)
2864 do_get_tree(Translate_context
*)
2865 { return null_pointer_node
; }
2868 do_export(Export
* exp
) const
2869 { exp
->write_c_string("nil"); }
2872 // Import a nil expression.
2875 Nil_expression::do_import(Import
* imp
)
2877 imp
->require_c_string("nil");
2878 return Expression::make_nil(imp
->location());
2881 // Make a nil expression.
2884 Expression::make_nil(source_location location
)
2886 return new Nil_expression(location
);
2889 // The value of the predeclared constant iota. This is little more
2890 // than a marker. This will be lowered to an integer in
2891 // Const_expression::do_lower, which is where we know the value that
2894 class Iota_expression
: public Parser_expression
2897 Iota_expression(source_location location
)
2898 : Parser_expression(EXPRESSION_IOTA
, location
)
2903 do_lower(Gogo
*, Named_object
*, int)
2904 { gcc_unreachable(); }
2906 // There should only ever be one of these.
2909 { gcc_unreachable(); }
2912 // Make an iota expression. This is only called for one case: the
2913 // value of the predeclared constant iota.
2916 Expression::make_iota()
2918 static Iota_expression
iota_expression(UNKNOWN_LOCATION
);
2919 return &iota_expression
;
2922 // A type conversion expression.
2924 class Type_conversion_expression
: public Expression
2927 Type_conversion_expression(Type
* type
, Expression
* expr
,
2928 source_location location
)
2929 : Expression(EXPRESSION_CONVERSION
, location
),
2930 type_(type
), expr_(expr
), may_convert_function_types_(false)
2933 // Return the type to which we are converting.
2936 { return this->type_
; }
2938 // Return the expression which we are converting.
2941 { return this->expr_
; }
2943 // Permit converting from one function type to another. This is
2944 // used internally for method expressions.
2946 set_may_convert_function_types()
2948 this->may_convert_function_types_
= true;
2951 // Import a type conversion expression.
2957 do_traverse(Traverse
* traverse
);
2960 do_lower(Gogo
*, Named_object
*, int);
2963 do_is_constant() const
2964 { return this->expr_
->is_constant(); }
2967 do_integer_constant_value(bool, mpz_t
, Type
**) const;
2970 do_float_constant_value(mpfr_t
, Type
**) const;
2973 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
2976 do_string_constant_value(std::string
*) const;
2980 { return this->type_
; }
2983 do_determine_type(const Type_context
*)
2985 Type_context
subcontext(this->type_
, false);
2986 this->expr_
->determine_type(&subcontext
);
2990 do_check_types(Gogo
*);
2995 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
3000 do_get_tree(Translate_context
* context
);
3003 do_export(Export
*) const;
3006 // The type to convert to.
3008 // The expression to convert.
3010 // True if this is permitted to convert function types. This is
3011 // used internally for method expressions.
3012 bool may_convert_function_types_
;
3018 Type_conversion_expression::do_traverse(Traverse
* traverse
)
3020 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3021 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3022 return TRAVERSE_EXIT
;
3023 return TRAVERSE_CONTINUE
;
3026 // Convert to a constant at lowering time.
3029 Type_conversion_expression::do_lower(Gogo
*, Named_object
*, int)
3031 Type
* type
= this->type_
;
3032 Expression
* val
= this->expr_
;
3033 source_location location
= this->location();
3035 if (type
->integer_type() != NULL
)
3040 if (val
->integer_constant_value(false, ival
, &dummy
))
3042 if (!Integer_expression::check_constant(ival
, type
, location
))
3043 mpz_set_ui(ival
, 0);
3044 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
3051 if (val
->float_constant_value(fval
, &dummy
))
3053 if (!mpfr_integer_p(fval
))
3056 "floating point constant truncated to integer");
3057 return Expression::make_error(location
);
3059 mpfr_get_z(ival
, fval
, GMP_RNDN
);
3060 if (!Integer_expression::check_constant(ival
, type
, location
))
3061 mpz_set_ui(ival
, 0);
3062 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
3071 if (type
->float_type() != NULL
)
3076 if (val
->float_constant_value(fval
, &dummy
))
3078 if (!Float_expression::check_constant(fval
, type
, location
))
3079 mpfr_set_ui(fval
, 0, GMP_RNDN
);
3080 Float_expression::constrain_float(fval
, type
);
3081 Expression
*ret
= Expression::make_float(&fval
, type
, location
);
3088 if (type
->complex_type() != NULL
)
3095 if (val
->complex_constant_value(real
, imag
, &dummy
))
3097 if (!Complex_expression::check_constant(real
, imag
, type
, location
))
3099 mpfr_set_ui(real
, 0, GMP_RNDN
);
3100 mpfr_set_ui(imag
, 0, GMP_RNDN
);
3102 Complex_expression::constrain_complex(real
, imag
, type
);
3103 Expression
* ret
= Expression::make_complex(&real
, &imag
, type
,
3113 if (type
->is_open_array_type() && type
->named_type() == NULL
)
3115 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3116 bool is_byte
= element_type
== Type::lookup_integer_type("uint8");
3117 bool is_int
= element_type
== Type::lookup_integer_type("int");
3118 if (is_byte
|| is_int
)
3121 if (val
->string_constant_value(&s
))
3123 Expression_list
* vals
= new Expression_list();
3126 for (std::string::const_iterator p
= s
.begin();
3131 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
3132 Expression
* v
= Expression::make_integer(&val
,
3141 const char *p
= s
.data();
3142 const char *pend
= s
.data() + s
.length();
3146 int adv
= Lex::fetch_char(p
, &c
);
3149 warning_at(this->location(), 0,
3150 "invalid UTF-8 encoding");
3155 mpz_init_set_ui(val
, c
);
3156 Expression
* v
= Expression::make_integer(&val
,
3164 return Expression::make_slice_composite_literal(type
, vals
,
3173 // Return the constant integer value if there is one.
3176 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant
,
3180 if (this->type_
->integer_type() == NULL
)
3186 if (this->expr_
->integer_constant_value(iota_is_constant
, ival
, &dummy
))
3188 if (!Integer_expression::check_constant(ival
, this->type_
,
3196 *ptype
= this->type_
;
3203 if (this->expr_
->float_constant_value(fval
, &dummy
))
3205 mpfr_get_z(val
, fval
, GMP_RNDN
);
3207 if (!Integer_expression::check_constant(val
, this->type_
,
3210 *ptype
= this->type_
;
3218 // Return the constant floating point value if there is one.
3221 Type_conversion_expression::do_float_constant_value(mpfr_t val
,
3224 if (this->type_
->float_type() == NULL
)
3230 if (this->expr_
->float_constant_value(fval
, &dummy
))
3232 if (!Float_expression::check_constant(fval
, this->type_
,
3238 mpfr_set(val
, fval
, GMP_RNDN
);
3240 Float_expression::constrain_float(val
, this->type_
);
3241 *ptype
= this->type_
;
3249 // Return the constant complex value if there is one.
3252 Type_conversion_expression::do_complex_constant_value(mpfr_t real
,
3256 if (this->type_
->complex_type() == NULL
)
3264 if (this->expr_
->complex_constant_value(rval
, ival
, &dummy
))
3266 if (!Complex_expression::check_constant(rval
, ival
, this->type_
,
3273 mpfr_set(real
, rval
, GMP_RNDN
);
3274 mpfr_set(imag
, ival
, GMP_RNDN
);
3277 Complex_expression::constrain_complex(real
, imag
, this->type_
);
3278 *ptype
= this->type_
;
3287 // Return the constant string value if there is one.
3290 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3292 if (this->type_
->is_string_type()
3293 && this->expr_
->type()->integer_type() != NULL
)
3298 if (this->expr_
->integer_constant_value(false, ival
, &dummy
))
3300 unsigned long ulval
= mpz_get_ui(ival
);
3301 if (mpz_cmp_ui(ival
, ulval
) == 0)
3303 Lex::append_char(ulval
, true, val
, this->location());
3311 // FIXME: Could handle conversion from const []int here.
3316 // Check that types are convertible.
3319 Type_conversion_expression::do_check_types(Gogo
*)
3321 Type
* type
= this->type_
;
3322 Type
* expr_type
= this->expr_
->type();
3325 if (type
->is_error_type()
3326 || type
->is_undefined()
3327 || expr_type
->is_error_type()
3328 || expr_type
->is_undefined())
3330 // Make sure we emit an error for an undefined type.
3333 this->set_is_error();
3337 if (this->may_convert_function_types_
3338 && type
->function_type() != NULL
3339 && expr_type
->function_type() != NULL
)
3342 if (Type::are_convertible(type
, expr_type
, &reason
))
3345 error_at(this->location(), "%s", reason
.c_str());
3346 this->set_is_error();
3349 // Get a tree for a type conversion.
3352 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3354 Gogo
* gogo
= context
->gogo();
3355 tree type_tree
= this->type_
->get_tree(gogo
);
3356 tree expr_tree
= this->expr_
->get_tree(context
);
3358 if (type_tree
== error_mark_node
3359 || expr_tree
== error_mark_node
3360 || TREE_TYPE(expr_tree
) == error_mark_node
)
3361 return error_mark_node
;
3363 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3364 return fold_convert(type_tree
, expr_tree
);
3366 Type
* type
= this->type_
;
3367 Type
* expr_type
= this->expr_
->type();
3369 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3370 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3371 expr_tree
, this->location());
3372 else if (type
->integer_type() != NULL
)
3374 if (expr_type
->integer_type() != NULL
3375 || expr_type
->float_type() != NULL
3376 || expr_type
->is_unsafe_pointer_type())
3377 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3381 else if (type
->float_type() != NULL
)
3383 if (expr_type
->integer_type() != NULL
3384 || expr_type
->float_type() != NULL
)
3385 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3389 else if (type
->complex_type() != NULL
)
3391 if (expr_type
->complex_type() != NULL
)
3392 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3396 else if (type
->is_string_type()
3397 && expr_type
->integer_type() != NULL
)
3399 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3400 if (host_integerp(expr_tree
, 0))
3402 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3404 Lex::append_char(intval
, true, &s
, this->location());
3405 Expression
* se
= Expression::make_string(s
, this->location());
3406 return se
->get_tree(context
);
3409 static tree int_to_string_fndecl
;
3410 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3412 "__go_int_to_string",
3416 fold_convert(integer_type_node
, expr_tree
));
3418 else if (type
->is_string_type()
3419 && (expr_type
->array_type() != NULL
3420 || (expr_type
->points_to() != NULL
3421 && expr_type
->points_to()->array_type() != NULL
)))
3423 Type
* t
= expr_type
;
3424 if (t
->points_to() != NULL
)
3427 expr_tree
= build_fold_indirect_ref(expr_tree
);
3429 if (!DECL_P(expr_tree
))
3430 expr_tree
= save_expr(expr_tree
);
3431 Array_type
* a
= t
->array_type();
3432 Type
* e
= a
->element_type()->forwarded();
3433 gcc_assert(e
->integer_type() != NULL
);
3434 tree valptr
= fold_convert(const_ptr_type_node
,
3435 a
->value_pointer_tree(gogo
, expr_tree
));
3436 tree len
= a
->length_tree(gogo
, expr_tree
);
3437 len
= fold_convert_loc(this->location(), size_type_node
, len
);
3438 if (e
->integer_type()->is_unsigned()
3439 && e
->integer_type()->bits() == 8)
3441 static tree byte_array_to_string_fndecl
;
3442 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3444 "__go_byte_array_to_string",
3447 const_ptr_type_node
,
3454 gcc_assert(e
== Type::lookup_integer_type("int"));
3455 static tree int_array_to_string_fndecl
;
3456 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3458 "__go_int_array_to_string",
3461 const_ptr_type_node
,
3467 else if (type
->is_open_array_type() && expr_type
->is_string_type())
3469 Type
* e
= type
->array_type()->element_type()->forwarded();
3470 gcc_assert(e
->integer_type() != NULL
);
3471 if (e
->integer_type()->is_unsigned()
3472 && e
->integer_type()->bits() == 8)
3474 static tree string_to_byte_array_fndecl
;
3475 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3477 "__go_string_to_byte_array",
3480 TREE_TYPE(expr_tree
),
3485 gcc_assert(e
== Type::lookup_integer_type("int"));
3486 static tree string_to_int_array_fndecl
;
3487 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3489 "__go_string_to_int_array",
3492 TREE_TYPE(expr_tree
),
3496 else if ((type
->is_unsafe_pointer_type()
3497 && expr_type
->points_to() != NULL
)
3498 || (expr_type
->is_unsafe_pointer_type()
3499 && type
->points_to() != NULL
))
3500 ret
= fold_convert(type_tree
, expr_tree
);
3501 else if (type
->is_unsafe_pointer_type()
3502 && expr_type
->integer_type() != NULL
)
3503 ret
= convert_to_pointer(type_tree
, expr_tree
);
3504 else if (this->may_convert_function_types_
3505 && type
->function_type() != NULL
3506 && expr_type
->function_type() != NULL
)
3507 ret
= fold_convert_loc(this->location(), type_tree
, expr_tree
);
3509 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3510 expr_tree
, this->location());
3515 // Output a type conversion in a constant expression.
3518 Type_conversion_expression::do_export(Export
* exp
) const
3520 exp
->write_c_string("convert(");
3521 exp
->write_type(this->type_
);
3522 exp
->write_c_string(", ");
3523 this->expr_
->export_expression(exp
);
3524 exp
->write_c_string(")");
3527 // Import a type conversion or a struct construction.
3530 Type_conversion_expression::do_import(Import
* imp
)
3532 imp
->require_c_string("convert(");
3533 Type
* type
= imp
->read_type();
3534 imp
->require_c_string(", ");
3535 Expression
* val
= Expression::import_expression(imp
);
3536 imp
->require_c_string(")");
3537 return Expression::make_cast(type
, val
, imp
->location());
3540 // Make a type cast expression.
3543 Expression::make_cast(Type
* type
, Expression
* val
, source_location location
)
3545 if (type
->is_error_type() || val
->is_error_expression())
3546 return Expression::make_error(location
);
3547 return new Type_conversion_expression(type
, val
, location
);
3550 // Unary expressions.
3552 class Unary_expression
: public Expression
3555 Unary_expression(Operator op
, Expression
* expr
, source_location location
)
3556 : Expression(EXPRESSION_UNARY
, location
),
3557 op_(op
), escapes_(true), expr_(expr
)
3560 // Return the operator.
3563 { return this->op_
; }
3565 // Return the operand.
3568 { return this->expr_
; }
3570 // Record that an address expression does not escape.
3572 set_does_not_escape()
3574 gcc_assert(this->op_
== OPERATOR_AND
);
3575 this->escapes_
= false;
3578 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3579 // could be done, false if not.
3581 eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3584 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3585 // could be done, false if not.
3587 eval_float(Operator op
, mpfr_t uval
, mpfr_t val
);
3589 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3590 // true if this could be done, false if not.
3592 eval_complex(Operator op
, mpfr_t ureal
, mpfr_t uimag
, mpfr_t real
,
3600 do_traverse(Traverse
* traverse
)
3601 { return Expression::traverse(&this->expr_
, traverse
); }
3604 do_lower(Gogo
*, Named_object
*, int);
3607 do_is_constant() const;
3610 do_integer_constant_value(bool, mpz_t
, Type
**) const;
3613 do_float_constant_value(mpfr_t
, Type
**) const;
3616 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
3622 do_determine_type(const Type_context
*);
3625 do_check_types(Gogo
*);
3630 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3635 do_is_addressable() const
3636 { return this->op_
== OPERATOR_MULT
; }
3639 do_get_tree(Translate_context
*);
3642 do_export(Export
*) const;
3645 // The unary operator to apply.
3647 // Normally true. False if this is an address expression which does
3648 // not escape the current function.
3654 // If we are taking the address of a composite literal, and the
3655 // contents are not constant, then we want to make a heap composite
3659 Unary_expression::do_lower(Gogo
*, Named_object
*, int)
3661 source_location loc
= this->location();
3662 Operator op
= this->op_
;
3663 Expression
* expr
= this->expr_
;
3665 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3666 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3668 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3669 // moving x to the heap. FIXME: Is it worth doing a real escape
3670 // analysis here? This case is found in math/unsafe.go and is
3671 // therefore worth special casing.
3672 if (op
== OPERATOR_MULT
)
3674 Expression
* e
= expr
;
3675 while (e
->classification() == EXPRESSION_CONVERSION
)
3677 Type_conversion_expression
* te
3678 = static_cast<Type_conversion_expression
*>(e
);
3682 if (e
->classification() == EXPRESSION_UNARY
)
3684 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3685 if (ue
->op_
== OPERATOR_AND
)
3692 ue
->set_does_not_escape();
3697 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
3698 || op
== OPERATOR_NOT
|| op
== OPERATOR_XOR
)
3700 Expression
* ret
= NULL
;
3705 if (expr
->integer_constant_value(false, eval
, &etype
))
3709 if (Unary_expression::eval_integer(op
, etype
, eval
, val
, loc
))
3710 ret
= Expression::make_integer(&val
, etype
, loc
);
3717 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
)
3722 if (expr
->float_constant_value(fval
, &ftype
))
3726 if (Unary_expression::eval_float(op
, fval
, val
))
3727 ret
= Expression::make_float(&val
, ftype
, loc
);
3738 if (expr
->complex_constant_value(fval
, ival
, &ftype
))
3744 if (Unary_expression::eval_complex(op
, fval
, ival
, real
, imag
))
3745 ret
= Expression::make_complex(&real
, &imag
, ftype
, loc
);
3759 // Return whether a unary expression is a constant.
3762 Unary_expression::do_is_constant() const
3764 if (this->op_
== OPERATOR_MULT
)
3766 // Indirecting through a pointer is only constant if the object
3767 // to which the expression points is constant, but we currently
3768 // have no way to determine that.
3771 else if (this->op_
== OPERATOR_AND
)
3773 // Taking the address of a variable is constant if it is a
3774 // global variable, not constant otherwise. In other cases
3775 // taking the address is probably not a constant.
3776 Var_expression
* ve
= this->expr_
->var_expression();
3779 Named_object
* no
= ve
->named_object();
3780 return no
->is_variable() && no
->var_value()->is_global();
3785 return this->expr_
->is_constant();
3788 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3789 // UVAL, if known; it may be NULL. Return true if this could be done,
3793 Unary_expression::eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3794 source_location location
)
3801 case OPERATOR_MINUS
:
3803 return Integer_expression::check_constant(val
, utype
, location
);
3805 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3809 || utype
->integer_type() == NULL
3810 || utype
->integer_type()->is_abstract())
3814 // The number of HOST_WIDE_INTs that it takes to represent
3816 size_t count
= ((mpz_sizeinbase(uval
, 2)
3817 + HOST_BITS_PER_WIDE_INT
3819 / HOST_BITS_PER_WIDE_INT
);
3821 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3822 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3825 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3826 gcc_assert(ecount
<= count
);
3828 // Trim down to the number of words required by the type.
3829 size_t obits
= utype
->integer_type()->bits();
3830 if (!utype
->integer_type()->is_unsigned())
3832 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3833 / HOST_BITS_PER_WIDE_INT
);
3834 gcc_assert(ocount
<= ocount
);
3836 for (size_t i
= 0; i
< ocount
; ++i
)
3839 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3841 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3844 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3848 return Integer_expression::check_constant(val
, utype
, location
);
3857 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3858 // could be done, false if not.
3861 Unary_expression::eval_float(Operator op
, mpfr_t uval
, mpfr_t val
)
3866 mpfr_set(val
, uval
, GMP_RNDN
);
3868 case OPERATOR_MINUS
:
3869 mpfr_neg(val
, uval
, GMP_RNDN
);
3881 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3882 // if this could be done, false if not.
3885 Unary_expression::eval_complex(Operator op
, mpfr_t rval
, mpfr_t ival
,
3886 mpfr_t real
, mpfr_t imag
)
3891 mpfr_set(real
, rval
, GMP_RNDN
);
3892 mpfr_set(imag
, ival
, GMP_RNDN
);
3894 case OPERATOR_MINUS
:
3895 mpfr_neg(real
, rval
, GMP_RNDN
);
3896 mpfr_neg(imag
, ival
, GMP_RNDN
);
3908 // Return the integral constant value of a unary expression, if it has one.
3911 Unary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
3917 if (!this->expr_
->integer_constant_value(iota_is_constant
, uval
, ptype
))
3920 ret
= Unary_expression::eval_integer(this->op_
, *ptype
, uval
, val
,
3926 // Return the floating point constant value of a unary expression, if
3930 Unary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
3935 if (!this->expr_
->float_constant_value(uval
, ptype
))
3938 ret
= Unary_expression::eval_float(this->op_
, uval
, val
);
3943 // Return the complex constant value of a unary expression, if it has
3947 Unary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
3955 if (!this->expr_
->complex_constant_value(rval
, ival
, ptype
))
3958 ret
= Unary_expression::eval_complex(this->op_
, rval
, ival
, real
, imag
);
3964 // Return the type of a unary expression.
3967 Unary_expression::do_type()
3972 case OPERATOR_MINUS
:
3975 return this->expr_
->type();
3978 return Type::make_pointer_type(this->expr_
->type());
3982 Type
* subtype
= this->expr_
->type();
3983 Type
* points_to
= subtype
->points_to();
3984 if (points_to
== NULL
)
3985 return Type::make_error_type();
3994 // Determine abstract types for a unary expression.
3997 Unary_expression::do_determine_type(const Type_context
* context
)
4002 case OPERATOR_MINUS
:
4005 this->expr_
->determine_type(context
);
4009 // Taking the address of something.
4011 Type
* subtype
= (context
->type
== NULL
4013 : context
->type
->points_to());
4014 Type_context
subcontext(subtype
, false);
4015 this->expr_
->determine_type(&subcontext
);
4020 // Indirecting through a pointer.
4022 Type
* subtype
= (context
->type
== NULL
4024 : Type::make_pointer_type(context
->type
));
4025 Type_context
subcontext(subtype
, false);
4026 this->expr_
->determine_type(&subcontext
);
4035 // Check types for a unary expression.
4038 Unary_expression::do_check_types(Gogo
*)
4040 Type
* type
= this->expr_
->type();
4041 if (type
->is_error_type())
4043 this->set_is_error();
4050 case OPERATOR_MINUS
:
4051 if (type
->integer_type() == NULL
4052 && type
->float_type() == NULL
4053 && type
->complex_type() == NULL
)
4054 this->report_error(_("expected numeric type"));
4059 if (type
->integer_type() == NULL
4060 && !type
->is_boolean_type())
4061 this->report_error(_("expected integer or boolean type"));
4065 if (!this->expr_
->is_addressable())
4066 this->report_error(_("invalid operand for unary %<&%>"));
4068 this->expr_
->address_taken(this->escapes_
);
4072 // Indirecting through a pointer.
4073 if (type
->points_to() == NULL
)
4074 this->report_error(_("expected pointer"));
4082 // Get a tree for a unary expression.
4085 Unary_expression::do_get_tree(Translate_context
* context
)
4087 tree expr
= this->expr_
->get_tree(context
);
4088 if (expr
== error_mark_node
)
4089 return error_mark_node
;
4091 source_location loc
= this->location();
4097 case OPERATOR_MINUS
:
4099 tree type
= TREE_TYPE(expr
);
4100 tree compute_type
= excess_precision_type(type
);
4101 if (compute_type
!= NULL_TREE
)
4102 expr
= ::convert(compute_type
, expr
);
4103 tree ret
= fold_build1_loc(loc
, NEGATE_EXPR
,
4104 (compute_type
!= NULL_TREE
4108 if (compute_type
!= NULL_TREE
)
4109 ret
= ::convert(type
, ret
);
4114 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
4115 return fold_build1_loc(loc
, TRUTH_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4117 return fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, expr
,
4118 build_int_cst(TREE_TYPE(expr
), 0));
4121 return fold_build1_loc(loc
, BIT_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4124 // We should not see a non-constant constructor here; cases
4125 // where we would see one should have been moved onto the heap
4126 // at parse time. Taking the address of a nonconstant
4127 // constructor will not do what the programmer expects.
4128 gcc_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
4129 gcc_assert(TREE_CODE(expr
) != ADDR_EXPR
);
4131 // Build a decl for a constant constructor.
4132 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
4134 tree decl
= build_decl(this->location(), VAR_DECL
,
4135 create_tmp_var_name("C"), TREE_TYPE(expr
));
4136 DECL_EXTERNAL(decl
) = 0;
4137 TREE_PUBLIC(decl
) = 0;
4138 TREE_READONLY(decl
) = 1;
4139 TREE_CONSTANT(decl
) = 1;
4140 TREE_STATIC(decl
) = 1;
4141 TREE_ADDRESSABLE(decl
) = 1;
4142 DECL_ARTIFICIAL(decl
) = 1;
4143 DECL_INITIAL(decl
) = expr
;
4144 rest_of_decl_compilation(decl
, 1, 0);
4148 return build_fold_addr_expr_loc(loc
, expr
);
4152 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
4154 // If we are dereferencing the pointer to a large struct, we
4155 // need to check for nil. We don't bother to check for small
4156 // structs because we expect the system to crash on a nil
4157 // pointer dereference.
4158 HOST_WIDE_INT s
= int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr
)));
4159 if (s
== -1 || s
>= 4096)
4162 expr
= save_expr(expr
);
4163 tree compare
= fold_build2_loc(loc
, EQ_EXPR
, boolean_type_node
,
4165 fold_convert(TREE_TYPE(expr
),
4166 null_pointer_node
));
4167 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4169 expr
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(expr
),
4170 build3(COND_EXPR
, void_type_node
,
4171 compare
, crash
, NULL_TREE
),
4175 // If the type of EXPR is a recursive pointer type, then we
4176 // need to insert a cast before indirecting.
4177 if (TREE_TYPE(TREE_TYPE(expr
)) == ptr_type_node
)
4179 Type
* pt
= this->expr_
->type()->points_to();
4180 tree ind
= pt
->get_tree(context
->gogo());
4181 expr
= fold_convert_loc(loc
, build_pointer_type(ind
), expr
);
4184 return build_fold_indirect_ref_loc(loc
, expr
);
4192 // Export a unary expression.
4195 Unary_expression::do_export(Export
* exp
) const
4200 exp
->write_c_string("+ ");
4202 case OPERATOR_MINUS
:
4203 exp
->write_c_string("- ");
4206 exp
->write_c_string("! ");
4209 exp
->write_c_string("^ ");
4216 this->expr_
->export_expression(exp
);
4219 // Import a unary expression.
4222 Unary_expression::do_import(Import
* imp
)
4225 switch (imp
->get_char())
4231 op
= OPERATOR_MINUS
;
4242 imp
->require_c_string(" ");
4243 Expression
* expr
= Expression::import_expression(imp
);
4244 return Expression::make_unary(op
, expr
, imp
->location());
4247 // Make a unary expression.
4250 Expression::make_unary(Operator op
, Expression
* expr
, source_location location
)
4252 return new Unary_expression(op
, expr
, location
);
4255 // If this is an indirection through a pointer, return the expression
4256 // being pointed through. Otherwise return this.
4261 if (this->classification_
== EXPRESSION_UNARY
)
4263 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4264 if (ue
->op() == OPERATOR_MULT
)
4265 return ue
->operand();
4270 // Class Binary_expression.
4275 Binary_expression::do_traverse(Traverse
* traverse
)
4277 int t
= Expression::traverse(&this->left_
, traverse
);
4278 if (t
== TRAVERSE_EXIT
)
4279 return TRAVERSE_EXIT
;
4280 return Expression::traverse(&this->right_
, traverse
);
4283 // Compare integer constants according to OP.
4286 Binary_expression::compare_integer(Operator op
, mpz_t left_val
,
4289 int i
= mpz_cmp(left_val
, right_val
);
4294 case OPERATOR_NOTEQ
:
4309 // Compare floating point constants according to OP.
4312 Binary_expression::compare_float(Operator op
, Type
* type
, mpfr_t left_val
,
4317 i
= mpfr_cmp(left_val
, right_val
);
4321 mpfr_init_set(lv
, left_val
, GMP_RNDN
);
4323 mpfr_init_set(rv
, right_val
, GMP_RNDN
);
4324 Float_expression::constrain_float(lv
, type
);
4325 Float_expression::constrain_float(rv
, type
);
4326 i
= mpfr_cmp(lv
, rv
);
4334 case OPERATOR_NOTEQ
:
4349 // Compare complex constants according to OP. Complex numbers may
4350 // only be compared for equality.
4353 Binary_expression::compare_complex(Operator op
, Type
* type
,
4354 mpfr_t left_real
, mpfr_t left_imag
,
4355 mpfr_t right_real
, mpfr_t right_imag
)
4359 is_equal
= (mpfr_cmp(left_real
, right_real
) == 0
4360 && mpfr_cmp(left_imag
, right_imag
) == 0);
4365 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4366 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4369 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4370 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4371 Complex_expression::constrain_complex(lr
, li
, type
);
4372 Complex_expression::constrain_complex(rr
, ri
, type
);
4373 is_equal
= mpfr_cmp(lr
, rr
) == 0 && mpfr_cmp(li
, ri
) == 0;
4383 case OPERATOR_NOTEQ
:
4390 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4391 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4392 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4393 // this could be done, false if not.
4396 Binary_expression::eval_integer(Operator op
, Type
* left_type
, mpz_t left_val
,
4397 Type
* right_type
, mpz_t right_val
,
4398 source_location location
, mpz_t val
)
4400 bool is_shift_op
= false;
4404 case OPERATOR_ANDAND
:
4406 case OPERATOR_NOTEQ
:
4411 // These return boolean values. We should probably handle them
4412 // anyhow in case a type conversion is used on the result.
4415 mpz_add(val
, left_val
, right_val
);
4417 case OPERATOR_MINUS
:
4418 mpz_sub(val
, left_val
, right_val
);
4421 mpz_ior(val
, left_val
, right_val
);
4424 mpz_xor(val
, left_val
, right_val
);
4427 mpz_mul(val
, left_val
, right_val
);
4430 if (mpz_sgn(right_val
) != 0)
4431 mpz_tdiv_q(val
, left_val
, right_val
);
4434 error_at(location
, "division by zero");
4440 if (mpz_sgn(right_val
) != 0)
4441 mpz_tdiv_r(val
, left_val
, right_val
);
4444 error_at(location
, "division by zero");
4449 case OPERATOR_LSHIFT
:
4451 unsigned long shift
= mpz_get_ui(right_val
);
4452 if (mpz_cmp_ui(right_val
, shift
) != 0 || shift
> 0x100000)
4454 error_at(location
, "shift count overflow");
4458 mpz_mul_2exp(val
, left_val
, shift
);
4463 case OPERATOR_RSHIFT
:
4465 unsigned long shift
= mpz_get_ui(right_val
);
4466 if (mpz_cmp_ui(right_val
, shift
) != 0)
4468 error_at(location
, "shift count overflow");
4472 if (mpz_cmp_ui(left_val
, 0) >= 0)
4473 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4475 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4481 mpz_and(val
, left_val
, right_val
);
4483 case OPERATOR_BITCLEAR
:
4487 mpz_com(tval
, right_val
);
4488 mpz_and(val
, left_val
, tval
);
4496 Type
* type
= left_type
;
4501 else if (type
!= right_type
&& right_type
!= NULL
)
4503 if (type
->is_abstract())
4505 else if (!right_type
->is_abstract())
4507 // This look like a type error which should be diagnosed
4508 // elsewhere. Don't do anything here, to avoid an
4509 // unhelpful chain of error messages.
4515 if (type
!= NULL
&& !type
->is_abstract())
4517 // We have to check the operands too, as we have implicitly
4518 // coerced them to TYPE.
4519 if ((type
!= left_type
4520 && !Integer_expression::check_constant(left_val
, type
, location
))
4522 && type
!= right_type
4523 && !Integer_expression::check_constant(right_val
, type
,
4525 || !Integer_expression::check_constant(val
, type
, location
))
4532 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4533 // Return true if this could be done, false if not.
4536 Binary_expression::eval_float(Operator op
, Type
* left_type
, mpfr_t left_val
,
4537 Type
* right_type
, mpfr_t right_val
,
4538 mpfr_t val
, source_location location
)
4543 case OPERATOR_ANDAND
:
4545 case OPERATOR_NOTEQ
:
4550 // These return boolean values. We should probably handle them
4551 // anyhow in case a type conversion is used on the result.
4554 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4556 case OPERATOR_MINUS
:
4557 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4562 case OPERATOR_BITCLEAR
:
4565 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4568 if (mpfr_zero_p(right_val
))
4569 error_at(location
, "division by zero");
4570 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4574 case OPERATOR_LSHIFT
:
4575 case OPERATOR_RSHIFT
:
4581 Type
* type
= left_type
;
4584 else if (type
!= right_type
&& right_type
!= NULL
)
4586 if (type
->is_abstract())
4588 else if (!right_type
->is_abstract())
4590 // This looks like a type error which should be diagnosed
4591 // elsewhere. Don't do anything here, to avoid an unhelpful
4592 // chain of error messages.
4597 if (type
!= NULL
&& !type
->is_abstract())
4599 if ((type
!= left_type
4600 && !Float_expression::check_constant(left_val
, type
, location
))
4601 || (type
!= right_type
4602 && !Float_expression::check_constant(right_val
, type
,
4604 || !Float_expression::check_constant(val
, type
, location
))
4605 mpfr_set_ui(val
, 0, GMP_RNDN
);
4611 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4612 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4613 // could be done, false if not.
4616 Binary_expression::eval_complex(Operator op
, Type
* left_type
,
4617 mpfr_t left_real
, mpfr_t left_imag
,
4619 mpfr_t right_real
, mpfr_t right_imag
,
4620 mpfr_t real
, mpfr_t imag
,
4621 source_location location
)
4626 case OPERATOR_ANDAND
:
4628 case OPERATOR_NOTEQ
:
4633 // These return boolean values and must be handled differently.
4636 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4637 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4639 case OPERATOR_MINUS
:
4640 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4641 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4646 case OPERATOR_BITCLEAR
:
4650 // You might think that multiplying two complex numbers would
4651 // be simple, and you would be right, until you start to think
4652 // about getting the right answer for infinity. If one
4653 // operand here is infinity and the other is anything other
4654 // than zero or NaN, then we are going to wind up subtracting
4655 // two infinity values. That will give us a NaN, but the
4656 // correct answer is infinity.
4660 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4664 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4668 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4672 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4674 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4675 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4677 // If we get NaN on both sides, check whether it should really
4678 // be infinity. The rule is that if either side of the
4679 // complex number is infinity, then the whole value is
4680 // infinity, even if the other side is NaN. So the only case
4681 // we have to fix is the one in which both sides are NaN.
4682 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4683 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4684 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4686 bool is_infinity
= false;
4690 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4691 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4695 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4696 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4698 // If the left side is infinity, then the result is
4700 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4702 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4703 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4704 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4705 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4708 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4709 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4713 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4714 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4719 // If the right side is infinity, then the result is
4721 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4723 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4724 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4725 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4726 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4729 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4730 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4734 mpfr_set_ui(li
, 0, GMP_RNDN
);
4735 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4740 // If we got an overflow in the intermediate computations,
4741 // then the result is infinity.
4743 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4744 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4748 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4749 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4753 mpfr_set_ui(li
, 0, GMP_RNDN
);
4754 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4758 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4759 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4763 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4764 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4771 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4772 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4773 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4774 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4775 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4776 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4777 mpfr_set_inf(real
, mpfr_sgn(real
));
4778 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4795 // For complex division we want to avoid having an
4796 // intermediate overflow turn the whole result in a NaN. We
4797 // scale the values to try to avoid this.
4799 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4800 error_at(location
, "division by zero");
4806 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4807 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4810 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4814 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4815 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4817 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4819 ilogbw
= mpfr_get_exp(t
);
4820 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4821 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4826 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4827 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4828 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4830 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4831 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4832 mpfr_add(real
, real
, t
, GMP_RNDN
);
4833 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4834 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4836 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4837 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4838 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4839 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4840 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4842 // If we wind up with NaN on both sides, check whether we
4843 // should really have infinity. The rule is that if either
4844 // side of the complex number is infinity, then the whole
4845 // value is infinity, even if the other side is NaN. So the
4846 // only case we have to fix is the one in which both sides are
4848 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4849 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4850 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4852 if (mpfr_zero_p(denom
))
4854 mpfr_set_inf(real
, mpfr_sgn(rr
));
4855 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4856 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4857 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4859 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4860 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4862 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4863 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4866 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4867 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4871 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
4875 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
4877 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4878 mpfr_set_inf(real
, mpfr_sgn(t3
));
4880 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
4881 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
4882 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4883 mpfr_set_inf(imag
, mpfr_sgn(t3
));
4889 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
4890 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
4892 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4893 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
4896 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4897 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
4901 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
4905 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
4907 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4908 mpfr_set_ui(real
, 0, GMP_RNDN
);
4909 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
4911 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
4912 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
4913 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4914 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4915 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
4933 case OPERATOR_LSHIFT
:
4934 case OPERATOR_RSHIFT
:
4940 Type
* type
= left_type
;
4943 else if (type
!= right_type
&& right_type
!= NULL
)
4945 if (type
->is_abstract())
4947 else if (!right_type
->is_abstract())
4949 // This looks like a type error which should be diagnosed
4950 // elsewhere. Don't do anything here, to avoid an unhelpful
4951 // chain of error messages.
4956 if (type
!= NULL
&& !type
->is_abstract())
4958 if ((type
!= left_type
4959 && !Complex_expression::check_constant(left_real
, left_imag
,
4961 || (type
!= right_type
4962 && !Complex_expression::check_constant(right_real
, right_imag
,
4964 || !Complex_expression::check_constant(real
, imag
, type
,
4967 mpfr_set_ui(real
, 0, GMP_RNDN
);
4968 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4975 // Lower a binary expression. We have to evaluate constant
4976 // expressions now, in order to implement Go's unlimited precision
4980 Binary_expression::do_lower(Gogo
*, Named_object
*, int)
4982 source_location location
= this->location();
4983 Operator op
= this->op_
;
4984 Expression
* left
= this->left_
;
4985 Expression
* right
= this->right_
;
4987 const bool is_comparison
= (op
== OPERATOR_EQEQ
4988 || op
== OPERATOR_NOTEQ
4989 || op
== OPERATOR_LT
4990 || op
== OPERATOR_LE
4991 || op
== OPERATOR_GT
4992 || op
== OPERATOR_GE
);
4994 // Integer constant expressions.
5000 mpz_init(right_val
);
5002 if (left
->integer_constant_value(false, left_val
, &left_type
)
5003 && right
->integer_constant_value(false, right_val
, &right_type
))
5005 Expression
* ret
= NULL
;
5006 if (left_type
!= right_type
5007 && left_type
!= NULL
5008 && right_type
!= NULL
5009 && left_type
->base() != right_type
->base()
5010 && op
!= OPERATOR_LSHIFT
5011 && op
!= OPERATOR_RSHIFT
)
5013 // May be a type error--let it be diagnosed later.
5015 else if (is_comparison
)
5017 bool b
= Binary_expression::compare_integer(op
, left_val
,
5019 ret
= Expression::make_cast(Type::lookup_bool_type(),
5020 Expression::make_boolean(b
, location
),
5028 if (Binary_expression::eval_integer(op
, left_type
, left_val
,
5029 right_type
, right_val
,
5032 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
);
5034 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
5036 else if (left_type
== NULL
)
5038 else if (right_type
== NULL
)
5040 else if (!left_type
->is_abstract()
5041 && left_type
->named_type() != NULL
)
5043 else if (!right_type
->is_abstract()
5044 && right_type
->named_type() != NULL
)
5046 else if (!left_type
->is_abstract())
5048 else if (!right_type
->is_abstract())
5050 else if (left_type
->float_type() != NULL
)
5052 else if (right_type
->float_type() != NULL
)
5054 else if (left_type
->complex_type() != NULL
)
5056 else if (right_type
->complex_type() != NULL
)
5060 ret
= Expression::make_integer(&val
, type
, location
);
5068 mpz_clear(right_val
);
5069 mpz_clear(left_val
);
5073 mpz_clear(right_val
);
5074 mpz_clear(left_val
);
5077 // Floating point constant expressions.
5080 mpfr_init(left_val
);
5083 mpfr_init(right_val
);
5085 if (left
->float_constant_value(left_val
, &left_type
)
5086 && right
->float_constant_value(right_val
, &right_type
))
5088 Expression
* ret
= NULL
;
5089 if (left_type
!= right_type
5090 && left_type
!= NULL
5091 && right_type
!= NULL
5092 && left_type
->base() != right_type
->base()
5093 && op
!= OPERATOR_LSHIFT
5094 && op
!= OPERATOR_RSHIFT
)
5096 // May be a type error--let it be diagnosed later.
5098 else if (is_comparison
)
5100 bool b
= Binary_expression::compare_float(op
,
5104 left_val
, right_val
);
5105 ret
= Expression::make_boolean(b
, location
);
5112 if (Binary_expression::eval_float(op
, left_type
, left_val
,
5113 right_type
, right_val
, val
,
5116 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5117 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5119 if (left_type
== NULL
)
5121 else if (right_type
== NULL
)
5123 else if (!left_type
->is_abstract()
5124 && left_type
->named_type() != NULL
)
5126 else if (!right_type
->is_abstract()
5127 && right_type
->named_type() != NULL
)
5129 else if (!left_type
->is_abstract())
5131 else if (!right_type
->is_abstract())
5133 else if (left_type
->float_type() != NULL
)
5135 else if (right_type
->float_type() != NULL
)
5139 ret
= Expression::make_float(&val
, type
, location
);
5147 mpfr_clear(right_val
);
5148 mpfr_clear(left_val
);
5152 mpfr_clear(right_val
);
5153 mpfr_clear(left_val
);
5156 // Complex constant expressions.
5160 mpfr_init(left_real
);
5161 mpfr_init(left_imag
);
5166 mpfr_init(right_real
);
5167 mpfr_init(right_imag
);
5170 if (left
->complex_constant_value(left_real
, left_imag
, &left_type
)
5171 && right
->complex_constant_value(right_real
, right_imag
, &right_type
))
5173 Expression
* ret
= NULL
;
5174 if (left_type
!= right_type
5175 && left_type
!= NULL
5176 && right_type
!= NULL
5177 && left_type
->base() != right_type
->base())
5179 // May be a type error--let it be diagnosed later.
5181 else if (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
)
5183 bool b
= Binary_expression::compare_complex(op
,
5191 ret
= Expression::make_boolean(b
, location
);
5200 if (Binary_expression::eval_complex(op
, left_type
,
5201 left_real
, left_imag
,
5203 right_real
, right_imag
,
5207 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5208 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5210 if (left_type
== NULL
)
5212 else if (right_type
== NULL
)
5214 else if (!left_type
->is_abstract()
5215 && left_type
->named_type() != NULL
)
5217 else if (!right_type
->is_abstract()
5218 && right_type
->named_type() != NULL
)
5220 else if (!left_type
->is_abstract())
5222 else if (!right_type
->is_abstract())
5224 else if (left_type
->complex_type() != NULL
)
5226 else if (right_type
->complex_type() != NULL
)
5230 ret
= Expression::make_complex(&real
, &imag
, type
,
5239 mpfr_clear(left_real
);
5240 mpfr_clear(left_imag
);
5241 mpfr_clear(right_real
);
5242 mpfr_clear(right_imag
);
5247 mpfr_clear(left_real
);
5248 mpfr_clear(left_imag
);
5249 mpfr_clear(right_real
);
5250 mpfr_clear(right_imag
);
5253 // String constant expressions.
5254 if (op
== OPERATOR_PLUS
5255 && left
->type()->is_string_type()
5256 && right
->type()->is_string_type())
5258 std::string left_string
;
5259 std::string right_string
;
5260 if (left
->string_constant_value(&left_string
)
5261 && right
->string_constant_value(&right_string
))
5262 return Expression::make_string(left_string
+ right_string
, location
);
5268 // Return the integer constant value, if it has one.
5271 Binary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
5277 if (!this->left_
->integer_constant_value(iota_is_constant
, left_val
,
5280 mpz_clear(left_val
);
5285 mpz_init(right_val
);
5287 if (!this->right_
->integer_constant_value(iota_is_constant
, right_val
,
5290 mpz_clear(right_val
);
5291 mpz_clear(left_val
);
5296 if (left_type
!= right_type
5297 && left_type
!= NULL
5298 && right_type
!= NULL
5299 && left_type
->base() != right_type
->base()
5300 && this->op_
!= OPERATOR_RSHIFT
5301 && this->op_
!= OPERATOR_LSHIFT
)
5304 ret
= Binary_expression::eval_integer(this->op_
, left_type
, left_val
,
5305 right_type
, right_val
,
5306 this->location(), val
);
5308 mpz_clear(right_val
);
5309 mpz_clear(left_val
);
5317 // Return the floating point constant value, if it has one.
5320 Binary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
5323 mpfr_init(left_val
);
5325 if (!this->left_
->float_constant_value(left_val
, &left_type
))
5327 mpfr_clear(left_val
);
5332 mpfr_init(right_val
);
5334 if (!this->right_
->float_constant_value(right_val
, &right_type
))
5336 mpfr_clear(right_val
);
5337 mpfr_clear(left_val
);
5342 if (left_type
!= right_type
5343 && left_type
!= NULL
5344 && right_type
!= NULL
5345 && left_type
->base() != right_type
->base())
5348 ret
= Binary_expression::eval_float(this->op_
, left_type
, left_val
,
5349 right_type
, right_val
,
5350 val
, this->location());
5352 mpfr_clear(left_val
);
5353 mpfr_clear(right_val
);
5361 // Return the complex constant value, if it has one.
5364 Binary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
5369 mpfr_init(left_real
);
5370 mpfr_init(left_imag
);
5372 if (!this->left_
->complex_constant_value(left_real
, left_imag
, &left_type
))
5374 mpfr_clear(left_real
);
5375 mpfr_clear(left_imag
);
5381 mpfr_init(right_real
);
5382 mpfr_init(right_imag
);
5384 if (!this->right_
->complex_constant_value(right_real
, right_imag
,
5387 mpfr_clear(left_real
);
5388 mpfr_clear(left_imag
);
5389 mpfr_clear(right_real
);
5390 mpfr_clear(right_imag
);
5395 if (left_type
!= right_type
5396 && left_type
!= NULL
5397 && right_type
!= NULL
5398 && left_type
->base() != right_type
->base())
5401 ret
= Binary_expression::eval_complex(this->op_
, left_type
,
5402 left_real
, left_imag
,
5404 right_real
, right_imag
,
5407 mpfr_clear(left_real
);
5408 mpfr_clear(left_imag
);
5409 mpfr_clear(right_real
);
5410 mpfr_clear(right_imag
);
5418 // Note that the value is being discarded.
5421 Binary_expression::do_discarding_value()
5423 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5424 this->right_
->discarding_value();
5426 this->warn_about_unused_value();
5432 Binary_expression::do_type()
5434 if (this->classification() == EXPRESSION_ERROR
)
5435 return Type::make_error_type();
5440 case OPERATOR_ANDAND
:
5442 case OPERATOR_NOTEQ
:
5447 return Type::lookup_bool_type();
5450 case OPERATOR_MINUS
:
5457 case OPERATOR_BITCLEAR
:
5459 Type
* left_type
= this->left_
->type();
5460 Type
* right_type
= this->right_
->type();
5461 if (left_type
->is_error_type())
5463 else if (right_type
->is_error_type())
5465 else if (!Type::are_compatible_for_binop(left_type
, right_type
))
5467 this->report_error(_("incompatible types in binary expression"));
5468 return Type::make_error_type();
5470 else if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5472 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5474 else if (!left_type
->is_abstract())
5476 else if (!right_type
->is_abstract())
5478 else if (left_type
->complex_type() != NULL
)
5480 else if (right_type
->complex_type() != NULL
)
5482 else if (left_type
->float_type() != NULL
)
5484 else if (right_type
->float_type() != NULL
)
5490 case OPERATOR_LSHIFT
:
5491 case OPERATOR_RSHIFT
:
5492 return this->left_
->type();
5499 // Set type for a binary expression.
5502 Binary_expression::do_determine_type(const Type_context
* context
)
5504 Type
* tleft
= this->left_
->type();
5505 Type
* tright
= this->right_
->type();
5507 // Both sides should have the same type, except for the shift
5508 // operations. For a comparison, we should ignore the incoming
5511 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5512 || this->op_
== OPERATOR_RSHIFT
);
5514 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5515 || this->op_
== OPERATOR_NOTEQ
5516 || this->op_
== OPERATOR_LT
5517 || this->op_
== OPERATOR_LE
5518 || this->op_
== OPERATOR_GT
5519 || this->op_
== OPERATOR_GE
);
5521 Type_context
subcontext(*context
);
5525 // In a comparison, the context does not determine the types of
5527 subcontext
.type
= NULL
;
5530 // Set the context for the left hand operand.
5533 // The right hand operand plays no role in determining the type
5534 // of the left hand operand. A shift of an abstract integer in
5535 // a string context gets special treatment, which may be a
5537 if (subcontext
.type
!= NULL
5538 && subcontext
.type
->is_string_type()
5539 && tleft
->is_abstract())
5540 error_at(this->location(), "shift of non-integer operand");
5542 else if (!tleft
->is_abstract())
5543 subcontext
.type
= tleft
;
5544 else if (!tright
->is_abstract())
5545 subcontext
.type
= tright
;
5546 else if (subcontext
.type
== NULL
)
5548 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5549 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5550 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5552 // Both sides have an abstract integer, abstract float, or
5553 // abstract complex type. Just let CONTEXT determine
5554 // whether they may remain abstract or not.
5556 else if (tleft
->complex_type() != NULL
)
5557 subcontext
.type
= tleft
;
5558 else if (tright
->complex_type() != NULL
)
5559 subcontext
.type
= tright
;
5560 else if (tleft
->float_type() != NULL
)
5561 subcontext
.type
= tleft
;
5562 else if (tright
->float_type() != NULL
)
5563 subcontext
.type
= tright
;
5565 subcontext
.type
= tleft
;
5567 if (subcontext
.type
!= NULL
&& !context
->may_be_abstract
)
5568 subcontext
.type
= subcontext
.type
->make_non_abstract_type();
5571 this->left_
->determine_type(&subcontext
);
5573 // The context for the right hand operand is the same as for the
5574 // left hand operand, except for a shift operator.
5577 subcontext
.type
= Type::lookup_integer_type("uint");
5578 subcontext
.may_be_abstract
= false;
5581 this->right_
->determine_type(&subcontext
);
5584 // Report an error if the binary operator OP does not support TYPE.
5585 // Return whether the operation is OK. This should not be used for
5589 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5590 source_location location
)
5595 case OPERATOR_ANDAND
:
5596 if (!type
->is_boolean_type())
5598 error_at(location
, "expected boolean type");
5604 case OPERATOR_NOTEQ
:
5605 if (type
->integer_type() == NULL
5606 && type
->float_type() == NULL
5607 && type
->complex_type() == NULL
5608 && !type
->is_string_type()
5609 && type
->points_to() == NULL
5610 && !type
->is_nil_type()
5611 && !type
->is_boolean_type()
5612 && type
->interface_type() == NULL
5613 && (type
->array_type() == NULL
5614 || type
->array_type()->length() != NULL
)
5615 && type
->map_type() == NULL
5616 && type
->channel_type() == NULL
5617 && type
->function_type() == NULL
)
5620 ("expected integer, floating, complex, string, pointer, "
5621 "boolean, interface, slice, map, channel, "
5622 "or function type"));
5631 if (type
->integer_type() == NULL
5632 && type
->float_type() == NULL
5633 && !type
->is_string_type())
5635 error_at(location
, "expected integer, floating, or string type");
5641 case OPERATOR_PLUSEQ
:
5642 if (type
->integer_type() == NULL
5643 && type
->float_type() == NULL
5644 && type
->complex_type() == NULL
5645 && !type
->is_string_type())
5648 "expected integer, floating, complex, or string type");
5653 case OPERATOR_MINUS
:
5654 case OPERATOR_MINUSEQ
:
5656 case OPERATOR_MULTEQ
:
5658 case OPERATOR_DIVEQ
:
5659 if (type
->integer_type() == NULL
5660 && type
->float_type() == NULL
5661 && type
->complex_type() == NULL
)
5663 error_at(location
, "expected integer, floating, or complex type");
5669 case OPERATOR_MODEQ
:
5673 case OPERATOR_ANDEQ
:
5675 case OPERATOR_XOREQ
:
5676 case OPERATOR_BITCLEAR
:
5677 case OPERATOR_BITCLEAREQ
:
5678 if (type
->integer_type() == NULL
)
5680 error_at(location
, "expected integer type");
5695 Binary_expression::do_check_types(Gogo
*)
5697 if (this->classification() == EXPRESSION_ERROR
)
5700 Type
* left_type
= this->left_
->type();
5701 Type
* right_type
= this->right_
->type();
5702 if (left_type
->is_error_type() || right_type
->is_error_type())
5704 this->set_is_error();
5708 if (this->op_
== OPERATOR_EQEQ
5709 || this->op_
== OPERATOR_NOTEQ
5710 || this->op_
== OPERATOR_LT
5711 || this->op_
== OPERATOR_LE
5712 || this->op_
== OPERATOR_GT
5713 || this->op_
== OPERATOR_GE
)
5715 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5716 && !Type::are_assignable(right_type
, left_type
, NULL
))
5718 this->report_error(_("incompatible types in binary expression"));
5721 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5723 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5726 this->set_is_error();
5730 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5732 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5734 this->report_error(_("incompatible types in binary expression"));
5737 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5740 this->set_is_error();
5746 if (left_type
->integer_type() == NULL
)
5747 this->report_error(_("shift of non-integer operand"));
5749 if (!right_type
->is_abstract()
5750 && (right_type
->integer_type() == NULL
5751 || !right_type
->integer_type()->is_unsigned()))
5752 this->report_error(_("shift count not unsigned integer"));
5758 if (this->right_
->integer_constant_value(true, val
, &type
))
5760 if (mpz_sgn(val
) < 0)
5761 this->report_error(_("negative shift count"));
5768 // Get a tree for a binary expression.
5771 Binary_expression::do_get_tree(Translate_context
* context
)
5773 tree left
= this->left_
->get_tree(context
);
5774 tree right
= this->right_
->get_tree(context
);
5776 if (left
== error_mark_node
|| right
== error_mark_node
)
5777 return error_mark_node
;
5779 enum tree_code code
;
5780 bool use_left_type
= true;
5781 bool is_shift_op
= false;
5785 case OPERATOR_NOTEQ
:
5790 return Expression::comparison_tree(context
, this->op_
,
5791 this->left_
->type(), left
,
5792 this->right_
->type(), right
,
5796 code
= TRUTH_ORIF_EXPR
;
5797 use_left_type
= false;
5799 case OPERATOR_ANDAND
:
5800 code
= TRUTH_ANDIF_EXPR
;
5801 use_left_type
= false;
5806 case OPERATOR_MINUS
:
5810 code
= BIT_IOR_EXPR
;
5813 code
= BIT_XOR_EXPR
;
5820 Type
*t
= this->left_
->type();
5821 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5824 code
= TRUNC_DIV_EXPR
;
5828 code
= TRUNC_MOD_EXPR
;
5830 case OPERATOR_LSHIFT
:
5834 case OPERATOR_RSHIFT
:
5839 code
= BIT_AND_EXPR
;
5841 case OPERATOR_BITCLEAR
:
5842 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5843 code
= BIT_AND_EXPR
;
5849 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5851 if (this->left_
->type()->is_string_type())
5853 gcc_assert(this->op_
== OPERATOR_PLUS
);
5854 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5855 static tree string_plus_decl
;
5856 return Gogo::call_builtin(&string_plus_decl
,
5867 tree compute_type
= excess_precision_type(type
);
5868 if (compute_type
!= NULL_TREE
)
5870 left
= ::convert(compute_type
, left
);
5871 right
= ::convert(compute_type
, right
);
5874 tree eval_saved
= NULL_TREE
;
5877 // Make sure the values are evaluated.
5878 if (!DECL_P(left
) && TREE_SIDE_EFFECTS(left
))
5880 left
= save_expr(left
);
5883 if (!DECL_P(right
) && TREE_SIDE_EFFECTS(right
))
5885 right
= save_expr(right
);
5886 if (eval_saved
== NULL_TREE
)
5889 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5890 void_type_node
, eval_saved
, right
);
5894 tree ret
= fold_build2_loc(this->location(),
5896 compute_type
!= NULL_TREE
? compute_type
: type
,
5899 if (compute_type
!= NULL_TREE
)
5900 ret
= ::convert(type
, ret
);
5902 // In Go, a shift larger than the size of the type is well-defined.
5903 // This is not true in GENERIC, so we need to insert a conditional.
5906 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5907 gcc_assert(this->left_
->type()->integer_type() != NULL
);
5908 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5910 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5911 build_int_cst_type(TREE_TYPE(right
), bits
));
5913 tree overflow_result
= fold_convert_loc(this->location(),
5916 if (this->op_
== OPERATOR_RSHIFT
5917 && !this->left_
->type()->integer_type()->is_unsigned())
5919 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
5920 boolean_type_node
, left
,
5921 fold_convert_loc(this->location(),
5923 integer_zero_node
));
5924 tree neg_one
= fold_build2_loc(this->location(),
5925 MINUS_EXPR
, TREE_TYPE(left
),
5926 fold_convert_loc(this->location(),
5929 fold_convert_loc(this->location(),
5932 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
5933 TREE_TYPE(left
), neg
, neg_one
,
5937 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
5938 compare
, ret
, overflow_result
);
5940 if (eval_saved
!= NULL_TREE
)
5941 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5942 TREE_TYPE(ret
), eval_saved
, ret
);
5948 // Export a binary expression.
5951 Binary_expression::do_export(Export
* exp
) const
5953 exp
->write_c_string("(");
5954 this->left_
->export_expression(exp
);
5958 exp
->write_c_string(" || ");
5960 case OPERATOR_ANDAND
:
5961 exp
->write_c_string(" && ");
5964 exp
->write_c_string(" == ");
5966 case OPERATOR_NOTEQ
:
5967 exp
->write_c_string(" != ");
5970 exp
->write_c_string(" < ");
5973 exp
->write_c_string(" <= ");
5976 exp
->write_c_string(" > ");
5979 exp
->write_c_string(" >= ");
5982 exp
->write_c_string(" + ");
5984 case OPERATOR_MINUS
:
5985 exp
->write_c_string(" - ");
5988 exp
->write_c_string(" | ");
5991 exp
->write_c_string(" ^ ");
5994 exp
->write_c_string(" * ");
5997 exp
->write_c_string(" / ");
6000 exp
->write_c_string(" % ");
6002 case OPERATOR_LSHIFT
:
6003 exp
->write_c_string(" << ");
6005 case OPERATOR_RSHIFT
:
6006 exp
->write_c_string(" >> ");
6009 exp
->write_c_string(" & ");
6011 case OPERATOR_BITCLEAR
:
6012 exp
->write_c_string(" &^ ");
6017 this->right_
->export_expression(exp
);
6018 exp
->write_c_string(")");
6021 // Import a binary expression.
6024 Binary_expression::do_import(Import
* imp
)
6026 imp
->require_c_string("(");
6028 Expression
* left
= Expression::import_expression(imp
);
6031 if (imp
->match_c_string(" || "))
6036 else if (imp
->match_c_string(" && "))
6038 op
= OPERATOR_ANDAND
;
6041 else if (imp
->match_c_string(" == "))
6046 else if (imp
->match_c_string(" != "))
6048 op
= OPERATOR_NOTEQ
;
6051 else if (imp
->match_c_string(" < "))
6056 else if (imp
->match_c_string(" <= "))
6061 else if (imp
->match_c_string(" > "))
6066 else if (imp
->match_c_string(" >= "))
6071 else if (imp
->match_c_string(" + "))
6076 else if (imp
->match_c_string(" - "))
6078 op
= OPERATOR_MINUS
;
6081 else if (imp
->match_c_string(" | "))
6086 else if (imp
->match_c_string(" ^ "))
6091 else if (imp
->match_c_string(" * "))
6096 else if (imp
->match_c_string(" / "))
6101 else if (imp
->match_c_string(" % "))
6106 else if (imp
->match_c_string(" << "))
6108 op
= OPERATOR_LSHIFT
;
6111 else if (imp
->match_c_string(" >> "))
6113 op
= OPERATOR_RSHIFT
;
6116 else if (imp
->match_c_string(" & "))
6121 else if (imp
->match_c_string(" &^ "))
6123 op
= OPERATOR_BITCLEAR
;
6128 error_at(imp
->location(), "unrecognized binary operator");
6129 return Expression::make_error(imp
->location());
6132 Expression
* right
= Expression::import_expression(imp
);
6134 imp
->require_c_string(")");
6136 return Expression::make_binary(op
, left
, right
, imp
->location());
6139 // Make a binary expression.
6142 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6143 source_location location
)
6145 return new Binary_expression(op
, left
, right
, location
);
6148 // Implement a comparison.
6151 Expression::comparison_tree(Translate_context
* context
, Operator op
,
6152 Type
* left_type
, tree left_tree
,
6153 Type
* right_type
, tree right_tree
,
6154 source_location location
)
6156 enum tree_code code
;
6162 case OPERATOR_NOTEQ
:
6181 if (left_type
->is_string_type() && right_type
->is_string_type())
6183 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
6184 static tree string_compare_decl
;
6185 left_tree
= Gogo::call_builtin(&string_compare_decl
,
6194 right_tree
= build_int_cst_type(integer_type_node
, 0);
6196 else if ((left_type
->interface_type() != NULL
6197 && right_type
->interface_type() == NULL
6198 && !right_type
->is_nil_type())
6199 || (left_type
->interface_type() == NULL
6200 && !left_type
->is_nil_type()
6201 && right_type
->interface_type() != NULL
))
6203 // Comparing an interface value to a non-interface value.
6204 if (left_type
->interface_type() == NULL
)
6206 std::swap(left_type
, right_type
);
6207 std::swap(left_tree
, right_tree
);
6210 // The right operand is not an interface. We need to take its
6211 // address if it is not a pointer.
6214 if (right_type
->points_to() != NULL
)
6216 make_tmp
= NULL_TREE
;
6219 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
6221 make_tmp
= NULL_TREE
;
6222 arg
= build_fold_addr_expr_loc(location
, right_tree
);
6223 if (DECL_P(right_tree
))
6224 TREE_ADDRESSABLE(right_tree
) = 1;
6228 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
6229 get_name(right_tree
));
6230 DECL_IGNORED_P(tmp
) = 0;
6231 DECL_INITIAL(tmp
) = right_tree
;
6232 TREE_ADDRESSABLE(tmp
) = 1;
6233 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6234 SET_EXPR_LOCATION(make_tmp
, location
);
6235 arg
= build_fold_addr_expr_loc(location
, tmp
);
6237 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
6239 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
6241 if (left_type
->interface_type()->is_empty())
6243 static tree empty_interface_value_compare_decl
;
6244 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6246 "__go_empty_interface_value_compare",
6249 TREE_TYPE(left_tree
),
6251 TREE_TYPE(descriptor
),
6255 if (left_tree
== error_mark_node
)
6256 return error_mark_node
;
6257 // This can panic if the type is not comparable.
6258 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6262 static tree interface_value_compare_decl
;
6263 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6265 "__go_interface_value_compare",
6268 TREE_TYPE(left_tree
),
6270 TREE_TYPE(descriptor
),
6274 if (left_tree
== error_mark_node
)
6275 return error_mark_node
;
6276 // This can panic if the type is not comparable.
6277 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6279 right_tree
= build_int_cst_type(integer_type_node
, 0);
6281 if (make_tmp
!= NULL_TREE
)
6282 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6285 else if (left_type
->interface_type() != NULL
6286 && right_type
->interface_type() != NULL
)
6288 if (left_type
->interface_type()->is_empty()
6289 && right_type
->interface_type()->is_empty())
6291 static tree empty_interface_compare_decl
;
6292 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6294 "__go_empty_interface_compare",
6297 TREE_TYPE(left_tree
),
6299 TREE_TYPE(right_tree
),
6301 if (left_tree
== error_mark_node
)
6302 return error_mark_node
;
6303 // This can panic if the type is uncomparable.
6304 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6306 else if (!left_type
->interface_type()->is_empty()
6307 && !right_type
->interface_type()->is_empty())
6309 static tree interface_compare_decl
;
6310 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6312 "__go_interface_compare",
6315 TREE_TYPE(left_tree
),
6317 TREE_TYPE(right_tree
),
6319 if (left_tree
== error_mark_node
)
6320 return error_mark_node
;
6321 // This can panic if the type is uncomparable.
6322 TREE_NOTHROW(interface_compare_decl
) = 0;
6326 if (left_type
->interface_type()->is_empty())
6328 gcc_assert(op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
);
6329 std::swap(left_type
, right_type
);
6330 std::swap(left_tree
, right_tree
);
6332 gcc_assert(!left_type
->interface_type()->is_empty());
6333 gcc_assert(right_type
->interface_type()->is_empty());
6334 static tree interface_empty_compare_decl
;
6335 left_tree
= Gogo::call_builtin(&interface_empty_compare_decl
,
6337 "__go_interface_empty_compare",
6340 TREE_TYPE(left_tree
),
6342 TREE_TYPE(right_tree
),
6344 if (left_tree
== error_mark_node
)
6345 return error_mark_node
;
6346 // This can panic if the type is uncomparable.
6347 TREE_NOTHROW(interface_empty_compare_decl
) = 0;
6350 right_tree
= build_int_cst_type(integer_type_node
, 0);
6353 if (left_type
->is_nil_type()
6354 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6356 std::swap(left_type
, right_type
);
6357 std::swap(left_tree
, right_tree
);
6360 if (right_type
->is_nil_type())
6362 if (left_type
->array_type() != NULL
6363 && left_type
->array_type()->length() == NULL
)
6365 Array_type
* at
= left_type
->array_type();
6366 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6367 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6369 else if (left_type
->interface_type() != NULL
)
6371 // An interface is nil if the first field is nil.
6372 tree left_type_tree
= TREE_TYPE(left_tree
);
6373 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6374 tree field
= TYPE_FIELDS(left_type_tree
);
6375 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6377 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6381 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6382 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6386 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6387 return error_mark_node
;
6389 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6390 if (CAN_HAVE_LOCATION_P(ret
))
6391 SET_EXPR_LOCATION(ret
, location
);
6395 // Class Bound_method_expression.
6400 Bound_method_expression::do_traverse(Traverse
* traverse
)
6402 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6403 return TRAVERSE_EXIT
;
6404 return Expression::traverse(&this->method_
, traverse
);
6407 // Return the type of a bound method expression. The type of this
6408 // object is really the type of the method with no receiver. We
6409 // should be able to get away with just returning the type of the
6413 Bound_method_expression::do_type()
6415 return this->method_
->type();
6418 // Determine the types of a method expression.
6421 Bound_method_expression::do_determine_type(const Type_context
*)
6423 this->method_
->determine_type_no_context();
6424 Type
* mtype
= this->method_
->type();
6425 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6426 if (fntype
== NULL
|| !fntype
->is_method())
6427 this->expr_
->determine_type_no_context();
6430 Type_context
subcontext(fntype
->receiver()->type(), false);
6431 this->expr_
->determine_type(&subcontext
);
6435 // Check the types of a method expression.
6438 Bound_method_expression::do_check_types(Gogo
*)
6440 Type
* type
= this->method_
->type()->deref();
6442 || type
->function_type() == NULL
6443 || !type
->function_type()->is_method())
6444 this->report_error(_("object is not a method"));
6447 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6448 Type
* etype
= (this->expr_type_
!= NULL
6450 : this->expr_
->type());
6451 etype
= etype
->deref();
6452 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6453 this->report_error(_("method type does not match object type"));
6457 // Get the tree for a method expression. There is no standard tree
6458 // representation for this. The only places it may currently be used
6459 // are in a Call_expression or a Go_statement, which will take it
6460 // apart directly. So this has nothing to do at present.
6463 Bound_method_expression::do_get_tree(Translate_context
*)
6465 error_at(this->location(), "reference to method other than calling it");
6466 return error_mark_node
;
6469 // Make a method expression.
6471 Bound_method_expression
*
6472 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6473 source_location location
)
6475 return new Bound_method_expression(expr
, method
, location
);
6478 // Class Builtin_call_expression. This is used for a call to a
6479 // builtin function.
6481 class Builtin_call_expression
: public Call_expression
6484 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6485 bool is_varargs
, source_location location
);
6488 // This overrides Call_expression::do_lower.
6490 do_lower(Gogo
*, Named_object
*, int);
6493 do_is_constant() const;
6496 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6499 do_float_constant_value(mpfr_t
, Type
**) const;
6502 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6508 do_determine_type(const Type_context
*);
6511 do_check_types(Gogo
*);
6516 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6517 this->args()->copy(),
6523 do_get_tree(Translate_context
*);
6526 do_export(Export
*) const;
6529 do_is_recover_call() const;
6532 do_set_recover_arg(Expression
*);
6535 // The builtin functions.
6536 enum Builtin_function_code
6540 // Predeclared builtin functions.
6557 // Builtin functions from the unsafe package.
6570 real_imag_type(Type
*);
6573 complex_type(Type
*);
6575 // A pointer back to the general IR structure. This avoids a global
6576 // variable, or passing it around everywhere.
6578 // The builtin function being called.
6579 Builtin_function_code code_
;
6580 // Used to stop endless loops when the length of an array uses len
6581 // or cap of the array itself.
6585 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6587 Expression_list
* args
,
6589 source_location location
)
6590 : Call_expression(fn
, args
, is_varargs
, location
),
6591 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false)
6593 Func_expression
* fnexp
= this->fn()->func_expression();
6594 gcc_assert(fnexp
!= NULL
);
6595 const std::string
& name(fnexp
->named_object()->name());
6596 if (name
== "append")
6597 this->code_
= BUILTIN_APPEND
;
6598 else if (name
== "cap")
6599 this->code_
= BUILTIN_CAP
;
6600 else if (name
== "close")
6601 this->code_
= BUILTIN_CLOSE
;
6602 else if (name
== "closed")
6603 this->code_
= BUILTIN_CLOSED
;
6604 else if (name
== "complex")
6605 this->code_
= BUILTIN_COMPLEX
;
6606 else if (name
== "copy")
6607 this->code_
= BUILTIN_COPY
;
6608 else if (name
== "imag")
6609 this->code_
= BUILTIN_IMAG
;
6610 else if (name
== "len")
6611 this->code_
= BUILTIN_LEN
;
6612 else if (name
== "make")
6613 this->code_
= BUILTIN_MAKE
;
6614 else if (name
== "new")
6615 this->code_
= BUILTIN_NEW
;
6616 else if (name
== "panic")
6617 this->code_
= BUILTIN_PANIC
;
6618 else if (name
== "print")
6619 this->code_
= BUILTIN_PRINT
;
6620 else if (name
== "println")
6621 this->code_
= BUILTIN_PRINTLN
;
6622 else if (name
== "real")
6623 this->code_
= BUILTIN_REAL
;
6624 else if (name
== "recover")
6625 this->code_
= BUILTIN_RECOVER
;
6626 else if (name
== "Alignof")
6627 this->code_
= BUILTIN_ALIGNOF
;
6628 else if (name
== "Offsetof")
6629 this->code_
= BUILTIN_OFFSETOF
;
6630 else if (name
== "Sizeof")
6631 this->code_
= BUILTIN_SIZEOF
;
6636 // Return whether this is a call to recover. This is a virtual
6637 // function called from the parent class.
6640 Builtin_call_expression::do_is_recover_call() const
6642 if (this->classification() == EXPRESSION_ERROR
)
6644 return this->code_
== BUILTIN_RECOVER
;
6647 // Set the argument for a call to recover.
6650 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6652 const Expression_list
* args
= this->args();
6653 gcc_assert(args
== NULL
|| args
->empty());
6654 Expression_list
* new_args
= new Expression_list();
6655 new_args
->push_back(arg
);
6656 this->set_args(new_args
);
6659 // A traversal class which looks for a call expression.
6661 class Find_call_expression
: public Traverse
6664 Find_call_expression()
6665 : Traverse(traverse_expressions
),
6670 expression(Expression
**);
6674 { return this->found_
; }
6681 Find_call_expression::expression(Expression
** pexpr
)
6683 if ((*pexpr
)->call_expression() != NULL
)
6685 this->found_
= true;
6686 return TRAVERSE_EXIT
;
6688 return TRAVERSE_CONTINUE
;
6691 // Lower a builtin call expression. This turns new and make into
6692 // specific expressions. We also convert to a constant if we can.
6695 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6697 if (this->code_
== BUILTIN_NEW
)
6699 const Expression_list
* args
= this->args();
6700 if (args
== NULL
|| args
->size() < 1)
6701 this->report_error(_("not enough arguments"));
6702 else if (args
->size() > 1)
6703 this->report_error(_("too many arguments"));
6706 Expression
* arg
= args
->front();
6707 if (!arg
->is_type_expression())
6709 error_at(arg
->location(), "expected type");
6710 this->set_is_error();
6713 return Expression::make_allocation(arg
->type(), this->location());
6716 else if (this->code_
== BUILTIN_MAKE
)
6718 const Expression_list
* args
= this->args();
6719 if (args
== NULL
|| args
->size() < 1)
6720 this->report_error(_("not enough arguments"));
6723 Expression
* arg
= args
->front();
6724 if (!arg
->is_type_expression())
6726 error_at(arg
->location(), "expected type");
6727 this->set_is_error();
6731 Expression_list
* newargs
;
6732 if (args
->size() == 1)
6736 newargs
= new Expression_list();
6737 Expression_list::const_iterator p
= args
->begin();
6739 for (; p
!= args
->end(); ++p
)
6740 newargs
->push_back(*p
);
6742 return Expression::make_make(arg
->type(), newargs
,
6747 else if (this->is_constant())
6749 // We can only lower len and cap if there are no function calls
6750 // in the arguments. Otherwise we have to make the call.
6751 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6753 Expression
* arg
= this->one_arg();
6754 if (!arg
->is_constant())
6756 Find_call_expression find_call
;
6757 Expression::traverse(&arg
, &find_call
);
6758 if (find_call
.found())
6766 if (this->integer_constant_value(true, ival
, &type
))
6768 Expression
* ret
= Expression::make_integer(&ival
, type
,
6777 if (this->float_constant_value(rval
, &type
))
6779 Expression
* ret
= Expression::make_float(&rval
, type
,
6787 if (this->complex_constant_value(rval
, imag
, &type
))
6789 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6798 else if (this->code_
== BUILTIN_RECOVER
)
6800 if (function
!= NULL
)
6801 function
->func_value()->set_calls_recover();
6804 // Calling recover outside of a function always returns the
6805 // nil empty interface.
6806 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6807 return Expression::make_cast(eface
,
6808 Expression::make_nil(this->location()),
6812 else if (this->code_
== BUILTIN_APPEND
)
6814 // Lower the varargs.
6815 const Expression_list
* args
= this->args();
6816 if (args
== NULL
|| args
->empty())
6818 Type
* slice_type
= args
->front()->type();
6819 if (!slice_type
->is_open_array_type())
6821 error_at(args
->front()->location(), "argument 1 must be a slice");
6822 this->set_is_error();
6825 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6831 // Return the type of the real or imag functions, given the type of
6832 // the argument. We need to map complex to float, complex64 to
6833 // float32, and complex128 to float64, so it has to be done by name.
6834 // This returns NULL if it can't figure out the type.
6837 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6839 if (arg_type
== NULL
|| arg_type
->is_abstract())
6841 Named_type
* nt
= arg_type
->named_type();
6844 while (nt
->real_type()->named_type() != NULL
)
6845 nt
= nt
->real_type()->named_type();
6846 if (nt
->name() == "complex64")
6847 return Type::lookup_float_type("float32");
6848 else if (nt
->name() == "complex128")
6849 return Type::lookup_float_type("float64");
6854 // Return the type of the complex function, given the type of one of the
6855 // argments. Like real_imag_type, we have to map by name.
6858 Builtin_call_expression::complex_type(Type
* arg_type
)
6860 if (arg_type
== NULL
|| arg_type
->is_abstract())
6862 Named_type
* nt
= arg_type
->named_type();
6865 while (nt
->real_type()->named_type() != NULL
)
6866 nt
= nt
->real_type()->named_type();
6867 if (nt
->name() == "float32")
6868 return Type::lookup_complex_type("complex64");
6869 else if (nt
->name() == "float64")
6870 return Type::lookup_complex_type("complex128");
6875 // Return a single argument, or NULL if there isn't one.
6878 Builtin_call_expression::one_arg() const
6880 const Expression_list
* args
= this->args();
6881 if (args
->size() != 1)
6883 return args
->front();
6886 // Return whether this is constant: len of a string, or len or cap of
6887 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6890 Builtin_call_expression::do_is_constant() const
6892 switch (this->code_
)
6900 Expression
* arg
= this->one_arg();
6903 Type
* arg_type
= arg
->type();
6905 if (arg_type
->points_to() != NULL
6906 && arg_type
->points_to()->array_type() != NULL
6907 && !arg_type
->points_to()->is_open_array_type())
6908 arg_type
= arg_type
->points_to();
6910 if (arg_type
->array_type() != NULL
6911 && arg_type
->array_type()->length() != NULL
)
6914 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6917 bool ret
= arg
->is_constant();
6918 this->seen_
= false;
6924 case BUILTIN_SIZEOF
:
6925 case BUILTIN_ALIGNOF
:
6926 return this->one_arg() != NULL
;
6928 case BUILTIN_OFFSETOF
:
6930 Expression
* arg
= this->one_arg();
6933 return arg
->field_reference_expression() != NULL
;
6936 case BUILTIN_COMPLEX
:
6938 const Expression_list
* args
= this->args();
6939 if (args
!= NULL
&& args
->size() == 2)
6940 return args
->front()->is_constant() && args
->back()->is_constant();
6947 Expression
* arg
= this->one_arg();
6948 return arg
!= NULL
&& arg
->is_constant();
6958 // Return an integer constant value if possible.
6961 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
6965 if (this->code_
== BUILTIN_LEN
6966 || this->code_
== BUILTIN_CAP
)
6968 Expression
* arg
= this->one_arg();
6971 Type
* arg_type
= arg
->type();
6973 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6976 if (arg
->string_constant_value(&sval
))
6978 mpz_set_ui(val
, sval
.length());
6979 *ptype
= Type::lookup_integer_type("int");
6984 if (arg_type
->points_to() != NULL
6985 && arg_type
->points_to()->array_type() != NULL
6986 && !arg_type
->points_to()->is_open_array_type())
6987 arg_type
= arg_type
->points_to();
6989 if (arg_type
->array_type() != NULL
6990 && arg_type
->array_type()->length() != NULL
)
6994 Expression
* e
= arg_type
->array_type()->length();
6996 bool r
= e
->integer_constant_value(iota_is_constant
, val
, ptype
);
6997 this->seen_
= false;
7000 *ptype
= Type::lookup_integer_type("int");
7005 else if (this->code_
== BUILTIN_SIZEOF
7006 || this->code_
== BUILTIN_ALIGNOF
)
7008 Expression
* arg
= this->one_arg();
7011 Type
* arg_type
= arg
->type();
7012 if (arg_type
->is_error_type() || arg_type
->is_undefined())
7014 if (arg_type
->is_abstract())
7016 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
7017 if (arg_type_tree
== error_mark_node
)
7019 unsigned long val_long
;
7020 if (this->code_
== BUILTIN_SIZEOF
)
7022 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
7023 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
7024 if (TREE_INT_CST_HIGH(type_size
) != 0)
7026 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
7027 val_long
= static_cast<unsigned long>(val_wide
);
7028 if (val_long
!= val_wide
)
7031 else if (this->code_
== BUILTIN_ALIGNOF
)
7033 if (arg
->field_reference_expression() == NULL
)
7034 val_long
= go_type_alignment(arg_type_tree
);
7037 // Calling unsafe.Alignof(s.f) returns the alignment of
7038 // the type of f when it is used as a field in a struct.
7039 val_long
= go_field_alignment(arg_type_tree
);
7044 mpz_set_ui(val
, val_long
);
7048 else if (this->code_
== BUILTIN_OFFSETOF
)
7050 Expression
* arg
= this->one_arg();
7053 Field_reference_expression
* farg
= arg
->field_reference_expression();
7056 Expression
* struct_expr
= farg
->expr();
7057 Type
* st
= struct_expr
->type();
7058 if (st
->struct_type() == NULL
)
7060 tree struct_tree
= st
->get_tree(this->gogo_
);
7061 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
7062 tree field
= TYPE_FIELDS(struct_tree
);
7063 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
7065 field
= DECL_CHAIN(field
);
7066 gcc_assert(field
!= NULL_TREE
);
7068 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
7069 if (offset_wide
< 0)
7071 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
7072 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
7074 mpz_set_ui(val
, offset_long
);
7080 // Return a floating point constant value if possible.
7083 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
7086 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
7088 Expression
* arg
= this->one_arg();
7099 if (arg
->complex_constant_value(real
, imag
, &type
))
7101 if (this->code_
== BUILTIN_REAL
)
7102 mpfr_set(val
, real
, GMP_RNDN
);
7104 mpfr_set(val
, imag
, GMP_RNDN
);
7105 *ptype
= Builtin_call_expression::real_imag_type(type
);
7117 // Return a complex constant value if possible.
7120 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
7123 if (this->code_
== BUILTIN_COMPLEX
)
7125 const Expression_list
* args
= this->args();
7126 if (args
== NULL
|| args
->size() != 2)
7132 if (!args
->front()->float_constant_value(r
, &rtype
))
7143 if (args
->back()->float_constant_value(i
, &itype
)
7144 && Type::are_identical(rtype
, itype
, false, NULL
))
7146 mpfr_set(real
, r
, GMP_RNDN
);
7147 mpfr_set(imag
, i
, GMP_RNDN
);
7148 *ptype
= Builtin_call_expression::complex_type(rtype
);
7164 Builtin_call_expression::do_type()
7166 switch (this->code_
)
7168 case BUILTIN_INVALID
:
7175 const Expression_list
* args
= this->args();
7176 if (args
== NULL
|| args
->empty())
7177 return Type::make_error_type();
7178 return Type::make_pointer_type(args
->front()->type());
7184 case BUILTIN_ALIGNOF
:
7185 case BUILTIN_OFFSETOF
:
7186 case BUILTIN_SIZEOF
:
7187 return Type::lookup_integer_type("int");
7192 case BUILTIN_PRINTLN
:
7193 return Type::make_void_type();
7195 case BUILTIN_CLOSED
:
7196 return Type::lookup_bool_type();
7198 case BUILTIN_RECOVER
:
7199 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7201 case BUILTIN_APPEND
:
7203 const Expression_list
* args
= this->args();
7204 if (args
== NULL
|| args
->empty())
7205 return Type::make_error_type();
7206 return args
->front()->type();
7212 Expression
* arg
= this->one_arg();
7214 return Type::make_error_type();
7215 Type
* t
= arg
->type();
7216 if (t
->is_abstract())
7217 t
= t
->make_non_abstract_type();
7218 t
= Builtin_call_expression::real_imag_type(t
);
7220 t
= Type::make_error_type();
7224 case BUILTIN_COMPLEX
:
7226 const Expression_list
* args
= this->args();
7227 if (args
== NULL
|| args
->size() != 2)
7228 return Type::make_error_type();
7229 Type
* t
= args
->front()->type();
7230 if (t
->is_abstract())
7232 t
= args
->back()->type();
7233 if (t
->is_abstract())
7234 t
= t
->make_non_abstract_type();
7236 t
= Builtin_call_expression::complex_type(t
);
7238 t
= Type::make_error_type();
7244 // Determine the type.
7247 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7249 this->fn()->determine_type_no_context();
7251 const Expression_list
* args
= this->args();
7254 Type
* arg_type
= NULL
;
7255 switch (this->code_
)
7258 case BUILTIN_PRINTLN
:
7259 // Do not force a large integer constant to "int".
7265 arg_type
= Builtin_call_expression::complex_type(context
->type
);
7269 case BUILTIN_COMPLEX
:
7271 // For the complex function the type of one operand can
7272 // determine the type of the other, as in a binary expression.
7273 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7274 if (args
!= NULL
&& args
->size() == 2)
7276 Type
* t1
= args
->front()->type();
7277 Type
* t2
= args
->front()->type();
7278 if (!t1
->is_abstract())
7280 else if (!t2
->is_abstract())
7294 for (Expression_list::const_iterator pa
= args
->begin();
7298 Type_context subcontext
;
7299 subcontext
.type
= arg_type
;
7303 // We want to print large constants, we so can't just
7304 // use the appropriate nonabstract type. Use uint64 for
7305 // an integer if we know it is nonnegative, otherwise
7306 // use int64 for a integer, otherwise use float64 for a
7307 // float or complex128 for a complex.
7308 Type
* want_type
= NULL
;
7309 Type
* atype
= (*pa
)->type();
7310 if (atype
->is_abstract())
7312 if (atype
->integer_type() != NULL
)
7317 if (this->integer_constant_value(true, val
, &dummy
)
7318 && mpz_sgn(val
) >= 0)
7319 want_type
= Type::lookup_integer_type("uint64");
7321 want_type
= Type::lookup_integer_type("int64");
7324 else if (atype
->float_type() != NULL
)
7325 want_type
= Type::lookup_float_type("float64");
7326 else if (atype
->complex_type() != NULL
)
7327 want_type
= Type::lookup_complex_type("complex128");
7328 else if (atype
->is_abstract_string_type())
7329 want_type
= Type::lookup_string_type();
7330 else if (atype
->is_abstract_boolean_type())
7331 want_type
= Type::lookup_bool_type();
7334 subcontext
.type
= want_type
;
7338 (*pa
)->determine_type(&subcontext
);
7343 // If there is exactly one argument, return true. Otherwise give an
7344 // error message and return false.
7347 Builtin_call_expression::check_one_arg()
7349 const Expression_list
* args
= this->args();
7350 if (args
== NULL
|| args
->size() < 1)
7352 this->report_error(_("not enough arguments"));
7355 else if (args
->size() > 1)
7357 this->report_error(_("too many arguments"));
7360 if (args
->front()->is_error_expression()
7361 || args
->front()->type()->is_error_type()
7362 || args
->front()->type()->is_undefined())
7364 this->set_is_error();
7370 // Check argument types for a builtin function.
7373 Builtin_call_expression::do_check_types(Gogo
*)
7375 switch (this->code_
)
7377 case BUILTIN_INVALID
:
7385 // The single argument may be either a string or an array or a
7386 // map or a channel, or a pointer to a closed array.
7387 if (this->check_one_arg())
7389 Type
* arg_type
= this->one_arg()->type();
7390 if (arg_type
->points_to() != NULL
7391 && arg_type
->points_to()->array_type() != NULL
7392 && !arg_type
->points_to()->is_open_array_type())
7393 arg_type
= arg_type
->points_to();
7394 if (this->code_
== BUILTIN_CAP
)
7396 if (!arg_type
->is_error_type()
7397 && arg_type
->array_type() == NULL
7398 && arg_type
->channel_type() == NULL
)
7399 this->report_error(_("argument must be array or slice "
7404 if (!arg_type
->is_error_type()
7405 && !arg_type
->is_string_type()
7406 && arg_type
->array_type() == NULL
7407 && arg_type
->map_type() == NULL
7408 && arg_type
->channel_type() == NULL
)
7409 this->report_error(_("argument must be string or "
7410 "array or slice or map or channel"));
7417 case BUILTIN_PRINTLN
:
7419 const Expression_list
* args
= this->args();
7422 if (this->code_
== BUILTIN_PRINT
)
7423 warning_at(this->location(), 0,
7424 "no arguments for builtin function %<%s%>",
7425 (this->code_
== BUILTIN_PRINT
7431 for (Expression_list::const_iterator p
= args
->begin();
7435 Type
* type
= (*p
)->type();
7436 if (type
->is_error_type()
7437 || type
->is_string_type()
7438 || type
->integer_type() != NULL
7439 || type
->float_type() != NULL
7440 || type
->complex_type() != NULL
7441 || type
->is_boolean_type()
7442 || type
->points_to() != NULL
7443 || type
->interface_type() != NULL
7444 || type
->channel_type() != NULL
7445 || type
->map_type() != NULL
7446 || type
->function_type() != NULL
7447 || type
->is_open_array_type())
7450 this->report_error(_("unsupported argument type to "
7451 "builtin function"));
7458 case BUILTIN_CLOSED
:
7459 if (this->check_one_arg())
7461 if (this->one_arg()->type()->channel_type() == NULL
)
7462 this->report_error(_("argument must be channel"));
7467 case BUILTIN_SIZEOF
:
7468 case BUILTIN_ALIGNOF
:
7469 this->check_one_arg();
7472 case BUILTIN_RECOVER
:
7473 if (this->args() != NULL
&& !this->args()->empty())
7474 this->report_error(_("too many arguments"));
7477 case BUILTIN_OFFSETOF
:
7478 if (this->check_one_arg())
7480 Expression
* arg
= this->one_arg();
7481 if (arg
->field_reference_expression() == NULL
)
7482 this->report_error(_("argument must be a field reference"));
7488 const Expression_list
* args
= this->args();
7489 if (args
== NULL
|| args
->size() < 2)
7491 this->report_error(_("not enough arguments"));
7494 else if (args
->size() > 2)
7496 this->report_error(_("too many arguments"));
7499 Type
* arg1_type
= args
->front()->type();
7500 Type
* arg2_type
= args
->back()->type();
7501 if (arg1_type
->is_error_type() || arg2_type
->is_error_type())
7505 if (arg1_type
->is_open_array_type())
7506 e1
= arg1_type
->array_type()->element_type();
7509 this->report_error(_("left argument must be a slice"));
7514 if (arg2_type
->is_open_array_type())
7515 e2
= arg2_type
->array_type()->element_type();
7516 else if (arg2_type
->is_string_type())
7517 e2
= Type::lookup_integer_type("uint8");
7520 this->report_error(_("right argument must be a slice or a string"));
7524 if (!Type::are_identical(e1
, e2
, true, NULL
))
7525 this->report_error(_("element types must be the same"));
7529 case BUILTIN_APPEND
:
7531 const Expression_list
* args
= this->args();
7532 if (args
== NULL
|| args
->size() < 2)
7534 this->report_error(_("not enough arguments"));
7537 if (args
->size() > 2)
7539 this->report_error(_("too many arguments"));
7543 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7547 this->report_error(_("arguments 1 and 2 have different types"));
7550 error_at(this->location(),
7551 "arguments 1 and 2 have different types (%s)",
7553 this->set_is_error();
7561 if (this->check_one_arg())
7563 if (this->one_arg()->type()->complex_type() == NULL
)
7564 this->report_error(_("argument must have complex type"));
7568 case BUILTIN_COMPLEX
:
7570 const Expression_list
* args
= this->args();
7571 if (args
== NULL
|| args
->size() < 2)
7572 this->report_error(_("not enough arguments"));
7573 else if (args
->size() > 2)
7574 this->report_error(_("too many arguments"));
7575 else if (args
->front()->is_error_expression()
7576 || args
->front()->type()->is_error_type()
7577 || args
->back()->is_error_expression()
7578 || args
->back()->type()->is_error_type())
7579 this->set_is_error();
7580 else if (!Type::are_identical(args
->front()->type(),
7581 args
->back()->type(), true, NULL
))
7582 this->report_error(_("complex arguments must have identical types"));
7583 else if (args
->front()->type()->float_type() == NULL
)
7584 this->report_error(_("complex arguments must have "
7585 "floating-point type"));
7594 // Return the tree for a builtin function.
7597 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7599 Gogo
* gogo
= context
->gogo();
7600 source_location location
= this->location();
7601 switch (this->code_
)
7603 case BUILTIN_INVALID
:
7611 const Expression_list
* args
= this->args();
7612 gcc_assert(args
!= NULL
&& args
->size() == 1);
7613 Expression
* arg
= *args
->begin();
7614 Type
* arg_type
= arg
->type();
7618 gcc_assert(saw_errors());
7619 return error_mark_node
;
7623 tree arg_tree
= arg
->get_tree(context
);
7625 this->seen_
= false;
7627 if (arg_tree
== error_mark_node
)
7628 return error_mark_node
;
7630 if (arg_type
->points_to() != NULL
)
7632 arg_type
= arg_type
->points_to();
7633 gcc_assert(arg_type
->array_type() != NULL
7634 && !arg_type
->is_open_array_type());
7635 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7636 arg_tree
= build_fold_indirect_ref(arg_tree
);
7640 if (this->code_
== BUILTIN_LEN
)
7642 if (arg_type
->is_string_type())
7643 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7644 else if (arg_type
->array_type() != NULL
)
7648 gcc_assert(saw_errors());
7649 return error_mark_node
;
7652 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7653 this->seen_
= false;
7655 else if (arg_type
->map_type() != NULL
)
7657 static tree map_len_fndecl
;
7658 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7663 arg_type
->get_tree(gogo
),
7666 else if (arg_type
->channel_type() != NULL
)
7668 static tree chan_len_fndecl
;
7669 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7674 arg_type
->get_tree(gogo
),
7682 if (arg_type
->array_type() != NULL
)
7686 gcc_assert(saw_errors());
7687 return error_mark_node
;
7690 val_tree
= arg_type
->array_type()->capacity_tree(gogo
,
7692 this->seen_
= false;
7694 else if (arg_type
->channel_type() != NULL
)
7696 static tree chan_cap_fndecl
;
7697 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7702 arg_type
->get_tree(gogo
),
7709 if (val_tree
== error_mark_node
)
7710 return error_mark_node
;
7712 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7713 if (type_tree
== TREE_TYPE(val_tree
))
7716 return fold(convert_to_integer(type_tree
, val_tree
));
7720 case BUILTIN_PRINTLN
:
7722 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7723 tree stmt_list
= NULL_TREE
;
7725 const Expression_list
* call_args
= this->args();
7726 if (call_args
!= NULL
)
7728 for (Expression_list::const_iterator p
= call_args
->begin();
7729 p
!= call_args
->end();
7732 if (is_ln
&& p
!= call_args
->begin())
7734 static tree print_space_fndecl
;
7735 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7740 if (call
== error_mark_node
)
7741 return error_mark_node
;
7742 append_to_statement_list(call
, &stmt_list
);
7745 Type
* type
= (*p
)->type();
7747 tree arg
= (*p
)->get_tree(context
);
7748 if (arg
== error_mark_node
)
7749 return error_mark_node
;
7753 if (type
->is_string_type())
7755 static tree print_string_fndecl
;
7756 pfndecl
= &print_string_fndecl
;
7757 fnname
= "__go_print_string";
7759 else if (type
->integer_type() != NULL
7760 && type
->integer_type()->is_unsigned())
7762 static tree print_uint64_fndecl
;
7763 pfndecl
= &print_uint64_fndecl
;
7764 fnname
= "__go_print_uint64";
7765 Type
* itype
= Type::lookup_integer_type("uint64");
7766 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7769 else if (type
->integer_type() != NULL
)
7771 static tree print_int64_fndecl
;
7772 pfndecl
= &print_int64_fndecl
;
7773 fnname
= "__go_print_int64";
7774 Type
* itype
= Type::lookup_integer_type("int64");
7775 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7778 else if (type
->float_type() != NULL
)
7780 static tree print_double_fndecl
;
7781 pfndecl
= &print_double_fndecl
;
7782 fnname
= "__go_print_double";
7783 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7785 else if (type
->complex_type() != NULL
)
7787 static tree print_complex_fndecl
;
7788 pfndecl
= &print_complex_fndecl
;
7789 fnname
= "__go_print_complex";
7790 arg
= fold_convert_loc(location
, complex_double_type_node
,
7793 else if (type
->is_boolean_type())
7795 static tree print_bool_fndecl
;
7796 pfndecl
= &print_bool_fndecl
;
7797 fnname
= "__go_print_bool";
7799 else if (type
->points_to() != NULL
7800 || type
->channel_type() != NULL
7801 || type
->map_type() != NULL
7802 || type
->function_type() != NULL
)
7804 static tree print_pointer_fndecl
;
7805 pfndecl
= &print_pointer_fndecl
;
7806 fnname
= "__go_print_pointer";
7807 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7809 else if (type
->interface_type() != NULL
)
7811 if (type
->interface_type()->is_empty())
7813 static tree print_empty_interface_fndecl
;
7814 pfndecl
= &print_empty_interface_fndecl
;
7815 fnname
= "__go_print_empty_interface";
7819 static tree print_interface_fndecl
;
7820 pfndecl
= &print_interface_fndecl
;
7821 fnname
= "__go_print_interface";
7824 else if (type
->is_open_array_type())
7826 static tree print_slice_fndecl
;
7827 pfndecl
= &print_slice_fndecl
;
7828 fnname
= "__go_print_slice";
7833 tree call
= Gogo::call_builtin(pfndecl
,
7840 if (call
== error_mark_node
)
7841 return error_mark_node
;
7842 append_to_statement_list(call
, &stmt_list
);
7848 static tree print_nl_fndecl
;
7849 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7854 if (call
== error_mark_node
)
7855 return error_mark_node
;
7856 append_to_statement_list(call
, &stmt_list
);
7864 const Expression_list
* args
= this->args();
7865 gcc_assert(args
!= NULL
&& args
->size() == 1);
7866 Expression
* arg
= args
->front();
7867 tree arg_tree
= arg
->get_tree(context
);
7868 if (arg_tree
== error_mark_node
)
7869 return error_mark_node
;
7870 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7871 arg_tree
= Expression::convert_for_assignment(context
, empty
,
7873 arg_tree
, location
);
7874 static tree panic_fndecl
;
7875 tree call
= Gogo::call_builtin(&panic_fndecl
,
7880 TREE_TYPE(arg_tree
),
7882 if (call
== error_mark_node
)
7883 return error_mark_node
;
7884 // This function will throw an exception.
7885 TREE_NOTHROW(panic_fndecl
) = 0;
7886 // This function will not return.
7887 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
7891 case BUILTIN_RECOVER
:
7893 // The argument is set when building recover thunks. It's a
7894 // boolean value which is true if we can recover a value now.
7895 const Expression_list
* args
= this->args();
7896 gcc_assert(args
!= NULL
&& args
->size() == 1);
7897 Expression
* arg
= args
->front();
7898 tree arg_tree
= arg
->get_tree(context
);
7899 if (arg_tree
== error_mark_node
)
7900 return error_mark_node
;
7902 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7903 tree empty_tree
= empty
->get_tree(context
->gogo());
7905 Type
* nil_type
= Type::make_nil_type();
7906 Expression
* nil
= Expression::make_nil(location
);
7907 tree nil_tree
= nil
->get_tree(context
);
7908 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
7914 // We need to handle a deferred call to recover specially,
7915 // because it changes whether it can recover a panic or not.
7916 // See test7 in test/recover1.go.
7918 if (this->is_deferred())
7920 static tree deferred_recover_fndecl
;
7921 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
7923 "__go_deferred_recover",
7929 static tree recover_fndecl
;
7930 call
= Gogo::call_builtin(&recover_fndecl
,
7936 if (call
== error_mark_node
)
7937 return error_mark_node
;
7938 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
7939 call
, empty_nil_tree
);
7943 case BUILTIN_CLOSED
:
7945 const Expression_list
* args
= this->args();
7946 gcc_assert(args
!= NULL
&& args
->size() == 1);
7947 Expression
* arg
= args
->front();
7948 tree arg_tree
= arg
->get_tree(context
);
7949 if (arg_tree
== error_mark_node
)
7950 return error_mark_node
;
7951 if (this->code_
== BUILTIN_CLOSE
)
7953 static tree close_fndecl
;
7954 return Gogo::call_builtin(&close_fndecl
,
7956 "__go_builtin_close",
7959 TREE_TYPE(arg_tree
),
7964 static tree closed_fndecl
;
7965 return Gogo::call_builtin(&closed_fndecl
,
7967 "__go_builtin_closed",
7970 TREE_TYPE(arg_tree
),
7975 case BUILTIN_SIZEOF
:
7976 case BUILTIN_OFFSETOF
:
7977 case BUILTIN_ALIGNOF
:
7982 bool b
= this->integer_constant_value(true, val
, &dummy
);
7985 gcc_assert(saw_errors());
7986 return error_mark_node
;
7988 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
7989 tree ret
= Expression::integer_constant_tree(val
, type
);
7996 const Expression_list
* args
= this->args();
7997 gcc_assert(args
!= NULL
&& args
->size() == 2);
7998 Expression
* arg1
= args
->front();
7999 Expression
* arg2
= args
->back();
8001 tree arg1_tree
= arg1
->get_tree(context
);
8002 tree arg2_tree
= arg2
->get_tree(context
);
8003 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
8004 return error_mark_node
;
8006 Type
* arg1_type
= arg1
->type();
8007 Array_type
* at
= arg1_type
->array_type();
8008 arg1_tree
= save_expr(arg1_tree
);
8009 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
8010 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
8011 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
8012 return error_mark_node
;
8014 Type
* arg2_type
= arg2
->type();
8017 if (arg2_type
->is_open_array_type())
8019 at
= arg2_type
->array_type();
8020 arg2_tree
= save_expr(arg2_tree
);
8021 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
8022 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
8026 arg2_tree
= save_expr(arg2_tree
);
8027 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
8028 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
8030 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
8031 return error_mark_node
;
8033 arg1_len
= save_expr(arg1_len
);
8034 arg2_len
= save_expr(arg2_len
);
8035 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
8036 fold_build2_loc(location
, LT_EXPR
,
8038 arg1_len
, arg2_len
),
8039 arg1_len
, arg2_len
);
8040 len
= save_expr(len
);
8042 Type
* element_type
= at
->element_type();
8043 tree element_type_tree
= element_type
->get_tree(gogo
);
8044 if (element_type_tree
== error_mark_node
)
8045 return error_mark_node
;
8046 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
8047 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
8049 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
8050 TREE_TYPE(element_size
),
8051 bytecount
, element_size
);
8052 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
8054 arg1_val
= fold_convert_loc(location
, ptr_type_node
, arg1_val
);
8055 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
8057 static tree copy_fndecl
;
8058 tree call
= Gogo::call_builtin(©_fndecl
,
8069 if (call
== error_mark_node
)
8070 return error_mark_node
;
8072 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
8076 case BUILTIN_APPEND
:
8078 const Expression_list
* args
= this->args();
8079 gcc_assert(args
!= NULL
&& args
->size() == 2);
8080 Expression
* arg1
= args
->front();
8081 Expression
* arg2
= args
->back();
8083 tree arg1_tree
= arg1
->get_tree(context
);
8084 tree arg2_tree
= arg2
->get_tree(context
);
8085 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
8086 return error_mark_node
;
8088 Array_type
* at
= arg1
->type()->array_type();
8089 Type
* element_type
= at
->element_type();
8091 arg2_tree
= Expression::convert_for_assignment(context
, at
,
8095 if (arg2_tree
== error_mark_node
)
8096 return error_mark_node
;
8098 arg2_tree
= save_expr(arg2_tree
);
8099 tree arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
8100 tree arg2_len
= at
->length_tree(gogo
, arg2_tree
);
8101 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
8102 return error_mark_node
;
8103 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
8104 arg2_len
= fold_convert_loc(location
, size_type_node
, arg2_len
);
8106 tree element_type_tree
= element_type
->get_tree(gogo
);
8107 if (element_type_tree
== error_mark_node
)
8108 return error_mark_node
;
8109 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
8110 element_size
= fold_convert_loc(location
, size_type_node
,
8113 // We rebuild the decl each time since the slice types may
8115 tree append_fndecl
= NULL_TREE
;
8116 return Gogo::call_builtin(&append_fndecl
,
8120 TREE_TYPE(arg1_tree
),
8121 TREE_TYPE(arg1_tree
),
8134 const Expression_list
* args
= this->args();
8135 gcc_assert(args
!= NULL
&& args
->size() == 1);
8136 Expression
* arg
= args
->front();
8137 tree arg_tree
= arg
->get_tree(context
);
8138 if (arg_tree
== error_mark_node
)
8139 return error_mark_node
;
8140 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
8141 if (this->code_
== BUILTIN_REAL
)
8142 return fold_build1_loc(location
, REALPART_EXPR
,
8143 TREE_TYPE(TREE_TYPE(arg_tree
)),
8146 return fold_build1_loc(location
, IMAGPART_EXPR
,
8147 TREE_TYPE(TREE_TYPE(arg_tree
)),
8151 case BUILTIN_COMPLEX
:
8153 const Expression_list
* args
= this->args();
8154 gcc_assert(args
!= NULL
&& args
->size() == 2);
8155 tree r
= args
->front()->get_tree(context
);
8156 tree i
= args
->back()->get_tree(context
);
8157 if (r
== error_mark_node
|| i
== error_mark_node
)
8158 return error_mark_node
;
8159 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
8160 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
8161 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
8162 return fold_build2_loc(location
, COMPLEX_EXPR
,
8163 build_complex_type(TREE_TYPE(r
)),
8172 // We have to support exporting a builtin call expression, because
8173 // code can set a constant to the result of a builtin expression.
8176 Builtin_call_expression::do_export(Export
* exp
) const
8183 if (this->integer_constant_value(true, val
, &dummy
))
8185 Integer_expression::export_integer(exp
, val
);
8194 if (this->float_constant_value(fval
, &dummy
))
8196 Float_expression::export_float(exp
, fval
);
8208 if (this->complex_constant_value(real
, imag
, &dummy
))
8210 Complex_expression::export_complex(exp
, real
, imag
);
8219 error_at(this->location(), "value is not constant");
8223 // A trailing space lets us reliably identify the end of the number.
8224 exp
->write_c_string(" ");
8227 // Class Call_expression.
8232 Call_expression::do_traverse(Traverse
* traverse
)
8234 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8235 return TRAVERSE_EXIT
;
8236 if (this->args_
!= NULL
)
8238 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8239 return TRAVERSE_EXIT
;
8241 return TRAVERSE_CONTINUE
;
8244 // Lower a call statement.
8247 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
8249 // A type case can look like a function call.
8250 if (this->fn_
->is_type_expression()
8251 && this->args_
!= NULL
8252 && this->args_
->size() == 1)
8253 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8256 // Recognize a call to a builtin function.
8257 Func_expression
* fne
= this->fn_
->func_expression();
8259 && fne
->named_object()->is_function_declaration()
8260 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
8261 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8262 this->is_varargs_
, this->location());
8264 // Handle an argument which is a call to a function which returns
8265 // multiple results.
8266 if (this->args_
!= NULL
8267 && this->args_
->size() == 1
8268 && this->args_
->front()->call_expression() != NULL
8269 && this->fn_
->type()->function_type() != NULL
)
8271 Function_type
* fntype
= this->fn_
->type()->function_type();
8272 size_t rc
= this->args_
->front()->call_expression()->result_count();
8274 && fntype
->parameters() != NULL
8275 && (fntype
->parameters()->size() == rc
8276 || (fntype
->is_varargs()
8277 && fntype
->parameters()->size() - 1 <= rc
)))
8279 Call_expression
* call
= this->args_
->front()->call_expression();
8280 Expression_list
* args
= new Expression_list
;
8281 for (size_t i
= 0; i
< rc
; ++i
)
8282 args
->push_back(Expression::make_call_result(call
, i
));
8283 // We can't return a new call expression here, because this
8284 // one may be referenced by Call_result expressions. We
8285 // also can't delete the old arguments, because we may still
8286 // traverse them somewhere up the call stack. FIXME.
8291 // Handle a call to a varargs function by packaging up the extra
8293 if (this->fn_
->type()->function_type() != NULL
8294 && this->fn_
->type()->function_type()->is_varargs())
8296 Function_type
* fntype
= this->fn_
->type()->function_type();
8297 const Typed_identifier_list
* parameters
= fntype
->parameters();
8298 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
8299 Type
* varargs_type
= parameters
->back().type();
8300 return this->lower_varargs(gogo
, function
, varargs_type
,
8301 parameters
->size());
8307 // Lower a call to a varargs function. FUNCTION is the function in
8308 // which the call occurs--it's not the function we are calling.
8309 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8310 // PARAM_COUNT is the number of parameters of the function we are
8311 // calling; the last of these parameters will be the varargs
8315 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8316 Type
* varargs_type
, size_t param_count
)
8318 if (this->varargs_are_lowered_
)
8321 source_location loc
= this->location();
8323 gcc_assert(param_count
> 0);
8324 gcc_assert(varargs_type
->is_open_array_type());
8326 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8327 if (arg_count
< param_count
- 1)
8329 // Not enough arguments; will be caught in check_types.
8333 Expression_list
* old_args
= this->args_
;
8334 Expression_list
* new_args
= new Expression_list();
8335 bool push_empty_arg
= false;
8336 if (old_args
== NULL
|| old_args
->empty())
8338 gcc_assert(param_count
== 1);
8339 push_empty_arg
= true;
8343 Expression_list::const_iterator pa
;
8345 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8347 if (static_cast<size_t>(i
) == param_count
)
8349 new_args
->push_back(*pa
);
8352 // We have reached the varargs parameter.
8354 bool issued_error
= false;
8355 if (pa
== old_args
->end())
8356 push_empty_arg
= true;
8357 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8358 new_args
->push_back(*pa
);
8359 else if (this->is_varargs_
)
8361 this->report_error(_("too many arguments"));
8366 Type
* element_type
= varargs_type
->array_type()->element_type();
8367 Expression_list
* vals
= new Expression_list
;
8368 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8370 // Check types here so that we get a better message.
8371 Type
* patype
= (*pa
)->type();
8372 source_location paloc
= (*pa
)->location();
8373 if (!this->check_argument_type(i
, element_type
, patype
,
8374 paloc
, issued_error
))
8376 vals
->push_back(*pa
);
8379 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8380 new_args
->push_back(val
);
8385 new_args
->push_back(Expression::make_nil(loc
));
8387 // We can't return a new call expression here, because this one may
8388 // be referenced by Call_result expressions. FIXME.
8389 if (old_args
!= NULL
)
8391 this->args_
= new_args
;
8392 this->varargs_are_lowered_
= true;
8394 // Lower all the new subexpressions.
8395 Expression
* ret
= this;
8396 gogo
->lower_expression(function
, &ret
);
8397 gcc_assert(ret
== this);
8401 // Get the function type. Returns NULL if we don't know the type. If
8402 // this returns NULL, and if_ERROR is true, issues an error.
8405 Call_expression::get_function_type() const
8407 return this->fn_
->type()->function_type();
8410 // Return the number of values which this call will return.
8413 Call_expression::result_count() const
8415 const Function_type
* fntype
= this->get_function_type();
8418 if (fntype
->results() == NULL
)
8420 return fntype
->results()->size();
8423 // Return whether this is a call to the predeclared function recover.
8426 Call_expression::is_recover_call() const
8428 return this->do_is_recover_call();
8431 // Set the argument to the recover function.
8434 Call_expression::set_recover_arg(Expression
* arg
)
8436 this->do_set_recover_arg(arg
);
8439 // Virtual functions also implemented by Builtin_call_expression.
8442 Call_expression::do_is_recover_call() const
8448 Call_expression::do_set_recover_arg(Expression
*)
8456 Call_expression::do_type()
8458 if (this->type_
!= NULL
)
8462 Function_type
* fntype
= this->get_function_type();
8464 return Type::make_error_type();
8466 const Typed_identifier_list
* results
= fntype
->results();
8467 if (results
== NULL
)
8468 ret
= Type::make_void_type();
8469 else if (results
->size() == 1)
8470 ret
= results
->begin()->type();
8472 ret
= Type::make_call_multiple_result_type(this);
8479 // Determine types for a call expression. We can use the function
8480 // parameter types to set the types of the arguments.
8483 Call_expression::do_determine_type(const Type_context
*)
8485 this->fn_
->determine_type_no_context();
8486 Function_type
* fntype
= this->get_function_type();
8487 const Typed_identifier_list
* parameters
= NULL
;
8489 parameters
= fntype
->parameters();
8490 if (this->args_
!= NULL
)
8492 Typed_identifier_list::const_iterator pt
;
8493 if (parameters
!= NULL
)
8494 pt
= parameters
->begin();
8495 for (Expression_list::const_iterator pa
= this->args_
->begin();
8496 pa
!= this->args_
->end();
8499 if (parameters
!= NULL
&& pt
!= parameters
->end())
8501 Type_context
subcontext(pt
->type(), false);
8502 (*pa
)->determine_type(&subcontext
);
8506 (*pa
)->determine_type_no_context();
8511 // Check types for parameter I.
8514 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8515 const Type
* argument_type
,
8516 source_location argument_location
,
8520 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8525 error_at(argument_location
, "argument %d has incompatible type", i
);
8527 error_at(argument_location
,
8528 "argument %d has incompatible type (%s)",
8531 this->set_is_error();
8540 Call_expression::do_check_types(Gogo
*)
8542 Function_type
* fntype
= this->get_function_type();
8545 if (!this->fn_
->type()->is_error_type())
8546 this->report_error(_("expected function"));
8550 if (fntype
->is_method())
8552 // We don't support pointers to methods, so the function has to
8553 // be a bound method expression.
8554 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8557 this->report_error(_("method call without object"));
8560 Type
* first_arg_type
= bme
->first_argument()->type();
8561 if (first_arg_type
->points_to() == NULL
)
8563 // When passing a value, we need to check that we are
8564 // permitted to copy it.
8566 if (!Type::are_assignable(fntype
->receiver()->type(),
8567 first_arg_type
, &reason
))
8570 this->report_error(_("incompatible type for receiver"));
8573 error_at(this->location(),
8574 "incompatible type for receiver (%s)",
8576 this->set_is_error();
8582 // Note that varargs was handled by the lower_varargs() method, so
8583 // we don't have to worry about it here.
8585 const Typed_identifier_list
* parameters
= fntype
->parameters();
8586 if (this->args_
== NULL
)
8588 if (parameters
!= NULL
&& !parameters
->empty())
8589 this->report_error(_("not enough arguments"));
8591 else if (parameters
== NULL
)
8592 this->report_error(_("too many arguments"));
8596 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8597 for (Expression_list::const_iterator pa
= this->args_
->begin();
8598 pa
!= this->args_
->end();
8601 if (pt
== parameters
->end())
8603 this->report_error(_("too many arguments"));
8606 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8607 (*pa
)->location(), false);
8609 if (pt
!= parameters
->end())
8610 this->report_error(_("not enough arguments"));
8614 // Return whether we have to use a temporary variable to ensure that
8615 // we evaluate this call expression in order. If the call returns no
8616 // results then it will inevitably be executed last. If the call
8617 // returns more than one result then it will be used with Call_result
8618 // expressions. So we only have to use a temporary variable if the
8619 // call returns exactly one result.
8622 Call_expression::do_must_eval_in_order() const
8624 return this->result_count() == 1;
8627 // Get the function and the first argument to use when calling a bound
8631 Call_expression::bound_method_function(Translate_context
* context
,
8632 Bound_method_expression
* bound_method
,
8633 tree
* first_arg_ptr
)
8635 Expression
* first_argument
= bound_method
->first_argument();
8636 tree first_arg
= first_argument
->get_tree(context
);
8637 if (first_arg
== error_mark_node
)
8638 return error_mark_node
;
8640 // We always pass a pointer to the first argument when calling a
8642 if (first_argument
->type()->points_to() == NULL
)
8644 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8645 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8646 || DECL_P(first_arg
)
8647 || TREE_CODE(first_arg
) == INDIRECT_REF
8648 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8650 first_arg
= build_fold_addr_expr(first_arg
);
8651 if (DECL_P(first_arg
))
8652 TREE_ADDRESSABLE(first_arg
) = 1;
8656 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8657 get_name(first_arg
));
8658 DECL_IGNORED_P(tmp
) = 0;
8659 DECL_INITIAL(tmp
) = first_arg
;
8660 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8661 build1(DECL_EXPR
, void_type_node
, tmp
),
8662 build_fold_addr_expr(tmp
));
8663 TREE_ADDRESSABLE(tmp
) = 1;
8665 if (first_arg
== error_mark_node
)
8666 return error_mark_node
;
8669 Type
* fatype
= bound_method
->first_argument_type();
8672 if (fatype
->points_to() == NULL
)
8673 fatype
= Type::make_pointer_type(fatype
);
8674 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8675 if (first_arg
== error_mark_node
8676 || TREE_TYPE(first_arg
) == error_mark_node
)
8677 return error_mark_node
;
8680 *first_arg_ptr
= first_arg
;
8682 return bound_method
->method()->get_tree(context
);
8685 // Get the function and the first argument to use when calling an
8686 // interface method.
8689 Call_expression::interface_method_function(
8690 Translate_context
* context
,
8691 Interface_field_reference_expression
* interface_method
,
8692 tree
* first_arg_ptr
)
8694 tree expr
= interface_method
->expr()->get_tree(context
);
8695 if (expr
== error_mark_node
)
8696 return error_mark_node
;
8697 expr
= save_expr(expr
);
8698 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8699 if (first_arg
== error_mark_node
)
8700 return error_mark_node
;
8701 *first_arg_ptr
= first_arg
;
8702 return interface_method
->get_function_tree(context
, expr
);
8705 // Build the call expression.
8708 Call_expression::do_get_tree(Translate_context
* context
)
8710 if (this->tree_
!= NULL_TREE
)
8713 Function_type
* fntype
= this->get_function_type();
8715 return error_mark_node
;
8717 if (this->fn_
->is_error_expression())
8718 return error_mark_node
;
8720 Gogo
* gogo
= context
->gogo();
8721 source_location location
= this->location();
8723 Func_expression
* func
= this->fn_
->func_expression();
8724 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8725 Interface_field_reference_expression
* interface_method
=
8726 this->fn_
->interface_field_reference_expression();
8727 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8728 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8729 gcc_assert(!fntype
->is_method() || is_method
);
8733 if (this->args_
== NULL
|| this->args_
->empty())
8735 nargs
= is_method
? 1 : 0;
8736 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8740 const Typed_identifier_list
* params
= fntype
->parameters();
8741 gcc_assert(params
!= NULL
);
8743 nargs
= this->args_
->size();
8744 int i
= is_method
? 1 : 0;
8746 args
= new tree
[nargs
];
8748 Typed_identifier_list::const_iterator pp
= params
->begin();
8749 Expression_list::const_iterator pe
;
8750 for (pe
= this->args_
->begin();
8751 pe
!= this->args_
->end();
8754 gcc_assert(pp
!= params
->end());
8755 tree arg_val
= (*pe
)->get_tree(context
);
8756 args
[i
] = Expression::convert_for_assignment(context
,
8761 if (args
[i
] == error_mark_node
)
8764 return error_mark_node
;
8767 gcc_assert(pp
== params
->end());
8768 gcc_assert(i
== nargs
);
8771 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8772 if (rettype
== error_mark_node
)
8775 return error_mark_node
;
8780 fn
= func
->get_tree_without_closure(gogo
);
8781 else if (!is_method
)
8782 fn
= this->fn_
->get_tree(context
);
8783 else if (bound_method
!= NULL
)
8784 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8785 else if (interface_method
!= NULL
)
8786 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8790 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8793 return error_mark_node
;
8796 // This is to support builtin math functions when using 80387 math.
8798 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8799 fndecl
= TREE_OPERAND(fndecl
, 0);
8800 tree excess_type
= NULL_TREE
;
8802 && DECL_IS_BUILTIN(fndecl
)
8803 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8805 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8806 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8807 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8808 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8810 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8811 if (excess_type
!= NULL_TREE
)
8813 tree excess_fndecl
= mathfn_built_in(excess_type
,
8814 DECL_FUNCTION_CODE(fndecl
));
8815 if (excess_fndecl
== NULL_TREE
)
8816 excess_type
= NULL_TREE
;
8819 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8820 for (int i
= 0; i
< nargs
; ++i
)
8821 args
[i
] = ::convert(excess_type
, args
[i
]);
8826 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8830 SET_EXPR_LOCATION(ret
, location
);
8834 tree closure_tree
= func
->closure()->get_tree(context
);
8835 if (closure_tree
!= error_mark_node
)
8836 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8839 // If this is a recursive function type which returns itself, as in
8841 // we have used ptr_type_node for the return type. Add a cast here
8842 // to the correct type.
8843 if (TREE_TYPE(ret
) == ptr_type_node
)
8845 tree t
= this->type()->get_tree(gogo
);
8846 ret
= fold_convert_loc(location
, t
, ret
);
8849 if (excess_type
!= NULL_TREE
)
8851 // Calling convert here can undo our excess precision change.
8852 // That may or may not be a bug in convert_to_real.
8853 ret
= build1(NOP_EXPR
, rettype
, ret
);
8856 // If there is more than one result, we will refer to the call
8858 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
8859 ret
= save_expr(ret
);
8866 // Make a call expression.
8869 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
8870 source_location location
)
8872 return new Call_expression(fn
, args
, is_varargs
, location
);
8875 // A single result from a call which returns multiple results.
8877 class Call_result_expression
: public Expression
8880 Call_result_expression(Call_expression
* call
, unsigned int index
)
8881 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
8882 call_(call
), index_(index
)
8887 do_traverse(Traverse
*);
8893 do_determine_type(const Type_context
*);
8896 do_check_types(Gogo
*);
8901 return new Call_result_expression(this->call_
->call_expression(),
8906 do_must_eval_in_order() const
8910 do_get_tree(Translate_context
*);
8913 // The underlying call expression.
8915 // Which result we want.
8916 unsigned int index_
;
8919 // Traverse a call result.
8922 Call_result_expression::do_traverse(Traverse
* traverse
)
8924 if (traverse
->remember_expression(this->call_
))
8926 // We have already traversed the call expression.
8927 return TRAVERSE_CONTINUE
;
8929 return Expression::traverse(&this->call_
, traverse
);
8935 Call_result_expression::do_type()
8937 if (this->classification() == EXPRESSION_ERROR
)
8938 return Type::make_error_type();
8940 // THIS->CALL_ can be replaced with a temporary reference due to
8941 // Call_expression::do_must_eval_in_order when there is an error.
8942 Call_expression
* ce
= this->call_
->call_expression();
8945 this->set_is_error();
8946 return Type::make_error_type();
8948 Function_type
* fntype
= ce
->get_function_type();
8951 this->set_is_error();
8952 return Type::make_error_type();
8954 const Typed_identifier_list
* results
= fntype
->results();
8955 if (results
== NULL
)
8957 this->report_error(_("number of results does not match "
8958 "number of values"));
8959 return Type::make_error_type();
8961 Typed_identifier_list::const_iterator pr
= results
->begin();
8962 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8964 if (pr
== results
->end())
8968 if (pr
== results
->end())
8970 this->report_error(_("number of results does not match "
8971 "number of values"));
8972 return Type::make_error_type();
8977 // Check the type. Just make sure that we trigger the warning in
8981 Call_result_expression::do_check_types(Gogo
*)
8986 // Determine the type. We have nothing to do here, but the 0 result
8987 // needs to pass down to the caller.
8990 Call_result_expression::do_determine_type(const Type_context
*)
8992 if (this->index_
== 0)
8993 this->call_
->determine_type_no_context();
8999 Call_result_expression::do_get_tree(Translate_context
* context
)
9001 tree call_tree
= this->call_
->get_tree(context
);
9002 if (call_tree
== error_mark_node
)
9003 return error_mark_node
;
9004 if (TREE_CODE(TREE_TYPE(call_tree
)) != RECORD_TYPE
)
9006 gcc_assert(saw_errors());
9007 return error_mark_node
;
9009 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
9010 for (unsigned int i
= 0; i
< this->index_
; ++i
)
9012 gcc_assert(field
!= NULL_TREE
);
9013 field
= DECL_CHAIN(field
);
9015 gcc_assert(field
!= NULL_TREE
);
9016 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
9019 // Make a reference to a single result of a call which returns
9020 // multiple results.
9023 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
9025 return new Call_result_expression(call
, index
);
9028 // Class Index_expression.
9033 Index_expression::do_traverse(Traverse
* traverse
)
9035 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
9036 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
9037 || (this->end_
!= NULL
9038 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
9039 return TRAVERSE_EXIT
;
9040 return TRAVERSE_CONTINUE
;
9043 // Lower an index expression. This converts the generic index
9044 // expression into an array index, a string index, or a map index.
9047 Index_expression::do_lower(Gogo
*, Named_object
*, int)
9049 source_location location
= this->location();
9050 Expression
* left
= this->left_
;
9051 Expression
* start
= this->start_
;
9052 Expression
* end
= this->end_
;
9054 Type
* type
= left
->type();
9055 if (type
->is_error_type())
9056 return Expression::make_error(location
);
9057 else if (left
->is_type_expression())
9059 error_at(location
, "attempt to index type expression");
9060 return Expression::make_error(location
);
9062 else if (type
->array_type() != NULL
)
9063 return Expression::make_array_index(left
, start
, end
, location
);
9064 else if (type
->points_to() != NULL
9065 && type
->points_to()->array_type() != NULL
9066 && !type
->points_to()->is_open_array_type())
9068 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
9070 return Expression::make_array_index(deref
, start
, end
, location
);
9072 else if (type
->is_string_type())
9073 return Expression::make_string_index(left
, start
, end
, location
);
9074 else if (type
->map_type() != NULL
)
9078 error_at(location
, "invalid slice of map");
9079 return Expression::make_error(location
);
9081 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
9083 if (this->is_lvalue_
)
9084 ret
->set_is_lvalue();
9090 "attempt to index object which is not array, string, or map");
9091 return Expression::make_error(location
);
9095 // Make an index expression.
9098 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
9099 source_location location
)
9101 return new Index_expression(left
, start
, end
, location
);
9104 // An array index. This is used for both indexing and slicing.
9106 class Array_index_expression
: public Expression
9109 Array_index_expression(Expression
* array
, Expression
* start
,
9110 Expression
* end
, source_location location
)
9111 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
9112 array_(array
), start_(start
), end_(end
), type_(NULL
)
9117 do_traverse(Traverse
*);
9123 do_determine_type(const Type_context
*);
9126 do_check_types(Gogo
*);
9131 return Expression::make_array_index(this->array_
->copy(),
9132 this->start_
->copy(),
9135 : this->end_
->copy()),
9140 do_is_addressable() const;
9143 do_address_taken(bool escapes
)
9144 { this->array_
->address_taken(escapes
); }
9147 do_get_tree(Translate_context
*);
9150 // The array we are getting a value from.
9152 // The start or only index.
9154 // The end index of a slice. This may be NULL for a simple array
9155 // index, or it may be a nil expression for the length of the array.
9157 // The type of the expression.
9161 // Array index traversal.
9164 Array_index_expression::do_traverse(Traverse
* traverse
)
9166 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9167 return TRAVERSE_EXIT
;
9168 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9169 return TRAVERSE_EXIT
;
9170 if (this->end_
!= NULL
)
9172 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9173 return TRAVERSE_EXIT
;
9175 return TRAVERSE_CONTINUE
;
9178 // Return the type of an array index.
9181 Array_index_expression::do_type()
9183 if (this->type_
== NULL
)
9185 Array_type
* type
= this->array_
->type()->array_type();
9187 this->type_
= Type::make_error_type();
9188 else if (this->end_
== NULL
)
9189 this->type_
= type
->element_type();
9190 else if (type
->is_open_array_type())
9192 // A slice of a slice has the same type as the original
9194 this->type_
= this->array_
->type()->deref();
9198 // A slice of an array is a slice.
9199 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9205 // Set the type of an array index.
9208 Array_index_expression::do_determine_type(const Type_context
*)
9210 this->array_
->determine_type_no_context();
9211 this->start_
->determine_type_no_context();
9212 if (this->end_
!= NULL
)
9213 this->end_
->determine_type_no_context();
9216 // Check types of an array index.
9219 Array_index_expression::do_check_types(Gogo
*)
9221 if (this->start_
->type()->integer_type() == NULL
)
9222 this->report_error(_("index must be integer"));
9223 if (this->end_
!= NULL
9224 && this->end_
->type()->integer_type() == NULL
9225 && !this->end_
->is_nil_expression())
9226 this->report_error(_("slice end must be integer"));
9228 Array_type
* array_type
= this->array_
->type()->array_type();
9229 if (array_type
== NULL
)
9231 gcc_assert(this->array_
->type()->is_error_type());
9235 unsigned int int_bits
=
9236 Type::lookup_integer_type("int")->integer_type()->bits();
9241 bool lval_valid
= (array_type
->length() != NULL
9242 && array_type
->length()->integer_constant_value(true,
9247 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9249 if (mpz_sgn(ival
) < 0
9250 || mpz_sizeinbase(ival
, 2) >= int_bits
9252 && (this->end_
== NULL
9253 ? mpz_cmp(ival
, lval
) >= 0
9254 : mpz_cmp(ival
, lval
) > 0)))
9256 error_at(this->start_
->location(), "array index out of bounds");
9257 this->set_is_error();
9260 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9262 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9264 if (mpz_sgn(ival
) < 0
9265 || mpz_sizeinbase(ival
, 2) >= int_bits
9266 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
9268 error_at(this->end_
->location(), "array index out of bounds");
9269 this->set_is_error();
9276 // A slice of an array requires an addressable array. A slice of a
9277 // slice is always possible.
9278 if (this->end_
!= NULL
9279 && !array_type
->is_open_array_type()
9280 && !this->array_
->is_addressable())
9281 this->report_error(_("array is not addressable"));
9284 // Return whether this expression is addressable.
9287 Array_index_expression::do_is_addressable() const
9289 // A slice expression is not addressable.
9290 if (this->end_
!= NULL
)
9293 // An index into a slice is addressable.
9294 if (this->array_
->type()->is_open_array_type())
9297 // An index into an array is addressable if the array is
9299 return this->array_
->is_addressable();
9302 // Get a tree for an array index.
9305 Array_index_expression::do_get_tree(Translate_context
* context
)
9307 Gogo
* gogo
= context
->gogo();
9308 source_location loc
= this->location();
9310 Array_type
* array_type
= this->array_
->type()->array_type();
9311 if (array_type
== NULL
)
9313 gcc_assert(this->array_
->type()->is_error_type());
9314 return error_mark_node
;
9317 tree type_tree
= array_type
->get_tree(gogo
);
9318 if (type_tree
== error_mark_node
)
9319 return error_mark_node
;
9321 tree array_tree
= this->array_
->get_tree(context
);
9322 if (array_tree
== error_mark_node
)
9323 return error_mark_node
;
9325 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9326 array_tree
= save_expr(array_tree
);
9327 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
9328 if (length_tree
== error_mark_node
)
9329 return error_mark_node
;
9330 length_tree
= save_expr(length_tree
);
9331 tree length_type
= TREE_TYPE(length_tree
);
9333 tree bad_index
= boolean_false_node
;
9335 tree start_tree
= this->start_
->get_tree(context
);
9336 if (start_tree
== error_mark_node
)
9337 return error_mark_node
;
9338 if (!DECL_P(start_tree
))
9339 start_tree
= save_expr(start_tree
);
9340 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9341 start_tree
= convert_to_integer(length_type
, start_tree
);
9343 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9346 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9347 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9348 fold_build2_loc(loc
,
9352 boolean_type_node
, start_tree
,
9355 int code
= (array_type
->length() != NULL
9356 ? (this->end_
== NULL
9357 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9358 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9359 : (this->end_
== NULL
9360 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9361 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9362 tree crash
= Gogo::runtime_error(code
, loc
);
9364 if (this->end_
== NULL
)
9366 // Simple array indexing. This has to return an l-value, so
9367 // wrap the index check into START_TREE.
9368 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9369 build3(COND_EXPR
, void_type_node
,
9370 bad_index
, crash
, NULL_TREE
),
9372 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9374 if (array_type
->length() != NULL
)
9377 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9378 start_tree
, NULL_TREE
, NULL_TREE
);
9383 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9384 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9385 if (element_type_tree
== error_mark_node
)
9386 return error_mark_node
;
9387 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9388 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9389 start_tree
, element_size
);
9390 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9391 TREE_TYPE(values
), values
, offset
);
9392 return build_fold_indirect_ref(ptr
);
9398 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9399 if (capacity_tree
== error_mark_node
)
9400 return error_mark_node
;
9401 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9404 if (this->end_
->is_nil_expression())
9405 end_tree
= length_tree
;
9408 end_tree
= this->end_
->get_tree(context
);
9409 if (end_tree
== error_mark_node
)
9410 return error_mark_node
;
9411 if (!DECL_P(end_tree
))
9412 end_tree
= save_expr(end_tree
);
9413 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9414 end_tree
= convert_to_integer(length_type
, end_tree
);
9416 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9419 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9421 capacity_tree
= save_expr(capacity_tree
);
9422 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9423 fold_build2_loc(loc
, LT_EXPR
,
9425 end_tree
, start_tree
),
9426 fold_build2_loc(loc
, GT_EXPR
,
9428 end_tree
, capacity_tree
));
9429 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9430 bad_index
, bad_end
);
9433 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9434 if (element_type_tree
== error_mark_node
)
9435 return error_mark_node
;
9436 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9438 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9439 fold_convert_loc(loc
, sizetype
, start_tree
),
9442 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9443 if (value_pointer
== error_mark_node
)
9444 return error_mark_node
;
9446 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9447 TREE_TYPE(value_pointer
),
9448 value_pointer
, offset
);
9450 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9451 end_tree
, start_tree
);
9453 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9454 capacity_tree
, start_tree
);
9456 tree struct_tree
= this->type()->get_tree(gogo
);
9457 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9459 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9461 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9462 tree field
= TYPE_FIELDS(struct_tree
);
9463 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9465 elt
->value
= value_pointer
;
9467 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9468 field
= DECL_CHAIN(field
);
9469 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9471 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9473 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9474 field
= DECL_CHAIN(field
);
9475 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9477 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9479 tree constructor
= build_constructor(struct_tree
, init
);
9481 if (TREE_CONSTANT(value_pointer
)
9482 && TREE_CONSTANT(result_length_tree
)
9483 && TREE_CONSTANT(result_capacity_tree
))
9484 TREE_CONSTANT(constructor
) = 1;
9486 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9487 build3(COND_EXPR
, void_type_node
,
9488 bad_index
, crash
, NULL_TREE
),
9492 // Make an array index expression. END may be NULL.
9495 Expression::make_array_index(Expression
* array
, Expression
* start
,
9496 Expression
* end
, source_location location
)
9498 // Taking a slice of a composite literal requires moving the literal
9500 if (end
!= NULL
&& array
->is_composite_literal())
9502 array
= Expression::make_heap_composite(array
, location
);
9503 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9505 return new Array_index_expression(array
, start
, end
, location
);
9508 // A string index. This is used for both indexing and slicing.
9510 class String_index_expression
: public Expression
9513 String_index_expression(Expression
* string
, Expression
* start
,
9514 Expression
* end
, source_location location
)
9515 : Expression(EXPRESSION_STRING_INDEX
, location
),
9516 string_(string
), start_(start
), end_(end
)
9521 do_traverse(Traverse
*);
9527 do_determine_type(const Type_context
*);
9530 do_check_types(Gogo
*);
9535 return Expression::make_string_index(this->string_
->copy(),
9536 this->start_
->copy(),
9539 : this->end_
->copy()),
9544 do_get_tree(Translate_context
*);
9547 // The string we are getting a value from.
9548 Expression
* string_
;
9549 // The start or only index.
9551 // The end index of a slice. This may be NULL for a single index,
9552 // or it may be a nil expression for the length of the string.
9556 // String index traversal.
9559 String_index_expression::do_traverse(Traverse
* traverse
)
9561 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9562 return TRAVERSE_EXIT
;
9563 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9564 return TRAVERSE_EXIT
;
9565 if (this->end_
!= NULL
)
9567 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9568 return TRAVERSE_EXIT
;
9570 return TRAVERSE_CONTINUE
;
9573 // Return the type of a string index.
9576 String_index_expression::do_type()
9578 if (this->end_
== NULL
)
9579 return Type::lookup_integer_type("uint8");
9581 return this->string_
->type();
9584 // Determine the type of a string index.
9587 String_index_expression::do_determine_type(const Type_context
*)
9589 this->string_
->determine_type_no_context();
9590 this->start_
->determine_type_no_context();
9591 if (this->end_
!= NULL
)
9592 this->end_
->determine_type_no_context();
9595 // Check types of a string index.
9598 String_index_expression::do_check_types(Gogo
*)
9600 if (this->start_
->type()->integer_type() == NULL
)
9601 this->report_error(_("index must be integer"));
9602 if (this->end_
!= NULL
9603 && this->end_
->type()->integer_type() == NULL
9604 && !this->end_
->is_nil_expression())
9605 this->report_error(_("slice end must be integer"));
9608 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9613 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9615 if (mpz_sgn(ival
) < 0
9616 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9618 error_at(this->start_
->location(), "string index out of bounds");
9619 this->set_is_error();
9622 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9624 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9626 if (mpz_sgn(ival
) < 0
9627 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9629 error_at(this->end_
->location(), "string index out of bounds");
9630 this->set_is_error();
9637 // Get a tree for a string index.
9640 String_index_expression::do_get_tree(Translate_context
* context
)
9642 source_location loc
= this->location();
9644 tree string_tree
= this->string_
->get_tree(context
);
9645 if (string_tree
== error_mark_node
)
9646 return error_mark_node
;
9648 if (this->string_
->type()->points_to() != NULL
)
9649 string_tree
= build_fold_indirect_ref(string_tree
);
9650 if (!DECL_P(string_tree
))
9651 string_tree
= save_expr(string_tree
);
9652 tree string_type
= TREE_TYPE(string_tree
);
9654 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9655 length_tree
= save_expr(length_tree
);
9656 tree length_type
= TREE_TYPE(length_tree
);
9658 tree bad_index
= boolean_false_node
;
9660 tree start_tree
= this->start_
->get_tree(context
);
9661 if (start_tree
== error_mark_node
)
9662 return error_mark_node
;
9663 if (!DECL_P(start_tree
))
9664 start_tree
= save_expr(start_tree
);
9665 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9666 start_tree
= convert_to_integer(length_type
, start_tree
);
9668 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9671 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9673 int code
= (this->end_
== NULL
9674 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9675 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9676 tree crash
= Gogo::runtime_error(code
, loc
);
9678 if (this->end_
== NULL
)
9680 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9682 fold_build2_loc(loc
, GE_EXPR
,
9684 start_tree
, length_tree
));
9686 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9687 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9689 fold_convert_loc(loc
, sizetype
, start_tree
));
9690 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9692 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9693 build3(COND_EXPR
, void_type_node
,
9694 bad_index
, crash
, NULL_TREE
),
9700 if (this->end_
->is_nil_expression())
9701 end_tree
= build_int_cst(length_type
, -1);
9704 end_tree
= this->end_
->get_tree(context
);
9705 if (end_tree
== error_mark_node
)
9706 return error_mark_node
;
9707 if (!DECL_P(end_tree
))
9708 end_tree
= save_expr(end_tree
);
9709 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9710 end_tree
= convert_to_integer(length_type
, end_tree
);
9712 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9715 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9718 static tree strslice_fndecl
;
9719 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9721 "__go_string_slice",
9730 if (ret
== error_mark_node
)
9731 return error_mark_node
;
9732 // This will panic if the bounds are out of range for the
9734 TREE_NOTHROW(strslice_fndecl
) = 0;
9736 if (bad_index
== boolean_false_node
)
9739 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9740 build3(COND_EXPR
, void_type_node
,
9741 bad_index
, crash
, NULL_TREE
),
9746 // Make a string index expression. END may be NULL.
9749 Expression::make_string_index(Expression
* string
, Expression
* start
,
9750 Expression
* end
, source_location location
)
9752 return new String_index_expression(string
, start
, end
, location
);
9757 // Get the type of the map.
9760 Map_index_expression::get_map_type() const
9762 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9764 gcc_assert(saw_errors());
9768 // Map index traversal.
9771 Map_index_expression::do_traverse(Traverse
* traverse
)
9773 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9774 return TRAVERSE_EXIT
;
9775 return Expression::traverse(&this->index_
, traverse
);
9778 // Return the type of a map index.
9781 Map_index_expression::do_type()
9783 Map_type
* mt
= this->get_map_type();
9785 return Type::make_error_type();
9786 Type
* type
= mt
->val_type();
9787 // If this map index is in a tuple assignment, we actually return a
9788 // pointer to the value type. Tuple_map_assignment_statement is
9789 // responsible for handling this correctly. We need to get the type
9790 // right in case this gets assigned to a temporary variable.
9791 if (this->is_in_tuple_assignment_
)
9792 type
= Type::make_pointer_type(type
);
9796 // Fix the type of a map index.
9799 Map_index_expression::do_determine_type(const Type_context
*)
9801 this->map_
->determine_type_no_context();
9802 Map_type
* mt
= this->get_map_type();
9803 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
9804 Type_context
subcontext(key_type
, false);
9805 this->index_
->determine_type(&subcontext
);
9808 // Check types of a map index.
9811 Map_index_expression::do_check_types(Gogo
*)
9814 Map_type
* mt
= this->get_map_type();
9817 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
9820 this->report_error(_("incompatible type for map index"));
9823 error_at(this->location(), "incompatible type for map index (%s)",
9825 this->set_is_error();
9830 // Get a tree for a map index.
9833 Map_index_expression::do_get_tree(Translate_context
* context
)
9835 Map_type
* type
= this->get_map_type();
9837 return error_mark_node
;
9839 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9840 if (valptr
== error_mark_node
)
9841 return error_mark_node
;
9842 valptr
= save_expr(valptr
);
9844 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
9846 if (this->is_lvalue_
)
9847 return build_fold_indirect_ref(valptr
);
9848 else if (this->is_in_tuple_assignment_
)
9850 // Tuple_map_assignment_statement is responsible for using this
9856 return fold_build3(COND_EXPR
, val_type_tree
,
9857 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
9858 fold_convert(TREE_TYPE(valptr
),
9859 null_pointer_node
)),
9860 type
->val_type()->get_init_tree(context
->gogo(),
9862 build_fold_indirect_ref(valptr
));
9866 // Get a tree for the map index. This returns a tree which evaluates
9867 // to a pointer to a value. The pointer will be NULL if the key is
9871 Map_index_expression::get_value_pointer(Translate_context
* context
,
9874 Map_type
* type
= this->get_map_type();
9876 return error_mark_node
;
9878 tree map_tree
= this->map_
->get_tree(context
);
9879 tree index_tree
= this->index_
->get_tree(context
);
9880 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
9881 this->index_
->type(),
9884 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
9885 return error_mark_node
;
9887 if (this->map_
->type()->points_to() != NULL
)
9888 map_tree
= build_fold_indirect_ref(map_tree
);
9890 // We need to pass in a pointer to the key, so stuff it into a
9894 if (current_function_decl
!= NULL
)
9896 tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
9897 DECL_IGNORED_P(tmp
) = 0;
9898 DECL_INITIAL(tmp
) = index_tree
;
9899 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
9900 TREE_ADDRESSABLE(tmp
) = 1;
9904 tmp
= build_decl(this->location(), VAR_DECL
, create_tmp_var_name("M"),
9905 TREE_TYPE(index_tree
));
9906 DECL_EXTERNAL(tmp
) = 0;
9907 TREE_PUBLIC(tmp
) = 0;
9908 TREE_STATIC(tmp
) = 1;
9909 DECL_ARTIFICIAL(tmp
) = 1;
9910 if (!TREE_CONSTANT(index_tree
))
9911 make_tmp
= fold_build2_loc(this->location(), INIT_EXPR
, void_type_node
,
9915 TREE_READONLY(tmp
) = 1;
9916 TREE_CONSTANT(tmp
) = 1;
9917 DECL_INITIAL(tmp
) = index_tree
;
9918 make_tmp
= NULL_TREE
;
9920 rest_of_decl_compilation(tmp
, 1, 0);
9922 tree tmpref
= fold_convert_loc(this->location(), const_ptr_type_node
,
9923 build_fold_addr_expr_loc(this->location(),
9926 static tree map_index_fndecl
;
9927 tree call
= Gogo::call_builtin(&map_index_fndecl
,
9931 const_ptr_type_node
,
9932 TREE_TYPE(map_tree
),
9934 const_ptr_type_node
,
9939 : boolean_false_node
));
9940 if (call
== error_mark_node
)
9941 return error_mark_node
;
9942 // This can panic on a map of interface type if the interface holds
9943 // an uncomparable or unhashable type.
9944 TREE_NOTHROW(map_index_fndecl
) = 0;
9946 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
9947 if (val_type_tree
== error_mark_node
)
9948 return error_mark_node
;
9949 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
9951 tree ret
= fold_convert_loc(this->location(), ptr_val_type_tree
, call
);
9952 if (make_tmp
!= NULL_TREE
)
9953 ret
= build2(COMPOUND_EXPR
, ptr_val_type_tree
, make_tmp
, ret
);
9957 // Make a map index expression.
9959 Map_index_expression
*
9960 Expression::make_map_index(Expression
* map
, Expression
* index
,
9961 source_location location
)
9963 return new Map_index_expression(map
, index
, location
);
9966 // Class Field_reference_expression.
9968 // Return the type of a field reference.
9971 Field_reference_expression::do_type()
9973 Type
* type
= this->expr_
->type();
9974 if (type
->is_error_type())
9976 Struct_type
* struct_type
= type
->struct_type();
9977 gcc_assert(struct_type
!= NULL
);
9978 return struct_type
->field(this->field_index_
)->type();
9981 // Check the types for a field reference.
9984 Field_reference_expression::do_check_types(Gogo
*)
9986 Type
* type
= this->expr_
->type();
9987 if (type
->is_error_type())
9989 Struct_type
* struct_type
= type
->struct_type();
9990 gcc_assert(struct_type
!= NULL
);
9991 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
9994 // Get a tree for a field reference.
9997 Field_reference_expression::do_get_tree(Translate_context
* context
)
9999 tree struct_tree
= this->expr_
->get_tree(context
);
10000 if (struct_tree
== error_mark_node
10001 || TREE_TYPE(struct_tree
) == error_mark_node
)
10002 return error_mark_node
;
10003 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
10004 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
10005 if (field
== NULL_TREE
)
10007 // This can happen for a type which refers to itself indirectly
10008 // and then turns out to be erroneous.
10009 gcc_assert(saw_errors());
10010 return error_mark_node
;
10012 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
10014 field
= DECL_CHAIN(field
);
10015 gcc_assert(field
!= NULL_TREE
);
10017 if (TREE_TYPE(field
) == error_mark_node
)
10018 return error_mark_node
;
10019 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
10023 // Make a reference to a qualified identifier in an expression.
10025 Field_reference_expression
*
10026 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
10027 source_location location
)
10029 return new Field_reference_expression(expr
, field_index
, location
);
10032 // Class Interface_field_reference_expression.
10034 // Return a tree for the pointer to the function to call.
10037 Interface_field_reference_expression::get_function_tree(Translate_context
*,
10040 if (this->expr_
->type()->points_to() != NULL
)
10041 expr
= build_fold_indirect_ref(expr
);
10043 tree expr_type
= TREE_TYPE(expr
);
10044 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10046 tree field
= TYPE_FIELDS(expr_type
);
10047 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
10049 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10050 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
10052 table
= build_fold_indirect_ref(table
);
10053 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
10055 std::string name
= Gogo::unpack_hidden_name(this->name_
);
10056 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
10057 field
!= NULL_TREE
;
10058 field
= DECL_CHAIN(field
))
10060 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
10063 gcc_assert(field
!= NULL_TREE
);
10065 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
10068 // Return a tree for the first argument to pass to the interface
10072 Interface_field_reference_expression::get_underlying_object_tree(
10073 Translate_context
*,
10076 if (this->expr_
->type()->points_to() != NULL
)
10077 expr
= build_fold_indirect_ref(expr
);
10079 tree expr_type
= TREE_TYPE(expr
);
10080 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10082 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
10083 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
10085 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10091 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
10093 return Expression::traverse(&this->expr_
, traverse
);
10096 // Return the type of an interface field reference.
10099 Interface_field_reference_expression::do_type()
10101 Type
* expr_type
= this->expr_
->type();
10103 Type
* points_to
= expr_type
->points_to();
10104 if (points_to
!= NULL
)
10105 expr_type
= points_to
;
10107 Interface_type
* interface_type
= expr_type
->interface_type();
10108 if (interface_type
== NULL
)
10109 return Type::make_error_type();
10111 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
10112 if (method
== NULL
)
10113 return Type::make_error_type();
10115 return method
->type();
10118 // Determine types.
10121 Interface_field_reference_expression::do_determine_type(const Type_context
*)
10123 this->expr_
->determine_type_no_context();
10126 // Check the types for an interface field reference.
10129 Interface_field_reference_expression::do_check_types(Gogo
*)
10131 Type
* type
= this->expr_
->type();
10133 Type
* points_to
= type
->points_to();
10134 if (points_to
!= NULL
)
10137 Interface_type
* interface_type
= type
->interface_type();
10138 if (interface_type
== NULL
)
10139 this->report_error(_("expected interface or pointer to interface"));
10142 const Typed_identifier
* method
=
10143 interface_type
->find_method(this->name_
);
10144 if (method
== NULL
)
10146 error_at(this->location(), "method %qs not in interface",
10147 Gogo::message_name(this->name_
).c_str());
10148 this->set_is_error();
10153 // Get a tree for a reference to a field in an interface. There is no
10154 // standard tree type representation for this: it's a function
10155 // attached to its first argument, like a Bound_method_expression.
10156 // The only places it may currently be used are in a Call_expression
10157 // or a Go_statement, which will take it apart directly. So this has
10158 // nothing to do at present.
10161 Interface_field_reference_expression::do_get_tree(Translate_context
*)
10166 // Make a reference to a field in an interface.
10169 Expression::make_interface_field_reference(Expression
* expr
,
10170 const std::string
& field
,
10171 source_location location
)
10173 return new Interface_field_reference_expression(expr
, field
, location
);
10176 // A general selector. This is a Parser_expression for LEFT.NAME. It
10177 // is lowered after we know the type of the left hand side.
10179 class Selector_expression
: public Parser_expression
10182 Selector_expression(Expression
* left
, const std::string
& name
,
10183 source_location location
)
10184 : Parser_expression(EXPRESSION_SELECTOR
, location
),
10185 left_(left
), name_(name
)
10190 do_traverse(Traverse
* traverse
)
10191 { return Expression::traverse(&this->left_
, traverse
); }
10194 do_lower(Gogo
*, Named_object
*, int);
10199 return new Selector_expression(this->left_
->copy(), this->name_
,
10205 lower_method_expression(Gogo
*);
10207 // The expression on the left hand side.
10209 // The name on the right hand side.
10213 // Lower a selector expression once we know the real type of the left
10217 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
10219 Expression
* left
= this->left_
;
10220 if (left
->is_type_expression())
10221 return this->lower_method_expression(gogo
);
10222 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
10226 // Lower a method expression T.M or (*T).M. We turn this into a
10227 // function literal.
10230 Selector_expression::lower_method_expression(Gogo
* gogo
)
10232 source_location location
= this->location();
10233 Type
* type
= this->left_
->type();
10234 const std::string
& name(this->name_
);
10237 if (type
->points_to() == NULL
)
10238 is_pointer
= false;
10242 type
= type
->points_to();
10244 Named_type
* nt
= type
->named_type();
10248 ("method expression requires named type or "
10249 "pointer to named type"));
10250 return Expression::make_error(location
);
10254 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
10255 if (method
== NULL
)
10258 error_at(location
, "type %<%s%> has no method %<%s%>",
10259 nt
->message_name().c_str(),
10260 Gogo::message_name(name
).c_str());
10262 error_at(location
, "method %<%s%> is ambiguous in type %<%s%>",
10263 Gogo::message_name(name
).c_str(),
10264 nt
->message_name().c_str());
10265 return Expression::make_error(location
);
10268 if (!is_pointer
&& !method
->is_value_method())
10270 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
10271 nt
->message_name().c_str(),
10272 Gogo::message_name(name
).c_str());
10273 return Expression::make_error(location
);
10276 // Build a new function type in which the receiver becomes the first
10278 Function_type
* method_type
= method
->type();
10279 gcc_assert(method_type
->is_method());
10281 const char* const receiver_name
= "$this";
10282 Typed_identifier_list
* parameters
= new Typed_identifier_list();
10283 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
10286 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
10287 if (method_parameters
!= NULL
)
10289 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10290 p
!= method_parameters
->end();
10292 parameters
->push_back(*p
);
10295 const Typed_identifier_list
* method_results
= method_type
->results();
10296 Typed_identifier_list
* results
;
10297 if (method_results
== NULL
)
10301 results
= new Typed_identifier_list();
10302 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
10303 p
!= method_results
->end();
10305 results
->push_back(*p
);
10308 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
10310 if (method_type
->is_varargs())
10311 fntype
->set_is_varargs();
10313 // We generate methods which always takes a pointer to the receiver
10314 // as their first argument. If this is for a pointer type, we can
10315 // simply reuse the existing function. We use an internal hack to
10316 // get the right type.
10320 Named_object
* mno
= (method
->needs_stub_method()
10321 ? method
->stub_object()
10322 : method
->named_object());
10323 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
10324 f
= Expression::make_cast(fntype
, f
, location
);
10325 Type_conversion_expression
* tce
=
10326 static_cast<Type_conversion_expression
*>(f
);
10327 tce
->set_may_convert_function_types();
10331 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
10334 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
10335 gcc_assert(vno
!= NULL
);
10336 Expression
* ve
= Expression::make_var_reference(vno
, location
);
10337 Expression
* bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
10339 // Even though we found the method above, if it has an error type we
10340 // may see an error here.
10341 if (bm
->is_error_expression())
10343 gogo
->finish_function(location
);
10347 Expression_list
* args
;
10348 if (method_parameters
== NULL
)
10352 args
= new Expression_list();
10353 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10354 p
!= method_parameters
->end();
10357 vno
= gogo
->lookup(p
->name(), NULL
);
10358 gcc_assert(vno
!= NULL
);
10359 args
->push_back(Expression::make_var_reference(vno
, location
));
10363 Call_expression
* call
= Expression::make_call(bm
, args
,
10364 method_type
->is_varargs(),
10367 size_t count
= call
->result_count();
10370 s
= Statement::make_statement(call
);
10373 Expression_list
* retvals
= new Expression_list();
10375 retvals
->push_back(call
);
10378 for (size_t i
= 0; i
< count
; ++i
)
10379 retvals
->push_back(Expression::make_call_result(call
, i
));
10381 s
= Statement::make_return_statement(no
->func_value()->type()->results(),
10382 retvals
, location
);
10384 gogo
->add_statement(s
);
10386 gogo
->finish_function(location
);
10388 return Expression::make_func_reference(no
, NULL
, location
);
10391 // Make a selector expression.
10394 Expression::make_selector(Expression
* left
, const std::string
& name
,
10395 source_location location
)
10397 return new Selector_expression(left
, name
, location
);
10400 // Implement the builtin function new.
10402 class Allocation_expression
: public Expression
10405 Allocation_expression(Type
* type
, source_location location
)
10406 : Expression(EXPRESSION_ALLOCATION
, location
),
10412 do_traverse(Traverse
* traverse
)
10413 { return Type::traverse(this->type_
, traverse
); }
10417 { return Type::make_pointer_type(this->type_
); }
10420 do_determine_type(const Type_context
*)
10424 do_check_types(Gogo
*);
10428 { return new Allocation_expression(this->type_
, this->location()); }
10431 do_get_tree(Translate_context
*);
10434 // The type we are allocating.
10438 // Check the type of an allocation expression.
10441 Allocation_expression::do_check_types(Gogo
*)
10443 if (this->type_
->function_type() != NULL
)
10444 this->report_error(_("invalid new of function type"));
10447 // Return a tree for an allocation expression.
10450 Allocation_expression::do_get_tree(Translate_context
* context
)
10452 tree type_tree
= this->type_
->get_tree(context
->gogo());
10453 if (type_tree
== error_mark_node
)
10454 return error_mark_node
;
10455 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10456 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10458 if (space
== error_mark_node
)
10459 return error_mark_node
;
10460 return fold_convert(build_pointer_type(type_tree
), space
);
10463 // Make an allocation expression.
10466 Expression::make_allocation(Type
* type
, source_location location
)
10468 return new Allocation_expression(type
, location
);
10471 // Implement the builtin function make.
10473 class Make_expression
: public Expression
10476 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10477 : Expression(EXPRESSION_MAKE
, location
),
10478 type_(type
), args_(args
)
10483 do_traverse(Traverse
* traverse
);
10487 { return this->type_
; }
10490 do_determine_type(const Type_context
*);
10493 do_check_types(Gogo
*);
10498 return new Make_expression(this->type_
, this->args_
->copy(),
10503 do_get_tree(Translate_context
*);
10506 // The type we are making.
10508 // The arguments to pass to the make routine.
10509 Expression_list
* args_
;
10515 Make_expression::do_traverse(Traverse
* traverse
)
10517 if (this->args_
!= NULL
10518 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10519 return TRAVERSE_EXIT
;
10520 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10521 return TRAVERSE_EXIT
;
10522 return TRAVERSE_CONTINUE
;
10525 // Set types of arguments.
10528 Make_expression::do_determine_type(const Type_context
*)
10530 if (this->args_
!= NULL
)
10532 Type_context
context(Type::lookup_integer_type("int"), false);
10533 for (Expression_list::const_iterator pe
= this->args_
->begin();
10534 pe
!= this->args_
->end();
10536 (*pe
)->determine_type(&context
);
10540 // Check types for a make expression.
10543 Make_expression::do_check_types(Gogo
*)
10545 if (this->type_
->channel_type() == NULL
10546 && this->type_
->map_type() == NULL
10547 && (this->type_
->array_type() == NULL
10548 || this->type_
->array_type()->length() != NULL
))
10549 this->report_error(_("invalid type for make function"));
10550 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10551 this->set_is_error();
10554 // Return a tree for a make expression.
10557 Make_expression::do_get_tree(Translate_context
* context
)
10559 return this->type_
->make_expression_tree(context
, this->args_
,
10563 // Make a make expression.
10566 Expression::make_make(Type
* type
, Expression_list
* args
,
10567 source_location location
)
10569 return new Make_expression(type
, args
, location
);
10572 // Construct a struct.
10574 class Struct_construction_expression
: public Expression
10577 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10578 source_location location
)
10579 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10580 type_(type
), vals_(vals
)
10583 // Return whether this is a constant initializer.
10585 is_constant_struct() const;
10589 do_traverse(Traverse
* traverse
);
10593 { return this->type_
; }
10596 do_determine_type(const Type_context
*);
10599 do_check_types(Gogo
*);
10604 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10609 do_is_addressable() const
10613 do_get_tree(Translate_context
*);
10616 do_export(Export
*) const;
10619 // The type of the struct to construct.
10621 // The list of values, in order of the fields in the struct. A NULL
10622 // entry means that the field should be zero-initialized.
10623 Expression_list
* vals_
;
10629 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10631 if (this->vals_
!= NULL
10632 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10633 return TRAVERSE_EXIT
;
10634 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10635 return TRAVERSE_EXIT
;
10636 return TRAVERSE_CONTINUE
;
10639 // Return whether this is a constant initializer.
10642 Struct_construction_expression::is_constant_struct() const
10644 if (this->vals_
== NULL
)
10646 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10647 pv
!= this->vals_
->end();
10651 && !(*pv
)->is_constant()
10652 && (!(*pv
)->is_composite_literal()
10653 || (*pv
)->is_nonconstant_composite_literal()))
10657 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10658 for (Struct_field_list::const_iterator pf
= fields
->begin();
10659 pf
!= fields
->end();
10662 // There are no constant constructors for interfaces.
10663 if (pf
->type()->interface_type() != NULL
)
10670 // Final type determination.
10673 Struct_construction_expression::do_determine_type(const Type_context
*)
10675 if (this->vals_
== NULL
)
10677 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10678 Expression_list::const_iterator pv
= this->vals_
->begin();
10679 for (Struct_field_list::const_iterator pf
= fields
->begin();
10680 pf
!= fields
->end();
10683 if (pv
== this->vals_
->end())
10687 Type_context
subcontext(pf
->type(), false);
10688 (*pv
)->determine_type(&subcontext
);
10691 // Extra values are an error we will report elsewhere; we still want
10692 // to determine the type to avoid knockon errors.
10693 for (; pv
!= this->vals_
->end(); ++pv
)
10694 (*pv
)->determine_type_no_context();
10700 Struct_construction_expression::do_check_types(Gogo
*)
10702 if (this->vals_
== NULL
)
10705 Struct_type
* st
= this->type_
->struct_type();
10706 if (this->vals_
->size() > st
->field_count())
10708 this->report_error(_("too many expressions for struct"));
10712 const Struct_field_list
* fields
= st
->fields();
10713 Expression_list::const_iterator pv
= this->vals_
->begin();
10715 for (Struct_field_list::const_iterator pf
= fields
->begin();
10716 pf
!= fields
->end();
10719 if (pv
== this->vals_
->end())
10721 this->report_error(_("too few expressions for struct"));
10728 std::string reason
;
10729 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10731 if (reason
.empty())
10732 error_at((*pv
)->location(),
10733 "incompatible type for field %d in struct construction",
10736 error_at((*pv
)->location(),
10737 ("incompatible type for field %d in "
10738 "struct construction (%s)"),
10739 i
+ 1, reason
.c_str());
10740 this->set_is_error();
10743 gcc_assert(pv
== this->vals_
->end());
10746 // Return a tree for constructing a struct.
10749 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10751 Gogo
* gogo
= context
->gogo();
10753 if (this->vals_
== NULL
)
10754 return this->type_
->get_init_tree(gogo
, false);
10756 tree type_tree
= this->type_
->get_tree(gogo
);
10757 if (type_tree
== error_mark_node
)
10758 return error_mark_node
;
10759 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10761 bool is_constant
= true;
10762 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10763 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10765 Struct_field_list::const_iterator pf
= fields
->begin();
10766 Expression_list::const_iterator pv
= this->vals_
->begin();
10767 for (tree field
= TYPE_FIELDS(type_tree
);
10768 field
!= NULL_TREE
;
10769 field
= DECL_CHAIN(field
), ++pf
)
10771 gcc_assert(pf
!= fields
->end());
10774 if (pv
== this->vals_
->end())
10775 val
= pf
->type()->get_init_tree(gogo
, false);
10776 else if (*pv
== NULL
)
10778 val
= pf
->type()->get_init_tree(gogo
, false);
10783 val
= Expression::convert_for_assignment(context
, pf
->type(),
10785 (*pv
)->get_tree(context
),
10790 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10791 return error_mark_node
;
10793 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10794 elt
->index
= field
;
10796 if (!TREE_CONSTANT(val
))
10797 is_constant
= false;
10799 gcc_assert(pf
== fields
->end());
10801 tree ret
= build_constructor(type_tree
, elts
);
10803 TREE_CONSTANT(ret
) = 1;
10807 // Export a struct construction.
10810 Struct_construction_expression::do_export(Export
* exp
) const
10812 exp
->write_c_string("convert(");
10813 exp
->write_type(this->type_
);
10814 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10815 pv
!= this->vals_
->end();
10818 exp
->write_c_string(", ");
10820 (*pv
)->export_expression(exp
);
10822 exp
->write_c_string(")");
10825 // Make a struct composite literal. This used by the thunk code.
10828 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
10829 source_location location
)
10831 gcc_assert(type
->struct_type() != NULL
);
10832 return new Struct_construction_expression(type
, vals
, location
);
10835 // Construct an array. This class is not used directly; instead we
10836 // use the child classes, Fixed_array_construction_expression and
10837 // Open_array_construction_expression.
10839 class Array_construction_expression
: public Expression
10842 Array_construction_expression(Expression_classification classification
,
10843 Type
* type
, Expression_list
* vals
,
10844 source_location location
)
10845 : Expression(classification
, location
),
10846 type_(type
), vals_(vals
)
10850 // Return whether this is a constant initializer.
10852 is_constant_array() const;
10854 // Return the number of elements.
10856 element_count() const
10857 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
10861 do_traverse(Traverse
* traverse
);
10865 { return this->type_
; }
10868 do_determine_type(const Type_context
*);
10871 do_check_types(Gogo
*);
10874 do_is_addressable() const
10878 do_export(Export
*) const;
10880 // The list of values.
10883 { return this->vals_
; }
10885 // Get a constructor tree for the array values.
10887 get_constructor_tree(Translate_context
* context
, tree type_tree
);
10890 // The type of the array to construct.
10892 // The list of values.
10893 Expression_list
* vals_
;
10899 Array_construction_expression::do_traverse(Traverse
* traverse
)
10901 if (this->vals_
!= NULL
10902 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10903 return TRAVERSE_EXIT
;
10904 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10905 return TRAVERSE_EXIT
;
10906 return TRAVERSE_CONTINUE
;
10909 // Return whether this is a constant initializer.
10912 Array_construction_expression::is_constant_array() const
10914 if (this->vals_
== NULL
)
10917 // There are no constant constructors for interfaces.
10918 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
10921 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10922 pv
!= this->vals_
->end();
10926 && !(*pv
)->is_constant()
10927 && (!(*pv
)->is_composite_literal()
10928 || (*pv
)->is_nonconstant_composite_literal()))
10934 // Final type determination.
10937 Array_construction_expression::do_determine_type(const Type_context
*)
10939 if (this->vals_
== NULL
)
10941 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
10942 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10943 pv
!= this->vals_
->end();
10947 (*pv
)->determine_type(&subcontext
);
10954 Array_construction_expression::do_check_types(Gogo
*)
10956 if (this->vals_
== NULL
)
10959 Array_type
* at
= this->type_
->array_type();
10961 Type
* element_type
= at
->element_type();
10962 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10963 pv
!= this->vals_
->end();
10967 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
10969 error_at((*pv
)->location(),
10970 "incompatible type for element %d in composite literal",
10972 this->set_is_error();
10976 Expression
* length
= at
->length();
10977 if (length
!= NULL
)
10982 if (at
->length()->integer_constant_value(true, val
, &type
))
10984 if (this->vals_
->size() > mpz_get_ui(val
))
10985 this->report_error(_("too many elements in composite literal"));
10991 // Get a constructor tree for the array values.
10994 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
10997 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10998 (this->vals_
== NULL
11000 : this->vals_
->size()));
11001 Type
* element_type
= this->type_
->array_type()->element_type();
11002 bool is_constant
= true;
11003 if (this->vals_
!= NULL
)
11006 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11007 pv
!= this->vals_
->end();
11010 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11011 elt
->index
= size_int(i
);
11013 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
11016 tree value_tree
= (*pv
)->get_tree(context
);
11017 elt
->value
= Expression::convert_for_assignment(context
,
11023 if (elt
->value
== error_mark_node
)
11024 return error_mark_node
;
11025 if (!TREE_CONSTANT(elt
->value
))
11026 is_constant
= false;
11030 tree ret
= build_constructor(type_tree
, values
);
11032 TREE_CONSTANT(ret
) = 1;
11036 // Export an array construction.
11039 Array_construction_expression::do_export(Export
* exp
) const
11041 exp
->write_c_string("convert(");
11042 exp
->write_type(this->type_
);
11043 if (this->vals_
!= NULL
)
11045 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11046 pv
!= this->vals_
->end();
11049 exp
->write_c_string(", ");
11051 (*pv
)->export_expression(exp
);
11054 exp
->write_c_string(")");
11057 // Construct a fixed array.
11059 class Fixed_array_construction_expression
:
11060 public Array_construction_expression
11063 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
11064 source_location location
)
11065 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
11066 type
, vals
, location
)
11068 gcc_assert(type
->array_type() != NULL
11069 && type
->array_type()->length() != NULL
);
11076 return new Fixed_array_construction_expression(this->type(),
11077 (this->vals() == NULL
11079 : this->vals()->copy()),
11084 do_get_tree(Translate_context
*);
11087 // Return a tree for constructing a fixed array.
11090 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
11092 return this->get_constructor_tree(context
,
11093 this->type()->get_tree(context
->gogo()));
11096 // Construct an open array.
11098 class Open_array_construction_expression
: public Array_construction_expression
11101 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
11102 source_location location
)
11103 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
11104 type
, vals
, location
)
11106 gcc_assert(type
->array_type() != NULL
11107 && type
->array_type()->length() == NULL
);
11111 // Note that taking the address of an open array literal is invalid.
11116 return new Open_array_construction_expression(this->type(),
11117 (this->vals() == NULL
11119 : this->vals()->copy()),
11124 do_get_tree(Translate_context
*);
11127 // Return a tree for constructing an open array.
11130 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
11132 Array_type
* array_type
= this->type()->array_type();
11133 if (array_type
== NULL
)
11135 gcc_assert(this->type()->is_error_type());
11136 return error_mark_node
;
11139 Type
* element_type
= array_type
->element_type();
11140 tree element_type_tree
= element_type
->get_tree(context
->gogo());
11141 if (element_type_tree
== error_mark_node
)
11142 return error_mark_node
;
11146 if (this->vals() == NULL
|| this->vals()->empty())
11148 // We need to create a unique value.
11149 tree max
= size_int(0);
11150 tree constructor_type
= build_array_type(element_type_tree
,
11151 build_index_type(max
));
11152 if (constructor_type
== error_mark_node
)
11153 return error_mark_node
;
11154 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
11155 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
11156 elt
->index
= size_int(0);
11157 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
11158 values
= build_constructor(constructor_type
, vec
);
11159 if (TREE_CONSTANT(elt
->value
))
11160 TREE_CONSTANT(values
) = 1;
11161 length_tree
= size_int(0);
11165 tree max
= size_int(this->vals()->size() - 1);
11166 tree constructor_type
= build_array_type(element_type_tree
,
11167 build_index_type(max
));
11168 if (constructor_type
== error_mark_node
)
11169 return error_mark_node
;
11170 values
= this->get_constructor_tree(context
, constructor_type
);
11171 length_tree
= size_int(this->vals()->size());
11174 if (values
== error_mark_node
)
11175 return error_mark_node
;
11177 bool is_constant_initializer
= TREE_CONSTANT(values
);
11179 // We have to copy the initial values into heap memory if we are in
11180 // a function or if the values are not constants. We also have to
11181 // copy them if they may contain pointers in a non-constant context,
11182 // as otherwise the garbage collector won't see them.
11183 bool copy_to_heap
= (context
->function() != NULL
11184 || !is_constant_initializer
11185 || (element_type
->has_pointer()
11186 && !context
->is_const()));
11188 if (is_constant_initializer
)
11190 tree tmp
= build_decl(this->location(), VAR_DECL
,
11191 create_tmp_var_name("C"), TREE_TYPE(values
));
11192 DECL_EXTERNAL(tmp
) = 0;
11193 TREE_PUBLIC(tmp
) = 0;
11194 TREE_STATIC(tmp
) = 1;
11195 DECL_ARTIFICIAL(tmp
) = 1;
11198 // If we are not copying the value to the heap, we will only
11199 // initialize the value once, so we can use this directly
11200 // rather than copying it. In that case we can't make it
11201 // read-only, because the program is permitted to change it.
11202 TREE_READONLY(tmp
) = 1;
11203 TREE_CONSTANT(tmp
) = 1;
11205 DECL_INITIAL(tmp
) = values
;
11206 rest_of_decl_compilation(tmp
, 1, 0);
11214 // the initializer will only run once.
11215 space
= build_fold_addr_expr(values
);
11220 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
11221 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
11223 space
= save_expr(space
);
11225 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
11226 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
11227 TREE_THIS_NOTRAP(ref
) = 1;
11228 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
11231 // Build a constructor for the open array.
11233 tree type_tree
= this->type()->get_tree(context
->gogo());
11234 if (type_tree
== error_mark_node
)
11235 return error_mark_node
;
11236 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
11238 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
11240 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11241 tree field
= TYPE_FIELDS(type_tree
);
11242 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
11243 elt
->index
= field
;
11244 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
11246 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11247 field
= DECL_CHAIN(field
);
11248 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
11249 elt
->index
= field
;
11250 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11252 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11253 field
= DECL_CHAIN(field
);
11254 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
11255 elt
->index
= field
;
11256 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11258 tree constructor
= build_constructor(type_tree
, init
);
11259 if (constructor
== error_mark_node
)
11260 return error_mark_node
;
11262 TREE_CONSTANT(constructor
) = 1;
11264 if (set
== NULL_TREE
)
11265 return constructor
;
11267 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
11270 // Make a slice composite literal. This is used by the type
11271 // descriptor code.
11274 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
11275 source_location location
)
11277 gcc_assert(type
->is_open_array_type());
11278 return new Open_array_construction_expression(type
, vals
, location
);
11281 // Construct a map.
11283 class Map_construction_expression
: public Expression
11286 Map_construction_expression(Type
* type
, Expression_list
* vals
,
11287 source_location location
)
11288 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
11289 type_(type
), vals_(vals
)
11290 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
11294 do_traverse(Traverse
* traverse
);
11298 { return this->type_
; }
11301 do_determine_type(const Type_context
*);
11304 do_check_types(Gogo
*);
11309 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
11314 do_get_tree(Translate_context
*);
11317 do_export(Export
*) const;
11320 // The type of the map to construct.
11322 // The list of values.
11323 Expression_list
* vals_
;
11329 Map_construction_expression::do_traverse(Traverse
* traverse
)
11331 if (this->vals_
!= NULL
11332 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11333 return TRAVERSE_EXIT
;
11334 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11335 return TRAVERSE_EXIT
;
11336 return TRAVERSE_CONTINUE
;
11339 // Final type determination.
11342 Map_construction_expression::do_determine_type(const Type_context
*)
11344 if (this->vals_
== NULL
)
11347 Map_type
* mt
= this->type_
->map_type();
11348 Type_context
key_context(mt
->key_type(), false);
11349 Type_context
val_context(mt
->val_type(), false);
11350 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11351 pv
!= this->vals_
->end();
11354 (*pv
)->determine_type(&key_context
);
11356 (*pv
)->determine_type(&val_context
);
11363 Map_construction_expression::do_check_types(Gogo
*)
11365 if (this->vals_
== NULL
)
11368 Map_type
* mt
= this->type_
->map_type();
11370 Type
* key_type
= mt
->key_type();
11371 Type
* val_type
= mt
->val_type();
11372 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11373 pv
!= this->vals_
->end();
11376 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
11378 error_at((*pv
)->location(),
11379 "incompatible type for element %d key in map construction",
11381 this->set_is_error();
11384 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
11386 error_at((*pv
)->location(),
11387 ("incompatible type for element %d value "
11388 "in map construction"),
11390 this->set_is_error();
11395 // Return a tree for constructing a map.
11398 Map_construction_expression::do_get_tree(Translate_context
* context
)
11400 Gogo
* gogo
= context
->gogo();
11401 source_location loc
= this->location();
11403 Map_type
* mt
= this->type_
->map_type();
11405 // Build a struct to hold the key and value.
11406 tree struct_type
= make_node(RECORD_TYPE
);
11408 Type
* key_type
= mt
->key_type();
11409 tree id
= get_identifier("__key");
11410 tree key_type_tree
= key_type
->get_tree(gogo
);
11411 if (key_type_tree
== error_mark_node
)
11412 return error_mark_node
;
11413 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type_tree
);
11414 DECL_CONTEXT(key_field
) = struct_type
;
11415 TYPE_FIELDS(struct_type
) = key_field
;
11417 Type
* val_type
= mt
->val_type();
11418 id
= get_identifier("__val");
11419 tree val_type_tree
= val_type
->get_tree(gogo
);
11420 if (val_type_tree
== error_mark_node
)
11421 return error_mark_node
;
11422 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type_tree
);
11423 DECL_CONTEXT(val_field
) = struct_type
;
11424 DECL_CHAIN(key_field
) = val_field
;
11426 layout_type(struct_type
);
11428 bool is_constant
= true;
11433 if (this->vals_
== NULL
|| this->vals_
->empty())
11435 valaddr
= null_pointer_node
;
11436 make_tmp
= NULL_TREE
;
11440 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11441 this->vals_
->size() / 2);
11443 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11444 pv
!= this->vals_
->end();
11447 bool one_is_constant
= true;
11449 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11451 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11452 elt
->index
= key_field
;
11453 tree val_tree
= (*pv
)->get_tree(context
);
11454 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11457 if (elt
->value
== error_mark_node
)
11458 return error_mark_node
;
11459 if (!TREE_CONSTANT(elt
->value
))
11460 one_is_constant
= false;
11464 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11465 elt
->index
= val_field
;
11466 val_tree
= (*pv
)->get_tree(context
);
11467 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11470 if (elt
->value
== error_mark_node
)
11471 return error_mark_node
;
11472 if (!TREE_CONSTANT(elt
->value
))
11473 one_is_constant
= false;
11475 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11476 elt
->index
= size_int(i
);
11477 elt
->value
= build_constructor(struct_type
, one
);
11478 if (one_is_constant
)
11479 TREE_CONSTANT(elt
->value
) = 1;
11481 is_constant
= false;
11484 tree index_type
= build_index_type(size_int(i
- 1));
11485 tree array_type
= build_array_type(struct_type
, index_type
);
11486 tree init
= build_constructor(array_type
, values
);
11488 TREE_CONSTANT(init
) = 1;
11490 if (current_function_decl
!= NULL
)
11492 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11493 DECL_INITIAL(tmp
) = init
;
11494 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11495 TREE_ADDRESSABLE(tmp
) = 1;
11499 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11500 DECL_EXTERNAL(tmp
) = 0;
11501 TREE_PUBLIC(tmp
) = 0;
11502 TREE_STATIC(tmp
) = 1;
11503 DECL_ARTIFICIAL(tmp
) = 1;
11504 if (!TREE_CONSTANT(init
))
11505 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11509 TREE_READONLY(tmp
) = 1;
11510 TREE_CONSTANT(tmp
) = 1;
11511 DECL_INITIAL(tmp
) = init
;
11512 make_tmp
= NULL_TREE
;
11514 rest_of_decl_compilation(tmp
, 1, 0);
11517 valaddr
= build_fold_addr_expr(tmp
);
11520 tree descriptor
= gogo
->map_descriptor(mt
);
11522 tree type_tree
= this->type_
->get_tree(gogo
);
11523 if (type_tree
== error_mark_node
)
11524 return error_mark_node
;
11526 static tree construct_map_fndecl
;
11527 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11529 "__go_construct_map",
11532 TREE_TYPE(descriptor
),
11537 TYPE_SIZE_UNIT(struct_type
),
11539 byte_position(val_field
),
11541 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11542 const_ptr_type_node
,
11543 fold_convert(const_ptr_type_node
, valaddr
));
11544 if (call
== error_mark_node
)
11545 return error_mark_node
;
11548 if (make_tmp
== NULL
)
11551 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11555 // Export an array construction.
11558 Map_construction_expression::do_export(Export
* exp
) const
11560 exp
->write_c_string("convert(");
11561 exp
->write_type(this->type_
);
11562 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11563 pv
!= this->vals_
->end();
11566 exp
->write_c_string(", ");
11567 (*pv
)->export_expression(exp
);
11569 exp
->write_c_string(")");
11572 // A general composite literal. This is lowered to a type specific
11575 class Composite_literal_expression
: public Parser_expression
11578 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11579 Expression_list
* vals
, source_location location
)
11580 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11581 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11586 do_traverse(Traverse
* traverse
);
11589 do_lower(Gogo
*, Named_object
*, int);
11594 return new Composite_literal_expression(this->type_
, this->depth_
,
11596 (this->vals_
== NULL
11598 : this->vals_
->copy()),
11604 lower_struct(Type
*);
11607 lower_array(Type
*);
11610 make_array(Type
*, Expression_list
*);
11613 lower_map(Gogo
*, Named_object
*, Type
*);
11615 // The type of the composite literal.
11617 // The depth within a list of composite literals within a composite
11618 // literal, when the type is omitted.
11620 // The values to put in the composite literal.
11621 Expression_list
* vals_
;
11622 // If this is true, then VALS_ is a list of pairs: a key and a
11623 // value. In an array initializer, a missing key will be NULL.
11630 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11632 if (this->vals_
!= NULL
11633 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11634 return TRAVERSE_EXIT
;
11635 return Type::traverse(this->type_
, traverse
);
11638 // Lower a generic composite literal into a specific version based on
11642 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
11644 Type
* type
= this->type_
;
11646 for (int depth
= this->depth_
; depth
> 0; --depth
)
11648 if (type
->array_type() != NULL
)
11649 type
= type
->array_type()->element_type();
11650 else if (type
->map_type() != NULL
)
11651 type
= type
->map_type()->val_type();
11654 if (!type
->is_error_type())
11655 error_at(this->location(),
11656 ("may only omit types within composite literals "
11657 "of slice, array, or map type"));
11658 return Expression::make_error(this->location());
11662 if (type
->is_error_type())
11663 return Expression::make_error(this->location());
11664 else if (type
->struct_type() != NULL
)
11665 return this->lower_struct(type
);
11666 else if (type
->array_type() != NULL
)
11667 return this->lower_array(type
);
11668 else if (type
->map_type() != NULL
)
11669 return this->lower_map(gogo
, function
, type
);
11672 error_at(this->location(),
11673 ("expected struct, slice, array, or map type "
11674 "for composite literal"));
11675 return Expression::make_error(this->location());
11679 // Lower a struct composite literal.
11682 Composite_literal_expression::lower_struct(Type
* type
)
11684 source_location location
= this->location();
11685 Struct_type
* st
= type
->struct_type();
11686 if (this->vals_
== NULL
|| !this->has_keys_
)
11687 return new Struct_construction_expression(type
, this->vals_
, location
);
11689 size_t field_count
= st
->field_count();
11690 std::vector
<Expression
*> vals(field_count
);
11691 Expression_list::const_iterator p
= this->vals_
->begin();
11692 while (p
!= this->vals_
->end())
11694 Expression
* name_expr
= *p
;
11697 gcc_assert(p
!= this->vals_
->end());
11698 Expression
* val
= *p
;
11702 if (name_expr
== NULL
)
11704 error_at(val
->location(), "mixture of field and value initializers");
11705 return Expression::make_error(location
);
11708 bool bad_key
= false;
11710 switch (name_expr
->classification())
11712 case EXPRESSION_UNKNOWN_REFERENCE
:
11713 name
= name_expr
->unknown_expression()->name();
11716 case EXPRESSION_CONST_REFERENCE
:
11717 name
= static_cast<Const_expression
*>(name_expr
)->name();
11720 case EXPRESSION_TYPE
:
11722 Type
* t
= name_expr
->type();
11723 Named_type
* nt
= t
->named_type();
11731 case EXPRESSION_VAR_REFERENCE
:
11732 name
= name_expr
->var_expression()->name();
11735 case EXPRESSION_FUNC_REFERENCE
:
11736 name
= name_expr
->func_expression()->name();
11739 case EXPRESSION_UNARY
:
11740 // If there is a local variable around with the same name as
11741 // the field, and this occurs in the closure, then the
11742 // parser may turn the field reference into an indirection
11743 // through the closure. FIXME: This is a mess.
11746 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11747 if (ue
->op() == OPERATOR_MULT
)
11749 Field_reference_expression
* fre
=
11750 ue
->operand()->field_reference_expression();
11754 fre
->expr()->type()->deref()->struct_type();
11757 const Struct_field
* sf
= st
->field(fre
->field_index());
11758 name
= sf
->field_name();
11760 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11761 size_t buflen
= strlen(buf
);
11762 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11765 name
= name
.substr(0, name
.length() - buflen
);
11780 error_at(name_expr
->location(), "expected struct field name");
11781 return Expression::make_error(location
);
11784 unsigned int index
;
11785 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11788 error_at(name_expr
->location(), "unknown field %qs in %qs",
11789 Gogo::message_name(name
).c_str(),
11790 (type
->named_type() != NULL
11791 ? type
->named_type()->message_name().c_str()
11792 : "unnamed struct"));
11793 return Expression::make_error(location
);
11795 if (vals
[index
] != NULL
)
11797 error_at(name_expr
->location(),
11798 "duplicate value for field %qs in %qs",
11799 Gogo::message_name(name
).c_str(),
11800 (type
->named_type() != NULL
11801 ? type
->named_type()->message_name().c_str()
11802 : "unnamed struct"));
11803 return Expression::make_error(location
);
11809 Expression_list
* list
= new Expression_list
;
11810 list
->reserve(field_count
);
11811 for (size_t i
= 0; i
< field_count
; ++i
)
11812 list
->push_back(vals
[i
]);
11814 return new Struct_construction_expression(type
, list
, location
);
11817 // Lower an array composite literal.
11820 Composite_literal_expression::lower_array(Type
* type
)
11822 source_location location
= this->location();
11823 if (this->vals_
== NULL
|| !this->has_keys_
)
11824 return this->make_array(type
, this->vals_
);
11826 std::vector
<Expression
*> vals
;
11827 vals
.reserve(this->vals_
->size());
11828 unsigned long index
= 0;
11829 Expression_list::const_iterator p
= this->vals_
->begin();
11830 while (p
!= this->vals_
->end())
11832 Expression
* index_expr
= *p
;
11835 gcc_assert(p
!= this->vals_
->end());
11836 Expression
* val
= *p
;
11840 if (index_expr
!= NULL
)
11845 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
11848 error_at(index_expr
->location(),
11849 "index expression is not integer constant");
11850 return Expression::make_error(location
);
11852 if (mpz_sgn(ival
) < 0)
11855 error_at(index_expr
->location(), "index expression is negative");
11856 return Expression::make_error(location
);
11858 index
= mpz_get_ui(ival
);
11859 if (mpz_cmp_ui(ival
, index
) != 0)
11862 error_at(index_expr
->location(), "index value overflow");
11863 return Expression::make_error(location
);
11868 if (index
== vals
.size())
11869 vals
.push_back(val
);
11872 if (index
> vals
.size())
11874 vals
.reserve(index
+ 32);
11875 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
11877 if (vals
[index
] != NULL
)
11879 error_at((index_expr
!= NULL
11880 ? index_expr
->location()
11881 : val
->location()),
11882 "duplicate value for index %lu",
11884 return Expression::make_error(location
);
11892 size_t size
= vals
.size();
11893 Expression_list
* list
= new Expression_list
;
11894 list
->reserve(size
);
11895 for (size_t i
= 0; i
< size
; ++i
)
11896 list
->push_back(vals
[i
]);
11898 return this->make_array(type
, list
);
11901 // Actually build the array composite literal. This handles
11905 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
11907 source_location location
= this->location();
11908 Array_type
* at
= type
->array_type();
11909 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
11911 size_t size
= vals
== NULL
? 0 : vals
->size();
11913 mpz_init_set_ui(vlen
, size
);
11914 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
11916 at
= Type::make_array_type(at
->element_type(), elen
);
11919 if (at
->length() != NULL
)
11920 return new Fixed_array_construction_expression(type
, vals
, location
);
11922 return new Open_array_construction_expression(type
, vals
, location
);
11925 // Lower a map composite literal.
11928 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
11931 source_location location
= this->location();
11932 if (this->vals_
!= NULL
)
11934 if (!this->has_keys_
)
11936 error_at(location
, "map composite literal must have keys");
11937 return Expression::make_error(location
);
11940 for (Expression_list::iterator p
= this->vals_
->begin();
11941 p
!= this->vals_
->end();
11947 error_at((*p
)->location(),
11948 "map composite literal must have keys for every value");
11949 return Expression::make_error(location
);
11951 // Make sure we have lowered the key; it may not have been
11952 // lowered in order to handle keys for struct composite
11953 // literals. Lower it now to get the right error message.
11954 if ((*p
)->unknown_expression() != NULL
)
11956 (*p
)->unknown_expression()->clear_is_composite_literal_key();
11957 gogo
->lower_expression(function
, &*p
);
11958 gcc_assert((*p
)->is_error_expression());
11959 return Expression::make_error(location
);
11964 return new Map_construction_expression(type
, this->vals_
, location
);
11967 // Make a composite literal expression.
11970 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
11971 Expression_list
* vals
,
11972 source_location location
)
11974 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
11978 // Return whether this expression is a composite literal.
11981 Expression::is_composite_literal() const
11983 switch (this->classification_
)
11985 case EXPRESSION_COMPOSITE_LITERAL
:
11986 case EXPRESSION_STRUCT_CONSTRUCTION
:
11987 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11988 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11989 case EXPRESSION_MAP_CONSTRUCTION
:
11996 // Return whether this expression is a composite literal which is not
12000 Expression::is_nonconstant_composite_literal() const
12002 switch (this->classification_
)
12004 case EXPRESSION_STRUCT_CONSTRUCTION
:
12006 const Struct_construction_expression
*psce
=
12007 static_cast<const Struct_construction_expression
*>(this);
12008 return !psce
->is_constant_struct();
12010 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
12012 const Fixed_array_construction_expression
*pace
=
12013 static_cast<const Fixed_array_construction_expression
*>(this);
12014 return !pace
->is_constant_array();
12016 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
12018 const Open_array_construction_expression
*pace
=
12019 static_cast<const Open_array_construction_expression
*>(this);
12020 return !pace
->is_constant_array();
12022 case EXPRESSION_MAP_CONSTRUCTION
:
12029 // Return true if this is a reference to a local variable.
12032 Expression::is_local_variable() const
12034 const Var_expression
* ve
= this->var_expression();
12037 const Named_object
* no
= ve
->named_object();
12038 return (no
->is_result_variable()
12039 || (no
->is_variable() && !no
->var_value()->is_global()));
12042 // Class Type_guard_expression.
12047 Type_guard_expression::do_traverse(Traverse
* traverse
)
12049 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
12050 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
12051 return TRAVERSE_EXIT
;
12052 return TRAVERSE_CONTINUE
;
12055 // Check types of a type guard expression. The expression must have
12056 // an interface type, but the actual type conversion is checked at run
12060 Type_guard_expression::do_check_types(Gogo
*)
12062 // 6g permits using a type guard with unsafe.pointer; we are
12064 Type
* expr_type
= this->expr_
->type();
12065 if (expr_type
->is_unsafe_pointer_type())
12067 if (this->type_
->points_to() == NULL
12068 && (this->type_
->integer_type() == NULL
12069 || (this->type_
->forwarded()
12070 != Type::lookup_integer_type("uintptr"))))
12071 this->report_error(_("invalid unsafe.Pointer conversion"));
12073 else if (this->type_
->is_unsafe_pointer_type())
12075 if (expr_type
->points_to() == NULL
12076 && (expr_type
->integer_type() == NULL
12077 || (expr_type
->forwarded()
12078 != Type::lookup_integer_type("uintptr"))))
12079 this->report_error(_("invalid unsafe.Pointer conversion"));
12081 else if (expr_type
->interface_type() == NULL
)
12083 if (!expr_type
->is_error_type() && !this->type_
->is_error_type())
12084 this->report_error(_("type assertion only valid for interface types"));
12085 this->set_is_error();
12087 else if (this->type_
->interface_type() == NULL
)
12089 std::string reason
;
12090 if (!expr_type
->interface_type()->implements_interface(this->type_
,
12093 if (!this->type_
->is_error_type())
12095 if (reason
.empty())
12096 this->report_error(_("impossible type assertion: "
12097 "type does not implement interface"));
12099 error_at(this->location(),
12100 ("impossible type assertion: "
12101 "type does not implement interface (%s)"),
12104 this->set_is_error();
12109 // Return a tree for a type guard expression.
12112 Type_guard_expression::do_get_tree(Translate_context
* context
)
12114 Gogo
* gogo
= context
->gogo();
12115 tree expr_tree
= this->expr_
->get_tree(context
);
12116 if (expr_tree
== error_mark_node
)
12117 return error_mark_node
;
12118 Type
* expr_type
= this->expr_
->type();
12119 if ((this->type_
->is_unsafe_pointer_type()
12120 && (expr_type
->points_to() != NULL
12121 || expr_type
->integer_type() != NULL
))
12122 || (expr_type
->is_unsafe_pointer_type()
12123 && this->type_
->points_to() != NULL
))
12124 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
12125 else if (expr_type
->is_unsafe_pointer_type()
12126 && this->type_
->integer_type() != NULL
)
12127 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
12128 else if (this->type_
->interface_type() != NULL
)
12129 return Expression::convert_interface_to_interface(context
, this->type_
,
12130 this->expr_
->type(),
12134 return Expression::convert_for_assignment(context
, this->type_
,
12135 this->expr_
->type(), expr_tree
,
12139 // Make a type guard expression.
12142 Expression::make_type_guard(Expression
* expr
, Type
* type
,
12143 source_location location
)
12145 return new Type_guard_expression(expr
, type
, location
);
12148 // Class Heap_composite_expression.
12150 // When you take the address of a composite literal, it is allocated
12151 // on the heap. This class implements that.
12153 class Heap_composite_expression
: public Expression
12156 Heap_composite_expression(Expression
* expr
, source_location location
)
12157 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
12163 do_traverse(Traverse
* traverse
)
12164 { return Expression::traverse(&this->expr_
, traverse
); }
12168 { return Type::make_pointer_type(this->expr_
->type()); }
12171 do_determine_type(const Type_context
*)
12172 { this->expr_
->determine_type_no_context(); }
12177 return Expression::make_heap_composite(this->expr_
->copy(),
12182 do_get_tree(Translate_context
*);
12184 // We only export global objects, and the parser does not generate
12185 // this in global scope.
12187 do_export(Export
*) const
12188 { gcc_unreachable(); }
12191 // The composite literal which is being put on the heap.
12195 // Return a tree which allocates a composite literal on the heap.
12198 Heap_composite_expression::do_get_tree(Translate_context
* context
)
12200 tree expr_tree
= this->expr_
->get_tree(context
);
12201 if (expr_tree
== error_mark_node
)
12202 return error_mark_node
;
12203 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
12204 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
12205 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
12206 expr_size
, this->location());
12207 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
12208 space
= save_expr(space
);
12209 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
12210 TREE_THIS_NOTRAP(ref
) = 1;
12211 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
12212 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
12214 SET_EXPR_LOCATION(ret
, this->location());
12218 // Allocate a composite literal on the heap.
12221 Expression::make_heap_composite(Expression
* expr
, source_location location
)
12223 return new Heap_composite_expression(expr
, location
);
12226 // Class Receive_expression.
12228 // Return the type of a receive expression.
12231 Receive_expression::do_type()
12233 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12234 if (channel_type
== NULL
)
12235 return Type::make_error_type();
12236 return channel_type
->element_type();
12239 // Check types for a receive expression.
12242 Receive_expression::do_check_types(Gogo
*)
12244 Type
* type
= this->channel_
->type();
12245 if (type
->is_error_type())
12247 this->set_is_error();
12250 if (type
->channel_type() == NULL
)
12252 this->report_error(_("expected channel"));
12255 if (!type
->channel_type()->may_receive())
12257 this->report_error(_("invalid receive on send-only channel"));
12262 // Get a tree for a receive expression.
12265 Receive_expression::do_get_tree(Translate_context
* context
)
12267 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12268 if (channel_type
== NULL
)
12270 gcc_assert(this->channel_
->type()->is_error_type());
12271 return error_mark_node
;
12273 Type
* element_type
= channel_type
->element_type();
12274 tree element_type_tree
= element_type
->get_tree(context
->gogo());
12276 tree channel
= this->channel_
->get_tree(context
);
12277 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
12278 return error_mark_node
;
12280 return Gogo::receive_from_channel(element_type_tree
, channel
,
12281 this->for_select_
, this->location());
12284 // Make a receive expression.
12286 Receive_expression
*
12287 Expression::make_receive(Expression
* channel
, source_location location
)
12289 return new Receive_expression(channel
, location
);
12292 // Class Send_expression.
12297 Send_expression::do_traverse(Traverse
* traverse
)
12299 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
12300 return TRAVERSE_EXIT
;
12301 return Expression::traverse(&this->val_
, traverse
);
12307 Send_expression::do_type()
12309 return Type::lookup_bool_type();
12315 Send_expression::do_determine_type(const Type_context
*)
12317 this->channel_
->determine_type_no_context();
12319 Type
* type
= this->channel_
->type();
12320 Type_context subcontext
;
12321 if (type
->channel_type() != NULL
)
12322 subcontext
.type
= type
->channel_type()->element_type();
12323 this->val_
->determine_type(&subcontext
);
12329 Send_expression::do_check_types(Gogo
*)
12331 Type
* type
= this->channel_
->type();
12332 if (type
->is_error_type())
12334 this->set_is_error();
12337 Channel_type
* channel_type
= type
->channel_type();
12338 if (channel_type
== NULL
)
12340 error_at(this->location(), "left operand of %<<-%> must be channel");
12341 this->set_is_error();
12344 Type
* element_type
= channel_type
->element_type();
12345 if (element_type
!= NULL
12346 && !Type::are_assignable(element_type
, this->val_
->type(), NULL
))
12348 this->report_error(_("incompatible types in send"));
12351 if (!channel_type
->may_send())
12353 this->report_error(_("invalid send on receive-only channel"));
12358 // Get a tree for a send expression.
12361 Send_expression::do_get_tree(Translate_context
* context
)
12363 tree channel
= this->channel_
->get_tree(context
);
12364 tree val
= this->val_
->get_tree(context
);
12365 if (channel
== error_mark_node
|| val
== error_mark_node
)
12366 return error_mark_node
;
12367 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12368 val
= Expression::convert_for_assignment(context
,
12369 channel_type
->element_type(),
12370 this->val_
->type(),
12373 return Gogo::send_on_channel(channel
, val
, this->is_value_discarded_
,
12374 this->for_select_
, this->location());
12377 // Make a send expression
12380 Expression::make_send(Expression
* channel
, Expression
* val
,
12381 source_location location
)
12383 return new Send_expression(channel
, val
, location
);
12386 // An expression which evaluates to a pointer to the type descriptor
12389 class Type_descriptor_expression
: public Expression
12392 Type_descriptor_expression(Type
* type
, source_location location
)
12393 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
12400 { return Type::make_type_descriptor_ptr_type(); }
12403 do_determine_type(const Type_context
*)
12411 do_get_tree(Translate_context
* context
)
12412 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
12415 // The type for which this is the descriptor.
12419 // Make a type descriptor expression.
12422 Expression::make_type_descriptor(Type
* type
, source_location location
)
12424 return new Type_descriptor_expression(type
, location
);
12427 // An expression which evaluates to some characteristic of a type.
12428 // This is only used to initialize fields of a type descriptor. Using
12429 // a new expression class is slightly inefficient but gives us a good
12430 // separation between the frontend and the middle-end with regard to
12431 // how types are laid out.
12433 class Type_info_expression
: public Expression
12436 Type_info_expression(Type
* type
, Type_info type_info
)
12437 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
12438 type_(type
), type_info_(type_info
)
12446 do_determine_type(const Type_context
*)
12454 do_get_tree(Translate_context
* context
);
12457 // The type for which we are getting information.
12459 // What information we want.
12460 Type_info type_info_
;
12463 // The type is chosen to match what the type descriptor struct
12467 Type_info_expression::do_type()
12469 switch (this->type_info_
)
12471 case TYPE_INFO_SIZE
:
12472 return Type::lookup_integer_type("uintptr");
12473 case TYPE_INFO_ALIGNMENT
:
12474 case TYPE_INFO_FIELD_ALIGNMENT
:
12475 return Type::lookup_integer_type("uint8");
12481 // Return type information in GENERIC.
12484 Type_info_expression::do_get_tree(Translate_context
* context
)
12486 tree type_tree
= this->type_
->get_tree(context
->gogo());
12487 if (type_tree
== error_mark_node
)
12488 return error_mark_node
;
12490 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12491 gcc_assert(val_type_tree
!= error_mark_node
);
12493 if (this->type_info_
== TYPE_INFO_SIZE
)
12494 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12495 TYPE_SIZE_UNIT(type_tree
));
12499 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12500 val
= go_type_alignment(type_tree
);
12502 val
= go_field_alignment(type_tree
);
12503 return build_int_cstu(val_type_tree
, val
);
12507 // Make a type info expression.
12510 Expression::make_type_info(Type
* type
, Type_info type_info
)
12512 return new Type_info_expression(type
, type_info
);
12515 // An expression which evaluates to the offset of a field within a
12516 // struct. This, like Type_info_expression, q.v., is only used to
12517 // initialize fields of a type descriptor.
12519 class Struct_field_offset_expression
: public Expression
12522 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12523 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12524 type_(type
), field_(field
)
12530 { return Type::lookup_integer_type("uintptr"); }
12533 do_determine_type(const Type_context
*)
12541 do_get_tree(Translate_context
* context
);
12544 // The type of the struct.
12545 Struct_type
* type_
;
12547 const Struct_field
* field_
;
12550 // Return a struct field offset in GENERIC.
12553 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12555 tree type_tree
= this->type_
->get_tree(context
->gogo());
12556 if (type_tree
== error_mark_node
)
12557 return error_mark_node
;
12559 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12560 gcc_assert(val_type_tree
!= error_mark_node
);
12562 const Struct_field_list
* fields
= this->type_
->fields();
12563 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12564 Struct_field_list::const_iterator p
;
12565 for (p
= fields
->begin();
12566 p
!= fields
->end();
12567 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12569 gcc_assert(struct_field_tree
!= NULL_TREE
);
12570 if (&*p
== this->field_
)
12573 gcc_assert(&*p
== this->field_
);
12575 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12576 byte_position(struct_field_tree
));
12579 // Make an expression for a struct field offset.
12582 Expression::make_struct_field_offset(Struct_type
* type
,
12583 const Struct_field
* field
)
12585 return new Struct_field_offset_expression(type
, field
);
12588 // An expression which evaluates to the address of an unnamed label.
12590 class Label_addr_expression
: public Expression
12593 Label_addr_expression(Label
* label
, source_location location
)
12594 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12601 { return Type::make_pointer_type(Type::make_void_type()); }
12604 do_determine_type(const Type_context
*)
12609 { return new Label_addr_expression(this->label_
, this->location()); }
12612 do_get_tree(Translate_context
*)
12613 { return this->label_
->get_addr(this->location()); }
12616 // The label whose address we are taking.
12620 // Make an expression for the address of an unnamed label.
12623 Expression::make_label_addr(Label
* label
, source_location location
)
12625 return new Label_addr_expression(label
, location
);
12628 // Import an expression. This comes at the end in order to see the
12629 // various class definitions.
12632 Expression::import_expression(Import
* imp
)
12634 int c
= imp
->peek_char();
12635 if (imp
->match_c_string("- ")
12636 || imp
->match_c_string("! ")
12637 || imp
->match_c_string("^ "))
12638 return Unary_expression::do_import(imp
);
12640 return Binary_expression::do_import(imp
);
12641 else if (imp
->match_c_string("true")
12642 || imp
->match_c_string("false"))
12643 return Boolean_expression::do_import(imp
);
12645 return String_expression::do_import(imp
);
12646 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12648 // This handles integers, floats and complex constants.
12649 return Integer_expression::do_import(imp
);
12651 else if (imp
->match_c_string("nil"))
12652 return Nil_expression::do_import(imp
);
12653 else if (imp
->match_c_string("convert"))
12654 return Type_conversion_expression::do_import(imp
);
12657 error_at(imp
->location(), "import error: expected expression");
12658 return Expression::make_error(imp
->location());
12662 // Class Expression_list.
12664 // Traverse the list.
12667 Expression_list::traverse(Traverse
* traverse
)
12669 for (Expression_list::iterator p
= this->begin();
12675 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12676 return TRAVERSE_EXIT
;
12679 return TRAVERSE_CONTINUE
;
12685 Expression_list::copy()
12687 Expression_list
* ret
= new Expression_list();
12688 for (Expression_list::iterator p
= this->begin();
12693 ret
->push_back(NULL
);
12695 ret
->push_back((*p
)->copy());
12700 // Return whether an expression list has an error expression.
12703 Expression_list::contains_error() const
12705 for (Expression_list::const_iterator p
= this->begin();
12708 if (*p
!= NULL
&& (*p
)->is_error_expression())