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("float");
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("complex");
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(); }
2353 do_lower(Gogo
*, Named_object
*, int);
2356 do_is_constant() const
2360 do_integer_constant_value(bool, mpz_t val
, Type
**) const;
2363 do_float_constant_value(mpfr_t val
, Type
**) const;
2366 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2369 do_string_constant_value(std::string
* val
) const
2370 { return this->constant_
->const_value()->expr()->string_constant_value(val
); }
2375 // The type of a const is set by the declaration, not the use.
2377 do_determine_type(const Type_context
*);
2380 do_check_types(Gogo
*);
2387 do_get_tree(Translate_context
* context
);
2389 // When exporting a reference to a const as part of a const
2390 // expression, we export the value. We ignore the fact that it has
2393 do_export(Export
* exp
) const
2394 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2398 Named_object
* constant_
;
2399 // The type of this reference. This is used if the constant has an
2402 // Used to prevent infinite recursion when a constant incorrectly
2403 // refers to itself.
2407 // Lower a constant expression. This is where we convert the
2408 // predeclared constant iota into an integer value.
2411 Const_expression::do_lower(Gogo
* gogo
, Named_object
*, int iota_value
)
2413 if (this->constant_
->const_value()->expr()->classification()
2416 if (iota_value
== -1)
2418 error_at(this->location(),
2419 "iota is only defined in const declarations");
2423 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2424 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2430 // Make sure that the constant itself has been lowered.
2431 gogo
->lower_constant(this->constant_
);
2436 // Return an integer constant value.
2439 Const_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
2446 if (this->type_
!= NULL
)
2447 ctype
= this->type_
;
2449 ctype
= this->constant_
->const_value()->type();
2450 if (ctype
!= NULL
&& ctype
->integer_type() == NULL
)
2453 Expression
* e
= this->constant_
->const_value()->expr();
2458 bool r
= e
->integer_constant_value(iota_is_constant
, val
, &t
);
2460 this->seen_
= false;
2464 && !Integer_expression::check_constant(val
, ctype
, this->location()))
2467 *ptype
= ctype
!= NULL
? ctype
: t
;
2471 // Return a floating point constant value.
2474 Const_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
2480 if (this->type_
!= NULL
)
2481 ctype
= this->type_
;
2483 ctype
= this->constant_
->const_value()->type();
2484 if (ctype
!= NULL
&& ctype
->float_type() == NULL
)
2490 bool r
= this->constant_
->const_value()->expr()->float_constant_value(val
,
2493 this->seen_
= false;
2495 if (r
&& ctype
!= NULL
)
2497 if (!Float_expression::check_constant(val
, ctype
, this->location()))
2499 Float_expression::constrain_float(val
, ctype
);
2501 *ptype
= ctype
!= NULL
? ctype
: t
;
2505 // Return a complex constant value.
2508 Const_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2515 if (this->type_
!= NULL
)
2516 ctype
= this->type_
;
2518 ctype
= this->constant_
->const_value()->type();
2519 if (ctype
!= NULL
&& ctype
->complex_type() == NULL
)
2525 bool r
= this->constant_
->const_value()->expr()->complex_constant_value(real
,
2529 this->seen_
= false;
2531 if (r
&& ctype
!= NULL
)
2533 if (!Complex_expression::check_constant(real
, imag
, ctype
,
2536 Complex_expression::constrain_complex(real
, imag
, ctype
);
2538 *ptype
= ctype
!= NULL
? ctype
: t
;
2542 // Return the type of the const reference.
2545 Const_expression::do_type()
2547 if (this->type_
!= NULL
)
2550 Named_constant
* nc
= this->constant_
->const_value();
2552 if (this->seen_
|| nc
->lowering())
2554 this->report_error(_("constant refers to itself"));
2555 this->type_
= Type::make_error_type();
2561 Type
* ret
= nc
->type();
2565 this->seen_
= false;
2569 // During parsing, a named constant may have a NULL type, but we
2570 // must not return a NULL type here.
2571 ret
= nc
->expr()->type();
2573 this->seen_
= false;
2578 // Set the type of the const reference.
2581 Const_expression::do_determine_type(const Type_context
* context
)
2583 Type
* ctype
= this->constant_
->const_value()->type();
2584 Type
* cetype
= (ctype
!= NULL
2586 : this->constant_
->const_value()->expr()->type());
2587 if (ctype
!= NULL
&& !ctype
->is_abstract())
2589 else if (context
->type
!= NULL
2590 && (context
->type
->integer_type() != NULL
2591 || context
->type
->float_type() != NULL
2592 || context
->type
->complex_type() != NULL
)
2593 && (cetype
->integer_type() != NULL
2594 || cetype
->float_type() != NULL
2595 || cetype
->complex_type() != NULL
))
2596 this->type_
= context
->type
;
2597 else if (context
->type
!= NULL
2598 && context
->type
->is_string_type()
2599 && cetype
->is_string_type())
2600 this->type_
= context
->type
;
2601 else if (context
->type
!= NULL
2602 && context
->type
->is_boolean_type()
2603 && cetype
->is_boolean_type())
2604 this->type_
= context
->type
;
2605 else if (!context
->may_be_abstract
)
2607 if (cetype
->is_abstract())
2608 cetype
= cetype
->make_non_abstract_type();
2609 this->type_
= cetype
;
2613 // Check types of a const reference.
2616 Const_expression::do_check_types(Gogo
*)
2618 if (this->type_
!= NULL
&& this->type_
->is_error_type())
2621 Expression
* init
= this->constant_
->const_value()->expr();
2622 Find_named_object
find_named_object(this->constant_
);
2623 Expression::traverse(&init
, &find_named_object
);
2624 if (find_named_object
.found())
2626 this->report_error(_("constant refers to itself"));
2627 this->type_
= Type::make_error_type();
2631 if (this->type_
== NULL
|| this->type_
->is_abstract())
2634 // Check for integer overflow.
2635 if (this->type_
->integer_type() != NULL
)
2640 if (!this->integer_constant_value(true, ival
, &dummy
))
2644 Expression
* cexpr
= this->constant_
->const_value()->expr();
2645 if (cexpr
->float_constant_value(fval
, &dummy
))
2647 if (!mpfr_integer_p(fval
))
2648 this->report_error(_("floating point constant "
2649 "truncated to integer"));
2652 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2653 Integer_expression::check_constant(ival
, this->type_
,
2663 // Return a tree for the const reference.
2666 Const_expression::do_get_tree(Translate_context
* context
)
2668 Gogo
* gogo
= context
->gogo();
2670 if (this->type_
== NULL
)
2671 type_tree
= NULL_TREE
;
2674 type_tree
= this->type_
->get_tree(gogo
);
2675 if (type_tree
== error_mark_node
)
2676 return error_mark_node
;
2679 // If the type has been set for this expression, but the underlying
2680 // object is an abstract int or float, we try to get the abstract
2681 // value. Otherwise we may lose something in the conversion.
2682 if (this->type_
!= NULL
2683 && this->constant_
->const_value()->type()->is_abstract())
2685 Expression
* expr
= this->constant_
->const_value()->expr();
2689 if (expr
->integer_constant_value(true, ival
, &t
))
2691 tree ret
= Expression::integer_constant_tree(ival
, type_tree
);
2699 if (expr
->float_constant_value(fval
, &t
))
2701 tree ret
= Expression::float_constant_tree(fval
, type_tree
);
2708 if (expr
->complex_constant_value(fval
, imag
, &t
))
2710 tree ret
= Expression::complex_constant_tree(fval
, imag
, type_tree
);
2719 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2720 if (this->type_
== NULL
2721 || const_tree
== error_mark_node
2722 || TREE_TYPE(const_tree
) == error_mark_node
)
2726 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2727 ret
= fold_convert(type_tree
, const_tree
);
2728 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2729 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2730 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2731 ret
= fold(convert_to_real(type_tree
, const_tree
));
2732 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2733 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2739 // Make a reference to a constant in an expression.
2742 Expression::make_const_reference(Named_object
* constant
,
2743 source_location location
)
2745 return new Const_expression(constant
, location
);
2748 // Find a named object in an expression.
2751 Find_named_object::expression(Expression
** pexpr
)
2753 switch ((*pexpr
)->classification())
2755 case Expression::EXPRESSION_CONST_REFERENCE
:
2756 if (static_cast<Const_expression
*>(*pexpr
)->named_object() == this->no_
)
2758 return TRAVERSE_CONTINUE
;
2759 case Expression::EXPRESSION_VAR_REFERENCE
:
2760 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
2762 return TRAVERSE_CONTINUE
;
2763 case Expression::EXPRESSION_FUNC_REFERENCE
:
2764 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
2766 return TRAVERSE_CONTINUE
;
2768 return TRAVERSE_CONTINUE
;
2770 this->found_
= true;
2771 return TRAVERSE_EXIT
;
2776 class Nil_expression
: public Expression
2779 Nil_expression(source_location location
)
2780 : Expression(EXPRESSION_NIL
, location
)
2788 do_is_constant() const
2793 { return Type::make_nil_type(); }
2796 do_determine_type(const Type_context
*)
2804 do_get_tree(Translate_context
*)
2805 { return null_pointer_node
; }
2808 do_export(Export
* exp
) const
2809 { exp
->write_c_string("nil"); }
2812 // Import a nil expression.
2815 Nil_expression::do_import(Import
* imp
)
2817 imp
->require_c_string("nil");
2818 return Expression::make_nil(imp
->location());
2821 // Make a nil expression.
2824 Expression::make_nil(source_location location
)
2826 return new Nil_expression(location
);
2829 // The value of the predeclared constant iota. This is little more
2830 // than a marker. This will be lowered to an integer in
2831 // Const_expression::do_lower, which is where we know the value that
2834 class Iota_expression
: public Parser_expression
2837 Iota_expression(source_location location
)
2838 : Parser_expression(EXPRESSION_IOTA
, location
)
2843 do_lower(Gogo
*, Named_object
*, int)
2844 { gcc_unreachable(); }
2846 // There should only ever be one of these.
2849 { gcc_unreachable(); }
2852 // Make an iota expression. This is only called for one case: the
2853 // value of the predeclared constant iota.
2856 Expression::make_iota()
2858 static Iota_expression
iota_expression(UNKNOWN_LOCATION
);
2859 return &iota_expression
;
2862 // A type conversion expression.
2864 class Type_conversion_expression
: public Expression
2867 Type_conversion_expression(Type
* type
, Expression
* expr
,
2868 source_location location
)
2869 : Expression(EXPRESSION_CONVERSION
, location
),
2870 type_(type
), expr_(expr
), may_convert_function_types_(false)
2873 // Return the type to which we are converting.
2876 { return this->type_
; }
2878 // Return the expression which we are converting.
2881 { return this->expr_
; }
2883 // Permit converting from one function type to another. This is
2884 // used internally for method expressions.
2886 set_may_convert_function_types()
2888 this->may_convert_function_types_
= true;
2891 // Import a type conversion expression.
2897 do_traverse(Traverse
* traverse
);
2900 do_lower(Gogo
*, Named_object
*, int);
2903 do_is_constant() const
2904 { return this->expr_
->is_constant(); }
2907 do_integer_constant_value(bool, mpz_t
, Type
**) const;
2910 do_float_constant_value(mpfr_t
, Type
**) const;
2913 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
2916 do_string_constant_value(std::string
*) const;
2920 { return this->type_
; }
2923 do_determine_type(const Type_context
*)
2925 Type_context
subcontext(this->type_
, false);
2926 this->expr_
->determine_type(&subcontext
);
2930 do_check_types(Gogo
*);
2935 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
2940 do_get_tree(Translate_context
* context
);
2943 do_export(Export
*) const;
2946 // The type to convert to.
2948 // The expression to convert.
2950 // True if this is permitted to convert function types. This is
2951 // used internally for method expressions.
2952 bool may_convert_function_types_
;
2958 Type_conversion_expression::do_traverse(Traverse
* traverse
)
2960 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
2961 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
2962 return TRAVERSE_EXIT
;
2963 return TRAVERSE_CONTINUE
;
2966 // Convert to a constant at lowering time.
2969 Type_conversion_expression::do_lower(Gogo
*, Named_object
*, int)
2971 Type
* type
= this->type_
;
2972 Expression
* val
= this->expr_
;
2973 source_location location
= this->location();
2975 if (type
->integer_type() != NULL
)
2980 if (val
->integer_constant_value(false, ival
, &dummy
))
2982 if (!Integer_expression::check_constant(ival
, type
, location
))
2983 mpz_set_ui(ival
, 0);
2984 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2991 if (val
->float_constant_value(fval
, &dummy
))
2993 if (!mpfr_integer_p(fval
))
2996 "floating point constant truncated to integer");
2997 return Expression::make_error(location
);
2999 mpfr_get_z(ival
, fval
, GMP_RNDN
);
3000 if (!Integer_expression::check_constant(ival
, type
, location
))
3001 mpz_set_ui(ival
, 0);
3002 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
3011 if (type
->float_type() != NULL
)
3016 if (val
->float_constant_value(fval
, &dummy
))
3018 if (!Float_expression::check_constant(fval
, type
, location
))
3019 mpfr_set_ui(fval
, 0, GMP_RNDN
);
3020 Float_expression::constrain_float(fval
, type
);
3021 Expression
*ret
= Expression::make_float(&fval
, type
, location
);
3028 if (type
->complex_type() != NULL
)
3035 if (val
->complex_constant_value(real
, imag
, &dummy
))
3037 if (!Complex_expression::check_constant(real
, imag
, type
, location
))
3039 mpfr_set_ui(real
, 0, GMP_RNDN
);
3040 mpfr_set_ui(imag
, 0, GMP_RNDN
);
3042 Complex_expression::constrain_complex(real
, imag
, type
);
3043 Expression
* ret
= Expression::make_complex(&real
, &imag
, type
,
3053 if (type
->is_open_array_type() && type
->named_type() == NULL
)
3055 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3056 bool is_byte
= element_type
== Type::lookup_integer_type("uint8");
3057 bool is_int
= element_type
== Type::lookup_integer_type("int");
3058 if (is_byte
|| is_int
)
3061 if (val
->string_constant_value(&s
))
3063 Expression_list
* vals
= new Expression_list();
3066 for (std::string::const_iterator p
= s
.begin();
3071 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
3072 Expression
* v
= Expression::make_integer(&val
,
3081 const char *p
= s
.data();
3082 const char *pend
= s
.data() + s
.length();
3086 int adv
= Lex::fetch_char(p
, &c
);
3089 warning_at(this->location(), 0,
3090 "invalid UTF-8 encoding");
3095 mpz_init_set_ui(val
, c
);
3096 Expression
* v
= Expression::make_integer(&val
,
3104 return Expression::make_slice_composite_literal(type
, vals
,
3113 // Return the constant integer value if there is one.
3116 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant
,
3120 if (this->type_
->integer_type() == NULL
)
3126 if (this->expr_
->integer_constant_value(iota_is_constant
, ival
, &dummy
))
3128 if (!Integer_expression::check_constant(ival
, this->type_
,
3136 *ptype
= this->type_
;
3143 if (this->expr_
->float_constant_value(fval
, &dummy
))
3145 mpfr_get_z(val
, fval
, GMP_RNDN
);
3147 if (!Integer_expression::check_constant(val
, this->type_
,
3150 *ptype
= this->type_
;
3158 // Return the constant floating point value if there is one.
3161 Type_conversion_expression::do_float_constant_value(mpfr_t val
,
3164 if (this->type_
->float_type() == NULL
)
3170 if (this->expr_
->float_constant_value(fval
, &dummy
))
3172 if (!Float_expression::check_constant(fval
, this->type_
,
3178 mpfr_set(val
, fval
, GMP_RNDN
);
3180 Float_expression::constrain_float(val
, this->type_
);
3181 *ptype
= this->type_
;
3189 // Return the constant complex value if there is one.
3192 Type_conversion_expression::do_complex_constant_value(mpfr_t real
,
3196 if (this->type_
->complex_type() == NULL
)
3204 if (this->expr_
->complex_constant_value(rval
, ival
, &dummy
))
3206 if (!Complex_expression::check_constant(rval
, ival
, this->type_
,
3213 mpfr_set(real
, rval
, GMP_RNDN
);
3214 mpfr_set(imag
, ival
, GMP_RNDN
);
3217 Complex_expression::constrain_complex(real
, imag
, this->type_
);
3218 *ptype
= this->type_
;
3227 // Return the constant string value if there is one.
3230 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3232 if (this->type_
->is_string_type()
3233 && this->expr_
->type()->integer_type() != NULL
)
3238 if (this->expr_
->integer_constant_value(false, ival
, &dummy
))
3240 unsigned long ulval
= mpz_get_ui(ival
);
3241 if (mpz_cmp_ui(ival
, ulval
) == 0)
3243 Lex::append_char(ulval
, true, val
, this->location());
3251 // FIXME: Could handle conversion from const []int here.
3256 // Check that types are convertible.
3259 Type_conversion_expression::do_check_types(Gogo
*)
3261 Type
* type
= this->type_
;
3262 Type
* expr_type
= this->expr_
->type();
3265 if (type
->is_error_type()
3266 || type
->is_undefined()
3267 || expr_type
->is_error_type()
3268 || expr_type
->is_undefined())
3270 // Make sure we emit an error for an undefined type.
3273 this->set_is_error();
3277 if (this->may_convert_function_types_
3278 && type
->function_type() != NULL
3279 && expr_type
->function_type() != NULL
)
3282 if (Type::are_convertible(type
, expr_type
, &reason
))
3285 error_at(this->location(), "%s", reason
.c_str());
3286 this->set_is_error();
3289 // Get a tree for a type conversion.
3292 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3294 Gogo
* gogo
= context
->gogo();
3295 tree type_tree
= this->type_
->get_tree(gogo
);
3296 tree expr_tree
= this->expr_
->get_tree(context
);
3298 if (type_tree
== error_mark_node
3299 || expr_tree
== error_mark_node
3300 || TREE_TYPE(expr_tree
) == error_mark_node
)
3301 return error_mark_node
;
3303 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3304 return fold_convert(type_tree
, expr_tree
);
3306 Type
* type
= this->type_
;
3307 Type
* expr_type
= this->expr_
->type();
3309 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3310 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3311 expr_tree
, this->location());
3312 else if (type
->integer_type() != NULL
)
3314 if (expr_type
->integer_type() != NULL
3315 || expr_type
->float_type() != NULL
3316 || expr_type
->is_unsafe_pointer_type())
3317 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3321 else if (type
->float_type() != NULL
)
3323 if (expr_type
->integer_type() != NULL
3324 || expr_type
->float_type() != NULL
)
3325 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3329 else if (type
->complex_type() != NULL
)
3331 if (expr_type
->complex_type() != NULL
)
3332 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3336 else if (type
->is_string_type()
3337 && expr_type
->integer_type() != NULL
)
3339 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3340 if (host_integerp(expr_tree
, 0))
3342 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3344 Lex::append_char(intval
, true, &s
, this->location());
3345 Expression
* se
= Expression::make_string(s
, this->location());
3346 return se
->get_tree(context
);
3349 static tree int_to_string_fndecl
;
3350 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3352 "__go_int_to_string",
3356 fold_convert(integer_type_node
, expr_tree
));
3358 else if (type
->is_string_type()
3359 && (expr_type
->array_type() != NULL
3360 || (expr_type
->points_to() != NULL
3361 && expr_type
->points_to()->array_type() != NULL
)))
3363 Type
* t
= expr_type
;
3364 if (t
->points_to() != NULL
)
3367 expr_tree
= build_fold_indirect_ref(expr_tree
);
3369 if (!DECL_P(expr_tree
))
3370 expr_tree
= save_expr(expr_tree
);
3371 Array_type
* a
= t
->array_type();
3372 Type
* e
= a
->element_type()->forwarded();
3373 gcc_assert(e
->integer_type() != NULL
);
3374 tree valptr
= fold_convert(const_ptr_type_node
,
3375 a
->value_pointer_tree(gogo
, expr_tree
));
3376 tree len
= a
->length_tree(gogo
, expr_tree
);
3377 len
= fold_convert_loc(this->location(), size_type_node
, len
);
3378 if (e
->integer_type()->is_unsigned()
3379 && e
->integer_type()->bits() == 8)
3381 static tree byte_array_to_string_fndecl
;
3382 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3384 "__go_byte_array_to_string",
3387 const_ptr_type_node
,
3394 gcc_assert(e
== Type::lookup_integer_type("int"));
3395 static tree int_array_to_string_fndecl
;
3396 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3398 "__go_int_array_to_string",
3401 const_ptr_type_node
,
3407 else if (type
->is_open_array_type() && expr_type
->is_string_type())
3409 Type
* e
= type
->array_type()->element_type()->forwarded();
3410 gcc_assert(e
->integer_type() != NULL
);
3411 if (e
->integer_type()->is_unsigned()
3412 && e
->integer_type()->bits() == 8)
3414 static tree string_to_byte_array_fndecl
;
3415 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3417 "__go_string_to_byte_array",
3420 TREE_TYPE(expr_tree
),
3425 gcc_assert(e
== Type::lookup_integer_type("int"));
3426 static tree string_to_int_array_fndecl
;
3427 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3429 "__go_string_to_int_array",
3432 TREE_TYPE(expr_tree
),
3436 else if ((type
->is_unsafe_pointer_type()
3437 && expr_type
->points_to() != NULL
)
3438 || (expr_type
->is_unsafe_pointer_type()
3439 && type
->points_to() != NULL
))
3440 ret
= fold_convert(type_tree
, expr_tree
);
3441 else if (type
->is_unsafe_pointer_type()
3442 && expr_type
->integer_type() != NULL
)
3443 ret
= convert_to_pointer(type_tree
, expr_tree
);
3444 else if (this->may_convert_function_types_
3445 && type
->function_type() != NULL
3446 && expr_type
->function_type() != NULL
)
3447 ret
= fold_convert_loc(this->location(), type_tree
, expr_tree
);
3449 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3450 expr_tree
, this->location());
3455 // Output a type conversion in a constant expression.
3458 Type_conversion_expression::do_export(Export
* exp
) const
3460 exp
->write_c_string("convert(");
3461 exp
->write_type(this->type_
);
3462 exp
->write_c_string(", ");
3463 this->expr_
->export_expression(exp
);
3464 exp
->write_c_string(")");
3467 // Import a type conversion or a struct construction.
3470 Type_conversion_expression::do_import(Import
* imp
)
3472 imp
->require_c_string("convert(");
3473 Type
* type
= imp
->read_type();
3474 imp
->require_c_string(", ");
3475 Expression
* val
= Expression::import_expression(imp
);
3476 imp
->require_c_string(")");
3477 return Expression::make_cast(type
, val
, imp
->location());
3480 // Make a type cast expression.
3483 Expression::make_cast(Type
* type
, Expression
* val
, source_location location
)
3485 if (type
->is_error_type() || val
->is_error_expression())
3486 return Expression::make_error(location
);
3487 return new Type_conversion_expression(type
, val
, location
);
3490 // Unary expressions.
3492 class Unary_expression
: public Expression
3495 Unary_expression(Operator op
, Expression
* expr
, source_location location
)
3496 : Expression(EXPRESSION_UNARY
, location
),
3497 op_(op
), escapes_(true), expr_(expr
)
3500 // Return the operator.
3503 { return this->op_
; }
3505 // Return the operand.
3508 { return this->expr_
; }
3510 // Record that an address expression does not escape.
3512 set_does_not_escape()
3514 gcc_assert(this->op_
== OPERATOR_AND
);
3515 this->escapes_
= false;
3518 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3519 // could be done, false if not.
3521 eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3524 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3525 // could be done, false if not.
3527 eval_float(Operator op
, mpfr_t uval
, mpfr_t val
);
3529 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3530 // true if this could be done, false if not.
3532 eval_complex(Operator op
, mpfr_t ureal
, mpfr_t uimag
, mpfr_t real
,
3540 do_traverse(Traverse
* traverse
)
3541 { return Expression::traverse(&this->expr_
, traverse
); }
3544 do_lower(Gogo
*, Named_object
*, int);
3547 do_is_constant() const;
3550 do_integer_constant_value(bool, mpz_t
, Type
**) const;
3553 do_float_constant_value(mpfr_t
, Type
**) const;
3556 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
3562 do_determine_type(const Type_context
*);
3565 do_check_types(Gogo
*);
3570 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3575 do_is_addressable() const
3576 { return this->op_
== OPERATOR_MULT
; }
3579 do_get_tree(Translate_context
*);
3582 do_export(Export
*) const;
3585 // The unary operator to apply.
3587 // Normally true. False if this is an address expression which does
3588 // not escape the current function.
3594 // If we are taking the address of a composite literal, and the
3595 // contents are not constant, then we want to make a heap composite
3599 Unary_expression::do_lower(Gogo
*, Named_object
*, int)
3601 source_location loc
= this->location();
3602 Operator op
= this->op_
;
3603 Expression
* expr
= this->expr_
;
3605 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3606 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3608 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3609 // moving x to the heap. FIXME: Is it worth doing a real escape
3610 // analysis here? This case is found in math/unsafe.go and is
3611 // therefore worth special casing.
3612 if (op
== OPERATOR_MULT
)
3614 Expression
* e
= expr
;
3615 while (e
->classification() == EXPRESSION_CONVERSION
)
3617 Type_conversion_expression
* te
3618 = static_cast<Type_conversion_expression
*>(e
);
3622 if (e
->classification() == EXPRESSION_UNARY
)
3624 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3625 if (ue
->op_
== OPERATOR_AND
)
3632 ue
->set_does_not_escape();
3637 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
3638 || op
== OPERATOR_NOT
|| op
== OPERATOR_XOR
)
3640 Expression
* ret
= NULL
;
3645 if (expr
->integer_constant_value(false, eval
, &etype
))
3649 if (Unary_expression::eval_integer(op
, etype
, eval
, val
, loc
))
3650 ret
= Expression::make_integer(&val
, etype
, loc
);
3657 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
)
3662 if (expr
->float_constant_value(fval
, &ftype
))
3666 if (Unary_expression::eval_float(op
, fval
, val
))
3667 ret
= Expression::make_float(&val
, ftype
, loc
);
3678 if (expr
->complex_constant_value(fval
, ival
, &ftype
))
3684 if (Unary_expression::eval_complex(op
, fval
, ival
, real
, imag
))
3685 ret
= Expression::make_complex(&real
, &imag
, ftype
, loc
);
3699 // Return whether a unary expression is a constant.
3702 Unary_expression::do_is_constant() const
3704 if (this->op_
== OPERATOR_MULT
)
3706 // Indirecting through a pointer is only constant if the object
3707 // to which the expression points is constant, but we currently
3708 // have no way to determine that.
3711 else if (this->op_
== OPERATOR_AND
)
3713 // Taking the address of a variable is constant if it is a
3714 // global variable, not constant otherwise. In other cases
3715 // taking the address is probably not a constant.
3716 Var_expression
* ve
= this->expr_
->var_expression();
3719 Named_object
* no
= ve
->named_object();
3720 return no
->is_variable() && no
->var_value()->is_global();
3725 return this->expr_
->is_constant();
3728 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3729 // UVAL, if known; it may be NULL. Return true if this could be done,
3733 Unary_expression::eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3734 source_location location
)
3741 case OPERATOR_MINUS
:
3743 return Integer_expression::check_constant(val
, utype
, location
);
3745 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3749 || utype
->integer_type() == NULL
3750 || utype
->integer_type()->is_abstract())
3754 // The number of HOST_WIDE_INTs that it takes to represent
3756 size_t count
= ((mpz_sizeinbase(uval
, 2)
3757 + HOST_BITS_PER_WIDE_INT
3759 / HOST_BITS_PER_WIDE_INT
);
3761 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3762 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3765 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3766 gcc_assert(ecount
<= count
);
3768 // Trim down to the number of words required by the type.
3769 size_t obits
= utype
->integer_type()->bits();
3770 if (!utype
->integer_type()->is_unsigned())
3772 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3773 / HOST_BITS_PER_WIDE_INT
);
3774 gcc_assert(ocount
<= ocount
);
3776 for (size_t i
= 0; i
< ocount
; ++i
)
3779 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3781 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3784 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3788 return Integer_expression::check_constant(val
, utype
, location
);
3797 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3798 // could be done, false if not.
3801 Unary_expression::eval_float(Operator op
, mpfr_t uval
, mpfr_t val
)
3806 mpfr_set(val
, uval
, GMP_RNDN
);
3808 case OPERATOR_MINUS
:
3809 mpfr_neg(val
, uval
, GMP_RNDN
);
3821 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3822 // if this could be done, false if not.
3825 Unary_expression::eval_complex(Operator op
, mpfr_t rval
, mpfr_t ival
,
3826 mpfr_t real
, mpfr_t imag
)
3831 mpfr_set(real
, rval
, GMP_RNDN
);
3832 mpfr_set(imag
, ival
, GMP_RNDN
);
3834 case OPERATOR_MINUS
:
3835 mpfr_neg(real
, rval
, GMP_RNDN
);
3836 mpfr_neg(imag
, ival
, GMP_RNDN
);
3848 // Return the integral constant value of a unary expression, if it has one.
3851 Unary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
3857 if (!this->expr_
->integer_constant_value(iota_is_constant
, uval
, ptype
))
3860 ret
= Unary_expression::eval_integer(this->op_
, *ptype
, uval
, val
,
3866 // Return the floating point constant value of a unary expression, if
3870 Unary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
3875 if (!this->expr_
->float_constant_value(uval
, ptype
))
3878 ret
= Unary_expression::eval_float(this->op_
, uval
, val
);
3883 // Return the complex constant value of a unary expression, if it has
3887 Unary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
3895 if (!this->expr_
->complex_constant_value(rval
, ival
, ptype
))
3898 ret
= Unary_expression::eval_complex(this->op_
, rval
, ival
, real
, imag
);
3904 // Return the type of a unary expression.
3907 Unary_expression::do_type()
3912 case OPERATOR_MINUS
:
3915 return this->expr_
->type();
3918 return Type::make_pointer_type(this->expr_
->type());
3922 Type
* subtype
= this->expr_
->type();
3923 Type
* points_to
= subtype
->points_to();
3924 if (points_to
== NULL
)
3925 return Type::make_error_type();
3934 // Determine abstract types for a unary expression.
3937 Unary_expression::do_determine_type(const Type_context
* context
)
3942 case OPERATOR_MINUS
:
3945 this->expr_
->determine_type(context
);
3949 // Taking the address of something.
3951 Type
* subtype
= (context
->type
== NULL
3953 : context
->type
->points_to());
3954 Type_context
subcontext(subtype
, false);
3955 this->expr_
->determine_type(&subcontext
);
3960 // Indirecting through a pointer.
3962 Type
* subtype
= (context
->type
== NULL
3964 : Type::make_pointer_type(context
->type
));
3965 Type_context
subcontext(subtype
, false);
3966 this->expr_
->determine_type(&subcontext
);
3975 // Check types for a unary expression.
3978 Unary_expression::do_check_types(Gogo
*)
3980 Type
* type
= this->expr_
->type();
3981 if (type
->is_error_type())
3983 this->set_is_error();
3990 case OPERATOR_MINUS
:
3991 if (type
->integer_type() == NULL
3992 && type
->float_type() == NULL
3993 && type
->complex_type() == NULL
)
3994 this->report_error(_("expected numeric type"));
3999 if (type
->integer_type() == NULL
4000 && !type
->is_boolean_type())
4001 this->report_error(_("expected integer or boolean type"));
4005 if (!this->expr_
->is_addressable())
4006 this->report_error(_("invalid operand for unary %<&%>"));
4008 this->expr_
->address_taken(this->escapes_
);
4012 // Indirecting through a pointer.
4013 if (type
->points_to() == NULL
)
4014 this->report_error(_("expected pointer"));
4022 // Get a tree for a unary expression.
4025 Unary_expression::do_get_tree(Translate_context
* context
)
4027 tree expr
= this->expr_
->get_tree(context
);
4028 if (expr
== error_mark_node
)
4029 return error_mark_node
;
4031 source_location loc
= this->location();
4037 case OPERATOR_MINUS
:
4039 tree type
= TREE_TYPE(expr
);
4040 tree compute_type
= excess_precision_type(type
);
4041 if (compute_type
!= NULL_TREE
)
4042 expr
= ::convert(compute_type
, expr
);
4043 tree ret
= fold_build1_loc(loc
, NEGATE_EXPR
,
4044 (compute_type
!= NULL_TREE
4048 if (compute_type
!= NULL_TREE
)
4049 ret
= ::convert(type
, ret
);
4054 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
4055 return fold_build1_loc(loc
, TRUTH_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4057 return fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, expr
,
4058 build_int_cst(TREE_TYPE(expr
), 0));
4061 return fold_build1_loc(loc
, BIT_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4064 // We should not see a non-constant constructor here; cases
4065 // where we would see one should have been moved onto the heap
4066 // at parse time. Taking the address of a nonconstant
4067 // constructor will not do what the programmer expects.
4068 gcc_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
4069 gcc_assert(TREE_CODE(expr
) != ADDR_EXPR
);
4071 // Build a decl for a constant constructor.
4072 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
4074 tree decl
= build_decl(this->location(), VAR_DECL
,
4075 create_tmp_var_name("C"), TREE_TYPE(expr
));
4076 DECL_EXTERNAL(decl
) = 0;
4077 TREE_PUBLIC(decl
) = 0;
4078 TREE_READONLY(decl
) = 1;
4079 TREE_CONSTANT(decl
) = 1;
4080 TREE_STATIC(decl
) = 1;
4081 TREE_ADDRESSABLE(decl
) = 1;
4082 DECL_ARTIFICIAL(decl
) = 1;
4083 DECL_INITIAL(decl
) = expr
;
4084 rest_of_decl_compilation(decl
, 1, 0);
4088 return build_fold_addr_expr_loc(loc
, expr
);
4092 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
4094 // If we are dereferencing the pointer to a large struct, we
4095 // need to check for nil. We don't bother to check for small
4096 // structs because we expect the system to crash on a nil
4097 // pointer dereference.
4098 HOST_WIDE_INT s
= int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr
)));
4099 if (s
== -1 || s
>= 4096)
4102 expr
= save_expr(expr
);
4103 tree compare
= fold_build2_loc(loc
, EQ_EXPR
, boolean_type_node
,
4105 fold_convert(TREE_TYPE(expr
),
4106 null_pointer_node
));
4107 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4109 expr
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(expr
),
4110 build3(COND_EXPR
, void_type_node
,
4111 compare
, crash
, NULL_TREE
),
4115 // If the type of EXPR is a recursive pointer type, then we
4116 // need to insert a cast before indirecting.
4117 if (TREE_TYPE(TREE_TYPE(expr
)) == ptr_type_node
)
4119 Type
* pt
= this->expr_
->type()->points_to();
4120 tree ind
= pt
->get_tree(context
->gogo());
4121 expr
= fold_convert_loc(loc
, build_pointer_type(ind
), expr
);
4124 return build_fold_indirect_ref_loc(loc
, expr
);
4132 // Export a unary expression.
4135 Unary_expression::do_export(Export
* exp
) const
4140 exp
->write_c_string("+ ");
4142 case OPERATOR_MINUS
:
4143 exp
->write_c_string("- ");
4146 exp
->write_c_string("! ");
4149 exp
->write_c_string("^ ");
4156 this->expr_
->export_expression(exp
);
4159 // Import a unary expression.
4162 Unary_expression::do_import(Import
* imp
)
4165 switch (imp
->get_char())
4171 op
= OPERATOR_MINUS
;
4182 imp
->require_c_string(" ");
4183 Expression
* expr
= Expression::import_expression(imp
);
4184 return Expression::make_unary(op
, expr
, imp
->location());
4187 // Make a unary expression.
4190 Expression::make_unary(Operator op
, Expression
* expr
, source_location location
)
4192 return new Unary_expression(op
, expr
, location
);
4195 // If this is an indirection through a pointer, return the expression
4196 // being pointed through. Otherwise return this.
4201 if (this->classification_
== EXPRESSION_UNARY
)
4203 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4204 if (ue
->op() == OPERATOR_MULT
)
4205 return ue
->operand();
4210 // Class Binary_expression.
4215 Binary_expression::do_traverse(Traverse
* traverse
)
4217 int t
= Expression::traverse(&this->left_
, traverse
);
4218 if (t
== TRAVERSE_EXIT
)
4219 return TRAVERSE_EXIT
;
4220 return Expression::traverse(&this->right_
, traverse
);
4223 // Compare integer constants according to OP.
4226 Binary_expression::compare_integer(Operator op
, mpz_t left_val
,
4229 int i
= mpz_cmp(left_val
, right_val
);
4234 case OPERATOR_NOTEQ
:
4249 // Compare floating point constants according to OP.
4252 Binary_expression::compare_float(Operator op
, Type
* type
, mpfr_t left_val
,
4257 i
= mpfr_cmp(left_val
, right_val
);
4261 mpfr_init_set(lv
, left_val
, GMP_RNDN
);
4263 mpfr_init_set(rv
, right_val
, GMP_RNDN
);
4264 Float_expression::constrain_float(lv
, type
);
4265 Float_expression::constrain_float(rv
, type
);
4266 i
= mpfr_cmp(lv
, rv
);
4274 case OPERATOR_NOTEQ
:
4289 // Compare complex constants according to OP. Complex numbers may
4290 // only be compared for equality.
4293 Binary_expression::compare_complex(Operator op
, Type
* type
,
4294 mpfr_t left_real
, mpfr_t left_imag
,
4295 mpfr_t right_real
, mpfr_t right_imag
)
4299 is_equal
= (mpfr_cmp(left_real
, right_real
) == 0
4300 && mpfr_cmp(left_imag
, right_imag
) == 0);
4305 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4306 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4309 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4310 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4311 Complex_expression::constrain_complex(lr
, li
, type
);
4312 Complex_expression::constrain_complex(rr
, ri
, type
);
4313 is_equal
= mpfr_cmp(lr
, rr
) == 0 && mpfr_cmp(li
, ri
) == 0;
4323 case OPERATOR_NOTEQ
:
4330 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4331 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4332 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4333 // this could be done, false if not.
4336 Binary_expression::eval_integer(Operator op
, Type
* left_type
, mpz_t left_val
,
4337 Type
* right_type
, mpz_t right_val
,
4338 source_location location
, mpz_t val
)
4340 bool is_shift_op
= false;
4344 case OPERATOR_ANDAND
:
4346 case OPERATOR_NOTEQ
:
4351 // These return boolean values. We should probably handle them
4352 // anyhow in case a type conversion is used on the result.
4355 mpz_add(val
, left_val
, right_val
);
4357 case OPERATOR_MINUS
:
4358 mpz_sub(val
, left_val
, right_val
);
4361 mpz_ior(val
, left_val
, right_val
);
4364 mpz_xor(val
, left_val
, right_val
);
4367 mpz_mul(val
, left_val
, right_val
);
4370 if (mpz_sgn(right_val
) != 0)
4371 mpz_tdiv_q(val
, left_val
, right_val
);
4374 error_at(location
, "division by zero");
4380 if (mpz_sgn(right_val
) != 0)
4381 mpz_tdiv_r(val
, left_val
, right_val
);
4384 error_at(location
, "division by zero");
4389 case OPERATOR_LSHIFT
:
4391 unsigned long shift
= mpz_get_ui(right_val
);
4392 if (mpz_cmp_ui(right_val
, shift
) != 0)
4394 error_at(location
, "shift count overflow");
4398 mpz_mul_2exp(val
, left_val
, shift
);
4403 case OPERATOR_RSHIFT
:
4405 unsigned long shift
= mpz_get_ui(right_val
);
4406 if (mpz_cmp_ui(right_val
, shift
) != 0)
4408 error_at(location
, "shift count overflow");
4412 if (mpz_cmp_ui(left_val
, 0) >= 0)
4413 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4415 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4421 mpz_and(val
, left_val
, right_val
);
4423 case OPERATOR_BITCLEAR
:
4427 mpz_com(tval
, right_val
);
4428 mpz_and(val
, left_val
, tval
);
4436 Type
* type
= left_type
;
4441 else if (type
!= right_type
&& right_type
!= NULL
)
4443 if (type
->is_abstract())
4445 else if (!right_type
->is_abstract())
4447 // This look like a type error which should be diagnosed
4448 // elsewhere. Don't do anything here, to avoid an
4449 // unhelpful chain of error messages.
4455 if (type
!= NULL
&& !type
->is_abstract())
4457 // We have to check the operands too, as we have implicitly
4458 // coerced them to TYPE.
4459 if ((type
!= left_type
4460 && !Integer_expression::check_constant(left_val
, type
, location
))
4462 && type
!= right_type
4463 && !Integer_expression::check_constant(right_val
, type
,
4465 || !Integer_expression::check_constant(val
, type
, location
))
4472 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4473 // Return true if this could be done, false if not.
4476 Binary_expression::eval_float(Operator op
, Type
* left_type
, mpfr_t left_val
,
4477 Type
* right_type
, mpfr_t right_val
,
4478 mpfr_t val
, source_location location
)
4483 case OPERATOR_ANDAND
:
4485 case OPERATOR_NOTEQ
:
4490 // These return boolean values. We should probably handle them
4491 // anyhow in case a type conversion is used on the result.
4494 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4496 case OPERATOR_MINUS
:
4497 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4502 case OPERATOR_BITCLEAR
:
4505 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4508 if (mpfr_zero_p(right_val
))
4509 error_at(location
, "division by zero");
4510 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4514 case OPERATOR_LSHIFT
:
4515 case OPERATOR_RSHIFT
:
4521 Type
* type
= left_type
;
4524 else if (type
!= right_type
&& right_type
!= NULL
)
4526 if (type
->is_abstract())
4528 else if (!right_type
->is_abstract())
4530 // This looks like a type error which should be diagnosed
4531 // elsewhere. Don't do anything here, to avoid an unhelpful
4532 // chain of error messages.
4537 if (type
!= NULL
&& !type
->is_abstract())
4539 if ((type
!= left_type
4540 && !Float_expression::check_constant(left_val
, type
, location
))
4541 || (type
!= right_type
4542 && !Float_expression::check_constant(right_val
, type
,
4544 || !Float_expression::check_constant(val
, type
, location
))
4545 mpfr_set_ui(val
, 0, GMP_RNDN
);
4551 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4552 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4553 // could be done, false if not.
4556 Binary_expression::eval_complex(Operator op
, Type
* left_type
,
4557 mpfr_t left_real
, mpfr_t left_imag
,
4559 mpfr_t right_real
, mpfr_t right_imag
,
4560 mpfr_t real
, mpfr_t imag
,
4561 source_location location
)
4566 case OPERATOR_ANDAND
:
4568 case OPERATOR_NOTEQ
:
4573 // These return boolean values and must be handled differently.
4576 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4577 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4579 case OPERATOR_MINUS
:
4580 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4581 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4586 case OPERATOR_BITCLEAR
:
4590 // You might think that multiplying two complex numbers would
4591 // be simple, and you would be right, until you start to think
4592 // about getting the right answer for infinity. If one
4593 // operand here is infinity and the other is anything other
4594 // than zero or NaN, then we are going to wind up subtracting
4595 // two infinity values. That will give us a NaN, but the
4596 // correct answer is infinity.
4600 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4604 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4608 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4612 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4614 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4615 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4617 // If we get NaN on both sides, check whether it should really
4618 // be infinity. The rule is that if either side of the
4619 // complex number is infinity, then the whole value is
4620 // infinity, even if the other side is NaN. So the only case
4621 // we have to fix is the one in which both sides are NaN.
4622 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4623 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4624 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4626 bool is_infinity
= false;
4630 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4631 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4635 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4636 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4638 // If the left side is infinity, then the result is
4640 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4642 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4643 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4644 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4645 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4648 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4649 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4653 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4654 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4659 // If the right side is infinity, then the result is
4661 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4663 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4664 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4665 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4666 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4669 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4670 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4674 mpfr_set_ui(li
, 0, GMP_RNDN
);
4675 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4680 // If we got an overflow in the intermediate computations,
4681 // then the result is infinity.
4683 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4684 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4688 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4689 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4693 mpfr_set_ui(li
, 0, GMP_RNDN
);
4694 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4698 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4699 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4703 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4704 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4711 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4712 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4713 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4714 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4715 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4716 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4717 mpfr_set_inf(real
, mpfr_sgn(real
));
4718 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4735 // For complex division we want to avoid having an
4736 // intermediate overflow turn the whole result in a NaN. We
4737 // scale the values to try to avoid this.
4739 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4740 error_at(location
, "division by zero");
4746 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4747 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4750 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4754 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4755 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4757 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4759 ilogbw
= mpfr_get_exp(t
);
4760 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4761 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4766 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4767 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4768 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4770 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4771 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4772 mpfr_add(real
, real
, t
, GMP_RNDN
);
4773 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4774 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4776 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4777 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4778 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4779 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4780 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4782 // If we wind up with NaN on both sides, check whether we
4783 // should really have infinity. The rule is that if either
4784 // side of the complex number is infinity, then the whole
4785 // value is infinity, even if the other side is NaN. So the
4786 // only case we have to fix is the one in which both sides are
4788 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4789 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4790 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4792 if (mpfr_zero_p(denom
))
4794 mpfr_set_inf(real
, mpfr_sgn(rr
));
4795 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4796 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4797 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4799 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4800 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4802 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4803 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4806 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4807 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4811 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
4815 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
4817 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4818 mpfr_set_inf(real
, mpfr_sgn(t3
));
4820 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
4821 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
4822 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4823 mpfr_set_inf(imag
, mpfr_sgn(t3
));
4829 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
4830 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
4832 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4833 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
4836 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4837 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
4841 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
4845 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
4847 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4848 mpfr_set_ui(real
, 0, GMP_RNDN
);
4849 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
4851 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
4852 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
4853 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4854 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4855 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
4873 case OPERATOR_LSHIFT
:
4874 case OPERATOR_RSHIFT
:
4880 Type
* type
= left_type
;
4883 else if (type
!= right_type
&& right_type
!= NULL
)
4885 if (type
->is_abstract())
4887 else if (!right_type
->is_abstract())
4889 // This looks like a type error which should be diagnosed
4890 // elsewhere. Don't do anything here, to avoid an unhelpful
4891 // chain of error messages.
4896 if (type
!= NULL
&& !type
->is_abstract())
4898 if ((type
!= left_type
4899 && !Complex_expression::check_constant(left_real
, left_imag
,
4901 || (type
!= right_type
4902 && !Complex_expression::check_constant(right_real
, right_imag
,
4904 || !Complex_expression::check_constant(real
, imag
, type
,
4907 mpfr_set_ui(real
, 0, GMP_RNDN
);
4908 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4915 // Lower a binary expression. We have to evaluate constant
4916 // expressions now, in order to implement Go's unlimited precision
4920 Binary_expression::do_lower(Gogo
*, Named_object
*, int)
4922 source_location location
= this->location();
4923 Operator op
= this->op_
;
4924 Expression
* left
= this->left_
;
4925 Expression
* right
= this->right_
;
4927 const bool is_comparison
= (op
== OPERATOR_EQEQ
4928 || op
== OPERATOR_NOTEQ
4929 || op
== OPERATOR_LT
4930 || op
== OPERATOR_LE
4931 || op
== OPERATOR_GT
4932 || op
== OPERATOR_GE
);
4934 // Integer constant expressions.
4940 mpz_init(right_val
);
4942 if (left
->integer_constant_value(false, left_val
, &left_type
)
4943 && right
->integer_constant_value(false, right_val
, &right_type
))
4945 Expression
* ret
= NULL
;
4946 if (left_type
!= right_type
4947 && left_type
!= NULL
4948 && right_type
!= NULL
4949 && left_type
->base() != right_type
->base()
4950 && op
!= OPERATOR_LSHIFT
4951 && op
!= OPERATOR_RSHIFT
)
4953 // May be a type error--let it be diagnosed later.
4955 else if (is_comparison
)
4957 bool b
= Binary_expression::compare_integer(op
, left_val
,
4959 ret
= Expression::make_cast(Type::lookup_bool_type(),
4960 Expression::make_boolean(b
, location
),
4968 if (Binary_expression::eval_integer(op
, left_type
, left_val
,
4969 right_type
, right_val
,
4972 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
);
4974 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
4976 else if (left_type
== NULL
)
4978 else if (right_type
== NULL
)
4980 else if (!left_type
->is_abstract()
4981 && left_type
->named_type() != NULL
)
4983 else if (!right_type
->is_abstract()
4984 && right_type
->named_type() != NULL
)
4986 else if (!left_type
->is_abstract())
4988 else if (!right_type
->is_abstract())
4990 else if (left_type
->float_type() != NULL
)
4992 else if (right_type
->float_type() != NULL
)
4994 else if (left_type
->complex_type() != NULL
)
4996 else if (right_type
->complex_type() != NULL
)
5000 ret
= Expression::make_integer(&val
, type
, location
);
5008 mpz_clear(right_val
);
5009 mpz_clear(left_val
);
5013 mpz_clear(right_val
);
5014 mpz_clear(left_val
);
5017 // Floating point constant expressions.
5020 mpfr_init(left_val
);
5023 mpfr_init(right_val
);
5025 if (left
->float_constant_value(left_val
, &left_type
)
5026 && right
->float_constant_value(right_val
, &right_type
))
5028 Expression
* ret
= NULL
;
5029 if (left_type
!= right_type
5030 && left_type
!= NULL
5031 && right_type
!= NULL
5032 && left_type
->base() != right_type
->base()
5033 && op
!= OPERATOR_LSHIFT
5034 && op
!= OPERATOR_RSHIFT
)
5036 // May be a type error--let it be diagnosed later.
5038 else if (is_comparison
)
5040 bool b
= Binary_expression::compare_float(op
,
5044 left_val
, right_val
);
5045 ret
= Expression::make_boolean(b
, location
);
5052 if (Binary_expression::eval_float(op
, left_type
, left_val
,
5053 right_type
, right_val
, val
,
5056 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5057 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5059 if (left_type
== NULL
)
5061 else if (right_type
== NULL
)
5063 else if (!left_type
->is_abstract()
5064 && left_type
->named_type() != NULL
)
5066 else if (!right_type
->is_abstract()
5067 && right_type
->named_type() != NULL
)
5069 else if (!left_type
->is_abstract())
5071 else if (!right_type
->is_abstract())
5073 else if (left_type
->float_type() != NULL
)
5075 else if (right_type
->float_type() != NULL
)
5079 ret
= Expression::make_float(&val
, type
, location
);
5087 mpfr_clear(right_val
);
5088 mpfr_clear(left_val
);
5092 mpfr_clear(right_val
);
5093 mpfr_clear(left_val
);
5096 // Complex constant expressions.
5100 mpfr_init(left_real
);
5101 mpfr_init(left_imag
);
5106 mpfr_init(right_real
);
5107 mpfr_init(right_imag
);
5110 if (left
->complex_constant_value(left_real
, left_imag
, &left_type
)
5111 && right
->complex_constant_value(right_real
, right_imag
, &right_type
))
5113 Expression
* ret
= NULL
;
5114 if (left_type
!= right_type
5115 && left_type
!= NULL
5116 && right_type
!= NULL
5117 && left_type
->base() != right_type
->base())
5119 // May be a type error--let it be diagnosed later.
5121 else if (is_comparison
)
5123 bool b
= Binary_expression::compare_complex(op
,
5131 ret
= Expression::make_boolean(b
, location
);
5140 if (Binary_expression::eval_complex(op
, left_type
,
5141 left_real
, left_imag
,
5143 right_real
, right_imag
,
5147 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5148 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5150 if (left_type
== NULL
)
5152 else if (right_type
== NULL
)
5154 else if (!left_type
->is_abstract()
5155 && left_type
->named_type() != NULL
)
5157 else if (!right_type
->is_abstract()
5158 && right_type
->named_type() != NULL
)
5160 else if (!left_type
->is_abstract())
5162 else if (!right_type
->is_abstract())
5164 else if (left_type
->complex_type() != NULL
)
5166 else if (right_type
->complex_type() != NULL
)
5170 ret
= Expression::make_complex(&real
, &imag
, type
,
5179 mpfr_clear(left_real
);
5180 mpfr_clear(left_imag
);
5181 mpfr_clear(right_real
);
5182 mpfr_clear(right_imag
);
5187 mpfr_clear(left_real
);
5188 mpfr_clear(left_imag
);
5189 mpfr_clear(right_real
);
5190 mpfr_clear(right_imag
);
5193 // String constant expressions.
5194 if (op
== OPERATOR_PLUS
5195 && left
->type()->is_string_type()
5196 && right
->type()->is_string_type())
5198 std::string left_string
;
5199 std::string right_string
;
5200 if (left
->string_constant_value(&left_string
)
5201 && right
->string_constant_value(&right_string
))
5202 return Expression::make_string(left_string
+ right_string
, location
);
5208 // Return the integer constant value, if it has one.
5211 Binary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
5217 if (!this->left_
->integer_constant_value(iota_is_constant
, left_val
,
5220 mpz_clear(left_val
);
5225 mpz_init(right_val
);
5227 if (!this->right_
->integer_constant_value(iota_is_constant
, right_val
,
5230 mpz_clear(right_val
);
5231 mpz_clear(left_val
);
5236 if (left_type
!= right_type
5237 && left_type
!= NULL
5238 && right_type
!= NULL
5239 && left_type
->base() != right_type
->base()
5240 && this->op_
!= OPERATOR_RSHIFT
5241 && this->op_
!= OPERATOR_LSHIFT
)
5244 ret
= Binary_expression::eval_integer(this->op_
, left_type
, left_val
,
5245 right_type
, right_val
,
5246 this->location(), val
);
5248 mpz_clear(right_val
);
5249 mpz_clear(left_val
);
5257 // Return the floating point constant value, if it has one.
5260 Binary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
5263 mpfr_init(left_val
);
5265 if (!this->left_
->float_constant_value(left_val
, &left_type
))
5267 mpfr_clear(left_val
);
5272 mpfr_init(right_val
);
5274 if (!this->right_
->float_constant_value(right_val
, &right_type
))
5276 mpfr_clear(right_val
);
5277 mpfr_clear(left_val
);
5282 if (left_type
!= right_type
5283 && left_type
!= NULL
5284 && right_type
!= NULL
5285 && left_type
->base() != right_type
->base())
5288 ret
= Binary_expression::eval_float(this->op_
, left_type
, left_val
,
5289 right_type
, right_val
,
5290 val
, this->location());
5292 mpfr_clear(left_val
);
5293 mpfr_clear(right_val
);
5301 // Return the complex constant value, if it has one.
5304 Binary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
5309 mpfr_init(left_real
);
5310 mpfr_init(left_imag
);
5312 if (!this->left_
->complex_constant_value(left_real
, left_imag
, &left_type
))
5314 mpfr_clear(left_real
);
5315 mpfr_clear(left_imag
);
5321 mpfr_init(right_real
);
5322 mpfr_init(right_imag
);
5324 if (!this->right_
->complex_constant_value(right_real
, right_imag
,
5327 mpfr_clear(left_real
);
5328 mpfr_clear(left_imag
);
5329 mpfr_clear(right_real
);
5330 mpfr_clear(right_imag
);
5335 if (left_type
!= right_type
5336 && left_type
!= NULL
5337 && right_type
!= NULL
5338 && left_type
->base() != right_type
->base())
5341 ret
= Binary_expression::eval_complex(this->op_
, left_type
,
5342 left_real
, left_imag
,
5344 right_real
, right_imag
,
5347 mpfr_clear(left_real
);
5348 mpfr_clear(left_imag
);
5349 mpfr_clear(right_real
);
5350 mpfr_clear(right_imag
);
5358 // Note that the value is being discarded.
5361 Binary_expression::do_discarding_value()
5363 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5364 this->right_
->discarding_value();
5366 this->warn_about_unused_value();
5372 Binary_expression::do_type()
5377 case OPERATOR_ANDAND
:
5379 case OPERATOR_NOTEQ
:
5384 return Type::lookup_bool_type();
5387 case OPERATOR_MINUS
:
5394 case OPERATOR_BITCLEAR
:
5396 Type
* left_type
= this->left_
->type();
5397 Type
* right_type
= this->right_
->type();
5398 if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5400 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5402 else if (!left_type
->is_abstract())
5404 else if (!right_type
->is_abstract())
5406 else if (left_type
->complex_type() != NULL
)
5408 else if (right_type
->complex_type() != NULL
)
5410 else if (left_type
->float_type() != NULL
)
5412 else if (right_type
->float_type() != NULL
)
5418 case OPERATOR_LSHIFT
:
5419 case OPERATOR_RSHIFT
:
5420 return this->left_
->type();
5427 // Set type for a binary expression.
5430 Binary_expression::do_determine_type(const Type_context
* context
)
5432 Type
* tleft
= this->left_
->type();
5433 Type
* tright
= this->right_
->type();
5435 // Both sides should have the same type, except for the shift
5436 // operations. For a comparison, we should ignore the incoming
5439 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5440 || this->op_
== OPERATOR_RSHIFT
);
5442 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5443 || this->op_
== OPERATOR_NOTEQ
5444 || this->op_
== OPERATOR_LT
5445 || this->op_
== OPERATOR_LE
5446 || this->op_
== OPERATOR_GT
5447 || this->op_
== OPERATOR_GE
);
5449 Type_context
subcontext(*context
);
5453 // In a comparison, the context does not determine the types of
5455 subcontext
.type
= NULL
;
5458 // Set the context for the left hand operand.
5461 // The right hand operand plays no role in determining the type
5462 // of the left hand operand. A shift of an abstract integer in
5463 // a string context gets special treatment, which may be a
5465 if (subcontext
.type
!= NULL
5466 && subcontext
.type
->is_string_type()
5467 && tleft
->is_abstract())
5468 error_at(this->location(), "shift of non-integer operand");
5470 else if (!tleft
->is_abstract())
5471 subcontext
.type
= tleft
;
5472 else if (!tright
->is_abstract())
5473 subcontext
.type
= tright
;
5474 else if (subcontext
.type
== NULL
)
5476 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5477 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5478 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5480 // Both sides have an abstract integer, abstract float, or
5481 // abstract complex type. Just let CONTEXT determine
5482 // whether they may remain abstract or not.
5484 else if (tleft
->complex_type() != NULL
)
5485 subcontext
.type
= tleft
;
5486 else if (tright
->complex_type() != NULL
)
5487 subcontext
.type
= tright
;
5488 else if (tleft
->float_type() != NULL
)
5489 subcontext
.type
= tleft
;
5490 else if (tright
->float_type() != NULL
)
5491 subcontext
.type
= tright
;
5493 subcontext
.type
= tleft
;
5496 this->left_
->determine_type(&subcontext
);
5498 // The context for the right hand operand is the same as for the
5499 // left hand operand, except for a shift operator.
5502 subcontext
.type
= Type::lookup_integer_type("uint");
5503 subcontext
.may_be_abstract
= false;
5506 this->right_
->determine_type(&subcontext
);
5509 // Report an error if the binary operator OP does not support TYPE.
5510 // Return whether the operation is OK. This should not be used for
5514 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5515 source_location location
)
5520 case OPERATOR_ANDAND
:
5521 if (!type
->is_boolean_type())
5523 error_at(location
, "expected boolean type");
5529 case OPERATOR_NOTEQ
:
5530 if (type
->integer_type() == NULL
5531 && type
->float_type() == NULL
5532 && type
->complex_type() == NULL
5533 && !type
->is_string_type()
5534 && type
->points_to() == NULL
5535 && !type
->is_nil_type()
5536 && !type
->is_boolean_type()
5537 && type
->interface_type() == NULL
5538 && (type
->array_type() == NULL
5539 || type
->array_type()->length() != NULL
)
5540 && type
->map_type() == NULL
5541 && type
->channel_type() == NULL
5542 && type
->function_type() == NULL
)
5545 ("expected integer, floating, complex, string, pointer, "
5546 "boolean, interface, slice, map, channel, "
5547 "or function type"));
5556 if (type
->integer_type() == NULL
5557 && type
->float_type() == NULL
5558 && !type
->is_string_type())
5560 error_at(location
, "expected integer, floating, or string type");
5566 case OPERATOR_PLUSEQ
:
5567 if (type
->integer_type() == NULL
5568 && type
->float_type() == NULL
5569 && type
->complex_type() == NULL
5570 && !type
->is_string_type())
5573 "expected integer, floating, complex, or string type");
5578 case OPERATOR_MINUS
:
5579 case OPERATOR_MINUSEQ
:
5581 case OPERATOR_MULTEQ
:
5583 case OPERATOR_DIVEQ
:
5584 if (type
->integer_type() == NULL
5585 && type
->float_type() == NULL
5586 && type
->complex_type() == NULL
)
5588 error_at(location
, "expected integer, floating, or complex type");
5594 case OPERATOR_MODEQ
:
5598 case OPERATOR_ANDEQ
:
5600 case OPERATOR_XOREQ
:
5601 case OPERATOR_BITCLEAR
:
5602 case OPERATOR_BITCLEAREQ
:
5603 if (type
->integer_type() == NULL
)
5605 error_at(location
, "expected integer type");
5620 Binary_expression::do_check_types(Gogo
*)
5622 Type
* left_type
= this->left_
->type();
5623 Type
* right_type
= this->right_
->type();
5624 if (left_type
->is_error_type() || right_type
->is_error_type())
5626 this->set_is_error();
5630 if (this->op_
== OPERATOR_EQEQ
5631 || this->op_
== OPERATOR_NOTEQ
5632 || this->op_
== OPERATOR_LT
5633 || this->op_
== OPERATOR_LE
5634 || this->op_
== OPERATOR_GT
5635 || this->op_
== OPERATOR_GE
)
5637 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5638 && !Type::are_assignable(right_type
, left_type
, NULL
))
5640 this->report_error(_("incompatible types in binary expression"));
5643 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5645 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5648 this->set_is_error();
5652 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5654 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5656 this->report_error(_("incompatible types in binary expression"));
5659 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5662 this->set_is_error();
5668 if (left_type
->integer_type() == NULL
)
5669 this->report_error(_("shift of non-integer operand"));
5671 if (!right_type
->is_abstract()
5672 && (right_type
->integer_type() == NULL
5673 || !right_type
->integer_type()->is_unsigned()))
5674 this->report_error(_("shift count not unsigned integer"));
5680 if (this->right_
->integer_constant_value(true, val
, &type
))
5682 if (mpz_sgn(val
) < 0)
5683 this->report_error(_("negative shift count"));
5690 // Get a tree for a binary expression.
5693 Binary_expression::do_get_tree(Translate_context
* context
)
5695 tree left
= this->left_
->get_tree(context
);
5696 tree right
= this->right_
->get_tree(context
);
5698 if (left
== error_mark_node
|| right
== error_mark_node
)
5699 return error_mark_node
;
5701 enum tree_code code
;
5702 bool use_left_type
= true;
5703 bool is_shift_op
= false;
5707 case OPERATOR_NOTEQ
:
5712 return Expression::comparison_tree(context
, this->op_
,
5713 this->left_
->type(), left
,
5714 this->right_
->type(), right
,
5718 code
= TRUTH_ORIF_EXPR
;
5719 use_left_type
= false;
5721 case OPERATOR_ANDAND
:
5722 code
= TRUTH_ANDIF_EXPR
;
5723 use_left_type
= false;
5728 case OPERATOR_MINUS
:
5732 code
= BIT_IOR_EXPR
;
5735 code
= BIT_XOR_EXPR
;
5742 Type
*t
= this->left_
->type();
5743 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5746 code
= TRUNC_DIV_EXPR
;
5750 code
= TRUNC_MOD_EXPR
;
5752 case OPERATOR_LSHIFT
:
5756 case OPERATOR_RSHIFT
:
5761 code
= BIT_AND_EXPR
;
5763 case OPERATOR_BITCLEAR
:
5764 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5765 code
= BIT_AND_EXPR
;
5771 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5773 if (this->left_
->type()->is_string_type())
5775 gcc_assert(this->op_
== OPERATOR_PLUS
);
5776 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5777 static tree string_plus_decl
;
5778 return Gogo::call_builtin(&string_plus_decl
,
5789 tree compute_type
= excess_precision_type(type
);
5790 if (compute_type
!= NULL_TREE
)
5792 left
= ::convert(compute_type
, left
);
5793 right
= ::convert(compute_type
, right
);
5796 tree eval_saved
= NULL_TREE
;
5800 left
= save_expr(left
);
5802 right
= save_expr(right
);
5803 // Make sure the values are evaluated.
5804 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5805 void_type_node
, left
, right
);
5808 tree ret
= fold_build2_loc(this->location(),
5810 compute_type
!= NULL_TREE
? compute_type
: type
,
5813 if (compute_type
!= NULL_TREE
)
5814 ret
= ::convert(type
, ret
);
5816 // In Go, a shift larger than the size of the type is well-defined.
5817 // This is not true in GENERIC, so we need to insert a conditional.
5820 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5821 gcc_assert(this->left_
->type()->integer_type() != NULL
);
5822 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5824 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5825 build_int_cst_type(TREE_TYPE(right
), bits
));
5827 tree overflow_result
= fold_convert_loc(this->location(),
5830 if (this->op_
== OPERATOR_RSHIFT
5831 && !this->left_
->type()->integer_type()->is_unsigned())
5833 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
5834 boolean_type_node
, left
,
5835 fold_convert_loc(this->location(),
5837 integer_zero_node
));
5838 tree neg_one
= fold_build2_loc(this->location(),
5839 MINUS_EXPR
, TREE_TYPE(left
),
5840 fold_convert_loc(this->location(),
5843 fold_convert_loc(this->location(),
5846 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
5847 TREE_TYPE(left
), neg
, neg_one
,
5851 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
5852 compare
, ret
, overflow_result
);
5854 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5855 TREE_TYPE(ret
), eval_saved
, ret
);
5861 // Export a binary expression.
5864 Binary_expression::do_export(Export
* exp
) const
5866 exp
->write_c_string("(");
5867 this->left_
->export_expression(exp
);
5871 exp
->write_c_string(" || ");
5873 case OPERATOR_ANDAND
:
5874 exp
->write_c_string(" && ");
5877 exp
->write_c_string(" == ");
5879 case OPERATOR_NOTEQ
:
5880 exp
->write_c_string(" != ");
5883 exp
->write_c_string(" < ");
5886 exp
->write_c_string(" <= ");
5889 exp
->write_c_string(" > ");
5892 exp
->write_c_string(" >= ");
5895 exp
->write_c_string(" + ");
5897 case OPERATOR_MINUS
:
5898 exp
->write_c_string(" - ");
5901 exp
->write_c_string(" | ");
5904 exp
->write_c_string(" ^ ");
5907 exp
->write_c_string(" * ");
5910 exp
->write_c_string(" / ");
5913 exp
->write_c_string(" % ");
5915 case OPERATOR_LSHIFT
:
5916 exp
->write_c_string(" << ");
5918 case OPERATOR_RSHIFT
:
5919 exp
->write_c_string(" >> ");
5922 exp
->write_c_string(" & ");
5924 case OPERATOR_BITCLEAR
:
5925 exp
->write_c_string(" &^ ");
5930 this->right_
->export_expression(exp
);
5931 exp
->write_c_string(")");
5934 // Import a binary expression.
5937 Binary_expression::do_import(Import
* imp
)
5939 imp
->require_c_string("(");
5941 Expression
* left
= Expression::import_expression(imp
);
5944 if (imp
->match_c_string(" || "))
5949 else if (imp
->match_c_string(" && "))
5951 op
= OPERATOR_ANDAND
;
5954 else if (imp
->match_c_string(" == "))
5959 else if (imp
->match_c_string(" != "))
5961 op
= OPERATOR_NOTEQ
;
5964 else if (imp
->match_c_string(" < "))
5969 else if (imp
->match_c_string(" <= "))
5974 else if (imp
->match_c_string(" > "))
5979 else if (imp
->match_c_string(" >= "))
5984 else if (imp
->match_c_string(" + "))
5989 else if (imp
->match_c_string(" - "))
5991 op
= OPERATOR_MINUS
;
5994 else if (imp
->match_c_string(" | "))
5999 else if (imp
->match_c_string(" ^ "))
6004 else if (imp
->match_c_string(" * "))
6009 else if (imp
->match_c_string(" / "))
6014 else if (imp
->match_c_string(" % "))
6019 else if (imp
->match_c_string(" << "))
6021 op
= OPERATOR_LSHIFT
;
6024 else if (imp
->match_c_string(" >> "))
6026 op
= OPERATOR_RSHIFT
;
6029 else if (imp
->match_c_string(" & "))
6034 else if (imp
->match_c_string(" &^ "))
6036 op
= OPERATOR_BITCLEAR
;
6041 error_at(imp
->location(), "unrecognized binary operator");
6042 return Expression::make_error(imp
->location());
6045 Expression
* right
= Expression::import_expression(imp
);
6047 imp
->require_c_string(")");
6049 return Expression::make_binary(op
, left
, right
, imp
->location());
6052 // Make a binary expression.
6055 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6056 source_location location
)
6058 return new Binary_expression(op
, left
, right
, location
);
6061 // Implement a comparison.
6064 Expression::comparison_tree(Translate_context
* context
, Operator op
,
6065 Type
* left_type
, tree left_tree
,
6066 Type
* right_type
, tree right_tree
,
6067 source_location location
)
6069 enum tree_code code
;
6075 case OPERATOR_NOTEQ
:
6094 if (left_type
->is_string_type() && right_type
->is_string_type())
6096 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
6097 static tree string_compare_decl
;
6098 left_tree
= Gogo::call_builtin(&string_compare_decl
,
6107 right_tree
= build_int_cst_type(integer_type_node
, 0);
6109 else if ((left_type
->interface_type() != NULL
6110 && right_type
->interface_type() == NULL
6111 && !right_type
->is_nil_type())
6112 || (left_type
->interface_type() == NULL
6113 && !left_type
->is_nil_type()
6114 && right_type
->interface_type() != NULL
))
6116 // Comparing an interface value to a non-interface value.
6117 if (left_type
->interface_type() == NULL
)
6119 std::swap(left_type
, right_type
);
6120 std::swap(left_tree
, right_tree
);
6123 // The right operand is not an interface. We need to take its
6124 // address if it is not a pointer.
6127 if (right_type
->points_to() != NULL
)
6129 make_tmp
= NULL_TREE
;
6132 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
6134 make_tmp
= NULL_TREE
;
6135 arg
= build_fold_addr_expr_loc(location
, right_tree
);
6136 if (DECL_P(right_tree
))
6137 TREE_ADDRESSABLE(right_tree
) = 1;
6141 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
6142 get_name(right_tree
));
6143 DECL_IGNORED_P(tmp
) = 0;
6144 DECL_INITIAL(tmp
) = right_tree
;
6145 TREE_ADDRESSABLE(tmp
) = 1;
6146 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6147 SET_EXPR_LOCATION(make_tmp
, location
);
6148 arg
= build_fold_addr_expr_loc(location
, tmp
);
6150 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
6152 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
6154 if (left_type
->interface_type()->is_empty())
6156 static tree empty_interface_value_compare_decl
;
6157 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6159 "__go_empty_interface_value_compare",
6162 TREE_TYPE(left_tree
),
6164 TREE_TYPE(descriptor
),
6168 if (left_tree
== error_mark_node
)
6169 return error_mark_node
;
6170 // This can panic if the type is not comparable.
6171 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6175 static tree interface_value_compare_decl
;
6176 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6178 "__go_interface_value_compare",
6181 TREE_TYPE(left_tree
),
6183 TREE_TYPE(descriptor
),
6187 if (left_tree
== error_mark_node
)
6188 return error_mark_node
;
6189 // This can panic if the type is not comparable.
6190 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6192 right_tree
= build_int_cst_type(integer_type_node
, 0);
6194 if (make_tmp
!= NULL_TREE
)
6195 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6198 else if (left_type
->interface_type() != NULL
6199 && right_type
->interface_type() != NULL
)
6201 if (left_type
->interface_type()->is_empty())
6203 gcc_assert(right_type
->interface_type()->is_empty());
6204 static tree empty_interface_compare_decl
;
6205 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6207 "__go_empty_interface_compare",
6210 TREE_TYPE(left_tree
),
6212 TREE_TYPE(right_tree
),
6214 if (left_tree
== error_mark_node
)
6215 return error_mark_node
;
6216 // This can panic if the type is uncomparable.
6217 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6221 gcc_assert(!right_type
->interface_type()->is_empty());
6222 static tree interface_compare_decl
;
6223 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6225 "__go_interface_compare",
6228 TREE_TYPE(left_tree
),
6230 TREE_TYPE(right_tree
),
6232 if (left_tree
== error_mark_node
)
6233 return error_mark_node
;
6234 // This can panic if the type is uncomparable.
6235 TREE_NOTHROW(interface_compare_decl
) = 0;
6237 right_tree
= build_int_cst_type(integer_type_node
, 0);
6240 if (left_type
->is_nil_type()
6241 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6243 std::swap(left_type
, right_type
);
6244 std::swap(left_tree
, right_tree
);
6247 if (right_type
->is_nil_type())
6249 if (left_type
->array_type() != NULL
6250 && left_type
->array_type()->length() == NULL
)
6252 Array_type
* at
= left_type
->array_type();
6253 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6254 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6256 else if (left_type
->interface_type() != NULL
)
6258 // An interface is nil if the first field is nil.
6259 tree left_type_tree
= TREE_TYPE(left_tree
);
6260 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6261 tree field
= TYPE_FIELDS(left_type_tree
);
6262 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6264 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6268 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6269 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6273 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6274 return error_mark_node
;
6276 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6277 if (CAN_HAVE_LOCATION_P(ret
))
6278 SET_EXPR_LOCATION(ret
, location
);
6282 // Class Bound_method_expression.
6287 Bound_method_expression::do_traverse(Traverse
* traverse
)
6289 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6290 return TRAVERSE_EXIT
;
6291 return Expression::traverse(&this->method_
, traverse
);
6294 // Return the type of a bound method expression. The type of this
6295 // object is really the type of the method with no receiver. We
6296 // should be able to get away with just returning the type of the
6300 Bound_method_expression::do_type()
6302 return this->method_
->type();
6305 // Determine the types of a method expression.
6308 Bound_method_expression::do_determine_type(const Type_context
*)
6310 this->method_
->determine_type_no_context();
6311 Type
* mtype
= this->method_
->type();
6312 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6313 if (fntype
== NULL
|| !fntype
->is_method())
6314 this->expr_
->determine_type_no_context();
6317 Type_context
subcontext(fntype
->receiver()->type(), false);
6318 this->expr_
->determine_type(&subcontext
);
6322 // Check the types of a method expression.
6325 Bound_method_expression::do_check_types(Gogo
*)
6327 Type
* type
= this->method_
->type()->deref();
6329 || type
->function_type() == NULL
6330 || !type
->function_type()->is_method())
6331 this->report_error(_("object is not a method"));
6334 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6335 Type
* etype
= (this->expr_type_
!= NULL
6337 : this->expr_
->type());
6338 etype
= etype
->deref();
6339 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6340 this->report_error(_("method type does not match object type"));
6344 // Get the tree for a method expression. There is no standard tree
6345 // representation for this. The only places it may currently be used
6346 // are in a Call_expression or a Go_statement, which will take it
6347 // apart directly. So this has nothing to do at present.
6350 Bound_method_expression::do_get_tree(Translate_context
*)
6355 // Make a method expression.
6357 Bound_method_expression
*
6358 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6359 source_location location
)
6361 return new Bound_method_expression(expr
, method
, location
);
6364 // Class Builtin_call_expression. This is used for a call to a
6365 // builtin function.
6367 class Builtin_call_expression
: public Call_expression
6370 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6371 bool is_varargs
, source_location location
);
6374 // This overrides Call_expression::do_lower.
6376 do_lower(Gogo
*, Named_object
*, int);
6379 do_is_constant() const;
6382 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6385 do_float_constant_value(mpfr_t
, Type
**) const;
6388 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6394 do_determine_type(const Type_context
*);
6397 do_check_types(Gogo
*);
6402 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6403 this->args()->copy(),
6409 do_get_tree(Translate_context
*);
6412 do_export(Export
*) const;
6415 do_is_recover_call() const;
6418 do_set_recover_arg(Expression
*);
6421 // The builtin functions.
6422 enum Builtin_function_code
6426 // Predeclared builtin functions.
6443 // Builtin functions from the unsafe package.
6456 real_imag_type(Type
*);
6461 // A pointer back to the general IR structure. This avoids a global
6462 // variable, or passing it around everywhere.
6464 // The builtin function being called.
6465 Builtin_function_code code_
;
6466 // Used to stop endless loops when the length of an array uses len
6467 // or cap of the array itself.
6471 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6473 Expression_list
* args
,
6475 source_location location
)
6476 : Call_expression(fn
, args
, is_varargs
, location
),
6477 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false)
6479 Func_expression
* fnexp
= this->fn()->func_expression();
6480 gcc_assert(fnexp
!= NULL
);
6481 const std::string
& name(fnexp
->named_object()->name());
6482 if (name
== "append")
6483 this->code_
= BUILTIN_APPEND
;
6484 else if (name
== "cap")
6485 this->code_
= BUILTIN_CAP
;
6486 else if (name
== "close")
6487 this->code_
= BUILTIN_CLOSE
;
6488 else if (name
== "closed")
6489 this->code_
= BUILTIN_CLOSED
;
6490 else if (name
== "cmplx")
6491 this->code_
= BUILTIN_CMPLX
;
6492 else if (name
== "copy")
6493 this->code_
= BUILTIN_COPY
;
6494 else if (name
== "imag")
6495 this->code_
= BUILTIN_IMAG
;
6496 else if (name
== "len")
6497 this->code_
= BUILTIN_LEN
;
6498 else if (name
== "make")
6499 this->code_
= BUILTIN_MAKE
;
6500 else if (name
== "new")
6501 this->code_
= BUILTIN_NEW
;
6502 else if (name
== "panic")
6503 this->code_
= BUILTIN_PANIC
;
6504 else if (name
== "print")
6505 this->code_
= BUILTIN_PRINT
;
6506 else if (name
== "println")
6507 this->code_
= BUILTIN_PRINTLN
;
6508 else if (name
== "real")
6509 this->code_
= BUILTIN_REAL
;
6510 else if (name
== "recover")
6511 this->code_
= BUILTIN_RECOVER
;
6512 else if (name
== "Alignof")
6513 this->code_
= BUILTIN_ALIGNOF
;
6514 else if (name
== "Offsetof")
6515 this->code_
= BUILTIN_OFFSETOF
;
6516 else if (name
== "Sizeof")
6517 this->code_
= BUILTIN_SIZEOF
;
6522 // Return whether this is a call to recover. This is a virtual
6523 // function called from the parent class.
6526 Builtin_call_expression::do_is_recover_call() const
6528 if (this->classification() == EXPRESSION_ERROR
)
6530 return this->code_
== BUILTIN_RECOVER
;
6533 // Set the argument for a call to recover.
6536 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6538 const Expression_list
* args
= this->args();
6539 gcc_assert(args
== NULL
|| args
->empty());
6540 Expression_list
* new_args
= new Expression_list();
6541 new_args
->push_back(arg
);
6542 this->set_args(new_args
);
6545 // A traversal class which looks for a call expression.
6547 class Find_call_expression
: public Traverse
6550 Find_call_expression()
6551 : Traverse(traverse_expressions
),
6556 expression(Expression
**);
6560 { return this->found_
; }
6567 Find_call_expression::expression(Expression
** pexpr
)
6569 if ((*pexpr
)->call_expression() != NULL
)
6571 this->found_
= true;
6572 return TRAVERSE_EXIT
;
6574 return TRAVERSE_CONTINUE
;
6577 // Lower a builtin call expression. This turns new and make into
6578 // specific expressions. We also convert to a constant if we can.
6581 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6583 if (this->code_
== BUILTIN_NEW
)
6585 const Expression_list
* args
= this->args();
6586 if (args
== NULL
|| args
->size() < 1)
6587 this->report_error(_("not enough arguments"));
6588 else if (args
->size() > 1)
6589 this->report_error(_("too many arguments"));
6592 Expression
* arg
= args
->front();
6593 if (!arg
->is_type_expression())
6595 error_at(arg
->location(), "expected type");
6596 this->set_is_error();
6599 return Expression::make_allocation(arg
->type(), this->location());
6602 else if (this->code_
== BUILTIN_MAKE
)
6604 const Expression_list
* args
= this->args();
6605 if (args
== NULL
|| args
->size() < 1)
6606 this->report_error(_("not enough arguments"));
6609 Expression
* arg
= args
->front();
6610 if (!arg
->is_type_expression())
6612 error_at(arg
->location(), "expected type");
6613 this->set_is_error();
6617 Expression_list
* newargs
;
6618 if (args
->size() == 1)
6622 newargs
= new Expression_list();
6623 Expression_list::const_iterator p
= args
->begin();
6625 for (; p
!= args
->end(); ++p
)
6626 newargs
->push_back(*p
);
6628 return Expression::make_make(arg
->type(), newargs
,
6633 else if (this->is_constant())
6635 // We can only lower len and cap if there are no function calls
6636 // in the arguments. Otherwise we have to make the call.
6637 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6639 Expression
* arg
= this->one_arg();
6640 if (!arg
->is_constant())
6642 Find_call_expression find_call
;
6643 Expression::traverse(&arg
, &find_call
);
6644 if (find_call
.found())
6652 if (this->integer_constant_value(true, ival
, &type
))
6654 Expression
* ret
= Expression::make_integer(&ival
, type
,
6663 if (this->float_constant_value(rval
, &type
))
6665 Expression
* ret
= Expression::make_float(&rval
, type
,
6673 if (this->complex_constant_value(rval
, imag
, &type
))
6675 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6684 else if (this->code_
== BUILTIN_RECOVER
)
6686 if (function
!= NULL
)
6687 function
->func_value()->set_calls_recover();
6690 // Calling recover outside of a function always returns the
6691 // nil empty interface.
6692 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6693 return Expression::make_cast(eface
,
6694 Expression::make_nil(this->location()),
6698 else if (this->code_
== BUILTIN_APPEND
)
6700 // Lower the varargs.
6701 const Expression_list
* args
= this->args();
6702 if (args
== NULL
|| args
->empty())
6704 Type
* slice_type
= args
->front()->type();
6705 if (!slice_type
->is_open_array_type())
6707 error_at(args
->front()->location(), "argument 1 must be a slice");
6708 this->set_is_error();
6711 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6717 // Return the type of the real or imag functions, given the type of
6718 // the argument. We need to map complex to float, complex64 to
6719 // float32, and complex128 to float64, so it has to be done by name.
6720 // This returns NULL if it can't figure out the type.
6723 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6725 if (arg_type
== NULL
|| arg_type
->is_abstract())
6727 Named_type
* nt
= arg_type
->named_type();
6730 while (nt
->real_type()->named_type() != NULL
)
6731 nt
= nt
->real_type()->named_type();
6732 if (nt
->name() == "complex")
6733 return Type::lookup_float_type("float");
6734 else if (nt
->name() == "complex64")
6735 return Type::lookup_float_type("float32");
6736 else if (nt
->name() == "complex128")
6737 return Type::lookup_float_type("float64");
6742 // Return the type of the cmplx function, given the type of one of the
6743 // argments. Like real_imag_type, we have to map by name.
6746 Builtin_call_expression::cmplx_type(Type
* arg_type
)
6748 if (arg_type
== NULL
|| arg_type
->is_abstract())
6750 Named_type
* nt
= arg_type
->named_type();
6753 while (nt
->real_type()->named_type() != NULL
)
6754 nt
= nt
->real_type()->named_type();
6755 if (nt
->name() == "float")
6756 return Type::lookup_complex_type("complex");
6757 else if (nt
->name() == "float32")
6758 return Type::lookup_complex_type("complex64");
6759 else if (nt
->name() == "float64")
6760 return Type::lookup_complex_type("complex128");
6765 // Return a single argument, or NULL if there isn't one.
6768 Builtin_call_expression::one_arg() const
6770 const Expression_list
* args
= this->args();
6771 if (args
->size() != 1)
6773 return args
->front();
6776 // Return whether this is constant: len of a string, or len or cap of
6777 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6780 Builtin_call_expression::do_is_constant() const
6782 switch (this->code_
)
6790 Expression
* arg
= this->one_arg();
6793 Type
* arg_type
= arg
->type();
6795 if (arg_type
->points_to() != NULL
6796 && arg_type
->points_to()->array_type() != NULL
6797 && !arg_type
->points_to()->is_open_array_type())
6798 arg_type
= arg_type
->points_to();
6800 if (arg_type
->array_type() != NULL
6801 && arg_type
->array_type()->length() != NULL
)
6804 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6807 bool ret
= arg
->is_constant();
6808 this->seen_
= false;
6814 case BUILTIN_SIZEOF
:
6815 case BUILTIN_ALIGNOF
:
6816 return this->one_arg() != NULL
;
6818 case BUILTIN_OFFSETOF
:
6820 Expression
* arg
= this->one_arg();
6823 return arg
->field_reference_expression() != NULL
;
6828 const Expression_list
* args
= this->args();
6829 if (args
!= NULL
&& args
->size() == 2)
6830 return args
->front()->is_constant() && args
->back()->is_constant();
6837 Expression
* arg
= this->one_arg();
6838 return arg
!= NULL
&& arg
->is_constant();
6848 // Return an integer constant value if possible.
6851 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
6855 if (this->code_
== BUILTIN_LEN
6856 || this->code_
== BUILTIN_CAP
)
6858 Expression
* arg
= this->one_arg();
6861 Type
* arg_type
= arg
->type();
6863 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6866 if (arg
->string_constant_value(&sval
))
6868 mpz_set_ui(val
, sval
.length());
6869 *ptype
= Type::lookup_integer_type("int");
6874 if (arg_type
->points_to() != NULL
6875 && arg_type
->points_to()->array_type() != NULL
6876 && !arg_type
->points_to()->is_open_array_type())
6877 arg_type
= arg_type
->points_to();
6879 if (arg_type
->array_type() != NULL
6880 && arg_type
->array_type()->length() != NULL
)
6884 Expression
* e
= arg_type
->array_type()->length();
6886 bool r
= e
->integer_constant_value(iota_is_constant
, val
, ptype
);
6887 this->seen_
= false;
6890 *ptype
= Type::lookup_integer_type("int");
6895 else if (this->code_
== BUILTIN_SIZEOF
6896 || this->code_
== BUILTIN_ALIGNOF
)
6898 Expression
* arg
= this->one_arg();
6901 Type
* arg_type
= arg
->type();
6902 if (arg_type
->is_error_type() || arg_type
->is_undefined())
6904 if (arg_type
->is_abstract())
6906 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
6907 unsigned long val_long
;
6908 if (this->code_
== BUILTIN_SIZEOF
)
6910 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
6911 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
6912 if (TREE_INT_CST_HIGH(type_size
) != 0)
6914 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
6915 val_long
= static_cast<unsigned long>(val_wide
);
6916 if (val_long
!= val_wide
)
6919 else if (this->code_
== BUILTIN_ALIGNOF
)
6921 if (arg
->field_reference_expression() == NULL
)
6922 val_long
= go_type_alignment(arg_type_tree
);
6925 // Calling unsafe.Alignof(s.f) returns the alignment of
6926 // the type of f when it is used as a field in a struct.
6927 val_long
= go_field_alignment(arg_type_tree
);
6932 mpz_set_ui(val
, val_long
);
6936 else if (this->code_
== BUILTIN_OFFSETOF
)
6938 Expression
* arg
= this->one_arg();
6941 Field_reference_expression
* farg
= arg
->field_reference_expression();
6944 Expression
* struct_expr
= farg
->expr();
6945 Type
* st
= struct_expr
->type();
6946 if (st
->struct_type() == NULL
)
6948 tree struct_tree
= st
->get_tree(this->gogo_
);
6949 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
6950 tree field
= TYPE_FIELDS(struct_tree
);
6951 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
6953 field
= DECL_CHAIN(field
);
6954 gcc_assert(field
!= NULL_TREE
);
6956 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
6957 if (offset_wide
< 0)
6959 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
6960 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
6962 mpz_set_ui(val
, offset_long
);
6968 // Return a floating point constant value if possible.
6971 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
6974 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
6976 Expression
* arg
= this->one_arg();
6987 if (arg
->complex_constant_value(real
, imag
, &type
))
6989 if (this->code_
== BUILTIN_REAL
)
6990 mpfr_set(val
, real
, GMP_RNDN
);
6992 mpfr_set(val
, imag
, GMP_RNDN
);
6993 *ptype
= Builtin_call_expression::real_imag_type(type
);
7005 // Return a complex constant value if possible.
7008 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
7011 if (this->code_
== BUILTIN_CMPLX
)
7013 const Expression_list
* args
= this->args();
7014 if (args
== NULL
|| args
->size() != 2)
7020 if (!args
->front()->float_constant_value(r
, &rtype
))
7031 if (args
->back()->float_constant_value(i
, &itype
)
7032 && Type::are_identical(rtype
, itype
, false, NULL
))
7034 mpfr_set(real
, r
, GMP_RNDN
);
7035 mpfr_set(imag
, i
, GMP_RNDN
);
7036 *ptype
= Builtin_call_expression::cmplx_type(rtype
);
7052 Builtin_call_expression::do_type()
7054 switch (this->code_
)
7056 case BUILTIN_INVALID
:
7063 const Expression_list
* args
= this->args();
7064 if (args
== NULL
|| args
->empty())
7065 return Type::make_error_type();
7066 return Type::make_pointer_type(args
->front()->type());
7072 case BUILTIN_ALIGNOF
:
7073 case BUILTIN_OFFSETOF
:
7074 case BUILTIN_SIZEOF
:
7075 return Type::lookup_integer_type("int");
7080 case BUILTIN_PRINTLN
:
7081 return Type::make_void_type();
7083 case BUILTIN_CLOSED
:
7084 return Type::lookup_bool_type();
7086 case BUILTIN_RECOVER
:
7087 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7089 case BUILTIN_APPEND
:
7091 const Expression_list
* args
= this->args();
7092 if (args
== NULL
|| args
->empty())
7093 return Type::make_error_type();
7094 return args
->front()->type();
7100 Expression
* arg
= this->one_arg();
7102 return Type::make_error_type();
7103 Type
* t
= arg
->type();
7104 if (t
->is_abstract())
7105 t
= t
->make_non_abstract_type();
7106 t
= Builtin_call_expression::real_imag_type(t
);
7108 t
= Type::make_error_type();
7114 const Expression_list
* args
= this->args();
7115 if (args
== NULL
|| args
->size() != 2)
7116 return Type::make_error_type();
7117 Type
* t
= args
->front()->type();
7118 if (t
->is_abstract())
7120 t
= args
->back()->type();
7121 if (t
->is_abstract())
7122 t
= t
->make_non_abstract_type();
7124 t
= Builtin_call_expression::cmplx_type(t
);
7126 t
= Type::make_error_type();
7132 // Determine the type.
7135 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7137 this->fn()->determine_type_no_context();
7139 const Expression_list
* args
= this->args();
7142 Type
* arg_type
= NULL
;
7143 switch (this->code_
)
7146 case BUILTIN_PRINTLN
:
7147 // Do not force a large integer constant to "int".
7153 arg_type
= Builtin_call_expression::cmplx_type(context
->type
);
7159 // For the cmplx function the type of one operand can
7160 // determine the type of the other, as in a binary expression.
7161 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7162 if (args
!= NULL
&& args
->size() == 2)
7164 Type
* t1
= args
->front()->type();
7165 Type
* t2
= args
->front()->type();
7166 if (!t1
->is_abstract())
7168 else if (!t2
->is_abstract())
7182 for (Expression_list::const_iterator pa
= args
->begin();
7186 Type_context subcontext
;
7187 subcontext
.type
= arg_type
;
7191 // We want to print large constants, we so can't just
7192 // use the appropriate nonabstract type. Use uint64 for
7193 // an integer if we know it is nonnegative, otherwise
7194 // use int64 for a integer, otherwise use float64 for a
7195 // float or complex128 for a complex.
7196 Type
* want_type
= NULL
;
7197 Type
* atype
= (*pa
)->type();
7198 if (atype
->is_abstract())
7200 if (atype
->integer_type() != NULL
)
7205 if (this->integer_constant_value(true, val
, &dummy
)
7206 && mpz_sgn(val
) >= 0)
7207 want_type
= Type::lookup_integer_type("uint64");
7209 want_type
= Type::lookup_integer_type("int64");
7212 else if (atype
->float_type() != NULL
)
7213 want_type
= Type::lookup_float_type("float64");
7214 else if (atype
->complex_type() != NULL
)
7215 want_type
= Type::lookup_complex_type("complex128");
7216 else if (atype
->is_abstract_string_type())
7217 want_type
= Type::lookup_string_type();
7218 else if (atype
->is_abstract_boolean_type())
7219 want_type
= Type::lookup_bool_type();
7222 subcontext
.type
= want_type
;
7226 (*pa
)->determine_type(&subcontext
);
7231 // If there is exactly one argument, return true. Otherwise give an
7232 // error message and return false.
7235 Builtin_call_expression::check_one_arg()
7237 const Expression_list
* args
= this->args();
7238 if (args
== NULL
|| args
->size() < 1)
7240 this->report_error(_("not enough arguments"));
7243 else if (args
->size() > 1)
7245 this->report_error(_("too many arguments"));
7248 if (args
->front()->is_error_expression()
7249 || args
->front()->type()->is_error_type()
7250 || args
->front()->type()->is_undefined())
7252 this->set_is_error();
7258 // Check argument types for a builtin function.
7261 Builtin_call_expression::do_check_types(Gogo
*)
7263 switch (this->code_
)
7265 case BUILTIN_INVALID
:
7273 // The single argument may be either a string or an array or a
7274 // map or a channel, or a pointer to a closed array.
7275 if (this->check_one_arg())
7277 Type
* arg_type
= this->one_arg()->type();
7278 if (arg_type
->points_to() != NULL
7279 && arg_type
->points_to()->array_type() != NULL
7280 && !arg_type
->points_to()->is_open_array_type())
7281 arg_type
= arg_type
->points_to();
7282 if (this->code_
== BUILTIN_CAP
)
7284 if (!arg_type
->is_error_type()
7285 && arg_type
->array_type() == NULL
7286 && arg_type
->channel_type() == NULL
)
7287 this->report_error(_("argument must be array or slice "
7292 if (!arg_type
->is_error_type()
7293 && !arg_type
->is_string_type()
7294 && arg_type
->array_type() == NULL
7295 && arg_type
->map_type() == NULL
7296 && arg_type
->channel_type() == NULL
)
7297 this->report_error(_("argument must be string or "
7298 "array or slice or map or channel"));
7305 case BUILTIN_PRINTLN
:
7307 const Expression_list
* args
= this->args();
7310 if (this->code_
== BUILTIN_PRINT
)
7311 warning_at(this->location(), 0,
7312 "no arguments for builtin function %<%s%>",
7313 (this->code_
== BUILTIN_PRINT
7319 for (Expression_list::const_iterator p
= args
->begin();
7323 Type
* type
= (*p
)->type();
7324 if (type
->is_error_type()
7325 || type
->is_string_type()
7326 || type
->integer_type() != NULL
7327 || type
->float_type() != NULL
7328 || type
->complex_type() != NULL
7329 || type
->is_boolean_type()
7330 || type
->points_to() != NULL
7331 || type
->interface_type() != NULL
7332 || type
->channel_type() != NULL
7333 || type
->map_type() != NULL
7334 || type
->function_type() != NULL
7335 || type
->is_open_array_type())
7338 this->report_error(_("unsupported argument type to "
7339 "builtin function"));
7346 case BUILTIN_CLOSED
:
7347 if (this->check_one_arg())
7349 if (this->one_arg()->type()->channel_type() == NULL
)
7350 this->report_error(_("argument must be channel"));
7355 case BUILTIN_SIZEOF
:
7356 case BUILTIN_ALIGNOF
:
7357 this->check_one_arg();
7360 case BUILTIN_RECOVER
:
7361 if (this->args() != NULL
&& !this->args()->empty())
7362 this->report_error(_("too many arguments"));
7365 case BUILTIN_OFFSETOF
:
7366 if (this->check_one_arg())
7368 Expression
* arg
= this->one_arg();
7369 if (arg
->field_reference_expression() == NULL
)
7370 this->report_error(_("argument must be a field reference"));
7376 const Expression_list
* args
= this->args();
7377 if (args
== NULL
|| args
->size() < 2)
7379 this->report_error(_("not enough arguments"));
7382 else if (args
->size() > 2)
7384 this->report_error(_("too many arguments"));
7387 Type
* arg1_type
= args
->front()->type();
7388 Type
* arg2_type
= args
->back()->type();
7389 if (arg1_type
->is_error_type() || arg2_type
->is_error_type())
7393 if (arg1_type
->is_open_array_type())
7394 e1
= arg1_type
->array_type()->element_type();
7397 this->report_error(_("left argument must be a slice"));
7402 if (arg2_type
->is_open_array_type())
7403 e2
= arg2_type
->array_type()->element_type();
7404 else if (arg2_type
->is_string_type())
7405 e2
= Type::lookup_integer_type("uint8");
7408 this->report_error(_("right argument must be a slice or a string"));
7412 if (!Type::are_identical(e1
, e2
, true, NULL
))
7413 this->report_error(_("element types must be the same"));
7417 case BUILTIN_APPEND
:
7419 const Expression_list
* args
= this->args();
7420 if (args
== NULL
|| args
->size() < 2)
7422 this->report_error(_("not enough arguments"));
7425 if (args
->size() > 2)
7427 this->report_error(_("too many arguments"));
7431 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7435 this->report_error(_("arguments 1 and 2 have different types"));
7438 error_at(this->location(),
7439 "arguments 1 and 2 have different types (%s)",
7441 this->set_is_error();
7449 if (this->check_one_arg())
7451 if (this->one_arg()->type()->complex_type() == NULL
)
7452 this->report_error(_("argument must have complex type"));
7458 const Expression_list
* args
= this->args();
7459 if (args
== NULL
|| args
->size() < 2)
7460 this->report_error(_("not enough arguments"));
7461 else if (args
->size() > 2)
7462 this->report_error(_("too many arguments"));
7463 else if (args
->front()->is_error_expression()
7464 || args
->front()->type()->is_error_type()
7465 || args
->back()->is_error_expression()
7466 || args
->back()->type()->is_error_type())
7467 this->set_is_error();
7468 else if (!Type::are_identical(args
->front()->type(),
7469 args
->back()->type(), true, NULL
))
7470 this->report_error(_("cmplx arguments must have identical types"));
7471 else if (args
->front()->type()->float_type() == NULL
)
7472 this->report_error(_("cmplx arguments must have "
7473 "floating-point type"));
7482 // Return the tree for a builtin function.
7485 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7487 Gogo
* gogo
= context
->gogo();
7488 source_location location
= this->location();
7489 switch (this->code_
)
7491 case BUILTIN_INVALID
:
7499 const Expression_list
* args
= this->args();
7500 gcc_assert(args
!= NULL
&& args
->size() == 1);
7501 Expression
* arg
= *args
->begin();
7502 Type
* arg_type
= arg
->type();
7506 gcc_assert(saw_errors());
7507 return error_mark_node
;
7511 tree arg_tree
= arg
->get_tree(context
);
7513 this->seen_
= false;
7515 if (arg_tree
== error_mark_node
)
7516 return error_mark_node
;
7518 if (arg_type
->points_to() != NULL
)
7520 arg_type
= arg_type
->points_to();
7521 gcc_assert(arg_type
->array_type() != NULL
7522 && !arg_type
->is_open_array_type());
7523 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7524 arg_tree
= build_fold_indirect_ref(arg_tree
);
7528 if (this->code_
== BUILTIN_LEN
)
7530 if (arg_type
->is_string_type())
7531 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7532 else if (arg_type
->array_type() != NULL
)
7536 gcc_assert(saw_errors());
7537 return error_mark_node
;
7540 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7541 this->seen_
= false;
7543 else if (arg_type
->map_type() != NULL
)
7545 static tree map_len_fndecl
;
7546 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7551 arg_type
->get_tree(gogo
),
7554 else if (arg_type
->channel_type() != NULL
)
7556 static tree chan_len_fndecl
;
7557 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7562 arg_type
->get_tree(gogo
),
7570 if (arg_type
->array_type() != NULL
)
7574 gcc_assert(saw_errors());
7575 return error_mark_node
;
7578 val_tree
= arg_type
->array_type()->capacity_tree(gogo
,
7580 this->seen_
= false;
7582 else if (arg_type
->channel_type() != NULL
)
7584 static tree chan_cap_fndecl
;
7585 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7590 arg_type
->get_tree(gogo
),
7597 if (val_tree
== error_mark_node
)
7598 return error_mark_node
;
7600 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7601 if (type_tree
== TREE_TYPE(val_tree
))
7604 return fold(convert_to_integer(type_tree
, val_tree
));
7608 case BUILTIN_PRINTLN
:
7610 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7611 tree stmt_list
= NULL_TREE
;
7613 const Expression_list
* call_args
= this->args();
7614 if (call_args
!= NULL
)
7616 for (Expression_list::const_iterator p
= call_args
->begin();
7617 p
!= call_args
->end();
7620 if (is_ln
&& p
!= call_args
->begin())
7622 static tree print_space_fndecl
;
7623 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7628 if (call
== error_mark_node
)
7629 return error_mark_node
;
7630 append_to_statement_list(call
, &stmt_list
);
7633 Type
* type
= (*p
)->type();
7635 tree arg
= (*p
)->get_tree(context
);
7636 if (arg
== error_mark_node
)
7637 return error_mark_node
;
7641 if (type
->is_string_type())
7643 static tree print_string_fndecl
;
7644 pfndecl
= &print_string_fndecl
;
7645 fnname
= "__go_print_string";
7647 else if (type
->integer_type() != NULL
7648 && type
->integer_type()->is_unsigned())
7650 static tree print_uint64_fndecl
;
7651 pfndecl
= &print_uint64_fndecl
;
7652 fnname
= "__go_print_uint64";
7653 Type
* itype
= Type::lookup_integer_type("uint64");
7654 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7657 else if (type
->integer_type() != NULL
)
7659 static tree print_int64_fndecl
;
7660 pfndecl
= &print_int64_fndecl
;
7661 fnname
= "__go_print_int64";
7662 Type
* itype
= Type::lookup_integer_type("int64");
7663 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7666 else if (type
->float_type() != NULL
)
7668 static tree print_double_fndecl
;
7669 pfndecl
= &print_double_fndecl
;
7670 fnname
= "__go_print_double";
7671 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7673 else if (type
->complex_type() != NULL
)
7675 static tree print_complex_fndecl
;
7676 pfndecl
= &print_complex_fndecl
;
7677 fnname
= "__go_print_complex";
7678 arg
= fold_convert_loc(location
, complex_double_type_node
,
7681 else if (type
->is_boolean_type())
7683 static tree print_bool_fndecl
;
7684 pfndecl
= &print_bool_fndecl
;
7685 fnname
= "__go_print_bool";
7687 else if (type
->points_to() != NULL
7688 || type
->channel_type() != NULL
7689 || type
->map_type() != NULL
7690 || type
->function_type() != NULL
)
7692 static tree print_pointer_fndecl
;
7693 pfndecl
= &print_pointer_fndecl
;
7694 fnname
= "__go_print_pointer";
7695 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7697 else if (type
->interface_type() != NULL
)
7699 if (type
->interface_type()->is_empty())
7701 static tree print_empty_interface_fndecl
;
7702 pfndecl
= &print_empty_interface_fndecl
;
7703 fnname
= "__go_print_empty_interface";
7707 static tree print_interface_fndecl
;
7708 pfndecl
= &print_interface_fndecl
;
7709 fnname
= "__go_print_interface";
7712 else if (type
->is_open_array_type())
7714 static tree print_slice_fndecl
;
7715 pfndecl
= &print_slice_fndecl
;
7716 fnname
= "__go_print_slice";
7721 tree call
= Gogo::call_builtin(pfndecl
,
7728 if (call
== error_mark_node
)
7729 return error_mark_node
;
7730 append_to_statement_list(call
, &stmt_list
);
7736 static tree print_nl_fndecl
;
7737 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7742 if (call
== error_mark_node
)
7743 return error_mark_node
;
7744 append_to_statement_list(call
, &stmt_list
);
7752 const Expression_list
* args
= this->args();
7753 gcc_assert(args
!= NULL
&& args
->size() == 1);
7754 Expression
* arg
= args
->front();
7755 tree arg_tree
= arg
->get_tree(context
);
7756 if (arg_tree
== error_mark_node
)
7757 return error_mark_node
;
7758 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7759 arg_tree
= Expression::convert_for_assignment(context
, empty
,
7761 arg_tree
, location
);
7762 static tree panic_fndecl
;
7763 tree call
= Gogo::call_builtin(&panic_fndecl
,
7768 TREE_TYPE(arg_tree
),
7770 if (call
== error_mark_node
)
7771 return error_mark_node
;
7772 // This function will throw an exception.
7773 TREE_NOTHROW(panic_fndecl
) = 0;
7774 // This function will not return.
7775 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
7779 case BUILTIN_RECOVER
:
7781 // The argument is set when building recover thunks. It's a
7782 // boolean value which is true if we can recover a value now.
7783 const Expression_list
* args
= this->args();
7784 gcc_assert(args
!= NULL
&& args
->size() == 1);
7785 Expression
* arg
= args
->front();
7786 tree arg_tree
= arg
->get_tree(context
);
7787 if (arg_tree
== error_mark_node
)
7788 return error_mark_node
;
7790 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7791 tree empty_tree
= empty
->get_tree(context
->gogo());
7793 Type
* nil_type
= Type::make_nil_type();
7794 Expression
* nil
= Expression::make_nil(location
);
7795 tree nil_tree
= nil
->get_tree(context
);
7796 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
7802 // We need to handle a deferred call to recover specially,
7803 // because it changes whether it can recover a panic or not.
7804 // See test7 in test/recover1.go.
7806 if (this->is_deferred())
7808 static tree deferred_recover_fndecl
;
7809 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
7811 "__go_deferred_recover",
7817 static tree recover_fndecl
;
7818 call
= Gogo::call_builtin(&recover_fndecl
,
7824 if (call
== error_mark_node
)
7825 return error_mark_node
;
7826 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
7827 call
, empty_nil_tree
);
7831 case BUILTIN_CLOSED
:
7833 const Expression_list
* args
= this->args();
7834 gcc_assert(args
!= NULL
&& args
->size() == 1);
7835 Expression
* arg
= args
->front();
7836 tree arg_tree
= arg
->get_tree(context
);
7837 if (arg_tree
== error_mark_node
)
7838 return error_mark_node
;
7839 if (this->code_
== BUILTIN_CLOSE
)
7841 static tree close_fndecl
;
7842 return Gogo::call_builtin(&close_fndecl
,
7844 "__go_builtin_close",
7847 TREE_TYPE(arg_tree
),
7852 static tree closed_fndecl
;
7853 return Gogo::call_builtin(&closed_fndecl
,
7855 "__go_builtin_closed",
7858 TREE_TYPE(arg_tree
),
7863 case BUILTIN_SIZEOF
:
7864 case BUILTIN_OFFSETOF
:
7865 case BUILTIN_ALIGNOF
:
7870 bool b
= this->integer_constant_value(true, val
, &dummy
);
7872 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
7873 tree ret
= Expression::integer_constant_tree(val
, type
);
7880 const Expression_list
* args
= this->args();
7881 gcc_assert(args
!= NULL
&& args
->size() == 2);
7882 Expression
* arg1
= args
->front();
7883 Expression
* arg2
= args
->back();
7885 tree arg1_tree
= arg1
->get_tree(context
);
7886 tree arg2_tree
= arg2
->get_tree(context
);
7887 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7888 return error_mark_node
;
7890 Type
* arg1_type
= arg1
->type();
7891 Array_type
* at
= arg1_type
->array_type();
7892 arg1_tree
= save_expr(arg1_tree
);
7893 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
7894 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
7895 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
7896 return error_mark_node
;
7898 Type
* arg2_type
= arg2
->type();
7901 if (arg2_type
->is_open_array_type())
7903 at
= arg2_type
->array_type();
7904 arg2_tree
= save_expr(arg2_tree
);
7905 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7906 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7910 arg2_tree
= save_expr(arg2_tree
);
7911 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
7912 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
7914 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7915 return error_mark_node
;
7917 arg1_len
= save_expr(arg1_len
);
7918 arg2_len
= save_expr(arg2_len
);
7919 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
7920 fold_build2_loc(location
, LT_EXPR
,
7922 arg1_len
, arg2_len
),
7923 arg1_len
, arg2_len
);
7924 len
= save_expr(len
);
7926 Type
* element_type
= at
->element_type();
7927 tree element_type_tree
= element_type
->get_tree(gogo
);
7928 if (element_type_tree
== error_mark_node
)
7929 return error_mark_node
;
7930 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7931 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
7933 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
7934 TREE_TYPE(element_size
),
7935 bytecount
, element_size
);
7936 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
7938 arg1_val
= fold_convert_loc(location
, ptr_type_node
, arg1_val
);
7939 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7941 static tree copy_fndecl
;
7942 tree call
= Gogo::call_builtin(©_fndecl
,
7953 if (call
== error_mark_node
)
7954 return error_mark_node
;
7956 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
7960 case BUILTIN_APPEND
:
7962 const Expression_list
* args
= this->args();
7963 gcc_assert(args
!= NULL
&& args
->size() == 2);
7964 Expression
* arg1
= args
->front();
7965 Expression
* arg2
= args
->back();
7967 Array_type
* at
= arg1
->type()->array_type();
7968 Type
* element_type
= at
->element_type();
7970 tree arg1_tree
= arg1
->get_tree(context
);
7971 tree arg2_tree
= arg2
->get_tree(context
);
7972 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7973 return error_mark_node
;
7975 arg2_tree
= Expression::convert_for_assignment(context
, at
,
7979 if (arg2_tree
== error_mark_node
)
7980 return error_mark_node
;
7982 arg2_tree
= save_expr(arg2_tree
);
7983 tree arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7984 tree arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7985 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7986 return error_mark_node
;
7987 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7988 arg2_len
= fold_convert_loc(location
, size_type_node
, arg2_len
);
7990 tree element_type_tree
= element_type
->get_tree(gogo
);
7991 if (element_type_tree
== error_mark_node
)
7992 return error_mark_node
;
7993 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7994 element_size
= fold_convert_loc(location
, size_type_node
,
7997 // We rebuild the decl each time since the slice types may
7999 tree append_fndecl
= NULL_TREE
;
8000 return Gogo::call_builtin(&append_fndecl
,
8004 TREE_TYPE(arg1_tree
),
8005 TREE_TYPE(arg1_tree
),
8018 const Expression_list
* args
= this->args();
8019 gcc_assert(args
!= NULL
&& args
->size() == 1);
8020 Expression
* arg
= args
->front();
8021 tree arg_tree
= arg
->get_tree(context
);
8022 if (arg_tree
== error_mark_node
)
8023 return error_mark_node
;
8024 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
8025 if (this->code_
== BUILTIN_REAL
)
8026 return fold_build1_loc(location
, REALPART_EXPR
,
8027 TREE_TYPE(TREE_TYPE(arg_tree
)),
8030 return fold_build1_loc(location
, IMAGPART_EXPR
,
8031 TREE_TYPE(TREE_TYPE(arg_tree
)),
8037 const Expression_list
* args
= this->args();
8038 gcc_assert(args
!= NULL
&& args
->size() == 2);
8039 tree r
= args
->front()->get_tree(context
);
8040 tree i
= args
->back()->get_tree(context
);
8041 if (r
== error_mark_node
|| i
== error_mark_node
)
8042 return error_mark_node
;
8043 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
8044 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
8045 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
8046 return fold_build2_loc(location
, COMPLEX_EXPR
,
8047 build_complex_type(TREE_TYPE(r
)),
8056 // We have to support exporting a builtin call expression, because
8057 // code can set a constant to the result of a builtin expression.
8060 Builtin_call_expression::do_export(Export
* exp
) const
8067 if (this->integer_constant_value(true, val
, &dummy
))
8069 Integer_expression::export_integer(exp
, val
);
8078 if (this->float_constant_value(fval
, &dummy
))
8080 Float_expression::export_float(exp
, fval
);
8092 if (this->complex_constant_value(real
, imag
, &dummy
))
8094 Complex_expression::export_complex(exp
, real
, imag
);
8103 error_at(this->location(), "value is not constant");
8107 // A trailing space lets us reliably identify the end of the number.
8108 exp
->write_c_string(" ");
8111 // Class Call_expression.
8116 Call_expression::do_traverse(Traverse
* traverse
)
8118 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8119 return TRAVERSE_EXIT
;
8120 if (this->args_
!= NULL
)
8122 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8123 return TRAVERSE_EXIT
;
8125 return TRAVERSE_CONTINUE
;
8128 // Lower a call statement.
8131 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
8133 // A type case can look like a function call.
8134 if (this->fn_
->is_type_expression()
8135 && this->args_
!= NULL
8136 && this->args_
->size() == 1)
8137 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8140 // Recognize a call to a builtin function.
8141 Func_expression
* fne
= this->fn_
->func_expression();
8143 && fne
->named_object()->is_function_declaration()
8144 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
8145 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8146 this->is_varargs_
, this->location());
8148 // Handle an argument which is a call to a function which returns
8149 // multiple results.
8150 if (this->args_
!= NULL
8151 && this->args_
->size() == 1
8152 && this->args_
->front()->call_expression() != NULL
8153 && this->fn_
->type()->function_type() != NULL
)
8155 Function_type
* fntype
= this->fn_
->type()->function_type();
8156 size_t rc
= this->args_
->front()->call_expression()->result_count();
8158 && fntype
->parameters() != NULL
8159 && (fntype
->parameters()->size() == rc
8160 || (fntype
->is_varargs()
8161 && fntype
->parameters()->size() - 1 <= rc
)))
8163 Call_expression
* call
= this->args_
->front()->call_expression();
8164 Expression_list
* args
= new Expression_list
;
8165 for (size_t i
= 0; i
< rc
; ++i
)
8166 args
->push_back(Expression::make_call_result(call
, i
));
8167 // We can't return a new call expression here, because this
8168 // one may be referenced by Call_result expressions. FIXME.
8174 // Handle a call to a varargs function by packaging up the extra
8176 if (this->fn_
->type()->function_type() != NULL
8177 && this->fn_
->type()->function_type()->is_varargs())
8179 Function_type
* fntype
= this->fn_
->type()->function_type();
8180 const Typed_identifier_list
* parameters
= fntype
->parameters();
8181 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
8182 Type
* varargs_type
= parameters
->back().type();
8183 return this->lower_varargs(gogo
, function
, varargs_type
,
8184 parameters
->size());
8190 // Lower a call to a varargs function. FUNCTION is the function in
8191 // which the call occurs--it's not the function we are calling.
8192 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8193 // PARAM_COUNT is the number of parameters of the function we are
8194 // calling; the last of these parameters will be the varargs
8198 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8199 Type
* varargs_type
, size_t param_count
)
8201 if (this->varargs_are_lowered_
)
8204 source_location loc
= this->location();
8206 gcc_assert(param_count
> 0);
8207 gcc_assert(varargs_type
->is_open_array_type());
8209 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8210 if (arg_count
< param_count
- 1)
8212 // Not enough arguments; will be caught in check_types.
8216 Expression_list
* old_args
= this->args_
;
8217 Expression_list
* new_args
= new Expression_list();
8218 bool push_empty_arg
= false;
8219 if (old_args
== NULL
|| old_args
->empty())
8221 gcc_assert(param_count
== 1);
8222 push_empty_arg
= true;
8226 Expression_list::const_iterator pa
;
8228 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8230 if (static_cast<size_t>(i
) == param_count
)
8232 new_args
->push_back(*pa
);
8235 // We have reached the varargs parameter.
8237 bool issued_error
= false;
8238 if (pa
== old_args
->end())
8239 push_empty_arg
= true;
8240 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8241 new_args
->push_back(*pa
);
8242 else if (this->is_varargs_
)
8244 this->report_error(_("too many arguments"));
8247 else if (pa
+ 1 == old_args
->end()
8248 && this->is_compatible_varargs_argument(function
, *pa
,
8251 new_args
->push_back(*pa
);
8254 Type
* element_type
= varargs_type
->array_type()->element_type();
8255 Expression_list
* vals
= new Expression_list
;
8256 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8258 // Check types here so that we get a better message.
8259 Type
* patype
= (*pa
)->type();
8260 source_location paloc
= (*pa
)->location();
8261 if (!this->check_argument_type(i
, element_type
, patype
,
8262 paloc
, issued_error
))
8264 vals
->push_back(*pa
);
8267 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8268 new_args
->push_back(val
);
8273 new_args
->push_back(Expression::make_nil(loc
));
8275 // We can't return a new call expression here, because this one may
8276 // be referenced by Call_result expressions. FIXME.
8277 if (old_args
!= NULL
)
8279 this->args_
= new_args
;
8280 this->varargs_are_lowered_
= true;
8282 // Lower all the new subexpressions.
8283 Expression
* ret
= this;
8284 gogo
->lower_expression(function
, &ret
);
8285 gcc_assert(ret
== this);
8289 // Return true if ARG is a varargs argment which should be passed to
8290 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8291 // will be the last argument passed in the call, and PARAM_TYPE will
8292 // be the type of the last parameter of the varargs function being
8296 Call_expression::is_compatible_varargs_argument(Named_object
* function
,
8301 *issued_error
= false;
8303 Type
* var_type
= NULL
;
8305 // The simple case is passing the varargs parameter of the caller.
8306 Var_expression
* ve
= arg
->var_expression();
8307 if (ve
!= NULL
&& ve
->named_object()->is_variable())
8309 Variable
* var
= ve
->named_object()->var_value();
8310 if (var
->is_varargs_parameter())
8311 var_type
= var
->type();
8314 // The complex case is passing the varargs parameter of some
8315 // enclosing function. This will look like passing down *c.f where
8316 // c is the closure variable and f is a field in the closure.
8317 if (function
!= NULL
8318 && function
->func_value()->needs_closure()
8319 && arg
->classification() == EXPRESSION_UNARY
)
8321 Unary_expression
* ue
= static_cast<Unary_expression
*>(arg
);
8322 if (ue
->op() == OPERATOR_MULT
)
8324 Field_reference_expression
* fre
=
8325 ue
->operand()->deref()->field_reference_expression();
8328 Var_expression
* ve
= fre
->expr()->deref()->var_expression();
8331 Named_object
* no
= ve
->named_object();
8332 Function
* f
= function
->func_value();
8333 if (no
== f
->closure_var())
8335 // At this point we know that this indeed a
8336 // reference to some enclosing variable. Now we
8337 // need to figure out whether that variable is a
8338 // varargs parameter.
8339 Named_object
* enclosing
=
8340 f
->enclosing_var(fre
->field_index());
8341 Variable
* var
= enclosing
->var_value();
8342 if (var
->is_varargs_parameter())
8343 var_type
= var
->type();
8350 if (var_type
== NULL
)
8353 // We only match if the parameter is the same, with an identical
8355 Array_type
* var_at
= var_type
->array_type();
8356 gcc_assert(var_at
!= NULL
);
8357 Array_type
* param_at
= param_type
->array_type();
8358 if (param_at
!= NULL
8359 && Type::are_identical(var_at
->element_type(),
8360 param_at
->element_type(), true, NULL
))
8362 error_at(arg
->location(), "... mismatch: passing ...T as ...");
8363 *issued_error
= true;
8367 // Get the function type. Returns NULL if we don't know the type. If
8368 // this returns NULL, and if_ERROR is true, issues an error.
8371 Call_expression::get_function_type() const
8373 return this->fn_
->type()->function_type();
8376 // Return the number of values which this call will return.
8379 Call_expression::result_count() const
8381 const Function_type
* fntype
= this->get_function_type();
8384 if (fntype
->results() == NULL
)
8386 return fntype
->results()->size();
8389 // Return whether this is a call to the predeclared function recover.
8392 Call_expression::is_recover_call() const
8394 return this->do_is_recover_call();
8397 // Set the argument to the recover function.
8400 Call_expression::set_recover_arg(Expression
* arg
)
8402 this->do_set_recover_arg(arg
);
8405 // Virtual functions also implemented by Builtin_call_expression.
8408 Call_expression::do_is_recover_call() const
8414 Call_expression::do_set_recover_arg(Expression
*)
8422 Call_expression::do_type()
8424 if (this->type_
!= NULL
)
8428 Function_type
* fntype
= this->get_function_type();
8430 return Type::make_error_type();
8432 const Typed_identifier_list
* results
= fntype
->results();
8433 if (results
== NULL
)
8434 ret
= Type::make_void_type();
8435 else if (results
->size() == 1)
8436 ret
= results
->begin()->type();
8438 ret
= Type::make_call_multiple_result_type(this);
8445 // Determine types for a call expression. We can use the function
8446 // parameter types to set the types of the arguments.
8449 Call_expression::do_determine_type(const Type_context
*)
8451 this->fn_
->determine_type_no_context();
8452 Function_type
* fntype
= this->get_function_type();
8453 const Typed_identifier_list
* parameters
= NULL
;
8455 parameters
= fntype
->parameters();
8456 if (this->args_
!= NULL
)
8458 Typed_identifier_list::const_iterator pt
;
8459 if (parameters
!= NULL
)
8460 pt
= parameters
->begin();
8461 for (Expression_list::const_iterator pa
= this->args_
->begin();
8462 pa
!= this->args_
->end();
8465 if (parameters
!= NULL
&& pt
!= parameters
->end())
8467 Type_context
subcontext(pt
->type(), false);
8468 (*pa
)->determine_type(&subcontext
);
8472 (*pa
)->determine_type_no_context();
8477 // Check types for parameter I.
8480 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8481 const Type
* argument_type
,
8482 source_location argument_location
,
8486 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8491 error_at(argument_location
, "argument %d has incompatible type", i
);
8493 error_at(argument_location
,
8494 "argument %d has incompatible type (%s)",
8497 this->set_is_error();
8506 Call_expression::do_check_types(Gogo
*)
8508 Function_type
* fntype
= this->get_function_type();
8511 if (!this->fn_
->type()->is_error_type())
8512 this->report_error(_("expected function"));
8516 if (fntype
->is_method())
8518 // We don't support pointers to methods, so the function has to
8519 // be a bound method expression.
8520 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8523 this->report_error(_("method call without object"));
8526 Type
* first_arg_type
= bme
->first_argument()->type();
8527 if (first_arg_type
->points_to() == NULL
)
8529 // When passing a value, we need to check that we are
8530 // permitted to copy it.
8532 if (!Type::are_assignable(fntype
->receiver()->type(),
8533 first_arg_type
, &reason
))
8536 this->report_error(_("incompatible type for receiver"));
8539 error_at(this->location(),
8540 "incompatible type for receiver (%s)",
8542 this->set_is_error();
8548 // Note that varargs was handled by the lower_varargs() method, so
8549 // we don't have to worry about it here.
8551 const Typed_identifier_list
* parameters
= fntype
->parameters();
8552 if (this->args_
== NULL
)
8554 if (parameters
!= NULL
&& !parameters
->empty())
8555 this->report_error(_("not enough arguments"));
8557 else if (parameters
== NULL
)
8558 this->report_error(_("too many arguments"));
8562 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8563 for (Expression_list::const_iterator pa
= this->args_
->begin();
8564 pa
!= this->args_
->end();
8567 if (pt
== parameters
->end())
8569 this->report_error(_("too many arguments"));
8572 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8573 (*pa
)->location(), false);
8575 if (pt
!= parameters
->end())
8576 this->report_error(_("not enough arguments"));
8580 // Return whether we have to use a temporary variable to ensure that
8581 // we evaluate this call expression in order. If the call returns no
8582 // results then it will inevitably be executed last. If the call
8583 // returns more than one result then it will be used with Call_result
8584 // expressions. So we only have to use a temporary variable if the
8585 // call returns exactly one result.
8588 Call_expression::do_must_eval_in_order() const
8590 return this->result_count() == 1;
8593 // Get the function and the first argument to use when calling a bound
8597 Call_expression::bound_method_function(Translate_context
* context
,
8598 Bound_method_expression
* bound_method
,
8599 tree
* first_arg_ptr
)
8601 Expression
* first_argument
= bound_method
->first_argument();
8602 tree first_arg
= first_argument
->get_tree(context
);
8603 if (first_arg
== error_mark_node
)
8604 return error_mark_node
;
8606 // We always pass a pointer to the first argument when calling a
8608 if (first_argument
->type()->points_to() == NULL
)
8610 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8611 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8612 || DECL_P(first_arg
)
8613 || TREE_CODE(first_arg
) == INDIRECT_REF
8614 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8616 first_arg
= build_fold_addr_expr(first_arg
);
8617 if (DECL_P(first_arg
))
8618 TREE_ADDRESSABLE(first_arg
) = 1;
8622 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8623 get_name(first_arg
));
8624 DECL_IGNORED_P(tmp
) = 0;
8625 DECL_INITIAL(tmp
) = first_arg
;
8626 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8627 build1(DECL_EXPR
, void_type_node
, tmp
),
8628 build_fold_addr_expr(tmp
));
8629 TREE_ADDRESSABLE(tmp
) = 1;
8631 if (first_arg
== error_mark_node
)
8632 return error_mark_node
;
8635 Type
* fatype
= bound_method
->first_argument_type();
8638 if (fatype
->points_to() == NULL
)
8639 fatype
= Type::make_pointer_type(fatype
);
8640 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8641 if (first_arg
== error_mark_node
8642 || TREE_TYPE(first_arg
) == error_mark_node
)
8643 return error_mark_node
;
8646 *first_arg_ptr
= first_arg
;
8648 return bound_method
->method()->get_tree(context
);
8651 // Get the function and the first argument to use when calling an
8652 // interface method.
8655 Call_expression::interface_method_function(
8656 Translate_context
* context
,
8657 Interface_field_reference_expression
* interface_method
,
8658 tree
* first_arg_ptr
)
8660 tree expr
= interface_method
->expr()->get_tree(context
);
8661 if (expr
== error_mark_node
)
8662 return error_mark_node
;
8663 expr
= save_expr(expr
);
8664 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8665 if (first_arg
== error_mark_node
)
8666 return error_mark_node
;
8667 *first_arg_ptr
= first_arg
;
8668 return interface_method
->get_function_tree(context
, expr
);
8671 // Build the call expression.
8674 Call_expression::do_get_tree(Translate_context
* context
)
8676 if (this->tree_
!= NULL_TREE
)
8679 Function_type
* fntype
= this->get_function_type();
8681 return error_mark_node
;
8683 if (this->fn_
->is_error_expression())
8684 return error_mark_node
;
8686 Gogo
* gogo
= context
->gogo();
8687 source_location location
= this->location();
8689 Func_expression
* func
= this->fn_
->func_expression();
8690 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8691 Interface_field_reference_expression
* interface_method
=
8692 this->fn_
->interface_field_reference_expression();
8693 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8694 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8695 gcc_assert(!fntype
->is_method() || is_method
);
8699 if (this->args_
== NULL
|| this->args_
->empty())
8701 nargs
= is_method
? 1 : 0;
8702 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8706 const Typed_identifier_list
* params
= fntype
->parameters();
8707 gcc_assert(params
!= NULL
);
8709 nargs
= this->args_
->size();
8710 int i
= is_method
? 1 : 0;
8712 args
= new tree
[nargs
];
8714 Typed_identifier_list::const_iterator pp
= params
->begin();
8715 Expression_list::const_iterator pe
;
8716 for (pe
= this->args_
->begin();
8717 pe
!= this->args_
->end();
8720 gcc_assert(pp
!= params
->end());
8721 tree arg_val
= (*pe
)->get_tree(context
);
8722 args
[i
] = Expression::convert_for_assignment(context
,
8727 if (args
[i
] == error_mark_node
)
8728 return error_mark_node
;
8730 gcc_assert(pp
== params
->end());
8731 gcc_assert(i
== nargs
);
8734 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8735 if (rettype
== error_mark_node
)
8736 return error_mark_node
;
8740 fn
= func
->get_tree_without_closure(gogo
);
8741 else if (!is_method
)
8742 fn
= this->fn_
->get_tree(context
);
8743 else if (bound_method
!= NULL
)
8744 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8745 else if (interface_method
!= NULL
)
8746 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8750 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8751 return error_mark_node
;
8753 // This is to support builtin math functions when using 80387 math.
8755 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8756 fndecl
= TREE_OPERAND(fndecl
, 0);
8757 tree excess_type
= NULL_TREE
;
8759 && DECL_IS_BUILTIN(fndecl
)
8760 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8762 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8763 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8764 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8765 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8767 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8768 if (excess_type
!= NULL_TREE
)
8770 tree excess_fndecl
= mathfn_built_in(excess_type
,
8771 DECL_FUNCTION_CODE(fndecl
));
8772 if (excess_fndecl
== NULL_TREE
)
8773 excess_type
= NULL_TREE
;
8776 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8777 for (int i
= 0; i
< nargs
; ++i
)
8778 args
[i
] = ::convert(excess_type
, args
[i
]);
8783 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8787 SET_EXPR_LOCATION(ret
, location
);
8791 tree closure_tree
= func
->closure()->get_tree(context
);
8792 if (closure_tree
!= error_mark_node
)
8793 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8796 // If this is a recursive function type which returns itself, as in
8798 // we have used ptr_type_node for the return type. Add a cast here
8799 // to the correct type.
8800 if (TREE_TYPE(ret
) == ptr_type_node
)
8802 tree t
= this->type()->get_tree(gogo
);
8803 ret
= fold_convert_loc(location
, t
, ret
);
8806 if (excess_type
!= NULL_TREE
)
8808 // Calling convert here can undo our excess precision change.
8809 // That may or may not be a bug in convert_to_real.
8810 ret
= build1(NOP_EXPR
, rettype
, ret
);
8813 // If there is more than one result, we will refer to the call
8815 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
8816 ret
= save_expr(ret
);
8823 // Make a call expression.
8826 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
8827 source_location location
)
8829 return new Call_expression(fn
, args
, is_varargs
, location
);
8832 // A single result from a call which returns multiple results.
8834 class Call_result_expression
: public Expression
8837 Call_result_expression(Call_expression
* call
, unsigned int index
)
8838 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
8839 call_(call
), index_(index
)
8844 do_traverse(Traverse
*);
8850 do_determine_type(const Type_context
*);
8853 do_check_types(Gogo
*);
8858 return new Call_result_expression(this->call_
->call_expression(),
8863 do_must_eval_in_order() const
8867 do_get_tree(Translate_context
*);
8870 // The underlying call expression.
8872 // Which result we want.
8873 unsigned int index_
;
8876 // Traverse a call result.
8879 Call_result_expression::do_traverse(Traverse
* traverse
)
8881 if (traverse
->remember_expression(this->call_
))
8883 // We have already traversed the call expression.
8884 return TRAVERSE_CONTINUE
;
8886 return Expression::traverse(&this->call_
, traverse
);
8892 Call_result_expression::do_type()
8894 if (this->classification() == EXPRESSION_ERROR
)
8895 return Type::make_error_type();
8897 // THIS->CALL_ can be replaced with a temporary reference due to
8898 // Call_expression::do_must_eval_in_order when there is an error.
8899 Call_expression
* ce
= this->call_
->call_expression();
8902 this->set_is_error();
8903 return Type::make_error_type();
8905 Function_type
* fntype
= ce
->get_function_type();
8908 this->set_is_error();
8909 return Type::make_error_type();
8911 const Typed_identifier_list
* results
= fntype
->results();
8912 if (results
== NULL
)
8914 this->report_error(_("number of results does not match "
8915 "number of values"));
8916 return Type::make_error_type();
8918 Typed_identifier_list::const_iterator pr
= results
->begin();
8919 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8921 if (pr
== results
->end())
8925 if (pr
== results
->end())
8927 this->report_error(_("number of results does not match "
8928 "number of values"));
8929 return Type::make_error_type();
8934 // Check the type. Just make sure that we trigger the warning in
8938 Call_result_expression::do_check_types(Gogo
*)
8943 // Determine the type. We have nothing to do here, but the 0 result
8944 // needs to pass down to the caller.
8947 Call_result_expression::do_determine_type(const Type_context
*)
8949 if (this->index_
== 0)
8950 this->call_
->determine_type_no_context();
8956 Call_result_expression::do_get_tree(Translate_context
* context
)
8958 tree call_tree
= this->call_
->get_tree(context
);
8959 if (call_tree
== error_mark_node
)
8960 return error_mark_node
;
8961 if (TREE_CODE(TREE_TYPE(call_tree
)) != RECORD_TYPE
)
8963 gcc_assert(saw_errors());
8964 return error_mark_node
;
8966 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
8967 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8969 gcc_assert(field
!= NULL_TREE
);
8970 field
= DECL_CHAIN(field
);
8972 gcc_assert(field
!= NULL_TREE
);
8973 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
8976 // Make a reference to a single result of a call which returns
8977 // multiple results.
8980 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
8982 return new Call_result_expression(call
, index
);
8985 // Class Index_expression.
8990 Index_expression::do_traverse(Traverse
* traverse
)
8992 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
8993 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
8994 || (this->end_
!= NULL
8995 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
8996 return TRAVERSE_EXIT
;
8997 return TRAVERSE_CONTINUE
;
9000 // Lower an index expression. This converts the generic index
9001 // expression into an array index, a string index, or a map index.
9004 Index_expression::do_lower(Gogo
*, Named_object
*, int)
9006 source_location location
= this->location();
9007 Expression
* left
= this->left_
;
9008 Expression
* start
= this->start_
;
9009 Expression
* end
= this->end_
;
9011 Type
* type
= left
->type();
9012 if (type
->is_error_type())
9013 return Expression::make_error(location
);
9014 else if (type
->array_type() != NULL
)
9015 return Expression::make_array_index(left
, start
, end
, location
);
9016 else if (type
->points_to() != NULL
9017 && type
->points_to()->array_type() != NULL
9018 && !type
->points_to()->is_open_array_type())
9020 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
9022 return Expression::make_array_index(deref
, start
, end
, location
);
9024 else if (type
->is_string_type())
9025 return Expression::make_string_index(left
, start
, end
, location
);
9026 else if (type
->map_type() != NULL
)
9030 error_at(location
, "invalid slice of map");
9031 return Expression::make_error(location
);
9033 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
9035 if (this->is_lvalue_
)
9036 ret
->set_is_lvalue();
9042 "attempt to index object which is not array, string, or map");
9043 return Expression::make_error(location
);
9047 // Make an index expression.
9050 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
9051 source_location location
)
9053 return new Index_expression(left
, start
, end
, location
);
9056 // An array index. This is used for both indexing and slicing.
9058 class Array_index_expression
: public Expression
9061 Array_index_expression(Expression
* array
, Expression
* start
,
9062 Expression
* end
, source_location location
)
9063 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
9064 array_(array
), start_(start
), end_(end
), type_(NULL
)
9069 do_traverse(Traverse
*);
9075 do_determine_type(const Type_context
*);
9078 do_check_types(Gogo
*);
9083 return Expression::make_array_index(this->array_
->copy(),
9084 this->start_
->copy(),
9087 : this->end_
->copy()),
9092 do_is_addressable() const;
9095 do_address_taken(bool escapes
)
9096 { this->array_
->address_taken(escapes
); }
9099 do_get_tree(Translate_context
*);
9102 // The array we are getting a value from.
9104 // The start or only index.
9106 // The end index of a slice. This may be NULL for a simple array
9107 // index, or it may be a nil expression for the length of the array.
9109 // The type of the expression.
9113 // Array index traversal.
9116 Array_index_expression::do_traverse(Traverse
* traverse
)
9118 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9119 return TRAVERSE_EXIT
;
9120 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9121 return TRAVERSE_EXIT
;
9122 if (this->end_
!= NULL
)
9124 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9125 return TRAVERSE_EXIT
;
9127 return TRAVERSE_CONTINUE
;
9130 // Return the type of an array index.
9133 Array_index_expression::do_type()
9135 if (this->type_
== NULL
)
9137 Array_type
* type
= this->array_
->type()->array_type();
9139 this->type_
= Type::make_error_type();
9140 else if (this->end_
== NULL
)
9141 this->type_
= type
->element_type();
9142 else if (type
->is_open_array_type())
9144 // A slice of a slice has the same type as the original
9146 this->type_
= this->array_
->type()->deref();
9150 // A slice of an array is a slice.
9151 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9157 // Set the type of an array index.
9160 Array_index_expression::do_determine_type(const Type_context
*)
9162 this->array_
->determine_type_no_context();
9163 Type_context
subcontext(NULL
, true);
9164 this->start_
->determine_type(&subcontext
);
9165 if (this->end_
!= NULL
)
9166 this->end_
->determine_type(&subcontext
);
9169 // Check types of an array index.
9172 Array_index_expression::do_check_types(Gogo
*)
9174 if (this->start_
->type()->integer_type() == NULL
)
9175 this->report_error(_("index must be integer"));
9176 if (this->end_
!= NULL
9177 && this->end_
->type()->integer_type() == NULL
9178 && !this->end_
->is_nil_expression())
9179 this->report_error(_("slice end must be integer"));
9181 Array_type
* array_type
= this->array_
->type()->array_type();
9182 if (array_type
== NULL
)
9184 gcc_assert(this->array_
->type()->is_error_type());
9188 unsigned int int_bits
=
9189 Type::lookup_integer_type("int")->integer_type()->bits();
9194 bool lval_valid
= (array_type
->length() != NULL
9195 && array_type
->length()->integer_constant_value(true,
9200 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9202 if (mpz_sgn(ival
) < 0
9203 || mpz_sizeinbase(ival
, 2) >= int_bits
9205 && (this->end_
== NULL
9206 ? mpz_cmp(ival
, lval
) >= 0
9207 : mpz_cmp(ival
, lval
) > 0)))
9209 error_at(this->start_
->location(), "array index out of bounds");
9210 this->set_is_error();
9213 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9215 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9217 if (mpz_sgn(ival
) < 0
9218 || mpz_sizeinbase(ival
, 2) >= int_bits
9219 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
9221 error_at(this->end_
->location(), "array index out of bounds");
9222 this->set_is_error();
9229 // A slice of an array requires an addressable array. A slice of a
9230 // slice is always possible.
9231 if (this->end_
!= NULL
9232 && !array_type
->is_open_array_type()
9233 && !this->array_
->is_addressable())
9234 this->report_error(_("array is not addressable"));
9237 // Return whether this expression is addressable.
9240 Array_index_expression::do_is_addressable() const
9242 // A slice expression is not addressable.
9243 if (this->end_
!= NULL
)
9246 // An index into a slice is addressable.
9247 if (this->array_
->type()->is_open_array_type())
9250 // An index into an array is addressable if the array is
9252 return this->array_
->is_addressable();
9255 // Get a tree for an array index.
9258 Array_index_expression::do_get_tree(Translate_context
* context
)
9260 Gogo
* gogo
= context
->gogo();
9261 source_location loc
= this->location();
9263 Array_type
* array_type
= this->array_
->type()->array_type();
9264 if (array_type
== NULL
)
9266 gcc_assert(this->array_
->type()->is_error_type());
9267 return error_mark_node
;
9270 tree type_tree
= array_type
->get_tree(gogo
);
9271 if (type_tree
== error_mark_node
)
9272 return error_mark_node
;
9274 tree array_tree
= this->array_
->get_tree(context
);
9275 if (array_tree
== error_mark_node
)
9276 return error_mark_node
;
9278 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9279 array_tree
= save_expr(array_tree
);
9280 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
9281 if (length_tree
== error_mark_node
)
9282 return error_mark_node
;
9283 length_tree
= save_expr(length_tree
);
9284 tree length_type
= TREE_TYPE(length_tree
);
9286 tree bad_index
= boolean_false_node
;
9288 tree start_tree
= this->start_
->get_tree(context
);
9289 if (start_tree
== error_mark_node
)
9290 return error_mark_node
;
9291 if (!DECL_P(start_tree
))
9292 start_tree
= save_expr(start_tree
);
9293 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9294 start_tree
= convert_to_integer(length_type
, start_tree
);
9296 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9299 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9300 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9301 fold_build2_loc(loc
,
9305 boolean_type_node
, start_tree
,
9308 int code
= (array_type
->length() != NULL
9309 ? (this->end_
== NULL
9310 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9311 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9312 : (this->end_
== NULL
9313 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9314 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9315 tree crash
= Gogo::runtime_error(code
, loc
);
9317 if (this->end_
== NULL
)
9319 // Simple array indexing. This has to return an l-value, so
9320 // wrap the index check into START_TREE.
9321 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9322 build3(COND_EXPR
, void_type_node
,
9323 bad_index
, crash
, NULL_TREE
),
9325 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9327 if (array_type
->length() != NULL
)
9330 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9331 start_tree
, NULL_TREE
, NULL_TREE
);
9336 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9337 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9338 if (element_type_tree
== error_mark_node
)
9339 return error_mark_node
;
9340 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9341 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9342 start_tree
, element_size
);
9343 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9344 TREE_TYPE(values
), values
, offset
);
9345 return build_fold_indirect_ref(ptr
);
9351 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9352 if (capacity_tree
== error_mark_node
)
9353 return error_mark_node
;
9354 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9357 if (this->end_
->is_nil_expression())
9358 end_tree
= length_tree
;
9361 end_tree
= this->end_
->get_tree(context
);
9362 if (end_tree
== error_mark_node
)
9363 return error_mark_node
;
9364 if (!DECL_P(end_tree
))
9365 end_tree
= save_expr(end_tree
);
9366 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9367 end_tree
= convert_to_integer(length_type
, end_tree
);
9369 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9372 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9374 capacity_tree
= save_expr(capacity_tree
);
9375 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9376 fold_build2_loc(loc
, LT_EXPR
,
9378 end_tree
, start_tree
),
9379 fold_build2_loc(loc
, GT_EXPR
,
9381 end_tree
, capacity_tree
));
9382 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9383 bad_index
, bad_end
);
9386 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9387 if (element_type_tree
== error_mark_node
)
9388 return error_mark_node
;
9389 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9391 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9392 fold_convert_loc(loc
, sizetype
, start_tree
),
9395 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9396 if (value_pointer
== error_mark_node
)
9397 return error_mark_node
;
9399 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9400 TREE_TYPE(value_pointer
),
9401 value_pointer
, offset
);
9403 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9404 end_tree
, start_tree
);
9406 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9407 capacity_tree
, start_tree
);
9409 tree struct_tree
= this->type()->get_tree(gogo
);
9410 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9412 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9414 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9415 tree field
= TYPE_FIELDS(struct_tree
);
9416 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9418 elt
->value
= value_pointer
;
9420 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9421 field
= DECL_CHAIN(field
);
9422 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9424 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9426 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9427 field
= DECL_CHAIN(field
);
9428 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9430 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9432 tree constructor
= build_constructor(struct_tree
, init
);
9434 if (TREE_CONSTANT(value_pointer
)
9435 && TREE_CONSTANT(result_length_tree
)
9436 && TREE_CONSTANT(result_capacity_tree
))
9437 TREE_CONSTANT(constructor
) = 1;
9439 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9440 build3(COND_EXPR
, void_type_node
,
9441 bad_index
, crash
, NULL_TREE
),
9445 // Make an array index expression. END may be NULL.
9448 Expression::make_array_index(Expression
* array
, Expression
* start
,
9449 Expression
* end
, source_location location
)
9451 // Taking a slice of a composite literal requires moving the literal
9453 if (end
!= NULL
&& array
->is_composite_literal())
9455 array
= Expression::make_heap_composite(array
, location
);
9456 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9458 return new Array_index_expression(array
, start
, end
, location
);
9461 // A string index. This is used for both indexing and slicing.
9463 class String_index_expression
: public Expression
9466 String_index_expression(Expression
* string
, Expression
* start
,
9467 Expression
* end
, source_location location
)
9468 : Expression(EXPRESSION_STRING_INDEX
, location
),
9469 string_(string
), start_(start
), end_(end
)
9474 do_traverse(Traverse
*);
9480 do_determine_type(const Type_context
*);
9483 do_check_types(Gogo
*);
9488 return Expression::make_string_index(this->string_
->copy(),
9489 this->start_
->copy(),
9492 : this->end_
->copy()),
9497 do_get_tree(Translate_context
*);
9500 // The string we are getting a value from.
9501 Expression
* string_
;
9502 // The start or only index.
9504 // The end index of a slice. This may be NULL for a single index,
9505 // or it may be a nil expression for the length of the string.
9509 // String index traversal.
9512 String_index_expression::do_traverse(Traverse
* traverse
)
9514 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9515 return TRAVERSE_EXIT
;
9516 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9517 return TRAVERSE_EXIT
;
9518 if (this->end_
!= NULL
)
9520 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9521 return TRAVERSE_EXIT
;
9523 return TRAVERSE_CONTINUE
;
9526 // Return the type of a string index.
9529 String_index_expression::do_type()
9531 if (this->end_
== NULL
)
9532 return Type::lookup_integer_type("uint8");
9534 return Type::make_string_type();
9537 // Determine the type of a string index.
9540 String_index_expression::do_determine_type(const Type_context
*)
9542 this->string_
->determine_type_no_context();
9543 Type_context
subcontext(NULL
, true);
9544 this->start_
->determine_type(&subcontext
);
9545 if (this->end_
!= NULL
)
9546 this->end_
->determine_type(&subcontext
);
9549 // Check types of a string index.
9552 String_index_expression::do_check_types(Gogo
*)
9554 if (this->start_
->type()->integer_type() == NULL
)
9555 this->report_error(_("index must be integer"));
9556 if (this->end_
!= NULL
9557 && this->end_
->type()->integer_type() == NULL
9558 && !this->end_
->is_nil_expression())
9559 this->report_error(_("slice end must be integer"));
9562 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9567 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9569 if (mpz_sgn(ival
) < 0
9570 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9572 error_at(this->start_
->location(), "string index out of bounds");
9573 this->set_is_error();
9576 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9578 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9580 if (mpz_sgn(ival
) < 0
9581 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9583 error_at(this->end_
->location(), "string index out of bounds");
9584 this->set_is_error();
9591 // Get a tree for a string index.
9594 String_index_expression::do_get_tree(Translate_context
* context
)
9596 source_location loc
= this->location();
9598 tree string_tree
= this->string_
->get_tree(context
);
9599 if (string_tree
== error_mark_node
)
9600 return error_mark_node
;
9602 if (this->string_
->type()->points_to() != NULL
)
9603 string_tree
= build_fold_indirect_ref(string_tree
);
9604 if (!DECL_P(string_tree
))
9605 string_tree
= save_expr(string_tree
);
9606 tree string_type
= TREE_TYPE(string_tree
);
9608 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9609 length_tree
= save_expr(length_tree
);
9610 tree length_type
= TREE_TYPE(length_tree
);
9612 tree bad_index
= boolean_false_node
;
9614 tree start_tree
= this->start_
->get_tree(context
);
9615 if (start_tree
== error_mark_node
)
9616 return error_mark_node
;
9617 if (!DECL_P(start_tree
))
9618 start_tree
= save_expr(start_tree
);
9619 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9620 start_tree
= convert_to_integer(length_type
, start_tree
);
9622 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9625 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9627 int code
= (this->end_
== NULL
9628 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9629 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9630 tree crash
= Gogo::runtime_error(code
, loc
);
9632 if (this->end_
== NULL
)
9634 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9636 fold_build2_loc(loc
, GE_EXPR
,
9638 start_tree
, length_tree
));
9640 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9641 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9643 fold_convert_loc(loc
, sizetype
, start_tree
));
9644 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9646 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9647 build3(COND_EXPR
, void_type_node
,
9648 bad_index
, crash
, NULL_TREE
),
9654 if (this->end_
->is_nil_expression())
9655 end_tree
= build_int_cst(length_type
, -1);
9658 end_tree
= this->end_
->get_tree(context
);
9659 if (end_tree
== error_mark_node
)
9660 return error_mark_node
;
9661 if (!DECL_P(end_tree
))
9662 end_tree
= save_expr(end_tree
);
9663 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9664 end_tree
= convert_to_integer(length_type
, end_tree
);
9666 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9669 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9672 static tree strslice_fndecl
;
9673 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9675 "__go_string_slice",
9684 if (ret
== error_mark_node
)
9685 return error_mark_node
;
9686 // This will panic if the bounds are out of range for the
9688 TREE_NOTHROW(strslice_fndecl
) = 0;
9690 if (bad_index
== boolean_false_node
)
9693 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9694 build3(COND_EXPR
, void_type_node
,
9695 bad_index
, crash
, NULL_TREE
),
9700 // Make a string index expression. END may be NULL.
9703 Expression::make_string_index(Expression
* string
, Expression
* start
,
9704 Expression
* end
, source_location location
)
9706 return new String_index_expression(string
, start
, end
, location
);
9711 // Get the type of the map.
9714 Map_index_expression::get_map_type() const
9716 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9718 gcc_assert(saw_errors());
9722 // Map index traversal.
9725 Map_index_expression::do_traverse(Traverse
* traverse
)
9727 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9728 return TRAVERSE_EXIT
;
9729 return Expression::traverse(&this->index_
, traverse
);
9732 // Return the type of a map index.
9735 Map_index_expression::do_type()
9737 Map_type
* mt
= this->get_map_type();
9739 return Type::make_error_type();
9740 Type
* type
= mt
->val_type();
9741 // If this map index is in a tuple assignment, we actually return a
9742 // pointer to the value type. Tuple_map_assignment_statement is
9743 // responsible for handling this correctly. We need to get the type
9744 // right in case this gets assigned to a temporary variable.
9745 if (this->is_in_tuple_assignment_
)
9746 type
= Type::make_pointer_type(type
);
9750 // Fix the type of a map index.
9753 Map_index_expression::do_determine_type(const Type_context
*)
9755 this->map_
->determine_type_no_context();
9756 Map_type
* mt
= this->get_map_type();
9757 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
9758 Type_context
subcontext(key_type
, false);
9759 this->index_
->determine_type(&subcontext
);
9762 // Check types of a map index.
9765 Map_index_expression::do_check_types(Gogo
*)
9768 Map_type
* mt
= this->get_map_type();
9771 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
9774 this->report_error(_("incompatible type for map index"));
9777 error_at(this->location(), "incompatible type for map index (%s)",
9779 this->set_is_error();
9784 // Get a tree for a map index.
9787 Map_index_expression::do_get_tree(Translate_context
* context
)
9789 Map_type
* type
= this->get_map_type();
9791 return error_mark_node
;
9793 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9794 if (valptr
== error_mark_node
)
9795 return error_mark_node
;
9796 valptr
= save_expr(valptr
);
9798 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
9800 if (this->is_lvalue_
)
9801 return build_fold_indirect_ref(valptr
);
9802 else if (this->is_in_tuple_assignment_
)
9804 // Tuple_map_assignment_statement is responsible for using this
9810 return fold_build3(COND_EXPR
, val_type_tree
,
9811 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
9812 fold_convert(TREE_TYPE(valptr
),
9813 null_pointer_node
)),
9814 type
->val_type()->get_init_tree(context
->gogo(),
9816 build_fold_indirect_ref(valptr
));
9820 // Get a tree for the map index. This returns a tree which evaluates
9821 // to a pointer to a value. The pointer will be NULL if the key is
9825 Map_index_expression::get_value_pointer(Translate_context
* context
,
9828 Map_type
* type
= this->get_map_type();
9830 return error_mark_node
;
9832 tree map_tree
= this->map_
->get_tree(context
);
9833 tree index_tree
= this->index_
->get_tree(context
);
9834 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
9835 this->index_
->type(),
9838 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
9839 return error_mark_node
;
9841 if (this->map_
->type()->points_to() != NULL
)
9842 map_tree
= build_fold_indirect_ref(map_tree
);
9844 // We need to pass in a pointer to the key, so stuff it into a
9846 tree tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
9847 DECL_IGNORED_P(tmp
) = 0;
9848 DECL_INITIAL(tmp
) = index_tree
;
9849 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
9850 tree tmpref
= fold_convert(const_ptr_type_node
, build_fold_addr_expr(tmp
));
9851 TREE_ADDRESSABLE(tmp
) = 1;
9853 static tree map_index_fndecl
;
9854 tree call
= Gogo::call_builtin(&map_index_fndecl
,
9858 const_ptr_type_node
,
9859 TREE_TYPE(map_tree
),
9861 const_ptr_type_node
,
9866 : boolean_false_node
));
9867 if (call
== error_mark_node
)
9868 return error_mark_node
;
9869 // This can panic on a map of interface type if the interface holds
9870 // an uncomparable or unhashable type.
9871 TREE_NOTHROW(map_index_fndecl
) = 0;
9873 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
9874 if (val_type_tree
== error_mark_node
)
9875 return error_mark_node
;
9876 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
9878 return build2(COMPOUND_EXPR
, ptr_val_type_tree
,
9880 fold_convert(ptr_val_type_tree
, call
));
9883 // Make a map index expression.
9885 Map_index_expression
*
9886 Expression::make_map_index(Expression
* map
, Expression
* index
,
9887 source_location location
)
9889 return new Map_index_expression(map
, index
, location
);
9892 // Class Field_reference_expression.
9894 // Return the type of a field reference.
9897 Field_reference_expression::do_type()
9899 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9900 gcc_assert(struct_type
!= NULL
);
9901 return struct_type
->field(this->field_index_
)->type();
9904 // Check the types for a field reference.
9907 Field_reference_expression::do_check_types(Gogo
*)
9909 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9910 gcc_assert(struct_type
!= NULL
);
9911 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
9914 // Get a tree for a field reference.
9917 Field_reference_expression::do_get_tree(Translate_context
* context
)
9919 tree struct_tree
= this->expr_
->get_tree(context
);
9920 if (struct_tree
== error_mark_node
9921 || TREE_TYPE(struct_tree
) == error_mark_node
)
9922 return error_mark_node
;
9923 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
9924 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
9925 if (field
== NULL_TREE
)
9927 // This can happen for a type which refers to itself indirectly
9928 // and then turns out to be erroneous.
9929 gcc_assert(saw_errors());
9930 return error_mark_node
;
9932 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
9934 field
= DECL_CHAIN(field
);
9935 gcc_assert(field
!= NULL_TREE
);
9937 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
9941 // Make a reference to a qualified identifier in an expression.
9943 Field_reference_expression
*
9944 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
9945 source_location location
)
9947 return new Field_reference_expression(expr
, field_index
, location
);
9950 // Class Interface_field_reference_expression.
9952 // Return a tree for the pointer to the function to call.
9955 Interface_field_reference_expression::get_function_tree(Translate_context
*,
9958 if (this->expr_
->type()->points_to() != NULL
)
9959 expr
= build_fold_indirect_ref(expr
);
9961 tree expr_type
= TREE_TYPE(expr
);
9962 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9964 tree field
= TYPE_FIELDS(expr_type
);
9965 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
9967 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9968 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
9970 table
= build_fold_indirect_ref(table
);
9971 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
9973 std::string name
= Gogo::unpack_hidden_name(this->name_
);
9974 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
9976 field
= DECL_CHAIN(field
))
9978 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
9981 gcc_assert(field
!= NULL_TREE
);
9983 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
9986 // Return a tree for the first argument to pass to the interface
9990 Interface_field_reference_expression::get_underlying_object_tree(
9994 if (this->expr_
->type()->points_to() != NULL
)
9995 expr
= build_fold_indirect_ref(expr
);
9997 tree expr_type
= TREE_TYPE(expr
);
9998 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10000 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
10001 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
10003 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10009 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
10011 return Expression::traverse(&this->expr_
, traverse
);
10014 // Return the type of an interface field reference.
10017 Interface_field_reference_expression::do_type()
10019 Type
* expr_type
= this->expr_
->type();
10021 Type
* points_to
= expr_type
->points_to();
10022 if (points_to
!= NULL
)
10023 expr_type
= points_to
;
10025 Interface_type
* interface_type
= expr_type
->interface_type();
10026 if (interface_type
== NULL
)
10027 return Type::make_error_type();
10029 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
10030 if (method
== NULL
)
10031 return Type::make_error_type();
10033 return method
->type();
10036 // Determine types.
10039 Interface_field_reference_expression::do_determine_type(const Type_context
*)
10041 this->expr_
->determine_type_no_context();
10044 // Check the types for an interface field reference.
10047 Interface_field_reference_expression::do_check_types(Gogo
*)
10049 Type
* type
= this->expr_
->type();
10051 Type
* points_to
= type
->points_to();
10052 if (points_to
!= NULL
)
10055 Interface_type
* interface_type
= type
->interface_type();
10056 if (interface_type
== NULL
)
10057 this->report_error(_("expected interface or pointer to interface"));
10060 const Typed_identifier
* method
=
10061 interface_type
->find_method(this->name_
);
10062 if (method
== NULL
)
10064 error_at(this->location(), "method %qs not in interface",
10065 Gogo::message_name(this->name_
).c_str());
10066 this->set_is_error();
10071 // Get a tree for a reference to a field in an interface. There is no
10072 // standard tree type representation for this: it's a function
10073 // attached to its first argument, like a Bound_method_expression.
10074 // The only places it may currently be used are in a Call_expression
10075 // or a Go_statement, which will take it apart directly. So this has
10076 // nothing to do at present.
10079 Interface_field_reference_expression::do_get_tree(Translate_context
*)
10084 // Make a reference to a field in an interface.
10087 Expression::make_interface_field_reference(Expression
* expr
,
10088 const std::string
& field
,
10089 source_location location
)
10091 return new Interface_field_reference_expression(expr
, field
, location
);
10094 // A general selector. This is a Parser_expression for LEFT.NAME. It
10095 // is lowered after we know the type of the left hand side.
10097 class Selector_expression
: public Parser_expression
10100 Selector_expression(Expression
* left
, const std::string
& name
,
10101 source_location location
)
10102 : Parser_expression(EXPRESSION_SELECTOR
, location
),
10103 left_(left
), name_(name
)
10108 do_traverse(Traverse
* traverse
)
10109 { return Expression::traverse(&this->left_
, traverse
); }
10112 do_lower(Gogo
*, Named_object
*, int);
10117 return new Selector_expression(this->left_
->copy(), this->name_
,
10123 lower_method_expression(Gogo
*);
10125 // The expression on the left hand side.
10127 // The name on the right hand side.
10131 // Lower a selector expression once we know the real type of the left
10135 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
10137 Expression
* left
= this->left_
;
10138 if (left
->is_type_expression())
10139 return this->lower_method_expression(gogo
);
10140 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
10144 // Lower a method expression T.M or (*T).M. We turn this into a
10145 // function literal.
10148 Selector_expression::lower_method_expression(Gogo
* gogo
)
10150 source_location location
= this->location();
10151 Type
* type
= this->left_
->type();
10152 const std::string
& name(this->name_
);
10155 if (type
->points_to() == NULL
)
10156 is_pointer
= false;
10160 type
= type
->points_to();
10162 Named_type
* nt
= type
->named_type();
10166 ("method expression requires named type or "
10167 "pointer to named type"));
10168 return Expression::make_error(location
);
10172 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
10173 if (method
== NULL
)
10176 error_at(location
, "type %<%s%> has no method %<%s%>",
10177 nt
->message_name().c_str(),
10178 Gogo::message_name(name
).c_str());
10180 error_at(location
, "method %<%s%> is ambiguous in type %<%s%>",
10181 Gogo::message_name(name
).c_str(),
10182 nt
->message_name().c_str());
10183 return Expression::make_error(location
);
10186 if (!is_pointer
&& !method
->is_value_method())
10188 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
10189 nt
->message_name().c_str(),
10190 Gogo::message_name(name
).c_str());
10191 return Expression::make_error(location
);
10194 // Build a new function type in which the receiver becomes the first
10196 Function_type
* method_type
= method
->type();
10197 gcc_assert(method_type
->is_method());
10199 const char* const receiver_name
= "$this";
10200 Typed_identifier_list
* parameters
= new Typed_identifier_list();
10201 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
10204 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
10205 if (method_parameters
!= NULL
)
10207 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10208 p
!= method_parameters
->end();
10210 parameters
->push_back(*p
);
10213 const Typed_identifier_list
* method_results
= method_type
->results();
10214 Typed_identifier_list
* results
;
10215 if (method_results
== NULL
)
10219 results
= new Typed_identifier_list();
10220 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
10221 p
!= method_results
->end();
10223 results
->push_back(*p
);
10226 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
10228 if (method_type
->is_varargs())
10229 fntype
->set_is_varargs();
10231 // We generate methods which always takes a pointer to the receiver
10232 // as their first argument. If this is for a pointer type, we can
10233 // simply reuse the existing function. We use an internal hack to
10234 // get the right type.
10238 Named_object
* mno
= (method
->needs_stub_method()
10239 ? method
->stub_object()
10240 : method
->named_object());
10241 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
10242 f
= Expression::make_cast(fntype
, f
, location
);
10243 Type_conversion_expression
* tce
=
10244 static_cast<Type_conversion_expression
*>(f
);
10245 tce
->set_may_convert_function_types();
10249 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
10252 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
10253 gcc_assert(vno
!= NULL
);
10254 Expression
* ve
= Expression::make_var_reference(vno
, location
);
10255 Expression
* bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
10256 gcc_assert(bm
!= NULL
&& !bm
->is_error_expression());
10258 Expression_list
* args
;
10259 if (method_parameters
== NULL
)
10263 args
= new Expression_list();
10264 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10265 p
!= method_parameters
->end();
10268 vno
= gogo
->lookup(p
->name(), NULL
);
10269 gcc_assert(vno
!= NULL
);
10270 args
->push_back(Expression::make_var_reference(vno
, location
));
10274 Call_expression
* call
= Expression::make_call(bm
, args
,
10275 method_type
->is_varargs(),
10278 size_t count
= call
->result_count();
10281 s
= Statement::make_statement(call
);
10284 Expression_list
* retvals
= new Expression_list();
10286 retvals
->push_back(call
);
10289 for (size_t i
= 0; i
< count
; ++i
)
10290 retvals
->push_back(Expression::make_call_result(call
, i
));
10292 s
= Statement::make_return_statement(no
->func_value()->type()->results(),
10293 retvals
, location
);
10295 gogo
->add_statement(s
);
10297 gogo
->finish_function(location
);
10299 return Expression::make_func_reference(no
, NULL
, location
);
10302 // Make a selector expression.
10305 Expression::make_selector(Expression
* left
, const std::string
& name
,
10306 source_location location
)
10308 return new Selector_expression(left
, name
, location
);
10311 // Implement the builtin function new.
10313 class Allocation_expression
: public Expression
10316 Allocation_expression(Type
* type
, source_location location
)
10317 : Expression(EXPRESSION_ALLOCATION
, location
),
10323 do_traverse(Traverse
* traverse
)
10324 { return Type::traverse(this->type_
, traverse
); }
10328 { return Type::make_pointer_type(this->type_
); }
10331 do_determine_type(const Type_context
*)
10335 do_check_types(Gogo
*);
10339 { return new Allocation_expression(this->type_
, this->location()); }
10342 do_get_tree(Translate_context
*);
10345 // The type we are allocating.
10349 // Check the type of an allocation expression.
10352 Allocation_expression::do_check_types(Gogo
*)
10354 if (this->type_
->function_type() != NULL
)
10355 this->report_error(_("invalid new of function type"));
10358 // Return a tree for an allocation expression.
10361 Allocation_expression::do_get_tree(Translate_context
* context
)
10363 tree type_tree
= this->type_
->get_tree(context
->gogo());
10364 if (type_tree
== error_mark_node
)
10365 return error_mark_node
;
10366 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10367 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10369 if (space
== error_mark_node
)
10370 return error_mark_node
;
10371 return fold_convert(build_pointer_type(type_tree
), space
);
10374 // Make an allocation expression.
10377 Expression::make_allocation(Type
* type
, source_location location
)
10379 return new Allocation_expression(type
, location
);
10382 // Implement the builtin function make.
10384 class Make_expression
: public Expression
10387 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10388 : Expression(EXPRESSION_MAKE
, location
),
10389 type_(type
), args_(args
)
10394 do_traverse(Traverse
* traverse
);
10398 { return this->type_
; }
10401 do_determine_type(const Type_context
*);
10404 do_check_types(Gogo
*);
10409 return new Make_expression(this->type_
, this->args_
->copy(),
10414 do_get_tree(Translate_context
*);
10417 // The type we are making.
10419 // The arguments to pass to the make routine.
10420 Expression_list
* args_
;
10426 Make_expression::do_traverse(Traverse
* traverse
)
10428 if (this->args_
!= NULL
10429 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10430 return TRAVERSE_EXIT
;
10431 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10432 return TRAVERSE_EXIT
;
10433 return TRAVERSE_CONTINUE
;
10436 // Set types of arguments.
10439 Make_expression::do_determine_type(const Type_context
*)
10441 if (this->args_
!= NULL
)
10443 Type_context
context(Type::lookup_integer_type("int"), false);
10444 for (Expression_list::const_iterator pe
= this->args_
->begin();
10445 pe
!= this->args_
->end();
10447 (*pe
)->determine_type(&context
);
10451 // Check types for a make expression.
10454 Make_expression::do_check_types(Gogo
*)
10456 if (this->type_
->channel_type() == NULL
10457 && this->type_
->map_type() == NULL
10458 && (this->type_
->array_type() == NULL
10459 || this->type_
->array_type()->length() != NULL
))
10460 this->report_error(_("invalid type for make function"));
10461 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10462 this->set_is_error();
10465 // Return a tree for a make expression.
10468 Make_expression::do_get_tree(Translate_context
* context
)
10470 return this->type_
->make_expression_tree(context
, this->args_
,
10474 // Make a make expression.
10477 Expression::make_make(Type
* type
, Expression_list
* args
,
10478 source_location location
)
10480 return new Make_expression(type
, args
, location
);
10483 // Construct a struct.
10485 class Struct_construction_expression
: public Expression
10488 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10489 source_location location
)
10490 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10491 type_(type
), vals_(vals
)
10494 // Return whether this is a constant initializer.
10496 is_constant_struct() const;
10500 do_traverse(Traverse
* traverse
);
10504 { return this->type_
; }
10507 do_determine_type(const Type_context
*);
10510 do_check_types(Gogo
*);
10515 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10520 do_is_addressable() const
10524 do_get_tree(Translate_context
*);
10527 do_export(Export
*) const;
10530 // The type of the struct to construct.
10532 // The list of values, in order of the fields in the struct. A NULL
10533 // entry means that the field should be zero-initialized.
10534 Expression_list
* vals_
;
10540 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10542 if (this->vals_
!= NULL
10543 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10544 return TRAVERSE_EXIT
;
10545 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10546 return TRAVERSE_EXIT
;
10547 return TRAVERSE_CONTINUE
;
10550 // Return whether this is a constant initializer.
10553 Struct_construction_expression::is_constant_struct() const
10555 if (this->vals_
== NULL
)
10557 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10558 pv
!= this->vals_
->end();
10562 && !(*pv
)->is_constant()
10563 && (!(*pv
)->is_composite_literal()
10564 || (*pv
)->is_nonconstant_composite_literal()))
10568 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10569 for (Struct_field_list::const_iterator pf
= fields
->begin();
10570 pf
!= fields
->end();
10573 // There are no constant constructors for interfaces.
10574 if (pf
->type()->interface_type() != NULL
)
10581 // Final type determination.
10584 Struct_construction_expression::do_determine_type(const Type_context
*)
10586 if (this->vals_
== NULL
)
10588 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10589 Expression_list::const_iterator pv
= this->vals_
->begin();
10590 for (Struct_field_list::const_iterator pf
= fields
->begin();
10591 pf
!= fields
->end();
10594 if (pv
== this->vals_
->end())
10598 Type_context
subcontext(pf
->type(), false);
10599 (*pv
)->determine_type(&subcontext
);
10607 Struct_construction_expression::do_check_types(Gogo
*)
10609 if (this->vals_
== NULL
)
10612 Struct_type
* st
= this->type_
->struct_type();
10613 if (this->vals_
->size() > st
->field_count())
10615 this->report_error(_("too many expressions for struct"));
10619 const Struct_field_list
* fields
= st
->fields();
10620 Expression_list::const_iterator pv
= this->vals_
->begin();
10622 for (Struct_field_list::const_iterator pf
= fields
->begin();
10623 pf
!= fields
->end();
10626 if (pv
== this->vals_
->end())
10628 this->report_error(_("too few expressions for struct"));
10635 std::string reason
;
10636 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10638 if (reason
.empty())
10639 error_at((*pv
)->location(),
10640 "incompatible type for field %d in struct construction",
10643 error_at((*pv
)->location(),
10644 ("incompatible type for field %d in "
10645 "struct construction (%s)"),
10646 i
+ 1, reason
.c_str());
10647 this->set_is_error();
10650 gcc_assert(pv
== this->vals_
->end());
10653 // Return a tree for constructing a struct.
10656 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10658 Gogo
* gogo
= context
->gogo();
10660 if (this->vals_
== NULL
)
10661 return this->type_
->get_init_tree(gogo
, false);
10663 tree type_tree
= this->type_
->get_tree(gogo
);
10664 if (type_tree
== error_mark_node
)
10665 return error_mark_node
;
10666 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10668 bool is_constant
= true;
10669 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10670 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10672 Struct_field_list::const_iterator pf
= fields
->begin();
10673 Expression_list::const_iterator pv
= this->vals_
->begin();
10674 for (tree field
= TYPE_FIELDS(type_tree
);
10675 field
!= NULL_TREE
;
10676 field
= DECL_CHAIN(field
), ++pf
)
10678 gcc_assert(pf
!= fields
->end());
10681 if (pv
== this->vals_
->end())
10682 val
= pf
->type()->get_init_tree(gogo
, false);
10683 else if (*pv
== NULL
)
10685 val
= pf
->type()->get_init_tree(gogo
, false);
10690 val
= Expression::convert_for_assignment(context
, pf
->type(),
10692 (*pv
)->get_tree(context
),
10697 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10698 return error_mark_node
;
10700 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10701 elt
->index
= field
;
10703 if (!TREE_CONSTANT(val
))
10704 is_constant
= false;
10706 gcc_assert(pf
== fields
->end());
10708 tree ret
= build_constructor(type_tree
, elts
);
10710 TREE_CONSTANT(ret
) = 1;
10714 // Export a struct construction.
10717 Struct_construction_expression::do_export(Export
* exp
) const
10719 exp
->write_c_string("convert(");
10720 exp
->write_type(this->type_
);
10721 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10722 pv
!= this->vals_
->end();
10725 exp
->write_c_string(", ");
10727 (*pv
)->export_expression(exp
);
10729 exp
->write_c_string(")");
10732 // Make a struct composite literal. This used by the thunk code.
10735 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
10736 source_location location
)
10738 gcc_assert(type
->struct_type() != NULL
);
10739 return new Struct_construction_expression(type
, vals
, location
);
10742 // Construct an array. This class is not used directly; instead we
10743 // use the child classes, Fixed_array_construction_expression and
10744 // Open_array_construction_expression.
10746 class Array_construction_expression
: public Expression
10749 Array_construction_expression(Expression_classification classification
,
10750 Type
* type
, Expression_list
* vals
,
10751 source_location location
)
10752 : Expression(classification
, location
),
10753 type_(type
), vals_(vals
)
10757 // Return whether this is a constant initializer.
10759 is_constant_array() const;
10761 // Return the number of elements.
10763 element_count() const
10764 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
10768 do_traverse(Traverse
* traverse
);
10772 { return this->type_
; }
10775 do_determine_type(const Type_context
*);
10778 do_check_types(Gogo
*);
10781 do_is_addressable() const
10785 do_export(Export
*) const;
10787 // The list of values.
10790 { return this->vals_
; }
10792 // Get a constructor tree for the array values.
10794 get_constructor_tree(Translate_context
* context
, tree type_tree
);
10797 // The type of the array to construct.
10799 // The list of values.
10800 Expression_list
* vals_
;
10806 Array_construction_expression::do_traverse(Traverse
* traverse
)
10808 if (this->vals_
!= NULL
10809 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10810 return TRAVERSE_EXIT
;
10811 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10812 return TRAVERSE_EXIT
;
10813 return TRAVERSE_CONTINUE
;
10816 // Return whether this is a constant initializer.
10819 Array_construction_expression::is_constant_array() const
10821 if (this->vals_
== NULL
)
10824 // There are no constant constructors for interfaces.
10825 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
10828 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10829 pv
!= this->vals_
->end();
10833 && !(*pv
)->is_constant()
10834 && (!(*pv
)->is_composite_literal()
10835 || (*pv
)->is_nonconstant_composite_literal()))
10841 // Final type determination.
10844 Array_construction_expression::do_determine_type(const Type_context
*)
10846 if (this->vals_
== NULL
)
10848 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
10849 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10850 pv
!= this->vals_
->end();
10854 (*pv
)->determine_type(&subcontext
);
10861 Array_construction_expression::do_check_types(Gogo
*)
10863 if (this->vals_
== NULL
)
10866 Array_type
* at
= this->type_
->array_type();
10868 Type
* element_type
= at
->element_type();
10869 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10870 pv
!= this->vals_
->end();
10874 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
10876 error_at((*pv
)->location(),
10877 "incompatible type for element %d in composite literal",
10879 this->set_is_error();
10883 Expression
* length
= at
->length();
10884 if (length
!= NULL
)
10889 if (at
->length()->integer_constant_value(true, val
, &type
))
10891 if (this->vals_
->size() > mpz_get_ui(val
))
10892 this->report_error(_("too many elements in composite literal"));
10898 // Get a constructor tree for the array values.
10901 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
10904 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10905 (this->vals_
== NULL
10907 : this->vals_
->size()));
10908 Type
* element_type
= this->type_
->array_type()->element_type();
10909 bool is_constant
= true;
10910 if (this->vals_
!= NULL
)
10913 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10914 pv
!= this->vals_
->end();
10917 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
10918 elt
->index
= size_int(i
);
10920 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10923 tree value_tree
= (*pv
)->get_tree(context
);
10924 elt
->value
= Expression::convert_for_assignment(context
,
10930 if (elt
->value
== error_mark_node
)
10931 return error_mark_node
;
10932 if (!TREE_CONSTANT(elt
->value
))
10933 is_constant
= false;
10937 tree ret
= build_constructor(type_tree
, values
);
10939 TREE_CONSTANT(ret
) = 1;
10943 // Export an array construction.
10946 Array_construction_expression::do_export(Export
* exp
) const
10948 exp
->write_c_string("convert(");
10949 exp
->write_type(this->type_
);
10950 if (this->vals_
!= NULL
)
10952 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10953 pv
!= this->vals_
->end();
10956 exp
->write_c_string(", ");
10958 (*pv
)->export_expression(exp
);
10961 exp
->write_c_string(")");
10964 // Construct a fixed array.
10966 class Fixed_array_construction_expression
:
10967 public Array_construction_expression
10970 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
10971 source_location location
)
10972 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
10973 type
, vals
, location
)
10975 gcc_assert(type
->array_type() != NULL
10976 && type
->array_type()->length() != NULL
);
10983 return new Fixed_array_construction_expression(this->type(),
10984 (this->vals() == NULL
10986 : this->vals()->copy()),
10991 do_get_tree(Translate_context
*);
10994 // Return a tree for constructing a fixed array.
10997 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
10999 return this->get_constructor_tree(context
,
11000 this->type()->get_tree(context
->gogo()));
11003 // Construct an open array.
11005 class Open_array_construction_expression
: public Array_construction_expression
11008 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
11009 source_location location
)
11010 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
11011 type
, vals
, location
)
11013 gcc_assert(type
->array_type() != NULL
11014 && type
->array_type()->length() == NULL
);
11018 // Note that taking the address of an open array literal is invalid.
11023 return new Open_array_construction_expression(this->type(),
11024 (this->vals() == NULL
11026 : this->vals()->copy()),
11031 do_get_tree(Translate_context
*);
11034 // Return a tree for constructing an open array.
11037 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
11039 Array_type
* array_type
= this->type()->array_type();
11040 if (array_type
== NULL
)
11042 gcc_assert(this->type()->is_error_type());
11043 return error_mark_node
;
11046 Type
* element_type
= array_type
->element_type();
11047 tree element_type_tree
= element_type
->get_tree(context
->gogo());
11048 if (element_type_tree
== error_mark_node
)
11049 return error_mark_node
;
11053 if (this->vals() == NULL
|| this->vals()->empty())
11055 // We need to create a unique value.
11056 tree max
= size_int(0);
11057 tree constructor_type
= build_array_type(element_type_tree
,
11058 build_index_type(max
));
11059 if (constructor_type
== error_mark_node
)
11060 return error_mark_node
;
11061 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
11062 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
11063 elt
->index
= size_int(0);
11064 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
11065 values
= build_constructor(constructor_type
, vec
);
11066 if (TREE_CONSTANT(elt
->value
))
11067 TREE_CONSTANT(values
) = 1;
11068 length_tree
= size_int(0);
11072 tree max
= size_int(this->vals()->size() - 1);
11073 tree constructor_type
= build_array_type(element_type_tree
,
11074 build_index_type(max
));
11075 if (constructor_type
== error_mark_node
)
11076 return error_mark_node
;
11077 values
= this->get_constructor_tree(context
, constructor_type
);
11078 length_tree
= size_int(this->vals()->size());
11081 if (values
== error_mark_node
)
11082 return error_mark_node
;
11084 bool is_constant_initializer
= TREE_CONSTANT(values
);
11085 bool is_in_function
= context
->function() != NULL
;
11087 if (is_constant_initializer
)
11089 tree tmp
= build_decl(this->location(), VAR_DECL
,
11090 create_tmp_var_name("C"), TREE_TYPE(values
));
11091 DECL_EXTERNAL(tmp
) = 0;
11092 TREE_PUBLIC(tmp
) = 0;
11093 TREE_STATIC(tmp
) = 1;
11094 DECL_ARTIFICIAL(tmp
) = 1;
11095 if (is_in_function
)
11097 // If this is not a function, we will only initialize the
11098 // value once, so we can use this directly rather than
11099 // copying it. In that case we can't make it read-only,
11100 // because the program is permitted to change it.
11101 TREE_READONLY(tmp
) = 1;
11102 TREE_CONSTANT(tmp
) = 1;
11104 DECL_INITIAL(tmp
) = values
;
11105 rest_of_decl_compilation(tmp
, 1, 0);
11111 if (!is_in_function
&& is_constant_initializer
)
11113 // Outside of a function, we know the initializer will only run
11115 space
= build_fold_addr_expr(values
);
11120 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
11121 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
11123 space
= save_expr(space
);
11125 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
11126 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
11127 TREE_THIS_NOTRAP(ref
) = 1;
11128 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
11131 // Build a constructor for the open array.
11133 tree type_tree
= this->type()->get_tree(context
->gogo());
11134 if (type_tree
== error_mark_node
)
11135 return error_mark_node
;
11136 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
11138 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
11140 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11141 tree field
= TYPE_FIELDS(type_tree
);
11142 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
11143 elt
->index
= field
;
11144 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
11146 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11147 field
= DECL_CHAIN(field
);
11148 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
11149 elt
->index
= field
;
11150 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11152 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11153 field
= DECL_CHAIN(field
);
11154 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
11155 elt
->index
= field
;
11156 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11158 tree constructor
= build_constructor(type_tree
, init
);
11159 if (constructor
== error_mark_node
)
11160 return error_mark_node
;
11161 if (!is_in_function
&& is_constant_initializer
)
11162 TREE_CONSTANT(constructor
) = 1;
11164 if (set
== NULL_TREE
)
11165 return constructor
;
11167 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
11170 // Make a slice composite literal. This is used by the type
11171 // descriptor code.
11174 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
11175 source_location location
)
11177 gcc_assert(type
->is_open_array_type());
11178 return new Open_array_construction_expression(type
, vals
, location
);
11181 // Construct a map.
11183 class Map_construction_expression
: public Expression
11186 Map_construction_expression(Type
* type
, Expression_list
* vals
,
11187 source_location location
)
11188 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
11189 type_(type
), vals_(vals
)
11190 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
11194 do_traverse(Traverse
* traverse
);
11198 { return this->type_
; }
11201 do_determine_type(const Type_context
*);
11204 do_check_types(Gogo
*);
11209 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
11214 do_get_tree(Translate_context
*);
11217 do_export(Export
*) const;
11220 // The type of the map to construct.
11222 // The list of values.
11223 Expression_list
* vals_
;
11229 Map_construction_expression::do_traverse(Traverse
* traverse
)
11231 if (this->vals_
!= NULL
11232 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11233 return TRAVERSE_EXIT
;
11234 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11235 return TRAVERSE_EXIT
;
11236 return TRAVERSE_CONTINUE
;
11239 // Final type determination.
11242 Map_construction_expression::do_determine_type(const Type_context
*)
11244 if (this->vals_
== NULL
)
11247 Map_type
* mt
= this->type_
->map_type();
11248 Type_context
key_context(mt
->key_type(), false);
11249 Type_context
val_context(mt
->val_type(), false);
11250 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11251 pv
!= this->vals_
->end();
11254 (*pv
)->determine_type(&key_context
);
11256 (*pv
)->determine_type(&val_context
);
11263 Map_construction_expression::do_check_types(Gogo
*)
11265 if (this->vals_
== NULL
)
11268 Map_type
* mt
= this->type_
->map_type();
11270 Type
* key_type
= mt
->key_type();
11271 Type
* val_type
= mt
->val_type();
11272 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11273 pv
!= this->vals_
->end();
11276 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
11278 error_at((*pv
)->location(),
11279 "incompatible type for element %d key in map construction",
11281 this->set_is_error();
11284 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
11286 error_at((*pv
)->location(),
11287 ("incompatible type for element %d value "
11288 "in map construction"),
11290 this->set_is_error();
11295 // Return a tree for constructing a map.
11298 Map_construction_expression::do_get_tree(Translate_context
* context
)
11300 Gogo
* gogo
= context
->gogo();
11301 source_location loc
= this->location();
11303 Map_type
* mt
= this->type_
->map_type();
11305 // Build a struct to hold the key and value.
11306 tree struct_type
= make_node(RECORD_TYPE
);
11308 Type
* key_type
= mt
->key_type();
11309 tree id
= get_identifier("__key");
11310 tree key_type_tree
= key_type
->get_tree(gogo
);
11311 if (key_type_tree
== error_mark_node
)
11312 return error_mark_node
;
11313 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type_tree
);
11314 DECL_CONTEXT(key_field
) = struct_type
;
11315 TYPE_FIELDS(struct_type
) = key_field
;
11317 Type
* val_type
= mt
->val_type();
11318 id
= get_identifier("__val");
11319 tree val_type_tree
= val_type
->get_tree(gogo
);
11320 if (val_type_tree
== error_mark_node
)
11321 return error_mark_node
;
11322 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type_tree
);
11323 DECL_CONTEXT(val_field
) = struct_type
;
11324 DECL_CHAIN(key_field
) = val_field
;
11326 layout_type(struct_type
);
11328 bool is_constant
= true;
11333 if (this->vals_
== NULL
|| this->vals_
->empty())
11335 valaddr
= null_pointer_node
;
11336 make_tmp
= NULL_TREE
;
11340 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11341 this->vals_
->size() / 2);
11343 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11344 pv
!= this->vals_
->end();
11347 bool one_is_constant
= true;
11349 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11351 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11352 elt
->index
= key_field
;
11353 tree val_tree
= (*pv
)->get_tree(context
);
11354 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11357 if (elt
->value
== error_mark_node
)
11358 return error_mark_node
;
11359 if (!TREE_CONSTANT(elt
->value
))
11360 one_is_constant
= false;
11364 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11365 elt
->index
= val_field
;
11366 val_tree
= (*pv
)->get_tree(context
);
11367 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11370 if (elt
->value
== error_mark_node
)
11371 return error_mark_node
;
11372 if (!TREE_CONSTANT(elt
->value
))
11373 one_is_constant
= false;
11375 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11376 elt
->index
= size_int(i
);
11377 elt
->value
= build_constructor(struct_type
, one
);
11378 if (one_is_constant
)
11379 TREE_CONSTANT(elt
->value
) = 1;
11381 is_constant
= false;
11384 tree index_type
= build_index_type(size_int(i
- 1));
11385 tree array_type
= build_array_type(struct_type
, index_type
);
11386 tree init
= build_constructor(array_type
, values
);
11388 TREE_CONSTANT(init
) = 1;
11390 if (current_function_decl
!= NULL
)
11392 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11393 DECL_INITIAL(tmp
) = init
;
11394 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11395 TREE_ADDRESSABLE(tmp
) = 1;
11399 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11400 DECL_EXTERNAL(tmp
) = 0;
11401 TREE_PUBLIC(tmp
) = 0;
11402 TREE_STATIC(tmp
) = 1;
11403 DECL_ARTIFICIAL(tmp
) = 1;
11404 if (!TREE_CONSTANT(init
))
11405 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11409 TREE_READONLY(tmp
) = 1;
11410 TREE_CONSTANT(tmp
) = 1;
11411 DECL_INITIAL(tmp
) = init
;
11412 make_tmp
= NULL_TREE
;
11414 rest_of_decl_compilation(tmp
, 1, 0);
11417 valaddr
= build_fold_addr_expr(tmp
);
11420 tree descriptor
= gogo
->map_descriptor(mt
);
11422 tree type_tree
= this->type_
->get_tree(gogo
);
11423 if (type_tree
== error_mark_node
)
11424 return error_mark_node
;
11426 static tree construct_map_fndecl
;
11427 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11429 "__go_construct_map",
11432 TREE_TYPE(descriptor
),
11437 TYPE_SIZE_UNIT(struct_type
),
11439 byte_position(val_field
),
11441 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11442 const_ptr_type_node
,
11443 fold_convert(const_ptr_type_node
, valaddr
));
11444 if (call
== error_mark_node
)
11445 return error_mark_node
;
11448 if (make_tmp
== NULL
)
11451 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11455 // Export an array construction.
11458 Map_construction_expression::do_export(Export
* exp
) const
11460 exp
->write_c_string("convert(");
11461 exp
->write_type(this->type_
);
11462 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11463 pv
!= this->vals_
->end();
11466 exp
->write_c_string(", ");
11467 (*pv
)->export_expression(exp
);
11469 exp
->write_c_string(")");
11472 // A general composite literal. This is lowered to a type specific
11475 class Composite_literal_expression
: public Parser_expression
11478 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11479 Expression_list
* vals
, source_location location
)
11480 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11481 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11486 do_traverse(Traverse
* traverse
);
11489 do_lower(Gogo
*, Named_object
*, int);
11494 return new Composite_literal_expression(this->type_
, this->depth_
,
11496 (this->vals_
== NULL
11498 : this->vals_
->copy()),
11504 lower_struct(Type
*);
11507 lower_array(Type
*);
11510 make_array(Type
*, Expression_list
*);
11513 lower_map(Gogo
*, Named_object
*, Type
*);
11515 // The type of the composite literal.
11517 // The depth within a list of composite literals within a composite
11518 // literal, when the type is omitted.
11520 // The values to put in the composite literal.
11521 Expression_list
* vals_
;
11522 // If this is true, then VALS_ is a list of pairs: a key and a
11523 // value. In an array initializer, a missing key will be NULL.
11530 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11532 if (this->vals_
!= NULL
11533 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11534 return TRAVERSE_EXIT
;
11535 return Type::traverse(this->type_
, traverse
);
11538 // Lower a generic composite literal into a specific version based on
11542 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
11544 Type
* type
= this->type_
;
11546 for (int depth
= this->depth_
; depth
> 0; --depth
)
11548 if (type
->array_type() != NULL
)
11549 type
= type
->array_type()->element_type();
11550 else if (type
->map_type() != NULL
)
11551 type
= type
->map_type()->val_type();
11554 if (!type
->is_error_type())
11555 error_at(this->location(),
11556 ("may only omit types within composite literals "
11557 "of slice, array, or map type"));
11558 return Expression::make_error(this->location());
11562 if (type
->is_error_type())
11563 return Expression::make_error(this->location());
11564 else if (type
->struct_type() != NULL
)
11565 return this->lower_struct(type
);
11566 else if (type
->array_type() != NULL
)
11567 return this->lower_array(type
);
11568 else if (type
->map_type() != NULL
)
11569 return this->lower_map(gogo
, function
, type
);
11572 error_at(this->location(),
11573 ("expected struct, slice, array, or map type "
11574 "for composite literal"));
11575 return Expression::make_error(this->location());
11579 // Lower a struct composite literal.
11582 Composite_literal_expression::lower_struct(Type
* type
)
11584 source_location location
= this->location();
11585 Struct_type
* st
= type
->struct_type();
11586 if (this->vals_
== NULL
|| !this->has_keys_
)
11587 return new Struct_construction_expression(type
, this->vals_
, location
);
11589 size_t field_count
= st
->field_count();
11590 std::vector
<Expression
*> vals(field_count
);
11591 Expression_list::const_iterator p
= this->vals_
->begin();
11592 while (p
!= this->vals_
->end())
11594 Expression
* name_expr
= *p
;
11597 gcc_assert(p
!= this->vals_
->end());
11598 Expression
* val
= *p
;
11602 if (name_expr
== NULL
)
11604 error_at(val
->location(), "mixture of field and value initializers");
11605 return Expression::make_error(location
);
11608 bool bad_key
= false;
11610 switch (name_expr
->classification())
11612 case EXPRESSION_UNKNOWN_REFERENCE
:
11613 name
= name_expr
->unknown_expression()->name();
11616 case EXPRESSION_CONST_REFERENCE
:
11617 name
= static_cast<Const_expression
*>(name_expr
)->name();
11620 case EXPRESSION_TYPE
:
11622 Type
* t
= name_expr
->type();
11623 Named_type
* nt
= t
->named_type();
11631 case EXPRESSION_VAR_REFERENCE
:
11632 name
= name_expr
->var_expression()->name();
11635 case EXPRESSION_FUNC_REFERENCE
:
11636 name
= name_expr
->func_expression()->name();
11639 case EXPRESSION_UNARY
:
11640 // If there is a local variable around with the same name as
11641 // the field, and this occurs in the closure, then the
11642 // parser may turn the field reference into an indirection
11643 // through the closure. FIXME: This is a mess.
11646 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11647 if (ue
->op() == OPERATOR_MULT
)
11649 Field_reference_expression
* fre
=
11650 ue
->operand()->field_reference_expression();
11654 fre
->expr()->type()->deref()->struct_type();
11657 const Struct_field
* sf
= st
->field(fre
->field_index());
11658 name
= sf
->field_name();
11660 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11661 size_t buflen
= strlen(buf
);
11662 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11665 name
= name
.substr(0, name
.length() - buflen
);
11680 error_at(name_expr
->location(), "expected struct field name");
11681 return Expression::make_error(location
);
11684 unsigned int index
;
11685 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11688 error_at(name_expr
->location(), "unknown field %qs in %qs",
11689 Gogo::message_name(name
).c_str(),
11690 (type
->named_type() != NULL
11691 ? type
->named_type()->message_name().c_str()
11692 : "unnamed struct"));
11693 return Expression::make_error(location
);
11695 if (vals
[index
] != NULL
)
11697 error_at(name_expr
->location(),
11698 "duplicate value for field %qs in %qs",
11699 Gogo::message_name(name
).c_str(),
11700 (type
->named_type() != NULL
11701 ? type
->named_type()->message_name().c_str()
11702 : "unnamed struct"));
11703 return Expression::make_error(location
);
11709 Expression_list
* list
= new Expression_list
;
11710 list
->reserve(field_count
);
11711 for (size_t i
= 0; i
< field_count
; ++i
)
11712 list
->push_back(vals
[i
]);
11714 return new Struct_construction_expression(type
, list
, location
);
11717 // Lower an array composite literal.
11720 Composite_literal_expression::lower_array(Type
* type
)
11722 source_location location
= this->location();
11723 if (this->vals_
== NULL
|| !this->has_keys_
)
11724 return this->make_array(type
, this->vals_
);
11726 std::vector
<Expression
*> vals
;
11727 vals
.reserve(this->vals_
->size());
11728 unsigned long index
= 0;
11729 Expression_list::const_iterator p
= this->vals_
->begin();
11730 while (p
!= this->vals_
->end())
11732 Expression
* index_expr
= *p
;
11735 gcc_assert(p
!= this->vals_
->end());
11736 Expression
* val
= *p
;
11740 if (index_expr
!= NULL
)
11745 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
11748 error_at(index_expr
->location(),
11749 "index expression is not integer constant");
11750 return Expression::make_error(location
);
11752 if (mpz_sgn(ival
) < 0)
11755 error_at(index_expr
->location(), "index expression is negative");
11756 return Expression::make_error(location
);
11758 index
= mpz_get_ui(ival
);
11759 if (mpz_cmp_ui(ival
, index
) != 0)
11762 error_at(index_expr
->location(), "index value overflow");
11763 return Expression::make_error(location
);
11768 if (index
== vals
.size())
11769 vals
.push_back(val
);
11772 if (index
> vals
.size())
11774 vals
.reserve(index
+ 32);
11775 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
11777 if (vals
[index
] != NULL
)
11779 error_at((index_expr
!= NULL
11780 ? index_expr
->location()
11781 : val
->location()),
11782 "duplicate value for index %lu",
11784 return Expression::make_error(location
);
11792 size_t size
= vals
.size();
11793 Expression_list
* list
= new Expression_list
;
11794 list
->reserve(size
);
11795 for (size_t i
= 0; i
< size
; ++i
)
11796 list
->push_back(vals
[i
]);
11798 return this->make_array(type
, list
);
11801 // Actually build the array composite literal. This handles
11805 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
11807 source_location location
= this->location();
11808 Array_type
* at
= type
->array_type();
11809 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
11811 size_t size
= vals
== NULL
? 0 : vals
->size();
11813 mpz_init_set_ui(vlen
, size
);
11814 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
11816 at
= Type::make_array_type(at
->element_type(), elen
);
11819 if (at
->length() != NULL
)
11820 return new Fixed_array_construction_expression(type
, vals
, location
);
11822 return new Open_array_construction_expression(type
, vals
, location
);
11825 // Lower a map composite literal.
11828 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
11831 source_location location
= this->location();
11832 if (this->vals_
!= NULL
)
11834 if (!this->has_keys_
)
11836 error_at(location
, "map composite literal must have keys");
11837 return Expression::make_error(location
);
11840 for (Expression_list::iterator p
= this->vals_
->begin();
11841 p
!= this->vals_
->end();
11847 error_at((*p
)->location(),
11848 "map composite literal must have keys for every value");
11849 return Expression::make_error(location
);
11851 // Make sure we have lowered the key; it may not have been
11852 // lowered in order to handle keys for struct composite
11853 // literals. Lower it now to get the right error message.
11854 if ((*p
)->unknown_expression() != NULL
)
11856 (*p
)->unknown_expression()->clear_is_composite_literal_key();
11857 gogo
->lower_expression(function
, &*p
);
11858 gcc_assert((*p
)->is_error_expression());
11859 return Expression::make_error(location
);
11864 return new Map_construction_expression(type
, this->vals_
, location
);
11867 // Make a composite literal expression.
11870 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
11871 Expression_list
* vals
,
11872 source_location location
)
11874 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
11878 // Return whether this expression is a composite literal.
11881 Expression::is_composite_literal() const
11883 switch (this->classification_
)
11885 case EXPRESSION_COMPOSITE_LITERAL
:
11886 case EXPRESSION_STRUCT_CONSTRUCTION
:
11887 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11888 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11889 case EXPRESSION_MAP_CONSTRUCTION
:
11896 // Return whether this expression is a composite literal which is not
11900 Expression::is_nonconstant_composite_literal() const
11902 switch (this->classification_
)
11904 case EXPRESSION_STRUCT_CONSTRUCTION
:
11906 const Struct_construction_expression
*psce
=
11907 static_cast<const Struct_construction_expression
*>(this);
11908 return !psce
->is_constant_struct();
11910 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11912 const Fixed_array_construction_expression
*pace
=
11913 static_cast<const Fixed_array_construction_expression
*>(this);
11914 return !pace
->is_constant_array();
11916 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11918 const Open_array_construction_expression
*pace
=
11919 static_cast<const Open_array_construction_expression
*>(this);
11920 return !pace
->is_constant_array();
11922 case EXPRESSION_MAP_CONSTRUCTION
:
11929 // Return true if this is a reference to a local variable.
11932 Expression::is_local_variable() const
11934 const Var_expression
* ve
= this->var_expression();
11937 const Named_object
* no
= ve
->named_object();
11938 return (no
->is_result_variable()
11939 || (no
->is_variable() && !no
->var_value()->is_global()));
11942 // Class Type_guard_expression.
11947 Type_guard_expression::do_traverse(Traverse
* traverse
)
11949 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
11950 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11951 return TRAVERSE_EXIT
;
11952 return TRAVERSE_CONTINUE
;
11955 // Check types of a type guard expression. The expression must have
11956 // an interface type, but the actual type conversion is checked at run
11960 Type_guard_expression::do_check_types(Gogo
*)
11962 // 6g permits using a type guard with unsafe.pointer; we are
11964 Type
* expr_type
= this->expr_
->type();
11965 if (expr_type
->is_unsafe_pointer_type())
11967 if (this->type_
->points_to() == NULL
11968 && (this->type_
->integer_type() == NULL
11969 || (this->type_
->forwarded()
11970 != Type::lookup_integer_type("uintptr"))))
11971 this->report_error(_("invalid unsafe.Pointer conversion"));
11973 else if (this->type_
->is_unsafe_pointer_type())
11975 if (expr_type
->points_to() == NULL
11976 && (expr_type
->integer_type() == NULL
11977 || (expr_type
->forwarded()
11978 != Type::lookup_integer_type("uintptr"))))
11979 this->report_error(_("invalid unsafe.Pointer conversion"));
11981 else if (expr_type
->interface_type() == NULL
)
11983 if (!expr_type
->is_error_type() && !this->type_
->is_error_type())
11984 this->report_error(_("type assertion only valid for interface types"));
11985 this->set_is_error();
11987 else if (this->type_
->interface_type() == NULL
)
11989 std::string reason
;
11990 if (!expr_type
->interface_type()->implements_interface(this->type_
,
11993 if (!this->type_
->is_error_type())
11995 if (reason
.empty())
11996 this->report_error(_("impossible type assertion: "
11997 "type does not implement interface"));
11999 error_at(this->location(),
12000 ("impossible type assertion: "
12001 "type does not implement interface (%s)"),
12004 this->set_is_error();
12009 // Return a tree for a type guard expression.
12012 Type_guard_expression::do_get_tree(Translate_context
* context
)
12014 Gogo
* gogo
= context
->gogo();
12015 tree expr_tree
= this->expr_
->get_tree(context
);
12016 if (expr_tree
== error_mark_node
)
12017 return error_mark_node
;
12018 Type
* expr_type
= this->expr_
->type();
12019 if ((this->type_
->is_unsafe_pointer_type()
12020 && (expr_type
->points_to() != NULL
12021 || expr_type
->integer_type() != NULL
))
12022 || (expr_type
->is_unsafe_pointer_type()
12023 && this->type_
->points_to() != NULL
))
12024 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
12025 else if (expr_type
->is_unsafe_pointer_type()
12026 && this->type_
->integer_type() != NULL
)
12027 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
12028 else if (this->type_
->interface_type() != NULL
)
12029 return Expression::convert_interface_to_interface(context
, this->type_
,
12030 this->expr_
->type(),
12034 return Expression::convert_for_assignment(context
, this->type_
,
12035 this->expr_
->type(), expr_tree
,
12039 // Make a type guard expression.
12042 Expression::make_type_guard(Expression
* expr
, Type
* type
,
12043 source_location location
)
12045 return new Type_guard_expression(expr
, type
, location
);
12048 // Class Heap_composite_expression.
12050 // When you take the address of a composite literal, it is allocated
12051 // on the heap. This class implements that.
12053 class Heap_composite_expression
: public Expression
12056 Heap_composite_expression(Expression
* expr
, source_location location
)
12057 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
12063 do_traverse(Traverse
* traverse
)
12064 { return Expression::traverse(&this->expr_
, traverse
); }
12068 { return Type::make_pointer_type(this->expr_
->type()); }
12071 do_determine_type(const Type_context
*)
12072 { this->expr_
->determine_type_no_context(); }
12077 return Expression::make_heap_composite(this->expr_
->copy(),
12082 do_get_tree(Translate_context
*);
12084 // We only export global objects, and the parser does not generate
12085 // this in global scope.
12087 do_export(Export
*) const
12088 { gcc_unreachable(); }
12091 // The composite literal which is being put on the heap.
12095 // Return a tree which allocates a composite literal on the heap.
12098 Heap_composite_expression::do_get_tree(Translate_context
* context
)
12100 tree expr_tree
= this->expr_
->get_tree(context
);
12101 if (expr_tree
== error_mark_node
)
12102 return error_mark_node
;
12103 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
12104 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
12105 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
12106 expr_size
, this->location());
12107 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
12108 space
= save_expr(space
);
12109 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
12110 TREE_THIS_NOTRAP(ref
) = 1;
12111 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
12112 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
12114 SET_EXPR_LOCATION(ret
, this->location());
12118 // Allocate a composite literal on the heap.
12121 Expression::make_heap_composite(Expression
* expr
, source_location location
)
12123 return new Heap_composite_expression(expr
, location
);
12126 // Class Receive_expression.
12128 // Return the type of a receive expression.
12131 Receive_expression::do_type()
12133 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12134 if (channel_type
== NULL
)
12135 return Type::make_error_type();
12136 return channel_type
->element_type();
12139 // Check types for a receive expression.
12142 Receive_expression::do_check_types(Gogo
*)
12144 Type
* type
= this->channel_
->type();
12145 if (type
->is_error_type())
12147 this->set_is_error();
12150 if (type
->channel_type() == NULL
)
12152 this->report_error(_("expected channel"));
12155 if (!type
->channel_type()->may_receive())
12157 this->report_error(_("invalid receive on send-only channel"));
12162 // Get a tree for a receive expression.
12165 Receive_expression::do_get_tree(Translate_context
* context
)
12167 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12168 gcc_assert(channel_type
!= NULL
);
12169 Type
* element_type
= channel_type
->element_type();
12170 tree element_type_tree
= element_type
->get_tree(context
->gogo());
12172 tree channel
= this->channel_
->get_tree(context
);
12173 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
12174 return error_mark_node
;
12176 return Gogo::receive_from_channel(element_type_tree
, channel
,
12177 this->for_select_
, this->location());
12180 // Make a receive expression.
12182 Receive_expression
*
12183 Expression::make_receive(Expression
* channel
, source_location location
)
12185 return new Receive_expression(channel
, location
);
12188 // Class Send_expression.
12193 Send_expression::do_traverse(Traverse
* traverse
)
12195 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
12196 return TRAVERSE_EXIT
;
12197 return Expression::traverse(&this->val_
, traverse
);
12203 Send_expression::do_type()
12205 return Type::lookup_bool_type();
12211 Send_expression::do_determine_type(const Type_context
*)
12213 this->channel_
->determine_type_no_context();
12215 Type
* type
= this->channel_
->type();
12216 Type_context subcontext
;
12217 if (type
->channel_type() != NULL
)
12218 subcontext
.type
= type
->channel_type()->element_type();
12219 this->val_
->determine_type(&subcontext
);
12225 Send_expression::do_check_types(Gogo
*)
12227 Type
* type
= this->channel_
->type();
12228 if (type
->is_error_type())
12230 this->set_is_error();
12233 Channel_type
* channel_type
= type
->channel_type();
12234 if (channel_type
== NULL
)
12236 error_at(this->location(), "left operand of %<<-%> must be channel");
12237 this->set_is_error();
12240 Type
* element_type
= channel_type
->element_type();
12241 if (element_type
!= NULL
12242 && !Type::are_assignable(element_type
, this->val_
->type(), NULL
))
12244 this->report_error(_("incompatible types in send"));
12247 if (!channel_type
->may_send())
12249 this->report_error(_("invalid send on receive-only channel"));
12254 // Get a tree for a send expression.
12257 Send_expression::do_get_tree(Translate_context
* context
)
12259 tree channel
= this->channel_
->get_tree(context
);
12260 tree val
= this->val_
->get_tree(context
);
12261 if (channel
== error_mark_node
|| val
== error_mark_node
)
12262 return error_mark_node
;
12263 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12264 val
= Expression::convert_for_assignment(context
,
12265 channel_type
->element_type(),
12266 this->val_
->type(),
12269 return Gogo::send_on_channel(channel
, val
, this->is_value_discarded_
,
12270 this->for_select_
, this->location());
12273 // Make a send expression
12276 Expression::make_send(Expression
* channel
, Expression
* val
,
12277 source_location location
)
12279 return new Send_expression(channel
, val
, location
);
12282 // An expression which evaluates to a pointer to the type descriptor
12285 class Type_descriptor_expression
: public Expression
12288 Type_descriptor_expression(Type
* type
, source_location location
)
12289 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
12296 { return Type::make_type_descriptor_ptr_type(); }
12299 do_determine_type(const Type_context
*)
12307 do_get_tree(Translate_context
* context
)
12308 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
12311 // The type for which this is the descriptor.
12315 // Make a type descriptor expression.
12318 Expression::make_type_descriptor(Type
* type
, source_location location
)
12320 return new Type_descriptor_expression(type
, location
);
12323 // An expression which evaluates to some characteristic of a type.
12324 // This is only used to initialize fields of a type descriptor. Using
12325 // a new expression class is slightly inefficient but gives us a good
12326 // separation between the frontend and the middle-end with regard to
12327 // how types are laid out.
12329 class Type_info_expression
: public Expression
12332 Type_info_expression(Type
* type
, Type_info type_info
)
12333 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
12334 type_(type
), type_info_(type_info
)
12342 do_determine_type(const Type_context
*)
12350 do_get_tree(Translate_context
* context
);
12353 // The type for which we are getting information.
12355 // What information we want.
12356 Type_info type_info_
;
12359 // The type is chosen to match what the type descriptor struct
12363 Type_info_expression::do_type()
12365 switch (this->type_info_
)
12367 case TYPE_INFO_SIZE
:
12368 return Type::lookup_integer_type("uintptr");
12369 case TYPE_INFO_ALIGNMENT
:
12370 case TYPE_INFO_FIELD_ALIGNMENT
:
12371 return Type::lookup_integer_type("uint8");
12377 // Return type information in GENERIC.
12380 Type_info_expression::do_get_tree(Translate_context
* context
)
12382 tree type_tree
= this->type_
->get_tree(context
->gogo());
12383 if (type_tree
== error_mark_node
)
12384 return error_mark_node
;
12386 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12387 gcc_assert(val_type_tree
!= error_mark_node
);
12389 if (this->type_info_
== TYPE_INFO_SIZE
)
12390 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12391 TYPE_SIZE_UNIT(type_tree
));
12395 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12396 val
= go_type_alignment(type_tree
);
12398 val
= go_field_alignment(type_tree
);
12399 return build_int_cstu(val_type_tree
, val
);
12403 // Make a type info expression.
12406 Expression::make_type_info(Type
* type
, Type_info type_info
)
12408 return new Type_info_expression(type
, type_info
);
12411 // An expression which evaluates to the offset of a field within a
12412 // struct. This, like Type_info_expression, q.v., is only used to
12413 // initialize fields of a type descriptor.
12415 class Struct_field_offset_expression
: public Expression
12418 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12419 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12420 type_(type
), field_(field
)
12426 { return Type::lookup_integer_type("uintptr"); }
12429 do_determine_type(const Type_context
*)
12437 do_get_tree(Translate_context
* context
);
12440 // The type of the struct.
12441 Struct_type
* type_
;
12443 const Struct_field
* field_
;
12446 // Return a struct field offset in GENERIC.
12449 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12451 tree type_tree
= this->type_
->get_tree(context
->gogo());
12452 if (type_tree
== error_mark_node
)
12453 return error_mark_node
;
12455 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12456 gcc_assert(val_type_tree
!= error_mark_node
);
12458 const Struct_field_list
* fields
= this->type_
->fields();
12459 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12460 Struct_field_list::const_iterator p
;
12461 for (p
= fields
->begin();
12462 p
!= fields
->end();
12463 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12465 gcc_assert(struct_field_tree
!= NULL_TREE
);
12466 if (&*p
== this->field_
)
12469 gcc_assert(&*p
== this->field_
);
12471 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12472 byte_position(struct_field_tree
));
12475 // Make an expression for a struct field offset.
12478 Expression::make_struct_field_offset(Struct_type
* type
,
12479 const Struct_field
* field
)
12481 return new Struct_field_offset_expression(type
, field
);
12484 // An expression which evaluates to the address of an unnamed label.
12486 class Label_addr_expression
: public Expression
12489 Label_addr_expression(Label
* label
, source_location location
)
12490 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12497 { return Type::make_pointer_type(Type::make_void_type()); }
12500 do_determine_type(const Type_context
*)
12505 { return new Label_addr_expression(this->label_
, this->location()); }
12508 do_get_tree(Translate_context
*)
12509 { return this->label_
->get_addr(this->location()); }
12512 // The label whose address we are taking.
12516 // Make an expression for the address of an unnamed label.
12519 Expression::make_label_addr(Label
* label
, source_location location
)
12521 return new Label_addr_expression(label
, location
);
12524 // Import an expression. This comes at the end in order to see the
12525 // various class definitions.
12528 Expression::import_expression(Import
* imp
)
12530 int c
= imp
->peek_char();
12531 if (imp
->match_c_string("- ")
12532 || imp
->match_c_string("! ")
12533 || imp
->match_c_string("^ "))
12534 return Unary_expression::do_import(imp
);
12536 return Binary_expression::do_import(imp
);
12537 else if (imp
->match_c_string("true")
12538 || imp
->match_c_string("false"))
12539 return Boolean_expression::do_import(imp
);
12541 return String_expression::do_import(imp
);
12542 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12544 // This handles integers, floats and complex constants.
12545 return Integer_expression::do_import(imp
);
12547 else if (imp
->match_c_string("nil"))
12548 return Nil_expression::do_import(imp
);
12549 else if (imp
->match_c_string("convert"))
12550 return Type_conversion_expression::do_import(imp
);
12553 error_at(imp
->location(), "import error: expected expression");
12554 return Expression::make_error(imp
->location());
12558 // Class Expression_list.
12560 // Traverse the list.
12563 Expression_list::traverse(Traverse
* traverse
)
12565 for (Expression_list::iterator p
= this->begin();
12571 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12572 return TRAVERSE_EXIT
;
12575 return TRAVERSE_CONTINUE
;
12581 Expression_list::copy()
12583 Expression_list
* ret
= new Expression_list();
12584 for (Expression_list::iterator p
= this->begin();
12589 ret
->push_back(NULL
);
12591 ret
->push_back((*p
)->copy());
12596 // Return whether an expression list has an error expression.
12599 Expression_list::contains_error() const
12601 for (Expression_list::const_iterator p
= this->begin();
12604 if (*p
!= NULL
&& (*p
)->is_error_expression())