1 // expressions.cc -- Go frontend expression handling.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #ifndef ENABLE_BUILD_WITH_CXX
20 #include "tree-iterator.h"
25 #ifndef ENABLE_BUILD_WITH_CXX
34 #include "statements.h"
36 #include "expressions.h"
40 Expression::Expression(Expression_classification classification
,
41 source_location location
)
42 : classification_(classification
), location_(location
)
46 Expression::~Expression()
50 // If this expression has a constant integer value, return it.
53 Expression::integer_constant_value(bool iota_is_constant
, mpz_t val
,
57 return this->do_integer_constant_value(iota_is_constant
, val
, ptype
);
60 // If this expression has a constant floating point value, return it.
63 Expression::float_constant_value(mpfr_t val
, Type
** ptype
) const
66 if (this->do_float_constant_value(val
, ptype
))
72 if (!this->do_integer_constant_value(false, ival
, &t
))
76 mpfr_set_z(val
, ival
, GMP_RNDN
);
83 // If this expression has a constant complex value, return it.
86 Expression::complex_constant_value(mpfr_t real
, mpfr_t imag
,
90 if (this->do_complex_constant_value(real
, imag
, ptype
))
93 if (this->float_constant_value(real
, &t
))
95 mpfr_set_ui(imag
, 0, GMP_RNDN
);
101 // Traverse the expressions.
104 Expression::traverse(Expression
** pexpr
, Traverse
* traverse
)
106 Expression
* expr
= *pexpr
;
107 if ((traverse
->traverse_mask() & Traverse::traverse_expressions
) != 0)
109 int t
= traverse
->expression(pexpr
);
110 if (t
== TRAVERSE_EXIT
)
111 return TRAVERSE_EXIT
;
112 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
113 return TRAVERSE_CONTINUE
;
115 return expr
->do_traverse(traverse
);
118 // Traverse subexpressions of this expression.
121 Expression::traverse_subexpressions(Traverse
* traverse
)
123 return this->do_traverse(traverse
);
126 // Default implementation for do_traverse for child classes.
129 Expression::do_traverse(Traverse
*)
131 return TRAVERSE_CONTINUE
;
134 // This virtual function is called by the parser if the value of this
135 // expression is being discarded. By default, we warn. Expressions
136 // with side effects override.
139 Expression::do_discarding_value()
141 this->warn_about_unused_value();
144 // This virtual function is called to export expressions. This will
145 // only be used by expressions which may be constant.
148 Expression::do_export(Export
*) const
153 // Warn that the value of the expression is not used.
156 Expression::warn_about_unused_value()
158 warning_at(this->location(), OPT_Wunused_value
, "value computed is not used");
161 // Note that this expression is an error. This is called by children
162 // when they discover an error.
165 Expression::set_is_error()
167 this->classification_
= EXPRESSION_ERROR
;
170 // For children to call to report an error conveniently.
173 Expression::report_error(const char* msg
)
175 error_at(this->location_
, "%s", msg
);
176 this->set_is_error();
179 // Set types of variables and constants. This is implemented by the
183 Expression::determine_type(const Type_context
* context
)
185 this->do_determine_type(context
);
188 // Set types when there is no context.
191 Expression::determine_type_no_context()
193 Type_context context
;
194 this->do_determine_type(&context
);
197 // Return a tree handling any conversions which must be done during
201 Expression::convert_for_assignment(Translate_context
* context
, Type
* lhs_type
,
202 Type
* rhs_type
, tree rhs_tree
,
203 source_location location
)
205 if (lhs_type
== rhs_type
)
208 if (lhs_type
->is_error_type() || rhs_type
->is_error_type())
209 return error_mark_node
;
211 if (lhs_type
->is_undefined() || rhs_type
->is_undefined())
213 // Make sure we report the error.
216 return error_mark_node
;
219 if (rhs_tree
== error_mark_node
|| TREE_TYPE(rhs_tree
) == error_mark_node
)
220 return error_mark_node
;
222 Gogo
* gogo
= context
->gogo();
224 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
225 if (lhs_type_tree
== error_mark_node
)
226 return error_mark_node
;
228 if (lhs_type
->interface_type() != NULL
)
230 if (rhs_type
->interface_type() == NULL
)
231 return Expression::convert_type_to_interface(context
, lhs_type
,
235 return Expression::convert_interface_to_interface(context
, lhs_type
,
239 else if (rhs_type
->interface_type() != NULL
)
240 return Expression::convert_interface_to_type(context
, lhs_type
, rhs_type
,
242 else if (lhs_type
->is_open_array_type()
243 && rhs_type
->is_nil_type())
245 // Assigning nil to an open array.
246 gcc_assert(TREE_CODE(lhs_type_tree
) == RECORD_TYPE
);
248 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
250 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
251 tree field
= TYPE_FIELDS(lhs_type_tree
);
252 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
255 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
257 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
258 field
= DECL_CHAIN(field
);
259 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
262 elt
->value
= fold_convert(TREE_TYPE(field
), integer_zero_node
);
264 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
265 field
= DECL_CHAIN(field
);
266 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
269 elt
->value
= fold_convert(TREE_TYPE(field
), integer_zero_node
);
271 tree val
= build_constructor(lhs_type_tree
, init
);
272 TREE_CONSTANT(val
) = 1;
276 else if (rhs_type
->is_nil_type())
278 // The left hand side should be a pointer type at the tree
280 gcc_assert(POINTER_TYPE_P(lhs_type_tree
));
281 return fold_convert(lhs_type_tree
, null_pointer_node
);
283 else if (lhs_type_tree
== TREE_TYPE(rhs_tree
))
285 // No conversion is needed.
288 else if (POINTER_TYPE_P(lhs_type_tree
)
289 || INTEGRAL_TYPE_P(lhs_type_tree
)
290 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
291 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
292 return fold_convert_loc(location
, lhs_type_tree
, rhs_tree
);
293 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
294 && TREE_CODE(TREE_TYPE(rhs_tree
)) == RECORD_TYPE
)
296 // This conversion must be permitted by Go, or we wouldn't have
298 gcc_assert(int_size_in_bytes(lhs_type_tree
)
299 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
300 return fold_build1_loc(location
, VIEW_CONVERT_EXPR
, lhs_type_tree
,
305 gcc_assert(useless_type_conversion_p(lhs_type_tree
, TREE_TYPE(rhs_tree
)));
310 // Return a tree for a conversion from a non-interface type to an
314 Expression::convert_type_to_interface(Translate_context
* context
,
315 Type
* lhs_type
, Type
* rhs_type
,
316 tree rhs_tree
, source_location location
)
318 Gogo
* gogo
= context
->gogo();
319 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
320 bool lhs_is_empty
= lhs_interface_type
->is_empty();
322 // Since RHS_TYPE is a static type, we can create the interface
323 // method table at compile time.
325 // When setting an interface to nil, we just set both fields to
327 if (rhs_type
->is_nil_type())
328 return lhs_type
->get_init_tree(gogo
, false);
330 // This should have been checked already.
331 gcc_assert(lhs_interface_type
->implements_interface(rhs_type
, NULL
));
333 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
334 if (lhs_type_tree
== error_mark_node
)
335 return error_mark_node
;
337 // An interface is a tuple. If LHS_TYPE is an empty interface type,
338 // then the first field is the type descriptor for RHS_TYPE.
339 // Otherwise it is the interface method table for RHS_TYPE.
340 tree first_field_value
;
342 first_field_value
= rhs_type
->type_descriptor_pointer(gogo
);
345 // Build the interface method table for this interface and this
346 // object type: a list of function pointers for each interface
348 Named_type
* rhs_named_type
= rhs_type
->named_type();
349 bool is_pointer
= false;
350 if (rhs_named_type
== NULL
)
352 rhs_named_type
= rhs_type
->deref()->named_type();
356 if (rhs_named_type
== NULL
)
357 method_table
= null_pointer_node
;
360 rhs_named_type
->interface_method_table(gogo
, lhs_interface_type
,
362 first_field_value
= fold_convert_loc(location
, const_ptr_type_node
,
366 // Start building a constructor for the value we will return.
368 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
370 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
371 tree field
= TYPE_FIELDS(lhs_type_tree
);
372 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
373 (lhs_is_empty
? "__type_descriptor" : "__methods")) == 0);
375 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), first_field_value
);
377 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
378 field
= DECL_CHAIN(field
);
379 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
382 if (rhs_type
->points_to() != NULL
)
384 // We are assigning a pointer to the interface; the interface
385 // holds the pointer itself.
386 elt
->value
= rhs_tree
;
387 return build_constructor(lhs_type_tree
, init
);
390 // We are assigning a non-pointer value to the interface; the
391 // interface gets a copy of the value in the heap.
393 tree object_size
= TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree
));
395 tree space
= gogo
->allocate_memory(rhs_type
, object_size
, location
);
396 space
= fold_convert_loc(location
, build_pointer_type(TREE_TYPE(rhs_tree
)),
398 space
= save_expr(space
);
400 tree ref
= build_fold_indirect_ref_loc(location
, space
);
401 TREE_THIS_NOTRAP(ref
) = 1;
402 tree set
= fold_build2_loc(location
, MODIFY_EXPR
, void_type_node
,
405 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), space
);
407 return build2(COMPOUND_EXPR
, lhs_type_tree
, set
,
408 build_constructor(lhs_type_tree
, init
));
411 // Return a tree for the type descriptor of RHS_TREE, which has
412 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
416 Expression::get_interface_type_descriptor(Translate_context
*,
417 Type
* rhs_type
, tree rhs_tree
,
418 source_location location
)
420 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
421 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
422 tree rhs_field
= TYPE_FIELDS(rhs_type_tree
);
423 tree v
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
425 if (rhs_type
->interface_type()->is_empty())
427 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)),
428 "__type_descriptor") == 0);
432 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__methods")
434 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v
)));
436 tree v1
= build_fold_indirect_ref_loc(location
, v
);
437 gcc_assert(TREE_CODE(TREE_TYPE(v1
)) == RECORD_TYPE
);
438 tree f
= TYPE_FIELDS(TREE_TYPE(v1
));
439 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f
)), "__type_descriptor")
441 v1
= build3(COMPONENT_REF
, TREE_TYPE(f
), v1
, f
, NULL_TREE
);
443 tree eq
= fold_build2_loc(location
, EQ_EXPR
, boolean_type_node
, v
,
444 fold_convert_loc(location
, TREE_TYPE(v
),
446 tree n
= fold_convert_loc(location
, TREE_TYPE(v1
), null_pointer_node
);
447 return fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(v1
),
451 // Return a tree for the conversion of an interface type to an
455 Expression::convert_interface_to_interface(Translate_context
* context
,
456 Type
*lhs_type
, Type
*rhs_type
,
457 tree rhs_tree
, bool for_type_guard
,
458 source_location location
)
460 Gogo
* gogo
= context
->gogo();
461 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
462 bool lhs_is_empty
= lhs_interface_type
->is_empty();
464 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
465 if (lhs_type_tree
== error_mark_node
)
466 return error_mark_node
;
468 // In the general case this requires runtime examination of the type
469 // method table to match it up with the interface methods.
471 // FIXME: If all of the methods in the right hand side interface
472 // also appear in the left hand side interface, then we don't need
473 // to do a runtime check, although we still need to build a new
476 // Get the type descriptor for the right hand side. This will be
477 // NULL for a nil interface.
479 if (!DECL_P(rhs_tree
))
480 rhs_tree
= save_expr(rhs_tree
);
482 tree rhs_type_descriptor
=
483 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
486 // The result is going to be a two element constructor.
488 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
490 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
491 tree field
= TYPE_FIELDS(lhs_type_tree
);
496 // A type assertion fails when converting a nil interface.
497 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
498 static tree assert_interface_decl
;
499 tree call
= Gogo::call_builtin(&assert_interface_decl
,
501 "__go_assert_interface",
504 TREE_TYPE(lhs_type_descriptor
),
506 TREE_TYPE(rhs_type_descriptor
),
507 rhs_type_descriptor
);
508 if (call
== error_mark_node
)
509 return error_mark_node
;
510 // This will panic if the interface conversion fails.
511 TREE_NOTHROW(assert_interface_decl
) = 0;
512 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
514 else if (lhs_is_empty
)
516 // A convertion to an empty interface always succeeds, and the
517 // first field is just the type descriptor of the object.
518 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
519 "__type_descriptor") == 0);
520 gcc_assert(TREE_TYPE(field
) == TREE_TYPE(rhs_type_descriptor
));
521 elt
->value
= rhs_type_descriptor
;
525 // A conversion to a non-empty interface may fail, but unlike a
526 // type assertion converting nil will always succeed.
527 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods")
529 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
530 static tree convert_interface_decl
;
531 tree call
= Gogo::call_builtin(&convert_interface_decl
,
533 "__go_convert_interface",
536 TREE_TYPE(lhs_type_descriptor
),
538 TREE_TYPE(rhs_type_descriptor
),
539 rhs_type_descriptor
);
540 if (call
== error_mark_node
)
541 return error_mark_node
;
542 // This will panic if the interface conversion fails.
543 TREE_NOTHROW(convert_interface_decl
) = 0;
544 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
547 // The second field is simply the object pointer.
549 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
550 field
= DECL_CHAIN(field
);
551 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
554 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
555 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
556 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
557 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
558 elt
->value
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
561 return build_constructor(lhs_type_tree
, init
);
564 // Return a tree for the conversion of an interface type to a
565 // non-interface type.
568 Expression::convert_interface_to_type(Translate_context
* context
,
569 Type
*lhs_type
, Type
* rhs_type
,
570 tree rhs_tree
, source_location location
)
572 Gogo
* gogo
= context
->gogo();
573 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
575 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
576 if (lhs_type_tree
== error_mark_node
)
577 return error_mark_node
;
579 // Call a function to check that the type is valid. The function
580 // will panic with an appropriate runtime type error if the type is
583 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
585 if (!DECL_P(rhs_tree
))
586 rhs_tree
= save_expr(rhs_tree
);
588 tree rhs_type_descriptor
=
589 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
592 tree rhs_inter_descriptor
= rhs_type
->type_descriptor_pointer(gogo
);
594 static tree check_interface_type_decl
;
595 tree call
= Gogo::call_builtin(&check_interface_type_decl
,
597 "__go_check_interface_type",
600 TREE_TYPE(lhs_type_descriptor
),
602 TREE_TYPE(rhs_type_descriptor
),
604 TREE_TYPE(rhs_inter_descriptor
),
605 rhs_inter_descriptor
);
606 if (call
== error_mark_node
)
607 return error_mark_node
;
608 // This call will panic if the conversion is invalid.
609 TREE_NOTHROW(check_interface_type_decl
) = 0;
611 // If the call succeeds, pull out the value.
612 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
613 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
614 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
615 tree val
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
618 // If the value is a pointer, then it is the value we want.
619 // Otherwise it points to the value.
620 if (lhs_type
->points_to() == NULL
)
622 val
= fold_convert_loc(location
, build_pointer_type(lhs_type_tree
), val
);
623 val
= build_fold_indirect_ref_loc(location
, val
);
626 return build2(COMPOUND_EXPR
, lhs_type_tree
, call
,
627 fold_convert_loc(location
, lhs_type_tree
, val
));
630 // Convert an expression to a tree. This is implemented by the child
631 // class. Not that it is not in general safe to call this multiple
632 // times for a single expression, but that we don't catch such errors.
635 Expression::get_tree(Translate_context
* context
)
637 // The child may have marked this expression as having an error.
638 if (this->classification_
== EXPRESSION_ERROR
)
639 return error_mark_node
;
641 return this->do_get_tree(context
);
644 // Return a tree for VAL in TYPE.
647 Expression::integer_constant_tree(mpz_t val
, tree type
)
649 if (type
== error_mark_node
)
650 return error_mark_node
;
651 else if (TREE_CODE(type
) == INTEGER_TYPE
)
652 return double_int_to_tree(type
,
653 mpz_get_double_int(type
, val
, true));
654 else if (TREE_CODE(type
) == REAL_TYPE
)
657 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
658 tree ret
= Expression::float_constant_tree(fval
, type
);
662 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
665 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
666 tree real
= Expression::float_constant_tree(fval
, TREE_TYPE(type
));
668 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
670 return build_complex(type
, real
, imag
);
676 // Return a tree for VAL in TYPE.
679 Expression::float_constant_tree(mpfr_t val
, tree type
)
681 if (type
== error_mark_node
)
682 return error_mark_node
;
683 else if (TREE_CODE(type
) == INTEGER_TYPE
)
687 mpfr_get_z(ival
, val
, GMP_RNDN
);
688 tree ret
= Expression::integer_constant_tree(ival
, type
);
692 else if (TREE_CODE(type
) == REAL_TYPE
)
695 real_from_mpfr(&r1
, val
, type
, GMP_RNDN
);
697 real_convert(&r2
, TYPE_MODE(type
), &r1
);
698 return build_real(type
, r2
);
700 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
703 real_from_mpfr(&r1
, val
, TREE_TYPE(type
), GMP_RNDN
);
705 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
706 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
708 return build_complex(type
, build_real(TREE_TYPE(type
), r2
), imag
);
714 // Return a tree for REAL/IMAG in TYPE.
717 Expression::complex_constant_tree(mpfr_t real
, mpfr_t imag
, tree type
)
719 if (TREE_CODE(type
) == COMPLEX_TYPE
)
722 real_from_mpfr(&r1
, real
, TREE_TYPE(type
), GMP_RNDN
);
724 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
727 real_from_mpfr(&r3
, imag
, TREE_TYPE(type
), GMP_RNDN
);
729 real_convert(&r4
, TYPE_MODE(TREE_TYPE(type
)), &r3
);
731 return build_complex(type
, build_real(TREE_TYPE(type
), r2
),
732 build_real(TREE_TYPE(type
), r4
));
738 // Return a tree which evaluates to true if VAL, of arbitrary integer
739 // type, is negative or is more than the maximum value of BOUND_TYPE.
740 // If SOFAR is not NULL, it is or'red into the result. The return
741 // value may be NULL if SOFAR is NULL.
744 Expression::check_bounds(tree val
, tree bound_type
, tree sofar
,
747 tree val_type
= TREE_TYPE(val
);
748 tree ret
= NULL_TREE
;
750 if (!TYPE_UNSIGNED(val_type
))
752 ret
= fold_build2_loc(loc
, LT_EXPR
, boolean_type_node
, val
,
753 build_int_cst(val_type
, 0));
754 if (ret
== boolean_false_node
)
758 if ((TYPE_UNSIGNED(val_type
) && !TYPE_UNSIGNED(bound_type
))
759 || TYPE_SIZE(val_type
) > TYPE_SIZE(bound_type
))
761 tree max
= TYPE_MAX_VALUE(bound_type
);
762 tree big
= fold_build2_loc(loc
, GT_EXPR
, boolean_type_node
, val
,
763 fold_convert_loc(loc
, val_type
, max
));
764 if (big
== boolean_false_node
)
766 else if (ret
== NULL_TREE
)
769 ret
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
773 if (ret
== NULL_TREE
)
775 else if (sofar
== NULL_TREE
)
778 return fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
782 // Error expressions. This are used to avoid cascading errors.
784 class Error_expression
: public Expression
787 Error_expression(source_location location
)
788 : Expression(EXPRESSION_ERROR
, location
)
793 do_is_constant() const
797 do_integer_constant_value(bool, mpz_t val
, Type
**) const
804 do_float_constant_value(mpfr_t val
, Type
**) const
806 mpfr_set_ui(val
, 0, GMP_RNDN
);
811 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const
813 mpfr_set_ui(real
, 0, GMP_RNDN
);
814 mpfr_set_ui(imag
, 0, GMP_RNDN
);
819 do_discarding_value()
824 { return Type::make_error_type(); }
827 do_determine_type(const Type_context
*)
835 do_is_addressable() const
839 do_get_tree(Translate_context
*)
840 { return error_mark_node
; }
844 Expression::make_error(source_location location
)
846 return new Error_expression(location
);
849 // An expression which is really a type. This is used during parsing.
850 // It is an error if these survive after lowering.
853 Type_expression
: public Expression
856 Type_expression(Type
* type
, source_location location
)
857 : Expression(EXPRESSION_TYPE
, location
),
863 do_traverse(Traverse
* traverse
)
864 { return Type::traverse(this->type_
, traverse
); }
868 { return this->type_
; }
871 do_determine_type(const Type_context
*)
875 do_check_types(Gogo
*)
876 { this->report_error(_("invalid use of type")); }
883 do_get_tree(Translate_context
*)
884 { gcc_unreachable(); }
887 // The type which we are representing as an expression.
892 Expression::make_type(Type
* type
, source_location location
)
894 return new Type_expression(type
, location
);
897 // Class Parser_expression.
900 Parser_expression::do_type()
902 // We should never really ask for the type of a Parser_expression.
903 // However, it can happen, at least when we have an invalid const
904 // whose initializer refers to the const itself. In that case we
905 // may ask for the type when lowering the const itself.
906 gcc_assert(saw_errors());
907 return Type::make_error_type();
910 // Class Var_expression.
912 // Lower a variable expression. Here we just make sure that the
913 // initialization expression of the variable has been lowered. This
914 // ensures that we will be able to determine the type of the variable
918 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
920 if (this->variable_
->is_variable())
922 Variable
* var
= this->variable_
->var_value();
923 // This is either a local variable or a global variable. A
924 // reference to a variable which is local to an enclosing
925 // function will be a reference to a field in a closure.
926 if (var
->is_global())
928 var
->lower_init_expression(gogo
, function
);
933 // Return the name of the variable.
936 Var_expression::name() const
938 return this->variable_
->name();
941 // Return the type of a reference to a variable.
944 Var_expression::do_type()
946 if (this->variable_
->is_variable())
947 return this->variable_
->var_value()->type();
948 else if (this->variable_
->is_result_variable())
949 return this->variable_
->result_var_value()->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 return this->variable_
->get_tree(context
->gogo(), context
->function());
978 // Make a reference to a variable in an expression.
981 Expression::make_var_reference(Named_object
* var
, source_location location
)
984 return Expression::make_sink(location
);
986 // FIXME: Creating a new object for each reference to a variable is
988 return new Var_expression(var
, location
);
991 // Class Temporary_reference_expression.
996 Temporary_reference_expression::do_type()
998 return this->statement_
->type();
1001 // Called if something takes the address of this temporary variable.
1002 // We never have to move temporary variables to the heap, but we do
1003 // need to know that they must live in the stack rather than in a
1007 Temporary_reference_expression::do_address_taken(bool)
1009 this->statement_
->set_is_address_taken();
1012 // Get a tree referring to the variable.
1015 Temporary_reference_expression::do_get_tree(Translate_context
*)
1017 return this->statement_
->get_decl();
1020 // Make a reference to a temporary variable.
1023 Expression::make_temporary_reference(Temporary_statement
* statement
,
1024 source_location location
)
1026 return new Temporary_reference_expression(statement
, location
);
1029 // A sink expression--a use of the blank identifier _.
1031 class Sink_expression
: public Expression
1034 Sink_expression(source_location location
)
1035 : Expression(EXPRESSION_SINK
, location
),
1036 type_(NULL
), var_(NULL_TREE
)
1041 do_discarding_value()
1048 do_determine_type(const Type_context
*);
1052 { return new Sink_expression(this->location()); }
1055 do_get_tree(Translate_context
*);
1058 // The type of this sink variable.
1060 // The temporary variable we generate.
1064 // Return the type of a sink expression.
1067 Sink_expression::do_type()
1069 if (this->type_
== NULL
)
1070 return Type::make_sink_type();
1074 // Determine the type of a sink expression.
1077 Sink_expression::do_determine_type(const Type_context
* context
)
1079 if (context
->type
!= NULL
)
1080 this->type_
= context
->type
;
1083 // Return a temporary variable for a sink expression. This will
1084 // presumably be a write-only variable which the middle-end will drop.
1087 Sink_expression::do_get_tree(Translate_context
* context
)
1089 if (this->var_
== NULL_TREE
)
1091 gcc_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
1092 this->var_
= create_tmp_var(this->type_
->get_tree(context
->gogo()),
1098 // Make a sink expression.
1101 Expression::make_sink(source_location location
)
1103 return new Sink_expression(location
);
1106 // Class Func_expression.
1108 // FIXME: Can a function expression appear in a constant expression?
1109 // The value is unchanging. Initializing a constant to the address of
1110 // a function seems like it could work, though there might be little
1113 // Return the name of the function.
1116 Func_expression::name() const
1118 return this->function_
->name();
1124 Func_expression::do_traverse(Traverse
* traverse
)
1126 return (this->closure_
== NULL
1128 : Expression::traverse(&this->closure_
, traverse
));
1131 // Return the type of a function expression.
1134 Func_expression::do_type()
1136 if (this->function_
->is_function())
1137 return this->function_
->func_value()->type();
1138 else if (this->function_
->is_function_declaration())
1139 return this->function_
->func_declaration_value()->type();
1144 // Get the tree for a function expression without evaluating the
1148 Func_expression::get_tree_without_closure(Gogo
* gogo
)
1150 Function_type
* fntype
;
1151 if (this->function_
->is_function())
1152 fntype
= this->function_
->func_value()->type();
1153 else if (this->function_
->is_function_declaration())
1154 fntype
= this->function_
->func_declaration_value()->type();
1158 // Builtin functions are handled specially by Call_expression. We
1159 // can't take their address.
1160 if (fntype
->is_builtin())
1162 error_at(this->location(), "invalid use of special builtin function %qs",
1163 this->function_
->name().c_str());
1164 return error_mark_node
;
1167 Named_object
* no
= this->function_
;
1169 tree id
= no
->get_id(gogo
);
1170 if (id
== error_mark_node
)
1171 return error_mark_node
;
1174 if (no
->is_function())
1175 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
, id
);
1176 else if (no
->is_function_declaration())
1177 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
, id
);
1181 if (fndecl
== error_mark_node
)
1182 return error_mark_node
;
1184 return build_fold_addr_expr_loc(this->location(), fndecl
);
1187 // Get the tree for a function expression. This is used when we take
1188 // the address of a function rather than simply calling it. If the
1189 // function has a closure, we must use a trampoline.
1192 Func_expression::do_get_tree(Translate_context
* context
)
1194 Gogo
* gogo
= context
->gogo();
1196 tree fnaddr
= this->get_tree_without_closure(gogo
);
1197 if (fnaddr
== error_mark_node
)
1198 return error_mark_node
;
1200 gcc_assert(TREE_CODE(fnaddr
) == ADDR_EXPR
1201 && TREE_CODE(TREE_OPERAND(fnaddr
, 0)) == FUNCTION_DECL
);
1202 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr
, 0)) = 1;
1204 // For a normal non-nested function call, that is all we have to do.
1205 if (!this->function_
->is_function()
1206 || this->function_
->func_value()->enclosing() == NULL
)
1208 gcc_assert(this->closure_
== NULL
);
1212 // For a nested function call, we have to always allocate a
1213 // trampoline. If we don't always allocate, then closures will not
1214 // be reliably distinct.
1215 Expression
* closure
= this->closure_
;
1217 if (closure
== NULL
)
1218 closure_tree
= null_pointer_node
;
1221 // Get the value of the closure. This will be a pointer to
1222 // space allocated on the heap.
1223 closure_tree
= closure
->get_tree(context
);
1224 if (closure_tree
== error_mark_node
)
1225 return error_mark_node
;
1226 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree
)));
1229 // Now we need to build some code on the heap. This code will load
1230 // the static chain pointer with the closure and then jump to the
1231 // body of the function. The normal gcc approach is to build the
1232 // code on the stack. Unfortunately we can not do that, as Go
1233 // permits us to return the function pointer.
1235 return gogo
->make_trampoline(fnaddr
, closure_tree
, this->location());
1238 // Make a reference to a function in an expression.
1241 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1242 source_location location
)
1244 return new Func_expression(function
, closure
, location
);
1247 // Class Unknown_expression.
1249 // Return the name of an unknown expression.
1252 Unknown_expression::name() const
1254 return this->named_object_
->name();
1257 // Lower a reference to an unknown name.
1260 Unknown_expression::do_lower(Gogo
*, Named_object
*, int)
1262 source_location location
= this->location();
1263 Named_object
* no
= this->named_object_
;
1265 if (!no
->is_unknown())
1269 real
= no
->unknown_value()->real_named_object();
1272 if (this->is_composite_literal_key_
)
1274 error_at(location
, "reference to undefined name %qs",
1275 this->named_object_
->message_name().c_str());
1276 return Expression::make_error(location
);
1279 switch (real
->classification())
1281 case Named_object::NAMED_OBJECT_CONST
:
1282 return Expression::make_const_reference(real
, location
);
1283 case Named_object::NAMED_OBJECT_TYPE
:
1284 return Expression::make_type(real
->type_value(), location
);
1285 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1286 if (this->is_composite_literal_key_
)
1288 error_at(location
, "reference to undefined type %qs",
1289 real
->message_name().c_str());
1290 return Expression::make_error(location
);
1291 case Named_object::NAMED_OBJECT_VAR
:
1292 return Expression::make_var_reference(real
, location
);
1293 case Named_object::NAMED_OBJECT_FUNC
:
1294 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1295 return Expression::make_func_reference(real
, NULL
, location
);
1296 case Named_object::NAMED_OBJECT_PACKAGE
:
1297 if (this->is_composite_literal_key_
)
1299 error_at(location
, "unexpected reference to package");
1300 return Expression::make_error(location
);
1306 // Make a reference to an unknown name.
1309 Expression::make_unknown_reference(Named_object
* no
, source_location location
)
1311 gcc_assert(no
->resolve()->is_unknown());
1312 return new Unknown_expression(no
, location
);
1315 // A boolean expression.
1317 class Boolean_expression
: public Expression
1320 Boolean_expression(bool val
, source_location location
)
1321 : Expression(EXPRESSION_BOOLEAN
, location
),
1322 val_(val
), type_(NULL
)
1330 do_is_constant() const
1337 do_determine_type(const Type_context
*);
1344 do_get_tree(Translate_context
*)
1345 { return this->val_
? boolean_true_node
: boolean_false_node
; }
1348 do_export(Export
* exp
) const
1349 { exp
->write_c_string(this->val_
? "true" : "false"); }
1354 // The type as determined by context.
1361 Boolean_expression::do_type()
1363 if (this->type_
== NULL
)
1364 this->type_
= Type::make_boolean_type();
1368 // Set the type from the context.
1371 Boolean_expression::do_determine_type(const Type_context
* context
)
1373 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1375 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1376 this->type_
= context
->type
;
1377 else if (!context
->may_be_abstract
)
1378 this->type_
= Type::lookup_bool_type();
1381 // Import a boolean constant.
1384 Boolean_expression::do_import(Import
* imp
)
1386 if (imp
->peek_char() == 't')
1388 imp
->require_c_string("true");
1389 return Expression::make_boolean(true, imp
->location());
1393 imp
->require_c_string("false");
1394 return Expression::make_boolean(false, imp
->location());
1398 // Make a boolean expression.
1401 Expression::make_boolean(bool val
, source_location location
)
1403 return new Boolean_expression(val
, location
);
1406 // Class String_expression.
1411 String_expression::do_type()
1413 if (this->type_
== NULL
)
1414 this->type_
= Type::make_string_type();
1418 // Set the type from the context.
1421 String_expression::do_determine_type(const Type_context
* context
)
1423 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1425 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1426 this->type_
= context
->type
;
1427 else if (!context
->may_be_abstract
)
1428 this->type_
= Type::lookup_string_type();
1431 // Build a string constant.
1434 String_expression::do_get_tree(Translate_context
* context
)
1436 return context
->gogo()->go_string_constant_tree(this->val_
);
1439 // Export a string expression.
1442 String_expression::do_export(Export
* exp
) const
1445 s
.reserve(this->val_
.length() * 4 + 2);
1447 for (std::string::const_iterator p
= this->val_
.begin();
1448 p
!= this->val_
.end();
1451 if (*p
== '\\' || *p
== '"')
1456 else if (*p
>= 0x20 && *p
< 0x7f)
1458 else if (*p
== '\n')
1460 else if (*p
== '\t')
1465 unsigned char c
= *p
;
1466 unsigned int dig
= c
>> 4;
1467 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1469 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1473 exp
->write_string(s
);
1476 // Import a string expression.
1479 String_expression::do_import(Import
* imp
)
1481 imp
->require_c_string("\"");
1485 int c
= imp
->get_char();
1486 if (c
== '"' || c
== -1)
1489 val
+= static_cast<char>(c
);
1492 c
= imp
->get_char();
1493 if (c
== '\\' || c
== '"')
1494 val
+= static_cast<char>(c
);
1501 c
= imp
->get_char();
1502 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1503 c
= imp
->get_char();
1504 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1505 char v
= (vh
<< 4) | vl
;
1510 error_at(imp
->location(), "bad string constant");
1511 return Expression::make_error(imp
->location());
1515 return Expression::make_string(val
, imp
->location());
1518 // Make a string expression.
1521 Expression::make_string(const std::string
& val
, source_location location
)
1523 return new String_expression(val
, location
);
1526 // Make an integer expression.
1528 class Integer_expression
: public Expression
1531 Integer_expression(const mpz_t
* val
, Type
* type
, source_location location
)
1532 : Expression(EXPRESSION_INTEGER
, location
),
1534 { mpz_init_set(this->val_
, *val
); }
1539 // Return whether VAL fits in the type.
1541 check_constant(mpz_t val
, Type
*, source_location
);
1543 // Write VAL to export data.
1545 export_integer(Export
* exp
, const mpz_t val
);
1549 do_is_constant() const
1553 do_integer_constant_value(bool, mpz_t val
, Type
** ptype
) const;
1559 do_determine_type(const Type_context
* context
);
1562 do_check_types(Gogo
*);
1565 do_get_tree(Translate_context
*);
1569 { return Expression::make_integer(&this->val_
, this->type_
,
1570 this->location()); }
1573 do_export(Export
*) const;
1576 // The integer value.
1582 // Return an integer constant value.
1585 Integer_expression::do_integer_constant_value(bool, mpz_t val
,
1588 if (this->type_
!= NULL
)
1589 *ptype
= this->type_
;
1590 mpz_set(val
, this->val_
);
1594 // Return the current type. If we haven't set the type yet, we return
1595 // an abstract integer type.
1598 Integer_expression::do_type()
1600 if (this->type_
== NULL
)
1601 this->type_
= Type::make_abstract_integer_type();
1605 // Set the type of the integer value. Here we may switch from an
1606 // abstract type to a real type.
1609 Integer_expression::do_determine_type(const Type_context
* context
)
1611 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1613 else if (context
->type
!= NULL
1614 && (context
->type
->integer_type() != NULL
1615 || context
->type
->float_type() != NULL
1616 || context
->type
->complex_type() != NULL
))
1617 this->type_
= context
->type
;
1618 else if (!context
->may_be_abstract
)
1619 this->type_
= Type::lookup_integer_type("int");
1622 // Return true if the integer VAL fits in the range of the type TYPE.
1623 // Otherwise give an error and return false. TYPE may be NULL.
1626 Integer_expression::check_constant(mpz_t val
, Type
* type
,
1627 source_location location
)
1631 Integer_type
* itype
= type
->integer_type();
1632 if (itype
== NULL
|| itype
->is_abstract())
1635 int bits
= mpz_sizeinbase(val
, 2);
1637 if (itype
->is_unsigned())
1639 // For an unsigned type we can only accept a nonnegative number,
1640 // and we must be able to represent at least BITS.
1641 if (mpz_sgn(val
) >= 0
1642 && bits
<= itype
->bits())
1647 // For a signed type we need an extra bit to indicate the sign.
1648 // We have to handle the most negative integer specially.
1649 if (bits
+ 1 <= itype
->bits()
1650 || (bits
<= itype
->bits()
1652 && (mpz_scan1(val
, 0)
1653 == static_cast<unsigned long>(itype
->bits() - 1))
1654 && mpz_scan0(val
, itype
->bits()) == ULONG_MAX
))
1658 error_at(location
, "integer constant overflow");
1662 // Check the type of an integer constant.
1665 Integer_expression::do_check_types(Gogo
*)
1667 if (this->type_
== NULL
)
1669 if (!Integer_expression::check_constant(this->val_
, this->type_
,
1671 this->set_is_error();
1674 // Get a tree for an integer constant.
1677 Integer_expression::do_get_tree(Translate_context
* context
)
1679 Gogo
* gogo
= context
->gogo();
1681 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1682 type
= this->type_
->get_tree(gogo
);
1683 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
1685 // We are converting to an abstract floating point type.
1686 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
1688 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
1690 // We are converting to an abstract complex type.
1691 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
1695 // If we still have an abstract type here, then this is being
1696 // used in a constant expression which didn't get reduced for
1697 // some reason. Use a type which will fit the value. We use <,
1698 // not <=, because we need an extra bit for the sign bit.
1699 int bits
= mpz_sizeinbase(this->val_
, 2);
1700 if (bits
< INT_TYPE_SIZE
)
1701 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
1703 type
= Type::lookup_integer_type("int64")->get_tree(gogo
);
1705 type
= long_long_integer_type_node
;
1707 return Expression::integer_constant_tree(this->val_
, type
);
1710 // Write VAL to export data.
1713 Integer_expression::export_integer(Export
* exp
, const mpz_t val
)
1715 char* s
= mpz_get_str(NULL
, 10, val
);
1716 exp
->write_c_string(s
);
1720 // Export an integer in a constant expression.
1723 Integer_expression::do_export(Export
* exp
) const
1725 Integer_expression::export_integer(exp
, this->val_
);
1726 // A trailing space lets us reliably identify the end of the number.
1727 exp
->write_c_string(" ");
1730 // Import an integer, floating point, or complex value. This handles
1731 // all these types because they all start with digits.
1734 Integer_expression::do_import(Import
* imp
)
1736 std::string num
= imp
->read_identifier();
1737 imp
->require_c_string(" ");
1738 if (!num
.empty() && num
[num
.length() - 1] == 'i')
1741 size_t plus_pos
= num
.find('+', 1);
1742 size_t minus_pos
= num
.find('-', 1);
1744 if (plus_pos
== std::string::npos
)
1746 else if (minus_pos
== std::string::npos
)
1750 error_at(imp
->location(), "bad number in import data: %qs",
1752 return Expression::make_error(imp
->location());
1754 if (pos
== std::string::npos
)
1755 mpfr_set_ui(real
, 0, GMP_RNDN
);
1758 std::string real_str
= num
.substr(0, pos
);
1759 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
1761 error_at(imp
->location(), "bad number in import data: %qs",
1763 return Expression::make_error(imp
->location());
1767 std::string imag_str
;
1768 if (pos
== std::string::npos
)
1771 imag_str
= num
.substr(pos
);
1772 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
1774 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
1776 error_at(imp
->location(), "bad number in import data: %qs",
1778 return Expression::make_error(imp
->location());
1780 Expression
* ret
= Expression::make_complex(&real
, &imag
, NULL
,
1786 else if (num
.find('.') == std::string::npos
1787 && num
.find('E') == std::string::npos
)
1790 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
1792 error_at(imp
->location(), "bad number in import data: %qs",
1794 return Expression::make_error(imp
->location());
1796 Expression
* ret
= Expression::make_integer(&val
, NULL
, imp
->location());
1803 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
1805 error_at(imp
->location(), "bad number in import data: %qs",
1807 return Expression::make_error(imp
->location());
1809 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
1815 // Build a new integer value.
1818 Expression::make_integer(const mpz_t
* val
, Type
* type
,
1819 source_location location
)
1821 return new Integer_expression(val
, type
, location
);
1826 class Float_expression
: public Expression
1829 Float_expression(const mpfr_t
* val
, Type
* type
, source_location location
)
1830 : Expression(EXPRESSION_FLOAT
, location
),
1833 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
1836 // Constrain VAL to fit into TYPE.
1838 constrain_float(mpfr_t val
, Type
* type
);
1840 // Return whether VAL fits in the type.
1842 check_constant(mpfr_t val
, Type
*, source_location
);
1844 // Write VAL to export data.
1846 export_float(Export
* exp
, const mpfr_t val
);
1850 do_is_constant() const
1854 do_float_constant_value(mpfr_t val
, Type
**) const;
1860 do_determine_type(const Type_context
*);
1863 do_check_types(Gogo
*);
1867 { return Expression::make_float(&this->val_
, this->type_
,
1868 this->location()); }
1871 do_get_tree(Translate_context
*);
1874 do_export(Export
*) const;
1877 // The floating point value.
1883 // Constrain VAL to fit into TYPE.
1886 Float_expression::constrain_float(mpfr_t val
, Type
* type
)
1888 Float_type
* ftype
= type
->float_type();
1889 if (ftype
!= NULL
&& !ftype
->is_abstract())
1891 tree type_tree
= ftype
->type_tree();
1892 REAL_VALUE_TYPE rvt
;
1893 real_from_mpfr(&rvt
, val
, type_tree
, GMP_RNDN
);
1894 real_convert(&rvt
, TYPE_MODE(type_tree
), &rvt
);
1895 mpfr_from_real(val
, &rvt
, GMP_RNDN
);
1899 // Return a floating point constant value.
1902 Float_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
1904 if (this->type_
!= NULL
)
1905 *ptype
= this->type_
;
1906 mpfr_set(val
, this->val_
, GMP_RNDN
);
1910 // Return the current type. If we haven't set the type yet, we return
1911 // an abstract float type.
1914 Float_expression::do_type()
1916 if (this->type_
== NULL
)
1917 this->type_
= Type::make_abstract_float_type();
1921 // Set the type of the float value. Here we may switch from an
1922 // abstract type to a real type.
1925 Float_expression::do_determine_type(const Type_context
* context
)
1927 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1929 else if (context
->type
!= NULL
1930 && (context
->type
->integer_type() != NULL
1931 || context
->type
->float_type() != NULL
1932 || context
->type
->complex_type() != NULL
))
1933 this->type_
= context
->type
;
1934 else if (!context
->may_be_abstract
)
1935 this->type_
= Type::lookup_float_type("float64");
1938 // Return true if the floating point value VAL fits in the range of
1939 // the type TYPE. Otherwise give an error and return false. TYPE may
1943 Float_expression::check_constant(mpfr_t val
, Type
* type
,
1944 source_location location
)
1948 Float_type
* ftype
= type
->float_type();
1949 if (ftype
== NULL
|| ftype
->is_abstract())
1952 // A NaN or Infinity always fits in the range of the type.
1953 if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
1956 mp_exp_t exp
= mpfr_get_exp(val
);
1958 switch (ftype
->bits())
1971 error_at(location
, "floating point constant overflow");
1977 // Check the type of a float value.
1980 Float_expression::do_check_types(Gogo
*)
1982 if (this->type_
== NULL
)
1985 if (!Float_expression::check_constant(this->val_
, this->type_
,
1987 this->set_is_error();
1989 Integer_type
* integer_type
= this->type_
->integer_type();
1990 if (integer_type
!= NULL
)
1992 if (!mpfr_integer_p(this->val_
))
1993 this->report_error(_("floating point constant truncated to integer"));
1996 gcc_assert(!integer_type
->is_abstract());
1999 mpfr_get_z(ival
, this->val_
, GMP_RNDN
);
2000 Integer_expression::check_constant(ival
, integer_type
,
2007 // Get a tree for a float constant.
2010 Float_expression::do_get_tree(Translate_context
* context
)
2012 Gogo
* gogo
= context
->gogo();
2014 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2015 type
= this->type_
->get_tree(gogo
);
2016 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2018 // We have an abstract integer type. We just hope for the best.
2019 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
2023 // If we still have an abstract type here, then this is being
2024 // used in a constant expression which didn't get reduced. We
2025 // just use float64 and hope for the best.
2026 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
2028 return Expression::float_constant_tree(this->val_
, type
);
2031 // Write a floating point number to export data.
2034 Float_expression::export_float(Export
*exp
, const mpfr_t val
)
2037 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2039 exp
->write_c_string("-");
2040 exp
->write_c_string("0.");
2041 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2044 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2045 exp
->write_c_string(buf
);
2048 // Export a floating point number in a constant expression.
2051 Float_expression::do_export(Export
* exp
) const
2053 Float_expression::export_float(exp
, this->val_
);
2054 // A trailing space lets us reliably identify the end of the number.
2055 exp
->write_c_string(" ");
2058 // Make a float expression.
2061 Expression::make_float(const mpfr_t
* val
, Type
* type
, source_location location
)
2063 return new Float_expression(val
, type
, location
);
2068 class Complex_expression
: public Expression
2071 Complex_expression(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2072 source_location location
)
2073 : Expression(EXPRESSION_COMPLEX
, location
),
2076 mpfr_init_set(this->real_
, *real
, GMP_RNDN
);
2077 mpfr_init_set(this->imag_
, *imag
, GMP_RNDN
);
2080 // Constrain REAL/IMAG to fit into TYPE.
2082 constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
);
2084 // Return whether REAL/IMAG fits in the type.
2086 check_constant(mpfr_t real
, mpfr_t imag
, Type
*, source_location
);
2088 // Write REAL/IMAG to export data.
2090 export_complex(Export
* exp
, const mpfr_t real
, const mpfr_t val
);
2094 do_is_constant() const
2098 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2104 do_determine_type(const Type_context
*);
2107 do_check_types(Gogo
*);
2112 return Expression::make_complex(&this->real_
, &this->imag_
, this->type_
,
2117 do_get_tree(Translate_context
*);
2120 do_export(Export
*) const;
2125 // The imaginary part;
2127 // The type if known.
2131 // Constrain REAL/IMAG to fit into TYPE.
2134 Complex_expression::constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
)
2136 Complex_type
* ctype
= type
->complex_type();
2137 if (ctype
!= NULL
&& !ctype
->is_abstract())
2139 tree type_tree
= ctype
->type_tree();
2141 REAL_VALUE_TYPE rvt
;
2142 real_from_mpfr(&rvt
, real
, TREE_TYPE(type_tree
), GMP_RNDN
);
2143 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2144 mpfr_from_real(real
, &rvt
, GMP_RNDN
);
2146 real_from_mpfr(&rvt
, imag
, TREE_TYPE(type_tree
), GMP_RNDN
);
2147 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2148 mpfr_from_real(imag
, &rvt
, GMP_RNDN
);
2152 // Return a complex constant value.
2155 Complex_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2158 if (this->type_
!= NULL
)
2159 *ptype
= this->type_
;
2160 mpfr_set(real
, this->real_
, GMP_RNDN
);
2161 mpfr_set(imag
, this->imag_
, GMP_RNDN
);
2165 // Return the current type. If we haven't set the type yet, we return
2166 // an abstract complex type.
2169 Complex_expression::do_type()
2171 if (this->type_
== NULL
)
2172 this->type_
= Type::make_abstract_complex_type();
2176 // Set the type of the complex value. Here we may switch from an
2177 // abstract type to a real type.
2180 Complex_expression::do_determine_type(const Type_context
* context
)
2182 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2184 else if (context
->type
!= NULL
2185 && context
->type
->complex_type() != NULL
)
2186 this->type_
= context
->type
;
2187 else if (!context
->may_be_abstract
)
2188 this->type_
= Type::lookup_complex_type("complex128");
2191 // Return true if the complex value REAL/IMAG fits in the range of the
2192 // type TYPE. Otherwise give an error and return false. TYPE may be
2196 Complex_expression::check_constant(mpfr_t real
, mpfr_t imag
, Type
* type
,
2197 source_location location
)
2201 Complex_type
* ctype
= type
->complex_type();
2202 if (ctype
== NULL
|| ctype
->is_abstract())
2206 switch (ctype
->bits())
2218 // A NaN or Infinity always fits in the range of the type.
2219 if (!mpfr_nan_p(real
) && !mpfr_inf_p(real
) && !mpfr_zero_p(real
))
2221 if (mpfr_get_exp(real
) > max_exp
)
2223 error_at(location
, "complex real part constant overflow");
2228 if (!mpfr_nan_p(imag
) && !mpfr_inf_p(imag
) && !mpfr_zero_p(imag
))
2230 if (mpfr_get_exp(imag
) > max_exp
)
2232 error_at(location
, "complex imaginary part constant overflow");
2240 // Check the type of a complex value.
2243 Complex_expression::do_check_types(Gogo
*)
2245 if (this->type_
== NULL
)
2248 if (!Complex_expression::check_constant(this->real_
, this->imag_
,
2249 this->type_
, this->location()))
2250 this->set_is_error();
2253 // Get a tree for a complex constant.
2256 Complex_expression::do_get_tree(Translate_context
* context
)
2258 Gogo
* gogo
= context
->gogo();
2260 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2261 type
= this->type_
->get_tree(gogo
);
2264 // If we still have an abstract type here, this this is being
2265 // used in a constant expression which didn't get reduced. We
2266 // just use complex128 and hope for the best.
2267 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
2269 return Expression::complex_constant_tree(this->real_
, this->imag_
, type
);
2272 // Write REAL/IMAG to export data.
2275 Complex_expression::export_complex(Export
* exp
, const mpfr_t real
,
2278 if (!mpfr_zero_p(real
))
2280 Float_expression::export_float(exp
, real
);
2281 if (mpfr_sgn(imag
) > 0)
2282 exp
->write_c_string("+");
2284 Float_expression::export_float(exp
, imag
);
2285 exp
->write_c_string("i");
2288 // Export a complex number in a constant expression.
2291 Complex_expression::do_export(Export
* exp
) const
2293 Complex_expression::export_complex(exp
, this->real_
, this->imag_
);
2294 // A trailing space lets us reliably identify the end of the number.
2295 exp
->write_c_string(" ");
2298 // Make a complex expression.
2301 Expression::make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2302 source_location location
)
2304 return new Complex_expression(real
, imag
, type
, location
);
2307 // Find a named object in an expression.
2309 class Find_named_object
: public Traverse
2312 Find_named_object(Named_object
* no
)
2313 : Traverse(traverse_expressions
),
2314 no_(no
), found_(false)
2317 // Whether we found the object.
2320 { return this->found_
; }
2324 expression(Expression
**);
2327 // The object we are looking for.
2329 // Whether we found it.
2333 // A reference to a const in an expression.
2335 class Const_expression
: public Expression
2338 Const_expression(Named_object
* constant
, source_location location
)
2339 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2340 constant_(constant
), type_(NULL
), seen_(false)
2345 { return this->constant_
; }
2349 { return this->constant_
->name(); }
2351 // Check that the initializer does not refer to the constant itself.
2353 check_for_init_loop();
2357 do_lower(Gogo
*, Named_object
*, int);
2360 do_is_constant() const
2364 do_integer_constant_value(bool, mpz_t val
, Type
**) const;
2367 do_float_constant_value(mpfr_t val
, Type
**) const;
2370 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2373 do_string_constant_value(std::string
* val
) const
2374 { return this->constant_
->const_value()->expr()->string_constant_value(val
); }
2379 // The type of a const is set by the declaration, not the use.
2381 do_determine_type(const Type_context
*);
2384 do_check_types(Gogo
*);
2391 do_get_tree(Translate_context
* context
);
2393 // When exporting a reference to a const as part of a const
2394 // expression, we export the value. We ignore the fact that it has
2397 do_export(Export
* exp
) const
2398 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2402 Named_object
* constant_
;
2403 // The type of this reference. This is used if the constant has an
2406 // Used to prevent infinite recursion when a constant incorrectly
2407 // refers to itself.
2411 // Lower a constant expression. This is where we convert the
2412 // predeclared constant iota into an integer value.
2415 Const_expression::do_lower(Gogo
* gogo
, Named_object
*, int iota_value
)
2417 if (this->constant_
->const_value()->expr()->classification()
2420 if (iota_value
== -1)
2422 error_at(this->location(),
2423 "iota is only defined in const declarations");
2427 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2428 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2434 // Make sure that the constant itself has been lowered.
2435 gogo
->lower_constant(this->constant_
);
2440 // Return an integer constant value.
2443 Const_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
2450 if (this->type_
!= NULL
)
2451 ctype
= this->type_
;
2453 ctype
= this->constant_
->const_value()->type();
2454 if (ctype
!= NULL
&& ctype
->integer_type() == NULL
)
2457 Expression
* e
= this->constant_
->const_value()->expr();
2462 bool r
= e
->integer_constant_value(iota_is_constant
, val
, &t
);
2464 this->seen_
= false;
2468 && !Integer_expression::check_constant(val
, ctype
, this->location()))
2471 *ptype
= ctype
!= NULL
? ctype
: t
;
2475 // Return a floating point constant value.
2478 Const_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
2484 if (this->type_
!= NULL
)
2485 ctype
= this->type_
;
2487 ctype
= this->constant_
->const_value()->type();
2488 if (ctype
!= NULL
&& ctype
->float_type() == NULL
)
2494 bool r
= this->constant_
->const_value()->expr()->float_constant_value(val
,
2497 this->seen_
= false;
2499 if (r
&& ctype
!= NULL
)
2501 if (!Float_expression::check_constant(val
, ctype
, this->location()))
2503 Float_expression::constrain_float(val
, ctype
);
2505 *ptype
= ctype
!= NULL
? ctype
: t
;
2509 // Return a complex constant value.
2512 Const_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2519 if (this->type_
!= NULL
)
2520 ctype
= this->type_
;
2522 ctype
= this->constant_
->const_value()->type();
2523 if (ctype
!= NULL
&& ctype
->complex_type() == NULL
)
2529 bool r
= this->constant_
->const_value()->expr()->complex_constant_value(real
,
2533 this->seen_
= false;
2535 if (r
&& ctype
!= NULL
)
2537 if (!Complex_expression::check_constant(real
, imag
, ctype
,
2540 Complex_expression::constrain_complex(real
, imag
, ctype
);
2542 *ptype
= ctype
!= NULL
? ctype
: t
;
2546 // Return the type of the const reference.
2549 Const_expression::do_type()
2551 if (this->type_
!= NULL
)
2554 Named_constant
* nc
= this->constant_
->const_value();
2556 if (this->seen_
|| nc
->lowering())
2558 this->report_error(_("constant refers to itself"));
2559 this->type_
= Type::make_error_type();
2565 Type
* ret
= nc
->type();
2569 this->seen_
= false;
2573 // During parsing, a named constant may have a NULL type, but we
2574 // must not return a NULL type here.
2575 ret
= nc
->expr()->type();
2577 this->seen_
= false;
2582 // Set the type of the const reference.
2585 Const_expression::do_determine_type(const Type_context
* context
)
2587 Type
* ctype
= this->constant_
->const_value()->type();
2588 Type
* cetype
= (ctype
!= NULL
2590 : this->constant_
->const_value()->expr()->type());
2591 if (ctype
!= NULL
&& !ctype
->is_abstract())
2593 else if (context
->type
!= NULL
2594 && (context
->type
->integer_type() != NULL
2595 || context
->type
->float_type() != NULL
2596 || context
->type
->complex_type() != NULL
)
2597 && (cetype
->integer_type() != NULL
2598 || cetype
->float_type() != NULL
2599 || cetype
->complex_type() != NULL
))
2600 this->type_
= context
->type
;
2601 else if (context
->type
!= NULL
2602 && context
->type
->is_string_type()
2603 && cetype
->is_string_type())
2604 this->type_
= context
->type
;
2605 else if (context
->type
!= NULL
2606 && context
->type
->is_boolean_type()
2607 && cetype
->is_boolean_type())
2608 this->type_
= context
->type
;
2609 else if (!context
->may_be_abstract
)
2611 if (cetype
->is_abstract())
2612 cetype
= cetype
->make_non_abstract_type();
2613 this->type_
= cetype
;
2617 // Check for a loop in which the initializer of a constant refers to
2618 // the constant itself.
2621 Const_expression::check_for_init_loop()
2623 if (this->type_
!= NULL
&& this->type_
->is_error_type())
2628 this->report_error(_("constant refers to itself"));
2629 this->type_
= Type::make_error_type();
2633 Expression
* init
= this->constant_
->const_value()->expr();
2634 Find_named_object
find_named_object(this->constant_
);
2637 Expression::traverse(&init
, &find_named_object
);
2638 this->seen_
= false;
2640 if (find_named_object
.found())
2642 if (this->type_
== NULL
|| !this->type_
->is_error_type())
2644 this->report_error(_("constant refers to itself"));
2645 this->type_
= Type::make_error_type();
2651 // Check types of a const reference.
2654 Const_expression::do_check_types(Gogo
*)
2656 if (this->type_
!= NULL
&& this->type_
->is_error_type())
2659 this->check_for_init_loop();
2661 if (this->type_
== NULL
|| this->type_
->is_abstract())
2664 // Check for integer overflow.
2665 if (this->type_
->integer_type() != NULL
)
2670 if (!this->integer_constant_value(true, ival
, &dummy
))
2674 Expression
* cexpr
= this->constant_
->const_value()->expr();
2675 if (cexpr
->float_constant_value(fval
, &dummy
))
2677 if (!mpfr_integer_p(fval
))
2678 this->report_error(_("floating point constant "
2679 "truncated to integer"));
2682 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2683 Integer_expression::check_constant(ival
, this->type_
,
2693 // Return a tree for the const reference.
2696 Const_expression::do_get_tree(Translate_context
* context
)
2698 Gogo
* gogo
= context
->gogo();
2700 if (this->type_
== NULL
)
2701 type_tree
= NULL_TREE
;
2704 type_tree
= this->type_
->get_tree(gogo
);
2705 if (type_tree
== error_mark_node
)
2706 return error_mark_node
;
2709 // If the type has been set for this expression, but the underlying
2710 // object is an abstract int or float, we try to get the abstract
2711 // value. Otherwise we may lose something in the conversion.
2712 if (this->type_
!= NULL
2713 && (this->constant_
->const_value()->type() == NULL
2714 || this->constant_
->const_value()->type()->is_abstract()))
2716 Expression
* expr
= this->constant_
->const_value()->expr();
2720 if (expr
->integer_constant_value(true, ival
, &t
))
2722 tree ret
= Expression::integer_constant_tree(ival
, type_tree
);
2730 if (expr
->float_constant_value(fval
, &t
))
2732 tree ret
= Expression::float_constant_tree(fval
, type_tree
);
2739 if (expr
->complex_constant_value(fval
, imag
, &t
))
2741 tree ret
= Expression::complex_constant_tree(fval
, imag
, type_tree
);
2750 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2751 if (this->type_
== NULL
2752 || const_tree
== error_mark_node
2753 || TREE_TYPE(const_tree
) == error_mark_node
)
2757 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2758 ret
= fold_convert(type_tree
, const_tree
);
2759 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2760 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2761 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2762 ret
= fold(convert_to_real(type_tree
, const_tree
));
2763 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2764 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2770 // Make a reference to a constant in an expression.
2773 Expression::make_const_reference(Named_object
* constant
,
2774 source_location location
)
2776 return new Const_expression(constant
, location
);
2779 // Find a named object in an expression.
2782 Find_named_object::expression(Expression
** pexpr
)
2784 switch ((*pexpr
)->classification())
2786 case Expression::EXPRESSION_CONST_REFERENCE
:
2788 Const_expression
* ce
= static_cast<Const_expression
*>(*pexpr
);
2789 if (ce
->named_object() == this->no_
)
2792 // We need to check a constant initializer explicitly, as
2793 // loops here will not be caught by the loop checking for
2794 // variable initializers.
2795 ce
->check_for_init_loop();
2797 return TRAVERSE_CONTINUE
;
2800 case Expression::EXPRESSION_VAR_REFERENCE
:
2801 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
2803 return TRAVERSE_CONTINUE
;
2804 case Expression::EXPRESSION_FUNC_REFERENCE
:
2805 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
2807 return TRAVERSE_CONTINUE
;
2809 return TRAVERSE_CONTINUE
;
2811 this->found_
= true;
2812 return TRAVERSE_EXIT
;
2817 class Nil_expression
: public Expression
2820 Nil_expression(source_location location
)
2821 : Expression(EXPRESSION_NIL
, location
)
2829 do_is_constant() const
2834 { return Type::make_nil_type(); }
2837 do_determine_type(const Type_context
*)
2845 do_get_tree(Translate_context
*)
2846 { return null_pointer_node
; }
2849 do_export(Export
* exp
) const
2850 { exp
->write_c_string("nil"); }
2853 // Import a nil expression.
2856 Nil_expression::do_import(Import
* imp
)
2858 imp
->require_c_string("nil");
2859 return Expression::make_nil(imp
->location());
2862 // Make a nil expression.
2865 Expression::make_nil(source_location location
)
2867 return new Nil_expression(location
);
2870 // The value of the predeclared constant iota. This is little more
2871 // than a marker. This will be lowered to an integer in
2872 // Const_expression::do_lower, which is where we know the value that
2875 class Iota_expression
: public Parser_expression
2878 Iota_expression(source_location location
)
2879 : Parser_expression(EXPRESSION_IOTA
, location
)
2884 do_lower(Gogo
*, Named_object
*, int)
2885 { gcc_unreachable(); }
2887 // There should only ever be one of these.
2890 { gcc_unreachable(); }
2893 // Make an iota expression. This is only called for one case: the
2894 // value of the predeclared constant iota.
2897 Expression::make_iota()
2899 static Iota_expression
iota_expression(UNKNOWN_LOCATION
);
2900 return &iota_expression
;
2903 // A type conversion expression.
2905 class Type_conversion_expression
: public Expression
2908 Type_conversion_expression(Type
* type
, Expression
* expr
,
2909 source_location location
)
2910 : Expression(EXPRESSION_CONVERSION
, location
),
2911 type_(type
), expr_(expr
), may_convert_function_types_(false)
2914 // Return the type to which we are converting.
2917 { return this->type_
; }
2919 // Return the expression which we are converting.
2922 { return this->expr_
; }
2924 // Permit converting from one function type to another. This is
2925 // used internally for method expressions.
2927 set_may_convert_function_types()
2929 this->may_convert_function_types_
= true;
2932 // Import a type conversion expression.
2938 do_traverse(Traverse
* traverse
);
2941 do_lower(Gogo
*, Named_object
*, int);
2944 do_is_constant() const
2945 { return this->expr_
->is_constant(); }
2948 do_integer_constant_value(bool, mpz_t
, Type
**) const;
2951 do_float_constant_value(mpfr_t
, Type
**) const;
2954 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
2957 do_string_constant_value(std::string
*) const;
2961 { return this->type_
; }
2964 do_determine_type(const Type_context
*)
2966 Type_context
subcontext(this->type_
, false);
2967 this->expr_
->determine_type(&subcontext
);
2971 do_check_types(Gogo
*);
2976 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
2981 do_get_tree(Translate_context
* context
);
2984 do_export(Export
*) const;
2987 // The type to convert to.
2989 // The expression to convert.
2991 // True if this is permitted to convert function types. This is
2992 // used internally for method expressions.
2993 bool may_convert_function_types_
;
2999 Type_conversion_expression::do_traverse(Traverse
* traverse
)
3001 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
3002 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
3003 return TRAVERSE_EXIT
;
3004 return TRAVERSE_CONTINUE
;
3007 // Convert to a constant at lowering time.
3010 Type_conversion_expression::do_lower(Gogo
*, Named_object
*, int)
3012 Type
* type
= this->type_
;
3013 Expression
* val
= this->expr_
;
3014 source_location location
= this->location();
3016 if (type
->integer_type() != NULL
)
3021 if (val
->integer_constant_value(false, ival
, &dummy
))
3023 if (!Integer_expression::check_constant(ival
, type
, location
))
3024 mpz_set_ui(ival
, 0);
3025 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
3032 if (val
->float_constant_value(fval
, &dummy
))
3034 if (!mpfr_integer_p(fval
))
3037 "floating point constant truncated to integer");
3038 return Expression::make_error(location
);
3040 mpfr_get_z(ival
, fval
, GMP_RNDN
);
3041 if (!Integer_expression::check_constant(ival
, type
, location
))
3042 mpz_set_ui(ival
, 0);
3043 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
3052 if (type
->float_type() != NULL
)
3057 if (val
->float_constant_value(fval
, &dummy
))
3059 if (!Float_expression::check_constant(fval
, type
, location
))
3060 mpfr_set_ui(fval
, 0, GMP_RNDN
);
3061 Float_expression::constrain_float(fval
, type
);
3062 Expression
*ret
= Expression::make_float(&fval
, type
, location
);
3069 if (type
->complex_type() != NULL
)
3076 if (val
->complex_constant_value(real
, imag
, &dummy
))
3078 if (!Complex_expression::check_constant(real
, imag
, type
, location
))
3080 mpfr_set_ui(real
, 0, GMP_RNDN
);
3081 mpfr_set_ui(imag
, 0, GMP_RNDN
);
3083 Complex_expression::constrain_complex(real
, imag
, type
);
3084 Expression
* ret
= Expression::make_complex(&real
, &imag
, type
,
3094 if (type
->is_open_array_type() && type
->named_type() == NULL
)
3096 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3097 bool is_byte
= element_type
== Type::lookup_integer_type("uint8");
3098 bool is_int
= element_type
== Type::lookup_integer_type("int");
3099 if (is_byte
|| is_int
)
3102 if (val
->string_constant_value(&s
))
3104 Expression_list
* vals
= new Expression_list();
3107 for (std::string::const_iterator p
= s
.begin();
3112 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
3113 Expression
* v
= Expression::make_integer(&val
,
3122 const char *p
= s
.data();
3123 const char *pend
= s
.data() + s
.length();
3127 int adv
= Lex::fetch_char(p
, &c
);
3130 warning_at(this->location(), 0,
3131 "invalid UTF-8 encoding");
3136 mpz_init_set_ui(val
, c
);
3137 Expression
* v
= Expression::make_integer(&val
,
3145 return Expression::make_slice_composite_literal(type
, vals
,
3154 // Return the constant integer value if there is one.
3157 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant
,
3161 if (this->type_
->integer_type() == NULL
)
3167 if (this->expr_
->integer_constant_value(iota_is_constant
, ival
, &dummy
))
3169 if (!Integer_expression::check_constant(ival
, this->type_
,
3177 *ptype
= this->type_
;
3184 if (this->expr_
->float_constant_value(fval
, &dummy
))
3186 mpfr_get_z(val
, fval
, GMP_RNDN
);
3188 if (!Integer_expression::check_constant(val
, this->type_
,
3191 *ptype
= this->type_
;
3199 // Return the constant floating point value if there is one.
3202 Type_conversion_expression::do_float_constant_value(mpfr_t val
,
3205 if (this->type_
->float_type() == NULL
)
3211 if (this->expr_
->float_constant_value(fval
, &dummy
))
3213 if (!Float_expression::check_constant(fval
, this->type_
,
3219 mpfr_set(val
, fval
, GMP_RNDN
);
3221 Float_expression::constrain_float(val
, this->type_
);
3222 *ptype
= this->type_
;
3230 // Return the constant complex value if there is one.
3233 Type_conversion_expression::do_complex_constant_value(mpfr_t real
,
3237 if (this->type_
->complex_type() == NULL
)
3245 if (this->expr_
->complex_constant_value(rval
, ival
, &dummy
))
3247 if (!Complex_expression::check_constant(rval
, ival
, this->type_
,
3254 mpfr_set(real
, rval
, GMP_RNDN
);
3255 mpfr_set(imag
, ival
, GMP_RNDN
);
3258 Complex_expression::constrain_complex(real
, imag
, this->type_
);
3259 *ptype
= this->type_
;
3268 // Return the constant string value if there is one.
3271 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3273 if (this->type_
->is_string_type()
3274 && this->expr_
->type()->integer_type() != NULL
)
3279 if (this->expr_
->integer_constant_value(false, ival
, &dummy
))
3281 unsigned long ulval
= mpz_get_ui(ival
);
3282 if (mpz_cmp_ui(ival
, ulval
) == 0)
3284 Lex::append_char(ulval
, true, val
, this->location());
3292 // FIXME: Could handle conversion from const []int here.
3297 // Check that types are convertible.
3300 Type_conversion_expression::do_check_types(Gogo
*)
3302 Type
* type
= this->type_
;
3303 Type
* expr_type
= this->expr_
->type();
3306 if (type
->is_error_type()
3307 || type
->is_undefined()
3308 || expr_type
->is_error_type()
3309 || expr_type
->is_undefined())
3311 // Make sure we emit an error for an undefined type.
3314 this->set_is_error();
3318 if (this->may_convert_function_types_
3319 && type
->function_type() != NULL
3320 && expr_type
->function_type() != NULL
)
3323 if (Type::are_convertible(type
, expr_type
, &reason
))
3326 error_at(this->location(), "%s", reason
.c_str());
3327 this->set_is_error();
3330 // Get a tree for a type conversion.
3333 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3335 Gogo
* gogo
= context
->gogo();
3336 tree type_tree
= this->type_
->get_tree(gogo
);
3337 tree expr_tree
= this->expr_
->get_tree(context
);
3339 if (type_tree
== error_mark_node
3340 || expr_tree
== error_mark_node
3341 || TREE_TYPE(expr_tree
) == error_mark_node
)
3342 return error_mark_node
;
3344 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3345 return fold_convert(type_tree
, expr_tree
);
3347 Type
* type
= this->type_
;
3348 Type
* expr_type
= this->expr_
->type();
3350 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3351 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3352 expr_tree
, this->location());
3353 else if (type
->integer_type() != NULL
)
3355 if (expr_type
->integer_type() != NULL
3356 || expr_type
->float_type() != NULL
3357 || expr_type
->is_unsafe_pointer_type())
3358 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3362 else if (type
->float_type() != NULL
)
3364 if (expr_type
->integer_type() != NULL
3365 || expr_type
->float_type() != NULL
)
3366 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3370 else if (type
->complex_type() != NULL
)
3372 if (expr_type
->complex_type() != NULL
)
3373 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3377 else if (type
->is_string_type()
3378 && expr_type
->integer_type() != NULL
)
3380 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3381 if (host_integerp(expr_tree
, 0))
3383 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3385 Lex::append_char(intval
, true, &s
, this->location());
3386 Expression
* se
= Expression::make_string(s
, this->location());
3387 return se
->get_tree(context
);
3390 static tree int_to_string_fndecl
;
3391 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3393 "__go_int_to_string",
3397 fold_convert(integer_type_node
, expr_tree
));
3399 else if (type
->is_string_type()
3400 && (expr_type
->array_type() != NULL
3401 || (expr_type
->points_to() != NULL
3402 && expr_type
->points_to()->array_type() != NULL
)))
3404 Type
* t
= expr_type
;
3405 if (t
->points_to() != NULL
)
3408 expr_tree
= build_fold_indirect_ref(expr_tree
);
3410 if (!DECL_P(expr_tree
))
3411 expr_tree
= save_expr(expr_tree
);
3412 Array_type
* a
= t
->array_type();
3413 Type
* e
= a
->element_type()->forwarded();
3414 gcc_assert(e
->integer_type() != NULL
);
3415 tree valptr
= fold_convert(const_ptr_type_node
,
3416 a
->value_pointer_tree(gogo
, expr_tree
));
3417 tree len
= a
->length_tree(gogo
, expr_tree
);
3418 len
= fold_convert_loc(this->location(), size_type_node
, len
);
3419 if (e
->integer_type()->is_unsigned()
3420 && e
->integer_type()->bits() == 8)
3422 static tree byte_array_to_string_fndecl
;
3423 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3425 "__go_byte_array_to_string",
3428 const_ptr_type_node
,
3435 gcc_assert(e
== Type::lookup_integer_type("int"));
3436 static tree int_array_to_string_fndecl
;
3437 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3439 "__go_int_array_to_string",
3442 const_ptr_type_node
,
3448 else if (type
->is_open_array_type() && expr_type
->is_string_type())
3450 Type
* e
= type
->array_type()->element_type()->forwarded();
3451 gcc_assert(e
->integer_type() != NULL
);
3452 if (e
->integer_type()->is_unsigned()
3453 && e
->integer_type()->bits() == 8)
3455 static tree string_to_byte_array_fndecl
;
3456 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3458 "__go_string_to_byte_array",
3461 TREE_TYPE(expr_tree
),
3466 gcc_assert(e
== Type::lookup_integer_type("int"));
3467 static tree string_to_int_array_fndecl
;
3468 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3470 "__go_string_to_int_array",
3473 TREE_TYPE(expr_tree
),
3477 else if ((type
->is_unsafe_pointer_type()
3478 && expr_type
->points_to() != NULL
)
3479 || (expr_type
->is_unsafe_pointer_type()
3480 && type
->points_to() != NULL
))
3481 ret
= fold_convert(type_tree
, expr_tree
);
3482 else if (type
->is_unsafe_pointer_type()
3483 && expr_type
->integer_type() != NULL
)
3484 ret
= convert_to_pointer(type_tree
, expr_tree
);
3485 else if (this->may_convert_function_types_
3486 && type
->function_type() != NULL
3487 && expr_type
->function_type() != NULL
)
3488 ret
= fold_convert_loc(this->location(), type_tree
, expr_tree
);
3490 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3491 expr_tree
, this->location());
3496 // Output a type conversion in a constant expression.
3499 Type_conversion_expression::do_export(Export
* exp
) const
3501 exp
->write_c_string("convert(");
3502 exp
->write_type(this->type_
);
3503 exp
->write_c_string(", ");
3504 this->expr_
->export_expression(exp
);
3505 exp
->write_c_string(")");
3508 // Import a type conversion or a struct construction.
3511 Type_conversion_expression::do_import(Import
* imp
)
3513 imp
->require_c_string("convert(");
3514 Type
* type
= imp
->read_type();
3515 imp
->require_c_string(", ");
3516 Expression
* val
= Expression::import_expression(imp
);
3517 imp
->require_c_string(")");
3518 return Expression::make_cast(type
, val
, imp
->location());
3521 // Make a type cast expression.
3524 Expression::make_cast(Type
* type
, Expression
* val
, source_location location
)
3526 if (type
->is_error_type() || val
->is_error_expression())
3527 return Expression::make_error(location
);
3528 return new Type_conversion_expression(type
, val
, location
);
3531 // Unary expressions.
3533 class Unary_expression
: public Expression
3536 Unary_expression(Operator op
, Expression
* expr
, source_location location
)
3537 : Expression(EXPRESSION_UNARY
, location
),
3538 op_(op
), escapes_(true), expr_(expr
)
3541 // Return the operator.
3544 { return this->op_
; }
3546 // Return the operand.
3549 { return this->expr_
; }
3551 // Record that an address expression does not escape.
3553 set_does_not_escape()
3555 gcc_assert(this->op_
== OPERATOR_AND
);
3556 this->escapes_
= false;
3559 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3560 // could be done, false if not.
3562 eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3565 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3566 // could be done, false if not.
3568 eval_float(Operator op
, mpfr_t uval
, mpfr_t val
);
3570 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3571 // true if this could be done, false if not.
3573 eval_complex(Operator op
, mpfr_t ureal
, mpfr_t uimag
, mpfr_t real
,
3581 do_traverse(Traverse
* traverse
)
3582 { return Expression::traverse(&this->expr_
, traverse
); }
3585 do_lower(Gogo
*, Named_object
*, int);
3588 do_is_constant() const;
3591 do_integer_constant_value(bool, mpz_t
, Type
**) const;
3594 do_float_constant_value(mpfr_t
, Type
**) const;
3597 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
3603 do_determine_type(const Type_context
*);
3606 do_check_types(Gogo
*);
3611 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3616 do_is_addressable() const
3617 { return this->op_
== OPERATOR_MULT
; }
3620 do_get_tree(Translate_context
*);
3623 do_export(Export
*) const;
3626 // The unary operator to apply.
3628 // Normally true. False if this is an address expression which does
3629 // not escape the current function.
3635 // If we are taking the address of a composite literal, and the
3636 // contents are not constant, then we want to make a heap composite
3640 Unary_expression::do_lower(Gogo
*, Named_object
*, int)
3642 source_location loc
= this->location();
3643 Operator op
= this->op_
;
3644 Expression
* expr
= this->expr_
;
3646 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3647 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3649 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3650 // moving x to the heap. FIXME: Is it worth doing a real escape
3651 // analysis here? This case is found in math/unsafe.go and is
3652 // therefore worth special casing.
3653 if (op
== OPERATOR_MULT
)
3655 Expression
* e
= expr
;
3656 while (e
->classification() == EXPRESSION_CONVERSION
)
3658 Type_conversion_expression
* te
3659 = static_cast<Type_conversion_expression
*>(e
);
3663 if (e
->classification() == EXPRESSION_UNARY
)
3665 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3666 if (ue
->op_
== OPERATOR_AND
)
3673 ue
->set_does_not_escape();
3678 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
3679 || op
== OPERATOR_NOT
|| op
== OPERATOR_XOR
)
3681 Expression
* ret
= NULL
;
3686 if (expr
->integer_constant_value(false, eval
, &etype
))
3690 if (Unary_expression::eval_integer(op
, etype
, eval
, val
, loc
))
3691 ret
= Expression::make_integer(&val
, etype
, loc
);
3698 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
)
3703 if (expr
->float_constant_value(fval
, &ftype
))
3707 if (Unary_expression::eval_float(op
, fval
, val
))
3708 ret
= Expression::make_float(&val
, ftype
, loc
);
3719 if (expr
->complex_constant_value(fval
, ival
, &ftype
))
3725 if (Unary_expression::eval_complex(op
, fval
, ival
, real
, imag
))
3726 ret
= Expression::make_complex(&real
, &imag
, ftype
, loc
);
3740 // Return whether a unary expression is a constant.
3743 Unary_expression::do_is_constant() const
3745 if (this->op_
== OPERATOR_MULT
)
3747 // Indirecting through a pointer is only constant if the object
3748 // to which the expression points is constant, but we currently
3749 // have no way to determine that.
3752 else if (this->op_
== OPERATOR_AND
)
3754 // Taking the address of a variable is constant if it is a
3755 // global variable, not constant otherwise. In other cases
3756 // taking the address is probably not a constant.
3757 Var_expression
* ve
= this->expr_
->var_expression();
3760 Named_object
* no
= ve
->named_object();
3761 return no
->is_variable() && no
->var_value()->is_global();
3766 return this->expr_
->is_constant();
3769 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3770 // UVAL, if known; it may be NULL. Return true if this could be done,
3774 Unary_expression::eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3775 source_location location
)
3782 case OPERATOR_MINUS
:
3784 return Integer_expression::check_constant(val
, utype
, location
);
3786 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3790 || utype
->integer_type() == NULL
3791 || utype
->integer_type()->is_abstract())
3795 // The number of HOST_WIDE_INTs that it takes to represent
3797 size_t count
= ((mpz_sizeinbase(uval
, 2)
3798 + HOST_BITS_PER_WIDE_INT
3800 / HOST_BITS_PER_WIDE_INT
);
3802 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3803 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3806 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3807 gcc_assert(ecount
<= count
);
3809 // Trim down to the number of words required by the type.
3810 size_t obits
= utype
->integer_type()->bits();
3811 if (!utype
->integer_type()->is_unsigned())
3813 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3814 / HOST_BITS_PER_WIDE_INT
);
3815 gcc_assert(ocount
<= ocount
);
3817 for (size_t i
= 0; i
< ocount
; ++i
)
3820 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3822 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3825 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3829 return Integer_expression::check_constant(val
, utype
, location
);
3838 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3839 // could be done, false if not.
3842 Unary_expression::eval_float(Operator op
, mpfr_t uval
, mpfr_t val
)
3847 mpfr_set(val
, uval
, GMP_RNDN
);
3849 case OPERATOR_MINUS
:
3850 mpfr_neg(val
, uval
, GMP_RNDN
);
3862 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3863 // if this could be done, false if not.
3866 Unary_expression::eval_complex(Operator op
, mpfr_t rval
, mpfr_t ival
,
3867 mpfr_t real
, mpfr_t imag
)
3872 mpfr_set(real
, rval
, GMP_RNDN
);
3873 mpfr_set(imag
, ival
, GMP_RNDN
);
3875 case OPERATOR_MINUS
:
3876 mpfr_neg(real
, rval
, GMP_RNDN
);
3877 mpfr_neg(imag
, ival
, GMP_RNDN
);
3889 // Return the integral constant value of a unary expression, if it has one.
3892 Unary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
3898 if (!this->expr_
->integer_constant_value(iota_is_constant
, uval
, ptype
))
3901 ret
= Unary_expression::eval_integer(this->op_
, *ptype
, uval
, val
,
3907 // Return the floating point constant value of a unary expression, if
3911 Unary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
3916 if (!this->expr_
->float_constant_value(uval
, ptype
))
3919 ret
= Unary_expression::eval_float(this->op_
, uval
, val
);
3924 // Return the complex constant value of a unary expression, if it has
3928 Unary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
3936 if (!this->expr_
->complex_constant_value(rval
, ival
, ptype
))
3939 ret
= Unary_expression::eval_complex(this->op_
, rval
, ival
, real
, imag
);
3945 // Return the type of a unary expression.
3948 Unary_expression::do_type()
3953 case OPERATOR_MINUS
:
3956 return this->expr_
->type();
3959 return Type::make_pointer_type(this->expr_
->type());
3963 Type
* subtype
= this->expr_
->type();
3964 Type
* points_to
= subtype
->points_to();
3965 if (points_to
== NULL
)
3966 return Type::make_error_type();
3975 // Determine abstract types for a unary expression.
3978 Unary_expression::do_determine_type(const Type_context
* context
)
3983 case OPERATOR_MINUS
:
3986 this->expr_
->determine_type(context
);
3990 // Taking the address of something.
3992 Type
* subtype
= (context
->type
== NULL
3994 : context
->type
->points_to());
3995 Type_context
subcontext(subtype
, false);
3996 this->expr_
->determine_type(&subcontext
);
4001 // Indirecting through a pointer.
4003 Type
* subtype
= (context
->type
== NULL
4005 : Type::make_pointer_type(context
->type
));
4006 Type_context
subcontext(subtype
, false);
4007 this->expr_
->determine_type(&subcontext
);
4016 // Check types for a unary expression.
4019 Unary_expression::do_check_types(Gogo
*)
4021 Type
* type
= this->expr_
->type();
4022 if (type
->is_error_type())
4024 this->set_is_error();
4031 case OPERATOR_MINUS
:
4032 if (type
->integer_type() == NULL
4033 && type
->float_type() == NULL
4034 && type
->complex_type() == NULL
)
4035 this->report_error(_("expected numeric type"));
4040 if (type
->integer_type() == NULL
4041 && !type
->is_boolean_type())
4042 this->report_error(_("expected integer or boolean type"));
4046 if (!this->expr_
->is_addressable())
4047 this->report_error(_("invalid operand for unary %<&%>"));
4049 this->expr_
->address_taken(this->escapes_
);
4053 // Indirecting through a pointer.
4054 if (type
->points_to() == NULL
)
4055 this->report_error(_("expected pointer"));
4063 // Get a tree for a unary expression.
4066 Unary_expression::do_get_tree(Translate_context
* context
)
4068 tree expr
= this->expr_
->get_tree(context
);
4069 if (expr
== error_mark_node
)
4070 return error_mark_node
;
4072 source_location loc
= this->location();
4078 case OPERATOR_MINUS
:
4080 tree type
= TREE_TYPE(expr
);
4081 tree compute_type
= excess_precision_type(type
);
4082 if (compute_type
!= NULL_TREE
)
4083 expr
= ::convert(compute_type
, expr
);
4084 tree ret
= fold_build1_loc(loc
, NEGATE_EXPR
,
4085 (compute_type
!= NULL_TREE
4089 if (compute_type
!= NULL_TREE
)
4090 ret
= ::convert(type
, ret
);
4095 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
4096 return fold_build1_loc(loc
, TRUTH_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4098 return fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, expr
,
4099 build_int_cst(TREE_TYPE(expr
), 0));
4102 return fold_build1_loc(loc
, BIT_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4105 // We should not see a non-constant constructor here; cases
4106 // where we would see one should have been moved onto the heap
4107 // at parse time. Taking the address of a nonconstant
4108 // constructor will not do what the programmer expects.
4109 gcc_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
4110 gcc_assert(TREE_CODE(expr
) != ADDR_EXPR
);
4112 // Build a decl for a constant constructor.
4113 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
4115 tree decl
= build_decl(this->location(), VAR_DECL
,
4116 create_tmp_var_name("C"), TREE_TYPE(expr
));
4117 DECL_EXTERNAL(decl
) = 0;
4118 TREE_PUBLIC(decl
) = 0;
4119 TREE_READONLY(decl
) = 1;
4120 TREE_CONSTANT(decl
) = 1;
4121 TREE_STATIC(decl
) = 1;
4122 TREE_ADDRESSABLE(decl
) = 1;
4123 DECL_ARTIFICIAL(decl
) = 1;
4124 DECL_INITIAL(decl
) = expr
;
4125 rest_of_decl_compilation(decl
, 1, 0);
4129 return build_fold_addr_expr_loc(loc
, expr
);
4133 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
4135 // If we are dereferencing the pointer to a large struct, we
4136 // need to check for nil. We don't bother to check for small
4137 // structs because we expect the system to crash on a nil
4138 // pointer dereference.
4139 HOST_WIDE_INT s
= int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr
)));
4140 if (s
== -1 || s
>= 4096)
4143 expr
= save_expr(expr
);
4144 tree compare
= fold_build2_loc(loc
, EQ_EXPR
, boolean_type_node
,
4146 fold_convert(TREE_TYPE(expr
),
4147 null_pointer_node
));
4148 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4150 expr
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(expr
),
4151 build3(COND_EXPR
, void_type_node
,
4152 compare
, crash
, NULL_TREE
),
4156 // If the type of EXPR is a recursive pointer type, then we
4157 // need to insert a cast before indirecting.
4158 if (TREE_TYPE(TREE_TYPE(expr
)) == ptr_type_node
)
4160 Type
* pt
= this->expr_
->type()->points_to();
4161 tree ind
= pt
->get_tree(context
->gogo());
4162 expr
= fold_convert_loc(loc
, build_pointer_type(ind
), expr
);
4165 return build_fold_indirect_ref_loc(loc
, expr
);
4173 // Export a unary expression.
4176 Unary_expression::do_export(Export
* exp
) const
4181 exp
->write_c_string("+ ");
4183 case OPERATOR_MINUS
:
4184 exp
->write_c_string("- ");
4187 exp
->write_c_string("! ");
4190 exp
->write_c_string("^ ");
4197 this->expr_
->export_expression(exp
);
4200 // Import a unary expression.
4203 Unary_expression::do_import(Import
* imp
)
4206 switch (imp
->get_char())
4212 op
= OPERATOR_MINUS
;
4223 imp
->require_c_string(" ");
4224 Expression
* expr
= Expression::import_expression(imp
);
4225 return Expression::make_unary(op
, expr
, imp
->location());
4228 // Make a unary expression.
4231 Expression::make_unary(Operator op
, Expression
* expr
, source_location location
)
4233 return new Unary_expression(op
, expr
, location
);
4236 // If this is an indirection through a pointer, return the expression
4237 // being pointed through. Otherwise return this.
4242 if (this->classification_
== EXPRESSION_UNARY
)
4244 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4245 if (ue
->op() == OPERATOR_MULT
)
4246 return ue
->operand();
4251 // Class Binary_expression.
4256 Binary_expression::do_traverse(Traverse
* traverse
)
4258 int t
= Expression::traverse(&this->left_
, traverse
);
4259 if (t
== TRAVERSE_EXIT
)
4260 return TRAVERSE_EXIT
;
4261 return Expression::traverse(&this->right_
, traverse
);
4264 // Compare integer constants according to OP.
4267 Binary_expression::compare_integer(Operator op
, mpz_t left_val
,
4270 int i
= mpz_cmp(left_val
, right_val
);
4275 case OPERATOR_NOTEQ
:
4290 // Compare floating point constants according to OP.
4293 Binary_expression::compare_float(Operator op
, Type
* type
, mpfr_t left_val
,
4298 i
= mpfr_cmp(left_val
, right_val
);
4302 mpfr_init_set(lv
, left_val
, GMP_RNDN
);
4304 mpfr_init_set(rv
, right_val
, GMP_RNDN
);
4305 Float_expression::constrain_float(lv
, type
);
4306 Float_expression::constrain_float(rv
, type
);
4307 i
= mpfr_cmp(lv
, rv
);
4315 case OPERATOR_NOTEQ
:
4330 // Compare complex constants according to OP. Complex numbers may
4331 // only be compared for equality.
4334 Binary_expression::compare_complex(Operator op
, Type
* type
,
4335 mpfr_t left_real
, mpfr_t left_imag
,
4336 mpfr_t right_real
, mpfr_t right_imag
)
4340 is_equal
= (mpfr_cmp(left_real
, right_real
) == 0
4341 && mpfr_cmp(left_imag
, right_imag
) == 0);
4346 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4347 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4350 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4351 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4352 Complex_expression::constrain_complex(lr
, li
, type
);
4353 Complex_expression::constrain_complex(rr
, ri
, type
);
4354 is_equal
= mpfr_cmp(lr
, rr
) == 0 && mpfr_cmp(li
, ri
) == 0;
4364 case OPERATOR_NOTEQ
:
4371 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4372 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4373 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4374 // this could be done, false if not.
4377 Binary_expression::eval_integer(Operator op
, Type
* left_type
, mpz_t left_val
,
4378 Type
* right_type
, mpz_t right_val
,
4379 source_location location
, mpz_t val
)
4381 bool is_shift_op
= false;
4385 case OPERATOR_ANDAND
:
4387 case OPERATOR_NOTEQ
:
4392 // These return boolean values. We should probably handle them
4393 // anyhow in case a type conversion is used on the result.
4396 mpz_add(val
, left_val
, right_val
);
4398 case OPERATOR_MINUS
:
4399 mpz_sub(val
, left_val
, right_val
);
4402 mpz_ior(val
, left_val
, right_val
);
4405 mpz_xor(val
, left_val
, right_val
);
4408 mpz_mul(val
, left_val
, right_val
);
4411 if (mpz_sgn(right_val
) != 0)
4412 mpz_tdiv_q(val
, left_val
, right_val
);
4415 error_at(location
, "division by zero");
4421 if (mpz_sgn(right_val
) != 0)
4422 mpz_tdiv_r(val
, left_val
, right_val
);
4425 error_at(location
, "division by zero");
4430 case OPERATOR_LSHIFT
:
4432 unsigned long shift
= mpz_get_ui(right_val
);
4433 if (mpz_cmp_ui(right_val
, shift
) != 0)
4435 error_at(location
, "shift count overflow");
4439 mpz_mul_2exp(val
, left_val
, shift
);
4444 case OPERATOR_RSHIFT
:
4446 unsigned long shift
= mpz_get_ui(right_val
);
4447 if (mpz_cmp_ui(right_val
, shift
) != 0)
4449 error_at(location
, "shift count overflow");
4453 if (mpz_cmp_ui(left_val
, 0) >= 0)
4454 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4456 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4462 mpz_and(val
, left_val
, right_val
);
4464 case OPERATOR_BITCLEAR
:
4468 mpz_com(tval
, right_val
);
4469 mpz_and(val
, left_val
, tval
);
4477 Type
* type
= left_type
;
4482 else if (type
!= right_type
&& right_type
!= NULL
)
4484 if (type
->is_abstract())
4486 else if (!right_type
->is_abstract())
4488 // This look like a type error which should be diagnosed
4489 // elsewhere. Don't do anything here, to avoid an
4490 // unhelpful chain of error messages.
4496 if (type
!= NULL
&& !type
->is_abstract())
4498 // We have to check the operands too, as we have implicitly
4499 // coerced them to TYPE.
4500 if ((type
!= left_type
4501 && !Integer_expression::check_constant(left_val
, type
, location
))
4503 && type
!= right_type
4504 && !Integer_expression::check_constant(right_val
, type
,
4506 || !Integer_expression::check_constant(val
, type
, location
))
4513 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4514 // Return true if this could be done, false if not.
4517 Binary_expression::eval_float(Operator op
, Type
* left_type
, mpfr_t left_val
,
4518 Type
* right_type
, mpfr_t right_val
,
4519 mpfr_t val
, source_location location
)
4524 case OPERATOR_ANDAND
:
4526 case OPERATOR_NOTEQ
:
4531 // These return boolean values. We should probably handle them
4532 // anyhow in case a type conversion is used on the result.
4535 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4537 case OPERATOR_MINUS
:
4538 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4543 case OPERATOR_BITCLEAR
:
4546 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4549 if (mpfr_zero_p(right_val
))
4550 error_at(location
, "division by zero");
4551 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4555 case OPERATOR_LSHIFT
:
4556 case OPERATOR_RSHIFT
:
4562 Type
* type
= left_type
;
4565 else if (type
!= right_type
&& right_type
!= NULL
)
4567 if (type
->is_abstract())
4569 else if (!right_type
->is_abstract())
4571 // This looks like a type error which should be diagnosed
4572 // elsewhere. Don't do anything here, to avoid an unhelpful
4573 // chain of error messages.
4578 if (type
!= NULL
&& !type
->is_abstract())
4580 if ((type
!= left_type
4581 && !Float_expression::check_constant(left_val
, type
, location
))
4582 || (type
!= right_type
4583 && !Float_expression::check_constant(right_val
, type
,
4585 || !Float_expression::check_constant(val
, type
, location
))
4586 mpfr_set_ui(val
, 0, GMP_RNDN
);
4592 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4593 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4594 // could be done, false if not.
4597 Binary_expression::eval_complex(Operator op
, Type
* left_type
,
4598 mpfr_t left_real
, mpfr_t left_imag
,
4600 mpfr_t right_real
, mpfr_t right_imag
,
4601 mpfr_t real
, mpfr_t imag
,
4602 source_location location
)
4607 case OPERATOR_ANDAND
:
4609 case OPERATOR_NOTEQ
:
4614 // These return boolean values and must be handled differently.
4617 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4618 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4620 case OPERATOR_MINUS
:
4621 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4622 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4627 case OPERATOR_BITCLEAR
:
4631 // You might think that multiplying two complex numbers would
4632 // be simple, and you would be right, until you start to think
4633 // about getting the right answer for infinity. If one
4634 // operand here is infinity and the other is anything other
4635 // than zero or NaN, then we are going to wind up subtracting
4636 // two infinity values. That will give us a NaN, but the
4637 // correct answer is infinity.
4641 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4645 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4649 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4653 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4655 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4656 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4658 // If we get NaN on both sides, check whether it should really
4659 // be infinity. The rule is that if either side of the
4660 // complex number is infinity, then the whole value is
4661 // infinity, even if the other side is NaN. So the only case
4662 // we have to fix is the one in which both sides are NaN.
4663 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4664 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4665 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4667 bool is_infinity
= false;
4671 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4672 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4676 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4677 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4679 // If the left side is infinity, then the result is
4681 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4683 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4684 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4685 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4686 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4689 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4690 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4694 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4695 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4700 // If the right side is infinity, then the result is
4702 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4704 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4705 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4706 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4707 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4710 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4711 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4715 mpfr_set_ui(li
, 0, GMP_RNDN
);
4716 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4721 // If we got an overflow in the intermediate computations,
4722 // then the result is infinity.
4724 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4725 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4729 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4730 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4734 mpfr_set_ui(li
, 0, GMP_RNDN
);
4735 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4739 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4740 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4744 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4745 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4752 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4753 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4754 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4755 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4756 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4757 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4758 mpfr_set_inf(real
, mpfr_sgn(real
));
4759 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4776 // For complex division we want to avoid having an
4777 // intermediate overflow turn the whole result in a NaN. We
4778 // scale the values to try to avoid this.
4780 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4781 error_at(location
, "division by zero");
4787 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4788 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4791 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4795 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4796 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4798 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4800 ilogbw
= mpfr_get_exp(t
);
4801 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4802 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4807 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4808 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4809 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4811 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4812 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4813 mpfr_add(real
, real
, t
, GMP_RNDN
);
4814 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4815 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4817 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4818 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4819 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4820 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4821 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4823 // If we wind up with NaN on both sides, check whether we
4824 // should really have infinity. The rule is that if either
4825 // side of the complex number is infinity, then the whole
4826 // value is infinity, even if the other side is NaN. So the
4827 // only case we have to fix is the one in which both sides are
4829 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4830 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4831 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4833 if (mpfr_zero_p(denom
))
4835 mpfr_set_inf(real
, mpfr_sgn(rr
));
4836 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4837 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4838 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4840 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4841 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4843 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4844 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4847 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4848 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4852 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
4856 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
4858 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4859 mpfr_set_inf(real
, mpfr_sgn(t3
));
4861 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
4862 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
4863 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4864 mpfr_set_inf(imag
, mpfr_sgn(t3
));
4870 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
4871 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
4873 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4874 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
4877 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4878 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
4882 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
4886 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
4888 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4889 mpfr_set_ui(real
, 0, GMP_RNDN
);
4890 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
4892 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
4893 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
4894 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4895 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4896 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
4914 case OPERATOR_LSHIFT
:
4915 case OPERATOR_RSHIFT
:
4921 Type
* type
= left_type
;
4924 else if (type
!= right_type
&& right_type
!= NULL
)
4926 if (type
->is_abstract())
4928 else if (!right_type
->is_abstract())
4930 // This looks like a type error which should be diagnosed
4931 // elsewhere. Don't do anything here, to avoid an unhelpful
4932 // chain of error messages.
4937 if (type
!= NULL
&& !type
->is_abstract())
4939 if ((type
!= left_type
4940 && !Complex_expression::check_constant(left_real
, left_imag
,
4942 || (type
!= right_type
4943 && !Complex_expression::check_constant(right_real
, right_imag
,
4945 || !Complex_expression::check_constant(real
, imag
, type
,
4948 mpfr_set_ui(real
, 0, GMP_RNDN
);
4949 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4956 // Lower a binary expression. We have to evaluate constant
4957 // expressions now, in order to implement Go's unlimited precision
4961 Binary_expression::do_lower(Gogo
*, Named_object
*, int)
4963 source_location location
= this->location();
4964 Operator op
= this->op_
;
4965 Expression
* left
= this->left_
;
4966 Expression
* right
= this->right_
;
4968 const bool is_comparison
= (op
== OPERATOR_EQEQ
4969 || op
== OPERATOR_NOTEQ
4970 || op
== OPERATOR_LT
4971 || op
== OPERATOR_LE
4972 || op
== OPERATOR_GT
4973 || op
== OPERATOR_GE
);
4975 // Integer constant expressions.
4981 mpz_init(right_val
);
4983 if (left
->integer_constant_value(false, left_val
, &left_type
)
4984 && right
->integer_constant_value(false, right_val
, &right_type
))
4986 Expression
* ret
= NULL
;
4987 if (left_type
!= right_type
4988 && left_type
!= NULL
4989 && right_type
!= NULL
4990 && left_type
->base() != right_type
->base()
4991 && op
!= OPERATOR_LSHIFT
4992 && op
!= OPERATOR_RSHIFT
)
4994 // May be a type error--let it be diagnosed later.
4996 else if (is_comparison
)
4998 bool b
= Binary_expression::compare_integer(op
, left_val
,
5000 ret
= Expression::make_cast(Type::lookup_bool_type(),
5001 Expression::make_boolean(b
, location
),
5009 if (Binary_expression::eval_integer(op
, left_type
, left_val
,
5010 right_type
, right_val
,
5013 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
);
5015 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
5017 else if (left_type
== NULL
)
5019 else if (right_type
== NULL
)
5021 else if (!left_type
->is_abstract()
5022 && left_type
->named_type() != NULL
)
5024 else if (!right_type
->is_abstract()
5025 && right_type
->named_type() != NULL
)
5027 else if (!left_type
->is_abstract())
5029 else if (!right_type
->is_abstract())
5031 else if (left_type
->float_type() != NULL
)
5033 else if (right_type
->float_type() != NULL
)
5035 else if (left_type
->complex_type() != NULL
)
5037 else if (right_type
->complex_type() != NULL
)
5041 ret
= Expression::make_integer(&val
, type
, location
);
5049 mpz_clear(right_val
);
5050 mpz_clear(left_val
);
5054 mpz_clear(right_val
);
5055 mpz_clear(left_val
);
5058 // Floating point constant expressions.
5061 mpfr_init(left_val
);
5064 mpfr_init(right_val
);
5066 if (left
->float_constant_value(left_val
, &left_type
)
5067 && right
->float_constant_value(right_val
, &right_type
))
5069 Expression
* ret
= NULL
;
5070 if (left_type
!= right_type
5071 && left_type
!= NULL
5072 && right_type
!= NULL
5073 && left_type
->base() != right_type
->base()
5074 && op
!= OPERATOR_LSHIFT
5075 && op
!= OPERATOR_RSHIFT
)
5077 // May be a type error--let it be diagnosed later.
5079 else if (is_comparison
)
5081 bool b
= Binary_expression::compare_float(op
,
5085 left_val
, right_val
);
5086 ret
= Expression::make_boolean(b
, location
);
5093 if (Binary_expression::eval_float(op
, left_type
, left_val
,
5094 right_type
, right_val
, val
,
5097 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5098 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5100 if (left_type
== NULL
)
5102 else if (right_type
== NULL
)
5104 else if (!left_type
->is_abstract()
5105 && left_type
->named_type() != NULL
)
5107 else if (!right_type
->is_abstract()
5108 && right_type
->named_type() != NULL
)
5110 else if (!left_type
->is_abstract())
5112 else if (!right_type
->is_abstract())
5114 else if (left_type
->float_type() != NULL
)
5116 else if (right_type
->float_type() != NULL
)
5120 ret
= Expression::make_float(&val
, type
, location
);
5128 mpfr_clear(right_val
);
5129 mpfr_clear(left_val
);
5133 mpfr_clear(right_val
);
5134 mpfr_clear(left_val
);
5137 // Complex constant expressions.
5141 mpfr_init(left_real
);
5142 mpfr_init(left_imag
);
5147 mpfr_init(right_real
);
5148 mpfr_init(right_imag
);
5151 if (left
->complex_constant_value(left_real
, left_imag
, &left_type
)
5152 && right
->complex_constant_value(right_real
, right_imag
, &right_type
))
5154 Expression
* ret
= NULL
;
5155 if (left_type
!= right_type
5156 && left_type
!= NULL
5157 && right_type
!= NULL
5158 && left_type
->base() != right_type
->base())
5160 // May be a type error--let it be diagnosed later.
5162 else if (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
)
5164 bool b
= Binary_expression::compare_complex(op
,
5172 ret
= Expression::make_boolean(b
, location
);
5181 if (Binary_expression::eval_complex(op
, left_type
,
5182 left_real
, left_imag
,
5184 right_real
, right_imag
,
5188 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5189 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5191 if (left_type
== NULL
)
5193 else if (right_type
== NULL
)
5195 else if (!left_type
->is_abstract()
5196 && left_type
->named_type() != NULL
)
5198 else if (!right_type
->is_abstract()
5199 && right_type
->named_type() != NULL
)
5201 else if (!left_type
->is_abstract())
5203 else if (!right_type
->is_abstract())
5205 else if (left_type
->complex_type() != NULL
)
5207 else if (right_type
->complex_type() != NULL
)
5211 ret
= Expression::make_complex(&real
, &imag
, type
,
5220 mpfr_clear(left_real
);
5221 mpfr_clear(left_imag
);
5222 mpfr_clear(right_real
);
5223 mpfr_clear(right_imag
);
5228 mpfr_clear(left_real
);
5229 mpfr_clear(left_imag
);
5230 mpfr_clear(right_real
);
5231 mpfr_clear(right_imag
);
5234 // String constant expressions.
5235 if (op
== OPERATOR_PLUS
5236 && left
->type()->is_string_type()
5237 && right
->type()->is_string_type())
5239 std::string left_string
;
5240 std::string right_string
;
5241 if (left
->string_constant_value(&left_string
)
5242 && right
->string_constant_value(&right_string
))
5243 return Expression::make_string(left_string
+ right_string
, location
);
5249 // Return the integer constant value, if it has one.
5252 Binary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
5258 if (!this->left_
->integer_constant_value(iota_is_constant
, left_val
,
5261 mpz_clear(left_val
);
5266 mpz_init(right_val
);
5268 if (!this->right_
->integer_constant_value(iota_is_constant
, right_val
,
5271 mpz_clear(right_val
);
5272 mpz_clear(left_val
);
5277 if (left_type
!= right_type
5278 && left_type
!= NULL
5279 && right_type
!= NULL
5280 && left_type
->base() != right_type
->base()
5281 && this->op_
!= OPERATOR_RSHIFT
5282 && this->op_
!= OPERATOR_LSHIFT
)
5285 ret
= Binary_expression::eval_integer(this->op_
, left_type
, left_val
,
5286 right_type
, right_val
,
5287 this->location(), val
);
5289 mpz_clear(right_val
);
5290 mpz_clear(left_val
);
5298 // Return the floating point constant value, if it has one.
5301 Binary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
5304 mpfr_init(left_val
);
5306 if (!this->left_
->float_constant_value(left_val
, &left_type
))
5308 mpfr_clear(left_val
);
5313 mpfr_init(right_val
);
5315 if (!this->right_
->float_constant_value(right_val
, &right_type
))
5317 mpfr_clear(right_val
);
5318 mpfr_clear(left_val
);
5323 if (left_type
!= right_type
5324 && left_type
!= NULL
5325 && right_type
!= NULL
5326 && left_type
->base() != right_type
->base())
5329 ret
= Binary_expression::eval_float(this->op_
, left_type
, left_val
,
5330 right_type
, right_val
,
5331 val
, this->location());
5333 mpfr_clear(left_val
);
5334 mpfr_clear(right_val
);
5342 // Return the complex constant value, if it has one.
5345 Binary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
5350 mpfr_init(left_real
);
5351 mpfr_init(left_imag
);
5353 if (!this->left_
->complex_constant_value(left_real
, left_imag
, &left_type
))
5355 mpfr_clear(left_real
);
5356 mpfr_clear(left_imag
);
5362 mpfr_init(right_real
);
5363 mpfr_init(right_imag
);
5365 if (!this->right_
->complex_constant_value(right_real
, right_imag
,
5368 mpfr_clear(left_real
);
5369 mpfr_clear(left_imag
);
5370 mpfr_clear(right_real
);
5371 mpfr_clear(right_imag
);
5376 if (left_type
!= right_type
5377 && left_type
!= NULL
5378 && right_type
!= NULL
5379 && left_type
->base() != right_type
->base())
5382 ret
= Binary_expression::eval_complex(this->op_
, left_type
,
5383 left_real
, left_imag
,
5385 right_real
, right_imag
,
5388 mpfr_clear(left_real
);
5389 mpfr_clear(left_imag
);
5390 mpfr_clear(right_real
);
5391 mpfr_clear(right_imag
);
5399 // Note that the value is being discarded.
5402 Binary_expression::do_discarding_value()
5404 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5405 this->right_
->discarding_value();
5407 this->warn_about_unused_value();
5413 Binary_expression::do_type()
5418 case OPERATOR_ANDAND
:
5420 case OPERATOR_NOTEQ
:
5425 return Type::lookup_bool_type();
5428 case OPERATOR_MINUS
:
5435 case OPERATOR_BITCLEAR
:
5437 Type
* left_type
= this->left_
->type();
5438 Type
* right_type
= this->right_
->type();
5439 if (left_type
->is_error_type())
5441 else if (right_type
->is_error_type())
5443 else if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5445 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5447 else if (!left_type
->is_abstract())
5449 else if (!right_type
->is_abstract())
5451 else if (left_type
->complex_type() != NULL
)
5453 else if (right_type
->complex_type() != NULL
)
5455 else if (left_type
->float_type() != NULL
)
5457 else if (right_type
->float_type() != NULL
)
5463 case OPERATOR_LSHIFT
:
5464 case OPERATOR_RSHIFT
:
5465 return this->left_
->type();
5472 // Set type for a binary expression.
5475 Binary_expression::do_determine_type(const Type_context
* context
)
5477 Type
* tleft
= this->left_
->type();
5478 Type
* tright
= this->right_
->type();
5480 // Both sides should have the same type, except for the shift
5481 // operations. For a comparison, we should ignore the incoming
5484 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5485 || this->op_
== OPERATOR_RSHIFT
);
5487 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5488 || this->op_
== OPERATOR_NOTEQ
5489 || this->op_
== OPERATOR_LT
5490 || this->op_
== OPERATOR_LE
5491 || this->op_
== OPERATOR_GT
5492 || this->op_
== OPERATOR_GE
);
5494 Type_context
subcontext(*context
);
5498 // In a comparison, the context does not determine the types of
5500 subcontext
.type
= NULL
;
5503 // Set the context for the left hand operand.
5506 // The right hand operand plays no role in determining the type
5507 // of the left hand operand. A shift of an abstract integer in
5508 // a string context gets special treatment, which may be a
5510 if (subcontext
.type
!= NULL
5511 && subcontext
.type
->is_string_type()
5512 && tleft
->is_abstract())
5513 error_at(this->location(), "shift of non-integer operand");
5515 else if (!tleft
->is_abstract())
5516 subcontext
.type
= tleft
;
5517 else if (!tright
->is_abstract())
5518 subcontext
.type
= tright
;
5519 else if (subcontext
.type
== NULL
)
5521 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5522 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5523 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5525 // Both sides have an abstract integer, abstract float, or
5526 // abstract complex type. Just let CONTEXT determine
5527 // whether they may remain abstract or not.
5529 else if (tleft
->complex_type() != NULL
)
5530 subcontext
.type
= tleft
;
5531 else if (tright
->complex_type() != NULL
)
5532 subcontext
.type
= tright
;
5533 else if (tleft
->float_type() != NULL
)
5534 subcontext
.type
= tleft
;
5535 else if (tright
->float_type() != NULL
)
5536 subcontext
.type
= tright
;
5538 subcontext
.type
= tleft
;
5540 if (subcontext
.type
!= NULL
&& !context
->may_be_abstract
)
5541 subcontext
.type
= subcontext
.type
->make_non_abstract_type();
5544 this->left_
->determine_type(&subcontext
);
5546 // The context for the right hand operand is the same as for the
5547 // left hand operand, except for a shift operator.
5550 subcontext
.type
= Type::lookup_integer_type("uint");
5551 subcontext
.may_be_abstract
= false;
5554 this->right_
->determine_type(&subcontext
);
5557 // Report an error if the binary operator OP does not support TYPE.
5558 // Return whether the operation is OK. This should not be used for
5562 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5563 source_location location
)
5568 case OPERATOR_ANDAND
:
5569 if (!type
->is_boolean_type())
5571 error_at(location
, "expected boolean type");
5577 case OPERATOR_NOTEQ
:
5578 if (type
->integer_type() == NULL
5579 && type
->float_type() == NULL
5580 && type
->complex_type() == NULL
5581 && !type
->is_string_type()
5582 && type
->points_to() == NULL
5583 && !type
->is_nil_type()
5584 && !type
->is_boolean_type()
5585 && type
->interface_type() == NULL
5586 && (type
->array_type() == NULL
5587 || type
->array_type()->length() != NULL
)
5588 && type
->map_type() == NULL
5589 && type
->channel_type() == NULL
5590 && type
->function_type() == NULL
)
5593 ("expected integer, floating, complex, string, pointer, "
5594 "boolean, interface, slice, map, channel, "
5595 "or function type"));
5604 if (type
->integer_type() == NULL
5605 && type
->float_type() == NULL
5606 && !type
->is_string_type())
5608 error_at(location
, "expected integer, floating, or string type");
5614 case OPERATOR_PLUSEQ
:
5615 if (type
->integer_type() == NULL
5616 && type
->float_type() == NULL
5617 && type
->complex_type() == NULL
5618 && !type
->is_string_type())
5621 "expected integer, floating, complex, or string type");
5626 case OPERATOR_MINUS
:
5627 case OPERATOR_MINUSEQ
:
5629 case OPERATOR_MULTEQ
:
5631 case OPERATOR_DIVEQ
:
5632 if (type
->integer_type() == NULL
5633 && type
->float_type() == NULL
5634 && type
->complex_type() == NULL
)
5636 error_at(location
, "expected integer, floating, or complex type");
5642 case OPERATOR_MODEQ
:
5646 case OPERATOR_ANDEQ
:
5648 case OPERATOR_XOREQ
:
5649 case OPERATOR_BITCLEAR
:
5650 case OPERATOR_BITCLEAREQ
:
5651 if (type
->integer_type() == NULL
)
5653 error_at(location
, "expected integer type");
5668 Binary_expression::do_check_types(Gogo
*)
5670 Type
* left_type
= this->left_
->type();
5671 Type
* right_type
= this->right_
->type();
5672 if (left_type
->is_error_type() || right_type
->is_error_type())
5674 this->set_is_error();
5678 if (this->op_
== OPERATOR_EQEQ
5679 || this->op_
== OPERATOR_NOTEQ
5680 || this->op_
== OPERATOR_LT
5681 || this->op_
== OPERATOR_LE
5682 || this->op_
== OPERATOR_GT
5683 || this->op_
== OPERATOR_GE
)
5685 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5686 && !Type::are_assignable(right_type
, left_type
, NULL
))
5688 this->report_error(_("incompatible types in binary expression"));
5691 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5693 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5696 this->set_is_error();
5700 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5702 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5704 this->report_error(_("incompatible types in binary expression"));
5707 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5710 this->set_is_error();
5716 if (left_type
->integer_type() == NULL
)
5717 this->report_error(_("shift of non-integer operand"));
5719 if (!right_type
->is_abstract()
5720 && (right_type
->integer_type() == NULL
5721 || !right_type
->integer_type()->is_unsigned()))
5722 this->report_error(_("shift count not unsigned integer"));
5728 if (this->right_
->integer_constant_value(true, val
, &type
))
5730 if (mpz_sgn(val
) < 0)
5731 this->report_error(_("negative shift count"));
5738 // Get a tree for a binary expression.
5741 Binary_expression::do_get_tree(Translate_context
* context
)
5743 tree left
= this->left_
->get_tree(context
);
5744 tree right
= this->right_
->get_tree(context
);
5746 if (left
== error_mark_node
|| right
== error_mark_node
)
5747 return error_mark_node
;
5749 enum tree_code code
;
5750 bool use_left_type
= true;
5751 bool is_shift_op
= false;
5755 case OPERATOR_NOTEQ
:
5760 return Expression::comparison_tree(context
, this->op_
,
5761 this->left_
->type(), left
,
5762 this->right_
->type(), right
,
5766 code
= TRUTH_ORIF_EXPR
;
5767 use_left_type
= false;
5769 case OPERATOR_ANDAND
:
5770 code
= TRUTH_ANDIF_EXPR
;
5771 use_left_type
= false;
5776 case OPERATOR_MINUS
:
5780 code
= BIT_IOR_EXPR
;
5783 code
= BIT_XOR_EXPR
;
5790 Type
*t
= this->left_
->type();
5791 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5794 code
= TRUNC_DIV_EXPR
;
5798 code
= TRUNC_MOD_EXPR
;
5800 case OPERATOR_LSHIFT
:
5804 case OPERATOR_RSHIFT
:
5809 code
= BIT_AND_EXPR
;
5811 case OPERATOR_BITCLEAR
:
5812 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5813 code
= BIT_AND_EXPR
;
5819 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5821 if (this->left_
->type()->is_string_type())
5823 gcc_assert(this->op_
== OPERATOR_PLUS
);
5824 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5825 static tree string_plus_decl
;
5826 return Gogo::call_builtin(&string_plus_decl
,
5837 tree compute_type
= excess_precision_type(type
);
5838 if (compute_type
!= NULL_TREE
)
5840 left
= ::convert(compute_type
, left
);
5841 right
= ::convert(compute_type
, right
);
5844 tree eval_saved
= NULL_TREE
;
5848 left
= save_expr(left
);
5850 right
= save_expr(right
);
5851 // Make sure the values are evaluated.
5852 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5853 void_type_node
, left
, right
);
5856 tree ret
= fold_build2_loc(this->location(),
5858 compute_type
!= NULL_TREE
? compute_type
: type
,
5861 if (compute_type
!= NULL_TREE
)
5862 ret
= ::convert(type
, ret
);
5864 // In Go, a shift larger than the size of the type is well-defined.
5865 // This is not true in GENERIC, so we need to insert a conditional.
5868 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5869 gcc_assert(this->left_
->type()->integer_type() != NULL
);
5870 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5872 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5873 build_int_cst_type(TREE_TYPE(right
), bits
));
5875 tree overflow_result
= fold_convert_loc(this->location(),
5878 if (this->op_
== OPERATOR_RSHIFT
5879 && !this->left_
->type()->integer_type()->is_unsigned())
5881 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
5882 boolean_type_node
, left
,
5883 fold_convert_loc(this->location(),
5885 integer_zero_node
));
5886 tree neg_one
= fold_build2_loc(this->location(),
5887 MINUS_EXPR
, TREE_TYPE(left
),
5888 fold_convert_loc(this->location(),
5891 fold_convert_loc(this->location(),
5894 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
5895 TREE_TYPE(left
), neg
, neg_one
,
5899 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
5900 compare
, ret
, overflow_result
);
5902 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5903 TREE_TYPE(ret
), eval_saved
, ret
);
5909 // Export a binary expression.
5912 Binary_expression::do_export(Export
* exp
) const
5914 exp
->write_c_string("(");
5915 this->left_
->export_expression(exp
);
5919 exp
->write_c_string(" || ");
5921 case OPERATOR_ANDAND
:
5922 exp
->write_c_string(" && ");
5925 exp
->write_c_string(" == ");
5927 case OPERATOR_NOTEQ
:
5928 exp
->write_c_string(" != ");
5931 exp
->write_c_string(" < ");
5934 exp
->write_c_string(" <= ");
5937 exp
->write_c_string(" > ");
5940 exp
->write_c_string(" >= ");
5943 exp
->write_c_string(" + ");
5945 case OPERATOR_MINUS
:
5946 exp
->write_c_string(" - ");
5949 exp
->write_c_string(" | ");
5952 exp
->write_c_string(" ^ ");
5955 exp
->write_c_string(" * ");
5958 exp
->write_c_string(" / ");
5961 exp
->write_c_string(" % ");
5963 case OPERATOR_LSHIFT
:
5964 exp
->write_c_string(" << ");
5966 case OPERATOR_RSHIFT
:
5967 exp
->write_c_string(" >> ");
5970 exp
->write_c_string(" & ");
5972 case OPERATOR_BITCLEAR
:
5973 exp
->write_c_string(" &^ ");
5978 this->right_
->export_expression(exp
);
5979 exp
->write_c_string(")");
5982 // Import a binary expression.
5985 Binary_expression::do_import(Import
* imp
)
5987 imp
->require_c_string("(");
5989 Expression
* left
= Expression::import_expression(imp
);
5992 if (imp
->match_c_string(" || "))
5997 else if (imp
->match_c_string(" && "))
5999 op
= OPERATOR_ANDAND
;
6002 else if (imp
->match_c_string(" == "))
6007 else if (imp
->match_c_string(" != "))
6009 op
= OPERATOR_NOTEQ
;
6012 else if (imp
->match_c_string(" < "))
6017 else if (imp
->match_c_string(" <= "))
6022 else if (imp
->match_c_string(" > "))
6027 else if (imp
->match_c_string(" >= "))
6032 else if (imp
->match_c_string(" + "))
6037 else if (imp
->match_c_string(" - "))
6039 op
= OPERATOR_MINUS
;
6042 else if (imp
->match_c_string(" | "))
6047 else if (imp
->match_c_string(" ^ "))
6052 else if (imp
->match_c_string(" * "))
6057 else if (imp
->match_c_string(" / "))
6062 else if (imp
->match_c_string(" % "))
6067 else if (imp
->match_c_string(" << "))
6069 op
= OPERATOR_LSHIFT
;
6072 else if (imp
->match_c_string(" >> "))
6074 op
= OPERATOR_RSHIFT
;
6077 else if (imp
->match_c_string(" & "))
6082 else if (imp
->match_c_string(" &^ "))
6084 op
= OPERATOR_BITCLEAR
;
6089 error_at(imp
->location(), "unrecognized binary operator");
6090 return Expression::make_error(imp
->location());
6093 Expression
* right
= Expression::import_expression(imp
);
6095 imp
->require_c_string(")");
6097 return Expression::make_binary(op
, left
, right
, imp
->location());
6100 // Make a binary expression.
6103 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6104 source_location location
)
6106 return new Binary_expression(op
, left
, right
, location
);
6109 // Implement a comparison.
6112 Expression::comparison_tree(Translate_context
* context
, Operator op
,
6113 Type
* left_type
, tree left_tree
,
6114 Type
* right_type
, tree right_tree
,
6115 source_location location
)
6117 enum tree_code code
;
6123 case OPERATOR_NOTEQ
:
6142 if (left_type
->is_string_type() && right_type
->is_string_type())
6144 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
6145 static tree string_compare_decl
;
6146 left_tree
= Gogo::call_builtin(&string_compare_decl
,
6155 right_tree
= build_int_cst_type(integer_type_node
, 0);
6157 else if ((left_type
->interface_type() != NULL
6158 && right_type
->interface_type() == NULL
6159 && !right_type
->is_nil_type())
6160 || (left_type
->interface_type() == NULL
6161 && !left_type
->is_nil_type()
6162 && right_type
->interface_type() != NULL
))
6164 // Comparing an interface value to a non-interface value.
6165 if (left_type
->interface_type() == NULL
)
6167 std::swap(left_type
, right_type
);
6168 std::swap(left_tree
, right_tree
);
6171 // The right operand is not an interface. We need to take its
6172 // address if it is not a pointer.
6175 if (right_type
->points_to() != NULL
)
6177 make_tmp
= NULL_TREE
;
6180 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
6182 make_tmp
= NULL_TREE
;
6183 arg
= build_fold_addr_expr_loc(location
, right_tree
);
6184 if (DECL_P(right_tree
))
6185 TREE_ADDRESSABLE(right_tree
) = 1;
6189 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
6190 get_name(right_tree
));
6191 DECL_IGNORED_P(tmp
) = 0;
6192 DECL_INITIAL(tmp
) = right_tree
;
6193 TREE_ADDRESSABLE(tmp
) = 1;
6194 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6195 SET_EXPR_LOCATION(make_tmp
, location
);
6196 arg
= build_fold_addr_expr_loc(location
, tmp
);
6198 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
6200 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
6202 if (left_type
->interface_type()->is_empty())
6204 static tree empty_interface_value_compare_decl
;
6205 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6207 "__go_empty_interface_value_compare",
6210 TREE_TYPE(left_tree
),
6212 TREE_TYPE(descriptor
),
6216 if (left_tree
== error_mark_node
)
6217 return error_mark_node
;
6218 // This can panic if the type is not comparable.
6219 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6223 static tree interface_value_compare_decl
;
6224 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6226 "__go_interface_value_compare",
6229 TREE_TYPE(left_tree
),
6231 TREE_TYPE(descriptor
),
6235 if (left_tree
== error_mark_node
)
6236 return error_mark_node
;
6237 // This can panic if the type is not comparable.
6238 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6240 right_tree
= build_int_cst_type(integer_type_node
, 0);
6242 if (make_tmp
!= NULL_TREE
)
6243 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6246 else if (left_type
->interface_type() != NULL
6247 && right_type
->interface_type() != NULL
)
6249 if (left_type
->interface_type()->is_empty())
6251 gcc_assert(right_type
->interface_type()->is_empty());
6252 static tree empty_interface_compare_decl
;
6253 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6255 "__go_empty_interface_compare",
6258 TREE_TYPE(left_tree
),
6260 TREE_TYPE(right_tree
),
6262 if (left_tree
== error_mark_node
)
6263 return error_mark_node
;
6264 // This can panic if the type is uncomparable.
6265 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6269 gcc_assert(!right_type
->interface_type()->is_empty());
6270 static tree interface_compare_decl
;
6271 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6273 "__go_interface_compare",
6276 TREE_TYPE(left_tree
),
6278 TREE_TYPE(right_tree
),
6280 if (left_tree
== error_mark_node
)
6281 return error_mark_node
;
6282 // This can panic if the type is uncomparable.
6283 TREE_NOTHROW(interface_compare_decl
) = 0;
6285 right_tree
= build_int_cst_type(integer_type_node
, 0);
6288 if (left_type
->is_nil_type()
6289 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6291 std::swap(left_type
, right_type
);
6292 std::swap(left_tree
, right_tree
);
6295 if (right_type
->is_nil_type())
6297 if (left_type
->array_type() != NULL
6298 && left_type
->array_type()->length() == NULL
)
6300 Array_type
* at
= left_type
->array_type();
6301 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6302 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6304 else if (left_type
->interface_type() != NULL
)
6306 // An interface is nil if the first field is nil.
6307 tree left_type_tree
= TREE_TYPE(left_tree
);
6308 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6309 tree field
= TYPE_FIELDS(left_type_tree
);
6310 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6312 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6316 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6317 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6321 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6322 return error_mark_node
;
6324 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6325 if (CAN_HAVE_LOCATION_P(ret
))
6326 SET_EXPR_LOCATION(ret
, location
);
6330 // Class Bound_method_expression.
6335 Bound_method_expression::do_traverse(Traverse
* traverse
)
6337 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6338 return TRAVERSE_EXIT
;
6339 return Expression::traverse(&this->method_
, traverse
);
6342 // Return the type of a bound method expression. The type of this
6343 // object is really the type of the method with no receiver. We
6344 // should be able to get away with just returning the type of the
6348 Bound_method_expression::do_type()
6350 return this->method_
->type();
6353 // Determine the types of a method expression.
6356 Bound_method_expression::do_determine_type(const Type_context
*)
6358 this->method_
->determine_type_no_context();
6359 Type
* mtype
= this->method_
->type();
6360 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6361 if (fntype
== NULL
|| !fntype
->is_method())
6362 this->expr_
->determine_type_no_context();
6365 Type_context
subcontext(fntype
->receiver()->type(), false);
6366 this->expr_
->determine_type(&subcontext
);
6370 // Check the types of a method expression.
6373 Bound_method_expression::do_check_types(Gogo
*)
6375 Type
* type
= this->method_
->type()->deref();
6377 || type
->function_type() == NULL
6378 || !type
->function_type()->is_method())
6379 this->report_error(_("object is not a method"));
6382 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6383 Type
* etype
= (this->expr_type_
!= NULL
6385 : this->expr_
->type());
6386 etype
= etype
->deref();
6387 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6388 this->report_error(_("method type does not match object type"));
6392 // Get the tree for a method expression. There is no standard tree
6393 // representation for this. The only places it may currently be used
6394 // are in a Call_expression or a Go_statement, which will take it
6395 // apart directly. So this has nothing to do at present.
6398 Bound_method_expression::do_get_tree(Translate_context
*)
6400 error_at(this->location(), "reference to method other than calling it");
6401 return error_mark_node
;
6404 // Make a method expression.
6406 Bound_method_expression
*
6407 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6408 source_location location
)
6410 return new Bound_method_expression(expr
, method
, location
);
6413 // Class Builtin_call_expression. This is used for a call to a
6414 // builtin function.
6416 class Builtin_call_expression
: public Call_expression
6419 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6420 bool is_varargs
, source_location location
);
6423 // This overrides Call_expression::do_lower.
6425 do_lower(Gogo
*, Named_object
*, int);
6428 do_is_constant() const;
6431 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6434 do_float_constant_value(mpfr_t
, Type
**) const;
6437 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6443 do_determine_type(const Type_context
*);
6446 do_check_types(Gogo
*);
6451 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6452 this->args()->copy(),
6458 do_get_tree(Translate_context
*);
6461 do_export(Export
*) const;
6464 do_is_recover_call() const;
6467 do_set_recover_arg(Expression
*);
6470 // The builtin functions.
6471 enum Builtin_function_code
6475 // Predeclared builtin functions.
6492 // Builtin functions from the unsafe package.
6505 real_imag_type(Type
*);
6508 complex_type(Type
*);
6510 // A pointer back to the general IR structure. This avoids a global
6511 // variable, or passing it around everywhere.
6513 // The builtin function being called.
6514 Builtin_function_code code_
;
6515 // Used to stop endless loops when the length of an array uses len
6516 // or cap of the array itself.
6520 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6522 Expression_list
* args
,
6524 source_location location
)
6525 : Call_expression(fn
, args
, is_varargs
, location
),
6526 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false)
6528 Func_expression
* fnexp
= this->fn()->func_expression();
6529 gcc_assert(fnexp
!= NULL
);
6530 const std::string
& name(fnexp
->named_object()->name());
6531 if (name
== "append")
6532 this->code_
= BUILTIN_APPEND
;
6533 else if (name
== "cap")
6534 this->code_
= BUILTIN_CAP
;
6535 else if (name
== "close")
6536 this->code_
= BUILTIN_CLOSE
;
6537 else if (name
== "closed")
6538 this->code_
= BUILTIN_CLOSED
;
6539 else if (name
== "complex")
6540 this->code_
= BUILTIN_COMPLEX
;
6541 else if (name
== "copy")
6542 this->code_
= BUILTIN_COPY
;
6543 else if (name
== "imag")
6544 this->code_
= BUILTIN_IMAG
;
6545 else if (name
== "len")
6546 this->code_
= BUILTIN_LEN
;
6547 else if (name
== "make")
6548 this->code_
= BUILTIN_MAKE
;
6549 else if (name
== "new")
6550 this->code_
= BUILTIN_NEW
;
6551 else if (name
== "panic")
6552 this->code_
= BUILTIN_PANIC
;
6553 else if (name
== "print")
6554 this->code_
= BUILTIN_PRINT
;
6555 else if (name
== "println")
6556 this->code_
= BUILTIN_PRINTLN
;
6557 else if (name
== "real")
6558 this->code_
= BUILTIN_REAL
;
6559 else if (name
== "recover")
6560 this->code_
= BUILTIN_RECOVER
;
6561 else if (name
== "Alignof")
6562 this->code_
= BUILTIN_ALIGNOF
;
6563 else if (name
== "Offsetof")
6564 this->code_
= BUILTIN_OFFSETOF
;
6565 else if (name
== "Sizeof")
6566 this->code_
= BUILTIN_SIZEOF
;
6571 // Return whether this is a call to recover. This is a virtual
6572 // function called from the parent class.
6575 Builtin_call_expression::do_is_recover_call() const
6577 if (this->classification() == EXPRESSION_ERROR
)
6579 return this->code_
== BUILTIN_RECOVER
;
6582 // Set the argument for a call to recover.
6585 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6587 const Expression_list
* args
= this->args();
6588 gcc_assert(args
== NULL
|| args
->empty());
6589 Expression_list
* new_args
= new Expression_list();
6590 new_args
->push_back(arg
);
6591 this->set_args(new_args
);
6594 // A traversal class which looks for a call expression.
6596 class Find_call_expression
: public Traverse
6599 Find_call_expression()
6600 : Traverse(traverse_expressions
),
6605 expression(Expression
**);
6609 { return this->found_
; }
6616 Find_call_expression::expression(Expression
** pexpr
)
6618 if ((*pexpr
)->call_expression() != NULL
)
6620 this->found_
= true;
6621 return TRAVERSE_EXIT
;
6623 return TRAVERSE_CONTINUE
;
6626 // Lower a builtin call expression. This turns new and make into
6627 // specific expressions. We also convert to a constant if we can.
6630 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6632 if (this->code_
== BUILTIN_NEW
)
6634 const Expression_list
* args
= this->args();
6635 if (args
== NULL
|| args
->size() < 1)
6636 this->report_error(_("not enough arguments"));
6637 else if (args
->size() > 1)
6638 this->report_error(_("too many arguments"));
6641 Expression
* arg
= args
->front();
6642 if (!arg
->is_type_expression())
6644 error_at(arg
->location(), "expected type");
6645 this->set_is_error();
6648 return Expression::make_allocation(arg
->type(), this->location());
6651 else if (this->code_
== BUILTIN_MAKE
)
6653 const Expression_list
* args
= this->args();
6654 if (args
== NULL
|| args
->size() < 1)
6655 this->report_error(_("not enough arguments"));
6658 Expression
* arg
= args
->front();
6659 if (!arg
->is_type_expression())
6661 error_at(arg
->location(), "expected type");
6662 this->set_is_error();
6666 Expression_list
* newargs
;
6667 if (args
->size() == 1)
6671 newargs
= new Expression_list();
6672 Expression_list::const_iterator p
= args
->begin();
6674 for (; p
!= args
->end(); ++p
)
6675 newargs
->push_back(*p
);
6677 return Expression::make_make(arg
->type(), newargs
,
6682 else if (this->is_constant())
6684 // We can only lower len and cap if there are no function calls
6685 // in the arguments. Otherwise we have to make the call.
6686 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6688 Expression
* arg
= this->one_arg();
6689 if (!arg
->is_constant())
6691 Find_call_expression find_call
;
6692 Expression::traverse(&arg
, &find_call
);
6693 if (find_call
.found())
6701 if (this->integer_constant_value(true, ival
, &type
))
6703 Expression
* ret
= Expression::make_integer(&ival
, type
,
6712 if (this->float_constant_value(rval
, &type
))
6714 Expression
* ret
= Expression::make_float(&rval
, type
,
6722 if (this->complex_constant_value(rval
, imag
, &type
))
6724 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6733 else if (this->code_
== BUILTIN_RECOVER
)
6735 if (function
!= NULL
)
6736 function
->func_value()->set_calls_recover();
6739 // Calling recover outside of a function always returns the
6740 // nil empty interface.
6741 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6742 return Expression::make_cast(eface
,
6743 Expression::make_nil(this->location()),
6747 else if (this->code_
== BUILTIN_APPEND
)
6749 // Lower the varargs.
6750 const Expression_list
* args
= this->args();
6751 if (args
== NULL
|| args
->empty())
6753 Type
* slice_type
= args
->front()->type();
6754 if (!slice_type
->is_open_array_type())
6756 error_at(args
->front()->location(), "argument 1 must be a slice");
6757 this->set_is_error();
6760 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6766 // Return the type of the real or imag functions, given the type of
6767 // the argument. We need to map complex to float, complex64 to
6768 // float32, and complex128 to float64, so it has to be done by name.
6769 // This returns NULL if it can't figure out the type.
6772 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6774 if (arg_type
== NULL
|| arg_type
->is_abstract())
6776 Named_type
* nt
= arg_type
->named_type();
6779 while (nt
->real_type()->named_type() != NULL
)
6780 nt
= nt
->real_type()->named_type();
6781 if (nt
->name() == "complex64")
6782 return Type::lookup_float_type("float32");
6783 else if (nt
->name() == "complex128")
6784 return Type::lookup_float_type("float64");
6789 // Return the type of the complex function, given the type of one of the
6790 // argments. Like real_imag_type, we have to map by name.
6793 Builtin_call_expression::complex_type(Type
* arg_type
)
6795 if (arg_type
== NULL
|| arg_type
->is_abstract())
6797 Named_type
* nt
= arg_type
->named_type();
6800 while (nt
->real_type()->named_type() != NULL
)
6801 nt
= nt
->real_type()->named_type();
6802 if (nt
->name() == "float32")
6803 return Type::lookup_complex_type("complex64");
6804 else if (nt
->name() == "float64")
6805 return Type::lookup_complex_type("complex128");
6810 // Return a single argument, or NULL if there isn't one.
6813 Builtin_call_expression::one_arg() const
6815 const Expression_list
* args
= this->args();
6816 if (args
->size() != 1)
6818 return args
->front();
6821 // Return whether this is constant: len of a string, or len or cap of
6822 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6825 Builtin_call_expression::do_is_constant() const
6827 switch (this->code_
)
6835 Expression
* arg
= this->one_arg();
6838 Type
* arg_type
= arg
->type();
6840 if (arg_type
->points_to() != NULL
6841 && arg_type
->points_to()->array_type() != NULL
6842 && !arg_type
->points_to()->is_open_array_type())
6843 arg_type
= arg_type
->points_to();
6845 if (arg_type
->array_type() != NULL
6846 && arg_type
->array_type()->length() != NULL
)
6849 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6852 bool ret
= arg
->is_constant();
6853 this->seen_
= false;
6859 case BUILTIN_SIZEOF
:
6860 case BUILTIN_ALIGNOF
:
6861 return this->one_arg() != NULL
;
6863 case BUILTIN_OFFSETOF
:
6865 Expression
* arg
= this->one_arg();
6868 return arg
->field_reference_expression() != NULL
;
6871 case BUILTIN_COMPLEX
:
6873 const Expression_list
* args
= this->args();
6874 if (args
!= NULL
&& args
->size() == 2)
6875 return args
->front()->is_constant() && args
->back()->is_constant();
6882 Expression
* arg
= this->one_arg();
6883 return arg
!= NULL
&& arg
->is_constant();
6893 // Return an integer constant value if possible.
6896 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
6900 if (this->code_
== BUILTIN_LEN
6901 || this->code_
== BUILTIN_CAP
)
6903 Expression
* arg
= this->one_arg();
6906 Type
* arg_type
= arg
->type();
6908 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6911 if (arg
->string_constant_value(&sval
))
6913 mpz_set_ui(val
, sval
.length());
6914 *ptype
= Type::lookup_integer_type("int");
6919 if (arg_type
->points_to() != NULL
6920 && arg_type
->points_to()->array_type() != NULL
6921 && !arg_type
->points_to()->is_open_array_type())
6922 arg_type
= arg_type
->points_to();
6924 if (arg_type
->array_type() != NULL
6925 && arg_type
->array_type()->length() != NULL
)
6929 Expression
* e
= arg_type
->array_type()->length();
6931 bool r
= e
->integer_constant_value(iota_is_constant
, val
, ptype
);
6932 this->seen_
= false;
6935 *ptype
= Type::lookup_integer_type("int");
6940 else if (this->code_
== BUILTIN_SIZEOF
6941 || this->code_
== BUILTIN_ALIGNOF
)
6943 Expression
* arg
= this->one_arg();
6946 Type
* arg_type
= arg
->type();
6947 if (arg_type
->is_error_type() || arg_type
->is_undefined())
6949 if (arg_type
->is_abstract())
6951 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
6952 unsigned long val_long
;
6953 if (this->code_
== BUILTIN_SIZEOF
)
6955 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
6956 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
6957 if (TREE_INT_CST_HIGH(type_size
) != 0)
6959 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
6960 val_long
= static_cast<unsigned long>(val_wide
);
6961 if (val_long
!= val_wide
)
6964 else if (this->code_
== BUILTIN_ALIGNOF
)
6966 if (arg
->field_reference_expression() == NULL
)
6967 val_long
= go_type_alignment(arg_type_tree
);
6970 // Calling unsafe.Alignof(s.f) returns the alignment of
6971 // the type of f when it is used as a field in a struct.
6972 val_long
= go_field_alignment(arg_type_tree
);
6977 mpz_set_ui(val
, val_long
);
6981 else if (this->code_
== BUILTIN_OFFSETOF
)
6983 Expression
* arg
= this->one_arg();
6986 Field_reference_expression
* farg
= arg
->field_reference_expression();
6989 Expression
* struct_expr
= farg
->expr();
6990 Type
* st
= struct_expr
->type();
6991 if (st
->struct_type() == NULL
)
6993 tree struct_tree
= st
->get_tree(this->gogo_
);
6994 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
6995 tree field
= TYPE_FIELDS(struct_tree
);
6996 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
6998 field
= DECL_CHAIN(field
);
6999 gcc_assert(field
!= NULL_TREE
);
7001 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
7002 if (offset_wide
< 0)
7004 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
7005 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
7007 mpz_set_ui(val
, offset_long
);
7013 // Return a floating point constant value if possible.
7016 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
7019 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
7021 Expression
* arg
= this->one_arg();
7032 if (arg
->complex_constant_value(real
, imag
, &type
))
7034 if (this->code_
== BUILTIN_REAL
)
7035 mpfr_set(val
, real
, GMP_RNDN
);
7037 mpfr_set(val
, imag
, GMP_RNDN
);
7038 *ptype
= Builtin_call_expression::real_imag_type(type
);
7050 // Return a complex constant value if possible.
7053 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
7056 if (this->code_
== BUILTIN_COMPLEX
)
7058 const Expression_list
* args
= this->args();
7059 if (args
== NULL
|| args
->size() != 2)
7065 if (!args
->front()->float_constant_value(r
, &rtype
))
7076 if (args
->back()->float_constant_value(i
, &itype
)
7077 && Type::are_identical(rtype
, itype
, false, NULL
))
7079 mpfr_set(real
, r
, GMP_RNDN
);
7080 mpfr_set(imag
, i
, GMP_RNDN
);
7081 *ptype
= Builtin_call_expression::complex_type(rtype
);
7097 Builtin_call_expression::do_type()
7099 switch (this->code_
)
7101 case BUILTIN_INVALID
:
7108 const Expression_list
* args
= this->args();
7109 if (args
== NULL
|| args
->empty())
7110 return Type::make_error_type();
7111 return Type::make_pointer_type(args
->front()->type());
7117 case BUILTIN_ALIGNOF
:
7118 case BUILTIN_OFFSETOF
:
7119 case BUILTIN_SIZEOF
:
7120 return Type::lookup_integer_type("int");
7125 case BUILTIN_PRINTLN
:
7126 return Type::make_void_type();
7128 case BUILTIN_CLOSED
:
7129 return Type::lookup_bool_type();
7131 case BUILTIN_RECOVER
:
7132 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7134 case BUILTIN_APPEND
:
7136 const Expression_list
* args
= this->args();
7137 if (args
== NULL
|| args
->empty())
7138 return Type::make_error_type();
7139 return args
->front()->type();
7145 Expression
* arg
= this->one_arg();
7147 return Type::make_error_type();
7148 Type
* t
= arg
->type();
7149 if (t
->is_abstract())
7150 t
= t
->make_non_abstract_type();
7151 t
= Builtin_call_expression::real_imag_type(t
);
7153 t
= Type::make_error_type();
7157 case BUILTIN_COMPLEX
:
7159 const Expression_list
* args
= this->args();
7160 if (args
== NULL
|| args
->size() != 2)
7161 return Type::make_error_type();
7162 Type
* t
= args
->front()->type();
7163 if (t
->is_abstract())
7165 t
= args
->back()->type();
7166 if (t
->is_abstract())
7167 t
= t
->make_non_abstract_type();
7169 t
= Builtin_call_expression::complex_type(t
);
7171 t
= Type::make_error_type();
7177 // Determine the type.
7180 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7182 this->fn()->determine_type_no_context();
7184 const Expression_list
* args
= this->args();
7187 Type
* arg_type
= NULL
;
7188 switch (this->code_
)
7191 case BUILTIN_PRINTLN
:
7192 // Do not force a large integer constant to "int".
7198 arg_type
= Builtin_call_expression::complex_type(context
->type
);
7202 case BUILTIN_COMPLEX
:
7204 // For the complex function the type of one operand can
7205 // determine the type of the other, as in a binary expression.
7206 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7207 if (args
!= NULL
&& args
->size() == 2)
7209 Type
* t1
= args
->front()->type();
7210 Type
* t2
= args
->front()->type();
7211 if (!t1
->is_abstract())
7213 else if (!t2
->is_abstract())
7227 for (Expression_list::const_iterator pa
= args
->begin();
7231 Type_context subcontext
;
7232 subcontext
.type
= arg_type
;
7236 // We want to print large constants, we so can't just
7237 // use the appropriate nonabstract type. Use uint64 for
7238 // an integer if we know it is nonnegative, otherwise
7239 // use int64 for a integer, otherwise use float64 for a
7240 // float or complex128 for a complex.
7241 Type
* want_type
= NULL
;
7242 Type
* atype
= (*pa
)->type();
7243 if (atype
->is_abstract())
7245 if (atype
->integer_type() != NULL
)
7250 if (this->integer_constant_value(true, val
, &dummy
)
7251 && mpz_sgn(val
) >= 0)
7252 want_type
= Type::lookup_integer_type("uint64");
7254 want_type
= Type::lookup_integer_type("int64");
7257 else if (atype
->float_type() != NULL
)
7258 want_type
= Type::lookup_float_type("float64");
7259 else if (atype
->complex_type() != NULL
)
7260 want_type
= Type::lookup_complex_type("complex128");
7261 else if (atype
->is_abstract_string_type())
7262 want_type
= Type::lookup_string_type();
7263 else if (atype
->is_abstract_boolean_type())
7264 want_type
= Type::lookup_bool_type();
7267 subcontext
.type
= want_type
;
7271 (*pa
)->determine_type(&subcontext
);
7276 // If there is exactly one argument, return true. Otherwise give an
7277 // error message and return false.
7280 Builtin_call_expression::check_one_arg()
7282 const Expression_list
* args
= this->args();
7283 if (args
== NULL
|| args
->size() < 1)
7285 this->report_error(_("not enough arguments"));
7288 else if (args
->size() > 1)
7290 this->report_error(_("too many arguments"));
7293 if (args
->front()->is_error_expression()
7294 || args
->front()->type()->is_error_type()
7295 || args
->front()->type()->is_undefined())
7297 this->set_is_error();
7303 // Check argument types for a builtin function.
7306 Builtin_call_expression::do_check_types(Gogo
*)
7308 switch (this->code_
)
7310 case BUILTIN_INVALID
:
7318 // The single argument may be either a string or an array or a
7319 // map or a channel, or a pointer to a closed array.
7320 if (this->check_one_arg())
7322 Type
* arg_type
= this->one_arg()->type();
7323 if (arg_type
->points_to() != NULL
7324 && arg_type
->points_to()->array_type() != NULL
7325 && !arg_type
->points_to()->is_open_array_type())
7326 arg_type
= arg_type
->points_to();
7327 if (this->code_
== BUILTIN_CAP
)
7329 if (!arg_type
->is_error_type()
7330 && arg_type
->array_type() == NULL
7331 && arg_type
->channel_type() == NULL
)
7332 this->report_error(_("argument must be array or slice "
7337 if (!arg_type
->is_error_type()
7338 && !arg_type
->is_string_type()
7339 && arg_type
->array_type() == NULL
7340 && arg_type
->map_type() == NULL
7341 && arg_type
->channel_type() == NULL
)
7342 this->report_error(_("argument must be string or "
7343 "array or slice or map or channel"));
7350 case BUILTIN_PRINTLN
:
7352 const Expression_list
* args
= this->args();
7355 if (this->code_
== BUILTIN_PRINT
)
7356 warning_at(this->location(), 0,
7357 "no arguments for builtin function %<%s%>",
7358 (this->code_
== BUILTIN_PRINT
7364 for (Expression_list::const_iterator p
= args
->begin();
7368 Type
* type
= (*p
)->type();
7369 if (type
->is_error_type()
7370 || type
->is_string_type()
7371 || type
->integer_type() != NULL
7372 || type
->float_type() != NULL
7373 || type
->complex_type() != NULL
7374 || type
->is_boolean_type()
7375 || type
->points_to() != NULL
7376 || type
->interface_type() != NULL
7377 || type
->channel_type() != NULL
7378 || type
->map_type() != NULL
7379 || type
->function_type() != NULL
7380 || type
->is_open_array_type())
7383 this->report_error(_("unsupported argument type to "
7384 "builtin function"));
7391 case BUILTIN_CLOSED
:
7392 if (this->check_one_arg())
7394 if (this->one_arg()->type()->channel_type() == NULL
)
7395 this->report_error(_("argument must be channel"));
7400 case BUILTIN_SIZEOF
:
7401 case BUILTIN_ALIGNOF
:
7402 this->check_one_arg();
7405 case BUILTIN_RECOVER
:
7406 if (this->args() != NULL
&& !this->args()->empty())
7407 this->report_error(_("too many arguments"));
7410 case BUILTIN_OFFSETOF
:
7411 if (this->check_one_arg())
7413 Expression
* arg
= this->one_arg();
7414 if (arg
->field_reference_expression() == NULL
)
7415 this->report_error(_("argument must be a field reference"));
7421 const Expression_list
* args
= this->args();
7422 if (args
== NULL
|| args
->size() < 2)
7424 this->report_error(_("not enough arguments"));
7427 else if (args
->size() > 2)
7429 this->report_error(_("too many arguments"));
7432 Type
* arg1_type
= args
->front()->type();
7433 Type
* arg2_type
= args
->back()->type();
7434 if (arg1_type
->is_error_type() || arg2_type
->is_error_type())
7438 if (arg1_type
->is_open_array_type())
7439 e1
= arg1_type
->array_type()->element_type();
7442 this->report_error(_("left argument must be a slice"));
7447 if (arg2_type
->is_open_array_type())
7448 e2
= arg2_type
->array_type()->element_type();
7449 else if (arg2_type
->is_string_type())
7450 e2
= Type::lookup_integer_type("uint8");
7453 this->report_error(_("right argument must be a slice or a string"));
7457 if (!Type::are_identical(e1
, e2
, true, NULL
))
7458 this->report_error(_("element types must be the same"));
7462 case BUILTIN_APPEND
:
7464 const Expression_list
* args
= this->args();
7465 if (args
== NULL
|| args
->size() < 2)
7467 this->report_error(_("not enough arguments"));
7470 if (args
->size() > 2)
7472 this->report_error(_("too many arguments"));
7476 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7480 this->report_error(_("arguments 1 and 2 have different types"));
7483 error_at(this->location(),
7484 "arguments 1 and 2 have different types (%s)",
7486 this->set_is_error();
7494 if (this->check_one_arg())
7496 if (this->one_arg()->type()->complex_type() == NULL
)
7497 this->report_error(_("argument must have complex type"));
7501 case BUILTIN_COMPLEX
:
7503 const Expression_list
* args
= this->args();
7504 if (args
== NULL
|| args
->size() < 2)
7505 this->report_error(_("not enough arguments"));
7506 else if (args
->size() > 2)
7507 this->report_error(_("too many arguments"));
7508 else if (args
->front()->is_error_expression()
7509 || args
->front()->type()->is_error_type()
7510 || args
->back()->is_error_expression()
7511 || args
->back()->type()->is_error_type())
7512 this->set_is_error();
7513 else if (!Type::are_identical(args
->front()->type(),
7514 args
->back()->type(), true, NULL
))
7515 this->report_error(_("complex arguments must have identical types"));
7516 else if (args
->front()->type()->float_type() == NULL
)
7517 this->report_error(_("complex arguments must have "
7518 "floating-point type"));
7527 // Return the tree for a builtin function.
7530 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7532 Gogo
* gogo
= context
->gogo();
7533 source_location location
= this->location();
7534 switch (this->code_
)
7536 case BUILTIN_INVALID
:
7544 const Expression_list
* args
= this->args();
7545 gcc_assert(args
!= NULL
&& args
->size() == 1);
7546 Expression
* arg
= *args
->begin();
7547 Type
* arg_type
= arg
->type();
7551 gcc_assert(saw_errors());
7552 return error_mark_node
;
7556 tree arg_tree
= arg
->get_tree(context
);
7558 this->seen_
= false;
7560 if (arg_tree
== error_mark_node
)
7561 return error_mark_node
;
7563 if (arg_type
->points_to() != NULL
)
7565 arg_type
= arg_type
->points_to();
7566 gcc_assert(arg_type
->array_type() != NULL
7567 && !arg_type
->is_open_array_type());
7568 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7569 arg_tree
= build_fold_indirect_ref(arg_tree
);
7573 if (this->code_
== BUILTIN_LEN
)
7575 if (arg_type
->is_string_type())
7576 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7577 else if (arg_type
->array_type() != NULL
)
7581 gcc_assert(saw_errors());
7582 return error_mark_node
;
7585 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7586 this->seen_
= false;
7588 else if (arg_type
->map_type() != NULL
)
7590 static tree map_len_fndecl
;
7591 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7596 arg_type
->get_tree(gogo
),
7599 else if (arg_type
->channel_type() != NULL
)
7601 static tree chan_len_fndecl
;
7602 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7607 arg_type
->get_tree(gogo
),
7615 if (arg_type
->array_type() != NULL
)
7619 gcc_assert(saw_errors());
7620 return error_mark_node
;
7623 val_tree
= arg_type
->array_type()->capacity_tree(gogo
,
7625 this->seen_
= false;
7627 else if (arg_type
->channel_type() != NULL
)
7629 static tree chan_cap_fndecl
;
7630 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7635 arg_type
->get_tree(gogo
),
7642 if (val_tree
== error_mark_node
)
7643 return error_mark_node
;
7645 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7646 if (type_tree
== TREE_TYPE(val_tree
))
7649 return fold(convert_to_integer(type_tree
, val_tree
));
7653 case BUILTIN_PRINTLN
:
7655 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7656 tree stmt_list
= NULL_TREE
;
7658 const Expression_list
* call_args
= this->args();
7659 if (call_args
!= NULL
)
7661 for (Expression_list::const_iterator p
= call_args
->begin();
7662 p
!= call_args
->end();
7665 if (is_ln
&& p
!= call_args
->begin())
7667 static tree print_space_fndecl
;
7668 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7673 if (call
== error_mark_node
)
7674 return error_mark_node
;
7675 append_to_statement_list(call
, &stmt_list
);
7678 Type
* type
= (*p
)->type();
7680 tree arg
= (*p
)->get_tree(context
);
7681 if (arg
== error_mark_node
)
7682 return error_mark_node
;
7686 if (type
->is_string_type())
7688 static tree print_string_fndecl
;
7689 pfndecl
= &print_string_fndecl
;
7690 fnname
= "__go_print_string";
7692 else if (type
->integer_type() != NULL
7693 && type
->integer_type()->is_unsigned())
7695 static tree print_uint64_fndecl
;
7696 pfndecl
= &print_uint64_fndecl
;
7697 fnname
= "__go_print_uint64";
7698 Type
* itype
= Type::lookup_integer_type("uint64");
7699 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7702 else if (type
->integer_type() != NULL
)
7704 static tree print_int64_fndecl
;
7705 pfndecl
= &print_int64_fndecl
;
7706 fnname
= "__go_print_int64";
7707 Type
* itype
= Type::lookup_integer_type("int64");
7708 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7711 else if (type
->float_type() != NULL
)
7713 static tree print_double_fndecl
;
7714 pfndecl
= &print_double_fndecl
;
7715 fnname
= "__go_print_double";
7716 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7718 else if (type
->complex_type() != NULL
)
7720 static tree print_complex_fndecl
;
7721 pfndecl
= &print_complex_fndecl
;
7722 fnname
= "__go_print_complex";
7723 arg
= fold_convert_loc(location
, complex_double_type_node
,
7726 else if (type
->is_boolean_type())
7728 static tree print_bool_fndecl
;
7729 pfndecl
= &print_bool_fndecl
;
7730 fnname
= "__go_print_bool";
7732 else if (type
->points_to() != NULL
7733 || type
->channel_type() != NULL
7734 || type
->map_type() != NULL
7735 || type
->function_type() != NULL
)
7737 static tree print_pointer_fndecl
;
7738 pfndecl
= &print_pointer_fndecl
;
7739 fnname
= "__go_print_pointer";
7740 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7742 else if (type
->interface_type() != NULL
)
7744 if (type
->interface_type()->is_empty())
7746 static tree print_empty_interface_fndecl
;
7747 pfndecl
= &print_empty_interface_fndecl
;
7748 fnname
= "__go_print_empty_interface";
7752 static tree print_interface_fndecl
;
7753 pfndecl
= &print_interface_fndecl
;
7754 fnname
= "__go_print_interface";
7757 else if (type
->is_open_array_type())
7759 static tree print_slice_fndecl
;
7760 pfndecl
= &print_slice_fndecl
;
7761 fnname
= "__go_print_slice";
7766 tree call
= Gogo::call_builtin(pfndecl
,
7773 if (call
== error_mark_node
)
7774 return error_mark_node
;
7775 append_to_statement_list(call
, &stmt_list
);
7781 static tree print_nl_fndecl
;
7782 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7787 if (call
== error_mark_node
)
7788 return error_mark_node
;
7789 append_to_statement_list(call
, &stmt_list
);
7797 const Expression_list
* args
= this->args();
7798 gcc_assert(args
!= NULL
&& args
->size() == 1);
7799 Expression
* arg
= args
->front();
7800 tree arg_tree
= arg
->get_tree(context
);
7801 if (arg_tree
== error_mark_node
)
7802 return error_mark_node
;
7803 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7804 arg_tree
= Expression::convert_for_assignment(context
, empty
,
7806 arg_tree
, location
);
7807 static tree panic_fndecl
;
7808 tree call
= Gogo::call_builtin(&panic_fndecl
,
7813 TREE_TYPE(arg_tree
),
7815 if (call
== error_mark_node
)
7816 return error_mark_node
;
7817 // This function will throw an exception.
7818 TREE_NOTHROW(panic_fndecl
) = 0;
7819 // This function will not return.
7820 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
7824 case BUILTIN_RECOVER
:
7826 // The argument is set when building recover thunks. It's a
7827 // boolean value which is true if we can recover a value now.
7828 const Expression_list
* args
= this->args();
7829 gcc_assert(args
!= NULL
&& args
->size() == 1);
7830 Expression
* arg
= args
->front();
7831 tree arg_tree
= arg
->get_tree(context
);
7832 if (arg_tree
== error_mark_node
)
7833 return error_mark_node
;
7835 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7836 tree empty_tree
= empty
->get_tree(context
->gogo());
7838 Type
* nil_type
= Type::make_nil_type();
7839 Expression
* nil
= Expression::make_nil(location
);
7840 tree nil_tree
= nil
->get_tree(context
);
7841 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
7847 // We need to handle a deferred call to recover specially,
7848 // because it changes whether it can recover a panic or not.
7849 // See test7 in test/recover1.go.
7851 if (this->is_deferred())
7853 static tree deferred_recover_fndecl
;
7854 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
7856 "__go_deferred_recover",
7862 static tree recover_fndecl
;
7863 call
= Gogo::call_builtin(&recover_fndecl
,
7869 if (call
== error_mark_node
)
7870 return error_mark_node
;
7871 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
7872 call
, empty_nil_tree
);
7876 case BUILTIN_CLOSED
:
7878 const Expression_list
* args
= this->args();
7879 gcc_assert(args
!= NULL
&& args
->size() == 1);
7880 Expression
* arg
= args
->front();
7881 tree arg_tree
= arg
->get_tree(context
);
7882 if (arg_tree
== error_mark_node
)
7883 return error_mark_node
;
7884 if (this->code_
== BUILTIN_CLOSE
)
7886 static tree close_fndecl
;
7887 return Gogo::call_builtin(&close_fndecl
,
7889 "__go_builtin_close",
7892 TREE_TYPE(arg_tree
),
7897 static tree closed_fndecl
;
7898 return Gogo::call_builtin(&closed_fndecl
,
7900 "__go_builtin_closed",
7903 TREE_TYPE(arg_tree
),
7908 case BUILTIN_SIZEOF
:
7909 case BUILTIN_OFFSETOF
:
7910 case BUILTIN_ALIGNOF
:
7915 bool b
= this->integer_constant_value(true, val
, &dummy
);
7917 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
7918 tree ret
= Expression::integer_constant_tree(val
, type
);
7925 const Expression_list
* args
= this->args();
7926 gcc_assert(args
!= NULL
&& args
->size() == 2);
7927 Expression
* arg1
= args
->front();
7928 Expression
* arg2
= args
->back();
7930 tree arg1_tree
= arg1
->get_tree(context
);
7931 tree arg2_tree
= arg2
->get_tree(context
);
7932 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7933 return error_mark_node
;
7935 Type
* arg1_type
= arg1
->type();
7936 Array_type
* at
= arg1_type
->array_type();
7937 arg1_tree
= save_expr(arg1_tree
);
7938 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
7939 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
7940 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
7941 return error_mark_node
;
7943 Type
* arg2_type
= arg2
->type();
7946 if (arg2_type
->is_open_array_type())
7948 at
= arg2_type
->array_type();
7949 arg2_tree
= save_expr(arg2_tree
);
7950 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7951 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7955 arg2_tree
= save_expr(arg2_tree
);
7956 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
7957 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
7959 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7960 return error_mark_node
;
7962 arg1_len
= save_expr(arg1_len
);
7963 arg2_len
= save_expr(arg2_len
);
7964 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
7965 fold_build2_loc(location
, LT_EXPR
,
7967 arg1_len
, arg2_len
),
7968 arg1_len
, arg2_len
);
7969 len
= save_expr(len
);
7971 Type
* element_type
= at
->element_type();
7972 tree element_type_tree
= element_type
->get_tree(gogo
);
7973 if (element_type_tree
== error_mark_node
)
7974 return error_mark_node
;
7975 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7976 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
7978 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
7979 TREE_TYPE(element_size
),
7980 bytecount
, element_size
);
7981 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
7983 arg1_val
= fold_convert_loc(location
, ptr_type_node
, arg1_val
);
7984 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7986 static tree copy_fndecl
;
7987 tree call
= Gogo::call_builtin(©_fndecl
,
7998 if (call
== error_mark_node
)
7999 return error_mark_node
;
8001 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
8005 case BUILTIN_APPEND
:
8007 const Expression_list
* args
= this->args();
8008 gcc_assert(args
!= NULL
&& args
->size() == 2);
8009 Expression
* arg1
= args
->front();
8010 Expression
* arg2
= args
->back();
8012 tree arg1_tree
= arg1
->get_tree(context
);
8013 tree arg2_tree
= arg2
->get_tree(context
);
8014 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
8015 return error_mark_node
;
8017 Array_type
* at
= arg1
->type()->array_type();
8018 Type
* element_type
= at
->element_type();
8020 arg2_tree
= Expression::convert_for_assignment(context
, at
,
8024 if (arg2_tree
== error_mark_node
)
8025 return error_mark_node
;
8027 arg2_tree
= save_expr(arg2_tree
);
8028 tree arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
8029 tree arg2_len
= at
->length_tree(gogo
, arg2_tree
);
8030 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
8031 return error_mark_node
;
8032 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
8033 arg2_len
= fold_convert_loc(location
, size_type_node
, arg2_len
);
8035 tree element_type_tree
= element_type
->get_tree(gogo
);
8036 if (element_type_tree
== error_mark_node
)
8037 return error_mark_node
;
8038 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
8039 element_size
= fold_convert_loc(location
, size_type_node
,
8042 // We rebuild the decl each time since the slice types may
8044 tree append_fndecl
= NULL_TREE
;
8045 return Gogo::call_builtin(&append_fndecl
,
8049 TREE_TYPE(arg1_tree
),
8050 TREE_TYPE(arg1_tree
),
8063 const Expression_list
* args
= this->args();
8064 gcc_assert(args
!= NULL
&& args
->size() == 1);
8065 Expression
* arg
= args
->front();
8066 tree arg_tree
= arg
->get_tree(context
);
8067 if (arg_tree
== error_mark_node
)
8068 return error_mark_node
;
8069 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
8070 if (this->code_
== BUILTIN_REAL
)
8071 return fold_build1_loc(location
, REALPART_EXPR
,
8072 TREE_TYPE(TREE_TYPE(arg_tree
)),
8075 return fold_build1_loc(location
, IMAGPART_EXPR
,
8076 TREE_TYPE(TREE_TYPE(arg_tree
)),
8080 case BUILTIN_COMPLEX
:
8082 const Expression_list
* args
= this->args();
8083 gcc_assert(args
!= NULL
&& args
->size() == 2);
8084 tree r
= args
->front()->get_tree(context
);
8085 tree i
= args
->back()->get_tree(context
);
8086 if (r
== error_mark_node
|| i
== error_mark_node
)
8087 return error_mark_node
;
8088 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
8089 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
8090 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
8091 return fold_build2_loc(location
, COMPLEX_EXPR
,
8092 build_complex_type(TREE_TYPE(r
)),
8101 // We have to support exporting a builtin call expression, because
8102 // code can set a constant to the result of a builtin expression.
8105 Builtin_call_expression::do_export(Export
* exp
) const
8112 if (this->integer_constant_value(true, val
, &dummy
))
8114 Integer_expression::export_integer(exp
, val
);
8123 if (this->float_constant_value(fval
, &dummy
))
8125 Float_expression::export_float(exp
, fval
);
8137 if (this->complex_constant_value(real
, imag
, &dummy
))
8139 Complex_expression::export_complex(exp
, real
, imag
);
8148 error_at(this->location(), "value is not constant");
8152 // A trailing space lets us reliably identify the end of the number.
8153 exp
->write_c_string(" ");
8156 // Class Call_expression.
8161 Call_expression::do_traverse(Traverse
* traverse
)
8163 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8164 return TRAVERSE_EXIT
;
8165 if (this->args_
!= NULL
)
8167 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8168 return TRAVERSE_EXIT
;
8170 return TRAVERSE_CONTINUE
;
8173 // Lower a call statement.
8176 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
8178 // A type case can look like a function call.
8179 if (this->fn_
->is_type_expression()
8180 && this->args_
!= NULL
8181 && this->args_
->size() == 1)
8182 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8185 // Recognize a call to a builtin function.
8186 Func_expression
* fne
= this->fn_
->func_expression();
8188 && fne
->named_object()->is_function_declaration()
8189 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
8190 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8191 this->is_varargs_
, this->location());
8193 // Handle an argument which is a call to a function which returns
8194 // multiple results.
8195 if (this->args_
!= NULL
8196 && this->args_
->size() == 1
8197 && this->args_
->front()->call_expression() != NULL
8198 && this->fn_
->type()->function_type() != NULL
)
8200 Function_type
* fntype
= this->fn_
->type()->function_type();
8201 size_t rc
= this->args_
->front()->call_expression()->result_count();
8203 && fntype
->parameters() != NULL
8204 && (fntype
->parameters()->size() == rc
8205 || (fntype
->is_varargs()
8206 && fntype
->parameters()->size() - 1 <= rc
)))
8208 Call_expression
* call
= this->args_
->front()->call_expression();
8209 Expression_list
* args
= new Expression_list
;
8210 for (size_t i
= 0; i
< rc
; ++i
)
8211 args
->push_back(Expression::make_call_result(call
, i
));
8212 // We can't return a new call expression here, because this
8213 // one may be referenced by Call_result expressions. FIXME.
8219 // Handle a call to a varargs function by packaging up the extra
8221 if (this->fn_
->type()->function_type() != NULL
8222 && this->fn_
->type()->function_type()->is_varargs())
8224 Function_type
* fntype
= this->fn_
->type()->function_type();
8225 const Typed_identifier_list
* parameters
= fntype
->parameters();
8226 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
8227 Type
* varargs_type
= parameters
->back().type();
8228 return this->lower_varargs(gogo
, function
, varargs_type
,
8229 parameters
->size());
8235 // Lower a call to a varargs function. FUNCTION is the function in
8236 // which the call occurs--it's not the function we are calling.
8237 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8238 // PARAM_COUNT is the number of parameters of the function we are
8239 // calling; the last of these parameters will be the varargs
8243 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8244 Type
* varargs_type
, size_t param_count
)
8246 if (this->varargs_are_lowered_
)
8249 source_location loc
= this->location();
8251 gcc_assert(param_count
> 0);
8252 gcc_assert(varargs_type
->is_open_array_type());
8254 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8255 if (arg_count
< param_count
- 1)
8257 // Not enough arguments; will be caught in check_types.
8261 Expression_list
* old_args
= this->args_
;
8262 Expression_list
* new_args
= new Expression_list();
8263 bool push_empty_arg
= false;
8264 if (old_args
== NULL
|| old_args
->empty())
8266 gcc_assert(param_count
== 1);
8267 push_empty_arg
= true;
8271 Expression_list::const_iterator pa
;
8273 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8275 if (static_cast<size_t>(i
) == param_count
)
8277 new_args
->push_back(*pa
);
8280 // We have reached the varargs parameter.
8282 bool issued_error
= false;
8283 if (pa
== old_args
->end())
8284 push_empty_arg
= true;
8285 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8286 new_args
->push_back(*pa
);
8287 else if (this->is_varargs_
)
8289 this->report_error(_("too many arguments"));
8292 else if (pa
+ 1 == old_args
->end()
8293 && this->is_compatible_varargs_argument(function
, *pa
,
8296 new_args
->push_back(*pa
);
8299 Type
* element_type
= varargs_type
->array_type()->element_type();
8300 Expression_list
* vals
= new Expression_list
;
8301 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8303 // Check types here so that we get a better message.
8304 Type
* patype
= (*pa
)->type();
8305 source_location paloc
= (*pa
)->location();
8306 if (!this->check_argument_type(i
, element_type
, patype
,
8307 paloc
, issued_error
))
8309 vals
->push_back(*pa
);
8312 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8313 new_args
->push_back(val
);
8318 new_args
->push_back(Expression::make_nil(loc
));
8320 // We can't return a new call expression here, because this one may
8321 // be referenced by Call_result expressions. FIXME.
8322 if (old_args
!= NULL
)
8324 this->args_
= new_args
;
8325 this->varargs_are_lowered_
= true;
8327 // Lower all the new subexpressions.
8328 Expression
* ret
= this;
8329 gogo
->lower_expression(function
, &ret
);
8330 gcc_assert(ret
== this);
8334 // Return true if ARG is a varargs argment which should be passed to
8335 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8336 // will be the last argument passed in the call, and PARAM_TYPE will
8337 // be the type of the last parameter of the varargs function being
8341 Call_expression::is_compatible_varargs_argument(Named_object
* function
,
8346 *issued_error
= false;
8348 Type
* var_type
= NULL
;
8350 // The simple case is passing the varargs parameter of the caller.
8351 Var_expression
* ve
= arg
->var_expression();
8352 if (ve
!= NULL
&& ve
->named_object()->is_variable())
8354 Variable
* var
= ve
->named_object()->var_value();
8355 if (var
->is_varargs_parameter())
8356 var_type
= var
->type();
8359 // The complex case is passing the varargs parameter of some
8360 // enclosing function. This will look like passing down *c.f where
8361 // c is the closure variable and f is a field in the closure.
8362 if (function
!= NULL
8363 && function
->func_value()->needs_closure()
8364 && arg
->classification() == EXPRESSION_UNARY
)
8366 Unary_expression
* ue
= static_cast<Unary_expression
*>(arg
);
8367 if (ue
->op() == OPERATOR_MULT
)
8369 Field_reference_expression
* fre
=
8370 ue
->operand()->deref()->field_reference_expression();
8373 Var_expression
* ve
= fre
->expr()->deref()->var_expression();
8376 Named_object
* no
= ve
->named_object();
8377 Function
* f
= function
->func_value();
8378 if (no
== f
->closure_var())
8380 // At this point we know that this indeed a
8381 // reference to some enclosing variable. Now we
8382 // need to figure out whether that variable is a
8383 // varargs parameter.
8384 Named_object
* enclosing
=
8385 f
->enclosing_var(fre
->field_index());
8386 Variable
* var
= enclosing
->var_value();
8387 if (var
->is_varargs_parameter())
8388 var_type
= var
->type();
8395 if (var_type
== NULL
)
8398 // We only match if the parameter is the same, with an identical
8400 Array_type
* var_at
= var_type
->array_type();
8401 gcc_assert(var_at
!= NULL
);
8402 Array_type
* param_at
= param_type
->array_type();
8403 if (param_at
!= NULL
8404 && Type::are_identical(var_at
->element_type(),
8405 param_at
->element_type(), true, NULL
))
8407 error_at(arg
->location(), "... mismatch: passing ...T as ...");
8408 *issued_error
= true;
8412 // Get the function type. Returns NULL if we don't know the type. If
8413 // this returns NULL, and if_ERROR is true, issues an error.
8416 Call_expression::get_function_type() const
8418 return this->fn_
->type()->function_type();
8421 // Return the number of values which this call will return.
8424 Call_expression::result_count() const
8426 const Function_type
* fntype
= this->get_function_type();
8429 if (fntype
->results() == NULL
)
8431 return fntype
->results()->size();
8434 // Return whether this is a call to the predeclared function recover.
8437 Call_expression::is_recover_call() const
8439 return this->do_is_recover_call();
8442 // Set the argument to the recover function.
8445 Call_expression::set_recover_arg(Expression
* arg
)
8447 this->do_set_recover_arg(arg
);
8450 // Virtual functions also implemented by Builtin_call_expression.
8453 Call_expression::do_is_recover_call() const
8459 Call_expression::do_set_recover_arg(Expression
*)
8467 Call_expression::do_type()
8469 if (this->type_
!= NULL
)
8473 Function_type
* fntype
= this->get_function_type();
8475 return Type::make_error_type();
8477 const Typed_identifier_list
* results
= fntype
->results();
8478 if (results
== NULL
)
8479 ret
= Type::make_void_type();
8480 else if (results
->size() == 1)
8481 ret
= results
->begin()->type();
8483 ret
= Type::make_call_multiple_result_type(this);
8490 // Determine types for a call expression. We can use the function
8491 // parameter types to set the types of the arguments.
8494 Call_expression::do_determine_type(const Type_context
*)
8496 this->fn_
->determine_type_no_context();
8497 Function_type
* fntype
= this->get_function_type();
8498 const Typed_identifier_list
* parameters
= NULL
;
8500 parameters
= fntype
->parameters();
8501 if (this->args_
!= NULL
)
8503 Typed_identifier_list::const_iterator pt
;
8504 if (parameters
!= NULL
)
8505 pt
= parameters
->begin();
8506 for (Expression_list::const_iterator pa
= this->args_
->begin();
8507 pa
!= this->args_
->end();
8510 if (parameters
!= NULL
&& pt
!= parameters
->end())
8512 Type_context
subcontext(pt
->type(), false);
8513 (*pa
)->determine_type(&subcontext
);
8517 (*pa
)->determine_type_no_context();
8522 // Check types for parameter I.
8525 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8526 const Type
* argument_type
,
8527 source_location argument_location
,
8531 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8536 error_at(argument_location
, "argument %d has incompatible type", i
);
8538 error_at(argument_location
,
8539 "argument %d has incompatible type (%s)",
8542 this->set_is_error();
8551 Call_expression::do_check_types(Gogo
*)
8553 Function_type
* fntype
= this->get_function_type();
8556 if (!this->fn_
->type()->is_error_type())
8557 this->report_error(_("expected function"));
8561 if (fntype
->is_method())
8563 // We don't support pointers to methods, so the function has to
8564 // be a bound method expression.
8565 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8568 this->report_error(_("method call without object"));
8571 Type
* first_arg_type
= bme
->first_argument()->type();
8572 if (first_arg_type
->points_to() == NULL
)
8574 // When passing a value, we need to check that we are
8575 // permitted to copy it.
8577 if (!Type::are_assignable(fntype
->receiver()->type(),
8578 first_arg_type
, &reason
))
8581 this->report_error(_("incompatible type for receiver"));
8584 error_at(this->location(),
8585 "incompatible type for receiver (%s)",
8587 this->set_is_error();
8593 // Note that varargs was handled by the lower_varargs() method, so
8594 // we don't have to worry about it here.
8596 const Typed_identifier_list
* parameters
= fntype
->parameters();
8597 if (this->args_
== NULL
)
8599 if (parameters
!= NULL
&& !parameters
->empty())
8600 this->report_error(_("not enough arguments"));
8602 else if (parameters
== NULL
)
8603 this->report_error(_("too many arguments"));
8607 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8608 for (Expression_list::const_iterator pa
= this->args_
->begin();
8609 pa
!= this->args_
->end();
8612 if (pt
== parameters
->end())
8614 this->report_error(_("too many arguments"));
8617 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8618 (*pa
)->location(), false);
8620 if (pt
!= parameters
->end())
8621 this->report_error(_("not enough arguments"));
8625 // Return whether we have to use a temporary variable to ensure that
8626 // we evaluate this call expression in order. If the call returns no
8627 // results then it will inevitably be executed last. If the call
8628 // returns more than one result then it will be used with Call_result
8629 // expressions. So we only have to use a temporary variable if the
8630 // call returns exactly one result.
8633 Call_expression::do_must_eval_in_order() const
8635 return this->result_count() == 1;
8638 // Get the function and the first argument to use when calling a bound
8642 Call_expression::bound_method_function(Translate_context
* context
,
8643 Bound_method_expression
* bound_method
,
8644 tree
* first_arg_ptr
)
8646 Expression
* first_argument
= bound_method
->first_argument();
8647 tree first_arg
= first_argument
->get_tree(context
);
8648 if (first_arg
== error_mark_node
)
8649 return error_mark_node
;
8651 // We always pass a pointer to the first argument when calling a
8653 if (first_argument
->type()->points_to() == NULL
)
8655 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8656 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8657 || DECL_P(first_arg
)
8658 || TREE_CODE(first_arg
) == INDIRECT_REF
8659 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8661 first_arg
= build_fold_addr_expr(first_arg
);
8662 if (DECL_P(first_arg
))
8663 TREE_ADDRESSABLE(first_arg
) = 1;
8667 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8668 get_name(first_arg
));
8669 DECL_IGNORED_P(tmp
) = 0;
8670 DECL_INITIAL(tmp
) = first_arg
;
8671 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8672 build1(DECL_EXPR
, void_type_node
, tmp
),
8673 build_fold_addr_expr(tmp
));
8674 TREE_ADDRESSABLE(tmp
) = 1;
8676 if (first_arg
== error_mark_node
)
8677 return error_mark_node
;
8680 Type
* fatype
= bound_method
->first_argument_type();
8683 if (fatype
->points_to() == NULL
)
8684 fatype
= Type::make_pointer_type(fatype
);
8685 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8686 if (first_arg
== error_mark_node
8687 || TREE_TYPE(first_arg
) == error_mark_node
)
8688 return error_mark_node
;
8691 *first_arg_ptr
= first_arg
;
8693 return bound_method
->method()->get_tree(context
);
8696 // Get the function and the first argument to use when calling an
8697 // interface method.
8700 Call_expression::interface_method_function(
8701 Translate_context
* context
,
8702 Interface_field_reference_expression
* interface_method
,
8703 tree
* first_arg_ptr
)
8705 tree expr
= interface_method
->expr()->get_tree(context
);
8706 if (expr
== error_mark_node
)
8707 return error_mark_node
;
8708 expr
= save_expr(expr
);
8709 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8710 if (first_arg
== error_mark_node
)
8711 return error_mark_node
;
8712 *first_arg_ptr
= first_arg
;
8713 return interface_method
->get_function_tree(context
, expr
);
8716 // Build the call expression.
8719 Call_expression::do_get_tree(Translate_context
* context
)
8721 if (this->tree_
!= NULL_TREE
)
8724 Function_type
* fntype
= this->get_function_type();
8726 return error_mark_node
;
8728 if (this->fn_
->is_error_expression())
8729 return error_mark_node
;
8731 Gogo
* gogo
= context
->gogo();
8732 source_location location
= this->location();
8734 Func_expression
* func
= this->fn_
->func_expression();
8735 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8736 Interface_field_reference_expression
* interface_method
=
8737 this->fn_
->interface_field_reference_expression();
8738 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8739 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8740 gcc_assert(!fntype
->is_method() || is_method
);
8744 if (this->args_
== NULL
|| this->args_
->empty())
8746 nargs
= is_method
? 1 : 0;
8747 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8751 const Typed_identifier_list
* params
= fntype
->parameters();
8752 gcc_assert(params
!= NULL
);
8754 nargs
= this->args_
->size();
8755 int i
= is_method
? 1 : 0;
8757 args
= new tree
[nargs
];
8759 Typed_identifier_list::const_iterator pp
= params
->begin();
8760 Expression_list::const_iterator pe
;
8761 for (pe
= this->args_
->begin();
8762 pe
!= this->args_
->end();
8765 gcc_assert(pp
!= params
->end());
8766 tree arg_val
= (*pe
)->get_tree(context
);
8767 args
[i
] = Expression::convert_for_assignment(context
,
8772 if (args
[i
] == error_mark_node
)
8775 return error_mark_node
;
8778 gcc_assert(pp
== params
->end());
8779 gcc_assert(i
== nargs
);
8782 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8783 if (rettype
== error_mark_node
)
8786 return error_mark_node
;
8791 fn
= func
->get_tree_without_closure(gogo
);
8792 else if (!is_method
)
8793 fn
= this->fn_
->get_tree(context
);
8794 else if (bound_method
!= NULL
)
8795 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8796 else if (interface_method
!= NULL
)
8797 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8801 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8804 return error_mark_node
;
8807 // This is to support builtin math functions when using 80387 math.
8809 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8810 fndecl
= TREE_OPERAND(fndecl
, 0);
8811 tree excess_type
= NULL_TREE
;
8813 && DECL_IS_BUILTIN(fndecl
)
8814 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8816 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8817 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8818 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8819 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8821 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8822 if (excess_type
!= NULL_TREE
)
8824 tree excess_fndecl
= mathfn_built_in(excess_type
,
8825 DECL_FUNCTION_CODE(fndecl
));
8826 if (excess_fndecl
== NULL_TREE
)
8827 excess_type
= NULL_TREE
;
8830 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8831 for (int i
= 0; i
< nargs
; ++i
)
8832 args
[i
] = ::convert(excess_type
, args
[i
]);
8837 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8841 SET_EXPR_LOCATION(ret
, location
);
8845 tree closure_tree
= func
->closure()->get_tree(context
);
8846 if (closure_tree
!= error_mark_node
)
8847 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8850 // If this is a recursive function type which returns itself, as in
8852 // we have used ptr_type_node for the return type. Add a cast here
8853 // to the correct type.
8854 if (TREE_TYPE(ret
) == ptr_type_node
)
8856 tree t
= this->type()->get_tree(gogo
);
8857 ret
= fold_convert_loc(location
, t
, ret
);
8860 if (excess_type
!= NULL_TREE
)
8862 // Calling convert here can undo our excess precision change.
8863 // That may or may not be a bug in convert_to_real.
8864 ret
= build1(NOP_EXPR
, rettype
, ret
);
8867 // If there is more than one result, we will refer to the call
8869 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
8870 ret
= save_expr(ret
);
8877 // Make a call expression.
8880 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
8881 source_location location
)
8883 return new Call_expression(fn
, args
, is_varargs
, location
);
8886 // A single result from a call which returns multiple results.
8888 class Call_result_expression
: public Expression
8891 Call_result_expression(Call_expression
* call
, unsigned int index
)
8892 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
8893 call_(call
), index_(index
)
8898 do_traverse(Traverse
*);
8904 do_determine_type(const Type_context
*);
8907 do_check_types(Gogo
*);
8912 return new Call_result_expression(this->call_
->call_expression(),
8917 do_must_eval_in_order() const
8921 do_get_tree(Translate_context
*);
8924 // The underlying call expression.
8926 // Which result we want.
8927 unsigned int index_
;
8930 // Traverse a call result.
8933 Call_result_expression::do_traverse(Traverse
* traverse
)
8935 if (traverse
->remember_expression(this->call_
))
8937 // We have already traversed the call expression.
8938 return TRAVERSE_CONTINUE
;
8940 return Expression::traverse(&this->call_
, traverse
);
8946 Call_result_expression::do_type()
8948 if (this->classification() == EXPRESSION_ERROR
)
8949 return Type::make_error_type();
8951 // THIS->CALL_ can be replaced with a temporary reference due to
8952 // Call_expression::do_must_eval_in_order when there is an error.
8953 Call_expression
* ce
= this->call_
->call_expression();
8956 this->set_is_error();
8957 return Type::make_error_type();
8959 Function_type
* fntype
= ce
->get_function_type();
8962 this->set_is_error();
8963 return Type::make_error_type();
8965 const Typed_identifier_list
* results
= fntype
->results();
8966 if (results
== NULL
)
8968 this->report_error(_("number of results does not match "
8969 "number of values"));
8970 return Type::make_error_type();
8972 Typed_identifier_list::const_iterator pr
= results
->begin();
8973 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8975 if (pr
== results
->end())
8979 if (pr
== results
->end())
8981 this->report_error(_("number of results does not match "
8982 "number of values"));
8983 return Type::make_error_type();
8988 // Check the type. Just make sure that we trigger the warning in
8992 Call_result_expression::do_check_types(Gogo
*)
8997 // Determine the type. We have nothing to do here, but the 0 result
8998 // needs to pass down to the caller.
9001 Call_result_expression::do_determine_type(const Type_context
*)
9003 if (this->index_
== 0)
9004 this->call_
->determine_type_no_context();
9010 Call_result_expression::do_get_tree(Translate_context
* context
)
9012 tree call_tree
= this->call_
->get_tree(context
);
9013 if (call_tree
== error_mark_node
)
9014 return error_mark_node
;
9015 if (TREE_CODE(TREE_TYPE(call_tree
)) != RECORD_TYPE
)
9017 gcc_assert(saw_errors());
9018 return error_mark_node
;
9020 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
9021 for (unsigned int i
= 0; i
< this->index_
; ++i
)
9023 gcc_assert(field
!= NULL_TREE
);
9024 field
= DECL_CHAIN(field
);
9026 gcc_assert(field
!= NULL_TREE
);
9027 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
9030 // Make a reference to a single result of a call which returns
9031 // multiple results.
9034 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
9036 return new Call_result_expression(call
, index
);
9039 // Class Index_expression.
9044 Index_expression::do_traverse(Traverse
* traverse
)
9046 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
9047 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
9048 || (this->end_
!= NULL
9049 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
9050 return TRAVERSE_EXIT
;
9051 return TRAVERSE_CONTINUE
;
9054 // Lower an index expression. This converts the generic index
9055 // expression into an array index, a string index, or a map index.
9058 Index_expression::do_lower(Gogo
*, Named_object
*, int)
9060 source_location location
= this->location();
9061 Expression
* left
= this->left_
;
9062 Expression
* start
= this->start_
;
9063 Expression
* end
= this->end_
;
9065 Type
* type
= left
->type();
9066 if (type
->is_error_type())
9067 return Expression::make_error(location
);
9068 else if (type
->array_type() != NULL
)
9069 return Expression::make_array_index(left
, start
, end
, location
);
9070 else if (type
->points_to() != NULL
9071 && type
->points_to()->array_type() != NULL
9072 && !type
->points_to()->is_open_array_type())
9074 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
9076 return Expression::make_array_index(deref
, start
, end
, location
);
9078 else if (type
->is_string_type())
9079 return Expression::make_string_index(left
, start
, end
, location
);
9080 else if (type
->map_type() != NULL
)
9084 error_at(location
, "invalid slice of map");
9085 return Expression::make_error(location
);
9087 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
9089 if (this->is_lvalue_
)
9090 ret
->set_is_lvalue();
9096 "attempt to index object which is not array, string, or map");
9097 return Expression::make_error(location
);
9101 // Make an index expression.
9104 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
9105 source_location location
)
9107 return new Index_expression(left
, start
, end
, location
);
9110 // An array index. This is used for both indexing and slicing.
9112 class Array_index_expression
: public Expression
9115 Array_index_expression(Expression
* array
, Expression
* start
,
9116 Expression
* end
, source_location location
)
9117 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
9118 array_(array
), start_(start
), end_(end
), type_(NULL
)
9123 do_traverse(Traverse
*);
9129 do_determine_type(const Type_context
*);
9132 do_check_types(Gogo
*);
9137 return Expression::make_array_index(this->array_
->copy(),
9138 this->start_
->copy(),
9141 : this->end_
->copy()),
9146 do_is_addressable() const;
9149 do_address_taken(bool escapes
)
9150 { this->array_
->address_taken(escapes
); }
9153 do_get_tree(Translate_context
*);
9156 // The array we are getting a value from.
9158 // The start or only index.
9160 // The end index of a slice. This may be NULL for a simple array
9161 // index, or it may be a nil expression for the length of the array.
9163 // The type of the expression.
9167 // Array index traversal.
9170 Array_index_expression::do_traverse(Traverse
* traverse
)
9172 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9173 return TRAVERSE_EXIT
;
9174 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9175 return TRAVERSE_EXIT
;
9176 if (this->end_
!= NULL
)
9178 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9179 return TRAVERSE_EXIT
;
9181 return TRAVERSE_CONTINUE
;
9184 // Return the type of an array index.
9187 Array_index_expression::do_type()
9189 if (this->type_
== NULL
)
9191 Array_type
* type
= this->array_
->type()->array_type();
9193 this->type_
= Type::make_error_type();
9194 else if (this->end_
== NULL
)
9195 this->type_
= type
->element_type();
9196 else if (type
->is_open_array_type())
9198 // A slice of a slice has the same type as the original
9200 this->type_
= this->array_
->type()->deref();
9204 // A slice of an array is a slice.
9205 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9211 // Set the type of an array index.
9214 Array_index_expression::do_determine_type(const Type_context
*)
9216 this->array_
->determine_type_no_context();
9217 Type_context
subcontext(NULL
, true);
9218 this->start_
->determine_type(&subcontext
);
9219 if (this->end_
!= NULL
)
9220 this->end_
->determine_type(&subcontext
);
9223 // Check types of an array index.
9226 Array_index_expression::do_check_types(Gogo
*)
9228 if (this->start_
->type()->integer_type() == NULL
)
9229 this->report_error(_("index must be integer"));
9230 if (this->end_
!= NULL
9231 && this->end_
->type()->integer_type() == NULL
9232 && !this->end_
->is_nil_expression())
9233 this->report_error(_("slice end must be integer"));
9235 Array_type
* array_type
= this->array_
->type()->array_type();
9236 if (array_type
== NULL
)
9238 gcc_assert(this->array_
->type()->is_error_type());
9242 unsigned int int_bits
=
9243 Type::lookup_integer_type("int")->integer_type()->bits();
9248 bool lval_valid
= (array_type
->length() != NULL
9249 && array_type
->length()->integer_constant_value(true,
9254 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9256 if (mpz_sgn(ival
) < 0
9257 || mpz_sizeinbase(ival
, 2) >= int_bits
9259 && (this->end_
== NULL
9260 ? mpz_cmp(ival
, lval
) >= 0
9261 : mpz_cmp(ival
, lval
) > 0)))
9263 error_at(this->start_
->location(), "array index out of bounds");
9264 this->set_is_error();
9267 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9269 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9271 if (mpz_sgn(ival
) < 0
9272 || mpz_sizeinbase(ival
, 2) >= int_bits
9273 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
9275 error_at(this->end_
->location(), "array index out of bounds");
9276 this->set_is_error();
9283 // A slice of an array requires an addressable array. A slice of a
9284 // slice is always possible.
9285 if (this->end_
!= NULL
9286 && !array_type
->is_open_array_type()
9287 && !this->array_
->is_addressable())
9288 this->report_error(_("array is not addressable"));
9291 // Return whether this expression is addressable.
9294 Array_index_expression::do_is_addressable() const
9296 // A slice expression is not addressable.
9297 if (this->end_
!= NULL
)
9300 // An index into a slice is addressable.
9301 if (this->array_
->type()->is_open_array_type())
9304 // An index into an array is addressable if the array is
9306 return this->array_
->is_addressable();
9309 // Get a tree for an array index.
9312 Array_index_expression::do_get_tree(Translate_context
* context
)
9314 Gogo
* gogo
= context
->gogo();
9315 source_location loc
= this->location();
9317 Array_type
* array_type
= this->array_
->type()->array_type();
9318 if (array_type
== NULL
)
9320 gcc_assert(this->array_
->type()->is_error_type());
9321 return error_mark_node
;
9324 tree type_tree
= array_type
->get_tree(gogo
);
9325 if (type_tree
== error_mark_node
)
9326 return error_mark_node
;
9328 tree array_tree
= this->array_
->get_tree(context
);
9329 if (array_tree
== error_mark_node
)
9330 return error_mark_node
;
9332 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9333 array_tree
= save_expr(array_tree
);
9334 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
9335 if (length_tree
== error_mark_node
)
9336 return error_mark_node
;
9337 length_tree
= save_expr(length_tree
);
9338 tree length_type
= TREE_TYPE(length_tree
);
9340 tree bad_index
= boolean_false_node
;
9342 tree start_tree
= this->start_
->get_tree(context
);
9343 if (start_tree
== error_mark_node
)
9344 return error_mark_node
;
9345 if (!DECL_P(start_tree
))
9346 start_tree
= save_expr(start_tree
);
9347 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9348 start_tree
= convert_to_integer(length_type
, start_tree
);
9350 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9353 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9354 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9355 fold_build2_loc(loc
,
9359 boolean_type_node
, start_tree
,
9362 int code
= (array_type
->length() != NULL
9363 ? (this->end_
== NULL
9364 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9365 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9366 : (this->end_
== NULL
9367 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9368 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9369 tree crash
= Gogo::runtime_error(code
, loc
);
9371 if (this->end_
== NULL
)
9373 // Simple array indexing. This has to return an l-value, so
9374 // wrap the index check into START_TREE.
9375 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9376 build3(COND_EXPR
, void_type_node
,
9377 bad_index
, crash
, NULL_TREE
),
9379 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9381 if (array_type
->length() != NULL
)
9384 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9385 start_tree
, NULL_TREE
, NULL_TREE
);
9390 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9391 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9392 if (element_type_tree
== error_mark_node
)
9393 return error_mark_node
;
9394 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9395 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9396 start_tree
, element_size
);
9397 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9398 TREE_TYPE(values
), values
, offset
);
9399 return build_fold_indirect_ref(ptr
);
9405 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9406 if (capacity_tree
== error_mark_node
)
9407 return error_mark_node
;
9408 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9411 if (this->end_
->is_nil_expression())
9412 end_tree
= length_tree
;
9415 end_tree
= this->end_
->get_tree(context
);
9416 if (end_tree
== error_mark_node
)
9417 return error_mark_node
;
9418 if (!DECL_P(end_tree
))
9419 end_tree
= save_expr(end_tree
);
9420 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9421 end_tree
= convert_to_integer(length_type
, end_tree
);
9423 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9426 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9428 capacity_tree
= save_expr(capacity_tree
);
9429 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9430 fold_build2_loc(loc
, LT_EXPR
,
9432 end_tree
, start_tree
),
9433 fold_build2_loc(loc
, GT_EXPR
,
9435 end_tree
, capacity_tree
));
9436 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9437 bad_index
, bad_end
);
9440 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9441 if (element_type_tree
== error_mark_node
)
9442 return error_mark_node
;
9443 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9445 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9446 fold_convert_loc(loc
, sizetype
, start_tree
),
9449 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9450 if (value_pointer
== error_mark_node
)
9451 return error_mark_node
;
9453 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9454 TREE_TYPE(value_pointer
),
9455 value_pointer
, offset
);
9457 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9458 end_tree
, start_tree
);
9460 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9461 capacity_tree
, start_tree
);
9463 tree struct_tree
= this->type()->get_tree(gogo
);
9464 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9466 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9468 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9469 tree field
= TYPE_FIELDS(struct_tree
);
9470 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9472 elt
->value
= value_pointer
;
9474 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9475 field
= DECL_CHAIN(field
);
9476 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9478 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9480 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9481 field
= DECL_CHAIN(field
);
9482 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9484 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9486 tree constructor
= build_constructor(struct_tree
, init
);
9488 if (TREE_CONSTANT(value_pointer
)
9489 && TREE_CONSTANT(result_length_tree
)
9490 && TREE_CONSTANT(result_capacity_tree
))
9491 TREE_CONSTANT(constructor
) = 1;
9493 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9494 build3(COND_EXPR
, void_type_node
,
9495 bad_index
, crash
, NULL_TREE
),
9499 // Make an array index expression. END may be NULL.
9502 Expression::make_array_index(Expression
* array
, Expression
* start
,
9503 Expression
* end
, source_location location
)
9505 // Taking a slice of a composite literal requires moving the literal
9507 if (end
!= NULL
&& array
->is_composite_literal())
9509 array
= Expression::make_heap_composite(array
, location
);
9510 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9512 return new Array_index_expression(array
, start
, end
, location
);
9515 // A string index. This is used for both indexing and slicing.
9517 class String_index_expression
: public Expression
9520 String_index_expression(Expression
* string
, Expression
* start
,
9521 Expression
* end
, source_location location
)
9522 : Expression(EXPRESSION_STRING_INDEX
, location
),
9523 string_(string
), start_(start
), end_(end
)
9528 do_traverse(Traverse
*);
9534 do_determine_type(const Type_context
*);
9537 do_check_types(Gogo
*);
9542 return Expression::make_string_index(this->string_
->copy(),
9543 this->start_
->copy(),
9546 : this->end_
->copy()),
9551 do_get_tree(Translate_context
*);
9554 // The string we are getting a value from.
9555 Expression
* string_
;
9556 // The start or only index.
9558 // The end index of a slice. This may be NULL for a single index,
9559 // or it may be a nil expression for the length of the string.
9563 // String index traversal.
9566 String_index_expression::do_traverse(Traverse
* traverse
)
9568 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9569 return TRAVERSE_EXIT
;
9570 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9571 return TRAVERSE_EXIT
;
9572 if (this->end_
!= NULL
)
9574 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9575 return TRAVERSE_EXIT
;
9577 return TRAVERSE_CONTINUE
;
9580 // Return the type of a string index.
9583 String_index_expression::do_type()
9585 if (this->end_
== NULL
)
9586 return Type::lookup_integer_type("uint8");
9588 return this->string_
->type();
9591 // Determine the type of a string index.
9594 String_index_expression::do_determine_type(const Type_context
*)
9596 this->string_
->determine_type_no_context();
9597 Type_context
subcontext(NULL
, true);
9598 this->start_
->determine_type(&subcontext
);
9599 if (this->end_
!= NULL
)
9600 this->end_
->determine_type(&subcontext
);
9603 // Check types of a string index.
9606 String_index_expression::do_check_types(Gogo
*)
9608 if (this->start_
->type()->integer_type() == NULL
)
9609 this->report_error(_("index must be integer"));
9610 if (this->end_
!= NULL
9611 && this->end_
->type()->integer_type() == NULL
9612 && !this->end_
->is_nil_expression())
9613 this->report_error(_("slice end must be integer"));
9616 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9621 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9623 if (mpz_sgn(ival
) < 0
9624 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9626 error_at(this->start_
->location(), "string index out of bounds");
9627 this->set_is_error();
9630 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9632 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9634 if (mpz_sgn(ival
) < 0
9635 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9637 error_at(this->end_
->location(), "string index out of bounds");
9638 this->set_is_error();
9645 // Get a tree for a string index.
9648 String_index_expression::do_get_tree(Translate_context
* context
)
9650 source_location loc
= this->location();
9652 tree string_tree
= this->string_
->get_tree(context
);
9653 if (string_tree
== error_mark_node
)
9654 return error_mark_node
;
9656 if (this->string_
->type()->points_to() != NULL
)
9657 string_tree
= build_fold_indirect_ref(string_tree
);
9658 if (!DECL_P(string_tree
))
9659 string_tree
= save_expr(string_tree
);
9660 tree string_type
= TREE_TYPE(string_tree
);
9662 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9663 length_tree
= save_expr(length_tree
);
9664 tree length_type
= TREE_TYPE(length_tree
);
9666 tree bad_index
= boolean_false_node
;
9668 tree start_tree
= this->start_
->get_tree(context
);
9669 if (start_tree
== error_mark_node
)
9670 return error_mark_node
;
9671 if (!DECL_P(start_tree
))
9672 start_tree
= save_expr(start_tree
);
9673 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9674 start_tree
= convert_to_integer(length_type
, start_tree
);
9676 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9679 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9681 int code
= (this->end_
== NULL
9682 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9683 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9684 tree crash
= Gogo::runtime_error(code
, loc
);
9686 if (this->end_
== NULL
)
9688 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9690 fold_build2_loc(loc
, GE_EXPR
,
9692 start_tree
, length_tree
));
9694 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9695 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9697 fold_convert_loc(loc
, sizetype
, start_tree
));
9698 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9700 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9701 build3(COND_EXPR
, void_type_node
,
9702 bad_index
, crash
, NULL_TREE
),
9708 if (this->end_
->is_nil_expression())
9709 end_tree
= build_int_cst(length_type
, -1);
9712 end_tree
= this->end_
->get_tree(context
);
9713 if (end_tree
== error_mark_node
)
9714 return error_mark_node
;
9715 if (!DECL_P(end_tree
))
9716 end_tree
= save_expr(end_tree
);
9717 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9718 end_tree
= convert_to_integer(length_type
, end_tree
);
9720 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9723 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9726 static tree strslice_fndecl
;
9727 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9729 "__go_string_slice",
9738 if (ret
== error_mark_node
)
9739 return error_mark_node
;
9740 // This will panic if the bounds are out of range for the
9742 TREE_NOTHROW(strslice_fndecl
) = 0;
9744 if (bad_index
== boolean_false_node
)
9747 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9748 build3(COND_EXPR
, void_type_node
,
9749 bad_index
, crash
, NULL_TREE
),
9754 // Make a string index expression. END may be NULL.
9757 Expression::make_string_index(Expression
* string
, Expression
* start
,
9758 Expression
* end
, source_location location
)
9760 return new String_index_expression(string
, start
, end
, location
);
9765 // Get the type of the map.
9768 Map_index_expression::get_map_type() const
9770 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9772 gcc_assert(saw_errors());
9776 // Map index traversal.
9779 Map_index_expression::do_traverse(Traverse
* traverse
)
9781 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9782 return TRAVERSE_EXIT
;
9783 return Expression::traverse(&this->index_
, traverse
);
9786 // Return the type of a map index.
9789 Map_index_expression::do_type()
9791 Map_type
* mt
= this->get_map_type();
9793 return Type::make_error_type();
9794 Type
* type
= mt
->val_type();
9795 // If this map index is in a tuple assignment, we actually return a
9796 // pointer to the value type. Tuple_map_assignment_statement is
9797 // responsible for handling this correctly. We need to get the type
9798 // right in case this gets assigned to a temporary variable.
9799 if (this->is_in_tuple_assignment_
)
9800 type
= Type::make_pointer_type(type
);
9804 // Fix the type of a map index.
9807 Map_index_expression::do_determine_type(const Type_context
*)
9809 this->map_
->determine_type_no_context();
9810 Map_type
* mt
= this->get_map_type();
9811 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
9812 Type_context
subcontext(key_type
, false);
9813 this->index_
->determine_type(&subcontext
);
9816 // Check types of a map index.
9819 Map_index_expression::do_check_types(Gogo
*)
9822 Map_type
* mt
= this->get_map_type();
9825 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
9828 this->report_error(_("incompatible type for map index"));
9831 error_at(this->location(), "incompatible type for map index (%s)",
9833 this->set_is_error();
9838 // Get a tree for a map index.
9841 Map_index_expression::do_get_tree(Translate_context
* context
)
9843 Map_type
* type
= this->get_map_type();
9845 return error_mark_node
;
9847 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9848 if (valptr
== error_mark_node
)
9849 return error_mark_node
;
9850 valptr
= save_expr(valptr
);
9852 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
9854 if (this->is_lvalue_
)
9855 return build_fold_indirect_ref(valptr
);
9856 else if (this->is_in_tuple_assignment_
)
9858 // Tuple_map_assignment_statement is responsible for using this
9864 return fold_build3(COND_EXPR
, val_type_tree
,
9865 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
9866 fold_convert(TREE_TYPE(valptr
),
9867 null_pointer_node
)),
9868 type
->val_type()->get_init_tree(context
->gogo(),
9870 build_fold_indirect_ref(valptr
));
9874 // Get a tree for the map index. This returns a tree which evaluates
9875 // to a pointer to a value. The pointer will be NULL if the key is
9879 Map_index_expression::get_value_pointer(Translate_context
* context
,
9882 Map_type
* type
= this->get_map_type();
9884 return error_mark_node
;
9886 tree map_tree
= this->map_
->get_tree(context
);
9887 tree index_tree
= this->index_
->get_tree(context
);
9888 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
9889 this->index_
->type(),
9892 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
9893 return error_mark_node
;
9895 if (this->map_
->type()->points_to() != NULL
)
9896 map_tree
= build_fold_indirect_ref(map_tree
);
9898 // We need to pass in a pointer to the key, so stuff it into a
9900 tree tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
9901 DECL_IGNORED_P(tmp
) = 0;
9902 DECL_INITIAL(tmp
) = index_tree
;
9903 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
9904 tree tmpref
= fold_convert(const_ptr_type_node
, build_fold_addr_expr(tmp
));
9905 TREE_ADDRESSABLE(tmp
) = 1;
9907 static tree map_index_fndecl
;
9908 tree call
= Gogo::call_builtin(&map_index_fndecl
,
9912 const_ptr_type_node
,
9913 TREE_TYPE(map_tree
),
9915 const_ptr_type_node
,
9920 : boolean_false_node
));
9921 if (call
== error_mark_node
)
9922 return error_mark_node
;
9923 // This can panic on a map of interface type if the interface holds
9924 // an uncomparable or unhashable type.
9925 TREE_NOTHROW(map_index_fndecl
) = 0;
9927 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
9928 if (val_type_tree
== error_mark_node
)
9929 return error_mark_node
;
9930 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
9932 return build2(COMPOUND_EXPR
, ptr_val_type_tree
,
9934 fold_convert(ptr_val_type_tree
, call
));
9937 // Make a map index expression.
9939 Map_index_expression
*
9940 Expression::make_map_index(Expression
* map
, Expression
* index
,
9941 source_location location
)
9943 return new Map_index_expression(map
, index
, location
);
9946 // Class Field_reference_expression.
9948 // Return the type of a field reference.
9951 Field_reference_expression::do_type()
9953 Type
* type
= this->expr_
->type();
9954 if (type
->is_error_type())
9956 Struct_type
* struct_type
= type
->struct_type();
9957 gcc_assert(struct_type
!= NULL
);
9958 return struct_type
->field(this->field_index_
)->type();
9961 // Check the types for a field reference.
9964 Field_reference_expression::do_check_types(Gogo
*)
9966 Type
* type
= this->expr_
->type();
9967 if (type
->is_error_type())
9969 Struct_type
* struct_type
= type
->struct_type();
9970 gcc_assert(struct_type
!= NULL
);
9971 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
9974 // Get a tree for a field reference.
9977 Field_reference_expression::do_get_tree(Translate_context
* context
)
9979 tree struct_tree
= this->expr_
->get_tree(context
);
9980 if (struct_tree
== error_mark_node
9981 || TREE_TYPE(struct_tree
) == error_mark_node
)
9982 return error_mark_node
;
9983 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
9984 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
9985 if (field
== NULL_TREE
)
9987 // This can happen for a type which refers to itself indirectly
9988 // and then turns out to be erroneous.
9989 gcc_assert(saw_errors());
9990 return error_mark_node
;
9992 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
9994 field
= DECL_CHAIN(field
);
9995 gcc_assert(field
!= NULL_TREE
);
9997 if (TREE_TYPE(field
) == error_mark_node
)
9998 return error_mark_node
;
9999 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
10003 // Make a reference to a qualified identifier in an expression.
10005 Field_reference_expression
*
10006 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
10007 source_location location
)
10009 return new Field_reference_expression(expr
, field_index
, location
);
10012 // Class Interface_field_reference_expression.
10014 // Return a tree for the pointer to the function to call.
10017 Interface_field_reference_expression::get_function_tree(Translate_context
*,
10020 if (this->expr_
->type()->points_to() != NULL
)
10021 expr
= build_fold_indirect_ref(expr
);
10023 tree expr_type
= TREE_TYPE(expr
);
10024 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10026 tree field
= TYPE_FIELDS(expr_type
);
10027 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
10029 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10030 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
10032 table
= build_fold_indirect_ref(table
);
10033 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
10035 std::string name
= Gogo::unpack_hidden_name(this->name_
);
10036 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
10037 field
!= NULL_TREE
;
10038 field
= DECL_CHAIN(field
))
10040 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
10043 gcc_assert(field
!= NULL_TREE
);
10045 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
10048 // Return a tree for the first argument to pass to the interface
10052 Interface_field_reference_expression::get_underlying_object_tree(
10053 Translate_context
*,
10056 if (this->expr_
->type()->points_to() != NULL
)
10057 expr
= build_fold_indirect_ref(expr
);
10059 tree expr_type
= TREE_TYPE(expr
);
10060 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10062 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
10063 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
10065 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10071 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
10073 return Expression::traverse(&this->expr_
, traverse
);
10076 // Return the type of an interface field reference.
10079 Interface_field_reference_expression::do_type()
10081 Type
* expr_type
= this->expr_
->type();
10083 Type
* points_to
= expr_type
->points_to();
10084 if (points_to
!= NULL
)
10085 expr_type
= points_to
;
10087 Interface_type
* interface_type
= expr_type
->interface_type();
10088 if (interface_type
== NULL
)
10089 return Type::make_error_type();
10091 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
10092 if (method
== NULL
)
10093 return Type::make_error_type();
10095 return method
->type();
10098 // Determine types.
10101 Interface_field_reference_expression::do_determine_type(const Type_context
*)
10103 this->expr_
->determine_type_no_context();
10106 // Check the types for an interface field reference.
10109 Interface_field_reference_expression::do_check_types(Gogo
*)
10111 Type
* type
= this->expr_
->type();
10113 Type
* points_to
= type
->points_to();
10114 if (points_to
!= NULL
)
10117 Interface_type
* interface_type
= type
->interface_type();
10118 if (interface_type
== NULL
)
10119 this->report_error(_("expected interface or pointer to interface"));
10122 const Typed_identifier
* method
=
10123 interface_type
->find_method(this->name_
);
10124 if (method
== NULL
)
10126 error_at(this->location(), "method %qs not in interface",
10127 Gogo::message_name(this->name_
).c_str());
10128 this->set_is_error();
10133 // Get a tree for a reference to a field in an interface. There is no
10134 // standard tree type representation for this: it's a function
10135 // attached to its first argument, like a Bound_method_expression.
10136 // The only places it may currently be used are in a Call_expression
10137 // or a Go_statement, which will take it apart directly. So this has
10138 // nothing to do at present.
10141 Interface_field_reference_expression::do_get_tree(Translate_context
*)
10146 // Make a reference to a field in an interface.
10149 Expression::make_interface_field_reference(Expression
* expr
,
10150 const std::string
& field
,
10151 source_location location
)
10153 return new Interface_field_reference_expression(expr
, field
, location
);
10156 // A general selector. This is a Parser_expression for LEFT.NAME. It
10157 // is lowered after we know the type of the left hand side.
10159 class Selector_expression
: public Parser_expression
10162 Selector_expression(Expression
* left
, const std::string
& name
,
10163 source_location location
)
10164 : Parser_expression(EXPRESSION_SELECTOR
, location
),
10165 left_(left
), name_(name
)
10170 do_traverse(Traverse
* traverse
)
10171 { return Expression::traverse(&this->left_
, traverse
); }
10174 do_lower(Gogo
*, Named_object
*, int);
10179 return new Selector_expression(this->left_
->copy(), this->name_
,
10185 lower_method_expression(Gogo
*);
10187 // The expression on the left hand side.
10189 // The name on the right hand side.
10193 // Lower a selector expression once we know the real type of the left
10197 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
10199 Expression
* left
= this->left_
;
10200 if (left
->is_type_expression())
10201 return this->lower_method_expression(gogo
);
10202 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
10206 // Lower a method expression T.M or (*T).M. We turn this into a
10207 // function literal.
10210 Selector_expression::lower_method_expression(Gogo
* gogo
)
10212 source_location location
= this->location();
10213 Type
* type
= this->left_
->type();
10214 const std::string
& name(this->name_
);
10217 if (type
->points_to() == NULL
)
10218 is_pointer
= false;
10222 type
= type
->points_to();
10224 Named_type
* nt
= type
->named_type();
10228 ("method expression requires named type or "
10229 "pointer to named type"));
10230 return Expression::make_error(location
);
10234 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
10235 if (method
== NULL
)
10238 error_at(location
, "type %<%s%> has no method %<%s%>",
10239 nt
->message_name().c_str(),
10240 Gogo::message_name(name
).c_str());
10242 error_at(location
, "method %<%s%> is ambiguous in type %<%s%>",
10243 Gogo::message_name(name
).c_str(),
10244 nt
->message_name().c_str());
10245 return Expression::make_error(location
);
10248 if (!is_pointer
&& !method
->is_value_method())
10250 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
10251 nt
->message_name().c_str(),
10252 Gogo::message_name(name
).c_str());
10253 return Expression::make_error(location
);
10256 // Build a new function type in which the receiver becomes the first
10258 Function_type
* method_type
= method
->type();
10259 gcc_assert(method_type
->is_method());
10261 const char* const receiver_name
= "$this";
10262 Typed_identifier_list
* parameters
= new Typed_identifier_list();
10263 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
10266 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
10267 if (method_parameters
!= NULL
)
10269 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10270 p
!= method_parameters
->end();
10272 parameters
->push_back(*p
);
10275 const Typed_identifier_list
* method_results
= method_type
->results();
10276 Typed_identifier_list
* results
;
10277 if (method_results
== NULL
)
10281 results
= new Typed_identifier_list();
10282 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
10283 p
!= method_results
->end();
10285 results
->push_back(*p
);
10288 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
10290 if (method_type
->is_varargs())
10291 fntype
->set_is_varargs();
10293 // We generate methods which always takes a pointer to the receiver
10294 // as their first argument. If this is for a pointer type, we can
10295 // simply reuse the existing function. We use an internal hack to
10296 // get the right type.
10300 Named_object
* mno
= (method
->needs_stub_method()
10301 ? method
->stub_object()
10302 : method
->named_object());
10303 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
10304 f
= Expression::make_cast(fntype
, f
, location
);
10305 Type_conversion_expression
* tce
=
10306 static_cast<Type_conversion_expression
*>(f
);
10307 tce
->set_may_convert_function_types();
10311 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
10314 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
10315 gcc_assert(vno
!= NULL
);
10316 Expression
* ve
= Expression::make_var_reference(vno
, location
);
10317 Expression
* bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
10318 gcc_assert(bm
!= NULL
&& !bm
->is_error_expression());
10320 Expression_list
* args
;
10321 if (method_parameters
== NULL
)
10325 args
= new Expression_list();
10326 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10327 p
!= method_parameters
->end();
10330 vno
= gogo
->lookup(p
->name(), NULL
);
10331 gcc_assert(vno
!= NULL
);
10332 args
->push_back(Expression::make_var_reference(vno
, location
));
10336 Call_expression
* call
= Expression::make_call(bm
, args
,
10337 method_type
->is_varargs(),
10340 size_t count
= call
->result_count();
10343 s
= Statement::make_statement(call
);
10346 Expression_list
* retvals
= new Expression_list();
10348 retvals
->push_back(call
);
10351 for (size_t i
= 0; i
< count
; ++i
)
10352 retvals
->push_back(Expression::make_call_result(call
, i
));
10354 s
= Statement::make_return_statement(no
->func_value()->type()->results(),
10355 retvals
, location
);
10357 gogo
->add_statement(s
);
10359 gogo
->finish_function(location
);
10361 return Expression::make_func_reference(no
, NULL
, location
);
10364 // Make a selector expression.
10367 Expression::make_selector(Expression
* left
, const std::string
& name
,
10368 source_location location
)
10370 return new Selector_expression(left
, name
, location
);
10373 // Implement the builtin function new.
10375 class Allocation_expression
: public Expression
10378 Allocation_expression(Type
* type
, source_location location
)
10379 : Expression(EXPRESSION_ALLOCATION
, location
),
10385 do_traverse(Traverse
* traverse
)
10386 { return Type::traverse(this->type_
, traverse
); }
10390 { return Type::make_pointer_type(this->type_
); }
10393 do_determine_type(const Type_context
*)
10397 do_check_types(Gogo
*);
10401 { return new Allocation_expression(this->type_
, this->location()); }
10404 do_get_tree(Translate_context
*);
10407 // The type we are allocating.
10411 // Check the type of an allocation expression.
10414 Allocation_expression::do_check_types(Gogo
*)
10416 if (this->type_
->function_type() != NULL
)
10417 this->report_error(_("invalid new of function type"));
10420 // Return a tree for an allocation expression.
10423 Allocation_expression::do_get_tree(Translate_context
* context
)
10425 tree type_tree
= this->type_
->get_tree(context
->gogo());
10426 if (type_tree
== error_mark_node
)
10427 return error_mark_node
;
10428 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10429 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10431 if (space
== error_mark_node
)
10432 return error_mark_node
;
10433 return fold_convert(build_pointer_type(type_tree
), space
);
10436 // Make an allocation expression.
10439 Expression::make_allocation(Type
* type
, source_location location
)
10441 return new Allocation_expression(type
, location
);
10444 // Implement the builtin function make.
10446 class Make_expression
: public Expression
10449 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10450 : Expression(EXPRESSION_MAKE
, location
),
10451 type_(type
), args_(args
)
10456 do_traverse(Traverse
* traverse
);
10460 { return this->type_
; }
10463 do_determine_type(const Type_context
*);
10466 do_check_types(Gogo
*);
10471 return new Make_expression(this->type_
, this->args_
->copy(),
10476 do_get_tree(Translate_context
*);
10479 // The type we are making.
10481 // The arguments to pass to the make routine.
10482 Expression_list
* args_
;
10488 Make_expression::do_traverse(Traverse
* traverse
)
10490 if (this->args_
!= NULL
10491 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10492 return TRAVERSE_EXIT
;
10493 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10494 return TRAVERSE_EXIT
;
10495 return TRAVERSE_CONTINUE
;
10498 // Set types of arguments.
10501 Make_expression::do_determine_type(const Type_context
*)
10503 if (this->args_
!= NULL
)
10505 Type_context
context(Type::lookup_integer_type("int"), false);
10506 for (Expression_list::const_iterator pe
= this->args_
->begin();
10507 pe
!= this->args_
->end();
10509 (*pe
)->determine_type(&context
);
10513 // Check types for a make expression.
10516 Make_expression::do_check_types(Gogo
*)
10518 if (this->type_
->channel_type() == NULL
10519 && this->type_
->map_type() == NULL
10520 && (this->type_
->array_type() == NULL
10521 || this->type_
->array_type()->length() != NULL
))
10522 this->report_error(_("invalid type for make function"));
10523 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10524 this->set_is_error();
10527 // Return a tree for a make expression.
10530 Make_expression::do_get_tree(Translate_context
* context
)
10532 return this->type_
->make_expression_tree(context
, this->args_
,
10536 // Make a make expression.
10539 Expression::make_make(Type
* type
, Expression_list
* args
,
10540 source_location location
)
10542 return new Make_expression(type
, args
, location
);
10545 // Construct a struct.
10547 class Struct_construction_expression
: public Expression
10550 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10551 source_location location
)
10552 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10553 type_(type
), vals_(vals
)
10556 // Return whether this is a constant initializer.
10558 is_constant_struct() const;
10562 do_traverse(Traverse
* traverse
);
10566 { return this->type_
; }
10569 do_determine_type(const Type_context
*);
10572 do_check_types(Gogo
*);
10577 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10582 do_is_addressable() const
10586 do_get_tree(Translate_context
*);
10589 do_export(Export
*) const;
10592 // The type of the struct to construct.
10594 // The list of values, in order of the fields in the struct. A NULL
10595 // entry means that the field should be zero-initialized.
10596 Expression_list
* vals_
;
10602 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10604 if (this->vals_
!= NULL
10605 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10606 return TRAVERSE_EXIT
;
10607 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10608 return TRAVERSE_EXIT
;
10609 return TRAVERSE_CONTINUE
;
10612 // Return whether this is a constant initializer.
10615 Struct_construction_expression::is_constant_struct() const
10617 if (this->vals_
== NULL
)
10619 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10620 pv
!= this->vals_
->end();
10624 && !(*pv
)->is_constant()
10625 && (!(*pv
)->is_composite_literal()
10626 || (*pv
)->is_nonconstant_composite_literal()))
10630 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10631 for (Struct_field_list::const_iterator pf
= fields
->begin();
10632 pf
!= fields
->end();
10635 // There are no constant constructors for interfaces.
10636 if (pf
->type()->interface_type() != NULL
)
10643 // Final type determination.
10646 Struct_construction_expression::do_determine_type(const Type_context
*)
10648 if (this->vals_
== NULL
)
10650 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10651 Expression_list::const_iterator pv
= this->vals_
->begin();
10652 for (Struct_field_list::const_iterator pf
= fields
->begin();
10653 pf
!= fields
->end();
10656 if (pv
== this->vals_
->end())
10660 Type_context
subcontext(pf
->type(), false);
10661 (*pv
)->determine_type(&subcontext
);
10664 // Extra values are an error we will report elsewhere; we still want
10665 // to determine the type to avoid knockon errors.
10666 for (; pv
!= this->vals_
->end(); ++pv
)
10667 (*pv
)->determine_type_no_context();
10673 Struct_construction_expression::do_check_types(Gogo
*)
10675 if (this->vals_
== NULL
)
10678 Struct_type
* st
= this->type_
->struct_type();
10679 if (this->vals_
->size() > st
->field_count())
10681 this->report_error(_("too many expressions for struct"));
10685 const Struct_field_list
* fields
= st
->fields();
10686 Expression_list::const_iterator pv
= this->vals_
->begin();
10688 for (Struct_field_list::const_iterator pf
= fields
->begin();
10689 pf
!= fields
->end();
10692 if (pv
== this->vals_
->end())
10694 this->report_error(_("too few expressions for struct"));
10701 std::string reason
;
10702 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10704 if (reason
.empty())
10705 error_at((*pv
)->location(),
10706 "incompatible type for field %d in struct construction",
10709 error_at((*pv
)->location(),
10710 ("incompatible type for field %d in "
10711 "struct construction (%s)"),
10712 i
+ 1, reason
.c_str());
10713 this->set_is_error();
10716 gcc_assert(pv
== this->vals_
->end());
10719 // Return a tree for constructing a struct.
10722 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10724 Gogo
* gogo
= context
->gogo();
10726 if (this->vals_
== NULL
)
10727 return this->type_
->get_init_tree(gogo
, false);
10729 tree type_tree
= this->type_
->get_tree(gogo
);
10730 if (type_tree
== error_mark_node
)
10731 return error_mark_node
;
10732 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10734 bool is_constant
= true;
10735 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10736 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10738 Struct_field_list::const_iterator pf
= fields
->begin();
10739 Expression_list::const_iterator pv
= this->vals_
->begin();
10740 for (tree field
= TYPE_FIELDS(type_tree
);
10741 field
!= NULL_TREE
;
10742 field
= DECL_CHAIN(field
), ++pf
)
10744 gcc_assert(pf
!= fields
->end());
10747 if (pv
== this->vals_
->end())
10748 val
= pf
->type()->get_init_tree(gogo
, false);
10749 else if (*pv
== NULL
)
10751 val
= pf
->type()->get_init_tree(gogo
, false);
10756 val
= Expression::convert_for_assignment(context
, pf
->type(),
10758 (*pv
)->get_tree(context
),
10763 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10764 return error_mark_node
;
10766 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10767 elt
->index
= field
;
10769 if (!TREE_CONSTANT(val
))
10770 is_constant
= false;
10772 gcc_assert(pf
== fields
->end());
10774 tree ret
= build_constructor(type_tree
, elts
);
10776 TREE_CONSTANT(ret
) = 1;
10780 // Export a struct construction.
10783 Struct_construction_expression::do_export(Export
* exp
) const
10785 exp
->write_c_string("convert(");
10786 exp
->write_type(this->type_
);
10787 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10788 pv
!= this->vals_
->end();
10791 exp
->write_c_string(", ");
10793 (*pv
)->export_expression(exp
);
10795 exp
->write_c_string(")");
10798 // Make a struct composite literal. This used by the thunk code.
10801 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
10802 source_location location
)
10804 gcc_assert(type
->struct_type() != NULL
);
10805 return new Struct_construction_expression(type
, vals
, location
);
10808 // Construct an array. This class is not used directly; instead we
10809 // use the child classes, Fixed_array_construction_expression and
10810 // Open_array_construction_expression.
10812 class Array_construction_expression
: public Expression
10815 Array_construction_expression(Expression_classification classification
,
10816 Type
* type
, Expression_list
* vals
,
10817 source_location location
)
10818 : Expression(classification
, location
),
10819 type_(type
), vals_(vals
)
10823 // Return whether this is a constant initializer.
10825 is_constant_array() const;
10827 // Return the number of elements.
10829 element_count() const
10830 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
10834 do_traverse(Traverse
* traverse
);
10838 { return this->type_
; }
10841 do_determine_type(const Type_context
*);
10844 do_check_types(Gogo
*);
10847 do_is_addressable() const
10851 do_export(Export
*) const;
10853 // The list of values.
10856 { return this->vals_
; }
10858 // Get a constructor tree for the array values.
10860 get_constructor_tree(Translate_context
* context
, tree type_tree
);
10863 // The type of the array to construct.
10865 // The list of values.
10866 Expression_list
* vals_
;
10872 Array_construction_expression::do_traverse(Traverse
* traverse
)
10874 if (this->vals_
!= NULL
10875 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10876 return TRAVERSE_EXIT
;
10877 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10878 return TRAVERSE_EXIT
;
10879 return TRAVERSE_CONTINUE
;
10882 // Return whether this is a constant initializer.
10885 Array_construction_expression::is_constant_array() const
10887 if (this->vals_
== NULL
)
10890 // There are no constant constructors for interfaces.
10891 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
10894 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10895 pv
!= this->vals_
->end();
10899 && !(*pv
)->is_constant()
10900 && (!(*pv
)->is_composite_literal()
10901 || (*pv
)->is_nonconstant_composite_literal()))
10907 // Final type determination.
10910 Array_construction_expression::do_determine_type(const Type_context
*)
10912 if (this->vals_
== NULL
)
10914 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
10915 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10916 pv
!= this->vals_
->end();
10920 (*pv
)->determine_type(&subcontext
);
10927 Array_construction_expression::do_check_types(Gogo
*)
10929 if (this->vals_
== NULL
)
10932 Array_type
* at
= this->type_
->array_type();
10934 Type
* element_type
= at
->element_type();
10935 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10936 pv
!= this->vals_
->end();
10940 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
10942 error_at((*pv
)->location(),
10943 "incompatible type for element %d in composite literal",
10945 this->set_is_error();
10949 Expression
* length
= at
->length();
10950 if (length
!= NULL
)
10955 if (at
->length()->integer_constant_value(true, val
, &type
))
10957 if (this->vals_
->size() > mpz_get_ui(val
))
10958 this->report_error(_("too many elements in composite literal"));
10964 // Get a constructor tree for the array values.
10967 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
10970 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10971 (this->vals_
== NULL
10973 : this->vals_
->size()));
10974 Type
* element_type
= this->type_
->array_type()->element_type();
10975 bool is_constant
= true;
10976 if (this->vals_
!= NULL
)
10979 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10980 pv
!= this->vals_
->end();
10983 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
10984 elt
->index
= size_int(i
);
10986 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10989 tree value_tree
= (*pv
)->get_tree(context
);
10990 elt
->value
= Expression::convert_for_assignment(context
,
10996 if (elt
->value
== error_mark_node
)
10997 return error_mark_node
;
10998 if (!TREE_CONSTANT(elt
->value
))
10999 is_constant
= false;
11003 tree ret
= build_constructor(type_tree
, values
);
11005 TREE_CONSTANT(ret
) = 1;
11009 // Export an array construction.
11012 Array_construction_expression::do_export(Export
* exp
) const
11014 exp
->write_c_string("convert(");
11015 exp
->write_type(this->type_
);
11016 if (this->vals_
!= NULL
)
11018 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11019 pv
!= this->vals_
->end();
11022 exp
->write_c_string(", ");
11024 (*pv
)->export_expression(exp
);
11027 exp
->write_c_string(")");
11030 // Construct a fixed array.
11032 class Fixed_array_construction_expression
:
11033 public Array_construction_expression
11036 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
11037 source_location location
)
11038 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
11039 type
, vals
, location
)
11041 gcc_assert(type
->array_type() != NULL
11042 && type
->array_type()->length() != NULL
);
11049 return new Fixed_array_construction_expression(this->type(),
11050 (this->vals() == NULL
11052 : this->vals()->copy()),
11057 do_get_tree(Translate_context
*);
11060 // Return a tree for constructing a fixed array.
11063 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
11065 return this->get_constructor_tree(context
,
11066 this->type()->get_tree(context
->gogo()));
11069 // Construct an open array.
11071 class Open_array_construction_expression
: public Array_construction_expression
11074 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
11075 source_location location
)
11076 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
11077 type
, vals
, location
)
11079 gcc_assert(type
->array_type() != NULL
11080 && type
->array_type()->length() == NULL
);
11084 // Note that taking the address of an open array literal is invalid.
11089 return new Open_array_construction_expression(this->type(),
11090 (this->vals() == NULL
11092 : this->vals()->copy()),
11097 do_get_tree(Translate_context
*);
11100 // Return a tree for constructing an open array.
11103 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
11105 Array_type
* array_type
= this->type()->array_type();
11106 if (array_type
== NULL
)
11108 gcc_assert(this->type()->is_error_type());
11109 return error_mark_node
;
11112 Type
* element_type
= array_type
->element_type();
11113 tree element_type_tree
= element_type
->get_tree(context
->gogo());
11114 if (element_type_tree
== error_mark_node
)
11115 return error_mark_node
;
11119 if (this->vals() == NULL
|| this->vals()->empty())
11121 // We need to create a unique value.
11122 tree max
= size_int(0);
11123 tree constructor_type
= build_array_type(element_type_tree
,
11124 build_index_type(max
));
11125 if (constructor_type
== error_mark_node
)
11126 return error_mark_node
;
11127 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
11128 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
11129 elt
->index
= size_int(0);
11130 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
11131 values
= build_constructor(constructor_type
, vec
);
11132 if (TREE_CONSTANT(elt
->value
))
11133 TREE_CONSTANT(values
) = 1;
11134 length_tree
= size_int(0);
11138 tree max
= size_int(this->vals()->size() - 1);
11139 tree constructor_type
= build_array_type(element_type_tree
,
11140 build_index_type(max
));
11141 if (constructor_type
== error_mark_node
)
11142 return error_mark_node
;
11143 values
= this->get_constructor_tree(context
, constructor_type
);
11144 length_tree
= size_int(this->vals()->size());
11147 if (values
== error_mark_node
)
11148 return error_mark_node
;
11150 bool is_constant_initializer
= TREE_CONSTANT(values
);
11152 // We have to copy the initial values into heap memory if we are in
11153 // a function or if the values are not constants. We also have to
11154 // copy them if they may contain pointers in a non-constant context,
11155 // as otherwise the garbage collector won't see them.
11156 bool copy_to_heap
= (context
->function() != NULL
11157 || !is_constant_initializer
11158 || (element_type
->has_pointer()
11159 && !context
->is_const()));
11161 if (is_constant_initializer
)
11163 tree tmp
= build_decl(this->location(), VAR_DECL
,
11164 create_tmp_var_name("C"), TREE_TYPE(values
));
11165 DECL_EXTERNAL(tmp
) = 0;
11166 TREE_PUBLIC(tmp
) = 0;
11167 TREE_STATIC(tmp
) = 1;
11168 DECL_ARTIFICIAL(tmp
) = 1;
11171 // If we are not copying the value to the heap, we will only
11172 // initialize the value once, so we can use this directly
11173 // rather than copying it. In that case we can't make it
11174 // read-only, because the program is permitted to change it.
11175 TREE_READONLY(tmp
) = 1;
11176 TREE_CONSTANT(tmp
) = 1;
11178 DECL_INITIAL(tmp
) = values
;
11179 rest_of_decl_compilation(tmp
, 1, 0);
11187 // the initializer will only run once.
11188 space
= build_fold_addr_expr(values
);
11193 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
11194 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
11196 space
= save_expr(space
);
11198 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
11199 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
11200 TREE_THIS_NOTRAP(ref
) = 1;
11201 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
11204 // Build a constructor for the open array.
11206 tree type_tree
= this->type()->get_tree(context
->gogo());
11207 if (type_tree
== error_mark_node
)
11208 return error_mark_node
;
11209 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
11211 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
11213 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11214 tree field
= TYPE_FIELDS(type_tree
);
11215 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
11216 elt
->index
= field
;
11217 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
11219 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11220 field
= DECL_CHAIN(field
);
11221 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
11222 elt
->index
= field
;
11223 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11225 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11226 field
= DECL_CHAIN(field
);
11227 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
11228 elt
->index
= field
;
11229 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11231 tree constructor
= build_constructor(type_tree
, init
);
11232 if (constructor
== error_mark_node
)
11233 return error_mark_node
;
11235 TREE_CONSTANT(constructor
) = 1;
11237 if (set
== NULL_TREE
)
11238 return constructor
;
11240 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
11243 // Make a slice composite literal. This is used by the type
11244 // descriptor code.
11247 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
11248 source_location location
)
11250 gcc_assert(type
->is_open_array_type());
11251 return new Open_array_construction_expression(type
, vals
, location
);
11254 // Construct a map.
11256 class Map_construction_expression
: public Expression
11259 Map_construction_expression(Type
* type
, Expression_list
* vals
,
11260 source_location location
)
11261 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
11262 type_(type
), vals_(vals
)
11263 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
11267 do_traverse(Traverse
* traverse
);
11271 { return this->type_
; }
11274 do_determine_type(const Type_context
*);
11277 do_check_types(Gogo
*);
11282 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
11287 do_get_tree(Translate_context
*);
11290 do_export(Export
*) const;
11293 // The type of the map to construct.
11295 // The list of values.
11296 Expression_list
* vals_
;
11302 Map_construction_expression::do_traverse(Traverse
* traverse
)
11304 if (this->vals_
!= NULL
11305 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11306 return TRAVERSE_EXIT
;
11307 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11308 return TRAVERSE_EXIT
;
11309 return TRAVERSE_CONTINUE
;
11312 // Final type determination.
11315 Map_construction_expression::do_determine_type(const Type_context
*)
11317 if (this->vals_
== NULL
)
11320 Map_type
* mt
= this->type_
->map_type();
11321 Type_context
key_context(mt
->key_type(), false);
11322 Type_context
val_context(mt
->val_type(), false);
11323 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11324 pv
!= this->vals_
->end();
11327 (*pv
)->determine_type(&key_context
);
11329 (*pv
)->determine_type(&val_context
);
11336 Map_construction_expression::do_check_types(Gogo
*)
11338 if (this->vals_
== NULL
)
11341 Map_type
* mt
= this->type_
->map_type();
11343 Type
* key_type
= mt
->key_type();
11344 Type
* val_type
= mt
->val_type();
11345 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11346 pv
!= this->vals_
->end();
11349 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
11351 error_at((*pv
)->location(),
11352 "incompatible type for element %d key in map construction",
11354 this->set_is_error();
11357 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
11359 error_at((*pv
)->location(),
11360 ("incompatible type for element %d value "
11361 "in map construction"),
11363 this->set_is_error();
11368 // Return a tree for constructing a map.
11371 Map_construction_expression::do_get_tree(Translate_context
* context
)
11373 Gogo
* gogo
= context
->gogo();
11374 source_location loc
= this->location();
11376 Map_type
* mt
= this->type_
->map_type();
11378 // Build a struct to hold the key and value.
11379 tree struct_type
= make_node(RECORD_TYPE
);
11381 Type
* key_type
= mt
->key_type();
11382 tree id
= get_identifier("__key");
11383 tree key_type_tree
= key_type
->get_tree(gogo
);
11384 if (key_type_tree
== error_mark_node
)
11385 return error_mark_node
;
11386 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type_tree
);
11387 DECL_CONTEXT(key_field
) = struct_type
;
11388 TYPE_FIELDS(struct_type
) = key_field
;
11390 Type
* val_type
= mt
->val_type();
11391 id
= get_identifier("__val");
11392 tree val_type_tree
= val_type
->get_tree(gogo
);
11393 if (val_type_tree
== error_mark_node
)
11394 return error_mark_node
;
11395 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type_tree
);
11396 DECL_CONTEXT(val_field
) = struct_type
;
11397 DECL_CHAIN(key_field
) = val_field
;
11399 layout_type(struct_type
);
11401 bool is_constant
= true;
11406 if (this->vals_
== NULL
|| this->vals_
->empty())
11408 valaddr
= null_pointer_node
;
11409 make_tmp
= NULL_TREE
;
11413 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11414 this->vals_
->size() / 2);
11416 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11417 pv
!= this->vals_
->end();
11420 bool one_is_constant
= true;
11422 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11424 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11425 elt
->index
= key_field
;
11426 tree val_tree
= (*pv
)->get_tree(context
);
11427 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11430 if (elt
->value
== error_mark_node
)
11431 return error_mark_node
;
11432 if (!TREE_CONSTANT(elt
->value
))
11433 one_is_constant
= false;
11437 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11438 elt
->index
= val_field
;
11439 val_tree
= (*pv
)->get_tree(context
);
11440 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11443 if (elt
->value
== error_mark_node
)
11444 return error_mark_node
;
11445 if (!TREE_CONSTANT(elt
->value
))
11446 one_is_constant
= false;
11448 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11449 elt
->index
= size_int(i
);
11450 elt
->value
= build_constructor(struct_type
, one
);
11451 if (one_is_constant
)
11452 TREE_CONSTANT(elt
->value
) = 1;
11454 is_constant
= false;
11457 tree index_type
= build_index_type(size_int(i
- 1));
11458 tree array_type
= build_array_type(struct_type
, index_type
);
11459 tree init
= build_constructor(array_type
, values
);
11461 TREE_CONSTANT(init
) = 1;
11463 if (current_function_decl
!= NULL
)
11465 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11466 DECL_INITIAL(tmp
) = init
;
11467 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11468 TREE_ADDRESSABLE(tmp
) = 1;
11472 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11473 DECL_EXTERNAL(tmp
) = 0;
11474 TREE_PUBLIC(tmp
) = 0;
11475 TREE_STATIC(tmp
) = 1;
11476 DECL_ARTIFICIAL(tmp
) = 1;
11477 if (!TREE_CONSTANT(init
))
11478 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11482 TREE_READONLY(tmp
) = 1;
11483 TREE_CONSTANT(tmp
) = 1;
11484 DECL_INITIAL(tmp
) = init
;
11485 make_tmp
= NULL_TREE
;
11487 rest_of_decl_compilation(tmp
, 1, 0);
11490 valaddr
= build_fold_addr_expr(tmp
);
11493 tree descriptor
= gogo
->map_descriptor(mt
);
11495 tree type_tree
= this->type_
->get_tree(gogo
);
11496 if (type_tree
== error_mark_node
)
11497 return error_mark_node
;
11499 static tree construct_map_fndecl
;
11500 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11502 "__go_construct_map",
11505 TREE_TYPE(descriptor
),
11510 TYPE_SIZE_UNIT(struct_type
),
11512 byte_position(val_field
),
11514 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11515 const_ptr_type_node
,
11516 fold_convert(const_ptr_type_node
, valaddr
));
11517 if (call
== error_mark_node
)
11518 return error_mark_node
;
11521 if (make_tmp
== NULL
)
11524 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11528 // Export an array construction.
11531 Map_construction_expression::do_export(Export
* exp
) const
11533 exp
->write_c_string("convert(");
11534 exp
->write_type(this->type_
);
11535 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11536 pv
!= this->vals_
->end();
11539 exp
->write_c_string(", ");
11540 (*pv
)->export_expression(exp
);
11542 exp
->write_c_string(")");
11545 // A general composite literal. This is lowered to a type specific
11548 class Composite_literal_expression
: public Parser_expression
11551 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11552 Expression_list
* vals
, source_location location
)
11553 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11554 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11559 do_traverse(Traverse
* traverse
);
11562 do_lower(Gogo
*, Named_object
*, int);
11567 return new Composite_literal_expression(this->type_
, this->depth_
,
11569 (this->vals_
== NULL
11571 : this->vals_
->copy()),
11577 lower_struct(Type
*);
11580 lower_array(Type
*);
11583 make_array(Type
*, Expression_list
*);
11586 lower_map(Gogo
*, Named_object
*, Type
*);
11588 // The type of the composite literal.
11590 // The depth within a list of composite literals within a composite
11591 // literal, when the type is omitted.
11593 // The values to put in the composite literal.
11594 Expression_list
* vals_
;
11595 // If this is true, then VALS_ is a list of pairs: a key and a
11596 // value. In an array initializer, a missing key will be NULL.
11603 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11605 if (this->vals_
!= NULL
11606 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11607 return TRAVERSE_EXIT
;
11608 return Type::traverse(this->type_
, traverse
);
11611 // Lower a generic composite literal into a specific version based on
11615 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
11617 Type
* type
= this->type_
;
11619 for (int depth
= this->depth_
; depth
> 0; --depth
)
11621 if (type
->array_type() != NULL
)
11622 type
= type
->array_type()->element_type();
11623 else if (type
->map_type() != NULL
)
11624 type
= type
->map_type()->val_type();
11627 if (!type
->is_error_type())
11628 error_at(this->location(),
11629 ("may only omit types within composite literals "
11630 "of slice, array, or map type"));
11631 return Expression::make_error(this->location());
11635 if (type
->is_error_type())
11636 return Expression::make_error(this->location());
11637 else if (type
->struct_type() != NULL
)
11638 return this->lower_struct(type
);
11639 else if (type
->array_type() != NULL
)
11640 return this->lower_array(type
);
11641 else if (type
->map_type() != NULL
)
11642 return this->lower_map(gogo
, function
, type
);
11645 error_at(this->location(),
11646 ("expected struct, slice, array, or map type "
11647 "for composite literal"));
11648 return Expression::make_error(this->location());
11652 // Lower a struct composite literal.
11655 Composite_literal_expression::lower_struct(Type
* type
)
11657 source_location location
= this->location();
11658 Struct_type
* st
= type
->struct_type();
11659 if (this->vals_
== NULL
|| !this->has_keys_
)
11660 return new Struct_construction_expression(type
, this->vals_
, location
);
11662 size_t field_count
= st
->field_count();
11663 std::vector
<Expression
*> vals(field_count
);
11664 Expression_list::const_iterator p
= this->vals_
->begin();
11665 while (p
!= this->vals_
->end())
11667 Expression
* name_expr
= *p
;
11670 gcc_assert(p
!= this->vals_
->end());
11671 Expression
* val
= *p
;
11675 if (name_expr
== NULL
)
11677 error_at(val
->location(), "mixture of field and value initializers");
11678 return Expression::make_error(location
);
11681 bool bad_key
= false;
11683 switch (name_expr
->classification())
11685 case EXPRESSION_UNKNOWN_REFERENCE
:
11686 name
= name_expr
->unknown_expression()->name();
11689 case EXPRESSION_CONST_REFERENCE
:
11690 name
= static_cast<Const_expression
*>(name_expr
)->name();
11693 case EXPRESSION_TYPE
:
11695 Type
* t
= name_expr
->type();
11696 Named_type
* nt
= t
->named_type();
11704 case EXPRESSION_VAR_REFERENCE
:
11705 name
= name_expr
->var_expression()->name();
11708 case EXPRESSION_FUNC_REFERENCE
:
11709 name
= name_expr
->func_expression()->name();
11712 case EXPRESSION_UNARY
:
11713 // If there is a local variable around with the same name as
11714 // the field, and this occurs in the closure, then the
11715 // parser may turn the field reference into an indirection
11716 // through the closure. FIXME: This is a mess.
11719 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11720 if (ue
->op() == OPERATOR_MULT
)
11722 Field_reference_expression
* fre
=
11723 ue
->operand()->field_reference_expression();
11727 fre
->expr()->type()->deref()->struct_type();
11730 const Struct_field
* sf
= st
->field(fre
->field_index());
11731 name
= sf
->field_name();
11733 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11734 size_t buflen
= strlen(buf
);
11735 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11738 name
= name
.substr(0, name
.length() - buflen
);
11753 error_at(name_expr
->location(), "expected struct field name");
11754 return Expression::make_error(location
);
11757 unsigned int index
;
11758 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11761 error_at(name_expr
->location(), "unknown field %qs in %qs",
11762 Gogo::message_name(name
).c_str(),
11763 (type
->named_type() != NULL
11764 ? type
->named_type()->message_name().c_str()
11765 : "unnamed struct"));
11766 return Expression::make_error(location
);
11768 if (vals
[index
] != NULL
)
11770 error_at(name_expr
->location(),
11771 "duplicate value for field %qs in %qs",
11772 Gogo::message_name(name
).c_str(),
11773 (type
->named_type() != NULL
11774 ? type
->named_type()->message_name().c_str()
11775 : "unnamed struct"));
11776 return Expression::make_error(location
);
11782 Expression_list
* list
= new Expression_list
;
11783 list
->reserve(field_count
);
11784 for (size_t i
= 0; i
< field_count
; ++i
)
11785 list
->push_back(vals
[i
]);
11787 return new Struct_construction_expression(type
, list
, location
);
11790 // Lower an array composite literal.
11793 Composite_literal_expression::lower_array(Type
* type
)
11795 source_location location
= this->location();
11796 if (this->vals_
== NULL
|| !this->has_keys_
)
11797 return this->make_array(type
, this->vals_
);
11799 std::vector
<Expression
*> vals
;
11800 vals
.reserve(this->vals_
->size());
11801 unsigned long index
= 0;
11802 Expression_list::const_iterator p
= this->vals_
->begin();
11803 while (p
!= this->vals_
->end())
11805 Expression
* index_expr
= *p
;
11808 gcc_assert(p
!= this->vals_
->end());
11809 Expression
* val
= *p
;
11813 if (index_expr
!= NULL
)
11818 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
11821 error_at(index_expr
->location(),
11822 "index expression is not integer constant");
11823 return Expression::make_error(location
);
11825 if (mpz_sgn(ival
) < 0)
11828 error_at(index_expr
->location(), "index expression is negative");
11829 return Expression::make_error(location
);
11831 index
= mpz_get_ui(ival
);
11832 if (mpz_cmp_ui(ival
, index
) != 0)
11835 error_at(index_expr
->location(), "index value overflow");
11836 return Expression::make_error(location
);
11841 if (index
== vals
.size())
11842 vals
.push_back(val
);
11845 if (index
> vals
.size())
11847 vals
.reserve(index
+ 32);
11848 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
11850 if (vals
[index
] != NULL
)
11852 error_at((index_expr
!= NULL
11853 ? index_expr
->location()
11854 : val
->location()),
11855 "duplicate value for index %lu",
11857 return Expression::make_error(location
);
11865 size_t size
= vals
.size();
11866 Expression_list
* list
= new Expression_list
;
11867 list
->reserve(size
);
11868 for (size_t i
= 0; i
< size
; ++i
)
11869 list
->push_back(vals
[i
]);
11871 return this->make_array(type
, list
);
11874 // Actually build the array composite literal. This handles
11878 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
11880 source_location location
= this->location();
11881 Array_type
* at
= type
->array_type();
11882 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
11884 size_t size
= vals
== NULL
? 0 : vals
->size();
11886 mpz_init_set_ui(vlen
, size
);
11887 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
11889 at
= Type::make_array_type(at
->element_type(), elen
);
11892 if (at
->length() != NULL
)
11893 return new Fixed_array_construction_expression(type
, vals
, location
);
11895 return new Open_array_construction_expression(type
, vals
, location
);
11898 // Lower a map composite literal.
11901 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
11904 source_location location
= this->location();
11905 if (this->vals_
!= NULL
)
11907 if (!this->has_keys_
)
11909 error_at(location
, "map composite literal must have keys");
11910 return Expression::make_error(location
);
11913 for (Expression_list::iterator p
= this->vals_
->begin();
11914 p
!= this->vals_
->end();
11920 error_at((*p
)->location(),
11921 "map composite literal must have keys for every value");
11922 return Expression::make_error(location
);
11924 // Make sure we have lowered the key; it may not have been
11925 // lowered in order to handle keys for struct composite
11926 // literals. Lower it now to get the right error message.
11927 if ((*p
)->unknown_expression() != NULL
)
11929 (*p
)->unknown_expression()->clear_is_composite_literal_key();
11930 gogo
->lower_expression(function
, &*p
);
11931 gcc_assert((*p
)->is_error_expression());
11932 return Expression::make_error(location
);
11937 return new Map_construction_expression(type
, this->vals_
, location
);
11940 // Make a composite literal expression.
11943 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
11944 Expression_list
* vals
,
11945 source_location location
)
11947 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
11951 // Return whether this expression is a composite literal.
11954 Expression::is_composite_literal() const
11956 switch (this->classification_
)
11958 case EXPRESSION_COMPOSITE_LITERAL
:
11959 case EXPRESSION_STRUCT_CONSTRUCTION
:
11960 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11961 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11962 case EXPRESSION_MAP_CONSTRUCTION
:
11969 // Return whether this expression is a composite literal which is not
11973 Expression::is_nonconstant_composite_literal() const
11975 switch (this->classification_
)
11977 case EXPRESSION_STRUCT_CONSTRUCTION
:
11979 const Struct_construction_expression
*psce
=
11980 static_cast<const Struct_construction_expression
*>(this);
11981 return !psce
->is_constant_struct();
11983 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11985 const Fixed_array_construction_expression
*pace
=
11986 static_cast<const Fixed_array_construction_expression
*>(this);
11987 return !pace
->is_constant_array();
11989 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11991 const Open_array_construction_expression
*pace
=
11992 static_cast<const Open_array_construction_expression
*>(this);
11993 return !pace
->is_constant_array();
11995 case EXPRESSION_MAP_CONSTRUCTION
:
12002 // Return true if this is a reference to a local variable.
12005 Expression::is_local_variable() const
12007 const Var_expression
* ve
= this->var_expression();
12010 const Named_object
* no
= ve
->named_object();
12011 return (no
->is_result_variable()
12012 || (no
->is_variable() && !no
->var_value()->is_global()));
12015 // Class Type_guard_expression.
12020 Type_guard_expression::do_traverse(Traverse
* traverse
)
12022 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
12023 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
12024 return TRAVERSE_EXIT
;
12025 return TRAVERSE_CONTINUE
;
12028 // Check types of a type guard expression. The expression must have
12029 // an interface type, but the actual type conversion is checked at run
12033 Type_guard_expression::do_check_types(Gogo
*)
12035 // 6g permits using a type guard with unsafe.pointer; we are
12037 Type
* expr_type
= this->expr_
->type();
12038 if (expr_type
->is_unsafe_pointer_type())
12040 if (this->type_
->points_to() == NULL
12041 && (this->type_
->integer_type() == NULL
12042 || (this->type_
->forwarded()
12043 != Type::lookup_integer_type("uintptr"))))
12044 this->report_error(_("invalid unsafe.Pointer conversion"));
12046 else if (this->type_
->is_unsafe_pointer_type())
12048 if (expr_type
->points_to() == NULL
12049 && (expr_type
->integer_type() == NULL
12050 || (expr_type
->forwarded()
12051 != Type::lookup_integer_type("uintptr"))))
12052 this->report_error(_("invalid unsafe.Pointer conversion"));
12054 else if (expr_type
->interface_type() == NULL
)
12056 if (!expr_type
->is_error_type() && !this->type_
->is_error_type())
12057 this->report_error(_("type assertion only valid for interface types"));
12058 this->set_is_error();
12060 else if (this->type_
->interface_type() == NULL
)
12062 std::string reason
;
12063 if (!expr_type
->interface_type()->implements_interface(this->type_
,
12066 if (!this->type_
->is_error_type())
12068 if (reason
.empty())
12069 this->report_error(_("impossible type assertion: "
12070 "type does not implement interface"));
12072 error_at(this->location(),
12073 ("impossible type assertion: "
12074 "type does not implement interface (%s)"),
12077 this->set_is_error();
12082 // Return a tree for a type guard expression.
12085 Type_guard_expression::do_get_tree(Translate_context
* context
)
12087 Gogo
* gogo
= context
->gogo();
12088 tree expr_tree
= this->expr_
->get_tree(context
);
12089 if (expr_tree
== error_mark_node
)
12090 return error_mark_node
;
12091 Type
* expr_type
= this->expr_
->type();
12092 if ((this->type_
->is_unsafe_pointer_type()
12093 && (expr_type
->points_to() != NULL
12094 || expr_type
->integer_type() != NULL
))
12095 || (expr_type
->is_unsafe_pointer_type()
12096 && this->type_
->points_to() != NULL
))
12097 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
12098 else if (expr_type
->is_unsafe_pointer_type()
12099 && this->type_
->integer_type() != NULL
)
12100 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
12101 else if (this->type_
->interface_type() != NULL
)
12102 return Expression::convert_interface_to_interface(context
, this->type_
,
12103 this->expr_
->type(),
12107 return Expression::convert_for_assignment(context
, this->type_
,
12108 this->expr_
->type(), expr_tree
,
12112 // Make a type guard expression.
12115 Expression::make_type_guard(Expression
* expr
, Type
* type
,
12116 source_location location
)
12118 return new Type_guard_expression(expr
, type
, location
);
12121 // Class Heap_composite_expression.
12123 // When you take the address of a composite literal, it is allocated
12124 // on the heap. This class implements that.
12126 class Heap_composite_expression
: public Expression
12129 Heap_composite_expression(Expression
* expr
, source_location location
)
12130 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
12136 do_traverse(Traverse
* traverse
)
12137 { return Expression::traverse(&this->expr_
, traverse
); }
12141 { return Type::make_pointer_type(this->expr_
->type()); }
12144 do_determine_type(const Type_context
*)
12145 { this->expr_
->determine_type_no_context(); }
12150 return Expression::make_heap_composite(this->expr_
->copy(),
12155 do_get_tree(Translate_context
*);
12157 // We only export global objects, and the parser does not generate
12158 // this in global scope.
12160 do_export(Export
*) const
12161 { gcc_unreachable(); }
12164 // The composite literal which is being put on the heap.
12168 // Return a tree which allocates a composite literal on the heap.
12171 Heap_composite_expression::do_get_tree(Translate_context
* context
)
12173 tree expr_tree
= this->expr_
->get_tree(context
);
12174 if (expr_tree
== error_mark_node
)
12175 return error_mark_node
;
12176 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
12177 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
12178 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
12179 expr_size
, this->location());
12180 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
12181 space
= save_expr(space
);
12182 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
12183 TREE_THIS_NOTRAP(ref
) = 1;
12184 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
12185 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
12187 SET_EXPR_LOCATION(ret
, this->location());
12191 // Allocate a composite literal on the heap.
12194 Expression::make_heap_composite(Expression
* expr
, source_location location
)
12196 return new Heap_composite_expression(expr
, location
);
12199 // Class Receive_expression.
12201 // Return the type of a receive expression.
12204 Receive_expression::do_type()
12206 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12207 if (channel_type
== NULL
)
12208 return Type::make_error_type();
12209 return channel_type
->element_type();
12212 // Check types for a receive expression.
12215 Receive_expression::do_check_types(Gogo
*)
12217 Type
* type
= this->channel_
->type();
12218 if (type
->is_error_type())
12220 this->set_is_error();
12223 if (type
->channel_type() == NULL
)
12225 this->report_error(_("expected channel"));
12228 if (!type
->channel_type()->may_receive())
12230 this->report_error(_("invalid receive on send-only channel"));
12235 // Get a tree for a receive expression.
12238 Receive_expression::do_get_tree(Translate_context
* context
)
12240 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12241 gcc_assert(channel_type
!= NULL
);
12242 Type
* element_type
= channel_type
->element_type();
12243 tree element_type_tree
= element_type
->get_tree(context
->gogo());
12245 tree channel
= this->channel_
->get_tree(context
);
12246 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
12247 return error_mark_node
;
12249 return Gogo::receive_from_channel(element_type_tree
, channel
,
12250 this->for_select_
, this->location());
12253 // Make a receive expression.
12255 Receive_expression
*
12256 Expression::make_receive(Expression
* channel
, source_location location
)
12258 return new Receive_expression(channel
, location
);
12261 // Class Send_expression.
12266 Send_expression::do_traverse(Traverse
* traverse
)
12268 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
12269 return TRAVERSE_EXIT
;
12270 return Expression::traverse(&this->val_
, traverse
);
12276 Send_expression::do_type()
12278 return Type::lookup_bool_type();
12284 Send_expression::do_determine_type(const Type_context
*)
12286 this->channel_
->determine_type_no_context();
12288 Type
* type
= this->channel_
->type();
12289 Type_context subcontext
;
12290 if (type
->channel_type() != NULL
)
12291 subcontext
.type
= type
->channel_type()->element_type();
12292 this->val_
->determine_type(&subcontext
);
12298 Send_expression::do_check_types(Gogo
*)
12300 Type
* type
= this->channel_
->type();
12301 if (type
->is_error_type())
12303 this->set_is_error();
12306 Channel_type
* channel_type
= type
->channel_type();
12307 if (channel_type
== NULL
)
12309 error_at(this->location(), "left operand of %<<-%> must be channel");
12310 this->set_is_error();
12313 Type
* element_type
= channel_type
->element_type();
12314 if (element_type
!= NULL
12315 && !Type::are_assignable(element_type
, this->val_
->type(), NULL
))
12317 this->report_error(_("incompatible types in send"));
12320 if (!channel_type
->may_send())
12322 this->report_error(_("invalid send on receive-only channel"));
12327 // Get a tree for a send expression.
12330 Send_expression::do_get_tree(Translate_context
* context
)
12332 tree channel
= this->channel_
->get_tree(context
);
12333 tree val
= this->val_
->get_tree(context
);
12334 if (channel
== error_mark_node
|| val
== error_mark_node
)
12335 return error_mark_node
;
12336 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12337 val
= Expression::convert_for_assignment(context
,
12338 channel_type
->element_type(),
12339 this->val_
->type(),
12342 return Gogo::send_on_channel(channel
, val
, this->is_value_discarded_
,
12343 this->for_select_
, this->location());
12346 // Make a send expression
12349 Expression::make_send(Expression
* channel
, Expression
* val
,
12350 source_location location
)
12352 return new Send_expression(channel
, val
, location
);
12355 // An expression which evaluates to a pointer to the type descriptor
12358 class Type_descriptor_expression
: public Expression
12361 Type_descriptor_expression(Type
* type
, source_location location
)
12362 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
12369 { return Type::make_type_descriptor_ptr_type(); }
12372 do_determine_type(const Type_context
*)
12380 do_get_tree(Translate_context
* context
)
12381 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
12384 // The type for which this is the descriptor.
12388 // Make a type descriptor expression.
12391 Expression::make_type_descriptor(Type
* type
, source_location location
)
12393 return new Type_descriptor_expression(type
, location
);
12396 // An expression which evaluates to some characteristic of a type.
12397 // This is only used to initialize fields of a type descriptor. Using
12398 // a new expression class is slightly inefficient but gives us a good
12399 // separation between the frontend and the middle-end with regard to
12400 // how types are laid out.
12402 class Type_info_expression
: public Expression
12405 Type_info_expression(Type
* type
, Type_info type_info
)
12406 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
12407 type_(type
), type_info_(type_info
)
12415 do_determine_type(const Type_context
*)
12423 do_get_tree(Translate_context
* context
);
12426 // The type for which we are getting information.
12428 // What information we want.
12429 Type_info type_info_
;
12432 // The type is chosen to match what the type descriptor struct
12436 Type_info_expression::do_type()
12438 switch (this->type_info_
)
12440 case TYPE_INFO_SIZE
:
12441 return Type::lookup_integer_type("uintptr");
12442 case TYPE_INFO_ALIGNMENT
:
12443 case TYPE_INFO_FIELD_ALIGNMENT
:
12444 return Type::lookup_integer_type("uint8");
12450 // Return type information in GENERIC.
12453 Type_info_expression::do_get_tree(Translate_context
* context
)
12455 tree type_tree
= this->type_
->get_tree(context
->gogo());
12456 if (type_tree
== error_mark_node
)
12457 return error_mark_node
;
12459 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12460 gcc_assert(val_type_tree
!= error_mark_node
);
12462 if (this->type_info_
== TYPE_INFO_SIZE
)
12463 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12464 TYPE_SIZE_UNIT(type_tree
));
12468 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12469 val
= go_type_alignment(type_tree
);
12471 val
= go_field_alignment(type_tree
);
12472 return build_int_cstu(val_type_tree
, val
);
12476 // Make a type info expression.
12479 Expression::make_type_info(Type
* type
, Type_info type_info
)
12481 return new Type_info_expression(type
, type_info
);
12484 // An expression which evaluates to the offset of a field within a
12485 // struct. This, like Type_info_expression, q.v., is only used to
12486 // initialize fields of a type descriptor.
12488 class Struct_field_offset_expression
: public Expression
12491 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12492 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12493 type_(type
), field_(field
)
12499 { return Type::lookup_integer_type("uintptr"); }
12502 do_determine_type(const Type_context
*)
12510 do_get_tree(Translate_context
* context
);
12513 // The type of the struct.
12514 Struct_type
* type_
;
12516 const Struct_field
* field_
;
12519 // Return a struct field offset in GENERIC.
12522 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12524 tree type_tree
= this->type_
->get_tree(context
->gogo());
12525 if (type_tree
== error_mark_node
)
12526 return error_mark_node
;
12528 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12529 gcc_assert(val_type_tree
!= error_mark_node
);
12531 const Struct_field_list
* fields
= this->type_
->fields();
12532 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12533 Struct_field_list::const_iterator p
;
12534 for (p
= fields
->begin();
12535 p
!= fields
->end();
12536 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12538 gcc_assert(struct_field_tree
!= NULL_TREE
);
12539 if (&*p
== this->field_
)
12542 gcc_assert(&*p
== this->field_
);
12544 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12545 byte_position(struct_field_tree
));
12548 // Make an expression for a struct field offset.
12551 Expression::make_struct_field_offset(Struct_type
* type
,
12552 const Struct_field
* field
)
12554 return new Struct_field_offset_expression(type
, field
);
12557 // An expression which evaluates to the address of an unnamed label.
12559 class Label_addr_expression
: public Expression
12562 Label_addr_expression(Label
* label
, source_location location
)
12563 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12570 { return Type::make_pointer_type(Type::make_void_type()); }
12573 do_determine_type(const Type_context
*)
12578 { return new Label_addr_expression(this->label_
, this->location()); }
12581 do_get_tree(Translate_context
*)
12582 { return this->label_
->get_addr(this->location()); }
12585 // The label whose address we are taking.
12589 // Make an expression for the address of an unnamed label.
12592 Expression::make_label_addr(Label
* label
, source_location location
)
12594 return new Label_addr_expression(label
, location
);
12597 // Import an expression. This comes at the end in order to see the
12598 // various class definitions.
12601 Expression::import_expression(Import
* imp
)
12603 int c
= imp
->peek_char();
12604 if (imp
->match_c_string("- ")
12605 || imp
->match_c_string("! ")
12606 || imp
->match_c_string("^ "))
12607 return Unary_expression::do_import(imp
);
12609 return Binary_expression::do_import(imp
);
12610 else if (imp
->match_c_string("true")
12611 || imp
->match_c_string("false"))
12612 return Boolean_expression::do_import(imp
);
12614 return String_expression::do_import(imp
);
12615 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12617 // This handles integers, floats and complex constants.
12618 return Integer_expression::do_import(imp
);
12620 else if (imp
->match_c_string("nil"))
12621 return Nil_expression::do_import(imp
);
12622 else if (imp
->match_c_string("convert"))
12623 return Type_conversion_expression::do_import(imp
);
12626 error_at(imp
->location(), "import error: expected expression");
12627 return Expression::make_error(imp
->location());
12631 // Class Expression_list.
12633 // Traverse the list.
12636 Expression_list::traverse(Traverse
* traverse
)
12638 for (Expression_list::iterator p
= this->begin();
12644 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12645 return TRAVERSE_EXIT
;
12648 return TRAVERSE_CONTINUE
;
12654 Expression_list::copy()
12656 Expression_list
* ret
= new Expression_list();
12657 for (Expression_list::iterator p
= this->begin();
12662 ret
->push_back(NULL
);
12664 ret
->push_back((*p
)->copy());
12669 // Return whether an expression list has an error expression.
12672 Expression_list::contains_error() const
12674 for (Expression_list::const_iterator p
= this->begin();
12677 if (*p
!= NULL
&& (*p
)->is_error_expression())