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"
37 #include "expressions.h"
41 Expression::Expression(Expression_classification classification
,
42 source_location location
)
43 : classification_(classification
), location_(location
)
47 Expression::~Expression()
51 // If this expression has a constant integer value, return it.
54 Expression::integer_constant_value(bool iota_is_constant
, mpz_t val
,
58 return this->do_integer_constant_value(iota_is_constant
, val
, ptype
);
61 // If this expression has a constant floating point value, return it.
64 Expression::float_constant_value(mpfr_t val
, Type
** ptype
) const
67 if (this->do_float_constant_value(val
, ptype
))
73 if (!this->do_integer_constant_value(false, ival
, &t
))
77 mpfr_set_z(val
, ival
, GMP_RNDN
);
84 // If this expression has a constant complex value, return it.
87 Expression::complex_constant_value(mpfr_t real
, mpfr_t imag
,
91 if (this->do_complex_constant_value(real
, imag
, ptype
))
94 if (this->float_constant_value(real
, &t
))
96 mpfr_set_ui(imag
, 0, GMP_RNDN
);
102 // Traverse the expressions.
105 Expression::traverse(Expression
** pexpr
, Traverse
* traverse
)
107 Expression
* expr
= *pexpr
;
108 if ((traverse
->traverse_mask() & Traverse::traverse_expressions
) != 0)
110 int t
= traverse
->expression(pexpr
);
111 if (t
== TRAVERSE_EXIT
)
112 return TRAVERSE_EXIT
;
113 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
114 return TRAVERSE_CONTINUE
;
116 return expr
->do_traverse(traverse
);
119 // Traverse subexpressions of this expression.
122 Expression::traverse_subexpressions(Traverse
* traverse
)
124 return this->do_traverse(traverse
);
127 // Default implementation for do_traverse for child classes.
130 Expression::do_traverse(Traverse
*)
132 return TRAVERSE_CONTINUE
;
135 // This virtual function is called by the parser if the value of this
136 // expression is being discarded. By default, we warn. Expressions
137 // with side effects override.
140 Expression::do_discarding_value()
142 this->warn_about_unused_value();
145 // This virtual function is called to export expressions. This will
146 // only be used by expressions which may be constant.
149 Expression::do_export(Export
*) const
154 // Warn that the value of the expression is not used.
157 Expression::warn_about_unused_value()
159 warning_at(this->location(), OPT_Wunused_value
, "value computed is not used");
162 // Note that this expression is an error. This is called by children
163 // when they discover an error.
166 Expression::set_is_error()
168 this->classification_
= EXPRESSION_ERROR
;
171 // For children to call to report an error conveniently.
174 Expression::report_error(const char* msg
)
176 error_at(this->location_
, "%s", msg
);
177 this->set_is_error();
180 // Set types of variables and constants. This is implemented by the
184 Expression::determine_type(const Type_context
* context
)
186 this->do_determine_type(context
);
189 // Set types when there is no context.
192 Expression::determine_type_no_context()
194 Type_context context
;
195 this->do_determine_type(&context
);
198 // Return a tree handling any conversions which must be done during
202 Expression::convert_for_assignment(Translate_context
* context
, Type
* lhs_type
,
203 Type
* rhs_type
, tree rhs_tree
,
204 source_location location
)
206 if (lhs_type
== rhs_type
)
209 if (lhs_type
->is_error() || rhs_type
->is_error())
210 return error_mark_node
;
212 if (rhs_tree
== error_mark_node
|| TREE_TYPE(rhs_tree
) == error_mark_node
)
213 return error_mark_node
;
215 Gogo
* gogo
= context
->gogo();
217 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
218 if (lhs_type_tree
== error_mark_node
)
219 return error_mark_node
;
221 if (lhs_type
->interface_type() != NULL
)
223 if (rhs_type
->interface_type() == NULL
)
224 return Expression::convert_type_to_interface(context
, lhs_type
,
228 return Expression::convert_interface_to_interface(context
, lhs_type
,
232 else if (rhs_type
->interface_type() != NULL
)
233 return Expression::convert_interface_to_type(context
, lhs_type
, rhs_type
,
235 else if (lhs_type
->is_open_array_type()
236 && rhs_type
->is_nil_type())
238 // Assigning nil to an open array.
239 gcc_assert(TREE_CODE(lhs_type_tree
) == RECORD_TYPE
);
241 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
243 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
244 tree field
= TYPE_FIELDS(lhs_type_tree
);
245 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
248 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
250 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
251 field
= DECL_CHAIN(field
);
252 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
255 elt
->value
= fold_convert(TREE_TYPE(field
), integer_zero_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 tree val
= build_constructor(lhs_type_tree
, init
);
265 TREE_CONSTANT(val
) = 1;
269 else if (rhs_type
->is_nil_type())
271 // The left hand side should be a pointer type at the tree
273 gcc_assert(POINTER_TYPE_P(lhs_type_tree
));
274 return fold_convert(lhs_type_tree
, null_pointer_node
);
276 else if (lhs_type_tree
== TREE_TYPE(rhs_tree
))
278 // No conversion is needed.
281 else if (POINTER_TYPE_P(lhs_type_tree
)
282 || INTEGRAL_TYPE_P(lhs_type_tree
)
283 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
284 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
285 return fold_convert_loc(location
, lhs_type_tree
, rhs_tree
);
286 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
287 && TREE_CODE(TREE_TYPE(rhs_tree
)) == RECORD_TYPE
)
289 // This conversion must be permitted by Go, or we wouldn't have
291 gcc_assert(int_size_in_bytes(lhs_type_tree
)
292 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
293 return fold_build1_loc(location
, VIEW_CONVERT_EXPR
, lhs_type_tree
,
298 gcc_assert(useless_type_conversion_p(lhs_type_tree
, TREE_TYPE(rhs_tree
)));
303 // Return a tree for a conversion from a non-interface type to an
307 Expression::convert_type_to_interface(Translate_context
* context
,
308 Type
* lhs_type
, Type
* rhs_type
,
309 tree rhs_tree
, source_location location
)
311 Gogo
* gogo
= context
->gogo();
312 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
313 bool lhs_is_empty
= lhs_interface_type
->is_empty();
315 // Since RHS_TYPE is a static type, we can create the interface
316 // method table at compile time.
318 // When setting an interface to nil, we just set both fields to
320 if (rhs_type
->is_nil_type())
321 return lhs_type
->get_init_tree(gogo
, false);
323 // This should have been checked already.
324 gcc_assert(lhs_interface_type
->implements_interface(rhs_type
, NULL
));
326 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
327 if (lhs_type_tree
== error_mark_node
)
328 return error_mark_node
;
330 // An interface is a tuple. If LHS_TYPE is an empty interface type,
331 // then the first field is the type descriptor for RHS_TYPE.
332 // Otherwise it is the interface method table for RHS_TYPE.
333 tree first_field_value
;
335 first_field_value
= rhs_type
->type_descriptor_pointer(gogo
);
338 // Build the interface method table for this interface and this
339 // object type: a list of function pointers for each interface
341 Named_type
* rhs_named_type
= rhs_type
->named_type();
342 bool is_pointer
= false;
343 if (rhs_named_type
== NULL
)
345 rhs_named_type
= rhs_type
->deref()->named_type();
349 if (rhs_named_type
== NULL
)
350 method_table
= null_pointer_node
;
353 rhs_named_type
->interface_method_table(gogo
, lhs_interface_type
,
355 first_field_value
= fold_convert_loc(location
, const_ptr_type_node
,
358 if (first_field_value
== error_mark_node
)
359 return error_mark_node
;
361 // Start building a constructor for the value we will return.
363 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
365 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
366 tree field
= TYPE_FIELDS(lhs_type_tree
);
367 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
368 (lhs_is_empty
? "__type_descriptor" : "__methods")) == 0);
370 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), first_field_value
);
372 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
373 field
= DECL_CHAIN(field
);
374 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
377 if (rhs_type
->points_to() != NULL
)
379 // We are assigning a pointer to the interface; the interface
380 // holds the pointer itself.
381 elt
->value
= rhs_tree
;
382 return build_constructor(lhs_type_tree
, init
);
385 // We are assigning a non-pointer value to the interface; the
386 // interface gets a copy of the value in the heap.
388 tree object_size
= TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree
));
390 tree space
= gogo
->allocate_memory(rhs_type
, object_size
, location
);
391 space
= fold_convert_loc(location
, build_pointer_type(TREE_TYPE(rhs_tree
)),
393 space
= save_expr(space
);
395 tree ref
= build_fold_indirect_ref_loc(location
, space
);
396 TREE_THIS_NOTRAP(ref
) = 1;
397 tree set
= fold_build2_loc(location
, MODIFY_EXPR
, void_type_node
,
400 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), space
);
402 return build2(COMPOUND_EXPR
, lhs_type_tree
, set
,
403 build_constructor(lhs_type_tree
, init
));
406 // Return a tree for the type descriptor of RHS_TREE, which has
407 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
411 Expression::get_interface_type_descriptor(Translate_context
*,
412 Type
* rhs_type
, tree rhs_tree
,
413 source_location location
)
415 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
416 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
417 tree rhs_field
= TYPE_FIELDS(rhs_type_tree
);
418 tree v
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
420 if (rhs_type
->interface_type()->is_empty())
422 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)),
423 "__type_descriptor") == 0);
427 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__methods")
429 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v
)));
431 tree v1
= build_fold_indirect_ref_loc(location
, v
);
432 gcc_assert(TREE_CODE(TREE_TYPE(v1
)) == RECORD_TYPE
);
433 tree f
= TYPE_FIELDS(TREE_TYPE(v1
));
434 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f
)), "__type_descriptor")
436 v1
= build3(COMPONENT_REF
, TREE_TYPE(f
), v1
, f
, NULL_TREE
);
438 tree eq
= fold_build2_loc(location
, EQ_EXPR
, boolean_type_node
, v
,
439 fold_convert_loc(location
, TREE_TYPE(v
),
441 tree n
= fold_convert_loc(location
, TREE_TYPE(v1
), null_pointer_node
);
442 return fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(v1
),
446 // Return a tree for the conversion of an interface type to an
450 Expression::convert_interface_to_interface(Translate_context
* context
,
451 Type
*lhs_type
, Type
*rhs_type
,
452 tree rhs_tree
, bool for_type_guard
,
453 source_location location
)
455 Gogo
* gogo
= context
->gogo();
456 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
457 bool lhs_is_empty
= lhs_interface_type
->is_empty();
459 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
460 if (lhs_type_tree
== error_mark_node
)
461 return error_mark_node
;
463 // In the general case this requires runtime examination of the type
464 // method table to match it up with the interface methods.
466 // FIXME: If all of the methods in the right hand side interface
467 // also appear in the left hand side interface, then we don't need
468 // to do a runtime check, although we still need to build a new
471 // Get the type descriptor for the right hand side. This will be
472 // NULL for a nil interface.
474 if (!DECL_P(rhs_tree
))
475 rhs_tree
= save_expr(rhs_tree
);
477 tree rhs_type_descriptor
=
478 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
481 // The result is going to be a two element constructor.
483 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
485 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
486 tree field
= TYPE_FIELDS(lhs_type_tree
);
491 // A type assertion fails when converting a nil interface.
492 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
493 static tree assert_interface_decl
;
494 tree call
= Gogo::call_builtin(&assert_interface_decl
,
496 "__go_assert_interface",
499 TREE_TYPE(lhs_type_descriptor
),
501 TREE_TYPE(rhs_type_descriptor
),
502 rhs_type_descriptor
);
503 if (call
== error_mark_node
)
504 return error_mark_node
;
505 // This will panic if the interface conversion fails.
506 TREE_NOTHROW(assert_interface_decl
) = 0;
507 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
509 else if (lhs_is_empty
)
511 // A convertion to an empty interface always succeeds, and the
512 // first field is just the type descriptor of the object.
513 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
514 "__type_descriptor") == 0);
515 gcc_assert(TREE_TYPE(field
) == TREE_TYPE(rhs_type_descriptor
));
516 elt
->value
= rhs_type_descriptor
;
520 // A conversion to a non-empty interface may fail, but unlike a
521 // type assertion converting nil will always succeed.
522 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods")
524 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
525 static tree convert_interface_decl
;
526 tree call
= Gogo::call_builtin(&convert_interface_decl
,
528 "__go_convert_interface",
531 TREE_TYPE(lhs_type_descriptor
),
533 TREE_TYPE(rhs_type_descriptor
),
534 rhs_type_descriptor
);
535 if (call
== error_mark_node
)
536 return error_mark_node
;
537 // This will panic if the interface conversion fails.
538 TREE_NOTHROW(convert_interface_decl
) = 0;
539 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
542 // The second field is simply the object pointer.
544 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
545 field
= DECL_CHAIN(field
);
546 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
549 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
550 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
551 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
552 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
553 elt
->value
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
556 return build_constructor(lhs_type_tree
, init
);
559 // Return a tree for the conversion of an interface type to a
560 // non-interface type.
563 Expression::convert_interface_to_type(Translate_context
* context
,
564 Type
*lhs_type
, Type
* rhs_type
,
565 tree rhs_tree
, source_location location
)
567 Gogo
* gogo
= context
->gogo();
568 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
570 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
571 if (lhs_type_tree
== error_mark_node
)
572 return error_mark_node
;
574 // Call a function to check that the type is valid. The function
575 // will panic with an appropriate runtime type error if the type is
578 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
580 if (!DECL_P(rhs_tree
))
581 rhs_tree
= save_expr(rhs_tree
);
583 tree rhs_type_descriptor
=
584 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
587 tree rhs_inter_descriptor
= rhs_type
->type_descriptor_pointer(gogo
);
589 static tree check_interface_type_decl
;
590 tree call
= Gogo::call_builtin(&check_interface_type_decl
,
592 "__go_check_interface_type",
595 TREE_TYPE(lhs_type_descriptor
),
597 TREE_TYPE(rhs_type_descriptor
),
599 TREE_TYPE(rhs_inter_descriptor
),
600 rhs_inter_descriptor
);
601 if (call
== error_mark_node
)
602 return error_mark_node
;
603 // This call will panic if the conversion is invalid.
604 TREE_NOTHROW(check_interface_type_decl
) = 0;
606 // If the call succeeds, pull out the value.
607 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
608 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
609 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
610 tree val
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
613 // If the value is a pointer, then it is the value we want.
614 // Otherwise it points to the value.
615 if (lhs_type
->points_to() == NULL
)
617 val
= fold_convert_loc(location
, build_pointer_type(lhs_type_tree
), val
);
618 val
= build_fold_indirect_ref_loc(location
, val
);
621 return build2(COMPOUND_EXPR
, lhs_type_tree
, call
,
622 fold_convert_loc(location
, lhs_type_tree
, val
));
625 // Convert an expression to a tree. This is implemented by the child
626 // class. Not that it is not in general safe to call this multiple
627 // times for a single expression, but that we don't catch such errors.
630 Expression::get_tree(Translate_context
* context
)
632 // The child may have marked this expression as having an error.
633 if (this->classification_
== EXPRESSION_ERROR
)
634 return error_mark_node
;
636 return this->do_get_tree(context
);
639 // Return a tree for VAL in TYPE.
642 Expression::integer_constant_tree(mpz_t val
, tree type
)
644 if (type
== error_mark_node
)
645 return error_mark_node
;
646 else if (TREE_CODE(type
) == INTEGER_TYPE
)
647 return double_int_to_tree(type
,
648 mpz_get_double_int(type
, val
, true));
649 else if (TREE_CODE(type
) == REAL_TYPE
)
652 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
653 tree ret
= Expression::float_constant_tree(fval
, type
);
657 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
660 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
661 tree real
= Expression::float_constant_tree(fval
, TREE_TYPE(type
));
663 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
665 return build_complex(type
, real
, imag
);
671 // Return a tree for VAL in TYPE.
674 Expression::float_constant_tree(mpfr_t val
, tree type
)
676 if (type
== error_mark_node
)
677 return error_mark_node
;
678 else if (TREE_CODE(type
) == INTEGER_TYPE
)
682 mpfr_get_z(ival
, val
, GMP_RNDN
);
683 tree ret
= Expression::integer_constant_tree(ival
, type
);
687 else if (TREE_CODE(type
) == REAL_TYPE
)
690 real_from_mpfr(&r1
, val
, type
, GMP_RNDN
);
692 real_convert(&r2
, TYPE_MODE(type
), &r1
);
693 return build_real(type
, r2
);
695 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
698 real_from_mpfr(&r1
, val
, TREE_TYPE(type
), GMP_RNDN
);
700 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
701 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
703 return build_complex(type
, build_real(TREE_TYPE(type
), r2
), imag
);
709 // Return a tree for REAL/IMAG in TYPE.
712 Expression::complex_constant_tree(mpfr_t real
, mpfr_t imag
, tree type
)
714 if (type
== error_mark_node
)
715 return error_mark_node
;
716 else if (TREE_CODE(type
) == INTEGER_TYPE
|| TREE_CODE(type
) == REAL_TYPE
)
717 return Expression::float_constant_tree(real
, type
);
718 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
721 real_from_mpfr(&r1
, real
, TREE_TYPE(type
), GMP_RNDN
);
723 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
726 real_from_mpfr(&r3
, imag
, TREE_TYPE(type
), GMP_RNDN
);
728 real_convert(&r4
, TYPE_MODE(TREE_TYPE(type
)), &r3
);
730 return build_complex(type
, build_real(TREE_TYPE(type
), r2
),
731 build_real(TREE_TYPE(type
), r4
));
737 // Return a tree which evaluates to true if VAL, of arbitrary integer
738 // type, is negative or is more than the maximum value of BOUND_TYPE.
739 // If SOFAR is not NULL, it is or'red into the result. The return
740 // value may be NULL if SOFAR is NULL.
743 Expression::check_bounds(tree val
, tree bound_type
, tree sofar
,
746 tree val_type
= TREE_TYPE(val
);
747 tree ret
= NULL_TREE
;
749 if (!TYPE_UNSIGNED(val_type
))
751 ret
= fold_build2_loc(loc
, LT_EXPR
, boolean_type_node
, val
,
752 build_int_cst(val_type
, 0));
753 if (ret
== boolean_false_node
)
757 if ((TYPE_UNSIGNED(val_type
) && !TYPE_UNSIGNED(bound_type
))
758 || TYPE_SIZE(val_type
) > TYPE_SIZE(bound_type
))
760 tree max
= TYPE_MAX_VALUE(bound_type
);
761 tree big
= fold_build2_loc(loc
, GT_EXPR
, boolean_type_node
, val
,
762 fold_convert_loc(loc
, val_type
, max
));
763 if (big
== boolean_false_node
)
765 else if (ret
== NULL_TREE
)
768 ret
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
772 if (ret
== NULL_TREE
)
774 else if (sofar
== NULL_TREE
)
777 return fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
781 // Error expressions. This are used to avoid cascading errors.
783 class Error_expression
: public Expression
786 Error_expression(source_location location
)
787 : Expression(EXPRESSION_ERROR
, location
)
792 do_is_constant() const
796 do_integer_constant_value(bool, mpz_t val
, Type
**) const
803 do_float_constant_value(mpfr_t val
, Type
**) const
805 mpfr_set_ui(val
, 0, GMP_RNDN
);
810 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const
812 mpfr_set_ui(real
, 0, GMP_RNDN
);
813 mpfr_set_ui(imag
, 0, GMP_RNDN
);
818 do_discarding_value()
823 { return Type::make_error_type(); }
826 do_determine_type(const Type_context
*)
834 do_is_addressable() const
838 do_get_tree(Translate_context
*)
839 { return error_mark_node
; }
843 Expression::make_error(source_location location
)
845 return new Error_expression(location
);
848 // An expression which is really a type. This is used during parsing.
849 // It is an error if these survive after lowering.
852 Type_expression
: public Expression
855 Type_expression(Type
* type
, source_location location
)
856 : Expression(EXPRESSION_TYPE
, location
),
862 do_traverse(Traverse
* traverse
)
863 { return Type::traverse(this->type_
, traverse
); }
867 { return this->type_
; }
870 do_determine_type(const Type_context
*)
874 do_check_types(Gogo
*)
875 { this->report_error(_("invalid use of type")); }
882 do_get_tree(Translate_context
*)
883 { gcc_unreachable(); }
886 // The type which we are representing as an expression.
891 Expression::make_type(Type
* type
, source_location location
)
893 return new Type_expression(type
, location
);
896 // Class Parser_expression.
899 Parser_expression::do_type()
901 // We should never really ask for the type of a Parser_expression.
902 // However, it can happen, at least when we have an invalid const
903 // whose initializer refers to the const itself. In that case we
904 // may ask for the type when lowering the const itself.
905 gcc_assert(saw_errors());
906 return Type::make_error_type();
909 // Class Var_expression.
911 // Lower a variable expression. Here we just make sure that the
912 // initialization expression of the variable has been lowered. This
913 // ensures that we will be able to determine the type of the variable
917 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
919 if (this->variable_
->is_variable())
921 Variable
* var
= this->variable_
->var_value();
922 // This is either a local variable or a global variable. A
923 // reference to a variable which is local to an enclosing
924 // function will be a reference to a field in a closure.
925 if (var
->is_global())
927 var
->lower_init_expression(gogo
, function
);
932 // Return the type of a reference to a variable.
935 Var_expression::do_type()
937 if (this->variable_
->is_variable())
938 return this->variable_
->var_value()->type();
939 else if (this->variable_
->is_result_variable())
940 return this->variable_
->result_var_value()->type();
945 // Determine the type of a reference to a variable.
948 Var_expression::do_determine_type(const Type_context
*)
950 if (this->variable_
->is_variable())
951 this->variable_
->var_value()->determine_type();
954 // Something takes the address of this variable. This means that we
955 // may want to move the variable onto the heap.
958 Var_expression::do_address_taken(bool escapes
)
962 else if (this->variable_
->is_variable())
963 this->variable_
->var_value()->set_address_taken();
964 else if (this->variable_
->is_result_variable())
965 this->variable_
->result_var_value()->set_address_taken();
970 // Get the tree for a reference to a variable.
973 Var_expression::do_get_tree(Translate_context
* context
)
975 Bvariable
* bvar
= this->variable_
->get_backend_variable(context
->gogo(),
976 context
->function());
977 tree ret
= var_to_tree(bvar
);
978 if (ret
== error_mark_node
)
979 return error_mark_node
;
981 if (this->variable_
->is_variable())
982 is_in_heap
= this->variable_
->var_value()->is_in_heap();
983 else if (this->variable_
->is_result_variable())
984 is_in_heap
= this->variable_
->result_var_value()->is_in_heap();
989 ret
= build_fold_indirect_ref_loc(this->location(), ret
);
990 TREE_THIS_NOTRAP(ret
) = 1;
995 // Make a reference to a variable in an expression.
998 Expression::make_var_reference(Named_object
* var
, source_location location
)
1001 return Expression::make_sink(location
);
1003 // FIXME: Creating a new object for each reference to a variable is
1005 return new Var_expression(var
, location
);
1008 // Class Temporary_reference_expression.
1013 Temporary_reference_expression::do_type()
1015 return this->statement_
->type();
1018 // Called if something takes the address of this temporary variable.
1019 // We never have to move temporary variables to the heap, but we do
1020 // need to know that they must live in the stack rather than in a
1024 Temporary_reference_expression::do_address_taken(bool)
1026 this->statement_
->set_is_address_taken();
1029 // Get a tree referring to the variable.
1032 Temporary_reference_expression::do_get_tree(Translate_context
* context
)
1034 Bvariable
* bvar
= this->statement_
->get_backend_variable(context
);
1036 // The gcc backend can't represent the same set of recursive types
1037 // that the Go frontend can. In some cases this means that a
1038 // temporary variable won't have the right backend type. Correct
1039 // that here by adding a type cast. We need to use base() to push
1040 // the circularity down one level.
1041 tree ret
= var_to_tree(bvar
);
1042 if (POINTER_TYPE_P(TREE_TYPE(ret
)) && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret
))))
1044 tree type_tree
= this->type()->base()->get_tree(context
->gogo());
1045 ret
= fold_convert_loc(this->location(), type_tree
, ret
);
1050 // Make a reference to a temporary variable.
1053 Expression::make_temporary_reference(Temporary_statement
* statement
,
1054 source_location location
)
1056 return new Temporary_reference_expression(statement
, location
);
1059 // A sink expression--a use of the blank identifier _.
1061 class Sink_expression
: public Expression
1064 Sink_expression(source_location location
)
1065 : Expression(EXPRESSION_SINK
, location
),
1066 type_(NULL
), var_(NULL_TREE
)
1071 do_discarding_value()
1078 do_determine_type(const Type_context
*);
1082 { return new Sink_expression(this->location()); }
1085 do_get_tree(Translate_context
*);
1088 // The type of this sink variable.
1090 // The temporary variable we generate.
1094 // Return the type of a sink expression.
1097 Sink_expression::do_type()
1099 if (this->type_
== NULL
)
1100 return Type::make_sink_type();
1104 // Determine the type of a sink expression.
1107 Sink_expression::do_determine_type(const Type_context
* context
)
1109 if (context
->type
!= NULL
)
1110 this->type_
= context
->type
;
1113 // Return a temporary variable for a sink expression. This will
1114 // presumably be a write-only variable which the middle-end will drop.
1117 Sink_expression::do_get_tree(Translate_context
* context
)
1119 if (this->var_
== NULL_TREE
)
1121 gcc_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
1122 this->var_
= create_tmp_var(this->type_
->get_tree(context
->gogo()),
1128 // Make a sink expression.
1131 Expression::make_sink(source_location location
)
1133 return new Sink_expression(location
);
1136 // Class Func_expression.
1138 // FIXME: Can a function expression appear in a constant expression?
1139 // The value is unchanging. Initializing a constant to the address of
1140 // a function seems like it could work, though there might be little
1146 Func_expression::do_traverse(Traverse
* traverse
)
1148 return (this->closure_
== NULL
1150 : Expression::traverse(&this->closure_
, traverse
));
1153 // Return the type of a function expression.
1156 Func_expression::do_type()
1158 if (this->function_
->is_function())
1159 return this->function_
->func_value()->type();
1160 else if (this->function_
->is_function_declaration())
1161 return this->function_
->func_declaration_value()->type();
1166 // Get the tree for a function expression without evaluating the
1170 Func_expression::get_tree_without_closure(Gogo
* gogo
)
1172 Function_type
* fntype
;
1173 if (this->function_
->is_function())
1174 fntype
= this->function_
->func_value()->type();
1175 else if (this->function_
->is_function_declaration())
1176 fntype
= this->function_
->func_declaration_value()->type();
1180 // Builtin functions are handled specially by Call_expression. We
1181 // can't take their address.
1182 if (fntype
->is_builtin())
1184 error_at(this->location(), "invalid use of special builtin function %qs",
1185 this->function_
->name().c_str());
1186 return error_mark_node
;
1189 Named_object
* no
= this->function_
;
1191 tree id
= no
->get_id(gogo
);
1192 if (id
== error_mark_node
)
1193 return error_mark_node
;
1196 if (no
->is_function())
1197 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
, id
);
1198 else if (no
->is_function_declaration())
1199 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
, id
);
1203 if (fndecl
== error_mark_node
)
1204 return error_mark_node
;
1206 return build_fold_addr_expr_loc(this->location(), fndecl
);
1209 // Get the tree for a function expression. This is used when we take
1210 // the address of a function rather than simply calling it. If the
1211 // function has a closure, we must use a trampoline.
1214 Func_expression::do_get_tree(Translate_context
* context
)
1216 Gogo
* gogo
= context
->gogo();
1218 tree fnaddr
= this->get_tree_without_closure(gogo
);
1219 if (fnaddr
== error_mark_node
)
1220 return error_mark_node
;
1222 gcc_assert(TREE_CODE(fnaddr
) == ADDR_EXPR
1223 && TREE_CODE(TREE_OPERAND(fnaddr
, 0)) == FUNCTION_DECL
);
1224 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr
, 0)) = 1;
1226 // For a normal non-nested function call, that is all we have to do.
1227 if (!this->function_
->is_function()
1228 || this->function_
->func_value()->enclosing() == NULL
)
1230 gcc_assert(this->closure_
== NULL
);
1234 // For a nested function call, we have to always allocate a
1235 // trampoline. If we don't always allocate, then closures will not
1236 // be reliably distinct.
1237 Expression
* closure
= this->closure_
;
1239 if (closure
== NULL
)
1240 closure_tree
= null_pointer_node
;
1243 // Get the value of the closure. This will be a pointer to
1244 // space allocated on the heap.
1245 closure_tree
= closure
->get_tree(context
);
1246 if (closure_tree
== error_mark_node
)
1247 return error_mark_node
;
1248 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree
)));
1251 // Now we need to build some code on the heap. This code will load
1252 // the static chain pointer with the closure and then jump to the
1253 // body of the function. The normal gcc approach is to build the
1254 // code on the stack. Unfortunately we can not do that, as Go
1255 // permits us to return the function pointer.
1257 return gogo
->make_trampoline(fnaddr
, closure_tree
, this->location());
1260 // Make a reference to a function in an expression.
1263 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1264 source_location location
)
1266 return new Func_expression(function
, closure
, location
);
1269 // Class Unknown_expression.
1271 // Return the name of an unknown expression.
1274 Unknown_expression::name() const
1276 return this->named_object_
->name();
1279 // Lower a reference to an unknown name.
1282 Unknown_expression::do_lower(Gogo
*, Named_object
*, int)
1284 source_location location
= this->location();
1285 Named_object
* no
= this->named_object_
;
1287 if (!no
->is_unknown())
1291 real
= no
->unknown_value()->real_named_object();
1294 if (this->is_composite_literal_key_
)
1296 error_at(location
, "reference to undefined name %qs",
1297 this->named_object_
->message_name().c_str());
1298 return Expression::make_error(location
);
1301 switch (real
->classification())
1303 case Named_object::NAMED_OBJECT_CONST
:
1304 return Expression::make_const_reference(real
, location
);
1305 case Named_object::NAMED_OBJECT_TYPE
:
1306 return Expression::make_type(real
->type_value(), location
);
1307 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1308 if (this->is_composite_literal_key_
)
1310 error_at(location
, "reference to undefined type %qs",
1311 real
->message_name().c_str());
1312 return Expression::make_error(location
);
1313 case Named_object::NAMED_OBJECT_VAR
:
1314 return Expression::make_var_reference(real
, location
);
1315 case Named_object::NAMED_OBJECT_FUNC
:
1316 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1317 return Expression::make_func_reference(real
, NULL
, location
);
1318 case Named_object::NAMED_OBJECT_PACKAGE
:
1319 if (this->is_composite_literal_key_
)
1321 error_at(location
, "unexpected reference to package");
1322 return Expression::make_error(location
);
1328 // Make a reference to an unknown name.
1331 Expression::make_unknown_reference(Named_object
* no
, source_location location
)
1333 gcc_assert(no
->resolve()->is_unknown());
1334 return new Unknown_expression(no
, location
);
1337 // A boolean expression.
1339 class Boolean_expression
: public Expression
1342 Boolean_expression(bool val
, source_location location
)
1343 : Expression(EXPRESSION_BOOLEAN
, location
),
1344 val_(val
), type_(NULL
)
1352 do_is_constant() const
1359 do_determine_type(const Type_context
*);
1366 do_get_tree(Translate_context
*)
1367 { return this->val_
? boolean_true_node
: boolean_false_node
; }
1370 do_export(Export
* exp
) const
1371 { exp
->write_c_string(this->val_
? "true" : "false"); }
1376 // The type as determined by context.
1383 Boolean_expression::do_type()
1385 if (this->type_
== NULL
)
1386 this->type_
= Type::make_boolean_type();
1390 // Set the type from the context.
1393 Boolean_expression::do_determine_type(const Type_context
* context
)
1395 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1397 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1398 this->type_
= context
->type
;
1399 else if (!context
->may_be_abstract
)
1400 this->type_
= Type::lookup_bool_type();
1403 // Import a boolean constant.
1406 Boolean_expression::do_import(Import
* imp
)
1408 if (imp
->peek_char() == 't')
1410 imp
->require_c_string("true");
1411 return Expression::make_boolean(true, imp
->location());
1415 imp
->require_c_string("false");
1416 return Expression::make_boolean(false, imp
->location());
1420 // Make a boolean expression.
1423 Expression::make_boolean(bool val
, source_location location
)
1425 return new Boolean_expression(val
, location
);
1428 // Class String_expression.
1433 String_expression::do_type()
1435 if (this->type_
== NULL
)
1436 this->type_
= Type::make_string_type();
1440 // Set the type from the context.
1443 String_expression::do_determine_type(const Type_context
* context
)
1445 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1447 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1448 this->type_
= context
->type
;
1449 else if (!context
->may_be_abstract
)
1450 this->type_
= Type::lookup_string_type();
1453 // Build a string constant.
1456 String_expression::do_get_tree(Translate_context
* context
)
1458 return context
->gogo()->go_string_constant_tree(this->val_
);
1461 // Export a string expression.
1464 String_expression::do_export(Export
* exp
) const
1467 s
.reserve(this->val_
.length() * 4 + 2);
1469 for (std::string::const_iterator p
= this->val_
.begin();
1470 p
!= this->val_
.end();
1473 if (*p
== '\\' || *p
== '"')
1478 else if (*p
>= 0x20 && *p
< 0x7f)
1480 else if (*p
== '\n')
1482 else if (*p
== '\t')
1487 unsigned char c
= *p
;
1488 unsigned int dig
= c
>> 4;
1489 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1491 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1495 exp
->write_string(s
);
1498 // Import a string expression.
1501 String_expression::do_import(Import
* imp
)
1503 imp
->require_c_string("\"");
1507 int c
= imp
->get_char();
1508 if (c
== '"' || c
== -1)
1511 val
+= static_cast<char>(c
);
1514 c
= imp
->get_char();
1515 if (c
== '\\' || c
== '"')
1516 val
+= static_cast<char>(c
);
1523 c
= imp
->get_char();
1524 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1525 c
= imp
->get_char();
1526 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1527 char v
= (vh
<< 4) | vl
;
1532 error_at(imp
->location(), "bad string constant");
1533 return Expression::make_error(imp
->location());
1537 return Expression::make_string(val
, imp
->location());
1540 // Make a string expression.
1543 Expression::make_string(const std::string
& val
, source_location location
)
1545 return new String_expression(val
, location
);
1548 // Make an integer expression.
1550 class Integer_expression
: public Expression
1553 Integer_expression(const mpz_t
* val
, Type
* type
, source_location location
)
1554 : Expression(EXPRESSION_INTEGER
, location
),
1556 { mpz_init_set(this->val_
, *val
); }
1561 // Return whether VAL fits in the type.
1563 check_constant(mpz_t val
, Type
*, source_location
);
1565 // Write VAL to export data.
1567 export_integer(Export
* exp
, const mpz_t val
);
1571 do_is_constant() const
1575 do_integer_constant_value(bool, mpz_t val
, Type
** ptype
) const;
1581 do_determine_type(const Type_context
* context
);
1584 do_check_types(Gogo
*);
1587 do_get_tree(Translate_context
*);
1591 { return Expression::make_integer(&this->val_
, this->type_
,
1592 this->location()); }
1595 do_export(Export
*) const;
1598 // The integer value.
1604 // Return an integer constant value.
1607 Integer_expression::do_integer_constant_value(bool, mpz_t val
,
1610 if (this->type_
!= NULL
)
1611 *ptype
= this->type_
;
1612 mpz_set(val
, this->val_
);
1616 // Return the current type. If we haven't set the type yet, we return
1617 // an abstract integer type.
1620 Integer_expression::do_type()
1622 if (this->type_
== NULL
)
1623 this->type_
= Type::make_abstract_integer_type();
1627 // Set the type of the integer value. Here we may switch from an
1628 // abstract type to a real type.
1631 Integer_expression::do_determine_type(const Type_context
* context
)
1633 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1635 else if (context
->type
!= NULL
1636 && (context
->type
->integer_type() != NULL
1637 || context
->type
->float_type() != NULL
1638 || context
->type
->complex_type() != NULL
))
1639 this->type_
= context
->type
;
1640 else if (!context
->may_be_abstract
)
1641 this->type_
= Type::lookup_integer_type("int");
1644 // Return true if the integer VAL fits in the range of the type TYPE.
1645 // Otherwise give an error and return false. TYPE may be NULL.
1648 Integer_expression::check_constant(mpz_t val
, Type
* type
,
1649 source_location location
)
1653 Integer_type
* itype
= type
->integer_type();
1654 if (itype
== NULL
|| itype
->is_abstract())
1657 int bits
= mpz_sizeinbase(val
, 2);
1659 if (itype
->is_unsigned())
1661 // For an unsigned type we can only accept a nonnegative number,
1662 // and we must be able to represent at least BITS.
1663 if (mpz_sgn(val
) >= 0
1664 && bits
<= itype
->bits())
1669 // For a signed type we need an extra bit to indicate the sign.
1670 // We have to handle the most negative integer specially.
1671 if (bits
+ 1 <= itype
->bits()
1672 || (bits
<= itype
->bits()
1674 && (mpz_scan1(val
, 0)
1675 == static_cast<unsigned long>(itype
->bits() - 1))
1676 && mpz_scan0(val
, itype
->bits()) == ULONG_MAX
))
1680 error_at(location
, "integer constant overflow");
1684 // Check the type of an integer constant.
1687 Integer_expression::do_check_types(Gogo
*)
1689 if (this->type_
== NULL
)
1691 if (!Integer_expression::check_constant(this->val_
, this->type_
,
1693 this->set_is_error();
1696 // Get a tree for an integer constant.
1699 Integer_expression::do_get_tree(Translate_context
* context
)
1701 Gogo
* gogo
= context
->gogo();
1703 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1704 type
= this->type_
->get_tree(gogo
);
1705 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
1707 // We are converting to an abstract floating point type.
1708 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
1710 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
1712 // We are converting to an abstract complex type.
1713 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
1717 // If we still have an abstract type here, then this is being
1718 // used in a constant expression which didn't get reduced for
1719 // some reason. Use a type which will fit the value. We use <,
1720 // not <=, because we need an extra bit for the sign bit.
1721 int bits
= mpz_sizeinbase(this->val_
, 2);
1722 if (bits
< INT_TYPE_SIZE
)
1723 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
1725 type
= Type::lookup_integer_type("int64")->get_tree(gogo
);
1727 type
= long_long_integer_type_node
;
1729 return Expression::integer_constant_tree(this->val_
, type
);
1732 // Write VAL to export data.
1735 Integer_expression::export_integer(Export
* exp
, const mpz_t val
)
1737 char* s
= mpz_get_str(NULL
, 10, val
);
1738 exp
->write_c_string(s
);
1742 // Export an integer in a constant expression.
1745 Integer_expression::do_export(Export
* exp
) const
1747 Integer_expression::export_integer(exp
, this->val_
);
1748 // A trailing space lets us reliably identify the end of the number.
1749 exp
->write_c_string(" ");
1752 // Import an integer, floating point, or complex value. This handles
1753 // all these types because they all start with digits.
1756 Integer_expression::do_import(Import
* imp
)
1758 std::string num
= imp
->read_identifier();
1759 imp
->require_c_string(" ");
1760 if (!num
.empty() && num
[num
.length() - 1] == 'i')
1763 size_t plus_pos
= num
.find('+', 1);
1764 size_t minus_pos
= num
.find('-', 1);
1766 if (plus_pos
== std::string::npos
)
1768 else if (minus_pos
== std::string::npos
)
1772 error_at(imp
->location(), "bad number in import data: %qs",
1774 return Expression::make_error(imp
->location());
1776 if (pos
== std::string::npos
)
1777 mpfr_set_ui(real
, 0, GMP_RNDN
);
1780 std::string real_str
= num
.substr(0, pos
);
1781 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
1783 error_at(imp
->location(), "bad number in import data: %qs",
1785 return Expression::make_error(imp
->location());
1789 std::string imag_str
;
1790 if (pos
== std::string::npos
)
1793 imag_str
= num
.substr(pos
);
1794 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
1796 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
1798 error_at(imp
->location(), "bad number in import data: %qs",
1800 return Expression::make_error(imp
->location());
1802 Expression
* ret
= Expression::make_complex(&real
, &imag
, NULL
,
1808 else if (num
.find('.') == std::string::npos
1809 && num
.find('E') == std::string::npos
)
1812 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
1814 error_at(imp
->location(), "bad number in import data: %qs",
1816 return Expression::make_error(imp
->location());
1818 Expression
* ret
= Expression::make_integer(&val
, NULL
, imp
->location());
1825 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
1827 error_at(imp
->location(), "bad number in import data: %qs",
1829 return Expression::make_error(imp
->location());
1831 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
1837 // Build a new integer value.
1840 Expression::make_integer(const mpz_t
* val
, Type
* type
,
1841 source_location location
)
1843 return new Integer_expression(val
, type
, location
);
1848 class Float_expression
: public Expression
1851 Float_expression(const mpfr_t
* val
, Type
* type
, source_location location
)
1852 : Expression(EXPRESSION_FLOAT
, location
),
1855 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
1858 // Constrain VAL to fit into TYPE.
1860 constrain_float(mpfr_t val
, Type
* type
);
1862 // Return whether VAL fits in the type.
1864 check_constant(mpfr_t val
, Type
*, source_location
);
1866 // Write VAL to export data.
1868 export_float(Export
* exp
, const mpfr_t val
);
1872 do_is_constant() const
1876 do_float_constant_value(mpfr_t val
, Type
**) const;
1882 do_determine_type(const Type_context
*);
1885 do_check_types(Gogo
*);
1889 { return Expression::make_float(&this->val_
, this->type_
,
1890 this->location()); }
1893 do_get_tree(Translate_context
*);
1896 do_export(Export
*) const;
1899 // The floating point value.
1905 // Constrain VAL to fit into TYPE.
1908 Float_expression::constrain_float(mpfr_t val
, Type
* type
)
1910 Float_type
* ftype
= type
->float_type();
1911 if (ftype
!= NULL
&& !ftype
->is_abstract())
1912 mpfr_prec_round(val
, ftype
->bits(), GMP_RNDN
);
1915 // Return a floating point constant value.
1918 Float_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
1920 if (this->type_
!= NULL
)
1921 *ptype
= this->type_
;
1922 mpfr_set(val
, this->val_
, GMP_RNDN
);
1926 // Return the current type. If we haven't set the type yet, we return
1927 // an abstract float type.
1930 Float_expression::do_type()
1932 if (this->type_
== NULL
)
1933 this->type_
= Type::make_abstract_float_type();
1937 // Set the type of the float value. Here we may switch from an
1938 // abstract type to a real type.
1941 Float_expression::do_determine_type(const Type_context
* context
)
1943 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1945 else if (context
->type
!= NULL
1946 && (context
->type
->integer_type() != NULL
1947 || context
->type
->float_type() != NULL
1948 || context
->type
->complex_type() != NULL
))
1949 this->type_
= context
->type
;
1950 else if (!context
->may_be_abstract
)
1951 this->type_
= Type::lookup_float_type("float64");
1954 // Return true if the floating point value VAL fits in the range of
1955 // the type TYPE. Otherwise give an error and return false. TYPE may
1959 Float_expression::check_constant(mpfr_t val
, Type
* type
,
1960 source_location location
)
1964 Float_type
* ftype
= type
->float_type();
1965 if (ftype
== NULL
|| ftype
->is_abstract())
1968 // A NaN or Infinity always fits in the range of the type.
1969 if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
1972 mp_exp_t exp
= mpfr_get_exp(val
);
1974 switch (ftype
->bits())
1987 error_at(location
, "floating point constant overflow");
1993 // Check the type of a float value.
1996 Float_expression::do_check_types(Gogo
*)
1998 if (this->type_
== NULL
)
2001 if (!Float_expression::check_constant(this->val_
, this->type_
,
2003 this->set_is_error();
2005 Integer_type
* integer_type
= this->type_
->integer_type();
2006 if (integer_type
!= NULL
)
2008 if (!mpfr_integer_p(this->val_
))
2009 this->report_error(_("floating point constant truncated to integer"));
2012 gcc_assert(!integer_type
->is_abstract());
2015 mpfr_get_z(ival
, this->val_
, GMP_RNDN
);
2016 Integer_expression::check_constant(ival
, integer_type
,
2023 // Get a tree for a float constant.
2026 Float_expression::do_get_tree(Translate_context
* context
)
2028 Gogo
* gogo
= context
->gogo();
2030 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2031 type
= this->type_
->get_tree(gogo
);
2032 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2034 // We have an abstract integer type. We just hope for the best.
2035 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
2039 // If we still have an abstract type here, then this is being
2040 // used in a constant expression which didn't get reduced. We
2041 // just use float64 and hope for the best.
2042 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
2044 return Expression::float_constant_tree(this->val_
, type
);
2047 // Write a floating point number to export data.
2050 Float_expression::export_float(Export
*exp
, const mpfr_t val
)
2053 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2055 exp
->write_c_string("-");
2056 exp
->write_c_string("0.");
2057 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2060 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2061 exp
->write_c_string(buf
);
2064 // Export a floating point number in a constant expression.
2067 Float_expression::do_export(Export
* exp
) const
2069 Float_expression::export_float(exp
, this->val_
);
2070 // A trailing space lets us reliably identify the end of the number.
2071 exp
->write_c_string(" ");
2074 // Make a float expression.
2077 Expression::make_float(const mpfr_t
* val
, Type
* type
, source_location location
)
2079 return new Float_expression(val
, type
, location
);
2084 class Complex_expression
: public Expression
2087 Complex_expression(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2088 source_location location
)
2089 : Expression(EXPRESSION_COMPLEX
, location
),
2092 mpfr_init_set(this->real_
, *real
, GMP_RNDN
);
2093 mpfr_init_set(this->imag_
, *imag
, GMP_RNDN
);
2096 // Constrain REAL/IMAG to fit into TYPE.
2098 constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
);
2100 // Return whether REAL/IMAG fits in the type.
2102 check_constant(mpfr_t real
, mpfr_t imag
, Type
*, source_location
);
2104 // Write REAL/IMAG to export data.
2106 export_complex(Export
* exp
, const mpfr_t real
, const mpfr_t val
);
2110 do_is_constant() const
2114 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2120 do_determine_type(const Type_context
*);
2123 do_check_types(Gogo
*);
2128 return Expression::make_complex(&this->real_
, &this->imag_
, this->type_
,
2133 do_get_tree(Translate_context
*);
2136 do_export(Export
*) const;
2141 // The imaginary part;
2143 // The type if known.
2147 // Constrain REAL/IMAG to fit into TYPE.
2150 Complex_expression::constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
)
2152 Complex_type
* ctype
= type
->complex_type();
2153 if (ctype
!= NULL
&& !ctype
->is_abstract())
2155 mpfr_prec_round(real
, ctype
->bits() / 2, GMP_RNDN
);
2156 mpfr_prec_round(imag
, ctype
->bits() / 2, GMP_RNDN
);
2160 // Return a complex constant value.
2163 Complex_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2166 if (this->type_
!= NULL
)
2167 *ptype
= this->type_
;
2168 mpfr_set(real
, this->real_
, GMP_RNDN
);
2169 mpfr_set(imag
, this->imag_
, GMP_RNDN
);
2173 // Return the current type. If we haven't set the type yet, we return
2174 // an abstract complex type.
2177 Complex_expression::do_type()
2179 if (this->type_
== NULL
)
2180 this->type_
= Type::make_abstract_complex_type();
2184 // Set the type of the complex value. Here we may switch from an
2185 // abstract type to a real type.
2188 Complex_expression::do_determine_type(const Type_context
* context
)
2190 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2192 else if (context
->type
!= NULL
2193 && context
->type
->complex_type() != NULL
)
2194 this->type_
= context
->type
;
2195 else if (!context
->may_be_abstract
)
2196 this->type_
= Type::lookup_complex_type("complex128");
2199 // Return true if the complex value REAL/IMAG fits in the range of the
2200 // type TYPE. Otherwise give an error and return false. TYPE may be
2204 Complex_expression::check_constant(mpfr_t real
, mpfr_t imag
, Type
* type
,
2205 source_location location
)
2209 Complex_type
* ctype
= type
->complex_type();
2210 if (ctype
== NULL
|| ctype
->is_abstract())
2214 switch (ctype
->bits())
2226 // A NaN or Infinity always fits in the range of the type.
2227 if (!mpfr_nan_p(real
) && !mpfr_inf_p(real
) && !mpfr_zero_p(real
))
2229 if (mpfr_get_exp(real
) > max_exp
)
2231 error_at(location
, "complex real part constant overflow");
2236 if (!mpfr_nan_p(imag
) && !mpfr_inf_p(imag
) && !mpfr_zero_p(imag
))
2238 if (mpfr_get_exp(imag
) > max_exp
)
2240 error_at(location
, "complex imaginary part constant overflow");
2248 // Check the type of a complex value.
2251 Complex_expression::do_check_types(Gogo
*)
2253 if (this->type_
== NULL
)
2256 if (!Complex_expression::check_constant(this->real_
, this->imag_
,
2257 this->type_
, this->location()))
2258 this->set_is_error();
2261 // Get a tree for a complex constant.
2264 Complex_expression::do_get_tree(Translate_context
* context
)
2266 Gogo
* gogo
= context
->gogo();
2268 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2269 type
= this->type_
->get_tree(gogo
);
2272 // If we still have an abstract type here, this this is being
2273 // used in a constant expression which didn't get reduced. We
2274 // just use complex128 and hope for the best.
2275 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
2277 return Expression::complex_constant_tree(this->real_
, this->imag_
, type
);
2280 // Write REAL/IMAG to export data.
2283 Complex_expression::export_complex(Export
* exp
, const mpfr_t real
,
2286 if (!mpfr_zero_p(real
))
2288 Float_expression::export_float(exp
, real
);
2289 if (mpfr_sgn(imag
) > 0)
2290 exp
->write_c_string("+");
2292 Float_expression::export_float(exp
, imag
);
2293 exp
->write_c_string("i");
2296 // Export a complex number in a constant expression.
2299 Complex_expression::do_export(Export
* exp
) const
2301 Complex_expression::export_complex(exp
, this->real_
, this->imag_
);
2302 // A trailing space lets us reliably identify the end of the number.
2303 exp
->write_c_string(" ");
2306 // Make a complex expression.
2309 Expression::make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2310 source_location location
)
2312 return new Complex_expression(real
, imag
, type
, location
);
2315 // Find a named object in an expression.
2317 class Find_named_object
: public Traverse
2320 Find_named_object(Named_object
* no
)
2321 : Traverse(traverse_expressions
),
2322 no_(no
), found_(false)
2325 // Whether we found the object.
2328 { return this->found_
; }
2332 expression(Expression
**);
2335 // The object we are looking for.
2337 // Whether we found it.
2341 // A reference to a const in an expression.
2343 class Const_expression
: public Expression
2346 Const_expression(Named_object
* constant
, source_location location
)
2347 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2348 constant_(constant
), type_(NULL
), seen_(false)
2353 { return this->constant_
; }
2355 // Check that the initializer does not refer to the constant itself.
2357 check_for_init_loop();
2361 do_traverse(Traverse
*);
2364 do_lower(Gogo
*, Named_object
*, int);
2367 do_is_constant() const
2371 do_integer_constant_value(bool, mpz_t val
, Type
**) const;
2374 do_float_constant_value(mpfr_t val
, Type
**) const;
2377 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2380 do_string_constant_value(std::string
* val
) const
2381 { return this->constant_
->const_value()->expr()->string_constant_value(val
); }
2386 // The type of a const is set by the declaration, not the use.
2388 do_determine_type(const Type_context
*);
2391 do_check_types(Gogo
*);
2398 do_get_tree(Translate_context
* context
);
2400 // When exporting a reference to a const as part of a const
2401 // expression, we export the value. We ignore the fact that it has
2404 do_export(Export
* exp
) const
2405 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2409 Named_object
* constant_
;
2410 // The type of this reference. This is used if the constant has an
2413 // Used to prevent infinite recursion when a constant incorrectly
2414 // refers to itself.
2421 Const_expression::do_traverse(Traverse
* traverse
)
2423 if (this->type_
!= NULL
)
2424 return Type::traverse(this->type_
, traverse
);
2425 return TRAVERSE_CONTINUE
;
2428 // Lower a constant expression. This is where we convert the
2429 // predeclared constant iota into an integer value.
2432 Const_expression::do_lower(Gogo
* gogo
, Named_object
*, int iota_value
)
2434 if (this->constant_
->const_value()->expr()->classification()
2437 if (iota_value
== -1)
2439 error_at(this->location(),
2440 "iota is only defined in const declarations");
2444 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2445 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2451 // Make sure that the constant itself has been lowered.
2452 gogo
->lower_constant(this->constant_
);
2457 // Return an integer constant value.
2460 Const_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
2467 if (this->type_
!= NULL
)
2468 ctype
= this->type_
;
2470 ctype
= this->constant_
->const_value()->type();
2471 if (ctype
!= NULL
&& ctype
->integer_type() == NULL
)
2474 Expression
* e
= this->constant_
->const_value()->expr();
2479 bool r
= e
->integer_constant_value(iota_is_constant
, val
, &t
);
2481 this->seen_
= false;
2485 && !Integer_expression::check_constant(val
, ctype
, this->location()))
2488 *ptype
= ctype
!= NULL
? ctype
: t
;
2492 // Return a floating point constant value.
2495 Const_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
2501 if (this->type_
!= NULL
)
2502 ctype
= this->type_
;
2504 ctype
= this->constant_
->const_value()->type();
2505 if (ctype
!= NULL
&& ctype
->float_type() == NULL
)
2511 bool r
= this->constant_
->const_value()->expr()->float_constant_value(val
,
2514 this->seen_
= false;
2516 if (r
&& ctype
!= NULL
)
2518 if (!Float_expression::check_constant(val
, ctype
, this->location()))
2520 Float_expression::constrain_float(val
, ctype
);
2522 *ptype
= ctype
!= NULL
? ctype
: t
;
2526 // Return a complex constant value.
2529 Const_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2536 if (this->type_
!= NULL
)
2537 ctype
= this->type_
;
2539 ctype
= this->constant_
->const_value()->type();
2540 if (ctype
!= NULL
&& ctype
->complex_type() == NULL
)
2546 bool r
= this->constant_
->const_value()->expr()->complex_constant_value(real
,
2550 this->seen_
= false;
2552 if (r
&& ctype
!= NULL
)
2554 if (!Complex_expression::check_constant(real
, imag
, ctype
,
2557 Complex_expression::constrain_complex(real
, imag
, ctype
);
2559 *ptype
= ctype
!= NULL
? ctype
: t
;
2563 // Return the type of the const reference.
2566 Const_expression::do_type()
2568 if (this->type_
!= NULL
)
2571 Named_constant
* nc
= this->constant_
->const_value();
2573 if (this->seen_
|| nc
->lowering())
2575 this->report_error(_("constant refers to itself"));
2576 this->type_
= Type::make_error_type();
2582 Type
* ret
= nc
->type();
2586 this->seen_
= false;
2590 // During parsing, a named constant may have a NULL type, but we
2591 // must not return a NULL type here.
2592 ret
= nc
->expr()->type();
2594 this->seen_
= false;
2599 // Set the type of the const reference.
2602 Const_expression::do_determine_type(const Type_context
* context
)
2604 Type
* ctype
= this->constant_
->const_value()->type();
2605 Type
* cetype
= (ctype
!= NULL
2607 : this->constant_
->const_value()->expr()->type());
2608 if (ctype
!= NULL
&& !ctype
->is_abstract())
2610 else if (context
->type
!= NULL
2611 && (context
->type
->integer_type() != NULL
2612 || context
->type
->float_type() != NULL
2613 || context
->type
->complex_type() != NULL
)
2614 && (cetype
->integer_type() != NULL
2615 || cetype
->float_type() != NULL
2616 || cetype
->complex_type() != NULL
))
2617 this->type_
= context
->type
;
2618 else if (context
->type
!= NULL
2619 && context
->type
->is_string_type()
2620 && cetype
->is_string_type())
2621 this->type_
= context
->type
;
2622 else if (context
->type
!= NULL
2623 && context
->type
->is_boolean_type()
2624 && cetype
->is_boolean_type())
2625 this->type_
= context
->type
;
2626 else if (!context
->may_be_abstract
)
2628 if (cetype
->is_abstract())
2629 cetype
= cetype
->make_non_abstract_type();
2630 this->type_
= cetype
;
2634 // Check for a loop in which the initializer of a constant refers to
2635 // the constant itself.
2638 Const_expression::check_for_init_loop()
2640 if (this->type_
!= NULL
&& this->type_
->is_error())
2645 this->report_error(_("constant refers to itself"));
2646 this->type_
= Type::make_error_type();
2650 Expression
* init
= this->constant_
->const_value()->expr();
2651 Find_named_object
find_named_object(this->constant_
);
2654 Expression::traverse(&init
, &find_named_object
);
2655 this->seen_
= false;
2657 if (find_named_object
.found())
2659 if (this->type_
== NULL
|| !this->type_
->is_error())
2661 this->report_error(_("constant refers to itself"));
2662 this->type_
= Type::make_error_type();
2668 // Check types of a const reference.
2671 Const_expression::do_check_types(Gogo
*)
2673 if (this->type_
!= NULL
&& this->type_
->is_error())
2676 this->check_for_init_loop();
2678 if (this->type_
== NULL
|| this->type_
->is_abstract())
2681 // Check for integer overflow.
2682 if (this->type_
->integer_type() != NULL
)
2687 if (!this->integer_constant_value(true, ival
, &dummy
))
2691 Expression
* cexpr
= this->constant_
->const_value()->expr();
2692 if (cexpr
->float_constant_value(fval
, &dummy
))
2694 if (!mpfr_integer_p(fval
))
2695 this->report_error(_("floating point constant "
2696 "truncated to integer"));
2699 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2700 Integer_expression::check_constant(ival
, this->type_
,
2710 // Return a tree for the const reference.
2713 Const_expression::do_get_tree(Translate_context
* context
)
2715 Gogo
* gogo
= context
->gogo();
2717 if (this->type_
== NULL
)
2718 type_tree
= NULL_TREE
;
2721 type_tree
= this->type_
->get_tree(gogo
);
2722 if (type_tree
== error_mark_node
)
2723 return error_mark_node
;
2726 // If the type has been set for this expression, but the underlying
2727 // object is an abstract int or float, we try to get the abstract
2728 // value. Otherwise we may lose something in the conversion.
2729 if (this->type_
!= NULL
2730 && (this->constant_
->const_value()->type() == NULL
2731 || this->constant_
->const_value()->type()->is_abstract()))
2733 Expression
* expr
= this->constant_
->const_value()->expr();
2737 if (expr
->integer_constant_value(true, ival
, &t
))
2739 tree ret
= Expression::integer_constant_tree(ival
, type_tree
);
2747 if (expr
->float_constant_value(fval
, &t
))
2749 tree ret
= Expression::float_constant_tree(fval
, type_tree
);
2756 if (expr
->complex_constant_value(fval
, imag
, &t
))
2758 tree ret
= Expression::complex_constant_tree(fval
, imag
, type_tree
);
2767 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2768 if (this->type_
== NULL
2769 || const_tree
== error_mark_node
2770 || TREE_TYPE(const_tree
) == error_mark_node
)
2774 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2775 ret
= fold_convert(type_tree
, const_tree
);
2776 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2777 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2778 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2779 ret
= fold(convert_to_real(type_tree
, const_tree
));
2780 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2781 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2787 // Make a reference to a constant in an expression.
2790 Expression::make_const_reference(Named_object
* constant
,
2791 source_location location
)
2793 return new Const_expression(constant
, location
);
2796 // Find a named object in an expression.
2799 Find_named_object::expression(Expression
** pexpr
)
2801 switch ((*pexpr
)->classification())
2803 case Expression::EXPRESSION_CONST_REFERENCE
:
2805 Const_expression
* ce
= static_cast<Const_expression
*>(*pexpr
);
2806 if (ce
->named_object() == this->no_
)
2809 // We need to check a constant initializer explicitly, as
2810 // loops here will not be caught by the loop checking for
2811 // variable initializers.
2812 ce
->check_for_init_loop();
2814 return TRAVERSE_CONTINUE
;
2817 case Expression::EXPRESSION_VAR_REFERENCE
:
2818 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
2820 return TRAVERSE_CONTINUE
;
2821 case Expression::EXPRESSION_FUNC_REFERENCE
:
2822 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
2824 return TRAVERSE_CONTINUE
;
2826 return TRAVERSE_CONTINUE
;
2828 this->found_
= true;
2829 return TRAVERSE_EXIT
;
2834 class Nil_expression
: public Expression
2837 Nil_expression(source_location location
)
2838 : Expression(EXPRESSION_NIL
, location
)
2846 do_is_constant() const
2851 { return Type::make_nil_type(); }
2854 do_determine_type(const Type_context
*)
2862 do_get_tree(Translate_context
*)
2863 { return null_pointer_node
; }
2866 do_export(Export
* exp
) const
2867 { exp
->write_c_string("nil"); }
2870 // Import a nil expression.
2873 Nil_expression::do_import(Import
* imp
)
2875 imp
->require_c_string("nil");
2876 return Expression::make_nil(imp
->location());
2879 // Make a nil expression.
2882 Expression::make_nil(source_location location
)
2884 return new Nil_expression(location
);
2887 // The value of the predeclared constant iota. This is little more
2888 // than a marker. This will be lowered to an integer in
2889 // Const_expression::do_lower, which is where we know the value that
2892 class Iota_expression
: public Parser_expression
2895 Iota_expression(source_location location
)
2896 : Parser_expression(EXPRESSION_IOTA
, location
)
2901 do_lower(Gogo
*, Named_object
*, int)
2902 { gcc_unreachable(); }
2904 // There should only ever be one of these.
2907 { gcc_unreachable(); }
2910 // Make an iota expression. This is only called for one case: the
2911 // value of the predeclared constant iota.
2914 Expression::make_iota()
2916 static Iota_expression
iota_expression(UNKNOWN_LOCATION
);
2917 return &iota_expression
;
2920 // A type conversion expression.
2922 class Type_conversion_expression
: public Expression
2925 Type_conversion_expression(Type
* type
, Expression
* expr
,
2926 source_location location
)
2927 : Expression(EXPRESSION_CONVERSION
, location
),
2928 type_(type
), expr_(expr
), may_convert_function_types_(false)
2931 // Return the type to which we are converting.
2934 { return this->type_
; }
2936 // Return the expression which we are converting.
2939 { return this->expr_
; }
2941 // Permit converting from one function type to another. This is
2942 // used internally for method expressions.
2944 set_may_convert_function_types()
2946 this->may_convert_function_types_
= true;
2949 // Import a type conversion expression.
2955 do_traverse(Traverse
* traverse
);
2958 do_lower(Gogo
*, Named_object
*, int);
2961 do_is_constant() const
2962 { return this->expr_
->is_constant(); }
2965 do_integer_constant_value(bool, mpz_t
, Type
**) const;
2968 do_float_constant_value(mpfr_t
, Type
**) const;
2971 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
2974 do_string_constant_value(std::string
*) const;
2978 { return this->type_
; }
2981 do_determine_type(const Type_context
*)
2983 Type_context
subcontext(this->type_
, false);
2984 this->expr_
->determine_type(&subcontext
);
2988 do_check_types(Gogo
*);
2993 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
2998 do_get_tree(Translate_context
* context
);
3001 do_export(Export
*) const;
3004 // The type to convert to.
3006 // The expression to convert.
3008 // True if this is permitted to convert function types. This is
3009 // used internally for method expressions.
3010 bool may_convert_function_types_
;
3016 Type_conversion_expression::do_traverse(Traverse
* traverse
)
3018 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3019 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3020 return TRAVERSE_EXIT
;
3021 return TRAVERSE_CONTINUE
;
3024 // Convert to a constant at lowering time.
3027 Type_conversion_expression::do_lower(Gogo
*, Named_object
*, int)
3029 Type
* type
= this->type_
;
3030 Expression
* val
= this->expr_
;
3031 source_location location
= this->location();
3033 if (type
->integer_type() != NULL
)
3038 if (val
->integer_constant_value(false, ival
, &dummy
))
3040 if (!Integer_expression::check_constant(ival
, type
, location
))
3041 mpz_set_ui(ival
, 0);
3042 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
3049 if (val
->float_constant_value(fval
, &dummy
))
3051 if (!mpfr_integer_p(fval
))
3054 "floating point constant truncated to integer");
3055 return Expression::make_error(location
);
3057 mpfr_get_z(ival
, fval
, GMP_RNDN
);
3058 if (!Integer_expression::check_constant(ival
, type
, location
))
3059 mpz_set_ui(ival
, 0);
3060 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
3069 if (type
->float_type() != NULL
)
3074 if (val
->float_constant_value(fval
, &dummy
))
3076 if (!Float_expression::check_constant(fval
, type
, location
))
3077 mpfr_set_ui(fval
, 0, GMP_RNDN
);
3078 Float_expression::constrain_float(fval
, type
);
3079 Expression
*ret
= Expression::make_float(&fval
, type
, location
);
3086 if (type
->complex_type() != NULL
)
3093 if (val
->complex_constant_value(real
, imag
, &dummy
))
3095 if (!Complex_expression::check_constant(real
, imag
, type
, location
))
3097 mpfr_set_ui(real
, 0, GMP_RNDN
);
3098 mpfr_set_ui(imag
, 0, GMP_RNDN
);
3100 Complex_expression::constrain_complex(real
, imag
, type
);
3101 Expression
* ret
= Expression::make_complex(&real
, &imag
, type
,
3111 if (type
->is_open_array_type() && type
->named_type() == NULL
)
3113 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3114 bool is_byte
= element_type
== Type::lookup_integer_type("uint8");
3115 bool is_int
= element_type
== Type::lookup_integer_type("int");
3116 if (is_byte
|| is_int
)
3119 if (val
->string_constant_value(&s
))
3121 Expression_list
* vals
= new Expression_list();
3124 for (std::string::const_iterator p
= s
.begin();
3129 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
3130 Expression
* v
= Expression::make_integer(&val
,
3139 const char *p
= s
.data();
3140 const char *pend
= s
.data() + s
.length();
3144 int adv
= Lex::fetch_char(p
, &c
);
3147 warning_at(this->location(), 0,
3148 "invalid UTF-8 encoding");
3153 mpz_init_set_ui(val
, c
);
3154 Expression
* v
= Expression::make_integer(&val
,
3162 return Expression::make_slice_composite_literal(type
, vals
,
3171 // Return the constant integer value if there is one.
3174 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant
,
3178 if (this->type_
->integer_type() == NULL
)
3184 if (this->expr_
->integer_constant_value(iota_is_constant
, ival
, &dummy
))
3186 if (!Integer_expression::check_constant(ival
, this->type_
,
3194 *ptype
= this->type_
;
3201 if (this->expr_
->float_constant_value(fval
, &dummy
))
3203 mpfr_get_z(val
, fval
, GMP_RNDN
);
3205 if (!Integer_expression::check_constant(val
, this->type_
,
3208 *ptype
= this->type_
;
3216 // Return the constant floating point value if there is one.
3219 Type_conversion_expression::do_float_constant_value(mpfr_t val
,
3222 if (this->type_
->float_type() == NULL
)
3228 if (this->expr_
->float_constant_value(fval
, &dummy
))
3230 if (!Float_expression::check_constant(fval
, this->type_
,
3236 mpfr_set(val
, fval
, GMP_RNDN
);
3238 Float_expression::constrain_float(val
, this->type_
);
3239 *ptype
= this->type_
;
3247 // Return the constant complex value if there is one.
3250 Type_conversion_expression::do_complex_constant_value(mpfr_t real
,
3254 if (this->type_
->complex_type() == NULL
)
3262 if (this->expr_
->complex_constant_value(rval
, ival
, &dummy
))
3264 if (!Complex_expression::check_constant(rval
, ival
, this->type_
,
3271 mpfr_set(real
, rval
, GMP_RNDN
);
3272 mpfr_set(imag
, ival
, GMP_RNDN
);
3275 Complex_expression::constrain_complex(real
, imag
, this->type_
);
3276 *ptype
= this->type_
;
3285 // Return the constant string value if there is one.
3288 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3290 if (this->type_
->is_string_type()
3291 && this->expr_
->type()->integer_type() != NULL
)
3296 if (this->expr_
->integer_constant_value(false, ival
, &dummy
))
3298 unsigned long ulval
= mpz_get_ui(ival
);
3299 if (mpz_cmp_ui(ival
, ulval
) == 0)
3301 Lex::append_char(ulval
, true, val
, this->location());
3309 // FIXME: Could handle conversion from const []int here.
3314 // Check that types are convertible.
3317 Type_conversion_expression::do_check_types(Gogo
*)
3319 Type
* type
= this->type_
;
3320 Type
* expr_type
= this->expr_
->type();
3323 if (type
->is_error() || expr_type
->is_error())
3325 this->set_is_error();
3329 if (this->may_convert_function_types_
3330 && type
->function_type() != NULL
3331 && expr_type
->function_type() != NULL
)
3334 if (Type::are_convertible(type
, expr_type
, &reason
))
3337 error_at(this->location(), "%s", reason
.c_str());
3338 this->set_is_error();
3341 // Get a tree for a type conversion.
3344 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3346 Gogo
* gogo
= context
->gogo();
3347 tree type_tree
= this->type_
->get_tree(gogo
);
3348 tree expr_tree
= this->expr_
->get_tree(context
);
3350 if (type_tree
== error_mark_node
3351 || expr_tree
== error_mark_node
3352 || TREE_TYPE(expr_tree
) == error_mark_node
)
3353 return error_mark_node
;
3355 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3356 return fold_convert(type_tree
, expr_tree
);
3358 Type
* type
= this->type_
;
3359 Type
* expr_type
= this->expr_
->type();
3361 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3362 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3363 expr_tree
, this->location());
3364 else if (type
->integer_type() != NULL
)
3366 if (expr_type
->integer_type() != NULL
3367 || expr_type
->float_type() != NULL
3368 || expr_type
->is_unsafe_pointer_type())
3369 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3373 else if (type
->float_type() != NULL
)
3375 if (expr_type
->integer_type() != NULL
3376 || expr_type
->float_type() != NULL
)
3377 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3381 else if (type
->complex_type() != NULL
)
3383 if (expr_type
->complex_type() != NULL
)
3384 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3388 else if (type
->is_string_type()
3389 && expr_type
->integer_type() != NULL
)
3391 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3392 if (host_integerp(expr_tree
, 0))
3394 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3396 Lex::append_char(intval
, true, &s
, this->location());
3397 Expression
* se
= Expression::make_string(s
, this->location());
3398 return se
->get_tree(context
);
3401 static tree int_to_string_fndecl
;
3402 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3404 "__go_int_to_string",
3408 fold_convert(integer_type_node
, expr_tree
));
3410 else if (type
->is_string_type()
3411 && (expr_type
->array_type() != NULL
3412 || (expr_type
->points_to() != NULL
3413 && expr_type
->points_to()->array_type() != NULL
)))
3415 Type
* t
= expr_type
;
3416 if (t
->points_to() != NULL
)
3419 expr_tree
= build_fold_indirect_ref(expr_tree
);
3421 if (!DECL_P(expr_tree
))
3422 expr_tree
= save_expr(expr_tree
);
3423 Array_type
* a
= t
->array_type();
3424 Type
* e
= a
->element_type()->forwarded();
3425 gcc_assert(e
->integer_type() != NULL
);
3426 tree valptr
= fold_convert(const_ptr_type_node
,
3427 a
->value_pointer_tree(gogo
, expr_tree
));
3428 tree len
= a
->length_tree(gogo
, expr_tree
);
3429 len
= fold_convert_loc(this->location(), integer_type_node
, len
);
3430 if (e
->integer_type()->is_unsigned()
3431 && e
->integer_type()->bits() == 8)
3433 static tree byte_array_to_string_fndecl
;
3434 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3436 "__go_byte_array_to_string",
3439 const_ptr_type_node
,
3446 gcc_assert(e
== Type::lookup_integer_type("int"));
3447 static tree int_array_to_string_fndecl
;
3448 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3450 "__go_int_array_to_string",
3453 const_ptr_type_node
,
3459 else if (type
->is_open_array_type() && expr_type
->is_string_type())
3461 Type
* e
= type
->array_type()->element_type()->forwarded();
3462 gcc_assert(e
->integer_type() != NULL
);
3463 if (e
->integer_type()->is_unsigned()
3464 && e
->integer_type()->bits() == 8)
3466 static tree string_to_byte_array_fndecl
;
3467 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3469 "__go_string_to_byte_array",
3472 TREE_TYPE(expr_tree
),
3477 gcc_assert(e
== Type::lookup_integer_type("int"));
3478 static tree string_to_int_array_fndecl
;
3479 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3481 "__go_string_to_int_array",
3484 TREE_TYPE(expr_tree
),
3488 else if ((type
->is_unsafe_pointer_type()
3489 && expr_type
->points_to() != NULL
)
3490 || (expr_type
->is_unsafe_pointer_type()
3491 && type
->points_to() != NULL
))
3492 ret
= fold_convert(type_tree
, expr_tree
);
3493 else if (type
->is_unsafe_pointer_type()
3494 && expr_type
->integer_type() != NULL
)
3495 ret
= convert_to_pointer(type_tree
, expr_tree
);
3496 else if (this->may_convert_function_types_
3497 && type
->function_type() != NULL
3498 && expr_type
->function_type() != NULL
)
3499 ret
= fold_convert_loc(this->location(), type_tree
, expr_tree
);
3501 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3502 expr_tree
, this->location());
3507 // Output a type conversion in a constant expression.
3510 Type_conversion_expression::do_export(Export
* exp
) const
3512 exp
->write_c_string("convert(");
3513 exp
->write_type(this->type_
);
3514 exp
->write_c_string(", ");
3515 this->expr_
->export_expression(exp
);
3516 exp
->write_c_string(")");
3519 // Import a type conversion or a struct construction.
3522 Type_conversion_expression::do_import(Import
* imp
)
3524 imp
->require_c_string("convert(");
3525 Type
* type
= imp
->read_type();
3526 imp
->require_c_string(", ");
3527 Expression
* val
= Expression::import_expression(imp
);
3528 imp
->require_c_string(")");
3529 return Expression::make_cast(type
, val
, imp
->location());
3532 // Make a type cast expression.
3535 Expression::make_cast(Type
* type
, Expression
* val
, source_location location
)
3537 if (type
->is_error_type() || val
->is_error_expression())
3538 return Expression::make_error(location
);
3539 return new Type_conversion_expression(type
, val
, location
);
3542 // An unsafe type conversion, used to pass values to builtin functions.
3544 class Unsafe_type_conversion_expression
: public Expression
3547 Unsafe_type_conversion_expression(Type
* type
, Expression
* expr
,
3548 source_location location
)
3549 : Expression(EXPRESSION_UNSAFE_CONVERSION
, location
),
3550 type_(type
), expr_(expr
)
3555 do_traverse(Traverse
* traverse
);
3559 { return this->type_
; }
3562 do_determine_type(const Type_context
*)
3568 return new Unsafe_type_conversion_expression(this->type_
,
3569 this->expr_
->copy(),
3574 do_get_tree(Translate_context
*);
3577 // The type to convert to.
3579 // The expression to convert.
3586 Unsafe_type_conversion_expression::do_traverse(Traverse
* traverse
)
3588 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3589 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3590 return TRAVERSE_EXIT
;
3591 return TRAVERSE_CONTINUE
;
3594 // Convert to backend representation.
3597 Unsafe_type_conversion_expression::do_get_tree(Translate_context
* context
)
3599 // We are only called for a limited number of cases.
3601 Type
* t
= this->type_
;
3602 Type
* et
= this->expr_
->type();
3604 tree type_tree
= this->type_
->get_tree(context
->gogo());
3605 tree expr_tree
= this->expr_
->get_tree(context
);
3606 if (type_tree
== error_mark_node
|| expr_tree
== error_mark_node
)
3607 return error_mark_node
;
3609 source_location loc
= this->location();
3611 bool use_view_convert
= false;
3612 if (t
->is_open_array_type())
3614 gcc_assert(et
->is_open_array_type());
3615 use_view_convert
= true;
3617 else if (t
->map_type() != NULL
)
3618 gcc_assert(et
->map_type() != NULL
);
3619 else if (t
->channel_type() != NULL
)
3620 gcc_assert(et
->channel_type() != NULL
);
3621 else if (t
->points_to() != NULL
&& t
->points_to()->channel_type() != NULL
)
3622 gcc_assert((et
->points_to() != NULL
3623 && et
->points_to()->channel_type() != NULL
)
3624 || et
->is_nil_type());
3625 else if (t
->is_unsafe_pointer_type())
3626 gcc_assert(et
->points_to() != NULL
|| et
->is_nil_type());
3627 else if (et
->is_unsafe_pointer_type())
3628 gcc_assert(t
->points_to() != NULL
);
3629 else if (t
->interface_type() != NULL
&& !t
->interface_type()->is_empty())
3631 gcc_assert(et
->interface_type() != NULL
3632 && !et
->interface_type()->is_empty());
3633 use_view_convert
= true;
3635 else if (t
->interface_type() != NULL
&& t
->interface_type()->is_empty())
3637 gcc_assert(et
->interface_type() != NULL
3638 && et
->interface_type()->is_empty());
3639 use_view_convert
= true;
3641 else if (t
->integer_type() != NULL
)
3643 gcc_assert(et
->is_boolean_type()
3644 || et
->integer_type() != NULL
3645 || et
->function_type() != NULL
3646 || et
->points_to() != NULL
3647 || et
->map_type() != NULL
3648 || et
->channel_type() != NULL
);
3649 return convert_to_integer(type_tree
, expr_tree
);
3654 if (use_view_convert
)
3655 return fold_build1_loc(loc
, VIEW_CONVERT_EXPR
, type_tree
, expr_tree
);
3657 return fold_convert_loc(loc
, type_tree
, expr_tree
);
3660 // Make an unsafe type conversion expression.
3663 Expression::make_unsafe_cast(Type
* type
, Expression
* expr
,
3664 source_location location
)
3666 return new Unsafe_type_conversion_expression(type
, expr
, location
);
3669 // Unary expressions.
3671 class Unary_expression
: public Expression
3674 Unary_expression(Operator op
, Expression
* expr
, source_location location
)
3675 : Expression(EXPRESSION_UNARY
, location
),
3676 op_(op
), escapes_(true), expr_(expr
)
3679 // Return the operator.
3682 { return this->op_
; }
3684 // Return the operand.
3687 { return this->expr_
; }
3689 // Record that an address expression does not escape.
3691 set_does_not_escape()
3693 gcc_assert(this->op_
== OPERATOR_AND
);
3694 this->escapes_
= false;
3697 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3698 // could be done, false if not.
3700 eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3703 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3704 // could be done, false if not.
3706 eval_float(Operator op
, mpfr_t uval
, mpfr_t val
);
3708 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3709 // true if this could be done, false if not.
3711 eval_complex(Operator op
, mpfr_t ureal
, mpfr_t uimag
, mpfr_t real
,
3719 do_traverse(Traverse
* traverse
)
3720 { return Expression::traverse(&this->expr_
, traverse
); }
3723 do_lower(Gogo
*, Named_object
*, int);
3726 do_is_constant() const;
3729 do_integer_constant_value(bool, mpz_t
, Type
**) const;
3732 do_float_constant_value(mpfr_t
, Type
**) const;
3735 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
3741 do_determine_type(const Type_context
*);
3744 do_check_types(Gogo
*);
3749 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3754 do_is_addressable() const
3755 { return this->op_
== OPERATOR_MULT
; }
3758 do_get_tree(Translate_context
*);
3761 do_export(Export
*) const;
3764 // The unary operator to apply.
3766 // Normally true. False if this is an address expression which does
3767 // not escape the current function.
3773 // If we are taking the address of a composite literal, and the
3774 // contents are not constant, then we want to make a heap composite
3778 Unary_expression::do_lower(Gogo
*, Named_object
*, int)
3780 source_location loc
= this->location();
3781 Operator op
= this->op_
;
3782 Expression
* expr
= this->expr_
;
3784 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3785 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3787 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3788 // moving x to the heap. FIXME: Is it worth doing a real escape
3789 // analysis here? This case is found in math/unsafe.go and is
3790 // therefore worth special casing.
3791 if (op
== OPERATOR_MULT
)
3793 Expression
* e
= expr
;
3794 while (e
->classification() == EXPRESSION_CONVERSION
)
3796 Type_conversion_expression
* te
3797 = static_cast<Type_conversion_expression
*>(e
);
3801 if (e
->classification() == EXPRESSION_UNARY
)
3803 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3804 if (ue
->op_
== OPERATOR_AND
)
3811 ue
->set_does_not_escape();
3816 // Catching an invalid indirection of unsafe.Pointer here avoid
3817 // having to deal with TYPE_VOID in other places.
3818 if (op
== OPERATOR_MULT
&& expr
->type()->is_unsafe_pointer_type())
3820 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3821 return Expression::make_error(this->location());
3824 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
3825 || op
== OPERATOR_NOT
|| op
== OPERATOR_XOR
)
3827 Expression
* ret
= NULL
;
3832 if (expr
->integer_constant_value(false, eval
, &etype
))
3836 if (Unary_expression::eval_integer(op
, etype
, eval
, val
, loc
))
3837 ret
= Expression::make_integer(&val
, etype
, loc
);
3844 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
)
3849 if (expr
->float_constant_value(fval
, &ftype
))
3853 if (Unary_expression::eval_float(op
, fval
, val
))
3854 ret
= Expression::make_float(&val
, ftype
, loc
);
3865 if (expr
->complex_constant_value(fval
, ival
, &ftype
))
3871 if (Unary_expression::eval_complex(op
, fval
, ival
, real
, imag
))
3872 ret
= Expression::make_complex(&real
, &imag
, ftype
, loc
);
3886 // Return whether a unary expression is a constant.
3889 Unary_expression::do_is_constant() const
3891 if (this->op_
== OPERATOR_MULT
)
3893 // Indirecting through a pointer is only constant if the object
3894 // to which the expression points is constant, but we currently
3895 // have no way to determine that.
3898 else if (this->op_
== OPERATOR_AND
)
3900 // Taking the address of a variable is constant if it is a
3901 // global variable, not constant otherwise. In other cases
3902 // taking the address is probably not a constant.
3903 Var_expression
* ve
= this->expr_
->var_expression();
3906 Named_object
* no
= ve
->named_object();
3907 return no
->is_variable() && no
->var_value()->is_global();
3912 return this->expr_
->is_constant();
3915 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3916 // UVAL, if known; it may be NULL. Return true if this could be done,
3920 Unary_expression::eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3921 source_location location
)
3928 case OPERATOR_MINUS
:
3930 return Integer_expression::check_constant(val
, utype
, location
);
3932 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3936 || utype
->integer_type() == NULL
3937 || utype
->integer_type()->is_abstract())
3941 // The number of HOST_WIDE_INTs that it takes to represent
3943 size_t count
= ((mpz_sizeinbase(uval
, 2)
3944 + HOST_BITS_PER_WIDE_INT
3946 / HOST_BITS_PER_WIDE_INT
);
3948 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3949 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3952 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3953 gcc_assert(ecount
<= count
);
3955 // Trim down to the number of words required by the type.
3956 size_t obits
= utype
->integer_type()->bits();
3957 if (!utype
->integer_type()->is_unsigned())
3959 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3960 / HOST_BITS_PER_WIDE_INT
);
3961 gcc_assert(ocount
<= count
);
3963 for (size_t i
= 0; i
< ocount
; ++i
)
3966 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3968 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3971 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3975 return Integer_expression::check_constant(val
, utype
, location
);
3984 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3985 // could be done, false if not.
3988 Unary_expression::eval_float(Operator op
, mpfr_t uval
, mpfr_t val
)
3993 mpfr_set(val
, uval
, GMP_RNDN
);
3995 case OPERATOR_MINUS
:
3996 mpfr_neg(val
, uval
, GMP_RNDN
);
4008 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
4009 // if this could be done, false if not.
4012 Unary_expression::eval_complex(Operator op
, mpfr_t rval
, mpfr_t ival
,
4013 mpfr_t real
, mpfr_t imag
)
4018 mpfr_set(real
, rval
, GMP_RNDN
);
4019 mpfr_set(imag
, ival
, GMP_RNDN
);
4021 case OPERATOR_MINUS
:
4022 mpfr_neg(real
, rval
, GMP_RNDN
);
4023 mpfr_neg(imag
, ival
, GMP_RNDN
);
4035 // Return the integral constant value of a unary expression, if it has one.
4038 Unary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
4044 if (!this->expr_
->integer_constant_value(iota_is_constant
, uval
, ptype
))
4047 ret
= Unary_expression::eval_integer(this->op_
, *ptype
, uval
, val
,
4053 // Return the floating point constant value of a unary expression, if
4057 Unary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
4062 if (!this->expr_
->float_constant_value(uval
, ptype
))
4065 ret
= Unary_expression::eval_float(this->op_
, uval
, val
);
4070 // Return the complex constant value of a unary expression, if it has
4074 Unary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
4082 if (!this->expr_
->complex_constant_value(rval
, ival
, ptype
))
4085 ret
= Unary_expression::eval_complex(this->op_
, rval
, ival
, real
, imag
);
4091 // Return the type of a unary expression.
4094 Unary_expression::do_type()
4099 case OPERATOR_MINUS
:
4102 return this->expr_
->type();
4105 return Type::make_pointer_type(this->expr_
->type());
4109 Type
* subtype
= this->expr_
->type();
4110 Type
* points_to
= subtype
->points_to();
4111 if (points_to
== NULL
)
4112 return Type::make_error_type();
4121 // Determine abstract types for a unary expression.
4124 Unary_expression::do_determine_type(const Type_context
* context
)
4129 case OPERATOR_MINUS
:
4132 this->expr_
->determine_type(context
);
4136 // Taking the address of something.
4138 Type
* subtype
= (context
->type
== NULL
4140 : context
->type
->points_to());
4141 Type_context
subcontext(subtype
, false);
4142 this->expr_
->determine_type(&subcontext
);
4147 // Indirecting through a pointer.
4149 Type
* subtype
= (context
->type
== NULL
4151 : Type::make_pointer_type(context
->type
));
4152 Type_context
subcontext(subtype
, false);
4153 this->expr_
->determine_type(&subcontext
);
4162 // Check types for a unary expression.
4165 Unary_expression::do_check_types(Gogo
*)
4167 Type
* type
= this->expr_
->type();
4168 if (type
->is_error())
4170 this->set_is_error();
4177 case OPERATOR_MINUS
:
4178 if (type
->integer_type() == NULL
4179 && type
->float_type() == NULL
4180 && type
->complex_type() == NULL
)
4181 this->report_error(_("expected numeric type"));
4186 if (type
->integer_type() == NULL
4187 && !type
->is_boolean_type())
4188 this->report_error(_("expected integer or boolean type"));
4192 if (!this->expr_
->is_addressable())
4193 this->report_error(_("invalid operand for unary %<&%>"));
4195 this->expr_
->address_taken(this->escapes_
);
4199 // Indirecting through a pointer.
4200 if (type
->points_to() == NULL
)
4201 this->report_error(_("expected pointer"));
4209 // Get a tree for a unary expression.
4212 Unary_expression::do_get_tree(Translate_context
* context
)
4214 tree expr
= this->expr_
->get_tree(context
);
4215 if (expr
== error_mark_node
)
4216 return error_mark_node
;
4218 source_location loc
= this->location();
4224 case OPERATOR_MINUS
:
4226 tree type
= TREE_TYPE(expr
);
4227 tree compute_type
= excess_precision_type(type
);
4228 if (compute_type
!= NULL_TREE
)
4229 expr
= ::convert(compute_type
, expr
);
4230 tree ret
= fold_build1_loc(loc
, NEGATE_EXPR
,
4231 (compute_type
!= NULL_TREE
4235 if (compute_type
!= NULL_TREE
)
4236 ret
= ::convert(type
, ret
);
4241 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
4242 return fold_build1_loc(loc
, TRUTH_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4244 return fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, expr
,
4245 build_int_cst(TREE_TYPE(expr
), 0));
4248 return fold_build1_loc(loc
, BIT_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4251 // We should not see a non-constant constructor here; cases
4252 // where we would see one should have been moved onto the heap
4253 // at parse time. Taking the address of a nonconstant
4254 // constructor will not do what the programmer expects.
4255 gcc_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
4256 gcc_assert(TREE_CODE(expr
) != ADDR_EXPR
);
4258 // Build a decl for a constant constructor.
4259 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
4261 tree decl
= build_decl(this->location(), VAR_DECL
,
4262 create_tmp_var_name("C"), TREE_TYPE(expr
));
4263 DECL_EXTERNAL(decl
) = 0;
4264 TREE_PUBLIC(decl
) = 0;
4265 TREE_READONLY(decl
) = 1;
4266 TREE_CONSTANT(decl
) = 1;
4267 TREE_STATIC(decl
) = 1;
4268 TREE_ADDRESSABLE(decl
) = 1;
4269 DECL_ARTIFICIAL(decl
) = 1;
4270 DECL_INITIAL(decl
) = expr
;
4271 rest_of_decl_compilation(decl
, 1, 0);
4275 return build_fold_addr_expr_loc(loc
, expr
);
4279 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
4281 // If we are dereferencing the pointer to a large struct, we
4282 // need to check for nil. We don't bother to check for small
4283 // structs because we expect the system to crash on a nil
4284 // pointer dereference.
4285 HOST_WIDE_INT s
= int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr
)));
4286 if (s
== -1 || s
>= 4096)
4289 expr
= save_expr(expr
);
4290 tree compare
= fold_build2_loc(loc
, EQ_EXPR
, boolean_type_node
,
4292 fold_convert(TREE_TYPE(expr
),
4293 null_pointer_node
));
4294 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4296 expr
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(expr
),
4297 build3(COND_EXPR
, void_type_node
,
4298 compare
, crash
, NULL_TREE
),
4302 // If the type of EXPR is a recursive pointer type, then we
4303 // need to insert a cast before indirecting.
4304 if (TREE_TYPE(TREE_TYPE(expr
)) == ptr_type_node
)
4306 Type
* pt
= this->expr_
->type()->points_to();
4307 tree ind
= pt
->get_tree(context
->gogo());
4308 expr
= fold_convert_loc(loc
, build_pointer_type(ind
), expr
);
4311 return build_fold_indirect_ref_loc(loc
, expr
);
4319 // Export a unary expression.
4322 Unary_expression::do_export(Export
* exp
) const
4327 exp
->write_c_string("+ ");
4329 case OPERATOR_MINUS
:
4330 exp
->write_c_string("- ");
4333 exp
->write_c_string("! ");
4336 exp
->write_c_string("^ ");
4343 this->expr_
->export_expression(exp
);
4346 // Import a unary expression.
4349 Unary_expression::do_import(Import
* imp
)
4352 switch (imp
->get_char())
4358 op
= OPERATOR_MINUS
;
4369 imp
->require_c_string(" ");
4370 Expression
* expr
= Expression::import_expression(imp
);
4371 return Expression::make_unary(op
, expr
, imp
->location());
4374 // Make a unary expression.
4377 Expression::make_unary(Operator op
, Expression
* expr
, source_location location
)
4379 return new Unary_expression(op
, expr
, location
);
4382 // If this is an indirection through a pointer, return the expression
4383 // being pointed through. Otherwise return this.
4388 if (this->classification_
== EXPRESSION_UNARY
)
4390 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4391 if (ue
->op() == OPERATOR_MULT
)
4392 return ue
->operand();
4397 // Class Binary_expression.
4402 Binary_expression::do_traverse(Traverse
* traverse
)
4404 int t
= Expression::traverse(&this->left_
, traverse
);
4405 if (t
== TRAVERSE_EXIT
)
4406 return TRAVERSE_EXIT
;
4407 return Expression::traverse(&this->right_
, traverse
);
4410 // Compare integer constants according to OP.
4413 Binary_expression::compare_integer(Operator op
, mpz_t left_val
,
4416 int i
= mpz_cmp(left_val
, right_val
);
4421 case OPERATOR_NOTEQ
:
4436 // Compare floating point constants according to OP.
4439 Binary_expression::compare_float(Operator op
, Type
* type
, mpfr_t left_val
,
4444 i
= mpfr_cmp(left_val
, right_val
);
4448 mpfr_init_set(lv
, left_val
, GMP_RNDN
);
4450 mpfr_init_set(rv
, right_val
, GMP_RNDN
);
4451 Float_expression::constrain_float(lv
, type
);
4452 Float_expression::constrain_float(rv
, type
);
4453 i
= mpfr_cmp(lv
, rv
);
4461 case OPERATOR_NOTEQ
:
4476 // Compare complex constants according to OP. Complex numbers may
4477 // only be compared for equality.
4480 Binary_expression::compare_complex(Operator op
, Type
* type
,
4481 mpfr_t left_real
, mpfr_t left_imag
,
4482 mpfr_t right_real
, mpfr_t right_imag
)
4486 is_equal
= (mpfr_cmp(left_real
, right_real
) == 0
4487 && mpfr_cmp(left_imag
, right_imag
) == 0);
4492 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4493 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4496 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4497 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4498 Complex_expression::constrain_complex(lr
, li
, type
);
4499 Complex_expression::constrain_complex(rr
, ri
, type
);
4500 is_equal
= mpfr_cmp(lr
, rr
) == 0 && mpfr_cmp(li
, ri
) == 0;
4510 case OPERATOR_NOTEQ
:
4517 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4518 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4519 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4520 // this could be done, false if not.
4523 Binary_expression::eval_integer(Operator op
, Type
* left_type
, mpz_t left_val
,
4524 Type
* right_type
, mpz_t right_val
,
4525 source_location location
, mpz_t val
)
4527 bool is_shift_op
= false;
4531 case OPERATOR_ANDAND
:
4533 case OPERATOR_NOTEQ
:
4538 // These return boolean values. We should probably handle them
4539 // anyhow in case a type conversion is used on the result.
4542 mpz_add(val
, left_val
, right_val
);
4544 case OPERATOR_MINUS
:
4545 mpz_sub(val
, left_val
, right_val
);
4548 mpz_ior(val
, left_val
, right_val
);
4551 mpz_xor(val
, left_val
, right_val
);
4554 mpz_mul(val
, left_val
, right_val
);
4557 if (mpz_sgn(right_val
) != 0)
4558 mpz_tdiv_q(val
, left_val
, right_val
);
4561 error_at(location
, "division by zero");
4567 if (mpz_sgn(right_val
) != 0)
4568 mpz_tdiv_r(val
, left_val
, right_val
);
4571 error_at(location
, "division by zero");
4576 case OPERATOR_LSHIFT
:
4578 unsigned long shift
= mpz_get_ui(right_val
);
4579 if (mpz_cmp_ui(right_val
, shift
) != 0 || shift
> 0x100000)
4581 error_at(location
, "shift count overflow");
4585 mpz_mul_2exp(val
, left_val
, shift
);
4590 case OPERATOR_RSHIFT
:
4592 unsigned long shift
= mpz_get_ui(right_val
);
4593 if (mpz_cmp_ui(right_val
, shift
) != 0)
4595 error_at(location
, "shift count overflow");
4599 if (mpz_cmp_ui(left_val
, 0) >= 0)
4600 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4602 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4608 mpz_and(val
, left_val
, right_val
);
4610 case OPERATOR_BITCLEAR
:
4614 mpz_com(tval
, right_val
);
4615 mpz_and(val
, left_val
, tval
);
4623 Type
* type
= left_type
;
4628 else if (type
!= right_type
&& right_type
!= NULL
)
4630 if (type
->is_abstract())
4632 else if (!right_type
->is_abstract())
4634 // This look like a type error which should be diagnosed
4635 // elsewhere. Don't do anything here, to avoid an
4636 // unhelpful chain of error messages.
4642 if (type
!= NULL
&& !type
->is_abstract())
4644 // We have to check the operands too, as we have implicitly
4645 // coerced them to TYPE.
4646 if ((type
!= left_type
4647 && !Integer_expression::check_constant(left_val
, type
, location
))
4649 && type
!= right_type
4650 && !Integer_expression::check_constant(right_val
, type
,
4652 || !Integer_expression::check_constant(val
, type
, location
))
4659 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4660 // Return true if this could be done, false if not.
4663 Binary_expression::eval_float(Operator op
, Type
* left_type
, mpfr_t left_val
,
4664 Type
* right_type
, mpfr_t right_val
,
4665 mpfr_t val
, source_location location
)
4670 case OPERATOR_ANDAND
:
4672 case OPERATOR_NOTEQ
:
4677 // These return boolean values. We should probably handle them
4678 // anyhow in case a type conversion is used on the result.
4681 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4683 case OPERATOR_MINUS
:
4684 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4689 case OPERATOR_BITCLEAR
:
4692 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4695 if (mpfr_zero_p(right_val
))
4696 error_at(location
, "division by zero");
4697 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4701 case OPERATOR_LSHIFT
:
4702 case OPERATOR_RSHIFT
:
4708 Type
* type
= left_type
;
4711 else if (type
!= right_type
&& right_type
!= NULL
)
4713 if (type
->is_abstract())
4715 else if (!right_type
->is_abstract())
4717 // This looks like a type error which should be diagnosed
4718 // elsewhere. Don't do anything here, to avoid an unhelpful
4719 // chain of error messages.
4724 if (type
!= NULL
&& !type
->is_abstract())
4726 if ((type
!= left_type
4727 && !Float_expression::check_constant(left_val
, type
, location
))
4728 || (type
!= right_type
4729 && !Float_expression::check_constant(right_val
, type
,
4731 || !Float_expression::check_constant(val
, type
, location
))
4732 mpfr_set_ui(val
, 0, GMP_RNDN
);
4738 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4739 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4740 // could be done, false if not.
4743 Binary_expression::eval_complex(Operator op
, Type
* left_type
,
4744 mpfr_t left_real
, mpfr_t left_imag
,
4746 mpfr_t right_real
, mpfr_t right_imag
,
4747 mpfr_t real
, mpfr_t imag
,
4748 source_location location
)
4753 case OPERATOR_ANDAND
:
4755 case OPERATOR_NOTEQ
:
4760 // These return boolean values and must be handled differently.
4763 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4764 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4766 case OPERATOR_MINUS
:
4767 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4768 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4773 case OPERATOR_BITCLEAR
:
4777 // You might think that multiplying two complex numbers would
4778 // be simple, and you would be right, until you start to think
4779 // about getting the right answer for infinity. If one
4780 // operand here is infinity and the other is anything other
4781 // than zero or NaN, then we are going to wind up subtracting
4782 // two infinity values. That will give us a NaN, but the
4783 // correct answer is infinity.
4787 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4791 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4795 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4799 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4801 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4802 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4804 // If we get NaN on both sides, check whether it should really
4805 // be infinity. The rule is that if either side of the
4806 // complex number is infinity, then the whole value is
4807 // infinity, even if the other side is NaN. So the only case
4808 // we have to fix is the one in which both sides are NaN.
4809 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4810 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4811 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4813 bool is_infinity
= false;
4817 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4818 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4822 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4823 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4825 // If the left side is infinity, then the result is
4827 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4829 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4830 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4831 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4832 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4835 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4836 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4840 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4841 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4846 // If the right side is infinity, then the result is
4848 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4850 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4851 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4852 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4853 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4856 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4857 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4861 mpfr_set_ui(li
, 0, GMP_RNDN
);
4862 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4867 // If we got an overflow in the intermediate computations,
4868 // then the result is infinity.
4870 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4871 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4875 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4876 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4880 mpfr_set_ui(li
, 0, GMP_RNDN
);
4881 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4885 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4886 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4890 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4891 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4898 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4899 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4900 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4901 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4902 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4903 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4904 mpfr_set_inf(real
, mpfr_sgn(real
));
4905 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4922 // For complex division we want to avoid having an
4923 // intermediate overflow turn the whole result in a NaN. We
4924 // scale the values to try to avoid this.
4926 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4927 error_at(location
, "division by zero");
4933 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4934 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4937 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4941 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4942 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4944 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4946 ilogbw
= mpfr_get_exp(t
);
4947 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4948 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4953 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4954 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4955 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4957 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4958 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4959 mpfr_add(real
, real
, t
, GMP_RNDN
);
4960 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4961 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4963 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4964 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4965 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4966 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4967 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4969 // If we wind up with NaN on both sides, check whether we
4970 // should really have infinity. The rule is that if either
4971 // side of the complex number is infinity, then the whole
4972 // value is infinity, even if the other side is NaN. So the
4973 // only case we have to fix is the one in which both sides are
4975 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4976 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4977 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4979 if (mpfr_zero_p(denom
))
4981 mpfr_set_inf(real
, mpfr_sgn(rr
));
4982 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4983 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4984 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4986 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4987 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4989 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4990 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4993 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4994 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4998 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
5002 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
5004 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
5005 mpfr_set_inf(real
, mpfr_sgn(t3
));
5007 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
5008 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
5009 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
5010 mpfr_set_inf(imag
, mpfr_sgn(t3
));
5016 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
5017 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
5019 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
5020 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
5023 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
5024 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
5028 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
5032 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
5034 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
5035 mpfr_set_ui(real
, 0, GMP_RNDN
);
5036 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
5038 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
5039 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
5040 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
5041 mpfr_set_ui(imag
, 0, GMP_RNDN
);
5042 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
5060 case OPERATOR_LSHIFT
:
5061 case OPERATOR_RSHIFT
:
5067 Type
* type
= left_type
;
5070 else if (type
!= right_type
&& right_type
!= NULL
)
5072 if (type
->is_abstract())
5074 else if (!right_type
->is_abstract())
5076 // This looks like a type error which should be diagnosed
5077 // elsewhere. Don't do anything here, to avoid an unhelpful
5078 // chain of error messages.
5083 if (type
!= NULL
&& !type
->is_abstract())
5085 if ((type
!= left_type
5086 && !Complex_expression::check_constant(left_real
, left_imag
,
5088 || (type
!= right_type
5089 && !Complex_expression::check_constant(right_real
, right_imag
,
5091 || !Complex_expression::check_constant(real
, imag
, type
,
5094 mpfr_set_ui(real
, 0, GMP_RNDN
);
5095 mpfr_set_ui(imag
, 0, GMP_RNDN
);
5102 // Lower a binary expression. We have to evaluate constant
5103 // expressions now, in order to implement Go's unlimited precision
5107 Binary_expression::do_lower(Gogo
*, Named_object
*, int)
5109 source_location location
= this->location();
5110 Operator op
= this->op_
;
5111 Expression
* left
= this->left_
;
5112 Expression
* right
= this->right_
;
5114 const bool is_comparison
= (op
== OPERATOR_EQEQ
5115 || op
== OPERATOR_NOTEQ
5116 || op
== OPERATOR_LT
5117 || op
== OPERATOR_LE
5118 || op
== OPERATOR_GT
5119 || op
== OPERATOR_GE
);
5121 // Integer constant expressions.
5127 mpz_init(right_val
);
5129 if (left
->integer_constant_value(false, left_val
, &left_type
)
5130 && right
->integer_constant_value(false, right_val
, &right_type
))
5132 Expression
* ret
= NULL
;
5133 if (left_type
!= right_type
5134 && left_type
!= NULL
5135 && right_type
!= NULL
5136 && left_type
->base() != right_type
->base()
5137 && op
!= OPERATOR_LSHIFT
5138 && op
!= OPERATOR_RSHIFT
)
5140 // May be a type error--let it be diagnosed later.
5142 else if (is_comparison
)
5144 bool b
= Binary_expression::compare_integer(op
, left_val
,
5146 ret
= Expression::make_cast(Type::lookup_bool_type(),
5147 Expression::make_boolean(b
, location
),
5155 if (Binary_expression::eval_integer(op
, left_type
, left_val
,
5156 right_type
, right_val
,
5159 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
);
5161 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
5163 else if (left_type
== NULL
)
5165 else if (right_type
== NULL
)
5167 else if (!left_type
->is_abstract()
5168 && left_type
->named_type() != NULL
)
5170 else if (!right_type
->is_abstract()
5171 && right_type
->named_type() != NULL
)
5173 else if (!left_type
->is_abstract())
5175 else if (!right_type
->is_abstract())
5177 else if (left_type
->float_type() != NULL
)
5179 else if (right_type
->float_type() != NULL
)
5181 else if (left_type
->complex_type() != NULL
)
5183 else if (right_type
->complex_type() != NULL
)
5187 ret
= Expression::make_integer(&val
, type
, location
);
5195 mpz_clear(right_val
);
5196 mpz_clear(left_val
);
5200 mpz_clear(right_val
);
5201 mpz_clear(left_val
);
5204 // Floating point constant expressions.
5207 mpfr_init(left_val
);
5210 mpfr_init(right_val
);
5212 if (left
->float_constant_value(left_val
, &left_type
)
5213 && right
->float_constant_value(right_val
, &right_type
))
5215 Expression
* ret
= NULL
;
5216 if (left_type
!= right_type
5217 && left_type
!= NULL
5218 && right_type
!= NULL
5219 && left_type
->base() != right_type
->base()
5220 && op
!= OPERATOR_LSHIFT
5221 && op
!= OPERATOR_RSHIFT
)
5223 // May be a type error--let it be diagnosed later.
5225 else if (is_comparison
)
5227 bool b
= Binary_expression::compare_float(op
,
5231 left_val
, right_val
);
5232 ret
= Expression::make_boolean(b
, location
);
5239 if (Binary_expression::eval_float(op
, left_type
, left_val
,
5240 right_type
, right_val
, val
,
5243 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5244 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5246 if (left_type
== NULL
)
5248 else if (right_type
== NULL
)
5250 else if (!left_type
->is_abstract()
5251 && left_type
->named_type() != NULL
)
5253 else if (!right_type
->is_abstract()
5254 && right_type
->named_type() != NULL
)
5256 else if (!left_type
->is_abstract())
5258 else if (!right_type
->is_abstract())
5260 else if (left_type
->float_type() != NULL
)
5262 else if (right_type
->float_type() != NULL
)
5266 ret
= Expression::make_float(&val
, type
, location
);
5274 mpfr_clear(right_val
);
5275 mpfr_clear(left_val
);
5279 mpfr_clear(right_val
);
5280 mpfr_clear(left_val
);
5283 // Complex constant expressions.
5287 mpfr_init(left_real
);
5288 mpfr_init(left_imag
);
5293 mpfr_init(right_real
);
5294 mpfr_init(right_imag
);
5297 if (left
->complex_constant_value(left_real
, left_imag
, &left_type
)
5298 && right
->complex_constant_value(right_real
, right_imag
, &right_type
))
5300 Expression
* ret
= NULL
;
5301 if (left_type
!= right_type
5302 && left_type
!= NULL
5303 && right_type
!= NULL
5304 && left_type
->base() != right_type
->base())
5306 // May be a type error--let it be diagnosed later.
5308 else if (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
)
5310 bool b
= Binary_expression::compare_complex(op
,
5318 ret
= Expression::make_boolean(b
, location
);
5327 if (Binary_expression::eval_complex(op
, left_type
,
5328 left_real
, left_imag
,
5330 right_real
, right_imag
,
5334 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5335 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5337 if (left_type
== NULL
)
5339 else if (right_type
== NULL
)
5341 else if (!left_type
->is_abstract()
5342 && left_type
->named_type() != NULL
)
5344 else if (!right_type
->is_abstract()
5345 && right_type
->named_type() != NULL
)
5347 else if (!left_type
->is_abstract())
5349 else if (!right_type
->is_abstract())
5351 else if (left_type
->complex_type() != NULL
)
5353 else if (right_type
->complex_type() != NULL
)
5357 ret
= Expression::make_complex(&real
, &imag
, type
,
5366 mpfr_clear(left_real
);
5367 mpfr_clear(left_imag
);
5368 mpfr_clear(right_real
);
5369 mpfr_clear(right_imag
);
5374 mpfr_clear(left_real
);
5375 mpfr_clear(left_imag
);
5376 mpfr_clear(right_real
);
5377 mpfr_clear(right_imag
);
5380 // String constant expressions.
5381 if (op
== OPERATOR_PLUS
5382 && left
->type()->is_string_type()
5383 && right
->type()->is_string_type())
5385 std::string left_string
;
5386 std::string right_string
;
5387 if (left
->string_constant_value(&left_string
)
5388 && right
->string_constant_value(&right_string
))
5389 return Expression::make_string(left_string
+ right_string
, location
);
5395 // Return the integer constant value, if it has one.
5398 Binary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
5404 if (!this->left_
->integer_constant_value(iota_is_constant
, left_val
,
5407 mpz_clear(left_val
);
5412 mpz_init(right_val
);
5414 if (!this->right_
->integer_constant_value(iota_is_constant
, right_val
,
5417 mpz_clear(right_val
);
5418 mpz_clear(left_val
);
5423 if (left_type
!= right_type
5424 && left_type
!= NULL
5425 && right_type
!= NULL
5426 && left_type
->base() != right_type
->base()
5427 && this->op_
!= OPERATOR_RSHIFT
5428 && this->op_
!= OPERATOR_LSHIFT
)
5431 ret
= Binary_expression::eval_integer(this->op_
, left_type
, left_val
,
5432 right_type
, right_val
,
5433 this->location(), val
);
5435 mpz_clear(right_val
);
5436 mpz_clear(left_val
);
5444 // Return the floating point constant value, if it has one.
5447 Binary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
5450 mpfr_init(left_val
);
5452 if (!this->left_
->float_constant_value(left_val
, &left_type
))
5454 mpfr_clear(left_val
);
5459 mpfr_init(right_val
);
5461 if (!this->right_
->float_constant_value(right_val
, &right_type
))
5463 mpfr_clear(right_val
);
5464 mpfr_clear(left_val
);
5469 if (left_type
!= right_type
5470 && left_type
!= NULL
5471 && right_type
!= NULL
5472 && left_type
->base() != right_type
->base())
5475 ret
= Binary_expression::eval_float(this->op_
, left_type
, left_val
,
5476 right_type
, right_val
,
5477 val
, this->location());
5479 mpfr_clear(left_val
);
5480 mpfr_clear(right_val
);
5488 // Return the complex constant value, if it has one.
5491 Binary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
5496 mpfr_init(left_real
);
5497 mpfr_init(left_imag
);
5499 if (!this->left_
->complex_constant_value(left_real
, left_imag
, &left_type
))
5501 mpfr_clear(left_real
);
5502 mpfr_clear(left_imag
);
5508 mpfr_init(right_real
);
5509 mpfr_init(right_imag
);
5511 if (!this->right_
->complex_constant_value(right_real
, right_imag
,
5514 mpfr_clear(left_real
);
5515 mpfr_clear(left_imag
);
5516 mpfr_clear(right_real
);
5517 mpfr_clear(right_imag
);
5522 if (left_type
!= right_type
5523 && left_type
!= NULL
5524 && right_type
!= NULL
5525 && left_type
->base() != right_type
->base())
5528 ret
= Binary_expression::eval_complex(this->op_
, left_type
,
5529 left_real
, left_imag
,
5531 right_real
, right_imag
,
5534 mpfr_clear(left_real
);
5535 mpfr_clear(left_imag
);
5536 mpfr_clear(right_real
);
5537 mpfr_clear(right_imag
);
5545 // Note that the value is being discarded.
5548 Binary_expression::do_discarding_value()
5550 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5551 this->right_
->discarding_value();
5553 this->warn_about_unused_value();
5559 Binary_expression::do_type()
5561 if (this->classification() == EXPRESSION_ERROR
)
5562 return Type::make_error_type();
5567 case OPERATOR_ANDAND
:
5569 case OPERATOR_NOTEQ
:
5574 return Type::lookup_bool_type();
5577 case OPERATOR_MINUS
:
5584 case OPERATOR_BITCLEAR
:
5586 Type
* left_type
= this->left_
->type();
5587 Type
* right_type
= this->right_
->type();
5588 if (left_type
->is_error())
5590 else if (right_type
->is_error())
5592 else if (!Type::are_compatible_for_binop(left_type
, right_type
))
5594 this->report_error(_("incompatible types in binary expression"));
5595 return Type::make_error_type();
5597 else if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5599 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5601 else if (!left_type
->is_abstract())
5603 else if (!right_type
->is_abstract())
5605 else if (left_type
->complex_type() != NULL
)
5607 else if (right_type
->complex_type() != NULL
)
5609 else if (left_type
->float_type() != NULL
)
5611 else if (right_type
->float_type() != NULL
)
5617 case OPERATOR_LSHIFT
:
5618 case OPERATOR_RSHIFT
:
5619 return this->left_
->type();
5626 // Set type for a binary expression.
5629 Binary_expression::do_determine_type(const Type_context
* context
)
5631 Type
* tleft
= this->left_
->type();
5632 Type
* tright
= this->right_
->type();
5634 // Both sides should have the same type, except for the shift
5635 // operations. For a comparison, we should ignore the incoming
5638 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5639 || this->op_
== OPERATOR_RSHIFT
);
5641 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5642 || this->op_
== OPERATOR_NOTEQ
5643 || this->op_
== OPERATOR_LT
5644 || this->op_
== OPERATOR_LE
5645 || this->op_
== OPERATOR_GT
5646 || this->op_
== OPERATOR_GE
);
5648 Type_context
subcontext(*context
);
5652 // In a comparison, the context does not determine the types of
5654 subcontext
.type
= NULL
;
5657 // Set the context for the left hand operand.
5660 // The right hand operand plays no role in determining the type
5661 // of the left hand operand. A shift of an abstract integer in
5662 // a string context gets special treatment, which may be a
5664 if (subcontext
.type
!= NULL
5665 && subcontext
.type
->is_string_type()
5666 && tleft
->is_abstract())
5667 error_at(this->location(), "shift of non-integer operand");
5669 else if (!tleft
->is_abstract())
5670 subcontext
.type
= tleft
;
5671 else if (!tright
->is_abstract())
5672 subcontext
.type
= tright
;
5673 else if (subcontext
.type
== NULL
)
5675 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5676 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5677 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5679 // Both sides have an abstract integer, abstract float, or
5680 // abstract complex type. Just let CONTEXT determine
5681 // whether they may remain abstract or not.
5683 else if (tleft
->complex_type() != NULL
)
5684 subcontext
.type
= tleft
;
5685 else if (tright
->complex_type() != NULL
)
5686 subcontext
.type
= tright
;
5687 else if (tleft
->float_type() != NULL
)
5688 subcontext
.type
= tleft
;
5689 else if (tright
->float_type() != NULL
)
5690 subcontext
.type
= tright
;
5692 subcontext
.type
= tleft
;
5694 if (subcontext
.type
!= NULL
&& !context
->may_be_abstract
)
5695 subcontext
.type
= subcontext
.type
->make_non_abstract_type();
5698 this->left_
->determine_type(&subcontext
);
5700 // The context for the right hand operand is the same as for the
5701 // left hand operand, except for a shift operator.
5704 subcontext
.type
= Type::lookup_integer_type("uint");
5705 subcontext
.may_be_abstract
= false;
5708 this->right_
->determine_type(&subcontext
);
5711 // Report an error if the binary operator OP does not support TYPE.
5712 // Return whether the operation is OK. This should not be used for
5716 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5717 source_location location
)
5722 case OPERATOR_ANDAND
:
5723 if (!type
->is_boolean_type())
5725 error_at(location
, "expected boolean type");
5731 case OPERATOR_NOTEQ
:
5732 if (type
->integer_type() == NULL
5733 && type
->float_type() == NULL
5734 && type
->complex_type() == NULL
5735 && !type
->is_string_type()
5736 && type
->points_to() == NULL
5737 && !type
->is_nil_type()
5738 && !type
->is_boolean_type()
5739 && type
->interface_type() == NULL
5740 && (type
->array_type() == NULL
5741 || type
->array_type()->length() != NULL
)
5742 && type
->map_type() == NULL
5743 && type
->channel_type() == NULL
5744 && type
->function_type() == NULL
)
5747 ("expected integer, floating, complex, string, pointer, "
5748 "boolean, interface, slice, map, channel, "
5749 "or function type"));
5758 if (type
->integer_type() == NULL
5759 && type
->float_type() == NULL
5760 && !type
->is_string_type())
5762 error_at(location
, "expected integer, floating, or string type");
5768 case OPERATOR_PLUSEQ
:
5769 if (type
->integer_type() == NULL
5770 && type
->float_type() == NULL
5771 && type
->complex_type() == NULL
5772 && !type
->is_string_type())
5775 "expected integer, floating, complex, or string type");
5780 case OPERATOR_MINUS
:
5781 case OPERATOR_MINUSEQ
:
5783 case OPERATOR_MULTEQ
:
5785 case OPERATOR_DIVEQ
:
5786 if (type
->integer_type() == NULL
5787 && type
->float_type() == NULL
5788 && type
->complex_type() == NULL
)
5790 error_at(location
, "expected integer, floating, or complex type");
5796 case OPERATOR_MODEQ
:
5800 case OPERATOR_ANDEQ
:
5802 case OPERATOR_XOREQ
:
5803 case OPERATOR_BITCLEAR
:
5804 case OPERATOR_BITCLEAREQ
:
5805 if (type
->integer_type() == NULL
)
5807 error_at(location
, "expected integer type");
5822 Binary_expression::do_check_types(Gogo
*)
5824 if (this->classification() == EXPRESSION_ERROR
)
5827 Type
* left_type
= this->left_
->type();
5828 Type
* right_type
= this->right_
->type();
5829 if (left_type
->is_error() || right_type
->is_error())
5831 this->set_is_error();
5835 if (this->op_
== OPERATOR_EQEQ
5836 || this->op_
== OPERATOR_NOTEQ
5837 || this->op_
== OPERATOR_LT
5838 || this->op_
== OPERATOR_LE
5839 || this->op_
== OPERATOR_GT
5840 || this->op_
== OPERATOR_GE
)
5842 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5843 && !Type::are_assignable(right_type
, left_type
, NULL
))
5845 this->report_error(_("incompatible types in binary expression"));
5848 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5850 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5853 this->set_is_error();
5857 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5859 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5861 this->report_error(_("incompatible types in binary expression"));
5864 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5867 this->set_is_error();
5873 if (left_type
->integer_type() == NULL
)
5874 this->report_error(_("shift of non-integer operand"));
5876 if (!right_type
->is_abstract()
5877 && (right_type
->integer_type() == NULL
5878 || !right_type
->integer_type()->is_unsigned()))
5879 this->report_error(_("shift count not unsigned integer"));
5885 if (this->right_
->integer_constant_value(true, val
, &type
))
5887 if (mpz_sgn(val
) < 0)
5889 this->report_error(_("negative shift count"));
5891 source_location rloc
= this->right_
->location();
5892 this->right_
= Expression::make_integer(&val
, right_type
,
5901 // Get a tree for a binary expression.
5904 Binary_expression::do_get_tree(Translate_context
* context
)
5906 tree left
= this->left_
->get_tree(context
);
5907 tree right
= this->right_
->get_tree(context
);
5909 if (left
== error_mark_node
|| right
== error_mark_node
)
5910 return error_mark_node
;
5912 enum tree_code code
;
5913 bool use_left_type
= true;
5914 bool is_shift_op
= false;
5918 case OPERATOR_NOTEQ
:
5923 return Expression::comparison_tree(context
, this->op_
,
5924 this->left_
->type(), left
,
5925 this->right_
->type(), right
,
5929 code
= TRUTH_ORIF_EXPR
;
5930 use_left_type
= false;
5932 case OPERATOR_ANDAND
:
5933 code
= TRUTH_ANDIF_EXPR
;
5934 use_left_type
= false;
5939 case OPERATOR_MINUS
:
5943 code
= BIT_IOR_EXPR
;
5946 code
= BIT_XOR_EXPR
;
5953 Type
*t
= this->left_
->type();
5954 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5957 code
= TRUNC_DIV_EXPR
;
5961 code
= TRUNC_MOD_EXPR
;
5963 case OPERATOR_LSHIFT
:
5967 case OPERATOR_RSHIFT
:
5972 code
= BIT_AND_EXPR
;
5974 case OPERATOR_BITCLEAR
:
5975 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5976 code
= BIT_AND_EXPR
;
5982 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5984 if (this->left_
->type()->is_string_type())
5986 gcc_assert(this->op_
== OPERATOR_PLUS
);
5987 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5988 static tree string_plus_decl
;
5989 return Gogo::call_builtin(&string_plus_decl
,
6000 tree compute_type
= excess_precision_type(type
);
6001 if (compute_type
!= NULL_TREE
)
6003 left
= ::convert(compute_type
, left
);
6004 right
= ::convert(compute_type
, right
);
6007 tree eval_saved
= NULL_TREE
;
6010 // Make sure the values are evaluated.
6011 if (!DECL_P(left
) && TREE_SIDE_EFFECTS(left
))
6013 left
= save_expr(left
);
6016 if (!DECL_P(right
) && TREE_SIDE_EFFECTS(right
))
6018 right
= save_expr(right
);
6019 if (eval_saved
== NULL_TREE
)
6022 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
6023 void_type_node
, eval_saved
, right
);
6027 tree ret
= fold_build2_loc(this->location(),
6029 compute_type
!= NULL_TREE
? compute_type
: type
,
6032 if (compute_type
!= NULL_TREE
)
6033 ret
= ::convert(type
, ret
);
6035 // In Go, a shift larger than the size of the type is well-defined.
6036 // This is not true in GENERIC, so we need to insert a conditional.
6039 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
6040 gcc_assert(this->left_
->type()->integer_type() != NULL
);
6041 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
6043 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
6044 build_int_cst_type(TREE_TYPE(right
), bits
));
6046 tree overflow_result
= fold_convert_loc(this->location(),
6049 if (this->op_
== OPERATOR_RSHIFT
6050 && !this->left_
->type()->integer_type()->is_unsigned())
6052 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
6053 boolean_type_node
, left
,
6054 fold_convert_loc(this->location(),
6056 integer_zero_node
));
6057 tree neg_one
= fold_build2_loc(this->location(),
6058 MINUS_EXPR
, TREE_TYPE(left
),
6059 fold_convert_loc(this->location(),
6062 fold_convert_loc(this->location(),
6065 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
6066 TREE_TYPE(left
), neg
, neg_one
,
6070 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
6071 compare
, ret
, overflow_result
);
6073 if (eval_saved
!= NULL_TREE
)
6074 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
6075 TREE_TYPE(ret
), eval_saved
, ret
);
6081 // Export a binary expression.
6084 Binary_expression::do_export(Export
* exp
) const
6086 exp
->write_c_string("(");
6087 this->left_
->export_expression(exp
);
6091 exp
->write_c_string(" || ");
6093 case OPERATOR_ANDAND
:
6094 exp
->write_c_string(" && ");
6097 exp
->write_c_string(" == ");
6099 case OPERATOR_NOTEQ
:
6100 exp
->write_c_string(" != ");
6103 exp
->write_c_string(" < ");
6106 exp
->write_c_string(" <= ");
6109 exp
->write_c_string(" > ");
6112 exp
->write_c_string(" >= ");
6115 exp
->write_c_string(" + ");
6117 case OPERATOR_MINUS
:
6118 exp
->write_c_string(" - ");
6121 exp
->write_c_string(" | ");
6124 exp
->write_c_string(" ^ ");
6127 exp
->write_c_string(" * ");
6130 exp
->write_c_string(" / ");
6133 exp
->write_c_string(" % ");
6135 case OPERATOR_LSHIFT
:
6136 exp
->write_c_string(" << ");
6138 case OPERATOR_RSHIFT
:
6139 exp
->write_c_string(" >> ");
6142 exp
->write_c_string(" & ");
6144 case OPERATOR_BITCLEAR
:
6145 exp
->write_c_string(" &^ ");
6150 this->right_
->export_expression(exp
);
6151 exp
->write_c_string(")");
6154 // Import a binary expression.
6157 Binary_expression::do_import(Import
* imp
)
6159 imp
->require_c_string("(");
6161 Expression
* left
= Expression::import_expression(imp
);
6164 if (imp
->match_c_string(" || "))
6169 else if (imp
->match_c_string(" && "))
6171 op
= OPERATOR_ANDAND
;
6174 else if (imp
->match_c_string(" == "))
6179 else if (imp
->match_c_string(" != "))
6181 op
= OPERATOR_NOTEQ
;
6184 else if (imp
->match_c_string(" < "))
6189 else if (imp
->match_c_string(" <= "))
6194 else if (imp
->match_c_string(" > "))
6199 else if (imp
->match_c_string(" >= "))
6204 else if (imp
->match_c_string(" + "))
6209 else if (imp
->match_c_string(" - "))
6211 op
= OPERATOR_MINUS
;
6214 else if (imp
->match_c_string(" | "))
6219 else if (imp
->match_c_string(" ^ "))
6224 else if (imp
->match_c_string(" * "))
6229 else if (imp
->match_c_string(" / "))
6234 else if (imp
->match_c_string(" % "))
6239 else if (imp
->match_c_string(" << "))
6241 op
= OPERATOR_LSHIFT
;
6244 else if (imp
->match_c_string(" >> "))
6246 op
= OPERATOR_RSHIFT
;
6249 else if (imp
->match_c_string(" & "))
6254 else if (imp
->match_c_string(" &^ "))
6256 op
= OPERATOR_BITCLEAR
;
6261 error_at(imp
->location(), "unrecognized binary operator");
6262 return Expression::make_error(imp
->location());
6265 Expression
* right
= Expression::import_expression(imp
);
6267 imp
->require_c_string(")");
6269 return Expression::make_binary(op
, left
, right
, imp
->location());
6272 // Make a binary expression.
6275 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6276 source_location location
)
6278 return new Binary_expression(op
, left
, right
, location
);
6281 // Implement a comparison.
6284 Expression::comparison_tree(Translate_context
* context
, Operator op
,
6285 Type
* left_type
, tree left_tree
,
6286 Type
* right_type
, tree right_tree
,
6287 source_location location
)
6289 enum tree_code code
;
6295 case OPERATOR_NOTEQ
:
6314 if (left_type
->is_string_type() && right_type
->is_string_type())
6316 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
6317 static tree string_compare_decl
;
6318 left_tree
= Gogo::call_builtin(&string_compare_decl
,
6327 right_tree
= build_int_cst_type(integer_type_node
, 0);
6329 else if ((left_type
->interface_type() != NULL
6330 && right_type
->interface_type() == NULL
6331 && !right_type
->is_nil_type())
6332 || (left_type
->interface_type() == NULL
6333 && !left_type
->is_nil_type()
6334 && right_type
->interface_type() != NULL
))
6336 // Comparing an interface value to a non-interface value.
6337 if (left_type
->interface_type() == NULL
)
6339 std::swap(left_type
, right_type
);
6340 std::swap(left_tree
, right_tree
);
6343 // The right operand is not an interface. We need to take its
6344 // address if it is not a pointer.
6347 if (right_type
->points_to() != NULL
)
6349 make_tmp
= NULL_TREE
;
6352 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
6354 make_tmp
= NULL_TREE
;
6355 arg
= build_fold_addr_expr_loc(location
, right_tree
);
6356 if (DECL_P(right_tree
))
6357 TREE_ADDRESSABLE(right_tree
) = 1;
6361 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
6362 get_name(right_tree
));
6363 DECL_IGNORED_P(tmp
) = 0;
6364 DECL_INITIAL(tmp
) = right_tree
;
6365 TREE_ADDRESSABLE(tmp
) = 1;
6366 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6367 SET_EXPR_LOCATION(make_tmp
, location
);
6368 arg
= build_fold_addr_expr_loc(location
, tmp
);
6370 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
6372 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
6374 if (left_type
->interface_type()->is_empty())
6376 static tree empty_interface_value_compare_decl
;
6377 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6379 "__go_empty_interface_value_compare",
6382 TREE_TYPE(left_tree
),
6384 TREE_TYPE(descriptor
),
6388 if (left_tree
== error_mark_node
)
6389 return error_mark_node
;
6390 // This can panic if the type is not comparable.
6391 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6395 static tree interface_value_compare_decl
;
6396 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6398 "__go_interface_value_compare",
6401 TREE_TYPE(left_tree
),
6403 TREE_TYPE(descriptor
),
6407 if (left_tree
== error_mark_node
)
6408 return error_mark_node
;
6409 // This can panic if the type is not comparable.
6410 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6412 right_tree
= build_int_cst_type(integer_type_node
, 0);
6414 if (make_tmp
!= NULL_TREE
)
6415 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6418 else if (left_type
->interface_type() != NULL
6419 && right_type
->interface_type() != NULL
)
6421 if (left_type
->interface_type()->is_empty()
6422 && right_type
->interface_type()->is_empty())
6424 static tree empty_interface_compare_decl
;
6425 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6427 "__go_empty_interface_compare",
6430 TREE_TYPE(left_tree
),
6432 TREE_TYPE(right_tree
),
6434 if (left_tree
== error_mark_node
)
6435 return error_mark_node
;
6436 // This can panic if the type is uncomparable.
6437 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6439 else if (!left_type
->interface_type()->is_empty()
6440 && !right_type
->interface_type()->is_empty())
6442 static tree interface_compare_decl
;
6443 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6445 "__go_interface_compare",
6448 TREE_TYPE(left_tree
),
6450 TREE_TYPE(right_tree
),
6452 if (left_tree
== error_mark_node
)
6453 return error_mark_node
;
6454 // This can panic if the type is uncomparable.
6455 TREE_NOTHROW(interface_compare_decl
) = 0;
6459 if (left_type
->interface_type()->is_empty())
6461 gcc_assert(op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
);
6462 std::swap(left_type
, right_type
);
6463 std::swap(left_tree
, right_tree
);
6465 gcc_assert(!left_type
->interface_type()->is_empty());
6466 gcc_assert(right_type
->interface_type()->is_empty());
6467 static tree interface_empty_compare_decl
;
6468 left_tree
= Gogo::call_builtin(&interface_empty_compare_decl
,
6470 "__go_interface_empty_compare",
6473 TREE_TYPE(left_tree
),
6475 TREE_TYPE(right_tree
),
6477 if (left_tree
== error_mark_node
)
6478 return error_mark_node
;
6479 // This can panic if the type is uncomparable.
6480 TREE_NOTHROW(interface_empty_compare_decl
) = 0;
6483 right_tree
= build_int_cst_type(integer_type_node
, 0);
6486 if (left_type
->is_nil_type()
6487 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6489 std::swap(left_type
, right_type
);
6490 std::swap(left_tree
, right_tree
);
6493 if (right_type
->is_nil_type())
6495 if (left_type
->array_type() != NULL
6496 && left_type
->array_type()->length() == NULL
)
6498 Array_type
* at
= left_type
->array_type();
6499 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6500 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6502 else if (left_type
->interface_type() != NULL
)
6504 // An interface is nil if the first field is nil.
6505 tree left_type_tree
= TREE_TYPE(left_tree
);
6506 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6507 tree field
= TYPE_FIELDS(left_type_tree
);
6508 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6510 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6514 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6515 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6519 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6520 return error_mark_node
;
6522 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6523 if (CAN_HAVE_LOCATION_P(ret
))
6524 SET_EXPR_LOCATION(ret
, location
);
6528 // Class Bound_method_expression.
6533 Bound_method_expression::do_traverse(Traverse
* traverse
)
6535 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6536 return TRAVERSE_EXIT
;
6537 return Expression::traverse(&this->method_
, traverse
);
6540 // Return the type of a bound method expression. The type of this
6541 // object is really the type of the method with no receiver. We
6542 // should be able to get away with just returning the type of the
6546 Bound_method_expression::do_type()
6548 return this->method_
->type();
6551 // Determine the types of a method expression.
6554 Bound_method_expression::do_determine_type(const Type_context
*)
6556 this->method_
->determine_type_no_context();
6557 Type
* mtype
= this->method_
->type();
6558 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6559 if (fntype
== NULL
|| !fntype
->is_method())
6560 this->expr_
->determine_type_no_context();
6563 Type_context
subcontext(fntype
->receiver()->type(), false);
6564 this->expr_
->determine_type(&subcontext
);
6568 // Check the types of a method expression.
6571 Bound_method_expression::do_check_types(Gogo
*)
6573 Type
* type
= this->method_
->type()->deref();
6575 || type
->function_type() == NULL
6576 || !type
->function_type()->is_method())
6577 this->report_error(_("object is not a method"));
6580 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6581 Type
* etype
= (this->expr_type_
!= NULL
6583 : this->expr_
->type());
6584 etype
= etype
->deref();
6585 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6586 this->report_error(_("method type does not match object type"));
6590 // Get the tree for a method expression. There is no standard tree
6591 // representation for this. The only places it may currently be used
6592 // are in a Call_expression or a Go_statement, which will take it
6593 // apart directly. So this has nothing to do at present.
6596 Bound_method_expression::do_get_tree(Translate_context
*)
6598 error_at(this->location(), "reference to method other than calling it");
6599 return error_mark_node
;
6602 // Make a method expression.
6604 Bound_method_expression
*
6605 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6606 source_location location
)
6608 return new Bound_method_expression(expr
, method
, location
);
6611 // Class Builtin_call_expression. This is used for a call to a
6612 // builtin function.
6614 class Builtin_call_expression
: public Call_expression
6617 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6618 bool is_varargs
, source_location location
);
6621 // This overrides Call_expression::do_lower.
6623 do_lower(Gogo
*, Named_object
*, int);
6626 do_is_constant() const;
6629 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6632 do_float_constant_value(mpfr_t
, Type
**) const;
6635 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6641 do_determine_type(const Type_context
*);
6644 do_check_types(Gogo
*);
6649 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6650 this->args()->copy(),
6656 do_get_tree(Translate_context
*);
6659 do_export(Export
*) const;
6662 do_is_recover_call() const;
6665 do_set_recover_arg(Expression
*);
6668 // The builtin functions.
6669 enum Builtin_function_code
6673 // Predeclared builtin functions.
6689 // Builtin functions from the unsafe package.
6702 real_imag_type(Type
*);
6705 complex_type(Type
*);
6707 // A pointer back to the general IR structure. This avoids a global
6708 // variable, or passing it around everywhere.
6710 // The builtin function being called.
6711 Builtin_function_code code_
;
6712 // Used to stop endless loops when the length of an array uses len
6713 // or cap of the array itself.
6717 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6719 Expression_list
* args
,
6721 source_location location
)
6722 : Call_expression(fn
, args
, is_varargs
, location
),
6723 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false)
6725 Func_expression
* fnexp
= this->fn()->func_expression();
6726 gcc_assert(fnexp
!= NULL
);
6727 const std::string
& name(fnexp
->named_object()->name());
6728 if (name
== "append")
6729 this->code_
= BUILTIN_APPEND
;
6730 else if (name
== "cap")
6731 this->code_
= BUILTIN_CAP
;
6732 else if (name
== "close")
6733 this->code_
= BUILTIN_CLOSE
;
6734 else if (name
== "complex")
6735 this->code_
= BUILTIN_COMPLEX
;
6736 else if (name
== "copy")
6737 this->code_
= BUILTIN_COPY
;
6738 else if (name
== "imag")
6739 this->code_
= BUILTIN_IMAG
;
6740 else if (name
== "len")
6741 this->code_
= BUILTIN_LEN
;
6742 else if (name
== "make")
6743 this->code_
= BUILTIN_MAKE
;
6744 else if (name
== "new")
6745 this->code_
= BUILTIN_NEW
;
6746 else if (name
== "panic")
6747 this->code_
= BUILTIN_PANIC
;
6748 else if (name
== "print")
6749 this->code_
= BUILTIN_PRINT
;
6750 else if (name
== "println")
6751 this->code_
= BUILTIN_PRINTLN
;
6752 else if (name
== "real")
6753 this->code_
= BUILTIN_REAL
;
6754 else if (name
== "recover")
6755 this->code_
= BUILTIN_RECOVER
;
6756 else if (name
== "Alignof")
6757 this->code_
= BUILTIN_ALIGNOF
;
6758 else if (name
== "Offsetof")
6759 this->code_
= BUILTIN_OFFSETOF
;
6760 else if (name
== "Sizeof")
6761 this->code_
= BUILTIN_SIZEOF
;
6766 // Return whether this is a call to recover. This is a virtual
6767 // function called from the parent class.
6770 Builtin_call_expression::do_is_recover_call() const
6772 if (this->classification() == EXPRESSION_ERROR
)
6774 return this->code_
== BUILTIN_RECOVER
;
6777 // Set the argument for a call to recover.
6780 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6782 const Expression_list
* args
= this->args();
6783 gcc_assert(args
== NULL
|| args
->empty());
6784 Expression_list
* new_args
= new Expression_list();
6785 new_args
->push_back(arg
);
6786 this->set_args(new_args
);
6789 // A traversal class which looks for a call expression.
6791 class Find_call_expression
: public Traverse
6794 Find_call_expression()
6795 : Traverse(traverse_expressions
),
6800 expression(Expression
**);
6804 { return this->found_
; }
6811 Find_call_expression::expression(Expression
** pexpr
)
6813 if ((*pexpr
)->call_expression() != NULL
)
6815 this->found_
= true;
6816 return TRAVERSE_EXIT
;
6818 return TRAVERSE_CONTINUE
;
6821 // Lower a builtin call expression. This turns new and make into
6822 // specific expressions. We also convert to a constant if we can.
6825 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6827 if (this->is_varargs() && this->code_
!= BUILTIN_APPEND
)
6829 this->report_error(_("invalid use of %<...%> with builtin function"));
6830 return Expression::make_error(this->location());
6833 if (this->code_
== BUILTIN_NEW
)
6835 const Expression_list
* args
= this->args();
6836 if (args
== NULL
|| args
->size() < 1)
6837 this->report_error(_("not enough arguments"));
6838 else if (args
->size() > 1)
6839 this->report_error(_("too many arguments"));
6842 Expression
* arg
= args
->front();
6843 if (!arg
->is_type_expression())
6845 error_at(arg
->location(), "expected type");
6846 this->set_is_error();
6849 return Expression::make_allocation(arg
->type(), this->location());
6852 else if (this->code_
== BUILTIN_MAKE
)
6854 const Expression_list
* args
= this->args();
6855 if (args
== NULL
|| args
->size() < 1)
6856 this->report_error(_("not enough arguments"));
6859 Expression
* arg
= args
->front();
6860 if (!arg
->is_type_expression())
6862 error_at(arg
->location(), "expected type");
6863 this->set_is_error();
6867 Expression_list
* newargs
;
6868 if (args
->size() == 1)
6872 newargs
= new Expression_list();
6873 Expression_list::const_iterator p
= args
->begin();
6875 for (; p
!= args
->end(); ++p
)
6876 newargs
->push_back(*p
);
6878 return Expression::make_make(arg
->type(), newargs
,
6883 else if (this->is_constant())
6885 // We can only lower len and cap if there are no function calls
6886 // in the arguments. Otherwise we have to make the call.
6887 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6889 Expression
* arg
= this->one_arg();
6890 if (!arg
->is_constant())
6892 Find_call_expression find_call
;
6893 Expression::traverse(&arg
, &find_call
);
6894 if (find_call
.found())
6902 if (this->integer_constant_value(true, ival
, &type
))
6904 Expression
* ret
= Expression::make_integer(&ival
, type
,
6913 if (this->float_constant_value(rval
, &type
))
6915 Expression
* ret
= Expression::make_float(&rval
, type
,
6923 if (this->complex_constant_value(rval
, imag
, &type
))
6925 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6934 else if (this->code_
== BUILTIN_RECOVER
)
6936 if (function
!= NULL
)
6937 function
->func_value()->set_calls_recover();
6940 // Calling recover outside of a function always returns the
6941 // nil empty interface.
6942 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6943 return Expression::make_cast(eface
,
6944 Expression::make_nil(this->location()),
6948 else if (this->code_
== BUILTIN_APPEND
)
6950 // Lower the varargs.
6951 const Expression_list
* args
= this->args();
6952 if (args
== NULL
|| args
->empty())
6954 Type
* slice_type
= args
->front()->type();
6955 if (!slice_type
->is_open_array_type())
6957 error_at(args
->front()->location(), "argument 1 must be a slice");
6958 this->set_is_error();
6961 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6967 // Return the type of the real or imag functions, given the type of
6968 // the argument. We need to map complex to float, complex64 to
6969 // float32, and complex128 to float64, so it has to be done by name.
6970 // This returns NULL if it can't figure out the type.
6973 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6975 if (arg_type
== NULL
|| arg_type
->is_abstract())
6977 Named_type
* nt
= arg_type
->named_type();
6980 while (nt
->real_type()->named_type() != NULL
)
6981 nt
= nt
->real_type()->named_type();
6982 if (nt
->name() == "complex64")
6983 return Type::lookup_float_type("float32");
6984 else if (nt
->name() == "complex128")
6985 return Type::lookup_float_type("float64");
6990 // Return the type of the complex function, given the type of one of the
6991 // argments. Like real_imag_type, we have to map by name.
6994 Builtin_call_expression::complex_type(Type
* arg_type
)
6996 if (arg_type
== NULL
|| arg_type
->is_abstract())
6998 Named_type
* nt
= arg_type
->named_type();
7001 while (nt
->real_type()->named_type() != NULL
)
7002 nt
= nt
->real_type()->named_type();
7003 if (nt
->name() == "float32")
7004 return Type::lookup_complex_type("complex64");
7005 else if (nt
->name() == "float64")
7006 return Type::lookup_complex_type("complex128");
7011 // Return a single argument, or NULL if there isn't one.
7014 Builtin_call_expression::one_arg() const
7016 const Expression_list
* args
= this->args();
7017 if (args
->size() != 1)
7019 return args
->front();
7022 // Return whether this is constant: len of a string, or len or cap of
7023 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
7026 Builtin_call_expression::do_is_constant() const
7028 switch (this->code_
)
7036 Expression
* arg
= this->one_arg();
7039 Type
* arg_type
= arg
->type();
7041 if (arg_type
->points_to() != NULL
7042 && arg_type
->points_to()->array_type() != NULL
7043 && !arg_type
->points_to()->is_open_array_type())
7044 arg_type
= arg_type
->points_to();
7046 if (arg_type
->array_type() != NULL
7047 && arg_type
->array_type()->length() != NULL
)
7050 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
7053 bool ret
= arg
->is_constant();
7054 this->seen_
= false;
7060 case BUILTIN_SIZEOF
:
7061 case BUILTIN_ALIGNOF
:
7062 return this->one_arg() != NULL
;
7064 case BUILTIN_OFFSETOF
:
7066 Expression
* arg
= this->one_arg();
7069 return arg
->field_reference_expression() != NULL
;
7072 case BUILTIN_COMPLEX
:
7074 const Expression_list
* args
= this->args();
7075 if (args
!= NULL
&& args
->size() == 2)
7076 return args
->front()->is_constant() && args
->back()->is_constant();
7083 Expression
* arg
= this->one_arg();
7084 return arg
!= NULL
&& arg
->is_constant();
7094 // Return an integer constant value if possible.
7097 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
7101 if (this->code_
== BUILTIN_LEN
7102 || this->code_
== BUILTIN_CAP
)
7104 Expression
* arg
= this->one_arg();
7107 Type
* arg_type
= arg
->type();
7109 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
7112 if (arg
->string_constant_value(&sval
))
7114 mpz_set_ui(val
, sval
.length());
7115 *ptype
= Type::lookup_integer_type("int");
7120 if (arg_type
->points_to() != NULL
7121 && arg_type
->points_to()->array_type() != NULL
7122 && !arg_type
->points_to()->is_open_array_type())
7123 arg_type
= arg_type
->points_to();
7125 if (arg_type
->array_type() != NULL
7126 && arg_type
->array_type()->length() != NULL
)
7130 Expression
* e
= arg_type
->array_type()->length();
7132 bool r
= e
->integer_constant_value(iota_is_constant
, val
, ptype
);
7133 this->seen_
= false;
7136 *ptype
= Type::lookup_integer_type("int");
7141 else if (this->code_
== BUILTIN_SIZEOF
7142 || this->code_
== BUILTIN_ALIGNOF
)
7144 Expression
* arg
= this->one_arg();
7147 Type
* arg_type
= arg
->type();
7148 if (arg_type
->is_error())
7150 if (arg_type
->is_abstract())
7152 if (arg_type
->named_type() != NULL
)
7153 arg_type
->named_type()->convert(this->gogo_
);
7154 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
7155 if (arg_type_tree
== error_mark_node
)
7157 unsigned long val_long
;
7158 if (this->code_
== BUILTIN_SIZEOF
)
7160 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
7161 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
7162 if (TREE_INT_CST_HIGH(type_size
) != 0)
7164 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
7165 val_long
= static_cast<unsigned long>(val_wide
);
7166 if (val_long
!= val_wide
)
7169 else if (this->code_
== BUILTIN_ALIGNOF
)
7171 if (arg
->field_reference_expression() == NULL
)
7172 val_long
= go_type_alignment(arg_type_tree
);
7175 // Calling unsafe.Alignof(s.f) returns the alignment of
7176 // the type of f when it is used as a field in a struct.
7177 val_long
= go_field_alignment(arg_type_tree
);
7182 mpz_set_ui(val
, val_long
);
7186 else if (this->code_
== BUILTIN_OFFSETOF
)
7188 Expression
* arg
= this->one_arg();
7191 Field_reference_expression
* farg
= arg
->field_reference_expression();
7194 Expression
* struct_expr
= farg
->expr();
7195 Type
* st
= struct_expr
->type();
7196 if (st
->struct_type() == NULL
)
7198 if (st
->named_type() != NULL
)
7199 st
->named_type()->convert(this->gogo_
);
7200 tree struct_tree
= st
->get_tree(this->gogo_
);
7201 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
7202 tree field
= TYPE_FIELDS(struct_tree
);
7203 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
7205 field
= DECL_CHAIN(field
);
7206 gcc_assert(field
!= NULL_TREE
);
7208 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
7209 if (offset_wide
< 0)
7211 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
7212 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
7214 mpz_set_ui(val
, offset_long
);
7220 // Return a floating point constant value if possible.
7223 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
7226 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
7228 Expression
* arg
= this->one_arg();
7239 if (arg
->complex_constant_value(real
, imag
, &type
))
7241 if (this->code_
== BUILTIN_REAL
)
7242 mpfr_set(val
, real
, GMP_RNDN
);
7244 mpfr_set(val
, imag
, GMP_RNDN
);
7245 *ptype
= Builtin_call_expression::real_imag_type(type
);
7257 // Return a complex constant value if possible.
7260 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
7263 if (this->code_
== BUILTIN_COMPLEX
)
7265 const Expression_list
* args
= this->args();
7266 if (args
== NULL
|| args
->size() != 2)
7272 if (!args
->front()->float_constant_value(r
, &rtype
))
7283 if (args
->back()->float_constant_value(i
, &itype
)
7284 && Type::are_identical(rtype
, itype
, false, NULL
))
7286 mpfr_set(real
, r
, GMP_RNDN
);
7287 mpfr_set(imag
, i
, GMP_RNDN
);
7288 *ptype
= Builtin_call_expression::complex_type(rtype
);
7304 Builtin_call_expression::do_type()
7306 switch (this->code_
)
7308 case BUILTIN_INVALID
:
7315 const Expression_list
* args
= this->args();
7316 if (args
== NULL
|| args
->empty())
7317 return Type::make_error_type();
7318 return Type::make_pointer_type(args
->front()->type());
7324 case BUILTIN_ALIGNOF
:
7325 case BUILTIN_OFFSETOF
:
7326 case BUILTIN_SIZEOF
:
7327 return Type::lookup_integer_type("int");
7332 case BUILTIN_PRINTLN
:
7333 return Type::make_void_type();
7335 case BUILTIN_RECOVER
:
7336 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7338 case BUILTIN_APPEND
:
7340 const Expression_list
* args
= this->args();
7341 if (args
== NULL
|| args
->empty())
7342 return Type::make_error_type();
7343 return args
->front()->type();
7349 Expression
* arg
= this->one_arg();
7351 return Type::make_error_type();
7352 Type
* t
= arg
->type();
7353 if (t
->is_abstract())
7354 t
= t
->make_non_abstract_type();
7355 t
= Builtin_call_expression::real_imag_type(t
);
7357 t
= Type::make_error_type();
7361 case BUILTIN_COMPLEX
:
7363 const Expression_list
* args
= this->args();
7364 if (args
== NULL
|| args
->size() != 2)
7365 return Type::make_error_type();
7366 Type
* t
= args
->front()->type();
7367 if (t
->is_abstract())
7369 t
= args
->back()->type();
7370 if (t
->is_abstract())
7371 t
= t
->make_non_abstract_type();
7373 t
= Builtin_call_expression::complex_type(t
);
7375 t
= Type::make_error_type();
7381 // Determine the type.
7384 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7386 if (!this->determining_types())
7389 this->fn()->determine_type_no_context();
7391 const Expression_list
* args
= this->args();
7394 Type
* arg_type
= NULL
;
7395 switch (this->code_
)
7398 case BUILTIN_PRINTLN
:
7399 // Do not force a large integer constant to "int".
7405 arg_type
= Builtin_call_expression::complex_type(context
->type
);
7409 case BUILTIN_COMPLEX
:
7411 // For the complex function the type of one operand can
7412 // determine the type of the other, as in a binary expression.
7413 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7414 if (args
!= NULL
&& args
->size() == 2)
7416 Type
* t1
= args
->front()->type();
7417 Type
* t2
= args
->front()->type();
7418 if (!t1
->is_abstract())
7420 else if (!t2
->is_abstract())
7434 for (Expression_list::const_iterator pa
= args
->begin();
7438 Type_context subcontext
;
7439 subcontext
.type
= arg_type
;
7443 // We want to print large constants, we so can't just
7444 // use the appropriate nonabstract type. Use uint64 for
7445 // an integer if we know it is nonnegative, otherwise
7446 // use int64 for a integer, otherwise use float64 for a
7447 // float or complex128 for a complex.
7448 Type
* want_type
= NULL
;
7449 Type
* atype
= (*pa
)->type();
7450 if (atype
->is_abstract())
7452 if (atype
->integer_type() != NULL
)
7457 if (this->integer_constant_value(true, val
, &dummy
)
7458 && mpz_sgn(val
) >= 0)
7459 want_type
= Type::lookup_integer_type("uint64");
7461 want_type
= Type::lookup_integer_type("int64");
7464 else if (atype
->float_type() != NULL
)
7465 want_type
= Type::lookup_float_type("float64");
7466 else if (atype
->complex_type() != NULL
)
7467 want_type
= Type::lookup_complex_type("complex128");
7468 else if (atype
->is_abstract_string_type())
7469 want_type
= Type::lookup_string_type();
7470 else if (atype
->is_abstract_boolean_type())
7471 want_type
= Type::lookup_bool_type();
7474 subcontext
.type
= want_type
;
7478 (*pa
)->determine_type(&subcontext
);
7483 // If there is exactly one argument, return true. Otherwise give an
7484 // error message and return false.
7487 Builtin_call_expression::check_one_arg()
7489 const Expression_list
* args
= this->args();
7490 if (args
== NULL
|| args
->size() < 1)
7492 this->report_error(_("not enough arguments"));
7495 else if (args
->size() > 1)
7497 this->report_error(_("too many arguments"));
7500 if (args
->front()->is_error_expression()
7501 || args
->front()->type()->is_error())
7503 this->set_is_error();
7509 // Check argument types for a builtin function.
7512 Builtin_call_expression::do_check_types(Gogo
*)
7514 switch (this->code_
)
7516 case BUILTIN_INVALID
:
7524 // The single argument may be either a string or an array or a
7525 // map or a channel, or a pointer to a closed array.
7526 if (this->check_one_arg())
7528 Type
* arg_type
= this->one_arg()->type();
7529 if (arg_type
->points_to() != NULL
7530 && arg_type
->points_to()->array_type() != NULL
7531 && !arg_type
->points_to()->is_open_array_type())
7532 arg_type
= arg_type
->points_to();
7533 if (this->code_
== BUILTIN_CAP
)
7535 if (!arg_type
->is_error()
7536 && arg_type
->array_type() == NULL
7537 && arg_type
->channel_type() == NULL
)
7538 this->report_error(_("argument must be array or slice "
7543 if (!arg_type
->is_error()
7544 && !arg_type
->is_string_type()
7545 && arg_type
->array_type() == NULL
7546 && arg_type
->map_type() == NULL
7547 && arg_type
->channel_type() == NULL
)
7548 this->report_error(_("argument must be string or "
7549 "array or slice or map or channel"));
7556 case BUILTIN_PRINTLN
:
7558 const Expression_list
* args
= this->args();
7561 if (this->code_
== BUILTIN_PRINT
)
7562 warning_at(this->location(), 0,
7563 "no arguments for builtin function %<%s%>",
7564 (this->code_
== BUILTIN_PRINT
7570 for (Expression_list::const_iterator p
= args
->begin();
7574 Type
* type
= (*p
)->type();
7575 if (type
->is_error()
7576 || type
->is_string_type()
7577 || type
->integer_type() != NULL
7578 || type
->float_type() != NULL
7579 || type
->complex_type() != NULL
7580 || type
->is_boolean_type()
7581 || type
->points_to() != NULL
7582 || type
->interface_type() != NULL
7583 || type
->channel_type() != NULL
7584 || type
->map_type() != NULL
7585 || type
->function_type() != NULL
7586 || type
->is_open_array_type())
7589 this->report_error(_("unsupported argument type to "
7590 "builtin function"));
7597 if (this->check_one_arg())
7599 if (this->one_arg()->type()->channel_type() == NULL
)
7600 this->report_error(_("argument must be channel"));
7605 case BUILTIN_SIZEOF
:
7606 case BUILTIN_ALIGNOF
:
7607 this->check_one_arg();
7610 case BUILTIN_RECOVER
:
7611 if (this->args() != NULL
&& !this->args()->empty())
7612 this->report_error(_("too many arguments"));
7615 case BUILTIN_OFFSETOF
:
7616 if (this->check_one_arg())
7618 Expression
* arg
= this->one_arg();
7619 if (arg
->field_reference_expression() == NULL
)
7620 this->report_error(_("argument must be a field reference"));
7626 const Expression_list
* args
= this->args();
7627 if (args
== NULL
|| args
->size() < 2)
7629 this->report_error(_("not enough arguments"));
7632 else if (args
->size() > 2)
7634 this->report_error(_("too many arguments"));
7637 Type
* arg1_type
= args
->front()->type();
7638 Type
* arg2_type
= args
->back()->type();
7639 if (arg1_type
->is_error() || arg2_type
->is_error())
7643 if (arg1_type
->is_open_array_type())
7644 e1
= arg1_type
->array_type()->element_type();
7647 this->report_error(_("left argument must be a slice"));
7652 if (arg2_type
->is_open_array_type())
7653 e2
= arg2_type
->array_type()->element_type();
7654 else if (arg2_type
->is_string_type())
7655 e2
= Type::lookup_integer_type("uint8");
7658 this->report_error(_("right argument must be a slice or a string"));
7662 if (!Type::are_identical(e1
, e2
, true, NULL
))
7663 this->report_error(_("element types must be the same"));
7667 case BUILTIN_APPEND
:
7669 const Expression_list
* args
= this->args();
7670 if (args
== NULL
|| args
->size() < 2)
7672 this->report_error(_("not enough arguments"));
7675 if (args
->size() > 2)
7677 this->report_error(_("too many arguments"));
7681 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7685 this->report_error(_("arguments 1 and 2 have different types"));
7688 error_at(this->location(),
7689 "arguments 1 and 2 have different types (%s)",
7691 this->set_is_error();
7699 if (this->check_one_arg())
7701 if (this->one_arg()->type()->complex_type() == NULL
)
7702 this->report_error(_("argument must have complex type"));
7706 case BUILTIN_COMPLEX
:
7708 const Expression_list
* args
= this->args();
7709 if (args
== NULL
|| args
->size() < 2)
7710 this->report_error(_("not enough arguments"));
7711 else if (args
->size() > 2)
7712 this->report_error(_("too many arguments"));
7713 else if (args
->front()->is_error_expression()
7714 || args
->front()->type()->is_error()
7715 || args
->back()->is_error_expression()
7716 || args
->back()->type()->is_error())
7717 this->set_is_error();
7718 else if (!Type::are_identical(args
->front()->type(),
7719 args
->back()->type(), true, NULL
))
7720 this->report_error(_("complex arguments must have identical types"));
7721 else if (args
->front()->type()->float_type() == NULL
)
7722 this->report_error(_("complex arguments must have "
7723 "floating-point type"));
7732 // Return the tree for a builtin function.
7735 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7737 Gogo
* gogo
= context
->gogo();
7738 source_location location
= this->location();
7739 switch (this->code_
)
7741 case BUILTIN_INVALID
:
7749 const Expression_list
* args
= this->args();
7750 gcc_assert(args
!= NULL
&& args
->size() == 1);
7751 Expression
* arg
= *args
->begin();
7752 Type
* arg_type
= arg
->type();
7756 gcc_assert(saw_errors());
7757 return error_mark_node
;
7761 tree arg_tree
= arg
->get_tree(context
);
7763 this->seen_
= false;
7765 if (arg_tree
== error_mark_node
)
7766 return error_mark_node
;
7768 if (arg_type
->points_to() != NULL
)
7770 arg_type
= arg_type
->points_to();
7771 gcc_assert(arg_type
->array_type() != NULL
7772 && !arg_type
->is_open_array_type());
7773 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7774 arg_tree
= build_fold_indirect_ref(arg_tree
);
7778 if (this->code_
== BUILTIN_LEN
)
7780 if (arg_type
->is_string_type())
7781 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7782 else if (arg_type
->array_type() != NULL
)
7786 gcc_assert(saw_errors());
7787 return error_mark_node
;
7790 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7791 this->seen_
= false;
7793 else if (arg_type
->map_type() != NULL
)
7795 static tree map_len_fndecl
;
7796 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7801 arg_type
->get_tree(gogo
),
7804 else if (arg_type
->channel_type() != NULL
)
7806 static tree chan_len_fndecl
;
7807 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7812 arg_type
->get_tree(gogo
),
7820 if (arg_type
->array_type() != NULL
)
7824 gcc_assert(saw_errors());
7825 return error_mark_node
;
7828 val_tree
= arg_type
->array_type()->capacity_tree(gogo
,
7830 this->seen_
= false;
7832 else if (arg_type
->channel_type() != NULL
)
7834 static tree chan_cap_fndecl
;
7835 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7840 arg_type
->get_tree(gogo
),
7847 if (val_tree
== error_mark_node
)
7848 return error_mark_node
;
7850 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7851 if (type_tree
== TREE_TYPE(val_tree
))
7854 return fold(convert_to_integer(type_tree
, val_tree
));
7858 case BUILTIN_PRINTLN
:
7860 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7861 tree stmt_list
= NULL_TREE
;
7863 const Expression_list
* call_args
= this->args();
7864 if (call_args
!= NULL
)
7866 for (Expression_list::const_iterator p
= call_args
->begin();
7867 p
!= call_args
->end();
7870 if (is_ln
&& p
!= call_args
->begin())
7872 static tree print_space_fndecl
;
7873 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7878 if (call
== error_mark_node
)
7879 return error_mark_node
;
7880 append_to_statement_list(call
, &stmt_list
);
7883 Type
* type
= (*p
)->type();
7885 tree arg
= (*p
)->get_tree(context
);
7886 if (arg
== error_mark_node
)
7887 return error_mark_node
;
7891 if (type
->is_string_type())
7893 static tree print_string_fndecl
;
7894 pfndecl
= &print_string_fndecl
;
7895 fnname
= "__go_print_string";
7897 else if (type
->integer_type() != NULL
7898 && type
->integer_type()->is_unsigned())
7900 static tree print_uint64_fndecl
;
7901 pfndecl
= &print_uint64_fndecl
;
7902 fnname
= "__go_print_uint64";
7903 Type
* itype
= Type::lookup_integer_type("uint64");
7904 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7907 else if (type
->integer_type() != NULL
)
7909 static tree print_int64_fndecl
;
7910 pfndecl
= &print_int64_fndecl
;
7911 fnname
= "__go_print_int64";
7912 Type
* itype
= Type::lookup_integer_type("int64");
7913 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7916 else if (type
->float_type() != NULL
)
7918 static tree print_double_fndecl
;
7919 pfndecl
= &print_double_fndecl
;
7920 fnname
= "__go_print_double";
7921 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7923 else if (type
->complex_type() != NULL
)
7925 static tree print_complex_fndecl
;
7926 pfndecl
= &print_complex_fndecl
;
7927 fnname
= "__go_print_complex";
7928 arg
= fold_convert_loc(location
, complex_double_type_node
,
7931 else if (type
->is_boolean_type())
7933 static tree print_bool_fndecl
;
7934 pfndecl
= &print_bool_fndecl
;
7935 fnname
= "__go_print_bool";
7937 else if (type
->points_to() != NULL
7938 || type
->channel_type() != NULL
7939 || type
->map_type() != NULL
7940 || type
->function_type() != NULL
)
7942 static tree print_pointer_fndecl
;
7943 pfndecl
= &print_pointer_fndecl
;
7944 fnname
= "__go_print_pointer";
7945 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7947 else if (type
->interface_type() != NULL
)
7949 if (type
->interface_type()->is_empty())
7951 static tree print_empty_interface_fndecl
;
7952 pfndecl
= &print_empty_interface_fndecl
;
7953 fnname
= "__go_print_empty_interface";
7957 static tree print_interface_fndecl
;
7958 pfndecl
= &print_interface_fndecl
;
7959 fnname
= "__go_print_interface";
7962 else if (type
->is_open_array_type())
7964 static tree print_slice_fndecl
;
7965 pfndecl
= &print_slice_fndecl
;
7966 fnname
= "__go_print_slice";
7971 tree call
= Gogo::call_builtin(pfndecl
,
7978 if (call
== error_mark_node
)
7979 return error_mark_node
;
7980 append_to_statement_list(call
, &stmt_list
);
7986 static tree print_nl_fndecl
;
7987 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7992 if (call
== error_mark_node
)
7993 return error_mark_node
;
7994 append_to_statement_list(call
, &stmt_list
);
8002 const Expression_list
* args
= this->args();
8003 gcc_assert(args
!= NULL
&& args
->size() == 1);
8004 Expression
* arg
= args
->front();
8005 tree arg_tree
= arg
->get_tree(context
);
8006 if (arg_tree
== error_mark_node
)
8007 return error_mark_node
;
8008 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
8009 arg_tree
= Expression::convert_for_assignment(context
, empty
,
8011 arg_tree
, location
);
8012 static tree panic_fndecl
;
8013 tree call
= Gogo::call_builtin(&panic_fndecl
,
8018 TREE_TYPE(arg_tree
),
8020 if (call
== error_mark_node
)
8021 return error_mark_node
;
8022 // This function will throw an exception.
8023 TREE_NOTHROW(panic_fndecl
) = 0;
8024 // This function will not return.
8025 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
8029 case BUILTIN_RECOVER
:
8031 // The argument is set when building recover thunks. It's a
8032 // boolean value which is true if we can recover a value now.
8033 const Expression_list
* args
= this->args();
8034 gcc_assert(args
!= NULL
&& args
->size() == 1);
8035 Expression
* arg
= args
->front();
8036 tree arg_tree
= arg
->get_tree(context
);
8037 if (arg_tree
== error_mark_node
)
8038 return error_mark_node
;
8040 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
8041 tree empty_tree
= empty
->get_tree(context
->gogo());
8043 Type
* nil_type
= Type::make_nil_type();
8044 Expression
* nil
= Expression::make_nil(location
);
8045 tree nil_tree
= nil
->get_tree(context
);
8046 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
8052 // We need to handle a deferred call to recover specially,
8053 // because it changes whether it can recover a panic or not.
8054 // See test7 in test/recover1.go.
8056 if (this->is_deferred())
8058 static tree deferred_recover_fndecl
;
8059 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
8061 "__go_deferred_recover",
8067 static tree recover_fndecl
;
8068 call
= Gogo::call_builtin(&recover_fndecl
,
8074 if (call
== error_mark_node
)
8075 return error_mark_node
;
8076 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
8077 call
, empty_nil_tree
);
8082 const Expression_list
* args
= this->args();
8083 gcc_assert(args
!= NULL
&& args
->size() == 1);
8084 Expression
* arg
= args
->front();
8085 tree arg_tree
= arg
->get_tree(context
);
8086 if (arg_tree
== error_mark_node
)
8087 return error_mark_node
;
8088 static tree close_fndecl
;
8089 return Gogo::call_builtin(&close_fndecl
,
8091 "__go_builtin_close",
8094 TREE_TYPE(arg_tree
),
8098 case BUILTIN_SIZEOF
:
8099 case BUILTIN_OFFSETOF
:
8100 case BUILTIN_ALIGNOF
:
8105 bool b
= this->integer_constant_value(true, val
, &dummy
);
8108 gcc_assert(saw_errors());
8109 return error_mark_node
;
8111 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
8112 tree ret
= Expression::integer_constant_tree(val
, type
);
8119 const Expression_list
* args
= this->args();
8120 gcc_assert(args
!= NULL
&& args
->size() == 2);
8121 Expression
* arg1
= args
->front();
8122 Expression
* arg2
= args
->back();
8124 tree arg1_tree
= arg1
->get_tree(context
);
8125 tree arg2_tree
= arg2
->get_tree(context
);
8126 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
8127 return error_mark_node
;
8129 Type
* arg1_type
= arg1
->type();
8130 Array_type
* at
= arg1_type
->array_type();
8131 arg1_tree
= save_expr(arg1_tree
);
8132 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
8133 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
8134 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
8135 return error_mark_node
;
8137 Type
* arg2_type
= arg2
->type();
8140 if (arg2_type
->is_open_array_type())
8142 at
= arg2_type
->array_type();
8143 arg2_tree
= save_expr(arg2_tree
);
8144 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
8145 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
8149 arg2_tree
= save_expr(arg2_tree
);
8150 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
8151 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
8153 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
8154 return error_mark_node
;
8156 arg1_len
= save_expr(arg1_len
);
8157 arg2_len
= save_expr(arg2_len
);
8158 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
8159 fold_build2_loc(location
, LT_EXPR
,
8161 arg1_len
, arg2_len
),
8162 arg1_len
, arg2_len
);
8163 len
= save_expr(len
);
8165 Type
* element_type
= at
->element_type();
8166 tree element_type_tree
= element_type
->get_tree(gogo
);
8167 if (element_type_tree
== error_mark_node
)
8168 return error_mark_node
;
8169 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
8170 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
8172 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
8173 TREE_TYPE(element_size
),
8174 bytecount
, element_size
);
8175 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
8177 arg1_val
= fold_convert_loc(location
, ptr_type_node
, arg1_val
);
8178 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
8180 static tree copy_fndecl
;
8181 tree call
= Gogo::call_builtin(©_fndecl
,
8192 if (call
== error_mark_node
)
8193 return error_mark_node
;
8195 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
8199 case BUILTIN_APPEND
:
8201 const Expression_list
* args
= this->args();
8202 gcc_assert(args
!= NULL
&& args
->size() == 2);
8203 Expression
* arg1
= args
->front();
8204 Expression
* arg2
= args
->back();
8206 tree arg1_tree
= arg1
->get_tree(context
);
8207 tree arg2_tree
= arg2
->get_tree(context
);
8208 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
8209 return error_mark_node
;
8211 Array_type
* at
= arg1
->type()->array_type();
8212 Type
* element_type
= at
->element_type();
8214 arg2_tree
= Expression::convert_for_assignment(context
, at
,
8218 if (arg2_tree
== error_mark_node
)
8219 return error_mark_node
;
8221 arg2_tree
= save_expr(arg2_tree
);
8222 tree arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
8223 tree arg2_len
= at
->length_tree(gogo
, arg2_tree
);
8224 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
8225 return error_mark_node
;
8226 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
8227 arg2_len
= fold_convert_loc(location
, size_type_node
, arg2_len
);
8229 tree element_type_tree
= element_type
->get_tree(gogo
);
8230 if (element_type_tree
== error_mark_node
)
8231 return error_mark_node
;
8232 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
8233 element_size
= fold_convert_loc(location
, size_type_node
,
8236 // We rebuild the decl each time since the slice types may
8238 tree append_fndecl
= NULL_TREE
;
8239 return Gogo::call_builtin(&append_fndecl
,
8243 TREE_TYPE(arg1_tree
),
8244 TREE_TYPE(arg1_tree
),
8257 const Expression_list
* args
= this->args();
8258 gcc_assert(args
!= NULL
&& args
->size() == 1);
8259 Expression
* arg
= args
->front();
8260 tree arg_tree
= arg
->get_tree(context
);
8261 if (arg_tree
== error_mark_node
)
8262 return error_mark_node
;
8263 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
8264 if (this->code_
== BUILTIN_REAL
)
8265 return fold_build1_loc(location
, REALPART_EXPR
,
8266 TREE_TYPE(TREE_TYPE(arg_tree
)),
8269 return fold_build1_loc(location
, IMAGPART_EXPR
,
8270 TREE_TYPE(TREE_TYPE(arg_tree
)),
8274 case BUILTIN_COMPLEX
:
8276 const Expression_list
* args
= this->args();
8277 gcc_assert(args
!= NULL
&& args
->size() == 2);
8278 tree r
= args
->front()->get_tree(context
);
8279 tree i
= args
->back()->get_tree(context
);
8280 if (r
== error_mark_node
|| i
== error_mark_node
)
8281 return error_mark_node
;
8282 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
8283 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
8284 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
8285 return fold_build2_loc(location
, COMPLEX_EXPR
,
8286 build_complex_type(TREE_TYPE(r
)),
8295 // We have to support exporting a builtin call expression, because
8296 // code can set a constant to the result of a builtin expression.
8299 Builtin_call_expression::do_export(Export
* exp
) const
8306 if (this->integer_constant_value(true, val
, &dummy
))
8308 Integer_expression::export_integer(exp
, val
);
8317 if (this->float_constant_value(fval
, &dummy
))
8319 Float_expression::export_float(exp
, fval
);
8331 if (this->complex_constant_value(real
, imag
, &dummy
))
8333 Complex_expression::export_complex(exp
, real
, imag
);
8342 error_at(this->location(), "value is not constant");
8346 // A trailing space lets us reliably identify the end of the number.
8347 exp
->write_c_string(" ");
8350 // Class Call_expression.
8355 Call_expression::do_traverse(Traverse
* traverse
)
8357 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8358 return TRAVERSE_EXIT
;
8359 if (this->args_
!= NULL
)
8361 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8362 return TRAVERSE_EXIT
;
8364 return TRAVERSE_CONTINUE
;
8367 // Lower a call statement.
8370 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
8372 // A type case can look like a function call.
8373 if (this->fn_
->is_type_expression()
8374 && this->args_
!= NULL
8375 && this->args_
->size() == 1)
8376 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8379 // Recognize a call to a builtin function.
8380 Func_expression
* fne
= this->fn_
->func_expression();
8382 && fne
->named_object()->is_function_declaration()
8383 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
8384 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8385 this->is_varargs_
, this->location());
8387 // Handle an argument which is a call to a function which returns
8388 // multiple results.
8389 if (this->args_
!= NULL
8390 && this->args_
->size() == 1
8391 && this->args_
->front()->call_expression() != NULL
8392 && this->fn_
->type()->function_type() != NULL
)
8394 Function_type
* fntype
= this->fn_
->type()->function_type();
8395 size_t rc
= this->args_
->front()->call_expression()->result_count();
8397 && fntype
->parameters() != NULL
8398 && (fntype
->parameters()->size() == rc
8399 || (fntype
->is_varargs()
8400 && fntype
->parameters()->size() - 1 <= rc
)))
8402 Call_expression
* call
= this->args_
->front()->call_expression();
8403 Expression_list
* args
= new Expression_list
;
8404 for (size_t i
= 0; i
< rc
; ++i
)
8405 args
->push_back(Expression::make_call_result(call
, i
));
8406 // We can't return a new call expression here, because this
8407 // one may be referenced by Call_result expressions. We
8408 // also can't delete the old arguments, because we may still
8409 // traverse them somewhere up the call stack. FIXME.
8414 // Handle a call to a varargs function by packaging up the extra
8416 if (this->fn_
->type()->function_type() != NULL
8417 && this->fn_
->type()->function_type()->is_varargs())
8419 Function_type
* fntype
= this->fn_
->type()->function_type();
8420 const Typed_identifier_list
* parameters
= fntype
->parameters();
8421 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
8422 Type
* varargs_type
= parameters
->back().type();
8423 return this->lower_varargs(gogo
, function
, varargs_type
,
8424 parameters
->size());
8430 // Lower a call to a varargs function. FUNCTION is the function in
8431 // which the call occurs--it's not the function we are calling.
8432 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8433 // PARAM_COUNT is the number of parameters of the function we are
8434 // calling; the last of these parameters will be the varargs
8438 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8439 Type
* varargs_type
, size_t param_count
)
8441 if (this->varargs_are_lowered_
)
8444 source_location loc
= this->location();
8446 gcc_assert(param_count
> 0);
8447 gcc_assert(varargs_type
->is_open_array_type());
8449 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8450 if (arg_count
< param_count
- 1)
8452 // Not enough arguments; will be caught in check_types.
8456 Expression_list
* old_args
= this->args_
;
8457 Expression_list
* new_args
= new Expression_list();
8458 bool push_empty_arg
= false;
8459 if (old_args
== NULL
|| old_args
->empty())
8461 gcc_assert(param_count
== 1);
8462 push_empty_arg
= true;
8466 Expression_list::const_iterator pa
;
8468 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8470 if (static_cast<size_t>(i
) == param_count
)
8472 new_args
->push_back(*pa
);
8475 // We have reached the varargs parameter.
8477 bool issued_error
= false;
8478 if (pa
== old_args
->end())
8479 push_empty_arg
= true;
8480 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8481 new_args
->push_back(*pa
);
8482 else if (this->is_varargs_
)
8484 this->report_error(_("too many arguments"));
8489 Type
* element_type
= varargs_type
->array_type()->element_type();
8490 Expression_list
* vals
= new Expression_list
;
8491 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8493 // Check types here so that we get a better message.
8494 Type
* patype
= (*pa
)->type();
8495 source_location paloc
= (*pa
)->location();
8496 if (!this->check_argument_type(i
, element_type
, patype
,
8497 paloc
, issued_error
))
8499 vals
->push_back(*pa
);
8502 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8503 new_args
->push_back(val
);
8508 new_args
->push_back(Expression::make_nil(loc
));
8510 // We can't return a new call expression here, because this one may
8511 // be referenced by Call_result expressions. FIXME.
8512 if (old_args
!= NULL
)
8514 this->args_
= new_args
;
8515 this->varargs_are_lowered_
= true;
8517 // Lower all the new subexpressions.
8518 Expression
* ret
= this;
8519 gogo
->lower_expression(function
, &ret
);
8520 gcc_assert(ret
== this);
8524 // Get the function type. Returns NULL if we don't know the type. If
8525 // this returns NULL, and if_ERROR is true, issues an error.
8528 Call_expression::get_function_type() const
8530 return this->fn_
->type()->function_type();
8533 // Return the number of values which this call will return.
8536 Call_expression::result_count() const
8538 const Function_type
* fntype
= this->get_function_type();
8541 if (fntype
->results() == NULL
)
8543 return fntype
->results()->size();
8546 // Return whether this is a call to the predeclared function recover.
8549 Call_expression::is_recover_call() const
8551 return this->do_is_recover_call();
8554 // Set the argument to the recover function.
8557 Call_expression::set_recover_arg(Expression
* arg
)
8559 this->do_set_recover_arg(arg
);
8562 // Virtual functions also implemented by Builtin_call_expression.
8565 Call_expression::do_is_recover_call() const
8571 Call_expression::do_set_recover_arg(Expression
*)
8579 Call_expression::do_type()
8581 if (this->type_
!= NULL
)
8585 Function_type
* fntype
= this->get_function_type();
8587 return Type::make_error_type();
8589 const Typed_identifier_list
* results
= fntype
->results();
8590 if (results
== NULL
)
8591 ret
= Type::make_void_type();
8592 else if (results
->size() == 1)
8593 ret
= results
->begin()->type();
8595 ret
= Type::make_call_multiple_result_type(this);
8602 // Determine types for a call expression. We can use the function
8603 // parameter types to set the types of the arguments.
8606 Call_expression::do_determine_type(const Type_context
*)
8608 if (!this->determining_types())
8611 this->fn_
->determine_type_no_context();
8612 Function_type
* fntype
= this->get_function_type();
8613 const Typed_identifier_list
* parameters
= NULL
;
8615 parameters
= fntype
->parameters();
8616 if (this->args_
!= NULL
)
8618 Typed_identifier_list::const_iterator pt
;
8619 if (parameters
!= NULL
)
8620 pt
= parameters
->begin();
8621 for (Expression_list::const_iterator pa
= this->args_
->begin();
8622 pa
!= this->args_
->end();
8625 if (parameters
!= NULL
&& pt
!= parameters
->end())
8627 Type_context
subcontext(pt
->type(), false);
8628 (*pa
)->determine_type(&subcontext
);
8632 (*pa
)->determine_type_no_context();
8637 // Called when determining types for a Call_expression. Return true
8638 // if we should go ahead, false if they have already been determined.
8641 Call_expression::determining_types()
8643 if (this->types_are_determined_
)
8647 this->types_are_determined_
= true;
8652 // Check types for parameter I.
8655 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8656 const Type
* argument_type
,
8657 source_location argument_location
,
8661 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8666 error_at(argument_location
, "argument %d has incompatible type", i
);
8668 error_at(argument_location
,
8669 "argument %d has incompatible type (%s)",
8672 this->set_is_error();
8681 Call_expression::do_check_types(Gogo
*)
8683 Function_type
* fntype
= this->get_function_type();
8686 if (!this->fn_
->type()->is_error())
8687 this->report_error(_("expected function"));
8691 if (fntype
->is_method())
8693 // We don't support pointers to methods, so the function has to
8694 // be a bound method expression.
8695 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8698 this->report_error(_("method call without object"));
8701 Type
* first_arg_type
= bme
->first_argument()->type();
8702 if (first_arg_type
->points_to() == NULL
)
8704 // When passing a value, we need to check that we are
8705 // permitted to copy it. The language permits copying
8706 // hidden fields for a method receiver.
8708 if (!Type::are_assignable_hidden_ok(fntype
->receiver()->type(),
8709 first_arg_type
, &reason
))
8712 this->report_error(_("incompatible type for receiver"));
8715 error_at(this->location(),
8716 "incompatible type for receiver (%s)",
8718 this->set_is_error();
8724 // Note that varargs was handled by the lower_varargs() method, so
8725 // we don't have to worry about it here.
8727 const Typed_identifier_list
* parameters
= fntype
->parameters();
8728 if (this->args_
== NULL
)
8730 if (parameters
!= NULL
&& !parameters
->empty())
8731 this->report_error(_("not enough arguments"));
8733 else if (parameters
== NULL
)
8734 this->report_error(_("too many arguments"));
8738 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8739 for (Expression_list::const_iterator pa
= this->args_
->begin();
8740 pa
!= this->args_
->end();
8743 if (pt
== parameters
->end())
8745 this->report_error(_("too many arguments"));
8748 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8749 (*pa
)->location(), false);
8751 if (pt
!= parameters
->end())
8752 this->report_error(_("not enough arguments"));
8756 // Return whether we have to use a temporary variable to ensure that
8757 // we evaluate this call expression in order. If the call returns no
8758 // results then it will inevitably be executed last. If the call
8759 // returns more than one result then it will be used with Call_result
8760 // expressions. So we only have to use a temporary variable if the
8761 // call returns exactly one result.
8764 Call_expression::do_must_eval_in_order() const
8766 return this->result_count() == 1;
8769 // Get the function and the first argument to use when calling a bound
8773 Call_expression::bound_method_function(Translate_context
* context
,
8774 Bound_method_expression
* bound_method
,
8775 tree
* first_arg_ptr
)
8777 Expression
* first_argument
= bound_method
->first_argument();
8778 tree first_arg
= first_argument
->get_tree(context
);
8779 if (first_arg
== error_mark_node
)
8780 return error_mark_node
;
8782 // We always pass a pointer to the first argument when calling a
8784 if (first_argument
->type()->points_to() == NULL
)
8786 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8787 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8788 || DECL_P(first_arg
)
8789 || TREE_CODE(first_arg
) == INDIRECT_REF
8790 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8792 first_arg
= build_fold_addr_expr(first_arg
);
8793 if (DECL_P(first_arg
))
8794 TREE_ADDRESSABLE(first_arg
) = 1;
8798 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8799 get_name(first_arg
));
8800 DECL_IGNORED_P(tmp
) = 0;
8801 DECL_INITIAL(tmp
) = first_arg
;
8802 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8803 build1(DECL_EXPR
, void_type_node
, tmp
),
8804 build_fold_addr_expr(tmp
));
8805 TREE_ADDRESSABLE(tmp
) = 1;
8807 if (first_arg
== error_mark_node
)
8808 return error_mark_node
;
8811 Type
* fatype
= bound_method
->first_argument_type();
8814 if (fatype
->points_to() == NULL
)
8815 fatype
= Type::make_pointer_type(fatype
);
8816 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8817 if (first_arg
== error_mark_node
8818 || TREE_TYPE(first_arg
) == error_mark_node
)
8819 return error_mark_node
;
8822 *first_arg_ptr
= first_arg
;
8824 return bound_method
->method()->get_tree(context
);
8827 // Get the function and the first argument to use when calling an
8828 // interface method.
8831 Call_expression::interface_method_function(
8832 Translate_context
* context
,
8833 Interface_field_reference_expression
* interface_method
,
8834 tree
* first_arg_ptr
)
8836 tree expr
= interface_method
->expr()->get_tree(context
);
8837 if (expr
== error_mark_node
)
8838 return error_mark_node
;
8839 expr
= save_expr(expr
);
8840 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8841 if (first_arg
== error_mark_node
)
8842 return error_mark_node
;
8843 *first_arg_ptr
= first_arg
;
8844 return interface_method
->get_function_tree(context
, expr
);
8847 // Build the call expression.
8850 Call_expression::do_get_tree(Translate_context
* context
)
8852 if (this->tree_
!= NULL_TREE
)
8855 Function_type
* fntype
= this->get_function_type();
8857 return error_mark_node
;
8859 if (this->fn_
->is_error_expression())
8860 return error_mark_node
;
8862 Gogo
* gogo
= context
->gogo();
8863 source_location location
= this->location();
8865 Func_expression
* func
= this->fn_
->func_expression();
8866 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8867 Interface_field_reference_expression
* interface_method
=
8868 this->fn_
->interface_field_reference_expression();
8869 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8870 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8871 gcc_assert(!fntype
->is_method() || is_method
);
8875 if (this->args_
== NULL
|| this->args_
->empty())
8877 nargs
= is_method
? 1 : 0;
8878 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8882 const Typed_identifier_list
* params
= fntype
->parameters();
8883 gcc_assert(params
!= NULL
);
8885 nargs
= this->args_
->size();
8886 int i
= is_method
? 1 : 0;
8888 args
= new tree
[nargs
];
8890 Typed_identifier_list::const_iterator pp
= params
->begin();
8891 Expression_list::const_iterator pe
;
8892 for (pe
= this->args_
->begin();
8893 pe
!= this->args_
->end();
8896 gcc_assert(pp
!= params
->end());
8897 tree arg_val
= (*pe
)->get_tree(context
);
8898 args
[i
] = Expression::convert_for_assignment(context
,
8903 if (args
[i
] == error_mark_node
)
8906 return error_mark_node
;
8909 gcc_assert(pp
== params
->end());
8910 gcc_assert(i
== nargs
);
8913 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8914 if (rettype
== error_mark_node
)
8917 return error_mark_node
;
8922 fn
= func
->get_tree_without_closure(gogo
);
8923 else if (!is_method
)
8924 fn
= this->fn_
->get_tree(context
);
8925 else if (bound_method
!= NULL
)
8926 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8927 else if (interface_method
!= NULL
)
8928 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8932 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8935 return error_mark_node
;
8939 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8940 fndecl
= TREE_OPERAND(fndecl
, 0);
8942 // Add a type cast in case the type of the function is a recursive
8943 // type which refers to itself.
8944 if (!DECL_P(fndecl
) || !DECL_IS_BUILTIN(fndecl
))
8946 tree fnt
= fntype
->get_tree(gogo
);
8947 if (fnt
== error_mark_node
)
8948 return error_mark_node
;
8949 fn
= fold_convert_loc(location
, fnt
, fn
);
8952 // This is to support builtin math functions when using 80387 math.
8953 tree excess_type
= NULL_TREE
;
8954 if (TREE_CODE(fndecl
) == FUNCTION_DECL
8955 && DECL_IS_BUILTIN(fndecl
)
8956 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8958 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8959 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8960 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8961 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8963 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8964 if (excess_type
!= NULL_TREE
)
8966 tree excess_fndecl
= mathfn_built_in(excess_type
,
8967 DECL_FUNCTION_CODE(fndecl
));
8968 if (excess_fndecl
== NULL_TREE
)
8969 excess_type
= NULL_TREE
;
8972 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8973 for (int i
= 0; i
< nargs
; ++i
)
8974 args
[i
] = ::convert(excess_type
, args
[i
]);
8979 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8983 SET_EXPR_LOCATION(ret
, location
);
8987 tree closure_tree
= func
->closure()->get_tree(context
);
8988 if (closure_tree
!= error_mark_node
)
8989 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8992 // If this is a recursive function type which returns itself, as in
8994 // we have used ptr_type_node for the return type. Add a cast here
8995 // to the correct type.
8996 if (TREE_TYPE(ret
) == ptr_type_node
)
8998 tree t
= this->type()->base()->get_tree(gogo
);
8999 ret
= fold_convert_loc(location
, t
, ret
);
9002 if (excess_type
!= NULL_TREE
)
9004 // Calling convert here can undo our excess precision change.
9005 // That may or may not be a bug in convert_to_real.
9006 ret
= build1(NOP_EXPR
, rettype
, ret
);
9009 // If there is more than one result, we will refer to the call
9011 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
9012 ret
= save_expr(ret
);
9019 // Make a call expression.
9022 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
9023 source_location location
)
9025 return new Call_expression(fn
, args
, is_varargs
, location
);
9028 // A single result from a call which returns multiple results.
9030 class Call_result_expression
: public Expression
9033 Call_result_expression(Call_expression
* call
, unsigned int index
)
9034 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
9035 call_(call
), index_(index
)
9040 do_traverse(Traverse
*);
9046 do_determine_type(const Type_context
*);
9049 do_check_types(Gogo
*);
9054 return new Call_result_expression(this->call_
->call_expression(),
9059 do_must_eval_in_order() const
9063 do_get_tree(Translate_context
*);
9066 // The underlying call expression.
9068 // Which result we want.
9069 unsigned int index_
;
9072 // Traverse a call result.
9075 Call_result_expression::do_traverse(Traverse
* traverse
)
9077 if (traverse
->remember_expression(this->call_
))
9079 // We have already traversed the call expression.
9080 return TRAVERSE_CONTINUE
;
9082 return Expression::traverse(&this->call_
, traverse
);
9088 Call_result_expression::do_type()
9090 if (this->classification() == EXPRESSION_ERROR
)
9091 return Type::make_error_type();
9093 // THIS->CALL_ can be replaced with a temporary reference due to
9094 // Call_expression::do_must_eval_in_order when there is an error.
9095 Call_expression
* ce
= this->call_
->call_expression();
9098 this->set_is_error();
9099 return Type::make_error_type();
9101 Function_type
* fntype
= ce
->get_function_type();
9104 this->set_is_error();
9105 return Type::make_error_type();
9107 const Typed_identifier_list
* results
= fntype
->results();
9108 if (results
== NULL
)
9110 this->report_error(_("number of results does not match "
9111 "number of values"));
9112 return Type::make_error_type();
9114 Typed_identifier_list::const_iterator pr
= results
->begin();
9115 for (unsigned int i
= 0; i
< this->index_
; ++i
)
9117 if (pr
== results
->end())
9121 if (pr
== results
->end())
9123 this->report_error(_("number of results does not match "
9124 "number of values"));
9125 return Type::make_error_type();
9130 // Check the type. Just make sure that we trigger the warning in
9134 Call_result_expression::do_check_types(Gogo
*)
9139 // Determine the type. We have nothing to do here, but the 0 result
9140 // needs to pass down to the caller.
9143 Call_result_expression::do_determine_type(const Type_context
*)
9145 this->call_
->determine_type_no_context();
9151 Call_result_expression::do_get_tree(Translate_context
* context
)
9153 tree call_tree
= this->call_
->get_tree(context
);
9154 if (call_tree
== error_mark_node
)
9155 return error_mark_node
;
9156 if (TREE_CODE(TREE_TYPE(call_tree
)) != RECORD_TYPE
)
9158 gcc_assert(saw_errors());
9159 return error_mark_node
;
9161 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
9162 for (unsigned int i
= 0; i
< this->index_
; ++i
)
9164 gcc_assert(field
!= NULL_TREE
);
9165 field
= DECL_CHAIN(field
);
9167 gcc_assert(field
!= NULL_TREE
);
9168 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
9171 // Make a reference to a single result of a call which returns
9172 // multiple results.
9175 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
9177 return new Call_result_expression(call
, index
);
9180 // Class Index_expression.
9185 Index_expression::do_traverse(Traverse
* traverse
)
9187 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
9188 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
9189 || (this->end_
!= NULL
9190 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
9191 return TRAVERSE_EXIT
;
9192 return TRAVERSE_CONTINUE
;
9195 // Lower an index expression. This converts the generic index
9196 // expression into an array index, a string index, or a map index.
9199 Index_expression::do_lower(Gogo
*, Named_object
*, int)
9201 source_location location
= this->location();
9202 Expression
* left
= this->left_
;
9203 Expression
* start
= this->start_
;
9204 Expression
* end
= this->end_
;
9206 Type
* type
= left
->type();
9207 if (type
->is_error())
9208 return Expression::make_error(location
);
9209 else if (left
->is_type_expression())
9211 error_at(location
, "attempt to index type expression");
9212 return Expression::make_error(location
);
9214 else if (type
->array_type() != NULL
)
9215 return Expression::make_array_index(left
, start
, end
, location
);
9216 else if (type
->points_to() != NULL
9217 && type
->points_to()->array_type() != NULL
9218 && !type
->points_to()->is_open_array_type())
9220 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
9222 return Expression::make_array_index(deref
, start
, end
, location
);
9224 else if (type
->is_string_type())
9225 return Expression::make_string_index(left
, start
, end
, location
);
9226 else if (type
->map_type() != NULL
)
9230 error_at(location
, "invalid slice of map");
9231 return Expression::make_error(location
);
9233 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
9235 if (this->is_lvalue_
)
9236 ret
->set_is_lvalue();
9242 "attempt to index object which is not array, string, or map");
9243 return Expression::make_error(location
);
9247 // Make an index expression.
9250 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
9251 source_location location
)
9253 return new Index_expression(left
, start
, end
, location
);
9256 // An array index. This is used for both indexing and slicing.
9258 class Array_index_expression
: public Expression
9261 Array_index_expression(Expression
* array
, Expression
* start
,
9262 Expression
* end
, source_location location
)
9263 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
9264 array_(array
), start_(start
), end_(end
), type_(NULL
)
9269 do_traverse(Traverse
*);
9275 do_determine_type(const Type_context
*);
9278 do_check_types(Gogo
*);
9283 return Expression::make_array_index(this->array_
->copy(),
9284 this->start_
->copy(),
9287 : this->end_
->copy()),
9292 do_is_addressable() const;
9295 do_address_taken(bool escapes
)
9296 { this->array_
->address_taken(escapes
); }
9299 do_get_tree(Translate_context
*);
9302 // The array we are getting a value from.
9304 // The start or only index.
9306 // The end index of a slice. This may be NULL for a simple array
9307 // index, or it may be a nil expression for the length of the array.
9309 // The type of the expression.
9313 // Array index traversal.
9316 Array_index_expression::do_traverse(Traverse
* traverse
)
9318 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9319 return TRAVERSE_EXIT
;
9320 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9321 return TRAVERSE_EXIT
;
9322 if (this->end_
!= NULL
)
9324 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9325 return TRAVERSE_EXIT
;
9327 return TRAVERSE_CONTINUE
;
9330 // Return the type of an array index.
9333 Array_index_expression::do_type()
9335 if (this->type_
== NULL
)
9337 Array_type
* type
= this->array_
->type()->array_type();
9339 this->type_
= Type::make_error_type();
9340 else if (this->end_
== NULL
)
9341 this->type_
= type
->element_type();
9342 else if (type
->is_open_array_type())
9344 // A slice of a slice has the same type as the original
9346 this->type_
= this->array_
->type()->deref();
9350 // A slice of an array is a slice.
9351 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9357 // Set the type of an array index.
9360 Array_index_expression::do_determine_type(const Type_context
*)
9362 this->array_
->determine_type_no_context();
9363 this->start_
->determine_type_no_context();
9364 if (this->end_
!= NULL
)
9365 this->end_
->determine_type_no_context();
9368 // Check types of an array index.
9371 Array_index_expression::do_check_types(Gogo
*)
9373 if (this->start_
->type()->integer_type() == NULL
)
9374 this->report_error(_("index must be integer"));
9375 if (this->end_
!= NULL
9376 && this->end_
->type()->integer_type() == NULL
9377 && !this->end_
->is_nil_expression())
9378 this->report_error(_("slice end must be integer"));
9380 Array_type
* array_type
= this->array_
->type()->array_type();
9381 if (array_type
== NULL
)
9383 gcc_assert(this->array_
->type()->is_error());
9387 unsigned int int_bits
=
9388 Type::lookup_integer_type("int")->integer_type()->bits();
9393 bool lval_valid
= (array_type
->length() != NULL
9394 && array_type
->length()->integer_constant_value(true,
9399 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9401 if (mpz_sgn(ival
) < 0
9402 || mpz_sizeinbase(ival
, 2) >= int_bits
9404 && (this->end_
== NULL
9405 ? mpz_cmp(ival
, lval
) >= 0
9406 : mpz_cmp(ival
, lval
) > 0)))
9408 error_at(this->start_
->location(), "array index out of bounds");
9409 this->set_is_error();
9412 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9414 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9416 if (mpz_sgn(ival
) < 0
9417 || mpz_sizeinbase(ival
, 2) >= int_bits
9418 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
9420 error_at(this->end_
->location(), "array index out of bounds");
9421 this->set_is_error();
9428 // A slice of an array requires an addressable array. A slice of a
9429 // slice is always possible.
9430 if (this->end_
!= NULL
&& !array_type
->is_open_array_type())
9432 if (!this->array_
->is_addressable())
9433 this->report_error(_("array is not addressable"));
9435 this->array_
->address_taken(true);
9439 // Return whether this expression is addressable.
9442 Array_index_expression::do_is_addressable() const
9444 // A slice expression is not addressable.
9445 if (this->end_
!= NULL
)
9448 // An index into a slice is addressable.
9449 if (this->array_
->type()->is_open_array_type())
9452 // An index into an array is addressable if the array is
9454 return this->array_
->is_addressable();
9457 // Get a tree for an array index.
9460 Array_index_expression::do_get_tree(Translate_context
* context
)
9462 Gogo
* gogo
= context
->gogo();
9463 source_location loc
= this->location();
9465 Array_type
* array_type
= this->array_
->type()->array_type();
9466 if (array_type
== NULL
)
9468 gcc_assert(this->array_
->type()->is_error());
9469 return error_mark_node
;
9472 tree type_tree
= array_type
->get_tree(gogo
);
9473 if (type_tree
== error_mark_node
)
9474 return error_mark_node
;
9476 tree array_tree
= this->array_
->get_tree(context
);
9477 if (array_tree
== error_mark_node
)
9478 return error_mark_node
;
9480 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9481 array_tree
= save_expr(array_tree
);
9482 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
9483 if (length_tree
== error_mark_node
)
9484 return error_mark_node
;
9485 length_tree
= save_expr(length_tree
);
9486 tree length_type
= TREE_TYPE(length_tree
);
9488 tree bad_index
= boolean_false_node
;
9490 tree start_tree
= this->start_
->get_tree(context
);
9491 if (start_tree
== error_mark_node
)
9492 return error_mark_node
;
9493 if (!DECL_P(start_tree
))
9494 start_tree
= save_expr(start_tree
);
9495 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9496 start_tree
= convert_to_integer(length_type
, start_tree
);
9498 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9501 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9502 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9503 fold_build2_loc(loc
,
9507 boolean_type_node
, start_tree
,
9510 int code
= (array_type
->length() != NULL
9511 ? (this->end_
== NULL
9512 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9513 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9514 : (this->end_
== NULL
9515 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9516 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9517 tree crash
= Gogo::runtime_error(code
, loc
);
9519 if (this->end_
== NULL
)
9521 // Simple array indexing. This has to return an l-value, so
9522 // wrap the index check into START_TREE.
9523 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9524 build3(COND_EXPR
, void_type_node
,
9525 bad_index
, crash
, NULL_TREE
),
9527 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9529 if (array_type
->length() != NULL
)
9532 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9533 start_tree
, NULL_TREE
, NULL_TREE
);
9538 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9539 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9540 if (element_type_tree
== error_mark_node
)
9541 return error_mark_node
;
9542 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9543 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9544 start_tree
, element_size
);
9545 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9546 TREE_TYPE(values
), values
, offset
);
9547 return build_fold_indirect_ref(ptr
);
9553 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9554 if (capacity_tree
== error_mark_node
)
9555 return error_mark_node
;
9556 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9559 if (this->end_
->is_nil_expression())
9560 end_tree
= length_tree
;
9563 end_tree
= this->end_
->get_tree(context
);
9564 if (end_tree
== error_mark_node
)
9565 return error_mark_node
;
9566 if (!DECL_P(end_tree
))
9567 end_tree
= save_expr(end_tree
);
9568 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9569 end_tree
= convert_to_integer(length_type
, end_tree
);
9571 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9574 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9576 capacity_tree
= save_expr(capacity_tree
);
9577 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9578 fold_build2_loc(loc
, LT_EXPR
,
9580 end_tree
, start_tree
),
9581 fold_build2_loc(loc
, GT_EXPR
,
9583 end_tree
, capacity_tree
));
9584 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9585 bad_index
, bad_end
);
9588 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9589 if (element_type_tree
== error_mark_node
)
9590 return error_mark_node
;
9591 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9593 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9594 fold_convert_loc(loc
, sizetype
, start_tree
),
9597 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9598 if (value_pointer
== error_mark_node
)
9599 return error_mark_node
;
9601 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9602 TREE_TYPE(value_pointer
),
9603 value_pointer
, offset
);
9605 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9606 end_tree
, start_tree
);
9608 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9609 capacity_tree
, start_tree
);
9611 tree struct_tree
= this->type()->get_tree(gogo
);
9612 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9614 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9616 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9617 tree field
= TYPE_FIELDS(struct_tree
);
9618 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9620 elt
->value
= value_pointer
;
9622 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9623 field
= DECL_CHAIN(field
);
9624 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9626 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9628 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9629 field
= DECL_CHAIN(field
);
9630 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9632 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9634 tree constructor
= build_constructor(struct_tree
, init
);
9636 if (TREE_CONSTANT(value_pointer
)
9637 && TREE_CONSTANT(result_length_tree
)
9638 && TREE_CONSTANT(result_capacity_tree
))
9639 TREE_CONSTANT(constructor
) = 1;
9641 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9642 build3(COND_EXPR
, void_type_node
,
9643 bad_index
, crash
, NULL_TREE
),
9647 // Make an array index expression. END may be NULL.
9650 Expression::make_array_index(Expression
* array
, Expression
* start
,
9651 Expression
* end
, source_location location
)
9653 // Taking a slice of a composite literal requires moving the literal
9655 if (end
!= NULL
&& array
->is_composite_literal())
9657 array
= Expression::make_heap_composite(array
, location
);
9658 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9660 return new Array_index_expression(array
, start
, end
, location
);
9663 // A string index. This is used for both indexing and slicing.
9665 class String_index_expression
: public Expression
9668 String_index_expression(Expression
* string
, Expression
* start
,
9669 Expression
* end
, source_location location
)
9670 : Expression(EXPRESSION_STRING_INDEX
, location
),
9671 string_(string
), start_(start
), end_(end
)
9676 do_traverse(Traverse
*);
9682 do_determine_type(const Type_context
*);
9685 do_check_types(Gogo
*);
9690 return Expression::make_string_index(this->string_
->copy(),
9691 this->start_
->copy(),
9694 : this->end_
->copy()),
9699 do_get_tree(Translate_context
*);
9702 // The string we are getting a value from.
9703 Expression
* string_
;
9704 // The start or only index.
9706 // The end index of a slice. This may be NULL for a single index,
9707 // or it may be a nil expression for the length of the string.
9711 // String index traversal.
9714 String_index_expression::do_traverse(Traverse
* traverse
)
9716 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9717 return TRAVERSE_EXIT
;
9718 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9719 return TRAVERSE_EXIT
;
9720 if (this->end_
!= NULL
)
9722 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9723 return TRAVERSE_EXIT
;
9725 return TRAVERSE_CONTINUE
;
9728 // Return the type of a string index.
9731 String_index_expression::do_type()
9733 if (this->end_
== NULL
)
9734 return Type::lookup_integer_type("uint8");
9736 return this->string_
->type();
9739 // Determine the type of a string index.
9742 String_index_expression::do_determine_type(const Type_context
*)
9744 this->string_
->determine_type_no_context();
9745 this->start_
->determine_type_no_context();
9746 if (this->end_
!= NULL
)
9747 this->end_
->determine_type_no_context();
9750 // Check types of a string index.
9753 String_index_expression::do_check_types(Gogo
*)
9755 if (this->start_
->type()->integer_type() == NULL
)
9756 this->report_error(_("index must be integer"));
9757 if (this->end_
!= NULL
9758 && this->end_
->type()->integer_type() == NULL
9759 && !this->end_
->is_nil_expression())
9760 this->report_error(_("slice end must be integer"));
9763 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9768 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9770 if (mpz_sgn(ival
) < 0
9771 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9773 error_at(this->start_
->location(), "string index out of bounds");
9774 this->set_is_error();
9777 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9779 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9781 if (mpz_sgn(ival
) < 0
9782 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9784 error_at(this->end_
->location(), "string index out of bounds");
9785 this->set_is_error();
9792 // Get a tree for a string index.
9795 String_index_expression::do_get_tree(Translate_context
* context
)
9797 source_location loc
= this->location();
9799 tree string_tree
= this->string_
->get_tree(context
);
9800 if (string_tree
== error_mark_node
)
9801 return error_mark_node
;
9803 if (this->string_
->type()->points_to() != NULL
)
9804 string_tree
= build_fold_indirect_ref(string_tree
);
9805 if (!DECL_P(string_tree
))
9806 string_tree
= save_expr(string_tree
);
9807 tree string_type
= TREE_TYPE(string_tree
);
9809 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9810 length_tree
= save_expr(length_tree
);
9811 tree length_type
= TREE_TYPE(length_tree
);
9813 tree bad_index
= boolean_false_node
;
9815 tree start_tree
= this->start_
->get_tree(context
);
9816 if (start_tree
== error_mark_node
)
9817 return error_mark_node
;
9818 if (!DECL_P(start_tree
))
9819 start_tree
= save_expr(start_tree
);
9820 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9821 start_tree
= convert_to_integer(length_type
, start_tree
);
9823 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9826 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9828 int code
= (this->end_
== NULL
9829 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9830 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9831 tree crash
= Gogo::runtime_error(code
, loc
);
9833 if (this->end_
== NULL
)
9835 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9837 fold_build2_loc(loc
, GE_EXPR
,
9839 start_tree
, length_tree
));
9841 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9842 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9844 fold_convert_loc(loc
, sizetype
, start_tree
));
9845 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9847 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9848 build3(COND_EXPR
, void_type_node
,
9849 bad_index
, crash
, NULL_TREE
),
9855 if (this->end_
->is_nil_expression())
9856 end_tree
= build_int_cst(length_type
, -1);
9859 end_tree
= this->end_
->get_tree(context
);
9860 if (end_tree
== error_mark_node
)
9861 return error_mark_node
;
9862 if (!DECL_P(end_tree
))
9863 end_tree
= save_expr(end_tree
);
9864 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9865 end_tree
= convert_to_integer(length_type
, end_tree
);
9867 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9870 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9873 static tree strslice_fndecl
;
9874 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9876 "__go_string_slice",
9885 if (ret
== error_mark_node
)
9886 return error_mark_node
;
9887 // This will panic if the bounds are out of range for the
9889 TREE_NOTHROW(strslice_fndecl
) = 0;
9891 if (bad_index
== boolean_false_node
)
9894 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9895 build3(COND_EXPR
, void_type_node
,
9896 bad_index
, crash
, NULL_TREE
),
9901 // Make a string index expression. END may be NULL.
9904 Expression::make_string_index(Expression
* string
, Expression
* start
,
9905 Expression
* end
, source_location location
)
9907 return new String_index_expression(string
, start
, end
, location
);
9912 // Get the type of the map.
9915 Map_index_expression::get_map_type() const
9917 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9919 gcc_assert(saw_errors());
9923 // Map index traversal.
9926 Map_index_expression::do_traverse(Traverse
* traverse
)
9928 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9929 return TRAVERSE_EXIT
;
9930 return Expression::traverse(&this->index_
, traverse
);
9933 // Return the type of a map index.
9936 Map_index_expression::do_type()
9938 Map_type
* mt
= this->get_map_type();
9940 return Type::make_error_type();
9941 Type
* type
= mt
->val_type();
9942 // If this map index is in a tuple assignment, we actually return a
9943 // pointer to the value type. Tuple_map_assignment_statement is
9944 // responsible for handling this correctly. We need to get the type
9945 // right in case this gets assigned to a temporary variable.
9946 if (this->is_in_tuple_assignment_
)
9947 type
= Type::make_pointer_type(type
);
9951 // Fix the type of a map index.
9954 Map_index_expression::do_determine_type(const Type_context
*)
9956 this->map_
->determine_type_no_context();
9957 Map_type
* mt
= this->get_map_type();
9958 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
9959 Type_context
subcontext(key_type
, false);
9960 this->index_
->determine_type(&subcontext
);
9963 // Check types of a map index.
9966 Map_index_expression::do_check_types(Gogo
*)
9969 Map_type
* mt
= this->get_map_type();
9972 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
9975 this->report_error(_("incompatible type for map index"));
9978 error_at(this->location(), "incompatible type for map index (%s)",
9980 this->set_is_error();
9985 // Get a tree for a map index.
9988 Map_index_expression::do_get_tree(Translate_context
* context
)
9990 Map_type
* type
= this->get_map_type();
9992 return error_mark_node
;
9994 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9995 if (valptr
== error_mark_node
)
9996 return error_mark_node
;
9997 valptr
= save_expr(valptr
);
9999 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
10001 if (this->is_lvalue_
)
10002 return build_fold_indirect_ref(valptr
);
10003 else if (this->is_in_tuple_assignment_
)
10005 // Tuple_map_assignment_statement is responsible for using this
10011 return fold_build3(COND_EXPR
, val_type_tree
,
10012 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
10013 fold_convert(TREE_TYPE(valptr
),
10014 null_pointer_node
)),
10015 type
->val_type()->get_init_tree(context
->gogo(),
10017 build_fold_indirect_ref(valptr
));
10021 // Get a tree for the map index. This returns a tree which evaluates
10022 // to a pointer to a value. The pointer will be NULL if the key is
10026 Map_index_expression::get_value_pointer(Translate_context
* context
,
10029 Map_type
* type
= this->get_map_type();
10031 return error_mark_node
;
10033 tree map_tree
= this->map_
->get_tree(context
);
10034 tree index_tree
= this->index_
->get_tree(context
);
10035 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
10036 this->index_
->type(),
10039 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
10040 return error_mark_node
;
10042 if (this->map_
->type()->points_to() != NULL
)
10043 map_tree
= build_fold_indirect_ref(map_tree
);
10045 // We need to pass in a pointer to the key, so stuff it into a
10049 if (current_function_decl
!= NULL
)
10051 tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
10052 DECL_IGNORED_P(tmp
) = 0;
10053 DECL_INITIAL(tmp
) = index_tree
;
10054 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
10055 TREE_ADDRESSABLE(tmp
) = 1;
10059 tmp
= build_decl(this->location(), VAR_DECL
, create_tmp_var_name("M"),
10060 TREE_TYPE(index_tree
));
10061 DECL_EXTERNAL(tmp
) = 0;
10062 TREE_PUBLIC(tmp
) = 0;
10063 TREE_STATIC(tmp
) = 1;
10064 DECL_ARTIFICIAL(tmp
) = 1;
10065 if (!TREE_CONSTANT(index_tree
))
10066 make_tmp
= fold_build2_loc(this->location(), INIT_EXPR
, void_type_node
,
10070 TREE_READONLY(tmp
) = 1;
10071 TREE_CONSTANT(tmp
) = 1;
10072 DECL_INITIAL(tmp
) = index_tree
;
10073 make_tmp
= NULL_TREE
;
10075 rest_of_decl_compilation(tmp
, 1, 0);
10077 tree tmpref
= fold_convert_loc(this->location(), const_ptr_type_node
,
10078 build_fold_addr_expr_loc(this->location(),
10081 static tree map_index_fndecl
;
10082 tree call
= Gogo::call_builtin(&map_index_fndecl
,
10086 const_ptr_type_node
,
10087 TREE_TYPE(map_tree
),
10089 const_ptr_type_node
,
10093 ? boolean_true_node
10094 : boolean_false_node
));
10095 if (call
== error_mark_node
)
10096 return error_mark_node
;
10097 // This can panic on a map of interface type if the interface holds
10098 // an uncomparable or unhashable type.
10099 TREE_NOTHROW(map_index_fndecl
) = 0;
10101 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
10102 if (val_type_tree
== error_mark_node
)
10103 return error_mark_node
;
10104 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
10106 tree ret
= fold_convert_loc(this->location(), ptr_val_type_tree
, call
);
10107 if (make_tmp
!= NULL_TREE
)
10108 ret
= build2(COMPOUND_EXPR
, ptr_val_type_tree
, make_tmp
, ret
);
10112 // Make a map index expression.
10114 Map_index_expression
*
10115 Expression::make_map_index(Expression
* map
, Expression
* index
,
10116 source_location location
)
10118 return new Map_index_expression(map
, index
, location
);
10121 // Class Field_reference_expression.
10123 // Return the type of a field reference.
10126 Field_reference_expression::do_type()
10128 Type
* type
= this->expr_
->type();
10129 if (type
->is_error())
10131 Struct_type
* struct_type
= type
->struct_type();
10132 gcc_assert(struct_type
!= NULL
);
10133 return struct_type
->field(this->field_index_
)->type();
10136 // Check the types for a field reference.
10139 Field_reference_expression::do_check_types(Gogo
*)
10141 Type
* type
= this->expr_
->type();
10142 if (type
->is_error())
10144 Struct_type
* struct_type
= type
->struct_type();
10145 gcc_assert(struct_type
!= NULL
);
10146 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
10149 // Get a tree for a field reference.
10152 Field_reference_expression::do_get_tree(Translate_context
* context
)
10154 tree struct_tree
= this->expr_
->get_tree(context
);
10155 if (struct_tree
== error_mark_node
10156 || TREE_TYPE(struct_tree
) == error_mark_node
)
10157 return error_mark_node
;
10158 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
10159 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
10160 if (field
== NULL_TREE
)
10162 // This can happen for a type which refers to itself indirectly
10163 // and then turns out to be erroneous.
10164 gcc_assert(saw_errors());
10165 return error_mark_node
;
10167 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
10169 field
= DECL_CHAIN(field
);
10170 gcc_assert(field
!= NULL_TREE
);
10172 if (TREE_TYPE(field
) == error_mark_node
)
10173 return error_mark_node
;
10174 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
10178 // Make a reference to a qualified identifier in an expression.
10180 Field_reference_expression
*
10181 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
10182 source_location location
)
10184 return new Field_reference_expression(expr
, field_index
, location
);
10187 // Class Interface_field_reference_expression.
10189 // Return a tree for the pointer to the function to call.
10192 Interface_field_reference_expression::get_function_tree(Translate_context
*,
10195 if (this->expr_
->type()->points_to() != NULL
)
10196 expr
= build_fold_indirect_ref(expr
);
10198 tree expr_type
= TREE_TYPE(expr
);
10199 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10201 tree field
= TYPE_FIELDS(expr_type
);
10202 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
10204 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10205 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
10207 table
= build_fold_indirect_ref(table
);
10208 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
10210 std::string name
= Gogo::unpack_hidden_name(this->name_
);
10211 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
10212 field
!= NULL_TREE
;
10213 field
= DECL_CHAIN(field
))
10215 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
10218 gcc_assert(field
!= NULL_TREE
);
10220 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
10223 // Return a tree for the first argument to pass to the interface
10227 Interface_field_reference_expression::get_underlying_object_tree(
10228 Translate_context
*,
10231 if (this->expr_
->type()->points_to() != NULL
)
10232 expr
= build_fold_indirect_ref(expr
);
10234 tree expr_type
= TREE_TYPE(expr
);
10235 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10237 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
10238 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
10240 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10246 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
10248 return Expression::traverse(&this->expr_
, traverse
);
10251 // Return the type of an interface field reference.
10254 Interface_field_reference_expression::do_type()
10256 Type
* expr_type
= this->expr_
->type();
10258 Type
* points_to
= expr_type
->points_to();
10259 if (points_to
!= NULL
)
10260 expr_type
= points_to
;
10262 Interface_type
* interface_type
= expr_type
->interface_type();
10263 if (interface_type
== NULL
)
10264 return Type::make_error_type();
10266 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
10267 if (method
== NULL
)
10268 return Type::make_error_type();
10270 return method
->type();
10273 // Determine types.
10276 Interface_field_reference_expression::do_determine_type(const Type_context
*)
10278 this->expr_
->determine_type_no_context();
10281 // Check the types for an interface field reference.
10284 Interface_field_reference_expression::do_check_types(Gogo
*)
10286 Type
* type
= this->expr_
->type();
10288 Type
* points_to
= type
->points_to();
10289 if (points_to
!= NULL
)
10292 Interface_type
* interface_type
= type
->interface_type();
10293 if (interface_type
== NULL
)
10295 if (!type
->is_error_type())
10296 this->report_error(_("expected interface or pointer to interface"));
10300 const Typed_identifier
* method
=
10301 interface_type
->find_method(this->name_
);
10302 if (method
== NULL
)
10304 error_at(this->location(), "method %qs not in interface",
10305 Gogo::message_name(this->name_
).c_str());
10306 this->set_is_error();
10311 // Get a tree for a reference to a field in an interface. There is no
10312 // standard tree type representation for this: it's a function
10313 // attached to its first argument, like a Bound_method_expression.
10314 // The only places it may currently be used are in a Call_expression
10315 // or a Go_statement, which will take it apart directly. So this has
10316 // nothing to do at present.
10319 Interface_field_reference_expression::do_get_tree(Translate_context
*)
10324 // Make a reference to a field in an interface.
10327 Expression::make_interface_field_reference(Expression
* expr
,
10328 const std::string
& field
,
10329 source_location location
)
10331 return new Interface_field_reference_expression(expr
, field
, location
);
10334 // A general selector. This is a Parser_expression for LEFT.NAME. It
10335 // is lowered after we know the type of the left hand side.
10337 class Selector_expression
: public Parser_expression
10340 Selector_expression(Expression
* left
, const std::string
& name
,
10341 source_location location
)
10342 : Parser_expression(EXPRESSION_SELECTOR
, location
),
10343 left_(left
), name_(name
)
10348 do_traverse(Traverse
* traverse
)
10349 { return Expression::traverse(&this->left_
, traverse
); }
10352 do_lower(Gogo
*, Named_object
*, int);
10357 return new Selector_expression(this->left_
->copy(), this->name_
,
10363 lower_method_expression(Gogo
*);
10365 // The expression on the left hand side.
10367 // The name on the right hand side.
10371 // Lower a selector expression once we know the real type of the left
10375 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
10377 Expression
* left
= this->left_
;
10378 if (left
->is_type_expression())
10379 return this->lower_method_expression(gogo
);
10380 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
10384 // Lower a method expression T.M or (*T).M. We turn this into a
10385 // function literal.
10388 Selector_expression::lower_method_expression(Gogo
* gogo
)
10390 source_location location
= this->location();
10391 Type
* type
= this->left_
->type();
10392 const std::string
& name(this->name_
);
10395 if (type
->points_to() == NULL
)
10396 is_pointer
= false;
10400 type
= type
->points_to();
10402 Named_type
* nt
= type
->named_type();
10406 ("method expression requires named type or "
10407 "pointer to named type"));
10408 return Expression::make_error(location
);
10412 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
10413 const Typed_identifier
* imethod
= NULL
;
10414 if (method
== NULL
&& !is_pointer
)
10416 Interface_type
* it
= nt
->interface_type();
10418 imethod
= it
->find_method(name
);
10421 if (method
== NULL
&& imethod
== NULL
)
10424 error_at(location
, "type %<%s%s%> has no method %<%s%>",
10425 is_pointer
? "*" : "",
10426 nt
->message_name().c_str(),
10427 Gogo::message_name(name
).c_str());
10429 error_at(location
, "method %<%s%s%> is ambiguous in type %<%s%>",
10430 Gogo::message_name(name
).c_str(),
10431 is_pointer
? "*" : "",
10432 nt
->message_name().c_str());
10433 return Expression::make_error(location
);
10436 if (method
!= NULL
&& !is_pointer
&& !method
->is_value_method())
10438 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
10439 nt
->message_name().c_str(),
10440 Gogo::message_name(name
).c_str());
10441 return Expression::make_error(location
);
10444 // Build a new function type in which the receiver becomes the first
10446 Function_type
* method_type
;
10447 if (method
!= NULL
)
10449 method_type
= method
->type();
10450 gcc_assert(method_type
->is_method());
10454 method_type
= imethod
->type()->function_type();
10455 gcc_assert(method_type
!= NULL
&& !method_type
->is_method());
10458 const char* const receiver_name
= "$this";
10459 Typed_identifier_list
* parameters
= new Typed_identifier_list();
10460 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
10463 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
10464 if (method_parameters
!= NULL
)
10466 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10467 p
!= method_parameters
->end();
10469 parameters
->push_back(*p
);
10472 const Typed_identifier_list
* method_results
= method_type
->results();
10473 Typed_identifier_list
* results
;
10474 if (method_results
== NULL
)
10478 results
= new Typed_identifier_list();
10479 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
10480 p
!= method_results
->end();
10482 results
->push_back(*p
);
10485 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
10487 if (method_type
->is_varargs())
10488 fntype
->set_is_varargs();
10490 // We generate methods which always takes a pointer to the receiver
10491 // as their first argument. If this is for a pointer type, we can
10492 // simply reuse the existing function. We use an internal hack to
10493 // get the right type.
10495 if (method
!= NULL
&& is_pointer
)
10497 Named_object
* mno
= (method
->needs_stub_method()
10498 ? method
->stub_object()
10499 : method
->named_object());
10500 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
10501 f
= Expression::make_cast(fntype
, f
, location
);
10502 Type_conversion_expression
* tce
=
10503 static_cast<Type_conversion_expression
*>(f
);
10504 tce
->set_may_convert_function_types();
10508 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
10511 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
10512 gcc_assert(vno
!= NULL
);
10513 Expression
* ve
= Expression::make_var_reference(vno
, location
);
10515 if (method
!= NULL
)
10516 bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
10518 bm
= Expression::make_interface_field_reference(ve
, name
, location
);
10520 // Even though we found the method above, if it has an error type we
10521 // may see an error here.
10522 if (bm
->is_error_expression())
10524 gogo
->finish_function(location
);
10528 Expression_list
* args
;
10529 if (method_parameters
== NULL
)
10533 args
= new Expression_list();
10534 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10535 p
!= method_parameters
->end();
10538 vno
= gogo
->lookup(p
->name(), NULL
);
10539 gcc_assert(vno
!= NULL
);
10540 args
->push_back(Expression::make_var_reference(vno
, location
));
10544 Call_expression
* call
= Expression::make_call(bm
, args
,
10545 method_type
->is_varargs(),
10548 size_t count
= call
->result_count();
10551 s
= Statement::make_statement(call
);
10554 Expression_list
* retvals
= new Expression_list();
10556 retvals
->push_back(call
);
10559 for (size_t i
= 0; i
< count
; ++i
)
10560 retvals
->push_back(Expression::make_call_result(call
, i
));
10562 s
= Statement::make_return_statement(retvals
, location
);
10564 gogo
->add_statement(s
);
10566 gogo
->finish_function(location
);
10568 return Expression::make_func_reference(no
, NULL
, location
);
10571 // Make a selector expression.
10574 Expression::make_selector(Expression
* left
, const std::string
& name
,
10575 source_location location
)
10577 return new Selector_expression(left
, name
, location
);
10580 // Implement the builtin function new.
10582 class Allocation_expression
: public Expression
10585 Allocation_expression(Type
* type
, source_location location
)
10586 : Expression(EXPRESSION_ALLOCATION
, location
),
10592 do_traverse(Traverse
* traverse
)
10593 { return Type::traverse(this->type_
, traverse
); }
10597 { return Type::make_pointer_type(this->type_
); }
10600 do_determine_type(const Type_context
*)
10604 do_check_types(Gogo
*);
10608 { return new Allocation_expression(this->type_
, this->location()); }
10611 do_get_tree(Translate_context
*);
10614 // The type we are allocating.
10618 // Check the type of an allocation expression.
10621 Allocation_expression::do_check_types(Gogo
*)
10623 if (this->type_
->function_type() != NULL
)
10624 this->report_error(_("invalid new of function type"));
10627 // Return a tree for an allocation expression.
10630 Allocation_expression::do_get_tree(Translate_context
* context
)
10632 tree type_tree
= this->type_
->get_tree(context
->gogo());
10633 if (type_tree
== error_mark_node
)
10634 return error_mark_node
;
10635 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10636 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10638 if (space
== error_mark_node
)
10639 return error_mark_node
;
10640 return fold_convert(build_pointer_type(type_tree
), space
);
10643 // Make an allocation expression.
10646 Expression::make_allocation(Type
* type
, source_location location
)
10648 return new Allocation_expression(type
, location
);
10651 // Implement the builtin function make.
10653 class Make_expression
: public Expression
10656 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10657 : Expression(EXPRESSION_MAKE
, location
),
10658 type_(type
), args_(args
)
10663 do_traverse(Traverse
* traverse
);
10667 { return this->type_
; }
10670 do_determine_type(const Type_context
*);
10673 do_check_types(Gogo
*);
10678 return new Make_expression(this->type_
, this->args_
->copy(),
10683 do_get_tree(Translate_context
*);
10686 // The type we are making.
10688 // The arguments to pass to the make routine.
10689 Expression_list
* args_
;
10695 Make_expression::do_traverse(Traverse
* traverse
)
10697 if (this->args_
!= NULL
10698 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10699 return TRAVERSE_EXIT
;
10700 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10701 return TRAVERSE_EXIT
;
10702 return TRAVERSE_CONTINUE
;
10705 // Set types of arguments.
10708 Make_expression::do_determine_type(const Type_context
*)
10710 if (this->args_
!= NULL
)
10712 Type_context
context(Type::lookup_integer_type("int"), false);
10713 for (Expression_list::const_iterator pe
= this->args_
->begin();
10714 pe
!= this->args_
->end();
10716 (*pe
)->determine_type(&context
);
10720 // Check types for a make expression.
10723 Make_expression::do_check_types(Gogo
*)
10725 if (this->type_
->channel_type() == NULL
10726 && this->type_
->map_type() == NULL
10727 && (this->type_
->array_type() == NULL
10728 || this->type_
->array_type()->length() != NULL
))
10729 this->report_error(_("invalid type for make function"));
10730 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10731 this->set_is_error();
10734 // Return a tree for a make expression.
10737 Make_expression::do_get_tree(Translate_context
* context
)
10739 return this->type_
->make_expression_tree(context
, this->args_
,
10743 // Make a make expression.
10746 Expression::make_make(Type
* type
, Expression_list
* args
,
10747 source_location location
)
10749 return new Make_expression(type
, args
, location
);
10752 // Construct a struct.
10754 class Struct_construction_expression
: public Expression
10757 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10758 source_location location
)
10759 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10760 type_(type
), vals_(vals
)
10763 // Return whether this is a constant initializer.
10765 is_constant_struct() const;
10769 do_traverse(Traverse
* traverse
);
10773 { return this->type_
; }
10776 do_determine_type(const Type_context
*);
10779 do_check_types(Gogo
*);
10784 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10789 do_is_addressable() const
10793 do_get_tree(Translate_context
*);
10796 do_export(Export
*) const;
10799 // The type of the struct to construct.
10801 // The list of values, in order of the fields in the struct. A NULL
10802 // entry means that the field should be zero-initialized.
10803 Expression_list
* vals_
;
10809 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10811 if (this->vals_
!= NULL
10812 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10813 return TRAVERSE_EXIT
;
10814 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10815 return TRAVERSE_EXIT
;
10816 return TRAVERSE_CONTINUE
;
10819 // Return whether this is a constant initializer.
10822 Struct_construction_expression::is_constant_struct() const
10824 if (this->vals_
== NULL
)
10826 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10827 pv
!= this->vals_
->end();
10831 && !(*pv
)->is_constant()
10832 && (!(*pv
)->is_composite_literal()
10833 || (*pv
)->is_nonconstant_composite_literal()))
10837 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10838 for (Struct_field_list::const_iterator pf
= fields
->begin();
10839 pf
!= fields
->end();
10842 // There are no constant constructors for interfaces.
10843 if (pf
->type()->interface_type() != NULL
)
10850 // Final type determination.
10853 Struct_construction_expression::do_determine_type(const Type_context
*)
10855 if (this->vals_
== NULL
)
10857 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10858 Expression_list::const_iterator pv
= this->vals_
->begin();
10859 for (Struct_field_list::const_iterator pf
= fields
->begin();
10860 pf
!= fields
->end();
10863 if (pv
== this->vals_
->end())
10867 Type_context
subcontext(pf
->type(), false);
10868 (*pv
)->determine_type(&subcontext
);
10871 // Extra values are an error we will report elsewhere; we still want
10872 // to determine the type to avoid knockon errors.
10873 for (; pv
!= this->vals_
->end(); ++pv
)
10874 (*pv
)->determine_type_no_context();
10880 Struct_construction_expression::do_check_types(Gogo
*)
10882 if (this->vals_
== NULL
)
10885 Struct_type
* st
= this->type_
->struct_type();
10886 if (this->vals_
->size() > st
->field_count())
10888 this->report_error(_("too many expressions for struct"));
10892 const Struct_field_list
* fields
= st
->fields();
10893 Expression_list::const_iterator pv
= this->vals_
->begin();
10895 for (Struct_field_list::const_iterator pf
= fields
->begin();
10896 pf
!= fields
->end();
10899 if (pv
== this->vals_
->end())
10901 this->report_error(_("too few expressions for struct"));
10908 std::string reason
;
10909 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10911 if (reason
.empty())
10912 error_at((*pv
)->location(),
10913 "incompatible type for field %d in struct construction",
10916 error_at((*pv
)->location(),
10917 ("incompatible type for field %d in "
10918 "struct construction (%s)"),
10919 i
+ 1, reason
.c_str());
10920 this->set_is_error();
10923 gcc_assert(pv
== this->vals_
->end());
10926 // Return a tree for constructing a struct.
10929 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10931 Gogo
* gogo
= context
->gogo();
10933 if (this->vals_
== NULL
)
10934 return this->type_
->get_init_tree(gogo
, false);
10936 tree type_tree
= this->type_
->get_tree(gogo
);
10937 if (type_tree
== error_mark_node
)
10938 return error_mark_node
;
10939 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10941 bool is_constant
= true;
10942 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10943 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10945 Struct_field_list::const_iterator pf
= fields
->begin();
10946 Expression_list::const_iterator pv
= this->vals_
->begin();
10947 for (tree field
= TYPE_FIELDS(type_tree
);
10948 field
!= NULL_TREE
;
10949 field
= DECL_CHAIN(field
), ++pf
)
10951 gcc_assert(pf
!= fields
->end());
10954 if (pv
== this->vals_
->end())
10955 val
= pf
->type()->get_init_tree(gogo
, false);
10956 else if (*pv
== NULL
)
10958 val
= pf
->type()->get_init_tree(gogo
, false);
10963 val
= Expression::convert_for_assignment(context
, pf
->type(),
10965 (*pv
)->get_tree(context
),
10970 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10971 return error_mark_node
;
10973 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10974 elt
->index
= field
;
10976 if (!TREE_CONSTANT(val
))
10977 is_constant
= false;
10979 gcc_assert(pf
== fields
->end());
10981 tree ret
= build_constructor(type_tree
, elts
);
10983 TREE_CONSTANT(ret
) = 1;
10987 // Export a struct construction.
10990 Struct_construction_expression::do_export(Export
* exp
) const
10992 exp
->write_c_string("convert(");
10993 exp
->write_type(this->type_
);
10994 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10995 pv
!= this->vals_
->end();
10998 exp
->write_c_string(", ");
11000 (*pv
)->export_expression(exp
);
11002 exp
->write_c_string(")");
11005 // Make a struct composite literal. This used by the thunk code.
11008 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
11009 source_location location
)
11011 gcc_assert(type
->struct_type() != NULL
);
11012 return new Struct_construction_expression(type
, vals
, location
);
11015 // Construct an array. This class is not used directly; instead we
11016 // use the child classes, Fixed_array_construction_expression and
11017 // Open_array_construction_expression.
11019 class Array_construction_expression
: public Expression
11022 Array_construction_expression(Expression_classification classification
,
11023 Type
* type
, Expression_list
* vals
,
11024 source_location location
)
11025 : Expression(classification
, location
),
11026 type_(type
), vals_(vals
)
11030 // Return whether this is a constant initializer.
11032 is_constant_array() const;
11034 // Return the number of elements.
11036 element_count() const
11037 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
11041 do_traverse(Traverse
* traverse
);
11045 { return this->type_
; }
11048 do_determine_type(const Type_context
*);
11051 do_check_types(Gogo
*);
11054 do_is_addressable() const
11058 do_export(Export
*) const;
11060 // The list of values.
11063 { return this->vals_
; }
11065 // Get a constructor tree for the array values.
11067 get_constructor_tree(Translate_context
* context
, tree type_tree
);
11070 // The type of the array to construct.
11072 // The list of values.
11073 Expression_list
* vals_
;
11079 Array_construction_expression::do_traverse(Traverse
* traverse
)
11081 if (this->vals_
!= NULL
11082 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11083 return TRAVERSE_EXIT
;
11084 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11085 return TRAVERSE_EXIT
;
11086 return TRAVERSE_CONTINUE
;
11089 // Return whether this is a constant initializer.
11092 Array_construction_expression::is_constant_array() const
11094 if (this->vals_
== NULL
)
11097 // There are no constant constructors for interfaces.
11098 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
11101 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11102 pv
!= this->vals_
->end();
11106 && !(*pv
)->is_constant()
11107 && (!(*pv
)->is_composite_literal()
11108 || (*pv
)->is_nonconstant_composite_literal()))
11114 // Final type determination.
11117 Array_construction_expression::do_determine_type(const Type_context
*)
11119 if (this->vals_
== NULL
)
11121 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
11122 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11123 pv
!= this->vals_
->end();
11127 (*pv
)->determine_type(&subcontext
);
11134 Array_construction_expression::do_check_types(Gogo
*)
11136 if (this->vals_
== NULL
)
11139 Array_type
* at
= this->type_
->array_type();
11141 Type
* element_type
= at
->element_type();
11142 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11143 pv
!= this->vals_
->end();
11147 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
11149 error_at((*pv
)->location(),
11150 "incompatible type for element %d in composite literal",
11152 this->set_is_error();
11156 Expression
* length
= at
->length();
11157 if (length
!= NULL
)
11162 if (at
->length()->integer_constant_value(true, val
, &type
))
11164 if (this->vals_
->size() > mpz_get_ui(val
))
11165 this->report_error(_("too many elements in composite literal"));
11171 // Get a constructor tree for the array values.
11174 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
11177 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11178 (this->vals_
== NULL
11180 : this->vals_
->size()));
11181 Type
* element_type
= this->type_
->array_type()->element_type();
11182 bool is_constant
= true;
11183 if (this->vals_
!= NULL
)
11186 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11187 pv
!= this->vals_
->end();
11190 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11191 elt
->index
= size_int(i
);
11193 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
11196 tree value_tree
= (*pv
)->get_tree(context
);
11197 elt
->value
= Expression::convert_for_assignment(context
,
11203 if (elt
->value
== error_mark_node
)
11204 return error_mark_node
;
11205 if (!TREE_CONSTANT(elt
->value
))
11206 is_constant
= false;
11210 tree ret
= build_constructor(type_tree
, values
);
11212 TREE_CONSTANT(ret
) = 1;
11216 // Export an array construction.
11219 Array_construction_expression::do_export(Export
* exp
) const
11221 exp
->write_c_string("convert(");
11222 exp
->write_type(this->type_
);
11223 if (this->vals_
!= NULL
)
11225 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11226 pv
!= this->vals_
->end();
11229 exp
->write_c_string(", ");
11231 (*pv
)->export_expression(exp
);
11234 exp
->write_c_string(")");
11237 // Construct a fixed array.
11239 class Fixed_array_construction_expression
:
11240 public Array_construction_expression
11243 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
11244 source_location location
)
11245 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
11246 type
, vals
, location
)
11248 gcc_assert(type
->array_type() != NULL
11249 && type
->array_type()->length() != NULL
);
11256 return new Fixed_array_construction_expression(this->type(),
11257 (this->vals() == NULL
11259 : this->vals()->copy()),
11264 do_get_tree(Translate_context
*);
11267 // Return a tree for constructing a fixed array.
11270 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
11272 return this->get_constructor_tree(context
,
11273 this->type()->get_tree(context
->gogo()));
11276 // Construct an open array.
11278 class Open_array_construction_expression
: public Array_construction_expression
11281 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
11282 source_location location
)
11283 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
11284 type
, vals
, location
)
11286 gcc_assert(type
->array_type() != NULL
11287 && type
->array_type()->length() == NULL
);
11291 // Note that taking the address of an open array literal is invalid.
11296 return new Open_array_construction_expression(this->type(),
11297 (this->vals() == NULL
11299 : this->vals()->copy()),
11304 do_get_tree(Translate_context
*);
11307 // Return a tree for constructing an open array.
11310 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
11312 Array_type
* array_type
= this->type()->array_type();
11313 if (array_type
== NULL
)
11315 gcc_assert(this->type()->is_error());
11316 return error_mark_node
;
11319 Type
* element_type
= array_type
->element_type();
11320 tree element_type_tree
= element_type
->get_tree(context
->gogo());
11321 if (element_type_tree
== error_mark_node
)
11322 return error_mark_node
;
11326 if (this->vals() == NULL
|| this->vals()->empty())
11328 // We need to create a unique value.
11329 tree max
= size_int(0);
11330 tree constructor_type
= build_array_type(element_type_tree
,
11331 build_index_type(max
));
11332 if (constructor_type
== error_mark_node
)
11333 return error_mark_node
;
11334 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
11335 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
11336 elt
->index
= size_int(0);
11337 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
11338 values
= build_constructor(constructor_type
, vec
);
11339 if (TREE_CONSTANT(elt
->value
))
11340 TREE_CONSTANT(values
) = 1;
11341 length_tree
= size_int(0);
11345 tree max
= size_int(this->vals()->size() - 1);
11346 tree constructor_type
= build_array_type(element_type_tree
,
11347 build_index_type(max
));
11348 if (constructor_type
== error_mark_node
)
11349 return error_mark_node
;
11350 values
= this->get_constructor_tree(context
, constructor_type
);
11351 length_tree
= size_int(this->vals()->size());
11354 if (values
== error_mark_node
)
11355 return error_mark_node
;
11357 bool is_constant_initializer
= TREE_CONSTANT(values
);
11359 // We have to copy the initial values into heap memory if we are in
11360 // a function or if the values are not constants. We also have to
11361 // copy them if they may contain pointers in a non-constant context,
11362 // as otherwise the garbage collector won't see them.
11363 bool copy_to_heap
= (context
->function() != NULL
11364 || !is_constant_initializer
11365 || (element_type
->has_pointer()
11366 && !context
->is_const()));
11368 if (is_constant_initializer
)
11370 tree tmp
= build_decl(this->location(), VAR_DECL
,
11371 create_tmp_var_name("C"), TREE_TYPE(values
));
11372 DECL_EXTERNAL(tmp
) = 0;
11373 TREE_PUBLIC(tmp
) = 0;
11374 TREE_STATIC(tmp
) = 1;
11375 DECL_ARTIFICIAL(tmp
) = 1;
11378 // If we are not copying the value to the heap, we will only
11379 // initialize the value once, so we can use this directly
11380 // rather than copying it. In that case we can't make it
11381 // read-only, because the program is permitted to change it.
11382 TREE_READONLY(tmp
) = 1;
11383 TREE_CONSTANT(tmp
) = 1;
11385 DECL_INITIAL(tmp
) = values
;
11386 rest_of_decl_compilation(tmp
, 1, 0);
11394 // the initializer will only run once.
11395 space
= build_fold_addr_expr(values
);
11400 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
11401 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
11403 space
= save_expr(space
);
11405 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
11406 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
11407 TREE_THIS_NOTRAP(ref
) = 1;
11408 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
11411 // Build a constructor for the open array.
11413 tree type_tree
= this->type()->get_tree(context
->gogo());
11414 if (type_tree
== error_mark_node
)
11415 return error_mark_node
;
11416 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
11418 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
11420 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11421 tree field
= TYPE_FIELDS(type_tree
);
11422 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
11423 elt
->index
= field
;
11424 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
11426 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11427 field
= DECL_CHAIN(field
);
11428 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
11429 elt
->index
= field
;
11430 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11432 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11433 field
= DECL_CHAIN(field
);
11434 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
11435 elt
->index
= field
;
11436 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11438 tree constructor
= build_constructor(type_tree
, init
);
11439 if (constructor
== error_mark_node
)
11440 return error_mark_node
;
11442 TREE_CONSTANT(constructor
) = 1;
11444 if (set
== NULL_TREE
)
11445 return constructor
;
11447 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
11450 // Make a slice composite literal. This is used by the type
11451 // descriptor code.
11454 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
11455 source_location location
)
11457 gcc_assert(type
->is_open_array_type());
11458 return new Open_array_construction_expression(type
, vals
, location
);
11461 // Construct a map.
11463 class Map_construction_expression
: public Expression
11466 Map_construction_expression(Type
* type
, Expression_list
* vals
,
11467 source_location location
)
11468 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
11469 type_(type
), vals_(vals
)
11470 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
11474 do_traverse(Traverse
* traverse
);
11478 { return this->type_
; }
11481 do_determine_type(const Type_context
*);
11484 do_check_types(Gogo
*);
11489 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
11494 do_get_tree(Translate_context
*);
11497 do_export(Export
*) const;
11500 // The type of the map to construct.
11502 // The list of values.
11503 Expression_list
* vals_
;
11509 Map_construction_expression::do_traverse(Traverse
* traverse
)
11511 if (this->vals_
!= NULL
11512 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11513 return TRAVERSE_EXIT
;
11514 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11515 return TRAVERSE_EXIT
;
11516 return TRAVERSE_CONTINUE
;
11519 // Final type determination.
11522 Map_construction_expression::do_determine_type(const Type_context
*)
11524 if (this->vals_
== NULL
)
11527 Map_type
* mt
= this->type_
->map_type();
11528 Type_context
key_context(mt
->key_type(), false);
11529 Type_context
val_context(mt
->val_type(), false);
11530 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11531 pv
!= this->vals_
->end();
11534 (*pv
)->determine_type(&key_context
);
11536 (*pv
)->determine_type(&val_context
);
11543 Map_construction_expression::do_check_types(Gogo
*)
11545 if (this->vals_
== NULL
)
11548 Map_type
* mt
= this->type_
->map_type();
11550 Type
* key_type
= mt
->key_type();
11551 Type
* val_type
= mt
->val_type();
11552 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11553 pv
!= this->vals_
->end();
11556 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
11558 error_at((*pv
)->location(),
11559 "incompatible type for element %d key in map construction",
11561 this->set_is_error();
11564 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
11566 error_at((*pv
)->location(),
11567 ("incompatible type for element %d value "
11568 "in map construction"),
11570 this->set_is_error();
11575 // Return a tree for constructing a map.
11578 Map_construction_expression::do_get_tree(Translate_context
* context
)
11580 Gogo
* gogo
= context
->gogo();
11581 source_location loc
= this->location();
11583 Map_type
* mt
= this->type_
->map_type();
11585 // Build a struct to hold the key and value.
11586 tree struct_type
= make_node(RECORD_TYPE
);
11588 Type
* key_type
= mt
->key_type();
11589 tree id
= get_identifier("__key");
11590 tree key_type_tree
= key_type
->get_tree(gogo
);
11591 if (key_type_tree
== error_mark_node
)
11592 return error_mark_node
;
11593 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type_tree
);
11594 DECL_CONTEXT(key_field
) = struct_type
;
11595 TYPE_FIELDS(struct_type
) = key_field
;
11597 Type
* val_type
= mt
->val_type();
11598 id
= get_identifier("__val");
11599 tree val_type_tree
= val_type
->get_tree(gogo
);
11600 if (val_type_tree
== error_mark_node
)
11601 return error_mark_node
;
11602 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type_tree
);
11603 DECL_CONTEXT(val_field
) = struct_type
;
11604 DECL_CHAIN(key_field
) = val_field
;
11606 layout_type(struct_type
);
11608 bool is_constant
= true;
11613 if (this->vals_
== NULL
|| this->vals_
->empty())
11615 valaddr
= null_pointer_node
;
11616 make_tmp
= NULL_TREE
;
11620 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11621 this->vals_
->size() / 2);
11623 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11624 pv
!= this->vals_
->end();
11627 bool one_is_constant
= true;
11629 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11631 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11632 elt
->index
= key_field
;
11633 tree val_tree
= (*pv
)->get_tree(context
);
11634 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11637 if (elt
->value
== error_mark_node
)
11638 return error_mark_node
;
11639 if (!TREE_CONSTANT(elt
->value
))
11640 one_is_constant
= false;
11644 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11645 elt
->index
= val_field
;
11646 val_tree
= (*pv
)->get_tree(context
);
11647 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11650 if (elt
->value
== error_mark_node
)
11651 return error_mark_node
;
11652 if (!TREE_CONSTANT(elt
->value
))
11653 one_is_constant
= false;
11655 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11656 elt
->index
= size_int(i
);
11657 elt
->value
= build_constructor(struct_type
, one
);
11658 if (one_is_constant
)
11659 TREE_CONSTANT(elt
->value
) = 1;
11661 is_constant
= false;
11664 tree index_type
= build_index_type(size_int(i
- 1));
11665 tree array_type
= build_array_type(struct_type
, index_type
);
11666 tree init
= build_constructor(array_type
, values
);
11668 TREE_CONSTANT(init
) = 1;
11670 if (current_function_decl
!= NULL
)
11672 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11673 DECL_INITIAL(tmp
) = init
;
11674 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11675 TREE_ADDRESSABLE(tmp
) = 1;
11679 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11680 DECL_EXTERNAL(tmp
) = 0;
11681 TREE_PUBLIC(tmp
) = 0;
11682 TREE_STATIC(tmp
) = 1;
11683 DECL_ARTIFICIAL(tmp
) = 1;
11684 if (!TREE_CONSTANT(init
))
11685 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11689 TREE_READONLY(tmp
) = 1;
11690 TREE_CONSTANT(tmp
) = 1;
11691 DECL_INITIAL(tmp
) = init
;
11692 make_tmp
= NULL_TREE
;
11694 rest_of_decl_compilation(tmp
, 1, 0);
11697 valaddr
= build_fold_addr_expr(tmp
);
11700 tree descriptor
= gogo
->map_descriptor(mt
);
11702 tree type_tree
= this->type_
->get_tree(gogo
);
11703 if (type_tree
== error_mark_node
)
11704 return error_mark_node
;
11706 static tree construct_map_fndecl
;
11707 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11709 "__go_construct_map",
11712 TREE_TYPE(descriptor
),
11717 TYPE_SIZE_UNIT(struct_type
),
11719 byte_position(val_field
),
11721 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11722 const_ptr_type_node
,
11723 fold_convert(const_ptr_type_node
, valaddr
));
11724 if (call
== error_mark_node
)
11725 return error_mark_node
;
11728 if (make_tmp
== NULL
)
11731 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11735 // Export an array construction.
11738 Map_construction_expression::do_export(Export
* exp
) const
11740 exp
->write_c_string("convert(");
11741 exp
->write_type(this->type_
);
11742 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11743 pv
!= this->vals_
->end();
11746 exp
->write_c_string(", ");
11747 (*pv
)->export_expression(exp
);
11749 exp
->write_c_string(")");
11752 // A general composite literal. This is lowered to a type specific
11755 class Composite_literal_expression
: public Parser_expression
11758 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11759 Expression_list
* vals
, source_location location
)
11760 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11761 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11766 do_traverse(Traverse
* traverse
);
11769 do_lower(Gogo
*, Named_object
*, int);
11774 return new Composite_literal_expression(this->type_
, this->depth_
,
11776 (this->vals_
== NULL
11778 : this->vals_
->copy()),
11784 lower_struct(Gogo
*, Type
*);
11787 lower_array(Type
*);
11790 make_array(Type
*, Expression_list
*);
11793 lower_map(Gogo
*, Named_object
*, Type
*);
11795 // The type of the composite literal.
11797 // The depth within a list of composite literals within a composite
11798 // literal, when the type is omitted.
11800 // The values to put in the composite literal.
11801 Expression_list
* vals_
;
11802 // If this is true, then VALS_ is a list of pairs: a key and a
11803 // value. In an array initializer, a missing key will be NULL.
11810 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11812 if (this->vals_
!= NULL
11813 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11814 return TRAVERSE_EXIT
;
11815 return Type::traverse(this->type_
, traverse
);
11818 // Lower a generic composite literal into a specific version based on
11822 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
11824 Type
* type
= this->type_
;
11826 for (int depth
= this->depth_
; depth
> 0; --depth
)
11828 if (type
->array_type() != NULL
)
11829 type
= type
->array_type()->element_type();
11830 else if (type
->map_type() != NULL
)
11831 type
= type
->map_type()->val_type();
11834 if (!type
->is_error())
11835 error_at(this->location(),
11836 ("may only omit types within composite literals "
11837 "of slice, array, or map type"));
11838 return Expression::make_error(this->location());
11842 if (type
->is_error())
11843 return Expression::make_error(this->location());
11844 else if (type
->struct_type() != NULL
)
11845 return this->lower_struct(gogo
, type
);
11846 else if (type
->array_type() != NULL
)
11847 return this->lower_array(type
);
11848 else if (type
->map_type() != NULL
)
11849 return this->lower_map(gogo
, function
, type
);
11852 error_at(this->location(),
11853 ("expected struct, slice, array, or map type "
11854 "for composite literal"));
11855 return Expression::make_error(this->location());
11859 // Lower a struct composite literal.
11862 Composite_literal_expression::lower_struct(Gogo
* gogo
, Type
* type
)
11864 source_location location
= this->location();
11865 Struct_type
* st
= type
->struct_type();
11866 if (this->vals_
== NULL
|| !this->has_keys_
)
11867 return new Struct_construction_expression(type
, this->vals_
, location
);
11869 size_t field_count
= st
->field_count();
11870 std::vector
<Expression
*> vals(field_count
);
11871 Expression_list::const_iterator p
= this->vals_
->begin();
11872 while (p
!= this->vals_
->end())
11874 Expression
* name_expr
= *p
;
11877 gcc_assert(p
!= this->vals_
->end());
11878 Expression
* val
= *p
;
11882 if (name_expr
== NULL
)
11884 error_at(val
->location(), "mixture of field and value initializers");
11885 return Expression::make_error(location
);
11888 bool bad_key
= false;
11890 const Named_object
* no
= NULL
;
11891 switch (name_expr
->classification())
11893 case EXPRESSION_UNKNOWN_REFERENCE
:
11894 name
= name_expr
->unknown_expression()->name();
11897 case EXPRESSION_CONST_REFERENCE
:
11898 no
= static_cast<Const_expression
*>(name_expr
)->named_object();
11901 case EXPRESSION_TYPE
:
11903 Type
* t
= name_expr
->type();
11904 Named_type
* nt
= t
->named_type();
11908 no
= nt
->named_object();
11912 case EXPRESSION_VAR_REFERENCE
:
11913 no
= name_expr
->var_expression()->named_object();
11916 case EXPRESSION_FUNC_REFERENCE
:
11917 no
= name_expr
->func_expression()->named_object();
11920 case EXPRESSION_UNARY
:
11921 // If there is a local variable around with the same name as
11922 // the field, and this occurs in the closure, then the
11923 // parser may turn the field reference into an indirection
11924 // through the closure. FIXME: This is a mess.
11927 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11928 if (ue
->op() == OPERATOR_MULT
)
11930 Field_reference_expression
* fre
=
11931 ue
->operand()->field_reference_expression();
11935 fre
->expr()->type()->deref()->struct_type();
11938 const Struct_field
* sf
= st
->field(fre
->field_index());
11939 name
= sf
->field_name();
11941 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11942 size_t buflen
= strlen(buf
);
11943 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11946 name
= name
.substr(0, name
.length() - buflen
);
11961 error_at(name_expr
->location(), "expected struct field name");
11962 return Expression::make_error(location
);
11969 // A predefined name won't be packed. If it starts with a
11970 // lower case letter we need to check for that case, because
11971 // the field name will be packed.
11972 if (!Gogo::is_hidden_name(name
)
11976 Named_object
* gno
= gogo
->lookup_global(name
.c_str());
11978 name
= gogo
->pack_hidden_name(name
, false);
11982 unsigned int index
;
11983 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11986 error_at(name_expr
->location(), "unknown field %qs in %qs",
11987 Gogo::message_name(name
).c_str(),
11988 (type
->named_type() != NULL
11989 ? type
->named_type()->message_name().c_str()
11990 : "unnamed struct"));
11991 return Expression::make_error(location
);
11993 if (vals
[index
] != NULL
)
11995 error_at(name_expr
->location(),
11996 "duplicate value for field %qs in %qs",
11997 Gogo::message_name(name
).c_str(),
11998 (type
->named_type() != NULL
11999 ? type
->named_type()->message_name().c_str()
12000 : "unnamed struct"));
12001 return Expression::make_error(location
);
12007 Expression_list
* list
= new Expression_list
;
12008 list
->reserve(field_count
);
12009 for (size_t i
= 0; i
< field_count
; ++i
)
12010 list
->push_back(vals
[i
]);
12012 return new Struct_construction_expression(type
, list
, location
);
12015 // Lower an array composite literal.
12018 Composite_literal_expression::lower_array(Type
* type
)
12020 source_location location
= this->location();
12021 if (this->vals_
== NULL
|| !this->has_keys_
)
12022 return this->make_array(type
, this->vals_
);
12024 std::vector
<Expression
*> vals
;
12025 vals
.reserve(this->vals_
->size());
12026 unsigned long index
= 0;
12027 Expression_list::const_iterator p
= this->vals_
->begin();
12028 while (p
!= this->vals_
->end())
12030 Expression
* index_expr
= *p
;
12033 gcc_assert(p
!= this->vals_
->end());
12034 Expression
* val
= *p
;
12038 if (index_expr
!= NULL
)
12044 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
12047 error_at(index_expr
->location(),
12048 "index expression is not integer constant");
12049 return Expression::make_error(location
);
12052 if (mpz_sgn(ival
) < 0)
12055 error_at(index_expr
->location(), "index expression is negative");
12056 return Expression::make_error(location
);
12059 index
= mpz_get_ui(ival
);
12060 if (mpz_cmp_ui(ival
, index
) != 0)
12063 error_at(index_expr
->location(), "index value overflow");
12064 return Expression::make_error(location
);
12067 Named_type
* ntype
= Type::lookup_integer_type("int");
12068 Integer_type
* inttype
= ntype
->integer_type();
12070 mpz_init_set_ui(max
, 1);
12071 mpz_mul_2exp(max
, max
, inttype
->bits() - 1);
12072 bool ok
= mpz_cmp(ival
, max
) < 0;
12077 error_at(index_expr
->location(), "index value overflow");
12078 return Expression::make_error(location
);
12083 // FIXME: Our representation isn't very good; this avoids
12085 if (index
> 0x1000000)
12087 error_at(index_expr
->location(), "index too large for compiler");
12088 return Expression::make_error(location
);
12092 if (index
== vals
.size())
12093 vals
.push_back(val
);
12096 if (index
> vals
.size())
12098 vals
.reserve(index
+ 32);
12099 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
12101 if (vals
[index
] != NULL
)
12103 error_at((index_expr
!= NULL
12104 ? index_expr
->location()
12105 : val
->location()),
12106 "duplicate value for index %lu",
12108 return Expression::make_error(location
);
12116 size_t size
= vals
.size();
12117 Expression_list
* list
= new Expression_list
;
12118 list
->reserve(size
);
12119 for (size_t i
= 0; i
< size
; ++i
)
12120 list
->push_back(vals
[i
]);
12122 return this->make_array(type
, list
);
12125 // Actually build the array composite literal. This handles
12129 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
12131 source_location location
= this->location();
12132 Array_type
* at
= type
->array_type();
12133 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
12135 size_t size
= vals
== NULL
? 0 : vals
->size();
12137 mpz_init_set_ui(vlen
, size
);
12138 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
12140 at
= Type::make_array_type(at
->element_type(), elen
);
12143 if (at
->length() != NULL
)
12144 return new Fixed_array_construction_expression(type
, vals
, location
);
12146 return new Open_array_construction_expression(type
, vals
, location
);
12149 // Lower a map composite literal.
12152 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
12155 source_location location
= this->location();
12156 if (this->vals_
!= NULL
)
12158 if (!this->has_keys_
)
12160 error_at(location
, "map composite literal must have keys");
12161 return Expression::make_error(location
);
12164 for (Expression_list::iterator p
= this->vals_
->begin();
12165 p
!= this->vals_
->end();
12171 error_at((*p
)->location(),
12172 "map composite literal must have keys for every value");
12173 return Expression::make_error(location
);
12175 // Make sure we have lowered the key; it may not have been
12176 // lowered in order to handle keys for struct composite
12177 // literals. Lower it now to get the right error message.
12178 if ((*p
)->unknown_expression() != NULL
)
12180 (*p
)->unknown_expression()->clear_is_composite_literal_key();
12181 gogo
->lower_expression(function
, &*p
);
12182 gcc_assert((*p
)->is_error_expression());
12183 return Expression::make_error(location
);
12188 return new Map_construction_expression(type
, this->vals_
, location
);
12191 // Make a composite literal expression.
12194 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
12195 Expression_list
* vals
,
12196 source_location location
)
12198 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
12202 // Return whether this expression is a composite literal.
12205 Expression::is_composite_literal() const
12207 switch (this->classification_
)
12209 case EXPRESSION_COMPOSITE_LITERAL
:
12210 case EXPRESSION_STRUCT_CONSTRUCTION
:
12211 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
12212 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
12213 case EXPRESSION_MAP_CONSTRUCTION
:
12220 // Return whether this expression is a composite literal which is not
12224 Expression::is_nonconstant_composite_literal() const
12226 switch (this->classification_
)
12228 case EXPRESSION_STRUCT_CONSTRUCTION
:
12230 const Struct_construction_expression
*psce
=
12231 static_cast<const Struct_construction_expression
*>(this);
12232 return !psce
->is_constant_struct();
12234 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
12236 const Fixed_array_construction_expression
*pace
=
12237 static_cast<const Fixed_array_construction_expression
*>(this);
12238 return !pace
->is_constant_array();
12240 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
12242 const Open_array_construction_expression
*pace
=
12243 static_cast<const Open_array_construction_expression
*>(this);
12244 return !pace
->is_constant_array();
12246 case EXPRESSION_MAP_CONSTRUCTION
:
12253 // Return true if this is a reference to a local variable.
12256 Expression::is_local_variable() const
12258 const Var_expression
* ve
= this->var_expression();
12261 const Named_object
* no
= ve
->named_object();
12262 return (no
->is_result_variable()
12263 || (no
->is_variable() && !no
->var_value()->is_global()));
12266 // Class Type_guard_expression.
12271 Type_guard_expression::do_traverse(Traverse
* traverse
)
12273 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
12274 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
12275 return TRAVERSE_EXIT
;
12276 return TRAVERSE_CONTINUE
;
12279 // Check types of a type guard expression. The expression must have
12280 // an interface type, but the actual type conversion is checked at run
12284 Type_guard_expression::do_check_types(Gogo
*)
12286 // 6g permits using a type guard with unsafe.pointer; we are
12288 Type
* expr_type
= this->expr_
->type();
12289 if (expr_type
->is_unsafe_pointer_type())
12291 if (this->type_
->points_to() == NULL
12292 && (this->type_
->integer_type() == NULL
12293 || (this->type_
->forwarded()
12294 != Type::lookup_integer_type("uintptr"))))
12295 this->report_error(_("invalid unsafe.Pointer conversion"));
12297 else if (this->type_
->is_unsafe_pointer_type())
12299 if (expr_type
->points_to() == NULL
12300 && (expr_type
->integer_type() == NULL
12301 || (expr_type
->forwarded()
12302 != Type::lookup_integer_type("uintptr"))))
12303 this->report_error(_("invalid unsafe.Pointer conversion"));
12305 else if (expr_type
->interface_type() == NULL
)
12307 if (!expr_type
->is_error() && !this->type_
->is_error())
12308 this->report_error(_("type assertion only valid for interface types"));
12309 this->set_is_error();
12311 else if (this->type_
->interface_type() == NULL
)
12313 std::string reason
;
12314 if (!expr_type
->interface_type()->implements_interface(this->type_
,
12317 if (!this->type_
->is_error())
12319 if (reason
.empty())
12320 this->report_error(_("impossible type assertion: "
12321 "type does not implement interface"));
12323 error_at(this->location(),
12324 ("impossible type assertion: "
12325 "type does not implement interface (%s)"),
12328 this->set_is_error();
12333 // Return a tree for a type guard expression.
12336 Type_guard_expression::do_get_tree(Translate_context
* context
)
12338 Gogo
* gogo
= context
->gogo();
12339 tree expr_tree
= this->expr_
->get_tree(context
);
12340 if (expr_tree
== error_mark_node
)
12341 return error_mark_node
;
12342 Type
* expr_type
= this->expr_
->type();
12343 if ((this->type_
->is_unsafe_pointer_type()
12344 && (expr_type
->points_to() != NULL
12345 || expr_type
->integer_type() != NULL
))
12346 || (expr_type
->is_unsafe_pointer_type()
12347 && this->type_
->points_to() != NULL
))
12348 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
12349 else if (expr_type
->is_unsafe_pointer_type()
12350 && this->type_
->integer_type() != NULL
)
12351 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
12352 else if (this->type_
->interface_type() != NULL
)
12353 return Expression::convert_interface_to_interface(context
, this->type_
,
12354 this->expr_
->type(),
12358 return Expression::convert_for_assignment(context
, this->type_
,
12359 this->expr_
->type(), expr_tree
,
12363 // Make a type guard expression.
12366 Expression::make_type_guard(Expression
* expr
, Type
* type
,
12367 source_location location
)
12369 return new Type_guard_expression(expr
, type
, location
);
12372 // Class Heap_composite_expression.
12374 // When you take the address of a composite literal, it is allocated
12375 // on the heap. This class implements that.
12377 class Heap_composite_expression
: public Expression
12380 Heap_composite_expression(Expression
* expr
, source_location location
)
12381 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
12387 do_traverse(Traverse
* traverse
)
12388 { return Expression::traverse(&this->expr_
, traverse
); }
12392 { return Type::make_pointer_type(this->expr_
->type()); }
12395 do_determine_type(const Type_context
*)
12396 { this->expr_
->determine_type_no_context(); }
12401 return Expression::make_heap_composite(this->expr_
->copy(),
12406 do_get_tree(Translate_context
*);
12408 // We only export global objects, and the parser does not generate
12409 // this in global scope.
12411 do_export(Export
*) const
12412 { gcc_unreachable(); }
12415 // The composite literal which is being put on the heap.
12419 // Return a tree which allocates a composite literal on the heap.
12422 Heap_composite_expression::do_get_tree(Translate_context
* context
)
12424 tree expr_tree
= this->expr_
->get_tree(context
);
12425 if (expr_tree
== error_mark_node
)
12426 return error_mark_node
;
12427 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
12428 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
12429 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
12430 expr_size
, this->location());
12431 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
12432 space
= save_expr(space
);
12433 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
12434 TREE_THIS_NOTRAP(ref
) = 1;
12435 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
12436 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
12438 SET_EXPR_LOCATION(ret
, this->location());
12442 // Allocate a composite literal on the heap.
12445 Expression::make_heap_composite(Expression
* expr
, source_location location
)
12447 return new Heap_composite_expression(expr
, location
);
12450 // Class Receive_expression.
12452 // Return the type of a receive expression.
12455 Receive_expression::do_type()
12457 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12458 if (channel_type
== NULL
)
12459 return Type::make_error_type();
12460 return channel_type
->element_type();
12463 // Check types for a receive expression.
12466 Receive_expression::do_check_types(Gogo
*)
12468 Type
* type
= this->channel_
->type();
12469 if (type
->is_error())
12471 this->set_is_error();
12474 if (type
->channel_type() == NULL
)
12476 this->report_error(_("expected channel"));
12479 if (!type
->channel_type()->may_receive())
12481 this->report_error(_("invalid receive on send-only channel"));
12486 // Get a tree for a receive expression.
12489 Receive_expression::do_get_tree(Translate_context
* context
)
12491 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12492 if (channel_type
== NULL
)
12494 gcc_assert(this->channel_
->type()->is_error());
12495 return error_mark_node
;
12497 Type
* element_type
= channel_type
->element_type();
12498 tree element_type_tree
= element_type
->get_tree(context
->gogo());
12500 tree channel
= this->channel_
->get_tree(context
);
12501 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
12502 return error_mark_node
;
12504 return Gogo::receive_from_channel(element_type_tree
, channel
,
12505 this->for_select_
, this->location());
12508 // Make a receive expression.
12510 Receive_expression
*
12511 Expression::make_receive(Expression
* channel
, source_location location
)
12513 return new Receive_expression(channel
, location
);
12516 // An expression which evaluates to a pointer to the type descriptor
12519 class Type_descriptor_expression
: public Expression
12522 Type_descriptor_expression(Type
* type
, source_location location
)
12523 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
12530 { return Type::make_type_descriptor_ptr_type(); }
12533 do_determine_type(const Type_context
*)
12541 do_get_tree(Translate_context
* context
)
12542 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
12545 // The type for which this is the descriptor.
12549 // Make a type descriptor expression.
12552 Expression::make_type_descriptor(Type
* type
, source_location location
)
12554 return new Type_descriptor_expression(type
, location
);
12557 // An expression which evaluates to some characteristic of a type.
12558 // This is only used to initialize fields of a type descriptor. Using
12559 // a new expression class is slightly inefficient but gives us a good
12560 // separation between the frontend and the middle-end with regard to
12561 // how types are laid out.
12563 class Type_info_expression
: public Expression
12566 Type_info_expression(Type
* type
, Type_info type_info
)
12567 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
12568 type_(type
), type_info_(type_info
)
12576 do_determine_type(const Type_context
*)
12584 do_get_tree(Translate_context
* context
);
12587 // The type for which we are getting information.
12589 // What information we want.
12590 Type_info type_info_
;
12593 // The type is chosen to match what the type descriptor struct
12597 Type_info_expression::do_type()
12599 switch (this->type_info_
)
12601 case TYPE_INFO_SIZE
:
12602 return Type::lookup_integer_type("uintptr");
12603 case TYPE_INFO_ALIGNMENT
:
12604 case TYPE_INFO_FIELD_ALIGNMENT
:
12605 return Type::lookup_integer_type("uint8");
12611 // Return type information in GENERIC.
12614 Type_info_expression::do_get_tree(Translate_context
* context
)
12616 tree type_tree
= this->type_
->get_tree(context
->gogo());
12617 if (type_tree
== error_mark_node
)
12618 return error_mark_node
;
12620 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12621 gcc_assert(val_type_tree
!= error_mark_node
);
12623 if (this->type_info_
== TYPE_INFO_SIZE
)
12624 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12625 TYPE_SIZE_UNIT(type_tree
));
12629 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12630 val
= go_type_alignment(type_tree
);
12632 val
= go_field_alignment(type_tree
);
12633 return build_int_cstu(val_type_tree
, val
);
12637 // Make a type info expression.
12640 Expression::make_type_info(Type
* type
, Type_info type_info
)
12642 return new Type_info_expression(type
, type_info
);
12645 // An expression which evaluates to the offset of a field within a
12646 // struct. This, like Type_info_expression, q.v., is only used to
12647 // initialize fields of a type descriptor.
12649 class Struct_field_offset_expression
: public Expression
12652 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12653 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12654 type_(type
), field_(field
)
12660 { return Type::lookup_integer_type("uintptr"); }
12663 do_determine_type(const Type_context
*)
12671 do_get_tree(Translate_context
* context
);
12674 // The type of the struct.
12675 Struct_type
* type_
;
12677 const Struct_field
* field_
;
12680 // Return a struct field offset in GENERIC.
12683 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12685 tree type_tree
= this->type_
->get_tree(context
->gogo());
12686 if (type_tree
== error_mark_node
)
12687 return error_mark_node
;
12689 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12690 gcc_assert(val_type_tree
!= error_mark_node
);
12692 const Struct_field_list
* fields
= this->type_
->fields();
12693 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12694 Struct_field_list::const_iterator p
;
12695 for (p
= fields
->begin();
12696 p
!= fields
->end();
12697 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12699 gcc_assert(struct_field_tree
!= NULL_TREE
);
12700 if (&*p
== this->field_
)
12703 gcc_assert(&*p
== this->field_
);
12705 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12706 byte_position(struct_field_tree
));
12709 // Make an expression for a struct field offset.
12712 Expression::make_struct_field_offset(Struct_type
* type
,
12713 const Struct_field
* field
)
12715 return new Struct_field_offset_expression(type
, field
);
12718 // An expression which evaluates to the address of an unnamed label.
12720 class Label_addr_expression
: public Expression
12723 Label_addr_expression(Label
* label
, source_location location
)
12724 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12731 { return Type::make_pointer_type(Type::make_void_type()); }
12734 do_determine_type(const Type_context
*)
12739 { return new Label_addr_expression(this->label_
, this->location()); }
12742 do_get_tree(Translate_context
* context
)
12744 return expr_to_tree(this->label_
->get_addr(context
, this->location()));
12748 // The label whose address we are taking.
12752 // Make an expression for the address of an unnamed label.
12755 Expression::make_label_addr(Label
* label
, source_location location
)
12757 return new Label_addr_expression(label
, location
);
12760 // Import an expression. This comes at the end in order to see the
12761 // various class definitions.
12764 Expression::import_expression(Import
* imp
)
12766 int c
= imp
->peek_char();
12767 if (imp
->match_c_string("- ")
12768 || imp
->match_c_string("! ")
12769 || imp
->match_c_string("^ "))
12770 return Unary_expression::do_import(imp
);
12772 return Binary_expression::do_import(imp
);
12773 else if (imp
->match_c_string("true")
12774 || imp
->match_c_string("false"))
12775 return Boolean_expression::do_import(imp
);
12777 return String_expression::do_import(imp
);
12778 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12780 // This handles integers, floats and complex constants.
12781 return Integer_expression::do_import(imp
);
12783 else if (imp
->match_c_string("nil"))
12784 return Nil_expression::do_import(imp
);
12785 else if (imp
->match_c_string("convert"))
12786 return Type_conversion_expression::do_import(imp
);
12789 error_at(imp
->location(), "import error: expected expression");
12790 return Expression::make_error(imp
->location());
12794 // Class Expression_list.
12796 // Traverse the list.
12799 Expression_list::traverse(Traverse
* traverse
)
12801 for (Expression_list::iterator p
= this->begin();
12807 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12808 return TRAVERSE_EXIT
;
12811 return TRAVERSE_CONTINUE
;
12817 Expression_list::copy()
12819 Expression_list
* ret
= new Expression_list();
12820 for (Expression_list::iterator p
= this->begin();
12825 ret
->push_back(NULL
);
12827 ret
->push_back((*p
)->copy());
12832 // Return whether an expression list has an error expression.
12835 Expression_list::contains_error() const
12837 for (Expression_list::const_iterator p
= this->begin();
12840 if (*p
!= NULL
&& (*p
)->is_error_expression())