1 // expressions.cc -- Go frontend expression handling.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #ifndef ENABLE_BUILD_WITH_CXX
20 #include "tree-iterator.h"
25 #ifndef ENABLE_BUILD_WITH_CXX
34 #include "statements.h"
36 #include "expressions.h"
40 Expression::Expression(Expression_classification classification
,
41 source_location location
)
42 : classification_(classification
), location_(location
)
46 Expression::~Expression()
50 // If this expression has a constant integer value, return it.
53 Expression::integer_constant_value(bool iota_is_constant
, mpz_t val
,
57 return this->do_integer_constant_value(iota_is_constant
, val
, ptype
);
60 // If this expression has a constant floating point value, return it.
63 Expression::float_constant_value(mpfr_t val
, Type
** ptype
) const
66 if (this->do_float_constant_value(val
, ptype
))
72 if (!this->do_integer_constant_value(false, ival
, &t
))
76 mpfr_set_z(val
, ival
, GMP_RNDN
);
83 // If this expression has a constant complex value, return it.
86 Expression::complex_constant_value(mpfr_t real
, mpfr_t imag
,
90 if (this->do_complex_constant_value(real
, imag
, ptype
))
93 if (this->float_constant_value(real
, &t
))
95 mpfr_set_ui(imag
, 0, GMP_RNDN
);
101 // Traverse the expressions.
104 Expression::traverse(Expression
** pexpr
, Traverse
* traverse
)
106 Expression
* expr
= *pexpr
;
107 if ((traverse
->traverse_mask() & Traverse::traverse_expressions
) != 0)
109 int t
= traverse
->expression(pexpr
);
110 if (t
== TRAVERSE_EXIT
)
111 return TRAVERSE_EXIT
;
112 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
113 return TRAVERSE_CONTINUE
;
115 return expr
->do_traverse(traverse
);
118 // Traverse subexpressions of this expression.
121 Expression::traverse_subexpressions(Traverse
* traverse
)
123 return this->do_traverse(traverse
);
126 // Default implementation for do_traverse for child classes.
129 Expression::do_traverse(Traverse
*)
131 return TRAVERSE_CONTINUE
;
134 // This virtual function is called by the parser if the value of this
135 // expression is being discarded. By default, we warn. Expressions
136 // with side effects override.
139 Expression::do_discarding_value()
141 this->warn_about_unused_value();
144 // This virtual function is called to export expressions. This will
145 // only be used by expressions which may be constant.
148 Expression::do_export(Export
*) const
153 // Warn that the value of the expression is not used.
156 Expression::warn_about_unused_value()
158 warning_at(this->location(), OPT_Wunused_value
, "value computed is not used");
161 // Note that this expression is an error. This is called by children
162 // when they discover an error.
165 Expression::set_is_error()
167 this->classification_
= EXPRESSION_ERROR
;
170 // For children to call to report an error conveniently.
173 Expression::report_error(const char* msg
)
175 error_at(this->location_
, "%s", msg
);
176 this->set_is_error();
179 // Set types of variables and constants. This is implemented by the
183 Expression::determine_type(const Type_context
* context
)
185 this->do_determine_type(context
);
188 // Set types when there is no context.
191 Expression::determine_type_no_context()
193 Type_context context
;
194 this->do_determine_type(&context
);
197 // Return a tree handling any conversions which must be done during
201 Expression::convert_for_assignment(Translate_context
* context
, Type
* lhs_type
,
202 Type
* rhs_type
, tree rhs_tree
,
203 source_location location
)
205 if (lhs_type
== rhs_type
)
208 if (lhs_type
->is_error_type() || rhs_type
->is_error_type())
209 return error_mark_node
;
211 if (lhs_type
->is_undefined() || rhs_type
->is_undefined())
213 // Make sure we report the error.
216 return error_mark_node
;
219 if (rhs_tree
== error_mark_node
|| TREE_TYPE(rhs_tree
) == error_mark_node
)
220 return error_mark_node
;
222 Gogo
* gogo
= context
->gogo();
224 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
225 if (lhs_type_tree
== error_mark_node
)
226 return error_mark_node
;
228 if (lhs_type
->interface_type() != NULL
)
230 if (rhs_type
->interface_type() == NULL
)
231 return Expression::convert_type_to_interface(context
, lhs_type
,
235 return Expression::convert_interface_to_interface(context
, lhs_type
,
239 else if (rhs_type
->interface_type() != NULL
)
240 return Expression::convert_interface_to_type(context
, lhs_type
, rhs_type
,
242 else if (lhs_type
->is_open_array_type()
243 && rhs_type
->is_nil_type())
245 // Assigning nil to an open array.
246 gcc_assert(TREE_CODE(lhs_type_tree
) == RECORD_TYPE
);
248 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
250 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
251 tree field
= TYPE_FIELDS(lhs_type_tree
);
252 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
255 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
257 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
258 field
= DECL_CHAIN(field
);
259 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
262 elt
->value
= fold_convert(TREE_TYPE(field
), integer_zero_node
);
264 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
265 field
= DECL_CHAIN(field
);
266 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
269 elt
->value
= fold_convert(TREE_TYPE(field
), integer_zero_node
);
271 tree val
= build_constructor(lhs_type_tree
, init
);
272 TREE_CONSTANT(val
) = 1;
276 else if (rhs_type
->is_nil_type())
278 // The left hand side should be a pointer type at the tree
280 gcc_assert(POINTER_TYPE_P(lhs_type_tree
));
281 return fold_convert(lhs_type_tree
, null_pointer_node
);
283 else if (lhs_type_tree
== TREE_TYPE(rhs_tree
))
285 // No conversion is needed.
288 else if (POINTER_TYPE_P(lhs_type_tree
)
289 || INTEGRAL_TYPE_P(lhs_type_tree
)
290 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
291 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
292 return fold_convert_loc(location
, lhs_type_tree
, rhs_tree
);
293 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
294 && TREE_CODE(TREE_TYPE(rhs_tree
)) == RECORD_TYPE
)
296 // This conversion must be permitted by Go, or we wouldn't have
298 gcc_assert(int_size_in_bytes(lhs_type_tree
)
299 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
300 return fold_build1_loc(location
, VIEW_CONVERT_EXPR
, lhs_type_tree
,
305 gcc_assert(useless_type_conversion_p(lhs_type_tree
, TREE_TYPE(rhs_tree
)));
310 // Return a tree for a conversion from a non-interface type to an
314 Expression::convert_type_to_interface(Translate_context
* context
,
315 Type
* lhs_type
, Type
* rhs_type
,
316 tree rhs_tree
, source_location location
)
318 Gogo
* gogo
= context
->gogo();
319 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
320 bool lhs_is_empty
= lhs_interface_type
->is_empty();
322 // Since RHS_TYPE is a static type, we can create the interface
323 // method table at compile time.
325 // When setting an interface to nil, we just set both fields to
327 if (rhs_type
->is_nil_type())
328 return lhs_type
->get_init_tree(gogo
, false);
330 // This should have been checked already.
331 gcc_assert(lhs_interface_type
->implements_interface(rhs_type
, NULL
));
333 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
334 if (lhs_type_tree
== error_mark_node
)
335 return error_mark_node
;
337 // An interface is a tuple. If LHS_TYPE is an empty interface type,
338 // then the first field is the type descriptor for RHS_TYPE.
339 // Otherwise it is the interface method table for RHS_TYPE.
340 tree first_field_value
;
342 first_field_value
= rhs_type
->type_descriptor_pointer(gogo
);
345 // Build the interface method table for this interface and this
346 // object type: a list of function pointers for each interface
348 Named_type
* rhs_named_type
= rhs_type
->named_type();
349 bool is_pointer
= false;
350 if (rhs_named_type
== NULL
)
352 rhs_named_type
= rhs_type
->deref()->named_type();
356 if (rhs_named_type
== NULL
)
357 method_table
= null_pointer_node
;
360 rhs_named_type
->interface_method_table(gogo
, lhs_interface_type
,
362 first_field_value
= fold_convert_loc(location
, const_ptr_type_node
,
366 // Start building a constructor for the value we will return.
368 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
370 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
371 tree field
= TYPE_FIELDS(lhs_type_tree
);
372 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
373 (lhs_is_empty
? "__type_descriptor" : "__methods")) == 0);
375 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), first_field_value
);
377 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
378 field
= DECL_CHAIN(field
);
379 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
382 if (rhs_type
->points_to() != NULL
)
384 // We are assigning a pointer to the interface; the interface
385 // holds the pointer itself.
386 elt
->value
= rhs_tree
;
387 return build_constructor(lhs_type_tree
, init
);
390 // We are assigning a non-pointer value to the interface; the
391 // interface gets a copy of the value in the heap.
393 tree object_size
= TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree
));
395 tree space
= gogo
->allocate_memory(rhs_type
, object_size
, location
);
396 space
= fold_convert_loc(location
, build_pointer_type(TREE_TYPE(rhs_tree
)),
398 space
= save_expr(space
);
400 tree ref
= build_fold_indirect_ref_loc(location
, space
);
401 TREE_THIS_NOTRAP(ref
) = 1;
402 tree set
= fold_build2_loc(location
, MODIFY_EXPR
, void_type_node
,
405 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), space
);
407 return build2(COMPOUND_EXPR
, lhs_type_tree
, set
,
408 build_constructor(lhs_type_tree
, init
));
411 // Return a tree for the type descriptor of RHS_TREE, which has
412 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
416 Expression::get_interface_type_descriptor(Translate_context
*,
417 Type
* rhs_type
, tree rhs_tree
,
418 source_location location
)
420 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
421 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
422 tree rhs_field
= TYPE_FIELDS(rhs_type_tree
);
423 tree v
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
425 if (rhs_type
->interface_type()->is_empty())
427 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)),
428 "__type_descriptor") == 0);
432 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__methods")
434 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v
)));
436 tree v1
= build_fold_indirect_ref_loc(location
, v
);
437 gcc_assert(TREE_CODE(TREE_TYPE(v1
)) == RECORD_TYPE
);
438 tree f
= TYPE_FIELDS(TREE_TYPE(v1
));
439 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f
)), "__type_descriptor")
441 v1
= build3(COMPONENT_REF
, TREE_TYPE(f
), v1
, f
, NULL_TREE
);
443 tree eq
= fold_build2_loc(location
, EQ_EXPR
, boolean_type_node
, v
,
444 fold_convert_loc(location
, TREE_TYPE(v
),
446 tree n
= fold_convert_loc(location
, TREE_TYPE(v1
), null_pointer_node
);
447 return fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(v1
),
451 // Return a tree for the conversion of an interface type to an
455 Expression::convert_interface_to_interface(Translate_context
* context
,
456 Type
*lhs_type
, Type
*rhs_type
,
457 tree rhs_tree
, bool for_type_guard
,
458 source_location location
)
460 Gogo
* gogo
= context
->gogo();
461 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
462 bool lhs_is_empty
= lhs_interface_type
->is_empty();
464 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
465 if (lhs_type_tree
== error_mark_node
)
466 return error_mark_node
;
468 // In the general case this requires runtime examination of the type
469 // method table to match it up with the interface methods.
471 // FIXME: If all of the methods in the right hand side interface
472 // also appear in the left hand side interface, then we don't need
473 // to do a runtime check, although we still need to build a new
476 // Get the type descriptor for the right hand side. This will be
477 // NULL for a nil interface.
479 if (!DECL_P(rhs_tree
))
480 rhs_tree
= save_expr(rhs_tree
);
482 tree rhs_type_descriptor
=
483 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
486 // The result is going to be a two element constructor.
488 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
490 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
491 tree field
= TYPE_FIELDS(lhs_type_tree
);
496 // A type assertion fails when converting a nil interface.
497 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
498 static tree assert_interface_decl
;
499 tree call
= Gogo::call_builtin(&assert_interface_decl
,
501 "__go_assert_interface",
504 TREE_TYPE(lhs_type_descriptor
),
506 TREE_TYPE(rhs_type_descriptor
),
507 rhs_type_descriptor
);
508 if (call
== error_mark_node
)
509 return error_mark_node
;
510 // This will panic if the interface conversion fails.
511 TREE_NOTHROW(assert_interface_decl
) = 0;
512 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
514 else if (lhs_is_empty
)
516 // A convertion to an empty interface always succeeds, and the
517 // first field is just the type descriptor of the object.
518 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
519 "__type_descriptor") == 0);
520 gcc_assert(TREE_TYPE(field
) == TREE_TYPE(rhs_type_descriptor
));
521 elt
->value
= rhs_type_descriptor
;
525 // A conversion to a non-empty interface may fail, but unlike a
526 // type assertion converting nil will always succeed.
527 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods")
529 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
530 static tree convert_interface_decl
;
531 tree call
= Gogo::call_builtin(&convert_interface_decl
,
533 "__go_convert_interface",
536 TREE_TYPE(lhs_type_descriptor
),
538 TREE_TYPE(rhs_type_descriptor
),
539 rhs_type_descriptor
);
540 if (call
== error_mark_node
)
541 return error_mark_node
;
542 // This will panic if the interface conversion fails.
543 TREE_NOTHROW(convert_interface_decl
) = 0;
544 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
547 // The second field is simply the object pointer.
549 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
550 field
= DECL_CHAIN(field
);
551 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
554 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
555 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
556 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
557 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
558 elt
->value
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
561 return build_constructor(lhs_type_tree
, init
);
564 // Return a tree for the conversion of an interface type to a
565 // non-interface type.
568 Expression::convert_interface_to_type(Translate_context
* context
,
569 Type
*lhs_type
, Type
* rhs_type
,
570 tree rhs_tree
, source_location location
)
572 Gogo
* gogo
= context
->gogo();
573 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
575 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
576 if (lhs_type_tree
== error_mark_node
)
577 return error_mark_node
;
579 // Call a function to check that the type is valid. The function
580 // will panic with an appropriate runtime type error if the type is
583 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
585 if (!DECL_P(rhs_tree
))
586 rhs_tree
= save_expr(rhs_tree
);
588 tree rhs_type_descriptor
=
589 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
592 tree rhs_inter_descriptor
= rhs_type
->type_descriptor_pointer(gogo
);
594 static tree check_interface_type_decl
;
595 tree call
= Gogo::call_builtin(&check_interface_type_decl
,
597 "__go_check_interface_type",
600 TREE_TYPE(lhs_type_descriptor
),
602 TREE_TYPE(rhs_type_descriptor
),
604 TREE_TYPE(rhs_inter_descriptor
),
605 rhs_inter_descriptor
);
606 if (call
== error_mark_node
)
607 return error_mark_node
;
608 // This call will panic if the conversion is invalid.
609 TREE_NOTHROW(check_interface_type_decl
) = 0;
611 // If the call succeeds, pull out the value.
612 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
613 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
614 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
615 tree val
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
618 // If the value is a pointer, then it is the value we want.
619 // Otherwise it points to the value.
620 if (lhs_type
->points_to() == NULL
)
622 val
= fold_convert_loc(location
, build_pointer_type(lhs_type_tree
), val
);
623 val
= build_fold_indirect_ref_loc(location
, val
);
626 return build2(COMPOUND_EXPR
, lhs_type_tree
, call
,
627 fold_convert_loc(location
, lhs_type_tree
, val
));
630 // Convert an expression to a tree. This is implemented by the child
631 // class. Not that it is not in general safe to call this multiple
632 // times for a single expression, but that we don't catch such errors.
635 Expression::get_tree(Translate_context
* context
)
637 // The child may have marked this expression as having an error.
638 if (this->classification_
== EXPRESSION_ERROR
)
639 return error_mark_node
;
641 return this->do_get_tree(context
);
644 // Return a tree for VAL in TYPE.
647 Expression::integer_constant_tree(mpz_t val
, tree type
)
649 if (type
== error_mark_node
)
650 return error_mark_node
;
651 else if (TREE_CODE(type
) == INTEGER_TYPE
)
652 return double_int_to_tree(type
,
653 mpz_get_double_int(type
, val
, true));
654 else if (TREE_CODE(type
) == REAL_TYPE
)
657 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
658 tree ret
= Expression::float_constant_tree(fval
, type
);
662 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
665 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
666 tree real
= Expression::float_constant_tree(fval
, TREE_TYPE(type
));
668 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
670 return build_complex(type
, real
, imag
);
676 // Return a tree for VAL in TYPE.
679 Expression::float_constant_tree(mpfr_t val
, tree type
)
681 if (type
== error_mark_node
)
682 return error_mark_node
;
683 else if (TREE_CODE(type
) == INTEGER_TYPE
)
687 mpfr_get_z(ival
, val
, GMP_RNDN
);
688 tree ret
= Expression::integer_constant_tree(ival
, type
);
692 else if (TREE_CODE(type
) == REAL_TYPE
)
695 real_from_mpfr(&r1
, val
, type
, GMP_RNDN
);
697 real_convert(&r2
, TYPE_MODE(type
), &r1
);
698 return build_real(type
, r2
);
700 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
703 real_from_mpfr(&r1
, val
, TREE_TYPE(type
), GMP_RNDN
);
705 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
706 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
708 return build_complex(type
, build_real(TREE_TYPE(type
), r2
), imag
);
714 // Return a tree for REAL/IMAG in TYPE.
717 Expression::complex_constant_tree(mpfr_t real
, mpfr_t imag
, tree type
)
719 if (type
== error_mark_node
)
720 return error_mark_node
;
721 else if (TREE_CODE(type
) == INTEGER_TYPE
|| TREE_CODE(type
) == REAL_TYPE
)
722 return Expression::float_constant_tree(real
, type
);
723 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
726 real_from_mpfr(&r1
, real
, TREE_TYPE(type
), GMP_RNDN
);
728 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
731 real_from_mpfr(&r3
, imag
, TREE_TYPE(type
), GMP_RNDN
);
733 real_convert(&r4
, TYPE_MODE(TREE_TYPE(type
)), &r3
);
735 return build_complex(type
, build_real(TREE_TYPE(type
), r2
),
736 build_real(TREE_TYPE(type
), r4
));
742 // Return a tree which evaluates to true if VAL, of arbitrary integer
743 // type, is negative or is more than the maximum value of BOUND_TYPE.
744 // If SOFAR is not NULL, it is or'red into the result. The return
745 // value may be NULL if SOFAR is NULL.
748 Expression::check_bounds(tree val
, tree bound_type
, tree sofar
,
751 tree val_type
= TREE_TYPE(val
);
752 tree ret
= NULL_TREE
;
754 if (!TYPE_UNSIGNED(val_type
))
756 ret
= fold_build2_loc(loc
, LT_EXPR
, boolean_type_node
, val
,
757 build_int_cst(val_type
, 0));
758 if (ret
== boolean_false_node
)
762 if ((TYPE_UNSIGNED(val_type
) && !TYPE_UNSIGNED(bound_type
))
763 || TYPE_SIZE(val_type
) > TYPE_SIZE(bound_type
))
765 tree max
= TYPE_MAX_VALUE(bound_type
);
766 tree big
= fold_build2_loc(loc
, GT_EXPR
, boolean_type_node
, val
,
767 fold_convert_loc(loc
, val_type
, max
));
768 if (big
== boolean_false_node
)
770 else if (ret
== NULL_TREE
)
773 ret
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
777 if (ret
== NULL_TREE
)
779 else if (sofar
== NULL_TREE
)
782 return fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
786 // Error expressions. This are used to avoid cascading errors.
788 class Error_expression
: public Expression
791 Error_expression(source_location location
)
792 : Expression(EXPRESSION_ERROR
, location
)
797 do_is_constant() const
801 do_integer_constant_value(bool, mpz_t val
, Type
**) const
808 do_float_constant_value(mpfr_t val
, Type
**) const
810 mpfr_set_ui(val
, 0, GMP_RNDN
);
815 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const
817 mpfr_set_ui(real
, 0, GMP_RNDN
);
818 mpfr_set_ui(imag
, 0, GMP_RNDN
);
823 do_discarding_value()
828 { return Type::make_error_type(); }
831 do_determine_type(const Type_context
*)
839 do_is_addressable() const
843 do_get_tree(Translate_context
*)
844 { return error_mark_node
; }
848 Expression::make_error(source_location location
)
850 return new Error_expression(location
);
853 // An expression which is really a type. This is used during parsing.
854 // It is an error if these survive after lowering.
857 Type_expression
: public Expression
860 Type_expression(Type
* type
, source_location location
)
861 : Expression(EXPRESSION_TYPE
, location
),
867 do_traverse(Traverse
* traverse
)
868 { return Type::traverse(this->type_
, traverse
); }
872 { return this->type_
; }
875 do_determine_type(const Type_context
*)
879 do_check_types(Gogo
*)
880 { this->report_error(_("invalid use of type")); }
887 do_get_tree(Translate_context
*)
888 { gcc_unreachable(); }
891 // The type which we are representing as an expression.
896 Expression::make_type(Type
* type
, source_location location
)
898 return new Type_expression(type
, location
);
901 // Class Parser_expression.
904 Parser_expression::do_type()
906 // We should never really ask for the type of a Parser_expression.
907 // However, it can happen, at least when we have an invalid const
908 // whose initializer refers to the const itself. In that case we
909 // may ask for the type when lowering the const itself.
910 gcc_assert(saw_errors());
911 return Type::make_error_type();
914 // Class Var_expression.
916 // Lower a variable expression. Here we just make sure that the
917 // initialization expression of the variable has been lowered. This
918 // ensures that we will be able to determine the type of the variable
922 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
924 if (this->variable_
->is_variable())
926 Variable
* var
= this->variable_
->var_value();
927 // This is either a local variable or a global variable. A
928 // reference to a variable which is local to an enclosing
929 // function will be a reference to a field in a closure.
930 if (var
->is_global())
932 var
->lower_init_expression(gogo
, function
);
937 // Return the name of the variable.
940 Var_expression::name() const
942 return this->variable_
->name();
945 // Return the type of a reference to a variable.
948 Var_expression::do_type()
950 if (this->variable_
->is_variable())
951 return this->variable_
->var_value()->type();
952 else if (this->variable_
->is_result_variable())
953 return this->variable_
->result_var_value()->type();
958 // Something takes the address of this variable. This means that we
959 // may want to move the variable onto the heap.
962 Var_expression::do_address_taken(bool escapes
)
966 else if (this->variable_
->is_variable())
967 this->variable_
->var_value()->set_address_taken();
968 else if (this->variable_
->is_result_variable())
969 this->variable_
->result_var_value()->set_address_taken();
974 // Get the tree for a reference to a variable.
977 Var_expression::do_get_tree(Translate_context
* context
)
979 return this->variable_
->get_tree(context
->gogo(), context
->function());
982 // Make a reference to a variable in an expression.
985 Expression::make_var_reference(Named_object
* var
, source_location location
)
988 return Expression::make_sink(location
);
990 // FIXME: Creating a new object for each reference to a variable is
992 return new Var_expression(var
, location
);
995 // Class Temporary_reference_expression.
1000 Temporary_reference_expression::do_type()
1002 return this->statement_
->type();
1005 // Called if something takes the address of this temporary variable.
1006 // We never have to move temporary variables to the heap, but we do
1007 // need to know that they must live in the stack rather than in a
1011 Temporary_reference_expression::do_address_taken(bool)
1013 this->statement_
->set_is_address_taken();
1016 // Get a tree referring to the variable.
1019 Temporary_reference_expression::do_get_tree(Translate_context
*)
1021 return this->statement_
->get_decl();
1024 // Make a reference to a temporary variable.
1027 Expression::make_temporary_reference(Temporary_statement
* statement
,
1028 source_location location
)
1030 return new Temporary_reference_expression(statement
, location
);
1033 // A sink expression--a use of the blank identifier _.
1035 class Sink_expression
: public Expression
1038 Sink_expression(source_location location
)
1039 : Expression(EXPRESSION_SINK
, location
),
1040 type_(NULL
), var_(NULL_TREE
)
1045 do_discarding_value()
1052 do_determine_type(const Type_context
*);
1056 { return new Sink_expression(this->location()); }
1059 do_get_tree(Translate_context
*);
1062 // The type of this sink variable.
1064 // The temporary variable we generate.
1068 // Return the type of a sink expression.
1071 Sink_expression::do_type()
1073 if (this->type_
== NULL
)
1074 return Type::make_sink_type();
1078 // Determine the type of a sink expression.
1081 Sink_expression::do_determine_type(const Type_context
* context
)
1083 if (context
->type
!= NULL
)
1084 this->type_
= context
->type
;
1087 // Return a temporary variable for a sink expression. This will
1088 // presumably be a write-only variable which the middle-end will drop.
1091 Sink_expression::do_get_tree(Translate_context
* context
)
1093 if (this->var_
== NULL_TREE
)
1095 gcc_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
1096 this->var_
= create_tmp_var(this->type_
->get_tree(context
->gogo()),
1102 // Make a sink expression.
1105 Expression::make_sink(source_location location
)
1107 return new Sink_expression(location
);
1110 // Class Func_expression.
1112 // FIXME: Can a function expression appear in a constant expression?
1113 // The value is unchanging. Initializing a constant to the address of
1114 // a function seems like it could work, though there might be little
1117 // Return the name of the function.
1120 Func_expression::name() const
1122 return this->function_
->name();
1128 Func_expression::do_traverse(Traverse
* traverse
)
1130 return (this->closure_
== NULL
1132 : Expression::traverse(&this->closure_
, traverse
));
1135 // Return the type of a function expression.
1138 Func_expression::do_type()
1140 if (this->function_
->is_function())
1141 return this->function_
->func_value()->type();
1142 else if (this->function_
->is_function_declaration())
1143 return this->function_
->func_declaration_value()->type();
1148 // Get the tree for a function expression without evaluating the
1152 Func_expression::get_tree_without_closure(Gogo
* gogo
)
1154 Function_type
* fntype
;
1155 if (this->function_
->is_function())
1156 fntype
= this->function_
->func_value()->type();
1157 else if (this->function_
->is_function_declaration())
1158 fntype
= this->function_
->func_declaration_value()->type();
1162 // Builtin functions are handled specially by Call_expression. We
1163 // can't take their address.
1164 if (fntype
->is_builtin())
1166 error_at(this->location(), "invalid use of special builtin function %qs",
1167 this->function_
->name().c_str());
1168 return error_mark_node
;
1171 Named_object
* no
= this->function_
;
1173 tree id
= no
->get_id(gogo
);
1174 if (id
== error_mark_node
)
1175 return error_mark_node
;
1178 if (no
->is_function())
1179 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
, id
);
1180 else if (no
->is_function_declaration())
1181 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
, id
);
1185 if (fndecl
== error_mark_node
)
1186 return error_mark_node
;
1188 return build_fold_addr_expr_loc(this->location(), fndecl
);
1191 // Get the tree for a function expression. This is used when we take
1192 // the address of a function rather than simply calling it. If the
1193 // function has a closure, we must use a trampoline.
1196 Func_expression::do_get_tree(Translate_context
* context
)
1198 Gogo
* gogo
= context
->gogo();
1200 tree fnaddr
= this->get_tree_without_closure(gogo
);
1201 if (fnaddr
== error_mark_node
)
1202 return error_mark_node
;
1204 gcc_assert(TREE_CODE(fnaddr
) == ADDR_EXPR
1205 && TREE_CODE(TREE_OPERAND(fnaddr
, 0)) == FUNCTION_DECL
);
1206 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr
, 0)) = 1;
1208 // For a normal non-nested function call, that is all we have to do.
1209 if (!this->function_
->is_function()
1210 || this->function_
->func_value()->enclosing() == NULL
)
1212 gcc_assert(this->closure_
== NULL
);
1216 // For a nested function call, we have to always allocate a
1217 // trampoline. If we don't always allocate, then closures will not
1218 // be reliably distinct.
1219 Expression
* closure
= this->closure_
;
1221 if (closure
== NULL
)
1222 closure_tree
= null_pointer_node
;
1225 // Get the value of the closure. This will be a pointer to
1226 // space allocated on the heap.
1227 closure_tree
= closure
->get_tree(context
);
1228 if (closure_tree
== error_mark_node
)
1229 return error_mark_node
;
1230 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree
)));
1233 // Now we need to build some code on the heap. This code will load
1234 // the static chain pointer with the closure and then jump to the
1235 // body of the function. The normal gcc approach is to build the
1236 // code on the stack. Unfortunately we can not do that, as Go
1237 // permits us to return the function pointer.
1239 return gogo
->make_trampoline(fnaddr
, closure_tree
, this->location());
1242 // Make a reference to a function in an expression.
1245 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1246 source_location location
)
1248 return new Func_expression(function
, closure
, location
);
1251 // Class Unknown_expression.
1253 // Return the name of an unknown expression.
1256 Unknown_expression::name() const
1258 return this->named_object_
->name();
1261 // Lower a reference to an unknown name.
1264 Unknown_expression::do_lower(Gogo
*, Named_object
*, int)
1266 source_location location
= this->location();
1267 Named_object
* no
= this->named_object_
;
1269 if (!no
->is_unknown())
1273 real
= no
->unknown_value()->real_named_object();
1276 if (this->is_composite_literal_key_
)
1278 error_at(location
, "reference to undefined name %qs",
1279 this->named_object_
->message_name().c_str());
1280 return Expression::make_error(location
);
1283 switch (real
->classification())
1285 case Named_object::NAMED_OBJECT_CONST
:
1286 return Expression::make_const_reference(real
, location
);
1287 case Named_object::NAMED_OBJECT_TYPE
:
1288 return Expression::make_type(real
->type_value(), location
);
1289 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1290 if (this->is_composite_literal_key_
)
1292 error_at(location
, "reference to undefined type %qs",
1293 real
->message_name().c_str());
1294 return Expression::make_error(location
);
1295 case Named_object::NAMED_OBJECT_VAR
:
1296 return Expression::make_var_reference(real
, location
);
1297 case Named_object::NAMED_OBJECT_FUNC
:
1298 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1299 return Expression::make_func_reference(real
, NULL
, location
);
1300 case Named_object::NAMED_OBJECT_PACKAGE
:
1301 if (this->is_composite_literal_key_
)
1303 error_at(location
, "unexpected reference to package");
1304 return Expression::make_error(location
);
1310 // Make a reference to an unknown name.
1313 Expression::make_unknown_reference(Named_object
* no
, source_location location
)
1315 gcc_assert(no
->resolve()->is_unknown());
1316 return new Unknown_expression(no
, location
);
1319 // A boolean expression.
1321 class Boolean_expression
: public Expression
1324 Boolean_expression(bool val
, source_location location
)
1325 : Expression(EXPRESSION_BOOLEAN
, location
),
1326 val_(val
), type_(NULL
)
1334 do_is_constant() const
1341 do_determine_type(const Type_context
*);
1348 do_get_tree(Translate_context
*)
1349 { return this->val_
? boolean_true_node
: boolean_false_node
; }
1352 do_export(Export
* exp
) const
1353 { exp
->write_c_string(this->val_
? "true" : "false"); }
1358 // The type as determined by context.
1365 Boolean_expression::do_type()
1367 if (this->type_
== NULL
)
1368 this->type_
= Type::make_boolean_type();
1372 // Set the type from the context.
1375 Boolean_expression::do_determine_type(const Type_context
* context
)
1377 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1379 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1380 this->type_
= context
->type
;
1381 else if (!context
->may_be_abstract
)
1382 this->type_
= Type::lookup_bool_type();
1385 // Import a boolean constant.
1388 Boolean_expression::do_import(Import
* imp
)
1390 if (imp
->peek_char() == 't')
1392 imp
->require_c_string("true");
1393 return Expression::make_boolean(true, imp
->location());
1397 imp
->require_c_string("false");
1398 return Expression::make_boolean(false, imp
->location());
1402 // Make a boolean expression.
1405 Expression::make_boolean(bool val
, source_location location
)
1407 return new Boolean_expression(val
, location
);
1410 // Class String_expression.
1415 String_expression::do_type()
1417 if (this->type_
== NULL
)
1418 this->type_
= Type::make_string_type();
1422 // Set the type from the context.
1425 String_expression::do_determine_type(const Type_context
* context
)
1427 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1429 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1430 this->type_
= context
->type
;
1431 else if (!context
->may_be_abstract
)
1432 this->type_
= Type::lookup_string_type();
1435 // Build a string constant.
1438 String_expression::do_get_tree(Translate_context
* context
)
1440 return context
->gogo()->go_string_constant_tree(this->val_
);
1443 // Export a string expression.
1446 String_expression::do_export(Export
* exp
) const
1449 s
.reserve(this->val_
.length() * 4 + 2);
1451 for (std::string::const_iterator p
= this->val_
.begin();
1452 p
!= this->val_
.end();
1455 if (*p
== '\\' || *p
== '"')
1460 else if (*p
>= 0x20 && *p
< 0x7f)
1462 else if (*p
== '\n')
1464 else if (*p
== '\t')
1469 unsigned char c
= *p
;
1470 unsigned int dig
= c
>> 4;
1471 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1473 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1477 exp
->write_string(s
);
1480 // Import a string expression.
1483 String_expression::do_import(Import
* imp
)
1485 imp
->require_c_string("\"");
1489 int c
= imp
->get_char();
1490 if (c
== '"' || c
== -1)
1493 val
+= static_cast<char>(c
);
1496 c
= imp
->get_char();
1497 if (c
== '\\' || c
== '"')
1498 val
+= static_cast<char>(c
);
1505 c
= imp
->get_char();
1506 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1507 c
= imp
->get_char();
1508 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1509 char v
= (vh
<< 4) | vl
;
1514 error_at(imp
->location(), "bad string constant");
1515 return Expression::make_error(imp
->location());
1519 return Expression::make_string(val
, imp
->location());
1522 // Make a string expression.
1525 Expression::make_string(const std::string
& val
, source_location location
)
1527 return new String_expression(val
, location
);
1530 // Make an integer expression.
1532 class Integer_expression
: public Expression
1535 Integer_expression(const mpz_t
* val
, Type
* type
, source_location location
)
1536 : Expression(EXPRESSION_INTEGER
, location
),
1538 { mpz_init_set(this->val_
, *val
); }
1543 // Return whether VAL fits in the type.
1545 check_constant(mpz_t val
, Type
*, source_location
);
1547 // Write VAL to export data.
1549 export_integer(Export
* exp
, const mpz_t val
);
1553 do_is_constant() const
1557 do_integer_constant_value(bool, mpz_t val
, Type
** ptype
) const;
1563 do_determine_type(const Type_context
* context
);
1566 do_check_types(Gogo
*);
1569 do_get_tree(Translate_context
*);
1573 { return Expression::make_integer(&this->val_
, this->type_
,
1574 this->location()); }
1577 do_export(Export
*) const;
1580 // The integer value.
1586 // Return an integer constant value.
1589 Integer_expression::do_integer_constant_value(bool, mpz_t val
,
1592 if (this->type_
!= NULL
)
1593 *ptype
= this->type_
;
1594 mpz_set(val
, this->val_
);
1598 // Return the current type. If we haven't set the type yet, we return
1599 // an abstract integer type.
1602 Integer_expression::do_type()
1604 if (this->type_
== NULL
)
1605 this->type_
= Type::make_abstract_integer_type();
1609 // Set the type of the integer value. Here we may switch from an
1610 // abstract type to a real type.
1613 Integer_expression::do_determine_type(const Type_context
* context
)
1615 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1617 else if (context
->type
!= NULL
1618 && (context
->type
->integer_type() != NULL
1619 || context
->type
->float_type() != NULL
1620 || context
->type
->complex_type() != NULL
))
1621 this->type_
= context
->type
;
1622 else if (!context
->may_be_abstract
)
1623 this->type_
= Type::lookup_integer_type("int");
1626 // Return true if the integer VAL fits in the range of the type TYPE.
1627 // Otherwise give an error and return false. TYPE may be NULL.
1630 Integer_expression::check_constant(mpz_t val
, Type
* type
,
1631 source_location location
)
1635 Integer_type
* itype
= type
->integer_type();
1636 if (itype
== NULL
|| itype
->is_abstract())
1639 int bits
= mpz_sizeinbase(val
, 2);
1641 if (itype
->is_unsigned())
1643 // For an unsigned type we can only accept a nonnegative number,
1644 // and we must be able to represent at least BITS.
1645 if (mpz_sgn(val
) >= 0
1646 && bits
<= itype
->bits())
1651 // For a signed type we need an extra bit to indicate the sign.
1652 // We have to handle the most negative integer specially.
1653 if (bits
+ 1 <= itype
->bits()
1654 || (bits
<= itype
->bits()
1656 && (mpz_scan1(val
, 0)
1657 == static_cast<unsigned long>(itype
->bits() - 1))
1658 && mpz_scan0(val
, itype
->bits()) == ULONG_MAX
))
1662 error_at(location
, "integer constant overflow");
1666 // Check the type of an integer constant.
1669 Integer_expression::do_check_types(Gogo
*)
1671 if (this->type_
== NULL
)
1673 if (!Integer_expression::check_constant(this->val_
, this->type_
,
1675 this->set_is_error();
1678 // Get a tree for an integer constant.
1681 Integer_expression::do_get_tree(Translate_context
* context
)
1683 Gogo
* gogo
= context
->gogo();
1685 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1686 type
= this->type_
->get_tree(gogo
);
1687 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
1689 // We are converting to an abstract floating point type.
1690 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
1692 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
1694 // We are converting to an abstract complex type.
1695 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
1699 // If we still have an abstract type here, then this is being
1700 // used in a constant expression which didn't get reduced for
1701 // some reason. Use a type which will fit the value. We use <,
1702 // not <=, because we need an extra bit for the sign bit.
1703 int bits
= mpz_sizeinbase(this->val_
, 2);
1704 if (bits
< INT_TYPE_SIZE
)
1705 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
1707 type
= Type::lookup_integer_type("int64")->get_tree(gogo
);
1709 type
= long_long_integer_type_node
;
1711 return Expression::integer_constant_tree(this->val_
, type
);
1714 // Write VAL to export data.
1717 Integer_expression::export_integer(Export
* exp
, const mpz_t val
)
1719 char* s
= mpz_get_str(NULL
, 10, val
);
1720 exp
->write_c_string(s
);
1724 // Export an integer in a constant expression.
1727 Integer_expression::do_export(Export
* exp
) const
1729 Integer_expression::export_integer(exp
, this->val_
);
1730 // A trailing space lets us reliably identify the end of the number.
1731 exp
->write_c_string(" ");
1734 // Import an integer, floating point, or complex value. This handles
1735 // all these types because they all start with digits.
1738 Integer_expression::do_import(Import
* imp
)
1740 std::string num
= imp
->read_identifier();
1741 imp
->require_c_string(" ");
1742 if (!num
.empty() && num
[num
.length() - 1] == 'i')
1745 size_t plus_pos
= num
.find('+', 1);
1746 size_t minus_pos
= num
.find('-', 1);
1748 if (plus_pos
== std::string::npos
)
1750 else if (minus_pos
== std::string::npos
)
1754 error_at(imp
->location(), "bad number in import data: %qs",
1756 return Expression::make_error(imp
->location());
1758 if (pos
== std::string::npos
)
1759 mpfr_set_ui(real
, 0, GMP_RNDN
);
1762 std::string real_str
= num
.substr(0, pos
);
1763 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
1765 error_at(imp
->location(), "bad number in import data: %qs",
1767 return Expression::make_error(imp
->location());
1771 std::string imag_str
;
1772 if (pos
== std::string::npos
)
1775 imag_str
= num
.substr(pos
);
1776 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
1778 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
1780 error_at(imp
->location(), "bad number in import data: %qs",
1782 return Expression::make_error(imp
->location());
1784 Expression
* ret
= Expression::make_complex(&real
, &imag
, NULL
,
1790 else if (num
.find('.') == std::string::npos
1791 && num
.find('E') == std::string::npos
)
1794 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
1796 error_at(imp
->location(), "bad number in import data: %qs",
1798 return Expression::make_error(imp
->location());
1800 Expression
* ret
= Expression::make_integer(&val
, NULL
, imp
->location());
1807 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
1809 error_at(imp
->location(), "bad number in import data: %qs",
1811 return Expression::make_error(imp
->location());
1813 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
1819 // Build a new integer value.
1822 Expression::make_integer(const mpz_t
* val
, Type
* type
,
1823 source_location location
)
1825 return new Integer_expression(val
, type
, location
);
1830 class Float_expression
: public Expression
1833 Float_expression(const mpfr_t
* val
, Type
* type
, source_location location
)
1834 : Expression(EXPRESSION_FLOAT
, location
),
1837 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
1840 // Constrain VAL to fit into TYPE.
1842 constrain_float(mpfr_t val
, Type
* type
);
1844 // Return whether VAL fits in the type.
1846 check_constant(mpfr_t val
, Type
*, source_location
);
1848 // Write VAL to export data.
1850 export_float(Export
* exp
, const mpfr_t val
);
1854 do_is_constant() const
1858 do_float_constant_value(mpfr_t val
, Type
**) const;
1864 do_determine_type(const Type_context
*);
1867 do_check_types(Gogo
*);
1871 { return Expression::make_float(&this->val_
, this->type_
,
1872 this->location()); }
1875 do_get_tree(Translate_context
*);
1878 do_export(Export
*) const;
1881 // The floating point value.
1887 // Constrain VAL to fit into TYPE.
1890 Float_expression::constrain_float(mpfr_t val
, Type
* type
)
1892 Float_type
* ftype
= type
->float_type();
1893 if (ftype
!= NULL
&& !ftype
->is_abstract())
1895 tree type_tree
= ftype
->type_tree();
1896 REAL_VALUE_TYPE rvt
;
1897 real_from_mpfr(&rvt
, val
, type_tree
, GMP_RNDN
);
1898 real_convert(&rvt
, TYPE_MODE(type_tree
), &rvt
);
1899 mpfr_from_real(val
, &rvt
, GMP_RNDN
);
1903 // Return a floating point constant value.
1906 Float_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
1908 if (this->type_
!= NULL
)
1909 *ptype
= this->type_
;
1910 mpfr_set(val
, this->val_
, GMP_RNDN
);
1914 // Return the current type. If we haven't set the type yet, we return
1915 // an abstract float type.
1918 Float_expression::do_type()
1920 if (this->type_
== NULL
)
1921 this->type_
= Type::make_abstract_float_type();
1925 // Set the type of the float value. Here we may switch from an
1926 // abstract type to a real type.
1929 Float_expression::do_determine_type(const Type_context
* context
)
1931 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1933 else if (context
->type
!= NULL
1934 && (context
->type
->integer_type() != NULL
1935 || context
->type
->float_type() != NULL
1936 || context
->type
->complex_type() != NULL
))
1937 this->type_
= context
->type
;
1938 else if (!context
->may_be_abstract
)
1939 this->type_
= Type::lookup_float_type("float64");
1942 // Return true if the floating point value VAL fits in the range of
1943 // the type TYPE. Otherwise give an error and return false. TYPE may
1947 Float_expression::check_constant(mpfr_t val
, Type
* type
,
1948 source_location location
)
1952 Float_type
* ftype
= type
->float_type();
1953 if (ftype
== NULL
|| ftype
->is_abstract())
1956 // A NaN or Infinity always fits in the range of the type.
1957 if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
1960 mp_exp_t exp
= mpfr_get_exp(val
);
1962 switch (ftype
->bits())
1975 error_at(location
, "floating point constant overflow");
1981 // Check the type of a float value.
1984 Float_expression::do_check_types(Gogo
*)
1986 if (this->type_
== NULL
)
1989 if (!Float_expression::check_constant(this->val_
, this->type_
,
1991 this->set_is_error();
1993 Integer_type
* integer_type
= this->type_
->integer_type();
1994 if (integer_type
!= NULL
)
1996 if (!mpfr_integer_p(this->val_
))
1997 this->report_error(_("floating point constant truncated to integer"));
2000 gcc_assert(!integer_type
->is_abstract());
2003 mpfr_get_z(ival
, this->val_
, GMP_RNDN
);
2004 Integer_expression::check_constant(ival
, integer_type
,
2011 // Get a tree for a float constant.
2014 Float_expression::do_get_tree(Translate_context
* context
)
2016 Gogo
* gogo
= context
->gogo();
2018 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2019 type
= this->type_
->get_tree(gogo
);
2020 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2022 // We have an abstract integer type. We just hope for the best.
2023 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
2027 // If we still have an abstract type here, then this is being
2028 // used in a constant expression which didn't get reduced. We
2029 // just use float64 and hope for the best.
2030 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
2032 return Expression::float_constant_tree(this->val_
, type
);
2035 // Write a floating point number to export data.
2038 Float_expression::export_float(Export
*exp
, const mpfr_t val
)
2041 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2043 exp
->write_c_string("-");
2044 exp
->write_c_string("0.");
2045 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2048 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2049 exp
->write_c_string(buf
);
2052 // Export a floating point number in a constant expression.
2055 Float_expression::do_export(Export
* exp
) const
2057 Float_expression::export_float(exp
, this->val_
);
2058 // A trailing space lets us reliably identify the end of the number.
2059 exp
->write_c_string(" ");
2062 // Make a float expression.
2065 Expression::make_float(const mpfr_t
* val
, Type
* type
, source_location location
)
2067 return new Float_expression(val
, type
, location
);
2072 class Complex_expression
: public Expression
2075 Complex_expression(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2076 source_location location
)
2077 : Expression(EXPRESSION_COMPLEX
, location
),
2080 mpfr_init_set(this->real_
, *real
, GMP_RNDN
);
2081 mpfr_init_set(this->imag_
, *imag
, GMP_RNDN
);
2084 // Constrain REAL/IMAG to fit into TYPE.
2086 constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
);
2088 // Return whether REAL/IMAG fits in the type.
2090 check_constant(mpfr_t real
, mpfr_t imag
, Type
*, source_location
);
2092 // Write REAL/IMAG to export data.
2094 export_complex(Export
* exp
, const mpfr_t real
, const mpfr_t val
);
2098 do_is_constant() const
2102 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2108 do_determine_type(const Type_context
*);
2111 do_check_types(Gogo
*);
2116 return Expression::make_complex(&this->real_
, &this->imag_
, this->type_
,
2121 do_get_tree(Translate_context
*);
2124 do_export(Export
*) const;
2129 // The imaginary part;
2131 // The type if known.
2135 // Constrain REAL/IMAG to fit into TYPE.
2138 Complex_expression::constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
)
2140 Complex_type
* ctype
= type
->complex_type();
2141 if (ctype
!= NULL
&& !ctype
->is_abstract())
2143 tree type_tree
= ctype
->type_tree();
2145 REAL_VALUE_TYPE rvt
;
2146 real_from_mpfr(&rvt
, real
, TREE_TYPE(type_tree
), GMP_RNDN
);
2147 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2148 mpfr_from_real(real
, &rvt
, GMP_RNDN
);
2150 real_from_mpfr(&rvt
, imag
, TREE_TYPE(type_tree
), GMP_RNDN
);
2151 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2152 mpfr_from_real(imag
, &rvt
, GMP_RNDN
);
2156 // Return a complex constant value.
2159 Complex_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2162 if (this->type_
!= NULL
)
2163 *ptype
= this->type_
;
2164 mpfr_set(real
, this->real_
, GMP_RNDN
);
2165 mpfr_set(imag
, this->imag_
, GMP_RNDN
);
2169 // Return the current type. If we haven't set the type yet, we return
2170 // an abstract complex type.
2173 Complex_expression::do_type()
2175 if (this->type_
== NULL
)
2176 this->type_
= Type::make_abstract_complex_type();
2180 // Set the type of the complex value. Here we may switch from an
2181 // abstract type to a real type.
2184 Complex_expression::do_determine_type(const Type_context
* context
)
2186 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2188 else if (context
->type
!= NULL
2189 && context
->type
->complex_type() != NULL
)
2190 this->type_
= context
->type
;
2191 else if (!context
->may_be_abstract
)
2192 this->type_
= Type::lookup_complex_type("complex128");
2195 // Return true if the complex value REAL/IMAG fits in the range of the
2196 // type TYPE. Otherwise give an error and return false. TYPE may be
2200 Complex_expression::check_constant(mpfr_t real
, mpfr_t imag
, Type
* type
,
2201 source_location location
)
2205 Complex_type
* ctype
= type
->complex_type();
2206 if (ctype
== NULL
|| ctype
->is_abstract())
2210 switch (ctype
->bits())
2222 // A NaN or Infinity always fits in the range of the type.
2223 if (!mpfr_nan_p(real
) && !mpfr_inf_p(real
) && !mpfr_zero_p(real
))
2225 if (mpfr_get_exp(real
) > max_exp
)
2227 error_at(location
, "complex real part constant overflow");
2232 if (!mpfr_nan_p(imag
) && !mpfr_inf_p(imag
) && !mpfr_zero_p(imag
))
2234 if (mpfr_get_exp(imag
) > max_exp
)
2236 error_at(location
, "complex imaginary part constant overflow");
2244 // Check the type of a complex value.
2247 Complex_expression::do_check_types(Gogo
*)
2249 if (this->type_
== NULL
)
2252 if (!Complex_expression::check_constant(this->real_
, this->imag_
,
2253 this->type_
, this->location()))
2254 this->set_is_error();
2257 // Get a tree for a complex constant.
2260 Complex_expression::do_get_tree(Translate_context
* context
)
2262 Gogo
* gogo
= context
->gogo();
2264 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2265 type
= this->type_
->get_tree(gogo
);
2268 // If we still have an abstract type here, this this is being
2269 // used in a constant expression which didn't get reduced. We
2270 // just use complex128 and hope for the best.
2271 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
2273 return Expression::complex_constant_tree(this->real_
, this->imag_
, type
);
2276 // Write REAL/IMAG to export data.
2279 Complex_expression::export_complex(Export
* exp
, const mpfr_t real
,
2282 if (!mpfr_zero_p(real
))
2284 Float_expression::export_float(exp
, real
);
2285 if (mpfr_sgn(imag
) > 0)
2286 exp
->write_c_string("+");
2288 Float_expression::export_float(exp
, imag
);
2289 exp
->write_c_string("i");
2292 // Export a complex number in a constant expression.
2295 Complex_expression::do_export(Export
* exp
) const
2297 Complex_expression::export_complex(exp
, this->real_
, this->imag_
);
2298 // A trailing space lets us reliably identify the end of the number.
2299 exp
->write_c_string(" ");
2302 // Make a complex expression.
2305 Expression::make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2306 source_location location
)
2308 return new Complex_expression(real
, imag
, type
, location
);
2311 // Find a named object in an expression.
2313 class Find_named_object
: public Traverse
2316 Find_named_object(Named_object
* no
)
2317 : Traverse(traverse_expressions
),
2318 no_(no
), found_(false)
2321 // Whether we found the object.
2324 { return this->found_
; }
2328 expression(Expression
**);
2331 // The object we are looking for.
2333 // Whether we found it.
2337 // A reference to a const in an expression.
2339 class Const_expression
: public Expression
2342 Const_expression(Named_object
* constant
, source_location location
)
2343 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2344 constant_(constant
), type_(NULL
), seen_(false)
2349 { return this->constant_
; }
2353 { return this->constant_
->name(); }
2355 // Check that the initializer does not refer to the constant itself.
2357 check_for_init_loop();
2361 do_lower(Gogo
*, Named_object
*, int);
2364 do_is_constant() const
2368 do_integer_constant_value(bool, mpz_t val
, Type
**) const;
2371 do_float_constant_value(mpfr_t val
, Type
**) const;
2374 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2377 do_string_constant_value(std::string
* val
) const
2378 { return this->constant_
->const_value()->expr()->string_constant_value(val
); }
2383 // The type of a const is set by the declaration, not the use.
2385 do_determine_type(const Type_context
*);
2388 do_check_types(Gogo
*);
2395 do_get_tree(Translate_context
* context
);
2397 // When exporting a reference to a const as part of a const
2398 // expression, we export the value. We ignore the fact that it has
2401 do_export(Export
* exp
) const
2402 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2406 Named_object
* constant_
;
2407 // The type of this reference. This is used if the constant has an
2410 // Used to prevent infinite recursion when a constant incorrectly
2411 // refers to itself.
2415 // Lower a constant expression. This is where we convert the
2416 // predeclared constant iota into an integer value.
2419 Const_expression::do_lower(Gogo
* gogo
, Named_object
*, int iota_value
)
2421 if (this->constant_
->const_value()->expr()->classification()
2424 if (iota_value
== -1)
2426 error_at(this->location(),
2427 "iota is only defined in const declarations");
2431 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2432 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2438 // Make sure that the constant itself has been lowered.
2439 gogo
->lower_constant(this->constant_
);
2444 // Return an integer constant value.
2447 Const_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
2454 if (this->type_
!= NULL
)
2455 ctype
= this->type_
;
2457 ctype
= this->constant_
->const_value()->type();
2458 if (ctype
!= NULL
&& ctype
->integer_type() == NULL
)
2461 Expression
* e
= this->constant_
->const_value()->expr();
2466 bool r
= e
->integer_constant_value(iota_is_constant
, val
, &t
);
2468 this->seen_
= false;
2472 && !Integer_expression::check_constant(val
, ctype
, this->location()))
2475 *ptype
= ctype
!= NULL
? ctype
: t
;
2479 // Return a floating point constant value.
2482 Const_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
2488 if (this->type_
!= NULL
)
2489 ctype
= this->type_
;
2491 ctype
= this->constant_
->const_value()->type();
2492 if (ctype
!= NULL
&& ctype
->float_type() == NULL
)
2498 bool r
= this->constant_
->const_value()->expr()->float_constant_value(val
,
2501 this->seen_
= false;
2503 if (r
&& ctype
!= NULL
)
2505 if (!Float_expression::check_constant(val
, ctype
, this->location()))
2507 Float_expression::constrain_float(val
, ctype
);
2509 *ptype
= ctype
!= NULL
? ctype
: t
;
2513 // Return a complex constant value.
2516 Const_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2523 if (this->type_
!= NULL
)
2524 ctype
= this->type_
;
2526 ctype
= this->constant_
->const_value()->type();
2527 if (ctype
!= NULL
&& ctype
->complex_type() == NULL
)
2533 bool r
= this->constant_
->const_value()->expr()->complex_constant_value(real
,
2537 this->seen_
= false;
2539 if (r
&& ctype
!= NULL
)
2541 if (!Complex_expression::check_constant(real
, imag
, ctype
,
2544 Complex_expression::constrain_complex(real
, imag
, ctype
);
2546 *ptype
= ctype
!= NULL
? ctype
: t
;
2550 // Return the type of the const reference.
2553 Const_expression::do_type()
2555 if (this->type_
!= NULL
)
2558 Named_constant
* nc
= this->constant_
->const_value();
2560 if (this->seen_
|| nc
->lowering())
2562 this->report_error(_("constant refers to itself"));
2563 this->type_
= Type::make_error_type();
2569 Type
* ret
= nc
->type();
2573 this->seen_
= false;
2577 // During parsing, a named constant may have a NULL type, but we
2578 // must not return a NULL type here.
2579 ret
= nc
->expr()->type();
2581 this->seen_
= false;
2586 // Set the type of the const reference.
2589 Const_expression::do_determine_type(const Type_context
* context
)
2591 Type
* ctype
= this->constant_
->const_value()->type();
2592 Type
* cetype
= (ctype
!= NULL
2594 : this->constant_
->const_value()->expr()->type());
2595 if (ctype
!= NULL
&& !ctype
->is_abstract())
2597 else if (context
->type
!= NULL
2598 && (context
->type
->integer_type() != NULL
2599 || context
->type
->float_type() != NULL
2600 || context
->type
->complex_type() != NULL
)
2601 && (cetype
->integer_type() != NULL
2602 || cetype
->float_type() != NULL
2603 || cetype
->complex_type() != NULL
))
2604 this->type_
= context
->type
;
2605 else if (context
->type
!= NULL
2606 && context
->type
->is_string_type()
2607 && cetype
->is_string_type())
2608 this->type_
= context
->type
;
2609 else if (context
->type
!= NULL
2610 && context
->type
->is_boolean_type()
2611 && cetype
->is_boolean_type())
2612 this->type_
= context
->type
;
2613 else if (!context
->may_be_abstract
)
2615 if (cetype
->is_abstract())
2616 cetype
= cetype
->make_non_abstract_type();
2617 this->type_
= cetype
;
2621 // Check for a loop in which the initializer of a constant refers to
2622 // the constant itself.
2625 Const_expression::check_for_init_loop()
2627 if (this->type_
!= NULL
&& this->type_
->is_error_type())
2632 this->report_error(_("constant refers to itself"));
2633 this->type_
= Type::make_error_type();
2637 Expression
* init
= this->constant_
->const_value()->expr();
2638 Find_named_object
find_named_object(this->constant_
);
2641 Expression::traverse(&init
, &find_named_object
);
2642 this->seen_
= false;
2644 if (find_named_object
.found())
2646 if (this->type_
== NULL
|| !this->type_
->is_error_type())
2648 this->report_error(_("constant refers to itself"));
2649 this->type_
= Type::make_error_type();
2655 // Check types of a const reference.
2658 Const_expression::do_check_types(Gogo
*)
2660 if (this->type_
!= NULL
&& this->type_
->is_error_type())
2663 this->check_for_init_loop();
2665 if (this->type_
== NULL
|| this->type_
->is_abstract())
2668 // Check for integer overflow.
2669 if (this->type_
->integer_type() != NULL
)
2674 if (!this->integer_constant_value(true, ival
, &dummy
))
2678 Expression
* cexpr
= this->constant_
->const_value()->expr();
2679 if (cexpr
->float_constant_value(fval
, &dummy
))
2681 if (!mpfr_integer_p(fval
))
2682 this->report_error(_("floating point constant "
2683 "truncated to integer"));
2686 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2687 Integer_expression::check_constant(ival
, this->type_
,
2697 // Return a tree for the const reference.
2700 Const_expression::do_get_tree(Translate_context
* context
)
2702 Gogo
* gogo
= context
->gogo();
2704 if (this->type_
== NULL
)
2705 type_tree
= NULL_TREE
;
2708 type_tree
= this->type_
->get_tree(gogo
);
2709 if (type_tree
== error_mark_node
)
2710 return error_mark_node
;
2713 // If the type has been set for this expression, but the underlying
2714 // object is an abstract int or float, we try to get the abstract
2715 // value. Otherwise we may lose something in the conversion.
2716 if (this->type_
!= NULL
2717 && (this->constant_
->const_value()->type() == NULL
2718 || this->constant_
->const_value()->type()->is_abstract()))
2720 Expression
* expr
= this->constant_
->const_value()->expr();
2724 if (expr
->integer_constant_value(true, ival
, &t
))
2726 tree ret
= Expression::integer_constant_tree(ival
, type_tree
);
2734 if (expr
->float_constant_value(fval
, &t
))
2736 tree ret
= Expression::float_constant_tree(fval
, type_tree
);
2743 if (expr
->complex_constant_value(fval
, imag
, &t
))
2745 tree ret
= Expression::complex_constant_tree(fval
, imag
, type_tree
);
2754 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2755 if (this->type_
== NULL
2756 || const_tree
== error_mark_node
2757 || TREE_TYPE(const_tree
) == error_mark_node
)
2761 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2762 ret
= fold_convert(type_tree
, const_tree
);
2763 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2764 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2765 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2766 ret
= fold(convert_to_real(type_tree
, const_tree
));
2767 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2768 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2774 // Make a reference to a constant in an expression.
2777 Expression::make_const_reference(Named_object
* constant
,
2778 source_location location
)
2780 return new Const_expression(constant
, location
);
2783 // Find a named object in an expression.
2786 Find_named_object::expression(Expression
** pexpr
)
2788 switch ((*pexpr
)->classification())
2790 case Expression::EXPRESSION_CONST_REFERENCE
:
2792 Const_expression
* ce
= static_cast<Const_expression
*>(*pexpr
);
2793 if (ce
->named_object() == this->no_
)
2796 // We need to check a constant initializer explicitly, as
2797 // loops here will not be caught by the loop checking for
2798 // variable initializers.
2799 ce
->check_for_init_loop();
2801 return TRAVERSE_CONTINUE
;
2804 case Expression::EXPRESSION_VAR_REFERENCE
:
2805 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
2807 return TRAVERSE_CONTINUE
;
2808 case Expression::EXPRESSION_FUNC_REFERENCE
:
2809 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
2811 return TRAVERSE_CONTINUE
;
2813 return TRAVERSE_CONTINUE
;
2815 this->found_
= true;
2816 return TRAVERSE_EXIT
;
2821 class Nil_expression
: public Expression
2824 Nil_expression(source_location location
)
2825 : Expression(EXPRESSION_NIL
, location
)
2833 do_is_constant() const
2838 { return Type::make_nil_type(); }
2841 do_determine_type(const Type_context
*)
2849 do_get_tree(Translate_context
*)
2850 { return null_pointer_node
; }
2853 do_export(Export
* exp
) const
2854 { exp
->write_c_string("nil"); }
2857 // Import a nil expression.
2860 Nil_expression::do_import(Import
* imp
)
2862 imp
->require_c_string("nil");
2863 return Expression::make_nil(imp
->location());
2866 // Make a nil expression.
2869 Expression::make_nil(source_location location
)
2871 return new Nil_expression(location
);
2874 // The value of the predeclared constant iota. This is little more
2875 // than a marker. This will be lowered to an integer in
2876 // Const_expression::do_lower, which is where we know the value that
2879 class Iota_expression
: public Parser_expression
2882 Iota_expression(source_location location
)
2883 : Parser_expression(EXPRESSION_IOTA
, location
)
2888 do_lower(Gogo
*, Named_object
*, int)
2889 { gcc_unreachable(); }
2891 // There should only ever be one of these.
2894 { gcc_unreachable(); }
2897 // Make an iota expression. This is only called for one case: the
2898 // value of the predeclared constant iota.
2901 Expression::make_iota()
2903 static Iota_expression
iota_expression(UNKNOWN_LOCATION
);
2904 return &iota_expression
;
2907 // A type conversion expression.
2909 class Type_conversion_expression
: public Expression
2912 Type_conversion_expression(Type
* type
, Expression
* expr
,
2913 source_location location
)
2914 : Expression(EXPRESSION_CONVERSION
, location
),
2915 type_(type
), expr_(expr
), may_convert_function_types_(false)
2918 // Return the type to which we are converting.
2921 { return this->type_
; }
2923 // Return the expression which we are converting.
2926 { return this->expr_
; }
2928 // Permit converting from one function type to another. This is
2929 // used internally for method expressions.
2931 set_may_convert_function_types()
2933 this->may_convert_function_types_
= true;
2936 // Import a type conversion expression.
2942 do_traverse(Traverse
* traverse
);
2945 do_lower(Gogo
*, Named_object
*, int);
2948 do_is_constant() const
2949 { return this->expr_
->is_constant(); }
2952 do_integer_constant_value(bool, mpz_t
, Type
**) const;
2955 do_float_constant_value(mpfr_t
, Type
**) const;
2958 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
2961 do_string_constant_value(std::string
*) const;
2965 { return this->type_
; }
2968 do_determine_type(const Type_context
*)
2970 Type_context
subcontext(this->type_
, false);
2971 this->expr_
->determine_type(&subcontext
);
2975 do_check_types(Gogo
*);
2980 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
2985 do_get_tree(Translate_context
* context
);
2988 do_export(Export
*) const;
2991 // The type to convert to.
2993 // The expression to convert.
2995 // True if this is permitted to convert function types. This is
2996 // used internally for method expressions.
2997 bool may_convert_function_types_
;
3003 Type_conversion_expression::do_traverse(Traverse
* traverse
)
3005 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3006 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3007 return TRAVERSE_EXIT
;
3008 return TRAVERSE_CONTINUE
;
3011 // Convert to a constant at lowering time.
3014 Type_conversion_expression::do_lower(Gogo
*, Named_object
*, int)
3016 Type
* type
= this->type_
;
3017 Expression
* val
= this->expr_
;
3018 source_location location
= this->location();
3020 if (type
->integer_type() != NULL
)
3025 if (val
->integer_constant_value(false, ival
, &dummy
))
3027 if (!Integer_expression::check_constant(ival
, type
, location
))
3028 mpz_set_ui(ival
, 0);
3029 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
3036 if (val
->float_constant_value(fval
, &dummy
))
3038 if (!mpfr_integer_p(fval
))
3041 "floating point constant truncated to integer");
3042 return Expression::make_error(location
);
3044 mpfr_get_z(ival
, fval
, GMP_RNDN
);
3045 if (!Integer_expression::check_constant(ival
, type
, location
))
3046 mpz_set_ui(ival
, 0);
3047 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
3056 if (type
->float_type() != NULL
)
3061 if (val
->float_constant_value(fval
, &dummy
))
3063 if (!Float_expression::check_constant(fval
, type
, location
))
3064 mpfr_set_ui(fval
, 0, GMP_RNDN
);
3065 Float_expression::constrain_float(fval
, type
);
3066 Expression
*ret
= Expression::make_float(&fval
, type
, location
);
3073 if (type
->complex_type() != NULL
)
3080 if (val
->complex_constant_value(real
, imag
, &dummy
))
3082 if (!Complex_expression::check_constant(real
, imag
, type
, location
))
3084 mpfr_set_ui(real
, 0, GMP_RNDN
);
3085 mpfr_set_ui(imag
, 0, GMP_RNDN
);
3087 Complex_expression::constrain_complex(real
, imag
, type
);
3088 Expression
* ret
= Expression::make_complex(&real
, &imag
, type
,
3098 if (type
->is_open_array_type() && type
->named_type() == NULL
)
3100 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3101 bool is_byte
= element_type
== Type::lookup_integer_type("uint8");
3102 bool is_int
= element_type
== Type::lookup_integer_type("int");
3103 if (is_byte
|| is_int
)
3106 if (val
->string_constant_value(&s
))
3108 Expression_list
* vals
= new Expression_list();
3111 for (std::string::const_iterator p
= s
.begin();
3116 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
3117 Expression
* v
= Expression::make_integer(&val
,
3126 const char *p
= s
.data();
3127 const char *pend
= s
.data() + s
.length();
3131 int adv
= Lex::fetch_char(p
, &c
);
3134 warning_at(this->location(), 0,
3135 "invalid UTF-8 encoding");
3140 mpz_init_set_ui(val
, c
);
3141 Expression
* v
= Expression::make_integer(&val
,
3149 return Expression::make_slice_composite_literal(type
, vals
,
3158 // Return the constant integer value if there is one.
3161 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant
,
3165 if (this->type_
->integer_type() == NULL
)
3171 if (this->expr_
->integer_constant_value(iota_is_constant
, ival
, &dummy
))
3173 if (!Integer_expression::check_constant(ival
, this->type_
,
3181 *ptype
= this->type_
;
3188 if (this->expr_
->float_constant_value(fval
, &dummy
))
3190 mpfr_get_z(val
, fval
, GMP_RNDN
);
3192 if (!Integer_expression::check_constant(val
, this->type_
,
3195 *ptype
= this->type_
;
3203 // Return the constant floating point value if there is one.
3206 Type_conversion_expression::do_float_constant_value(mpfr_t val
,
3209 if (this->type_
->float_type() == NULL
)
3215 if (this->expr_
->float_constant_value(fval
, &dummy
))
3217 if (!Float_expression::check_constant(fval
, this->type_
,
3223 mpfr_set(val
, fval
, GMP_RNDN
);
3225 Float_expression::constrain_float(val
, this->type_
);
3226 *ptype
= this->type_
;
3234 // Return the constant complex value if there is one.
3237 Type_conversion_expression::do_complex_constant_value(mpfr_t real
,
3241 if (this->type_
->complex_type() == NULL
)
3249 if (this->expr_
->complex_constant_value(rval
, ival
, &dummy
))
3251 if (!Complex_expression::check_constant(rval
, ival
, this->type_
,
3258 mpfr_set(real
, rval
, GMP_RNDN
);
3259 mpfr_set(imag
, ival
, GMP_RNDN
);
3262 Complex_expression::constrain_complex(real
, imag
, this->type_
);
3263 *ptype
= this->type_
;
3272 // Return the constant string value if there is one.
3275 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3277 if (this->type_
->is_string_type()
3278 && this->expr_
->type()->integer_type() != NULL
)
3283 if (this->expr_
->integer_constant_value(false, ival
, &dummy
))
3285 unsigned long ulval
= mpz_get_ui(ival
);
3286 if (mpz_cmp_ui(ival
, ulval
) == 0)
3288 Lex::append_char(ulval
, true, val
, this->location());
3296 // FIXME: Could handle conversion from const []int here.
3301 // Check that types are convertible.
3304 Type_conversion_expression::do_check_types(Gogo
*)
3306 Type
* type
= this->type_
;
3307 Type
* expr_type
= this->expr_
->type();
3310 if (type
->is_error_type()
3311 || type
->is_undefined()
3312 || expr_type
->is_error_type()
3313 || expr_type
->is_undefined())
3315 // Make sure we emit an error for an undefined type.
3318 this->set_is_error();
3322 if (this->may_convert_function_types_
3323 && type
->function_type() != NULL
3324 && expr_type
->function_type() != NULL
)
3327 if (Type::are_convertible(type
, expr_type
, &reason
))
3330 error_at(this->location(), "%s", reason
.c_str());
3331 this->set_is_error();
3334 // Get a tree for a type conversion.
3337 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3339 Gogo
* gogo
= context
->gogo();
3340 tree type_tree
= this->type_
->get_tree(gogo
);
3341 tree expr_tree
= this->expr_
->get_tree(context
);
3343 if (type_tree
== error_mark_node
3344 || expr_tree
== error_mark_node
3345 || TREE_TYPE(expr_tree
) == error_mark_node
)
3346 return error_mark_node
;
3348 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3349 return fold_convert(type_tree
, expr_tree
);
3351 Type
* type
= this->type_
;
3352 Type
* expr_type
= this->expr_
->type();
3354 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3355 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3356 expr_tree
, this->location());
3357 else if (type
->integer_type() != NULL
)
3359 if (expr_type
->integer_type() != NULL
3360 || expr_type
->float_type() != NULL
3361 || expr_type
->is_unsafe_pointer_type())
3362 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3366 else if (type
->float_type() != NULL
)
3368 if (expr_type
->integer_type() != NULL
3369 || expr_type
->float_type() != NULL
)
3370 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3374 else if (type
->complex_type() != NULL
)
3376 if (expr_type
->complex_type() != NULL
)
3377 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3381 else if (type
->is_string_type()
3382 && expr_type
->integer_type() != NULL
)
3384 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3385 if (host_integerp(expr_tree
, 0))
3387 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3389 Lex::append_char(intval
, true, &s
, this->location());
3390 Expression
* se
= Expression::make_string(s
, this->location());
3391 return se
->get_tree(context
);
3394 static tree int_to_string_fndecl
;
3395 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3397 "__go_int_to_string",
3401 fold_convert(integer_type_node
, expr_tree
));
3403 else if (type
->is_string_type()
3404 && (expr_type
->array_type() != NULL
3405 || (expr_type
->points_to() != NULL
3406 && expr_type
->points_to()->array_type() != NULL
)))
3408 Type
* t
= expr_type
;
3409 if (t
->points_to() != NULL
)
3412 expr_tree
= build_fold_indirect_ref(expr_tree
);
3414 if (!DECL_P(expr_tree
))
3415 expr_tree
= save_expr(expr_tree
);
3416 Array_type
* a
= t
->array_type();
3417 Type
* e
= a
->element_type()->forwarded();
3418 gcc_assert(e
->integer_type() != NULL
);
3419 tree valptr
= fold_convert(const_ptr_type_node
,
3420 a
->value_pointer_tree(gogo
, expr_tree
));
3421 tree len
= a
->length_tree(gogo
, expr_tree
);
3422 len
= fold_convert_loc(this->location(), size_type_node
, len
);
3423 if (e
->integer_type()->is_unsigned()
3424 && e
->integer_type()->bits() == 8)
3426 static tree byte_array_to_string_fndecl
;
3427 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3429 "__go_byte_array_to_string",
3432 const_ptr_type_node
,
3439 gcc_assert(e
== Type::lookup_integer_type("int"));
3440 static tree int_array_to_string_fndecl
;
3441 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3443 "__go_int_array_to_string",
3446 const_ptr_type_node
,
3452 else if (type
->is_open_array_type() && expr_type
->is_string_type())
3454 Type
* e
= type
->array_type()->element_type()->forwarded();
3455 gcc_assert(e
->integer_type() != NULL
);
3456 if (e
->integer_type()->is_unsigned()
3457 && e
->integer_type()->bits() == 8)
3459 static tree string_to_byte_array_fndecl
;
3460 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3462 "__go_string_to_byte_array",
3465 TREE_TYPE(expr_tree
),
3470 gcc_assert(e
== Type::lookup_integer_type("int"));
3471 static tree string_to_int_array_fndecl
;
3472 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3474 "__go_string_to_int_array",
3477 TREE_TYPE(expr_tree
),
3481 else if ((type
->is_unsafe_pointer_type()
3482 && expr_type
->points_to() != NULL
)
3483 || (expr_type
->is_unsafe_pointer_type()
3484 && type
->points_to() != NULL
))
3485 ret
= fold_convert(type_tree
, expr_tree
);
3486 else if (type
->is_unsafe_pointer_type()
3487 && expr_type
->integer_type() != NULL
)
3488 ret
= convert_to_pointer(type_tree
, expr_tree
);
3489 else if (this->may_convert_function_types_
3490 && type
->function_type() != NULL
3491 && expr_type
->function_type() != NULL
)
3492 ret
= fold_convert_loc(this->location(), type_tree
, expr_tree
);
3494 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3495 expr_tree
, this->location());
3500 // Output a type conversion in a constant expression.
3503 Type_conversion_expression::do_export(Export
* exp
) const
3505 exp
->write_c_string("convert(");
3506 exp
->write_type(this->type_
);
3507 exp
->write_c_string(", ");
3508 this->expr_
->export_expression(exp
);
3509 exp
->write_c_string(")");
3512 // Import a type conversion or a struct construction.
3515 Type_conversion_expression::do_import(Import
* imp
)
3517 imp
->require_c_string("convert(");
3518 Type
* type
= imp
->read_type();
3519 imp
->require_c_string(", ");
3520 Expression
* val
= Expression::import_expression(imp
);
3521 imp
->require_c_string(")");
3522 return Expression::make_cast(type
, val
, imp
->location());
3525 // Make a type cast expression.
3528 Expression::make_cast(Type
* type
, Expression
* val
, source_location location
)
3530 if (type
->is_error_type() || val
->is_error_expression())
3531 return Expression::make_error(location
);
3532 return new Type_conversion_expression(type
, val
, location
);
3535 // Unary expressions.
3537 class Unary_expression
: public Expression
3540 Unary_expression(Operator op
, Expression
* expr
, source_location location
)
3541 : Expression(EXPRESSION_UNARY
, location
),
3542 op_(op
), escapes_(true), expr_(expr
)
3545 // Return the operator.
3548 { return this->op_
; }
3550 // Return the operand.
3553 { return this->expr_
; }
3555 // Record that an address expression does not escape.
3557 set_does_not_escape()
3559 gcc_assert(this->op_
== OPERATOR_AND
);
3560 this->escapes_
= false;
3563 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3564 // could be done, false if not.
3566 eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3569 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3570 // could be done, false if not.
3572 eval_float(Operator op
, mpfr_t uval
, mpfr_t val
);
3574 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3575 // true if this could be done, false if not.
3577 eval_complex(Operator op
, mpfr_t ureal
, mpfr_t uimag
, mpfr_t real
,
3585 do_traverse(Traverse
* traverse
)
3586 { return Expression::traverse(&this->expr_
, traverse
); }
3589 do_lower(Gogo
*, Named_object
*, int);
3592 do_is_constant() const;
3595 do_integer_constant_value(bool, mpz_t
, Type
**) const;
3598 do_float_constant_value(mpfr_t
, Type
**) const;
3601 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
3607 do_determine_type(const Type_context
*);
3610 do_check_types(Gogo
*);
3615 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3620 do_is_addressable() const
3621 { return this->op_
== OPERATOR_MULT
; }
3624 do_get_tree(Translate_context
*);
3627 do_export(Export
*) const;
3630 // The unary operator to apply.
3632 // Normally true. False if this is an address expression which does
3633 // not escape the current function.
3639 // If we are taking the address of a composite literal, and the
3640 // contents are not constant, then we want to make a heap composite
3644 Unary_expression::do_lower(Gogo
*, Named_object
*, int)
3646 source_location loc
= this->location();
3647 Operator op
= this->op_
;
3648 Expression
* expr
= this->expr_
;
3650 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3651 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3653 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3654 // moving x to the heap. FIXME: Is it worth doing a real escape
3655 // analysis here? This case is found in math/unsafe.go and is
3656 // therefore worth special casing.
3657 if (op
== OPERATOR_MULT
)
3659 Expression
* e
= expr
;
3660 while (e
->classification() == EXPRESSION_CONVERSION
)
3662 Type_conversion_expression
* te
3663 = static_cast<Type_conversion_expression
*>(e
);
3667 if (e
->classification() == EXPRESSION_UNARY
)
3669 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3670 if (ue
->op_
== OPERATOR_AND
)
3677 ue
->set_does_not_escape();
3682 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
3683 || op
== OPERATOR_NOT
|| op
== OPERATOR_XOR
)
3685 Expression
* ret
= NULL
;
3690 if (expr
->integer_constant_value(false, eval
, &etype
))
3694 if (Unary_expression::eval_integer(op
, etype
, eval
, val
, loc
))
3695 ret
= Expression::make_integer(&val
, etype
, loc
);
3702 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
)
3707 if (expr
->float_constant_value(fval
, &ftype
))
3711 if (Unary_expression::eval_float(op
, fval
, val
))
3712 ret
= Expression::make_float(&val
, ftype
, loc
);
3723 if (expr
->complex_constant_value(fval
, ival
, &ftype
))
3729 if (Unary_expression::eval_complex(op
, fval
, ival
, real
, imag
))
3730 ret
= Expression::make_complex(&real
, &imag
, ftype
, loc
);
3744 // Return whether a unary expression is a constant.
3747 Unary_expression::do_is_constant() const
3749 if (this->op_
== OPERATOR_MULT
)
3751 // Indirecting through a pointer is only constant if the object
3752 // to which the expression points is constant, but we currently
3753 // have no way to determine that.
3756 else if (this->op_
== OPERATOR_AND
)
3758 // Taking the address of a variable is constant if it is a
3759 // global variable, not constant otherwise. In other cases
3760 // taking the address is probably not a constant.
3761 Var_expression
* ve
= this->expr_
->var_expression();
3764 Named_object
* no
= ve
->named_object();
3765 return no
->is_variable() && no
->var_value()->is_global();
3770 return this->expr_
->is_constant();
3773 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3774 // UVAL, if known; it may be NULL. Return true if this could be done,
3778 Unary_expression::eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3779 source_location location
)
3786 case OPERATOR_MINUS
:
3788 return Integer_expression::check_constant(val
, utype
, location
);
3790 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3794 || utype
->integer_type() == NULL
3795 || utype
->integer_type()->is_abstract())
3799 // The number of HOST_WIDE_INTs that it takes to represent
3801 size_t count
= ((mpz_sizeinbase(uval
, 2)
3802 + HOST_BITS_PER_WIDE_INT
3804 / HOST_BITS_PER_WIDE_INT
);
3806 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3807 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3810 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3811 gcc_assert(ecount
<= count
);
3813 // Trim down to the number of words required by the type.
3814 size_t obits
= utype
->integer_type()->bits();
3815 if (!utype
->integer_type()->is_unsigned())
3817 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3818 / HOST_BITS_PER_WIDE_INT
);
3819 gcc_assert(ocount
<= ocount
);
3821 for (size_t i
= 0; i
< ocount
; ++i
)
3824 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3826 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3829 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3833 return Integer_expression::check_constant(val
, utype
, location
);
3842 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3843 // could be done, false if not.
3846 Unary_expression::eval_float(Operator op
, mpfr_t uval
, mpfr_t val
)
3851 mpfr_set(val
, uval
, GMP_RNDN
);
3853 case OPERATOR_MINUS
:
3854 mpfr_neg(val
, uval
, GMP_RNDN
);
3866 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3867 // if this could be done, false if not.
3870 Unary_expression::eval_complex(Operator op
, mpfr_t rval
, mpfr_t ival
,
3871 mpfr_t real
, mpfr_t imag
)
3876 mpfr_set(real
, rval
, GMP_RNDN
);
3877 mpfr_set(imag
, ival
, GMP_RNDN
);
3879 case OPERATOR_MINUS
:
3880 mpfr_neg(real
, rval
, GMP_RNDN
);
3881 mpfr_neg(imag
, ival
, GMP_RNDN
);
3893 // Return the integral constant value of a unary expression, if it has one.
3896 Unary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
3902 if (!this->expr_
->integer_constant_value(iota_is_constant
, uval
, ptype
))
3905 ret
= Unary_expression::eval_integer(this->op_
, *ptype
, uval
, val
,
3911 // Return the floating point constant value of a unary expression, if
3915 Unary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
3920 if (!this->expr_
->float_constant_value(uval
, ptype
))
3923 ret
= Unary_expression::eval_float(this->op_
, uval
, val
);
3928 // Return the complex constant value of a unary expression, if it has
3932 Unary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
3940 if (!this->expr_
->complex_constant_value(rval
, ival
, ptype
))
3943 ret
= Unary_expression::eval_complex(this->op_
, rval
, ival
, real
, imag
);
3949 // Return the type of a unary expression.
3952 Unary_expression::do_type()
3957 case OPERATOR_MINUS
:
3960 return this->expr_
->type();
3963 return Type::make_pointer_type(this->expr_
->type());
3967 Type
* subtype
= this->expr_
->type();
3968 Type
* points_to
= subtype
->points_to();
3969 if (points_to
== NULL
)
3970 return Type::make_error_type();
3979 // Determine abstract types for a unary expression.
3982 Unary_expression::do_determine_type(const Type_context
* context
)
3987 case OPERATOR_MINUS
:
3990 this->expr_
->determine_type(context
);
3994 // Taking the address of something.
3996 Type
* subtype
= (context
->type
== NULL
3998 : context
->type
->points_to());
3999 Type_context
subcontext(subtype
, false);
4000 this->expr_
->determine_type(&subcontext
);
4005 // Indirecting through a pointer.
4007 Type
* subtype
= (context
->type
== NULL
4009 : Type::make_pointer_type(context
->type
));
4010 Type_context
subcontext(subtype
, false);
4011 this->expr_
->determine_type(&subcontext
);
4020 // Check types for a unary expression.
4023 Unary_expression::do_check_types(Gogo
*)
4025 Type
* type
= this->expr_
->type();
4026 if (type
->is_error_type())
4028 this->set_is_error();
4035 case OPERATOR_MINUS
:
4036 if (type
->integer_type() == NULL
4037 && type
->float_type() == NULL
4038 && type
->complex_type() == NULL
)
4039 this->report_error(_("expected numeric type"));
4044 if (type
->integer_type() == NULL
4045 && !type
->is_boolean_type())
4046 this->report_error(_("expected integer or boolean type"));
4050 if (!this->expr_
->is_addressable())
4051 this->report_error(_("invalid operand for unary %<&%>"));
4053 this->expr_
->address_taken(this->escapes_
);
4057 // Indirecting through a pointer.
4058 if (type
->points_to() == NULL
)
4059 this->report_error(_("expected pointer"));
4067 // Get a tree for a unary expression.
4070 Unary_expression::do_get_tree(Translate_context
* context
)
4072 tree expr
= this->expr_
->get_tree(context
);
4073 if (expr
== error_mark_node
)
4074 return error_mark_node
;
4076 source_location loc
= this->location();
4082 case OPERATOR_MINUS
:
4084 tree type
= TREE_TYPE(expr
);
4085 tree compute_type
= excess_precision_type(type
);
4086 if (compute_type
!= NULL_TREE
)
4087 expr
= ::convert(compute_type
, expr
);
4088 tree ret
= fold_build1_loc(loc
, NEGATE_EXPR
,
4089 (compute_type
!= NULL_TREE
4093 if (compute_type
!= NULL_TREE
)
4094 ret
= ::convert(type
, ret
);
4099 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
4100 return fold_build1_loc(loc
, TRUTH_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4102 return fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, expr
,
4103 build_int_cst(TREE_TYPE(expr
), 0));
4106 return fold_build1_loc(loc
, BIT_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4109 // We should not see a non-constant constructor here; cases
4110 // where we would see one should have been moved onto the heap
4111 // at parse time. Taking the address of a nonconstant
4112 // constructor will not do what the programmer expects.
4113 gcc_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
4114 gcc_assert(TREE_CODE(expr
) != ADDR_EXPR
);
4116 // Build a decl for a constant constructor.
4117 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
4119 tree decl
= build_decl(this->location(), VAR_DECL
,
4120 create_tmp_var_name("C"), TREE_TYPE(expr
));
4121 DECL_EXTERNAL(decl
) = 0;
4122 TREE_PUBLIC(decl
) = 0;
4123 TREE_READONLY(decl
) = 1;
4124 TREE_CONSTANT(decl
) = 1;
4125 TREE_STATIC(decl
) = 1;
4126 TREE_ADDRESSABLE(decl
) = 1;
4127 DECL_ARTIFICIAL(decl
) = 1;
4128 DECL_INITIAL(decl
) = expr
;
4129 rest_of_decl_compilation(decl
, 1, 0);
4133 return build_fold_addr_expr_loc(loc
, expr
);
4137 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
4139 // If we are dereferencing the pointer to a large struct, we
4140 // need to check for nil. We don't bother to check for small
4141 // structs because we expect the system to crash on a nil
4142 // pointer dereference.
4143 HOST_WIDE_INT s
= int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr
)));
4144 if (s
== -1 || s
>= 4096)
4147 expr
= save_expr(expr
);
4148 tree compare
= fold_build2_loc(loc
, EQ_EXPR
, boolean_type_node
,
4150 fold_convert(TREE_TYPE(expr
),
4151 null_pointer_node
));
4152 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4154 expr
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(expr
),
4155 build3(COND_EXPR
, void_type_node
,
4156 compare
, crash
, NULL_TREE
),
4160 // If the type of EXPR is a recursive pointer type, then we
4161 // need to insert a cast before indirecting.
4162 if (TREE_TYPE(TREE_TYPE(expr
)) == ptr_type_node
)
4164 Type
* pt
= this->expr_
->type()->points_to();
4165 tree ind
= pt
->get_tree(context
->gogo());
4166 expr
= fold_convert_loc(loc
, build_pointer_type(ind
), expr
);
4169 return build_fold_indirect_ref_loc(loc
, expr
);
4177 // Export a unary expression.
4180 Unary_expression::do_export(Export
* exp
) const
4185 exp
->write_c_string("+ ");
4187 case OPERATOR_MINUS
:
4188 exp
->write_c_string("- ");
4191 exp
->write_c_string("! ");
4194 exp
->write_c_string("^ ");
4201 this->expr_
->export_expression(exp
);
4204 // Import a unary expression.
4207 Unary_expression::do_import(Import
* imp
)
4210 switch (imp
->get_char())
4216 op
= OPERATOR_MINUS
;
4227 imp
->require_c_string(" ");
4228 Expression
* expr
= Expression::import_expression(imp
);
4229 return Expression::make_unary(op
, expr
, imp
->location());
4232 // Make a unary expression.
4235 Expression::make_unary(Operator op
, Expression
* expr
, source_location location
)
4237 return new Unary_expression(op
, expr
, location
);
4240 // If this is an indirection through a pointer, return the expression
4241 // being pointed through. Otherwise return this.
4246 if (this->classification_
== EXPRESSION_UNARY
)
4248 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4249 if (ue
->op() == OPERATOR_MULT
)
4250 return ue
->operand();
4255 // Class Binary_expression.
4260 Binary_expression::do_traverse(Traverse
* traverse
)
4262 int t
= Expression::traverse(&this->left_
, traverse
);
4263 if (t
== TRAVERSE_EXIT
)
4264 return TRAVERSE_EXIT
;
4265 return Expression::traverse(&this->right_
, traverse
);
4268 // Compare integer constants according to OP.
4271 Binary_expression::compare_integer(Operator op
, mpz_t left_val
,
4274 int i
= mpz_cmp(left_val
, right_val
);
4279 case OPERATOR_NOTEQ
:
4294 // Compare floating point constants according to OP.
4297 Binary_expression::compare_float(Operator op
, Type
* type
, mpfr_t left_val
,
4302 i
= mpfr_cmp(left_val
, right_val
);
4306 mpfr_init_set(lv
, left_val
, GMP_RNDN
);
4308 mpfr_init_set(rv
, right_val
, GMP_RNDN
);
4309 Float_expression::constrain_float(lv
, type
);
4310 Float_expression::constrain_float(rv
, type
);
4311 i
= mpfr_cmp(lv
, rv
);
4319 case OPERATOR_NOTEQ
:
4334 // Compare complex constants according to OP. Complex numbers may
4335 // only be compared for equality.
4338 Binary_expression::compare_complex(Operator op
, Type
* type
,
4339 mpfr_t left_real
, mpfr_t left_imag
,
4340 mpfr_t right_real
, mpfr_t right_imag
)
4344 is_equal
= (mpfr_cmp(left_real
, right_real
) == 0
4345 && mpfr_cmp(left_imag
, right_imag
) == 0);
4350 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4351 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4354 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4355 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4356 Complex_expression::constrain_complex(lr
, li
, type
);
4357 Complex_expression::constrain_complex(rr
, ri
, type
);
4358 is_equal
= mpfr_cmp(lr
, rr
) == 0 && mpfr_cmp(li
, ri
) == 0;
4368 case OPERATOR_NOTEQ
:
4375 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4376 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4377 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4378 // this could be done, false if not.
4381 Binary_expression::eval_integer(Operator op
, Type
* left_type
, mpz_t left_val
,
4382 Type
* right_type
, mpz_t right_val
,
4383 source_location location
, mpz_t val
)
4385 bool is_shift_op
= false;
4389 case OPERATOR_ANDAND
:
4391 case OPERATOR_NOTEQ
:
4396 // These return boolean values. We should probably handle them
4397 // anyhow in case a type conversion is used on the result.
4400 mpz_add(val
, left_val
, right_val
);
4402 case OPERATOR_MINUS
:
4403 mpz_sub(val
, left_val
, right_val
);
4406 mpz_ior(val
, left_val
, right_val
);
4409 mpz_xor(val
, left_val
, right_val
);
4412 mpz_mul(val
, left_val
, right_val
);
4415 if (mpz_sgn(right_val
) != 0)
4416 mpz_tdiv_q(val
, left_val
, right_val
);
4419 error_at(location
, "division by zero");
4425 if (mpz_sgn(right_val
) != 0)
4426 mpz_tdiv_r(val
, left_val
, right_val
);
4429 error_at(location
, "division by zero");
4434 case OPERATOR_LSHIFT
:
4436 unsigned long shift
= mpz_get_ui(right_val
);
4437 if (mpz_cmp_ui(right_val
, shift
) != 0)
4439 error_at(location
, "shift count overflow");
4443 mpz_mul_2exp(val
, left_val
, shift
);
4448 case OPERATOR_RSHIFT
:
4450 unsigned long shift
= mpz_get_ui(right_val
);
4451 if (mpz_cmp_ui(right_val
, shift
) != 0)
4453 error_at(location
, "shift count overflow");
4457 if (mpz_cmp_ui(left_val
, 0) >= 0)
4458 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4460 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4466 mpz_and(val
, left_val
, right_val
);
4468 case OPERATOR_BITCLEAR
:
4472 mpz_com(tval
, right_val
);
4473 mpz_and(val
, left_val
, tval
);
4481 Type
* type
= left_type
;
4486 else if (type
!= right_type
&& right_type
!= NULL
)
4488 if (type
->is_abstract())
4490 else if (!right_type
->is_abstract())
4492 // This look like a type error which should be diagnosed
4493 // elsewhere. Don't do anything here, to avoid an
4494 // unhelpful chain of error messages.
4500 if (type
!= NULL
&& !type
->is_abstract())
4502 // We have to check the operands too, as we have implicitly
4503 // coerced them to TYPE.
4504 if ((type
!= left_type
4505 && !Integer_expression::check_constant(left_val
, type
, location
))
4507 && type
!= right_type
4508 && !Integer_expression::check_constant(right_val
, type
,
4510 || !Integer_expression::check_constant(val
, type
, location
))
4517 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4518 // Return true if this could be done, false if not.
4521 Binary_expression::eval_float(Operator op
, Type
* left_type
, mpfr_t left_val
,
4522 Type
* right_type
, mpfr_t right_val
,
4523 mpfr_t val
, source_location location
)
4528 case OPERATOR_ANDAND
:
4530 case OPERATOR_NOTEQ
:
4535 // These return boolean values. We should probably handle them
4536 // anyhow in case a type conversion is used on the result.
4539 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4541 case OPERATOR_MINUS
:
4542 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4547 case OPERATOR_BITCLEAR
:
4550 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4553 if (mpfr_zero_p(right_val
))
4554 error_at(location
, "division by zero");
4555 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4559 case OPERATOR_LSHIFT
:
4560 case OPERATOR_RSHIFT
:
4566 Type
* type
= left_type
;
4569 else if (type
!= right_type
&& right_type
!= NULL
)
4571 if (type
->is_abstract())
4573 else if (!right_type
->is_abstract())
4575 // This looks like a type error which should be diagnosed
4576 // elsewhere. Don't do anything here, to avoid an unhelpful
4577 // chain of error messages.
4582 if (type
!= NULL
&& !type
->is_abstract())
4584 if ((type
!= left_type
4585 && !Float_expression::check_constant(left_val
, type
, location
))
4586 || (type
!= right_type
4587 && !Float_expression::check_constant(right_val
, type
,
4589 || !Float_expression::check_constant(val
, type
, location
))
4590 mpfr_set_ui(val
, 0, GMP_RNDN
);
4596 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4597 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4598 // could be done, false if not.
4601 Binary_expression::eval_complex(Operator op
, Type
* left_type
,
4602 mpfr_t left_real
, mpfr_t left_imag
,
4604 mpfr_t right_real
, mpfr_t right_imag
,
4605 mpfr_t real
, mpfr_t imag
,
4606 source_location location
)
4611 case OPERATOR_ANDAND
:
4613 case OPERATOR_NOTEQ
:
4618 // These return boolean values and must be handled differently.
4621 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4622 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4624 case OPERATOR_MINUS
:
4625 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4626 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4631 case OPERATOR_BITCLEAR
:
4635 // You might think that multiplying two complex numbers would
4636 // be simple, and you would be right, until you start to think
4637 // about getting the right answer for infinity. If one
4638 // operand here is infinity and the other is anything other
4639 // than zero or NaN, then we are going to wind up subtracting
4640 // two infinity values. That will give us a NaN, but the
4641 // correct answer is infinity.
4645 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4649 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4653 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4657 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4659 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4660 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4662 // If we get NaN on both sides, check whether it should really
4663 // be infinity. The rule is that if either side of the
4664 // complex number is infinity, then the whole value is
4665 // infinity, even if the other side is NaN. So the only case
4666 // we have to fix is the one in which both sides are NaN.
4667 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4668 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4669 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4671 bool is_infinity
= false;
4675 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4676 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4680 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4681 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4683 // If the left side is infinity, then the result is
4685 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4687 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4688 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4689 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4690 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4693 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4694 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4698 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4699 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4704 // If the right side is infinity, then the result is
4706 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4708 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4709 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4710 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4711 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4714 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4715 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4719 mpfr_set_ui(li
, 0, GMP_RNDN
);
4720 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4725 // If we got an overflow in the intermediate computations,
4726 // then the result is infinity.
4728 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4729 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4733 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4734 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4738 mpfr_set_ui(li
, 0, GMP_RNDN
);
4739 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4743 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4744 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4748 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4749 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4756 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4757 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4758 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4759 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4760 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4761 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4762 mpfr_set_inf(real
, mpfr_sgn(real
));
4763 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4780 // For complex division we want to avoid having an
4781 // intermediate overflow turn the whole result in a NaN. We
4782 // scale the values to try to avoid this.
4784 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4785 error_at(location
, "division by zero");
4791 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4792 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4795 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4799 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4800 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4802 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4804 ilogbw
= mpfr_get_exp(t
);
4805 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4806 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4811 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4812 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4813 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4815 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4816 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4817 mpfr_add(real
, real
, t
, GMP_RNDN
);
4818 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4819 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4821 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4822 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4823 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4824 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4825 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4827 // If we wind up with NaN on both sides, check whether we
4828 // should really have infinity. The rule is that if either
4829 // side of the complex number is infinity, then the whole
4830 // value is infinity, even if the other side is NaN. So the
4831 // only case we have to fix is the one in which both sides are
4833 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4834 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4835 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4837 if (mpfr_zero_p(denom
))
4839 mpfr_set_inf(real
, mpfr_sgn(rr
));
4840 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4841 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4842 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4844 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4845 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4847 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4848 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4851 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4852 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4856 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
4860 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
4862 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4863 mpfr_set_inf(real
, mpfr_sgn(t3
));
4865 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
4866 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
4867 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4868 mpfr_set_inf(imag
, mpfr_sgn(t3
));
4874 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
4875 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
4877 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4878 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
4881 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4882 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
4886 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
4890 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
4892 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4893 mpfr_set_ui(real
, 0, GMP_RNDN
);
4894 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
4896 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
4897 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
4898 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4899 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4900 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
4918 case OPERATOR_LSHIFT
:
4919 case OPERATOR_RSHIFT
:
4925 Type
* type
= left_type
;
4928 else if (type
!= right_type
&& right_type
!= NULL
)
4930 if (type
->is_abstract())
4932 else if (!right_type
->is_abstract())
4934 // This looks like a type error which should be diagnosed
4935 // elsewhere. Don't do anything here, to avoid an unhelpful
4936 // chain of error messages.
4941 if (type
!= NULL
&& !type
->is_abstract())
4943 if ((type
!= left_type
4944 && !Complex_expression::check_constant(left_real
, left_imag
,
4946 || (type
!= right_type
4947 && !Complex_expression::check_constant(right_real
, right_imag
,
4949 || !Complex_expression::check_constant(real
, imag
, type
,
4952 mpfr_set_ui(real
, 0, GMP_RNDN
);
4953 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4960 // Lower a binary expression. We have to evaluate constant
4961 // expressions now, in order to implement Go's unlimited precision
4965 Binary_expression::do_lower(Gogo
*, Named_object
*, int)
4967 source_location location
= this->location();
4968 Operator op
= this->op_
;
4969 Expression
* left
= this->left_
;
4970 Expression
* right
= this->right_
;
4972 const bool is_comparison
= (op
== OPERATOR_EQEQ
4973 || op
== OPERATOR_NOTEQ
4974 || op
== OPERATOR_LT
4975 || op
== OPERATOR_LE
4976 || op
== OPERATOR_GT
4977 || op
== OPERATOR_GE
);
4979 // Integer constant expressions.
4985 mpz_init(right_val
);
4987 if (left
->integer_constant_value(false, left_val
, &left_type
)
4988 && right
->integer_constant_value(false, right_val
, &right_type
))
4990 Expression
* ret
= NULL
;
4991 if (left_type
!= right_type
4992 && left_type
!= NULL
4993 && right_type
!= NULL
4994 && left_type
->base() != right_type
->base()
4995 && op
!= OPERATOR_LSHIFT
4996 && op
!= OPERATOR_RSHIFT
)
4998 // May be a type error--let it be diagnosed later.
5000 else if (is_comparison
)
5002 bool b
= Binary_expression::compare_integer(op
, left_val
,
5004 ret
= Expression::make_cast(Type::lookup_bool_type(),
5005 Expression::make_boolean(b
, location
),
5013 if (Binary_expression::eval_integer(op
, left_type
, left_val
,
5014 right_type
, right_val
,
5017 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
);
5019 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
5021 else if (left_type
== NULL
)
5023 else if (right_type
== NULL
)
5025 else if (!left_type
->is_abstract()
5026 && left_type
->named_type() != NULL
)
5028 else if (!right_type
->is_abstract()
5029 && right_type
->named_type() != NULL
)
5031 else if (!left_type
->is_abstract())
5033 else if (!right_type
->is_abstract())
5035 else if (left_type
->float_type() != NULL
)
5037 else if (right_type
->float_type() != NULL
)
5039 else if (left_type
->complex_type() != NULL
)
5041 else if (right_type
->complex_type() != NULL
)
5045 ret
= Expression::make_integer(&val
, type
, location
);
5053 mpz_clear(right_val
);
5054 mpz_clear(left_val
);
5058 mpz_clear(right_val
);
5059 mpz_clear(left_val
);
5062 // Floating point constant expressions.
5065 mpfr_init(left_val
);
5068 mpfr_init(right_val
);
5070 if (left
->float_constant_value(left_val
, &left_type
)
5071 && right
->float_constant_value(right_val
, &right_type
))
5073 Expression
* ret
= NULL
;
5074 if (left_type
!= right_type
5075 && left_type
!= NULL
5076 && right_type
!= NULL
5077 && left_type
->base() != right_type
->base()
5078 && op
!= OPERATOR_LSHIFT
5079 && op
!= OPERATOR_RSHIFT
)
5081 // May be a type error--let it be diagnosed later.
5083 else if (is_comparison
)
5085 bool b
= Binary_expression::compare_float(op
,
5089 left_val
, right_val
);
5090 ret
= Expression::make_boolean(b
, location
);
5097 if (Binary_expression::eval_float(op
, left_type
, left_val
,
5098 right_type
, right_val
, val
,
5101 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5102 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5104 if (left_type
== NULL
)
5106 else if (right_type
== NULL
)
5108 else if (!left_type
->is_abstract()
5109 && left_type
->named_type() != NULL
)
5111 else if (!right_type
->is_abstract()
5112 && right_type
->named_type() != NULL
)
5114 else if (!left_type
->is_abstract())
5116 else if (!right_type
->is_abstract())
5118 else if (left_type
->float_type() != NULL
)
5120 else if (right_type
->float_type() != NULL
)
5124 ret
= Expression::make_float(&val
, type
, location
);
5132 mpfr_clear(right_val
);
5133 mpfr_clear(left_val
);
5137 mpfr_clear(right_val
);
5138 mpfr_clear(left_val
);
5141 // Complex constant expressions.
5145 mpfr_init(left_real
);
5146 mpfr_init(left_imag
);
5151 mpfr_init(right_real
);
5152 mpfr_init(right_imag
);
5155 if (left
->complex_constant_value(left_real
, left_imag
, &left_type
)
5156 && right
->complex_constant_value(right_real
, right_imag
, &right_type
))
5158 Expression
* ret
= NULL
;
5159 if (left_type
!= right_type
5160 && left_type
!= NULL
5161 && right_type
!= NULL
5162 && left_type
->base() != right_type
->base())
5164 // May be a type error--let it be diagnosed later.
5166 else if (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
)
5168 bool b
= Binary_expression::compare_complex(op
,
5176 ret
= Expression::make_boolean(b
, location
);
5185 if (Binary_expression::eval_complex(op
, left_type
,
5186 left_real
, left_imag
,
5188 right_real
, right_imag
,
5192 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5193 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5195 if (left_type
== NULL
)
5197 else if (right_type
== NULL
)
5199 else if (!left_type
->is_abstract()
5200 && left_type
->named_type() != NULL
)
5202 else if (!right_type
->is_abstract()
5203 && right_type
->named_type() != NULL
)
5205 else if (!left_type
->is_abstract())
5207 else if (!right_type
->is_abstract())
5209 else if (left_type
->complex_type() != NULL
)
5211 else if (right_type
->complex_type() != NULL
)
5215 ret
= Expression::make_complex(&real
, &imag
, type
,
5224 mpfr_clear(left_real
);
5225 mpfr_clear(left_imag
);
5226 mpfr_clear(right_real
);
5227 mpfr_clear(right_imag
);
5232 mpfr_clear(left_real
);
5233 mpfr_clear(left_imag
);
5234 mpfr_clear(right_real
);
5235 mpfr_clear(right_imag
);
5238 // String constant expressions.
5239 if (op
== OPERATOR_PLUS
5240 && left
->type()->is_string_type()
5241 && right
->type()->is_string_type())
5243 std::string left_string
;
5244 std::string right_string
;
5245 if (left
->string_constant_value(&left_string
)
5246 && right
->string_constant_value(&right_string
))
5247 return Expression::make_string(left_string
+ right_string
, location
);
5253 // Return the integer constant value, if it has one.
5256 Binary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
5262 if (!this->left_
->integer_constant_value(iota_is_constant
, left_val
,
5265 mpz_clear(left_val
);
5270 mpz_init(right_val
);
5272 if (!this->right_
->integer_constant_value(iota_is_constant
, right_val
,
5275 mpz_clear(right_val
);
5276 mpz_clear(left_val
);
5281 if (left_type
!= right_type
5282 && left_type
!= NULL
5283 && right_type
!= NULL
5284 && left_type
->base() != right_type
->base()
5285 && this->op_
!= OPERATOR_RSHIFT
5286 && this->op_
!= OPERATOR_LSHIFT
)
5289 ret
= Binary_expression::eval_integer(this->op_
, left_type
, left_val
,
5290 right_type
, right_val
,
5291 this->location(), val
);
5293 mpz_clear(right_val
);
5294 mpz_clear(left_val
);
5302 // Return the floating point constant value, if it has one.
5305 Binary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
5308 mpfr_init(left_val
);
5310 if (!this->left_
->float_constant_value(left_val
, &left_type
))
5312 mpfr_clear(left_val
);
5317 mpfr_init(right_val
);
5319 if (!this->right_
->float_constant_value(right_val
, &right_type
))
5321 mpfr_clear(right_val
);
5322 mpfr_clear(left_val
);
5327 if (left_type
!= right_type
5328 && left_type
!= NULL
5329 && right_type
!= NULL
5330 && left_type
->base() != right_type
->base())
5333 ret
= Binary_expression::eval_float(this->op_
, left_type
, left_val
,
5334 right_type
, right_val
,
5335 val
, this->location());
5337 mpfr_clear(left_val
);
5338 mpfr_clear(right_val
);
5346 // Return the complex constant value, if it has one.
5349 Binary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
5354 mpfr_init(left_real
);
5355 mpfr_init(left_imag
);
5357 if (!this->left_
->complex_constant_value(left_real
, left_imag
, &left_type
))
5359 mpfr_clear(left_real
);
5360 mpfr_clear(left_imag
);
5366 mpfr_init(right_real
);
5367 mpfr_init(right_imag
);
5369 if (!this->right_
->complex_constant_value(right_real
, right_imag
,
5372 mpfr_clear(left_real
);
5373 mpfr_clear(left_imag
);
5374 mpfr_clear(right_real
);
5375 mpfr_clear(right_imag
);
5380 if (left_type
!= right_type
5381 && left_type
!= NULL
5382 && right_type
!= NULL
5383 && left_type
->base() != right_type
->base())
5386 ret
= Binary_expression::eval_complex(this->op_
, left_type
,
5387 left_real
, left_imag
,
5389 right_real
, right_imag
,
5392 mpfr_clear(left_real
);
5393 mpfr_clear(left_imag
);
5394 mpfr_clear(right_real
);
5395 mpfr_clear(right_imag
);
5403 // Note that the value is being discarded.
5406 Binary_expression::do_discarding_value()
5408 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5409 this->right_
->discarding_value();
5411 this->warn_about_unused_value();
5417 Binary_expression::do_type()
5419 if (this->classification() == EXPRESSION_ERROR
)
5420 return Type::make_error_type();
5425 case OPERATOR_ANDAND
:
5427 case OPERATOR_NOTEQ
:
5432 return Type::lookup_bool_type();
5435 case OPERATOR_MINUS
:
5442 case OPERATOR_BITCLEAR
:
5444 Type
* left_type
= this->left_
->type();
5445 Type
* right_type
= this->right_
->type();
5446 if (left_type
->is_error_type())
5448 else if (right_type
->is_error_type())
5450 else if (!Type::are_compatible_for_binop(left_type
, right_type
))
5452 this->report_error(_("incompatible types in binary expression"));
5453 return Type::make_error_type();
5455 else if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5457 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5459 else if (!left_type
->is_abstract())
5461 else if (!right_type
->is_abstract())
5463 else if (left_type
->complex_type() != NULL
)
5465 else if (right_type
->complex_type() != NULL
)
5467 else if (left_type
->float_type() != NULL
)
5469 else if (right_type
->float_type() != NULL
)
5475 case OPERATOR_LSHIFT
:
5476 case OPERATOR_RSHIFT
:
5477 return this->left_
->type();
5484 // Set type for a binary expression.
5487 Binary_expression::do_determine_type(const Type_context
* context
)
5489 Type
* tleft
= this->left_
->type();
5490 Type
* tright
= this->right_
->type();
5492 // Both sides should have the same type, except for the shift
5493 // operations. For a comparison, we should ignore the incoming
5496 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5497 || this->op_
== OPERATOR_RSHIFT
);
5499 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5500 || this->op_
== OPERATOR_NOTEQ
5501 || this->op_
== OPERATOR_LT
5502 || this->op_
== OPERATOR_LE
5503 || this->op_
== OPERATOR_GT
5504 || this->op_
== OPERATOR_GE
);
5506 Type_context
subcontext(*context
);
5510 // In a comparison, the context does not determine the types of
5512 subcontext
.type
= NULL
;
5515 // Set the context for the left hand operand.
5518 // The right hand operand plays no role in determining the type
5519 // of the left hand operand. A shift of an abstract integer in
5520 // a string context gets special treatment, which may be a
5522 if (subcontext
.type
!= NULL
5523 && subcontext
.type
->is_string_type()
5524 && tleft
->is_abstract())
5525 error_at(this->location(), "shift of non-integer operand");
5527 else if (!tleft
->is_abstract())
5528 subcontext
.type
= tleft
;
5529 else if (!tright
->is_abstract())
5530 subcontext
.type
= tright
;
5531 else if (subcontext
.type
== NULL
)
5533 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5534 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5535 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5537 // Both sides have an abstract integer, abstract float, or
5538 // abstract complex type. Just let CONTEXT determine
5539 // whether they may remain abstract or not.
5541 else if (tleft
->complex_type() != NULL
)
5542 subcontext
.type
= tleft
;
5543 else if (tright
->complex_type() != NULL
)
5544 subcontext
.type
= tright
;
5545 else if (tleft
->float_type() != NULL
)
5546 subcontext
.type
= tleft
;
5547 else if (tright
->float_type() != NULL
)
5548 subcontext
.type
= tright
;
5550 subcontext
.type
= tleft
;
5552 if (subcontext
.type
!= NULL
&& !context
->may_be_abstract
)
5553 subcontext
.type
= subcontext
.type
->make_non_abstract_type();
5556 this->left_
->determine_type(&subcontext
);
5558 // The context for the right hand operand is the same as for the
5559 // left hand operand, except for a shift operator.
5562 subcontext
.type
= Type::lookup_integer_type("uint");
5563 subcontext
.may_be_abstract
= false;
5566 this->right_
->determine_type(&subcontext
);
5569 // Report an error if the binary operator OP does not support TYPE.
5570 // Return whether the operation is OK. This should not be used for
5574 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5575 source_location location
)
5580 case OPERATOR_ANDAND
:
5581 if (!type
->is_boolean_type())
5583 error_at(location
, "expected boolean type");
5589 case OPERATOR_NOTEQ
:
5590 if (type
->integer_type() == NULL
5591 && type
->float_type() == NULL
5592 && type
->complex_type() == NULL
5593 && !type
->is_string_type()
5594 && type
->points_to() == NULL
5595 && !type
->is_nil_type()
5596 && !type
->is_boolean_type()
5597 && type
->interface_type() == NULL
5598 && (type
->array_type() == NULL
5599 || type
->array_type()->length() != NULL
)
5600 && type
->map_type() == NULL
5601 && type
->channel_type() == NULL
5602 && type
->function_type() == NULL
)
5605 ("expected integer, floating, complex, string, pointer, "
5606 "boolean, interface, slice, map, channel, "
5607 "or function type"));
5616 if (type
->integer_type() == NULL
5617 && type
->float_type() == NULL
5618 && !type
->is_string_type())
5620 error_at(location
, "expected integer, floating, or string type");
5626 case OPERATOR_PLUSEQ
:
5627 if (type
->integer_type() == NULL
5628 && type
->float_type() == NULL
5629 && type
->complex_type() == NULL
5630 && !type
->is_string_type())
5633 "expected integer, floating, complex, or string type");
5638 case OPERATOR_MINUS
:
5639 case OPERATOR_MINUSEQ
:
5641 case OPERATOR_MULTEQ
:
5643 case OPERATOR_DIVEQ
:
5644 if (type
->integer_type() == NULL
5645 && type
->float_type() == NULL
5646 && type
->complex_type() == NULL
)
5648 error_at(location
, "expected integer, floating, or complex type");
5654 case OPERATOR_MODEQ
:
5658 case OPERATOR_ANDEQ
:
5660 case OPERATOR_XOREQ
:
5661 case OPERATOR_BITCLEAR
:
5662 case OPERATOR_BITCLEAREQ
:
5663 if (type
->integer_type() == NULL
)
5665 error_at(location
, "expected integer type");
5680 Binary_expression::do_check_types(Gogo
*)
5682 if (this->classification() == EXPRESSION_ERROR
)
5685 Type
* left_type
= this->left_
->type();
5686 Type
* right_type
= this->right_
->type();
5687 if (left_type
->is_error_type() || right_type
->is_error_type())
5689 this->set_is_error();
5693 if (this->op_
== OPERATOR_EQEQ
5694 || this->op_
== OPERATOR_NOTEQ
5695 || this->op_
== OPERATOR_LT
5696 || this->op_
== OPERATOR_LE
5697 || this->op_
== OPERATOR_GT
5698 || this->op_
== OPERATOR_GE
)
5700 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5701 && !Type::are_assignable(right_type
, left_type
, NULL
))
5703 this->report_error(_("incompatible types in binary expression"));
5706 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5708 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5711 this->set_is_error();
5715 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5717 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5719 this->report_error(_("incompatible types in binary expression"));
5722 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5725 this->set_is_error();
5731 if (left_type
->integer_type() == NULL
)
5732 this->report_error(_("shift of non-integer operand"));
5734 if (!right_type
->is_abstract()
5735 && (right_type
->integer_type() == NULL
5736 || !right_type
->integer_type()->is_unsigned()))
5737 this->report_error(_("shift count not unsigned integer"));
5743 if (this->right_
->integer_constant_value(true, val
, &type
))
5745 if (mpz_sgn(val
) < 0)
5746 this->report_error(_("negative shift count"));
5753 // Get a tree for a binary expression.
5756 Binary_expression::do_get_tree(Translate_context
* context
)
5758 tree left
= this->left_
->get_tree(context
);
5759 tree right
= this->right_
->get_tree(context
);
5761 if (left
== error_mark_node
|| right
== error_mark_node
)
5762 return error_mark_node
;
5764 enum tree_code code
;
5765 bool use_left_type
= true;
5766 bool is_shift_op
= false;
5770 case OPERATOR_NOTEQ
:
5775 return Expression::comparison_tree(context
, this->op_
,
5776 this->left_
->type(), left
,
5777 this->right_
->type(), right
,
5781 code
= TRUTH_ORIF_EXPR
;
5782 use_left_type
= false;
5784 case OPERATOR_ANDAND
:
5785 code
= TRUTH_ANDIF_EXPR
;
5786 use_left_type
= false;
5791 case OPERATOR_MINUS
:
5795 code
= BIT_IOR_EXPR
;
5798 code
= BIT_XOR_EXPR
;
5805 Type
*t
= this->left_
->type();
5806 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5809 code
= TRUNC_DIV_EXPR
;
5813 code
= TRUNC_MOD_EXPR
;
5815 case OPERATOR_LSHIFT
:
5819 case OPERATOR_RSHIFT
:
5824 code
= BIT_AND_EXPR
;
5826 case OPERATOR_BITCLEAR
:
5827 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5828 code
= BIT_AND_EXPR
;
5834 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5836 if (this->left_
->type()->is_string_type())
5838 gcc_assert(this->op_
== OPERATOR_PLUS
);
5839 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5840 static tree string_plus_decl
;
5841 return Gogo::call_builtin(&string_plus_decl
,
5852 tree compute_type
= excess_precision_type(type
);
5853 if (compute_type
!= NULL_TREE
)
5855 left
= ::convert(compute_type
, left
);
5856 right
= ::convert(compute_type
, right
);
5859 tree eval_saved
= NULL_TREE
;
5863 left
= save_expr(left
);
5865 right
= save_expr(right
);
5866 // Make sure the values are evaluated.
5867 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5868 void_type_node
, left
, right
);
5871 tree ret
= fold_build2_loc(this->location(),
5873 compute_type
!= NULL_TREE
? compute_type
: type
,
5876 if (compute_type
!= NULL_TREE
)
5877 ret
= ::convert(type
, ret
);
5879 // In Go, a shift larger than the size of the type is well-defined.
5880 // This is not true in GENERIC, so we need to insert a conditional.
5883 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5884 gcc_assert(this->left_
->type()->integer_type() != NULL
);
5885 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5887 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5888 build_int_cst_type(TREE_TYPE(right
), bits
));
5890 tree overflow_result
= fold_convert_loc(this->location(),
5893 if (this->op_
== OPERATOR_RSHIFT
5894 && !this->left_
->type()->integer_type()->is_unsigned())
5896 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
5897 boolean_type_node
, left
,
5898 fold_convert_loc(this->location(),
5900 integer_zero_node
));
5901 tree neg_one
= fold_build2_loc(this->location(),
5902 MINUS_EXPR
, TREE_TYPE(left
),
5903 fold_convert_loc(this->location(),
5906 fold_convert_loc(this->location(),
5909 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
5910 TREE_TYPE(left
), neg
, neg_one
,
5914 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
5915 compare
, ret
, overflow_result
);
5917 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5918 TREE_TYPE(ret
), eval_saved
, ret
);
5924 // Export a binary expression.
5927 Binary_expression::do_export(Export
* exp
) const
5929 exp
->write_c_string("(");
5930 this->left_
->export_expression(exp
);
5934 exp
->write_c_string(" || ");
5936 case OPERATOR_ANDAND
:
5937 exp
->write_c_string(" && ");
5940 exp
->write_c_string(" == ");
5942 case OPERATOR_NOTEQ
:
5943 exp
->write_c_string(" != ");
5946 exp
->write_c_string(" < ");
5949 exp
->write_c_string(" <= ");
5952 exp
->write_c_string(" > ");
5955 exp
->write_c_string(" >= ");
5958 exp
->write_c_string(" + ");
5960 case OPERATOR_MINUS
:
5961 exp
->write_c_string(" - ");
5964 exp
->write_c_string(" | ");
5967 exp
->write_c_string(" ^ ");
5970 exp
->write_c_string(" * ");
5973 exp
->write_c_string(" / ");
5976 exp
->write_c_string(" % ");
5978 case OPERATOR_LSHIFT
:
5979 exp
->write_c_string(" << ");
5981 case OPERATOR_RSHIFT
:
5982 exp
->write_c_string(" >> ");
5985 exp
->write_c_string(" & ");
5987 case OPERATOR_BITCLEAR
:
5988 exp
->write_c_string(" &^ ");
5993 this->right_
->export_expression(exp
);
5994 exp
->write_c_string(")");
5997 // Import a binary expression.
6000 Binary_expression::do_import(Import
* imp
)
6002 imp
->require_c_string("(");
6004 Expression
* left
= Expression::import_expression(imp
);
6007 if (imp
->match_c_string(" || "))
6012 else if (imp
->match_c_string(" && "))
6014 op
= OPERATOR_ANDAND
;
6017 else if (imp
->match_c_string(" == "))
6022 else if (imp
->match_c_string(" != "))
6024 op
= OPERATOR_NOTEQ
;
6027 else if (imp
->match_c_string(" < "))
6032 else if (imp
->match_c_string(" <= "))
6037 else if (imp
->match_c_string(" > "))
6042 else if (imp
->match_c_string(" >= "))
6047 else if (imp
->match_c_string(" + "))
6052 else if (imp
->match_c_string(" - "))
6054 op
= OPERATOR_MINUS
;
6057 else if (imp
->match_c_string(" | "))
6062 else if (imp
->match_c_string(" ^ "))
6067 else if (imp
->match_c_string(" * "))
6072 else if (imp
->match_c_string(" / "))
6077 else if (imp
->match_c_string(" % "))
6082 else if (imp
->match_c_string(" << "))
6084 op
= OPERATOR_LSHIFT
;
6087 else if (imp
->match_c_string(" >> "))
6089 op
= OPERATOR_RSHIFT
;
6092 else if (imp
->match_c_string(" & "))
6097 else if (imp
->match_c_string(" &^ "))
6099 op
= OPERATOR_BITCLEAR
;
6104 error_at(imp
->location(), "unrecognized binary operator");
6105 return Expression::make_error(imp
->location());
6108 Expression
* right
= Expression::import_expression(imp
);
6110 imp
->require_c_string(")");
6112 return Expression::make_binary(op
, left
, right
, imp
->location());
6115 // Make a binary expression.
6118 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6119 source_location location
)
6121 return new Binary_expression(op
, left
, right
, location
);
6124 // Implement a comparison.
6127 Expression::comparison_tree(Translate_context
* context
, Operator op
,
6128 Type
* left_type
, tree left_tree
,
6129 Type
* right_type
, tree right_tree
,
6130 source_location location
)
6132 enum tree_code code
;
6138 case OPERATOR_NOTEQ
:
6157 if (left_type
->is_string_type() && right_type
->is_string_type())
6159 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
6160 static tree string_compare_decl
;
6161 left_tree
= Gogo::call_builtin(&string_compare_decl
,
6170 right_tree
= build_int_cst_type(integer_type_node
, 0);
6172 else if ((left_type
->interface_type() != NULL
6173 && right_type
->interface_type() == NULL
6174 && !right_type
->is_nil_type())
6175 || (left_type
->interface_type() == NULL
6176 && !left_type
->is_nil_type()
6177 && right_type
->interface_type() != NULL
))
6179 // Comparing an interface value to a non-interface value.
6180 if (left_type
->interface_type() == NULL
)
6182 std::swap(left_type
, right_type
);
6183 std::swap(left_tree
, right_tree
);
6186 // The right operand is not an interface. We need to take its
6187 // address if it is not a pointer.
6190 if (right_type
->points_to() != NULL
)
6192 make_tmp
= NULL_TREE
;
6195 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
6197 make_tmp
= NULL_TREE
;
6198 arg
= build_fold_addr_expr_loc(location
, right_tree
);
6199 if (DECL_P(right_tree
))
6200 TREE_ADDRESSABLE(right_tree
) = 1;
6204 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
6205 get_name(right_tree
));
6206 DECL_IGNORED_P(tmp
) = 0;
6207 DECL_INITIAL(tmp
) = right_tree
;
6208 TREE_ADDRESSABLE(tmp
) = 1;
6209 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6210 SET_EXPR_LOCATION(make_tmp
, location
);
6211 arg
= build_fold_addr_expr_loc(location
, tmp
);
6213 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
6215 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
6217 if (left_type
->interface_type()->is_empty())
6219 static tree empty_interface_value_compare_decl
;
6220 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6222 "__go_empty_interface_value_compare",
6225 TREE_TYPE(left_tree
),
6227 TREE_TYPE(descriptor
),
6231 if (left_tree
== error_mark_node
)
6232 return error_mark_node
;
6233 // This can panic if the type is not comparable.
6234 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6238 static tree interface_value_compare_decl
;
6239 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6241 "__go_interface_value_compare",
6244 TREE_TYPE(left_tree
),
6246 TREE_TYPE(descriptor
),
6250 if (left_tree
== error_mark_node
)
6251 return error_mark_node
;
6252 // This can panic if the type is not comparable.
6253 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6255 right_tree
= build_int_cst_type(integer_type_node
, 0);
6257 if (make_tmp
!= NULL_TREE
)
6258 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6261 else if (left_type
->interface_type() != NULL
6262 && right_type
->interface_type() != NULL
)
6264 if (left_type
->interface_type()->is_empty())
6266 gcc_assert(right_type
->interface_type()->is_empty());
6267 static tree empty_interface_compare_decl
;
6268 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6270 "__go_empty_interface_compare",
6273 TREE_TYPE(left_tree
),
6275 TREE_TYPE(right_tree
),
6277 if (left_tree
== error_mark_node
)
6278 return error_mark_node
;
6279 // This can panic if the type is uncomparable.
6280 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6284 gcc_assert(!right_type
->interface_type()->is_empty());
6285 static tree interface_compare_decl
;
6286 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6288 "__go_interface_compare",
6291 TREE_TYPE(left_tree
),
6293 TREE_TYPE(right_tree
),
6295 if (left_tree
== error_mark_node
)
6296 return error_mark_node
;
6297 // This can panic if the type is uncomparable.
6298 TREE_NOTHROW(interface_compare_decl
) = 0;
6300 right_tree
= build_int_cst_type(integer_type_node
, 0);
6303 if (left_type
->is_nil_type()
6304 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6306 std::swap(left_type
, right_type
);
6307 std::swap(left_tree
, right_tree
);
6310 if (right_type
->is_nil_type())
6312 if (left_type
->array_type() != NULL
6313 && left_type
->array_type()->length() == NULL
)
6315 Array_type
* at
= left_type
->array_type();
6316 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6317 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6319 else if (left_type
->interface_type() != NULL
)
6321 // An interface is nil if the first field is nil.
6322 tree left_type_tree
= TREE_TYPE(left_tree
);
6323 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6324 tree field
= TYPE_FIELDS(left_type_tree
);
6325 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6327 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6331 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6332 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6336 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6337 return error_mark_node
;
6339 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6340 if (CAN_HAVE_LOCATION_P(ret
))
6341 SET_EXPR_LOCATION(ret
, location
);
6345 // Class Bound_method_expression.
6350 Bound_method_expression::do_traverse(Traverse
* traverse
)
6352 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6353 return TRAVERSE_EXIT
;
6354 return Expression::traverse(&this->method_
, traverse
);
6357 // Return the type of a bound method expression. The type of this
6358 // object is really the type of the method with no receiver. We
6359 // should be able to get away with just returning the type of the
6363 Bound_method_expression::do_type()
6365 return this->method_
->type();
6368 // Determine the types of a method expression.
6371 Bound_method_expression::do_determine_type(const Type_context
*)
6373 this->method_
->determine_type_no_context();
6374 Type
* mtype
= this->method_
->type();
6375 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6376 if (fntype
== NULL
|| !fntype
->is_method())
6377 this->expr_
->determine_type_no_context();
6380 Type_context
subcontext(fntype
->receiver()->type(), false);
6381 this->expr_
->determine_type(&subcontext
);
6385 // Check the types of a method expression.
6388 Bound_method_expression::do_check_types(Gogo
*)
6390 Type
* type
= this->method_
->type()->deref();
6392 || type
->function_type() == NULL
6393 || !type
->function_type()->is_method())
6394 this->report_error(_("object is not a method"));
6397 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6398 Type
* etype
= (this->expr_type_
!= NULL
6400 : this->expr_
->type());
6401 etype
= etype
->deref();
6402 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6403 this->report_error(_("method type does not match object type"));
6407 // Get the tree for a method expression. There is no standard tree
6408 // representation for this. The only places it may currently be used
6409 // are in a Call_expression or a Go_statement, which will take it
6410 // apart directly. So this has nothing to do at present.
6413 Bound_method_expression::do_get_tree(Translate_context
*)
6415 error_at(this->location(), "reference to method other than calling it");
6416 return error_mark_node
;
6419 // Make a method expression.
6421 Bound_method_expression
*
6422 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6423 source_location location
)
6425 return new Bound_method_expression(expr
, method
, location
);
6428 // Class Builtin_call_expression. This is used for a call to a
6429 // builtin function.
6431 class Builtin_call_expression
: public Call_expression
6434 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6435 bool is_varargs
, source_location location
);
6438 // This overrides Call_expression::do_lower.
6440 do_lower(Gogo
*, Named_object
*, int);
6443 do_is_constant() const;
6446 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6449 do_float_constant_value(mpfr_t
, Type
**) const;
6452 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6458 do_determine_type(const Type_context
*);
6461 do_check_types(Gogo
*);
6466 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6467 this->args()->copy(),
6473 do_get_tree(Translate_context
*);
6476 do_export(Export
*) const;
6479 do_is_recover_call() const;
6482 do_set_recover_arg(Expression
*);
6485 // The builtin functions.
6486 enum Builtin_function_code
6490 // Predeclared builtin functions.
6507 // Builtin functions from the unsafe package.
6520 real_imag_type(Type
*);
6523 complex_type(Type
*);
6525 // A pointer back to the general IR structure. This avoids a global
6526 // variable, or passing it around everywhere.
6528 // The builtin function being called.
6529 Builtin_function_code code_
;
6530 // Used to stop endless loops when the length of an array uses len
6531 // or cap of the array itself.
6535 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6537 Expression_list
* args
,
6539 source_location location
)
6540 : Call_expression(fn
, args
, is_varargs
, location
),
6541 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false)
6543 Func_expression
* fnexp
= this->fn()->func_expression();
6544 gcc_assert(fnexp
!= NULL
);
6545 const std::string
& name(fnexp
->named_object()->name());
6546 if (name
== "append")
6547 this->code_
= BUILTIN_APPEND
;
6548 else if (name
== "cap")
6549 this->code_
= BUILTIN_CAP
;
6550 else if (name
== "close")
6551 this->code_
= BUILTIN_CLOSE
;
6552 else if (name
== "closed")
6553 this->code_
= BUILTIN_CLOSED
;
6554 else if (name
== "complex")
6555 this->code_
= BUILTIN_COMPLEX
;
6556 else if (name
== "copy")
6557 this->code_
= BUILTIN_COPY
;
6558 else if (name
== "imag")
6559 this->code_
= BUILTIN_IMAG
;
6560 else if (name
== "len")
6561 this->code_
= BUILTIN_LEN
;
6562 else if (name
== "make")
6563 this->code_
= BUILTIN_MAKE
;
6564 else if (name
== "new")
6565 this->code_
= BUILTIN_NEW
;
6566 else if (name
== "panic")
6567 this->code_
= BUILTIN_PANIC
;
6568 else if (name
== "print")
6569 this->code_
= BUILTIN_PRINT
;
6570 else if (name
== "println")
6571 this->code_
= BUILTIN_PRINTLN
;
6572 else if (name
== "real")
6573 this->code_
= BUILTIN_REAL
;
6574 else if (name
== "recover")
6575 this->code_
= BUILTIN_RECOVER
;
6576 else if (name
== "Alignof")
6577 this->code_
= BUILTIN_ALIGNOF
;
6578 else if (name
== "Offsetof")
6579 this->code_
= BUILTIN_OFFSETOF
;
6580 else if (name
== "Sizeof")
6581 this->code_
= BUILTIN_SIZEOF
;
6586 // Return whether this is a call to recover. This is a virtual
6587 // function called from the parent class.
6590 Builtin_call_expression::do_is_recover_call() const
6592 if (this->classification() == EXPRESSION_ERROR
)
6594 return this->code_
== BUILTIN_RECOVER
;
6597 // Set the argument for a call to recover.
6600 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6602 const Expression_list
* args
= this->args();
6603 gcc_assert(args
== NULL
|| args
->empty());
6604 Expression_list
* new_args
= new Expression_list();
6605 new_args
->push_back(arg
);
6606 this->set_args(new_args
);
6609 // A traversal class which looks for a call expression.
6611 class Find_call_expression
: public Traverse
6614 Find_call_expression()
6615 : Traverse(traverse_expressions
),
6620 expression(Expression
**);
6624 { return this->found_
; }
6631 Find_call_expression::expression(Expression
** pexpr
)
6633 if ((*pexpr
)->call_expression() != NULL
)
6635 this->found_
= true;
6636 return TRAVERSE_EXIT
;
6638 return TRAVERSE_CONTINUE
;
6641 // Lower a builtin call expression. This turns new and make into
6642 // specific expressions. We also convert to a constant if we can.
6645 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6647 if (this->code_
== BUILTIN_NEW
)
6649 const Expression_list
* args
= this->args();
6650 if (args
== NULL
|| args
->size() < 1)
6651 this->report_error(_("not enough arguments"));
6652 else if (args
->size() > 1)
6653 this->report_error(_("too many arguments"));
6656 Expression
* arg
= args
->front();
6657 if (!arg
->is_type_expression())
6659 error_at(arg
->location(), "expected type");
6660 this->set_is_error();
6663 return Expression::make_allocation(arg
->type(), this->location());
6666 else if (this->code_
== BUILTIN_MAKE
)
6668 const Expression_list
* args
= this->args();
6669 if (args
== NULL
|| args
->size() < 1)
6670 this->report_error(_("not enough arguments"));
6673 Expression
* arg
= args
->front();
6674 if (!arg
->is_type_expression())
6676 error_at(arg
->location(), "expected type");
6677 this->set_is_error();
6681 Expression_list
* newargs
;
6682 if (args
->size() == 1)
6686 newargs
= new Expression_list();
6687 Expression_list::const_iterator p
= args
->begin();
6689 for (; p
!= args
->end(); ++p
)
6690 newargs
->push_back(*p
);
6692 return Expression::make_make(arg
->type(), newargs
,
6697 else if (this->is_constant())
6699 // We can only lower len and cap if there are no function calls
6700 // in the arguments. Otherwise we have to make the call.
6701 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6703 Expression
* arg
= this->one_arg();
6704 if (!arg
->is_constant())
6706 Find_call_expression find_call
;
6707 Expression::traverse(&arg
, &find_call
);
6708 if (find_call
.found())
6716 if (this->integer_constant_value(true, ival
, &type
))
6718 Expression
* ret
= Expression::make_integer(&ival
, type
,
6727 if (this->float_constant_value(rval
, &type
))
6729 Expression
* ret
= Expression::make_float(&rval
, type
,
6737 if (this->complex_constant_value(rval
, imag
, &type
))
6739 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6748 else if (this->code_
== BUILTIN_RECOVER
)
6750 if (function
!= NULL
)
6751 function
->func_value()->set_calls_recover();
6754 // Calling recover outside of a function always returns the
6755 // nil empty interface.
6756 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6757 return Expression::make_cast(eface
,
6758 Expression::make_nil(this->location()),
6762 else if (this->code_
== BUILTIN_APPEND
)
6764 // Lower the varargs.
6765 const Expression_list
* args
= this->args();
6766 if (args
== NULL
|| args
->empty())
6768 Type
* slice_type
= args
->front()->type();
6769 if (!slice_type
->is_open_array_type())
6771 error_at(args
->front()->location(), "argument 1 must be a slice");
6772 this->set_is_error();
6775 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6781 // Return the type of the real or imag functions, given the type of
6782 // the argument. We need to map complex to float, complex64 to
6783 // float32, and complex128 to float64, so it has to be done by name.
6784 // This returns NULL if it can't figure out the type.
6787 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6789 if (arg_type
== NULL
|| arg_type
->is_abstract())
6791 Named_type
* nt
= arg_type
->named_type();
6794 while (nt
->real_type()->named_type() != NULL
)
6795 nt
= nt
->real_type()->named_type();
6796 if (nt
->name() == "complex64")
6797 return Type::lookup_float_type("float32");
6798 else if (nt
->name() == "complex128")
6799 return Type::lookup_float_type("float64");
6804 // Return the type of the complex function, given the type of one of the
6805 // argments. Like real_imag_type, we have to map by name.
6808 Builtin_call_expression::complex_type(Type
* arg_type
)
6810 if (arg_type
== NULL
|| arg_type
->is_abstract())
6812 Named_type
* nt
= arg_type
->named_type();
6815 while (nt
->real_type()->named_type() != NULL
)
6816 nt
= nt
->real_type()->named_type();
6817 if (nt
->name() == "float32")
6818 return Type::lookup_complex_type("complex64");
6819 else if (nt
->name() == "float64")
6820 return Type::lookup_complex_type("complex128");
6825 // Return a single argument, or NULL if there isn't one.
6828 Builtin_call_expression::one_arg() const
6830 const Expression_list
* args
= this->args();
6831 if (args
->size() != 1)
6833 return args
->front();
6836 // Return whether this is constant: len of a string, or len or cap of
6837 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6840 Builtin_call_expression::do_is_constant() const
6842 switch (this->code_
)
6850 Expression
* arg
= this->one_arg();
6853 Type
* arg_type
= arg
->type();
6855 if (arg_type
->points_to() != NULL
6856 && arg_type
->points_to()->array_type() != NULL
6857 && !arg_type
->points_to()->is_open_array_type())
6858 arg_type
= arg_type
->points_to();
6860 if (arg_type
->array_type() != NULL
6861 && arg_type
->array_type()->length() != NULL
)
6864 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6867 bool ret
= arg
->is_constant();
6868 this->seen_
= false;
6874 case BUILTIN_SIZEOF
:
6875 case BUILTIN_ALIGNOF
:
6876 return this->one_arg() != NULL
;
6878 case BUILTIN_OFFSETOF
:
6880 Expression
* arg
= this->one_arg();
6883 return arg
->field_reference_expression() != NULL
;
6886 case BUILTIN_COMPLEX
:
6888 const Expression_list
* args
= this->args();
6889 if (args
!= NULL
&& args
->size() == 2)
6890 return args
->front()->is_constant() && args
->back()->is_constant();
6897 Expression
* arg
= this->one_arg();
6898 return arg
!= NULL
&& arg
->is_constant();
6908 // Return an integer constant value if possible.
6911 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
6915 if (this->code_
== BUILTIN_LEN
6916 || this->code_
== BUILTIN_CAP
)
6918 Expression
* arg
= this->one_arg();
6921 Type
* arg_type
= arg
->type();
6923 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6926 if (arg
->string_constant_value(&sval
))
6928 mpz_set_ui(val
, sval
.length());
6929 *ptype
= Type::lookup_integer_type("int");
6934 if (arg_type
->points_to() != NULL
6935 && arg_type
->points_to()->array_type() != NULL
6936 && !arg_type
->points_to()->is_open_array_type())
6937 arg_type
= arg_type
->points_to();
6939 if (arg_type
->array_type() != NULL
6940 && arg_type
->array_type()->length() != NULL
)
6944 Expression
* e
= arg_type
->array_type()->length();
6946 bool r
= e
->integer_constant_value(iota_is_constant
, val
, ptype
);
6947 this->seen_
= false;
6950 *ptype
= Type::lookup_integer_type("int");
6955 else if (this->code_
== BUILTIN_SIZEOF
6956 || this->code_
== BUILTIN_ALIGNOF
)
6958 Expression
* arg
= this->one_arg();
6961 Type
* arg_type
= arg
->type();
6962 if (arg_type
->is_error_type() || arg_type
->is_undefined())
6964 if (arg_type
->is_abstract())
6966 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
6967 if (arg_type_tree
== error_mark_node
)
6969 unsigned long val_long
;
6970 if (this->code_
== BUILTIN_SIZEOF
)
6972 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
6973 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
6974 if (TREE_INT_CST_HIGH(type_size
) != 0)
6976 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
6977 val_long
= static_cast<unsigned long>(val_wide
);
6978 if (val_long
!= val_wide
)
6981 else if (this->code_
== BUILTIN_ALIGNOF
)
6983 if (arg
->field_reference_expression() == NULL
)
6984 val_long
= go_type_alignment(arg_type_tree
);
6987 // Calling unsafe.Alignof(s.f) returns the alignment of
6988 // the type of f when it is used as a field in a struct.
6989 val_long
= go_field_alignment(arg_type_tree
);
6994 mpz_set_ui(val
, val_long
);
6998 else if (this->code_
== BUILTIN_OFFSETOF
)
7000 Expression
* arg
= this->one_arg();
7003 Field_reference_expression
* farg
= arg
->field_reference_expression();
7006 Expression
* struct_expr
= farg
->expr();
7007 Type
* st
= struct_expr
->type();
7008 if (st
->struct_type() == NULL
)
7010 tree struct_tree
= st
->get_tree(this->gogo_
);
7011 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
7012 tree field
= TYPE_FIELDS(struct_tree
);
7013 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
7015 field
= DECL_CHAIN(field
);
7016 gcc_assert(field
!= NULL_TREE
);
7018 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
7019 if (offset_wide
< 0)
7021 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
7022 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
7024 mpz_set_ui(val
, offset_long
);
7030 // Return a floating point constant value if possible.
7033 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
7036 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
7038 Expression
* arg
= this->one_arg();
7049 if (arg
->complex_constant_value(real
, imag
, &type
))
7051 if (this->code_
== BUILTIN_REAL
)
7052 mpfr_set(val
, real
, GMP_RNDN
);
7054 mpfr_set(val
, imag
, GMP_RNDN
);
7055 *ptype
= Builtin_call_expression::real_imag_type(type
);
7067 // Return a complex constant value if possible.
7070 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
7073 if (this->code_
== BUILTIN_COMPLEX
)
7075 const Expression_list
* args
= this->args();
7076 if (args
== NULL
|| args
->size() != 2)
7082 if (!args
->front()->float_constant_value(r
, &rtype
))
7093 if (args
->back()->float_constant_value(i
, &itype
)
7094 && Type::are_identical(rtype
, itype
, false, NULL
))
7096 mpfr_set(real
, r
, GMP_RNDN
);
7097 mpfr_set(imag
, i
, GMP_RNDN
);
7098 *ptype
= Builtin_call_expression::complex_type(rtype
);
7114 Builtin_call_expression::do_type()
7116 switch (this->code_
)
7118 case BUILTIN_INVALID
:
7125 const Expression_list
* args
= this->args();
7126 if (args
== NULL
|| args
->empty())
7127 return Type::make_error_type();
7128 return Type::make_pointer_type(args
->front()->type());
7134 case BUILTIN_ALIGNOF
:
7135 case BUILTIN_OFFSETOF
:
7136 case BUILTIN_SIZEOF
:
7137 return Type::lookup_integer_type("int");
7142 case BUILTIN_PRINTLN
:
7143 return Type::make_void_type();
7145 case BUILTIN_CLOSED
:
7146 return Type::lookup_bool_type();
7148 case BUILTIN_RECOVER
:
7149 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7151 case BUILTIN_APPEND
:
7153 const Expression_list
* args
= this->args();
7154 if (args
== NULL
|| args
->empty())
7155 return Type::make_error_type();
7156 return args
->front()->type();
7162 Expression
* arg
= this->one_arg();
7164 return Type::make_error_type();
7165 Type
* t
= arg
->type();
7166 if (t
->is_abstract())
7167 t
= t
->make_non_abstract_type();
7168 t
= Builtin_call_expression::real_imag_type(t
);
7170 t
= Type::make_error_type();
7174 case BUILTIN_COMPLEX
:
7176 const Expression_list
* args
= this->args();
7177 if (args
== NULL
|| args
->size() != 2)
7178 return Type::make_error_type();
7179 Type
* t
= args
->front()->type();
7180 if (t
->is_abstract())
7182 t
= args
->back()->type();
7183 if (t
->is_abstract())
7184 t
= t
->make_non_abstract_type();
7186 t
= Builtin_call_expression::complex_type(t
);
7188 t
= Type::make_error_type();
7194 // Determine the type.
7197 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7199 this->fn()->determine_type_no_context();
7201 const Expression_list
* args
= this->args();
7204 Type
* arg_type
= NULL
;
7205 switch (this->code_
)
7208 case BUILTIN_PRINTLN
:
7209 // Do not force a large integer constant to "int".
7215 arg_type
= Builtin_call_expression::complex_type(context
->type
);
7219 case BUILTIN_COMPLEX
:
7221 // For the complex function the type of one operand can
7222 // determine the type of the other, as in a binary expression.
7223 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7224 if (args
!= NULL
&& args
->size() == 2)
7226 Type
* t1
= args
->front()->type();
7227 Type
* t2
= args
->front()->type();
7228 if (!t1
->is_abstract())
7230 else if (!t2
->is_abstract())
7244 for (Expression_list::const_iterator pa
= args
->begin();
7248 Type_context subcontext
;
7249 subcontext
.type
= arg_type
;
7253 // We want to print large constants, we so can't just
7254 // use the appropriate nonabstract type. Use uint64 for
7255 // an integer if we know it is nonnegative, otherwise
7256 // use int64 for a integer, otherwise use float64 for a
7257 // float or complex128 for a complex.
7258 Type
* want_type
= NULL
;
7259 Type
* atype
= (*pa
)->type();
7260 if (atype
->is_abstract())
7262 if (atype
->integer_type() != NULL
)
7267 if (this->integer_constant_value(true, val
, &dummy
)
7268 && mpz_sgn(val
) >= 0)
7269 want_type
= Type::lookup_integer_type("uint64");
7271 want_type
= Type::lookup_integer_type("int64");
7274 else if (atype
->float_type() != NULL
)
7275 want_type
= Type::lookup_float_type("float64");
7276 else if (atype
->complex_type() != NULL
)
7277 want_type
= Type::lookup_complex_type("complex128");
7278 else if (atype
->is_abstract_string_type())
7279 want_type
= Type::lookup_string_type();
7280 else if (atype
->is_abstract_boolean_type())
7281 want_type
= Type::lookup_bool_type();
7284 subcontext
.type
= want_type
;
7288 (*pa
)->determine_type(&subcontext
);
7293 // If there is exactly one argument, return true. Otherwise give an
7294 // error message and return false.
7297 Builtin_call_expression::check_one_arg()
7299 const Expression_list
* args
= this->args();
7300 if (args
== NULL
|| args
->size() < 1)
7302 this->report_error(_("not enough arguments"));
7305 else if (args
->size() > 1)
7307 this->report_error(_("too many arguments"));
7310 if (args
->front()->is_error_expression()
7311 || args
->front()->type()->is_error_type()
7312 || args
->front()->type()->is_undefined())
7314 this->set_is_error();
7320 // Check argument types for a builtin function.
7323 Builtin_call_expression::do_check_types(Gogo
*)
7325 switch (this->code_
)
7327 case BUILTIN_INVALID
:
7335 // The single argument may be either a string or an array or a
7336 // map or a channel, or a pointer to a closed array.
7337 if (this->check_one_arg())
7339 Type
* arg_type
= this->one_arg()->type();
7340 if (arg_type
->points_to() != NULL
7341 && arg_type
->points_to()->array_type() != NULL
7342 && !arg_type
->points_to()->is_open_array_type())
7343 arg_type
= arg_type
->points_to();
7344 if (this->code_
== BUILTIN_CAP
)
7346 if (!arg_type
->is_error_type()
7347 && arg_type
->array_type() == NULL
7348 && arg_type
->channel_type() == NULL
)
7349 this->report_error(_("argument must be array or slice "
7354 if (!arg_type
->is_error_type()
7355 && !arg_type
->is_string_type()
7356 && arg_type
->array_type() == NULL
7357 && arg_type
->map_type() == NULL
7358 && arg_type
->channel_type() == NULL
)
7359 this->report_error(_("argument must be string or "
7360 "array or slice or map or channel"));
7367 case BUILTIN_PRINTLN
:
7369 const Expression_list
* args
= this->args();
7372 if (this->code_
== BUILTIN_PRINT
)
7373 warning_at(this->location(), 0,
7374 "no arguments for builtin function %<%s%>",
7375 (this->code_
== BUILTIN_PRINT
7381 for (Expression_list::const_iterator p
= args
->begin();
7385 Type
* type
= (*p
)->type();
7386 if (type
->is_error_type()
7387 || type
->is_string_type()
7388 || type
->integer_type() != NULL
7389 || type
->float_type() != NULL
7390 || type
->complex_type() != NULL
7391 || type
->is_boolean_type()
7392 || type
->points_to() != NULL
7393 || type
->interface_type() != NULL
7394 || type
->channel_type() != NULL
7395 || type
->map_type() != NULL
7396 || type
->function_type() != NULL
7397 || type
->is_open_array_type())
7400 this->report_error(_("unsupported argument type to "
7401 "builtin function"));
7408 case BUILTIN_CLOSED
:
7409 if (this->check_one_arg())
7411 if (this->one_arg()->type()->channel_type() == NULL
)
7412 this->report_error(_("argument must be channel"));
7417 case BUILTIN_SIZEOF
:
7418 case BUILTIN_ALIGNOF
:
7419 this->check_one_arg();
7422 case BUILTIN_RECOVER
:
7423 if (this->args() != NULL
&& !this->args()->empty())
7424 this->report_error(_("too many arguments"));
7427 case BUILTIN_OFFSETOF
:
7428 if (this->check_one_arg())
7430 Expression
* arg
= this->one_arg();
7431 if (arg
->field_reference_expression() == NULL
)
7432 this->report_error(_("argument must be a field reference"));
7438 const Expression_list
* args
= this->args();
7439 if (args
== NULL
|| args
->size() < 2)
7441 this->report_error(_("not enough arguments"));
7444 else if (args
->size() > 2)
7446 this->report_error(_("too many arguments"));
7449 Type
* arg1_type
= args
->front()->type();
7450 Type
* arg2_type
= args
->back()->type();
7451 if (arg1_type
->is_error_type() || arg2_type
->is_error_type())
7455 if (arg1_type
->is_open_array_type())
7456 e1
= arg1_type
->array_type()->element_type();
7459 this->report_error(_("left argument must be a slice"));
7464 if (arg2_type
->is_open_array_type())
7465 e2
= arg2_type
->array_type()->element_type();
7466 else if (arg2_type
->is_string_type())
7467 e2
= Type::lookup_integer_type("uint8");
7470 this->report_error(_("right argument must be a slice or a string"));
7474 if (!Type::are_identical(e1
, e2
, true, NULL
))
7475 this->report_error(_("element types must be the same"));
7479 case BUILTIN_APPEND
:
7481 const Expression_list
* args
= this->args();
7482 if (args
== NULL
|| args
->size() < 2)
7484 this->report_error(_("not enough arguments"));
7487 if (args
->size() > 2)
7489 this->report_error(_("too many arguments"));
7493 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7497 this->report_error(_("arguments 1 and 2 have different types"));
7500 error_at(this->location(),
7501 "arguments 1 and 2 have different types (%s)",
7503 this->set_is_error();
7511 if (this->check_one_arg())
7513 if (this->one_arg()->type()->complex_type() == NULL
)
7514 this->report_error(_("argument must have complex type"));
7518 case BUILTIN_COMPLEX
:
7520 const Expression_list
* args
= this->args();
7521 if (args
== NULL
|| args
->size() < 2)
7522 this->report_error(_("not enough arguments"));
7523 else if (args
->size() > 2)
7524 this->report_error(_("too many arguments"));
7525 else if (args
->front()->is_error_expression()
7526 || args
->front()->type()->is_error_type()
7527 || args
->back()->is_error_expression()
7528 || args
->back()->type()->is_error_type())
7529 this->set_is_error();
7530 else if (!Type::are_identical(args
->front()->type(),
7531 args
->back()->type(), true, NULL
))
7532 this->report_error(_("complex arguments must have identical types"));
7533 else if (args
->front()->type()->float_type() == NULL
)
7534 this->report_error(_("complex arguments must have "
7535 "floating-point type"));
7544 // Return the tree for a builtin function.
7547 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7549 Gogo
* gogo
= context
->gogo();
7550 source_location location
= this->location();
7551 switch (this->code_
)
7553 case BUILTIN_INVALID
:
7561 const Expression_list
* args
= this->args();
7562 gcc_assert(args
!= NULL
&& args
->size() == 1);
7563 Expression
* arg
= *args
->begin();
7564 Type
* arg_type
= arg
->type();
7568 gcc_assert(saw_errors());
7569 return error_mark_node
;
7573 tree arg_tree
= arg
->get_tree(context
);
7575 this->seen_
= false;
7577 if (arg_tree
== error_mark_node
)
7578 return error_mark_node
;
7580 if (arg_type
->points_to() != NULL
)
7582 arg_type
= arg_type
->points_to();
7583 gcc_assert(arg_type
->array_type() != NULL
7584 && !arg_type
->is_open_array_type());
7585 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7586 arg_tree
= build_fold_indirect_ref(arg_tree
);
7590 if (this->code_
== BUILTIN_LEN
)
7592 if (arg_type
->is_string_type())
7593 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7594 else if (arg_type
->array_type() != NULL
)
7598 gcc_assert(saw_errors());
7599 return error_mark_node
;
7602 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7603 this->seen_
= false;
7605 else if (arg_type
->map_type() != NULL
)
7607 static tree map_len_fndecl
;
7608 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7613 arg_type
->get_tree(gogo
),
7616 else if (arg_type
->channel_type() != NULL
)
7618 static tree chan_len_fndecl
;
7619 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7624 arg_type
->get_tree(gogo
),
7632 if (arg_type
->array_type() != NULL
)
7636 gcc_assert(saw_errors());
7637 return error_mark_node
;
7640 val_tree
= arg_type
->array_type()->capacity_tree(gogo
,
7642 this->seen_
= false;
7644 else if (arg_type
->channel_type() != NULL
)
7646 static tree chan_cap_fndecl
;
7647 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7652 arg_type
->get_tree(gogo
),
7659 if (val_tree
== error_mark_node
)
7660 return error_mark_node
;
7662 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7663 if (type_tree
== TREE_TYPE(val_tree
))
7666 return fold(convert_to_integer(type_tree
, val_tree
));
7670 case BUILTIN_PRINTLN
:
7672 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7673 tree stmt_list
= NULL_TREE
;
7675 const Expression_list
* call_args
= this->args();
7676 if (call_args
!= NULL
)
7678 for (Expression_list::const_iterator p
= call_args
->begin();
7679 p
!= call_args
->end();
7682 if (is_ln
&& p
!= call_args
->begin())
7684 static tree print_space_fndecl
;
7685 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7690 if (call
== error_mark_node
)
7691 return error_mark_node
;
7692 append_to_statement_list(call
, &stmt_list
);
7695 Type
* type
= (*p
)->type();
7697 tree arg
= (*p
)->get_tree(context
);
7698 if (arg
== error_mark_node
)
7699 return error_mark_node
;
7703 if (type
->is_string_type())
7705 static tree print_string_fndecl
;
7706 pfndecl
= &print_string_fndecl
;
7707 fnname
= "__go_print_string";
7709 else if (type
->integer_type() != NULL
7710 && type
->integer_type()->is_unsigned())
7712 static tree print_uint64_fndecl
;
7713 pfndecl
= &print_uint64_fndecl
;
7714 fnname
= "__go_print_uint64";
7715 Type
* itype
= Type::lookup_integer_type("uint64");
7716 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7719 else if (type
->integer_type() != NULL
)
7721 static tree print_int64_fndecl
;
7722 pfndecl
= &print_int64_fndecl
;
7723 fnname
= "__go_print_int64";
7724 Type
* itype
= Type::lookup_integer_type("int64");
7725 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7728 else if (type
->float_type() != NULL
)
7730 static tree print_double_fndecl
;
7731 pfndecl
= &print_double_fndecl
;
7732 fnname
= "__go_print_double";
7733 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7735 else if (type
->complex_type() != NULL
)
7737 static tree print_complex_fndecl
;
7738 pfndecl
= &print_complex_fndecl
;
7739 fnname
= "__go_print_complex";
7740 arg
= fold_convert_loc(location
, complex_double_type_node
,
7743 else if (type
->is_boolean_type())
7745 static tree print_bool_fndecl
;
7746 pfndecl
= &print_bool_fndecl
;
7747 fnname
= "__go_print_bool";
7749 else if (type
->points_to() != NULL
7750 || type
->channel_type() != NULL
7751 || type
->map_type() != NULL
7752 || type
->function_type() != NULL
)
7754 static tree print_pointer_fndecl
;
7755 pfndecl
= &print_pointer_fndecl
;
7756 fnname
= "__go_print_pointer";
7757 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7759 else if (type
->interface_type() != NULL
)
7761 if (type
->interface_type()->is_empty())
7763 static tree print_empty_interface_fndecl
;
7764 pfndecl
= &print_empty_interface_fndecl
;
7765 fnname
= "__go_print_empty_interface";
7769 static tree print_interface_fndecl
;
7770 pfndecl
= &print_interface_fndecl
;
7771 fnname
= "__go_print_interface";
7774 else if (type
->is_open_array_type())
7776 static tree print_slice_fndecl
;
7777 pfndecl
= &print_slice_fndecl
;
7778 fnname
= "__go_print_slice";
7783 tree call
= Gogo::call_builtin(pfndecl
,
7790 if (call
== error_mark_node
)
7791 return error_mark_node
;
7792 append_to_statement_list(call
, &stmt_list
);
7798 static tree print_nl_fndecl
;
7799 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7804 if (call
== error_mark_node
)
7805 return error_mark_node
;
7806 append_to_statement_list(call
, &stmt_list
);
7814 const Expression_list
* args
= this->args();
7815 gcc_assert(args
!= NULL
&& args
->size() == 1);
7816 Expression
* arg
= args
->front();
7817 tree arg_tree
= arg
->get_tree(context
);
7818 if (arg_tree
== error_mark_node
)
7819 return error_mark_node
;
7820 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7821 arg_tree
= Expression::convert_for_assignment(context
, empty
,
7823 arg_tree
, location
);
7824 static tree panic_fndecl
;
7825 tree call
= Gogo::call_builtin(&panic_fndecl
,
7830 TREE_TYPE(arg_tree
),
7832 if (call
== error_mark_node
)
7833 return error_mark_node
;
7834 // This function will throw an exception.
7835 TREE_NOTHROW(panic_fndecl
) = 0;
7836 // This function will not return.
7837 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
7841 case BUILTIN_RECOVER
:
7843 // The argument is set when building recover thunks. It's a
7844 // boolean value which is true if we can recover a value now.
7845 const Expression_list
* args
= this->args();
7846 gcc_assert(args
!= NULL
&& args
->size() == 1);
7847 Expression
* arg
= args
->front();
7848 tree arg_tree
= arg
->get_tree(context
);
7849 if (arg_tree
== error_mark_node
)
7850 return error_mark_node
;
7852 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7853 tree empty_tree
= empty
->get_tree(context
->gogo());
7855 Type
* nil_type
= Type::make_nil_type();
7856 Expression
* nil
= Expression::make_nil(location
);
7857 tree nil_tree
= nil
->get_tree(context
);
7858 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
7864 // We need to handle a deferred call to recover specially,
7865 // because it changes whether it can recover a panic or not.
7866 // See test7 in test/recover1.go.
7868 if (this->is_deferred())
7870 static tree deferred_recover_fndecl
;
7871 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
7873 "__go_deferred_recover",
7879 static tree recover_fndecl
;
7880 call
= Gogo::call_builtin(&recover_fndecl
,
7886 if (call
== error_mark_node
)
7887 return error_mark_node
;
7888 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
7889 call
, empty_nil_tree
);
7893 case BUILTIN_CLOSED
:
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
;
7901 if (this->code_
== BUILTIN_CLOSE
)
7903 static tree close_fndecl
;
7904 return Gogo::call_builtin(&close_fndecl
,
7906 "__go_builtin_close",
7909 TREE_TYPE(arg_tree
),
7914 static tree closed_fndecl
;
7915 return Gogo::call_builtin(&closed_fndecl
,
7917 "__go_builtin_closed",
7920 TREE_TYPE(arg_tree
),
7925 case BUILTIN_SIZEOF
:
7926 case BUILTIN_OFFSETOF
:
7927 case BUILTIN_ALIGNOF
:
7932 bool b
= this->integer_constant_value(true, val
, &dummy
);
7934 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
7935 tree ret
= Expression::integer_constant_tree(val
, type
);
7942 const Expression_list
* args
= this->args();
7943 gcc_assert(args
!= NULL
&& args
->size() == 2);
7944 Expression
* arg1
= args
->front();
7945 Expression
* arg2
= args
->back();
7947 tree arg1_tree
= arg1
->get_tree(context
);
7948 tree arg2_tree
= arg2
->get_tree(context
);
7949 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7950 return error_mark_node
;
7952 Type
* arg1_type
= arg1
->type();
7953 Array_type
* at
= arg1_type
->array_type();
7954 arg1_tree
= save_expr(arg1_tree
);
7955 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
7956 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
7957 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
7958 return error_mark_node
;
7960 Type
* arg2_type
= arg2
->type();
7963 if (arg2_type
->is_open_array_type())
7965 at
= arg2_type
->array_type();
7966 arg2_tree
= save_expr(arg2_tree
);
7967 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7968 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7972 arg2_tree
= save_expr(arg2_tree
);
7973 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
7974 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
7976 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7977 return error_mark_node
;
7979 arg1_len
= save_expr(arg1_len
);
7980 arg2_len
= save_expr(arg2_len
);
7981 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
7982 fold_build2_loc(location
, LT_EXPR
,
7984 arg1_len
, arg2_len
),
7985 arg1_len
, arg2_len
);
7986 len
= save_expr(len
);
7988 Type
* element_type
= at
->element_type();
7989 tree element_type_tree
= element_type
->get_tree(gogo
);
7990 if (element_type_tree
== error_mark_node
)
7991 return error_mark_node
;
7992 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7993 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
7995 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
7996 TREE_TYPE(element_size
),
7997 bytecount
, element_size
);
7998 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
8000 arg1_val
= fold_convert_loc(location
, ptr_type_node
, arg1_val
);
8001 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
8003 static tree copy_fndecl
;
8004 tree call
= Gogo::call_builtin(©_fndecl
,
8015 if (call
== error_mark_node
)
8016 return error_mark_node
;
8018 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
8022 case BUILTIN_APPEND
:
8024 const Expression_list
* args
= this->args();
8025 gcc_assert(args
!= NULL
&& args
->size() == 2);
8026 Expression
* arg1
= args
->front();
8027 Expression
* arg2
= args
->back();
8029 tree arg1_tree
= arg1
->get_tree(context
);
8030 tree arg2_tree
= arg2
->get_tree(context
);
8031 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
8032 return error_mark_node
;
8034 Array_type
* at
= arg1
->type()->array_type();
8035 Type
* element_type
= at
->element_type();
8037 arg2_tree
= Expression::convert_for_assignment(context
, at
,
8041 if (arg2_tree
== error_mark_node
)
8042 return error_mark_node
;
8044 arg2_tree
= save_expr(arg2_tree
);
8045 tree arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
8046 tree arg2_len
= at
->length_tree(gogo
, arg2_tree
);
8047 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
8048 return error_mark_node
;
8049 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
8050 arg2_len
= fold_convert_loc(location
, size_type_node
, arg2_len
);
8052 tree element_type_tree
= element_type
->get_tree(gogo
);
8053 if (element_type_tree
== error_mark_node
)
8054 return error_mark_node
;
8055 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
8056 element_size
= fold_convert_loc(location
, size_type_node
,
8059 // We rebuild the decl each time since the slice types may
8061 tree append_fndecl
= NULL_TREE
;
8062 return Gogo::call_builtin(&append_fndecl
,
8066 TREE_TYPE(arg1_tree
),
8067 TREE_TYPE(arg1_tree
),
8080 const Expression_list
* args
= this->args();
8081 gcc_assert(args
!= NULL
&& args
->size() == 1);
8082 Expression
* arg
= args
->front();
8083 tree arg_tree
= arg
->get_tree(context
);
8084 if (arg_tree
== error_mark_node
)
8085 return error_mark_node
;
8086 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
8087 if (this->code_
== BUILTIN_REAL
)
8088 return fold_build1_loc(location
, REALPART_EXPR
,
8089 TREE_TYPE(TREE_TYPE(arg_tree
)),
8092 return fold_build1_loc(location
, IMAGPART_EXPR
,
8093 TREE_TYPE(TREE_TYPE(arg_tree
)),
8097 case BUILTIN_COMPLEX
:
8099 const Expression_list
* args
= this->args();
8100 gcc_assert(args
!= NULL
&& args
->size() == 2);
8101 tree r
= args
->front()->get_tree(context
);
8102 tree i
= args
->back()->get_tree(context
);
8103 if (r
== error_mark_node
|| i
== error_mark_node
)
8104 return error_mark_node
;
8105 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
8106 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
8107 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
8108 return fold_build2_loc(location
, COMPLEX_EXPR
,
8109 build_complex_type(TREE_TYPE(r
)),
8118 // We have to support exporting a builtin call expression, because
8119 // code can set a constant to the result of a builtin expression.
8122 Builtin_call_expression::do_export(Export
* exp
) const
8129 if (this->integer_constant_value(true, val
, &dummy
))
8131 Integer_expression::export_integer(exp
, val
);
8140 if (this->float_constant_value(fval
, &dummy
))
8142 Float_expression::export_float(exp
, fval
);
8154 if (this->complex_constant_value(real
, imag
, &dummy
))
8156 Complex_expression::export_complex(exp
, real
, imag
);
8165 error_at(this->location(), "value is not constant");
8169 // A trailing space lets us reliably identify the end of the number.
8170 exp
->write_c_string(" ");
8173 // Class Call_expression.
8178 Call_expression::do_traverse(Traverse
* traverse
)
8180 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8181 return TRAVERSE_EXIT
;
8182 if (this->args_
!= NULL
)
8184 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8185 return TRAVERSE_EXIT
;
8187 return TRAVERSE_CONTINUE
;
8190 // Lower a call statement.
8193 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
8195 // A type case can look like a function call.
8196 if (this->fn_
->is_type_expression()
8197 && this->args_
!= NULL
8198 && this->args_
->size() == 1)
8199 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8202 // Recognize a call to a builtin function.
8203 Func_expression
* fne
= this->fn_
->func_expression();
8205 && fne
->named_object()->is_function_declaration()
8206 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
8207 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8208 this->is_varargs_
, this->location());
8210 // Handle an argument which is a call to a function which returns
8211 // multiple results.
8212 if (this->args_
!= NULL
8213 && this->args_
->size() == 1
8214 && this->args_
->front()->call_expression() != NULL
8215 && this->fn_
->type()->function_type() != NULL
)
8217 Function_type
* fntype
= this->fn_
->type()->function_type();
8218 size_t rc
= this->args_
->front()->call_expression()->result_count();
8220 && fntype
->parameters() != NULL
8221 && (fntype
->parameters()->size() == rc
8222 || (fntype
->is_varargs()
8223 && fntype
->parameters()->size() - 1 <= rc
)))
8225 Call_expression
* call
= this->args_
->front()->call_expression();
8226 Expression_list
* args
= new Expression_list
;
8227 for (size_t i
= 0; i
< rc
; ++i
)
8228 args
->push_back(Expression::make_call_result(call
, i
));
8229 // We can't return a new call expression here, because this
8230 // one may be referenced by Call_result expressions. FIXME.
8236 // Handle a call to a varargs function by packaging up the extra
8238 if (this->fn_
->type()->function_type() != NULL
8239 && this->fn_
->type()->function_type()->is_varargs())
8241 Function_type
* fntype
= this->fn_
->type()->function_type();
8242 const Typed_identifier_list
* parameters
= fntype
->parameters();
8243 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
8244 Type
* varargs_type
= parameters
->back().type();
8245 return this->lower_varargs(gogo
, function
, varargs_type
,
8246 parameters
->size());
8252 // Lower a call to a varargs function. FUNCTION is the function in
8253 // which the call occurs--it's not the function we are calling.
8254 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8255 // PARAM_COUNT is the number of parameters of the function we are
8256 // calling; the last of these parameters will be the varargs
8260 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8261 Type
* varargs_type
, size_t param_count
)
8263 if (this->varargs_are_lowered_
)
8266 source_location loc
= this->location();
8268 gcc_assert(param_count
> 0);
8269 gcc_assert(varargs_type
->is_open_array_type());
8271 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8272 if (arg_count
< param_count
- 1)
8274 // Not enough arguments; will be caught in check_types.
8278 Expression_list
* old_args
= this->args_
;
8279 Expression_list
* new_args
= new Expression_list();
8280 bool push_empty_arg
= false;
8281 if (old_args
== NULL
|| old_args
->empty())
8283 gcc_assert(param_count
== 1);
8284 push_empty_arg
= true;
8288 Expression_list::const_iterator pa
;
8290 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8292 if (static_cast<size_t>(i
) == param_count
)
8294 new_args
->push_back(*pa
);
8297 // We have reached the varargs parameter.
8299 bool issued_error
= false;
8300 if (pa
== old_args
->end())
8301 push_empty_arg
= true;
8302 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8303 new_args
->push_back(*pa
);
8304 else if (this->is_varargs_
)
8306 this->report_error(_("too many arguments"));
8309 else if (pa
+ 1 == old_args
->end()
8310 && this->is_compatible_varargs_argument(function
, *pa
,
8313 new_args
->push_back(*pa
);
8316 Type
* element_type
= varargs_type
->array_type()->element_type();
8317 Expression_list
* vals
= new Expression_list
;
8318 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8320 // Check types here so that we get a better message.
8321 Type
* patype
= (*pa
)->type();
8322 source_location paloc
= (*pa
)->location();
8323 if (!this->check_argument_type(i
, element_type
, patype
,
8324 paloc
, issued_error
))
8326 vals
->push_back(*pa
);
8329 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8330 new_args
->push_back(val
);
8335 new_args
->push_back(Expression::make_nil(loc
));
8337 // We can't return a new call expression here, because this one may
8338 // be referenced by Call_result expressions. FIXME.
8339 if (old_args
!= NULL
)
8341 this->args_
= new_args
;
8342 this->varargs_are_lowered_
= true;
8344 // Lower all the new subexpressions.
8345 Expression
* ret
= this;
8346 gogo
->lower_expression(function
, &ret
);
8347 gcc_assert(ret
== this);
8351 // Return true if ARG is a varargs argment which should be passed to
8352 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8353 // will be the last argument passed in the call, and PARAM_TYPE will
8354 // be the type of the last parameter of the varargs function being
8358 Call_expression::is_compatible_varargs_argument(Named_object
* function
,
8363 *issued_error
= false;
8365 Type
* var_type
= NULL
;
8367 // The simple case is passing the varargs parameter of the caller.
8368 Var_expression
* ve
= arg
->var_expression();
8369 if (ve
!= NULL
&& ve
->named_object()->is_variable())
8371 Variable
* var
= ve
->named_object()->var_value();
8372 if (var
->is_varargs_parameter())
8373 var_type
= var
->type();
8376 // The complex case is passing the varargs parameter of some
8377 // enclosing function. This will look like passing down *c.f where
8378 // c is the closure variable and f is a field in the closure.
8379 if (function
!= NULL
8380 && function
->func_value()->needs_closure()
8381 && arg
->classification() == EXPRESSION_UNARY
)
8383 Unary_expression
* ue
= static_cast<Unary_expression
*>(arg
);
8384 if (ue
->op() == OPERATOR_MULT
)
8386 Field_reference_expression
* fre
=
8387 ue
->operand()->deref()->field_reference_expression();
8390 Var_expression
* ve
= fre
->expr()->deref()->var_expression();
8393 Named_object
* no
= ve
->named_object();
8394 Function
* f
= function
->func_value();
8395 if (no
== f
->closure_var())
8397 // At this point we know that this indeed a
8398 // reference to some enclosing variable. Now we
8399 // need to figure out whether that variable is a
8400 // varargs parameter.
8401 Named_object
* enclosing
=
8402 f
->enclosing_var(fre
->field_index());
8403 Variable
* var
= enclosing
->var_value();
8404 if (var
->is_varargs_parameter())
8405 var_type
= var
->type();
8412 if (var_type
== NULL
)
8415 // We only match if the parameter is the same, with an identical
8417 Array_type
* var_at
= var_type
->array_type();
8418 gcc_assert(var_at
!= NULL
);
8419 Array_type
* param_at
= param_type
->array_type();
8420 if (param_at
!= NULL
8421 && Type::are_identical(var_at
->element_type(),
8422 param_at
->element_type(), true, NULL
))
8424 error_at(arg
->location(), "... mismatch: passing ...T as ...");
8425 *issued_error
= true;
8429 // Get the function type. Returns NULL if we don't know the type. If
8430 // this returns NULL, and if_ERROR is true, issues an error.
8433 Call_expression::get_function_type() const
8435 return this->fn_
->type()->function_type();
8438 // Return the number of values which this call will return.
8441 Call_expression::result_count() const
8443 const Function_type
* fntype
= this->get_function_type();
8446 if (fntype
->results() == NULL
)
8448 return fntype
->results()->size();
8451 // Return whether this is a call to the predeclared function recover.
8454 Call_expression::is_recover_call() const
8456 return this->do_is_recover_call();
8459 // Set the argument to the recover function.
8462 Call_expression::set_recover_arg(Expression
* arg
)
8464 this->do_set_recover_arg(arg
);
8467 // Virtual functions also implemented by Builtin_call_expression.
8470 Call_expression::do_is_recover_call() const
8476 Call_expression::do_set_recover_arg(Expression
*)
8484 Call_expression::do_type()
8486 if (this->type_
!= NULL
)
8490 Function_type
* fntype
= this->get_function_type();
8492 return Type::make_error_type();
8494 const Typed_identifier_list
* results
= fntype
->results();
8495 if (results
== NULL
)
8496 ret
= Type::make_void_type();
8497 else if (results
->size() == 1)
8498 ret
= results
->begin()->type();
8500 ret
= Type::make_call_multiple_result_type(this);
8507 // Determine types for a call expression. We can use the function
8508 // parameter types to set the types of the arguments.
8511 Call_expression::do_determine_type(const Type_context
*)
8513 this->fn_
->determine_type_no_context();
8514 Function_type
* fntype
= this->get_function_type();
8515 const Typed_identifier_list
* parameters
= NULL
;
8517 parameters
= fntype
->parameters();
8518 if (this->args_
!= NULL
)
8520 Typed_identifier_list::const_iterator pt
;
8521 if (parameters
!= NULL
)
8522 pt
= parameters
->begin();
8523 for (Expression_list::const_iterator pa
= this->args_
->begin();
8524 pa
!= this->args_
->end();
8527 if (parameters
!= NULL
&& pt
!= parameters
->end())
8529 Type_context
subcontext(pt
->type(), false);
8530 (*pa
)->determine_type(&subcontext
);
8534 (*pa
)->determine_type_no_context();
8539 // Check types for parameter I.
8542 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8543 const Type
* argument_type
,
8544 source_location argument_location
,
8548 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8553 error_at(argument_location
, "argument %d has incompatible type", i
);
8555 error_at(argument_location
,
8556 "argument %d has incompatible type (%s)",
8559 this->set_is_error();
8568 Call_expression::do_check_types(Gogo
*)
8570 Function_type
* fntype
= this->get_function_type();
8573 if (!this->fn_
->type()->is_error_type())
8574 this->report_error(_("expected function"));
8578 if (fntype
->is_method())
8580 // We don't support pointers to methods, so the function has to
8581 // be a bound method expression.
8582 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8585 this->report_error(_("method call without object"));
8588 Type
* first_arg_type
= bme
->first_argument()->type();
8589 if (first_arg_type
->points_to() == NULL
)
8591 // When passing a value, we need to check that we are
8592 // permitted to copy it.
8594 if (!Type::are_assignable(fntype
->receiver()->type(),
8595 first_arg_type
, &reason
))
8598 this->report_error(_("incompatible type for receiver"));
8601 error_at(this->location(),
8602 "incompatible type for receiver (%s)",
8604 this->set_is_error();
8610 // Note that varargs was handled by the lower_varargs() method, so
8611 // we don't have to worry about it here.
8613 const Typed_identifier_list
* parameters
= fntype
->parameters();
8614 if (this->args_
== NULL
)
8616 if (parameters
!= NULL
&& !parameters
->empty())
8617 this->report_error(_("not enough arguments"));
8619 else if (parameters
== NULL
)
8620 this->report_error(_("too many arguments"));
8624 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8625 for (Expression_list::const_iterator pa
= this->args_
->begin();
8626 pa
!= this->args_
->end();
8629 if (pt
== parameters
->end())
8631 this->report_error(_("too many arguments"));
8634 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8635 (*pa
)->location(), false);
8637 if (pt
!= parameters
->end())
8638 this->report_error(_("not enough arguments"));
8642 // Return whether we have to use a temporary variable to ensure that
8643 // we evaluate this call expression in order. If the call returns no
8644 // results then it will inevitably be executed last. If the call
8645 // returns more than one result then it will be used with Call_result
8646 // expressions. So we only have to use a temporary variable if the
8647 // call returns exactly one result.
8650 Call_expression::do_must_eval_in_order() const
8652 return this->result_count() == 1;
8655 // Get the function and the first argument to use when calling a bound
8659 Call_expression::bound_method_function(Translate_context
* context
,
8660 Bound_method_expression
* bound_method
,
8661 tree
* first_arg_ptr
)
8663 Expression
* first_argument
= bound_method
->first_argument();
8664 tree first_arg
= first_argument
->get_tree(context
);
8665 if (first_arg
== error_mark_node
)
8666 return error_mark_node
;
8668 // We always pass a pointer to the first argument when calling a
8670 if (first_argument
->type()->points_to() == NULL
)
8672 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8673 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8674 || DECL_P(first_arg
)
8675 || TREE_CODE(first_arg
) == INDIRECT_REF
8676 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8678 first_arg
= build_fold_addr_expr(first_arg
);
8679 if (DECL_P(first_arg
))
8680 TREE_ADDRESSABLE(first_arg
) = 1;
8684 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8685 get_name(first_arg
));
8686 DECL_IGNORED_P(tmp
) = 0;
8687 DECL_INITIAL(tmp
) = first_arg
;
8688 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8689 build1(DECL_EXPR
, void_type_node
, tmp
),
8690 build_fold_addr_expr(tmp
));
8691 TREE_ADDRESSABLE(tmp
) = 1;
8693 if (first_arg
== error_mark_node
)
8694 return error_mark_node
;
8697 Type
* fatype
= bound_method
->first_argument_type();
8700 if (fatype
->points_to() == NULL
)
8701 fatype
= Type::make_pointer_type(fatype
);
8702 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8703 if (first_arg
== error_mark_node
8704 || TREE_TYPE(first_arg
) == error_mark_node
)
8705 return error_mark_node
;
8708 *first_arg_ptr
= first_arg
;
8710 return bound_method
->method()->get_tree(context
);
8713 // Get the function and the first argument to use when calling an
8714 // interface method.
8717 Call_expression::interface_method_function(
8718 Translate_context
* context
,
8719 Interface_field_reference_expression
* interface_method
,
8720 tree
* first_arg_ptr
)
8722 tree expr
= interface_method
->expr()->get_tree(context
);
8723 if (expr
== error_mark_node
)
8724 return error_mark_node
;
8725 expr
= save_expr(expr
);
8726 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8727 if (first_arg
== error_mark_node
)
8728 return error_mark_node
;
8729 *first_arg_ptr
= first_arg
;
8730 return interface_method
->get_function_tree(context
, expr
);
8733 // Build the call expression.
8736 Call_expression::do_get_tree(Translate_context
* context
)
8738 if (this->tree_
!= NULL_TREE
)
8741 Function_type
* fntype
= this->get_function_type();
8743 return error_mark_node
;
8745 if (this->fn_
->is_error_expression())
8746 return error_mark_node
;
8748 Gogo
* gogo
= context
->gogo();
8749 source_location location
= this->location();
8751 Func_expression
* func
= this->fn_
->func_expression();
8752 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8753 Interface_field_reference_expression
* interface_method
=
8754 this->fn_
->interface_field_reference_expression();
8755 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8756 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8757 gcc_assert(!fntype
->is_method() || is_method
);
8761 if (this->args_
== NULL
|| this->args_
->empty())
8763 nargs
= is_method
? 1 : 0;
8764 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8768 const Typed_identifier_list
* params
= fntype
->parameters();
8769 gcc_assert(params
!= NULL
);
8771 nargs
= this->args_
->size();
8772 int i
= is_method
? 1 : 0;
8774 args
= new tree
[nargs
];
8776 Typed_identifier_list::const_iterator pp
= params
->begin();
8777 Expression_list::const_iterator pe
;
8778 for (pe
= this->args_
->begin();
8779 pe
!= this->args_
->end();
8782 gcc_assert(pp
!= params
->end());
8783 tree arg_val
= (*pe
)->get_tree(context
);
8784 args
[i
] = Expression::convert_for_assignment(context
,
8789 if (args
[i
] == error_mark_node
)
8792 return error_mark_node
;
8795 gcc_assert(pp
== params
->end());
8796 gcc_assert(i
== nargs
);
8799 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8800 if (rettype
== error_mark_node
)
8803 return error_mark_node
;
8808 fn
= func
->get_tree_without_closure(gogo
);
8809 else if (!is_method
)
8810 fn
= this->fn_
->get_tree(context
);
8811 else if (bound_method
!= NULL
)
8812 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8813 else if (interface_method
!= NULL
)
8814 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8818 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8821 return error_mark_node
;
8824 // This is to support builtin math functions when using 80387 math.
8826 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8827 fndecl
= TREE_OPERAND(fndecl
, 0);
8828 tree excess_type
= NULL_TREE
;
8830 && DECL_IS_BUILTIN(fndecl
)
8831 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8833 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8834 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8835 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8836 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8838 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8839 if (excess_type
!= NULL_TREE
)
8841 tree excess_fndecl
= mathfn_built_in(excess_type
,
8842 DECL_FUNCTION_CODE(fndecl
));
8843 if (excess_fndecl
== NULL_TREE
)
8844 excess_type
= NULL_TREE
;
8847 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8848 for (int i
= 0; i
< nargs
; ++i
)
8849 args
[i
] = ::convert(excess_type
, args
[i
]);
8854 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8858 SET_EXPR_LOCATION(ret
, location
);
8862 tree closure_tree
= func
->closure()->get_tree(context
);
8863 if (closure_tree
!= error_mark_node
)
8864 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8867 // If this is a recursive function type which returns itself, as in
8869 // we have used ptr_type_node for the return type. Add a cast here
8870 // to the correct type.
8871 if (TREE_TYPE(ret
) == ptr_type_node
)
8873 tree t
= this->type()->get_tree(gogo
);
8874 ret
= fold_convert_loc(location
, t
, ret
);
8877 if (excess_type
!= NULL_TREE
)
8879 // Calling convert here can undo our excess precision change.
8880 // That may or may not be a bug in convert_to_real.
8881 ret
= build1(NOP_EXPR
, rettype
, ret
);
8884 // If there is more than one result, we will refer to the call
8886 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
8887 ret
= save_expr(ret
);
8894 // Make a call expression.
8897 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
8898 source_location location
)
8900 return new Call_expression(fn
, args
, is_varargs
, location
);
8903 // A single result from a call which returns multiple results.
8905 class Call_result_expression
: public Expression
8908 Call_result_expression(Call_expression
* call
, unsigned int index
)
8909 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
8910 call_(call
), index_(index
)
8915 do_traverse(Traverse
*);
8921 do_determine_type(const Type_context
*);
8924 do_check_types(Gogo
*);
8929 return new Call_result_expression(this->call_
->call_expression(),
8934 do_must_eval_in_order() const
8938 do_get_tree(Translate_context
*);
8941 // The underlying call expression.
8943 // Which result we want.
8944 unsigned int index_
;
8947 // Traverse a call result.
8950 Call_result_expression::do_traverse(Traverse
* traverse
)
8952 if (traverse
->remember_expression(this->call_
))
8954 // We have already traversed the call expression.
8955 return TRAVERSE_CONTINUE
;
8957 return Expression::traverse(&this->call_
, traverse
);
8963 Call_result_expression::do_type()
8965 if (this->classification() == EXPRESSION_ERROR
)
8966 return Type::make_error_type();
8968 // THIS->CALL_ can be replaced with a temporary reference due to
8969 // Call_expression::do_must_eval_in_order when there is an error.
8970 Call_expression
* ce
= this->call_
->call_expression();
8973 this->set_is_error();
8974 return Type::make_error_type();
8976 Function_type
* fntype
= ce
->get_function_type();
8979 this->set_is_error();
8980 return Type::make_error_type();
8982 const Typed_identifier_list
* results
= fntype
->results();
8983 if (results
== NULL
)
8985 this->report_error(_("number of results does not match "
8986 "number of values"));
8987 return Type::make_error_type();
8989 Typed_identifier_list::const_iterator pr
= results
->begin();
8990 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8992 if (pr
== results
->end())
8996 if (pr
== results
->end())
8998 this->report_error(_("number of results does not match "
8999 "number of values"));
9000 return Type::make_error_type();
9005 // Check the type. Just make sure that we trigger the warning in
9009 Call_result_expression::do_check_types(Gogo
*)
9014 // Determine the type. We have nothing to do here, but the 0 result
9015 // needs to pass down to the caller.
9018 Call_result_expression::do_determine_type(const Type_context
*)
9020 if (this->index_
== 0)
9021 this->call_
->determine_type_no_context();
9027 Call_result_expression::do_get_tree(Translate_context
* context
)
9029 tree call_tree
= this->call_
->get_tree(context
);
9030 if (call_tree
== error_mark_node
)
9031 return error_mark_node
;
9032 if (TREE_CODE(TREE_TYPE(call_tree
)) != RECORD_TYPE
)
9034 gcc_assert(saw_errors());
9035 return error_mark_node
;
9037 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
9038 for (unsigned int i
= 0; i
< this->index_
; ++i
)
9040 gcc_assert(field
!= NULL_TREE
);
9041 field
= DECL_CHAIN(field
);
9043 gcc_assert(field
!= NULL_TREE
);
9044 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
9047 // Make a reference to a single result of a call which returns
9048 // multiple results.
9051 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
9053 return new Call_result_expression(call
, index
);
9056 // Class Index_expression.
9061 Index_expression::do_traverse(Traverse
* traverse
)
9063 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
9064 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
9065 || (this->end_
!= NULL
9066 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
9067 return TRAVERSE_EXIT
;
9068 return TRAVERSE_CONTINUE
;
9071 // Lower an index expression. This converts the generic index
9072 // expression into an array index, a string index, or a map index.
9075 Index_expression::do_lower(Gogo
*, Named_object
*, int)
9077 source_location location
= this->location();
9078 Expression
* left
= this->left_
;
9079 Expression
* start
= this->start_
;
9080 Expression
* end
= this->end_
;
9082 Type
* type
= left
->type();
9083 if (type
->is_error_type())
9084 return Expression::make_error(location
);
9085 else if (type
->array_type() != NULL
)
9086 return Expression::make_array_index(left
, start
, end
, location
);
9087 else if (type
->points_to() != NULL
9088 && type
->points_to()->array_type() != NULL
9089 && !type
->points_to()->is_open_array_type())
9091 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
9093 return Expression::make_array_index(deref
, start
, end
, location
);
9095 else if (type
->is_string_type())
9096 return Expression::make_string_index(left
, start
, end
, location
);
9097 else if (type
->map_type() != NULL
)
9101 error_at(location
, "invalid slice of map");
9102 return Expression::make_error(location
);
9104 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
9106 if (this->is_lvalue_
)
9107 ret
->set_is_lvalue();
9113 "attempt to index object which is not array, string, or map");
9114 return Expression::make_error(location
);
9118 // Make an index expression.
9121 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
9122 source_location location
)
9124 return new Index_expression(left
, start
, end
, location
);
9127 // An array index. This is used for both indexing and slicing.
9129 class Array_index_expression
: public Expression
9132 Array_index_expression(Expression
* array
, Expression
* start
,
9133 Expression
* end
, source_location location
)
9134 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
9135 array_(array
), start_(start
), end_(end
), type_(NULL
)
9140 do_traverse(Traverse
*);
9146 do_determine_type(const Type_context
*);
9149 do_check_types(Gogo
*);
9154 return Expression::make_array_index(this->array_
->copy(),
9155 this->start_
->copy(),
9158 : this->end_
->copy()),
9163 do_is_addressable() const;
9166 do_address_taken(bool escapes
)
9167 { this->array_
->address_taken(escapes
); }
9170 do_get_tree(Translate_context
*);
9173 // The array we are getting a value from.
9175 // The start or only index.
9177 // The end index of a slice. This may be NULL for a simple array
9178 // index, or it may be a nil expression for the length of the array.
9180 // The type of the expression.
9184 // Array index traversal.
9187 Array_index_expression::do_traverse(Traverse
* traverse
)
9189 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9190 return TRAVERSE_EXIT
;
9191 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9192 return TRAVERSE_EXIT
;
9193 if (this->end_
!= NULL
)
9195 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9196 return TRAVERSE_EXIT
;
9198 return TRAVERSE_CONTINUE
;
9201 // Return the type of an array index.
9204 Array_index_expression::do_type()
9206 if (this->type_
== NULL
)
9208 Array_type
* type
= this->array_
->type()->array_type();
9210 this->type_
= Type::make_error_type();
9211 else if (this->end_
== NULL
)
9212 this->type_
= type
->element_type();
9213 else if (type
->is_open_array_type())
9215 // A slice of a slice has the same type as the original
9217 this->type_
= this->array_
->type()->deref();
9221 // A slice of an array is a slice.
9222 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9228 // Set the type of an array index.
9231 Array_index_expression::do_determine_type(const Type_context
*)
9233 this->array_
->determine_type_no_context();
9234 this->start_
->determine_type_no_context();
9235 if (this->end_
!= NULL
)
9236 this->end_
->determine_type_no_context();
9239 // Check types of an array index.
9242 Array_index_expression::do_check_types(Gogo
*)
9244 if (this->start_
->type()->integer_type() == NULL
)
9245 this->report_error(_("index must be integer"));
9246 if (this->end_
!= NULL
9247 && this->end_
->type()->integer_type() == NULL
9248 && !this->end_
->is_nil_expression())
9249 this->report_error(_("slice end must be integer"));
9251 Array_type
* array_type
= this->array_
->type()->array_type();
9252 if (array_type
== NULL
)
9254 gcc_assert(this->array_
->type()->is_error_type());
9258 unsigned int int_bits
=
9259 Type::lookup_integer_type("int")->integer_type()->bits();
9264 bool lval_valid
= (array_type
->length() != NULL
9265 && array_type
->length()->integer_constant_value(true,
9270 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9272 if (mpz_sgn(ival
) < 0
9273 || mpz_sizeinbase(ival
, 2) >= int_bits
9275 && (this->end_
== NULL
9276 ? mpz_cmp(ival
, lval
) >= 0
9277 : mpz_cmp(ival
, lval
) > 0)))
9279 error_at(this->start_
->location(), "array index out of bounds");
9280 this->set_is_error();
9283 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9285 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9287 if (mpz_sgn(ival
) < 0
9288 || mpz_sizeinbase(ival
, 2) >= int_bits
9289 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
9291 error_at(this->end_
->location(), "array index out of bounds");
9292 this->set_is_error();
9299 // A slice of an array requires an addressable array. A slice of a
9300 // slice is always possible.
9301 if (this->end_
!= NULL
9302 && !array_type
->is_open_array_type()
9303 && !this->array_
->is_addressable())
9304 this->report_error(_("array is not addressable"));
9307 // Return whether this expression is addressable.
9310 Array_index_expression::do_is_addressable() const
9312 // A slice expression is not addressable.
9313 if (this->end_
!= NULL
)
9316 // An index into a slice is addressable.
9317 if (this->array_
->type()->is_open_array_type())
9320 // An index into an array is addressable if the array is
9322 return this->array_
->is_addressable();
9325 // Get a tree for an array index.
9328 Array_index_expression::do_get_tree(Translate_context
* context
)
9330 Gogo
* gogo
= context
->gogo();
9331 source_location loc
= this->location();
9333 Array_type
* array_type
= this->array_
->type()->array_type();
9334 if (array_type
== NULL
)
9336 gcc_assert(this->array_
->type()->is_error_type());
9337 return error_mark_node
;
9340 tree type_tree
= array_type
->get_tree(gogo
);
9341 if (type_tree
== error_mark_node
)
9342 return error_mark_node
;
9344 tree array_tree
= this->array_
->get_tree(context
);
9345 if (array_tree
== error_mark_node
)
9346 return error_mark_node
;
9348 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9349 array_tree
= save_expr(array_tree
);
9350 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
9351 if (length_tree
== error_mark_node
)
9352 return error_mark_node
;
9353 length_tree
= save_expr(length_tree
);
9354 tree length_type
= TREE_TYPE(length_tree
);
9356 tree bad_index
= boolean_false_node
;
9358 tree start_tree
= this->start_
->get_tree(context
);
9359 if (start_tree
== error_mark_node
)
9360 return error_mark_node
;
9361 if (!DECL_P(start_tree
))
9362 start_tree
= save_expr(start_tree
);
9363 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9364 start_tree
= convert_to_integer(length_type
, start_tree
);
9366 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9369 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9370 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9371 fold_build2_loc(loc
,
9375 boolean_type_node
, start_tree
,
9378 int code
= (array_type
->length() != NULL
9379 ? (this->end_
== NULL
9380 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9381 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9382 : (this->end_
== NULL
9383 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9384 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9385 tree crash
= Gogo::runtime_error(code
, loc
);
9387 if (this->end_
== NULL
)
9389 // Simple array indexing. This has to return an l-value, so
9390 // wrap the index check into START_TREE.
9391 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9392 build3(COND_EXPR
, void_type_node
,
9393 bad_index
, crash
, NULL_TREE
),
9395 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9397 if (array_type
->length() != NULL
)
9400 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9401 start_tree
, NULL_TREE
, NULL_TREE
);
9406 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9407 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9408 if (element_type_tree
== error_mark_node
)
9409 return error_mark_node
;
9410 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9411 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9412 start_tree
, element_size
);
9413 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9414 TREE_TYPE(values
), values
, offset
);
9415 return build_fold_indirect_ref(ptr
);
9421 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9422 if (capacity_tree
== error_mark_node
)
9423 return error_mark_node
;
9424 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9427 if (this->end_
->is_nil_expression())
9428 end_tree
= length_tree
;
9431 end_tree
= this->end_
->get_tree(context
);
9432 if (end_tree
== error_mark_node
)
9433 return error_mark_node
;
9434 if (!DECL_P(end_tree
))
9435 end_tree
= save_expr(end_tree
);
9436 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9437 end_tree
= convert_to_integer(length_type
, end_tree
);
9439 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9442 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9444 capacity_tree
= save_expr(capacity_tree
);
9445 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9446 fold_build2_loc(loc
, LT_EXPR
,
9448 end_tree
, start_tree
),
9449 fold_build2_loc(loc
, GT_EXPR
,
9451 end_tree
, capacity_tree
));
9452 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9453 bad_index
, bad_end
);
9456 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9457 if (element_type_tree
== error_mark_node
)
9458 return error_mark_node
;
9459 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9461 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9462 fold_convert_loc(loc
, sizetype
, start_tree
),
9465 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9466 if (value_pointer
== error_mark_node
)
9467 return error_mark_node
;
9469 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9470 TREE_TYPE(value_pointer
),
9471 value_pointer
, offset
);
9473 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9474 end_tree
, start_tree
);
9476 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9477 capacity_tree
, start_tree
);
9479 tree struct_tree
= this->type()->get_tree(gogo
);
9480 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9482 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9484 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9485 tree field
= TYPE_FIELDS(struct_tree
);
9486 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9488 elt
->value
= value_pointer
;
9490 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9491 field
= DECL_CHAIN(field
);
9492 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9494 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9496 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9497 field
= DECL_CHAIN(field
);
9498 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9500 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9502 tree constructor
= build_constructor(struct_tree
, init
);
9504 if (TREE_CONSTANT(value_pointer
)
9505 && TREE_CONSTANT(result_length_tree
)
9506 && TREE_CONSTANT(result_capacity_tree
))
9507 TREE_CONSTANT(constructor
) = 1;
9509 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9510 build3(COND_EXPR
, void_type_node
,
9511 bad_index
, crash
, NULL_TREE
),
9515 // Make an array index expression. END may be NULL.
9518 Expression::make_array_index(Expression
* array
, Expression
* start
,
9519 Expression
* end
, source_location location
)
9521 // Taking a slice of a composite literal requires moving the literal
9523 if (end
!= NULL
&& array
->is_composite_literal())
9525 array
= Expression::make_heap_composite(array
, location
);
9526 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9528 return new Array_index_expression(array
, start
, end
, location
);
9531 // A string index. This is used for both indexing and slicing.
9533 class String_index_expression
: public Expression
9536 String_index_expression(Expression
* string
, Expression
* start
,
9537 Expression
* end
, source_location location
)
9538 : Expression(EXPRESSION_STRING_INDEX
, location
),
9539 string_(string
), start_(start
), end_(end
)
9544 do_traverse(Traverse
*);
9550 do_determine_type(const Type_context
*);
9553 do_check_types(Gogo
*);
9558 return Expression::make_string_index(this->string_
->copy(),
9559 this->start_
->copy(),
9562 : this->end_
->copy()),
9567 do_get_tree(Translate_context
*);
9570 // The string we are getting a value from.
9571 Expression
* string_
;
9572 // The start or only index.
9574 // The end index of a slice. This may be NULL for a single index,
9575 // or it may be a nil expression for the length of the string.
9579 // String index traversal.
9582 String_index_expression::do_traverse(Traverse
* traverse
)
9584 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9585 return TRAVERSE_EXIT
;
9586 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9587 return TRAVERSE_EXIT
;
9588 if (this->end_
!= NULL
)
9590 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9591 return TRAVERSE_EXIT
;
9593 return TRAVERSE_CONTINUE
;
9596 // Return the type of a string index.
9599 String_index_expression::do_type()
9601 if (this->end_
== NULL
)
9602 return Type::lookup_integer_type("uint8");
9604 return this->string_
->type();
9607 // Determine the type of a string index.
9610 String_index_expression::do_determine_type(const Type_context
*)
9612 this->string_
->determine_type_no_context();
9613 Type_context
subcontext(NULL
, true);
9614 this->start_
->determine_type(&subcontext
);
9615 if (this->end_
!= NULL
)
9616 this->end_
->determine_type(&subcontext
);
9619 // Check types of a string index.
9622 String_index_expression::do_check_types(Gogo
*)
9624 if (this->start_
->type()->integer_type() == NULL
)
9625 this->report_error(_("index must be integer"));
9626 if (this->end_
!= NULL
9627 && this->end_
->type()->integer_type() == NULL
9628 && !this->end_
->is_nil_expression())
9629 this->report_error(_("slice end must be integer"));
9632 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9637 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9639 if (mpz_sgn(ival
) < 0
9640 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9642 error_at(this->start_
->location(), "string index out of bounds");
9643 this->set_is_error();
9646 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9648 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9650 if (mpz_sgn(ival
) < 0
9651 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9653 error_at(this->end_
->location(), "string index out of bounds");
9654 this->set_is_error();
9661 // Get a tree for a string index.
9664 String_index_expression::do_get_tree(Translate_context
* context
)
9666 source_location loc
= this->location();
9668 tree string_tree
= this->string_
->get_tree(context
);
9669 if (string_tree
== error_mark_node
)
9670 return error_mark_node
;
9672 if (this->string_
->type()->points_to() != NULL
)
9673 string_tree
= build_fold_indirect_ref(string_tree
);
9674 if (!DECL_P(string_tree
))
9675 string_tree
= save_expr(string_tree
);
9676 tree string_type
= TREE_TYPE(string_tree
);
9678 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9679 length_tree
= save_expr(length_tree
);
9680 tree length_type
= TREE_TYPE(length_tree
);
9682 tree bad_index
= boolean_false_node
;
9684 tree start_tree
= this->start_
->get_tree(context
);
9685 if (start_tree
== error_mark_node
)
9686 return error_mark_node
;
9687 if (!DECL_P(start_tree
))
9688 start_tree
= save_expr(start_tree
);
9689 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9690 start_tree
= convert_to_integer(length_type
, start_tree
);
9692 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9695 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9697 int code
= (this->end_
== NULL
9698 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9699 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9700 tree crash
= Gogo::runtime_error(code
, loc
);
9702 if (this->end_
== NULL
)
9704 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9706 fold_build2_loc(loc
, GE_EXPR
,
9708 start_tree
, length_tree
));
9710 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9711 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9713 fold_convert_loc(loc
, sizetype
, start_tree
));
9714 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9716 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9717 build3(COND_EXPR
, void_type_node
,
9718 bad_index
, crash
, NULL_TREE
),
9724 if (this->end_
->is_nil_expression())
9725 end_tree
= build_int_cst(length_type
, -1);
9728 end_tree
= this->end_
->get_tree(context
);
9729 if (end_tree
== error_mark_node
)
9730 return error_mark_node
;
9731 if (!DECL_P(end_tree
))
9732 end_tree
= save_expr(end_tree
);
9733 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9734 end_tree
= convert_to_integer(length_type
, end_tree
);
9736 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9739 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9742 static tree strslice_fndecl
;
9743 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9745 "__go_string_slice",
9754 if (ret
== error_mark_node
)
9755 return error_mark_node
;
9756 // This will panic if the bounds are out of range for the
9758 TREE_NOTHROW(strslice_fndecl
) = 0;
9760 if (bad_index
== boolean_false_node
)
9763 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9764 build3(COND_EXPR
, void_type_node
,
9765 bad_index
, crash
, NULL_TREE
),
9770 // Make a string index expression. END may be NULL.
9773 Expression::make_string_index(Expression
* string
, Expression
* start
,
9774 Expression
* end
, source_location location
)
9776 return new String_index_expression(string
, start
, end
, location
);
9781 // Get the type of the map.
9784 Map_index_expression::get_map_type() const
9786 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9788 gcc_assert(saw_errors());
9792 // Map index traversal.
9795 Map_index_expression::do_traverse(Traverse
* traverse
)
9797 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9798 return TRAVERSE_EXIT
;
9799 return Expression::traverse(&this->index_
, traverse
);
9802 // Return the type of a map index.
9805 Map_index_expression::do_type()
9807 Map_type
* mt
= this->get_map_type();
9809 return Type::make_error_type();
9810 Type
* type
= mt
->val_type();
9811 // If this map index is in a tuple assignment, we actually return a
9812 // pointer to the value type. Tuple_map_assignment_statement is
9813 // responsible for handling this correctly. We need to get the type
9814 // right in case this gets assigned to a temporary variable.
9815 if (this->is_in_tuple_assignment_
)
9816 type
= Type::make_pointer_type(type
);
9820 // Fix the type of a map index.
9823 Map_index_expression::do_determine_type(const Type_context
*)
9825 this->map_
->determine_type_no_context();
9826 Map_type
* mt
= this->get_map_type();
9827 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
9828 Type_context
subcontext(key_type
, false);
9829 this->index_
->determine_type(&subcontext
);
9832 // Check types of a map index.
9835 Map_index_expression::do_check_types(Gogo
*)
9838 Map_type
* mt
= this->get_map_type();
9841 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
9844 this->report_error(_("incompatible type for map index"));
9847 error_at(this->location(), "incompatible type for map index (%s)",
9849 this->set_is_error();
9854 // Get a tree for a map index.
9857 Map_index_expression::do_get_tree(Translate_context
* context
)
9859 Map_type
* type
= this->get_map_type();
9861 return error_mark_node
;
9863 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9864 if (valptr
== error_mark_node
)
9865 return error_mark_node
;
9866 valptr
= save_expr(valptr
);
9868 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
9870 if (this->is_lvalue_
)
9871 return build_fold_indirect_ref(valptr
);
9872 else if (this->is_in_tuple_assignment_
)
9874 // Tuple_map_assignment_statement is responsible for using this
9880 return fold_build3(COND_EXPR
, val_type_tree
,
9881 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
9882 fold_convert(TREE_TYPE(valptr
),
9883 null_pointer_node
)),
9884 type
->val_type()->get_init_tree(context
->gogo(),
9886 build_fold_indirect_ref(valptr
));
9890 // Get a tree for the map index. This returns a tree which evaluates
9891 // to a pointer to a value. The pointer will be NULL if the key is
9895 Map_index_expression::get_value_pointer(Translate_context
* context
,
9898 Map_type
* type
= this->get_map_type();
9900 return error_mark_node
;
9902 tree map_tree
= this->map_
->get_tree(context
);
9903 tree index_tree
= this->index_
->get_tree(context
);
9904 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
9905 this->index_
->type(),
9908 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
9909 return error_mark_node
;
9911 if (this->map_
->type()->points_to() != NULL
)
9912 map_tree
= build_fold_indirect_ref(map_tree
);
9914 // We need to pass in a pointer to the key, so stuff it into a
9916 tree tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
9917 DECL_IGNORED_P(tmp
) = 0;
9918 DECL_INITIAL(tmp
) = index_tree
;
9919 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
9920 tree tmpref
= fold_convert(const_ptr_type_node
, build_fold_addr_expr(tmp
));
9921 TREE_ADDRESSABLE(tmp
) = 1;
9923 static tree map_index_fndecl
;
9924 tree call
= Gogo::call_builtin(&map_index_fndecl
,
9928 const_ptr_type_node
,
9929 TREE_TYPE(map_tree
),
9931 const_ptr_type_node
,
9936 : boolean_false_node
));
9937 if (call
== error_mark_node
)
9938 return error_mark_node
;
9939 // This can panic on a map of interface type if the interface holds
9940 // an uncomparable or unhashable type.
9941 TREE_NOTHROW(map_index_fndecl
) = 0;
9943 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
9944 if (val_type_tree
== error_mark_node
)
9945 return error_mark_node
;
9946 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
9948 return build2(COMPOUND_EXPR
, ptr_val_type_tree
,
9950 fold_convert(ptr_val_type_tree
, call
));
9953 // Make a map index expression.
9955 Map_index_expression
*
9956 Expression::make_map_index(Expression
* map
, Expression
* index
,
9957 source_location location
)
9959 return new Map_index_expression(map
, index
, location
);
9962 // Class Field_reference_expression.
9964 // Return the type of a field reference.
9967 Field_reference_expression::do_type()
9969 Type
* type
= this->expr_
->type();
9970 if (type
->is_error_type())
9972 Struct_type
* struct_type
= type
->struct_type();
9973 gcc_assert(struct_type
!= NULL
);
9974 return struct_type
->field(this->field_index_
)->type();
9977 // Check the types for a field reference.
9980 Field_reference_expression::do_check_types(Gogo
*)
9982 Type
* type
= this->expr_
->type();
9983 if (type
->is_error_type())
9985 Struct_type
* struct_type
= type
->struct_type();
9986 gcc_assert(struct_type
!= NULL
);
9987 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
9990 // Get a tree for a field reference.
9993 Field_reference_expression::do_get_tree(Translate_context
* context
)
9995 tree struct_tree
= this->expr_
->get_tree(context
);
9996 if (struct_tree
== error_mark_node
9997 || TREE_TYPE(struct_tree
) == error_mark_node
)
9998 return error_mark_node
;
9999 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
10000 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
10001 if (field
== NULL_TREE
)
10003 // This can happen for a type which refers to itself indirectly
10004 // and then turns out to be erroneous.
10005 gcc_assert(saw_errors());
10006 return error_mark_node
;
10008 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
10010 field
= DECL_CHAIN(field
);
10011 gcc_assert(field
!= NULL_TREE
);
10013 if (TREE_TYPE(field
) == error_mark_node
)
10014 return error_mark_node
;
10015 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
10019 // Make a reference to a qualified identifier in an expression.
10021 Field_reference_expression
*
10022 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
10023 source_location location
)
10025 return new Field_reference_expression(expr
, field_index
, location
);
10028 // Class Interface_field_reference_expression.
10030 // Return a tree for the pointer to the function to call.
10033 Interface_field_reference_expression::get_function_tree(Translate_context
*,
10036 if (this->expr_
->type()->points_to() != NULL
)
10037 expr
= build_fold_indirect_ref(expr
);
10039 tree expr_type
= TREE_TYPE(expr
);
10040 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10042 tree field
= TYPE_FIELDS(expr_type
);
10043 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
10045 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10046 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
10048 table
= build_fold_indirect_ref(table
);
10049 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
10051 std::string name
= Gogo::unpack_hidden_name(this->name_
);
10052 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
10053 field
!= NULL_TREE
;
10054 field
= DECL_CHAIN(field
))
10056 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
10059 gcc_assert(field
!= NULL_TREE
);
10061 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
10064 // Return a tree for the first argument to pass to the interface
10068 Interface_field_reference_expression::get_underlying_object_tree(
10069 Translate_context
*,
10072 if (this->expr_
->type()->points_to() != NULL
)
10073 expr
= build_fold_indirect_ref(expr
);
10075 tree expr_type
= TREE_TYPE(expr
);
10076 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10078 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
10079 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
10081 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10087 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
10089 return Expression::traverse(&this->expr_
, traverse
);
10092 // Return the type of an interface field reference.
10095 Interface_field_reference_expression::do_type()
10097 Type
* expr_type
= this->expr_
->type();
10099 Type
* points_to
= expr_type
->points_to();
10100 if (points_to
!= NULL
)
10101 expr_type
= points_to
;
10103 Interface_type
* interface_type
= expr_type
->interface_type();
10104 if (interface_type
== NULL
)
10105 return Type::make_error_type();
10107 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
10108 if (method
== NULL
)
10109 return Type::make_error_type();
10111 return method
->type();
10114 // Determine types.
10117 Interface_field_reference_expression::do_determine_type(const Type_context
*)
10119 this->expr_
->determine_type_no_context();
10122 // Check the types for an interface field reference.
10125 Interface_field_reference_expression::do_check_types(Gogo
*)
10127 Type
* type
= this->expr_
->type();
10129 Type
* points_to
= type
->points_to();
10130 if (points_to
!= NULL
)
10133 Interface_type
* interface_type
= type
->interface_type();
10134 if (interface_type
== NULL
)
10135 this->report_error(_("expected interface or pointer to interface"));
10138 const Typed_identifier
* method
=
10139 interface_type
->find_method(this->name_
);
10140 if (method
== NULL
)
10142 error_at(this->location(), "method %qs not in interface",
10143 Gogo::message_name(this->name_
).c_str());
10144 this->set_is_error();
10149 // Get a tree for a reference to a field in an interface. There is no
10150 // standard tree type representation for this: it's a function
10151 // attached to its first argument, like a Bound_method_expression.
10152 // The only places it may currently be used are in a Call_expression
10153 // or a Go_statement, which will take it apart directly. So this has
10154 // nothing to do at present.
10157 Interface_field_reference_expression::do_get_tree(Translate_context
*)
10162 // Make a reference to a field in an interface.
10165 Expression::make_interface_field_reference(Expression
* expr
,
10166 const std::string
& field
,
10167 source_location location
)
10169 return new Interface_field_reference_expression(expr
, field
, location
);
10172 // A general selector. This is a Parser_expression for LEFT.NAME. It
10173 // is lowered after we know the type of the left hand side.
10175 class Selector_expression
: public Parser_expression
10178 Selector_expression(Expression
* left
, const std::string
& name
,
10179 source_location location
)
10180 : Parser_expression(EXPRESSION_SELECTOR
, location
),
10181 left_(left
), name_(name
)
10186 do_traverse(Traverse
* traverse
)
10187 { return Expression::traverse(&this->left_
, traverse
); }
10190 do_lower(Gogo
*, Named_object
*, int);
10195 return new Selector_expression(this->left_
->copy(), this->name_
,
10201 lower_method_expression(Gogo
*);
10203 // The expression on the left hand side.
10205 // The name on the right hand side.
10209 // Lower a selector expression once we know the real type of the left
10213 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
10215 Expression
* left
= this->left_
;
10216 if (left
->is_type_expression())
10217 return this->lower_method_expression(gogo
);
10218 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
10222 // Lower a method expression T.M or (*T).M. We turn this into a
10223 // function literal.
10226 Selector_expression::lower_method_expression(Gogo
* gogo
)
10228 source_location location
= this->location();
10229 Type
* type
= this->left_
->type();
10230 const std::string
& name(this->name_
);
10233 if (type
->points_to() == NULL
)
10234 is_pointer
= false;
10238 type
= type
->points_to();
10240 Named_type
* nt
= type
->named_type();
10244 ("method expression requires named type or "
10245 "pointer to named type"));
10246 return Expression::make_error(location
);
10250 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
10251 if (method
== NULL
)
10254 error_at(location
, "type %<%s%> has no method %<%s%>",
10255 nt
->message_name().c_str(),
10256 Gogo::message_name(name
).c_str());
10258 error_at(location
, "method %<%s%> is ambiguous in type %<%s%>",
10259 Gogo::message_name(name
).c_str(),
10260 nt
->message_name().c_str());
10261 return Expression::make_error(location
);
10264 if (!is_pointer
&& !method
->is_value_method())
10266 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
10267 nt
->message_name().c_str(),
10268 Gogo::message_name(name
).c_str());
10269 return Expression::make_error(location
);
10272 // Build a new function type in which the receiver becomes the first
10274 Function_type
* method_type
= method
->type();
10275 gcc_assert(method_type
->is_method());
10277 const char* const receiver_name
= "$this";
10278 Typed_identifier_list
* parameters
= new Typed_identifier_list();
10279 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
10282 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
10283 if (method_parameters
!= NULL
)
10285 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10286 p
!= method_parameters
->end();
10288 parameters
->push_back(*p
);
10291 const Typed_identifier_list
* method_results
= method_type
->results();
10292 Typed_identifier_list
* results
;
10293 if (method_results
== NULL
)
10297 results
= new Typed_identifier_list();
10298 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
10299 p
!= method_results
->end();
10301 results
->push_back(*p
);
10304 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
10306 if (method_type
->is_varargs())
10307 fntype
->set_is_varargs();
10309 // We generate methods which always takes a pointer to the receiver
10310 // as their first argument. If this is for a pointer type, we can
10311 // simply reuse the existing function. We use an internal hack to
10312 // get the right type.
10316 Named_object
* mno
= (method
->needs_stub_method()
10317 ? method
->stub_object()
10318 : method
->named_object());
10319 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
10320 f
= Expression::make_cast(fntype
, f
, location
);
10321 Type_conversion_expression
* tce
=
10322 static_cast<Type_conversion_expression
*>(f
);
10323 tce
->set_may_convert_function_types();
10327 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
10330 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
10331 gcc_assert(vno
!= NULL
);
10332 Expression
* ve
= Expression::make_var_reference(vno
, location
);
10333 Expression
* bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
10335 // Even though we found the method above, if it has an error type we
10336 // may see an error here.
10337 if (bm
->is_error_expression())
10339 gogo
->finish_function(location
);
10343 Expression_list
* args
;
10344 if (method_parameters
== NULL
)
10348 args
= new Expression_list();
10349 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10350 p
!= method_parameters
->end();
10353 vno
= gogo
->lookup(p
->name(), NULL
);
10354 gcc_assert(vno
!= NULL
);
10355 args
->push_back(Expression::make_var_reference(vno
, location
));
10359 Call_expression
* call
= Expression::make_call(bm
, args
,
10360 method_type
->is_varargs(),
10363 size_t count
= call
->result_count();
10366 s
= Statement::make_statement(call
);
10369 Expression_list
* retvals
= new Expression_list();
10371 retvals
->push_back(call
);
10374 for (size_t i
= 0; i
< count
; ++i
)
10375 retvals
->push_back(Expression::make_call_result(call
, i
));
10377 s
= Statement::make_return_statement(no
->func_value()->type()->results(),
10378 retvals
, location
);
10380 gogo
->add_statement(s
);
10382 gogo
->finish_function(location
);
10384 return Expression::make_func_reference(no
, NULL
, location
);
10387 // Make a selector expression.
10390 Expression::make_selector(Expression
* left
, const std::string
& name
,
10391 source_location location
)
10393 return new Selector_expression(left
, name
, location
);
10396 // Implement the builtin function new.
10398 class Allocation_expression
: public Expression
10401 Allocation_expression(Type
* type
, source_location location
)
10402 : Expression(EXPRESSION_ALLOCATION
, location
),
10408 do_traverse(Traverse
* traverse
)
10409 { return Type::traverse(this->type_
, traverse
); }
10413 { return Type::make_pointer_type(this->type_
); }
10416 do_determine_type(const Type_context
*)
10420 do_check_types(Gogo
*);
10424 { return new Allocation_expression(this->type_
, this->location()); }
10427 do_get_tree(Translate_context
*);
10430 // The type we are allocating.
10434 // Check the type of an allocation expression.
10437 Allocation_expression::do_check_types(Gogo
*)
10439 if (this->type_
->function_type() != NULL
)
10440 this->report_error(_("invalid new of function type"));
10443 // Return a tree for an allocation expression.
10446 Allocation_expression::do_get_tree(Translate_context
* context
)
10448 tree type_tree
= this->type_
->get_tree(context
->gogo());
10449 if (type_tree
== error_mark_node
)
10450 return error_mark_node
;
10451 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10452 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10454 if (space
== error_mark_node
)
10455 return error_mark_node
;
10456 return fold_convert(build_pointer_type(type_tree
), space
);
10459 // Make an allocation expression.
10462 Expression::make_allocation(Type
* type
, source_location location
)
10464 return new Allocation_expression(type
, location
);
10467 // Implement the builtin function make.
10469 class Make_expression
: public Expression
10472 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10473 : Expression(EXPRESSION_MAKE
, location
),
10474 type_(type
), args_(args
)
10479 do_traverse(Traverse
* traverse
);
10483 { return this->type_
; }
10486 do_determine_type(const Type_context
*);
10489 do_check_types(Gogo
*);
10494 return new Make_expression(this->type_
, this->args_
->copy(),
10499 do_get_tree(Translate_context
*);
10502 // The type we are making.
10504 // The arguments to pass to the make routine.
10505 Expression_list
* args_
;
10511 Make_expression::do_traverse(Traverse
* traverse
)
10513 if (this->args_
!= NULL
10514 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10515 return TRAVERSE_EXIT
;
10516 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10517 return TRAVERSE_EXIT
;
10518 return TRAVERSE_CONTINUE
;
10521 // Set types of arguments.
10524 Make_expression::do_determine_type(const Type_context
*)
10526 if (this->args_
!= NULL
)
10528 Type_context
context(Type::lookup_integer_type("int"), false);
10529 for (Expression_list::const_iterator pe
= this->args_
->begin();
10530 pe
!= this->args_
->end();
10532 (*pe
)->determine_type(&context
);
10536 // Check types for a make expression.
10539 Make_expression::do_check_types(Gogo
*)
10541 if (this->type_
->channel_type() == NULL
10542 && this->type_
->map_type() == NULL
10543 && (this->type_
->array_type() == NULL
10544 || this->type_
->array_type()->length() != NULL
))
10545 this->report_error(_("invalid type for make function"));
10546 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10547 this->set_is_error();
10550 // Return a tree for a make expression.
10553 Make_expression::do_get_tree(Translate_context
* context
)
10555 return this->type_
->make_expression_tree(context
, this->args_
,
10559 // Make a make expression.
10562 Expression::make_make(Type
* type
, Expression_list
* args
,
10563 source_location location
)
10565 return new Make_expression(type
, args
, location
);
10568 // Construct a struct.
10570 class Struct_construction_expression
: public Expression
10573 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10574 source_location location
)
10575 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10576 type_(type
), vals_(vals
)
10579 // Return whether this is a constant initializer.
10581 is_constant_struct() const;
10585 do_traverse(Traverse
* traverse
);
10589 { return this->type_
; }
10592 do_determine_type(const Type_context
*);
10595 do_check_types(Gogo
*);
10600 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10605 do_is_addressable() const
10609 do_get_tree(Translate_context
*);
10612 do_export(Export
*) const;
10615 // The type of the struct to construct.
10617 // The list of values, in order of the fields in the struct. A NULL
10618 // entry means that the field should be zero-initialized.
10619 Expression_list
* vals_
;
10625 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10627 if (this->vals_
!= NULL
10628 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10629 return TRAVERSE_EXIT
;
10630 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10631 return TRAVERSE_EXIT
;
10632 return TRAVERSE_CONTINUE
;
10635 // Return whether this is a constant initializer.
10638 Struct_construction_expression::is_constant_struct() const
10640 if (this->vals_
== NULL
)
10642 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10643 pv
!= this->vals_
->end();
10647 && !(*pv
)->is_constant()
10648 && (!(*pv
)->is_composite_literal()
10649 || (*pv
)->is_nonconstant_composite_literal()))
10653 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10654 for (Struct_field_list::const_iterator pf
= fields
->begin();
10655 pf
!= fields
->end();
10658 // There are no constant constructors for interfaces.
10659 if (pf
->type()->interface_type() != NULL
)
10666 // Final type determination.
10669 Struct_construction_expression::do_determine_type(const Type_context
*)
10671 if (this->vals_
== NULL
)
10673 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10674 Expression_list::const_iterator pv
= this->vals_
->begin();
10675 for (Struct_field_list::const_iterator pf
= fields
->begin();
10676 pf
!= fields
->end();
10679 if (pv
== this->vals_
->end())
10683 Type_context
subcontext(pf
->type(), false);
10684 (*pv
)->determine_type(&subcontext
);
10687 // Extra values are an error we will report elsewhere; we still want
10688 // to determine the type to avoid knockon errors.
10689 for (; pv
!= this->vals_
->end(); ++pv
)
10690 (*pv
)->determine_type_no_context();
10696 Struct_construction_expression::do_check_types(Gogo
*)
10698 if (this->vals_
== NULL
)
10701 Struct_type
* st
= this->type_
->struct_type();
10702 if (this->vals_
->size() > st
->field_count())
10704 this->report_error(_("too many expressions for struct"));
10708 const Struct_field_list
* fields
= st
->fields();
10709 Expression_list::const_iterator pv
= this->vals_
->begin();
10711 for (Struct_field_list::const_iterator pf
= fields
->begin();
10712 pf
!= fields
->end();
10715 if (pv
== this->vals_
->end())
10717 this->report_error(_("too few expressions for struct"));
10724 std::string reason
;
10725 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10727 if (reason
.empty())
10728 error_at((*pv
)->location(),
10729 "incompatible type for field %d in struct construction",
10732 error_at((*pv
)->location(),
10733 ("incompatible type for field %d in "
10734 "struct construction (%s)"),
10735 i
+ 1, reason
.c_str());
10736 this->set_is_error();
10739 gcc_assert(pv
== this->vals_
->end());
10742 // Return a tree for constructing a struct.
10745 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10747 Gogo
* gogo
= context
->gogo();
10749 if (this->vals_
== NULL
)
10750 return this->type_
->get_init_tree(gogo
, false);
10752 tree type_tree
= this->type_
->get_tree(gogo
);
10753 if (type_tree
== error_mark_node
)
10754 return error_mark_node
;
10755 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10757 bool is_constant
= true;
10758 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10759 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10761 Struct_field_list::const_iterator pf
= fields
->begin();
10762 Expression_list::const_iterator pv
= this->vals_
->begin();
10763 for (tree field
= TYPE_FIELDS(type_tree
);
10764 field
!= NULL_TREE
;
10765 field
= DECL_CHAIN(field
), ++pf
)
10767 gcc_assert(pf
!= fields
->end());
10770 if (pv
== this->vals_
->end())
10771 val
= pf
->type()->get_init_tree(gogo
, false);
10772 else if (*pv
== NULL
)
10774 val
= pf
->type()->get_init_tree(gogo
, false);
10779 val
= Expression::convert_for_assignment(context
, pf
->type(),
10781 (*pv
)->get_tree(context
),
10786 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10787 return error_mark_node
;
10789 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10790 elt
->index
= field
;
10792 if (!TREE_CONSTANT(val
))
10793 is_constant
= false;
10795 gcc_assert(pf
== fields
->end());
10797 tree ret
= build_constructor(type_tree
, elts
);
10799 TREE_CONSTANT(ret
) = 1;
10803 // Export a struct construction.
10806 Struct_construction_expression::do_export(Export
* exp
) const
10808 exp
->write_c_string("convert(");
10809 exp
->write_type(this->type_
);
10810 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10811 pv
!= this->vals_
->end();
10814 exp
->write_c_string(", ");
10816 (*pv
)->export_expression(exp
);
10818 exp
->write_c_string(")");
10821 // Make a struct composite literal. This used by the thunk code.
10824 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
10825 source_location location
)
10827 gcc_assert(type
->struct_type() != NULL
);
10828 return new Struct_construction_expression(type
, vals
, location
);
10831 // Construct an array. This class is not used directly; instead we
10832 // use the child classes, Fixed_array_construction_expression and
10833 // Open_array_construction_expression.
10835 class Array_construction_expression
: public Expression
10838 Array_construction_expression(Expression_classification classification
,
10839 Type
* type
, Expression_list
* vals
,
10840 source_location location
)
10841 : Expression(classification
, location
),
10842 type_(type
), vals_(vals
)
10846 // Return whether this is a constant initializer.
10848 is_constant_array() const;
10850 // Return the number of elements.
10852 element_count() const
10853 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
10857 do_traverse(Traverse
* traverse
);
10861 { return this->type_
; }
10864 do_determine_type(const Type_context
*);
10867 do_check_types(Gogo
*);
10870 do_is_addressable() const
10874 do_export(Export
*) const;
10876 // The list of values.
10879 { return this->vals_
; }
10881 // Get a constructor tree for the array values.
10883 get_constructor_tree(Translate_context
* context
, tree type_tree
);
10886 // The type of the array to construct.
10888 // The list of values.
10889 Expression_list
* vals_
;
10895 Array_construction_expression::do_traverse(Traverse
* traverse
)
10897 if (this->vals_
!= NULL
10898 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10899 return TRAVERSE_EXIT
;
10900 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10901 return TRAVERSE_EXIT
;
10902 return TRAVERSE_CONTINUE
;
10905 // Return whether this is a constant initializer.
10908 Array_construction_expression::is_constant_array() const
10910 if (this->vals_
== NULL
)
10913 // There are no constant constructors for interfaces.
10914 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
10917 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10918 pv
!= this->vals_
->end();
10922 && !(*pv
)->is_constant()
10923 && (!(*pv
)->is_composite_literal()
10924 || (*pv
)->is_nonconstant_composite_literal()))
10930 // Final type determination.
10933 Array_construction_expression::do_determine_type(const Type_context
*)
10935 if (this->vals_
== NULL
)
10937 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
10938 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10939 pv
!= this->vals_
->end();
10943 (*pv
)->determine_type(&subcontext
);
10950 Array_construction_expression::do_check_types(Gogo
*)
10952 if (this->vals_
== NULL
)
10955 Array_type
* at
= this->type_
->array_type();
10957 Type
* element_type
= at
->element_type();
10958 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10959 pv
!= this->vals_
->end();
10963 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
10965 error_at((*pv
)->location(),
10966 "incompatible type for element %d in composite literal",
10968 this->set_is_error();
10972 Expression
* length
= at
->length();
10973 if (length
!= NULL
)
10978 if (at
->length()->integer_constant_value(true, val
, &type
))
10980 if (this->vals_
->size() > mpz_get_ui(val
))
10981 this->report_error(_("too many elements in composite literal"));
10987 // Get a constructor tree for the array values.
10990 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
10993 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10994 (this->vals_
== NULL
10996 : this->vals_
->size()));
10997 Type
* element_type
= this->type_
->array_type()->element_type();
10998 bool is_constant
= true;
10999 if (this->vals_
!= NULL
)
11002 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11003 pv
!= this->vals_
->end();
11006 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11007 elt
->index
= size_int(i
);
11009 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
11012 tree value_tree
= (*pv
)->get_tree(context
);
11013 elt
->value
= Expression::convert_for_assignment(context
,
11019 if (elt
->value
== error_mark_node
)
11020 return error_mark_node
;
11021 if (!TREE_CONSTANT(elt
->value
))
11022 is_constant
= false;
11026 tree ret
= build_constructor(type_tree
, values
);
11028 TREE_CONSTANT(ret
) = 1;
11032 // Export an array construction.
11035 Array_construction_expression::do_export(Export
* exp
) const
11037 exp
->write_c_string("convert(");
11038 exp
->write_type(this->type_
);
11039 if (this->vals_
!= NULL
)
11041 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11042 pv
!= this->vals_
->end();
11045 exp
->write_c_string(", ");
11047 (*pv
)->export_expression(exp
);
11050 exp
->write_c_string(")");
11053 // Construct a fixed array.
11055 class Fixed_array_construction_expression
:
11056 public Array_construction_expression
11059 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
11060 source_location location
)
11061 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
11062 type
, vals
, location
)
11064 gcc_assert(type
->array_type() != NULL
11065 && type
->array_type()->length() != NULL
);
11072 return new Fixed_array_construction_expression(this->type(),
11073 (this->vals() == NULL
11075 : this->vals()->copy()),
11080 do_get_tree(Translate_context
*);
11083 // Return a tree for constructing a fixed array.
11086 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
11088 return this->get_constructor_tree(context
,
11089 this->type()->get_tree(context
->gogo()));
11092 // Construct an open array.
11094 class Open_array_construction_expression
: public Array_construction_expression
11097 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
11098 source_location location
)
11099 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
11100 type
, vals
, location
)
11102 gcc_assert(type
->array_type() != NULL
11103 && type
->array_type()->length() == NULL
);
11107 // Note that taking the address of an open array literal is invalid.
11112 return new Open_array_construction_expression(this->type(),
11113 (this->vals() == NULL
11115 : this->vals()->copy()),
11120 do_get_tree(Translate_context
*);
11123 // Return a tree for constructing an open array.
11126 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
11128 Array_type
* array_type
= this->type()->array_type();
11129 if (array_type
== NULL
)
11131 gcc_assert(this->type()->is_error_type());
11132 return error_mark_node
;
11135 Type
* element_type
= array_type
->element_type();
11136 tree element_type_tree
= element_type
->get_tree(context
->gogo());
11137 if (element_type_tree
== error_mark_node
)
11138 return error_mark_node
;
11142 if (this->vals() == NULL
|| this->vals()->empty())
11144 // We need to create a unique value.
11145 tree max
= size_int(0);
11146 tree constructor_type
= build_array_type(element_type_tree
,
11147 build_index_type(max
));
11148 if (constructor_type
== error_mark_node
)
11149 return error_mark_node
;
11150 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
11151 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
11152 elt
->index
= size_int(0);
11153 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
11154 values
= build_constructor(constructor_type
, vec
);
11155 if (TREE_CONSTANT(elt
->value
))
11156 TREE_CONSTANT(values
) = 1;
11157 length_tree
= size_int(0);
11161 tree max
= size_int(this->vals()->size() - 1);
11162 tree constructor_type
= build_array_type(element_type_tree
,
11163 build_index_type(max
));
11164 if (constructor_type
== error_mark_node
)
11165 return error_mark_node
;
11166 values
= this->get_constructor_tree(context
, constructor_type
);
11167 length_tree
= size_int(this->vals()->size());
11170 if (values
== error_mark_node
)
11171 return error_mark_node
;
11173 bool is_constant_initializer
= TREE_CONSTANT(values
);
11175 // We have to copy the initial values into heap memory if we are in
11176 // a function or if the values are not constants. We also have to
11177 // copy them if they may contain pointers in a non-constant context,
11178 // as otherwise the garbage collector won't see them.
11179 bool copy_to_heap
= (context
->function() != NULL
11180 || !is_constant_initializer
11181 || (element_type
->has_pointer()
11182 && !context
->is_const()));
11184 if (is_constant_initializer
)
11186 tree tmp
= build_decl(this->location(), VAR_DECL
,
11187 create_tmp_var_name("C"), TREE_TYPE(values
));
11188 DECL_EXTERNAL(tmp
) = 0;
11189 TREE_PUBLIC(tmp
) = 0;
11190 TREE_STATIC(tmp
) = 1;
11191 DECL_ARTIFICIAL(tmp
) = 1;
11194 // If we are not copying the value to the heap, we will only
11195 // initialize the value once, so we can use this directly
11196 // rather than copying it. In that case we can't make it
11197 // read-only, because the program is permitted to change it.
11198 TREE_READONLY(tmp
) = 1;
11199 TREE_CONSTANT(tmp
) = 1;
11201 DECL_INITIAL(tmp
) = values
;
11202 rest_of_decl_compilation(tmp
, 1, 0);
11210 // the initializer will only run once.
11211 space
= build_fold_addr_expr(values
);
11216 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
11217 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
11219 space
= save_expr(space
);
11221 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
11222 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
11223 TREE_THIS_NOTRAP(ref
) = 1;
11224 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
11227 // Build a constructor for the open array.
11229 tree type_tree
= this->type()->get_tree(context
->gogo());
11230 if (type_tree
== error_mark_node
)
11231 return error_mark_node
;
11232 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
11234 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
11236 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11237 tree field
= TYPE_FIELDS(type_tree
);
11238 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
11239 elt
->index
= field
;
11240 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
11242 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11243 field
= DECL_CHAIN(field
);
11244 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
11245 elt
->index
= field
;
11246 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11248 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11249 field
= DECL_CHAIN(field
);
11250 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
11251 elt
->index
= field
;
11252 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11254 tree constructor
= build_constructor(type_tree
, init
);
11255 if (constructor
== error_mark_node
)
11256 return error_mark_node
;
11258 TREE_CONSTANT(constructor
) = 1;
11260 if (set
== NULL_TREE
)
11261 return constructor
;
11263 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
11266 // Make a slice composite literal. This is used by the type
11267 // descriptor code.
11270 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
11271 source_location location
)
11273 gcc_assert(type
->is_open_array_type());
11274 return new Open_array_construction_expression(type
, vals
, location
);
11277 // Construct a map.
11279 class Map_construction_expression
: public Expression
11282 Map_construction_expression(Type
* type
, Expression_list
* vals
,
11283 source_location location
)
11284 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
11285 type_(type
), vals_(vals
)
11286 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
11290 do_traverse(Traverse
* traverse
);
11294 { return this->type_
; }
11297 do_determine_type(const Type_context
*);
11300 do_check_types(Gogo
*);
11305 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
11310 do_get_tree(Translate_context
*);
11313 do_export(Export
*) const;
11316 // The type of the map to construct.
11318 // The list of values.
11319 Expression_list
* vals_
;
11325 Map_construction_expression::do_traverse(Traverse
* traverse
)
11327 if (this->vals_
!= NULL
11328 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11329 return TRAVERSE_EXIT
;
11330 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11331 return TRAVERSE_EXIT
;
11332 return TRAVERSE_CONTINUE
;
11335 // Final type determination.
11338 Map_construction_expression::do_determine_type(const Type_context
*)
11340 if (this->vals_
== NULL
)
11343 Map_type
* mt
= this->type_
->map_type();
11344 Type_context
key_context(mt
->key_type(), false);
11345 Type_context
val_context(mt
->val_type(), false);
11346 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11347 pv
!= this->vals_
->end();
11350 (*pv
)->determine_type(&key_context
);
11352 (*pv
)->determine_type(&val_context
);
11359 Map_construction_expression::do_check_types(Gogo
*)
11361 if (this->vals_
== NULL
)
11364 Map_type
* mt
= this->type_
->map_type();
11366 Type
* key_type
= mt
->key_type();
11367 Type
* val_type
= mt
->val_type();
11368 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11369 pv
!= this->vals_
->end();
11372 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
11374 error_at((*pv
)->location(),
11375 "incompatible type for element %d key in map construction",
11377 this->set_is_error();
11380 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
11382 error_at((*pv
)->location(),
11383 ("incompatible type for element %d value "
11384 "in map construction"),
11386 this->set_is_error();
11391 // Return a tree for constructing a map.
11394 Map_construction_expression::do_get_tree(Translate_context
* context
)
11396 Gogo
* gogo
= context
->gogo();
11397 source_location loc
= this->location();
11399 Map_type
* mt
= this->type_
->map_type();
11401 // Build a struct to hold the key and value.
11402 tree struct_type
= make_node(RECORD_TYPE
);
11404 Type
* key_type
= mt
->key_type();
11405 tree id
= get_identifier("__key");
11406 tree key_type_tree
= key_type
->get_tree(gogo
);
11407 if (key_type_tree
== error_mark_node
)
11408 return error_mark_node
;
11409 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type_tree
);
11410 DECL_CONTEXT(key_field
) = struct_type
;
11411 TYPE_FIELDS(struct_type
) = key_field
;
11413 Type
* val_type
= mt
->val_type();
11414 id
= get_identifier("__val");
11415 tree val_type_tree
= val_type
->get_tree(gogo
);
11416 if (val_type_tree
== error_mark_node
)
11417 return error_mark_node
;
11418 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type_tree
);
11419 DECL_CONTEXT(val_field
) = struct_type
;
11420 DECL_CHAIN(key_field
) = val_field
;
11422 layout_type(struct_type
);
11424 bool is_constant
= true;
11429 if (this->vals_
== NULL
|| this->vals_
->empty())
11431 valaddr
= null_pointer_node
;
11432 make_tmp
= NULL_TREE
;
11436 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11437 this->vals_
->size() / 2);
11439 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11440 pv
!= this->vals_
->end();
11443 bool one_is_constant
= true;
11445 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11447 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11448 elt
->index
= key_field
;
11449 tree val_tree
= (*pv
)->get_tree(context
);
11450 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11453 if (elt
->value
== error_mark_node
)
11454 return error_mark_node
;
11455 if (!TREE_CONSTANT(elt
->value
))
11456 one_is_constant
= false;
11460 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11461 elt
->index
= val_field
;
11462 val_tree
= (*pv
)->get_tree(context
);
11463 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11466 if (elt
->value
== error_mark_node
)
11467 return error_mark_node
;
11468 if (!TREE_CONSTANT(elt
->value
))
11469 one_is_constant
= false;
11471 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11472 elt
->index
= size_int(i
);
11473 elt
->value
= build_constructor(struct_type
, one
);
11474 if (one_is_constant
)
11475 TREE_CONSTANT(elt
->value
) = 1;
11477 is_constant
= false;
11480 tree index_type
= build_index_type(size_int(i
- 1));
11481 tree array_type
= build_array_type(struct_type
, index_type
);
11482 tree init
= build_constructor(array_type
, values
);
11484 TREE_CONSTANT(init
) = 1;
11486 if (current_function_decl
!= NULL
)
11488 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11489 DECL_INITIAL(tmp
) = init
;
11490 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11491 TREE_ADDRESSABLE(tmp
) = 1;
11495 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11496 DECL_EXTERNAL(tmp
) = 0;
11497 TREE_PUBLIC(tmp
) = 0;
11498 TREE_STATIC(tmp
) = 1;
11499 DECL_ARTIFICIAL(tmp
) = 1;
11500 if (!TREE_CONSTANT(init
))
11501 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11505 TREE_READONLY(tmp
) = 1;
11506 TREE_CONSTANT(tmp
) = 1;
11507 DECL_INITIAL(tmp
) = init
;
11508 make_tmp
= NULL_TREE
;
11510 rest_of_decl_compilation(tmp
, 1, 0);
11513 valaddr
= build_fold_addr_expr(tmp
);
11516 tree descriptor
= gogo
->map_descriptor(mt
);
11518 tree type_tree
= this->type_
->get_tree(gogo
);
11519 if (type_tree
== error_mark_node
)
11520 return error_mark_node
;
11522 static tree construct_map_fndecl
;
11523 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11525 "__go_construct_map",
11528 TREE_TYPE(descriptor
),
11533 TYPE_SIZE_UNIT(struct_type
),
11535 byte_position(val_field
),
11537 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11538 const_ptr_type_node
,
11539 fold_convert(const_ptr_type_node
, valaddr
));
11540 if (call
== error_mark_node
)
11541 return error_mark_node
;
11544 if (make_tmp
== NULL
)
11547 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11551 // Export an array construction.
11554 Map_construction_expression::do_export(Export
* exp
) const
11556 exp
->write_c_string("convert(");
11557 exp
->write_type(this->type_
);
11558 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11559 pv
!= this->vals_
->end();
11562 exp
->write_c_string(", ");
11563 (*pv
)->export_expression(exp
);
11565 exp
->write_c_string(")");
11568 // A general composite literal. This is lowered to a type specific
11571 class Composite_literal_expression
: public Parser_expression
11574 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11575 Expression_list
* vals
, source_location location
)
11576 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11577 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11582 do_traverse(Traverse
* traverse
);
11585 do_lower(Gogo
*, Named_object
*, int);
11590 return new Composite_literal_expression(this->type_
, this->depth_
,
11592 (this->vals_
== NULL
11594 : this->vals_
->copy()),
11600 lower_struct(Type
*);
11603 lower_array(Type
*);
11606 make_array(Type
*, Expression_list
*);
11609 lower_map(Gogo
*, Named_object
*, Type
*);
11611 // The type of the composite literal.
11613 // The depth within a list of composite literals within a composite
11614 // literal, when the type is omitted.
11616 // The values to put in the composite literal.
11617 Expression_list
* vals_
;
11618 // If this is true, then VALS_ is a list of pairs: a key and a
11619 // value. In an array initializer, a missing key will be NULL.
11626 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11628 if (this->vals_
!= NULL
11629 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11630 return TRAVERSE_EXIT
;
11631 return Type::traverse(this->type_
, traverse
);
11634 // Lower a generic composite literal into a specific version based on
11638 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
11640 Type
* type
= this->type_
;
11642 for (int depth
= this->depth_
; depth
> 0; --depth
)
11644 if (type
->array_type() != NULL
)
11645 type
= type
->array_type()->element_type();
11646 else if (type
->map_type() != NULL
)
11647 type
= type
->map_type()->val_type();
11650 if (!type
->is_error_type())
11651 error_at(this->location(),
11652 ("may only omit types within composite literals "
11653 "of slice, array, or map type"));
11654 return Expression::make_error(this->location());
11658 if (type
->is_error_type())
11659 return Expression::make_error(this->location());
11660 else if (type
->struct_type() != NULL
)
11661 return this->lower_struct(type
);
11662 else if (type
->array_type() != NULL
)
11663 return this->lower_array(type
);
11664 else if (type
->map_type() != NULL
)
11665 return this->lower_map(gogo
, function
, type
);
11668 error_at(this->location(),
11669 ("expected struct, slice, array, or map type "
11670 "for composite literal"));
11671 return Expression::make_error(this->location());
11675 // Lower a struct composite literal.
11678 Composite_literal_expression::lower_struct(Type
* type
)
11680 source_location location
= this->location();
11681 Struct_type
* st
= type
->struct_type();
11682 if (this->vals_
== NULL
|| !this->has_keys_
)
11683 return new Struct_construction_expression(type
, this->vals_
, location
);
11685 size_t field_count
= st
->field_count();
11686 std::vector
<Expression
*> vals(field_count
);
11687 Expression_list::const_iterator p
= this->vals_
->begin();
11688 while (p
!= this->vals_
->end())
11690 Expression
* name_expr
= *p
;
11693 gcc_assert(p
!= this->vals_
->end());
11694 Expression
* val
= *p
;
11698 if (name_expr
== NULL
)
11700 error_at(val
->location(), "mixture of field and value initializers");
11701 return Expression::make_error(location
);
11704 bool bad_key
= false;
11706 switch (name_expr
->classification())
11708 case EXPRESSION_UNKNOWN_REFERENCE
:
11709 name
= name_expr
->unknown_expression()->name();
11712 case EXPRESSION_CONST_REFERENCE
:
11713 name
= static_cast<Const_expression
*>(name_expr
)->name();
11716 case EXPRESSION_TYPE
:
11718 Type
* t
= name_expr
->type();
11719 Named_type
* nt
= t
->named_type();
11727 case EXPRESSION_VAR_REFERENCE
:
11728 name
= name_expr
->var_expression()->name();
11731 case EXPRESSION_FUNC_REFERENCE
:
11732 name
= name_expr
->func_expression()->name();
11735 case EXPRESSION_UNARY
:
11736 // If there is a local variable around with the same name as
11737 // the field, and this occurs in the closure, then the
11738 // parser may turn the field reference into an indirection
11739 // through the closure. FIXME: This is a mess.
11742 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11743 if (ue
->op() == OPERATOR_MULT
)
11745 Field_reference_expression
* fre
=
11746 ue
->operand()->field_reference_expression();
11750 fre
->expr()->type()->deref()->struct_type();
11753 const Struct_field
* sf
= st
->field(fre
->field_index());
11754 name
= sf
->field_name();
11756 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11757 size_t buflen
= strlen(buf
);
11758 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11761 name
= name
.substr(0, name
.length() - buflen
);
11776 error_at(name_expr
->location(), "expected struct field name");
11777 return Expression::make_error(location
);
11780 unsigned int index
;
11781 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11784 error_at(name_expr
->location(), "unknown field %qs in %qs",
11785 Gogo::message_name(name
).c_str(),
11786 (type
->named_type() != NULL
11787 ? type
->named_type()->message_name().c_str()
11788 : "unnamed struct"));
11789 return Expression::make_error(location
);
11791 if (vals
[index
] != NULL
)
11793 error_at(name_expr
->location(),
11794 "duplicate value for field %qs in %qs",
11795 Gogo::message_name(name
).c_str(),
11796 (type
->named_type() != NULL
11797 ? type
->named_type()->message_name().c_str()
11798 : "unnamed struct"));
11799 return Expression::make_error(location
);
11805 Expression_list
* list
= new Expression_list
;
11806 list
->reserve(field_count
);
11807 for (size_t i
= 0; i
< field_count
; ++i
)
11808 list
->push_back(vals
[i
]);
11810 return new Struct_construction_expression(type
, list
, location
);
11813 // Lower an array composite literal.
11816 Composite_literal_expression::lower_array(Type
* type
)
11818 source_location location
= this->location();
11819 if (this->vals_
== NULL
|| !this->has_keys_
)
11820 return this->make_array(type
, this->vals_
);
11822 std::vector
<Expression
*> vals
;
11823 vals
.reserve(this->vals_
->size());
11824 unsigned long index
= 0;
11825 Expression_list::const_iterator p
= this->vals_
->begin();
11826 while (p
!= this->vals_
->end())
11828 Expression
* index_expr
= *p
;
11831 gcc_assert(p
!= this->vals_
->end());
11832 Expression
* val
= *p
;
11836 if (index_expr
!= NULL
)
11841 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
11844 error_at(index_expr
->location(),
11845 "index expression is not integer constant");
11846 return Expression::make_error(location
);
11848 if (mpz_sgn(ival
) < 0)
11851 error_at(index_expr
->location(), "index expression is negative");
11852 return Expression::make_error(location
);
11854 index
= mpz_get_ui(ival
);
11855 if (mpz_cmp_ui(ival
, index
) != 0)
11858 error_at(index_expr
->location(), "index value overflow");
11859 return Expression::make_error(location
);
11864 if (index
== vals
.size())
11865 vals
.push_back(val
);
11868 if (index
> vals
.size())
11870 vals
.reserve(index
+ 32);
11871 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
11873 if (vals
[index
] != NULL
)
11875 error_at((index_expr
!= NULL
11876 ? index_expr
->location()
11877 : val
->location()),
11878 "duplicate value for index %lu",
11880 return Expression::make_error(location
);
11888 size_t size
= vals
.size();
11889 Expression_list
* list
= new Expression_list
;
11890 list
->reserve(size
);
11891 for (size_t i
= 0; i
< size
; ++i
)
11892 list
->push_back(vals
[i
]);
11894 return this->make_array(type
, list
);
11897 // Actually build the array composite literal. This handles
11901 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
11903 source_location location
= this->location();
11904 Array_type
* at
= type
->array_type();
11905 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
11907 size_t size
= vals
== NULL
? 0 : vals
->size();
11909 mpz_init_set_ui(vlen
, size
);
11910 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
11912 at
= Type::make_array_type(at
->element_type(), elen
);
11915 if (at
->length() != NULL
)
11916 return new Fixed_array_construction_expression(type
, vals
, location
);
11918 return new Open_array_construction_expression(type
, vals
, location
);
11921 // Lower a map composite literal.
11924 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
11927 source_location location
= this->location();
11928 if (this->vals_
!= NULL
)
11930 if (!this->has_keys_
)
11932 error_at(location
, "map composite literal must have keys");
11933 return Expression::make_error(location
);
11936 for (Expression_list::iterator p
= this->vals_
->begin();
11937 p
!= this->vals_
->end();
11943 error_at((*p
)->location(),
11944 "map composite literal must have keys for every value");
11945 return Expression::make_error(location
);
11947 // Make sure we have lowered the key; it may not have been
11948 // lowered in order to handle keys for struct composite
11949 // literals. Lower it now to get the right error message.
11950 if ((*p
)->unknown_expression() != NULL
)
11952 (*p
)->unknown_expression()->clear_is_composite_literal_key();
11953 gogo
->lower_expression(function
, &*p
);
11954 gcc_assert((*p
)->is_error_expression());
11955 return Expression::make_error(location
);
11960 return new Map_construction_expression(type
, this->vals_
, location
);
11963 // Make a composite literal expression.
11966 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
11967 Expression_list
* vals
,
11968 source_location location
)
11970 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
11974 // Return whether this expression is a composite literal.
11977 Expression::is_composite_literal() const
11979 switch (this->classification_
)
11981 case EXPRESSION_COMPOSITE_LITERAL
:
11982 case EXPRESSION_STRUCT_CONSTRUCTION
:
11983 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11984 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11985 case EXPRESSION_MAP_CONSTRUCTION
:
11992 // Return whether this expression is a composite literal which is not
11996 Expression::is_nonconstant_composite_literal() const
11998 switch (this->classification_
)
12000 case EXPRESSION_STRUCT_CONSTRUCTION
:
12002 const Struct_construction_expression
*psce
=
12003 static_cast<const Struct_construction_expression
*>(this);
12004 return !psce
->is_constant_struct();
12006 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
12008 const Fixed_array_construction_expression
*pace
=
12009 static_cast<const Fixed_array_construction_expression
*>(this);
12010 return !pace
->is_constant_array();
12012 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
12014 const Open_array_construction_expression
*pace
=
12015 static_cast<const Open_array_construction_expression
*>(this);
12016 return !pace
->is_constant_array();
12018 case EXPRESSION_MAP_CONSTRUCTION
:
12025 // Return true if this is a reference to a local variable.
12028 Expression::is_local_variable() const
12030 const Var_expression
* ve
= this->var_expression();
12033 const Named_object
* no
= ve
->named_object();
12034 return (no
->is_result_variable()
12035 || (no
->is_variable() && !no
->var_value()->is_global()));
12038 // Class Type_guard_expression.
12043 Type_guard_expression::do_traverse(Traverse
* traverse
)
12045 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
12046 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
12047 return TRAVERSE_EXIT
;
12048 return TRAVERSE_CONTINUE
;
12051 // Check types of a type guard expression. The expression must have
12052 // an interface type, but the actual type conversion is checked at run
12056 Type_guard_expression::do_check_types(Gogo
*)
12058 // 6g permits using a type guard with unsafe.pointer; we are
12060 Type
* expr_type
= this->expr_
->type();
12061 if (expr_type
->is_unsafe_pointer_type())
12063 if (this->type_
->points_to() == NULL
12064 && (this->type_
->integer_type() == NULL
12065 || (this->type_
->forwarded()
12066 != Type::lookup_integer_type("uintptr"))))
12067 this->report_error(_("invalid unsafe.Pointer conversion"));
12069 else if (this->type_
->is_unsafe_pointer_type())
12071 if (expr_type
->points_to() == NULL
12072 && (expr_type
->integer_type() == NULL
12073 || (expr_type
->forwarded()
12074 != Type::lookup_integer_type("uintptr"))))
12075 this->report_error(_("invalid unsafe.Pointer conversion"));
12077 else if (expr_type
->interface_type() == NULL
)
12079 if (!expr_type
->is_error_type() && !this->type_
->is_error_type())
12080 this->report_error(_("type assertion only valid for interface types"));
12081 this->set_is_error();
12083 else if (this->type_
->interface_type() == NULL
)
12085 std::string reason
;
12086 if (!expr_type
->interface_type()->implements_interface(this->type_
,
12089 if (!this->type_
->is_error_type())
12091 if (reason
.empty())
12092 this->report_error(_("impossible type assertion: "
12093 "type does not implement interface"));
12095 error_at(this->location(),
12096 ("impossible type assertion: "
12097 "type does not implement interface (%s)"),
12100 this->set_is_error();
12105 // Return a tree for a type guard expression.
12108 Type_guard_expression::do_get_tree(Translate_context
* context
)
12110 Gogo
* gogo
= context
->gogo();
12111 tree expr_tree
= this->expr_
->get_tree(context
);
12112 if (expr_tree
== error_mark_node
)
12113 return error_mark_node
;
12114 Type
* expr_type
= this->expr_
->type();
12115 if ((this->type_
->is_unsafe_pointer_type()
12116 && (expr_type
->points_to() != NULL
12117 || expr_type
->integer_type() != NULL
))
12118 || (expr_type
->is_unsafe_pointer_type()
12119 && this->type_
->points_to() != NULL
))
12120 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
12121 else if (expr_type
->is_unsafe_pointer_type()
12122 && this->type_
->integer_type() != NULL
)
12123 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
12124 else if (this->type_
->interface_type() != NULL
)
12125 return Expression::convert_interface_to_interface(context
, this->type_
,
12126 this->expr_
->type(),
12130 return Expression::convert_for_assignment(context
, this->type_
,
12131 this->expr_
->type(), expr_tree
,
12135 // Make a type guard expression.
12138 Expression::make_type_guard(Expression
* expr
, Type
* type
,
12139 source_location location
)
12141 return new Type_guard_expression(expr
, type
, location
);
12144 // Class Heap_composite_expression.
12146 // When you take the address of a composite literal, it is allocated
12147 // on the heap. This class implements that.
12149 class Heap_composite_expression
: public Expression
12152 Heap_composite_expression(Expression
* expr
, source_location location
)
12153 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
12159 do_traverse(Traverse
* traverse
)
12160 { return Expression::traverse(&this->expr_
, traverse
); }
12164 { return Type::make_pointer_type(this->expr_
->type()); }
12167 do_determine_type(const Type_context
*)
12168 { this->expr_
->determine_type_no_context(); }
12173 return Expression::make_heap_composite(this->expr_
->copy(),
12178 do_get_tree(Translate_context
*);
12180 // We only export global objects, and the parser does not generate
12181 // this in global scope.
12183 do_export(Export
*) const
12184 { gcc_unreachable(); }
12187 // The composite literal which is being put on the heap.
12191 // Return a tree which allocates a composite literal on the heap.
12194 Heap_composite_expression::do_get_tree(Translate_context
* context
)
12196 tree expr_tree
= this->expr_
->get_tree(context
);
12197 if (expr_tree
== error_mark_node
)
12198 return error_mark_node
;
12199 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
12200 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
12201 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
12202 expr_size
, this->location());
12203 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
12204 space
= save_expr(space
);
12205 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
12206 TREE_THIS_NOTRAP(ref
) = 1;
12207 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
12208 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
12210 SET_EXPR_LOCATION(ret
, this->location());
12214 // Allocate a composite literal on the heap.
12217 Expression::make_heap_composite(Expression
* expr
, source_location location
)
12219 return new Heap_composite_expression(expr
, location
);
12222 // Class Receive_expression.
12224 // Return the type of a receive expression.
12227 Receive_expression::do_type()
12229 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12230 if (channel_type
== NULL
)
12231 return Type::make_error_type();
12232 return channel_type
->element_type();
12235 // Check types for a receive expression.
12238 Receive_expression::do_check_types(Gogo
*)
12240 Type
* type
= this->channel_
->type();
12241 if (type
->is_error_type())
12243 this->set_is_error();
12246 if (type
->channel_type() == NULL
)
12248 this->report_error(_("expected channel"));
12251 if (!type
->channel_type()->may_receive())
12253 this->report_error(_("invalid receive on send-only channel"));
12258 // Get a tree for a receive expression.
12261 Receive_expression::do_get_tree(Translate_context
* context
)
12263 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12264 gcc_assert(channel_type
!= NULL
);
12265 Type
* element_type
= channel_type
->element_type();
12266 tree element_type_tree
= element_type
->get_tree(context
->gogo());
12268 tree channel
= this->channel_
->get_tree(context
);
12269 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
12270 return error_mark_node
;
12272 return Gogo::receive_from_channel(element_type_tree
, channel
,
12273 this->for_select_
, this->location());
12276 // Make a receive expression.
12278 Receive_expression
*
12279 Expression::make_receive(Expression
* channel
, source_location location
)
12281 return new Receive_expression(channel
, location
);
12284 // Class Send_expression.
12289 Send_expression::do_traverse(Traverse
* traverse
)
12291 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
12292 return TRAVERSE_EXIT
;
12293 return Expression::traverse(&this->val_
, traverse
);
12299 Send_expression::do_type()
12301 return Type::lookup_bool_type();
12307 Send_expression::do_determine_type(const Type_context
*)
12309 this->channel_
->determine_type_no_context();
12311 Type
* type
= this->channel_
->type();
12312 Type_context subcontext
;
12313 if (type
->channel_type() != NULL
)
12314 subcontext
.type
= type
->channel_type()->element_type();
12315 this->val_
->determine_type(&subcontext
);
12321 Send_expression::do_check_types(Gogo
*)
12323 Type
* type
= this->channel_
->type();
12324 if (type
->is_error_type())
12326 this->set_is_error();
12329 Channel_type
* channel_type
= type
->channel_type();
12330 if (channel_type
== NULL
)
12332 error_at(this->location(), "left operand of %<<-%> must be channel");
12333 this->set_is_error();
12336 Type
* element_type
= channel_type
->element_type();
12337 if (element_type
!= NULL
12338 && !Type::are_assignable(element_type
, this->val_
->type(), NULL
))
12340 this->report_error(_("incompatible types in send"));
12343 if (!channel_type
->may_send())
12345 this->report_error(_("invalid send on receive-only channel"));
12350 // Get a tree for a send expression.
12353 Send_expression::do_get_tree(Translate_context
* context
)
12355 tree channel
= this->channel_
->get_tree(context
);
12356 tree val
= this->val_
->get_tree(context
);
12357 if (channel
== error_mark_node
|| val
== error_mark_node
)
12358 return error_mark_node
;
12359 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12360 val
= Expression::convert_for_assignment(context
,
12361 channel_type
->element_type(),
12362 this->val_
->type(),
12365 return Gogo::send_on_channel(channel
, val
, this->is_value_discarded_
,
12366 this->for_select_
, this->location());
12369 // Make a send expression
12372 Expression::make_send(Expression
* channel
, Expression
* val
,
12373 source_location location
)
12375 return new Send_expression(channel
, val
, location
);
12378 // An expression which evaluates to a pointer to the type descriptor
12381 class Type_descriptor_expression
: public Expression
12384 Type_descriptor_expression(Type
* type
, source_location location
)
12385 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
12392 { return Type::make_type_descriptor_ptr_type(); }
12395 do_determine_type(const Type_context
*)
12403 do_get_tree(Translate_context
* context
)
12404 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
12407 // The type for which this is the descriptor.
12411 // Make a type descriptor expression.
12414 Expression::make_type_descriptor(Type
* type
, source_location location
)
12416 return new Type_descriptor_expression(type
, location
);
12419 // An expression which evaluates to some characteristic of a type.
12420 // This is only used to initialize fields of a type descriptor. Using
12421 // a new expression class is slightly inefficient but gives us a good
12422 // separation between the frontend and the middle-end with regard to
12423 // how types are laid out.
12425 class Type_info_expression
: public Expression
12428 Type_info_expression(Type
* type
, Type_info type_info
)
12429 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
12430 type_(type
), type_info_(type_info
)
12438 do_determine_type(const Type_context
*)
12446 do_get_tree(Translate_context
* context
);
12449 // The type for which we are getting information.
12451 // What information we want.
12452 Type_info type_info_
;
12455 // The type is chosen to match what the type descriptor struct
12459 Type_info_expression::do_type()
12461 switch (this->type_info_
)
12463 case TYPE_INFO_SIZE
:
12464 return Type::lookup_integer_type("uintptr");
12465 case TYPE_INFO_ALIGNMENT
:
12466 case TYPE_INFO_FIELD_ALIGNMENT
:
12467 return Type::lookup_integer_type("uint8");
12473 // Return type information in GENERIC.
12476 Type_info_expression::do_get_tree(Translate_context
* context
)
12478 tree type_tree
= this->type_
->get_tree(context
->gogo());
12479 if (type_tree
== error_mark_node
)
12480 return error_mark_node
;
12482 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12483 gcc_assert(val_type_tree
!= error_mark_node
);
12485 if (this->type_info_
== TYPE_INFO_SIZE
)
12486 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12487 TYPE_SIZE_UNIT(type_tree
));
12491 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12492 val
= go_type_alignment(type_tree
);
12494 val
= go_field_alignment(type_tree
);
12495 return build_int_cstu(val_type_tree
, val
);
12499 // Make a type info expression.
12502 Expression::make_type_info(Type
* type
, Type_info type_info
)
12504 return new Type_info_expression(type
, type_info
);
12507 // An expression which evaluates to the offset of a field within a
12508 // struct. This, like Type_info_expression, q.v., is only used to
12509 // initialize fields of a type descriptor.
12511 class Struct_field_offset_expression
: public Expression
12514 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12515 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12516 type_(type
), field_(field
)
12522 { return Type::lookup_integer_type("uintptr"); }
12525 do_determine_type(const Type_context
*)
12533 do_get_tree(Translate_context
* context
);
12536 // The type of the struct.
12537 Struct_type
* type_
;
12539 const Struct_field
* field_
;
12542 // Return a struct field offset in GENERIC.
12545 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12547 tree type_tree
= this->type_
->get_tree(context
->gogo());
12548 if (type_tree
== error_mark_node
)
12549 return error_mark_node
;
12551 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12552 gcc_assert(val_type_tree
!= error_mark_node
);
12554 const Struct_field_list
* fields
= this->type_
->fields();
12555 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12556 Struct_field_list::const_iterator p
;
12557 for (p
= fields
->begin();
12558 p
!= fields
->end();
12559 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12561 gcc_assert(struct_field_tree
!= NULL_TREE
);
12562 if (&*p
== this->field_
)
12565 gcc_assert(&*p
== this->field_
);
12567 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12568 byte_position(struct_field_tree
));
12571 // Make an expression for a struct field offset.
12574 Expression::make_struct_field_offset(Struct_type
* type
,
12575 const Struct_field
* field
)
12577 return new Struct_field_offset_expression(type
, field
);
12580 // An expression which evaluates to the address of an unnamed label.
12582 class Label_addr_expression
: public Expression
12585 Label_addr_expression(Label
* label
, source_location location
)
12586 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12593 { return Type::make_pointer_type(Type::make_void_type()); }
12596 do_determine_type(const Type_context
*)
12601 { return new Label_addr_expression(this->label_
, this->location()); }
12604 do_get_tree(Translate_context
*)
12605 { return this->label_
->get_addr(this->location()); }
12608 // The label whose address we are taking.
12612 // Make an expression for the address of an unnamed label.
12615 Expression::make_label_addr(Label
* label
, source_location location
)
12617 return new Label_addr_expression(label
, location
);
12620 // Import an expression. This comes at the end in order to see the
12621 // various class definitions.
12624 Expression::import_expression(Import
* imp
)
12626 int c
= imp
->peek_char();
12627 if (imp
->match_c_string("- ")
12628 || imp
->match_c_string("! ")
12629 || imp
->match_c_string("^ "))
12630 return Unary_expression::do_import(imp
);
12632 return Binary_expression::do_import(imp
);
12633 else if (imp
->match_c_string("true")
12634 || imp
->match_c_string("false"))
12635 return Boolean_expression::do_import(imp
);
12637 return String_expression::do_import(imp
);
12638 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12640 // This handles integers, floats and complex constants.
12641 return Integer_expression::do_import(imp
);
12643 else if (imp
->match_c_string("nil"))
12644 return Nil_expression::do_import(imp
);
12645 else if (imp
->match_c_string("convert"))
12646 return Type_conversion_expression::do_import(imp
);
12649 error_at(imp
->location(), "import error: expected expression");
12650 return Expression::make_error(imp
->location());
12654 // Class Expression_list.
12656 // Traverse the list.
12659 Expression_list::traverse(Traverse
* traverse
)
12661 for (Expression_list::iterator p
= this->begin();
12667 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12668 return TRAVERSE_EXIT
;
12671 return TRAVERSE_CONTINUE
;
12677 Expression_list::copy()
12679 Expression_list
* ret
= new Expression_list();
12680 for (Expression_list::iterator p
= this->begin();
12685 ret
->push_back(NULL
);
12687 ret
->push_back((*p
)->copy());
12692 // Return whether an expression list has an error expression.
12695 Expression_list::contains_error() const
12697 for (Expression_list::const_iterator p
= this->begin();
12700 if (*p
!= NULL
&& (*p
)->is_error_expression())