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 Var_expression.
899 // Lower a variable expression. Here we just make sure that the
900 // initialization expression of the variable has been lowered. This
901 // ensures that we will be able to determine the type of the variable
905 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
907 if (this->variable_
->is_variable())
909 Variable
* var
= this->variable_
->var_value();
910 // This is either a local variable or a global variable. A
911 // reference to a variable which is local to an enclosing
912 // function will be a reference to a field in a closure.
913 if (var
->is_global())
915 var
->lower_init_expression(gogo
, function
);
920 // Return the name of the variable.
923 Var_expression::name() const
925 return this->variable_
->name();
928 // Return the type of a reference to a variable.
931 Var_expression::do_type()
933 if (this->variable_
->is_variable())
934 return this->variable_
->var_value()->type();
935 else if (this->variable_
->is_result_variable())
936 return this->variable_
->result_var_value()->type();
941 // Something takes the address of this variable. This means that we
942 // may want to move the variable onto the heap.
945 Var_expression::do_address_taken(bool escapes
)
949 else if (this->variable_
->is_variable())
950 this->variable_
->var_value()->set_address_taken();
951 else if (this->variable_
->is_result_variable())
952 this->variable_
->result_var_value()->set_address_taken();
957 // Get the tree for a reference to a variable.
960 Var_expression::do_get_tree(Translate_context
* context
)
962 return this->variable_
->get_tree(context
->gogo(), context
->function());
965 // Make a reference to a variable in an expression.
968 Expression::make_var_reference(Named_object
* var
, source_location location
)
971 return Expression::make_sink(location
);
973 // FIXME: Creating a new object for each reference to a variable is
975 return new Var_expression(var
, location
);
978 // Class Temporary_reference_expression.
983 Temporary_reference_expression::do_type()
985 return this->statement_
->type();
988 // Called if something takes the address of this temporary variable.
989 // We never have to move temporary variables to the heap, but we do
990 // need to know that they must live in the stack rather than in a
994 Temporary_reference_expression::do_address_taken(bool)
996 this->statement_
->set_is_address_taken();
999 // Get a tree referring to the variable.
1002 Temporary_reference_expression::do_get_tree(Translate_context
*)
1004 return this->statement_
->get_decl();
1007 // Make a reference to a temporary variable.
1010 Expression::make_temporary_reference(Temporary_statement
* statement
,
1011 source_location location
)
1013 return new Temporary_reference_expression(statement
, location
);
1016 // A sink expression--a use of the blank identifier _.
1018 class Sink_expression
: public Expression
1021 Sink_expression(source_location location
)
1022 : Expression(EXPRESSION_SINK
, location
),
1023 type_(NULL
), var_(NULL_TREE
)
1028 do_discarding_value()
1035 do_determine_type(const Type_context
*);
1039 { return new Sink_expression(this->location()); }
1042 do_get_tree(Translate_context
*);
1045 // The type of this sink variable.
1047 // The temporary variable we generate.
1051 // Return the type of a sink expression.
1054 Sink_expression::do_type()
1056 if (this->type_
== NULL
)
1057 return Type::make_sink_type();
1061 // Determine the type of a sink expression.
1064 Sink_expression::do_determine_type(const Type_context
* context
)
1066 if (context
->type
!= NULL
)
1067 this->type_
= context
->type
;
1070 // Return a temporary variable for a sink expression. This will
1071 // presumably be a write-only variable which the middle-end will drop.
1074 Sink_expression::do_get_tree(Translate_context
* context
)
1076 if (this->var_
== NULL_TREE
)
1078 gcc_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
1079 this->var_
= create_tmp_var(this->type_
->get_tree(context
->gogo()),
1085 // Make a sink expression.
1088 Expression::make_sink(source_location location
)
1090 return new Sink_expression(location
);
1093 // Class Func_expression.
1095 // FIXME: Can a function expression appear in a constant expression?
1096 // The value is unchanging. Initializing a constant to the address of
1097 // a function seems like it could work, though there might be little
1100 // Return the name of the function.
1103 Func_expression::name() const
1105 return this->function_
->name();
1111 Func_expression::do_traverse(Traverse
* traverse
)
1113 return (this->closure_
== NULL
1115 : Expression::traverse(&this->closure_
, traverse
));
1118 // Return the type of a function expression.
1121 Func_expression::do_type()
1123 if (this->function_
->is_function())
1124 return this->function_
->func_value()->type();
1125 else if (this->function_
->is_function_declaration())
1126 return this->function_
->func_declaration_value()->type();
1131 // Get the tree for a function expression without evaluating the
1135 Func_expression::get_tree_without_closure(Gogo
* gogo
)
1137 Function_type
* fntype
;
1138 if (this->function_
->is_function())
1139 fntype
= this->function_
->func_value()->type();
1140 else if (this->function_
->is_function_declaration())
1141 fntype
= this->function_
->func_declaration_value()->type();
1145 // Builtin functions are handled specially by Call_expression. We
1146 // can't take their address.
1147 if (fntype
->is_builtin())
1149 error_at(this->location(), "invalid use of special builtin function %qs",
1150 this->function_
->name().c_str());
1151 return error_mark_node
;
1154 Named_object
* no
= this->function_
;
1156 tree id
= no
->get_id(gogo
);
1157 if (id
== error_mark_node
)
1158 return error_mark_node
;
1161 if (no
->is_function())
1162 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
, id
);
1163 else if (no
->is_function_declaration())
1164 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
, id
);
1168 if (fndecl
== error_mark_node
)
1169 return error_mark_node
;
1171 return build_fold_addr_expr_loc(this->location(), fndecl
);
1174 // Get the tree for a function expression. This is used when we take
1175 // the address of a function rather than simply calling it. If the
1176 // function has a closure, we must use a trampoline.
1179 Func_expression::do_get_tree(Translate_context
* context
)
1181 Gogo
* gogo
= context
->gogo();
1183 tree fnaddr
= this->get_tree_without_closure(gogo
);
1184 if (fnaddr
== error_mark_node
)
1185 return error_mark_node
;
1187 gcc_assert(TREE_CODE(fnaddr
) == ADDR_EXPR
1188 && TREE_CODE(TREE_OPERAND(fnaddr
, 0)) == FUNCTION_DECL
);
1189 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr
, 0)) = 1;
1191 // For a normal non-nested function call, that is all we have to do.
1192 if (!this->function_
->is_function()
1193 || this->function_
->func_value()->enclosing() == NULL
)
1195 gcc_assert(this->closure_
== NULL
);
1199 // For a nested function call, we have to always allocate a
1200 // trampoline. If we don't always allocate, then closures will not
1201 // be reliably distinct.
1202 Expression
* closure
= this->closure_
;
1204 if (closure
== NULL
)
1205 closure_tree
= null_pointer_node
;
1208 // Get the value of the closure. This will be a pointer to
1209 // space allocated on the heap.
1210 closure_tree
= closure
->get_tree(context
);
1211 if (closure_tree
== error_mark_node
)
1212 return error_mark_node
;
1213 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree
)));
1216 // Now we need to build some code on the heap. This code will load
1217 // the static chain pointer with the closure and then jump to the
1218 // body of the function. The normal gcc approach is to build the
1219 // code on the stack. Unfortunately we can not do that, as Go
1220 // permits us to return the function pointer.
1222 return gogo
->make_trampoline(fnaddr
, closure_tree
, this->location());
1225 // Make a reference to a function in an expression.
1228 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1229 source_location location
)
1231 return new Func_expression(function
, closure
, location
);
1234 // Class Unknown_expression.
1236 // Return the name of an unknown expression.
1239 Unknown_expression::name() const
1241 return this->named_object_
->name();
1244 // Lower a reference to an unknown name.
1247 Unknown_expression::do_lower(Gogo
*, Named_object
*, int)
1249 source_location location
= this->location();
1250 Named_object
* no
= this->named_object_
;
1252 if (!no
->is_unknown())
1256 real
= no
->unknown_value()->real_named_object();
1259 if (this->is_composite_literal_key_
)
1261 error_at(location
, "reference to undefined name %qs",
1262 this->named_object_
->message_name().c_str());
1263 return Expression::make_error(location
);
1266 switch (real
->classification())
1268 case Named_object::NAMED_OBJECT_CONST
:
1269 return Expression::make_const_reference(real
, location
);
1270 case Named_object::NAMED_OBJECT_TYPE
:
1271 return Expression::make_type(real
->type_value(), location
);
1272 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1273 if (this->is_composite_literal_key_
)
1275 error_at(location
, "reference to undefined type %qs",
1276 real
->message_name().c_str());
1277 return Expression::make_error(location
);
1278 case Named_object::NAMED_OBJECT_VAR
:
1279 return Expression::make_var_reference(real
, location
);
1280 case Named_object::NAMED_OBJECT_FUNC
:
1281 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1282 return Expression::make_func_reference(real
, NULL
, location
);
1283 case Named_object::NAMED_OBJECT_PACKAGE
:
1284 if (this->is_composite_literal_key_
)
1286 error_at(location
, "unexpected reference to package");
1287 return Expression::make_error(location
);
1293 // Make a reference to an unknown name.
1296 Expression::make_unknown_reference(Named_object
* no
, source_location location
)
1298 gcc_assert(no
->resolve()->is_unknown());
1299 return new Unknown_expression(no
, location
);
1302 // A boolean expression.
1304 class Boolean_expression
: public Expression
1307 Boolean_expression(bool val
, source_location location
)
1308 : Expression(EXPRESSION_BOOLEAN
, location
),
1309 val_(val
), type_(NULL
)
1317 do_is_constant() const
1324 do_determine_type(const Type_context
*);
1331 do_get_tree(Translate_context
*)
1332 { return this->val_
? boolean_true_node
: boolean_false_node
; }
1335 do_export(Export
* exp
) const
1336 { exp
->write_c_string(this->val_
? "true" : "false"); }
1341 // The type as determined by context.
1348 Boolean_expression::do_type()
1350 if (this->type_
== NULL
)
1351 this->type_
= Type::make_boolean_type();
1355 // Set the type from the context.
1358 Boolean_expression::do_determine_type(const Type_context
* context
)
1360 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1362 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1363 this->type_
= context
->type
;
1364 else if (!context
->may_be_abstract
)
1365 this->type_
= Type::lookup_bool_type();
1368 // Import a boolean constant.
1371 Boolean_expression::do_import(Import
* imp
)
1373 if (imp
->peek_char() == 't')
1375 imp
->require_c_string("true");
1376 return Expression::make_boolean(true, imp
->location());
1380 imp
->require_c_string("false");
1381 return Expression::make_boolean(false, imp
->location());
1385 // Make a boolean expression.
1388 Expression::make_boolean(bool val
, source_location location
)
1390 return new Boolean_expression(val
, location
);
1393 // Class String_expression.
1398 String_expression::do_type()
1400 if (this->type_
== NULL
)
1401 this->type_
= Type::make_string_type();
1405 // Set the type from the context.
1408 String_expression::do_determine_type(const Type_context
* context
)
1410 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1412 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1413 this->type_
= context
->type
;
1414 else if (!context
->may_be_abstract
)
1415 this->type_
= Type::lookup_string_type();
1418 // Build a string constant.
1421 String_expression::do_get_tree(Translate_context
* context
)
1423 return context
->gogo()->go_string_constant_tree(this->val_
);
1426 // Export a string expression.
1429 String_expression::do_export(Export
* exp
) const
1432 s
.reserve(this->val_
.length() * 4 + 2);
1434 for (std::string::const_iterator p
= this->val_
.begin();
1435 p
!= this->val_
.end();
1438 if (*p
== '\\' || *p
== '"')
1443 else if (*p
>= 0x20 && *p
< 0x7f)
1445 else if (*p
== '\n')
1447 else if (*p
== '\t')
1452 unsigned char c
= *p
;
1453 unsigned int dig
= c
>> 4;
1454 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1456 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1460 exp
->write_string(s
);
1463 // Import a string expression.
1466 String_expression::do_import(Import
* imp
)
1468 imp
->require_c_string("\"");
1472 int c
= imp
->get_char();
1473 if (c
== '"' || c
== -1)
1476 val
+= static_cast<char>(c
);
1479 c
= imp
->get_char();
1480 if (c
== '\\' || c
== '"')
1481 val
+= static_cast<char>(c
);
1488 c
= imp
->get_char();
1489 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1490 c
= imp
->get_char();
1491 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1492 char v
= (vh
<< 4) | vl
;
1497 error_at(imp
->location(), "bad string constant");
1498 return Expression::make_error(imp
->location());
1502 return Expression::make_string(val
, imp
->location());
1505 // Make a string expression.
1508 Expression::make_string(const std::string
& val
, source_location location
)
1510 return new String_expression(val
, location
);
1513 // Make an integer expression.
1515 class Integer_expression
: public Expression
1518 Integer_expression(const mpz_t
* val
, Type
* type
, source_location location
)
1519 : Expression(EXPRESSION_INTEGER
, location
),
1521 { mpz_init_set(this->val_
, *val
); }
1526 // Return whether VAL fits in the type.
1528 check_constant(mpz_t val
, Type
*, source_location
);
1530 // Write VAL to export data.
1532 export_integer(Export
* exp
, const mpz_t val
);
1536 do_is_constant() const
1540 do_integer_constant_value(bool, mpz_t val
, Type
** ptype
) const;
1546 do_determine_type(const Type_context
* context
);
1549 do_check_types(Gogo
*);
1552 do_get_tree(Translate_context
*);
1556 { return Expression::make_integer(&this->val_
, this->type_
,
1557 this->location()); }
1560 do_export(Export
*) const;
1563 // The integer value.
1569 // Return an integer constant value.
1572 Integer_expression::do_integer_constant_value(bool, mpz_t val
,
1575 if (this->type_
!= NULL
)
1576 *ptype
= this->type_
;
1577 mpz_set(val
, this->val_
);
1581 // Return the current type. If we haven't set the type yet, we return
1582 // an abstract integer type.
1585 Integer_expression::do_type()
1587 if (this->type_
== NULL
)
1588 this->type_
= Type::make_abstract_integer_type();
1592 // Set the type of the integer value. Here we may switch from an
1593 // abstract type to a real type.
1596 Integer_expression::do_determine_type(const Type_context
* context
)
1598 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1600 else if (context
->type
!= NULL
1601 && (context
->type
->integer_type() != NULL
1602 || context
->type
->float_type() != NULL
1603 || context
->type
->complex_type() != NULL
))
1604 this->type_
= context
->type
;
1605 else if (!context
->may_be_abstract
)
1606 this->type_
= Type::lookup_integer_type("int");
1609 // Return true if the integer VAL fits in the range of the type TYPE.
1610 // Otherwise give an error and return false. TYPE may be NULL.
1613 Integer_expression::check_constant(mpz_t val
, Type
* type
,
1614 source_location location
)
1618 Integer_type
* itype
= type
->integer_type();
1619 if (itype
== NULL
|| itype
->is_abstract())
1622 int bits
= mpz_sizeinbase(val
, 2);
1624 if (itype
->is_unsigned())
1626 // For an unsigned type we can only accept a nonnegative number,
1627 // and we must be able to represent at least BITS.
1628 if (mpz_sgn(val
) >= 0
1629 && bits
<= itype
->bits())
1634 // For a signed type we need an extra bit to indicate the sign.
1635 // We have to handle the most negative integer specially.
1636 if (bits
+ 1 <= itype
->bits()
1637 || (bits
<= itype
->bits()
1639 && (mpz_scan1(val
, 0)
1640 == static_cast<unsigned long>(itype
->bits() - 1))
1641 && mpz_scan0(val
, itype
->bits()) == ULONG_MAX
))
1645 error_at(location
, "integer constant overflow");
1649 // Check the type of an integer constant.
1652 Integer_expression::do_check_types(Gogo
*)
1654 if (this->type_
== NULL
)
1656 if (!Integer_expression::check_constant(this->val_
, this->type_
,
1658 this->set_is_error();
1661 // Get a tree for an integer constant.
1664 Integer_expression::do_get_tree(Translate_context
* context
)
1666 Gogo
* gogo
= context
->gogo();
1668 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1669 type
= this->type_
->get_tree(gogo
);
1670 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
1672 // We are converting to an abstract floating point type.
1673 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
1675 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
1677 // We are converting to an abstract complex type.
1678 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
1682 // If we still have an abstract type here, then this is being
1683 // used in a constant expression which didn't get reduced for
1684 // some reason. Use a type which will fit the value. We use <,
1685 // not <=, because we need an extra bit for the sign bit.
1686 int bits
= mpz_sizeinbase(this->val_
, 2);
1687 if (bits
< INT_TYPE_SIZE
)
1688 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
1690 type
= Type::lookup_integer_type("int64")->get_tree(gogo
);
1692 type
= long_long_integer_type_node
;
1694 return Expression::integer_constant_tree(this->val_
, type
);
1697 // Write VAL to export data.
1700 Integer_expression::export_integer(Export
* exp
, const mpz_t val
)
1702 char* s
= mpz_get_str(NULL
, 10, val
);
1703 exp
->write_c_string(s
);
1707 // Export an integer in a constant expression.
1710 Integer_expression::do_export(Export
* exp
) const
1712 Integer_expression::export_integer(exp
, this->val_
);
1713 // A trailing space lets us reliably identify the end of the number.
1714 exp
->write_c_string(" ");
1717 // Import an integer, floating point, or complex value. This handles
1718 // all these types because they all start with digits.
1721 Integer_expression::do_import(Import
* imp
)
1723 std::string num
= imp
->read_identifier();
1724 imp
->require_c_string(" ");
1725 if (!num
.empty() && num
[num
.length() - 1] == 'i')
1728 size_t plus_pos
= num
.find('+', 1);
1729 size_t minus_pos
= num
.find('-', 1);
1731 if (plus_pos
== std::string::npos
)
1733 else if (minus_pos
== std::string::npos
)
1737 error_at(imp
->location(), "bad number in import data: %qs",
1739 return Expression::make_error(imp
->location());
1741 if (pos
== std::string::npos
)
1742 mpfr_set_ui(real
, 0, GMP_RNDN
);
1745 std::string real_str
= num
.substr(0, pos
);
1746 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
1748 error_at(imp
->location(), "bad number in import data: %qs",
1750 return Expression::make_error(imp
->location());
1754 std::string imag_str
;
1755 if (pos
== std::string::npos
)
1758 imag_str
= num
.substr(pos
);
1759 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
1761 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
1763 error_at(imp
->location(), "bad number in import data: %qs",
1765 return Expression::make_error(imp
->location());
1767 Expression
* ret
= Expression::make_complex(&real
, &imag
, NULL
,
1773 else if (num
.find('.') == std::string::npos
1774 && num
.find('E') == std::string::npos
)
1777 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
1779 error_at(imp
->location(), "bad number in import data: %qs",
1781 return Expression::make_error(imp
->location());
1783 Expression
* ret
= Expression::make_integer(&val
, NULL
, imp
->location());
1790 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
1792 error_at(imp
->location(), "bad number in import data: %qs",
1794 return Expression::make_error(imp
->location());
1796 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
1802 // Build a new integer value.
1805 Expression::make_integer(const mpz_t
* val
, Type
* type
,
1806 source_location location
)
1808 return new Integer_expression(val
, type
, location
);
1813 class Float_expression
: public Expression
1816 Float_expression(const mpfr_t
* val
, Type
* type
, source_location location
)
1817 : Expression(EXPRESSION_FLOAT
, location
),
1820 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
1823 // Constrain VAL to fit into TYPE.
1825 constrain_float(mpfr_t val
, Type
* type
);
1827 // Return whether VAL fits in the type.
1829 check_constant(mpfr_t val
, Type
*, source_location
);
1831 // Write VAL to export data.
1833 export_float(Export
* exp
, const mpfr_t val
);
1837 do_is_constant() const
1841 do_float_constant_value(mpfr_t val
, Type
**) const;
1847 do_determine_type(const Type_context
*);
1850 do_check_types(Gogo
*);
1854 { return Expression::make_float(&this->val_
, this->type_
,
1855 this->location()); }
1858 do_get_tree(Translate_context
*);
1861 do_export(Export
*) const;
1864 // The floating point value.
1870 // Constrain VAL to fit into TYPE.
1873 Float_expression::constrain_float(mpfr_t val
, Type
* type
)
1875 Float_type
* ftype
= type
->float_type();
1876 if (ftype
!= NULL
&& !ftype
->is_abstract())
1878 tree type_tree
= ftype
->type_tree();
1879 REAL_VALUE_TYPE rvt
;
1880 real_from_mpfr(&rvt
, val
, type_tree
, GMP_RNDN
);
1881 real_convert(&rvt
, TYPE_MODE(type_tree
), &rvt
);
1882 mpfr_from_real(val
, &rvt
, GMP_RNDN
);
1886 // Return a floating point constant value.
1889 Float_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
1891 if (this->type_
!= NULL
)
1892 *ptype
= this->type_
;
1893 mpfr_set(val
, this->val_
, GMP_RNDN
);
1897 // Return the current type. If we haven't set the type yet, we return
1898 // an abstract float type.
1901 Float_expression::do_type()
1903 if (this->type_
== NULL
)
1904 this->type_
= Type::make_abstract_float_type();
1908 // Set the type of the float value. Here we may switch from an
1909 // abstract type to a real type.
1912 Float_expression::do_determine_type(const Type_context
* context
)
1914 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1916 else if (context
->type
!= NULL
1917 && (context
->type
->integer_type() != NULL
1918 || context
->type
->float_type() != NULL
1919 || context
->type
->complex_type() != NULL
))
1920 this->type_
= context
->type
;
1921 else if (!context
->may_be_abstract
)
1922 this->type_
= Type::lookup_float_type("float");
1925 // Return true if the floating point value VAL fits in the range of
1926 // the type TYPE. Otherwise give an error and return false. TYPE may
1930 Float_expression::check_constant(mpfr_t val
, Type
* type
,
1931 source_location location
)
1935 Float_type
* ftype
= type
->float_type();
1936 if (ftype
== NULL
|| ftype
->is_abstract())
1939 // A NaN or Infinity always fits in the range of the type.
1940 if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
1943 mp_exp_t exp
= mpfr_get_exp(val
);
1945 switch (ftype
->bits())
1958 error_at(location
, "floating point constant overflow");
1964 // Check the type of a float value.
1967 Float_expression::do_check_types(Gogo
*)
1969 if (this->type_
== NULL
)
1972 if (!Float_expression::check_constant(this->val_
, this->type_
,
1974 this->set_is_error();
1976 Integer_type
* integer_type
= this->type_
->integer_type();
1977 if (integer_type
!= NULL
)
1979 if (!mpfr_integer_p(this->val_
))
1980 this->report_error(_("floating point constant truncated to integer"));
1983 gcc_assert(!integer_type
->is_abstract());
1986 mpfr_get_z(ival
, this->val_
, GMP_RNDN
);
1987 Integer_expression::check_constant(ival
, integer_type
,
1994 // Get a tree for a float constant.
1997 Float_expression::do_get_tree(Translate_context
* context
)
1999 Gogo
* gogo
= context
->gogo();
2001 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2002 type
= this->type_
->get_tree(gogo
);
2003 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2005 // We have an abstract integer type. We just hope for the best.
2006 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
2010 // If we still have an abstract type here, then this is being
2011 // used in a constant expression which didn't get reduced. We
2012 // just use float64 and hope for the best.
2013 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
2015 return Expression::float_constant_tree(this->val_
, type
);
2018 // Write a floating point number to export data.
2021 Float_expression::export_float(Export
*exp
, const mpfr_t val
)
2024 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2026 exp
->write_c_string("-");
2027 exp
->write_c_string("0.");
2028 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2031 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2032 exp
->write_c_string(buf
);
2035 // Export a floating point number in a constant expression.
2038 Float_expression::do_export(Export
* exp
) const
2040 Float_expression::export_float(exp
, this->val_
);
2041 // A trailing space lets us reliably identify the end of the number.
2042 exp
->write_c_string(" ");
2045 // Make a float expression.
2048 Expression::make_float(const mpfr_t
* val
, Type
* type
, source_location location
)
2050 return new Float_expression(val
, type
, location
);
2055 class Complex_expression
: public Expression
2058 Complex_expression(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2059 source_location location
)
2060 : Expression(EXPRESSION_COMPLEX
, location
),
2063 mpfr_init_set(this->real_
, *real
, GMP_RNDN
);
2064 mpfr_init_set(this->imag_
, *imag
, GMP_RNDN
);
2067 // Constrain REAL/IMAG to fit into TYPE.
2069 constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
);
2071 // Return whether REAL/IMAG fits in the type.
2073 check_constant(mpfr_t real
, mpfr_t imag
, Type
*, source_location
);
2075 // Write REAL/IMAG to export data.
2077 export_complex(Export
* exp
, const mpfr_t real
, const mpfr_t val
);
2081 do_is_constant() const
2085 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2091 do_determine_type(const Type_context
*);
2094 do_check_types(Gogo
*);
2099 return Expression::make_complex(&this->real_
, &this->imag_
, this->type_
,
2104 do_get_tree(Translate_context
*);
2107 do_export(Export
*) const;
2112 // The imaginary part;
2114 // The type if known.
2118 // Constrain REAL/IMAG to fit into TYPE.
2121 Complex_expression::constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
)
2123 Complex_type
* ctype
= type
->complex_type();
2124 if (ctype
!= NULL
&& !ctype
->is_abstract())
2126 tree type_tree
= ctype
->type_tree();
2128 REAL_VALUE_TYPE rvt
;
2129 real_from_mpfr(&rvt
, real
, TREE_TYPE(type_tree
), GMP_RNDN
);
2130 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2131 mpfr_from_real(real
, &rvt
, GMP_RNDN
);
2133 real_from_mpfr(&rvt
, imag
, TREE_TYPE(type_tree
), GMP_RNDN
);
2134 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2135 mpfr_from_real(imag
, &rvt
, GMP_RNDN
);
2139 // Return a complex constant value.
2142 Complex_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2145 if (this->type_
!= NULL
)
2146 *ptype
= this->type_
;
2147 mpfr_set(real
, this->real_
, GMP_RNDN
);
2148 mpfr_set(imag
, this->imag_
, GMP_RNDN
);
2152 // Return the current type. If we haven't set the type yet, we return
2153 // an abstract complex type.
2156 Complex_expression::do_type()
2158 if (this->type_
== NULL
)
2159 this->type_
= Type::make_abstract_complex_type();
2163 // Set the type of the complex value. Here we may switch from an
2164 // abstract type to a real type.
2167 Complex_expression::do_determine_type(const Type_context
* context
)
2169 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2171 else if (context
->type
!= NULL
2172 && context
->type
->complex_type() != NULL
)
2173 this->type_
= context
->type
;
2174 else if (!context
->may_be_abstract
)
2175 this->type_
= Type::lookup_complex_type("complex");
2178 // Return true if the complex value REAL/IMAG fits in the range of the
2179 // type TYPE. Otherwise give an error and return false. TYPE may be
2183 Complex_expression::check_constant(mpfr_t real
, mpfr_t imag
, Type
* type
,
2184 source_location location
)
2188 Complex_type
* ctype
= type
->complex_type();
2189 if (ctype
== NULL
|| ctype
->is_abstract())
2193 switch (ctype
->bits())
2205 // A NaN or Infinity always fits in the range of the type.
2206 if (!mpfr_nan_p(real
) && !mpfr_inf_p(real
) && !mpfr_zero_p(real
))
2208 if (mpfr_get_exp(real
) > max_exp
)
2210 error_at(location
, "complex real part constant overflow");
2215 if (!mpfr_nan_p(imag
) && !mpfr_inf_p(imag
) && !mpfr_zero_p(imag
))
2217 if (mpfr_get_exp(imag
) > max_exp
)
2219 error_at(location
, "complex imaginary part constant overflow");
2227 // Check the type of a complex value.
2230 Complex_expression::do_check_types(Gogo
*)
2232 if (this->type_
== NULL
)
2235 if (!Complex_expression::check_constant(this->real_
, this->imag_
,
2236 this->type_
, this->location()))
2237 this->set_is_error();
2240 // Get a tree for a complex constant.
2243 Complex_expression::do_get_tree(Translate_context
* context
)
2245 Gogo
* gogo
= context
->gogo();
2247 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2248 type
= this->type_
->get_tree(gogo
);
2251 // If we still have an abstract type here, this this is being
2252 // used in a constant expression which didn't get reduced. We
2253 // just use complex128 and hope for the best.
2254 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
2256 return Expression::complex_constant_tree(this->real_
, this->imag_
, type
);
2259 // Write REAL/IMAG to export data.
2262 Complex_expression::export_complex(Export
* exp
, const mpfr_t real
,
2265 if (!mpfr_zero_p(real
))
2267 Float_expression::export_float(exp
, real
);
2268 if (mpfr_sgn(imag
) > 0)
2269 exp
->write_c_string("+");
2271 Float_expression::export_float(exp
, imag
);
2272 exp
->write_c_string("i");
2275 // Export a complex number in a constant expression.
2278 Complex_expression::do_export(Export
* exp
) const
2280 Complex_expression::export_complex(exp
, this->real_
, this->imag_
);
2281 // A trailing space lets us reliably identify the end of the number.
2282 exp
->write_c_string(" ");
2285 // Make a complex expression.
2288 Expression::make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2289 source_location location
)
2291 return new Complex_expression(real
, imag
, type
, location
);
2294 // Find a named object in an expression.
2296 class Find_named_object
: public Traverse
2299 Find_named_object(Named_object
* no
)
2300 : Traverse(traverse_expressions
),
2301 no_(no
), found_(false)
2304 // Whether we found the object.
2307 { return this->found_
; }
2311 expression(Expression
**);
2314 // The object we are looking for.
2316 // Whether we found it.
2320 // A reference to a const in an expression.
2322 class Const_expression
: public Expression
2325 Const_expression(Named_object
* constant
, source_location location
)
2326 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2327 constant_(constant
), type_(NULL
), seen_(false)
2332 { return this->constant_
; }
2336 { return this->constant_
->name(); }
2340 do_lower(Gogo
*, Named_object
*, int);
2343 do_is_constant() const
2347 do_integer_constant_value(bool, mpz_t val
, Type
**) const;
2350 do_float_constant_value(mpfr_t val
, Type
**) const;
2353 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2356 do_string_constant_value(std::string
* val
) const
2357 { return this->constant_
->const_value()->expr()->string_constant_value(val
); }
2362 // The type of a const is set by the declaration, not the use.
2364 do_determine_type(const Type_context
*);
2367 do_check_types(Gogo
*);
2374 do_get_tree(Translate_context
* context
);
2376 // When exporting a reference to a const as part of a const
2377 // expression, we export the value. We ignore the fact that it has
2380 do_export(Export
* exp
) const
2381 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2385 Named_object
* constant_
;
2386 // The type of this reference. This is used if the constant has an
2389 // Used to prevent infinite recursion when a constant incorrectly
2390 // refers to itself.
2394 // Lower a constant expression. This is where we convert the
2395 // predeclared constant iota into an integer value.
2398 Const_expression::do_lower(Gogo
* gogo
, Named_object
*, int iota_value
)
2400 if (this->constant_
->const_value()->expr()->classification()
2403 if (iota_value
== -1)
2405 error_at(this->location(),
2406 "iota is only defined in const declarations");
2410 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2411 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2417 // Make sure that the constant itself has been lowered.
2418 gogo
->lower_constant(this->constant_
);
2423 // Return an integer constant value.
2426 Const_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
2433 if (this->type_
!= NULL
)
2434 ctype
= this->type_
;
2436 ctype
= this->constant_
->const_value()->type();
2437 if (ctype
!= NULL
&& ctype
->integer_type() == NULL
)
2440 Expression
* e
= this->constant_
->const_value()->expr();
2445 bool r
= e
->integer_constant_value(iota_is_constant
, val
, &t
);
2447 this->seen_
= false;
2451 && !Integer_expression::check_constant(val
, ctype
, this->location()))
2454 *ptype
= ctype
!= NULL
? ctype
: t
;
2458 // Return a floating point constant value.
2461 Const_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
2467 if (this->type_
!= NULL
)
2468 ctype
= this->type_
;
2470 ctype
= this->constant_
->const_value()->type();
2471 if (ctype
!= NULL
&& ctype
->float_type() == NULL
)
2477 bool r
= this->constant_
->const_value()->expr()->float_constant_value(val
,
2480 this->seen_
= false;
2482 if (r
&& ctype
!= NULL
)
2484 if (!Float_expression::check_constant(val
, ctype
, this->location()))
2486 Float_expression::constrain_float(val
, ctype
);
2488 *ptype
= ctype
!= NULL
? ctype
: t
;
2492 // Return a complex constant value.
2495 Const_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2502 if (this->type_
!= NULL
)
2503 ctype
= this->type_
;
2505 ctype
= this->constant_
->const_value()->type();
2506 if (ctype
!= NULL
&& ctype
->complex_type() == NULL
)
2512 bool r
= this->constant_
->const_value()->expr()->complex_constant_value(real
,
2516 this->seen_
= false;
2518 if (r
&& ctype
!= NULL
)
2520 if (!Complex_expression::check_constant(real
, imag
, ctype
,
2523 Complex_expression::constrain_complex(real
, imag
, ctype
);
2525 *ptype
= ctype
!= NULL
? ctype
: t
;
2529 // Return the type of the const reference.
2532 Const_expression::do_type()
2534 if (this->type_
!= NULL
)
2537 Named_constant
* nc
= this->constant_
->const_value();
2539 if (this->seen_
|| nc
->lowering())
2541 this->report_error(_("constant refers to itself"));
2542 this->type_
= Type::make_error_type();
2548 Type
* ret
= nc
->type();
2552 this->seen_
= false;
2556 // During parsing, a named constant may have a NULL type, but we
2557 // must not return a NULL type here.
2558 ret
= nc
->expr()->type();
2560 this->seen_
= false;
2565 // Set the type of the const reference.
2568 Const_expression::do_determine_type(const Type_context
* context
)
2570 Type
* ctype
= this->constant_
->const_value()->type();
2571 Type
* cetype
= (ctype
!= NULL
2573 : this->constant_
->const_value()->expr()->type());
2574 if (ctype
!= NULL
&& !ctype
->is_abstract())
2576 else if (context
->type
!= NULL
2577 && (context
->type
->integer_type() != NULL
2578 || context
->type
->float_type() != NULL
2579 || context
->type
->complex_type() != NULL
)
2580 && (cetype
->integer_type() != NULL
2581 || cetype
->float_type() != NULL
2582 || cetype
->complex_type() != NULL
))
2583 this->type_
= context
->type
;
2584 else if (context
->type
!= NULL
2585 && context
->type
->is_string_type()
2586 && cetype
->is_string_type())
2587 this->type_
= context
->type
;
2588 else if (context
->type
!= NULL
2589 && context
->type
->is_boolean_type()
2590 && cetype
->is_boolean_type())
2591 this->type_
= context
->type
;
2592 else if (!context
->may_be_abstract
)
2594 if (cetype
->is_abstract())
2595 cetype
= cetype
->make_non_abstract_type();
2596 this->type_
= cetype
;
2600 // Check types of a const reference.
2603 Const_expression::do_check_types(Gogo
*)
2605 if (this->type_
!= NULL
&& this->type_
->is_error_type())
2608 Expression
* init
= this->constant_
->const_value()->expr();
2609 Find_named_object
find_named_object(this->constant_
);
2610 Expression::traverse(&init
, &find_named_object
);
2611 if (find_named_object
.found())
2613 this->report_error(_("constant refers to itself"));
2614 this->type_
= Type::make_error_type();
2618 if (this->type_
== NULL
|| this->type_
->is_abstract())
2621 // Check for integer overflow.
2622 if (this->type_
->integer_type() != NULL
)
2627 if (!this->integer_constant_value(true, ival
, &dummy
))
2631 Expression
* cexpr
= this->constant_
->const_value()->expr();
2632 if (cexpr
->float_constant_value(fval
, &dummy
))
2634 if (!mpfr_integer_p(fval
))
2635 this->report_error(_("floating point constant "
2636 "truncated to integer"));
2639 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2640 Integer_expression::check_constant(ival
, this->type_
,
2650 // Return a tree for the const reference.
2653 Const_expression::do_get_tree(Translate_context
* context
)
2655 Gogo
* gogo
= context
->gogo();
2657 if (this->type_
== NULL
)
2658 type_tree
= NULL_TREE
;
2661 type_tree
= this->type_
->get_tree(gogo
);
2662 if (type_tree
== error_mark_node
)
2663 return error_mark_node
;
2666 // If the type has been set for this expression, but the underlying
2667 // object is an abstract int or float, we try to get the abstract
2668 // value. Otherwise we may lose something in the conversion.
2669 if (this->type_
!= NULL
2670 && this->constant_
->const_value()->type()->is_abstract())
2672 Expression
* expr
= this->constant_
->const_value()->expr();
2676 if (expr
->integer_constant_value(true, ival
, &t
))
2678 tree ret
= Expression::integer_constant_tree(ival
, type_tree
);
2686 if (expr
->float_constant_value(fval
, &t
))
2688 tree ret
= Expression::float_constant_tree(fval
, type_tree
);
2695 if (expr
->complex_constant_value(fval
, imag
, &t
))
2697 tree ret
= Expression::complex_constant_tree(fval
, imag
, type_tree
);
2706 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2707 if (this->type_
== NULL
2708 || const_tree
== error_mark_node
2709 || TREE_TYPE(const_tree
) == error_mark_node
)
2713 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2714 ret
= fold_convert(type_tree
, const_tree
);
2715 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2716 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2717 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2718 ret
= fold(convert_to_real(type_tree
, const_tree
));
2719 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2720 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2726 // Make a reference to a constant in an expression.
2729 Expression::make_const_reference(Named_object
* constant
,
2730 source_location location
)
2732 return new Const_expression(constant
, location
);
2735 // Find a named object in an expression.
2738 Find_named_object::expression(Expression
** pexpr
)
2740 switch ((*pexpr
)->classification())
2742 case Expression::EXPRESSION_CONST_REFERENCE
:
2743 if (static_cast<Const_expression
*>(*pexpr
)->named_object() == this->no_
)
2745 return TRAVERSE_CONTINUE
;
2746 case Expression::EXPRESSION_VAR_REFERENCE
:
2747 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
2749 return TRAVERSE_CONTINUE
;
2750 case Expression::EXPRESSION_FUNC_REFERENCE
:
2751 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
2753 return TRAVERSE_CONTINUE
;
2755 return TRAVERSE_CONTINUE
;
2757 this->found_
= true;
2758 return TRAVERSE_EXIT
;
2763 class Nil_expression
: public Expression
2766 Nil_expression(source_location location
)
2767 : Expression(EXPRESSION_NIL
, location
)
2775 do_is_constant() const
2780 { return Type::make_nil_type(); }
2783 do_determine_type(const Type_context
*)
2791 do_get_tree(Translate_context
*)
2792 { return null_pointer_node
; }
2795 do_export(Export
* exp
) const
2796 { exp
->write_c_string("nil"); }
2799 // Import a nil expression.
2802 Nil_expression::do_import(Import
* imp
)
2804 imp
->require_c_string("nil");
2805 return Expression::make_nil(imp
->location());
2808 // Make a nil expression.
2811 Expression::make_nil(source_location location
)
2813 return new Nil_expression(location
);
2816 // The value of the predeclared constant iota. This is little more
2817 // than a marker. This will be lowered to an integer in
2818 // Const_expression::do_lower, which is where we know the value that
2821 class Iota_expression
: public Parser_expression
2824 Iota_expression(source_location location
)
2825 : Parser_expression(EXPRESSION_IOTA
, location
)
2830 do_lower(Gogo
*, Named_object
*, int)
2831 { gcc_unreachable(); }
2833 // There should only ever be one of these.
2836 { gcc_unreachable(); }
2839 // Make an iota expression. This is only called for one case: the
2840 // value of the predeclared constant iota.
2843 Expression::make_iota()
2845 static Iota_expression
iota_expression(UNKNOWN_LOCATION
);
2846 return &iota_expression
;
2849 // A type conversion expression.
2851 class Type_conversion_expression
: public Expression
2854 Type_conversion_expression(Type
* type
, Expression
* expr
,
2855 source_location location
)
2856 : Expression(EXPRESSION_CONVERSION
, location
),
2857 type_(type
), expr_(expr
), may_convert_function_types_(false)
2860 // Return the type to which we are converting.
2863 { return this->type_
; }
2865 // Return the expression which we are converting.
2868 { return this->expr_
; }
2870 // Permit converting from one function type to another. This is
2871 // used internally for method expressions.
2873 set_may_convert_function_types()
2875 this->may_convert_function_types_
= true;
2878 // Import a type conversion expression.
2884 do_traverse(Traverse
* traverse
);
2887 do_lower(Gogo
*, Named_object
*, int);
2890 do_is_constant() const
2891 { return this->expr_
->is_constant(); }
2894 do_integer_constant_value(bool, mpz_t
, Type
**) const;
2897 do_float_constant_value(mpfr_t
, Type
**) const;
2900 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
2903 do_string_constant_value(std::string
*) const;
2907 { return this->type_
; }
2910 do_determine_type(const Type_context
*)
2912 Type_context
subcontext(this->type_
, false);
2913 this->expr_
->determine_type(&subcontext
);
2917 do_check_types(Gogo
*);
2922 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
2927 do_get_tree(Translate_context
* context
);
2930 do_export(Export
*) const;
2933 // The type to convert to.
2935 // The expression to convert.
2937 // True if this is permitted to convert function types. This is
2938 // used internally for method expressions.
2939 bool may_convert_function_types_
;
2945 Type_conversion_expression::do_traverse(Traverse
* traverse
)
2947 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
2948 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
2949 return TRAVERSE_EXIT
;
2950 return TRAVERSE_CONTINUE
;
2953 // Convert to a constant at lowering time.
2956 Type_conversion_expression::do_lower(Gogo
*, Named_object
*, int)
2958 Type
* type
= this->type_
;
2959 Expression
* val
= this->expr_
;
2960 source_location location
= this->location();
2962 if (type
->integer_type() != NULL
)
2967 if (val
->integer_constant_value(false, ival
, &dummy
))
2969 if (!Integer_expression::check_constant(ival
, type
, location
))
2970 mpz_set_ui(ival
, 0);
2971 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2978 if (val
->float_constant_value(fval
, &dummy
))
2980 if (!mpfr_integer_p(fval
))
2983 "floating point constant truncated to integer");
2984 return Expression::make_error(location
);
2986 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2987 if (!Integer_expression::check_constant(ival
, type
, location
))
2988 mpz_set_ui(ival
, 0);
2989 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2998 if (type
->float_type() != NULL
)
3003 if (val
->float_constant_value(fval
, &dummy
))
3005 if (!Float_expression::check_constant(fval
, type
, location
))
3006 mpfr_set_ui(fval
, 0, GMP_RNDN
);
3007 Float_expression::constrain_float(fval
, type
);
3008 Expression
*ret
= Expression::make_float(&fval
, type
, location
);
3015 if (type
->complex_type() != NULL
)
3022 if (val
->complex_constant_value(real
, imag
, &dummy
))
3024 if (!Complex_expression::check_constant(real
, imag
, type
, location
))
3026 mpfr_set_ui(real
, 0, GMP_RNDN
);
3027 mpfr_set_ui(imag
, 0, GMP_RNDN
);
3029 Complex_expression::constrain_complex(real
, imag
, type
);
3030 Expression
* ret
= Expression::make_complex(&real
, &imag
, type
,
3040 if (type
->is_open_array_type() && type
->named_type() == NULL
)
3042 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3043 bool is_byte
= element_type
== Type::lookup_integer_type("uint8");
3044 bool is_int
= element_type
== Type::lookup_integer_type("int");
3045 if (is_byte
|| is_int
)
3048 if (val
->string_constant_value(&s
))
3050 Expression_list
* vals
= new Expression_list();
3053 for (std::string::const_iterator p
= s
.begin();
3058 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
3059 Expression
* v
= Expression::make_integer(&val
,
3068 const char *p
= s
.data();
3069 const char *pend
= s
.data() + s
.length();
3073 int adv
= Lex::fetch_char(p
, &c
);
3076 warning_at(this->location(), 0,
3077 "invalid UTF-8 encoding");
3082 mpz_init_set_ui(val
, c
);
3083 Expression
* v
= Expression::make_integer(&val
,
3091 return Expression::make_slice_composite_literal(type
, vals
,
3100 // Return the constant integer value if there is one.
3103 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant
,
3107 if (this->type_
->integer_type() == NULL
)
3113 if (this->expr_
->integer_constant_value(iota_is_constant
, ival
, &dummy
))
3115 if (!Integer_expression::check_constant(ival
, this->type_
,
3123 *ptype
= this->type_
;
3130 if (this->expr_
->float_constant_value(fval
, &dummy
))
3132 mpfr_get_z(val
, fval
, GMP_RNDN
);
3134 if (!Integer_expression::check_constant(val
, this->type_
,
3137 *ptype
= this->type_
;
3145 // Return the constant floating point value if there is one.
3148 Type_conversion_expression::do_float_constant_value(mpfr_t val
,
3151 if (this->type_
->float_type() == NULL
)
3157 if (this->expr_
->float_constant_value(fval
, &dummy
))
3159 if (!Float_expression::check_constant(fval
, this->type_
,
3165 mpfr_set(val
, fval
, GMP_RNDN
);
3167 Float_expression::constrain_float(val
, this->type_
);
3168 *ptype
= this->type_
;
3176 // Return the constant complex value if there is one.
3179 Type_conversion_expression::do_complex_constant_value(mpfr_t real
,
3183 if (this->type_
->complex_type() == NULL
)
3191 if (this->expr_
->complex_constant_value(rval
, ival
, &dummy
))
3193 if (!Complex_expression::check_constant(rval
, ival
, this->type_
,
3200 mpfr_set(real
, rval
, GMP_RNDN
);
3201 mpfr_set(imag
, ival
, GMP_RNDN
);
3204 Complex_expression::constrain_complex(real
, imag
, this->type_
);
3205 *ptype
= this->type_
;
3214 // Return the constant string value if there is one.
3217 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3219 if (this->type_
->is_string_type()
3220 && this->expr_
->type()->integer_type() != NULL
)
3225 if (this->expr_
->integer_constant_value(false, ival
, &dummy
))
3227 unsigned long ulval
= mpz_get_ui(ival
);
3228 if (mpz_cmp_ui(ival
, ulval
) == 0)
3230 Lex::append_char(ulval
, true, val
, this->location());
3238 // FIXME: Could handle conversion from const []int here.
3243 // Check that types are convertible.
3246 Type_conversion_expression::do_check_types(Gogo
*)
3248 Type
* type
= this->type_
;
3249 Type
* expr_type
= this->expr_
->type();
3252 if (type
->is_error_type()
3253 || type
->is_undefined()
3254 || expr_type
->is_error_type()
3255 || expr_type
->is_undefined())
3257 // Make sure we emit an error for an undefined type.
3260 this->set_is_error();
3264 if (this->may_convert_function_types_
3265 && type
->function_type() != NULL
3266 && expr_type
->function_type() != NULL
)
3269 if (Type::are_convertible(type
, expr_type
, &reason
))
3272 error_at(this->location(), "%s", reason
.c_str());
3273 this->set_is_error();
3276 // Get a tree for a type conversion.
3279 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3281 Gogo
* gogo
= context
->gogo();
3282 tree type_tree
= this->type_
->get_tree(gogo
);
3283 tree expr_tree
= this->expr_
->get_tree(context
);
3285 if (type_tree
== error_mark_node
3286 || expr_tree
== error_mark_node
3287 || TREE_TYPE(expr_tree
) == error_mark_node
)
3288 return error_mark_node
;
3290 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3291 return fold_convert(type_tree
, expr_tree
);
3293 Type
* type
= this->type_
;
3294 Type
* expr_type
= this->expr_
->type();
3296 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3297 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3298 expr_tree
, this->location());
3299 else if (type
->integer_type() != NULL
)
3301 if (expr_type
->integer_type() != NULL
3302 || expr_type
->float_type() != NULL
3303 || expr_type
->is_unsafe_pointer_type())
3304 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3308 else if (type
->float_type() != NULL
)
3310 if (expr_type
->integer_type() != NULL
3311 || expr_type
->float_type() != NULL
)
3312 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3316 else if (type
->complex_type() != NULL
)
3318 if (expr_type
->complex_type() != NULL
)
3319 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3323 else if (type
->is_string_type()
3324 && expr_type
->integer_type() != NULL
)
3326 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3327 if (host_integerp(expr_tree
, 0))
3329 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3331 Lex::append_char(intval
, true, &s
, this->location());
3332 Expression
* se
= Expression::make_string(s
, this->location());
3333 return se
->get_tree(context
);
3336 static tree int_to_string_fndecl
;
3337 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3339 "__go_int_to_string",
3343 fold_convert(integer_type_node
, expr_tree
));
3345 else if (type
->is_string_type()
3346 && (expr_type
->array_type() != NULL
3347 || (expr_type
->points_to() != NULL
3348 && expr_type
->points_to()->array_type() != NULL
)))
3350 Type
* t
= expr_type
;
3351 if (t
->points_to() != NULL
)
3354 expr_tree
= build_fold_indirect_ref(expr_tree
);
3356 if (!DECL_P(expr_tree
))
3357 expr_tree
= save_expr(expr_tree
);
3358 Array_type
* a
= t
->array_type();
3359 Type
* e
= a
->element_type()->forwarded();
3360 gcc_assert(e
->integer_type() != NULL
);
3361 tree valptr
= fold_convert(const_ptr_type_node
,
3362 a
->value_pointer_tree(gogo
, expr_tree
));
3363 tree len
= a
->length_tree(gogo
, expr_tree
);
3364 len
= fold_convert_loc(this->location(), size_type_node
, len
);
3365 if (e
->integer_type()->is_unsigned()
3366 && e
->integer_type()->bits() == 8)
3368 static tree byte_array_to_string_fndecl
;
3369 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3371 "__go_byte_array_to_string",
3374 const_ptr_type_node
,
3381 gcc_assert(e
== Type::lookup_integer_type("int"));
3382 static tree int_array_to_string_fndecl
;
3383 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3385 "__go_int_array_to_string",
3388 const_ptr_type_node
,
3394 else if (type
->is_open_array_type() && expr_type
->is_string_type())
3396 Type
* e
= type
->array_type()->element_type()->forwarded();
3397 gcc_assert(e
->integer_type() != NULL
);
3398 if (e
->integer_type()->is_unsigned()
3399 && e
->integer_type()->bits() == 8)
3401 static tree string_to_byte_array_fndecl
;
3402 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3404 "__go_string_to_byte_array",
3407 TREE_TYPE(expr_tree
),
3412 gcc_assert(e
== Type::lookup_integer_type("int"));
3413 static tree string_to_int_array_fndecl
;
3414 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3416 "__go_string_to_int_array",
3419 TREE_TYPE(expr_tree
),
3423 else if ((type
->is_unsafe_pointer_type()
3424 && expr_type
->points_to() != NULL
)
3425 || (expr_type
->is_unsafe_pointer_type()
3426 && type
->points_to() != NULL
))
3427 ret
= fold_convert(type_tree
, expr_tree
);
3428 else if (type
->is_unsafe_pointer_type()
3429 && expr_type
->integer_type() != NULL
)
3430 ret
= convert_to_pointer(type_tree
, expr_tree
);
3431 else if (this->may_convert_function_types_
3432 && type
->function_type() != NULL
3433 && expr_type
->function_type() != NULL
)
3434 ret
= fold_convert_loc(this->location(), type_tree
, expr_tree
);
3436 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3437 expr_tree
, this->location());
3442 // Output a type conversion in a constant expression.
3445 Type_conversion_expression::do_export(Export
* exp
) const
3447 exp
->write_c_string("convert(");
3448 exp
->write_type(this->type_
);
3449 exp
->write_c_string(", ");
3450 this->expr_
->export_expression(exp
);
3451 exp
->write_c_string(")");
3454 // Import a type conversion or a struct construction.
3457 Type_conversion_expression::do_import(Import
* imp
)
3459 imp
->require_c_string("convert(");
3460 Type
* type
= imp
->read_type();
3461 imp
->require_c_string(", ");
3462 Expression
* val
= Expression::import_expression(imp
);
3463 imp
->require_c_string(")");
3464 return Expression::make_cast(type
, val
, imp
->location());
3467 // Make a type cast expression.
3470 Expression::make_cast(Type
* type
, Expression
* val
, source_location location
)
3472 if (type
->is_error_type() || val
->is_error_expression())
3473 return Expression::make_error(location
);
3474 return new Type_conversion_expression(type
, val
, location
);
3477 // Unary expressions.
3479 class Unary_expression
: public Expression
3482 Unary_expression(Operator op
, Expression
* expr
, source_location location
)
3483 : Expression(EXPRESSION_UNARY
, location
),
3484 op_(op
), escapes_(true), expr_(expr
)
3487 // Return the operator.
3490 { return this->op_
; }
3492 // Return the operand.
3495 { return this->expr_
; }
3497 // Record that an address expression does not escape.
3499 set_does_not_escape()
3501 gcc_assert(this->op_
== OPERATOR_AND
);
3502 this->escapes_
= false;
3505 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3506 // could be done, false if not.
3508 eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3511 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3512 // could be done, false if not.
3514 eval_float(Operator op
, mpfr_t uval
, mpfr_t val
);
3516 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3517 // true if this could be done, false if not.
3519 eval_complex(Operator op
, mpfr_t ureal
, mpfr_t uimag
, mpfr_t real
,
3527 do_traverse(Traverse
* traverse
)
3528 { return Expression::traverse(&this->expr_
, traverse
); }
3531 do_lower(Gogo
*, Named_object
*, int);
3534 do_is_constant() const;
3537 do_integer_constant_value(bool, mpz_t
, Type
**) const;
3540 do_float_constant_value(mpfr_t
, Type
**) const;
3543 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
3549 do_determine_type(const Type_context
*);
3552 do_check_types(Gogo
*);
3557 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3562 do_is_addressable() const
3563 { return this->op_
== OPERATOR_MULT
; }
3566 do_get_tree(Translate_context
*);
3569 do_export(Export
*) const;
3572 // The unary operator to apply.
3574 // Normally true. False if this is an address expression which does
3575 // not escape the current function.
3581 // If we are taking the address of a composite literal, and the
3582 // contents are not constant, then we want to make a heap composite
3586 Unary_expression::do_lower(Gogo
*, Named_object
*, int)
3588 source_location loc
= this->location();
3589 Operator op
= this->op_
;
3590 Expression
* expr
= this->expr_
;
3592 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3593 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3595 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3596 // moving x to the heap. FIXME: Is it worth doing a real escape
3597 // analysis here? This case is found in math/unsafe.go and is
3598 // therefore worth special casing.
3599 if (op
== OPERATOR_MULT
)
3601 Expression
* e
= expr
;
3602 while (e
->classification() == EXPRESSION_CONVERSION
)
3604 Type_conversion_expression
* te
3605 = static_cast<Type_conversion_expression
*>(e
);
3609 if (e
->classification() == EXPRESSION_UNARY
)
3611 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3612 if (ue
->op_
== OPERATOR_AND
)
3619 ue
->set_does_not_escape();
3624 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
3625 || op
== OPERATOR_NOT
|| op
== OPERATOR_XOR
)
3627 Expression
* ret
= NULL
;
3632 if (expr
->integer_constant_value(false, eval
, &etype
))
3636 if (Unary_expression::eval_integer(op
, etype
, eval
, val
, loc
))
3637 ret
= Expression::make_integer(&val
, etype
, loc
);
3644 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
)
3649 if (expr
->float_constant_value(fval
, &ftype
))
3653 if (Unary_expression::eval_float(op
, fval
, val
))
3654 ret
= Expression::make_float(&val
, ftype
, loc
);
3665 if (expr
->complex_constant_value(fval
, ival
, &ftype
))
3671 if (Unary_expression::eval_complex(op
, fval
, ival
, real
, imag
))
3672 ret
= Expression::make_complex(&real
, &imag
, ftype
, loc
);
3686 // Return whether a unary expression is a constant.
3689 Unary_expression::do_is_constant() const
3691 if (this->op_
== OPERATOR_MULT
)
3693 // Indirecting through a pointer is only constant if the object
3694 // to which the expression points is constant, but we currently
3695 // have no way to determine that.
3698 else if (this->op_
== OPERATOR_AND
)
3700 // Taking the address of a variable is constant if it is a
3701 // global variable, not constant otherwise. In other cases
3702 // taking the address is probably not a constant.
3703 Var_expression
* ve
= this->expr_
->var_expression();
3706 Named_object
* no
= ve
->named_object();
3707 return no
->is_variable() && no
->var_value()->is_global();
3712 return this->expr_
->is_constant();
3715 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3716 // UVAL, if known; it may be NULL. Return true if this could be done,
3720 Unary_expression::eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3721 source_location location
)
3728 case OPERATOR_MINUS
:
3730 return Integer_expression::check_constant(val
, utype
, location
);
3732 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3736 || utype
->integer_type() == NULL
3737 || utype
->integer_type()->is_abstract())
3741 // The number of HOST_WIDE_INTs that it takes to represent
3743 size_t count
= ((mpz_sizeinbase(uval
, 2)
3744 + HOST_BITS_PER_WIDE_INT
3746 / HOST_BITS_PER_WIDE_INT
);
3748 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3749 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3752 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3753 gcc_assert(ecount
<= count
);
3755 // Trim down to the number of words required by the type.
3756 size_t obits
= utype
->integer_type()->bits();
3757 if (!utype
->integer_type()->is_unsigned())
3759 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3760 / HOST_BITS_PER_WIDE_INT
);
3761 gcc_assert(ocount
<= ocount
);
3763 for (size_t i
= 0; i
< ocount
; ++i
)
3766 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3768 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3771 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3775 return Integer_expression::check_constant(val
, utype
, location
);
3784 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3785 // could be done, false if not.
3788 Unary_expression::eval_float(Operator op
, mpfr_t uval
, mpfr_t val
)
3793 mpfr_set(val
, uval
, GMP_RNDN
);
3795 case OPERATOR_MINUS
:
3796 mpfr_neg(val
, uval
, GMP_RNDN
);
3808 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3809 // if this could be done, false if not.
3812 Unary_expression::eval_complex(Operator op
, mpfr_t rval
, mpfr_t ival
,
3813 mpfr_t real
, mpfr_t imag
)
3818 mpfr_set(real
, rval
, GMP_RNDN
);
3819 mpfr_set(imag
, ival
, GMP_RNDN
);
3821 case OPERATOR_MINUS
:
3822 mpfr_neg(real
, rval
, GMP_RNDN
);
3823 mpfr_neg(imag
, ival
, GMP_RNDN
);
3835 // Return the integral constant value of a unary expression, if it has one.
3838 Unary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
3844 if (!this->expr_
->integer_constant_value(iota_is_constant
, uval
, ptype
))
3847 ret
= Unary_expression::eval_integer(this->op_
, *ptype
, uval
, val
,
3853 // Return the floating point constant value of a unary expression, if
3857 Unary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
3862 if (!this->expr_
->float_constant_value(uval
, ptype
))
3865 ret
= Unary_expression::eval_float(this->op_
, uval
, val
);
3870 // Return the complex constant value of a unary expression, if it has
3874 Unary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
3882 if (!this->expr_
->complex_constant_value(rval
, ival
, ptype
))
3885 ret
= Unary_expression::eval_complex(this->op_
, rval
, ival
, real
, imag
);
3891 // Return the type of a unary expression.
3894 Unary_expression::do_type()
3899 case OPERATOR_MINUS
:
3902 return this->expr_
->type();
3905 return Type::make_pointer_type(this->expr_
->type());
3909 Type
* subtype
= this->expr_
->type();
3910 Type
* points_to
= subtype
->points_to();
3911 if (points_to
== NULL
)
3912 return Type::make_error_type();
3921 // Determine abstract types for a unary expression.
3924 Unary_expression::do_determine_type(const Type_context
* context
)
3929 case OPERATOR_MINUS
:
3932 this->expr_
->determine_type(context
);
3936 // Taking the address of something.
3938 Type
* subtype
= (context
->type
== NULL
3940 : context
->type
->points_to());
3941 Type_context
subcontext(subtype
, false);
3942 this->expr_
->determine_type(&subcontext
);
3947 // Indirecting through a pointer.
3949 Type
* subtype
= (context
->type
== NULL
3951 : Type::make_pointer_type(context
->type
));
3952 Type_context
subcontext(subtype
, false);
3953 this->expr_
->determine_type(&subcontext
);
3962 // Check types for a unary expression.
3965 Unary_expression::do_check_types(Gogo
*)
3967 Type
* type
= this->expr_
->type();
3968 if (type
->is_error_type())
3970 this->set_is_error();
3977 case OPERATOR_MINUS
:
3978 if (type
->integer_type() == NULL
3979 && type
->float_type() == NULL
3980 && type
->complex_type() == NULL
)
3981 this->report_error(_("expected numeric type"));
3986 if (type
->integer_type() == NULL
3987 && !type
->is_boolean_type())
3988 this->report_error(_("expected integer or boolean type"));
3992 if (!this->expr_
->is_addressable())
3993 this->report_error(_("invalid operand for unary %<&%>"));
3995 this->expr_
->address_taken(this->escapes_
);
3999 // Indirecting through a pointer.
4000 if (type
->points_to() == NULL
)
4001 this->report_error(_("expected pointer"));
4009 // Get a tree for a unary expression.
4012 Unary_expression::do_get_tree(Translate_context
* context
)
4014 tree expr
= this->expr_
->get_tree(context
);
4015 if (expr
== error_mark_node
)
4016 return error_mark_node
;
4018 source_location loc
= this->location();
4024 case OPERATOR_MINUS
:
4026 tree type
= TREE_TYPE(expr
);
4027 tree compute_type
= excess_precision_type(type
);
4028 if (compute_type
!= NULL_TREE
)
4029 expr
= ::convert(compute_type
, expr
);
4030 tree ret
= fold_build1_loc(loc
, NEGATE_EXPR
,
4031 (compute_type
!= NULL_TREE
4035 if (compute_type
!= NULL_TREE
)
4036 ret
= ::convert(type
, ret
);
4041 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
4042 return fold_build1_loc(loc
, TRUTH_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4044 return fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, expr
,
4045 build_int_cst(TREE_TYPE(expr
), 0));
4048 return fold_build1_loc(loc
, BIT_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4051 // We should not see a non-constant constructor here; cases
4052 // where we would see one should have been moved onto the heap
4053 // at parse time. Taking the address of a nonconstant
4054 // constructor will not do what the programmer expects.
4055 gcc_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
4056 gcc_assert(TREE_CODE(expr
) != ADDR_EXPR
);
4058 // Build a decl for a constant constructor.
4059 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
4061 tree decl
= build_decl(this->location(), VAR_DECL
,
4062 create_tmp_var_name("C"), TREE_TYPE(expr
));
4063 DECL_EXTERNAL(decl
) = 0;
4064 TREE_PUBLIC(decl
) = 0;
4065 TREE_READONLY(decl
) = 1;
4066 TREE_CONSTANT(decl
) = 1;
4067 TREE_STATIC(decl
) = 1;
4068 TREE_ADDRESSABLE(decl
) = 1;
4069 DECL_ARTIFICIAL(decl
) = 1;
4070 DECL_INITIAL(decl
) = expr
;
4071 rest_of_decl_compilation(decl
, 1, 0);
4075 return build_fold_addr_expr_loc(loc
, expr
);
4079 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
4081 // If we are dereferencing the pointer to a large struct, we
4082 // need to check for nil. We don't bother to check for small
4083 // structs because we expect the system to crash on a nil
4084 // pointer dereference.
4085 HOST_WIDE_INT s
= int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr
)));
4086 if (s
== -1 || s
>= 4096)
4089 expr
= save_expr(expr
);
4090 tree compare
= fold_build2_loc(loc
, EQ_EXPR
, boolean_type_node
,
4092 fold_convert(TREE_TYPE(expr
),
4093 null_pointer_node
));
4094 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4096 expr
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(expr
),
4097 build3(COND_EXPR
, void_type_node
,
4098 compare
, crash
, NULL_TREE
),
4102 // If the type of EXPR is a recursive pointer type, then we
4103 // need to insert a cast before indirecting.
4104 if (TREE_TYPE(TREE_TYPE(expr
)) == ptr_type_node
)
4106 Type
* pt
= this->expr_
->type()->points_to();
4107 tree ind
= pt
->get_tree(context
->gogo());
4108 expr
= fold_convert_loc(loc
, build_pointer_type(ind
), expr
);
4111 return build_fold_indirect_ref_loc(loc
, expr
);
4119 // Export a unary expression.
4122 Unary_expression::do_export(Export
* exp
) const
4127 exp
->write_c_string("+ ");
4129 case OPERATOR_MINUS
:
4130 exp
->write_c_string("- ");
4133 exp
->write_c_string("! ");
4136 exp
->write_c_string("^ ");
4143 this->expr_
->export_expression(exp
);
4146 // Import a unary expression.
4149 Unary_expression::do_import(Import
* imp
)
4152 switch (imp
->get_char())
4158 op
= OPERATOR_MINUS
;
4169 imp
->require_c_string(" ");
4170 Expression
* expr
= Expression::import_expression(imp
);
4171 return Expression::make_unary(op
, expr
, imp
->location());
4174 // Make a unary expression.
4177 Expression::make_unary(Operator op
, Expression
* expr
, source_location location
)
4179 return new Unary_expression(op
, expr
, location
);
4182 // If this is an indirection through a pointer, return the expression
4183 // being pointed through. Otherwise return this.
4188 if (this->classification_
== EXPRESSION_UNARY
)
4190 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4191 if (ue
->op() == OPERATOR_MULT
)
4192 return ue
->operand();
4197 // Class Binary_expression.
4202 Binary_expression::do_traverse(Traverse
* traverse
)
4204 int t
= Expression::traverse(&this->left_
, traverse
);
4205 if (t
== TRAVERSE_EXIT
)
4206 return TRAVERSE_EXIT
;
4207 return Expression::traverse(&this->right_
, traverse
);
4210 // Compare integer constants according to OP.
4213 Binary_expression::compare_integer(Operator op
, mpz_t left_val
,
4216 int i
= mpz_cmp(left_val
, right_val
);
4221 case OPERATOR_NOTEQ
:
4236 // Compare floating point constants according to OP.
4239 Binary_expression::compare_float(Operator op
, Type
* type
, mpfr_t left_val
,
4244 i
= mpfr_cmp(left_val
, right_val
);
4248 mpfr_init_set(lv
, left_val
, GMP_RNDN
);
4250 mpfr_init_set(rv
, right_val
, GMP_RNDN
);
4251 Float_expression::constrain_float(lv
, type
);
4252 Float_expression::constrain_float(rv
, type
);
4253 i
= mpfr_cmp(lv
, rv
);
4261 case OPERATOR_NOTEQ
:
4276 // Compare complex constants according to OP. Complex numbers may
4277 // only be compared for equality.
4280 Binary_expression::compare_complex(Operator op
, Type
* type
,
4281 mpfr_t left_real
, mpfr_t left_imag
,
4282 mpfr_t right_real
, mpfr_t right_imag
)
4286 is_equal
= (mpfr_cmp(left_real
, right_real
) == 0
4287 && mpfr_cmp(left_imag
, right_imag
) == 0);
4292 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4293 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4296 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4297 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4298 Complex_expression::constrain_complex(lr
, li
, type
);
4299 Complex_expression::constrain_complex(rr
, ri
, type
);
4300 is_equal
= mpfr_cmp(lr
, rr
) == 0 && mpfr_cmp(li
, ri
) == 0;
4310 case OPERATOR_NOTEQ
:
4317 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4318 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4319 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4320 // this could be done, false if not.
4323 Binary_expression::eval_integer(Operator op
, Type
* left_type
, mpz_t left_val
,
4324 Type
* right_type
, mpz_t right_val
,
4325 source_location location
, mpz_t val
)
4327 bool is_shift_op
= false;
4331 case OPERATOR_ANDAND
:
4333 case OPERATOR_NOTEQ
:
4338 // These return boolean values. We should probably handle them
4339 // anyhow in case a type conversion is used on the result.
4342 mpz_add(val
, left_val
, right_val
);
4344 case OPERATOR_MINUS
:
4345 mpz_sub(val
, left_val
, right_val
);
4348 mpz_ior(val
, left_val
, right_val
);
4351 mpz_xor(val
, left_val
, right_val
);
4354 mpz_mul(val
, left_val
, right_val
);
4357 if (mpz_sgn(right_val
) != 0)
4358 mpz_tdiv_q(val
, left_val
, right_val
);
4361 error_at(location
, "division by zero");
4367 if (mpz_sgn(right_val
) != 0)
4368 mpz_tdiv_r(val
, left_val
, right_val
);
4371 error_at(location
, "division by zero");
4376 case OPERATOR_LSHIFT
:
4378 unsigned long shift
= mpz_get_ui(right_val
);
4379 if (mpz_cmp_ui(right_val
, shift
) != 0)
4381 error_at(location
, "shift count overflow");
4385 mpz_mul_2exp(val
, left_val
, shift
);
4390 case OPERATOR_RSHIFT
:
4392 unsigned long shift
= mpz_get_ui(right_val
);
4393 if (mpz_cmp_ui(right_val
, shift
) != 0)
4395 error_at(location
, "shift count overflow");
4399 if (mpz_cmp_ui(left_val
, 0) >= 0)
4400 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4402 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4408 mpz_and(val
, left_val
, right_val
);
4410 case OPERATOR_BITCLEAR
:
4414 mpz_com(tval
, right_val
);
4415 mpz_and(val
, left_val
, tval
);
4423 Type
* type
= left_type
;
4428 else if (type
!= right_type
&& right_type
!= NULL
)
4430 if (type
->is_abstract())
4432 else if (!right_type
->is_abstract())
4434 // This look like a type error which should be diagnosed
4435 // elsewhere. Don't do anything here, to avoid an
4436 // unhelpful chain of error messages.
4442 if (type
!= NULL
&& !type
->is_abstract())
4444 // We have to check the operands too, as we have implicitly
4445 // coerced them to TYPE.
4446 if ((type
!= left_type
4447 && !Integer_expression::check_constant(left_val
, type
, location
))
4449 && type
!= right_type
4450 && !Integer_expression::check_constant(right_val
, type
,
4452 || !Integer_expression::check_constant(val
, type
, location
))
4459 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4460 // Return true if this could be done, false if not.
4463 Binary_expression::eval_float(Operator op
, Type
* left_type
, mpfr_t left_val
,
4464 Type
* right_type
, mpfr_t right_val
,
4465 mpfr_t val
, source_location location
)
4470 case OPERATOR_ANDAND
:
4472 case OPERATOR_NOTEQ
:
4477 // These return boolean values. We should probably handle them
4478 // anyhow in case a type conversion is used on the result.
4481 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4483 case OPERATOR_MINUS
:
4484 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4489 case OPERATOR_BITCLEAR
:
4492 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4495 if (mpfr_zero_p(right_val
))
4496 error_at(location
, "division by zero");
4497 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4501 case OPERATOR_LSHIFT
:
4502 case OPERATOR_RSHIFT
:
4508 Type
* type
= left_type
;
4511 else if (type
!= right_type
&& right_type
!= NULL
)
4513 if (type
->is_abstract())
4515 else if (!right_type
->is_abstract())
4517 // This looks like a type error which should be diagnosed
4518 // elsewhere. Don't do anything here, to avoid an unhelpful
4519 // chain of error messages.
4524 if (type
!= NULL
&& !type
->is_abstract())
4526 if ((type
!= left_type
4527 && !Float_expression::check_constant(left_val
, type
, location
))
4528 || (type
!= right_type
4529 && !Float_expression::check_constant(right_val
, type
,
4531 || !Float_expression::check_constant(val
, type
, location
))
4532 mpfr_set_ui(val
, 0, GMP_RNDN
);
4538 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4539 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4540 // could be done, false if not.
4543 Binary_expression::eval_complex(Operator op
, Type
* left_type
,
4544 mpfr_t left_real
, mpfr_t left_imag
,
4546 mpfr_t right_real
, mpfr_t right_imag
,
4547 mpfr_t real
, mpfr_t imag
,
4548 source_location location
)
4553 case OPERATOR_ANDAND
:
4555 case OPERATOR_NOTEQ
:
4560 // These return boolean values and must be handled differently.
4563 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4564 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4566 case OPERATOR_MINUS
:
4567 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4568 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4573 case OPERATOR_BITCLEAR
:
4577 // You might think that multiplying two complex numbers would
4578 // be simple, and you would be right, until you start to think
4579 // about getting the right answer for infinity. If one
4580 // operand here is infinity and the other is anything other
4581 // than zero or NaN, then we are going to wind up subtracting
4582 // two infinity values. That will give us a NaN, but the
4583 // correct answer is infinity.
4587 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4591 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4595 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4599 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4601 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4602 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4604 // If we get NaN on both sides, check whether it should really
4605 // be infinity. The rule is that if either side of the
4606 // complex number is infinity, then the whole value is
4607 // infinity, even if the other side is NaN. So the only case
4608 // we have to fix is the one in which both sides are NaN.
4609 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4610 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4611 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4613 bool is_infinity
= false;
4617 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4618 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4622 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4623 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4625 // If the left side is infinity, then the result is
4627 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4629 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4630 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4631 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4632 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4635 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4636 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4640 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4641 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4646 // If the right side is infinity, then the result is
4648 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4650 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4651 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4652 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4653 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4656 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4657 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4661 mpfr_set_ui(li
, 0, GMP_RNDN
);
4662 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4667 // If we got an overflow in the intermediate computations,
4668 // then the result is infinity.
4670 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4671 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4675 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4676 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4680 mpfr_set_ui(li
, 0, GMP_RNDN
);
4681 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4685 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4686 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4690 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4691 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4698 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4699 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4700 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4701 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4702 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4703 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4704 mpfr_set_inf(real
, mpfr_sgn(real
));
4705 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4722 // For complex division we want to avoid having an
4723 // intermediate overflow turn the whole result in a NaN. We
4724 // scale the values to try to avoid this.
4726 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4727 error_at(location
, "division by zero");
4733 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4734 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4737 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4741 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4742 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4744 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4746 ilogbw
= mpfr_get_exp(t
);
4747 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4748 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4753 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4754 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4755 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4757 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4758 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4759 mpfr_add(real
, real
, t
, GMP_RNDN
);
4760 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4761 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4763 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4764 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4765 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4766 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4767 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4769 // If we wind up with NaN on both sides, check whether we
4770 // should really have infinity. The rule is that if either
4771 // side of the complex number is infinity, then the whole
4772 // value is infinity, even if the other side is NaN. So the
4773 // only case we have to fix is the one in which both sides are
4775 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4776 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4777 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4779 if (mpfr_zero_p(denom
))
4781 mpfr_set_inf(real
, mpfr_sgn(rr
));
4782 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4783 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4784 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4786 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4787 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4789 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4790 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4793 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4794 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4798 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
4802 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
4804 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4805 mpfr_set_inf(real
, mpfr_sgn(t3
));
4807 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
4808 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
4809 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4810 mpfr_set_inf(imag
, mpfr_sgn(t3
));
4816 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
4817 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
4819 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4820 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
4823 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4824 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
4828 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
4832 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
4834 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4835 mpfr_set_ui(real
, 0, GMP_RNDN
);
4836 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
4838 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
4839 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
4840 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4841 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4842 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
4860 case OPERATOR_LSHIFT
:
4861 case OPERATOR_RSHIFT
:
4867 Type
* type
= left_type
;
4870 else if (type
!= right_type
&& right_type
!= NULL
)
4872 if (type
->is_abstract())
4874 else if (!right_type
->is_abstract())
4876 // This looks like a type error which should be diagnosed
4877 // elsewhere. Don't do anything here, to avoid an unhelpful
4878 // chain of error messages.
4883 if (type
!= NULL
&& !type
->is_abstract())
4885 if ((type
!= left_type
4886 && !Complex_expression::check_constant(left_real
, left_imag
,
4888 || (type
!= right_type
4889 && !Complex_expression::check_constant(right_real
, right_imag
,
4891 || !Complex_expression::check_constant(real
, imag
, type
,
4894 mpfr_set_ui(real
, 0, GMP_RNDN
);
4895 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4902 // Lower a binary expression. We have to evaluate constant
4903 // expressions now, in order to implement Go's unlimited precision
4907 Binary_expression::do_lower(Gogo
*, Named_object
*, int)
4909 source_location location
= this->location();
4910 Operator op
= this->op_
;
4911 Expression
* left
= this->left_
;
4912 Expression
* right
= this->right_
;
4914 const bool is_comparison
= (op
== OPERATOR_EQEQ
4915 || op
== OPERATOR_NOTEQ
4916 || op
== OPERATOR_LT
4917 || op
== OPERATOR_LE
4918 || op
== OPERATOR_GT
4919 || op
== OPERATOR_GE
);
4921 // Integer constant expressions.
4927 mpz_init(right_val
);
4929 if (left
->integer_constant_value(false, left_val
, &left_type
)
4930 && right
->integer_constant_value(false, right_val
, &right_type
))
4932 Expression
* ret
= NULL
;
4933 if (left_type
!= right_type
4934 && left_type
!= NULL
4935 && right_type
!= NULL
4936 && left_type
->base() != right_type
->base()
4937 && op
!= OPERATOR_LSHIFT
4938 && op
!= OPERATOR_RSHIFT
)
4940 // May be a type error--let it be diagnosed later.
4942 else if (is_comparison
)
4944 bool b
= Binary_expression::compare_integer(op
, left_val
,
4946 ret
= Expression::make_cast(Type::lookup_bool_type(),
4947 Expression::make_boolean(b
, location
),
4955 if (Binary_expression::eval_integer(op
, left_type
, left_val
,
4956 right_type
, right_val
,
4959 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
);
4961 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
4963 else if (left_type
== NULL
)
4965 else if (right_type
== NULL
)
4967 else if (!left_type
->is_abstract()
4968 && left_type
->named_type() != NULL
)
4970 else if (!right_type
->is_abstract()
4971 && right_type
->named_type() != NULL
)
4973 else if (!left_type
->is_abstract())
4975 else if (!right_type
->is_abstract())
4977 else if (left_type
->float_type() != NULL
)
4979 else if (right_type
->float_type() != NULL
)
4981 else if (left_type
->complex_type() != NULL
)
4983 else if (right_type
->complex_type() != NULL
)
4987 ret
= Expression::make_integer(&val
, type
, location
);
4995 mpz_clear(right_val
);
4996 mpz_clear(left_val
);
5000 mpz_clear(right_val
);
5001 mpz_clear(left_val
);
5004 // Floating point constant expressions.
5007 mpfr_init(left_val
);
5010 mpfr_init(right_val
);
5012 if (left
->float_constant_value(left_val
, &left_type
)
5013 && right
->float_constant_value(right_val
, &right_type
))
5015 Expression
* ret
= NULL
;
5016 if (left_type
!= right_type
5017 && left_type
!= NULL
5018 && right_type
!= NULL
5019 && left_type
->base() != right_type
->base()
5020 && op
!= OPERATOR_LSHIFT
5021 && op
!= OPERATOR_RSHIFT
)
5023 // May be a type error--let it be diagnosed later.
5025 else if (is_comparison
)
5027 bool b
= Binary_expression::compare_float(op
,
5031 left_val
, right_val
);
5032 ret
= Expression::make_boolean(b
, location
);
5039 if (Binary_expression::eval_float(op
, left_type
, left_val
,
5040 right_type
, right_val
, val
,
5043 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5044 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5046 if (left_type
== NULL
)
5048 else if (right_type
== NULL
)
5050 else if (!left_type
->is_abstract()
5051 && left_type
->named_type() != NULL
)
5053 else if (!right_type
->is_abstract()
5054 && right_type
->named_type() != NULL
)
5056 else if (!left_type
->is_abstract())
5058 else if (!right_type
->is_abstract())
5060 else if (left_type
->float_type() != NULL
)
5062 else if (right_type
->float_type() != NULL
)
5066 ret
= Expression::make_float(&val
, type
, location
);
5074 mpfr_clear(right_val
);
5075 mpfr_clear(left_val
);
5079 mpfr_clear(right_val
);
5080 mpfr_clear(left_val
);
5083 // Complex constant expressions.
5087 mpfr_init(left_real
);
5088 mpfr_init(left_imag
);
5093 mpfr_init(right_real
);
5094 mpfr_init(right_imag
);
5097 if (left
->complex_constant_value(left_real
, left_imag
, &left_type
)
5098 && right
->complex_constant_value(right_real
, right_imag
, &right_type
))
5100 Expression
* ret
= NULL
;
5101 if (left_type
!= right_type
5102 && left_type
!= NULL
5103 && right_type
!= NULL
5104 && left_type
->base() != right_type
->base())
5106 // May be a type error--let it be diagnosed later.
5108 else if (is_comparison
)
5110 bool b
= Binary_expression::compare_complex(op
,
5118 ret
= Expression::make_boolean(b
, location
);
5127 if (Binary_expression::eval_complex(op
, left_type
,
5128 left_real
, left_imag
,
5130 right_real
, right_imag
,
5134 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5135 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5137 if (left_type
== NULL
)
5139 else if (right_type
== NULL
)
5141 else if (!left_type
->is_abstract()
5142 && left_type
->named_type() != NULL
)
5144 else if (!right_type
->is_abstract()
5145 && right_type
->named_type() != NULL
)
5147 else if (!left_type
->is_abstract())
5149 else if (!right_type
->is_abstract())
5151 else if (left_type
->complex_type() != NULL
)
5153 else if (right_type
->complex_type() != NULL
)
5157 ret
= Expression::make_complex(&real
, &imag
, type
,
5166 mpfr_clear(left_real
);
5167 mpfr_clear(left_imag
);
5168 mpfr_clear(right_real
);
5169 mpfr_clear(right_imag
);
5174 mpfr_clear(left_real
);
5175 mpfr_clear(left_imag
);
5176 mpfr_clear(right_real
);
5177 mpfr_clear(right_imag
);
5180 // String constant expressions.
5181 if (op
== OPERATOR_PLUS
5182 && left
->type()->is_string_type()
5183 && right
->type()->is_string_type())
5185 std::string left_string
;
5186 std::string right_string
;
5187 if (left
->string_constant_value(&left_string
)
5188 && right
->string_constant_value(&right_string
))
5189 return Expression::make_string(left_string
+ right_string
, location
);
5195 // Return the integer constant value, if it has one.
5198 Binary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
5204 if (!this->left_
->integer_constant_value(iota_is_constant
, left_val
,
5207 mpz_clear(left_val
);
5212 mpz_init(right_val
);
5214 if (!this->right_
->integer_constant_value(iota_is_constant
, right_val
,
5217 mpz_clear(right_val
);
5218 mpz_clear(left_val
);
5223 if (left_type
!= right_type
5224 && left_type
!= NULL
5225 && right_type
!= NULL
5226 && left_type
->base() != right_type
->base()
5227 && this->op_
!= OPERATOR_RSHIFT
5228 && this->op_
!= OPERATOR_LSHIFT
)
5231 ret
= Binary_expression::eval_integer(this->op_
, left_type
, left_val
,
5232 right_type
, right_val
,
5233 this->location(), val
);
5235 mpz_clear(right_val
);
5236 mpz_clear(left_val
);
5244 // Return the floating point constant value, if it has one.
5247 Binary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
5250 mpfr_init(left_val
);
5252 if (!this->left_
->float_constant_value(left_val
, &left_type
))
5254 mpfr_clear(left_val
);
5259 mpfr_init(right_val
);
5261 if (!this->right_
->float_constant_value(right_val
, &right_type
))
5263 mpfr_clear(right_val
);
5264 mpfr_clear(left_val
);
5269 if (left_type
!= right_type
5270 && left_type
!= NULL
5271 && right_type
!= NULL
5272 && left_type
->base() != right_type
->base())
5275 ret
= Binary_expression::eval_float(this->op_
, left_type
, left_val
,
5276 right_type
, right_val
,
5277 val
, this->location());
5279 mpfr_clear(left_val
);
5280 mpfr_clear(right_val
);
5288 // Return the complex constant value, if it has one.
5291 Binary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
5296 mpfr_init(left_real
);
5297 mpfr_init(left_imag
);
5299 if (!this->left_
->complex_constant_value(left_real
, left_imag
, &left_type
))
5301 mpfr_clear(left_real
);
5302 mpfr_clear(left_imag
);
5308 mpfr_init(right_real
);
5309 mpfr_init(right_imag
);
5311 if (!this->right_
->complex_constant_value(right_real
, right_imag
,
5314 mpfr_clear(left_real
);
5315 mpfr_clear(left_imag
);
5316 mpfr_clear(right_real
);
5317 mpfr_clear(right_imag
);
5322 if (left_type
!= right_type
5323 && left_type
!= NULL
5324 && right_type
!= NULL
5325 && left_type
->base() != right_type
->base())
5328 ret
= Binary_expression::eval_complex(this->op_
, left_type
,
5329 left_real
, left_imag
,
5331 right_real
, right_imag
,
5334 mpfr_clear(left_real
);
5335 mpfr_clear(left_imag
);
5336 mpfr_clear(right_real
);
5337 mpfr_clear(right_imag
);
5345 // Note that the value is being discarded.
5348 Binary_expression::do_discarding_value()
5350 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5351 this->right_
->discarding_value();
5353 this->warn_about_unused_value();
5359 Binary_expression::do_type()
5364 case OPERATOR_ANDAND
:
5366 case OPERATOR_NOTEQ
:
5371 return Type::lookup_bool_type();
5374 case OPERATOR_MINUS
:
5381 case OPERATOR_BITCLEAR
:
5383 Type
* left_type
= this->left_
->type();
5384 Type
* right_type
= this->right_
->type();
5385 if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5387 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5389 else if (!left_type
->is_abstract())
5391 else if (!right_type
->is_abstract())
5393 else if (left_type
->complex_type() != NULL
)
5395 else if (right_type
->complex_type() != NULL
)
5397 else if (left_type
->float_type() != NULL
)
5399 else if (right_type
->float_type() != NULL
)
5405 case OPERATOR_LSHIFT
:
5406 case OPERATOR_RSHIFT
:
5407 return this->left_
->type();
5414 // Set type for a binary expression.
5417 Binary_expression::do_determine_type(const Type_context
* context
)
5419 Type
* tleft
= this->left_
->type();
5420 Type
* tright
= this->right_
->type();
5422 // Both sides should have the same type, except for the shift
5423 // operations. For a comparison, we should ignore the incoming
5426 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5427 || this->op_
== OPERATOR_RSHIFT
);
5429 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5430 || this->op_
== OPERATOR_NOTEQ
5431 || this->op_
== OPERATOR_LT
5432 || this->op_
== OPERATOR_LE
5433 || this->op_
== OPERATOR_GT
5434 || this->op_
== OPERATOR_GE
);
5436 Type_context
subcontext(*context
);
5440 // In a comparison, the context does not determine the types of
5442 subcontext
.type
= NULL
;
5445 // Set the context for the left hand operand.
5448 // The right hand operand plays no role in determining the type
5449 // of the left hand operand. A shift of an abstract integer in
5450 // a string context gets special treatment, which may be a
5452 if (subcontext
.type
!= NULL
5453 && subcontext
.type
->is_string_type()
5454 && tleft
->is_abstract())
5455 error_at(this->location(), "shift of non-integer operand");
5457 else if (!tleft
->is_abstract())
5458 subcontext
.type
= tleft
;
5459 else if (!tright
->is_abstract())
5460 subcontext
.type
= tright
;
5461 else if (subcontext
.type
== NULL
)
5463 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5464 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5465 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5467 // Both sides have an abstract integer, abstract float, or
5468 // abstract complex type. Just let CONTEXT determine
5469 // whether they may remain abstract or not.
5471 else if (tleft
->complex_type() != NULL
)
5472 subcontext
.type
= tleft
;
5473 else if (tright
->complex_type() != NULL
)
5474 subcontext
.type
= tright
;
5475 else if (tleft
->float_type() != NULL
)
5476 subcontext
.type
= tleft
;
5477 else if (tright
->float_type() != NULL
)
5478 subcontext
.type
= tright
;
5480 subcontext
.type
= tleft
;
5483 this->left_
->determine_type(&subcontext
);
5485 // The context for the right hand operand is the same as for the
5486 // left hand operand, except for a shift operator.
5489 subcontext
.type
= Type::lookup_integer_type("uint");
5490 subcontext
.may_be_abstract
= false;
5493 this->right_
->determine_type(&subcontext
);
5496 // Report an error if the binary operator OP does not support TYPE.
5497 // Return whether the operation is OK. This should not be used for
5501 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5502 source_location location
)
5507 case OPERATOR_ANDAND
:
5508 if (!type
->is_boolean_type())
5510 error_at(location
, "expected boolean type");
5516 case OPERATOR_NOTEQ
:
5517 if (type
->integer_type() == NULL
5518 && type
->float_type() == NULL
5519 && type
->complex_type() == NULL
5520 && !type
->is_string_type()
5521 && type
->points_to() == NULL
5522 && !type
->is_nil_type()
5523 && !type
->is_boolean_type()
5524 && type
->interface_type() == NULL
5525 && (type
->array_type() == NULL
5526 || type
->array_type()->length() != NULL
)
5527 && type
->map_type() == NULL
5528 && type
->channel_type() == NULL
5529 && type
->function_type() == NULL
)
5532 ("expected integer, floating, complex, string, pointer, "
5533 "boolean, interface, slice, map, channel, "
5534 "or function type"));
5543 if (type
->integer_type() == NULL
5544 && type
->float_type() == NULL
5545 && !type
->is_string_type())
5547 error_at(location
, "expected integer, floating, or string type");
5553 case OPERATOR_PLUSEQ
:
5554 if (type
->integer_type() == NULL
5555 && type
->float_type() == NULL
5556 && type
->complex_type() == NULL
5557 && !type
->is_string_type())
5560 "expected integer, floating, complex, or string type");
5565 case OPERATOR_MINUS
:
5566 case OPERATOR_MINUSEQ
:
5568 case OPERATOR_MULTEQ
:
5570 case OPERATOR_DIVEQ
:
5571 if (type
->integer_type() == NULL
5572 && type
->float_type() == NULL
5573 && type
->complex_type() == NULL
)
5575 error_at(location
, "expected integer, floating, or complex type");
5581 case OPERATOR_MODEQ
:
5585 case OPERATOR_ANDEQ
:
5587 case OPERATOR_XOREQ
:
5588 case OPERATOR_BITCLEAR
:
5589 case OPERATOR_BITCLEAREQ
:
5590 if (type
->integer_type() == NULL
)
5592 error_at(location
, "expected integer type");
5607 Binary_expression::do_check_types(Gogo
*)
5609 Type
* left_type
= this->left_
->type();
5610 Type
* right_type
= this->right_
->type();
5611 if (left_type
->is_error_type() || right_type
->is_error_type())
5613 this->set_is_error();
5617 if (this->op_
== OPERATOR_EQEQ
5618 || this->op_
== OPERATOR_NOTEQ
5619 || this->op_
== OPERATOR_LT
5620 || this->op_
== OPERATOR_LE
5621 || this->op_
== OPERATOR_GT
5622 || this->op_
== OPERATOR_GE
)
5624 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5625 && !Type::are_assignable(right_type
, left_type
, NULL
))
5627 this->report_error(_("incompatible types in binary expression"));
5630 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5632 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5635 this->set_is_error();
5639 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5641 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5643 this->report_error(_("incompatible types in binary expression"));
5646 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5649 this->set_is_error();
5655 if (left_type
->integer_type() == NULL
)
5656 this->report_error(_("shift of non-integer operand"));
5658 if (!right_type
->is_abstract()
5659 && (right_type
->integer_type() == NULL
5660 || !right_type
->integer_type()->is_unsigned()))
5661 this->report_error(_("shift count not unsigned integer"));
5667 if (this->right_
->integer_constant_value(true, val
, &type
))
5669 if (mpz_sgn(val
) < 0)
5670 this->report_error(_("negative shift count"));
5677 // Get a tree for a binary expression.
5680 Binary_expression::do_get_tree(Translate_context
* context
)
5682 tree left
= this->left_
->get_tree(context
);
5683 tree right
= this->right_
->get_tree(context
);
5685 if (left
== error_mark_node
|| right
== error_mark_node
)
5686 return error_mark_node
;
5688 enum tree_code code
;
5689 bool use_left_type
= true;
5690 bool is_shift_op
= false;
5694 case OPERATOR_NOTEQ
:
5699 return Expression::comparison_tree(context
, this->op_
,
5700 this->left_
->type(), left
,
5701 this->right_
->type(), right
,
5705 code
= TRUTH_ORIF_EXPR
;
5706 use_left_type
= false;
5708 case OPERATOR_ANDAND
:
5709 code
= TRUTH_ANDIF_EXPR
;
5710 use_left_type
= false;
5715 case OPERATOR_MINUS
:
5719 code
= BIT_IOR_EXPR
;
5722 code
= BIT_XOR_EXPR
;
5729 Type
*t
= this->left_
->type();
5730 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5733 code
= TRUNC_DIV_EXPR
;
5737 code
= TRUNC_MOD_EXPR
;
5739 case OPERATOR_LSHIFT
:
5743 case OPERATOR_RSHIFT
:
5748 code
= BIT_AND_EXPR
;
5750 case OPERATOR_BITCLEAR
:
5751 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5752 code
= BIT_AND_EXPR
;
5758 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5760 if (this->left_
->type()->is_string_type())
5762 gcc_assert(this->op_
== OPERATOR_PLUS
);
5763 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5764 static tree string_plus_decl
;
5765 return Gogo::call_builtin(&string_plus_decl
,
5776 tree compute_type
= excess_precision_type(type
);
5777 if (compute_type
!= NULL_TREE
)
5779 left
= ::convert(compute_type
, left
);
5780 right
= ::convert(compute_type
, right
);
5783 tree eval_saved
= NULL_TREE
;
5787 left
= save_expr(left
);
5789 right
= save_expr(right
);
5790 // Make sure the values are evaluated.
5791 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5792 void_type_node
, left
, right
);
5795 tree ret
= fold_build2_loc(this->location(),
5797 compute_type
!= NULL_TREE
? compute_type
: type
,
5800 if (compute_type
!= NULL_TREE
)
5801 ret
= ::convert(type
, ret
);
5803 // In Go, a shift larger than the size of the type is well-defined.
5804 // This is not true in GENERIC, so we need to insert a conditional.
5807 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5808 gcc_assert(this->left_
->type()->integer_type() != NULL
);
5809 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5811 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5812 build_int_cst_type(TREE_TYPE(right
), bits
));
5814 tree overflow_result
= fold_convert_loc(this->location(),
5817 if (this->op_
== OPERATOR_RSHIFT
5818 && !this->left_
->type()->integer_type()->is_unsigned())
5820 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
5821 boolean_type_node
, left
,
5822 fold_convert_loc(this->location(),
5824 integer_zero_node
));
5825 tree neg_one
= fold_build2_loc(this->location(),
5826 MINUS_EXPR
, TREE_TYPE(left
),
5827 fold_convert_loc(this->location(),
5830 fold_convert_loc(this->location(),
5833 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
5834 TREE_TYPE(left
), neg
, neg_one
,
5838 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
5839 compare
, ret
, overflow_result
);
5841 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5842 TREE_TYPE(ret
), eval_saved
, ret
);
5848 // Export a binary expression.
5851 Binary_expression::do_export(Export
* exp
) const
5853 exp
->write_c_string("(");
5854 this->left_
->export_expression(exp
);
5858 exp
->write_c_string(" || ");
5860 case OPERATOR_ANDAND
:
5861 exp
->write_c_string(" && ");
5864 exp
->write_c_string(" == ");
5866 case OPERATOR_NOTEQ
:
5867 exp
->write_c_string(" != ");
5870 exp
->write_c_string(" < ");
5873 exp
->write_c_string(" <= ");
5876 exp
->write_c_string(" > ");
5879 exp
->write_c_string(" >= ");
5882 exp
->write_c_string(" + ");
5884 case OPERATOR_MINUS
:
5885 exp
->write_c_string(" - ");
5888 exp
->write_c_string(" | ");
5891 exp
->write_c_string(" ^ ");
5894 exp
->write_c_string(" * ");
5897 exp
->write_c_string(" / ");
5900 exp
->write_c_string(" % ");
5902 case OPERATOR_LSHIFT
:
5903 exp
->write_c_string(" << ");
5905 case OPERATOR_RSHIFT
:
5906 exp
->write_c_string(" >> ");
5909 exp
->write_c_string(" & ");
5911 case OPERATOR_BITCLEAR
:
5912 exp
->write_c_string(" &^ ");
5917 this->right_
->export_expression(exp
);
5918 exp
->write_c_string(")");
5921 // Import a binary expression.
5924 Binary_expression::do_import(Import
* imp
)
5926 imp
->require_c_string("(");
5928 Expression
* left
= Expression::import_expression(imp
);
5931 if (imp
->match_c_string(" || "))
5936 else if (imp
->match_c_string(" && "))
5938 op
= OPERATOR_ANDAND
;
5941 else if (imp
->match_c_string(" == "))
5946 else if (imp
->match_c_string(" != "))
5948 op
= OPERATOR_NOTEQ
;
5951 else if (imp
->match_c_string(" < "))
5956 else if (imp
->match_c_string(" <= "))
5961 else if (imp
->match_c_string(" > "))
5966 else if (imp
->match_c_string(" >= "))
5971 else if (imp
->match_c_string(" + "))
5976 else if (imp
->match_c_string(" - "))
5978 op
= OPERATOR_MINUS
;
5981 else if (imp
->match_c_string(" | "))
5986 else if (imp
->match_c_string(" ^ "))
5991 else if (imp
->match_c_string(" * "))
5996 else if (imp
->match_c_string(" / "))
6001 else if (imp
->match_c_string(" % "))
6006 else if (imp
->match_c_string(" << "))
6008 op
= OPERATOR_LSHIFT
;
6011 else if (imp
->match_c_string(" >> "))
6013 op
= OPERATOR_RSHIFT
;
6016 else if (imp
->match_c_string(" & "))
6021 else if (imp
->match_c_string(" &^ "))
6023 op
= OPERATOR_BITCLEAR
;
6028 error_at(imp
->location(), "unrecognized binary operator");
6029 return Expression::make_error(imp
->location());
6032 Expression
* right
= Expression::import_expression(imp
);
6034 imp
->require_c_string(")");
6036 return Expression::make_binary(op
, left
, right
, imp
->location());
6039 // Make a binary expression.
6042 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6043 source_location location
)
6045 return new Binary_expression(op
, left
, right
, location
);
6048 // Implement a comparison.
6051 Expression::comparison_tree(Translate_context
* context
, Operator op
,
6052 Type
* left_type
, tree left_tree
,
6053 Type
* right_type
, tree right_tree
,
6054 source_location location
)
6056 enum tree_code code
;
6062 case OPERATOR_NOTEQ
:
6081 if (left_type
->is_string_type() && right_type
->is_string_type())
6083 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
6084 static tree string_compare_decl
;
6085 left_tree
= Gogo::call_builtin(&string_compare_decl
,
6094 right_tree
= build_int_cst_type(integer_type_node
, 0);
6096 else if ((left_type
->interface_type() != NULL
6097 && right_type
->interface_type() == NULL
6098 && !right_type
->is_nil_type())
6099 || (left_type
->interface_type() == NULL
6100 && !left_type
->is_nil_type()
6101 && right_type
->interface_type() != NULL
))
6103 // Comparing an interface value to a non-interface value.
6104 if (left_type
->interface_type() == NULL
)
6106 std::swap(left_type
, right_type
);
6107 std::swap(left_tree
, right_tree
);
6110 // The right operand is not an interface. We need to take its
6111 // address if it is not a pointer.
6114 if (right_type
->points_to() != NULL
)
6116 make_tmp
= NULL_TREE
;
6119 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
6121 make_tmp
= NULL_TREE
;
6122 arg
= build_fold_addr_expr_loc(location
, right_tree
);
6123 if (DECL_P(right_tree
))
6124 TREE_ADDRESSABLE(right_tree
) = 1;
6128 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
6129 get_name(right_tree
));
6130 DECL_IGNORED_P(tmp
) = 0;
6131 DECL_INITIAL(tmp
) = right_tree
;
6132 TREE_ADDRESSABLE(tmp
) = 1;
6133 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6134 SET_EXPR_LOCATION(make_tmp
, location
);
6135 arg
= build_fold_addr_expr_loc(location
, tmp
);
6137 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
6139 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
6141 if (left_type
->interface_type()->is_empty())
6143 static tree empty_interface_value_compare_decl
;
6144 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6146 "__go_empty_interface_value_compare",
6149 TREE_TYPE(left_tree
),
6151 TREE_TYPE(descriptor
),
6155 if (left_tree
== error_mark_node
)
6156 return error_mark_node
;
6157 // This can panic if the type is not comparable.
6158 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6162 static tree interface_value_compare_decl
;
6163 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6165 "__go_interface_value_compare",
6168 TREE_TYPE(left_tree
),
6170 TREE_TYPE(descriptor
),
6174 if (left_tree
== error_mark_node
)
6175 return error_mark_node
;
6176 // This can panic if the type is not comparable.
6177 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6179 right_tree
= build_int_cst_type(integer_type_node
, 0);
6181 if (make_tmp
!= NULL_TREE
)
6182 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6185 else if (left_type
->interface_type() != NULL
6186 && right_type
->interface_type() != NULL
)
6188 if (left_type
->interface_type()->is_empty())
6190 gcc_assert(right_type
->interface_type()->is_empty());
6191 static tree empty_interface_compare_decl
;
6192 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6194 "__go_empty_interface_compare",
6197 TREE_TYPE(left_tree
),
6199 TREE_TYPE(right_tree
),
6201 if (left_tree
== error_mark_node
)
6202 return error_mark_node
;
6203 // This can panic if the type is uncomparable.
6204 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6208 gcc_assert(!right_type
->interface_type()->is_empty());
6209 static tree interface_compare_decl
;
6210 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6212 "__go_interface_compare",
6215 TREE_TYPE(left_tree
),
6217 TREE_TYPE(right_tree
),
6219 if (left_tree
== error_mark_node
)
6220 return error_mark_node
;
6221 // This can panic if the type is uncomparable.
6222 TREE_NOTHROW(interface_compare_decl
) = 0;
6224 right_tree
= build_int_cst_type(integer_type_node
, 0);
6227 if (left_type
->is_nil_type()
6228 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6230 std::swap(left_type
, right_type
);
6231 std::swap(left_tree
, right_tree
);
6234 if (right_type
->is_nil_type())
6236 if (left_type
->array_type() != NULL
6237 && left_type
->array_type()->length() == NULL
)
6239 Array_type
* at
= left_type
->array_type();
6240 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6241 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6243 else if (left_type
->interface_type() != NULL
)
6245 // An interface is nil if the first field is nil.
6246 tree left_type_tree
= TREE_TYPE(left_tree
);
6247 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6248 tree field
= TYPE_FIELDS(left_type_tree
);
6249 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6251 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6255 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6256 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6260 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6261 return error_mark_node
;
6263 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6264 if (CAN_HAVE_LOCATION_P(ret
))
6265 SET_EXPR_LOCATION(ret
, location
);
6269 // Class Bound_method_expression.
6274 Bound_method_expression::do_traverse(Traverse
* traverse
)
6276 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6277 return TRAVERSE_EXIT
;
6278 return Expression::traverse(&this->method_
, traverse
);
6281 // Return the type of a bound method expression. The type of this
6282 // object is really the type of the method with no receiver. We
6283 // should be able to get away with just returning the type of the
6287 Bound_method_expression::do_type()
6289 return this->method_
->type();
6292 // Determine the types of a method expression.
6295 Bound_method_expression::do_determine_type(const Type_context
*)
6297 this->method_
->determine_type_no_context();
6298 Type
* mtype
= this->method_
->type();
6299 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6300 if (fntype
== NULL
|| !fntype
->is_method())
6301 this->expr_
->determine_type_no_context();
6304 Type_context
subcontext(fntype
->receiver()->type(), false);
6305 this->expr_
->determine_type(&subcontext
);
6309 // Check the types of a method expression.
6312 Bound_method_expression::do_check_types(Gogo
*)
6314 Type
* type
= this->method_
->type()->deref();
6316 || type
->function_type() == NULL
6317 || !type
->function_type()->is_method())
6318 this->report_error(_("object is not a method"));
6321 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6322 Type
* etype
= (this->expr_type_
!= NULL
6324 : this->expr_
->type());
6325 etype
= etype
->deref();
6326 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6327 this->report_error(_("method type does not match object type"));
6331 // Get the tree for a method expression. There is no standard tree
6332 // representation for this. The only places it may currently be used
6333 // are in a Call_expression or a Go_statement, which will take it
6334 // apart directly. So this has nothing to do at present.
6337 Bound_method_expression::do_get_tree(Translate_context
*)
6342 // Make a method expression.
6344 Bound_method_expression
*
6345 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6346 source_location location
)
6348 return new Bound_method_expression(expr
, method
, location
);
6351 // Class Builtin_call_expression. This is used for a call to a
6352 // builtin function.
6354 class Builtin_call_expression
: public Call_expression
6357 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6358 bool is_varargs
, source_location location
);
6361 // This overrides Call_expression::do_lower.
6363 do_lower(Gogo
*, Named_object
*, int);
6366 do_is_constant() const;
6369 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6372 do_float_constant_value(mpfr_t
, Type
**) const;
6375 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6381 do_determine_type(const Type_context
*);
6384 do_check_types(Gogo
*);
6389 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6390 this->args()->copy(),
6396 do_get_tree(Translate_context
*);
6399 do_export(Export
*) const;
6402 do_is_recover_call() const;
6405 do_set_recover_arg(Expression
*);
6408 // The builtin functions.
6409 enum Builtin_function_code
6413 // Predeclared builtin functions.
6430 // Builtin functions from the unsafe package.
6443 real_imag_type(Type
*);
6448 // A pointer back to the general IR structure. This avoids a global
6449 // variable, or passing it around everywhere.
6451 // The builtin function being called.
6452 Builtin_function_code code_
;
6455 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6457 Expression_list
* args
,
6459 source_location location
)
6460 : Call_expression(fn
, args
, is_varargs
, location
),
6461 gogo_(gogo
), code_(BUILTIN_INVALID
)
6463 Func_expression
* fnexp
= this->fn()->func_expression();
6464 gcc_assert(fnexp
!= NULL
);
6465 const std::string
& name(fnexp
->named_object()->name());
6466 if (name
== "append")
6467 this->code_
= BUILTIN_APPEND
;
6468 else if (name
== "cap")
6469 this->code_
= BUILTIN_CAP
;
6470 else if (name
== "close")
6471 this->code_
= BUILTIN_CLOSE
;
6472 else if (name
== "closed")
6473 this->code_
= BUILTIN_CLOSED
;
6474 else if (name
== "cmplx")
6475 this->code_
= BUILTIN_CMPLX
;
6476 else if (name
== "copy")
6477 this->code_
= BUILTIN_COPY
;
6478 else if (name
== "imag")
6479 this->code_
= BUILTIN_IMAG
;
6480 else if (name
== "len")
6481 this->code_
= BUILTIN_LEN
;
6482 else if (name
== "make")
6483 this->code_
= BUILTIN_MAKE
;
6484 else if (name
== "new")
6485 this->code_
= BUILTIN_NEW
;
6486 else if (name
== "panic")
6487 this->code_
= BUILTIN_PANIC
;
6488 else if (name
== "print")
6489 this->code_
= BUILTIN_PRINT
;
6490 else if (name
== "println")
6491 this->code_
= BUILTIN_PRINTLN
;
6492 else if (name
== "real")
6493 this->code_
= BUILTIN_REAL
;
6494 else if (name
== "recover")
6495 this->code_
= BUILTIN_RECOVER
;
6496 else if (name
== "Alignof")
6497 this->code_
= BUILTIN_ALIGNOF
;
6498 else if (name
== "Offsetof")
6499 this->code_
= BUILTIN_OFFSETOF
;
6500 else if (name
== "Sizeof")
6501 this->code_
= BUILTIN_SIZEOF
;
6506 // Return whether this is a call to recover. This is a virtual
6507 // function called from the parent class.
6510 Builtin_call_expression::do_is_recover_call() const
6512 if (this->classification() == EXPRESSION_ERROR
)
6514 return this->code_
== BUILTIN_RECOVER
;
6517 // Set the argument for a call to recover.
6520 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6522 const Expression_list
* args
= this->args();
6523 gcc_assert(args
== NULL
|| args
->empty());
6524 Expression_list
* new_args
= new Expression_list();
6525 new_args
->push_back(arg
);
6526 this->set_args(new_args
);
6529 // A traversal class which looks for a call expression.
6531 class Find_call_expression
: public Traverse
6534 Find_call_expression()
6535 : Traverse(traverse_expressions
),
6540 expression(Expression
**);
6544 { return this->found_
; }
6551 Find_call_expression::expression(Expression
** pexpr
)
6553 if ((*pexpr
)->call_expression() != NULL
)
6555 this->found_
= true;
6556 return TRAVERSE_EXIT
;
6558 return TRAVERSE_CONTINUE
;
6561 // Lower a builtin call expression. This turns new and make into
6562 // specific expressions. We also convert to a constant if we can.
6565 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6567 if (this->code_
== BUILTIN_NEW
)
6569 const Expression_list
* args
= this->args();
6570 if (args
== NULL
|| args
->size() < 1)
6571 this->report_error(_("not enough arguments"));
6572 else if (args
->size() > 1)
6573 this->report_error(_("too many arguments"));
6576 Expression
* arg
= args
->front();
6577 if (!arg
->is_type_expression())
6579 error_at(arg
->location(), "expected type");
6580 this->set_is_error();
6583 return Expression::make_allocation(arg
->type(), this->location());
6586 else if (this->code_
== BUILTIN_MAKE
)
6588 const Expression_list
* args
= this->args();
6589 if (args
== NULL
|| args
->size() < 1)
6590 this->report_error(_("not enough arguments"));
6593 Expression
* arg
= args
->front();
6594 if (!arg
->is_type_expression())
6596 error_at(arg
->location(), "expected type");
6597 this->set_is_error();
6601 Expression_list
* newargs
;
6602 if (args
->size() == 1)
6606 newargs
= new Expression_list();
6607 Expression_list::const_iterator p
= args
->begin();
6609 for (; p
!= args
->end(); ++p
)
6610 newargs
->push_back(*p
);
6612 return Expression::make_make(arg
->type(), newargs
,
6617 else if (this->is_constant())
6619 // We can only lower len and cap if there are no function calls
6620 // in the arguments. Otherwise we have to make the call.
6621 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6623 Expression
* arg
= this->one_arg();
6624 if (!arg
->is_constant())
6626 Find_call_expression find_call
;
6627 Expression::traverse(&arg
, &find_call
);
6628 if (find_call
.found())
6636 if (this->integer_constant_value(true, ival
, &type
))
6638 Expression
* ret
= Expression::make_integer(&ival
, type
,
6647 if (this->float_constant_value(rval
, &type
))
6649 Expression
* ret
= Expression::make_float(&rval
, type
,
6657 if (this->complex_constant_value(rval
, imag
, &type
))
6659 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6668 else if (this->code_
== BUILTIN_RECOVER
)
6670 if (function
!= NULL
)
6671 function
->func_value()->set_calls_recover();
6674 // Calling recover outside of a function always returns the
6675 // nil empty interface.
6676 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6677 return Expression::make_cast(eface
,
6678 Expression::make_nil(this->location()),
6682 else if (this->code_
== BUILTIN_APPEND
)
6684 // Lower the varargs.
6685 const Expression_list
* args
= this->args();
6686 if (args
== NULL
|| args
->empty())
6688 Type
* slice_type
= args
->front()->type();
6689 if (!slice_type
->is_open_array_type())
6691 error_at(args
->front()->location(), "argument 1 must be a slice");
6692 this->set_is_error();
6695 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6701 // Return the type of the real or imag functions, given the type of
6702 // the argument. We need to map complex to float, complex64 to
6703 // float32, and complex128 to float64, so it has to be done by name.
6704 // This returns NULL if it can't figure out the type.
6707 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6709 if (arg_type
== NULL
|| arg_type
->is_abstract())
6711 Named_type
* nt
= arg_type
->named_type();
6714 while (nt
->real_type()->named_type() != NULL
)
6715 nt
= nt
->real_type()->named_type();
6716 if (nt
->name() == "complex")
6717 return Type::lookup_float_type("float");
6718 else if (nt
->name() == "complex64")
6719 return Type::lookup_float_type("float32");
6720 else if (nt
->name() == "complex128")
6721 return Type::lookup_float_type("float64");
6726 // Return the type of the cmplx function, given the type of one of the
6727 // argments. Like real_imag_type, we have to map by name.
6730 Builtin_call_expression::cmplx_type(Type
* arg_type
)
6732 if (arg_type
== NULL
|| arg_type
->is_abstract())
6734 Named_type
* nt
= arg_type
->named_type();
6737 while (nt
->real_type()->named_type() != NULL
)
6738 nt
= nt
->real_type()->named_type();
6739 if (nt
->name() == "float")
6740 return Type::lookup_complex_type("complex");
6741 else if (nt
->name() == "float32")
6742 return Type::lookup_complex_type("complex64");
6743 else if (nt
->name() == "float64")
6744 return Type::lookup_complex_type("complex128");
6749 // Return a single argument, or NULL if there isn't one.
6752 Builtin_call_expression::one_arg() const
6754 const Expression_list
* args
= this->args();
6755 if (args
->size() != 1)
6757 return args
->front();
6760 // Return whether this is constant: len of a string, or len or cap of
6761 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6764 Builtin_call_expression::do_is_constant() const
6766 switch (this->code_
)
6771 Expression
* arg
= this->one_arg();
6774 Type
* arg_type
= arg
->type();
6776 if (arg_type
->points_to() != NULL
6777 && arg_type
->points_to()->array_type() != NULL
6778 && !arg_type
->points_to()->is_open_array_type())
6779 arg_type
= arg_type
->points_to();
6781 if (arg_type
->array_type() != NULL
6782 && arg_type
->array_type()->length() != NULL
)
6783 return arg_type
->array_type()->length()->is_constant();
6785 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6786 return arg
->is_constant();
6790 case BUILTIN_SIZEOF
:
6791 case BUILTIN_ALIGNOF
:
6792 return this->one_arg() != NULL
;
6794 case BUILTIN_OFFSETOF
:
6796 Expression
* arg
= this->one_arg();
6799 return arg
->field_reference_expression() != NULL
;
6804 const Expression_list
* args
= this->args();
6805 if (args
!= NULL
&& args
->size() == 2)
6806 return args
->front()->is_constant() && args
->back()->is_constant();
6813 Expression
* arg
= this->one_arg();
6814 return arg
!= NULL
&& arg
->is_constant();
6824 // Return an integer constant value if possible.
6827 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
6831 if (this->code_
== BUILTIN_LEN
6832 || this->code_
== BUILTIN_CAP
)
6834 Expression
* arg
= this->one_arg();
6837 Type
* arg_type
= arg
->type();
6839 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6842 if (arg
->string_constant_value(&sval
))
6844 mpz_set_ui(val
, sval
.length());
6845 *ptype
= Type::lookup_integer_type("int");
6850 if (arg_type
->points_to() != NULL
6851 && arg_type
->points_to()->array_type() != NULL
6852 && !arg_type
->points_to()->is_open_array_type())
6853 arg_type
= arg_type
->points_to();
6855 if (arg_type
->array_type() != NULL
6856 && arg_type
->array_type()->length() != NULL
)
6858 Expression
* e
= arg_type
->array_type()->length();
6859 if (e
->integer_constant_value(iota_is_constant
, val
, ptype
))
6861 *ptype
= Type::lookup_integer_type("int");
6866 else if (this->code_
== BUILTIN_SIZEOF
6867 || this->code_
== BUILTIN_ALIGNOF
)
6869 Expression
* arg
= this->one_arg();
6872 Type
* arg_type
= arg
->type();
6873 if (arg_type
->is_error_type() || arg_type
->is_undefined())
6875 if (arg_type
->is_abstract())
6877 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
6878 unsigned long val_long
;
6879 if (this->code_
== BUILTIN_SIZEOF
)
6881 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
6882 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
6883 if (TREE_INT_CST_HIGH(type_size
) != 0)
6885 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
6886 val_long
= static_cast<unsigned long>(val_wide
);
6887 if (val_long
!= val_wide
)
6890 else if (this->code_
== BUILTIN_ALIGNOF
)
6892 if (arg
->field_reference_expression() == NULL
)
6893 val_long
= go_type_alignment(arg_type_tree
);
6896 // Calling unsafe.Alignof(s.f) returns the alignment of
6897 // the type of f when it is used as a field in a struct.
6898 val_long
= go_field_alignment(arg_type_tree
);
6903 mpz_set_ui(val
, val_long
);
6907 else if (this->code_
== BUILTIN_OFFSETOF
)
6909 Expression
* arg
= this->one_arg();
6912 Field_reference_expression
* farg
= arg
->field_reference_expression();
6915 Expression
* struct_expr
= farg
->expr();
6916 Type
* st
= struct_expr
->type();
6917 if (st
->struct_type() == NULL
)
6919 tree struct_tree
= st
->get_tree(this->gogo_
);
6920 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
6921 tree field
= TYPE_FIELDS(struct_tree
);
6922 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
6924 field
= DECL_CHAIN(field
);
6925 gcc_assert(field
!= NULL_TREE
);
6927 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
6928 if (offset_wide
< 0)
6930 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
6931 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
6933 mpz_set_ui(val
, offset_long
);
6939 // Return a floating point constant value if possible.
6942 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
6945 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
6947 Expression
* arg
= this->one_arg();
6958 if (arg
->complex_constant_value(real
, imag
, &type
))
6960 if (this->code_
== BUILTIN_REAL
)
6961 mpfr_set(val
, real
, GMP_RNDN
);
6963 mpfr_set(val
, imag
, GMP_RNDN
);
6964 *ptype
= Builtin_call_expression::real_imag_type(type
);
6976 // Return a complex constant value if possible.
6979 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
6982 if (this->code_
== BUILTIN_CMPLX
)
6984 const Expression_list
* args
= this->args();
6985 if (args
== NULL
|| args
->size() != 2)
6991 if (!args
->front()->float_constant_value(r
, &rtype
))
7002 if (args
->back()->float_constant_value(i
, &itype
)
7003 && Type::are_identical(rtype
, itype
, false, NULL
))
7005 mpfr_set(real
, r
, GMP_RNDN
);
7006 mpfr_set(imag
, i
, GMP_RNDN
);
7007 *ptype
= Builtin_call_expression::cmplx_type(rtype
);
7023 Builtin_call_expression::do_type()
7025 switch (this->code_
)
7027 case BUILTIN_INVALID
:
7034 const Expression_list
* args
= this->args();
7035 if (args
== NULL
|| args
->empty())
7036 return Type::make_error_type();
7037 return Type::make_pointer_type(args
->front()->type());
7043 case BUILTIN_ALIGNOF
:
7044 case BUILTIN_OFFSETOF
:
7045 case BUILTIN_SIZEOF
:
7046 return Type::lookup_integer_type("int");
7051 case BUILTIN_PRINTLN
:
7052 return Type::make_void_type();
7054 case BUILTIN_CLOSED
:
7055 return Type::lookup_bool_type();
7057 case BUILTIN_RECOVER
:
7058 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7060 case BUILTIN_APPEND
:
7062 const Expression_list
* args
= this->args();
7063 if (args
== NULL
|| args
->empty())
7064 return Type::make_error_type();
7065 return args
->front()->type();
7071 Expression
* arg
= this->one_arg();
7073 return Type::make_error_type();
7074 Type
* t
= arg
->type();
7075 if (t
->is_abstract())
7076 t
= t
->make_non_abstract_type();
7077 t
= Builtin_call_expression::real_imag_type(t
);
7079 t
= Type::make_error_type();
7085 const Expression_list
* args
= this->args();
7086 if (args
== NULL
|| args
->size() != 2)
7087 return Type::make_error_type();
7088 Type
* t
= args
->front()->type();
7089 if (t
->is_abstract())
7091 t
= args
->back()->type();
7092 if (t
->is_abstract())
7093 t
= t
->make_non_abstract_type();
7095 t
= Builtin_call_expression::cmplx_type(t
);
7097 t
= Type::make_error_type();
7103 // Determine the type.
7106 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7108 this->fn()->determine_type_no_context();
7110 const Expression_list
* args
= this->args();
7113 Type
* arg_type
= NULL
;
7114 switch (this->code_
)
7117 case BUILTIN_PRINTLN
:
7118 // Do not force a large integer constant to "int".
7124 arg_type
= Builtin_call_expression::cmplx_type(context
->type
);
7130 // For the cmplx function the type of one operand can
7131 // determine the type of the other, as in a binary expression.
7132 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7133 if (args
!= NULL
&& args
->size() == 2)
7135 Type
* t1
= args
->front()->type();
7136 Type
* t2
= args
->front()->type();
7137 if (!t1
->is_abstract())
7139 else if (!t2
->is_abstract())
7153 for (Expression_list::const_iterator pa
= args
->begin();
7157 Type_context subcontext
;
7158 subcontext
.type
= arg_type
;
7162 // We want to print large constants, we so can't just
7163 // use the appropriate nonabstract type. Use uint64 for
7164 // an integer if we know it is nonnegative, otherwise
7165 // use int64 for a integer, otherwise use float64 for a
7166 // float or complex128 for a complex.
7167 Type
* want_type
= NULL
;
7168 Type
* atype
= (*pa
)->type();
7169 if (atype
->is_abstract())
7171 if (atype
->integer_type() != NULL
)
7176 if (this->integer_constant_value(true, val
, &dummy
)
7177 && mpz_sgn(val
) >= 0)
7178 want_type
= Type::lookup_integer_type("uint64");
7180 want_type
= Type::lookup_integer_type("int64");
7183 else if (atype
->float_type() != NULL
)
7184 want_type
= Type::lookup_float_type("float64");
7185 else if (atype
->complex_type() != NULL
)
7186 want_type
= Type::lookup_complex_type("complex128");
7187 else if (atype
->is_abstract_string_type())
7188 want_type
= Type::lookup_string_type();
7189 else if (atype
->is_abstract_boolean_type())
7190 want_type
= Type::lookup_bool_type();
7193 subcontext
.type
= want_type
;
7197 (*pa
)->determine_type(&subcontext
);
7202 // If there is exactly one argument, return true. Otherwise give an
7203 // error message and return false.
7206 Builtin_call_expression::check_one_arg()
7208 const Expression_list
* args
= this->args();
7209 if (args
== NULL
|| args
->size() < 1)
7211 this->report_error(_("not enough arguments"));
7214 else if (args
->size() > 1)
7216 this->report_error(_("too many arguments"));
7219 if (args
->front()->is_error_expression()
7220 || args
->front()->type()->is_error_type()
7221 || args
->front()->type()->is_undefined())
7223 this->set_is_error();
7229 // Check argument types for a builtin function.
7232 Builtin_call_expression::do_check_types(Gogo
*)
7234 switch (this->code_
)
7236 case BUILTIN_INVALID
:
7244 // The single argument may be either a string or an array or a
7245 // map or a channel, or a pointer to a closed array.
7246 if (this->check_one_arg())
7248 Type
* arg_type
= this->one_arg()->type();
7249 if (arg_type
->points_to() != NULL
7250 && arg_type
->points_to()->array_type() != NULL
7251 && !arg_type
->points_to()->is_open_array_type())
7252 arg_type
= arg_type
->points_to();
7253 if (this->code_
== BUILTIN_CAP
)
7255 if (!arg_type
->is_error_type()
7256 && arg_type
->array_type() == NULL
7257 && arg_type
->channel_type() == NULL
)
7258 this->report_error(_("argument must be array or slice "
7263 if (!arg_type
->is_error_type()
7264 && !arg_type
->is_string_type()
7265 && arg_type
->array_type() == NULL
7266 && arg_type
->map_type() == NULL
7267 && arg_type
->channel_type() == NULL
)
7268 this->report_error(_("argument must be string or "
7269 "array or slice or map or channel"));
7276 case BUILTIN_PRINTLN
:
7278 const Expression_list
* args
= this->args();
7281 if (this->code_
== BUILTIN_PRINT
)
7282 warning_at(this->location(), 0,
7283 "no arguments for builtin function %<%s%>",
7284 (this->code_
== BUILTIN_PRINT
7290 for (Expression_list::const_iterator p
= args
->begin();
7294 Type
* type
= (*p
)->type();
7295 if (type
->is_error_type()
7296 || type
->is_string_type()
7297 || type
->integer_type() != NULL
7298 || type
->float_type() != NULL
7299 || type
->complex_type() != NULL
7300 || type
->is_boolean_type()
7301 || type
->points_to() != NULL
7302 || type
->interface_type() != NULL
7303 || type
->channel_type() != NULL
7304 || type
->map_type() != NULL
7305 || type
->function_type() != NULL
7306 || type
->is_open_array_type())
7309 this->report_error(_("unsupported argument type to "
7310 "builtin function"));
7317 case BUILTIN_CLOSED
:
7318 if (this->check_one_arg())
7320 if (this->one_arg()->type()->channel_type() == NULL
)
7321 this->report_error(_("argument must be channel"));
7326 case BUILTIN_SIZEOF
:
7327 case BUILTIN_ALIGNOF
:
7328 this->check_one_arg();
7331 case BUILTIN_RECOVER
:
7332 if (this->args() != NULL
&& !this->args()->empty())
7333 this->report_error(_("too many arguments"));
7336 case BUILTIN_OFFSETOF
:
7337 if (this->check_one_arg())
7339 Expression
* arg
= this->one_arg();
7340 if (arg
->field_reference_expression() == NULL
)
7341 this->report_error(_("argument must be a field reference"));
7347 const Expression_list
* args
= this->args();
7348 if (args
== NULL
|| args
->size() < 2)
7350 this->report_error(_("not enough arguments"));
7353 else if (args
->size() > 2)
7355 this->report_error(_("too many arguments"));
7358 Type
* arg1_type
= args
->front()->type();
7359 Type
* arg2_type
= args
->back()->type();
7360 if (arg1_type
->is_error_type() || arg2_type
->is_error_type())
7364 if (arg1_type
->is_open_array_type())
7365 e1
= arg1_type
->array_type()->element_type();
7368 this->report_error(_("left argument must be a slice"));
7373 if (arg2_type
->is_open_array_type())
7374 e2
= arg2_type
->array_type()->element_type();
7375 else if (arg2_type
->is_string_type())
7376 e2
= Type::lookup_integer_type("uint8");
7379 this->report_error(_("right argument must be a slice or a string"));
7383 if (!Type::are_identical(e1
, e2
, true, NULL
))
7384 this->report_error(_("element types must be the same"));
7388 case BUILTIN_APPEND
:
7390 const Expression_list
* args
= this->args();
7391 if (args
== NULL
|| args
->size() < 2)
7393 this->report_error(_("not enough arguments"));
7396 if (args
->size() > 2)
7398 this->report_error(_("too many arguments"));
7402 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7406 this->report_error(_("arguments 1 and 2 have different types"));
7409 error_at(this->location(),
7410 "arguments 1 and 2 have different types (%s)",
7412 this->set_is_error();
7420 if (this->check_one_arg())
7422 if (this->one_arg()->type()->complex_type() == NULL
)
7423 this->report_error(_("argument must have complex type"));
7429 const Expression_list
* args
= this->args();
7430 if (args
== NULL
|| args
->size() < 2)
7431 this->report_error(_("not enough arguments"));
7432 else if (args
->size() > 2)
7433 this->report_error(_("too many arguments"));
7434 else if (args
->front()->is_error_expression()
7435 || args
->front()->type()->is_error_type()
7436 || args
->back()->is_error_expression()
7437 || args
->back()->type()->is_error_type())
7438 this->set_is_error();
7439 else if (!Type::are_identical(args
->front()->type(),
7440 args
->back()->type(), true, NULL
))
7441 this->report_error(_("cmplx arguments must have identical types"));
7442 else if (args
->front()->type()->float_type() == NULL
)
7443 this->report_error(_("cmplx arguments must have "
7444 "floating-point type"));
7453 // Return the tree for a builtin function.
7456 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7458 Gogo
* gogo
= context
->gogo();
7459 source_location location
= this->location();
7460 switch (this->code_
)
7462 case BUILTIN_INVALID
:
7470 const Expression_list
* args
= this->args();
7471 gcc_assert(args
!= NULL
&& args
->size() == 1);
7472 Expression
* arg
= *args
->begin();
7473 Type
* arg_type
= arg
->type();
7474 tree arg_tree
= arg
->get_tree(context
);
7475 if (arg_tree
== error_mark_node
)
7476 return error_mark_node
;
7478 if (arg_type
->points_to() != NULL
)
7480 arg_type
= arg_type
->points_to();
7481 gcc_assert(arg_type
->array_type() != NULL
7482 && !arg_type
->is_open_array_type());
7483 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7484 arg_tree
= build_fold_indirect_ref(arg_tree
);
7488 if (this->code_
== BUILTIN_LEN
)
7490 if (arg_type
->is_string_type())
7491 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7492 else if (arg_type
->array_type() != NULL
)
7493 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7494 else if (arg_type
->map_type() != NULL
)
7496 static tree map_len_fndecl
;
7497 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7502 arg_type
->get_tree(gogo
),
7505 else if (arg_type
->channel_type() != NULL
)
7507 static tree chan_len_fndecl
;
7508 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7513 arg_type
->get_tree(gogo
),
7521 if (arg_type
->array_type() != NULL
)
7522 val_tree
= arg_type
->array_type()->capacity_tree(gogo
, arg_tree
);
7523 else if (arg_type
->channel_type() != NULL
)
7525 static tree chan_cap_fndecl
;
7526 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7531 arg_type
->get_tree(gogo
),
7538 if (val_tree
== error_mark_node
)
7539 return error_mark_node
;
7541 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7542 if (type_tree
== TREE_TYPE(val_tree
))
7545 return fold(convert_to_integer(type_tree
, val_tree
));
7549 case BUILTIN_PRINTLN
:
7551 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7552 tree stmt_list
= NULL_TREE
;
7554 const Expression_list
* call_args
= this->args();
7555 if (call_args
!= NULL
)
7557 for (Expression_list::const_iterator p
= call_args
->begin();
7558 p
!= call_args
->end();
7561 if (is_ln
&& p
!= call_args
->begin())
7563 static tree print_space_fndecl
;
7564 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7569 if (call
== error_mark_node
)
7570 return error_mark_node
;
7571 append_to_statement_list(call
, &stmt_list
);
7574 Type
* type
= (*p
)->type();
7576 tree arg
= (*p
)->get_tree(context
);
7577 if (arg
== error_mark_node
)
7578 return error_mark_node
;
7582 if (type
->is_string_type())
7584 static tree print_string_fndecl
;
7585 pfndecl
= &print_string_fndecl
;
7586 fnname
= "__go_print_string";
7588 else if (type
->integer_type() != NULL
7589 && type
->integer_type()->is_unsigned())
7591 static tree print_uint64_fndecl
;
7592 pfndecl
= &print_uint64_fndecl
;
7593 fnname
= "__go_print_uint64";
7594 Type
* itype
= Type::lookup_integer_type("uint64");
7595 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7598 else if (type
->integer_type() != NULL
)
7600 static tree print_int64_fndecl
;
7601 pfndecl
= &print_int64_fndecl
;
7602 fnname
= "__go_print_int64";
7603 Type
* itype
= Type::lookup_integer_type("int64");
7604 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7607 else if (type
->float_type() != NULL
)
7609 static tree print_double_fndecl
;
7610 pfndecl
= &print_double_fndecl
;
7611 fnname
= "__go_print_double";
7612 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7614 else if (type
->complex_type() != NULL
)
7616 static tree print_complex_fndecl
;
7617 pfndecl
= &print_complex_fndecl
;
7618 fnname
= "__go_print_complex";
7619 arg
= fold_convert_loc(location
, complex_double_type_node
,
7622 else if (type
->is_boolean_type())
7624 static tree print_bool_fndecl
;
7625 pfndecl
= &print_bool_fndecl
;
7626 fnname
= "__go_print_bool";
7628 else if (type
->points_to() != NULL
7629 || type
->channel_type() != NULL
7630 || type
->map_type() != NULL
7631 || type
->function_type() != NULL
)
7633 static tree print_pointer_fndecl
;
7634 pfndecl
= &print_pointer_fndecl
;
7635 fnname
= "__go_print_pointer";
7636 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7638 else if (type
->interface_type() != NULL
)
7640 if (type
->interface_type()->is_empty())
7642 static tree print_empty_interface_fndecl
;
7643 pfndecl
= &print_empty_interface_fndecl
;
7644 fnname
= "__go_print_empty_interface";
7648 static tree print_interface_fndecl
;
7649 pfndecl
= &print_interface_fndecl
;
7650 fnname
= "__go_print_interface";
7653 else if (type
->is_open_array_type())
7655 static tree print_slice_fndecl
;
7656 pfndecl
= &print_slice_fndecl
;
7657 fnname
= "__go_print_slice";
7662 tree call
= Gogo::call_builtin(pfndecl
,
7669 if (call
== error_mark_node
)
7670 return error_mark_node
;
7671 append_to_statement_list(call
, &stmt_list
);
7677 static tree print_nl_fndecl
;
7678 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7683 if (call
== error_mark_node
)
7684 return error_mark_node
;
7685 append_to_statement_list(call
, &stmt_list
);
7693 const Expression_list
* args
= this->args();
7694 gcc_assert(args
!= NULL
&& args
->size() == 1);
7695 Expression
* arg
= args
->front();
7696 tree arg_tree
= arg
->get_tree(context
);
7697 if (arg_tree
== error_mark_node
)
7698 return error_mark_node
;
7699 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7700 arg_tree
= Expression::convert_for_assignment(context
, empty
,
7702 arg_tree
, location
);
7703 static tree panic_fndecl
;
7704 tree call
= Gogo::call_builtin(&panic_fndecl
,
7709 TREE_TYPE(arg_tree
),
7711 if (call
== error_mark_node
)
7712 return error_mark_node
;
7713 // This function will throw an exception.
7714 TREE_NOTHROW(panic_fndecl
) = 0;
7715 // This function will not return.
7716 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
7720 case BUILTIN_RECOVER
:
7722 // The argument is set when building recover thunks. It's a
7723 // boolean value which is true if we can recover a value now.
7724 const Expression_list
* args
= this->args();
7725 gcc_assert(args
!= NULL
&& args
->size() == 1);
7726 Expression
* arg
= args
->front();
7727 tree arg_tree
= arg
->get_tree(context
);
7728 if (arg_tree
== error_mark_node
)
7729 return error_mark_node
;
7731 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7732 tree empty_tree
= empty
->get_tree(context
->gogo());
7734 Type
* nil_type
= Type::make_nil_type();
7735 Expression
* nil
= Expression::make_nil(location
);
7736 tree nil_tree
= nil
->get_tree(context
);
7737 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
7743 // We need to handle a deferred call to recover specially,
7744 // because it changes whether it can recover a panic or not.
7745 // See test7 in test/recover1.go.
7747 if (this->is_deferred())
7749 static tree deferred_recover_fndecl
;
7750 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
7752 "__go_deferred_recover",
7758 static tree recover_fndecl
;
7759 call
= Gogo::call_builtin(&recover_fndecl
,
7765 if (call
== error_mark_node
)
7766 return error_mark_node
;
7767 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
7768 call
, empty_nil_tree
);
7772 case BUILTIN_CLOSED
:
7774 const Expression_list
* args
= this->args();
7775 gcc_assert(args
!= NULL
&& args
->size() == 1);
7776 Expression
* arg
= args
->front();
7777 tree arg_tree
= arg
->get_tree(context
);
7778 if (arg_tree
== error_mark_node
)
7779 return error_mark_node
;
7780 if (this->code_
== BUILTIN_CLOSE
)
7782 static tree close_fndecl
;
7783 return Gogo::call_builtin(&close_fndecl
,
7785 "__go_builtin_close",
7788 TREE_TYPE(arg_tree
),
7793 static tree closed_fndecl
;
7794 return Gogo::call_builtin(&closed_fndecl
,
7796 "__go_builtin_closed",
7799 TREE_TYPE(arg_tree
),
7804 case BUILTIN_SIZEOF
:
7805 case BUILTIN_OFFSETOF
:
7806 case BUILTIN_ALIGNOF
:
7811 bool b
= this->integer_constant_value(true, val
, &dummy
);
7813 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
7814 tree ret
= Expression::integer_constant_tree(val
, type
);
7821 const Expression_list
* args
= this->args();
7822 gcc_assert(args
!= NULL
&& args
->size() == 2);
7823 Expression
* arg1
= args
->front();
7824 Expression
* arg2
= args
->back();
7826 tree arg1_tree
= arg1
->get_tree(context
);
7827 tree arg2_tree
= arg2
->get_tree(context
);
7828 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7829 return error_mark_node
;
7831 Type
* arg1_type
= arg1
->type();
7832 Array_type
* at
= arg1_type
->array_type();
7833 arg1_tree
= save_expr(arg1_tree
);
7834 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
7835 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
7836 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
7837 return error_mark_node
;
7839 Type
* arg2_type
= arg2
->type();
7842 if (arg2_type
->is_open_array_type())
7844 at
= arg2_type
->array_type();
7845 arg2_tree
= save_expr(arg2_tree
);
7846 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7847 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7851 arg2_tree
= save_expr(arg2_tree
);
7852 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
7853 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
7855 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7856 return error_mark_node
;
7858 arg1_len
= save_expr(arg1_len
);
7859 arg2_len
= save_expr(arg2_len
);
7860 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
7861 fold_build2_loc(location
, LT_EXPR
,
7863 arg1_len
, arg2_len
),
7864 arg1_len
, arg2_len
);
7865 len
= save_expr(len
);
7867 Type
* element_type
= at
->element_type();
7868 tree element_type_tree
= element_type
->get_tree(gogo
);
7869 if (element_type_tree
== error_mark_node
)
7870 return error_mark_node
;
7871 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7872 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
7874 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
7875 TREE_TYPE(element_size
),
7876 bytecount
, element_size
);
7877 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
7879 arg1_val
= fold_convert_loc(location
, ptr_type_node
, arg1_val
);
7880 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7882 static tree copy_fndecl
;
7883 tree call
= Gogo::call_builtin(©_fndecl
,
7894 if (call
== error_mark_node
)
7895 return error_mark_node
;
7897 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
7901 case BUILTIN_APPEND
:
7903 const Expression_list
* args
= this->args();
7904 gcc_assert(args
!= NULL
&& args
->size() == 2);
7905 Expression
* arg1
= args
->front();
7906 Expression
* arg2
= args
->back();
7908 Array_type
* at
= arg1
->type()->array_type();
7909 Type
* element_type
= at
->element_type();
7911 tree arg1_tree
= arg1
->get_tree(context
);
7912 tree arg2_tree
= arg2
->get_tree(context
);
7913 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7914 return error_mark_node
;
7916 Array_type
* at2
= arg2
->type()->array_type();
7917 arg2_tree
= save_expr(arg2_tree
);
7918 tree arg2_val
= at2
->value_pointer_tree(gogo
, arg2_tree
);
7919 tree arg2_len
= at2
->length_tree(gogo
, arg2_tree
);
7920 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7921 return error_mark_node
;
7922 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7923 arg2_len
= fold_convert_loc(location
, size_type_node
, arg2_len
);
7925 tree element_type_tree
= element_type
->get_tree(gogo
);
7926 if (element_type_tree
== error_mark_node
)
7927 return error_mark_node
;
7928 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7929 element_size
= fold_convert_loc(location
, size_type_node
,
7932 // We rebuild the decl each time since the slice types may
7934 tree append_fndecl
= NULL_TREE
;
7935 return Gogo::call_builtin(&append_fndecl
,
7939 TREE_TYPE(arg1_tree
),
7940 TREE_TYPE(arg1_tree
),
7953 const Expression_list
* args
= this->args();
7954 gcc_assert(args
!= NULL
&& args
->size() == 1);
7955 Expression
* arg
= args
->front();
7956 tree arg_tree
= arg
->get_tree(context
);
7957 if (arg_tree
== error_mark_node
)
7958 return error_mark_node
;
7959 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
7960 if (this->code_
== BUILTIN_REAL
)
7961 return fold_build1_loc(location
, REALPART_EXPR
,
7962 TREE_TYPE(TREE_TYPE(arg_tree
)),
7965 return fold_build1_loc(location
, IMAGPART_EXPR
,
7966 TREE_TYPE(TREE_TYPE(arg_tree
)),
7972 const Expression_list
* args
= this->args();
7973 gcc_assert(args
!= NULL
&& args
->size() == 2);
7974 tree r
= args
->front()->get_tree(context
);
7975 tree i
= args
->back()->get_tree(context
);
7976 if (r
== error_mark_node
|| i
== error_mark_node
)
7977 return error_mark_node
;
7978 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
7979 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
7980 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
7981 return fold_build2_loc(location
, COMPLEX_EXPR
,
7982 build_complex_type(TREE_TYPE(r
)),
7991 // We have to support exporting a builtin call expression, because
7992 // code can set a constant to the result of a builtin expression.
7995 Builtin_call_expression::do_export(Export
* exp
) const
8002 if (this->integer_constant_value(true, val
, &dummy
))
8004 Integer_expression::export_integer(exp
, val
);
8013 if (this->float_constant_value(fval
, &dummy
))
8015 Float_expression::export_float(exp
, fval
);
8027 if (this->complex_constant_value(real
, imag
, &dummy
))
8029 Complex_expression::export_complex(exp
, real
, imag
);
8038 error_at(this->location(), "value is not constant");
8042 // A trailing space lets us reliably identify the end of the number.
8043 exp
->write_c_string(" ");
8046 // Class Call_expression.
8051 Call_expression::do_traverse(Traverse
* traverse
)
8053 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8054 return TRAVERSE_EXIT
;
8055 if (this->args_
!= NULL
)
8057 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8058 return TRAVERSE_EXIT
;
8060 return TRAVERSE_CONTINUE
;
8063 // Lower a call statement.
8066 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
8068 // A type case can look like a function call.
8069 if (this->fn_
->is_type_expression()
8070 && this->args_
!= NULL
8071 && this->args_
->size() == 1)
8072 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8075 // Recognize a call to a builtin function.
8076 Func_expression
* fne
= this->fn_
->func_expression();
8078 && fne
->named_object()->is_function_declaration()
8079 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
8080 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8081 this->is_varargs_
, this->location());
8083 // Handle an argument which is a call to a function which returns
8084 // multiple results.
8085 if (this->args_
!= NULL
8086 && this->args_
->size() == 1
8087 && this->args_
->front()->call_expression() != NULL
8088 && this->fn_
->type()->function_type() != NULL
)
8090 Function_type
* fntype
= this->fn_
->type()->function_type();
8091 size_t rc
= this->args_
->front()->call_expression()->result_count();
8093 && fntype
->parameters() != NULL
8094 && (fntype
->parameters()->size() == rc
8095 || (fntype
->is_varargs()
8096 && fntype
->parameters()->size() - 1 <= rc
)))
8098 Call_expression
* call
= this->args_
->front()->call_expression();
8099 Expression_list
* args
= new Expression_list
;
8100 for (size_t i
= 0; i
< rc
; ++i
)
8101 args
->push_back(Expression::make_call_result(call
, i
));
8102 // We can't return a new call expression here, because this
8103 // one may be referenced by Call_result expressions. FIXME.
8109 // Handle a call to a varargs function by packaging up the extra
8111 if (this->fn_
->type()->function_type() != NULL
8112 && this->fn_
->type()->function_type()->is_varargs())
8114 Function_type
* fntype
= this->fn_
->type()->function_type();
8115 const Typed_identifier_list
* parameters
= fntype
->parameters();
8116 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
8117 Type
* varargs_type
= parameters
->back().type();
8118 return this->lower_varargs(gogo
, function
, varargs_type
,
8119 parameters
->size());
8125 // Lower a call to a varargs function. FUNCTION is the function in
8126 // which the call occurs--it's not the function we are calling.
8127 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8128 // PARAM_COUNT is the number of parameters of the function we are
8129 // calling; the last of these parameters will be the varargs
8133 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8134 Type
* varargs_type
, size_t param_count
)
8136 if (this->varargs_are_lowered_
)
8139 source_location loc
= this->location();
8141 gcc_assert(param_count
> 0);
8142 gcc_assert(varargs_type
->is_open_array_type());
8144 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8145 if (arg_count
< param_count
- 1)
8147 // Not enough arguments; will be caught in check_types.
8151 Expression_list
* old_args
= this->args_
;
8152 Expression_list
* new_args
= new Expression_list();
8153 bool push_empty_arg
= false;
8154 if (old_args
== NULL
|| old_args
->empty())
8156 gcc_assert(param_count
== 1);
8157 push_empty_arg
= true;
8161 Expression_list::const_iterator pa
;
8163 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8165 if (static_cast<size_t>(i
) == param_count
)
8167 new_args
->push_back(*pa
);
8170 // We have reached the varargs parameter.
8172 bool issued_error
= false;
8173 if (pa
== old_args
->end())
8174 push_empty_arg
= true;
8175 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8176 new_args
->push_back(*pa
);
8177 else if (this->is_varargs_
)
8179 this->report_error(_("too many arguments"));
8182 else if (pa
+ 1 == old_args
->end()
8183 && this->is_compatible_varargs_argument(function
, *pa
,
8186 new_args
->push_back(*pa
);
8189 Type
* element_type
= varargs_type
->array_type()->element_type();
8190 Expression_list
* vals
= new Expression_list
;
8191 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8193 // Check types here so that we get a better message.
8194 Type
* patype
= (*pa
)->type();
8195 source_location paloc
= (*pa
)->location();
8196 if (!this->check_argument_type(i
, element_type
, patype
,
8197 paloc
, issued_error
))
8199 vals
->push_back(*pa
);
8202 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8203 new_args
->push_back(val
);
8208 new_args
->push_back(Expression::make_nil(loc
));
8210 // We can't return a new call expression here, because this one may
8211 // be referenced by Call_result expressions. FIXME.
8212 if (old_args
!= NULL
)
8214 this->args_
= new_args
;
8215 this->varargs_are_lowered_
= true;
8217 // Lower all the new subexpressions.
8218 Expression
* ret
= this;
8219 gogo
->lower_expression(function
, &ret
);
8220 gcc_assert(ret
== this);
8224 // Return true if ARG is a varargs argment which should be passed to
8225 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8226 // will be the last argument passed in the call, and PARAM_TYPE will
8227 // be the type of the last parameter of the varargs function being
8231 Call_expression::is_compatible_varargs_argument(Named_object
* function
,
8236 *issued_error
= false;
8238 Type
* var_type
= NULL
;
8240 // The simple case is passing the varargs parameter of the caller.
8241 Var_expression
* ve
= arg
->var_expression();
8242 if (ve
!= NULL
&& ve
->named_object()->is_variable())
8244 Variable
* var
= ve
->named_object()->var_value();
8245 if (var
->is_varargs_parameter())
8246 var_type
= var
->type();
8249 // The complex case is passing the varargs parameter of some
8250 // enclosing function. This will look like passing down *c.f where
8251 // c is the closure variable and f is a field in the closure.
8252 if (function
!= NULL
8253 && function
->func_value()->needs_closure()
8254 && arg
->classification() == EXPRESSION_UNARY
)
8256 Unary_expression
* ue
= static_cast<Unary_expression
*>(arg
);
8257 if (ue
->op() == OPERATOR_MULT
)
8259 Field_reference_expression
* fre
=
8260 ue
->operand()->deref()->field_reference_expression();
8263 Var_expression
* ve
= fre
->expr()->deref()->var_expression();
8266 Named_object
* no
= ve
->named_object();
8267 Function
* f
= function
->func_value();
8268 if (no
== f
->closure_var())
8270 // At this point we know that this indeed a
8271 // reference to some enclosing variable. Now we
8272 // need to figure out whether that variable is a
8273 // varargs parameter.
8274 Named_object
* enclosing
=
8275 f
->enclosing_var(fre
->field_index());
8276 Variable
* var
= enclosing
->var_value();
8277 if (var
->is_varargs_parameter())
8278 var_type
= var
->type();
8285 if (var_type
== NULL
)
8288 // We only match if the parameter is the same, with an identical
8290 Array_type
* var_at
= var_type
->array_type();
8291 gcc_assert(var_at
!= NULL
);
8292 Array_type
* param_at
= param_type
->array_type();
8293 if (param_at
!= NULL
8294 && Type::are_identical(var_at
->element_type(),
8295 param_at
->element_type(), true, NULL
))
8297 error_at(arg
->location(), "... mismatch: passing ...T as ...");
8298 *issued_error
= true;
8302 // Get the function type. Returns NULL if we don't know the type. If
8303 // this returns NULL, and if_ERROR is true, issues an error.
8306 Call_expression::get_function_type() const
8308 return this->fn_
->type()->function_type();
8311 // Return the number of values which this call will return.
8314 Call_expression::result_count() const
8316 const Function_type
* fntype
= this->get_function_type();
8319 if (fntype
->results() == NULL
)
8321 return fntype
->results()->size();
8324 // Return whether this is a call to the predeclared function recover.
8327 Call_expression::is_recover_call() const
8329 return this->do_is_recover_call();
8332 // Set the argument to the recover function.
8335 Call_expression::set_recover_arg(Expression
* arg
)
8337 this->do_set_recover_arg(arg
);
8340 // Virtual functions also implemented by Builtin_call_expression.
8343 Call_expression::do_is_recover_call() const
8349 Call_expression::do_set_recover_arg(Expression
*)
8357 Call_expression::do_type()
8359 if (this->type_
!= NULL
)
8363 Function_type
* fntype
= this->get_function_type();
8365 return Type::make_error_type();
8367 const Typed_identifier_list
* results
= fntype
->results();
8368 if (results
== NULL
)
8369 ret
= Type::make_void_type();
8370 else if (results
->size() == 1)
8371 ret
= results
->begin()->type();
8373 ret
= Type::make_call_multiple_result_type(this);
8380 // Determine types for a call expression. We can use the function
8381 // parameter types to set the types of the arguments.
8384 Call_expression::do_determine_type(const Type_context
*)
8386 this->fn_
->determine_type_no_context();
8387 Function_type
* fntype
= this->get_function_type();
8388 const Typed_identifier_list
* parameters
= NULL
;
8390 parameters
= fntype
->parameters();
8391 if (this->args_
!= NULL
)
8393 Typed_identifier_list::const_iterator pt
;
8394 if (parameters
!= NULL
)
8395 pt
= parameters
->begin();
8396 for (Expression_list::const_iterator pa
= this->args_
->begin();
8397 pa
!= this->args_
->end();
8400 if (parameters
!= NULL
&& pt
!= parameters
->end())
8402 Type_context
subcontext(pt
->type(), false);
8403 (*pa
)->determine_type(&subcontext
);
8407 (*pa
)->determine_type_no_context();
8412 // Check types for parameter I.
8415 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8416 const Type
* argument_type
,
8417 source_location argument_location
,
8421 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8426 error_at(argument_location
, "argument %d has incompatible type", i
);
8428 error_at(argument_location
,
8429 "argument %d has incompatible type (%s)",
8432 this->set_is_error();
8441 Call_expression::do_check_types(Gogo
*)
8443 Function_type
* fntype
= this->get_function_type();
8446 if (!this->fn_
->type()->is_error_type())
8447 this->report_error(_("expected function"));
8451 if (fntype
->is_method())
8453 // We don't support pointers to methods, so the function has to
8454 // be a bound method expression.
8455 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8458 this->report_error(_("method call without object"));
8461 Type
* first_arg_type
= bme
->first_argument()->type();
8462 if (first_arg_type
->points_to() == NULL
)
8464 // When passing a value, we need to check that we are
8465 // permitted to copy it.
8467 if (!Type::are_assignable(fntype
->receiver()->type(),
8468 first_arg_type
, &reason
))
8471 this->report_error(_("incompatible type for receiver"));
8474 error_at(this->location(),
8475 "incompatible type for receiver (%s)",
8477 this->set_is_error();
8483 // Note that varargs was handled by the lower_varargs() method, so
8484 // we don't have to worry about it here.
8486 const Typed_identifier_list
* parameters
= fntype
->parameters();
8487 if (this->args_
== NULL
)
8489 if (parameters
!= NULL
&& !parameters
->empty())
8490 this->report_error(_("not enough arguments"));
8492 else if (parameters
== NULL
)
8493 this->report_error(_("too many arguments"));
8497 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8498 for (Expression_list::const_iterator pa
= this->args_
->begin();
8499 pa
!= this->args_
->end();
8502 if (pt
== parameters
->end())
8504 this->report_error(_("too many arguments"));
8507 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8508 (*pa
)->location(), false);
8510 if (pt
!= parameters
->end())
8511 this->report_error(_("not enough arguments"));
8515 // Return whether we have to use a temporary variable to ensure that
8516 // we evaluate this call expression in order. If the call returns no
8517 // results then it will inevitably be executed last. If the call
8518 // returns more than one result then it will be used with Call_result
8519 // expressions. So we only have to use a temporary variable if the
8520 // call returns exactly one result.
8523 Call_expression::do_must_eval_in_order() const
8525 return this->result_count() == 1;
8528 // Get the function and the first argument to use when calling a bound
8532 Call_expression::bound_method_function(Translate_context
* context
,
8533 Bound_method_expression
* bound_method
,
8534 tree
* first_arg_ptr
)
8536 Expression
* first_argument
= bound_method
->first_argument();
8537 tree first_arg
= first_argument
->get_tree(context
);
8538 if (first_arg
== error_mark_node
)
8539 return error_mark_node
;
8541 // We always pass a pointer to the first argument when calling a
8543 if (first_argument
->type()->points_to() == NULL
)
8545 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8546 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8547 || DECL_P(first_arg
)
8548 || TREE_CODE(first_arg
) == INDIRECT_REF
8549 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8551 first_arg
= build_fold_addr_expr(first_arg
);
8552 if (DECL_P(first_arg
))
8553 TREE_ADDRESSABLE(first_arg
) = 1;
8557 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8558 get_name(first_arg
));
8559 DECL_IGNORED_P(tmp
) = 0;
8560 DECL_INITIAL(tmp
) = first_arg
;
8561 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8562 build1(DECL_EXPR
, void_type_node
, tmp
),
8563 build_fold_addr_expr(tmp
));
8564 TREE_ADDRESSABLE(tmp
) = 1;
8566 if (first_arg
== error_mark_node
)
8567 return error_mark_node
;
8570 Type
* fatype
= bound_method
->first_argument_type();
8573 if (fatype
->points_to() == NULL
)
8574 fatype
= Type::make_pointer_type(fatype
);
8575 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8576 if (first_arg
== error_mark_node
8577 || TREE_TYPE(first_arg
) == error_mark_node
)
8578 return error_mark_node
;
8581 *first_arg_ptr
= first_arg
;
8583 return bound_method
->method()->get_tree(context
);
8586 // Get the function and the first argument to use when calling an
8587 // interface method.
8590 Call_expression::interface_method_function(
8591 Translate_context
* context
,
8592 Interface_field_reference_expression
* interface_method
,
8593 tree
* first_arg_ptr
)
8595 tree expr
= interface_method
->expr()->get_tree(context
);
8596 if (expr
== error_mark_node
)
8597 return error_mark_node
;
8598 expr
= save_expr(expr
);
8599 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8600 if (first_arg
== error_mark_node
)
8601 return error_mark_node
;
8602 *first_arg_ptr
= first_arg
;
8603 return interface_method
->get_function_tree(context
, expr
);
8606 // Build the call expression.
8609 Call_expression::do_get_tree(Translate_context
* context
)
8611 if (this->tree_
!= NULL_TREE
)
8614 Function_type
* fntype
= this->get_function_type();
8616 return error_mark_node
;
8618 if (this->fn_
->is_error_expression())
8619 return error_mark_node
;
8621 Gogo
* gogo
= context
->gogo();
8622 source_location location
= this->location();
8624 Func_expression
* func
= this->fn_
->func_expression();
8625 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8626 Interface_field_reference_expression
* interface_method
=
8627 this->fn_
->interface_field_reference_expression();
8628 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8629 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8630 gcc_assert(!fntype
->is_method() || is_method
);
8634 if (this->args_
== NULL
|| this->args_
->empty())
8636 nargs
= is_method
? 1 : 0;
8637 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8641 const Typed_identifier_list
* params
= fntype
->parameters();
8642 gcc_assert(params
!= NULL
);
8644 nargs
= this->args_
->size();
8645 int i
= is_method
? 1 : 0;
8647 args
= new tree
[nargs
];
8649 Typed_identifier_list::const_iterator pp
= params
->begin();
8650 Expression_list::const_iterator pe
;
8651 for (pe
= this->args_
->begin();
8652 pe
!= this->args_
->end();
8655 gcc_assert(pp
!= params
->end());
8656 tree arg_val
= (*pe
)->get_tree(context
);
8657 args
[i
] = Expression::convert_for_assignment(context
,
8662 if (args
[i
] == error_mark_node
)
8663 return error_mark_node
;
8665 gcc_assert(pp
== params
->end());
8666 gcc_assert(i
== nargs
);
8669 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8670 if (rettype
== error_mark_node
)
8671 return error_mark_node
;
8675 fn
= func
->get_tree_without_closure(gogo
);
8676 else if (!is_method
)
8677 fn
= this->fn_
->get_tree(context
);
8678 else if (bound_method
!= NULL
)
8679 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8680 else if (interface_method
!= NULL
)
8681 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8685 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8686 return error_mark_node
;
8688 // This is to support builtin math functions when using 80387 math.
8690 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8691 fndecl
= TREE_OPERAND(fndecl
, 0);
8692 tree excess_type
= NULL_TREE
;
8694 && DECL_IS_BUILTIN(fndecl
)
8695 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8697 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8698 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8699 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8700 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8702 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8703 if (excess_type
!= NULL_TREE
)
8705 tree excess_fndecl
= mathfn_built_in(excess_type
,
8706 DECL_FUNCTION_CODE(fndecl
));
8707 if (excess_fndecl
== NULL_TREE
)
8708 excess_type
= NULL_TREE
;
8711 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8712 for (int i
= 0; i
< nargs
; ++i
)
8713 args
[i
] = ::convert(excess_type
, args
[i
]);
8718 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8722 SET_EXPR_LOCATION(ret
, location
);
8726 tree closure_tree
= func
->closure()->get_tree(context
);
8727 if (closure_tree
!= error_mark_node
)
8728 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8731 // If this is a recursive function type which returns itself, as in
8733 // we have used ptr_type_node for the return type. Add a cast here
8734 // to the correct type.
8735 if (TREE_TYPE(ret
) == ptr_type_node
)
8737 tree t
= this->type()->get_tree(gogo
);
8738 ret
= fold_convert_loc(location
, t
, ret
);
8741 if (excess_type
!= NULL_TREE
)
8743 // Calling convert here can undo our excess precision change.
8744 // That may or may not be a bug in convert_to_real.
8745 ret
= build1(NOP_EXPR
, rettype
, ret
);
8748 // If there is more than one result, we will refer to the call
8750 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
8751 ret
= save_expr(ret
);
8758 // Make a call expression.
8761 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
8762 source_location location
)
8764 return new Call_expression(fn
, args
, is_varargs
, location
);
8767 // A single result from a call which returns multiple results.
8769 class Call_result_expression
: public Expression
8772 Call_result_expression(Call_expression
* call
, unsigned int index
)
8773 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
8774 call_(call
), index_(index
)
8779 do_traverse(Traverse
*);
8785 do_determine_type(const Type_context
*);
8788 do_check_types(Gogo
*);
8793 return new Call_result_expression(this->call_
->call_expression(),
8798 do_must_eval_in_order() const
8802 do_get_tree(Translate_context
*);
8805 // The underlying call expression.
8807 // Which result we want.
8808 unsigned int index_
;
8811 // Traverse a call result.
8814 Call_result_expression::do_traverse(Traverse
* traverse
)
8816 if (traverse
->remember_expression(this->call_
))
8818 // We have already traversed the call expression.
8819 return TRAVERSE_CONTINUE
;
8821 return Expression::traverse(&this->call_
, traverse
);
8827 Call_result_expression::do_type()
8829 if (this->classification() == EXPRESSION_ERROR
)
8830 return Type::make_error_type();
8832 // THIS->CALL_ can be replaced with a temporary reference due to
8833 // Call_expression::do_must_eval_in_order when there is an error.
8834 Call_expression
* ce
= this->call_
->call_expression();
8836 return Type::make_error_type();
8837 Function_type
* fntype
= ce
->get_function_type();
8839 return Type::make_error_type();
8840 const Typed_identifier_list
* results
= fntype
->results();
8841 if (results
== NULL
)
8843 this->report_error(_("number of results does not match "
8844 "number of values"));
8845 return Type::make_error_type();
8847 Typed_identifier_list::const_iterator pr
= results
->begin();
8848 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8850 if (pr
== results
->end())
8854 if (pr
== results
->end())
8856 this->report_error(_("number of results does not match "
8857 "number of values"));
8858 return Type::make_error_type();
8863 // Check the type. Just make sure that we trigger the warning in
8867 Call_result_expression::do_check_types(Gogo
*)
8872 // Determine the type. We have nothing to do here, but the 0 result
8873 // needs to pass down to the caller.
8876 Call_result_expression::do_determine_type(const Type_context
*)
8878 if (this->index_
== 0)
8879 this->call_
->determine_type_no_context();
8885 Call_result_expression::do_get_tree(Translate_context
* context
)
8887 tree call_tree
= this->call_
->get_tree(context
);
8888 if (call_tree
== error_mark_node
)
8889 return error_mark_node
;
8890 gcc_assert(TREE_CODE(TREE_TYPE(call_tree
)) == RECORD_TYPE
);
8891 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
8892 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8894 gcc_assert(field
!= NULL_TREE
);
8895 field
= DECL_CHAIN(field
);
8897 gcc_assert(field
!= NULL_TREE
);
8898 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
8901 // Make a reference to a single result of a call which returns
8902 // multiple results.
8905 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
8907 return new Call_result_expression(call
, index
);
8910 // Class Index_expression.
8915 Index_expression::do_traverse(Traverse
* traverse
)
8917 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
8918 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
8919 || (this->end_
!= NULL
8920 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
8921 return TRAVERSE_EXIT
;
8922 return TRAVERSE_CONTINUE
;
8925 // Lower an index expression. This converts the generic index
8926 // expression into an array index, a string index, or a map index.
8929 Index_expression::do_lower(Gogo
*, Named_object
*, int)
8931 source_location location
= this->location();
8932 Expression
* left
= this->left_
;
8933 Expression
* start
= this->start_
;
8934 Expression
* end
= this->end_
;
8936 Type
* type
= left
->type();
8937 if (type
->is_error_type())
8938 return Expression::make_error(location
);
8939 else if (type
->array_type() != NULL
)
8940 return Expression::make_array_index(left
, start
, end
, location
);
8941 else if (type
->points_to() != NULL
8942 && type
->points_to()->array_type() != NULL
8943 && !type
->points_to()->is_open_array_type())
8945 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
8947 return Expression::make_array_index(deref
, start
, end
, location
);
8949 else if (type
->is_string_type())
8950 return Expression::make_string_index(left
, start
, end
, location
);
8951 else if (type
->map_type() != NULL
)
8955 error_at(location
, "invalid slice of map");
8956 return Expression::make_error(location
);
8958 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
8960 if (this->is_lvalue_
)
8961 ret
->set_is_lvalue();
8967 "attempt to index object which is not array, string, or map");
8968 return Expression::make_error(location
);
8972 // Make an index expression.
8975 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
8976 source_location location
)
8978 return new Index_expression(left
, start
, end
, location
);
8981 // An array index. This is used for both indexing and slicing.
8983 class Array_index_expression
: public Expression
8986 Array_index_expression(Expression
* array
, Expression
* start
,
8987 Expression
* end
, source_location location
)
8988 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
8989 array_(array
), start_(start
), end_(end
), type_(NULL
)
8994 do_traverse(Traverse
*);
9000 do_determine_type(const Type_context
*);
9003 do_check_types(Gogo
*);
9008 return Expression::make_array_index(this->array_
->copy(),
9009 this->start_
->copy(),
9012 : this->end_
->copy()),
9017 do_is_addressable() const;
9020 do_address_taken(bool escapes
)
9021 { this->array_
->address_taken(escapes
); }
9024 do_get_tree(Translate_context
*);
9027 // The array we are getting a value from.
9029 // The start or only index.
9031 // The end index of a slice. This may be NULL for a simple array
9032 // index, or it may be a nil expression for the length of the array.
9034 // The type of the expression.
9038 // Array index traversal.
9041 Array_index_expression::do_traverse(Traverse
* traverse
)
9043 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9044 return TRAVERSE_EXIT
;
9045 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9046 return TRAVERSE_EXIT
;
9047 if (this->end_
!= NULL
)
9049 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9050 return TRAVERSE_EXIT
;
9052 return TRAVERSE_CONTINUE
;
9055 // Return the type of an array index.
9058 Array_index_expression::do_type()
9060 if (this->type_
== NULL
)
9062 Array_type
* type
= this->array_
->type()->array_type();
9064 this->type_
= Type::make_error_type();
9065 else if (this->end_
== NULL
)
9066 this->type_
= type
->element_type();
9067 else if (type
->is_open_array_type())
9069 // A slice of a slice has the same type as the original
9071 this->type_
= this->array_
->type()->deref();
9075 // A slice of an array is a slice.
9076 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9082 // Set the type of an array index.
9085 Array_index_expression::do_determine_type(const Type_context
*)
9087 this->array_
->determine_type_no_context();
9088 Type_context
subcontext(NULL
, true);
9089 this->start_
->determine_type(&subcontext
);
9090 if (this->end_
!= NULL
)
9091 this->end_
->determine_type(&subcontext
);
9094 // Check types of an array index.
9097 Array_index_expression::do_check_types(Gogo
*)
9099 if (this->start_
->type()->integer_type() == NULL
)
9100 this->report_error(_("index must be integer"));
9101 if (this->end_
!= NULL
9102 && this->end_
->type()->integer_type() == NULL
9103 && !this->end_
->is_nil_expression())
9104 this->report_error(_("slice end must be integer"));
9106 Array_type
* array_type
= this->array_
->type()->array_type();
9107 if (array_type
== NULL
)
9109 gcc_assert(this->array_
->type()->is_error_type());
9113 unsigned int int_bits
=
9114 Type::lookup_integer_type("int")->integer_type()->bits();
9119 bool lval_valid
= (array_type
->length() != NULL
9120 && array_type
->length()->integer_constant_value(true,
9125 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9127 if (mpz_sgn(ival
) < 0
9128 || mpz_sizeinbase(ival
, 2) >= int_bits
9130 && (this->end_
== NULL
9131 ? mpz_cmp(ival
, lval
) >= 0
9132 : mpz_cmp(ival
, lval
) > 0)))
9134 error_at(this->start_
->location(), "array index out of bounds");
9135 this->set_is_error();
9138 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9140 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9142 if (mpz_sgn(ival
) < 0
9143 || mpz_sizeinbase(ival
, 2) >= int_bits
9144 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
9146 error_at(this->end_
->location(), "array index out of bounds");
9147 this->set_is_error();
9154 // A slice of an array requires an addressable array. A slice of a
9155 // slice is always possible.
9156 if (this->end_
!= NULL
9157 && !array_type
->is_open_array_type()
9158 && !this->array_
->is_addressable())
9159 this->report_error(_("array is not addressable"));
9162 // Return whether this expression is addressable.
9165 Array_index_expression::do_is_addressable() const
9167 // A slice expression is not addressable.
9168 if (this->end_
!= NULL
)
9171 // An index into a slice is addressable.
9172 if (this->array_
->type()->is_open_array_type())
9175 // An index into an array is addressable if the array is
9177 return this->array_
->is_addressable();
9180 // Get a tree for an array index.
9183 Array_index_expression::do_get_tree(Translate_context
* context
)
9185 Gogo
* gogo
= context
->gogo();
9186 source_location loc
= this->location();
9188 Array_type
* array_type
= this->array_
->type()->array_type();
9189 if (array_type
== NULL
)
9191 gcc_assert(this->array_
->type()->is_error_type());
9192 return error_mark_node
;
9195 tree type_tree
= array_type
->get_tree(gogo
);
9196 if (type_tree
== error_mark_node
)
9197 return error_mark_node
;
9199 tree array_tree
= this->array_
->get_tree(context
);
9200 if (array_tree
== error_mark_node
)
9201 return error_mark_node
;
9203 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9204 array_tree
= save_expr(array_tree
);
9205 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
9206 if (length_tree
== error_mark_node
)
9207 return error_mark_node
;
9208 length_tree
= save_expr(length_tree
);
9209 tree length_type
= TREE_TYPE(length_tree
);
9211 tree bad_index
= boolean_false_node
;
9213 tree start_tree
= this->start_
->get_tree(context
);
9214 if (start_tree
== error_mark_node
)
9215 return error_mark_node
;
9216 if (!DECL_P(start_tree
))
9217 start_tree
= save_expr(start_tree
);
9218 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9219 start_tree
= convert_to_integer(length_type
, start_tree
);
9221 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9224 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9225 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9226 fold_build2_loc(loc
,
9230 boolean_type_node
, start_tree
,
9233 int code
= (array_type
->length() != NULL
9234 ? (this->end_
== NULL
9235 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9236 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9237 : (this->end_
== NULL
9238 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9239 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9240 tree crash
= Gogo::runtime_error(code
, loc
);
9242 if (this->end_
== NULL
)
9244 // Simple array indexing. This has to return an l-value, so
9245 // wrap the index check into START_TREE.
9246 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9247 build3(COND_EXPR
, void_type_node
,
9248 bad_index
, crash
, NULL_TREE
),
9250 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9252 if (array_type
->length() != NULL
)
9255 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9256 start_tree
, NULL_TREE
, NULL_TREE
);
9261 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9262 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9263 if (element_type_tree
== error_mark_node
)
9264 return error_mark_node
;
9265 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9266 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9267 start_tree
, element_size
);
9268 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9269 TREE_TYPE(values
), values
, offset
);
9270 return build_fold_indirect_ref(ptr
);
9276 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9277 if (capacity_tree
== error_mark_node
)
9278 return error_mark_node
;
9279 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9282 if (this->end_
->is_nil_expression())
9283 end_tree
= length_tree
;
9286 end_tree
= this->end_
->get_tree(context
);
9287 if (end_tree
== error_mark_node
)
9288 return error_mark_node
;
9289 if (!DECL_P(end_tree
))
9290 end_tree
= save_expr(end_tree
);
9291 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9292 end_tree
= convert_to_integer(length_type
, end_tree
);
9294 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9297 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9299 capacity_tree
= save_expr(capacity_tree
);
9300 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9301 fold_build2_loc(loc
, LT_EXPR
,
9303 end_tree
, start_tree
),
9304 fold_build2_loc(loc
, GT_EXPR
,
9306 end_tree
, capacity_tree
));
9307 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9308 bad_index
, bad_end
);
9311 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9312 if (element_type_tree
== error_mark_node
)
9313 return error_mark_node
;
9314 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9316 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9317 fold_convert_loc(loc
, sizetype
, start_tree
),
9320 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9321 if (value_pointer
== error_mark_node
)
9322 return error_mark_node
;
9324 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9325 TREE_TYPE(value_pointer
),
9326 value_pointer
, offset
);
9328 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9329 end_tree
, start_tree
);
9331 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9332 capacity_tree
, start_tree
);
9334 tree struct_tree
= this->type()->get_tree(gogo
);
9335 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9337 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9339 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9340 tree field
= TYPE_FIELDS(struct_tree
);
9341 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9343 elt
->value
= value_pointer
;
9345 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9346 field
= DECL_CHAIN(field
);
9347 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9349 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9351 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9352 field
= DECL_CHAIN(field
);
9353 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9355 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9357 tree constructor
= build_constructor(struct_tree
, init
);
9359 if (TREE_CONSTANT(value_pointer
)
9360 && TREE_CONSTANT(result_length_tree
)
9361 && TREE_CONSTANT(result_capacity_tree
))
9362 TREE_CONSTANT(constructor
) = 1;
9364 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9365 build3(COND_EXPR
, void_type_node
,
9366 bad_index
, crash
, NULL_TREE
),
9370 // Make an array index expression. END may be NULL.
9373 Expression::make_array_index(Expression
* array
, Expression
* start
,
9374 Expression
* end
, source_location location
)
9376 // Taking a slice of a composite literal requires moving the literal
9378 if (end
!= NULL
&& array
->is_composite_literal())
9380 array
= Expression::make_heap_composite(array
, location
);
9381 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9383 return new Array_index_expression(array
, start
, end
, location
);
9386 // A string index. This is used for both indexing and slicing.
9388 class String_index_expression
: public Expression
9391 String_index_expression(Expression
* string
, Expression
* start
,
9392 Expression
* end
, source_location location
)
9393 : Expression(EXPRESSION_STRING_INDEX
, location
),
9394 string_(string
), start_(start
), end_(end
)
9399 do_traverse(Traverse
*);
9405 do_determine_type(const Type_context
*);
9408 do_check_types(Gogo
*);
9413 return Expression::make_string_index(this->string_
->copy(),
9414 this->start_
->copy(),
9417 : this->end_
->copy()),
9422 do_get_tree(Translate_context
*);
9425 // The string we are getting a value from.
9426 Expression
* string_
;
9427 // The start or only index.
9429 // The end index of a slice. This may be NULL for a single index,
9430 // or it may be a nil expression for the length of the string.
9434 // String index traversal.
9437 String_index_expression::do_traverse(Traverse
* traverse
)
9439 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9440 return TRAVERSE_EXIT
;
9441 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9442 return TRAVERSE_EXIT
;
9443 if (this->end_
!= NULL
)
9445 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9446 return TRAVERSE_EXIT
;
9448 return TRAVERSE_CONTINUE
;
9451 // Return the type of a string index.
9454 String_index_expression::do_type()
9456 if (this->end_
== NULL
)
9457 return Type::lookup_integer_type("uint8");
9459 return Type::make_string_type();
9462 // Determine the type of a string index.
9465 String_index_expression::do_determine_type(const Type_context
*)
9467 this->string_
->determine_type_no_context();
9468 Type_context
subcontext(NULL
, true);
9469 this->start_
->determine_type(&subcontext
);
9470 if (this->end_
!= NULL
)
9471 this->end_
->determine_type(&subcontext
);
9474 // Check types of a string index.
9477 String_index_expression::do_check_types(Gogo
*)
9479 if (this->start_
->type()->integer_type() == NULL
)
9480 this->report_error(_("index must be integer"));
9481 if (this->end_
!= NULL
9482 && this->end_
->type()->integer_type() == NULL
9483 && !this->end_
->is_nil_expression())
9484 this->report_error(_("slice end must be integer"));
9487 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9492 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9494 if (mpz_sgn(ival
) < 0
9495 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9497 error_at(this->start_
->location(), "string index out of bounds");
9498 this->set_is_error();
9501 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9503 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9505 if (mpz_sgn(ival
) < 0
9506 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9508 error_at(this->end_
->location(), "string index out of bounds");
9509 this->set_is_error();
9516 // Get a tree for a string index.
9519 String_index_expression::do_get_tree(Translate_context
* context
)
9521 source_location loc
= this->location();
9523 tree string_tree
= this->string_
->get_tree(context
);
9524 if (string_tree
== error_mark_node
)
9525 return error_mark_node
;
9527 if (this->string_
->type()->points_to() != NULL
)
9528 string_tree
= build_fold_indirect_ref(string_tree
);
9529 if (!DECL_P(string_tree
))
9530 string_tree
= save_expr(string_tree
);
9531 tree string_type
= TREE_TYPE(string_tree
);
9533 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9534 length_tree
= save_expr(length_tree
);
9535 tree length_type
= TREE_TYPE(length_tree
);
9537 tree bad_index
= boolean_false_node
;
9539 tree start_tree
= this->start_
->get_tree(context
);
9540 if (start_tree
== error_mark_node
)
9541 return error_mark_node
;
9542 if (!DECL_P(start_tree
))
9543 start_tree
= save_expr(start_tree
);
9544 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9545 start_tree
= convert_to_integer(length_type
, start_tree
);
9547 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9550 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9552 int code
= (this->end_
== NULL
9553 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9554 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9555 tree crash
= Gogo::runtime_error(code
, loc
);
9557 if (this->end_
== NULL
)
9559 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9561 fold_build2_loc(loc
, GE_EXPR
,
9563 start_tree
, length_tree
));
9565 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9566 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9568 fold_convert_loc(loc
, sizetype
, start_tree
));
9569 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9571 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9572 build3(COND_EXPR
, void_type_node
,
9573 bad_index
, crash
, NULL_TREE
),
9579 if (this->end_
->is_nil_expression())
9580 end_tree
= build_int_cst(length_type
, -1);
9583 end_tree
= this->end_
->get_tree(context
);
9584 if (end_tree
== error_mark_node
)
9585 return error_mark_node
;
9586 if (!DECL_P(end_tree
))
9587 end_tree
= save_expr(end_tree
);
9588 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9589 end_tree
= convert_to_integer(length_type
, end_tree
);
9591 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9594 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9597 static tree strslice_fndecl
;
9598 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9600 "__go_string_slice",
9609 if (ret
== error_mark_node
)
9610 return error_mark_node
;
9611 // This will panic if the bounds are out of range for the
9613 TREE_NOTHROW(strslice_fndecl
) = 0;
9615 if (bad_index
== boolean_false_node
)
9618 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9619 build3(COND_EXPR
, void_type_node
,
9620 bad_index
, crash
, NULL_TREE
),
9625 // Make a string index expression. END may be NULL.
9628 Expression::make_string_index(Expression
* string
, Expression
* start
,
9629 Expression
* end
, source_location location
)
9631 return new String_index_expression(string
, start
, end
, location
);
9636 // Get the type of the map.
9639 Map_index_expression::get_map_type() const
9641 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9643 gcc_assert(saw_errors());
9647 // Map index traversal.
9650 Map_index_expression::do_traverse(Traverse
* traverse
)
9652 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9653 return TRAVERSE_EXIT
;
9654 return Expression::traverse(&this->index_
, traverse
);
9657 // Return the type of a map index.
9660 Map_index_expression::do_type()
9662 Map_type
* mt
= this->get_map_type();
9664 return Type::make_error_type();
9665 Type
* type
= mt
->val_type();
9666 // If this map index is in a tuple assignment, we actually return a
9667 // pointer to the value type. Tuple_map_assignment_statement is
9668 // responsible for handling this correctly. We need to get the type
9669 // right in case this gets assigned to a temporary variable.
9670 if (this->is_in_tuple_assignment_
)
9671 type
= Type::make_pointer_type(type
);
9675 // Fix the type of a map index.
9678 Map_index_expression::do_determine_type(const Type_context
*)
9680 this->map_
->determine_type_no_context();
9681 Map_type
* mt
= this->get_map_type();
9682 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
9683 Type_context
subcontext(key_type
, false);
9684 this->index_
->determine_type(&subcontext
);
9687 // Check types of a map index.
9690 Map_index_expression::do_check_types(Gogo
*)
9693 Map_type
* mt
= this->get_map_type();
9696 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
9699 this->report_error(_("incompatible type for map index"));
9702 error_at(this->location(), "incompatible type for map index (%s)",
9704 this->set_is_error();
9709 // Get a tree for a map index.
9712 Map_index_expression::do_get_tree(Translate_context
* context
)
9714 Map_type
* type
= this->get_map_type();
9716 return error_mark_node
;
9718 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9719 if (valptr
== error_mark_node
)
9720 return error_mark_node
;
9721 valptr
= save_expr(valptr
);
9723 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
9725 if (this->is_lvalue_
)
9726 return build_fold_indirect_ref(valptr
);
9727 else if (this->is_in_tuple_assignment_
)
9729 // Tuple_map_assignment_statement is responsible for using this
9735 return fold_build3(COND_EXPR
, val_type_tree
,
9736 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
9737 fold_convert(TREE_TYPE(valptr
),
9738 null_pointer_node
)),
9739 type
->val_type()->get_init_tree(context
->gogo(),
9741 build_fold_indirect_ref(valptr
));
9745 // Get a tree for the map index. This returns a tree which evaluates
9746 // to a pointer to a value. The pointer will be NULL if the key is
9750 Map_index_expression::get_value_pointer(Translate_context
* context
,
9753 Map_type
* type
= this->get_map_type();
9755 return error_mark_node
;
9757 tree map_tree
= this->map_
->get_tree(context
);
9758 tree index_tree
= this->index_
->get_tree(context
);
9759 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
9760 this->index_
->type(),
9763 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
9764 return error_mark_node
;
9766 if (this->map_
->type()->points_to() != NULL
)
9767 map_tree
= build_fold_indirect_ref(map_tree
);
9769 // We need to pass in a pointer to the key, so stuff it into a
9771 tree tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
9772 DECL_IGNORED_P(tmp
) = 0;
9773 DECL_INITIAL(tmp
) = index_tree
;
9774 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
9775 tree tmpref
= fold_convert(const_ptr_type_node
, build_fold_addr_expr(tmp
));
9776 TREE_ADDRESSABLE(tmp
) = 1;
9778 static tree map_index_fndecl
;
9779 tree call
= Gogo::call_builtin(&map_index_fndecl
,
9783 const_ptr_type_node
,
9784 TREE_TYPE(map_tree
),
9786 const_ptr_type_node
,
9791 : boolean_false_node
));
9792 if (call
== error_mark_node
)
9793 return error_mark_node
;
9794 // This can panic on a map of interface type if the interface holds
9795 // an uncomparable or unhashable type.
9796 TREE_NOTHROW(map_index_fndecl
) = 0;
9798 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
9799 if (val_type_tree
== error_mark_node
)
9800 return error_mark_node
;
9801 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
9803 return build2(COMPOUND_EXPR
, ptr_val_type_tree
,
9805 fold_convert(ptr_val_type_tree
, call
));
9808 // Make a map index expression.
9810 Map_index_expression
*
9811 Expression::make_map_index(Expression
* map
, Expression
* index
,
9812 source_location location
)
9814 return new Map_index_expression(map
, index
, location
);
9817 // Class Field_reference_expression.
9819 // Return the type of a field reference.
9822 Field_reference_expression::do_type()
9824 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9825 gcc_assert(struct_type
!= NULL
);
9826 return struct_type
->field(this->field_index_
)->type();
9829 // Check the types for a field reference.
9832 Field_reference_expression::do_check_types(Gogo
*)
9834 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9835 gcc_assert(struct_type
!= NULL
);
9836 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
9839 // Get a tree for a field reference.
9842 Field_reference_expression::do_get_tree(Translate_context
* context
)
9844 tree struct_tree
= this->expr_
->get_tree(context
);
9845 if (struct_tree
== error_mark_node
9846 || TREE_TYPE(struct_tree
) == error_mark_node
)
9847 return error_mark_node
;
9848 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
9849 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
9850 if (field
== NULL_TREE
)
9852 // This can happen for a type which refers to itself indirectly
9853 // and then turns out to be erroneous.
9854 gcc_assert(saw_errors());
9855 return error_mark_node
;
9857 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
9859 field
= DECL_CHAIN(field
);
9860 gcc_assert(field
!= NULL_TREE
);
9862 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
9866 // Make a reference to a qualified identifier in an expression.
9868 Field_reference_expression
*
9869 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
9870 source_location location
)
9872 return new Field_reference_expression(expr
, field_index
, location
);
9875 // Class Interface_field_reference_expression.
9877 // Return a tree for the pointer to the function to call.
9880 Interface_field_reference_expression::get_function_tree(Translate_context
*,
9883 if (this->expr_
->type()->points_to() != NULL
)
9884 expr
= build_fold_indirect_ref(expr
);
9886 tree expr_type
= TREE_TYPE(expr
);
9887 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9889 tree field
= TYPE_FIELDS(expr_type
);
9890 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
9892 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9893 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
9895 table
= build_fold_indirect_ref(table
);
9896 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
9898 std::string name
= Gogo::unpack_hidden_name(this->name_
);
9899 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
9901 field
= DECL_CHAIN(field
))
9903 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
9906 gcc_assert(field
!= NULL_TREE
);
9908 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
9911 // Return a tree for the first argument to pass to the interface
9915 Interface_field_reference_expression::get_underlying_object_tree(
9919 if (this->expr_
->type()->points_to() != NULL
)
9920 expr
= build_fold_indirect_ref(expr
);
9922 tree expr_type
= TREE_TYPE(expr
);
9923 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9925 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
9926 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
9928 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9934 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
9936 return Expression::traverse(&this->expr_
, traverse
);
9939 // Return the type of an interface field reference.
9942 Interface_field_reference_expression::do_type()
9944 Type
* expr_type
= this->expr_
->type();
9946 Type
* points_to
= expr_type
->points_to();
9947 if (points_to
!= NULL
)
9948 expr_type
= points_to
;
9950 Interface_type
* interface_type
= expr_type
->interface_type();
9951 if (interface_type
== NULL
)
9952 return Type::make_error_type();
9954 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
9956 return Type::make_error_type();
9958 return method
->type();
9964 Interface_field_reference_expression::do_determine_type(const Type_context
*)
9966 this->expr_
->determine_type_no_context();
9969 // Check the types for an interface field reference.
9972 Interface_field_reference_expression::do_check_types(Gogo
*)
9974 Type
* type
= this->expr_
->type();
9976 Type
* points_to
= type
->points_to();
9977 if (points_to
!= NULL
)
9980 Interface_type
* interface_type
= type
->interface_type();
9981 if (interface_type
== NULL
)
9982 this->report_error(_("expected interface or pointer to interface"));
9985 const Typed_identifier
* method
=
9986 interface_type
->find_method(this->name_
);
9989 error_at(this->location(), "method %qs not in interface",
9990 Gogo::message_name(this->name_
).c_str());
9991 this->set_is_error();
9996 // Get a tree for a reference to a field in an interface. There is no
9997 // standard tree type representation for this: it's a function
9998 // attached to its first argument, like a Bound_method_expression.
9999 // The only places it may currently be used are in a Call_expression
10000 // or a Go_statement, which will take it apart directly. So this has
10001 // nothing to do at present.
10004 Interface_field_reference_expression::do_get_tree(Translate_context
*)
10009 // Make a reference to a field in an interface.
10012 Expression::make_interface_field_reference(Expression
* expr
,
10013 const std::string
& field
,
10014 source_location location
)
10016 return new Interface_field_reference_expression(expr
, field
, location
);
10019 // A general selector. This is a Parser_expression for LEFT.NAME. It
10020 // is lowered after we know the type of the left hand side.
10022 class Selector_expression
: public Parser_expression
10025 Selector_expression(Expression
* left
, const std::string
& name
,
10026 source_location location
)
10027 : Parser_expression(EXPRESSION_SELECTOR
, location
),
10028 left_(left
), name_(name
)
10033 do_traverse(Traverse
* traverse
)
10034 { return Expression::traverse(&this->left_
, traverse
); }
10037 do_lower(Gogo
*, Named_object
*, int);
10042 return new Selector_expression(this->left_
->copy(), this->name_
,
10048 lower_method_expression(Gogo
*);
10050 // The expression on the left hand side.
10052 // The name on the right hand side.
10056 // Lower a selector expression once we know the real type of the left
10060 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
10062 Expression
* left
= this->left_
;
10063 if (left
->is_type_expression())
10064 return this->lower_method_expression(gogo
);
10065 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
10069 // Lower a method expression T.M or (*T).M. We turn this into a
10070 // function literal.
10073 Selector_expression::lower_method_expression(Gogo
* gogo
)
10075 source_location location
= this->location();
10076 Type
* type
= this->left_
->type();
10077 const std::string
& name(this->name_
);
10080 if (type
->points_to() == NULL
)
10081 is_pointer
= false;
10085 type
= type
->points_to();
10087 Named_type
* nt
= type
->named_type();
10091 ("method expression requires named type or "
10092 "pointer to named type"));
10093 return Expression::make_error(location
);
10097 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
10098 if (method
== NULL
)
10101 error_at(location
, "type %<%s%> has no method %<%s%>",
10102 nt
->message_name().c_str(),
10103 Gogo::message_name(name
).c_str());
10105 error_at(location
, "method %<%s%> is ambiguous in type %<%s%>",
10106 Gogo::message_name(name
).c_str(),
10107 nt
->message_name().c_str());
10108 return Expression::make_error(location
);
10111 if (!is_pointer
&& !method
->is_value_method())
10113 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
10114 nt
->message_name().c_str(),
10115 Gogo::message_name(name
).c_str());
10116 return Expression::make_error(location
);
10119 // Build a new function type in which the receiver becomes the first
10121 Function_type
* method_type
= method
->type();
10122 gcc_assert(method_type
->is_method());
10124 const char* const receiver_name
= "$this";
10125 Typed_identifier_list
* parameters
= new Typed_identifier_list();
10126 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
10129 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
10130 if (method_parameters
!= NULL
)
10132 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10133 p
!= method_parameters
->end();
10135 parameters
->push_back(*p
);
10138 const Typed_identifier_list
* method_results
= method_type
->results();
10139 Typed_identifier_list
* results
;
10140 if (method_results
== NULL
)
10144 results
= new Typed_identifier_list();
10145 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
10146 p
!= method_results
->end();
10148 results
->push_back(*p
);
10151 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
10153 if (method_type
->is_varargs())
10154 fntype
->set_is_varargs();
10156 // We generate methods which always takes a pointer to the receiver
10157 // as their first argument. If this is for a pointer type, we can
10158 // simply reuse the existing function. We use an internal hack to
10159 // get the right type.
10163 Named_object
* mno
= (method
->needs_stub_method()
10164 ? method
->stub_object()
10165 : method
->named_object());
10166 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
10167 f
= Expression::make_cast(fntype
, f
, location
);
10168 Type_conversion_expression
* tce
=
10169 static_cast<Type_conversion_expression
*>(f
);
10170 tce
->set_may_convert_function_types();
10174 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
10177 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
10178 gcc_assert(vno
!= NULL
);
10179 Expression
* ve
= Expression::make_var_reference(vno
, location
);
10180 Expression
* bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
10181 gcc_assert(bm
!= NULL
&& !bm
->is_error_expression());
10183 Expression_list
* args
;
10184 if (method_parameters
== NULL
)
10188 args
= new Expression_list();
10189 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10190 p
!= method_parameters
->end();
10193 vno
= gogo
->lookup(p
->name(), NULL
);
10194 gcc_assert(vno
!= NULL
);
10195 args
->push_back(Expression::make_var_reference(vno
, location
));
10199 Call_expression
* call
= Expression::make_call(bm
, args
,
10200 method_type
->is_varargs(),
10203 size_t count
= call
->result_count();
10206 s
= Statement::make_statement(call
);
10209 Expression_list
* retvals
= new Expression_list();
10211 retvals
->push_back(call
);
10214 for (size_t i
= 0; i
< count
; ++i
)
10215 retvals
->push_back(Expression::make_call_result(call
, i
));
10217 s
= Statement::make_return_statement(no
->func_value()->type()->results(),
10218 retvals
, location
);
10220 gogo
->add_statement(s
);
10222 gogo
->finish_function(location
);
10224 return Expression::make_func_reference(no
, NULL
, location
);
10227 // Make a selector expression.
10230 Expression::make_selector(Expression
* left
, const std::string
& name
,
10231 source_location location
)
10233 return new Selector_expression(left
, name
, location
);
10236 // Implement the builtin function new.
10238 class Allocation_expression
: public Expression
10241 Allocation_expression(Type
* type
, source_location location
)
10242 : Expression(EXPRESSION_ALLOCATION
, location
),
10248 do_traverse(Traverse
* traverse
)
10249 { return Type::traverse(this->type_
, traverse
); }
10253 { return Type::make_pointer_type(this->type_
); }
10256 do_determine_type(const Type_context
*)
10260 do_check_types(Gogo
*);
10264 { return new Allocation_expression(this->type_
, this->location()); }
10267 do_get_tree(Translate_context
*);
10270 // The type we are allocating.
10274 // Check the type of an allocation expression.
10277 Allocation_expression::do_check_types(Gogo
*)
10279 if (this->type_
->function_type() != NULL
)
10280 this->report_error(_("invalid new of function type"));
10283 // Return a tree for an allocation expression.
10286 Allocation_expression::do_get_tree(Translate_context
* context
)
10288 tree type_tree
= this->type_
->get_tree(context
->gogo());
10289 if (type_tree
== error_mark_node
)
10290 return error_mark_node
;
10291 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10292 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10294 if (space
== error_mark_node
)
10295 return error_mark_node
;
10296 return fold_convert(build_pointer_type(type_tree
), space
);
10299 // Make an allocation expression.
10302 Expression::make_allocation(Type
* type
, source_location location
)
10304 return new Allocation_expression(type
, location
);
10307 // Implement the builtin function make.
10309 class Make_expression
: public Expression
10312 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10313 : Expression(EXPRESSION_MAKE
, location
),
10314 type_(type
), args_(args
)
10319 do_traverse(Traverse
* traverse
);
10323 { return this->type_
; }
10326 do_determine_type(const Type_context
*);
10329 do_check_types(Gogo
*);
10334 return new Make_expression(this->type_
, this->args_
->copy(),
10339 do_get_tree(Translate_context
*);
10342 // The type we are making.
10344 // The arguments to pass to the make routine.
10345 Expression_list
* args_
;
10351 Make_expression::do_traverse(Traverse
* traverse
)
10353 if (this->args_
!= NULL
10354 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10355 return TRAVERSE_EXIT
;
10356 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10357 return TRAVERSE_EXIT
;
10358 return TRAVERSE_CONTINUE
;
10361 // Set types of arguments.
10364 Make_expression::do_determine_type(const Type_context
*)
10366 if (this->args_
!= NULL
)
10368 Type_context
context(Type::lookup_integer_type("int"), false);
10369 for (Expression_list::const_iterator pe
= this->args_
->begin();
10370 pe
!= this->args_
->end();
10372 (*pe
)->determine_type(&context
);
10376 // Check types for a make expression.
10379 Make_expression::do_check_types(Gogo
*)
10381 if (this->type_
->channel_type() == NULL
10382 && this->type_
->map_type() == NULL
10383 && (this->type_
->array_type() == NULL
10384 || this->type_
->array_type()->length() != NULL
))
10385 this->report_error(_("invalid type for make function"));
10386 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10387 this->set_is_error();
10390 // Return a tree for a make expression.
10393 Make_expression::do_get_tree(Translate_context
* context
)
10395 return this->type_
->make_expression_tree(context
, this->args_
,
10399 // Make a make expression.
10402 Expression::make_make(Type
* type
, Expression_list
* args
,
10403 source_location location
)
10405 return new Make_expression(type
, args
, location
);
10408 // Construct a struct.
10410 class Struct_construction_expression
: public Expression
10413 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10414 source_location location
)
10415 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10416 type_(type
), vals_(vals
)
10419 // Return whether this is a constant initializer.
10421 is_constant_struct() const;
10425 do_traverse(Traverse
* traverse
);
10429 { return this->type_
; }
10432 do_determine_type(const Type_context
*);
10435 do_check_types(Gogo
*);
10440 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10445 do_is_addressable() const
10449 do_get_tree(Translate_context
*);
10452 do_export(Export
*) const;
10455 // The type of the struct to construct.
10457 // The list of values, in order of the fields in the struct. A NULL
10458 // entry means that the field should be zero-initialized.
10459 Expression_list
* vals_
;
10465 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10467 if (this->vals_
!= NULL
10468 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10469 return TRAVERSE_EXIT
;
10470 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10471 return TRAVERSE_EXIT
;
10472 return TRAVERSE_CONTINUE
;
10475 // Return whether this is a constant initializer.
10478 Struct_construction_expression::is_constant_struct() const
10480 if (this->vals_
== NULL
)
10482 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10483 pv
!= this->vals_
->end();
10487 && !(*pv
)->is_constant()
10488 && (!(*pv
)->is_composite_literal()
10489 || (*pv
)->is_nonconstant_composite_literal()))
10493 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10494 for (Struct_field_list::const_iterator pf
= fields
->begin();
10495 pf
!= fields
->end();
10498 // There are no constant constructors for interfaces.
10499 if (pf
->type()->interface_type() != NULL
)
10506 // Final type determination.
10509 Struct_construction_expression::do_determine_type(const Type_context
*)
10511 if (this->vals_
== NULL
)
10513 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10514 Expression_list::const_iterator pv
= this->vals_
->begin();
10515 for (Struct_field_list::const_iterator pf
= fields
->begin();
10516 pf
!= fields
->end();
10519 if (pv
== this->vals_
->end())
10523 Type_context
subcontext(pf
->type(), false);
10524 (*pv
)->determine_type(&subcontext
);
10532 Struct_construction_expression::do_check_types(Gogo
*)
10534 if (this->vals_
== NULL
)
10537 Struct_type
* st
= this->type_
->struct_type();
10538 if (this->vals_
->size() > st
->field_count())
10540 this->report_error(_("too many expressions for struct"));
10544 const Struct_field_list
* fields
= st
->fields();
10545 Expression_list::const_iterator pv
= this->vals_
->begin();
10547 for (Struct_field_list::const_iterator pf
= fields
->begin();
10548 pf
!= fields
->end();
10551 if (pv
== this->vals_
->end())
10553 this->report_error(_("too few expressions for struct"));
10560 std::string reason
;
10561 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10563 if (reason
.empty())
10564 error_at((*pv
)->location(),
10565 "incompatible type for field %d in struct construction",
10568 error_at((*pv
)->location(),
10569 ("incompatible type for field %d in "
10570 "struct construction (%s)"),
10571 i
+ 1, reason
.c_str());
10572 this->set_is_error();
10575 gcc_assert(pv
== this->vals_
->end());
10578 // Return a tree for constructing a struct.
10581 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10583 Gogo
* gogo
= context
->gogo();
10585 if (this->vals_
== NULL
)
10586 return this->type_
->get_init_tree(gogo
, false);
10588 tree type_tree
= this->type_
->get_tree(gogo
);
10589 if (type_tree
== error_mark_node
)
10590 return error_mark_node
;
10591 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10593 bool is_constant
= true;
10594 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10595 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10597 Struct_field_list::const_iterator pf
= fields
->begin();
10598 Expression_list::const_iterator pv
= this->vals_
->begin();
10599 for (tree field
= TYPE_FIELDS(type_tree
);
10600 field
!= NULL_TREE
;
10601 field
= DECL_CHAIN(field
), ++pf
)
10603 gcc_assert(pf
!= fields
->end());
10606 if (pv
== this->vals_
->end())
10607 val
= pf
->type()->get_init_tree(gogo
, false);
10608 else if (*pv
== NULL
)
10610 val
= pf
->type()->get_init_tree(gogo
, false);
10615 val
= Expression::convert_for_assignment(context
, pf
->type(),
10617 (*pv
)->get_tree(context
),
10622 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10623 return error_mark_node
;
10625 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10626 elt
->index
= field
;
10628 if (!TREE_CONSTANT(val
))
10629 is_constant
= false;
10631 gcc_assert(pf
== fields
->end());
10633 tree ret
= build_constructor(type_tree
, elts
);
10635 TREE_CONSTANT(ret
) = 1;
10639 // Export a struct construction.
10642 Struct_construction_expression::do_export(Export
* exp
) const
10644 exp
->write_c_string("convert(");
10645 exp
->write_type(this->type_
);
10646 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10647 pv
!= this->vals_
->end();
10650 exp
->write_c_string(", ");
10652 (*pv
)->export_expression(exp
);
10654 exp
->write_c_string(")");
10657 // Make a struct composite literal. This used by the thunk code.
10660 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
10661 source_location location
)
10663 gcc_assert(type
->struct_type() != NULL
);
10664 return new Struct_construction_expression(type
, vals
, location
);
10667 // Construct an array. This class is not used directly; instead we
10668 // use the child classes, Fixed_array_construction_expression and
10669 // Open_array_construction_expression.
10671 class Array_construction_expression
: public Expression
10674 Array_construction_expression(Expression_classification classification
,
10675 Type
* type
, Expression_list
* vals
,
10676 source_location location
)
10677 : Expression(classification
, location
),
10678 type_(type
), vals_(vals
)
10682 // Return whether this is a constant initializer.
10684 is_constant_array() const;
10686 // Return the number of elements.
10688 element_count() const
10689 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
10693 do_traverse(Traverse
* traverse
);
10697 { return this->type_
; }
10700 do_determine_type(const Type_context
*);
10703 do_check_types(Gogo
*);
10706 do_is_addressable() const
10710 do_export(Export
*) const;
10712 // The list of values.
10715 { return this->vals_
; }
10717 // Get a constructor tree for the array values.
10719 get_constructor_tree(Translate_context
* context
, tree type_tree
);
10722 // The type of the array to construct.
10724 // The list of values.
10725 Expression_list
* vals_
;
10731 Array_construction_expression::do_traverse(Traverse
* traverse
)
10733 if (this->vals_
!= NULL
10734 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10735 return TRAVERSE_EXIT
;
10736 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10737 return TRAVERSE_EXIT
;
10738 return TRAVERSE_CONTINUE
;
10741 // Return whether this is a constant initializer.
10744 Array_construction_expression::is_constant_array() const
10746 if (this->vals_
== NULL
)
10749 // There are no constant constructors for interfaces.
10750 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
10753 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10754 pv
!= this->vals_
->end();
10758 && !(*pv
)->is_constant()
10759 && (!(*pv
)->is_composite_literal()
10760 || (*pv
)->is_nonconstant_composite_literal()))
10766 // Final type determination.
10769 Array_construction_expression::do_determine_type(const Type_context
*)
10771 if (this->vals_
== NULL
)
10773 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
10774 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10775 pv
!= this->vals_
->end();
10779 (*pv
)->determine_type(&subcontext
);
10786 Array_construction_expression::do_check_types(Gogo
*)
10788 if (this->vals_
== NULL
)
10791 Array_type
* at
= this->type_
->array_type();
10793 Type
* element_type
= at
->element_type();
10794 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10795 pv
!= this->vals_
->end();
10799 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
10801 error_at((*pv
)->location(),
10802 "incompatible type for element %d in composite literal",
10804 this->set_is_error();
10808 Expression
* length
= at
->length();
10809 if (length
!= NULL
)
10814 if (at
->length()->integer_constant_value(true, val
, &type
))
10816 if (this->vals_
->size() > mpz_get_ui(val
))
10817 this->report_error(_("too many elements in composite literal"));
10823 // Get a constructor tree for the array values.
10826 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
10829 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10830 (this->vals_
== NULL
10832 : this->vals_
->size()));
10833 Type
* element_type
= this->type_
->array_type()->element_type();
10834 bool is_constant
= true;
10835 if (this->vals_
!= NULL
)
10838 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10839 pv
!= this->vals_
->end();
10842 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
10843 elt
->index
= size_int(i
);
10845 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10848 tree value_tree
= (*pv
)->get_tree(context
);
10849 elt
->value
= Expression::convert_for_assignment(context
,
10855 if (elt
->value
== error_mark_node
)
10856 return error_mark_node
;
10857 if (!TREE_CONSTANT(elt
->value
))
10858 is_constant
= false;
10862 tree ret
= build_constructor(type_tree
, values
);
10864 TREE_CONSTANT(ret
) = 1;
10868 // Export an array construction.
10871 Array_construction_expression::do_export(Export
* exp
) const
10873 exp
->write_c_string("convert(");
10874 exp
->write_type(this->type_
);
10875 if (this->vals_
!= NULL
)
10877 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10878 pv
!= this->vals_
->end();
10881 exp
->write_c_string(", ");
10883 (*pv
)->export_expression(exp
);
10886 exp
->write_c_string(")");
10889 // Construct a fixed array.
10891 class Fixed_array_construction_expression
:
10892 public Array_construction_expression
10895 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
10896 source_location location
)
10897 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
10898 type
, vals
, location
)
10900 gcc_assert(type
->array_type() != NULL
10901 && type
->array_type()->length() != NULL
);
10908 return new Fixed_array_construction_expression(this->type(),
10909 (this->vals() == NULL
10911 : this->vals()->copy()),
10916 do_get_tree(Translate_context
*);
10919 // Return a tree for constructing a fixed array.
10922 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
10924 return this->get_constructor_tree(context
,
10925 this->type()->get_tree(context
->gogo()));
10928 // Construct an open array.
10930 class Open_array_construction_expression
: public Array_construction_expression
10933 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
10934 source_location location
)
10935 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
10936 type
, vals
, location
)
10938 gcc_assert(type
->array_type() != NULL
10939 && type
->array_type()->length() == NULL
);
10943 // Note that taking the address of an open array literal is invalid.
10948 return new Open_array_construction_expression(this->type(),
10949 (this->vals() == NULL
10951 : this->vals()->copy()),
10956 do_get_tree(Translate_context
*);
10959 // Return a tree for constructing an open array.
10962 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
10964 Array_type
* array_type
= this->type()->array_type();
10965 if (array_type
== NULL
)
10967 gcc_assert(this->type()->is_error_type());
10968 return error_mark_node
;
10971 Type
* element_type
= array_type
->element_type();
10972 tree element_type_tree
= element_type
->get_tree(context
->gogo());
10973 if (element_type_tree
== error_mark_node
)
10974 return error_mark_node
;
10978 if (this->vals() == NULL
|| this->vals()->empty())
10980 // We need to create a unique value.
10981 tree max
= size_int(0);
10982 tree constructor_type
= build_array_type(element_type_tree
,
10983 build_index_type(max
));
10984 if (constructor_type
== error_mark_node
)
10985 return error_mark_node
;
10986 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
10987 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
10988 elt
->index
= size_int(0);
10989 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10990 values
= build_constructor(constructor_type
, vec
);
10991 if (TREE_CONSTANT(elt
->value
))
10992 TREE_CONSTANT(values
) = 1;
10993 length_tree
= size_int(0);
10997 tree max
= size_int(this->vals()->size() - 1);
10998 tree constructor_type
= build_array_type(element_type_tree
,
10999 build_index_type(max
));
11000 if (constructor_type
== error_mark_node
)
11001 return error_mark_node
;
11002 values
= this->get_constructor_tree(context
, constructor_type
);
11003 length_tree
= size_int(this->vals()->size());
11006 if (values
== error_mark_node
)
11007 return error_mark_node
;
11009 bool is_constant_initializer
= TREE_CONSTANT(values
);
11010 bool is_in_function
= context
->function() != NULL
;
11012 if (is_constant_initializer
)
11014 tree tmp
= build_decl(this->location(), VAR_DECL
,
11015 create_tmp_var_name("C"), TREE_TYPE(values
));
11016 DECL_EXTERNAL(tmp
) = 0;
11017 TREE_PUBLIC(tmp
) = 0;
11018 TREE_STATIC(tmp
) = 1;
11019 DECL_ARTIFICIAL(tmp
) = 1;
11020 if (is_in_function
)
11022 // If this is not a function, we will only initialize the
11023 // value once, so we can use this directly rather than
11024 // copying it. In that case we can't make it read-only,
11025 // because the program is permitted to change it.
11026 TREE_READONLY(tmp
) = 1;
11027 TREE_CONSTANT(tmp
) = 1;
11029 DECL_INITIAL(tmp
) = values
;
11030 rest_of_decl_compilation(tmp
, 1, 0);
11036 if (!is_in_function
&& is_constant_initializer
)
11038 // Outside of a function, we know the initializer will only run
11040 space
= build_fold_addr_expr(values
);
11045 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
11046 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
11048 space
= save_expr(space
);
11050 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
11051 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
11052 TREE_THIS_NOTRAP(ref
) = 1;
11053 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
11056 // Build a constructor for the open array.
11058 tree type_tree
= this->type()->get_tree(context
->gogo());
11059 if (type_tree
== error_mark_node
)
11060 return error_mark_node
;
11061 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
11063 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
11065 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11066 tree field
= TYPE_FIELDS(type_tree
);
11067 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
11068 elt
->index
= field
;
11069 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
11071 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11072 field
= DECL_CHAIN(field
);
11073 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
11074 elt
->index
= field
;
11075 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11077 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11078 field
= DECL_CHAIN(field
);
11079 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
11080 elt
->index
= field
;
11081 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11083 tree constructor
= build_constructor(type_tree
, init
);
11084 if (constructor
== error_mark_node
)
11085 return error_mark_node
;
11086 if (!is_in_function
&& is_constant_initializer
)
11087 TREE_CONSTANT(constructor
) = 1;
11089 if (set
== NULL_TREE
)
11090 return constructor
;
11092 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
11095 // Make a slice composite literal. This is used by the type
11096 // descriptor code.
11099 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
11100 source_location location
)
11102 gcc_assert(type
->is_open_array_type());
11103 return new Open_array_construction_expression(type
, vals
, location
);
11106 // Construct a map.
11108 class Map_construction_expression
: public Expression
11111 Map_construction_expression(Type
* type
, Expression_list
* vals
,
11112 source_location location
)
11113 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
11114 type_(type
), vals_(vals
)
11115 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
11119 do_traverse(Traverse
* traverse
);
11123 { return this->type_
; }
11126 do_determine_type(const Type_context
*);
11129 do_check_types(Gogo
*);
11134 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
11139 do_get_tree(Translate_context
*);
11142 do_export(Export
*) const;
11145 // The type of the map to construct.
11147 // The list of values.
11148 Expression_list
* vals_
;
11154 Map_construction_expression::do_traverse(Traverse
* traverse
)
11156 if (this->vals_
!= NULL
11157 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11158 return TRAVERSE_EXIT
;
11159 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11160 return TRAVERSE_EXIT
;
11161 return TRAVERSE_CONTINUE
;
11164 // Final type determination.
11167 Map_construction_expression::do_determine_type(const Type_context
*)
11169 if (this->vals_
== NULL
)
11172 Map_type
* mt
= this->type_
->map_type();
11173 Type_context
key_context(mt
->key_type(), false);
11174 Type_context
val_context(mt
->val_type(), false);
11175 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11176 pv
!= this->vals_
->end();
11179 (*pv
)->determine_type(&key_context
);
11181 (*pv
)->determine_type(&val_context
);
11188 Map_construction_expression::do_check_types(Gogo
*)
11190 if (this->vals_
== NULL
)
11193 Map_type
* mt
= this->type_
->map_type();
11195 Type
* key_type
= mt
->key_type();
11196 Type
* val_type
= mt
->val_type();
11197 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11198 pv
!= this->vals_
->end();
11201 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
11203 error_at((*pv
)->location(),
11204 "incompatible type for element %d key in map construction",
11206 this->set_is_error();
11209 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
11211 error_at((*pv
)->location(),
11212 ("incompatible type for element %d value "
11213 "in map construction"),
11215 this->set_is_error();
11220 // Return a tree for constructing a map.
11223 Map_construction_expression::do_get_tree(Translate_context
* context
)
11225 Gogo
* gogo
= context
->gogo();
11226 source_location loc
= this->location();
11228 Map_type
* mt
= this->type_
->map_type();
11230 // Build a struct to hold the key and value.
11231 tree struct_type
= make_node(RECORD_TYPE
);
11233 Type
* key_type
= mt
->key_type();
11234 tree id
= get_identifier("__key");
11235 tree key_type_tree
= key_type
->get_tree(gogo
);
11236 if (key_type_tree
== error_mark_node
)
11237 return error_mark_node
;
11238 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type_tree
);
11239 DECL_CONTEXT(key_field
) = struct_type
;
11240 TYPE_FIELDS(struct_type
) = key_field
;
11242 Type
* val_type
= mt
->val_type();
11243 id
= get_identifier("__val");
11244 tree val_type_tree
= val_type
->get_tree(gogo
);
11245 if (val_type_tree
== error_mark_node
)
11246 return error_mark_node
;
11247 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type_tree
);
11248 DECL_CONTEXT(val_field
) = struct_type
;
11249 DECL_CHAIN(key_field
) = val_field
;
11251 layout_type(struct_type
);
11253 bool is_constant
= true;
11258 if (this->vals_
== NULL
|| this->vals_
->empty())
11260 valaddr
= null_pointer_node
;
11261 make_tmp
= NULL_TREE
;
11265 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11266 this->vals_
->size() / 2);
11268 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11269 pv
!= this->vals_
->end();
11272 bool one_is_constant
= true;
11274 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11276 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11277 elt
->index
= key_field
;
11278 tree val_tree
= (*pv
)->get_tree(context
);
11279 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11282 if (elt
->value
== error_mark_node
)
11283 return error_mark_node
;
11284 if (!TREE_CONSTANT(elt
->value
))
11285 one_is_constant
= false;
11289 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11290 elt
->index
= val_field
;
11291 val_tree
= (*pv
)->get_tree(context
);
11292 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11295 if (elt
->value
== error_mark_node
)
11296 return error_mark_node
;
11297 if (!TREE_CONSTANT(elt
->value
))
11298 one_is_constant
= false;
11300 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11301 elt
->index
= size_int(i
);
11302 elt
->value
= build_constructor(struct_type
, one
);
11303 if (one_is_constant
)
11304 TREE_CONSTANT(elt
->value
) = 1;
11306 is_constant
= false;
11309 tree index_type
= build_index_type(size_int(i
- 1));
11310 tree array_type
= build_array_type(struct_type
, index_type
);
11311 tree init
= build_constructor(array_type
, values
);
11313 TREE_CONSTANT(init
) = 1;
11315 if (current_function_decl
!= NULL
)
11317 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11318 DECL_INITIAL(tmp
) = init
;
11319 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11320 TREE_ADDRESSABLE(tmp
) = 1;
11324 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11325 DECL_EXTERNAL(tmp
) = 0;
11326 TREE_PUBLIC(tmp
) = 0;
11327 TREE_STATIC(tmp
) = 1;
11328 DECL_ARTIFICIAL(tmp
) = 1;
11329 if (!TREE_CONSTANT(init
))
11330 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11334 TREE_READONLY(tmp
) = 1;
11335 TREE_CONSTANT(tmp
) = 1;
11336 DECL_INITIAL(tmp
) = init
;
11337 make_tmp
= NULL_TREE
;
11339 rest_of_decl_compilation(tmp
, 1, 0);
11342 valaddr
= build_fold_addr_expr(tmp
);
11345 tree descriptor
= gogo
->map_descriptor(mt
);
11347 tree type_tree
= this->type_
->get_tree(gogo
);
11348 if (type_tree
== error_mark_node
)
11349 return error_mark_node
;
11351 static tree construct_map_fndecl
;
11352 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11354 "__go_construct_map",
11357 TREE_TYPE(descriptor
),
11362 TYPE_SIZE_UNIT(struct_type
),
11364 byte_position(val_field
),
11366 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11367 const_ptr_type_node
,
11368 fold_convert(const_ptr_type_node
, valaddr
));
11369 if (call
== error_mark_node
)
11370 return error_mark_node
;
11373 if (make_tmp
== NULL
)
11376 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11380 // Export an array construction.
11383 Map_construction_expression::do_export(Export
* exp
) const
11385 exp
->write_c_string("convert(");
11386 exp
->write_type(this->type_
);
11387 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11388 pv
!= this->vals_
->end();
11391 exp
->write_c_string(", ");
11392 (*pv
)->export_expression(exp
);
11394 exp
->write_c_string(")");
11397 // A general composite literal. This is lowered to a type specific
11400 class Composite_literal_expression
: public Parser_expression
11403 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11404 Expression_list
* vals
, source_location location
)
11405 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11406 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11411 do_traverse(Traverse
* traverse
);
11414 do_lower(Gogo
*, Named_object
*, int);
11419 return new Composite_literal_expression(this->type_
, this->depth_
,
11421 (this->vals_
== NULL
11423 : this->vals_
->copy()),
11429 lower_struct(Type
*);
11432 lower_array(Type
*);
11435 make_array(Type
*, Expression_list
*);
11438 lower_map(Gogo
*, Named_object
*, Type
*);
11440 // The type of the composite literal.
11442 // The depth within a list of composite literals within a composite
11443 // literal, when the type is omitted.
11445 // The values to put in the composite literal.
11446 Expression_list
* vals_
;
11447 // If this is true, then VALS_ is a list of pairs: a key and a
11448 // value. In an array initializer, a missing key will be NULL.
11455 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11457 if (this->vals_
!= NULL
11458 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11459 return TRAVERSE_EXIT
;
11460 return Type::traverse(this->type_
, traverse
);
11463 // Lower a generic composite literal into a specific version based on
11467 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
11469 Type
* type
= this->type_
;
11471 for (int depth
= this->depth_
; depth
> 0; --depth
)
11473 if (type
->array_type() != NULL
)
11474 type
= type
->array_type()->element_type();
11475 else if (type
->map_type() != NULL
)
11476 type
= type
->map_type()->val_type();
11479 if (!type
->is_error_type())
11480 error_at(this->location(),
11481 ("may only omit types within composite literals "
11482 "of slice, array, or map type"));
11483 return Expression::make_error(this->location());
11487 if (type
->is_error_type())
11488 return Expression::make_error(this->location());
11489 else if (type
->struct_type() != NULL
)
11490 return this->lower_struct(type
);
11491 else if (type
->array_type() != NULL
)
11492 return this->lower_array(type
);
11493 else if (type
->map_type() != NULL
)
11494 return this->lower_map(gogo
, function
, type
);
11497 error_at(this->location(),
11498 ("expected struct, slice, array, or map type "
11499 "for composite literal"));
11500 return Expression::make_error(this->location());
11504 // Lower a struct composite literal.
11507 Composite_literal_expression::lower_struct(Type
* type
)
11509 source_location location
= this->location();
11510 Struct_type
* st
= type
->struct_type();
11511 if (this->vals_
== NULL
|| !this->has_keys_
)
11512 return new Struct_construction_expression(type
, this->vals_
, location
);
11514 size_t field_count
= st
->field_count();
11515 std::vector
<Expression
*> vals(field_count
);
11516 Expression_list::const_iterator p
= this->vals_
->begin();
11517 while (p
!= this->vals_
->end())
11519 Expression
* name_expr
= *p
;
11522 gcc_assert(p
!= this->vals_
->end());
11523 Expression
* val
= *p
;
11527 if (name_expr
== NULL
)
11529 error_at(val
->location(), "mixture of field and value initializers");
11530 return Expression::make_error(location
);
11533 bool bad_key
= false;
11535 switch (name_expr
->classification())
11537 case EXPRESSION_UNKNOWN_REFERENCE
:
11538 name
= name_expr
->unknown_expression()->name();
11541 case EXPRESSION_CONST_REFERENCE
:
11542 name
= static_cast<Const_expression
*>(name_expr
)->name();
11545 case EXPRESSION_TYPE
:
11547 Type
* t
= name_expr
->type();
11548 Named_type
* nt
= t
->named_type();
11556 case EXPRESSION_VAR_REFERENCE
:
11557 name
= name_expr
->var_expression()->name();
11560 case EXPRESSION_FUNC_REFERENCE
:
11561 name
= name_expr
->func_expression()->name();
11564 case EXPRESSION_UNARY
:
11565 // If there is a local variable around with the same name as
11566 // the field, and this occurs in the closure, then the
11567 // parser may turn the field reference into an indirection
11568 // through the closure. FIXME: This is a mess.
11571 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11572 if (ue
->op() == OPERATOR_MULT
)
11574 Field_reference_expression
* fre
=
11575 ue
->operand()->field_reference_expression();
11579 fre
->expr()->type()->deref()->struct_type();
11582 const Struct_field
* sf
= st
->field(fre
->field_index());
11583 name
= sf
->field_name();
11585 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11586 size_t buflen
= strlen(buf
);
11587 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11590 name
= name
.substr(0, name
.length() - buflen
);
11605 error_at(name_expr
->location(), "expected struct field name");
11606 return Expression::make_error(location
);
11609 unsigned int index
;
11610 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11613 error_at(name_expr
->location(), "unknown field %qs in %qs",
11614 Gogo::message_name(name
).c_str(),
11615 (type
->named_type() != NULL
11616 ? type
->named_type()->message_name().c_str()
11617 : "unnamed struct"));
11618 return Expression::make_error(location
);
11620 if (vals
[index
] != NULL
)
11622 error_at(name_expr
->location(),
11623 "duplicate value for field %qs in %qs",
11624 Gogo::message_name(name
).c_str(),
11625 (type
->named_type() != NULL
11626 ? type
->named_type()->message_name().c_str()
11627 : "unnamed struct"));
11628 return Expression::make_error(location
);
11634 Expression_list
* list
= new Expression_list
;
11635 list
->reserve(field_count
);
11636 for (size_t i
= 0; i
< field_count
; ++i
)
11637 list
->push_back(vals
[i
]);
11639 return new Struct_construction_expression(type
, list
, location
);
11642 // Lower an array composite literal.
11645 Composite_literal_expression::lower_array(Type
* type
)
11647 source_location location
= this->location();
11648 if (this->vals_
== NULL
|| !this->has_keys_
)
11649 return this->make_array(type
, this->vals_
);
11651 std::vector
<Expression
*> vals
;
11652 vals
.reserve(this->vals_
->size());
11653 unsigned long index
= 0;
11654 Expression_list::const_iterator p
= this->vals_
->begin();
11655 while (p
!= this->vals_
->end())
11657 Expression
* index_expr
= *p
;
11660 gcc_assert(p
!= this->vals_
->end());
11661 Expression
* val
= *p
;
11665 if (index_expr
!= NULL
)
11670 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
11673 error_at(index_expr
->location(),
11674 "index expression is not integer constant");
11675 return Expression::make_error(location
);
11677 if (mpz_sgn(ival
) < 0)
11680 error_at(index_expr
->location(), "index expression is negative");
11681 return Expression::make_error(location
);
11683 index
= mpz_get_ui(ival
);
11684 if (mpz_cmp_ui(ival
, index
) != 0)
11687 error_at(index_expr
->location(), "index value overflow");
11688 return Expression::make_error(location
);
11693 if (index
== vals
.size())
11694 vals
.push_back(val
);
11697 if (index
> vals
.size())
11699 vals
.reserve(index
+ 32);
11700 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
11702 if (vals
[index
] != NULL
)
11704 error_at((index_expr
!= NULL
11705 ? index_expr
->location()
11706 : val
->location()),
11707 "duplicate value for index %lu",
11709 return Expression::make_error(location
);
11717 size_t size
= vals
.size();
11718 Expression_list
* list
= new Expression_list
;
11719 list
->reserve(size
);
11720 for (size_t i
= 0; i
< size
; ++i
)
11721 list
->push_back(vals
[i
]);
11723 return this->make_array(type
, list
);
11726 // Actually build the array composite literal. This handles
11730 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
11732 source_location location
= this->location();
11733 Array_type
* at
= type
->array_type();
11734 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
11736 size_t size
= vals
== NULL
? 0 : vals
->size();
11738 mpz_init_set_ui(vlen
, size
);
11739 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
11741 at
= Type::make_array_type(at
->element_type(), elen
);
11744 if (at
->length() != NULL
)
11745 return new Fixed_array_construction_expression(type
, vals
, location
);
11747 return new Open_array_construction_expression(type
, vals
, location
);
11750 // Lower a map composite literal.
11753 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
11756 source_location location
= this->location();
11757 if (this->vals_
!= NULL
)
11759 if (!this->has_keys_
)
11761 error_at(location
, "map composite literal must have keys");
11762 return Expression::make_error(location
);
11765 for (Expression_list::iterator p
= this->vals_
->begin();
11766 p
!= this->vals_
->end();
11772 error_at((*p
)->location(),
11773 "map composite literal must have keys for every value");
11774 return Expression::make_error(location
);
11776 // Make sure we have lowered the key; it may not have been
11777 // lowered in order to handle keys for struct composite
11778 // literals. Lower it now to get the right error message.
11779 if ((*p
)->unknown_expression() != NULL
)
11781 (*p
)->unknown_expression()->clear_is_composite_literal_key();
11782 gogo
->lower_expression(function
, &*p
);
11783 gcc_assert((*p
)->is_error_expression());
11784 return Expression::make_error(location
);
11789 return new Map_construction_expression(type
, this->vals_
, location
);
11792 // Make a composite literal expression.
11795 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
11796 Expression_list
* vals
,
11797 source_location location
)
11799 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
11803 // Return whether this expression is a composite literal.
11806 Expression::is_composite_literal() const
11808 switch (this->classification_
)
11810 case EXPRESSION_COMPOSITE_LITERAL
:
11811 case EXPRESSION_STRUCT_CONSTRUCTION
:
11812 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11813 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11814 case EXPRESSION_MAP_CONSTRUCTION
:
11821 // Return whether this expression is a composite literal which is not
11825 Expression::is_nonconstant_composite_literal() const
11827 switch (this->classification_
)
11829 case EXPRESSION_STRUCT_CONSTRUCTION
:
11831 const Struct_construction_expression
*psce
=
11832 static_cast<const Struct_construction_expression
*>(this);
11833 return !psce
->is_constant_struct();
11835 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11837 const Fixed_array_construction_expression
*pace
=
11838 static_cast<const Fixed_array_construction_expression
*>(this);
11839 return !pace
->is_constant_array();
11841 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11843 const Open_array_construction_expression
*pace
=
11844 static_cast<const Open_array_construction_expression
*>(this);
11845 return !pace
->is_constant_array();
11847 case EXPRESSION_MAP_CONSTRUCTION
:
11854 // Return true if this is a reference to a local variable.
11857 Expression::is_local_variable() const
11859 const Var_expression
* ve
= this->var_expression();
11862 const Named_object
* no
= ve
->named_object();
11863 return (no
->is_result_variable()
11864 || (no
->is_variable() && !no
->var_value()->is_global()));
11867 // Class Type_guard_expression.
11872 Type_guard_expression::do_traverse(Traverse
* traverse
)
11874 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
11875 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11876 return TRAVERSE_EXIT
;
11877 return TRAVERSE_CONTINUE
;
11880 // Check types of a type guard expression. The expression must have
11881 // an interface type, but the actual type conversion is checked at run
11885 Type_guard_expression::do_check_types(Gogo
*)
11887 // 6g permits using a type guard with unsafe.pointer; we are
11889 Type
* expr_type
= this->expr_
->type();
11890 if (expr_type
->is_unsafe_pointer_type())
11892 if (this->type_
->points_to() == NULL
11893 && (this->type_
->integer_type() == NULL
11894 || (this->type_
->forwarded()
11895 != Type::lookup_integer_type("uintptr"))))
11896 this->report_error(_("invalid unsafe.Pointer conversion"));
11898 else if (this->type_
->is_unsafe_pointer_type())
11900 if (expr_type
->points_to() == NULL
11901 && (expr_type
->integer_type() == NULL
11902 || (expr_type
->forwarded()
11903 != Type::lookup_integer_type("uintptr"))))
11904 this->report_error(_("invalid unsafe.Pointer conversion"));
11906 else if (expr_type
->interface_type() == NULL
)
11908 if (!expr_type
->is_error_type() && !this->type_
->is_error_type())
11909 this->report_error(_("type assertion only valid for interface types"));
11910 this->set_is_error();
11912 else if (this->type_
->interface_type() == NULL
)
11914 std::string reason
;
11915 if (!expr_type
->interface_type()->implements_interface(this->type_
,
11918 if (!this->type_
->is_error_type())
11920 if (reason
.empty())
11921 this->report_error(_("impossible type assertion: "
11922 "type does not implement interface"));
11924 error_at(this->location(),
11925 ("impossible type assertion: "
11926 "type does not implement interface (%s)"),
11929 this->set_is_error();
11934 // Return a tree for a type guard expression.
11937 Type_guard_expression::do_get_tree(Translate_context
* context
)
11939 Gogo
* gogo
= context
->gogo();
11940 tree expr_tree
= this->expr_
->get_tree(context
);
11941 if (expr_tree
== error_mark_node
)
11942 return error_mark_node
;
11943 Type
* expr_type
= this->expr_
->type();
11944 if ((this->type_
->is_unsafe_pointer_type()
11945 && (expr_type
->points_to() != NULL
11946 || expr_type
->integer_type() != NULL
))
11947 || (expr_type
->is_unsafe_pointer_type()
11948 && this->type_
->points_to() != NULL
))
11949 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
11950 else if (expr_type
->is_unsafe_pointer_type()
11951 && this->type_
->integer_type() != NULL
)
11952 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
11953 else if (this->type_
->interface_type() != NULL
)
11954 return Expression::convert_interface_to_interface(context
, this->type_
,
11955 this->expr_
->type(),
11959 return Expression::convert_for_assignment(context
, this->type_
,
11960 this->expr_
->type(), expr_tree
,
11964 // Make a type guard expression.
11967 Expression::make_type_guard(Expression
* expr
, Type
* type
,
11968 source_location location
)
11970 return new Type_guard_expression(expr
, type
, location
);
11973 // Class Heap_composite_expression.
11975 // When you take the address of a composite literal, it is allocated
11976 // on the heap. This class implements that.
11978 class Heap_composite_expression
: public Expression
11981 Heap_composite_expression(Expression
* expr
, source_location location
)
11982 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
11988 do_traverse(Traverse
* traverse
)
11989 { return Expression::traverse(&this->expr_
, traverse
); }
11993 { return Type::make_pointer_type(this->expr_
->type()); }
11996 do_determine_type(const Type_context
*)
11997 { this->expr_
->determine_type_no_context(); }
12002 return Expression::make_heap_composite(this->expr_
->copy(),
12007 do_get_tree(Translate_context
*);
12009 // We only export global objects, and the parser does not generate
12010 // this in global scope.
12012 do_export(Export
*) const
12013 { gcc_unreachable(); }
12016 // The composite literal which is being put on the heap.
12020 // Return a tree which allocates a composite literal on the heap.
12023 Heap_composite_expression::do_get_tree(Translate_context
* context
)
12025 tree expr_tree
= this->expr_
->get_tree(context
);
12026 if (expr_tree
== error_mark_node
)
12027 return error_mark_node
;
12028 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
12029 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
12030 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
12031 expr_size
, this->location());
12032 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
12033 space
= save_expr(space
);
12034 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
12035 TREE_THIS_NOTRAP(ref
) = 1;
12036 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
12037 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
12039 SET_EXPR_LOCATION(ret
, this->location());
12043 // Allocate a composite literal on the heap.
12046 Expression::make_heap_composite(Expression
* expr
, source_location location
)
12048 return new Heap_composite_expression(expr
, location
);
12051 // Class Receive_expression.
12053 // Return the type of a receive expression.
12056 Receive_expression::do_type()
12058 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12059 if (channel_type
== NULL
)
12060 return Type::make_error_type();
12061 return channel_type
->element_type();
12064 // Check types for a receive expression.
12067 Receive_expression::do_check_types(Gogo
*)
12069 Type
* type
= this->channel_
->type();
12070 if (type
->is_error_type())
12072 this->set_is_error();
12075 if (type
->channel_type() == NULL
)
12077 this->report_error(_("expected channel"));
12080 if (!type
->channel_type()->may_receive())
12082 this->report_error(_("invalid receive on send-only channel"));
12087 // Get a tree for a receive expression.
12090 Receive_expression::do_get_tree(Translate_context
* context
)
12092 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12093 gcc_assert(channel_type
!= NULL
);
12094 Type
* element_type
= channel_type
->element_type();
12095 tree element_type_tree
= element_type
->get_tree(context
->gogo());
12097 tree channel
= this->channel_
->get_tree(context
);
12098 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
12099 return error_mark_node
;
12101 return Gogo::receive_from_channel(element_type_tree
, channel
,
12102 this->for_select_
, this->location());
12105 // Make a receive expression.
12107 Receive_expression
*
12108 Expression::make_receive(Expression
* channel
, source_location location
)
12110 return new Receive_expression(channel
, location
);
12113 // Class Send_expression.
12118 Send_expression::do_traverse(Traverse
* traverse
)
12120 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
12121 return TRAVERSE_EXIT
;
12122 return Expression::traverse(&this->val_
, traverse
);
12128 Send_expression::do_type()
12130 return Type::lookup_bool_type();
12136 Send_expression::do_determine_type(const Type_context
*)
12138 this->channel_
->determine_type_no_context();
12140 Type
* type
= this->channel_
->type();
12141 Type_context subcontext
;
12142 if (type
->channel_type() != NULL
)
12143 subcontext
.type
= type
->channel_type()->element_type();
12144 this->val_
->determine_type(&subcontext
);
12150 Send_expression::do_check_types(Gogo
*)
12152 Type
* type
= this->channel_
->type();
12153 if (type
->is_error_type())
12155 this->set_is_error();
12158 Channel_type
* channel_type
= type
->channel_type();
12159 if (channel_type
== NULL
)
12161 error_at(this->location(), "left operand of %<<-%> must be channel");
12162 this->set_is_error();
12165 Type
* element_type
= channel_type
->element_type();
12166 if (element_type
!= NULL
12167 && !Type::are_assignable(element_type
, this->val_
->type(), NULL
))
12169 this->report_error(_("incompatible types in send"));
12172 if (!channel_type
->may_send())
12174 this->report_error(_("invalid send on receive-only channel"));
12179 // Get a tree for a send expression.
12182 Send_expression::do_get_tree(Translate_context
* context
)
12184 tree channel
= this->channel_
->get_tree(context
);
12185 tree val
= this->val_
->get_tree(context
);
12186 if (channel
== error_mark_node
|| val
== error_mark_node
)
12187 return error_mark_node
;
12188 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12189 val
= Expression::convert_for_assignment(context
,
12190 channel_type
->element_type(),
12191 this->val_
->type(),
12194 return Gogo::send_on_channel(channel
, val
, this->is_value_discarded_
,
12195 this->for_select_
, this->location());
12198 // Make a send expression
12201 Expression::make_send(Expression
* channel
, Expression
* val
,
12202 source_location location
)
12204 return new Send_expression(channel
, val
, location
);
12207 // An expression which evaluates to a pointer to the type descriptor
12210 class Type_descriptor_expression
: public Expression
12213 Type_descriptor_expression(Type
* type
, source_location location
)
12214 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
12221 { return Type::make_type_descriptor_ptr_type(); }
12224 do_determine_type(const Type_context
*)
12232 do_get_tree(Translate_context
* context
)
12233 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
12236 // The type for which this is the descriptor.
12240 // Make a type descriptor expression.
12243 Expression::make_type_descriptor(Type
* type
, source_location location
)
12245 return new Type_descriptor_expression(type
, location
);
12248 // An expression which evaluates to some characteristic of a type.
12249 // This is only used to initialize fields of a type descriptor. Using
12250 // a new expression class is slightly inefficient but gives us a good
12251 // separation between the frontend and the middle-end with regard to
12252 // how types are laid out.
12254 class Type_info_expression
: public Expression
12257 Type_info_expression(Type
* type
, Type_info type_info
)
12258 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
12259 type_(type
), type_info_(type_info
)
12267 do_determine_type(const Type_context
*)
12275 do_get_tree(Translate_context
* context
);
12278 // The type for which we are getting information.
12280 // What information we want.
12281 Type_info type_info_
;
12284 // The type is chosen to match what the type descriptor struct
12288 Type_info_expression::do_type()
12290 switch (this->type_info_
)
12292 case TYPE_INFO_SIZE
:
12293 return Type::lookup_integer_type("uintptr");
12294 case TYPE_INFO_ALIGNMENT
:
12295 case TYPE_INFO_FIELD_ALIGNMENT
:
12296 return Type::lookup_integer_type("uint8");
12302 // Return type information in GENERIC.
12305 Type_info_expression::do_get_tree(Translate_context
* context
)
12307 tree type_tree
= this->type_
->get_tree(context
->gogo());
12308 if (type_tree
== error_mark_node
)
12309 return error_mark_node
;
12311 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12312 gcc_assert(val_type_tree
!= error_mark_node
);
12314 if (this->type_info_
== TYPE_INFO_SIZE
)
12315 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12316 TYPE_SIZE_UNIT(type_tree
));
12320 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12321 val
= go_type_alignment(type_tree
);
12323 val
= go_field_alignment(type_tree
);
12324 return build_int_cstu(val_type_tree
, val
);
12328 // Make a type info expression.
12331 Expression::make_type_info(Type
* type
, Type_info type_info
)
12333 return new Type_info_expression(type
, type_info
);
12336 // An expression which evaluates to the offset of a field within a
12337 // struct. This, like Type_info_expression, q.v., is only used to
12338 // initialize fields of a type descriptor.
12340 class Struct_field_offset_expression
: public Expression
12343 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12344 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12345 type_(type
), field_(field
)
12351 { return Type::lookup_integer_type("uintptr"); }
12354 do_determine_type(const Type_context
*)
12362 do_get_tree(Translate_context
* context
);
12365 // The type of the struct.
12366 Struct_type
* type_
;
12368 const Struct_field
* field_
;
12371 // Return a struct field offset in GENERIC.
12374 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12376 tree type_tree
= this->type_
->get_tree(context
->gogo());
12377 if (type_tree
== error_mark_node
)
12378 return error_mark_node
;
12380 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12381 gcc_assert(val_type_tree
!= error_mark_node
);
12383 const Struct_field_list
* fields
= this->type_
->fields();
12384 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12385 Struct_field_list::const_iterator p
;
12386 for (p
= fields
->begin();
12387 p
!= fields
->end();
12388 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12390 gcc_assert(struct_field_tree
!= NULL_TREE
);
12391 if (&*p
== this->field_
)
12394 gcc_assert(&*p
== this->field_
);
12396 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12397 byte_position(struct_field_tree
));
12400 // Make an expression for a struct field offset.
12403 Expression::make_struct_field_offset(Struct_type
* type
,
12404 const Struct_field
* field
)
12406 return new Struct_field_offset_expression(type
, field
);
12409 // An expression which evaluates to the address of an unnamed label.
12411 class Label_addr_expression
: public Expression
12414 Label_addr_expression(Label
* label
, source_location location
)
12415 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12422 { return Type::make_pointer_type(Type::make_void_type()); }
12425 do_determine_type(const Type_context
*)
12430 { return new Label_addr_expression(this->label_
, this->location()); }
12433 do_get_tree(Translate_context
*)
12434 { return this->label_
->get_addr(this->location()); }
12437 // The label whose address we are taking.
12441 // Make an expression for the address of an unnamed label.
12444 Expression::make_label_addr(Label
* label
, source_location location
)
12446 return new Label_addr_expression(label
, location
);
12449 // Import an expression. This comes at the end in order to see the
12450 // various class definitions.
12453 Expression::import_expression(Import
* imp
)
12455 int c
= imp
->peek_char();
12456 if (imp
->match_c_string("- ")
12457 || imp
->match_c_string("! ")
12458 || imp
->match_c_string("^ "))
12459 return Unary_expression::do_import(imp
);
12461 return Binary_expression::do_import(imp
);
12462 else if (imp
->match_c_string("true")
12463 || imp
->match_c_string("false"))
12464 return Boolean_expression::do_import(imp
);
12466 return String_expression::do_import(imp
);
12467 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12469 // This handles integers, floats and complex constants.
12470 return Integer_expression::do_import(imp
);
12472 else if (imp
->match_c_string("nil"))
12473 return Nil_expression::do_import(imp
);
12474 else if (imp
->match_c_string("convert"))
12475 return Type_conversion_expression::do_import(imp
);
12478 error_at(imp
->location(), "import error: expected expression");
12479 return Expression::make_error(imp
->location());
12483 // Class Expression_list.
12485 // Traverse the list.
12488 Expression_list::traverse(Traverse
* traverse
)
12490 for (Expression_list::iterator p
= this->begin();
12496 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12497 return TRAVERSE_EXIT
;
12500 return TRAVERSE_CONTINUE
;
12506 Expression_list::copy()
12508 Expression_list
* ret
= new Expression_list();
12509 for (Expression_list::iterator p
= this->begin();
12514 ret
->push_back(NULL
);
12516 ret
->push_back((*p
)->copy());
12521 // Return whether an expression list has an error expression.
12524 Expression_list::contains_error() const
12526 for (Expression_list::const_iterator p
= this->begin();
12529 if (*p
!= NULL
&& (*p
)->is_error_expression())