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_
;
1251 Named_object
* real
= no
->unknown_value()->real_named_object();
1254 if (this->is_composite_literal_key_
)
1256 error_at(location
, "reference to undefined name %qs",
1257 this->named_object_
->message_name().c_str());
1258 return Expression::make_error(location
);
1260 switch (real
->classification())
1262 case Named_object::NAMED_OBJECT_CONST
:
1263 return Expression::make_const_reference(real
, location
);
1264 case Named_object::NAMED_OBJECT_TYPE
:
1265 return Expression::make_type(real
->type_value(), location
);
1266 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1267 if (this->is_composite_literal_key_
)
1269 error_at(location
, "reference to undefined type %qs",
1270 real
->message_name().c_str());
1271 return Expression::make_error(location
);
1272 case Named_object::NAMED_OBJECT_VAR
:
1273 return Expression::make_var_reference(real
, location
);
1274 case Named_object::NAMED_OBJECT_FUNC
:
1275 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1276 return Expression::make_func_reference(real
, NULL
, location
);
1277 case Named_object::NAMED_OBJECT_PACKAGE
:
1278 if (this->is_composite_literal_key_
)
1280 error_at(location
, "unexpected reference to package");
1281 return Expression::make_error(location
);
1287 // Make a reference to an unknown name.
1290 Expression::make_unknown_reference(Named_object
* no
, source_location location
)
1292 gcc_assert(no
->resolve()->is_unknown());
1293 return new Unknown_expression(no
, location
);
1296 // A boolean expression.
1298 class Boolean_expression
: public Expression
1301 Boolean_expression(bool val
, source_location location
)
1302 : Expression(EXPRESSION_BOOLEAN
, location
),
1303 val_(val
), type_(NULL
)
1311 do_is_constant() const
1318 do_determine_type(const Type_context
*);
1325 do_get_tree(Translate_context
*)
1326 { return this->val_
? boolean_true_node
: boolean_false_node
; }
1329 do_export(Export
* exp
) const
1330 { exp
->write_c_string(this->val_
? "true" : "false"); }
1335 // The type as determined by context.
1342 Boolean_expression::do_type()
1344 if (this->type_
== NULL
)
1345 this->type_
= Type::make_boolean_type();
1349 // Set the type from the context.
1352 Boolean_expression::do_determine_type(const Type_context
* context
)
1354 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1356 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1357 this->type_
= context
->type
;
1358 else if (!context
->may_be_abstract
)
1359 this->type_
= Type::lookup_bool_type();
1362 // Import a boolean constant.
1365 Boolean_expression::do_import(Import
* imp
)
1367 if (imp
->peek_char() == 't')
1369 imp
->require_c_string("true");
1370 return Expression::make_boolean(true, imp
->location());
1374 imp
->require_c_string("false");
1375 return Expression::make_boolean(false, imp
->location());
1379 // Make a boolean expression.
1382 Expression::make_boolean(bool val
, source_location location
)
1384 return new Boolean_expression(val
, location
);
1387 // Class String_expression.
1392 String_expression::do_type()
1394 if (this->type_
== NULL
)
1395 this->type_
= Type::make_string_type();
1399 // Set the type from the context.
1402 String_expression::do_determine_type(const Type_context
* context
)
1404 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1406 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1407 this->type_
= context
->type
;
1408 else if (!context
->may_be_abstract
)
1409 this->type_
= Type::lookup_string_type();
1412 // Build a string constant.
1415 String_expression::do_get_tree(Translate_context
* context
)
1417 return context
->gogo()->go_string_constant_tree(this->val_
);
1420 // Export a string expression.
1423 String_expression::do_export(Export
* exp
) const
1426 s
.reserve(this->val_
.length() * 4 + 2);
1428 for (std::string::const_iterator p
= this->val_
.begin();
1429 p
!= this->val_
.end();
1432 if (*p
== '\\' || *p
== '"')
1437 else if (*p
>= 0x20 && *p
< 0x7f)
1439 else if (*p
== '\n')
1441 else if (*p
== '\t')
1446 unsigned char c
= *p
;
1447 unsigned int dig
= c
>> 4;
1448 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1450 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1454 exp
->write_string(s
);
1457 // Import a string expression.
1460 String_expression::do_import(Import
* imp
)
1462 imp
->require_c_string("\"");
1466 int c
= imp
->get_char();
1467 if (c
== '"' || c
== -1)
1470 val
+= static_cast<char>(c
);
1473 c
= imp
->get_char();
1474 if (c
== '\\' || c
== '"')
1475 val
+= static_cast<char>(c
);
1482 c
= imp
->get_char();
1483 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1484 c
= imp
->get_char();
1485 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1486 char v
= (vh
<< 4) | vl
;
1491 error_at(imp
->location(), "bad string constant");
1492 return Expression::make_error(imp
->location());
1496 return Expression::make_string(val
, imp
->location());
1499 // Make a string expression.
1502 Expression::make_string(const std::string
& val
, source_location location
)
1504 return new String_expression(val
, location
);
1507 // Make an integer expression.
1509 class Integer_expression
: public Expression
1512 Integer_expression(const mpz_t
* val
, Type
* type
, source_location location
)
1513 : Expression(EXPRESSION_INTEGER
, location
),
1515 { mpz_init_set(this->val_
, *val
); }
1520 // Return whether VAL fits in the type.
1522 check_constant(mpz_t val
, Type
*, source_location
);
1524 // Write VAL to export data.
1526 export_integer(Export
* exp
, const mpz_t val
);
1530 do_is_constant() const
1534 do_integer_constant_value(bool, mpz_t val
, Type
** ptype
) const;
1540 do_determine_type(const Type_context
* context
);
1543 do_check_types(Gogo
*);
1546 do_get_tree(Translate_context
*);
1550 { return Expression::make_integer(&this->val_
, this->type_
,
1551 this->location()); }
1554 do_export(Export
*) const;
1557 // The integer value.
1563 // Return an integer constant value.
1566 Integer_expression::do_integer_constant_value(bool, mpz_t val
,
1569 if (this->type_
!= NULL
)
1570 *ptype
= this->type_
;
1571 mpz_set(val
, this->val_
);
1575 // Return the current type. If we haven't set the type yet, we return
1576 // an abstract integer type.
1579 Integer_expression::do_type()
1581 if (this->type_
== NULL
)
1582 this->type_
= Type::make_abstract_integer_type();
1586 // Set the type of the integer value. Here we may switch from an
1587 // abstract type to a real type.
1590 Integer_expression::do_determine_type(const Type_context
* context
)
1592 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1594 else if (context
->type
!= NULL
1595 && (context
->type
->integer_type() != NULL
1596 || context
->type
->float_type() != NULL
1597 || context
->type
->complex_type() != NULL
))
1598 this->type_
= context
->type
;
1599 else if (!context
->may_be_abstract
)
1600 this->type_
= Type::lookup_integer_type("int");
1603 // Return true if the integer VAL fits in the range of the type TYPE.
1604 // Otherwise give an error and return false. TYPE may be NULL.
1607 Integer_expression::check_constant(mpz_t val
, Type
* type
,
1608 source_location location
)
1612 Integer_type
* itype
= type
->integer_type();
1613 if (itype
== NULL
|| itype
->is_abstract())
1616 int bits
= mpz_sizeinbase(val
, 2);
1618 if (itype
->is_unsigned())
1620 // For an unsigned type we can only accept a nonnegative number,
1621 // and we must be able to represent at least BITS.
1622 if (mpz_sgn(val
) >= 0
1623 && bits
<= itype
->bits())
1628 // For a signed type we need an extra bit to indicate the sign.
1629 // We have to handle the most negative integer specially.
1630 if (bits
+ 1 <= itype
->bits()
1631 || (bits
<= itype
->bits()
1633 && (mpz_scan1(val
, 0)
1634 == static_cast<unsigned long>(itype
->bits() - 1))
1635 && mpz_scan0(val
, itype
->bits()) == ULONG_MAX
))
1639 error_at(location
, "integer constant overflow");
1643 // Check the type of an integer constant.
1646 Integer_expression::do_check_types(Gogo
*)
1648 if (this->type_
== NULL
)
1650 if (!Integer_expression::check_constant(this->val_
, this->type_
,
1652 this->set_is_error();
1655 // Get a tree for an integer constant.
1658 Integer_expression::do_get_tree(Translate_context
* context
)
1660 Gogo
* gogo
= context
->gogo();
1662 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1663 type
= this->type_
->get_tree(gogo
);
1664 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
1666 // We are converting to an abstract floating point type.
1667 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
1669 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
1671 // We are converting to an abstract complex type.
1672 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
1676 // If we still have an abstract type here, then this is being
1677 // used in a constant expression which didn't get reduced for
1678 // some reason. Use a type which will fit the value. We use <,
1679 // not <=, because we need an extra bit for the sign bit.
1680 int bits
= mpz_sizeinbase(this->val_
, 2);
1681 if (bits
< INT_TYPE_SIZE
)
1682 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
1684 type
= Type::lookup_integer_type("int64")->get_tree(gogo
);
1686 type
= long_long_integer_type_node
;
1688 return Expression::integer_constant_tree(this->val_
, type
);
1691 // Write VAL to export data.
1694 Integer_expression::export_integer(Export
* exp
, const mpz_t val
)
1696 char* s
= mpz_get_str(NULL
, 10, val
);
1697 exp
->write_c_string(s
);
1701 // Export an integer in a constant expression.
1704 Integer_expression::do_export(Export
* exp
) const
1706 Integer_expression::export_integer(exp
, this->val_
);
1707 // A trailing space lets us reliably identify the end of the number.
1708 exp
->write_c_string(" ");
1711 // Import an integer, floating point, or complex value. This handles
1712 // all these types because they all start with digits.
1715 Integer_expression::do_import(Import
* imp
)
1717 std::string num
= imp
->read_identifier();
1718 imp
->require_c_string(" ");
1719 if (!num
.empty() && num
[num
.length() - 1] == 'i')
1722 size_t plus_pos
= num
.find('+', 1);
1723 size_t minus_pos
= num
.find('-', 1);
1725 if (plus_pos
== std::string::npos
)
1727 else if (minus_pos
== std::string::npos
)
1731 error_at(imp
->location(), "bad number in import data: %qs",
1733 return Expression::make_error(imp
->location());
1735 if (pos
== std::string::npos
)
1736 mpfr_set_ui(real
, 0, GMP_RNDN
);
1739 std::string real_str
= num
.substr(0, pos
);
1740 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
1742 error_at(imp
->location(), "bad number in import data: %qs",
1744 return Expression::make_error(imp
->location());
1748 std::string imag_str
;
1749 if (pos
== std::string::npos
)
1752 imag_str
= num
.substr(pos
);
1753 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
1755 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
1757 error_at(imp
->location(), "bad number in import data: %qs",
1759 return Expression::make_error(imp
->location());
1761 Expression
* ret
= Expression::make_complex(&real
, &imag
, NULL
,
1767 else if (num
.find('.') == std::string::npos
1768 && num
.find('E') == std::string::npos
)
1771 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
1773 error_at(imp
->location(), "bad number in import data: %qs",
1775 return Expression::make_error(imp
->location());
1777 Expression
* ret
= Expression::make_integer(&val
, NULL
, imp
->location());
1784 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
1786 error_at(imp
->location(), "bad number in import data: %qs",
1788 return Expression::make_error(imp
->location());
1790 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
1796 // Build a new integer value.
1799 Expression::make_integer(const mpz_t
* val
, Type
* type
,
1800 source_location location
)
1802 return new Integer_expression(val
, type
, location
);
1807 class Float_expression
: public Expression
1810 Float_expression(const mpfr_t
* val
, Type
* type
, source_location location
)
1811 : Expression(EXPRESSION_FLOAT
, location
),
1814 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
1817 // Constrain VAL to fit into TYPE.
1819 constrain_float(mpfr_t val
, Type
* type
);
1821 // Return whether VAL fits in the type.
1823 check_constant(mpfr_t val
, Type
*, source_location
);
1825 // Write VAL to export data.
1827 export_float(Export
* exp
, const mpfr_t val
);
1831 do_is_constant() const
1835 do_float_constant_value(mpfr_t val
, Type
**) const;
1841 do_determine_type(const Type_context
*);
1844 do_check_types(Gogo
*);
1848 { return Expression::make_float(&this->val_
, this->type_
,
1849 this->location()); }
1852 do_get_tree(Translate_context
*);
1855 do_export(Export
*) const;
1858 // The floating point value.
1864 // Constrain VAL to fit into TYPE.
1867 Float_expression::constrain_float(mpfr_t val
, Type
* type
)
1869 Float_type
* ftype
= type
->float_type();
1870 if (ftype
!= NULL
&& !ftype
->is_abstract())
1872 tree type_tree
= ftype
->type_tree();
1873 REAL_VALUE_TYPE rvt
;
1874 real_from_mpfr(&rvt
, val
, type_tree
, GMP_RNDN
);
1875 real_convert(&rvt
, TYPE_MODE(type_tree
), &rvt
);
1876 mpfr_from_real(val
, &rvt
, GMP_RNDN
);
1880 // Return a floating point constant value.
1883 Float_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
1885 if (this->type_
!= NULL
)
1886 *ptype
= this->type_
;
1887 mpfr_set(val
, this->val_
, GMP_RNDN
);
1891 // Return the current type. If we haven't set the type yet, we return
1892 // an abstract float type.
1895 Float_expression::do_type()
1897 if (this->type_
== NULL
)
1898 this->type_
= Type::make_abstract_float_type();
1902 // Set the type of the float value. Here we may switch from an
1903 // abstract type to a real type.
1906 Float_expression::do_determine_type(const Type_context
* context
)
1908 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1910 else if (context
->type
!= NULL
1911 && (context
->type
->integer_type() != NULL
1912 || context
->type
->float_type() != NULL
1913 || context
->type
->complex_type() != NULL
))
1914 this->type_
= context
->type
;
1915 else if (!context
->may_be_abstract
)
1916 this->type_
= Type::lookup_float_type("float");
1919 // Return true if the floating point value VAL fits in the range of
1920 // the type TYPE. Otherwise give an error and return false. TYPE may
1924 Float_expression::check_constant(mpfr_t val
, Type
* type
,
1925 source_location location
)
1929 Float_type
* ftype
= type
->float_type();
1930 if (ftype
== NULL
|| ftype
->is_abstract())
1933 // A NaN or Infinity always fits in the range of the type.
1934 if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
1937 mp_exp_t exp
= mpfr_get_exp(val
);
1939 switch (ftype
->bits())
1952 error_at(location
, "floating point constant overflow");
1958 // Check the type of a float value.
1961 Float_expression::do_check_types(Gogo
*)
1963 if (this->type_
== NULL
)
1966 if (!Float_expression::check_constant(this->val_
, this->type_
,
1968 this->set_is_error();
1970 Integer_type
* integer_type
= this->type_
->integer_type();
1971 if (integer_type
!= NULL
)
1973 if (!mpfr_integer_p(this->val_
))
1974 this->report_error(_("floating point constant truncated to integer"));
1977 gcc_assert(!integer_type
->is_abstract());
1980 mpfr_get_z(ival
, this->val_
, GMP_RNDN
);
1981 Integer_expression::check_constant(ival
, integer_type
,
1988 // Get a tree for a float constant.
1991 Float_expression::do_get_tree(Translate_context
* context
)
1993 Gogo
* gogo
= context
->gogo();
1995 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1996 type
= this->type_
->get_tree(gogo
);
1997 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
1999 // We have an abstract integer type. We just hope for the best.
2000 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
2004 // If we still have an abstract type here, then this is being
2005 // used in a constant expression which didn't get reduced. We
2006 // just use float64 and hope for the best.
2007 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
2009 return Expression::float_constant_tree(this->val_
, type
);
2012 // Write a floating point number to export data.
2015 Float_expression::export_float(Export
*exp
, const mpfr_t val
)
2018 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2020 exp
->write_c_string("-");
2021 exp
->write_c_string("0.");
2022 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2025 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2026 exp
->write_c_string(buf
);
2029 // Export a floating point number in a constant expression.
2032 Float_expression::do_export(Export
* exp
) const
2034 Float_expression::export_float(exp
, this->val_
);
2035 // A trailing space lets us reliably identify the end of the number.
2036 exp
->write_c_string(" ");
2039 // Make a float expression.
2042 Expression::make_float(const mpfr_t
* val
, Type
* type
, source_location location
)
2044 return new Float_expression(val
, type
, location
);
2049 class Complex_expression
: public Expression
2052 Complex_expression(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2053 source_location location
)
2054 : Expression(EXPRESSION_COMPLEX
, location
),
2057 mpfr_init_set(this->real_
, *real
, GMP_RNDN
);
2058 mpfr_init_set(this->imag_
, *imag
, GMP_RNDN
);
2061 // Constrain REAL/IMAG to fit into TYPE.
2063 constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
);
2065 // Return whether REAL/IMAG fits in the type.
2067 check_constant(mpfr_t real
, mpfr_t imag
, Type
*, source_location
);
2069 // Write REAL/IMAG to export data.
2071 export_complex(Export
* exp
, const mpfr_t real
, const mpfr_t val
);
2075 do_is_constant() const
2079 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2085 do_determine_type(const Type_context
*);
2088 do_check_types(Gogo
*);
2093 return Expression::make_complex(&this->real_
, &this->imag_
, this->type_
,
2098 do_get_tree(Translate_context
*);
2101 do_export(Export
*) const;
2106 // The imaginary part;
2108 // The type if known.
2112 // Constrain REAL/IMAG to fit into TYPE.
2115 Complex_expression::constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
)
2117 Complex_type
* ctype
= type
->complex_type();
2118 if (ctype
!= NULL
&& !ctype
->is_abstract())
2120 tree type_tree
= ctype
->type_tree();
2122 REAL_VALUE_TYPE rvt
;
2123 real_from_mpfr(&rvt
, real
, TREE_TYPE(type_tree
), GMP_RNDN
);
2124 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2125 mpfr_from_real(real
, &rvt
, GMP_RNDN
);
2127 real_from_mpfr(&rvt
, imag
, TREE_TYPE(type_tree
), GMP_RNDN
);
2128 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2129 mpfr_from_real(imag
, &rvt
, GMP_RNDN
);
2133 // Return a complex constant value.
2136 Complex_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2139 if (this->type_
!= NULL
)
2140 *ptype
= this->type_
;
2141 mpfr_set(real
, this->real_
, GMP_RNDN
);
2142 mpfr_set(imag
, this->imag_
, GMP_RNDN
);
2146 // Return the current type. If we haven't set the type yet, we return
2147 // an abstract complex type.
2150 Complex_expression::do_type()
2152 if (this->type_
== NULL
)
2153 this->type_
= Type::make_abstract_complex_type();
2157 // Set the type of the complex value. Here we may switch from an
2158 // abstract type to a real type.
2161 Complex_expression::do_determine_type(const Type_context
* context
)
2163 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2165 else if (context
->type
!= NULL
2166 && context
->type
->complex_type() != NULL
)
2167 this->type_
= context
->type
;
2168 else if (!context
->may_be_abstract
)
2169 this->type_
= Type::lookup_complex_type("complex");
2172 // Return true if the complex value REAL/IMAG fits in the range of the
2173 // type TYPE. Otherwise give an error and return false. TYPE may be
2177 Complex_expression::check_constant(mpfr_t real
, mpfr_t imag
, Type
* type
,
2178 source_location location
)
2182 Complex_type
* ctype
= type
->complex_type();
2183 if (ctype
== NULL
|| ctype
->is_abstract())
2187 switch (ctype
->bits())
2199 // A NaN or Infinity always fits in the range of the type.
2200 if (!mpfr_nan_p(real
) && !mpfr_inf_p(real
) && !mpfr_zero_p(real
))
2202 if (mpfr_get_exp(real
) > max_exp
)
2204 error_at(location
, "complex real part constant overflow");
2209 if (!mpfr_nan_p(imag
) && !mpfr_inf_p(imag
) && !mpfr_zero_p(imag
))
2211 if (mpfr_get_exp(imag
) > max_exp
)
2213 error_at(location
, "complex imaginary part constant overflow");
2221 // Check the type of a complex value.
2224 Complex_expression::do_check_types(Gogo
*)
2226 if (this->type_
== NULL
)
2229 if (!Complex_expression::check_constant(this->real_
, this->imag_
,
2230 this->type_
, this->location()))
2231 this->set_is_error();
2234 // Get a tree for a complex constant.
2237 Complex_expression::do_get_tree(Translate_context
* context
)
2239 Gogo
* gogo
= context
->gogo();
2241 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2242 type
= this->type_
->get_tree(gogo
);
2245 // If we still have an abstract type here, this this is being
2246 // used in a constant expression which didn't get reduced. We
2247 // just use complex128 and hope for the best.
2248 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
2250 return Expression::complex_constant_tree(this->real_
, this->imag_
, type
);
2253 // Write REAL/IMAG to export data.
2256 Complex_expression::export_complex(Export
* exp
, const mpfr_t real
,
2259 if (!mpfr_zero_p(real
))
2261 Float_expression::export_float(exp
, real
);
2262 if (mpfr_sgn(imag
) > 0)
2263 exp
->write_c_string("+");
2265 Float_expression::export_float(exp
, imag
);
2266 exp
->write_c_string("i");
2269 // Export a complex number in a constant expression.
2272 Complex_expression::do_export(Export
* exp
) const
2274 Complex_expression::export_complex(exp
, this->real_
, this->imag_
);
2275 // A trailing space lets us reliably identify the end of the number.
2276 exp
->write_c_string(" ");
2279 // Make a complex expression.
2282 Expression::make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2283 source_location location
)
2285 return new Complex_expression(real
, imag
, type
, location
);
2288 // Find a named object in an expression.
2290 class Find_named_object
: public Traverse
2293 Find_named_object(Named_object
* no
)
2294 : Traverse(traverse_expressions
),
2295 no_(no
), found_(false)
2298 // Whether we found the object.
2301 { return this->found_
; }
2305 expression(Expression
**);
2308 // The object we are looking for.
2310 // Whether we found it.
2314 // A reference to a const in an expression.
2316 class Const_expression
: public Expression
2319 Const_expression(Named_object
* constant
, source_location location
)
2320 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2321 constant_(constant
), type_(NULL
), seen_(false)
2326 { return this->constant_
; }
2330 { return this->constant_
->name(); }
2334 do_lower(Gogo
*, Named_object
*, int);
2337 do_is_constant() const
2341 do_integer_constant_value(bool, mpz_t val
, Type
**) const;
2344 do_float_constant_value(mpfr_t val
, Type
**) const;
2347 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2350 do_string_constant_value(std::string
* val
) const
2351 { return this->constant_
->const_value()->expr()->string_constant_value(val
); }
2356 // The type of a const is set by the declaration, not the use.
2358 do_determine_type(const Type_context
*);
2361 do_check_types(Gogo
*);
2368 do_get_tree(Translate_context
* context
);
2370 // When exporting a reference to a const as part of a const
2371 // expression, we export the value. We ignore the fact that it has
2374 do_export(Export
* exp
) const
2375 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2379 Named_object
* constant_
;
2380 // The type of this reference. This is used if the constant has an
2383 // Used to prevent infinite recursion when a constant incorrectly
2384 // refers to itself.
2388 // Lower a constant expression. This is where we convert the
2389 // predeclared constant iota into an integer value.
2392 Const_expression::do_lower(Gogo
* gogo
, Named_object
*, int iota_value
)
2394 if (this->constant_
->const_value()->expr()->classification()
2397 if (iota_value
== -1)
2399 error_at(this->location(),
2400 "iota is only defined in const declarations");
2404 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2405 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2411 // Make sure that the constant itself has been lowered.
2412 gogo
->lower_constant(this->constant_
);
2417 // Return an integer constant value.
2420 Const_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
2427 if (this->type_
!= NULL
)
2428 ctype
= this->type_
;
2430 ctype
= this->constant_
->const_value()->type();
2431 if (ctype
!= NULL
&& ctype
->integer_type() == NULL
)
2434 Expression
* e
= this->constant_
->const_value()->expr();
2439 bool r
= e
->integer_constant_value(iota_is_constant
, val
, &t
);
2441 this->seen_
= false;
2445 && !Integer_expression::check_constant(val
, ctype
, this->location()))
2448 *ptype
= ctype
!= NULL
? ctype
: t
;
2452 // Return a floating point constant value.
2455 Const_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
2461 if (this->type_
!= NULL
)
2462 ctype
= this->type_
;
2464 ctype
= this->constant_
->const_value()->type();
2465 if (ctype
!= NULL
&& ctype
->float_type() == NULL
)
2471 bool r
= this->constant_
->const_value()->expr()->float_constant_value(val
,
2474 this->seen_
= false;
2476 if (r
&& ctype
!= NULL
)
2478 if (!Float_expression::check_constant(val
, ctype
, this->location()))
2480 Float_expression::constrain_float(val
, ctype
);
2482 *ptype
= ctype
!= NULL
? ctype
: t
;
2486 // Return a complex constant value.
2489 Const_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2496 if (this->type_
!= NULL
)
2497 ctype
= this->type_
;
2499 ctype
= this->constant_
->const_value()->type();
2500 if (ctype
!= NULL
&& ctype
->complex_type() == NULL
)
2506 bool r
= this->constant_
->const_value()->expr()->complex_constant_value(real
,
2510 this->seen_
= false;
2512 if (r
&& ctype
!= NULL
)
2514 if (!Complex_expression::check_constant(real
, imag
, ctype
,
2517 Complex_expression::constrain_complex(real
, imag
, ctype
);
2519 *ptype
= ctype
!= NULL
? ctype
: t
;
2523 // Return the type of the const reference.
2526 Const_expression::do_type()
2528 if (this->type_
!= NULL
)
2531 Named_constant
* nc
= this->constant_
->const_value();
2533 if (this->seen_
|| nc
->lowering())
2535 this->report_error(_("constant refers to itself"));
2536 this->type_
= Type::make_error_type();
2542 Type
* ret
= nc
->type();
2546 this->seen_
= false;
2550 // During parsing, a named constant may have a NULL type, but we
2551 // must not return a NULL type here.
2552 ret
= nc
->expr()->type();
2554 this->seen_
= false;
2559 // Set the type of the const reference.
2562 Const_expression::do_determine_type(const Type_context
* context
)
2564 Type
* ctype
= this->constant_
->const_value()->type();
2565 Type
* cetype
= (ctype
!= NULL
2567 : this->constant_
->const_value()->expr()->type());
2568 if (ctype
!= NULL
&& !ctype
->is_abstract())
2570 else if (context
->type
!= NULL
2571 && (context
->type
->integer_type() != NULL
2572 || context
->type
->float_type() != NULL
2573 || context
->type
->complex_type() != NULL
)
2574 && (cetype
->integer_type() != NULL
2575 || cetype
->float_type() != NULL
2576 || cetype
->complex_type() != NULL
))
2577 this->type_
= context
->type
;
2578 else if (context
->type
!= NULL
2579 && context
->type
->is_string_type()
2580 && cetype
->is_string_type())
2581 this->type_
= context
->type
;
2582 else if (context
->type
!= NULL
2583 && context
->type
->is_boolean_type()
2584 && cetype
->is_boolean_type())
2585 this->type_
= context
->type
;
2586 else if (!context
->may_be_abstract
)
2588 if (cetype
->is_abstract())
2589 cetype
= cetype
->make_non_abstract_type();
2590 this->type_
= cetype
;
2594 // Check types of a const reference.
2597 Const_expression::do_check_types(Gogo
*)
2599 if (this->type_
!= NULL
&& this->type_
->is_error_type())
2602 Expression
* init
= this->constant_
->const_value()->expr();
2603 Find_named_object
find_named_object(this->constant_
);
2604 Expression::traverse(&init
, &find_named_object
);
2605 if (find_named_object
.found())
2607 this->report_error(_("constant refers to itself"));
2608 this->type_
= Type::make_error_type();
2612 if (this->type_
== NULL
|| this->type_
->is_abstract())
2615 // Check for integer overflow.
2616 if (this->type_
->integer_type() != NULL
)
2621 if (!this->integer_constant_value(true, ival
, &dummy
))
2625 Expression
* cexpr
= this->constant_
->const_value()->expr();
2626 if (cexpr
->float_constant_value(fval
, &dummy
))
2628 if (!mpfr_integer_p(fval
))
2629 this->report_error(_("floating point constant "
2630 "truncated to integer"));
2633 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2634 Integer_expression::check_constant(ival
, this->type_
,
2644 // Return a tree for the const reference.
2647 Const_expression::do_get_tree(Translate_context
* context
)
2649 Gogo
* gogo
= context
->gogo();
2651 if (this->type_
== NULL
)
2652 type_tree
= NULL_TREE
;
2655 type_tree
= this->type_
->get_tree(gogo
);
2656 if (type_tree
== error_mark_node
)
2657 return error_mark_node
;
2660 // If the type has been set for this expression, but the underlying
2661 // object is an abstract int or float, we try to get the abstract
2662 // value. Otherwise we may lose something in the conversion.
2663 if (this->type_
!= NULL
2664 && this->constant_
->const_value()->type()->is_abstract())
2666 Expression
* expr
= this->constant_
->const_value()->expr();
2670 if (expr
->integer_constant_value(true, ival
, &t
))
2672 tree ret
= Expression::integer_constant_tree(ival
, type_tree
);
2680 if (expr
->float_constant_value(fval
, &t
))
2682 tree ret
= Expression::float_constant_tree(fval
, type_tree
);
2689 if (expr
->complex_constant_value(fval
, imag
, &t
))
2691 tree ret
= Expression::complex_constant_tree(fval
, imag
, type_tree
);
2700 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2701 if (this->type_
== NULL
2702 || const_tree
== error_mark_node
2703 || TREE_TYPE(const_tree
) == error_mark_node
)
2707 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2708 ret
= fold_convert(type_tree
, const_tree
);
2709 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2710 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2711 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2712 ret
= fold(convert_to_real(type_tree
, const_tree
));
2713 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2714 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2720 // Make a reference to a constant in an expression.
2723 Expression::make_const_reference(Named_object
* constant
,
2724 source_location location
)
2726 return new Const_expression(constant
, location
);
2729 // Find a named object in an expression.
2732 Find_named_object::expression(Expression
** pexpr
)
2734 switch ((*pexpr
)->classification())
2736 case Expression::EXPRESSION_CONST_REFERENCE
:
2737 if (static_cast<Const_expression
*>(*pexpr
)->named_object() == this->no_
)
2739 return TRAVERSE_CONTINUE
;
2740 case Expression::EXPRESSION_VAR_REFERENCE
:
2741 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
2743 return TRAVERSE_CONTINUE
;
2744 case Expression::EXPRESSION_FUNC_REFERENCE
:
2745 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
2747 return TRAVERSE_CONTINUE
;
2749 return TRAVERSE_CONTINUE
;
2751 this->found_
= true;
2752 return TRAVERSE_EXIT
;
2757 class Nil_expression
: public Expression
2760 Nil_expression(source_location location
)
2761 : Expression(EXPRESSION_NIL
, location
)
2769 do_is_constant() const
2774 { return Type::make_nil_type(); }
2777 do_determine_type(const Type_context
*)
2785 do_get_tree(Translate_context
*)
2786 { return null_pointer_node
; }
2789 do_export(Export
* exp
) const
2790 { exp
->write_c_string("nil"); }
2793 // Import a nil expression.
2796 Nil_expression::do_import(Import
* imp
)
2798 imp
->require_c_string("nil");
2799 return Expression::make_nil(imp
->location());
2802 // Make a nil expression.
2805 Expression::make_nil(source_location location
)
2807 return new Nil_expression(location
);
2810 // The value of the predeclared constant iota. This is little more
2811 // than a marker. This will be lowered to an integer in
2812 // Const_expression::do_lower, which is where we know the value that
2815 class Iota_expression
: public Parser_expression
2818 Iota_expression(source_location location
)
2819 : Parser_expression(EXPRESSION_IOTA
, location
)
2824 do_lower(Gogo
*, Named_object
*, int)
2825 { gcc_unreachable(); }
2827 // There should only ever be one of these.
2830 { gcc_unreachable(); }
2833 // Make an iota expression. This is only called for one case: the
2834 // value of the predeclared constant iota.
2837 Expression::make_iota()
2839 static Iota_expression
iota_expression(UNKNOWN_LOCATION
);
2840 return &iota_expression
;
2843 // A type conversion expression.
2845 class Type_conversion_expression
: public Expression
2848 Type_conversion_expression(Type
* type
, Expression
* expr
,
2849 source_location location
)
2850 : Expression(EXPRESSION_CONVERSION
, location
),
2851 type_(type
), expr_(expr
), may_convert_function_types_(false)
2854 // Return the type to which we are converting.
2857 { return this->type_
; }
2859 // Return the expression which we are converting.
2862 { return this->expr_
; }
2864 // Permit converting from one function type to another. This is
2865 // used internally for method expressions.
2867 set_may_convert_function_types()
2869 this->may_convert_function_types_
= true;
2872 // Import a type conversion expression.
2878 do_traverse(Traverse
* traverse
);
2881 do_lower(Gogo
*, Named_object
*, int);
2884 do_is_constant() const
2885 { return this->expr_
->is_constant(); }
2888 do_integer_constant_value(bool, mpz_t
, Type
**) const;
2891 do_float_constant_value(mpfr_t
, Type
**) const;
2894 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
2897 do_string_constant_value(std::string
*) const;
2901 { return this->type_
; }
2904 do_determine_type(const Type_context
*)
2906 Type_context
subcontext(this->type_
, false);
2907 this->expr_
->determine_type(&subcontext
);
2911 do_check_types(Gogo
*);
2916 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
2921 do_get_tree(Translate_context
* context
);
2924 do_export(Export
*) const;
2927 // The type to convert to.
2929 // The expression to convert.
2931 // True if this is permitted to convert function types. This is
2932 // used internally for method expressions.
2933 bool may_convert_function_types_
;
2939 Type_conversion_expression::do_traverse(Traverse
* traverse
)
2941 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
2942 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
2943 return TRAVERSE_EXIT
;
2944 return TRAVERSE_CONTINUE
;
2947 // Convert to a constant at lowering time.
2950 Type_conversion_expression::do_lower(Gogo
*, Named_object
*, int)
2952 Type
* type
= this->type_
;
2953 Expression
* val
= this->expr_
;
2954 source_location location
= this->location();
2956 if (type
->integer_type() != NULL
)
2961 if (val
->integer_constant_value(false, ival
, &dummy
))
2963 if (!Integer_expression::check_constant(ival
, type
, location
))
2964 mpz_set_ui(ival
, 0);
2965 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2972 if (val
->float_constant_value(fval
, &dummy
))
2974 if (!mpfr_integer_p(fval
))
2977 "floating point constant truncated to integer");
2978 return Expression::make_error(location
);
2980 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2981 if (!Integer_expression::check_constant(ival
, type
, location
))
2982 mpz_set_ui(ival
, 0);
2983 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2992 if (type
->float_type() != NULL
)
2997 if (val
->float_constant_value(fval
, &dummy
))
2999 if (!Float_expression::check_constant(fval
, type
, location
))
3000 mpfr_set_ui(fval
, 0, GMP_RNDN
);
3001 Float_expression::constrain_float(fval
, type
);
3002 Expression
*ret
= Expression::make_float(&fval
, type
, location
);
3009 if (type
->complex_type() != NULL
)
3016 if (val
->complex_constant_value(real
, imag
, &dummy
))
3018 if (!Complex_expression::check_constant(real
, imag
, type
, location
))
3020 mpfr_set_ui(real
, 0, GMP_RNDN
);
3021 mpfr_set_ui(imag
, 0, GMP_RNDN
);
3023 Complex_expression::constrain_complex(real
, imag
, type
);
3024 Expression
* ret
= Expression::make_complex(&real
, &imag
, type
,
3034 if (type
->is_open_array_type() && type
->named_type() == NULL
)
3036 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3037 bool is_byte
= element_type
== Type::lookup_integer_type("uint8");
3038 bool is_int
= element_type
== Type::lookup_integer_type("int");
3039 if (is_byte
|| is_int
)
3042 if (val
->string_constant_value(&s
))
3044 Expression_list
* vals
= new Expression_list();
3047 for (std::string::const_iterator p
= s
.begin();
3052 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
3053 Expression
* v
= Expression::make_integer(&val
,
3062 const char *p
= s
.data();
3063 const char *pend
= s
.data() + s
.length();
3067 int adv
= Lex::fetch_char(p
, &c
);
3070 warning_at(this->location(), 0,
3071 "invalid UTF-8 encoding");
3076 mpz_init_set_ui(val
, c
);
3077 Expression
* v
= Expression::make_integer(&val
,
3085 return Expression::make_slice_composite_literal(type
, vals
,
3094 // Return the constant integer value if there is one.
3097 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant
,
3101 if (this->type_
->integer_type() == NULL
)
3107 if (this->expr_
->integer_constant_value(iota_is_constant
, ival
, &dummy
))
3109 if (!Integer_expression::check_constant(ival
, this->type_
,
3117 *ptype
= this->type_
;
3124 if (this->expr_
->float_constant_value(fval
, &dummy
))
3126 mpfr_get_z(val
, fval
, GMP_RNDN
);
3128 if (!Integer_expression::check_constant(val
, this->type_
,
3131 *ptype
= this->type_
;
3139 // Return the constant floating point value if there is one.
3142 Type_conversion_expression::do_float_constant_value(mpfr_t val
,
3145 if (this->type_
->float_type() == NULL
)
3151 if (this->expr_
->float_constant_value(fval
, &dummy
))
3153 if (!Float_expression::check_constant(fval
, this->type_
,
3159 mpfr_set(val
, fval
, GMP_RNDN
);
3161 Float_expression::constrain_float(val
, this->type_
);
3162 *ptype
= this->type_
;
3170 // Return the constant complex value if there is one.
3173 Type_conversion_expression::do_complex_constant_value(mpfr_t real
,
3177 if (this->type_
->complex_type() == NULL
)
3185 if (this->expr_
->complex_constant_value(rval
, ival
, &dummy
))
3187 if (!Complex_expression::check_constant(rval
, ival
, this->type_
,
3194 mpfr_set(real
, rval
, GMP_RNDN
);
3195 mpfr_set(imag
, ival
, GMP_RNDN
);
3198 Complex_expression::constrain_complex(real
, imag
, this->type_
);
3199 *ptype
= this->type_
;
3208 // Return the constant string value if there is one.
3211 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3213 if (this->type_
->is_string_type()
3214 && this->expr_
->type()->integer_type() != NULL
)
3219 if (this->expr_
->integer_constant_value(false, ival
, &dummy
))
3221 unsigned long ulval
= mpz_get_ui(ival
);
3222 if (mpz_cmp_ui(ival
, ulval
) == 0)
3224 Lex::append_char(ulval
, true, val
, this->location());
3232 // FIXME: Could handle conversion from const []int here.
3237 // Check that types are convertible.
3240 Type_conversion_expression::do_check_types(Gogo
*)
3242 Type
* type
= this->type_
;
3243 Type
* expr_type
= this->expr_
->type();
3246 if (type
->is_error_type()
3247 || type
->is_undefined()
3248 || expr_type
->is_error_type()
3249 || expr_type
->is_undefined())
3251 // Make sure we emit an error for an undefined type.
3254 this->set_is_error();
3258 if (this->may_convert_function_types_
3259 && type
->function_type() != NULL
3260 && expr_type
->function_type() != NULL
)
3263 if (Type::are_convertible(type
, expr_type
, &reason
))
3266 error_at(this->location(), "%s", reason
.c_str());
3267 this->set_is_error();
3270 // Get a tree for a type conversion.
3273 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3275 Gogo
* gogo
= context
->gogo();
3276 tree type_tree
= this->type_
->get_tree(gogo
);
3277 tree expr_tree
= this->expr_
->get_tree(context
);
3279 if (type_tree
== error_mark_node
3280 || expr_tree
== error_mark_node
3281 || TREE_TYPE(expr_tree
) == error_mark_node
)
3282 return error_mark_node
;
3284 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3285 return fold_convert(type_tree
, expr_tree
);
3287 Type
* type
= this->type_
;
3288 Type
* expr_type
= this->expr_
->type();
3290 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3291 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3292 expr_tree
, this->location());
3293 else if (type
->integer_type() != NULL
)
3295 if (expr_type
->integer_type() != NULL
3296 || expr_type
->float_type() != NULL
3297 || expr_type
->is_unsafe_pointer_type())
3298 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3302 else if (type
->float_type() != NULL
)
3304 if (expr_type
->integer_type() != NULL
3305 || expr_type
->float_type() != NULL
)
3306 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3310 else if (type
->complex_type() != NULL
)
3312 if (expr_type
->complex_type() != NULL
)
3313 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3317 else if (type
->is_string_type()
3318 && expr_type
->integer_type() != NULL
)
3320 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3321 if (host_integerp(expr_tree
, 0))
3323 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3325 Lex::append_char(intval
, true, &s
, this->location());
3326 Expression
* se
= Expression::make_string(s
, this->location());
3327 return se
->get_tree(context
);
3330 static tree int_to_string_fndecl
;
3331 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3333 "__go_int_to_string",
3337 fold_convert(integer_type_node
, expr_tree
));
3339 else if (type
->is_string_type()
3340 && (expr_type
->array_type() != NULL
3341 || (expr_type
->points_to() != NULL
3342 && expr_type
->points_to()->array_type() != NULL
)))
3344 Type
* t
= expr_type
;
3345 if (t
->points_to() != NULL
)
3348 expr_tree
= build_fold_indirect_ref(expr_tree
);
3350 if (!DECL_P(expr_tree
))
3351 expr_tree
= save_expr(expr_tree
);
3352 Array_type
* a
= t
->array_type();
3353 Type
* e
= a
->element_type()->forwarded();
3354 gcc_assert(e
->integer_type() != NULL
);
3355 tree valptr
= fold_convert(const_ptr_type_node
,
3356 a
->value_pointer_tree(gogo
, expr_tree
));
3357 tree len
= a
->length_tree(gogo
, expr_tree
);
3358 len
= fold_convert_loc(this->location(), size_type_node
, len
);
3359 if (e
->integer_type()->is_unsigned()
3360 && e
->integer_type()->bits() == 8)
3362 static tree byte_array_to_string_fndecl
;
3363 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3365 "__go_byte_array_to_string",
3368 const_ptr_type_node
,
3375 gcc_assert(e
== Type::lookup_integer_type("int"));
3376 static tree int_array_to_string_fndecl
;
3377 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3379 "__go_int_array_to_string",
3382 const_ptr_type_node
,
3388 else if (type
->is_open_array_type() && expr_type
->is_string_type())
3390 Type
* e
= type
->array_type()->element_type()->forwarded();
3391 gcc_assert(e
->integer_type() != NULL
);
3392 if (e
->integer_type()->is_unsigned()
3393 && e
->integer_type()->bits() == 8)
3395 static tree string_to_byte_array_fndecl
;
3396 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3398 "__go_string_to_byte_array",
3401 TREE_TYPE(expr_tree
),
3406 gcc_assert(e
== Type::lookup_integer_type("int"));
3407 static tree string_to_int_array_fndecl
;
3408 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3410 "__go_string_to_int_array",
3413 TREE_TYPE(expr_tree
),
3417 else if ((type
->is_unsafe_pointer_type()
3418 && expr_type
->points_to() != NULL
)
3419 || (expr_type
->is_unsafe_pointer_type()
3420 && type
->points_to() != NULL
))
3421 ret
= fold_convert(type_tree
, expr_tree
);
3422 else if (type
->is_unsafe_pointer_type()
3423 && expr_type
->integer_type() != NULL
)
3424 ret
= convert_to_pointer(type_tree
, expr_tree
);
3425 else if (this->may_convert_function_types_
3426 && type
->function_type() != NULL
3427 && expr_type
->function_type() != NULL
)
3428 ret
= fold_convert_loc(this->location(), type_tree
, expr_tree
);
3430 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3431 expr_tree
, this->location());
3436 // Output a type conversion in a constant expression.
3439 Type_conversion_expression::do_export(Export
* exp
) const
3441 exp
->write_c_string("convert(");
3442 exp
->write_type(this->type_
);
3443 exp
->write_c_string(", ");
3444 this->expr_
->export_expression(exp
);
3445 exp
->write_c_string(")");
3448 // Import a type conversion or a struct construction.
3451 Type_conversion_expression::do_import(Import
* imp
)
3453 imp
->require_c_string("convert(");
3454 Type
* type
= imp
->read_type();
3455 imp
->require_c_string(", ");
3456 Expression
* val
= Expression::import_expression(imp
);
3457 imp
->require_c_string(")");
3458 return Expression::make_cast(type
, val
, imp
->location());
3461 // Make a type cast expression.
3464 Expression::make_cast(Type
* type
, Expression
* val
, source_location location
)
3466 if (type
->is_error_type() || val
->is_error_expression())
3467 return Expression::make_error(location
);
3468 return new Type_conversion_expression(type
, val
, location
);
3471 // Unary expressions.
3473 class Unary_expression
: public Expression
3476 Unary_expression(Operator op
, Expression
* expr
, source_location location
)
3477 : Expression(EXPRESSION_UNARY
, location
),
3478 op_(op
), escapes_(true), expr_(expr
)
3481 // Return the operator.
3484 { return this->op_
; }
3486 // Return the operand.
3489 { return this->expr_
; }
3491 // Record that an address expression does not escape.
3493 set_does_not_escape()
3495 gcc_assert(this->op_
== OPERATOR_AND
);
3496 this->escapes_
= false;
3499 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3500 // could be done, false if not.
3502 eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3505 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3506 // could be done, false if not.
3508 eval_float(Operator op
, mpfr_t uval
, mpfr_t val
);
3510 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3511 // true if this could be done, false if not.
3513 eval_complex(Operator op
, mpfr_t ureal
, mpfr_t uimag
, mpfr_t real
,
3521 do_traverse(Traverse
* traverse
)
3522 { return Expression::traverse(&this->expr_
, traverse
); }
3525 do_lower(Gogo
*, Named_object
*, int);
3528 do_is_constant() const;
3531 do_integer_constant_value(bool, mpz_t
, Type
**) const;
3534 do_float_constant_value(mpfr_t
, Type
**) const;
3537 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
3543 do_determine_type(const Type_context
*);
3546 do_check_types(Gogo
*);
3551 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3556 do_is_addressable() const
3557 { return this->op_
== OPERATOR_MULT
; }
3560 do_get_tree(Translate_context
*);
3563 do_export(Export
*) const;
3566 // The unary operator to apply.
3568 // Normally true. False if this is an address expression which does
3569 // not escape the current function.
3575 // If we are taking the address of a composite literal, and the
3576 // contents are not constant, then we want to make a heap composite
3580 Unary_expression::do_lower(Gogo
*, Named_object
*, int)
3582 source_location loc
= this->location();
3583 Operator op
= this->op_
;
3584 Expression
* expr
= this->expr_
;
3586 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3587 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3589 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3590 // moving x to the heap. FIXME: Is it worth doing a real escape
3591 // analysis here? This case is found in math/unsafe.go and is
3592 // therefore worth special casing.
3593 if (op
== OPERATOR_MULT
)
3595 Expression
* e
= expr
;
3596 while (e
->classification() == EXPRESSION_CONVERSION
)
3598 Type_conversion_expression
* te
3599 = static_cast<Type_conversion_expression
*>(e
);
3603 if (e
->classification() == EXPRESSION_UNARY
)
3605 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3606 if (ue
->op_
== OPERATOR_AND
)
3613 ue
->set_does_not_escape();
3618 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
3619 || op
== OPERATOR_NOT
|| op
== OPERATOR_XOR
)
3621 Expression
* ret
= NULL
;
3626 if (expr
->integer_constant_value(false, eval
, &etype
))
3630 if (Unary_expression::eval_integer(op
, etype
, eval
, val
, loc
))
3631 ret
= Expression::make_integer(&val
, etype
, loc
);
3638 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
)
3643 if (expr
->float_constant_value(fval
, &ftype
))
3647 if (Unary_expression::eval_float(op
, fval
, val
))
3648 ret
= Expression::make_float(&val
, ftype
, loc
);
3659 if (expr
->complex_constant_value(fval
, ival
, &ftype
))
3665 if (Unary_expression::eval_complex(op
, fval
, ival
, real
, imag
))
3666 ret
= Expression::make_complex(&real
, &imag
, ftype
, loc
);
3680 // Return whether a unary expression is a constant.
3683 Unary_expression::do_is_constant() const
3685 if (this->op_
== OPERATOR_MULT
)
3687 // Indirecting through a pointer is only constant if the object
3688 // to which the expression points is constant, but we currently
3689 // have no way to determine that.
3692 else if (this->op_
== OPERATOR_AND
)
3694 // Taking the address of a variable is constant if it is a
3695 // global variable, not constant otherwise. In other cases
3696 // taking the address is probably not a constant.
3697 Var_expression
* ve
= this->expr_
->var_expression();
3700 Named_object
* no
= ve
->named_object();
3701 return no
->is_variable() && no
->var_value()->is_global();
3706 return this->expr_
->is_constant();
3709 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3710 // UVAL, if known; it may be NULL. Return true if this could be done,
3714 Unary_expression::eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3715 source_location location
)
3722 case OPERATOR_MINUS
:
3724 return Integer_expression::check_constant(val
, utype
, location
);
3726 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3730 || utype
->integer_type() == NULL
3731 || utype
->integer_type()->is_abstract())
3735 // The number of HOST_WIDE_INTs that it takes to represent
3737 size_t count
= ((mpz_sizeinbase(uval
, 2)
3738 + HOST_BITS_PER_WIDE_INT
3740 / HOST_BITS_PER_WIDE_INT
);
3742 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3743 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3746 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3747 gcc_assert(ecount
<= count
);
3749 // Trim down to the number of words required by the type.
3750 size_t obits
= utype
->integer_type()->bits();
3751 if (!utype
->integer_type()->is_unsigned())
3753 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3754 / HOST_BITS_PER_WIDE_INT
);
3755 gcc_assert(ocount
<= ocount
);
3757 for (size_t i
= 0; i
< ocount
; ++i
)
3760 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3762 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3765 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3769 return Integer_expression::check_constant(val
, utype
, location
);
3778 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3779 // could be done, false if not.
3782 Unary_expression::eval_float(Operator op
, mpfr_t uval
, mpfr_t val
)
3787 mpfr_set(val
, uval
, GMP_RNDN
);
3789 case OPERATOR_MINUS
:
3790 mpfr_neg(val
, uval
, GMP_RNDN
);
3802 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3803 // if this could be done, false if not.
3806 Unary_expression::eval_complex(Operator op
, mpfr_t rval
, mpfr_t ival
,
3807 mpfr_t real
, mpfr_t imag
)
3812 mpfr_set(real
, rval
, GMP_RNDN
);
3813 mpfr_set(imag
, ival
, GMP_RNDN
);
3815 case OPERATOR_MINUS
:
3816 mpfr_neg(real
, rval
, GMP_RNDN
);
3817 mpfr_neg(imag
, ival
, GMP_RNDN
);
3829 // Return the integral constant value of a unary expression, if it has one.
3832 Unary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
3838 if (!this->expr_
->integer_constant_value(iota_is_constant
, uval
, ptype
))
3841 ret
= Unary_expression::eval_integer(this->op_
, *ptype
, uval
, val
,
3847 // Return the floating point constant value of a unary expression, if
3851 Unary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
3856 if (!this->expr_
->float_constant_value(uval
, ptype
))
3859 ret
= Unary_expression::eval_float(this->op_
, uval
, val
);
3864 // Return the complex constant value of a unary expression, if it has
3868 Unary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
3876 if (!this->expr_
->complex_constant_value(rval
, ival
, ptype
))
3879 ret
= Unary_expression::eval_complex(this->op_
, rval
, ival
, real
, imag
);
3885 // Return the type of a unary expression.
3888 Unary_expression::do_type()
3893 case OPERATOR_MINUS
:
3896 return this->expr_
->type();
3899 return Type::make_pointer_type(this->expr_
->type());
3903 Type
* subtype
= this->expr_
->type();
3904 Type
* points_to
= subtype
->points_to();
3905 if (points_to
== NULL
)
3906 return Type::make_error_type();
3915 // Determine abstract types for a unary expression.
3918 Unary_expression::do_determine_type(const Type_context
* context
)
3923 case OPERATOR_MINUS
:
3926 this->expr_
->determine_type(context
);
3930 // Taking the address of something.
3932 Type
* subtype
= (context
->type
== NULL
3934 : context
->type
->points_to());
3935 Type_context
subcontext(subtype
, false);
3936 this->expr_
->determine_type(&subcontext
);
3941 // Indirecting through a pointer.
3943 Type
* subtype
= (context
->type
== NULL
3945 : Type::make_pointer_type(context
->type
));
3946 Type_context
subcontext(subtype
, false);
3947 this->expr_
->determine_type(&subcontext
);
3956 // Check types for a unary expression.
3959 Unary_expression::do_check_types(Gogo
*)
3961 Type
* type
= this->expr_
->type();
3962 if (type
->is_error_type())
3964 this->set_is_error();
3971 case OPERATOR_MINUS
:
3972 if (type
->integer_type() == NULL
3973 && type
->float_type() == NULL
3974 && type
->complex_type() == NULL
)
3975 this->report_error(_("expected numeric type"));
3980 if (type
->integer_type() == NULL
3981 && !type
->is_boolean_type())
3982 this->report_error(_("expected integer or boolean type"));
3986 if (!this->expr_
->is_addressable())
3987 this->report_error(_("invalid operand for unary %<&%>"));
3989 this->expr_
->address_taken(this->escapes_
);
3993 // Indirecting through a pointer.
3994 if (type
->points_to() == NULL
)
3995 this->report_error(_("expected pointer"));
4003 // Get a tree for a unary expression.
4006 Unary_expression::do_get_tree(Translate_context
* context
)
4008 tree expr
= this->expr_
->get_tree(context
);
4009 if (expr
== error_mark_node
)
4010 return error_mark_node
;
4012 source_location loc
= this->location();
4018 case OPERATOR_MINUS
:
4020 tree type
= TREE_TYPE(expr
);
4021 tree compute_type
= excess_precision_type(type
);
4022 if (compute_type
!= NULL_TREE
)
4023 expr
= ::convert(compute_type
, expr
);
4024 tree ret
= fold_build1_loc(loc
, NEGATE_EXPR
,
4025 (compute_type
!= NULL_TREE
4029 if (compute_type
!= NULL_TREE
)
4030 ret
= ::convert(type
, ret
);
4035 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
4036 return fold_build1_loc(loc
, TRUTH_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4038 return fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, expr
,
4039 build_int_cst(TREE_TYPE(expr
), 0));
4042 return fold_build1_loc(loc
, BIT_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4045 // We should not see a non-constant constructor here; cases
4046 // where we would see one should have been moved onto the heap
4047 // at parse time. Taking the address of a nonconstant
4048 // constructor will not do what the programmer expects.
4049 gcc_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
4050 gcc_assert(TREE_CODE(expr
) != ADDR_EXPR
);
4052 // Build a decl for a constant constructor.
4053 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
4055 tree decl
= build_decl(this->location(), VAR_DECL
,
4056 create_tmp_var_name("C"), TREE_TYPE(expr
));
4057 DECL_EXTERNAL(decl
) = 0;
4058 TREE_PUBLIC(decl
) = 0;
4059 TREE_READONLY(decl
) = 1;
4060 TREE_CONSTANT(decl
) = 1;
4061 TREE_STATIC(decl
) = 1;
4062 TREE_ADDRESSABLE(decl
) = 1;
4063 DECL_ARTIFICIAL(decl
) = 1;
4064 DECL_INITIAL(decl
) = expr
;
4065 rest_of_decl_compilation(decl
, 1, 0);
4069 return build_fold_addr_expr_loc(loc
, expr
);
4073 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
4075 // If we are dereferencing the pointer to a large struct, we
4076 // need to check for nil. We don't bother to check for small
4077 // structs because we expect the system to crash on a nil
4078 // pointer dereference.
4079 HOST_WIDE_INT s
= int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr
)));
4080 if (s
== -1 || s
>= 4096)
4083 expr
= save_expr(expr
);
4084 tree compare
= fold_build2_loc(loc
, EQ_EXPR
, boolean_type_node
,
4086 fold_convert(TREE_TYPE(expr
),
4087 null_pointer_node
));
4088 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4090 expr
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(expr
),
4091 build3(COND_EXPR
, void_type_node
,
4092 compare
, crash
, NULL_TREE
),
4096 // If the type of EXPR is a recursive pointer type, then we
4097 // need to insert a cast before indirecting.
4098 if (TREE_TYPE(TREE_TYPE(expr
)) == ptr_type_node
)
4100 Type
* pt
= this->expr_
->type()->points_to();
4101 tree ind
= pt
->get_tree(context
->gogo());
4102 expr
= fold_convert_loc(loc
, build_pointer_type(ind
), expr
);
4105 return build_fold_indirect_ref_loc(loc
, expr
);
4113 // Export a unary expression.
4116 Unary_expression::do_export(Export
* exp
) const
4121 exp
->write_c_string("+ ");
4123 case OPERATOR_MINUS
:
4124 exp
->write_c_string("- ");
4127 exp
->write_c_string("! ");
4130 exp
->write_c_string("^ ");
4137 this->expr_
->export_expression(exp
);
4140 // Import a unary expression.
4143 Unary_expression::do_import(Import
* imp
)
4146 switch (imp
->get_char())
4152 op
= OPERATOR_MINUS
;
4163 imp
->require_c_string(" ");
4164 Expression
* expr
= Expression::import_expression(imp
);
4165 return Expression::make_unary(op
, expr
, imp
->location());
4168 // Make a unary expression.
4171 Expression::make_unary(Operator op
, Expression
* expr
, source_location location
)
4173 return new Unary_expression(op
, expr
, location
);
4176 // If this is an indirection through a pointer, return the expression
4177 // being pointed through. Otherwise return this.
4182 if (this->classification_
== EXPRESSION_UNARY
)
4184 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4185 if (ue
->op() == OPERATOR_MULT
)
4186 return ue
->operand();
4191 // Class Binary_expression.
4196 Binary_expression::do_traverse(Traverse
* traverse
)
4198 int t
= Expression::traverse(&this->left_
, traverse
);
4199 if (t
== TRAVERSE_EXIT
)
4200 return TRAVERSE_EXIT
;
4201 return Expression::traverse(&this->right_
, traverse
);
4204 // Compare integer constants according to OP.
4207 Binary_expression::compare_integer(Operator op
, mpz_t left_val
,
4210 int i
= mpz_cmp(left_val
, right_val
);
4215 case OPERATOR_NOTEQ
:
4230 // Compare floating point constants according to OP.
4233 Binary_expression::compare_float(Operator op
, Type
* type
, mpfr_t left_val
,
4238 i
= mpfr_cmp(left_val
, right_val
);
4242 mpfr_init_set(lv
, left_val
, GMP_RNDN
);
4244 mpfr_init_set(rv
, right_val
, GMP_RNDN
);
4245 Float_expression::constrain_float(lv
, type
);
4246 Float_expression::constrain_float(rv
, type
);
4247 i
= mpfr_cmp(lv
, rv
);
4255 case OPERATOR_NOTEQ
:
4270 // Compare complex constants according to OP. Complex numbers may
4271 // only be compared for equality.
4274 Binary_expression::compare_complex(Operator op
, Type
* type
,
4275 mpfr_t left_real
, mpfr_t left_imag
,
4276 mpfr_t right_real
, mpfr_t right_imag
)
4280 is_equal
= (mpfr_cmp(left_real
, right_real
) == 0
4281 && mpfr_cmp(left_imag
, right_imag
) == 0);
4286 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4287 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4290 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4291 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4292 Complex_expression::constrain_complex(lr
, li
, type
);
4293 Complex_expression::constrain_complex(rr
, ri
, type
);
4294 is_equal
= mpfr_cmp(lr
, rr
) == 0 && mpfr_cmp(li
, ri
) == 0;
4304 case OPERATOR_NOTEQ
:
4311 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4312 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4313 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4314 // this could be done, false if not.
4317 Binary_expression::eval_integer(Operator op
, Type
* left_type
, mpz_t left_val
,
4318 Type
* right_type
, mpz_t right_val
,
4319 source_location location
, mpz_t val
)
4321 bool is_shift_op
= false;
4325 case OPERATOR_ANDAND
:
4327 case OPERATOR_NOTEQ
:
4332 // These return boolean values. We should probably handle them
4333 // anyhow in case a type conversion is used on the result.
4336 mpz_add(val
, left_val
, right_val
);
4338 case OPERATOR_MINUS
:
4339 mpz_sub(val
, left_val
, right_val
);
4342 mpz_ior(val
, left_val
, right_val
);
4345 mpz_xor(val
, left_val
, right_val
);
4348 mpz_mul(val
, left_val
, right_val
);
4351 if (mpz_sgn(right_val
) != 0)
4352 mpz_tdiv_q(val
, left_val
, right_val
);
4355 error_at(location
, "division by zero");
4361 if (mpz_sgn(right_val
) != 0)
4362 mpz_tdiv_r(val
, left_val
, right_val
);
4365 error_at(location
, "division by zero");
4370 case OPERATOR_LSHIFT
:
4372 unsigned long shift
= mpz_get_ui(right_val
);
4373 if (mpz_cmp_ui(right_val
, shift
) != 0)
4375 error_at(location
, "shift count overflow");
4379 mpz_mul_2exp(val
, left_val
, shift
);
4384 case OPERATOR_RSHIFT
:
4386 unsigned long shift
= mpz_get_ui(right_val
);
4387 if (mpz_cmp_ui(right_val
, shift
) != 0)
4389 error_at(location
, "shift count overflow");
4393 if (mpz_cmp_ui(left_val
, 0) >= 0)
4394 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4396 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4402 mpz_and(val
, left_val
, right_val
);
4404 case OPERATOR_BITCLEAR
:
4408 mpz_com(tval
, right_val
);
4409 mpz_and(val
, left_val
, tval
);
4417 Type
* type
= left_type
;
4422 else if (type
!= right_type
&& right_type
!= NULL
)
4424 if (type
->is_abstract())
4426 else if (!right_type
->is_abstract())
4428 // This look like a type error which should be diagnosed
4429 // elsewhere. Don't do anything here, to avoid an
4430 // unhelpful chain of error messages.
4436 if (type
!= NULL
&& !type
->is_abstract())
4438 // We have to check the operands too, as we have implicitly
4439 // coerced them to TYPE.
4440 if ((type
!= left_type
4441 && !Integer_expression::check_constant(left_val
, type
, location
))
4443 && type
!= right_type
4444 && !Integer_expression::check_constant(right_val
, type
,
4446 || !Integer_expression::check_constant(val
, type
, location
))
4453 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4454 // Return true if this could be done, false if not.
4457 Binary_expression::eval_float(Operator op
, Type
* left_type
, mpfr_t left_val
,
4458 Type
* right_type
, mpfr_t right_val
,
4459 mpfr_t val
, source_location location
)
4464 case OPERATOR_ANDAND
:
4466 case OPERATOR_NOTEQ
:
4471 // These return boolean values. We should probably handle them
4472 // anyhow in case a type conversion is used on the result.
4475 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4477 case OPERATOR_MINUS
:
4478 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4483 case OPERATOR_BITCLEAR
:
4486 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4489 if (mpfr_zero_p(right_val
))
4490 error_at(location
, "division by zero");
4491 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4495 case OPERATOR_LSHIFT
:
4496 case OPERATOR_RSHIFT
:
4502 Type
* type
= left_type
;
4505 else if (type
!= right_type
&& right_type
!= NULL
)
4507 if (type
->is_abstract())
4509 else if (!right_type
->is_abstract())
4511 // This looks like a type error which should be diagnosed
4512 // elsewhere. Don't do anything here, to avoid an unhelpful
4513 // chain of error messages.
4518 if (type
!= NULL
&& !type
->is_abstract())
4520 if ((type
!= left_type
4521 && !Float_expression::check_constant(left_val
, type
, location
))
4522 || (type
!= right_type
4523 && !Float_expression::check_constant(right_val
, type
,
4525 || !Float_expression::check_constant(val
, type
, location
))
4526 mpfr_set_ui(val
, 0, GMP_RNDN
);
4532 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4533 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4534 // could be done, false if not.
4537 Binary_expression::eval_complex(Operator op
, Type
* left_type
,
4538 mpfr_t left_real
, mpfr_t left_imag
,
4540 mpfr_t right_real
, mpfr_t right_imag
,
4541 mpfr_t real
, mpfr_t imag
,
4542 source_location location
)
4547 case OPERATOR_ANDAND
:
4549 case OPERATOR_NOTEQ
:
4554 // These return boolean values and must be handled differently.
4557 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4558 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4560 case OPERATOR_MINUS
:
4561 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4562 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4567 case OPERATOR_BITCLEAR
:
4571 // You might think that multiplying two complex numbers would
4572 // be simple, and you would be right, until you start to think
4573 // about getting the right answer for infinity. If one
4574 // operand here is infinity and the other is anything other
4575 // than zero or NaN, then we are going to wind up subtracting
4576 // two infinity values. That will give us a NaN, but the
4577 // correct answer is infinity.
4581 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4585 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4589 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4593 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4595 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4596 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4598 // If we get NaN on both sides, check whether it should really
4599 // be infinity. The rule is that if either side of the
4600 // complex number is infinity, then the whole value is
4601 // infinity, even if the other side is NaN. So the only case
4602 // we have to fix is the one in which both sides are NaN.
4603 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4604 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4605 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4607 bool is_infinity
= false;
4611 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4612 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4616 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4617 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4619 // If the left side is infinity, then the result is
4621 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4623 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4624 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4625 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4626 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4629 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4630 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4634 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4635 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4640 // If the right side is infinity, then the result is
4642 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4644 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4645 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4646 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4647 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4650 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4651 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4655 mpfr_set_ui(li
, 0, GMP_RNDN
);
4656 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4661 // If we got an overflow in the intermediate computations,
4662 // then the result is infinity.
4664 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4665 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4669 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4670 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4674 mpfr_set_ui(li
, 0, GMP_RNDN
);
4675 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4679 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4680 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4684 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4685 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4692 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4693 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4694 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4695 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4696 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4697 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4698 mpfr_set_inf(real
, mpfr_sgn(real
));
4699 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4716 // For complex division we want to avoid having an
4717 // intermediate overflow turn the whole result in a NaN. We
4718 // scale the values to try to avoid this.
4720 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4721 error_at(location
, "division by zero");
4727 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4728 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4731 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4735 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4736 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4738 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4740 ilogbw
= mpfr_get_exp(t
);
4741 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4742 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4747 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4748 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4749 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4751 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4752 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4753 mpfr_add(real
, real
, t
, GMP_RNDN
);
4754 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4755 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4757 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4758 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4759 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4760 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4761 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4763 // If we wind up with NaN on both sides, check whether we
4764 // should really have infinity. The rule is that if either
4765 // side of the complex number is infinity, then the whole
4766 // value is infinity, even if the other side is NaN. So the
4767 // only case we have to fix is the one in which both sides are
4769 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4770 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4771 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4773 if (mpfr_zero_p(denom
))
4775 mpfr_set_inf(real
, mpfr_sgn(rr
));
4776 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4777 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4778 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4780 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4781 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4783 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4784 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4787 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4788 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4792 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
4796 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
4798 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4799 mpfr_set_inf(real
, mpfr_sgn(t3
));
4801 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
4802 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
4803 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4804 mpfr_set_inf(imag
, mpfr_sgn(t3
));
4810 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
4811 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
4813 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4814 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
4817 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4818 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
4822 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
4826 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
4828 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4829 mpfr_set_ui(real
, 0, GMP_RNDN
);
4830 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
4832 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
4833 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
4834 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4835 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4836 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
4854 case OPERATOR_LSHIFT
:
4855 case OPERATOR_RSHIFT
:
4861 Type
* type
= left_type
;
4864 else if (type
!= right_type
&& right_type
!= NULL
)
4866 if (type
->is_abstract())
4868 else if (!right_type
->is_abstract())
4870 // This looks like a type error which should be diagnosed
4871 // elsewhere. Don't do anything here, to avoid an unhelpful
4872 // chain of error messages.
4877 if (type
!= NULL
&& !type
->is_abstract())
4879 if ((type
!= left_type
4880 && !Complex_expression::check_constant(left_real
, left_imag
,
4882 || (type
!= right_type
4883 && !Complex_expression::check_constant(right_real
, right_imag
,
4885 || !Complex_expression::check_constant(real
, imag
, type
,
4888 mpfr_set_ui(real
, 0, GMP_RNDN
);
4889 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4896 // Lower a binary expression. We have to evaluate constant
4897 // expressions now, in order to implement Go's unlimited precision
4901 Binary_expression::do_lower(Gogo
*, Named_object
*, int)
4903 source_location location
= this->location();
4904 Operator op
= this->op_
;
4905 Expression
* left
= this->left_
;
4906 Expression
* right
= this->right_
;
4908 const bool is_comparison
= (op
== OPERATOR_EQEQ
4909 || op
== OPERATOR_NOTEQ
4910 || op
== OPERATOR_LT
4911 || op
== OPERATOR_LE
4912 || op
== OPERATOR_GT
4913 || op
== OPERATOR_GE
);
4915 // Integer constant expressions.
4921 mpz_init(right_val
);
4923 if (left
->integer_constant_value(false, left_val
, &left_type
)
4924 && right
->integer_constant_value(false, right_val
, &right_type
))
4926 Expression
* ret
= NULL
;
4927 if (left_type
!= right_type
4928 && left_type
!= NULL
4929 && right_type
!= NULL
4930 && left_type
->base() != right_type
->base()
4931 && op
!= OPERATOR_LSHIFT
4932 && op
!= OPERATOR_RSHIFT
)
4934 // May be a type error--let it be diagnosed later.
4936 else if (is_comparison
)
4938 bool b
= Binary_expression::compare_integer(op
, left_val
,
4940 ret
= Expression::make_cast(Type::lookup_bool_type(),
4941 Expression::make_boolean(b
, location
),
4949 if (Binary_expression::eval_integer(op
, left_type
, left_val
,
4950 right_type
, right_val
,
4953 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
);
4955 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
4957 else if (left_type
== NULL
)
4959 else if (right_type
== NULL
)
4961 else if (!left_type
->is_abstract()
4962 && left_type
->named_type() != NULL
)
4964 else if (!right_type
->is_abstract()
4965 && right_type
->named_type() != NULL
)
4967 else if (!left_type
->is_abstract())
4969 else if (!right_type
->is_abstract())
4971 else if (left_type
->float_type() != NULL
)
4973 else if (right_type
->float_type() != NULL
)
4975 else if (left_type
->complex_type() != NULL
)
4977 else if (right_type
->complex_type() != NULL
)
4981 ret
= Expression::make_integer(&val
, type
, location
);
4989 mpz_clear(right_val
);
4990 mpz_clear(left_val
);
4994 mpz_clear(right_val
);
4995 mpz_clear(left_val
);
4998 // Floating point constant expressions.
5001 mpfr_init(left_val
);
5004 mpfr_init(right_val
);
5006 if (left
->float_constant_value(left_val
, &left_type
)
5007 && right
->float_constant_value(right_val
, &right_type
))
5009 Expression
* ret
= NULL
;
5010 if (left_type
!= right_type
5011 && left_type
!= NULL
5012 && right_type
!= NULL
5013 && left_type
->base() != right_type
->base()
5014 && op
!= OPERATOR_LSHIFT
5015 && op
!= OPERATOR_RSHIFT
)
5017 // May be a type error--let it be diagnosed later.
5019 else if (is_comparison
)
5021 bool b
= Binary_expression::compare_float(op
,
5025 left_val
, right_val
);
5026 ret
= Expression::make_boolean(b
, location
);
5033 if (Binary_expression::eval_float(op
, left_type
, left_val
,
5034 right_type
, right_val
, val
,
5037 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5038 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5040 if (left_type
== NULL
)
5042 else if (right_type
== NULL
)
5044 else if (!left_type
->is_abstract()
5045 && left_type
->named_type() != NULL
)
5047 else if (!right_type
->is_abstract()
5048 && right_type
->named_type() != NULL
)
5050 else if (!left_type
->is_abstract())
5052 else if (!right_type
->is_abstract())
5054 else if (left_type
->float_type() != NULL
)
5056 else if (right_type
->float_type() != NULL
)
5060 ret
= Expression::make_float(&val
, type
, location
);
5068 mpfr_clear(right_val
);
5069 mpfr_clear(left_val
);
5073 mpfr_clear(right_val
);
5074 mpfr_clear(left_val
);
5077 // Complex constant expressions.
5081 mpfr_init(left_real
);
5082 mpfr_init(left_imag
);
5087 mpfr_init(right_real
);
5088 mpfr_init(right_imag
);
5091 if (left
->complex_constant_value(left_real
, left_imag
, &left_type
)
5092 && right
->complex_constant_value(right_real
, right_imag
, &right_type
))
5094 Expression
* ret
= NULL
;
5095 if (left_type
!= right_type
5096 && left_type
!= NULL
5097 && right_type
!= NULL
5098 && left_type
->base() != right_type
->base())
5100 // May be a type error--let it be diagnosed later.
5102 else if (is_comparison
)
5104 bool b
= Binary_expression::compare_complex(op
,
5112 ret
= Expression::make_boolean(b
, location
);
5121 if (Binary_expression::eval_complex(op
, left_type
,
5122 left_real
, left_imag
,
5124 right_real
, right_imag
,
5128 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5129 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5131 if (left_type
== NULL
)
5133 else if (right_type
== NULL
)
5135 else if (!left_type
->is_abstract()
5136 && left_type
->named_type() != NULL
)
5138 else if (!right_type
->is_abstract()
5139 && right_type
->named_type() != NULL
)
5141 else if (!left_type
->is_abstract())
5143 else if (!right_type
->is_abstract())
5145 else if (left_type
->complex_type() != NULL
)
5147 else if (right_type
->complex_type() != NULL
)
5151 ret
= Expression::make_complex(&real
, &imag
, type
,
5160 mpfr_clear(left_real
);
5161 mpfr_clear(left_imag
);
5162 mpfr_clear(right_real
);
5163 mpfr_clear(right_imag
);
5168 mpfr_clear(left_real
);
5169 mpfr_clear(left_imag
);
5170 mpfr_clear(right_real
);
5171 mpfr_clear(right_imag
);
5174 // String constant expressions.
5175 if (op
== OPERATOR_PLUS
5176 && left
->type()->is_string_type()
5177 && right
->type()->is_string_type())
5179 std::string left_string
;
5180 std::string right_string
;
5181 if (left
->string_constant_value(&left_string
)
5182 && right
->string_constant_value(&right_string
))
5183 return Expression::make_string(left_string
+ right_string
, location
);
5189 // Return the integer constant value, if it has one.
5192 Binary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
5198 if (!this->left_
->integer_constant_value(iota_is_constant
, left_val
,
5201 mpz_clear(left_val
);
5206 mpz_init(right_val
);
5208 if (!this->right_
->integer_constant_value(iota_is_constant
, right_val
,
5211 mpz_clear(right_val
);
5212 mpz_clear(left_val
);
5217 if (left_type
!= right_type
5218 && left_type
!= NULL
5219 && right_type
!= NULL
5220 && left_type
->base() != right_type
->base()
5221 && this->op_
!= OPERATOR_RSHIFT
5222 && this->op_
!= OPERATOR_LSHIFT
)
5225 ret
= Binary_expression::eval_integer(this->op_
, left_type
, left_val
,
5226 right_type
, right_val
,
5227 this->location(), val
);
5229 mpz_clear(right_val
);
5230 mpz_clear(left_val
);
5238 // Return the floating point constant value, if it has one.
5241 Binary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
5244 mpfr_init(left_val
);
5246 if (!this->left_
->float_constant_value(left_val
, &left_type
))
5248 mpfr_clear(left_val
);
5253 mpfr_init(right_val
);
5255 if (!this->right_
->float_constant_value(right_val
, &right_type
))
5257 mpfr_clear(right_val
);
5258 mpfr_clear(left_val
);
5263 if (left_type
!= right_type
5264 && left_type
!= NULL
5265 && right_type
!= NULL
5266 && left_type
->base() != right_type
->base())
5269 ret
= Binary_expression::eval_float(this->op_
, left_type
, left_val
,
5270 right_type
, right_val
,
5271 val
, this->location());
5273 mpfr_clear(left_val
);
5274 mpfr_clear(right_val
);
5282 // Return the complex constant value, if it has one.
5285 Binary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
5290 mpfr_init(left_real
);
5291 mpfr_init(left_imag
);
5293 if (!this->left_
->complex_constant_value(left_real
, left_imag
, &left_type
))
5295 mpfr_clear(left_real
);
5296 mpfr_clear(left_imag
);
5302 mpfr_init(right_real
);
5303 mpfr_init(right_imag
);
5305 if (!this->right_
->complex_constant_value(right_real
, right_imag
,
5308 mpfr_clear(left_real
);
5309 mpfr_clear(left_imag
);
5310 mpfr_clear(right_real
);
5311 mpfr_clear(right_imag
);
5316 if (left_type
!= right_type
5317 && left_type
!= NULL
5318 && right_type
!= NULL
5319 && left_type
->base() != right_type
->base())
5322 ret
= Binary_expression::eval_complex(this->op_
, left_type
,
5323 left_real
, left_imag
,
5325 right_real
, right_imag
,
5328 mpfr_clear(left_real
);
5329 mpfr_clear(left_imag
);
5330 mpfr_clear(right_real
);
5331 mpfr_clear(right_imag
);
5339 // Note that the value is being discarded.
5342 Binary_expression::do_discarding_value()
5344 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5345 this->right_
->discarding_value();
5347 this->warn_about_unused_value();
5353 Binary_expression::do_type()
5358 case OPERATOR_ANDAND
:
5360 case OPERATOR_NOTEQ
:
5365 return Type::lookup_bool_type();
5368 case OPERATOR_MINUS
:
5375 case OPERATOR_BITCLEAR
:
5377 Type
* left_type
= this->left_
->type();
5378 Type
* right_type
= this->right_
->type();
5379 if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5381 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5383 else if (!left_type
->is_abstract())
5385 else if (!right_type
->is_abstract())
5387 else if (left_type
->complex_type() != NULL
)
5389 else if (right_type
->complex_type() != NULL
)
5391 else if (left_type
->float_type() != NULL
)
5393 else if (right_type
->float_type() != NULL
)
5399 case OPERATOR_LSHIFT
:
5400 case OPERATOR_RSHIFT
:
5401 return this->left_
->type();
5408 // Set type for a binary expression.
5411 Binary_expression::do_determine_type(const Type_context
* context
)
5413 Type
* tleft
= this->left_
->type();
5414 Type
* tright
= this->right_
->type();
5416 // Both sides should have the same type, except for the shift
5417 // operations. For a comparison, we should ignore the incoming
5420 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5421 || this->op_
== OPERATOR_RSHIFT
);
5423 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5424 || this->op_
== OPERATOR_NOTEQ
5425 || this->op_
== OPERATOR_LT
5426 || this->op_
== OPERATOR_LE
5427 || this->op_
== OPERATOR_GT
5428 || this->op_
== OPERATOR_GE
);
5430 Type_context
subcontext(*context
);
5434 // In a comparison, the context does not determine the types of
5436 subcontext
.type
= NULL
;
5439 // Set the context for the left hand operand.
5442 // The right hand operand plays no role in determining the type
5443 // of the left hand operand. A shift of an abstract integer in
5444 // a string context gets special treatment, which may be a
5446 if (subcontext
.type
!= NULL
5447 && subcontext
.type
->is_string_type()
5448 && tleft
->is_abstract())
5449 error_at(this->location(), "shift of non-integer operand");
5451 else if (!tleft
->is_abstract())
5452 subcontext
.type
= tleft
;
5453 else if (!tright
->is_abstract())
5454 subcontext
.type
= tright
;
5455 else if (subcontext
.type
== NULL
)
5457 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5458 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5459 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5461 // Both sides have an abstract integer, abstract float, or
5462 // abstract complex type. Just let CONTEXT determine
5463 // whether they may remain abstract or not.
5465 else if (tleft
->complex_type() != NULL
)
5466 subcontext
.type
= tleft
;
5467 else if (tright
->complex_type() != NULL
)
5468 subcontext
.type
= tright
;
5469 else if (tleft
->float_type() != NULL
)
5470 subcontext
.type
= tleft
;
5471 else if (tright
->float_type() != NULL
)
5472 subcontext
.type
= tright
;
5474 subcontext
.type
= tleft
;
5477 this->left_
->determine_type(&subcontext
);
5479 // The context for the right hand operand is the same as for the
5480 // left hand operand, except for a shift operator.
5483 subcontext
.type
= Type::lookup_integer_type("uint");
5484 subcontext
.may_be_abstract
= false;
5487 this->right_
->determine_type(&subcontext
);
5490 // Report an error if the binary operator OP does not support TYPE.
5491 // Return whether the operation is OK. This should not be used for
5495 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5496 source_location location
)
5501 case OPERATOR_ANDAND
:
5502 if (!type
->is_boolean_type())
5504 error_at(location
, "expected boolean type");
5510 case OPERATOR_NOTEQ
:
5511 if (type
->integer_type() == NULL
5512 && type
->float_type() == NULL
5513 && type
->complex_type() == NULL
5514 && !type
->is_string_type()
5515 && type
->points_to() == NULL
5516 && !type
->is_nil_type()
5517 && !type
->is_boolean_type()
5518 && type
->interface_type() == NULL
5519 && (type
->array_type() == NULL
5520 || type
->array_type()->length() != NULL
)
5521 && type
->map_type() == NULL
5522 && type
->channel_type() == NULL
5523 && type
->function_type() == NULL
)
5526 ("expected integer, floating, complex, string, pointer, "
5527 "boolean, interface, slice, map, channel, "
5528 "or function type"));
5537 if (type
->integer_type() == NULL
5538 && type
->float_type() == NULL
5539 && !type
->is_string_type())
5541 error_at(location
, "expected integer, floating, or string type");
5547 case OPERATOR_PLUSEQ
:
5548 if (type
->integer_type() == NULL
5549 && type
->float_type() == NULL
5550 && type
->complex_type() == NULL
5551 && !type
->is_string_type())
5554 "expected integer, floating, complex, or string type");
5559 case OPERATOR_MINUS
:
5560 case OPERATOR_MINUSEQ
:
5562 case OPERATOR_MULTEQ
:
5564 case OPERATOR_DIVEQ
:
5565 if (type
->integer_type() == NULL
5566 && type
->float_type() == NULL
5567 && type
->complex_type() == NULL
)
5569 error_at(location
, "expected integer, floating, or complex type");
5575 case OPERATOR_MODEQ
:
5579 case OPERATOR_ANDEQ
:
5581 case OPERATOR_XOREQ
:
5582 case OPERATOR_BITCLEAR
:
5583 case OPERATOR_BITCLEAREQ
:
5584 if (type
->integer_type() == NULL
)
5586 error_at(location
, "expected integer type");
5601 Binary_expression::do_check_types(Gogo
*)
5603 Type
* left_type
= this->left_
->type();
5604 Type
* right_type
= this->right_
->type();
5605 if (left_type
->is_error_type() || right_type
->is_error_type())
5607 this->set_is_error();
5611 if (this->op_
== OPERATOR_EQEQ
5612 || this->op_
== OPERATOR_NOTEQ
5613 || this->op_
== OPERATOR_LT
5614 || this->op_
== OPERATOR_LE
5615 || this->op_
== OPERATOR_GT
5616 || this->op_
== OPERATOR_GE
)
5618 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5619 && !Type::are_assignable(right_type
, left_type
, NULL
))
5621 this->report_error(_("incompatible types in binary expression"));
5624 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5626 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5629 this->set_is_error();
5633 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5635 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5637 this->report_error(_("incompatible types in binary expression"));
5640 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5643 this->set_is_error();
5649 if (left_type
->integer_type() == NULL
)
5650 this->report_error(_("shift of non-integer operand"));
5652 if (!right_type
->is_abstract()
5653 && (right_type
->integer_type() == NULL
5654 || !right_type
->integer_type()->is_unsigned()))
5655 this->report_error(_("shift count not unsigned integer"));
5661 if (this->right_
->integer_constant_value(true, val
, &type
))
5663 if (mpz_sgn(val
) < 0)
5664 this->report_error(_("negative shift count"));
5671 // Get a tree for a binary expression.
5674 Binary_expression::do_get_tree(Translate_context
* context
)
5676 tree left
= this->left_
->get_tree(context
);
5677 tree right
= this->right_
->get_tree(context
);
5679 if (left
== error_mark_node
|| right
== error_mark_node
)
5680 return error_mark_node
;
5682 enum tree_code code
;
5683 bool use_left_type
= true;
5684 bool is_shift_op
= false;
5688 case OPERATOR_NOTEQ
:
5693 return Expression::comparison_tree(context
, this->op_
,
5694 this->left_
->type(), left
,
5695 this->right_
->type(), right
,
5699 code
= TRUTH_ORIF_EXPR
;
5700 use_left_type
= false;
5702 case OPERATOR_ANDAND
:
5703 code
= TRUTH_ANDIF_EXPR
;
5704 use_left_type
= false;
5709 case OPERATOR_MINUS
:
5713 code
= BIT_IOR_EXPR
;
5716 code
= BIT_XOR_EXPR
;
5723 Type
*t
= this->left_
->type();
5724 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5727 code
= TRUNC_DIV_EXPR
;
5731 code
= TRUNC_MOD_EXPR
;
5733 case OPERATOR_LSHIFT
:
5737 case OPERATOR_RSHIFT
:
5742 code
= BIT_AND_EXPR
;
5744 case OPERATOR_BITCLEAR
:
5745 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5746 code
= BIT_AND_EXPR
;
5752 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5754 if (this->left_
->type()->is_string_type())
5756 gcc_assert(this->op_
== OPERATOR_PLUS
);
5757 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5758 static tree string_plus_decl
;
5759 return Gogo::call_builtin(&string_plus_decl
,
5770 tree compute_type
= excess_precision_type(type
);
5771 if (compute_type
!= NULL_TREE
)
5773 left
= ::convert(compute_type
, left
);
5774 right
= ::convert(compute_type
, right
);
5777 tree eval_saved
= NULL_TREE
;
5781 left
= save_expr(left
);
5783 right
= save_expr(right
);
5784 // Make sure the values are evaluated.
5785 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5786 void_type_node
, left
, right
);
5789 tree ret
= fold_build2_loc(this->location(),
5791 compute_type
!= NULL_TREE
? compute_type
: type
,
5794 if (compute_type
!= NULL_TREE
)
5795 ret
= ::convert(type
, ret
);
5797 // In Go, a shift larger than the size of the type is well-defined.
5798 // This is not true in GENERIC, so we need to insert a conditional.
5801 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5802 gcc_assert(this->left_
->type()->integer_type() != NULL
);
5803 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5805 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5806 build_int_cst_type(TREE_TYPE(right
), bits
));
5808 tree overflow_result
= fold_convert_loc(this->location(),
5811 if (this->op_
== OPERATOR_RSHIFT
5812 && !this->left_
->type()->integer_type()->is_unsigned())
5814 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
5815 boolean_type_node
, left
,
5816 fold_convert_loc(this->location(),
5818 integer_zero_node
));
5819 tree neg_one
= fold_build2_loc(this->location(),
5820 MINUS_EXPR
, TREE_TYPE(left
),
5821 fold_convert_loc(this->location(),
5824 fold_convert_loc(this->location(),
5827 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
5828 TREE_TYPE(left
), neg
, neg_one
,
5832 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
5833 compare
, ret
, overflow_result
);
5835 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5836 TREE_TYPE(ret
), eval_saved
, ret
);
5842 // Export a binary expression.
5845 Binary_expression::do_export(Export
* exp
) const
5847 exp
->write_c_string("(");
5848 this->left_
->export_expression(exp
);
5852 exp
->write_c_string(" || ");
5854 case OPERATOR_ANDAND
:
5855 exp
->write_c_string(" && ");
5858 exp
->write_c_string(" == ");
5860 case OPERATOR_NOTEQ
:
5861 exp
->write_c_string(" != ");
5864 exp
->write_c_string(" < ");
5867 exp
->write_c_string(" <= ");
5870 exp
->write_c_string(" > ");
5873 exp
->write_c_string(" >= ");
5876 exp
->write_c_string(" + ");
5878 case OPERATOR_MINUS
:
5879 exp
->write_c_string(" - ");
5882 exp
->write_c_string(" | ");
5885 exp
->write_c_string(" ^ ");
5888 exp
->write_c_string(" * ");
5891 exp
->write_c_string(" / ");
5894 exp
->write_c_string(" % ");
5896 case OPERATOR_LSHIFT
:
5897 exp
->write_c_string(" << ");
5899 case OPERATOR_RSHIFT
:
5900 exp
->write_c_string(" >> ");
5903 exp
->write_c_string(" & ");
5905 case OPERATOR_BITCLEAR
:
5906 exp
->write_c_string(" &^ ");
5911 this->right_
->export_expression(exp
);
5912 exp
->write_c_string(")");
5915 // Import a binary expression.
5918 Binary_expression::do_import(Import
* imp
)
5920 imp
->require_c_string("(");
5922 Expression
* left
= Expression::import_expression(imp
);
5925 if (imp
->match_c_string(" || "))
5930 else if (imp
->match_c_string(" && "))
5932 op
= OPERATOR_ANDAND
;
5935 else if (imp
->match_c_string(" == "))
5940 else if (imp
->match_c_string(" != "))
5942 op
= OPERATOR_NOTEQ
;
5945 else if (imp
->match_c_string(" < "))
5950 else if (imp
->match_c_string(" <= "))
5955 else if (imp
->match_c_string(" > "))
5960 else if (imp
->match_c_string(" >= "))
5965 else if (imp
->match_c_string(" + "))
5970 else if (imp
->match_c_string(" - "))
5972 op
= OPERATOR_MINUS
;
5975 else if (imp
->match_c_string(" | "))
5980 else if (imp
->match_c_string(" ^ "))
5985 else if (imp
->match_c_string(" * "))
5990 else if (imp
->match_c_string(" / "))
5995 else if (imp
->match_c_string(" % "))
6000 else if (imp
->match_c_string(" << "))
6002 op
= OPERATOR_LSHIFT
;
6005 else if (imp
->match_c_string(" >> "))
6007 op
= OPERATOR_RSHIFT
;
6010 else if (imp
->match_c_string(" & "))
6015 else if (imp
->match_c_string(" &^ "))
6017 op
= OPERATOR_BITCLEAR
;
6022 error_at(imp
->location(), "unrecognized binary operator");
6023 return Expression::make_error(imp
->location());
6026 Expression
* right
= Expression::import_expression(imp
);
6028 imp
->require_c_string(")");
6030 return Expression::make_binary(op
, left
, right
, imp
->location());
6033 // Make a binary expression.
6036 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6037 source_location location
)
6039 return new Binary_expression(op
, left
, right
, location
);
6042 // Implement a comparison.
6045 Expression::comparison_tree(Translate_context
* context
, Operator op
,
6046 Type
* left_type
, tree left_tree
,
6047 Type
* right_type
, tree right_tree
,
6048 source_location location
)
6050 enum tree_code code
;
6056 case OPERATOR_NOTEQ
:
6075 if (left_type
->is_string_type() && right_type
->is_string_type())
6077 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
6078 static tree string_compare_decl
;
6079 left_tree
= Gogo::call_builtin(&string_compare_decl
,
6088 right_tree
= build_int_cst_type(integer_type_node
, 0);
6090 else if ((left_type
->interface_type() != NULL
6091 && right_type
->interface_type() == NULL
6092 && !right_type
->is_nil_type())
6093 || (left_type
->interface_type() == NULL
6094 && !left_type
->is_nil_type()
6095 && right_type
->interface_type() != NULL
))
6097 // Comparing an interface value to a non-interface value.
6098 if (left_type
->interface_type() == NULL
)
6100 std::swap(left_type
, right_type
);
6101 std::swap(left_tree
, right_tree
);
6104 // The right operand is not an interface. We need to take its
6105 // address if it is not a pointer.
6108 if (right_type
->points_to() != NULL
)
6110 make_tmp
= NULL_TREE
;
6113 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
6115 make_tmp
= NULL_TREE
;
6116 arg
= build_fold_addr_expr_loc(location
, right_tree
);
6117 if (DECL_P(right_tree
))
6118 TREE_ADDRESSABLE(right_tree
) = 1;
6122 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
6123 get_name(right_tree
));
6124 DECL_IGNORED_P(tmp
) = 0;
6125 DECL_INITIAL(tmp
) = right_tree
;
6126 TREE_ADDRESSABLE(tmp
) = 1;
6127 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6128 SET_EXPR_LOCATION(make_tmp
, location
);
6129 arg
= build_fold_addr_expr_loc(location
, tmp
);
6131 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
6133 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
6135 if (left_type
->interface_type()->is_empty())
6137 static tree empty_interface_value_compare_decl
;
6138 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6140 "__go_empty_interface_value_compare",
6143 TREE_TYPE(left_tree
),
6145 TREE_TYPE(descriptor
),
6149 if (left_tree
== error_mark_node
)
6150 return error_mark_node
;
6151 // This can panic if the type is not comparable.
6152 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6156 static tree interface_value_compare_decl
;
6157 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6159 "__go_interface_value_compare",
6162 TREE_TYPE(left_tree
),
6164 TREE_TYPE(descriptor
),
6168 if (left_tree
== error_mark_node
)
6169 return error_mark_node
;
6170 // This can panic if the type is not comparable.
6171 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6173 right_tree
= build_int_cst_type(integer_type_node
, 0);
6175 if (make_tmp
!= NULL_TREE
)
6176 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6179 else if (left_type
->interface_type() != NULL
6180 && right_type
->interface_type() != NULL
)
6182 if (left_type
->interface_type()->is_empty())
6184 gcc_assert(right_type
->interface_type()->is_empty());
6185 static tree empty_interface_compare_decl
;
6186 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6188 "__go_empty_interface_compare",
6191 TREE_TYPE(left_tree
),
6193 TREE_TYPE(right_tree
),
6195 if (left_tree
== error_mark_node
)
6196 return error_mark_node
;
6197 // This can panic if the type is uncomparable.
6198 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6202 gcc_assert(!right_type
->interface_type()->is_empty());
6203 static tree interface_compare_decl
;
6204 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6206 "__go_interface_compare",
6209 TREE_TYPE(left_tree
),
6211 TREE_TYPE(right_tree
),
6213 if (left_tree
== error_mark_node
)
6214 return error_mark_node
;
6215 // This can panic if the type is uncomparable.
6216 TREE_NOTHROW(interface_compare_decl
) = 0;
6218 right_tree
= build_int_cst_type(integer_type_node
, 0);
6221 if (left_type
->is_nil_type()
6222 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6224 std::swap(left_type
, right_type
);
6225 std::swap(left_tree
, right_tree
);
6228 if (right_type
->is_nil_type())
6230 if (left_type
->array_type() != NULL
6231 && left_type
->array_type()->length() == NULL
)
6233 Array_type
* at
= left_type
->array_type();
6234 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6235 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6237 else if (left_type
->interface_type() != NULL
)
6239 // An interface is nil if the first field is nil.
6240 tree left_type_tree
= TREE_TYPE(left_tree
);
6241 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6242 tree field
= TYPE_FIELDS(left_type_tree
);
6243 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6245 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6249 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6250 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6254 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6255 return error_mark_node
;
6257 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6258 if (CAN_HAVE_LOCATION_P(ret
))
6259 SET_EXPR_LOCATION(ret
, location
);
6263 // Class Bound_method_expression.
6268 Bound_method_expression::do_traverse(Traverse
* traverse
)
6270 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6271 return TRAVERSE_EXIT
;
6272 return Expression::traverse(&this->method_
, traverse
);
6275 // Return the type of a bound method expression. The type of this
6276 // object is really the type of the method with no receiver. We
6277 // should be able to get away with just returning the type of the
6281 Bound_method_expression::do_type()
6283 return this->method_
->type();
6286 // Determine the types of a method expression.
6289 Bound_method_expression::do_determine_type(const Type_context
*)
6291 this->method_
->determine_type_no_context();
6292 Type
* mtype
= this->method_
->type();
6293 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6294 if (fntype
== NULL
|| !fntype
->is_method())
6295 this->expr_
->determine_type_no_context();
6298 Type_context
subcontext(fntype
->receiver()->type(), false);
6299 this->expr_
->determine_type(&subcontext
);
6303 // Check the types of a method expression.
6306 Bound_method_expression::do_check_types(Gogo
*)
6308 Type
* type
= this->method_
->type()->deref();
6310 || type
->function_type() == NULL
6311 || !type
->function_type()->is_method())
6312 this->report_error(_("object is not a method"));
6315 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6316 Type
* etype
= (this->expr_type_
!= NULL
6318 : this->expr_
->type());
6319 etype
= etype
->deref();
6320 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6321 this->report_error(_("method type does not match object type"));
6325 // Get the tree for a method expression. There is no standard tree
6326 // representation for this. The only places it may currently be used
6327 // are in a Call_expression or a Go_statement, which will take it
6328 // apart directly. So this has nothing to do at present.
6331 Bound_method_expression::do_get_tree(Translate_context
*)
6336 // Make a method expression.
6338 Bound_method_expression
*
6339 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6340 source_location location
)
6342 return new Bound_method_expression(expr
, method
, location
);
6345 // Class Builtin_call_expression. This is used for a call to a
6346 // builtin function.
6348 class Builtin_call_expression
: public Call_expression
6351 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6352 bool is_varargs
, source_location location
);
6355 // This overrides Call_expression::do_lower.
6357 do_lower(Gogo
*, Named_object
*, int);
6360 do_is_constant() const;
6363 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6366 do_float_constant_value(mpfr_t
, Type
**) const;
6369 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6375 do_determine_type(const Type_context
*);
6378 do_check_types(Gogo
*);
6383 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6384 this->args()->copy(),
6390 do_get_tree(Translate_context
*);
6393 do_export(Export
*) const;
6396 do_is_recover_call() const;
6399 do_set_recover_arg(Expression
*);
6402 // The builtin functions.
6403 enum Builtin_function_code
6407 // Predeclared builtin functions.
6424 // Builtin functions from the unsafe package.
6437 real_imag_type(Type
*);
6442 // A pointer back to the general IR structure. This avoids a global
6443 // variable, or passing it around everywhere.
6445 // The builtin function being called.
6446 Builtin_function_code code_
;
6449 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6451 Expression_list
* args
,
6453 source_location location
)
6454 : Call_expression(fn
, args
, is_varargs
, location
),
6455 gogo_(gogo
), code_(BUILTIN_INVALID
)
6457 Func_expression
* fnexp
= this->fn()->func_expression();
6458 gcc_assert(fnexp
!= NULL
);
6459 const std::string
& name(fnexp
->named_object()->name());
6460 if (name
== "append")
6461 this->code_
= BUILTIN_APPEND
;
6462 else if (name
== "cap")
6463 this->code_
= BUILTIN_CAP
;
6464 else if (name
== "close")
6465 this->code_
= BUILTIN_CLOSE
;
6466 else if (name
== "closed")
6467 this->code_
= BUILTIN_CLOSED
;
6468 else if (name
== "cmplx")
6469 this->code_
= BUILTIN_CMPLX
;
6470 else if (name
== "copy")
6471 this->code_
= BUILTIN_COPY
;
6472 else if (name
== "imag")
6473 this->code_
= BUILTIN_IMAG
;
6474 else if (name
== "len")
6475 this->code_
= BUILTIN_LEN
;
6476 else if (name
== "make")
6477 this->code_
= BUILTIN_MAKE
;
6478 else if (name
== "new")
6479 this->code_
= BUILTIN_NEW
;
6480 else if (name
== "panic")
6481 this->code_
= BUILTIN_PANIC
;
6482 else if (name
== "print")
6483 this->code_
= BUILTIN_PRINT
;
6484 else if (name
== "println")
6485 this->code_
= BUILTIN_PRINTLN
;
6486 else if (name
== "real")
6487 this->code_
= BUILTIN_REAL
;
6488 else if (name
== "recover")
6489 this->code_
= BUILTIN_RECOVER
;
6490 else if (name
== "Alignof")
6491 this->code_
= BUILTIN_ALIGNOF
;
6492 else if (name
== "Offsetof")
6493 this->code_
= BUILTIN_OFFSETOF
;
6494 else if (name
== "Sizeof")
6495 this->code_
= BUILTIN_SIZEOF
;
6500 // Return whether this is a call to recover. This is a virtual
6501 // function called from the parent class.
6504 Builtin_call_expression::do_is_recover_call() const
6506 if (this->classification() == EXPRESSION_ERROR
)
6508 return this->code_
== BUILTIN_RECOVER
;
6511 // Set the argument for a call to recover.
6514 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6516 const Expression_list
* args
= this->args();
6517 gcc_assert(args
== NULL
|| args
->empty());
6518 Expression_list
* new_args
= new Expression_list();
6519 new_args
->push_back(arg
);
6520 this->set_args(new_args
);
6523 // A traversal class which looks for a call expression.
6525 class Find_call_expression
: public Traverse
6528 Find_call_expression()
6529 : Traverse(traverse_expressions
),
6534 expression(Expression
**);
6538 { return this->found_
; }
6545 Find_call_expression::expression(Expression
** pexpr
)
6547 if ((*pexpr
)->call_expression() != NULL
)
6549 this->found_
= true;
6550 return TRAVERSE_EXIT
;
6552 return TRAVERSE_CONTINUE
;
6555 // Lower a builtin call expression. This turns new and make into
6556 // specific expressions. We also convert to a constant if we can.
6559 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6561 if (this->code_
== BUILTIN_NEW
)
6563 const Expression_list
* args
= this->args();
6564 if (args
== NULL
|| args
->size() < 1)
6565 this->report_error(_("not enough arguments"));
6566 else if (args
->size() > 1)
6567 this->report_error(_("too many arguments"));
6570 Expression
* arg
= args
->front();
6571 if (!arg
->is_type_expression())
6573 error_at(arg
->location(), "expected type");
6574 this->set_is_error();
6577 return Expression::make_allocation(arg
->type(), this->location());
6580 else if (this->code_
== BUILTIN_MAKE
)
6582 const Expression_list
* args
= this->args();
6583 if (args
== NULL
|| args
->size() < 1)
6584 this->report_error(_("not enough arguments"));
6587 Expression
* arg
= args
->front();
6588 if (!arg
->is_type_expression())
6590 error_at(arg
->location(), "expected type");
6591 this->set_is_error();
6595 Expression_list
* newargs
;
6596 if (args
->size() == 1)
6600 newargs
= new Expression_list();
6601 Expression_list::const_iterator p
= args
->begin();
6603 for (; p
!= args
->end(); ++p
)
6604 newargs
->push_back(*p
);
6606 return Expression::make_make(arg
->type(), newargs
,
6611 else if (this->is_constant())
6613 // We can only lower len and cap if there are no function calls
6614 // in the arguments. Otherwise we have to make the call.
6615 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6617 Expression
* arg
= this->one_arg();
6618 if (!arg
->is_constant())
6620 Find_call_expression find_call
;
6621 Expression::traverse(&arg
, &find_call
);
6622 if (find_call
.found())
6630 if (this->integer_constant_value(true, ival
, &type
))
6632 Expression
* ret
= Expression::make_integer(&ival
, type
,
6641 if (this->float_constant_value(rval
, &type
))
6643 Expression
* ret
= Expression::make_float(&rval
, type
,
6651 if (this->complex_constant_value(rval
, imag
, &type
))
6653 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6662 else if (this->code_
== BUILTIN_RECOVER
)
6664 if (function
!= NULL
)
6665 function
->func_value()->set_calls_recover();
6668 // Calling recover outside of a function always returns the
6669 // nil empty interface.
6670 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6671 return Expression::make_cast(eface
,
6672 Expression::make_nil(this->location()),
6676 else if (this->code_
== BUILTIN_APPEND
)
6678 // Lower the varargs.
6679 const Expression_list
* args
= this->args();
6680 if (args
== NULL
|| args
->empty())
6682 Type
* slice_type
= args
->front()->type();
6683 if (!slice_type
->is_open_array_type())
6685 error_at(args
->front()->location(), "argument 1 must be a slice");
6686 this->set_is_error();
6689 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6695 // Return the type of the real or imag functions, given the type of
6696 // the argument. We need to map complex to float, complex64 to
6697 // float32, and complex128 to float64, so it has to be done by name.
6698 // This returns NULL if it can't figure out the type.
6701 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6703 if (arg_type
== NULL
|| arg_type
->is_abstract())
6705 Named_type
* nt
= arg_type
->named_type();
6708 while (nt
->real_type()->named_type() != NULL
)
6709 nt
= nt
->real_type()->named_type();
6710 if (nt
->name() == "complex")
6711 return Type::lookup_float_type("float");
6712 else if (nt
->name() == "complex64")
6713 return Type::lookup_float_type("float32");
6714 else if (nt
->name() == "complex128")
6715 return Type::lookup_float_type("float64");
6720 // Return the type of the cmplx function, given the type of one of the
6721 // argments. Like real_imag_type, we have to map by name.
6724 Builtin_call_expression::cmplx_type(Type
* arg_type
)
6726 if (arg_type
== NULL
|| arg_type
->is_abstract())
6728 Named_type
* nt
= arg_type
->named_type();
6731 while (nt
->real_type()->named_type() != NULL
)
6732 nt
= nt
->real_type()->named_type();
6733 if (nt
->name() == "float")
6734 return Type::lookup_complex_type("complex");
6735 else if (nt
->name() == "float32")
6736 return Type::lookup_complex_type("complex64");
6737 else if (nt
->name() == "float64")
6738 return Type::lookup_complex_type("complex128");
6743 // Return a single argument, or NULL if there isn't one.
6746 Builtin_call_expression::one_arg() const
6748 const Expression_list
* args
= this->args();
6749 if (args
->size() != 1)
6751 return args
->front();
6754 // Return whether this is constant: len of a string, or len or cap of
6755 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6758 Builtin_call_expression::do_is_constant() const
6760 switch (this->code_
)
6765 Expression
* arg
= this->one_arg();
6768 Type
* arg_type
= arg
->type();
6770 if (arg_type
->points_to() != NULL
6771 && arg_type
->points_to()->array_type() != NULL
6772 && !arg_type
->points_to()->is_open_array_type())
6773 arg_type
= arg_type
->points_to();
6775 if (arg_type
->array_type() != NULL
6776 && arg_type
->array_type()->length() != NULL
)
6777 return arg_type
->array_type()->length()->is_constant();
6779 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6780 return arg
->is_constant();
6784 case BUILTIN_SIZEOF
:
6785 case BUILTIN_ALIGNOF
:
6786 return this->one_arg() != NULL
;
6788 case BUILTIN_OFFSETOF
:
6790 Expression
* arg
= this->one_arg();
6793 return arg
->field_reference_expression() != NULL
;
6798 const Expression_list
* args
= this->args();
6799 if (args
!= NULL
&& args
->size() == 2)
6800 return args
->front()->is_constant() && args
->back()->is_constant();
6807 Expression
* arg
= this->one_arg();
6808 return arg
!= NULL
&& arg
->is_constant();
6818 // Return an integer constant value if possible.
6821 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
6825 if (this->code_
== BUILTIN_LEN
6826 || this->code_
== BUILTIN_CAP
)
6828 Expression
* arg
= this->one_arg();
6831 Type
* arg_type
= arg
->type();
6833 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6836 if (arg
->string_constant_value(&sval
))
6838 mpz_set_ui(val
, sval
.length());
6839 *ptype
= Type::lookup_integer_type("int");
6844 if (arg_type
->points_to() != NULL
6845 && arg_type
->points_to()->array_type() != NULL
6846 && !arg_type
->points_to()->is_open_array_type())
6847 arg_type
= arg_type
->points_to();
6849 if (arg_type
->array_type() != NULL
6850 && arg_type
->array_type()->length() != NULL
)
6852 Expression
* e
= arg_type
->array_type()->length();
6853 if (e
->integer_constant_value(iota_is_constant
, val
, ptype
))
6855 *ptype
= Type::lookup_integer_type("int");
6860 else if (this->code_
== BUILTIN_SIZEOF
6861 || this->code_
== BUILTIN_ALIGNOF
)
6863 Expression
* arg
= this->one_arg();
6866 Type
* arg_type
= arg
->type();
6867 if (arg_type
->is_error_type() || arg_type
->is_undefined())
6869 if (arg_type
->is_abstract())
6871 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
6872 unsigned long val_long
;
6873 if (this->code_
== BUILTIN_SIZEOF
)
6875 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
6876 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
6877 if (TREE_INT_CST_HIGH(type_size
) != 0)
6879 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
6880 val_long
= static_cast<unsigned long>(val_wide
);
6881 if (val_long
!= val_wide
)
6884 else if (this->code_
== BUILTIN_ALIGNOF
)
6886 if (arg
->field_reference_expression() == NULL
)
6887 val_long
= go_type_alignment(arg_type_tree
);
6890 // Calling unsafe.Alignof(s.f) returns the alignment of
6891 // the type of f when it is used as a field in a struct.
6892 val_long
= go_field_alignment(arg_type_tree
);
6897 mpz_set_ui(val
, val_long
);
6901 else if (this->code_
== BUILTIN_OFFSETOF
)
6903 Expression
* arg
= this->one_arg();
6906 Field_reference_expression
* farg
= arg
->field_reference_expression();
6909 Expression
* struct_expr
= farg
->expr();
6910 Type
* st
= struct_expr
->type();
6911 if (st
->struct_type() == NULL
)
6913 tree struct_tree
= st
->get_tree(this->gogo_
);
6914 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
6915 tree field
= TYPE_FIELDS(struct_tree
);
6916 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
6918 field
= DECL_CHAIN(field
);
6919 gcc_assert(field
!= NULL_TREE
);
6921 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
6922 if (offset_wide
< 0)
6924 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
6925 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
6927 mpz_set_ui(val
, offset_long
);
6933 // Return a floating point constant value if possible.
6936 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
6939 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
6941 Expression
* arg
= this->one_arg();
6952 if (arg
->complex_constant_value(real
, imag
, &type
))
6954 if (this->code_
== BUILTIN_REAL
)
6955 mpfr_set(val
, real
, GMP_RNDN
);
6957 mpfr_set(val
, imag
, GMP_RNDN
);
6958 *ptype
= Builtin_call_expression::real_imag_type(type
);
6970 // Return a complex constant value if possible.
6973 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
6976 if (this->code_
== BUILTIN_CMPLX
)
6978 const Expression_list
* args
= this->args();
6979 if (args
== NULL
|| args
->size() != 2)
6985 if (!args
->front()->float_constant_value(r
, &rtype
))
6996 if (args
->back()->float_constant_value(i
, &itype
)
6997 && Type::are_identical(rtype
, itype
, false, NULL
))
6999 mpfr_set(real
, r
, GMP_RNDN
);
7000 mpfr_set(imag
, i
, GMP_RNDN
);
7001 *ptype
= Builtin_call_expression::cmplx_type(rtype
);
7017 Builtin_call_expression::do_type()
7019 switch (this->code_
)
7021 case BUILTIN_INVALID
:
7028 const Expression_list
* args
= this->args();
7029 if (args
== NULL
|| args
->empty())
7030 return Type::make_error_type();
7031 return Type::make_pointer_type(args
->front()->type());
7037 case BUILTIN_ALIGNOF
:
7038 case BUILTIN_OFFSETOF
:
7039 case BUILTIN_SIZEOF
:
7040 return Type::lookup_integer_type("int");
7045 case BUILTIN_PRINTLN
:
7046 return Type::make_void_type();
7048 case BUILTIN_CLOSED
:
7049 return Type::lookup_bool_type();
7051 case BUILTIN_RECOVER
:
7052 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7054 case BUILTIN_APPEND
:
7056 const Expression_list
* args
= this->args();
7057 if (args
== NULL
|| args
->empty())
7058 return Type::make_error_type();
7059 return args
->front()->type();
7065 Expression
* arg
= this->one_arg();
7067 return Type::make_error_type();
7068 Type
* t
= arg
->type();
7069 if (t
->is_abstract())
7070 t
= t
->make_non_abstract_type();
7071 t
= Builtin_call_expression::real_imag_type(t
);
7073 t
= Type::make_error_type();
7079 const Expression_list
* args
= this->args();
7080 if (args
== NULL
|| args
->size() != 2)
7081 return Type::make_error_type();
7082 Type
* t
= args
->front()->type();
7083 if (t
->is_abstract())
7085 t
= args
->back()->type();
7086 if (t
->is_abstract())
7087 t
= t
->make_non_abstract_type();
7089 t
= Builtin_call_expression::cmplx_type(t
);
7091 t
= Type::make_error_type();
7097 // Determine the type.
7100 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7102 this->fn()->determine_type_no_context();
7104 const Expression_list
* args
= this->args();
7107 Type
* arg_type
= NULL
;
7108 switch (this->code_
)
7111 case BUILTIN_PRINTLN
:
7112 // Do not force a large integer constant to "int".
7118 arg_type
= Builtin_call_expression::cmplx_type(context
->type
);
7124 // For the cmplx function the type of one operand can
7125 // determine the type of the other, as in a binary expression.
7126 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7127 if (args
!= NULL
&& args
->size() == 2)
7129 Type
* t1
= args
->front()->type();
7130 Type
* t2
= args
->front()->type();
7131 if (!t1
->is_abstract())
7133 else if (!t2
->is_abstract())
7147 for (Expression_list::const_iterator pa
= args
->begin();
7151 Type_context subcontext
;
7152 subcontext
.type
= arg_type
;
7156 // We want to print large constants, we so can't just
7157 // use the appropriate nonabstract type. Use uint64 for
7158 // an integer if we know it is nonnegative, otherwise
7159 // use int64 for a integer, otherwise use float64 for a
7160 // float or complex128 for a complex.
7161 Type
* want_type
= NULL
;
7162 Type
* atype
= (*pa
)->type();
7163 if (atype
->is_abstract())
7165 if (atype
->integer_type() != NULL
)
7170 if (this->integer_constant_value(true, val
, &dummy
)
7171 && mpz_sgn(val
) >= 0)
7172 want_type
= Type::lookup_integer_type("uint64");
7174 want_type
= Type::lookup_integer_type("int64");
7177 else if (atype
->float_type() != NULL
)
7178 want_type
= Type::lookup_float_type("float64");
7179 else if (atype
->complex_type() != NULL
)
7180 want_type
= Type::lookup_complex_type("complex128");
7181 else if (atype
->is_abstract_string_type())
7182 want_type
= Type::lookup_string_type();
7183 else if (atype
->is_abstract_boolean_type())
7184 want_type
= Type::lookup_bool_type();
7187 subcontext
.type
= want_type
;
7191 (*pa
)->determine_type(&subcontext
);
7196 // If there is exactly one argument, return true. Otherwise give an
7197 // error message and return false.
7200 Builtin_call_expression::check_one_arg()
7202 const Expression_list
* args
= this->args();
7203 if (args
== NULL
|| args
->size() < 1)
7205 this->report_error(_("not enough arguments"));
7208 else if (args
->size() > 1)
7210 this->report_error(_("too many arguments"));
7213 if (args
->front()->is_error_expression()
7214 || args
->front()->type()->is_error_type()
7215 || args
->front()->type()->is_undefined())
7217 this->set_is_error();
7223 // Check argument types for a builtin function.
7226 Builtin_call_expression::do_check_types(Gogo
*)
7228 switch (this->code_
)
7230 case BUILTIN_INVALID
:
7238 // The single argument may be either a string or an array or a
7239 // map or a channel, or a pointer to a closed array.
7240 if (this->check_one_arg())
7242 Type
* arg_type
= this->one_arg()->type();
7243 if (arg_type
->points_to() != NULL
7244 && arg_type
->points_to()->array_type() != NULL
7245 && !arg_type
->points_to()->is_open_array_type())
7246 arg_type
= arg_type
->points_to();
7247 if (this->code_
== BUILTIN_CAP
)
7249 if (!arg_type
->is_error_type()
7250 && arg_type
->array_type() == NULL
7251 && arg_type
->channel_type() == NULL
)
7252 this->report_error(_("argument must be array or slice "
7257 if (!arg_type
->is_error_type()
7258 && !arg_type
->is_string_type()
7259 && arg_type
->array_type() == NULL
7260 && arg_type
->map_type() == NULL
7261 && arg_type
->channel_type() == NULL
)
7262 this->report_error(_("argument must be string or "
7263 "array or slice or map or channel"));
7270 case BUILTIN_PRINTLN
:
7272 const Expression_list
* args
= this->args();
7275 if (this->code_
== BUILTIN_PRINT
)
7276 warning_at(this->location(), 0,
7277 "no arguments for builtin function %<%s%>",
7278 (this->code_
== BUILTIN_PRINT
7284 for (Expression_list::const_iterator p
= args
->begin();
7288 Type
* type
= (*p
)->type();
7289 if (type
->is_error_type()
7290 || type
->is_string_type()
7291 || type
->integer_type() != NULL
7292 || type
->float_type() != NULL
7293 || type
->complex_type() != NULL
7294 || type
->is_boolean_type()
7295 || type
->points_to() != NULL
7296 || type
->interface_type() != NULL
7297 || type
->channel_type() != NULL
7298 || type
->map_type() != NULL
7299 || type
->function_type() != NULL
7300 || type
->is_open_array_type())
7303 this->report_error(_("unsupported argument type to "
7304 "builtin function"));
7311 case BUILTIN_CLOSED
:
7312 if (this->check_one_arg())
7314 if (this->one_arg()->type()->channel_type() == NULL
)
7315 this->report_error(_("argument must be channel"));
7320 case BUILTIN_SIZEOF
:
7321 case BUILTIN_ALIGNOF
:
7322 this->check_one_arg();
7325 case BUILTIN_RECOVER
:
7326 if (this->args() != NULL
&& !this->args()->empty())
7327 this->report_error(_("too many arguments"));
7330 case BUILTIN_OFFSETOF
:
7331 if (this->check_one_arg())
7333 Expression
* arg
= this->one_arg();
7334 if (arg
->field_reference_expression() == NULL
)
7335 this->report_error(_("argument must be a field reference"));
7341 const Expression_list
* args
= this->args();
7342 if (args
== NULL
|| args
->size() < 2)
7344 this->report_error(_("not enough arguments"));
7347 else if (args
->size() > 2)
7349 this->report_error(_("too many arguments"));
7352 Type
* arg1_type
= args
->front()->type();
7353 Type
* arg2_type
= args
->back()->type();
7354 if (arg1_type
->is_error_type() || arg2_type
->is_error_type())
7358 if (arg1_type
->is_open_array_type())
7359 e1
= arg1_type
->array_type()->element_type();
7362 this->report_error(_("left argument must be a slice"));
7367 if (arg2_type
->is_open_array_type())
7368 e2
= arg2_type
->array_type()->element_type();
7369 else if (arg2_type
->is_string_type())
7370 e2
= Type::lookup_integer_type("uint8");
7373 this->report_error(_("right argument must be a slice or a string"));
7377 if (!Type::are_identical(e1
, e2
, true, NULL
))
7378 this->report_error(_("element types must be the same"));
7382 case BUILTIN_APPEND
:
7384 const Expression_list
* args
= this->args();
7385 if (args
== NULL
|| args
->empty())
7387 this->report_error(_("not enough arguments"));
7390 /* Lowering varargs should have left us with 2 arguments. */
7391 gcc_assert(args
->size() == 2);
7393 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7397 this->report_error(_("arguments 1 and 2 have different types"));
7400 error_at(this->location(),
7401 "arguments 1 and 2 have different types (%s)",
7403 this->set_is_error();
7411 if (this->check_one_arg())
7413 if (this->one_arg()->type()->complex_type() == NULL
)
7414 this->report_error(_("argument must have complex type"));
7420 const Expression_list
* args
= this->args();
7421 if (args
== NULL
|| args
->size() < 2)
7422 this->report_error(_("not enough arguments"));
7423 else if (args
->size() > 2)
7424 this->report_error(_("too many arguments"));
7425 else if (args
->front()->is_error_expression()
7426 || args
->front()->type()->is_error_type()
7427 || args
->back()->is_error_expression()
7428 || args
->back()->type()->is_error_type())
7429 this->set_is_error();
7430 else if (!Type::are_identical(args
->front()->type(),
7431 args
->back()->type(), true, NULL
))
7432 this->report_error(_("cmplx arguments must have identical types"));
7433 else if (args
->front()->type()->float_type() == NULL
)
7434 this->report_error(_("cmplx arguments must have "
7435 "floating-point type"));
7444 // Return the tree for a builtin function.
7447 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7449 Gogo
* gogo
= context
->gogo();
7450 source_location location
= this->location();
7451 switch (this->code_
)
7453 case BUILTIN_INVALID
:
7461 const Expression_list
* args
= this->args();
7462 gcc_assert(args
!= NULL
&& args
->size() == 1);
7463 Expression
* arg
= *args
->begin();
7464 Type
* arg_type
= arg
->type();
7465 tree arg_tree
= arg
->get_tree(context
);
7466 if (arg_tree
== error_mark_node
)
7467 return error_mark_node
;
7469 if (arg_type
->points_to() != NULL
)
7471 arg_type
= arg_type
->points_to();
7472 gcc_assert(arg_type
->array_type() != NULL
7473 && !arg_type
->is_open_array_type());
7474 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7475 arg_tree
= build_fold_indirect_ref(arg_tree
);
7479 if (this->code_
== BUILTIN_LEN
)
7481 if (arg_type
->is_string_type())
7482 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7483 else if (arg_type
->array_type() != NULL
)
7484 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7485 else if (arg_type
->map_type() != NULL
)
7487 static tree map_len_fndecl
;
7488 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7493 arg_type
->get_tree(gogo
),
7496 else if (arg_type
->channel_type() != NULL
)
7498 static tree chan_len_fndecl
;
7499 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7504 arg_type
->get_tree(gogo
),
7512 if (arg_type
->array_type() != NULL
)
7513 val_tree
= arg_type
->array_type()->capacity_tree(gogo
, arg_tree
);
7514 else if (arg_type
->channel_type() != NULL
)
7516 static tree chan_cap_fndecl
;
7517 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7522 arg_type
->get_tree(gogo
),
7529 if (val_tree
== error_mark_node
)
7530 return error_mark_node
;
7532 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7533 if (type_tree
== TREE_TYPE(val_tree
))
7536 return fold(convert_to_integer(type_tree
, val_tree
));
7540 case BUILTIN_PRINTLN
:
7542 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7543 tree stmt_list
= NULL_TREE
;
7545 const Expression_list
* call_args
= this->args();
7546 if (call_args
!= NULL
)
7548 for (Expression_list::const_iterator p
= call_args
->begin();
7549 p
!= call_args
->end();
7552 if (is_ln
&& p
!= call_args
->begin())
7554 static tree print_space_fndecl
;
7555 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7560 if (call
== error_mark_node
)
7561 return error_mark_node
;
7562 append_to_statement_list(call
, &stmt_list
);
7565 Type
* type
= (*p
)->type();
7567 tree arg
= (*p
)->get_tree(context
);
7568 if (arg
== error_mark_node
)
7569 return error_mark_node
;
7573 if (type
->is_string_type())
7575 static tree print_string_fndecl
;
7576 pfndecl
= &print_string_fndecl
;
7577 fnname
= "__go_print_string";
7579 else if (type
->integer_type() != NULL
7580 && type
->integer_type()->is_unsigned())
7582 static tree print_uint64_fndecl
;
7583 pfndecl
= &print_uint64_fndecl
;
7584 fnname
= "__go_print_uint64";
7585 Type
* itype
= Type::lookup_integer_type("uint64");
7586 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7589 else if (type
->integer_type() != NULL
)
7591 static tree print_int64_fndecl
;
7592 pfndecl
= &print_int64_fndecl
;
7593 fnname
= "__go_print_int64";
7594 Type
* itype
= Type::lookup_integer_type("int64");
7595 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7598 else if (type
->float_type() != NULL
)
7600 static tree print_double_fndecl
;
7601 pfndecl
= &print_double_fndecl
;
7602 fnname
= "__go_print_double";
7603 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7605 else if (type
->complex_type() != NULL
)
7607 static tree print_complex_fndecl
;
7608 pfndecl
= &print_complex_fndecl
;
7609 fnname
= "__go_print_complex";
7610 arg
= fold_convert_loc(location
, complex_double_type_node
,
7613 else if (type
->is_boolean_type())
7615 static tree print_bool_fndecl
;
7616 pfndecl
= &print_bool_fndecl
;
7617 fnname
= "__go_print_bool";
7619 else if (type
->points_to() != NULL
7620 || type
->channel_type() != NULL
7621 || type
->map_type() != NULL
7622 || type
->function_type() != NULL
)
7624 static tree print_pointer_fndecl
;
7625 pfndecl
= &print_pointer_fndecl
;
7626 fnname
= "__go_print_pointer";
7627 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7629 else if (type
->interface_type() != NULL
)
7631 if (type
->interface_type()->is_empty())
7633 static tree print_empty_interface_fndecl
;
7634 pfndecl
= &print_empty_interface_fndecl
;
7635 fnname
= "__go_print_empty_interface";
7639 static tree print_interface_fndecl
;
7640 pfndecl
= &print_interface_fndecl
;
7641 fnname
= "__go_print_interface";
7644 else if (type
->is_open_array_type())
7646 static tree print_slice_fndecl
;
7647 pfndecl
= &print_slice_fndecl
;
7648 fnname
= "__go_print_slice";
7653 tree call
= Gogo::call_builtin(pfndecl
,
7660 if (call
== error_mark_node
)
7661 return error_mark_node
;
7662 append_to_statement_list(call
, &stmt_list
);
7668 static tree print_nl_fndecl
;
7669 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7674 if (call
== error_mark_node
)
7675 return error_mark_node
;
7676 append_to_statement_list(call
, &stmt_list
);
7684 const Expression_list
* args
= this->args();
7685 gcc_assert(args
!= NULL
&& args
->size() == 1);
7686 Expression
* arg
= args
->front();
7687 tree arg_tree
= arg
->get_tree(context
);
7688 if (arg_tree
== error_mark_node
)
7689 return error_mark_node
;
7690 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7691 arg_tree
= Expression::convert_for_assignment(context
, empty
,
7693 arg_tree
, location
);
7694 static tree panic_fndecl
;
7695 tree call
= Gogo::call_builtin(&panic_fndecl
,
7700 TREE_TYPE(arg_tree
),
7702 if (call
== error_mark_node
)
7703 return error_mark_node
;
7704 // This function will throw an exception.
7705 TREE_NOTHROW(panic_fndecl
) = 0;
7706 // This function will not return.
7707 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
7711 case BUILTIN_RECOVER
:
7713 // The argument is set when building recover thunks. It's a
7714 // boolean value which is true if we can recover a value now.
7715 const Expression_list
* args
= this->args();
7716 gcc_assert(args
!= NULL
&& args
->size() == 1);
7717 Expression
* arg
= args
->front();
7718 tree arg_tree
= arg
->get_tree(context
);
7719 if (arg_tree
== error_mark_node
)
7720 return error_mark_node
;
7722 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7723 tree empty_tree
= empty
->get_tree(context
->gogo());
7725 Type
* nil_type
= Type::make_nil_type();
7726 Expression
* nil
= Expression::make_nil(location
);
7727 tree nil_tree
= nil
->get_tree(context
);
7728 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
7734 // We need to handle a deferred call to recover specially,
7735 // because it changes whether it can recover a panic or not.
7736 // See test7 in test/recover1.go.
7738 if (this->is_deferred())
7740 static tree deferred_recover_fndecl
;
7741 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
7743 "__go_deferred_recover",
7749 static tree recover_fndecl
;
7750 call
= Gogo::call_builtin(&recover_fndecl
,
7756 if (call
== error_mark_node
)
7757 return error_mark_node
;
7758 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
7759 call
, empty_nil_tree
);
7763 case BUILTIN_CLOSED
:
7765 const Expression_list
* args
= this->args();
7766 gcc_assert(args
!= NULL
&& args
->size() == 1);
7767 Expression
* arg
= args
->front();
7768 tree arg_tree
= arg
->get_tree(context
);
7769 if (arg_tree
== error_mark_node
)
7770 return error_mark_node
;
7771 if (this->code_
== BUILTIN_CLOSE
)
7773 static tree close_fndecl
;
7774 return Gogo::call_builtin(&close_fndecl
,
7776 "__go_builtin_close",
7779 TREE_TYPE(arg_tree
),
7784 static tree closed_fndecl
;
7785 return Gogo::call_builtin(&closed_fndecl
,
7787 "__go_builtin_closed",
7790 TREE_TYPE(arg_tree
),
7795 case BUILTIN_SIZEOF
:
7796 case BUILTIN_OFFSETOF
:
7797 case BUILTIN_ALIGNOF
:
7802 bool b
= this->integer_constant_value(true, val
, &dummy
);
7804 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
7805 tree ret
= Expression::integer_constant_tree(val
, type
);
7812 const Expression_list
* args
= this->args();
7813 gcc_assert(args
!= NULL
&& args
->size() == 2);
7814 Expression
* arg1
= args
->front();
7815 Expression
* arg2
= args
->back();
7817 tree arg1_tree
= arg1
->get_tree(context
);
7818 tree arg2_tree
= arg2
->get_tree(context
);
7819 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7820 return error_mark_node
;
7822 Type
* arg1_type
= arg1
->type();
7823 Array_type
* at
= arg1_type
->array_type();
7824 arg1_tree
= save_expr(arg1_tree
);
7825 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
7826 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
7827 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
7828 return error_mark_node
;
7830 Type
* arg2_type
= arg2
->type();
7833 if (arg2_type
->is_open_array_type())
7835 at
= arg2_type
->array_type();
7836 arg2_tree
= save_expr(arg2_tree
);
7837 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7838 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7842 arg2_tree
= save_expr(arg2_tree
);
7843 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
7844 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
7846 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7847 return error_mark_node
;
7849 arg1_len
= save_expr(arg1_len
);
7850 arg2_len
= save_expr(arg2_len
);
7851 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
7852 fold_build2_loc(location
, LT_EXPR
,
7854 arg1_len
, arg2_len
),
7855 arg1_len
, arg2_len
);
7856 len
= save_expr(len
);
7858 Type
* element_type
= at
->element_type();
7859 tree element_type_tree
= element_type
->get_tree(gogo
);
7860 if (element_type_tree
== error_mark_node
)
7861 return error_mark_node
;
7862 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7863 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
7865 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
7866 TREE_TYPE(element_size
),
7867 bytecount
, element_size
);
7868 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
7870 arg1_val
= fold_convert_loc(location
, ptr_type_node
, arg1_val
);
7871 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7873 static tree copy_fndecl
;
7874 tree call
= Gogo::call_builtin(©_fndecl
,
7885 if (call
== error_mark_node
)
7886 return error_mark_node
;
7888 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
7892 case BUILTIN_APPEND
:
7894 const Expression_list
* args
= this->args();
7895 gcc_assert(args
!= NULL
&& args
->size() == 2);
7896 Expression
* arg1
= args
->front();
7897 Expression
* arg2
= args
->back();
7899 Array_type
* at
= arg1
->type()->array_type();
7900 Type
* element_type
= at
->element_type();
7902 tree arg1_tree
= arg1
->get_tree(context
);
7903 tree arg2_tree
= arg2
->get_tree(context
);
7904 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7905 return error_mark_node
;
7907 Array_type
* at2
= arg2
->type()->array_type();
7908 arg2_tree
= save_expr(arg2_tree
);
7909 tree arg2_val
= at2
->value_pointer_tree(gogo
, arg2_tree
);
7910 tree arg2_len
= at2
->length_tree(gogo
, arg2_tree
);
7911 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7912 return error_mark_node
;
7913 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7914 arg2_len
= fold_convert_loc(location
, size_type_node
, arg2_len
);
7916 tree element_type_tree
= element_type
->get_tree(gogo
);
7917 if (element_type_tree
== error_mark_node
)
7918 return error_mark_node
;
7919 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7920 element_size
= fold_convert_loc(location
, size_type_node
,
7923 // We rebuild the decl each time since the slice types may
7925 tree append_fndecl
= NULL_TREE
;
7926 return Gogo::call_builtin(&append_fndecl
,
7930 TREE_TYPE(arg1_tree
),
7931 TREE_TYPE(arg1_tree
),
7944 const Expression_list
* args
= this->args();
7945 gcc_assert(args
!= NULL
&& args
->size() == 1);
7946 Expression
* arg
= args
->front();
7947 tree arg_tree
= arg
->get_tree(context
);
7948 if (arg_tree
== error_mark_node
)
7949 return error_mark_node
;
7950 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
7951 if (this->code_
== BUILTIN_REAL
)
7952 return fold_build1_loc(location
, REALPART_EXPR
,
7953 TREE_TYPE(TREE_TYPE(arg_tree
)),
7956 return fold_build1_loc(location
, IMAGPART_EXPR
,
7957 TREE_TYPE(TREE_TYPE(arg_tree
)),
7963 const Expression_list
* args
= this->args();
7964 gcc_assert(args
!= NULL
&& args
->size() == 2);
7965 tree r
= args
->front()->get_tree(context
);
7966 tree i
= args
->back()->get_tree(context
);
7967 if (r
== error_mark_node
|| i
== error_mark_node
)
7968 return error_mark_node
;
7969 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
7970 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
7971 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
7972 return fold_build2_loc(location
, COMPLEX_EXPR
,
7973 build_complex_type(TREE_TYPE(r
)),
7982 // We have to support exporting a builtin call expression, because
7983 // code can set a constant to the result of a builtin expression.
7986 Builtin_call_expression::do_export(Export
* exp
) const
7993 if (this->integer_constant_value(true, val
, &dummy
))
7995 Integer_expression::export_integer(exp
, val
);
8004 if (this->float_constant_value(fval
, &dummy
))
8006 Float_expression::export_float(exp
, fval
);
8018 if (this->complex_constant_value(real
, imag
, &dummy
))
8020 Complex_expression::export_complex(exp
, real
, imag
);
8029 error_at(this->location(), "value is not constant");
8033 // A trailing space lets us reliably identify the end of the number.
8034 exp
->write_c_string(" ");
8037 // Class Call_expression.
8042 Call_expression::do_traverse(Traverse
* traverse
)
8044 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8045 return TRAVERSE_EXIT
;
8046 if (this->args_
!= NULL
)
8048 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8049 return TRAVERSE_EXIT
;
8051 return TRAVERSE_CONTINUE
;
8054 // Lower a call statement.
8057 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
8059 // A type case can look like a function call.
8060 if (this->fn_
->is_type_expression()
8061 && this->args_
!= NULL
8062 && this->args_
->size() == 1)
8063 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8066 // Recognize a call to a builtin function.
8067 Func_expression
* fne
= this->fn_
->func_expression();
8069 && fne
->named_object()->is_function_declaration()
8070 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
8071 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8072 this->is_varargs_
, this->location());
8074 // Handle an argument which is a call to a function which returns
8075 // multiple results.
8076 if (this->args_
!= NULL
8077 && this->args_
->size() == 1
8078 && this->args_
->front()->call_expression() != NULL
8079 && this->fn_
->type()->function_type() != NULL
)
8081 Function_type
* fntype
= this->fn_
->type()->function_type();
8082 size_t rc
= this->args_
->front()->call_expression()->result_count();
8084 && fntype
->parameters() != NULL
8085 && (fntype
->parameters()->size() == rc
8086 || (fntype
->is_varargs()
8087 && fntype
->parameters()->size() - 1 <= rc
)))
8089 Call_expression
* call
= this->args_
->front()->call_expression();
8090 Expression_list
* args
= new Expression_list
;
8091 for (size_t i
= 0; i
< rc
; ++i
)
8092 args
->push_back(Expression::make_call_result(call
, i
));
8093 // We can't return a new call expression here, because this
8094 // one may be referenced by Call_result expressions. FIXME.
8100 // Handle a call to a varargs function by packaging up the extra
8102 if (this->fn_
->type()->function_type() != NULL
8103 && this->fn_
->type()->function_type()->is_varargs())
8105 Function_type
* fntype
= this->fn_
->type()->function_type();
8106 const Typed_identifier_list
* parameters
= fntype
->parameters();
8107 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
8108 Type
* varargs_type
= parameters
->back().type();
8109 return this->lower_varargs(gogo
, function
, varargs_type
,
8110 parameters
->size());
8116 // Lower a call to a varargs function. FUNCTION is the function in
8117 // which the call occurs--it's not the function we are calling.
8118 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8119 // PARAM_COUNT is the number of parameters of the function we are
8120 // calling; the last of these parameters will be the varargs
8124 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8125 Type
* varargs_type
, size_t param_count
)
8127 if (this->varargs_are_lowered_
)
8130 source_location loc
= this->location();
8132 gcc_assert(param_count
> 0);
8133 gcc_assert(varargs_type
->is_open_array_type());
8135 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8136 if (arg_count
< param_count
- 1)
8138 // Not enough arguments; will be caught in check_types.
8142 Expression_list
* old_args
= this->args_
;
8143 Expression_list
* new_args
= new Expression_list();
8144 bool push_empty_arg
= false;
8145 if (old_args
== NULL
|| old_args
->empty())
8147 gcc_assert(param_count
== 1);
8148 push_empty_arg
= true;
8152 Expression_list::const_iterator pa
;
8154 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8156 if (static_cast<size_t>(i
) == param_count
)
8158 new_args
->push_back(*pa
);
8161 // We have reached the varargs parameter.
8163 bool issued_error
= false;
8164 if (pa
== old_args
->end())
8165 push_empty_arg
= true;
8166 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8167 new_args
->push_back(*pa
);
8168 else if (this->is_varargs_
)
8170 this->report_error(_("too many arguments"));
8173 else if (pa
+ 1 == old_args
->end()
8174 && this->is_compatible_varargs_argument(function
, *pa
,
8177 new_args
->push_back(*pa
);
8180 Type
* element_type
= varargs_type
->array_type()->element_type();
8181 Expression_list
* vals
= new Expression_list
;
8182 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8184 // Check types here so that we get a better message.
8185 Type
* patype
= (*pa
)->type();
8186 source_location paloc
= (*pa
)->location();
8187 if (!this->check_argument_type(i
, element_type
, patype
,
8188 paloc
, issued_error
))
8190 vals
->push_back(*pa
);
8193 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8194 new_args
->push_back(val
);
8199 new_args
->push_back(Expression::make_nil(loc
));
8201 // We can't return a new call expression here, because this one may
8202 // be referenced by Call_result expressions. FIXME.
8203 if (old_args
!= NULL
)
8205 this->args_
= new_args
;
8206 this->varargs_are_lowered_
= true;
8208 // Lower all the new subexpressions.
8209 Expression
* ret
= this;
8210 gogo
->lower_expression(function
, &ret
);
8211 gcc_assert(ret
== this);
8215 // Return true if ARG is a varargs argment which should be passed to
8216 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8217 // will be the last argument passed in the call, and PARAM_TYPE will
8218 // be the type of the last parameter of the varargs function being
8222 Call_expression::is_compatible_varargs_argument(Named_object
* function
,
8227 *issued_error
= false;
8229 Type
* var_type
= NULL
;
8231 // The simple case is passing the varargs parameter of the caller.
8232 Var_expression
* ve
= arg
->var_expression();
8233 if (ve
!= NULL
&& ve
->named_object()->is_variable())
8235 Variable
* var
= ve
->named_object()->var_value();
8236 if (var
->is_varargs_parameter())
8237 var_type
= var
->type();
8240 // The complex case is passing the varargs parameter of some
8241 // enclosing function. This will look like passing down *c.f where
8242 // c is the closure variable and f is a field in the closure.
8243 if (function
!= NULL
8244 && function
->func_value()->needs_closure()
8245 && arg
->classification() == EXPRESSION_UNARY
)
8247 Unary_expression
* ue
= static_cast<Unary_expression
*>(arg
);
8248 if (ue
->op() == OPERATOR_MULT
)
8250 Field_reference_expression
* fre
=
8251 ue
->operand()->deref()->field_reference_expression();
8254 Var_expression
* ve
= fre
->expr()->deref()->var_expression();
8257 Named_object
* no
= ve
->named_object();
8258 Function
* f
= function
->func_value();
8259 if (no
== f
->closure_var())
8261 // At this point we know that this indeed a
8262 // reference to some enclosing variable. Now we
8263 // need to figure out whether that variable is a
8264 // varargs parameter.
8265 Named_object
* enclosing
=
8266 f
->enclosing_var(fre
->field_index());
8267 Variable
* var
= enclosing
->var_value();
8268 if (var
->is_varargs_parameter())
8269 var_type
= var
->type();
8276 if (var_type
== NULL
)
8279 // We only match if the parameter is the same, with an identical
8281 Array_type
* var_at
= var_type
->array_type();
8282 gcc_assert(var_at
!= NULL
);
8283 Array_type
* param_at
= param_type
->array_type();
8284 if (param_at
!= NULL
8285 && Type::are_identical(var_at
->element_type(),
8286 param_at
->element_type(), true, NULL
))
8288 error_at(arg
->location(), "... mismatch: passing ...T as ...");
8289 *issued_error
= true;
8293 // Get the function type. Returns NULL if we don't know the type. If
8294 // this returns NULL, and if_ERROR is true, issues an error.
8297 Call_expression::get_function_type() const
8299 return this->fn_
->type()->function_type();
8302 // Return the number of values which this call will return.
8305 Call_expression::result_count() const
8307 const Function_type
* fntype
= this->get_function_type();
8310 if (fntype
->results() == NULL
)
8312 return fntype
->results()->size();
8315 // Return whether this is a call to the predeclared function recover.
8318 Call_expression::is_recover_call() const
8320 return this->do_is_recover_call();
8323 // Set the argument to the recover function.
8326 Call_expression::set_recover_arg(Expression
* arg
)
8328 this->do_set_recover_arg(arg
);
8331 // Virtual functions also implemented by Builtin_call_expression.
8334 Call_expression::do_is_recover_call() const
8340 Call_expression::do_set_recover_arg(Expression
*)
8348 Call_expression::do_type()
8350 if (this->type_
!= NULL
)
8354 Function_type
* fntype
= this->get_function_type();
8356 return Type::make_error_type();
8358 const Typed_identifier_list
* results
= fntype
->results();
8359 if (results
== NULL
)
8360 ret
= Type::make_void_type();
8361 else if (results
->size() == 1)
8362 ret
= results
->begin()->type();
8364 ret
= Type::make_call_multiple_result_type(this);
8371 // Determine types for a call expression. We can use the function
8372 // parameter types to set the types of the arguments.
8375 Call_expression::do_determine_type(const Type_context
*)
8377 this->fn_
->determine_type_no_context();
8378 Function_type
* fntype
= this->get_function_type();
8379 const Typed_identifier_list
* parameters
= NULL
;
8381 parameters
= fntype
->parameters();
8382 if (this->args_
!= NULL
)
8384 Typed_identifier_list::const_iterator pt
;
8385 if (parameters
!= NULL
)
8386 pt
= parameters
->begin();
8387 for (Expression_list::const_iterator pa
= this->args_
->begin();
8388 pa
!= this->args_
->end();
8391 if (parameters
!= NULL
&& pt
!= parameters
->end())
8393 Type_context
subcontext(pt
->type(), false);
8394 (*pa
)->determine_type(&subcontext
);
8398 (*pa
)->determine_type_no_context();
8403 // Check types for parameter I.
8406 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8407 const Type
* argument_type
,
8408 source_location argument_location
,
8412 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8417 error_at(argument_location
, "argument %d has incompatible type", i
);
8419 error_at(argument_location
,
8420 "argument %d has incompatible type (%s)",
8423 this->set_is_error();
8432 Call_expression::do_check_types(Gogo
*)
8434 Function_type
* fntype
= this->get_function_type();
8437 if (!this->fn_
->type()->is_error_type())
8438 this->report_error(_("expected function"));
8442 if (fntype
->is_method())
8444 // We don't support pointers to methods, so the function has to
8445 // be a bound method expression.
8446 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8449 this->report_error(_("method call without object"));
8452 Type
* first_arg_type
= bme
->first_argument()->type();
8453 if (first_arg_type
->points_to() == NULL
)
8455 // When passing a value, we need to check that we are
8456 // permitted to copy it.
8458 if (!Type::are_assignable(fntype
->receiver()->type(),
8459 first_arg_type
, &reason
))
8462 this->report_error(_("incompatible type for receiver"));
8465 error_at(this->location(),
8466 "incompatible type for receiver (%s)",
8468 this->set_is_error();
8474 // Note that varargs was handled by the lower_varargs() method, so
8475 // we don't have to worry about it here.
8477 const Typed_identifier_list
* parameters
= fntype
->parameters();
8478 if (this->args_
== NULL
)
8480 if (parameters
!= NULL
&& !parameters
->empty())
8481 this->report_error(_("not enough arguments"));
8483 else if (parameters
== NULL
)
8484 this->report_error(_("too many arguments"));
8488 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8489 for (Expression_list::const_iterator pa
= this->args_
->begin();
8490 pa
!= this->args_
->end();
8493 if (pt
== parameters
->end())
8495 this->report_error(_("too many arguments"));
8498 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8499 (*pa
)->location(), false);
8501 if (pt
!= parameters
->end())
8502 this->report_error(_("not enough arguments"));
8506 // Return whether we have to use a temporary variable to ensure that
8507 // we evaluate this call expression in order. If the call returns no
8508 // results then it will inevitably be executed last. If the call
8509 // returns more than one result then it will be used with Call_result
8510 // expressions. So we only have to use a temporary variable if the
8511 // call returns exactly one result.
8514 Call_expression::do_must_eval_in_order() const
8516 return this->result_count() == 1;
8519 // Get the function and the first argument to use when calling a bound
8523 Call_expression::bound_method_function(Translate_context
* context
,
8524 Bound_method_expression
* bound_method
,
8525 tree
* first_arg_ptr
)
8527 Expression
* first_argument
= bound_method
->first_argument();
8528 tree first_arg
= first_argument
->get_tree(context
);
8529 if (first_arg
== error_mark_node
)
8530 return error_mark_node
;
8532 // We always pass a pointer to the first argument when calling a
8534 if (first_argument
->type()->points_to() == NULL
)
8536 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8537 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8538 || DECL_P(first_arg
)
8539 || TREE_CODE(first_arg
) == INDIRECT_REF
8540 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8542 first_arg
= build_fold_addr_expr(first_arg
);
8543 if (DECL_P(first_arg
))
8544 TREE_ADDRESSABLE(first_arg
) = 1;
8548 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8549 get_name(first_arg
));
8550 DECL_IGNORED_P(tmp
) = 0;
8551 DECL_INITIAL(tmp
) = first_arg
;
8552 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8553 build1(DECL_EXPR
, void_type_node
, tmp
),
8554 build_fold_addr_expr(tmp
));
8555 TREE_ADDRESSABLE(tmp
) = 1;
8557 if (first_arg
== error_mark_node
)
8558 return error_mark_node
;
8561 Type
* fatype
= bound_method
->first_argument_type();
8564 if (fatype
->points_to() == NULL
)
8565 fatype
= Type::make_pointer_type(fatype
);
8566 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8567 if (first_arg
== error_mark_node
8568 || TREE_TYPE(first_arg
) == error_mark_node
)
8569 return error_mark_node
;
8572 *first_arg_ptr
= first_arg
;
8574 return bound_method
->method()->get_tree(context
);
8577 // Get the function and the first argument to use when calling an
8578 // interface method.
8581 Call_expression::interface_method_function(
8582 Translate_context
* context
,
8583 Interface_field_reference_expression
* interface_method
,
8584 tree
* first_arg_ptr
)
8586 tree expr
= interface_method
->expr()->get_tree(context
);
8587 if (expr
== error_mark_node
)
8588 return error_mark_node
;
8589 expr
= save_expr(expr
);
8590 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8591 if (first_arg
== error_mark_node
)
8592 return error_mark_node
;
8593 *first_arg_ptr
= first_arg
;
8594 return interface_method
->get_function_tree(context
, expr
);
8597 // Build the call expression.
8600 Call_expression::do_get_tree(Translate_context
* context
)
8602 if (this->tree_
!= NULL_TREE
)
8605 Function_type
* fntype
= this->get_function_type();
8607 return error_mark_node
;
8609 if (this->fn_
->is_error_expression())
8610 return error_mark_node
;
8612 Gogo
* gogo
= context
->gogo();
8613 source_location location
= this->location();
8615 Func_expression
* func
= this->fn_
->func_expression();
8616 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8617 Interface_field_reference_expression
* interface_method
=
8618 this->fn_
->interface_field_reference_expression();
8619 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8620 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8621 gcc_assert(!fntype
->is_method() || is_method
);
8625 if (this->args_
== NULL
|| this->args_
->empty())
8627 nargs
= is_method
? 1 : 0;
8628 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8632 const Typed_identifier_list
* params
= fntype
->parameters();
8633 gcc_assert(params
!= NULL
);
8635 nargs
= this->args_
->size();
8636 int i
= is_method
? 1 : 0;
8638 args
= new tree
[nargs
];
8640 Typed_identifier_list::const_iterator pp
= params
->begin();
8641 Expression_list::const_iterator pe
;
8642 for (pe
= this->args_
->begin();
8643 pe
!= this->args_
->end();
8646 gcc_assert(pp
!= params
->end());
8647 tree arg_val
= (*pe
)->get_tree(context
);
8648 args
[i
] = Expression::convert_for_assignment(context
,
8653 if (args
[i
] == error_mark_node
)
8654 return error_mark_node
;
8656 gcc_assert(pp
== params
->end());
8657 gcc_assert(i
== nargs
);
8660 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8661 if (rettype
== error_mark_node
)
8662 return error_mark_node
;
8666 fn
= func
->get_tree_without_closure(gogo
);
8667 else if (!is_method
)
8668 fn
= this->fn_
->get_tree(context
);
8669 else if (bound_method
!= NULL
)
8670 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8671 else if (interface_method
!= NULL
)
8672 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8676 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8677 return error_mark_node
;
8679 // This is to support builtin math functions when using 80387 math.
8681 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8682 fndecl
= TREE_OPERAND(fndecl
, 0);
8683 tree excess_type
= NULL_TREE
;
8685 && DECL_IS_BUILTIN(fndecl
)
8686 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8688 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8689 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8690 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8691 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8693 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8694 if (excess_type
!= NULL_TREE
)
8696 tree excess_fndecl
= mathfn_built_in(excess_type
,
8697 DECL_FUNCTION_CODE(fndecl
));
8698 if (excess_fndecl
== NULL_TREE
)
8699 excess_type
= NULL_TREE
;
8702 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8703 for (int i
= 0; i
< nargs
; ++i
)
8704 args
[i
] = ::convert(excess_type
, args
[i
]);
8709 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8713 SET_EXPR_LOCATION(ret
, location
);
8717 tree closure_tree
= func
->closure()->get_tree(context
);
8718 if (closure_tree
!= error_mark_node
)
8719 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8722 // If this is a recursive function type which returns itself, as in
8724 // we have used ptr_type_node for the return type. Add a cast here
8725 // to the correct type.
8726 if (TREE_TYPE(ret
) == ptr_type_node
)
8728 tree t
= this->type()->get_tree(gogo
);
8729 ret
= fold_convert_loc(location
, t
, ret
);
8732 if (excess_type
!= NULL_TREE
)
8734 // Calling convert here can undo our excess precision change.
8735 // That may or may not be a bug in convert_to_real.
8736 ret
= build1(NOP_EXPR
, rettype
, ret
);
8739 // If there is more than one result, we will refer to the call
8741 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
8742 ret
= save_expr(ret
);
8749 // Make a call expression.
8752 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
8753 source_location location
)
8755 return new Call_expression(fn
, args
, is_varargs
, location
);
8758 // A single result from a call which returns multiple results.
8760 class Call_result_expression
: public Expression
8763 Call_result_expression(Call_expression
* call
, unsigned int index
)
8764 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
8765 call_(call
), index_(index
)
8770 do_traverse(Traverse
*);
8776 do_determine_type(const Type_context
*);
8779 do_check_types(Gogo
*);
8784 return new Call_result_expression(this->call_
->call_expression(),
8789 do_must_eval_in_order() const
8793 do_get_tree(Translate_context
*);
8796 // The underlying call expression.
8798 // Which result we want.
8799 unsigned int index_
;
8802 // Traverse a call result.
8805 Call_result_expression::do_traverse(Traverse
* traverse
)
8807 if (traverse
->remember_expression(this->call_
))
8809 // We have already traversed the call expression.
8810 return TRAVERSE_CONTINUE
;
8812 return Expression::traverse(&this->call_
, traverse
);
8818 Call_result_expression::do_type()
8820 if (this->classification() == EXPRESSION_ERROR
)
8821 return Type::make_error_type();
8823 // THIS->CALL_ can be replaced with a temporary reference due to
8824 // Call_expression::do_must_eval_in_order when there is an error.
8825 Call_expression
* ce
= this->call_
->call_expression();
8827 return Type::make_error_type();
8828 Function_type
* fntype
= ce
->get_function_type();
8830 return Type::make_error_type();
8831 const Typed_identifier_list
* results
= fntype
->results();
8832 if (results
== NULL
)
8834 this->report_error(_("number of results does not match "
8835 "number of values"));
8836 return Type::make_error_type();
8838 Typed_identifier_list::const_iterator pr
= results
->begin();
8839 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8841 if (pr
== results
->end())
8845 if (pr
== results
->end())
8847 this->report_error(_("number of results does not match "
8848 "number of values"));
8849 return Type::make_error_type();
8854 // Check the type. Just make sure that we trigger the warning in
8858 Call_result_expression::do_check_types(Gogo
*)
8863 // Determine the type. We have nothing to do here, but the 0 result
8864 // needs to pass down to the caller.
8867 Call_result_expression::do_determine_type(const Type_context
*)
8869 if (this->index_
== 0)
8870 this->call_
->determine_type_no_context();
8876 Call_result_expression::do_get_tree(Translate_context
* context
)
8878 tree call_tree
= this->call_
->get_tree(context
);
8879 if (call_tree
== error_mark_node
)
8880 return error_mark_node
;
8881 gcc_assert(TREE_CODE(TREE_TYPE(call_tree
)) == RECORD_TYPE
);
8882 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
8883 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8885 gcc_assert(field
!= NULL_TREE
);
8886 field
= DECL_CHAIN(field
);
8888 gcc_assert(field
!= NULL_TREE
);
8889 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
8892 // Make a reference to a single result of a call which returns
8893 // multiple results.
8896 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
8898 return new Call_result_expression(call
, index
);
8901 // Class Index_expression.
8906 Index_expression::do_traverse(Traverse
* traverse
)
8908 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
8909 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
8910 || (this->end_
!= NULL
8911 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
8912 return TRAVERSE_EXIT
;
8913 return TRAVERSE_CONTINUE
;
8916 // Lower an index expression. This converts the generic index
8917 // expression into an array index, a string index, or a map index.
8920 Index_expression::do_lower(Gogo
*, Named_object
*, int)
8922 source_location location
= this->location();
8923 Expression
* left
= this->left_
;
8924 Expression
* start
= this->start_
;
8925 Expression
* end
= this->end_
;
8927 Type
* type
= left
->type();
8928 if (type
->is_error_type())
8929 return Expression::make_error(location
);
8930 else if (type
->array_type() != NULL
)
8931 return Expression::make_array_index(left
, start
, end
, location
);
8932 else if (type
->points_to() != NULL
8933 && type
->points_to()->array_type() != NULL
8934 && !type
->points_to()->is_open_array_type())
8936 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
8938 return Expression::make_array_index(deref
, start
, end
, location
);
8940 else if (type
->is_string_type())
8941 return Expression::make_string_index(left
, start
, end
, location
);
8942 else if (type
->map_type() != NULL
)
8946 error_at(location
, "invalid slice of map");
8947 return Expression::make_error(location
);
8949 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
8951 if (this->is_lvalue_
)
8952 ret
->set_is_lvalue();
8958 "attempt to index object which is not array, string, or map");
8959 return Expression::make_error(location
);
8963 // Make an index expression.
8966 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
8967 source_location location
)
8969 return new Index_expression(left
, start
, end
, location
);
8972 // An array index. This is used for both indexing and slicing.
8974 class Array_index_expression
: public Expression
8977 Array_index_expression(Expression
* array
, Expression
* start
,
8978 Expression
* end
, source_location location
)
8979 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
8980 array_(array
), start_(start
), end_(end
), type_(NULL
)
8985 do_traverse(Traverse
*);
8991 do_determine_type(const Type_context
*);
8994 do_check_types(Gogo
*);
8999 return Expression::make_array_index(this->array_
->copy(),
9000 this->start_
->copy(),
9003 : this->end_
->copy()),
9008 do_is_addressable() const;
9011 do_address_taken(bool escapes
)
9012 { this->array_
->address_taken(escapes
); }
9015 do_get_tree(Translate_context
*);
9018 // The array we are getting a value from.
9020 // The start or only index.
9022 // The end index of a slice. This may be NULL for a simple array
9023 // index, or it may be a nil expression for the length of the array.
9025 // The type of the expression.
9029 // Array index traversal.
9032 Array_index_expression::do_traverse(Traverse
* traverse
)
9034 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9035 return TRAVERSE_EXIT
;
9036 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9037 return TRAVERSE_EXIT
;
9038 if (this->end_
!= NULL
)
9040 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9041 return TRAVERSE_EXIT
;
9043 return TRAVERSE_CONTINUE
;
9046 // Return the type of an array index.
9049 Array_index_expression::do_type()
9051 if (this->type_
== NULL
)
9053 Array_type
* type
= this->array_
->type()->array_type();
9055 this->type_
= Type::make_error_type();
9056 else if (this->end_
== NULL
)
9057 this->type_
= type
->element_type();
9058 else if (type
->is_open_array_type())
9060 // A slice of a slice has the same type as the original
9062 this->type_
= this->array_
->type()->deref();
9066 // A slice of an array is a slice.
9067 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9073 // Set the type of an array index.
9076 Array_index_expression::do_determine_type(const Type_context
*)
9078 this->array_
->determine_type_no_context();
9079 Type_context
subcontext(NULL
, true);
9080 this->start_
->determine_type(&subcontext
);
9081 if (this->end_
!= NULL
)
9082 this->end_
->determine_type(&subcontext
);
9085 // Check types of an array index.
9088 Array_index_expression::do_check_types(Gogo
*)
9090 if (this->start_
->type()->integer_type() == NULL
)
9091 this->report_error(_("index must be integer"));
9092 if (this->end_
!= NULL
9093 && this->end_
->type()->integer_type() == NULL
9094 && !this->end_
->is_nil_expression())
9095 this->report_error(_("slice end must be integer"));
9097 Array_type
* array_type
= this->array_
->type()->array_type();
9098 gcc_assert(array_type
!= NULL
);
9100 unsigned int int_bits
=
9101 Type::lookup_integer_type("int")->integer_type()->bits();
9106 bool lval_valid
= (array_type
->length() != NULL
9107 && array_type
->length()->integer_constant_value(true,
9112 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9114 if (mpz_sgn(ival
) < 0
9115 || mpz_sizeinbase(ival
, 2) >= int_bits
9117 && (this->end_
== NULL
9118 ? mpz_cmp(ival
, lval
) >= 0
9119 : mpz_cmp(ival
, lval
) > 0)))
9121 error_at(this->start_
->location(), "array index out of bounds");
9122 this->set_is_error();
9125 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9127 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9129 if (mpz_sgn(ival
) < 0
9130 || mpz_sizeinbase(ival
, 2) >= int_bits
9131 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
9133 error_at(this->end_
->location(), "array index out of bounds");
9134 this->set_is_error();
9141 // A slice of an array requires an addressable array. A slice of a
9142 // slice is always possible.
9143 if (this->end_
!= NULL
9144 && !array_type
->is_open_array_type()
9145 && !this->array_
->is_addressable())
9146 this->report_error(_("array is not addressable"));
9149 // Return whether this expression is addressable.
9152 Array_index_expression::do_is_addressable() const
9154 // A slice expression is not addressable.
9155 if (this->end_
!= NULL
)
9158 // An index into a slice is addressable.
9159 if (this->array_
->type()->is_open_array_type())
9162 // An index into an array is addressable if the array is
9164 return this->array_
->is_addressable();
9167 // Get a tree for an array index.
9170 Array_index_expression::do_get_tree(Translate_context
* context
)
9172 Gogo
* gogo
= context
->gogo();
9173 source_location loc
= this->location();
9175 Array_type
* array_type
= this->array_
->type()->array_type();
9176 if (array_type
== NULL
)
9178 gcc_assert(this->array_
->type()->is_error_type());
9179 return error_mark_node
;
9182 tree type_tree
= array_type
->get_tree(gogo
);
9183 if (type_tree
== error_mark_node
)
9184 return error_mark_node
;
9186 tree array_tree
= this->array_
->get_tree(context
);
9187 if (array_tree
== error_mark_node
)
9188 return error_mark_node
;
9190 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9191 array_tree
= save_expr(array_tree
);
9192 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
9193 if (length_tree
== error_mark_node
)
9194 return error_mark_node
;
9195 length_tree
= save_expr(length_tree
);
9196 tree length_type
= TREE_TYPE(length_tree
);
9198 tree bad_index
= boolean_false_node
;
9200 tree start_tree
= this->start_
->get_tree(context
);
9201 if (start_tree
== error_mark_node
)
9202 return error_mark_node
;
9203 if (!DECL_P(start_tree
))
9204 start_tree
= save_expr(start_tree
);
9205 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9206 start_tree
= convert_to_integer(length_type
, start_tree
);
9208 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9211 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9212 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9213 fold_build2_loc(loc
,
9217 boolean_type_node
, start_tree
,
9220 int code
= (array_type
->length() != NULL
9221 ? (this->end_
== NULL
9222 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9223 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9224 : (this->end_
== NULL
9225 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9226 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9227 tree crash
= Gogo::runtime_error(code
, loc
);
9229 if (this->end_
== NULL
)
9231 // Simple array indexing. This has to return an l-value, so
9232 // wrap the index check into START_TREE.
9233 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9234 build3(COND_EXPR
, void_type_node
,
9235 bad_index
, crash
, NULL_TREE
),
9237 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9239 if (array_type
->length() != NULL
)
9242 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9243 start_tree
, NULL_TREE
, NULL_TREE
);
9248 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9249 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9250 if (element_type_tree
== error_mark_node
)
9251 return error_mark_node
;
9252 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9253 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9254 start_tree
, element_size
);
9255 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9256 TREE_TYPE(values
), values
, offset
);
9257 return build_fold_indirect_ref(ptr
);
9263 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9264 if (capacity_tree
== error_mark_node
)
9265 return error_mark_node
;
9266 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9269 if (this->end_
->is_nil_expression())
9270 end_tree
= length_tree
;
9273 end_tree
= this->end_
->get_tree(context
);
9274 if (end_tree
== error_mark_node
)
9275 return error_mark_node
;
9276 if (!DECL_P(end_tree
))
9277 end_tree
= save_expr(end_tree
);
9278 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9279 end_tree
= convert_to_integer(length_type
, end_tree
);
9281 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9284 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9286 capacity_tree
= save_expr(capacity_tree
);
9287 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9288 fold_build2_loc(loc
, LT_EXPR
,
9290 end_tree
, start_tree
),
9291 fold_build2_loc(loc
, GT_EXPR
,
9293 end_tree
, capacity_tree
));
9294 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9295 bad_index
, bad_end
);
9298 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9299 if (element_type_tree
== error_mark_node
)
9300 return error_mark_node
;
9301 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9303 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9304 fold_convert_loc(loc
, sizetype
, start_tree
),
9307 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9308 if (value_pointer
== error_mark_node
)
9309 return error_mark_node
;
9311 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9312 TREE_TYPE(value_pointer
),
9313 value_pointer
, offset
);
9315 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9316 end_tree
, start_tree
);
9318 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9319 capacity_tree
, start_tree
);
9321 tree struct_tree
= this->type()->get_tree(gogo
);
9322 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9324 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9326 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9327 tree field
= TYPE_FIELDS(struct_tree
);
9328 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9330 elt
->value
= value_pointer
;
9332 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9333 field
= DECL_CHAIN(field
);
9334 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9336 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9338 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9339 field
= DECL_CHAIN(field
);
9340 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9342 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9344 tree constructor
= build_constructor(struct_tree
, init
);
9346 if (TREE_CONSTANT(value_pointer
)
9347 && TREE_CONSTANT(result_length_tree
)
9348 && TREE_CONSTANT(result_capacity_tree
))
9349 TREE_CONSTANT(constructor
) = 1;
9351 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9352 build3(COND_EXPR
, void_type_node
,
9353 bad_index
, crash
, NULL_TREE
),
9357 // Make an array index expression. END may be NULL.
9360 Expression::make_array_index(Expression
* array
, Expression
* start
,
9361 Expression
* end
, source_location location
)
9363 // Taking a slice of a composite literal requires moving the literal
9365 if (end
!= NULL
&& array
->is_composite_literal())
9367 array
= Expression::make_heap_composite(array
, location
);
9368 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9370 return new Array_index_expression(array
, start
, end
, location
);
9373 // A string index. This is used for both indexing and slicing.
9375 class String_index_expression
: public Expression
9378 String_index_expression(Expression
* string
, Expression
* start
,
9379 Expression
* end
, source_location location
)
9380 : Expression(EXPRESSION_STRING_INDEX
, location
),
9381 string_(string
), start_(start
), end_(end
)
9386 do_traverse(Traverse
*);
9392 do_determine_type(const Type_context
*);
9395 do_check_types(Gogo
*);
9400 return Expression::make_string_index(this->string_
->copy(),
9401 this->start_
->copy(),
9404 : this->end_
->copy()),
9409 do_get_tree(Translate_context
*);
9412 // The string we are getting a value from.
9413 Expression
* string_
;
9414 // The start or only index.
9416 // The end index of a slice. This may be NULL for a single index,
9417 // or it may be a nil expression for the length of the string.
9421 // String index traversal.
9424 String_index_expression::do_traverse(Traverse
* traverse
)
9426 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9427 return TRAVERSE_EXIT
;
9428 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9429 return TRAVERSE_EXIT
;
9430 if (this->end_
!= NULL
)
9432 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9433 return TRAVERSE_EXIT
;
9435 return TRAVERSE_CONTINUE
;
9438 // Return the type of a string index.
9441 String_index_expression::do_type()
9443 if (this->end_
== NULL
)
9444 return Type::lookup_integer_type("uint8");
9446 return Type::make_string_type();
9449 // Determine the type of a string index.
9452 String_index_expression::do_determine_type(const Type_context
*)
9454 this->string_
->determine_type_no_context();
9455 Type_context
subcontext(NULL
, true);
9456 this->start_
->determine_type(&subcontext
);
9457 if (this->end_
!= NULL
)
9458 this->end_
->determine_type(&subcontext
);
9461 // Check types of a string index.
9464 String_index_expression::do_check_types(Gogo
*)
9466 if (this->start_
->type()->integer_type() == NULL
)
9467 this->report_error(_("index must be integer"));
9468 if (this->end_
!= NULL
9469 && this->end_
->type()->integer_type() == NULL
9470 && !this->end_
->is_nil_expression())
9471 this->report_error(_("slice end must be integer"));
9474 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9479 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9481 if (mpz_sgn(ival
) < 0
9482 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9484 error_at(this->start_
->location(), "string index out of bounds");
9485 this->set_is_error();
9488 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9490 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9492 if (mpz_sgn(ival
) < 0
9493 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9495 error_at(this->end_
->location(), "string index out of bounds");
9496 this->set_is_error();
9503 // Get a tree for a string index.
9506 String_index_expression::do_get_tree(Translate_context
* context
)
9508 source_location loc
= this->location();
9510 tree string_tree
= this->string_
->get_tree(context
);
9511 if (string_tree
== error_mark_node
)
9512 return error_mark_node
;
9514 if (this->string_
->type()->points_to() != NULL
)
9515 string_tree
= build_fold_indirect_ref(string_tree
);
9516 if (!DECL_P(string_tree
))
9517 string_tree
= save_expr(string_tree
);
9518 tree string_type
= TREE_TYPE(string_tree
);
9520 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9521 length_tree
= save_expr(length_tree
);
9522 tree length_type
= TREE_TYPE(length_tree
);
9524 tree bad_index
= boolean_false_node
;
9526 tree start_tree
= this->start_
->get_tree(context
);
9527 if (start_tree
== error_mark_node
)
9528 return error_mark_node
;
9529 if (!DECL_P(start_tree
))
9530 start_tree
= save_expr(start_tree
);
9531 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9532 start_tree
= convert_to_integer(length_type
, start_tree
);
9534 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9537 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9539 int code
= (this->end_
== NULL
9540 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9541 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9542 tree crash
= Gogo::runtime_error(code
, loc
);
9544 if (this->end_
== NULL
)
9546 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9548 fold_build2_loc(loc
, GE_EXPR
,
9550 start_tree
, length_tree
));
9552 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9553 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9555 fold_convert_loc(loc
, sizetype
, start_tree
));
9556 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9558 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9559 build3(COND_EXPR
, void_type_node
,
9560 bad_index
, crash
, NULL_TREE
),
9566 if (this->end_
->is_nil_expression())
9567 end_tree
= build_int_cst(length_type
, -1);
9570 end_tree
= this->end_
->get_tree(context
);
9571 if (end_tree
== error_mark_node
)
9572 return error_mark_node
;
9573 if (!DECL_P(end_tree
))
9574 end_tree
= save_expr(end_tree
);
9575 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9576 end_tree
= convert_to_integer(length_type
, end_tree
);
9578 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9581 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9584 static tree strslice_fndecl
;
9585 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9587 "__go_string_slice",
9596 if (ret
== error_mark_node
)
9597 return error_mark_node
;
9598 // This will panic if the bounds are out of range for the
9600 TREE_NOTHROW(strslice_fndecl
) = 0;
9602 if (bad_index
== boolean_false_node
)
9605 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9606 build3(COND_EXPR
, void_type_node
,
9607 bad_index
, crash
, NULL_TREE
),
9612 // Make a string index expression. END may be NULL.
9615 Expression::make_string_index(Expression
* string
, Expression
* start
,
9616 Expression
* end
, source_location location
)
9618 return new String_index_expression(string
, start
, end
, location
);
9623 // Get the type of the map.
9626 Map_index_expression::get_map_type() const
9628 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9629 gcc_assert(mt
!= NULL
);
9633 // Map index traversal.
9636 Map_index_expression::do_traverse(Traverse
* traverse
)
9638 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9639 return TRAVERSE_EXIT
;
9640 return Expression::traverse(&this->index_
, traverse
);
9643 // Return the type of a map index.
9646 Map_index_expression::do_type()
9648 Type
* type
= this->get_map_type()->val_type();
9649 // If this map index is in a tuple assignment, we actually return a
9650 // pointer to the value type. Tuple_map_assignment_statement is
9651 // responsible for handling this correctly. We need to get the type
9652 // right in case this gets assigned to a temporary variable.
9653 if (this->is_in_tuple_assignment_
)
9654 type
= Type::make_pointer_type(type
);
9658 // Fix the type of a map index.
9661 Map_index_expression::do_determine_type(const Type_context
*)
9663 this->map_
->determine_type_no_context();
9664 Type_context
subcontext(this->get_map_type()->key_type(), false);
9665 this->index_
->determine_type(&subcontext
);
9668 // Check types of a map index.
9671 Map_index_expression::do_check_types(Gogo
*)
9674 if (!Type::are_assignable(this->get_map_type()->key_type(),
9675 this->index_
->type(), &reason
))
9678 this->report_error(_("incompatible type for map index"));
9681 error_at(this->location(), "incompatible type for map index (%s)",
9683 this->set_is_error();
9688 // Get a tree for a map index.
9691 Map_index_expression::do_get_tree(Translate_context
* context
)
9693 Map_type
* type
= this->get_map_type();
9695 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9696 if (valptr
== error_mark_node
)
9697 return error_mark_node
;
9698 valptr
= save_expr(valptr
);
9700 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
9702 if (this->is_lvalue_
)
9703 return build_fold_indirect_ref(valptr
);
9704 else if (this->is_in_tuple_assignment_
)
9706 // Tuple_map_assignment_statement is responsible for using this
9712 return fold_build3(COND_EXPR
, val_type_tree
,
9713 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
9714 fold_convert(TREE_TYPE(valptr
),
9715 null_pointer_node
)),
9716 type
->val_type()->get_init_tree(context
->gogo(),
9718 build_fold_indirect_ref(valptr
));
9722 // Get a tree for the map index. This returns a tree which evaluates
9723 // to a pointer to a value. The pointer will be NULL if the key is
9727 Map_index_expression::get_value_pointer(Translate_context
* context
,
9730 Map_type
* type
= this->get_map_type();
9732 tree map_tree
= this->map_
->get_tree(context
);
9733 tree index_tree
= this->index_
->get_tree(context
);
9734 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
9735 this->index_
->type(),
9738 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
9739 return error_mark_node
;
9741 if (this->map_
->type()->points_to() != NULL
)
9742 map_tree
= build_fold_indirect_ref(map_tree
);
9744 // We need to pass in a pointer to the key, so stuff it into a
9746 tree tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
9747 DECL_IGNORED_P(tmp
) = 0;
9748 DECL_INITIAL(tmp
) = index_tree
;
9749 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
9750 tree tmpref
= fold_convert(const_ptr_type_node
, build_fold_addr_expr(tmp
));
9751 TREE_ADDRESSABLE(tmp
) = 1;
9753 static tree map_index_fndecl
;
9754 tree call
= Gogo::call_builtin(&map_index_fndecl
,
9758 const_ptr_type_node
,
9759 TREE_TYPE(map_tree
),
9761 const_ptr_type_node
,
9766 : boolean_false_node
));
9767 if (call
== error_mark_node
)
9768 return error_mark_node
;
9769 // This can panic on a map of interface type if the interface holds
9770 // an uncomparable or unhashable type.
9771 TREE_NOTHROW(map_index_fndecl
) = 0;
9773 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
9774 if (val_type_tree
== error_mark_node
)
9775 return error_mark_node
;
9776 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
9778 return build2(COMPOUND_EXPR
, ptr_val_type_tree
,
9780 fold_convert(ptr_val_type_tree
, call
));
9783 // Make a map index expression.
9785 Map_index_expression
*
9786 Expression::make_map_index(Expression
* map
, Expression
* index
,
9787 source_location location
)
9789 return new Map_index_expression(map
, index
, location
);
9792 // Class Field_reference_expression.
9794 // Return the type of a field reference.
9797 Field_reference_expression::do_type()
9799 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9800 gcc_assert(struct_type
!= NULL
);
9801 return struct_type
->field(this->field_index_
)->type();
9804 // Check the types for a field reference.
9807 Field_reference_expression::do_check_types(Gogo
*)
9809 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9810 gcc_assert(struct_type
!= NULL
);
9811 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
9814 // Get a tree for a field reference.
9817 Field_reference_expression::do_get_tree(Translate_context
* context
)
9819 tree struct_tree
= this->expr_
->get_tree(context
);
9820 if (struct_tree
== error_mark_node
9821 || TREE_TYPE(struct_tree
) == error_mark_node
)
9822 return error_mark_node
;
9823 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
9824 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
9825 if (field
== NULL_TREE
)
9827 // This can happen for a type which refers to itself indirectly
9828 // and then turns out to be erroneous.
9829 gcc_assert(saw_errors());
9830 return error_mark_node
;
9832 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
9834 field
= DECL_CHAIN(field
);
9835 gcc_assert(field
!= NULL_TREE
);
9837 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
9841 // Make a reference to a qualified identifier in an expression.
9843 Field_reference_expression
*
9844 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
9845 source_location location
)
9847 return new Field_reference_expression(expr
, field_index
, location
);
9850 // Class Interface_field_reference_expression.
9852 // Return a tree for the pointer to the function to call.
9855 Interface_field_reference_expression::get_function_tree(Translate_context
*,
9858 if (this->expr_
->type()->points_to() != NULL
)
9859 expr
= build_fold_indirect_ref(expr
);
9861 tree expr_type
= TREE_TYPE(expr
);
9862 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9864 tree field
= TYPE_FIELDS(expr_type
);
9865 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
9867 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9868 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
9870 table
= build_fold_indirect_ref(table
);
9871 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
9873 std::string name
= Gogo::unpack_hidden_name(this->name_
);
9874 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
9876 field
= DECL_CHAIN(field
))
9878 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
9881 gcc_assert(field
!= NULL_TREE
);
9883 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
9886 // Return a tree for the first argument to pass to the interface
9890 Interface_field_reference_expression::get_underlying_object_tree(
9894 if (this->expr_
->type()->points_to() != NULL
)
9895 expr
= build_fold_indirect_ref(expr
);
9897 tree expr_type
= TREE_TYPE(expr
);
9898 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9900 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
9901 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
9903 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9909 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
9911 return Expression::traverse(&this->expr_
, traverse
);
9914 // Return the type of an interface field reference.
9917 Interface_field_reference_expression::do_type()
9919 Type
* expr_type
= this->expr_
->type();
9921 Type
* points_to
= expr_type
->points_to();
9922 if (points_to
!= NULL
)
9923 expr_type
= points_to
;
9925 Interface_type
* interface_type
= expr_type
->interface_type();
9926 if (interface_type
== NULL
)
9927 return Type::make_error_type();
9929 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
9931 return Type::make_error_type();
9933 return method
->type();
9939 Interface_field_reference_expression::do_determine_type(const Type_context
*)
9941 this->expr_
->determine_type_no_context();
9944 // Check the types for an interface field reference.
9947 Interface_field_reference_expression::do_check_types(Gogo
*)
9949 Type
* type
= this->expr_
->type();
9951 Type
* points_to
= type
->points_to();
9952 if (points_to
!= NULL
)
9955 Interface_type
* interface_type
= type
->interface_type();
9956 if (interface_type
== NULL
)
9957 this->report_error(_("expected interface or pointer to interface"));
9960 const Typed_identifier
* method
=
9961 interface_type
->find_method(this->name_
);
9964 error_at(this->location(), "method %qs not in interface",
9965 Gogo::message_name(this->name_
).c_str());
9966 this->set_is_error();
9971 // Get a tree for a reference to a field in an interface. There is no
9972 // standard tree type representation for this: it's a function
9973 // attached to its first argument, like a Bound_method_expression.
9974 // The only places it may currently be used are in a Call_expression
9975 // or a Go_statement, which will take it apart directly. So this has
9976 // nothing to do at present.
9979 Interface_field_reference_expression::do_get_tree(Translate_context
*)
9984 // Make a reference to a field in an interface.
9987 Expression::make_interface_field_reference(Expression
* expr
,
9988 const std::string
& field
,
9989 source_location location
)
9991 return new Interface_field_reference_expression(expr
, field
, location
);
9994 // A general selector. This is a Parser_expression for LEFT.NAME. It
9995 // is lowered after we know the type of the left hand side.
9997 class Selector_expression
: public Parser_expression
10000 Selector_expression(Expression
* left
, const std::string
& name
,
10001 source_location location
)
10002 : Parser_expression(EXPRESSION_SELECTOR
, location
),
10003 left_(left
), name_(name
)
10008 do_traverse(Traverse
* traverse
)
10009 { return Expression::traverse(&this->left_
, traverse
); }
10012 do_lower(Gogo
*, Named_object
*, int);
10017 return new Selector_expression(this->left_
->copy(), this->name_
,
10023 lower_method_expression(Gogo
*);
10025 // The expression on the left hand side.
10027 // The name on the right hand side.
10031 // Lower a selector expression once we know the real type of the left
10035 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
10037 Expression
* left
= this->left_
;
10038 if (left
->is_type_expression())
10039 return this->lower_method_expression(gogo
);
10040 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
10044 // Lower a method expression T.M or (*T).M. We turn this into a
10045 // function literal.
10048 Selector_expression::lower_method_expression(Gogo
* gogo
)
10050 source_location location
= this->location();
10051 Type
* type
= this->left_
->type();
10052 const std::string
& name(this->name_
);
10055 if (type
->points_to() == NULL
)
10056 is_pointer
= false;
10060 type
= type
->points_to();
10062 Named_type
* nt
= type
->named_type();
10066 ("method expression requires named type or "
10067 "pointer to named type"));
10068 return Expression::make_error(location
);
10072 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
10073 if (method
== NULL
)
10076 error_at(location
, "type %<%s%> has no method %<%s%>",
10077 nt
->message_name().c_str(),
10078 Gogo::message_name(name
).c_str());
10080 error_at(location
, "method %<%s%> is ambiguous in type %<%s%>",
10081 Gogo::message_name(name
).c_str(),
10082 nt
->message_name().c_str());
10083 return Expression::make_error(location
);
10086 if (!is_pointer
&& !method
->is_value_method())
10088 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
10089 nt
->message_name().c_str(),
10090 Gogo::message_name(name
).c_str());
10091 return Expression::make_error(location
);
10094 // Build a new function type in which the receiver becomes the first
10096 Function_type
* method_type
= method
->type();
10097 gcc_assert(method_type
->is_method());
10099 const char* const receiver_name
= "$this";
10100 Typed_identifier_list
* parameters
= new Typed_identifier_list();
10101 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
10104 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
10105 if (method_parameters
!= NULL
)
10107 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10108 p
!= method_parameters
->end();
10110 parameters
->push_back(*p
);
10113 const Typed_identifier_list
* method_results
= method_type
->results();
10114 Typed_identifier_list
* results
;
10115 if (method_results
== NULL
)
10119 results
= new Typed_identifier_list();
10120 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
10121 p
!= method_results
->end();
10123 results
->push_back(*p
);
10126 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
10128 if (method_type
->is_varargs())
10129 fntype
->set_is_varargs();
10131 // We generate methods which always takes a pointer to the receiver
10132 // as their first argument. If this is for a pointer type, we can
10133 // simply reuse the existing function. We use an internal hack to
10134 // get the right type.
10138 Named_object
* mno
= (method
->needs_stub_method()
10139 ? method
->stub_object()
10140 : method
->named_object());
10141 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
10142 f
= Expression::make_cast(fntype
, f
, location
);
10143 Type_conversion_expression
* tce
=
10144 static_cast<Type_conversion_expression
*>(f
);
10145 tce
->set_may_convert_function_types();
10149 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
10152 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
10153 gcc_assert(vno
!= NULL
);
10154 Expression
* ve
= Expression::make_var_reference(vno
, location
);
10155 Expression
* bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
10156 gcc_assert(bm
!= NULL
&& !bm
->is_error_expression());
10158 Expression_list
* args
;
10159 if (method_parameters
== NULL
)
10163 args
= new Expression_list();
10164 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10165 p
!= method_parameters
->end();
10168 vno
= gogo
->lookup(p
->name(), NULL
);
10169 gcc_assert(vno
!= NULL
);
10170 args
->push_back(Expression::make_var_reference(vno
, location
));
10174 Call_expression
* call
= Expression::make_call(bm
, args
,
10175 method_type
->is_varargs(),
10178 size_t count
= call
->result_count();
10181 s
= Statement::make_statement(call
);
10184 Expression_list
* retvals
= new Expression_list();
10186 retvals
->push_back(call
);
10189 for (size_t i
= 0; i
< count
; ++i
)
10190 retvals
->push_back(Expression::make_call_result(call
, i
));
10192 s
= Statement::make_return_statement(no
->func_value()->type()->results(),
10193 retvals
, location
);
10195 gogo
->add_statement(s
);
10197 gogo
->finish_function(location
);
10199 return Expression::make_func_reference(no
, NULL
, location
);
10202 // Make a selector expression.
10205 Expression::make_selector(Expression
* left
, const std::string
& name
,
10206 source_location location
)
10208 return new Selector_expression(left
, name
, location
);
10211 // Implement the builtin function new.
10213 class Allocation_expression
: public Expression
10216 Allocation_expression(Type
* type
, source_location location
)
10217 : Expression(EXPRESSION_ALLOCATION
, location
),
10223 do_traverse(Traverse
* traverse
)
10224 { return Type::traverse(this->type_
, traverse
); }
10228 { return Type::make_pointer_type(this->type_
); }
10231 do_determine_type(const Type_context
*)
10235 do_check_types(Gogo
*);
10239 { return new Allocation_expression(this->type_
, this->location()); }
10242 do_get_tree(Translate_context
*);
10245 // The type we are allocating.
10249 // Check the type of an allocation expression.
10252 Allocation_expression::do_check_types(Gogo
*)
10254 if (this->type_
->function_type() != NULL
)
10255 this->report_error(_("invalid new of function type"));
10258 // Return a tree for an allocation expression.
10261 Allocation_expression::do_get_tree(Translate_context
* context
)
10263 tree type_tree
= this->type_
->get_tree(context
->gogo());
10264 if (type_tree
== error_mark_node
)
10265 return error_mark_node
;
10266 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10267 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10269 if (space
== error_mark_node
)
10270 return error_mark_node
;
10271 return fold_convert(build_pointer_type(type_tree
), space
);
10274 // Make an allocation expression.
10277 Expression::make_allocation(Type
* type
, source_location location
)
10279 return new Allocation_expression(type
, location
);
10282 // Implement the builtin function make.
10284 class Make_expression
: public Expression
10287 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10288 : Expression(EXPRESSION_MAKE
, location
),
10289 type_(type
), args_(args
)
10294 do_traverse(Traverse
* traverse
);
10298 { return this->type_
; }
10301 do_determine_type(const Type_context
*);
10304 do_check_types(Gogo
*);
10309 return new Make_expression(this->type_
, this->args_
->copy(),
10314 do_get_tree(Translate_context
*);
10317 // The type we are making.
10319 // The arguments to pass to the make routine.
10320 Expression_list
* args_
;
10326 Make_expression::do_traverse(Traverse
* traverse
)
10328 if (this->args_
!= NULL
10329 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10330 return TRAVERSE_EXIT
;
10331 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10332 return TRAVERSE_EXIT
;
10333 return TRAVERSE_CONTINUE
;
10336 // Set types of arguments.
10339 Make_expression::do_determine_type(const Type_context
*)
10341 if (this->args_
!= NULL
)
10343 Type_context
context(Type::lookup_integer_type("int"), false);
10344 for (Expression_list::const_iterator pe
= this->args_
->begin();
10345 pe
!= this->args_
->end();
10347 (*pe
)->determine_type(&context
);
10351 // Check types for a make expression.
10354 Make_expression::do_check_types(Gogo
*)
10356 if (this->type_
->channel_type() == NULL
10357 && this->type_
->map_type() == NULL
10358 && (this->type_
->array_type() == NULL
10359 || this->type_
->array_type()->length() != NULL
))
10360 this->report_error(_("invalid type for make function"));
10361 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10362 this->set_is_error();
10365 // Return a tree for a make expression.
10368 Make_expression::do_get_tree(Translate_context
* context
)
10370 return this->type_
->make_expression_tree(context
, this->args_
,
10374 // Make a make expression.
10377 Expression::make_make(Type
* type
, Expression_list
* args
,
10378 source_location location
)
10380 return new Make_expression(type
, args
, location
);
10383 // Construct a struct.
10385 class Struct_construction_expression
: public Expression
10388 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10389 source_location location
)
10390 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10391 type_(type
), vals_(vals
)
10394 // Return whether this is a constant initializer.
10396 is_constant_struct() const;
10400 do_traverse(Traverse
* traverse
);
10404 { return this->type_
; }
10407 do_determine_type(const Type_context
*);
10410 do_check_types(Gogo
*);
10415 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10420 do_is_addressable() const
10424 do_get_tree(Translate_context
*);
10427 do_export(Export
*) const;
10430 // The type of the struct to construct.
10432 // The list of values, in order of the fields in the struct. A NULL
10433 // entry means that the field should be zero-initialized.
10434 Expression_list
* vals_
;
10440 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10442 if (this->vals_
!= NULL
10443 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10444 return TRAVERSE_EXIT
;
10445 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10446 return TRAVERSE_EXIT
;
10447 return TRAVERSE_CONTINUE
;
10450 // Return whether this is a constant initializer.
10453 Struct_construction_expression::is_constant_struct() const
10455 if (this->vals_
== NULL
)
10457 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10458 pv
!= this->vals_
->end();
10462 && !(*pv
)->is_constant()
10463 && (!(*pv
)->is_composite_literal()
10464 || (*pv
)->is_nonconstant_composite_literal()))
10468 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10469 for (Struct_field_list::const_iterator pf
= fields
->begin();
10470 pf
!= fields
->end();
10473 // There are no constant constructors for interfaces.
10474 if (pf
->type()->interface_type() != NULL
)
10481 // Final type determination.
10484 Struct_construction_expression::do_determine_type(const Type_context
*)
10486 if (this->vals_
== NULL
)
10488 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10489 Expression_list::const_iterator pv
= this->vals_
->begin();
10490 for (Struct_field_list::const_iterator pf
= fields
->begin();
10491 pf
!= fields
->end();
10494 if (pv
== this->vals_
->end())
10498 Type_context
subcontext(pf
->type(), false);
10499 (*pv
)->determine_type(&subcontext
);
10507 Struct_construction_expression::do_check_types(Gogo
*)
10509 if (this->vals_
== NULL
)
10512 Struct_type
* st
= this->type_
->struct_type();
10513 if (this->vals_
->size() > st
->field_count())
10515 this->report_error(_("too many expressions for struct"));
10519 const Struct_field_list
* fields
= st
->fields();
10520 Expression_list::const_iterator pv
= this->vals_
->begin();
10522 for (Struct_field_list::const_iterator pf
= fields
->begin();
10523 pf
!= fields
->end();
10526 if (pv
== this->vals_
->end())
10528 this->report_error(_("too few expressions for struct"));
10535 std::string reason
;
10536 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10538 if (reason
.empty())
10539 error_at((*pv
)->location(),
10540 "incompatible type for field %d in struct construction",
10543 error_at((*pv
)->location(),
10544 ("incompatible type for field %d in "
10545 "struct construction (%s)"),
10546 i
+ 1, reason
.c_str());
10547 this->set_is_error();
10550 gcc_assert(pv
== this->vals_
->end());
10553 // Return a tree for constructing a struct.
10556 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10558 Gogo
* gogo
= context
->gogo();
10560 if (this->vals_
== NULL
)
10561 return this->type_
->get_init_tree(gogo
, false);
10563 tree type_tree
= this->type_
->get_tree(gogo
);
10564 if (type_tree
== error_mark_node
)
10565 return error_mark_node
;
10566 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10568 bool is_constant
= true;
10569 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10570 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10572 Struct_field_list::const_iterator pf
= fields
->begin();
10573 Expression_list::const_iterator pv
= this->vals_
->begin();
10574 for (tree field
= TYPE_FIELDS(type_tree
);
10575 field
!= NULL_TREE
;
10576 field
= DECL_CHAIN(field
), ++pf
)
10578 gcc_assert(pf
!= fields
->end());
10581 if (pv
== this->vals_
->end())
10582 val
= pf
->type()->get_init_tree(gogo
, false);
10583 else if (*pv
== NULL
)
10585 val
= pf
->type()->get_init_tree(gogo
, false);
10590 val
= Expression::convert_for_assignment(context
, pf
->type(),
10592 (*pv
)->get_tree(context
),
10597 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10598 return error_mark_node
;
10600 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10601 elt
->index
= field
;
10603 if (!TREE_CONSTANT(val
))
10604 is_constant
= false;
10606 gcc_assert(pf
== fields
->end());
10608 tree ret
= build_constructor(type_tree
, elts
);
10610 TREE_CONSTANT(ret
) = 1;
10614 // Export a struct construction.
10617 Struct_construction_expression::do_export(Export
* exp
) const
10619 exp
->write_c_string("convert(");
10620 exp
->write_type(this->type_
);
10621 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10622 pv
!= this->vals_
->end();
10625 exp
->write_c_string(", ");
10627 (*pv
)->export_expression(exp
);
10629 exp
->write_c_string(")");
10632 // Make a struct composite literal. This used by the thunk code.
10635 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
10636 source_location location
)
10638 gcc_assert(type
->struct_type() != NULL
);
10639 return new Struct_construction_expression(type
, vals
, location
);
10642 // Construct an array. This class is not used directly; instead we
10643 // use the child classes, Fixed_array_construction_expression and
10644 // Open_array_construction_expression.
10646 class Array_construction_expression
: public Expression
10649 Array_construction_expression(Expression_classification classification
,
10650 Type
* type
, Expression_list
* vals
,
10651 source_location location
)
10652 : Expression(classification
, location
),
10653 type_(type
), vals_(vals
)
10657 // Return whether this is a constant initializer.
10659 is_constant_array() const;
10661 // Return the number of elements.
10663 element_count() const
10664 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
10668 do_traverse(Traverse
* traverse
);
10672 { return this->type_
; }
10675 do_determine_type(const Type_context
*);
10678 do_check_types(Gogo
*);
10681 do_is_addressable() const
10685 do_export(Export
*) const;
10687 // The list of values.
10690 { return this->vals_
; }
10692 // Get a constructor tree for the array values.
10694 get_constructor_tree(Translate_context
* context
, tree type_tree
);
10697 // The type of the array to construct.
10699 // The list of values.
10700 Expression_list
* vals_
;
10706 Array_construction_expression::do_traverse(Traverse
* traverse
)
10708 if (this->vals_
!= NULL
10709 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10710 return TRAVERSE_EXIT
;
10711 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10712 return TRAVERSE_EXIT
;
10713 return TRAVERSE_CONTINUE
;
10716 // Return whether this is a constant initializer.
10719 Array_construction_expression::is_constant_array() const
10721 if (this->vals_
== NULL
)
10724 // There are no constant constructors for interfaces.
10725 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
10728 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10729 pv
!= this->vals_
->end();
10733 && !(*pv
)->is_constant()
10734 && (!(*pv
)->is_composite_literal()
10735 || (*pv
)->is_nonconstant_composite_literal()))
10741 // Final type determination.
10744 Array_construction_expression::do_determine_type(const Type_context
*)
10746 if (this->vals_
== NULL
)
10748 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
10749 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10750 pv
!= this->vals_
->end();
10754 (*pv
)->determine_type(&subcontext
);
10761 Array_construction_expression::do_check_types(Gogo
*)
10763 if (this->vals_
== NULL
)
10766 Array_type
* at
= this->type_
->array_type();
10768 Type
* element_type
= at
->element_type();
10769 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10770 pv
!= this->vals_
->end();
10774 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
10776 error_at((*pv
)->location(),
10777 "incompatible type for element %d in composite literal",
10779 this->set_is_error();
10783 Expression
* length
= at
->length();
10784 if (length
!= NULL
)
10789 if (at
->length()->integer_constant_value(true, val
, &type
))
10791 if (this->vals_
->size() > mpz_get_ui(val
))
10792 this->report_error(_("too many elements in composite literal"));
10798 // Get a constructor tree for the array values.
10801 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
10804 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10805 (this->vals_
== NULL
10807 : this->vals_
->size()));
10808 Type
* element_type
= this->type_
->array_type()->element_type();
10809 bool is_constant
= true;
10810 if (this->vals_
!= NULL
)
10813 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10814 pv
!= this->vals_
->end();
10817 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
10818 elt
->index
= size_int(i
);
10820 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10823 tree value_tree
= (*pv
)->get_tree(context
);
10824 elt
->value
= Expression::convert_for_assignment(context
,
10830 if (elt
->value
== error_mark_node
)
10831 return error_mark_node
;
10832 if (!TREE_CONSTANT(elt
->value
))
10833 is_constant
= false;
10837 tree ret
= build_constructor(type_tree
, values
);
10839 TREE_CONSTANT(ret
) = 1;
10843 // Export an array construction.
10846 Array_construction_expression::do_export(Export
* exp
) const
10848 exp
->write_c_string("convert(");
10849 exp
->write_type(this->type_
);
10850 if (this->vals_
!= NULL
)
10852 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10853 pv
!= this->vals_
->end();
10856 exp
->write_c_string(", ");
10858 (*pv
)->export_expression(exp
);
10861 exp
->write_c_string(")");
10864 // Construct a fixed array.
10866 class Fixed_array_construction_expression
:
10867 public Array_construction_expression
10870 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
10871 source_location location
)
10872 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
10873 type
, vals
, location
)
10875 gcc_assert(type
->array_type() != NULL
10876 && type
->array_type()->length() != NULL
);
10883 return new Fixed_array_construction_expression(this->type(),
10884 (this->vals() == NULL
10886 : this->vals()->copy()),
10891 do_get_tree(Translate_context
*);
10894 // Return a tree for constructing a fixed array.
10897 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
10899 return this->get_constructor_tree(context
,
10900 this->type()->get_tree(context
->gogo()));
10903 // Construct an open array.
10905 class Open_array_construction_expression
: public Array_construction_expression
10908 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
10909 source_location location
)
10910 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
10911 type
, vals
, location
)
10913 gcc_assert(type
->array_type() != NULL
10914 && type
->array_type()->length() == NULL
);
10918 // Note that taking the address of an open array literal is invalid.
10923 return new Open_array_construction_expression(this->type(),
10924 (this->vals() == NULL
10926 : this->vals()->copy()),
10931 do_get_tree(Translate_context
*);
10934 // Return a tree for constructing an open array.
10937 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
10939 Type
* element_type
= this->type()->array_type()->element_type();
10940 tree element_type_tree
= element_type
->get_tree(context
->gogo());
10941 if (element_type_tree
== error_mark_node
)
10942 return error_mark_node
;
10946 if (this->vals() == NULL
|| this->vals()->empty())
10948 // We need to create a unique value.
10949 tree max
= size_int(0);
10950 tree constructor_type
= build_array_type(element_type_tree
,
10951 build_index_type(max
));
10952 if (constructor_type
== error_mark_node
)
10953 return error_mark_node
;
10954 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
10955 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
10956 elt
->index
= size_int(0);
10957 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10958 values
= build_constructor(constructor_type
, vec
);
10959 if (TREE_CONSTANT(elt
->value
))
10960 TREE_CONSTANT(values
) = 1;
10961 length_tree
= size_int(0);
10965 tree max
= size_int(this->vals()->size() - 1);
10966 tree constructor_type
= build_array_type(element_type_tree
,
10967 build_index_type(max
));
10968 if (constructor_type
== error_mark_node
)
10969 return error_mark_node
;
10970 values
= this->get_constructor_tree(context
, constructor_type
);
10971 length_tree
= size_int(this->vals()->size());
10974 if (values
== error_mark_node
)
10975 return error_mark_node
;
10977 bool is_constant_initializer
= TREE_CONSTANT(values
);
10978 bool is_in_function
= context
->function() != NULL
;
10980 if (is_constant_initializer
)
10982 tree tmp
= build_decl(this->location(), VAR_DECL
,
10983 create_tmp_var_name("C"), TREE_TYPE(values
));
10984 DECL_EXTERNAL(tmp
) = 0;
10985 TREE_PUBLIC(tmp
) = 0;
10986 TREE_STATIC(tmp
) = 1;
10987 DECL_ARTIFICIAL(tmp
) = 1;
10988 if (is_in_function
)
10990 // If this is not a function, we will only initialize the
10991 // value once, so we can use this directly rather than
10992 // copying it. In that case we can't make it read-only,
10993 // because the program is permitted to change it.
10994 TREE_READONLY(tmp
) = 1;
10995 TREE_CONSTANT(tmp
) = 1;
10997 DECL_INITIAL(tmp
) = values
;
10998 rest_of_decl_compilation(tmp
, 1, 0);
11004 if (!is_in_function
&& is_constant_initializer
)
11006 // Outside of a function, we know the initializer will only run
11008 space
= build_fold_addr_expr(values
);
11013 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
11014 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
11016 space
= save_expr(space
);
11018 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
11019 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
11020 TREE_THIS_NOTRAP(ref
) = 1;
11021 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
11024 // Build a constructor for the open array.
11026 tree type_tree
= this->type()->get_tree(context
->gogo());
11027 if (type_tree
== error_mark_node
)
11028 return error_mark_node
;
11029 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
11031 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
11033 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11034 tree field
= TYPE_FIELDS(type_tree
);
11035 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
11036 elt
->index
= field
;
11037 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
11039 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11040 field
= DECL_CHAIN(field
);
11041 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
11042 elt
->index
= field
;
11043 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11045 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11046 field
= DECL_CHAIN(field
);
11047 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
11048 elt
->index
= field
;
11049 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11051 tree constructor
= build_constructor(type_tree
, init
);
11052 if (constructor
== error_mark_node
)
11053 return error_mark_node
;
11054 if (!is_in_function
&& is_constant_initializer
)
11055 TREE_CONSTANT(constructor
) = 1;
11057 if (set
== NULL_TREE
)
11058 return constructor
;
11060 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
11063 // Make a slice composite literal. This is used by the type
11064 // descriptor code.
11067 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
11068 source_location location
)
11070 gcc_assert(type
->is_open_array_type());
11071 return new Open_array_construction_expression(type
, vals
, location
);
11074 // Construct a map.
11076 class Map_construction_expression
: public Expression
11079 Map_construction_expression(Type
* type
, Expression_list
* vals
,
11080 source_location location
)
11081 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
11082 type_(type
), vals_(vals
)
11083 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
11087 do_traverse(Traverse
* traverse
);
11091 { return this->type_
; }
11094 do_determine_type(const Type_context
*);
11097 do_check_types(Gogo
*);
11102 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
11107 do_get_tree(Translate_context
*);
11110 do_export(Export
*) const;
11113 // The type of the map to construct.
11115 // The list of values.
11116 Expression_list
* vals_
;
11122 Map_construction_expression::do_traverse(Traverse
* traverse
)
11124 if (this->vals_
!= NULL
11125 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11126 return TRAVERSE_EXIT
;
11127 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11128 return TRAVERSE_EXIT
;
11129 return TRAVERSE_CONTINUE
;
11132 // Final type determination.
11135 Map_construction_expression::do_determine_type(const Type_context
*)
11137 if (this->vals_
== NULL
)
11140 Map_type
* mt
= this->type_
->map_type();
11141 Type_context
key_context(mt
->key_type(), false);
11142 Type_context
val_context(mt
->val_type(), false);
11143 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11144 pv
!= this->vals_
->end();
11147 (*pv
)->determine_type(&key_context
);
11149 (*pv
)->determine_type(&val_context
);
11156 Map_construction_expression::do_check_types(Gogo
*)
11158 if (this->vals_
== NULL
)
11161 Map_type
* mt
= this->type_
->map_type();
11163 Type
* key_type
= mt
->key_type();
11164 Type
* val_type
= mt
->val_type();
11165 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11166 pv
!= this->vals_
->end();
11169 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
11171 error_at((*pv
)->location(),
11172 "incompatible type for element %d key in map construction",
11174 this->set_is_error();
11177 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
11179 error_at((*pv
)->location(),
11180 ("incompatible type for element %d value "
11181 "in map construction"),
11183 this->set_is_error();
11188 // Return a tree for constructing a map.
11191 Map_construction_expression::do_get_tree(Translate_context
* context
)
11193 Gogo
* gogo
= context
->gogo();
11194 source_location loc
= this->location();
11196 Map_type
* mt
= this->type_
->map_type();
11198 // Build a struct to hold the key and value.
11199 tree struct_type
= make_node(RECORD_TYPE
);
11201 Type
* key_type
= mt
->key_type();
11202 tree id
= get_identifier("__key");
11203 tree key_type_tree
= key_type
->get_tree(gogo
);
11204 if (key_type_tree
== error_mark_node
)
11205 return error_mark_node
;
11206 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type_tree
);
11207 DECL_CONTEXT(key_field
) = struct_type
;
11208 TYPE_FIELDS(struct_type
) = key_field
;
11210 Type
* val_type
= mt
->val_type();
11211 id
= get_identifier("__val");
11212 tree val_type_tree
= val_type
->get_tree(gogo
);
11213 if (val_type_tree
== error_mark_node
)
11214 return error_mark_node
;
11215 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type_tree
);
11216 DECL_CONTEXT(val_field
) = struct_type
;
11217 DECL_CHAIN(key_field
) = val_field
;
11219 layout_type(struct_type
);
11221 bool is_constant
= true;
11226 if (this->vals_
== NULL
|| this->vals_
->empty())
11228 valaddr
= null_pointer_node
;
11229 make_tmp
= NULL_TREE
;
11233 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11234 this->vals_
->size() / 2);
11236 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11237 pv
!= this->vals_
->end();
11240 bool one_is_constant
= true;
11242 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11244 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11245 elt
->index
= key_field
;
11246 tree val_tree
= (*pv
)->get_tree(context
);
11247 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11250 if (elt
->value
== error_mark_node
)
11251 return error_mark_node
;
11252 if (!TREE_CONSTANT(elt
->value
))
11253 one_is_constant
= false;
11257 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11258 elt
->index
= val_field
;
11259 val_tree
= (*pv
)->get_tree(context
);
11260 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11263 if (elt
->value
== error_mark_node
)
11264 return error_mark_node
;
11265 if (!TREE_CONSTANT(elt
->value
))
11266 one_is_constant
= false;
11268 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11269 elt
->index
= size_int(i
);
11270 elt
->value
= build_constructor(struct_type
, one
);
11271 if (one_is_constant
)
11272 TREE_CONSTANT(elt
->value
) = 1;
11274 is_constant
= false;
11277 tree index_type
= build_index_type(size_int(i
- 1));
11278 tree array_type
= build_array_type(struct_type
, index_type
);
11279 tree init
= build_constructor(array_type
, values
);
11281 TREE_CONSTANT(init
) = 1;
11283 if (current_function_decl
!= NULL
)
11285 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11286 DECL_INITIAL(tmp
) = init
;
11287 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11288 TREE_ADDRESSABLE(tmp
) = 1;
11292 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11293 DECL_EXTERNAL(tmp
) = 0;
11294 TREE_PUBLIC(tmp
) = 0;
11295 TREE_STATIC(tmp
) = 1;
11296 DECL_ARTIFICIAL(tmp
) = 1;
11297 if (!TREE_CONSTANT(init
))
11298 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11302 TREE_READONLY(tmp
) = 1;
11303 TREE_CONSTANT(tmp
) = 1;
11304 DECL_INITIAL(tmp
) = init
;
11305 make_tmp
= NULL_TREE
;
11307 rest_of_decl_compilation(tmp
, 1, 0);
11310 valaddr
= build_fold_addr_expr(tmp
);
11313 tree descriptor
= gogo
->map_descriptor(mt
);
11315 tree type_tree
= this->type_
->get_tree(gogo
);
11316 if (type_tree
== error_mark_node
)
11317 return error_mark_node
;
11319 static tree construct_map_fndecl
;
11320 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11322 "__go_construct_map",
11325 TREE_TYPE(descriptor
),
11330 TYPE_SIZE_UNIT(struct_type
),
11332 byte_position(val_field
),
11334 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11335 const_ptr_type_node
,
11336 fold_convert(const_ptr_type_node
, valaddr
));
11337 if (call
== error_mark_node
)
11338 return error_mark_node
;
11341 if (make_tmp
== NULL
)
11344 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11348 // Export an array construction.
11351 Map_construction_expression::do_export(Export
* exp
) const
11353 exp
->write_c_string("convert(");
11354 exp
->write_type(this->type_
);
11355 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11356 pv
!= this->vals_
->end();
11359 exp
->write_c_string(", ");
11360 (*pv
)->export_expression(exp
);
11362 exp
->write_c_string(")");
11365 // A general composite literal. This is lowered to a type specific
11368 class Composite_literal_expression
: public Parser_expression
11371 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11372 Expression_list
* vals
, source_location location
)
11373 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11374 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11379 do_traverse(Traverse
* traverse
);
11382 do_lower(Gogo
*, Named_object
*, int);
11387 return new Composite_literal_expression(this->type_
, this->depth_
,
11389 (this->vals_
== NULL
11391 : this->vals_
->copy()),
11397 lower_struct(Type
*);
11400 lower_array(Type
*);
11403 make_array(Type
*, Expression_list
*);
11406 lower_map(Gogo
*, Named_object
*, Type
*);
11408 // The type of the composite literal.
11410 // The depth within a list of composite literals within a composite
11411 // literal, when the type is omitted.
11413 // The values to put in the composite literal.
11414 Expression_list
* vals_
;
11415 // If this is true, then VALS_ is a list of pairs: a key and a
11416 // value. In an array initializer, a missing key will be NULL.
11423 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11425 if (this->vals_
!= NULL
11426 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11427 return TRAVERSE_EXIT
;
11428 return Type::traverse(this->type_
, traverse
);
11431 // Lower a generic composite literal into a specific version based on
11435 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
11437 Type
* type
= this->type_
;
11439 for (int depth
= this->depth_
; depth
> 0; --depth
)
11441 if (type
->array_type() != NULL
)
11442 type
= type
->array_type()->element_type();
11443 else if (type
->map_type() != NULL
)
11444 type
= type
->map_type()->val_type();
11447 if (!type
->is_error_type())
11448 error_at(this->location(),
11449 ("may only omit types within composite literals "
11450 "of slice, array, or map type"));
11451 return Expression::make_error(this->location());
11455 if (type
->is_error_type())
11456 return Expression::make_error(this->location());
11457 else if (type
->struct_type() != NULL
)
11458 return this->lower_struct(type
);
11459 else if (type
->array_type() != NULL
)
11460 return this->lower_array(type
);
11461 else if (type
->map_type() != NULL
)
11462 return this->lower_map(gogo
, function
, type
);
11465 error_at(this->location(),
11466 ("expected struct, slice, array, or map type "
11467 "for composite literal"));
11468 return Expression::make_error(this->location());
11472 // Lower a struct composite literal.
11475 Composite_literal_expression::lower_struct(Type
* type
)
11477 source_location location
= this->location();
11478 Struct_type
* st
= type
->struct_type();
11479 if (this->vals_
== NULL
|| !this->has_keys_
)
11480 return new Struct_construction_expression(type
, this->vals_
, location
);
11482 size_t field_count
= st
->field_count();
11483 std::vector
<Expression
*> vals(field_count
);
11484 Expression_list::const_iterator p
= this->vals_
->begin();
11485 while (p
!= this->vals_
->end())
11487 Expression
* name_expr
= *p
;
11490 gcc_assert(p
!= this->vals_
->end());
11491 Expression
* val
= *p
;
11495 if (name_expr
== NULL
)
11497 error_at(val
->location(), "mixture of field and value initializers");
11498 return Expression::make_error(location
);
11501 bool bad_key
= false;
11503 switch (name_expr
->classification())
11505 case EXPRESSION_UNKNOWN_REFERENCE
:
11506 name
= name_expr
->unknown_expression()->name();
11509 case EXPRESSION_CONST_REFERENCE
:
11510 name
= static_cast<Const_expression
*>(name_expr
)->name();
11513 case EXPRESSION_TYPE
:
11515 Type
* t
= name_expr
->type();
11516 Named_type
* nt
= t
->named_type();
11524 case EXPRESSION_VAR_REFERENCE
:
11525 name
= name_expr
->var_expression()->name();
11528 case EXPRESSION_FUNC_REFERENCE
:
11529 name
= name_expr
->func_expression()->name();
11532 case EXPRESSION_UNARY
:
11533 // If there is a local variable around with the same name as
11534 // the field, and this occurs in the closure, then the
11535 // parser may turn the field reference into an indirection
11536 // through the closure. FIXME: This is a mess.
11539 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11540 if (ue
->op() == OPERATOR_MULT
)
11542 Field_reference_expression
* fre
=
11543 ue
->operand()->field_reference_expression();
11547 fre
->expr()->type()->deref()->struct_type();
11550 const Struct_field
* sf
= st
->field(fre
->field_index());
11551 name
= sf
->field_name();
11553 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11554 size_t buflen
= strlen(buf
);
11555 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11558 name
= name
.substr(0, name
.length() - buflen
);
11573 error_at(name_expr
->location(), "expected struct field name");
11574 return Expression::make_error(location
);
11577 unsigned int index
;
11578 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11581 error_at(name_expr
->location(), "unknown field %qs in %qs",
11582 Gogo::message_name(name
).c_str(),
11583 (type
->named_type() != NULL
11584 ? type
->named_type()->message_name().c_str()
11585 : "unnamed struct"));
11586 return Expression::make_error(location
);
11588 if (vals
[index
] != NULL
)
11590 error_at(name_expr
->location(),
11591 "duplicate value for field %qs in %qs",
11592 Gogo::message_name(name
).c_str(),
11593 (type
->named_type() != NULL
11594 ? type
->named_type()->message_name().c_str()
11595 : "unnamed struct"));
11596 return Expression::make_error(location
);
11602 Expression_list
* list
= new Expression_list
;
11603 list
->reserve(field_count
);
11604 for (size_t i
= 0; i
< field_count
; ++i
)
11605 list
->push_back(vals
[i
]);
11607 return new Struct_construction_expression(type
, list
, location
);
11610 // Lower an array composite literal.
11613 Composite_literal_expression::lower_array(Type
* type
)
11615 source_location location
= this->location();
11616 if (this->vals_
== NULL
|| !this->has_keys_
)
11617 return this->make_array(type
, this->vals_
);
11619 std::vector
<Expression
*> vals
;
11620 vals
.reserve(this->vals_
->size());
11621 unsigned long index
= 0;
11622 Expression_list::const_iterator p
= this->vals_
->begin();
11623 while (p
!= this->vals_
->end())
11625 Expression
* index_expr
= *p
;
11628 gcc_assert(p
!= this->vals_
->end());
11629 Expression
* val
= *p
;
11633 if (index_expr
!= NULL
)
11638 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
11641 error_at(index_expr
->location(),
11642 "index expression is not integer constant");
11643 return Expression::make_error(location
);
11645 if (mpz_sgn(ival
) < 0)
11648 error_at(index_expr
->location(), "index expression is negative");
11649 return Expression::make_error(location
);
11651 index
= mpz_get_ui(ival
);
11652 if (mpz_cmp_ui(ival
, index
) != 0)
11655 error_at(index_expr
->location(), "index value overflow");
11656 return Expression::make_error(location
);
11661 if (index
== vals
.size())
11662 vals
.push_back(val
);
11665 if (index
> vals
.size())
11667 vals
.reserve(index
+ 32);
11668 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
11670 if (vals
[index
] != NULL
)
11672 error_at((index_expr
!= NULL
11673 ? index_expr
->location()
11674 : val
->location()),
11675 "duplicate value for index %lu",
11677 return Expression::make_error(location
);
11685 size_t size
= vals
.size();
11686 Expression_list
* list
= new Expression_list
;
11687 list
->reserve(size
);
11688 for (size_t i
= 0; i
< size
; ++i
)
11689 list
->push_back(vals
[i
]);
11691 return this->make_array(type
, list
);
11694 // Actually build the array composite literal. This handles
11698 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
11700 source_location location
= this->location();
11701 Array_type
* at
= type
->array_type();
11702 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
11704 size_t size
= vals
== NULL
? 0 : vals
->size();
11706 mpz_init_set_ui(vlen
, size
);
11707 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
11709 at
= Type::make_array_type(at
->element_type(), elen
);
11712 if (at
->length() != NULL
)
11713 return new Fixed_array_construction_expression(type
, vals
, location
);
11715 return new Open_array_construction_expression(type
, vals
, location
);
11718 // Lower a map composite literal.
11721 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
11724 source_location location
= this->location();
11725 if (this->vals_
!= NULL
)
11727 if (!this->has_keys_
)
11729 error_at(location
, "map composite literal must have keys");
11730 return Expression::make_error(location
);
11733 for (Expression_list::iterator p
= this->vals_
->begin();
11734 p
!= this->vals_
->end();
11740 error_at((*p
)->location(),
11741 "map composite literal must have keys for every value");
11742 return Expression::make_error(location
);
11744 // Make sure we have lowered the key; it may not have been
11745 // lowered in order to handle keys for struct composite
11746 // literals. Lower it now to get the right error message.
11747 if ((*p
)->unknown_expression() != NULL
)
11749 (*p
)->unknown_expression()->clear_is_composite_literal_key();
11750 gogo
->lower_expression(function
, &*p
);
11751 gcc_assert((*p
)->is_error_expression());
11752 return Expression::make_error(location
);
11757 return new Map_construction_expression(type
, this->vals_
, location
);
11760 // Make a composite literal expression.
11763 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
11764 Expression_list
* vals
,
11765 source_location location
)
11767 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
11771 // Return whether this expression is a composite literal.
11774 Expression::is_composite_literal() const
11776 switch (this->classification_
)
11778 case EXPRESSION_COMPOSITE_LITERAL
:
11779 case EXPRESSION_STRUCT_CONSTRUCTION
:
11780 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11781 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11782 case EXPRESSION_MAP_CONSTRUCTION
:
11789 // Return whether this expression is a composite literal which is not
11793 Expression::is_nonconstant_composite_literal() const
11795 switch (this->classification_
)
11797 case EXPRESSION_STRUCT_CONSTRUCTION
:
11799 const Struct_construction_expression
*psce
=
11800 static_cast<const Struct_construction_expression
*>(this);
11801 return !psce
->is_constant_struct();
11803 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11805 const Fixed_array_construction_expression
*pace
=
11806 static_cast<const Fixed_array_construction_expression
*>(this);
11807 return !pace
->is_constant_array();
11809 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11811 const Open_array_construction_expression
*pace
=
11812 static_cast<const Open_array_construction_expression
*>(this);
11813 return !pace
->is_constant_array();
11815 case EXPRESSION_MAP_CONSTRUCTION
:
11822 // Return true if this is a reference to a local variable.
11825 Expression::is_local_variable() const
11827 const Var_expression
* ve
= this->var_expression();
11830 const Named_object
* no
= ve
->named_object();
11831 return (no
->is_result_variable()
11832 || (no
->is_variable() && !no
->var_value()->is_global()));
11835 // Class Type_guard_expression.
11840 Type_guard_expression::do_traverse(Traverse
* traverse
)
11842 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
11843 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11844 return TRAVERSE_EXIT
;
11845 return TRAVERSE_CONTINUE
;
11848 // Check types of a type guard expression. The expression must have
11849 // an interface type, but the actual type conversion is checked at run
11853 Type_guard_expression::do_check_types(Gogo
*)
11855 // 6g permits using a type guard with unsafe.pointer; we are
11857 Type
* expr_type
= this->expr_
->type();
11858 if (expr_type
->is_unsafe_pointer_type())
11860 if (this->type_
->points_to() == NULL
11861 && (this->type_
->integer_type() == NULL
11862 || (this->type_
->forwarded()
11863 != Type::lookup_integer_type("uintptr"))))
11864 this->report_error(_("invalid unsafe.Pointer conversion"));
11866 else if (this->type_
->is_unsafe_pointer_type())
11868 if (expr_type
->points_to() == NULL
11869 && (expr_type
->integer_type() == NULL
11870 || (expr_type
->forwarded()
11871 != Type::lookup_integer_type("uintptr"))))
11872 this->report_error(_("invalid unsafe.Pointer conversion"));
11874 else if (expr_type
->interface_type() == NULL
)
11876 if (!expr_type
->is_error_type() && !this->type_
->is_error_type())
11877 this->report_error(_("type assertion only valid for interface types"));
11878 this->set_is_error();
11880 else if (this->type_
->interface_type() == NULL
)
11882 std::string reason
;
11883 if (!expr_type
->interface_type()->implements_interface(this->type_
,
11886 if (!this->type_
->is_error_type())
11888 if (reason
.empty())
11889 this->report_error(_("impossible type assertion: "
11890 "type does not implement interface"));
11892 error_at(this->location(),
11893 ("impossible type assertion: "
11894 "type does not implement interface (%s)"),
11897 this->set_is_error();
11902 // Return a tree for a type guard expression.
11905 Type_guard_expression::do_get_tree(Translate_context
* context
)
11907 Gogo
* gogo
= context
->gogo();
11908 tree expr_tree
= this->expr_
->get_tree(context
);
11909 if (expr_tree
== error_mark_node
)
11910 return error_mark_node
;
11911 Type
* expr_type
= this->expr_
->type();
11912 if ((this->type_
->is_unsafe_pointer_type()
11913 && (expr_type
->points_to() != NULL
11914 || expr_type
->integer_type() != NULL
))
11915 || (expr_type
->is_unsafe_pointer_type()
11916 && this->type_
->points_to() != NULL
))
11917 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
11918 else if (expr_type
->is_unsafe_pointer_type()
11919 && this->type_
->integer_type() != NULL
)
11920 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
11921 else if (this->type_
->interface_type() != NULL
)
11922 return Expression::convert_interface_to_interface(context
, this->type_
,
11923 this->expr_
->type(),
11927 return Expression::convert_for_assignment(context
, this->type_
,
11928 this->expr_
->type(), expr_tree
,
11932 // Make a type guard expression.
11935 Expression::make_type_guard(Expression
* expr
, Type
* type
,
11936 source_location location
)
11938 return new Type_guard_expression(expr
, type
, location
);
11941 // Class Heap_composite_expression.
11943 // When you take the address of a composite literal, it is allocated
11944 // on the heap. This class implements that.
11946 class Heap_composite_expression
: public Expression
11949 Heap_composite_expression(Expression
* expr
, source_location location
)
11950 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
11956 do_traverse(Traverse
* traverse
)
11957 { return Expression::traverse(&this->expr_
, traverse
); }
11961 { return Type::make_pointer_type(this->expr_
->type()); }
11964 do_determine_type(const Type_context
*)
11965 { this->expr_
->determine_type_no_context(); }
11970 return Expression::make_heap_composite(this->expr_
->copy(),
11975 do_get_tree(Translate_context
*);
11977 // We only export global objects, and the parser does not generate
11978 // this in global scope.
11980 do_export(Export
*) const
11981 { gcc_unreachable(); }
11984 // The composite literal which is being put on the heap.
11988 // Return a tree which allocates a composite literal on the heap.
11991 Heap_composite_expression::do_get_tree(Translate_context
* context
)
11993 tree expr_tree
= this->expr_
->get_tree(context
);
11994 if (expr_tree
== error_mark_node
)
11995 return error_mark_node
;
11996 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
11997 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
11998 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
11999 expr_size
, this->location());
12000 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
12001 space
= save_expr(space
);
12002 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
12003 TREE_THIS_NOTRAP(ref
) = 1;
12004 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
12005 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
12007 SET_EXPR_LOCATION(ret
, this->location());
12011 // Allocate a composite literal on the heap.
12014 Expression::make_heap_composite(Expression
* expr
, source_location location
)
12016 return new Heap_composite_expression(expr
, location
);
12019 // Class Receive_expression.
12021 // Return the type of a receive expression.
12024 Receive_expression::do_type()
12026 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12027 if (channel_type
== NULL
)
12028 return Type::make_error_type();
12029 return channel_type
->element_type();
12032 // Check types for a receive expression.
12035 Receive_expression::do_check_types(Gogo
*)
12037 Type
* type
= this->channel_
->type();
12038 if (type
->is_error_type())
12040 this->set_is_error();
12043 if (type
->channel_type() == NULL
)
12045 this->report_error(_("expected channel"));
12048 if (!type
->channel_type()->may_receive())
12050 this->report_error(_("invalid receive on send-only channel"));
12055 // Get a tree for a receive expression.
12058 Receive_expression::do_get_tree(Translate_context
* context
)
12060 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12061 gcc_assert(channel_type
!= NULL
);
12062 Type
* element_type
= channel_type
->element_type();
12063 tree element_type_tree
= element_type
->get_tree(context
->gogo());
12065 tree channel
= this->channel_
->get_tree(context
);
12066 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
12067 return error_mark_node
;
12069 return Gogo::receive_from_channel(element_type_tree
, channel
,
12070 this->for_select_
, this->location());
12073 // Make a receive expression.
12075 Receive_expression
*
12076 Expression::make_receive(Expression
* channel
, source_location location
)
12078 return new Receive_expression(channel
, location
);
12081 // Class Send_expression.
12086 Send_expression::do_traverse(Traverse
* traverse
)
12088 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
12089 return TRAVERSE_EXIT
;
12090 return Expression::traverse(&this->val_
, traverse
);
12096 Send_expression::do_type()
12098 return Type::lookup_bool_type();
12104 Send_expression::do_determine_type(const Type_context
*)
12106 this->channel_
->determine_type_no_context();
12108 Type
* type
= this->channel_
->type();
12109 Type_context subcontext
;
12110 if (type
->channel_type() != NULL
)
12111 subcontext
.type
= type
->channel_type()->element_type();
12112 this->val_
->determine_type(&subcontext
);
12118 Send_expression::do_check_types(Gogo
*)
12120 Type
* type
= this->channel_
->type();
12121 if (type
->is_error_type())
12123 this->set_is_error();
12126 Channel_type
* channel_type
= type
->channel_type();
12127 if (channel_type
== NULL
)
12129 error_at(this->location(), "left operand of %<<-%> must be channel");
12130 this->set_is_error();
12133 Type
* element_type
= channel_type
->element_type();
12134 if (element_type
!= NULL
12135 && !Type::are_assignable(element_type
, this->val_
->type(), NULL
))
12137 this->report_error(_("incompatible types in send"));
12140 if (!channel_type
->may_send())
12142 this->report_error(_("invalid send on receive-only channel"));
12147 // Get a tree for a send expression.
12150 Send_expression::do_get_tree(Translate_context
* context
)
12152 tree channel
= this->channel_
->get_tree(context
);
12153 tree val
= this->val_
->get_tree(context
);
12154 if (channel
== error_mark_node
|| val
== error_mark_node
)
12155 return error_mark_node
;
12156 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12157 val
= Expression::convert_for_assignment(context
,
12158 channel_type
->element_type(),
12159 this->val_
->type(),
12162 return Gogo::send_on_channel(channel
, val
, this->is_value_discarded_
,
12163 this->for_select_
, this->location());
12166 // Make a send expression
12169 Expression::make_send(Expression
* channel
, Expression
* val
,
12170 source_location location
)
12172 return new Send_expression(channel
, val
, location
);
12175 // An expression which evaluates to a pointer to the type descriptor
12178 class Type_descriptor_expression
: public Expression
12181 Type_descriptor_expression(Type
* type
, source_location location
)
12182 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
12189 { return Type::make_type_descriptor_ptr_type(); }
12192 do_determine_type(const Type_context
*)
12200 do_get_tree(Translate_context
* context
)
12201 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
12204 // The type for which this is the descriptor.
12208 // Make a type descriptor expression.
12211 Expression::make_type_descriptor(Type
* type
, source_location location
)
12213 return new Type_descriptor_expression(type
, location
);
12216 // An expression which evaluates to some characteristic of a type.
12217 // This is only used to initialize fields of a type descriptor. Using
12218 // a new expression class is slightly inefficient but gives us a good
12219 // separation between the frontend and the middle-end with regard to
12220 // how types are laid out.
12222 class Type_info_expression
: public Expression
12225 Type_info_expression(Type
* type
, Type_info type_info
)
12226 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
12227 type_(type
), type_info_(type_info
)
12235 do_determine_type(const Type_context
*)
12243 do_get_tree(Translate_context
* context
);
12246 // The type for which we are getting information.
12248 // What information we want.
12249 Type_info type_info_
;
12252 // The type is chosen to match what the type descriptor struct
12256 Type_info_expression::do_type()
12258 switch (this->type_info_
)
12260 case TYPE_INFO_SIZE
:
12261 return Type::lookup_integer_type("uintptr");
12262 case TYPE_INFO_ALIGNMENT
:
12263 case TYPE_INFO_FIELD_ALIGNMENT
:
12264 return Type::lookup_integer_type("uint8");
12270 // Return type information in GENERIC.
12273 Type_info_expression::do_get_tree(Translate_context
* context
)
12275 tree type_tree
= this->type_
->get_tree(context
->gogo());
12276 if (type_tree
== error_mark_node
)
12277 return error_mark_node
;
12279 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12280 gcc_assert(val_type_tree
!= error_mark_node
);
12282 if (this->type_info_
== TYPE_INFO_SIZE
)
12283 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12284 TYPE_SIZE_UNIT(type_tree
));
12288 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12289 val
= go_type_alignment(type_tree
);
12291 val
= go_field_alignment(type_tree
);
12292 return build_int_cstu(val_type_tree
, val
);
12296 // Make a type info expression.
12299 Expression::make_type_info(Type
* type
, Type_info type_info
)
12301 return new Type_info_expression(type
, type_info
);
12304 // An expression which evaluates to the offset of a field within a
12305 // struct. This, like Type_info_expression, q.v., is only used to
12306 // initialize fields of a type descriptor.
12308 class Struct_field_offset_expression
: public Expression
12311 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12312 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12313 type_(type
), field_(field
)
12319 { return Type::lookup_integer_type("uintptr"); }
12322 do_determine_type(const Type_context
*)
12330 do_get_tree(Translate_context
* context
);
12333 // The type of the struct.
12334 Struct_type
* type_
;
12336 const Struct_field
* field_
;
12339 // Return a struct field offset in GENERIC.
12342 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12344 tree type_tree
= this->type_
->get_tree(context
->gogo());
12345 if (type_tree
== error_mark_node
)
12346 return error_mark_node
;
12348 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12349 gcc_assert(val_type_tree
!= error_mark_node
);
12351 const Struct_field_list
* fields
= this->type_
->fields();
12352 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12353 Struct_field_list::const_iterator p
;
12354 for (p
= fields
->begin();
12355 p
!= fields
->end();
12356 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12358 gcc_assert(struct_field_tree
!= NULL_TREE
);
12359 if (&*p
== this->field_
)
12362 gcc_assert(&*p
== this->field_
);
12364 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12365 byte_position(struct_field_tree
));
12368 // Make an expression for a struct field offset.
12371 Expression::make_struct_field_offset(Struct_type
* type
,
12372 const Struct_field
* field
)
12374 return new Struct_field_offset_expression(type
, field
);
12377 // An expression which evaluates to the address of an unnamed label.
12379 class Label_addr_expression
: public Expression
12382 Label_addr_expression(Label
* label
, source_location location
)
12383 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12390 { return Type::make_pointer_type(Type::make_void_type()); }
12393 do_determine_type(const Type_context
*)
12398 { return new Label_addr_expression(this->label_
, this->location()); }
12401 do_get_tree(Translate_context
*)
12402 { return this->label_
->get_addr(this->location()); }
12405 // The label whose address we are taking.
12409 // Make an expression for the address of an unnamed label.
12412 Expression::make_label_addr(Label
* label
, source_location location
)
12414 return new Label_addr_expression(label
, location
);
12417 // Import an expression. This comes at the end in order to see the
12418 // various class definitions.
12421 Expression::import_expression(Import
* imp
)
12423 int c
= imp
->peek_char();
12424 if (imp
->match_c_string("- ")
12425 || imp
->match_c_string("! ")
12426 || imp
->match_c_string("^ "))
12427 return Unary_expression::do_import(imp
);
12429 return Binary_expression::do_import(imp
);
12430 else if (imp
->match_c_string("true")
12431 || imp
->match_c_string("false"))
12432 return Boolean_expression::do_import(imp
);
12434 return String_expression::do_import(imp
);
12435 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12437 // This handles integers, floats and complex constants.
12438 return Integer_expression::do_import(imp
);
12440 else if (imp
->match_c_string("nil"))
12441 return Nil_expression::do_import(imp
);
12442 else if (imp
->match_c_string("convert"))
12443 return Type_conversion_expression::do_import(imp
);
12446 error_at(imp
->location(), "import error: expected expression");
12447 return Expression::make_error(imp
->location());
12451 // Class Expression_list.
12453 // Traverse the list.
12456 Expression_list::traverse(Traverse
* traverse
)
12458 for (Expression_list::iterator p
= this->begin();
12464 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12465 return TRAVERSE_EXIT
;
12468 return TRAVERSE_CONTINUE
;
12474 Expression_list::copy()
12476 Expression_list
* ret
= new Expression_list();
12477 for (Expression_list::iterator p
= this->begin();
12482 ret
->push_back(NULL
);
12484 ret
->push_back((*p
)->copy());
12489 // Return whether an expression list has an error expression.
12492 Expression_list::contains_error() const
12494 for (Expression_list::const_iterator p
= this->begin();
12497 if (*p
!= NULL
&& (*p
)->is_error_expression())